{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":59093,"databundleVersionId":7469972,"sourceType":"competition"}],"dockerImageVersionId":30635,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import pandas as pd\nimport os\nfrom tqdm import tqdm","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-01-13T15:43:06.632089Z","iopub.execute_input":"2024-01-13T15:43:06.632470Z","iopub.status.idle":"2024-01-13T15:43:07.113560Z","shell.execute_reply.started":"2024-01-13T15:43:06.632437Z","shell.execute_reply":"2024-01-13T15:43:07.112302Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.mkdir('separate_spectogram')","metadata":{"execution":{"iopub.status.busy":"2024-01-13T15:43:07.115879Z","iopub.execute_input":"2024-01-13T15:43:07.116532Z","iopub.status.idle":"2024-01-13T15:43:07.122733Z","shell.execute_reply.started":"2024-01-13T15:43:07.116486Z","shell.execute_reply":"2024-01-13T15:43:07.121284Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('/kaggle/input/hms-harmful-brain-activity-classification/train.csv')","metadata":{"execution":{"iopub.status.busy":"2024-01-13T15:43:07.124831Z","iopub.execute_input":"2024-01-13T15:43:07.125301Z","iopub.status.idle":"2024-01-13T15:43:07.424400Z","shell.execute_reply.started":"2024-01-13T15:43:07.125259Z","shell.execute_reply":"2024-01-13T15:43:07.423450Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"k = 0\nfor eeg in tqdm(train['eeg_id'].unique()):\n    df = train[train['eeg_id'] == eeg][['spectrogram_label_offset_seconds', 'expert_consensus', 'spectrogram_id']]\n    specto_id = df['spectrogram_id'].values[0]\n    specto = pd.read_parquet(f'/kaggle/input/hms-harmful-brain-activity-classification/train_spectrograms/{specto_id}.parquet')\n    # Initialize variables for tracking class and times\n    current_class = None\n    start_time = None\n    end_time = None\n\n    for row in df.itertuples():\n        if row.expert_consensus != current_class:\n            if current_class is not None:\n                start_time = int(start_time)\n                if end_time:\n                    end_time = int(end_time)\n                else:\n                    end_time = specto.shape[0]\n                k += 1\n                specto.loc[start_time:end_time,:].to_parquet(f\"separate_spectogram/{eeg}-{specto_id}_{current_class}.parquet\")\n            \n            current_class = row.expert_consensus\n            start_time = row.spectrogram_label_offset_seconds\n        else:\n            # Update end time for the current class\n            end_time = row.spectrogram_label_offset_seconds\n\n    # Print the times for the last class\n    start_time = int(start_time)\n    if end_time:\n        end_time = int(end_time)\n    else:\n        end_time = specto.shape[0]\n    k += 1\n    specto.loc[start_time:end_time,:].to_parquet(f\"separate_spectogram/{eeg}-{specto_id}_{current_class}.parquet\")\n","metadata":{"execution":{"iopub.status.busy":"2024-01-13T16:04:36.128844Z","iopub.execute_input":"2024-01-13T16:04:36.129304Z","iopub.status.idle":"2024-01-13T16:04:36.787913Z","shell.execute_reply.started":"2024-01-13T16:04:36.129266Z","shell.execute_reply":"2024-01-13T16:04:36.786769Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\n\nlen(os.listdir('/kaggle/working/separate_spectogram/'))","metadata":{"execution":{"iopub.status.busy":"2024-01-13T16:05:17.537570Z","iopub.execute_input":"2024-01-13T16:05:17.538024Z","iopub.status.idle":"2024-01-13T16:05:17.546653Z","shell.execute_reply.started":"2024-01-13T16:05:17.537973Z","shell.execute_reply":"2024-01-13T16:05:17.545446Z"},"trusted":true},"execution_count":null,"outputs":[]}]}