Skip to content

Commit

Permalink
Merge pull request #20 from yardenmezi/update-notebook
Browse files Browse the repository at this point in the history
Notebook - adding graph saving options
  • Loading branch information
yardenmezi authored Dec 31, 2024
2 parents 4e8b123 + eb47aae commit ed2d4ac
Showing 1 changed file with 70 additions and 76 deletions.
146 changes: 70 additions & 76 deletions docs/notebooks/0_complete_pipeline.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"source": [
"import os\n",
"import sys\n",
"import sleepeegpy"
"import sleepeegpy\n"
]
},
{
Expand All @@ -61,21 +61,25 @@
"from IPython.display import display\n",
"\n",
"use_example_set = widgets.RadioButtons(\n",
" options=[True, False], description=\"Use example set?\", value=True\n",
" options=[True, False],\n",
" description='Use example set?',\n",
" value=True\n",
")\n",
"bad_channels_selection = widgets.RadioButtons(\n",
" options=[\"automatically\", \"manually\", \"from-file\"],\n",
" description=\"Clean Bad Channels:\",\n",
" value=\"from-file\",\n",
" options=['automatically', 'manually', \"from-file\"],\n",
" description='Clean Bad Channels:',\n",
" value='from-file'\n",
")\n",
"annotations_selection = widgets.RadioButtons(\n",
" options=[\"automatically\", \"manually\", \"from-file\"],\n",
" description=\"Clean bad epochs Annotations:\",\n",
" value=\"from-file\",\n",
" description='Clean bad epochs Annotations:',\n",
" value=\"from-file\"\n",
")\n",
"\n",
"hypno_selection = widgets.RadioButtons(\n",
" options=[\"automatically\", \"from-file\"], description=\"Hypnogram:\", value=\"from-file\"\n",
" options=[\"automatically\", \"from-file\"],\n",
" description='Hypnogram:',\n",
" value=\"from-file\"\n",
")"
]
},
Expand All @@ -87,13 +91,8 @@
"source": [
"import pooch\n",
"from sleepeegpy.pipeline import (\n",
" CleaningPipe,\n",
" ICAPipe,\n",
" SpectralPipe,\n",
" SpindlesPipe,\n",
" SlowWavesPipe,\n",
" RapidEyeMovementsPipe,\n",
")\n",
" CleaningPipe, ICAPipe, SpectralPipe, \n",
" SpindlesPipe, SlowWavesPipe, RapidEyeMovementsPipe)\n",
"from sleepeegpy.dashboard import create_dashboard"
]
},
Expand All @@ -120,8 +119,8 @@
"source": [
"from os import makedirs\n",
"\n",
"output_dir = \"output_folder\" # Output path and name can be changed here\n",
"input_dir = \"input_files\" # input files dir can be changed here\n",
"output_dir = \"output_folder\" # Output path and name can be changed here\n",
"input_dir = \"input_files\" # input files dir can be changed here\n",
"makedirs(input_dir, exist_ok=True)\n",
"makedirs(output_dir, exist_ok=True)"
]
Expand Down Expand Up @@ -225,7 +224,7 @@
"source": [
"#### If you use your own files (not the example set), write the relevant filenames below. ###\n",
"\n",
"subject_code = \"DK8\" # The subject code that will appear in the dashboard\n",
"subject_code = \"DK8\" # The subject code that will appear in the dashboard\n",
"eeg_file = \"resampled_raw.fif\"\n",
"bad_channels = \"bad_channels.txt\"\n",
"annotations = \"annotations.txt\""
Expand All @@ -248,7 +247,7 @@
"outputs": [],
"source": [
"hypnogram_filename = \"staging.txt\"\n",
"hypno_freq = 1 # If required, change the Hypnogram's sampling frequency (Visbrain's hypnograms default to 1)"
"hypno_freq = 1 # If required, change the Hypnogram's sampling frequency (Visbrain's hypnograms default to 1)"
]
},
{
Expand All @@ -268,15 +267,15 @@
"metadata": {},
"outputs": [],
"source": [
"if hypno_selection.value == \"automatically\":\n",
"if hypno_selection.value==\"automatically\":\n",
" hypnogram_path = \"predict\"\n",
" #### If you selected automatic hypnogram, select prediction arguments. ####\n",
"#### If you selected automatic hypnogram, select prediction arguments. ####\n",
" hypno_predict_arguments = {\n",
" \"eeg_name\": \"E183\", # (C4)\n",
" \"eog_name\": \"E252\", # (EOG1)\n",
" \"emg_name\": \"E247\",\n",
" \"ref_name\": \"E26\",\n",
" \"save\": False,\n",
" \"eeg_name\": \"E183\", #(C4)\n",
" \"eog_name\": \"E252\", #(EOG1)\n",
" \"emg_name\": \"E247\",\n",
" \"ref_name\" : \"E26\",\n",
" \"save\": False\n",
" }\n",
"else:\n",
" hypno_predict_arguments = None"
Expand All @@ -301,9 +300,9 @@
"outputs": [],
"source": [
"n_components = 30\n",
"picked_channel = \"E101\" # (Pz)\n",
"loc_chname = \"E252\" # (EOG1)\n",
"roc_chname = \"E226\" # (EOG2)"
"picked_channel = 'E101' # (Pz)\n",
"loc_chname = \"E252\" # (EOG1)\n",
"roc_chname = \"E226\" # (EOG2)"
]
},
{
Expand Down Expand Up @@ -336,26 +335,18 @@
"outputs": [],
"source": [
"if use_example_set.value:\n",
" ## Nap dataset files: ##\n",
"## Nap dataset files: ##\n",
" bad_channels = odie.fetch(\"nap_bad_channels.txt\")\n",
" annotations = odie.fetch(\"nap_annotations.txt\")\n",
" path_to_eeg = odie.fetch(\"nap_resampled_raw.fif\", progressbar=True)\n",
" if hypno_selection.value == \"from-file\":\n",
" hypnogram_path = odie.fetch(\"nap_staging.txt\", progressbar=True)\n",
"else:\n",
" path_to_eeg = os.path.join(input_dir, eeg_file)\n",
" hypnogram_path = odie.fetch(\"nap_staging.txt\", progressbar=True) \n",
"else: \n",
" path_to_eeg = os.path.join(input_dir,eeg_file)\n",
" if hypno_selection.value == \"from-file\":\n",
" hypnogram_path = os.path.join(input_dir, hypnogram_filename)\n",
" bad_channels = (\n",
" None\n",
" if bad_channels_selection == \"automatically\"\n",
" else os.path.join(input_dir, bad_channels)\n",
" )\n",
" annotations = (\n",
" None\n",
" if annotations_selection == \"automatically\"\n",
" else os.path.join(input_dir, annotations)\n",
" )"
" hypnogram_path = os.path.join(input_dir,hypnogram_filename)\n",
" bad_channels = None if bad_channels_selection == \"automatically\" else os.path.join(input_dir,bad_channels) \n",
" annotations = None if annotations_selection == \"automatically\" else os.path.join(input_dir,annotations) "
]
},
{
Expand Down Expand Up @@ -476,9 +467,7 @@
"metadata": {},
"outputs": [],
"source": [
"pipe.read_bad_channels(\n",
" path=None if bad_channels_selection.value == \"Manually\" else bad_channels\n",
")"
"pipe.read_bad_channels(path = None if bad_channels_selection.value == \"Manually\" else bad_channels)"
]
},
{
Expand Down Expand Up @@ -514,12 +503,15 @@
"outputs": [],
"source": [
"if annotations_selection.value == \"manually\":\n",
" pipe.plot(butterfly=True, save_annotations=True, overwrite=True)\n",
" pipe.plot(butterfly=True, save_annotations=True,overwrite=True) \n",
" pipe.read_annotations()\n",
"elif annotations_selection.value == \"from-file\":\n",
" pipe.read_annotations(path=annotations)\n",
" pipe.read_annotations(path = annotations)\n",
"elif annotations_selection.value == \"automatically\":\n",
" pipe.auto_set_annotations()"
" pipe.auto_set_annotations()\n",
" pipe.save_annotations()\n",
"\n",
"pipe.save_raw(subject_code + \"_cleaned_raw.fif\")"
]
},
{
Expand Down Expand Up @@ -548,14 +540,13 @@
],
"source": [
"fig = create_dashboard(\n",
" subject_code=subject_code,\n",
" prec_pipe=pipe,\n",
" subject_code=subject_code, \n",
" prec_pipe=pipe, \n",
" hypno_psd_pick=picked_channel,\n",
" hypnogram=hypnogram_path,\n",
" predict_hypno_args=hypno_predict_arguments,\n",
" hypnogram= hypnogram_path,\n",
" predict_hypno_args = hypno_predict_arguments,\n",
" hypno_freq=hypno_freq,\n",
" reference=\"average\",\n",
")"
" reference=\"average\")"
]
},
{
Expand Down Expand Up @@ -657,7 +648,8 @@
}
],
"source": [
"ica_pipe.apply()"
"ica_pipe.apply()\n",
"ica_pipe.save_raw(subject_code + \"_ica_raw.fif\")"
]
},
{
Expand Down Expand Up @@ -690,11 +682,10 @@
}
],
"source": [
"spectral_pipe = SpectralPipe(\n",
" prec_pipe=ica_pipe,\n",
" path_to_hypno=hypnogram_path,\n",
" hypno_freq=hypno_freq,\n",
")"
"if hypnogram_path and hypnogram_path!=\"predict\":\n",
" spectral_pipe = SpectralPipe(prec_pipe=ica_pipe,path_to_hypno=hypnogram_path,hypno_freq=hypno_freq)\n",
"else:\n",
" spectral_pipe = SpectralPipe(prec_pipe=ica_pipe)"
]
},
{
Expand All @@ -711,14 +702,13 @@
"metadata": {},
"outputs": [],
"source": [
"if hypnogram_path is None:\n",
"if hypnogram_path is None or hypnogram_path==\"predict\":\n",
" spectral_pipe.predict_hypno(\n",
" eeg_name=hypno_predict_arguments[\"eeg_name\"],\n",
" eog_name=hypno_predict_arguments[\"eog_name\"],\n",
" emg_name=hypno_predict_arguments[\"emg_name\"],\n",
" ref_name=hypno_predict_arguments[\"ref_name\"],\n",
" save=False,\n",
" )"
" eeg_name = hypno_predict_arguments[\"eeg_name\"],\n",
" eog_name = hypno_predict_arguments[\"eog_name\"],\n",
" emg_name = hypno_predict_arguments[\"emg_name\"],\n",
" ref_name = hypno_predict_arguments[\"ref_name\"],\n",
" save=True)\n"
]
},
{
Expand Down Expand Up @@ -746,7 +736,7 @@
}
],
"source": [
"spectral_pipe.plot_hypnospectrogram(picks=[picked_channel])"
"spectral_pipe.plot_hypnospectrogram(picks=[picked_channel],save=True)"
]
},
{
Expand Down Expand Up @@ -775,8 +765,9 @@
" n_overlap=512,\n",
" window=\"hamming\",\n",
" verbose=False,\n",
" save=True\n",
")\n",
"spectral_pipe.plot_psds(picks=[picked_channel], psd_range=(-30, 30))"
"spectral_pipe.plot_psds(picks=[picked_channel], psd_range=(-30, 30),save=True)"
]
},
{
Expand Down Expand Up @@ -804,7 +795,7 @@
}
],
"source": [
"spectral_pipe.plot_topomap_collage()"
"spectral_pipe.plot_topomap_collage(save=True)"
]
},
{
Expand Down Expand Up @@ -848,6 +839,7 @@
" hue=\"Stage\",\n",
" time_before=1,\n",
" time_after=1,\n",
" save=True\n",
")"
]
},
Expand Down Expand Up @@ -1005,7 +997,7 @@
}
],
"source": [
"spindles_pipe.compute_tfr(freqs=(10, 20), n_freqs=100, time_before=1, time_after=1)\n",
"spindles_pipe.compute_tfr(freqs=(10, 20), n_freqs=100, time_before=1, time_after=1,save=True)\n",
"spindles_pipe.tfrs[\"N2\"].plot([picked_channel])"
]
},
Expand Down Expand Up @@ -1060,6 +1052,7 @@
" hue=\"Stage\",\n",
" time_before=0.4,\n",
" time_after=0.8,\n",
" save=True\n",
")"
]
},
Expand Down Expand Up @@ -1100,9 +1093,9 @@
],
"source": [
"slow_waves_pipe.compute_tfr(\n",
" freqs=(0.5, 5), n_freqs=100, time_before=4, time_after=4, n_cycles=2\n",
" freqs=(0.5, 5), n_freqs=100, time_before=4, time_after=4, n_cycles=2,save=True\n",
")\n",
"ploted_channel = \"VREF\" # Cz\n",
"ploted_channel = \"VREF\" # Cz \n",
"slow_waves_pipe.tfrs[\"N3\"].plot([ploted_channel])"
]
},
Expand Down Expand Up @@ -1162,6 +1155,7 @@
" time_after=0.5,\n",
" filt=(None, None),\n",
" mask=None,\n",
" save=True\n",
")"
]
}
Expand All @@ -1182,7 +1176,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.8"
"version": "3.11.10"
},
"vscode": {
"interpreter": {
Expand Down

0 comments on commit ed2d4ac

Please sign in to comment.