{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.11.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":59156,"databundleVersionId":13874099,"sourceType":"competition"}],"dockerImageVersionId":31089,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import os\nimport pandas as pd\nimport torch\nfrom torch.utils.data import Dataset, DataLoader\nfrom torchvision.io import read_video\nfrom google.colab import files","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"folder_path = 'videos'\nif not os.path.exists(folder_path):\n    os.makedirs(folder_path)\nprint(f\"تم إنشاء المجلد: {folder_path}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-27T22:26:20.423952Z","iopub.status.idle":"2025-09-27T22:26:20.424694Z","shell.execute_reply.started":"2025-09-27T22:26:20.424479Z","shell.execute_reply":"2025-09-27T22:26:20.424501Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"uploaded = files.upload() ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-27T22:26:20.419403Z","iopub.status.idle":"2025-09-27T22:26:20.419798Z","shell.execute_reply.started":"2025-09-27T22:26:20.419631Z","shell.execute_reply":"2025-09-27T22:26:20.419649Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for filename in uploaded.keys():\n    if filename.endswith('.mp4') or filename.endswith('.avi'):  # لو ملف فيديو\n        os.rename(filename, f'videos/{filename}')\nprint(\"تم نقل كل الفيديوهات لمجلد videos\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-27T22:26:20.420932Z","iopub.status.idle":"2025-09-27T22:26:20.421332Z","shell.execute_reply.started":"2025-09-27T22:26:20.421138Z","shell.execute_reply":"2025-09-27T22:26:20.421156Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_csv = 'train.csv'\ntest_csv = 'test.csv'\n\n# التأكد من وجود الملفات\nprint(\"ملفات موجودة في Colab root:\", os.listdir('.'))\nprint(\"ملفات الفيديو في مجلد videos:\", os.listdir('videos'))","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df = pd.read_csv(train_csv)\ntest_df = pd.read_csv(test_csv)\n\nprint(\"عدد فيديوهات التدريب:\", len(train_df))\nprint(\"عدد فيديوهات التقييم:\", len(test_df))\nprint(\"أول 5 صفوف من التدريب:\")\nprint(train_df.head())","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\nimport pandas as pd\nimport torch\nfrom torch.utils.data import Dataset, DataLoader\nfrom torchvision.io import read_video\n\nclass MiceDataset(Dataset):\n    def __init__(self, data_dir, dataframe, transform=None):\n        super().__init__()  # Correct way to call the parent constructor\n        self.data_dir = data_dir\n        self.transform = transform\n        self.labels_df = dataframe  # Use the provided dataframe\n\n    def __len__(self):\n        return len(self.labels_df)\n\n    def __getitem__(self, idx):\n        video_name = self.labels_df.iloc[idx, 0]\n        # Ensure the label column exists and get the label\n        label_col_name = 'forward_returns'  # Assuming 'forward_returns' is the correct label column\n        if label_col_name not in self.labels_df.columns:\n            raise ValueError(f\"Label column '{label_col_name}' not found in the dataframe.\")\n        label = self.labels_df.loc[idx, label_col_name]\n\n        video_path = os.path.join(self.data_dir, str(video_name)) # Convert video_name to string\n\n        try:\n            # read_video returns (video_tensor, audio_tensor, info_dict)\n            video, _, _ = read_video(video_path, pts_unit='sec')  # قراءة الفيديو\n            video = video.float() / 255.0  # normalize\n\n            if self.transform:\n                video = self.transform(video)\n\n            # permute to (channels, frames, height, width) for 3D CNN\n            # read_video returns (frames, height, width, channels)\n            return video.permute(3, 0, 1, 2), label\n        except Exception as e:\n            print(f\"Error loading video {video_path}: {e}\")\n            # Return dummy data or skip\n            # For now, let's raise the error to highlight the issue\n            raise","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}