File size: 3,239 Bytes
4e90e81 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
{
"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
}
|