{"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":"gpu","dataSources":[{"sourceId":59093,"databundleVersionId":7469972,"sourceType":"competition"},{"sourceId":13427798,"sourceType":"datasetVersion","datasetId":8522703}],"dockerImageVersionId":31154,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import os\nimport pandas as pd\nimport numpy as np","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-10-22T16:24:43.215362Z","iopub.execute_input":"2025-10-22T16:24:43.216084Z","iopub.status.idle":"2025-10-22T16:24:43.806258Z","shell.execute_reply.started":"2025-10-22T16:24:43.216055Z","shell.execute_reply":"2025-10-22T16:24:43.805508Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Eldeki tüm veriyi ayrıca import edelim\n\nBASE_PATH = \"/kaggle/input/hms-harmful-brain-activity-classification\"\neeg_path = BASE_PATH+\"/\"+\"train_eegs\"\nimport pandas as pd\npd.read_parquet(eeg_path+\"/\"+os.listdir(eeg_path)[0])\n\ncsv = pd.read_csv(BASE_PATH+\"/train.csv\")\nunique_eeg_ids_df = csv.drop_duplicates(subset='eeg_id')\nunique_eeg_ids_df = unique_eeg_ids_df[['eeg_id', 'expert_consensus']]\nunique_values = unique_eeg_ids_df['expert_consensus'].unique()\nprint(unique_values)\nlabels = {0:\"Seizure\",1:\"GPD\",2:\"LRDA\",3:\"LPD\",4:\"GRDA\",5:\"Other\"}\n# Invert the labels dictionary to map string labels to their numeric values\nlabel_map = {v: k for k, v in labels.items()}\n\n# Replace the string values in the expert_consensus column with their numeric values\nunique_eeg_ids_df['expert_consensus'] = unique_eeg_ids_df['expert_consensus'].map(label_map)\nunique_eeg_ids_df","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-22T16:24:43.807059Z","iopub.execute_input":"2025-10-22T16:24:43.807425Z","iopub.status.idle":"2025-10-22T16:24:44.441791Z","shell.execute_reply.started":"2025-10-22T16:24:43.807399Z","shell.execute_reply":"2025-10-22T16:24:44.441058Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\nimport shutil\nfrom tqdm import tqdm\n\n# Dictionary eşlemesi\nlabel_map = {\n    0: \"Seizure\",\n    1: \"GPD\",\n    2: \"LRDA\",\n    3: \"LPD\",\n    4: \"GRDA\",\n    5: \"Other\"\n}\n\n# Kaynak klasör\nsource_dir = \"/kaggle/input/spectrograms-dataset/spectrograms\"\n# Hedef klasör\ntarget_base = \"2D\"\n\n# Eğer hedef klasör yoksa oluştur\nos.makedirs(target_base, exist_ok=True)\n\n# Her satır için döngü\nfor _, row in tqdm(unique_eeg_ids_df.iterrows(), total=len(unique_eeg_ids_df)):\n    eeg_id = row[\"eeg_id\"]\n    label_num = int(row[\"expert_consensus\"])\n    label_name = label_map.get(label_num, \"Unknown\")\n    \n    src_path = os.path.join(source_dir, f\"{eeg_id}.png\")\n    dest_dir = os.path.join(target_base, label_name)\n    dest_path = os.path.join(dest_dir, f\"{eeg_id}.png\")\n    \n    # Hedef klasör yoksa oluştur\n    os.makedirs(dest_dir, exist_ok=True)\n    \n    # Dosya varsa taşı\n    if os.path.exists(src_path):\n        shutil.copy(src_path, dest_path)\n    else:\n        print(f\"Uyarı: {src_path} bulunamadı.\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-22T16:26:40.385854Z","iopub.execute_input":"2025-10-22T16:26:40.386434Z","iopub.status.idle":"2025-10-22T16:28:34.399318Z","shell.execute_reply.started":"2025-10-22T16:26:40.386408Z","shell.execute_reply":"2025-10-22T16:28:34.398582Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\nfrom PIL import Image\nfrom tqdm import tqdm\n\n# Giriş (2D) ve çıkış (3D) klasörleri\ninput_base = \"2D\"\noutput_base = \"3D\"\n\n# Sınıflar\nclasses = [\"Seizure\", \"GPD\", \"LRDA\", \"LPD\", \"GRDA\", \"Other\"]\n\n# Çıkış klasörleri oluştur\nfor cls in classes:\n    os.makedirs(os.path.join(output_base, cls), exist_ok=True)\n\nfor cls in classes:\n    class_dir = os.path.join(input_base, cls)\n    if not os.path.exists(class_dir):\n        continue\n\n    # Her görseli işle\n    for file in tqdm(os.listdir(class_dir), desc=f\"Processing {cls}\"):\n        if not file.endswith(\".png\"):\n            continue\n\n        eeg_id = os.path.splitext(file)[0]\n        img_path = os.path.join(class_dir, file)\n\n        try:\n            img = Image.open(img_path)\n            width, height = img.size  # 1920x1440 bekleniyor\n\n            # Her parça 960x720 olacak\n            w2, h2 = width // 2, height // 2\n\n            # Dört parçayı kırp\n            crops = {\n                \"LT\": img.crop((0, 0, w2, h2)),             # Sol üst\n                \"RT\": img.crop((w2, 0, width, h2)),         # Sağ üst\n                \"LB\": img.crop((0, h2, w2, height)),        # Sol alt\n                \"RB\": img.crop((w2, h2, width, height))     # Sağ alt\n            }\n\n            # Her EEG için klasör oluştur\n            eeg_dir = os.path.join(output_base, cls, eeg_id)\n            os.makedirs(eeg_dir, exist_ok=True)\n\n            # Parçaları kaydet\n            for pos, crop in crops.items():\n                out_path = os.path.join(eeg_dir, f\"{eeg_id}_{pos}.png\")\n                crop.save(out_path)\n\n        except Exception as e:\n            print(f\"Hata: {file} işlenemedi -> {e}\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-22T16:31:04.897429Z","iopub.execute_input":"2025-10-22T16:31:04.897718Z","iopub.status.idle":"2025-10-22T16:32:11.483323Z","shell.execute_reply.started":"2025-10-22T16:31:04.897697Z","shell.execute_reply":"2025-10-22T16:32:11.482279Z"}},"outputs":[],"execution_count":null}]}