From a922b1c80b3f4a744b534983b4ad28593617c2d3 Mon Sep 17 00:00:00 2001 From: "Metaverse Crowdsource (MVCS)" Date: Wed, 9 Aug 2023 19:36:23 -0300 Subject: [PATCH] messing around with freesurfer --- .../Bifurcation Analysis-checkpoint.ipynb | 202 ++++++++++++++++-- Bifurcation Analysis.ipynb | 202 ++++++++++++++++-- 2 files changed, 380 insertions(+), 24 deletions(-) diff --git a/.ipynb_checkpoints/Bifurcation Analysis-checkpoint.ipynb b/.ipynb_checkpoints/Bifurcation Analysis-checkpoint.ipynb index b277c7d..05b7ee0 100644 --- a/.ipynb_checkpoints/Bifurcation Analysis-checkpoint.ipynb +++ b/.ipynb_checkpoints/Bifurcation Analysis-checkpoint.ipynb @@ -254,30 +254,133 @@ }, { "cell_type": "code", - "execution_count": 7, - "id": "0ff259ce-7442-4863-b8a4-ebc3f7055ef9", + "execution_count": 10, + "id": "ec8518b6-3142-4542-9eb0-033aaf495798", + "metadata": {}, + "outputs": [], + "source": [ + "import os\n", + "os.environ[\"FREESURFER_HOME\"] = \"/usr/local/freesurfer/7.4.1\" # Replace with your actual FreeSurfer installation path\n" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "309ec021-1263-42db-9950-bc3a3f6d1ecb", + "metadata": {}, + "outputs": [], + "source": [ + "os.environ[\"PATH\"] += os.pathsep + os.path.join(os.environ[\"FREESURFER_HOME\"], \"bin\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "id": "c071095d-d23c-4888-acc6-3b336850d1b7", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "/usr/local/freesurfer/7.4.1/bin/mri_watershed\n", + "\n" + ] + } + ], + "source": [ + "import subprocess\n", + "\n", + "result = subprocess.run(['which', 'mri_watershed'], stdout=subprocess.PIPE)\n", + "print(result.stdout.decode('utf-8'))\n" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "id": "abfc45eb-8fe2-43d3-aa86-11286cc1eecc", + "metadata": {}, + "outputs": [], + "source": [ + "os.environ[\"PATH\"] += os.pathsep + \"/usr/local/freesurfer/7.4.1/bin/\"\n" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "id": "98230e50-8d41-4a5e-b758-c85ea48fb5bc", "metadata": {}, "outputs": [ { - "ename": "RuntimeError", - "evalue": "The FREESURFER_HOME environment variable is not set.", + "name": "stdout", + "output_type": "stream", + "text": [ + "False\n" + ] + } + ], + "source": [ + "import os\n", + "file_path = \"/home/vincent/mne_data/MNE-sample-data/subjects/fsaverage/mri/transforms/talairach_with_skull.lta\"\n", + "print(os.path.exists(file_path))\n" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "id": "0ff259ce-7442-4863-b8a4-ebc3f7055ef9", + "metadata": { + "collapsed": true, + "jupyter": { + "outputs_hidden": true + }, + "scrolled": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Using GCA atlas: /usr/local/freesurfer/7.4.1/average/RB_all_withskull_2020_01_02.gca\n", + "\n", + "Running mri_watershed for BEM segmentation with the following parameters:\n", + "\n", + "Results dir = /home/vincent/mne_data/MNE-sample-data/subjects/fsaverage/bem/watershed\n", + "Command = mri_watershed -T1 -atlas -brain_atlas /usr/local/freesurfer/7.4.1/average/RB_all_withskull_2020_01_02.gca /home/vincent/mne_data/MNE-sample-data/subjects/fsaverage/mri/transforms/talairach_with_skull.lta -useSRAS -surf /home/vincent/mne_data/MNE-sample-data/subjects/fsaverage/bem/watershed/fsaverage /home/vincent/mne_data/MNE-sample-data/subjects/fsaverage/mri/T1.mgz /home/vincent/mne_data/MNE-sample-data/subjects/fsaverage/bem/watershed/ws.mgz\n", + "\n", + "Running subprocess: mri_watershed -T1 -atlas -brain_atlas /usr/local/freesurfer/7.4.1/average/RB_all_withskull_2020_01_02.gca /home/vincent/mne_data/MNE-sample-data/subjects/fsaverage/mri/transforms/talairach_with_skull.lta -useSRAS -surf /home/vincent/mne_data/MNE-sample-data/subjects/fsaverage/bem/watershed/fsaverage /home/vincent/mne_data/MNE-sample-data/subjects/fsaverage/mri/T1.mgz /home/vincent/mne_data/MNE-sample-data/subjects/fsaverage/bem/watershed/ws.mgz\n", + "\n", + "Mode: T1 normalized volume\n", + "Mode: Atlas analysis\n", + "Mode: Use the information of atlas (default parms, --help for details)\n", + "\n", + "mri_watershed Error: Cannot read the transform\n", + "\n", + "error: No such file or directory\n", + "error: ltaReadFile(/home/vincent/mne_data/MNE-sample-data/subjects/fsaverage/mri/transforms/talairach_with_skull.lta): can't open file\u0000\n" + ] + }, + { + "ename": "CalledProcessError", + "evalue": "Command '['mri_watershed', '-T1', '-atlas', '-brain_atlas', '/usr/local/freesurfer/7.4.1/average/RB_all_withskull_2020_01_02.gca', '/home/vincent/mne_data/MNE-sample-data/subjects/fsaverage/mri/transforms/talairach_with_skull.lta', '-useSRAS', '-surf', '/home/vincent/mne_data/MNE-sample-data/subjects/fsaverage/bem/watershed/fsaverage', '/home/vincent/mne_data/MNE-sample-data/subjects/fsaverage/mri/T1.mgz', '/home/vincent/mne_data/MNE-sample-data/subjects/fsaverage/bem/watershed/ws.mgz']' returned non-zero exit status 1.", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[0;31mRuntimeError\u001b[0m Traceback (most recent call last)", - "Cell \u001b[0;32mIn[7], line 8\u001b[0m\n\u001b[1;32m 5\u001b[0m subjects_dir \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mstr\u001b[39m(mne\u001b[38;5;241m.\u001b[39mdatasets\u001b[38;5;241m.\u001b[39msample\u001b[38;5;241m.\u001b[39mdata_path()) \u001b[38;5;241m+\u001b[39m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124m/subjects\u001b[39m\u001b[38;5;124m'\u001b[39m\n\u001b[1;32m 7\u001b[0m \u001b[38;5;66;03m# Create the BEM surfaces\u001b[39;00m\n\u001b[0;32m----> 8\u001b[0m \u001b[43mmne\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mbem\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mmake_watershed_bem\u001b[49m\u001b[43m(\u001b[49m\u001b[43msubject\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mfsaverage\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43msubjects_dir\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43msubjects_dir\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43moverwrite\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43;01mTrue\u001b[39;49;00m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mvolume\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mT1\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43matlas\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43;01mTrue\u001b[39;49;00m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mgcaatlas\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43;01mTrue\u001b[39;49;00m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mpreflood\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43;01mNone\u001b[39;49;00m\u001b[43m)\u001b[49m\n\u001b[1;32m 10\u001b[0m \u001b[38;5;66;03m# Set up source space, BEM, and forward solution\u001b[39;00m\n\u001b[1;32m 11\u001b[0m src \u001b[38;5;241m=\u001b[39m mne\u001b[38;5;241m.\u001b[39msetup_source_space(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mfsaverage\u001b[39m\u001b[38;5;124m\"\u001b[39m, spacing\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mico4\u001b[39m\u001b[38;5;124m\"\u001b[39m, subjects_dir\u001b[38;5;241m=\u001b[39msubjects_dir)\n", + "\u001b[0;31mCalledProcessError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[17], line 9\u001b[0m\n\u001b[1;32m 6\u001b[0m subjects_dir \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mstr\u001b[39m(mne\u001b[38;5;241m.\u001b[39mdatasets\u001b[38;5;241m.\u001b[39msample\u001b[38;5;241m.\u001b[39mdata_path()) \u001b[38;5;241m+\u001b[39m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124m/subjects\u001b[39m\u001b[38;5;124m'\u001b[39m\n\u001b[1;32m 8\u001b[0m \u001b[38;5;66;03m# Create the BEM surfaces\u001b[39;00m\n\u001b[0;32m----> 9\u001b[0m \u001b[43mmne\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mbem\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mmake_watershed_bem\u001b[49m\u001b[43m(\u001b[49m\u001b[43msubject\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mfsaverage\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43msubjects_dir\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43msubjects_dir\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43moverwrite\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43;01mTrue\u001b[39;49;00m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mvolume\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mT1\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43matlas\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43;01mTrue\u001b[39;49;00m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mgcaatlas\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43;01mTrue\u001b[39;49;00m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mpreflood\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43;01mNone\u001b[39;49;00m\u001b[43m)\u001b[49m\n\u001b[1;32m 11\u001b[0m \u001b[38;5;66;03m# Set up source space, BEM, and forward solution\u001b[39;00m\n\u001b[1;32m 12\u001b[0m src \u001b[38;5;241m=\u001b[39m mne\u001b[38;5;241m.\u001b[39msetup_source_space(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mfsaverage\u001b[39m\u001b[38;5;124m\"\u001b[39m, spacing\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mico4\u001b[39m\u001b[38;5;124m\"\u001b[39m, subjects_dir\u001b[38;5;241m=\u001b[39msubjects_dir)\n", "File \u001b[0;32m:12\u001b[0m, in \u001b[0;36mmake_watershed_bem\u001b[0;34m(subject, subjects_dir, overwrite, volume, atlas, gcaatlas, preflood, show, copy, T1, brainmask, verbose)\u001b[0m\n", - "File \u001b[0;32m~/miniconda3/lib/python3.10/site-packages/mne/bem.py:1261\u001b[0m, in \u001b[0;36mmake_watershed_bem\u001b[0;34m(subject, subjects_dir, overwrite, volume, atlas, gcaatlas, preflood, show, copy, T1, brainmask, verbose)\u001b[0m\n\u001b[1;32m 1206\u001b[0m \u001b[38;5;250m\u001b[39m\u001b[38;5;124;03m\"\"\"Create BEM surfaces using the FreeSurfer watershed algorithm.\u001b[39;00m\n\u001b[1;32m 1207\u001b[0m \n\u001b[1;32m 1208\u001b[0m \u001b[38;5;124;03mParameters\u001b[39;00m\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 1257\u001b[0m \u001b[38;5;124;03m.. versionadded:: 0.10\u001b[39;00m\n\u001b[1;32m 1258\u001b[0m \u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[1;32m 1259\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mviz\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mmisc\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m plot_bem\n\u001b[0;32m-> 1261\u001b[0m env, mri_dir, bem_dir \u001b[38;5;241m=\u001b[39m \u001b[43m_prepare_env\u001b[49m\u001b[43m(\u001b[49m\u001b[43msubject\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43msubjects_dir\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 1262\u001b[0m tempdir \u001b[38;5;241m=\u001b[39m _TempDir() \u001b[38;5;66;03m# fsl and Freesurfer create some random junk in CWD\u001b[39;00m\n\u001b[1;32m 1263\u001b[0m run_subprocess_env \u001b[38;5;241m=\u001b[39m partial(run_subprocess, env\u001b[38;5;241m=\u001b[39menv, cwd\u001b[38;5;241m=\u001b[39mtempdir)\n", - "File \u001b[0;32m~/miniconda3/lib/python3.10/site-packages/mne/bem.py:1917\u001b[0m, in \u001b[0;36m_prepare_env\u001b[0;34m(subject, subjects_dir)\u001b[0m\n\u001b[1;32m 1914\u001b[0m \u001b[38;5;250m\u001b[39m\u001b[38;5;124;03m\"\"\"Prepare an env object for subprocess calls.\"\"\"\u001b[39;00m\n\u001b[1;32m 1915\u001b[0m env \u001b[38;5;241m=\u001b[39m os\u001b[38;5;241m.\u001b[39menviron\u001b[38;5;241m.\u001b[39mcopy()\n\u001b[0;32m-> 1917\u001b[0m fs_home \u001b[38;5;241m=\u001b[39m \u001b[43m_check_freesurfer_home\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 1919\u001b[0m _validate_type(subject, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mstr\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 1921\u001b[0m subjects_dir \u001b[38;5;241m=\u001b[39m get_subjects_dir(subjects_dir, raise_error\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mTrue\u001b[39;00m)\n", - "File \u001b[0;32m~/miniconda3/lib/python3.10/site-packages/mne/utils/check.py:1117\u001b[0m, in \u001b[0;36m_check_freesurfer_home\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1115\u001b[0m fs_home \u001b[38;5;241m=\u001b[39m get_config(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mFREESURFER_HOME\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 1116\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m fs_home \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[0;32m-> 1117\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mRuntimeError\u001b[39;00m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mThe FREESURFER_HOME environment variable is not set.\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 1118\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m fs_home\n", - "\u001b[0;31mRuntimeError\u001b[0m: The FREESURFER_HOME environment variable is not set." + "File \u001b[0;32m~/miniconda3/lib/python3.10/site-packages/mne/bem.py:1329\u001b[0m, in \u001b[0;36mmake_watershed_bem\u001b[0;34m(subject, subjects_dir, overwrite, volume, atlas, gcaatlas, preflood, show, copy, T1, brainmask, verbose)\u001b[0m\n\u001b[1;32m 1323\u001b[0m logger\u001b[38;5;241m.\u001b[39minfo(\n\u001b[1;32m 1324\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;124mRunning mri_watershed for BEM segmentation with the \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 1325\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mfollowing parameters:\u001b[39m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;124mResults dir = \u001b[39m\u001b[38;5;132;01m%s\u001b[39;00m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;124mCommand = \u001b[39m\u001b[38;5;132;01m%s\u001b[39;00m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 1326\u001b[0m \u001b[38;5;241m%\u001b[39m (ws_dir, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m \u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;241m.\u001b[39mjoin(cmd))\n\u001b[1;32m 1327\u001b[0m )\n\u001b[1;32m 1328\u001b[0m os\u001b[38;5;241m.\u001b[39mmakedirs(op\u001b[38;5;241m.\u001b[39mjoin(ws_dir))\n\u001b[0;32m-> 1329\u001b[0m \u001b[43mrun_subprocess_env\u001b[49m\u001b[43m(\u001b[49m\u001b[43mcmd\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 1330\u001b[0m \u001b[38;5;28;01mdel\u001b[39;00m tempdir \u001b[38;5;66;03m# clean up directory\u001b[39;00m\n\u001b[1;32m 1331\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m op\u001b[38;5;241m.\u001b[39misfile(T1_mgz):\n", + "File \u001b[0;32m:12\u001b[0m, in \u001b[0;36mrun_subprocess\u001b[0;34m(command, return_code, verbose, *args, **kwargs)\u001b[0m\n", + "File \u001b[0;32m~/miniconda3/lib/python3.10/site-packages/mne/utils/misc.py:206\u001b[0m, in \u001b[0;36mrun_subprocess\u001b[0;34m(command, return_code, verbose, *args, **kwargs)\u001b[0m\n\u001b[1;32m 204\u001b[0m stdout \u001b[38;5;241m=\u001b[39m all_out \u001b[38;5;28;01mif\u001b[39;00m control_stdout \u001b[38;5;28;01melse\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m\n\u001b[1;32m 205\u001b[0m stderr \u001b[38;5;241m=\u001b[39m all_err \u001b[38;5;28;01mif\u001b[39;00m control_stderr \u001b[38;5;28;01melse\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m\n\u001b[0;32m--> 206\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m subprocess\u001b[38;5;241m.\u001b[39mCalledProcessError(\n\u001b[1;32m 207\u001b[0m p\u001b[38;5;241m.\u001b[39mreturncode, command, output\u001b[38;5;241m=\u001b[39mstdout, stderr\u001b[38;5;241m=\u001b[39mstderr\n\u001b[1;32m 208\u001b[0m )\n\u001b[1;32m 210\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m output\n", + "\u001b[0;31mCalledProcessError\u001b[0m: Command '['mri_watershed', '-T1', '-atlas', '-brain_atlas', '/usr/local/freesurfer/7.4.1/average/RB_all_withskull_2020_01_02.gca', '/home/vincent/mne_data/MNE-sample-data/subjects/fsaverage/mri/transforms/talairach_with_skull.lta', '-useSRAS', '-surf', '/home/vincent/mne_data/MNE-sample-data/subjects/fsaverage/bem/watershed/fsaverage', '/home/vincent/mne_data/MNE-sample-data/subjects/fsaverage/mri/T1.mgz', '/home/vincent/mne_data/MNE-sample-data/subjects/fsaverage/bem/watershed/ws.mgz']' returned non-zero exit status 1." ] } ], "source": [ "import mne\n", "import numpy as np\n", - "\n", + "import mne\n", + "from mne.datasets import sample\n", "# Set up subjects_dir before calling the make_watershed_bem function\n", "subjects_dir = str(mne.datasets.sample.data_path()) + '/subjects'\n", "\n", @@ -350,7 +453,11 @@ "cell_type": "code", "execution_count": 5, "id": "a5613c58-245b-47bf-ad2b-02c35586af26", - "metadata": {}, + "metadata": { + "jupyter": { + "source_hidden": true + } + }, "outputs": [ { "ename": "NameError", @@ -386,6 +493,77 @@ "plt.show()" ] }, + { + "cell_type": "code", + "execution_count": 19, + "id": "7c905fc6-2f4e-49f3-91e1-9d4da9f580d2", + "metadata": {}, + "outputs": [ + { + "ename": "ValueError", + "evalue": "too many values to unpack (expected 2)", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[19], line 11\u001b[0m\n\u001b[1;32m 8\u001b[0m EEG_data \u001b[38;5;241m=\u001b[39m np\u001b[38;5;241m.\u001b[39mload(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m/home/vincent/AAA_projects/MVCS/Neuroscience/eeg_data_with_channels.npy\u001b[39m\u001b[38;5;124m'\u001b[39m, allow_pickle\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mTrue\u001b[39;00m)\n\u001b[1;32m 10\u001b[0m \u001b[38;5;66;03m# Assuming EEG_data[0] contains EEG data and EEG_data[1] contains channel names\u001b[39;00m\n\u001b[0;32m---> 11\u001b[0m eeg_data, ch_names \u001b[38;5;241m=\u001b[39m EEG_data\n\u001b[1;32m 13\u001b[0m \u001b[38;5;66;03m# Create MNE Info object\u001b[39;00m\n\u001b[1;32m 14\u001b[0m info \u001b[38;5;241m=\u001b[39m mne\u001b[38;5;241m.\u001b[39mcreate_info(ch_names\u001b[38;5;241m=\u001b[39mch_names, sfreq\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m1000.0\u001b[39m, ch_types\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124meeg\u001b[39m\u001b[38;5;124m'\u001b[39m)\n", + "\u001b[0;31mValueError\u001b[0m: too many values to unpack (expected 2)" + ] + } + ], + "source": [ + "import mne\n", + "import numpy as np\n", + "from mne.datasets import sample\n", + "from mne.forward import make_forward_solution\n", + "from mne.minimum_norm import make_inverse_operator, apply_inverse\n", + "\n", + "# Load EEG data from numpy file\n", + "EEG_data = np.load('/home/vincent/AAA_projects/MVCS/Neuroscience/eeg_data_with_channels.npy', allow_pickle=True)\n", + "\n", + "# Assuming EEG_data[0] contains EEG data and EEG_data[1] contains channel names\n", + "eeg_data, ch_names = EEG_data\n", + "\n", + "# Create MNE Info object\n", + "info = mne.create_info(ch_names=ch_names, sfreq=1000.0, ch_types='eeg')\n", + "\n", + "# Create MNE Raw object\n", + "raw = mne.io.RawArray(eeg_data, info)\n", + "\n", + "# Use fsaverage source space\n", + "src = mne.setup_source_space('fsaverage', spacing='ico5', subjects_dir=sample.data_path() + '/subjects')\n", + "\n", + "# Create the BEM model for fsaverage\n", + "model = mne.make_bem_model(subject='fsaverage', ico=4, subjects_dir=sample.data_path() + '/subjects')\n", + "bem = mne.make_bem_solution(model)\n", + "\n", + "# Compute the forward solution\n", + "fwd = make_forward_solution(raw.info, trans='fsaverage-trans.fif', src=src, bem=bem, meg=False, eeg=True, mindist=5.0, n_jobs=1)\n", + "\n", + "# Compute noise covariance from empty room data or use regularization techniques\n", + "cov = mne.compute_covariance(raw, tmax=0., method='shrunk')\n", + "\n", + "# Compute the inverse operator\n", + "inverse_operator = make_inverse_operator(raw.info, fwd, cov, loose=0.2, depth=0.8)\n", + "\n", + "# Estimate sources\n", + "method = \"dSPM\" # or 'MNE', 'sLORETA'\n", + "snr = 3.0\n", + "lambda2 = 1.0 / snr ** 2\n", + "stc = apply_inverse(raw, inverse_operator, lambda2, method=method, pick_ori=None)\n", + "\n", + "# Visualization\n", + "brain = stc.plot(subject='fsaverage', subjects_dir=sample.data_path() + '/subjects', initial_time=0.1, hemi='split')\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "812a91b5-8293-47e6-b2c6-35b8a4e070aa", + "metadata": {}, + "outputs": [], + "source": [] + }, { "cell_type": "code", "execution_count": null, diff --git a/Bifurcation Analysis.ipynb b/Bifurcation Analysis.ipynb index b277c7d..05b7ee0 100644 --- a/Bifurcation Analysis.ipynb +++ b/Bifurcation Analysis.ipynb @@ -254,30 +254,133 @@ }, { "cell_type": "code", - "execution_count": 7, - "id": "0ff259ce-7442-4863-b8a4-ebc3f7055ef9", + "execution_count": 10, + "id": "ec8518b6-3142-4542-9eb0-033aaf495798", + "metadata": {}, + "outputs": [], + "source": [ + "import os\n", + "os.environ[\"FREESURFER_HOME\"] = \"/usr/local/freesurfer/7.4.1\" # Replace with your actual FreeSurfer installation path\n" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "309ec021-1263-42db-9950-bc3a3f6d1ecb", + "metadata": {}, + "outputs": [], + "source": [ + "os.environ[\"PATH\"] += os.pathsep + os.path.join(os.environ[\"FREESURFER_HOME\"], \"bin\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "id": "c071095d-d23c-4888-acc6-3b336850d1b7", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "/usr/local/freesurfer/7.4.1/bin/mri_watershed\n", + "\n" + ] + } + ], + "source": [ + "import subprocess\n", + "\n", + "result = subprocess.run(['which', 'mri_watershed'], stdout=subprocess.PIPE)\n", + "print(result.stdout.decode('utf-8'))\n" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "id": "abfc45eb-8fe2-43d3-aa86-11286cc1eecc", + "metadata": {}, + "outputs": [], + "source": [ + "os.environ[\"PATH\"] += os.pathsep + \"/usr/local/freesurfer/7.4.1/bin/\"\n" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "id": "98230e50-8d41-4a5e-b758-c85ea48fb5bc", "metadata": {}, "outputs": [ { - "ename": "RuntimeError", - "evalue": "The FREESURFER_HOME environment variable is not set.", + "name": "stdout", + "output_type": "stream", + "text": [ + "False\n" + ] + } + ], + "source": [ + "import os\n", + "file_path = \"/home/vincent/mne_data/MNE-sample-data/subjects/fsaverage/mri/transforms/talairach_with_skull.lta\"\n", + "print(os.path.exists(file_path))\n" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "id": "0ff259ce-7442-4863-b8a4-ebc3f7055ef9", + "metadata": { + "collapsed": true, + "jupyter": { + "outputs_hidden": true + }, + "scrolled": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Using GCA atlas: /usr/local/freesurfer/7.4.1/average/RB_all_withskull_2020_01_02.gca\n", + "\n", + "Running mri_watershed for BEM segmentation with the following parameters:\n", + "\n", + "Results dir = /home/vincent/mne_data/MNE-sample-data/subjects/fsaverage/bem/watershed\n", + "Command = mri_watershed -T1 -atlas -brain_atlas /usr/local/freesurfer/7.4.1/average/RB_all_withskull_2020_01_02.gca /home/vincent/mne_data/MNE-sample-data/subjects/fsaverage/mri/transforms/talairach_with_skull.lta -useSRAS -surf /home/vincent/mne_data/MNE-sample-data/subjects/fsaverage/bem/watershed/fsaverage /home/vincent/mne_data/MNE-sample-data/subjects/fsaverage/mri/T1.mgz /home/vincent/mne_data/MNE-sample-data/subjects/fsaverage/bem/watershed/ws.mgz\n", + "\n", + "Running subprocess: mri_watershed -T1 -atlas -brain_atlas /usr/local/freesurfer/7.4.1/average/RB_all_withskull_2020_01_02.gca /home/vincent/mne_data/MNE-sample-data/subjects/fsaverage/mri/transforms/talairach_with_skull.lta -useSRAS -surf /home/vincent/mne_data/MNE-sample-data/subjects/fsaverage/bem/watershed/fsaverage /home/vincent/mne_data/MNE-sample-data/subjects/fsaverage/mri/T1.mgz /home/vincent/mne_data/MNE-sample-data/subjects/fsaverage/bem/watershed/ws.mgz\n", + "\n", + "Mode: T1 normalized volume\n", + "Mode: Atlas analysis\n", + "Mode: Use the information of atlas (default parms, --help for details)\n", + "\n", + "mri_watershed Error: Cannot read the transform\n", + "\n", + "error: No such file or directory\n", + "error: ltaReadFile(/home/vincent/mne_data/MNE-sample-data/subjects/fsaverage/mri/transforms/talairach_with_skull.lta): can't open file\u0000\n" + ] + }, + { + "ename": "CalledProcessError", + "evalue": "Command '['mri_watershed', '-T1', '-atlas', '-brain_atlas', '/usr/local/freesurfer/7.4.1/average/RB_all_withskull_2020_01_02.gca', '/home/vincent/mne_data/MNE-sample-data/subjects/fsaverage/mri/transforms/talairach_with_skull.lta', '-useSRAS', '-surf', '/home/vincent/mne_data/MNE-sample-data/subjects/fsaverage/bem/watershed/fsaverage', '/home/vincent/mne_data/MNE-sample-data/subjects/fsaverage/mri/T1.mgz', '/home/vincent/mne_data/MNE-sample-data/subjects/fsaverage/bem/watershed/ws.mgz']' returned non-zero exit status 1.", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[0;31mRuntimeError\u001b[0m Traceback (most recent call last)", - "Cell \u001b[0;32mIn[7], line 8\u001b[0m\n\u001b[1;32m 5\u001b[0m subjects_dir \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mstr\u001b[39m(mne\u001b[38;5;241m.\u001b[39mdatasets\u001b[38;5;241m.\u001b[39msample\u001b[38;5;241m.\u001b[39mdata_path()) \u001b[38;5;241m+\u001b[39m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124m/subjects\u001b[39m\u001b[38;5;124m'\u001b[39m\n\u001b[1;32m 7\u001b[0m \u001b[38;5;66;03m# Create the BEM surfaces\u001b[39;00m\n\u001b[0;32m----> 8\u001b[0m \u001b[43mmne\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mbem\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mmake_watershed_bem\u001b[49m\u001b[43m(\u001b[49m\u001b[43msubject\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mfsaverage\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43msubjects_dir\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43msubjects_dir\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43moverwrite\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43;01mTrue\u001b[39;49;00m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mvolume\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mT1\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43matlas\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43;01mTrue\u001b[39;49;00m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mgcaatlas\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43;01mTrue\u001b[39;49;00m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mpreflood\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43;01mNone\u001b[39;49;00m\u001b[43m)\u001b[49m\n\u001b[1;32m 10\u001b[0m \u001b[38;5;66;03m# Set up source space, BEM, and forward solution\u001b[39;00m\n\u001b[1;32m 11\u001b[0m src \u001b[38;5;241m=\u001b[39m mne\u001b[38;5;241m.\u001b[39msetup_source_space(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mfsaverage\u001b[39m\u001b[38;5;124m\"\u001b[39m, spacing\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mico4\u001b[39m\u001b[38;5;124m\"\u001b[39m, subjects_dir\u001b[38;5;241m=\u001b[39msubjects_dir)\n", + "\u001b[0;31mCalledProcessError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[17], line 9\u001b[0m\n\u001b[1;32m 6\u001b[0m subjects_dir \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mstr\u001b[39m(mne\u001b[38;5;241m.\u001b[39mdatasets\u001b[38;5;241m.\u001b[39msample\u001b[38;5;241m.\u001b[39mdata_path()) \u001b[38;5;241m+\u001b[39m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124m/subjects\u001b[39m\u001b[38;5;124m'\u001b[39m\n\u001b[1;32m 8\u001b[0m \u001b[38;5;66;03m# Create the BEM surfaces\u001b[39;00m\n\u001b[0;32m----> 9\u001b[0m \u001b[43mmne\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mbem\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mmake_watershed_bem\u001b[49m\u001b[43m(\u001b[49m\u001b[43msubject\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mfsaverage\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43msubjects_dir\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43msubjects_dir\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43moverwrite\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43;01mTrue\u001b[39;49;00m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mvolume\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mT1\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43matlas\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43;01mTrue\u001b[39;49;00m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mgcaatlas\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43;01mTrue\u001b[39;49;00m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mpreflood\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43;01mNone\u001b[39;49;00m\u001b[43m)\u001b[49m\n\u001b[1;32m 11\u001b[0m \u001b[38;5;66;03m# Set up source space, BEM, and forward solution\u001b[39;00m\n\u001b[1;32m 12\u001b[0m src \u001b[38;5;241m=\u001b[39m mne\u001b[38;5;241m.\u001b[39msetup_source_space(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mfsaverage\u001b[39m\u001b[38;5;124m\"\u001b[39m, spacing\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mico4\u001b[39m\u001b[38;5;124m\"\u001b[39m, subjects_dir\u001b[38;5;241m=\u001b[39msubjects_dir)\n", "File \u001b[0;32m:12\u001b[0m, in \u001b[0;36mmake_watershed_bem\u001b[0;34m(subject, subjects_dir, overwrite, volume, atlas, gcaatlas, preflood, show, copy, T1, brainmask, verbose)\u001b[0m\n", - "File \u001b[0;32m~/miniconda3/lib/python3.10/site-packages/mne/bem.py:1261\u001b[0m, in \u001b[0;36mmake_watershed_bem\u001b[0;34m(subject, subjects_dir, overwrite, volume, atlas, gcaatlas, preflood, show, copy, T1, brainmask, verbose)\u001b[0m\n\u001b[1;32m 1206\u001b[0m \u001b[38;5;250m\u001b[39m\u001b[38;5;124;03m\"\"\"Create BEM surfaces using the FreeSurfer watershed algorithm.\u001b[39;00m\n\u001b[1;32m 1207\u001b[0m \n\u001b[1;32m 1208\u001b[0m \u001b[38;5;124;03mParameters\u001b[39;00m\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 1257\u001b[0m \u001b[38;5;124;03m.. versionadded:: 0.10\u001b[39;00m\n\u001b[1;32m 1258\u001b[0m \u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[1;32m 1259\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mviz\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mmisc\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m plot_bem\n\u001b[0;32m-> 1261\u001b[0m env, mri_dir, bem_dir \u001b[38;5;241m=\u001b[39m \u001b[43m_prepare_env\u001b[49m\u001b[43m(\u001b[49m\u001b[43msubject\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43msubjects_dir\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 1262\u001b[0m tempdir \u001b[38;5;241m=\u001b[39m _TempDir() \u001b[38;5;66;03m# fsl and Freesurfer create some random junk in CWD\u001b[39;00m\n\u001b[1;32m 1263\u001b[0m run_subprocess_env \u001b[38;5;241m=\u001b[39m partial(run_subprocess, env\u001b[38;5;241m=\u001b[39menv, cwd\u001b[38;5;241m=\u001b[39mtempdir)\n", - "File \u001b[0;32m~/miniconda3/lib/python3.10/site-packages/mne/bem.py:1917\u001b[0m, in \u001b[0;36m_prepare_env\u001b[0;34m(subject, subjects_dir)\u001b[0m\n\u001b[1;32m 1914\u001b[0m \u001b[38;5;250m\u001b[39m\u001b[38;5;124;03m\"\"\"Prepare an env object for subprocess calls.\"\"\"\u001b[39;00m\n\u001b[1;32m 1915\u001b[0m env \u001b[38;5;241m=\u001b[39m os\u001b[38;5;241m.\u001b[39menviron\u001b[38;5;241m.\u001b[39mcopy()\n\u001b[0;32m-> 1917\u001b[0m fs_home \u001b[38;5;241m=\u001b[39m \u001b[43m_check_freesurfer_home\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 1919\u001b[0m _validate_type(subject, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mstr\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 1921\u001b[0m subjects_dir \u001b[38;5;241m=\u001b[39m get_subjects_dir(subjects_dir, raise_error\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mTrue\u001b[39;00m)\n", - "File \u001b[0;32m~/miniconda3/lib/python3.10/site-packages/mne/utils/check.py:1117\u001b[0m, in \u001b[0;36m_check_freesurfer_home\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1115\u001b[0m fs_home \u001b[38;5;241m=\u001b[39m get_config(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mFREESURFER_HOME\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 1116\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m fs_home \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[0;32m-> 1117\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mRuntimeError\u001b[39;00m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mThe FREESURFER_HOME environment variable is not set.\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 1118\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m fs_home\n", - "\u001b[0;31mRuntimeError\u001b[0m: The FREESURFER_HOME environment variable is not set." + "File \u001b[0;32m~/miniconda3/lib/python3.10/site-packages/mne/bem.py:1329\u001b[0m, in \u001b[0;36mmake_watershed_bem\u001b[0;34m(subject, subjects_dir, overwrite, volume, atlas, gcaatlas, preflood, show, copy, T1, brainmask, verbose)\u001b[0m\n\u001b[1;32m 1323\u001b[0m logger\u001b[38;5;241m.\u001b[39minfo(\n\u001b[1;32m 1324\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;124mRunning mri_watershed for BEM segmentation with the \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 1325\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mfollowing parameters:\u001b[39m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;124mResults dir = \u001b[39m\u001b[38;5;132;01m%s\u001b[39;00m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;124mCommand = \u001b[39m\u001b[38;5;132;01m%s\u001b[39;00m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 1326\u001b[0m \u001b[38;5;241m%\u001b[39m (ws_dir, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m \u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;241m.\u001b[39mjoin(cmd))\n\u001b[1;32m 1327\u001b[0m )\n\u001b[1;32m 1328\u001b[0m os\u001b[38;5;241m.\u001b[39mmakedirs(op\u001b[38;5;241m.\u001b[39mjoin(ws_dir))\n\u001b[0;32m-> 1329\u001b[0m \u001b[43mrun_subprocess_env\u001b[49m\u001b[43m(\u001b[49m\u001b[43mcmd\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 1330\u001b[0m \u001b[38;5;28;01mdel\u001b[39;00m tempdir \u001b[38;5;66;03m# clean up directory\u001b[39;00m\n\u001b[1;32m 1331\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m op\u001b[38;5;241m.\u001b[39misfile(T1_mgz):\n", + "File \u001b[0;32m:12\u001b[0m, in \u001b[0;36mrun_subprocess\u001b[0;34m(command, return_code, verbose, *args, **kwargs)\u001b[0m\n", + "File \u001b[0;32m~/miniconda3/lib/python3.10/site-packages/mne/utils/misc.py:206\u001b[0m, in \u001b[0;36mrun_subprocess\u001b[0;34m(command, return_code, verbose, *args, **kwargs)\u001b[0m\n\u001b[1;32m 204\u001b[0m stdout \u001b[38;5;241m=\u001b[39m all_out \u001b[38;5;28;01mif\u001b[39;00m control_stdout \u001b[38;5;28;01melse\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m\n\u001b[1;32m 205\u001b[0m stderr \u001b[38;5;241m=\u001b[39m all_err \u001b[38;5;28;01mif\u001b[39;00m control_stderr \u001b[38;5;28;01melse\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m\n\u001b[0;32m--> 206\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m subprocess\u001b[38;5;241m.\u001b[39mCalledProcessError(\n\u001b[1;32m 207\u001b[0m p\u001b[38;5;241m.\u001b[39mreturncode, command, output\u001b[38;5;241m=\u001b[39mstdout, stderr\u001b[38;5;241m=\u001b[39mstderr\n\u001b[1;32m 208\u001b[0m )\n\u001b[1;32m 210\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m output\n", + "\u001b[0;31mCalledProcessError\u001b[0m: Command '['mri_watershed', '-T1', '-atlas', '-brain_atlas', '/usr/local/freesurfer/7.4.1/average/RB_all_withskull_2020_01_02.gca', '/home/vincent/mne_data/MNE-sample-data/subjects/fsaverage/mri/transforms/talairach_with_skull.lta', '-useSRAS', '-surf', '/home/vincent/mne_data/MNE-sample-data/subjects/fsaverage/bem/watershed/fsaverage', '/home/vincent/mne_data/MNE-sample-data/subjects/fsaverage/mri/T1.mgz', '/home/vincent/mne_data/MNE-sample-data/subjects/fsaverage/bem/watershed/ws.mgz']' returned non-zero exit status 1." ] } ], "source": [ "import mne\n", "import numpy as np\n", - "\n", + "import mne\n", + "from mne.datasets import sample\n", "# Set up subjects_dir before calling the make_watershed_bem function\n", "subjects_dir = str(mne.datasets.sample.data_path()) + '/subjects'\n", "\n", @@ -350,7 +453,11 @@ "cell_type": "code", "execution_count": 5, "id": "a5613c58-245b-47bf-ad2b-02c35586af26", - "metadata": {}, + "metadata": { + "jupyter": { + "source_hidden": true + } + }, "outputs": [ { "ename": "NameError", @@ -386,6 +493,77 @@ "plt.show()" ] }, + { + "cell_type": "code", + "execution_count": 19, + "id": "7c905fc6-2f4e-49f3-91e1-9d4da9f580d2", + "metadata": {}, + "outputs": [ + { + "ename": "ValueError", + "evalue": "too many values to unpack (expected 2)", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[19], line 11\u001b[0m\n\u001b[1;32m 8\u001b[0m EEG_data \u001b[38;5;241m=\u001b[39m np\u001b[38;5;241m.\u001b[39mload(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m/home/vincent/AAA_projects/MVCS/Neuroscience/eeg_data_with_channels.npy\u001b[39m\u001b[38;5;124m'\u001b[39m, allow_pickle\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mTrue\u001b[39;00m)\n\u001b[1;32m 10\u001b[0m \u001b[38;5;66;03m# Assuming EEG_data[0] contains EEG data and EEG_data[1] contains channel names\u001b[39;00m\n\u001b[0;32m---> 11\u001b[0m eeg_data, ch_names \u001b[38;5;241m=\u001b[39m EEG_data\n\u001b[1;32m 13\u001b[0m \u001b[38;5;66;03m# Create MNE Info object\u001b[39;00m\n\u001b[1;32m 14\u001b[0m info \u001b[38;5;241m=\u001b[39m mne\u001b[38;5;241m.\u001b[39mcreate_info(ch_names\u001b[38;5;241m=\u001b[39mch_names, sfreq\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m1000.0\u001b[39m, ch_types\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124meeg\u001b[39m\u001b[38;5;124m'\u001b[39m)\n", + "\u001b[0;31mValueError\u001b[0m: too many values to unpack (expected 2)" + ] + } + ], + "source": [ + "import mne\n", + "import numpy as np\n", + "from mne.datasets import sample\n", + "from mne.forward import make_forward_solution\n", + "from mne.minimum_norm import make_inverse_operator, apply_inverse\n", + "\n", + "# Load EEG data from numpy file\n", + "EEG_data = np.load('/home/vincent/AAA_projects/MVCS/Neuroscience/eeg_data_with_channels.npy', allow_pickle=True)\n", + "\n", + "# Assuming EEG_data[0] contains EEG data and EEG_data[1] contains channel names\n", + "eeg_data, ch_names = EEG_data\n", + "\n", + "# Create MNE Info object\n", + "info = mne.create_info(ch_names=ch_names, sfreq=1000.0, ch_types='eeg')\n", + "\n", + "# Create MNE Raw object\n", + "raw = mne.io.RawArray(eeg_data, info)\n", + "\n", + "# Use fsaverage source space\n", + "src = mne.setup_source_space('fsaverage', spacing='ico5', subjects_dir=sample.data_path() + '/subjects')\n", + "\n", + "# Create the BEM model for fsaverage\n", + "model = mne.make_bem_model(subject='fsaverage', ico=4, subjects_dir=sample.data_path() + '/subjects')\n", + "bem = mne.make_bem_solution(model)\n", + "\n", + "# Compute the forward solution\n", + "fwd = make_forward_solution(raw.info, trans='fsaverage-trans.fif', src=src, bem=bem, meg=False, eeg=True, mindist=5.0, n_jobs=1)\n", + "\n", + "# Compute noise covariance from empty room data or use regularization techniques\n", + "cov = mne.compute_covariance(raw, tmax=0., method='shrunk')\n", + "\n", + "# Compute the inverse operator\n", + "inverse_operator = make_inverse_operator(raw.info, fwd, cov, loose=0.2, depth=0.8)\n", + "\n", + "# Estimate sources\n", + "method = \"dSPM\" # or 'MNE', 'sLORETA'\n", + "snr = 3.0\n", + "lambda2 = 1.0 / snr ** 2\n", + "stc = apply_inverse(raw, inverse_operator, lambda2, method=method, pick_ori=None)\n", + "\n", + "# Visualization\n", + "brain = stc.plot(subject='fsaverage', subjects_dir=sample.data_path() + '/subjects', initial_time=0.1, hemi='split')\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "812a91b5-8293-47e6-b2c6-35b8a4e070aa", + "metadata": {}, + "outputs": [], + "source": [] + }, { "cell_type": "code", "execution_count": null,