{ "cells": [ { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "import torch\n", "import numpy as np\n", "import os" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "df = pd.DataFrame()\n", "\n", "for file in os.listdir(\"./dataset/train\"):\n", " df = pd.concat([df, pd.read_parquet(f\"./dataset/train/{file}\")])\n", " \n", "grouped_df = df.groupby('subject_id', group_keys=False)" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Processing subject_id: 1\n", "Finished processing subject_id: 1\n", "Processing subject_id: 2\n", "Finished processing subject_id: 2\n", "Processing subject_id: 3\n", "Finished processing subject_id: 3\n", "Processing subject_id: 4\n", "Finished processing subject_id: 4\n", "Processing subject_id: 5\n", "Finished processing subject_id: 5\n", "Processing subject_id: 6\n", "Finished processing subject_id: 6\n", "Processing subject_id: 7\n", "Finished processing subject_id: 7\n", "Processing subject_id: 8\n", "Finished processing subject_id: 8\n" ] } ], "source": [ "for subject_id, group in grouped_df:\n", " print(\"Processing subject_id: \", subject_id)\n", " arr = np.array([np.array([y for y in x]) for x in group[\"EEG\"].values])\n", " mean_each_channel = arr.mean(axis=2)\n", " std_each_channel = arr.std(axis=2)\n", " # Reshape mean and std to match arr dimensions for broadcasting\n", " mean_each_channel = mean_each_channel[:, :, np.newaxis]\n", " std_each_channel = std_each_channel[:, :, np.newaxis]\n", " normalized_arr = (arr - mean_each_channel) / std_each_channel\n", " \n", " normalized_df = pd.DataFrame()\n", " normalized_df[\"EEG\"] = normalized_arr.tolist()\n", " normalized_df[\"subject_id\"] = subject_id\n", " normalized_df[\"coco_id\"] = group[\"coco_id\"].values\n", " normalized_df[\"curr_time\"] = group[\"curr_time\"].values\n", " normalized_df[\"session\"] = group[\"session\"].values\n", " normalized_df[\"trial\"] = group[\"trial\"].values\n", " normalized_df[\"block\"] = group[\"block\"].values\n", " normalized_df[\"73k_id\"] = group[\"73k_id\"].values\n", " \n", " normalized_df.to_parquet(f\"./dataset/normalized_subject/subject_{subject_id}.parquet\")\n", " \n", " print(\"Finished processing subject_id: \", subject_id)\n" ] } ], "metadata": { "kernelspec": { "display_name": ".venv", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.12.8" } }, "nbformat": 4, "nbformat_minor": 2 }