From 7c80b70163d8179f39f0a2bed1763265a6abc9b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Victor=20F=C3=A9rat?= Date: Fri, 13 Sep 2024 16:23:05 +0200 Subject: [PATCH 01/22] Add src to Report.add_bem --- mne/report/report.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/mne/report/report.py b/mne/report/report.py index 0f72a85b905..861491ce7bb 100644 --- a/mne/report/report.py +++ b/mne/report/report.py @@ -2436,6 +2436,13 @@ def add_bem( decim : int Use this decimation factor for generating MRI/BEM images (since it can be time consuming). + src : SourceSpaces | path-like | None + SourceSpaces instance or path to a source space to plot individual + sources as scatter-plot. Sources will be shown on exactly one slice + (whichever slice is closest to each source in the given orientation + plane). Path can be absolute or relative to the subject's ``bem`` + folder. + .. versionadded:: 1.9 width : int The width of the MRI images (in pixels). Larger values will have clearer surface lines, but will create larger HTML files. @@ -2455,6 +2462,7 @@ def add_bem( subject=subject, subjects_dir=subjects_dir, decim=decim, + src=src, n_jobs=n_jobs, width=width, image_format=self.image_format, @@ -2542,6 +2550,8 @@ def _add_slider( replace=replace, ) + + ########################################################################### # global rendering functions @verbose @@ -3043,6 +3053,7 @@ def _render_one_bem_axis( image_format, orientation, decim=2, + src=None, n_jobs=None, width=512, tags, @@ -3062,7 +3073,7 @@ def _render_one_bem_axis( mri_fname=mri_fname, surfaces=surfaces, orientation=orientation, - src=None, + src=src, show=False, show_orientation="always", width=width, @@ -4213,6 +4224,7 @@ def _add_bem( decim, n_jobs, width=512, + src=None, image_format, title, tags, @@ -4242,6 +4254,7 @@ def _add_bem( surfaces=surfaces, orientation=orientation, decim=decim, + src=src, n_jobs=n_jobs, width=width, image_format=image_format, From e46426070a43b039b12420dcba06abcfaffa38ec Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 13 Sep 2024 14:37:15 +0000 Subject: [PATCH 02/22] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- mne/report/report.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/mne/report/report.py b/mne/report/report.py index 861491ce7bb..c4ed019556c 100644 --- a/mne/report/report.py +++ b/mne/report/report.py @@ -2550,8 +2550,6 @@ def _add_slider( replace=replace, ) - - ########################################################################### # global rendering functions @verbose From 1710b1132321e5088dfbb910cf021e62681a0854 Mon Sep 17 00:00:00 2001 From: Victor Ferat Date: Fri, 4 Oct 2024 14:24:42 +0200 Subject: [PATCH 03/22] Update report.py --- mne/report/report.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/mne/report/report.py b/mne/report/report.py index c4ed019556c..02c59006ddc 100644 --- a/mne/report/report.py +++ b/mne/report/report.py @@ -3371,6 +3371,34 @@ def _add_forward( replace=replace, ) + if self.subject: + kwargs = dict( + info=forward["info"], + trans=forward["mri_head_t"], + src=forward["src"], + subject=subject, + subjects_dir=subjects_dir, + dig=True, + meg=["helmet", "sensors"], + show_axes=True, + eeg=dict(original=0.2, projected=0.8), + coord_frame="mri", + ) + img, caption = _iterate_trans_views( + function=plot_alignment, alpha=0.5, **kwargs + ) + self._add_image( + img=img, + title=title, + section=section, + caption=caption, + image_format="png", + tags=tags, + replace=replace, + ) + + + def _add_inverse_operator( self, *, From 690945ac5149deed144d9d374aa4ab62cdff0b9b Mon Sep 17 00:00:00 2001 From: Victor Ferat Date: Fri, 4 Oct 2024 14:24:50 +0200 Subject: [PATCH 04/22] Revert "Add src to Report.add_bem" This reverts commit 7c80b70163d8179f39f0a2bed1763265a6abc9b2. --- mne/report/report.py | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/mne/report/report.py b/mne/report/report.py index 02c59006ddc..0329d8dd81d 100644 --- a/mne/report/report.py +++ b/mne/report/report.py @@ -2436,13 +2436,6 @@ def add_bem( decim : int Use this decimation factor for generating MRI/BEM images (since it can be time consuming). - src : SourceSpaces | path-like | None - SourceSpaces instance or path to a source space to plot individual - sources as scatter-plot. Sources will be shown on exactly one slice - (whichever slice is closest to each source in the given orientation - plane). Path can be absolute or relative to the subject's ``bem`` - folder. - .. versionadded:: 1.9 width : int The width of the MRI images (in pixels). Larger values will have clearer surface lines, but will create larger HTML files. @@ -2462,7 +2455,6 @@ def add_bem( subject=subject, subjects_dir=subjects_dir, decim=decim, - src=src, n_jobs=n_jobs, width=width, image_format=self.image_format, @@ -3051,7 +3043,6 @@ def _render_one_bem_axis( image_format, orientation, decim=2, - src=None, n_jobs=None, width=512, tags, @@ -3071,7 +3062,7 @@ def _render_one_bem_axis( mri_fname=mri_fname, surfaces=surfaces, orientation=orientation, - src=src, + src=None, show=False, show_orientation="always", width=width, @@ -4250,7 +4241,6 @@ def _add_bem( decim, n_jobs, width=512, - src=None, image_format, title, tags, @@ -4280,7 +4270,6 @@ def _add_bem( surfaces=surfaces, orientation=orientation, decim=decim, - src=src, n_jobs=n_jobs, width=width, image_format=image_format, From 00c60faff8781decd7f7529cb938aaa95497c0f2 Mon Sep 17 00:00:00 2001 From: Victor Ferat Date: Fri, 4 Oct 2024 15:41:06 +0200 Subject: [PATCH 05/22] Add fig parameter to src.plot --- mne/source_space/_source_space.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/mne/source_space/_source_space.py b/mne/source_space/_source_space.py index 4265de0cd11..f4c932390ba 100644 --- a/mne/source_space/_source_space.py +++ b/mne/source_space/_source_space.py @@ -329,6 +329,7 @@ def plot( skull=None, subjects_dir=None, trans=None, + fig=None, verbose=None, ): """Plot the source space. @@ -358,6 +359,11 @@ def plot( produced during coregistration. If trans is None, an identity matrix is assumed. This is only needed when the source space is in head coordinates. + fig : Figure3D | None + PyVista scene in which to plot the alignment. + If ``None``, creates a new 600x600 pixel figure with black background. + + .. versionadded:: 1.9 %(verbose)s Returns @@ -427,6 +433,7 @@ def plot( ecog=False, bem=bem, src=self, + fig=fig, ) def __getitem__(self, *args, **kwargs): From b2da28300b8b59cb091d61b831b3bd4c6818080b Mon Sep 17 00:00:00 2001 From: Victor Ferat Date: Fri, 4 Oct 2024 15:41:36 +0200 Subject: [PATCH 06/22] test forward rendering --- mne/report/report.py | 57 +++++++++++++++++++++++++++++++++++++------- 1 file changed, 48 insertions(+), 9 deletions(-) diff --git a/mne/report/report.py b/mne/report/report.py index 0329d8dd81d..d71902aaecf 100644 --- a/mne/report/report.py +++ b/mne/report/report.py @@ -484,6 +484,21 @@ def _get_bem_contour_figs_as_arrays( return out +def _iterate_alignment_views(function, alpha, **kwargs): + """Auxiliary function to iterate over views in trans fig.""" + from ..viz.backends.renderer import MNE_3D_BACKEND_TESTING + + # TODO: Eventually maybe we should expose the size option? + size = (80, 80) if MNE_3D_BACKEND_TESTING else (800, 800) + fig = create_3d_figure(size, bgcolor=(0.5, 0.5, 0.5)) + from ..viz.backends.renderer import backend + + try: + return _itv(function, fig, **kwargs) + finally: + backend._close_3d_figure(fig) + + def _iterate_trans_views(function, alpha, **kwargs): """Auxiliary function to iterate over views in trans fig.""" from ..viz.backends.renderer import MNE_3D_BACKEND_TESTING @@ -1409,7 +1424,7 @@ def add_forward( subjects_dir=subjects_dir, title=title, image_format=self.image_format, - section=None, + section=title, tags=tags, replace=replace, ) @@ -2454,12 +2469,14 @@ def add_bem( self._add_bem( subject=subject, subjects_dir=subjects_dir, + src=None, decim=decim, n_jobs=n_jobs, width=width, image_format=self.image_format, title=title, tags=tags, + section=None, # No nesting replace=replace, ) @@ -3042,6 +3059,7 @@ def _render_one_bem_axis( surfaces, image_format, orientation, + src=None, decim=2, n_jobs=None, width=512, @@ -3062,7 +3080,7 @@ def _render_one_bem_axis( mri_fname=mri_fname, surfaces=surfaces, orientation=orientation, - src=None, + src=src, show=False, show_orientation="always", width=width, @@ -3363,13 +3381,15 @@ def _add_forward( ) if self.subject: + src = forward["src"] + trans = forward["mri_head_t"] + # Alignment kwargs = dict( info=forward["info"], - trans=forward["mri_head_t"], - src=forward["src"], + trans=trans, + src=src, subject=subject, subjects_dir=subjects_dir, - dig=True, meg=["helmet", "sensors"], show_axes=True, eeg=dict(original=0.2, projected=0.8), @@ -3380,14 +3400,30 @@ def _add_forward( ) self._add_image( img=img, - title=title, + title='Alignment', section=section, caption=caption, image_format="png", tags=tags, - replace=replace, + replace=replace + ) + # Source space + kwargs = dict( + trans=trans, + subjects_dir=subjects_dir, + ) + img, caption = _iterate_alignment_views( + function=src.plot, alpha=0.5, **kwargs + ) + self._add_image( + img=img, + title='Source space', + section=section, + caption=caption, + image_format="png", + tags=tags, + replace=replace ) - def _add_inverse_operator( @@ -4238,11 +4274,13 @@ def _add_bem( *, subject, subjects_dir, + src, decim, n_jobs, width=512, image_format, title, + section, tags, replace, ): @@ -4269,6 +4307,7 @@ def _add_bem( mri_fname=mri_fname, surfaces=surfaces, orientation=orientation, + src=src, decim=decim, n_jobs=n_jobs, width=width, @@ -4293,7 +4332,7 @@ def _add_bem( ) self._add_or_replace( title=title, - section=None, # no nesting + section=section, tags=tags, html_partial=html_partial, replace=replace, From 924c029112741f60a0ce8e99693547d4d3ace196 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 4 Oct 2024 13:41:55 +0000 Subject: [PATCH 07/22] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- mne/report/report.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/mne/report/report.py b/mne/report/report.py index d71902aaecf..56959c2e816 100644 --- a/mne/report/report.py +++ b/mne/report/report.py @@ -2476,7 +2476,7 @@ def add_bem( image_format=self.image_format, title=title, tags=tags, - section=None, # No nesting + section=None, # No nesting replace=replace, ) @@ -3394,18 +3394,18 @@ def _add_forward( show_axes=True, eeg=dict(original=0.2, projected=0.8), coord_frame="mri", - ) + ) img, caption = _iterate_trans_views( function=plot_alignment, alpha=0.5, **kwargs ) self._add_image( img=img, - title='Alignment', + title="Alignment", section=section, caption=caption, image_format="png", tags=tags, - replace=replace + replace=replace, ) # Source space kwargs = dict( @@ -3417,15 +3417,14 @@ def _add_forward( ) self._add_image( img=img, - title='Source space', + title="Source space", section=section, caption=caption, image_format="png", tags=tags, - replace=replace + replace=replace, ) - def _add_inverse_operator( self, *, From 26b6a4253fc9bda986b377fbca341663085604d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Victor=20F=C3=A9rat?= Date: Mon, 3 Feb 2025 10:16:10 +0100 Subject: [PATCH 08/22] wip --- mne/report/report.py | 1 + test.ipynb | 319 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 320 insertions(+) create mode 100644 test.ipynb diff --git a/mne/report/report.py b/mne/report/report.py index 861491ce7bb..914197611be 100644 --- a/mne/report/report.py +++ b/mne/report/report.py @@ -2420,6 +2420,7 @@ def add_bem( subjects_dir=None, decim=2, width=512, + src=None, n_jobs=None, tags=("bem",), replace=False, diff --git a/test.ipynb b/test.ipynb new file mode 100644 index 00000000000..96420a505c9 --- /dev/null +++ b/test.ipynb @@ -0,0 +1,319 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Dataset testing version 0.152 out of date, latest version is 0.156\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Downloading file 'mne-testing-data-0.156.tar.gz' from 'https://codeload.github.com/mne-tools/mne-testing-data/tar.gz/0.156' to 'C:\\Users\\victor.ferat\\mne_data'.\n", + "0.00B [00:00, ?B/s] \n", + "Untarring contents of 'C:\\Users\\victor.ferat\\mne_data\\mne-testing-data-0.156.tar.gz' to 'C:\\Users\\victor.ferat\\mne_data'\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Download complete in 05m25s (1046.5 MB)\n" + ] + } + ], + "source": [ + "# Authors: The MNE-Python contributors.\n", + "# License: BSD-3-Clause\n", + "# Copyright the MNE-Python contributors.\n", + "\n", + "import base64\n", + "import glob\n", + "import os\n", + "import pickle\n", + "import re\n", + "import shutil\n", + "from io import BytesIO\n", + "from pathlib import Path\n", + "\n", + "import numpy as np\n", + "import pytest\n", + "from matplotlib import pyplot as plt\n", + "\n", + "from mne import (\n", + " Epochs,\n", + " create_info,\n", + " pick_channels_cov,\n", + " read_cov,\n", + " read_events,\n", + " read_evokeds,\n", + ")\n", + "from mne._fiff.write import DATE_NONE\n", + "from mne.datasets import testing\n", + "from mne.epochs import make_metadata\n", + "from mne.io import RawArray, read_info, read_raw_fif\n", + "from mne.preprocessing import ICA\n", + "from mne.report import Report, _ReportScraper, open_report, report\n", + "from mne.report import report as report_mod\n", + "from mne.report.report import (\n", + " _ALLOWED_IMAGE_FORMATS,\n", + " CONTENT_ORDER,\n", + ")\n", + "from mne.utils import Bunch, _record_warnings\n", + "from mne.utils._testing import assert_object_equal\n", + "from mne.viz import plot_alignment\n", + "\n", + "data_dir = testing.data_path(download=True)\n", + "subjects_dir = data_dir / \"subjects\"\n", + "sample_meg_dir = data_dir / \"MEG\" / \"sample\"\n", + "raw_fname = sample_meg_dir / \"sample_audvis_trunc_raw.fif\"\n", + "ms_fname = data_dir / \"SSS\" / \"test_move_anon_raw.fif\"\n", + "events_fname = sample_meg_dir / \"sample_audvis_trunc_raw-eve.fif\"\n", + "evoked_fname = sample_meg_dir / \"sample_audvis_trunc-ave.fif\"\n", + "cov_fname = sample_meg_dir / \"sample_audvis_trunc-cov.fif\"\n", + "ecg_proj_fname = sample_meg_dir / \"sample_audvis_ecg-proj.fif\"\n", + "eog_proj_fname = sample_meg_dir / \"sample_audvis_eog-proj.fif\"\n", + "fwd_fname = sample_meg_dir / \"sample_audvis_trunc-meg-eeg-oct-6-fwd.fif\"\n", + "trans_fname = sample_meg_dir / \"sample_audvis_trunc-trans.fif\"\n", + "inv_fname = sample_meg_dir / \"sample_audvis_trunc-meg-eeg-oct-6-meg-inv.fif\"\n", + "stc_fname = sample_meg_dir / \"sample_audvis_trunc-meg\"\n", + "mri_fname = subjects_dir / \"sample\" / \"mri\" / \"T1.mgz\"\n", + "nirs_fname = (\n", + " data_dir / \"SNIRF\" / \"NIRx\" / \"NIRSport2\" / \"1.0.3\" / \"2021-05-05_001.snirf\"\n", + ")\n", + "stc_plot_kwargs = dict( # for speed\n", + " smoothing_steps=1, size=(300, 300), views=\"lat\", hemi=\"lh\"\n", + ")\n", + "topomap_kwargs = dict(res=8, contours=0, sensors=False)\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Reading forward solution from C:\\Users\\victor.ferat\\mne_data\\MNE-testing-data\\MEG\\sample\\sample_audvis_trunc-meg-eeg-oct-6-fwd.fif...\n", + " Reading a source space...\n", + " Computing patch statistics...\n", + " Patch information added...\n", + " Distance information added...\n", + " [done]\n", + " Reading a source space...\n", + " Computing patch statistics...\n", + " Patch information added...\n", + " Distance information added...\n", + " [done]\n", + " 2 source spaces read\n", + " Desired named matrix (kind = 3523 (FIFF_MNE_FORWARD_SOLUTION_GRAD)) not available\n", + " Read MEG forward solution (7928 sources, 306 channels, free orientations)\n", + " Desired named matrix (kind = 3523 (FIFF_MNE_FORWARD_SOLUTION_GRAD)) not available\n", + " Read EEG forward solution (7928 sources, 60 channels, free orientations)\n", + " Forward solutions combined: MEG, EEG\n", + " Source spaces transformed to the forward solution coordinate frame\n" + ] + } + ], + "source": [ + "import mne\n", + "\n", + "fwd = mne.read_forward_solution(fwd_fname)\n", + "if fwd['coord_frame'] == 'head'\n", + "src = fwd[\"src\"]\n", + "src_" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Using outer_skin.surf for head surface.\n", + "Getting helmet for system 306m\n", + "Channel types::\tgrad: 203, mag: 102, eeg: 59\n", + "Projecting sensors to the head surface\n" + ] + }, + { + "data": { + "text/plain": [ + "" + ] + }, + "execution_count": 26, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "mne.viz.plot_alignment(subject=None, trans=fwd[\"mri_head_t\"], info=fwd[\"info\"], src=fwd[\"src\"], eeg=dict(original=0.2, projected=0.8))" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'sample'" + ] + }, + "execution_count": 27, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + " fwd[\"src\"]._subject" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "info" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "head>\n", + "[[ 0.99930954 0.00998479 -0.03578702 -0.00316745]\n", + " [ 0.01275934 0.81240475 0.58295429 0.00685511]\n", + " [ 0.0348942 -0.58300853 0.81171638 0.02888404]\n", + " [ 0. 0. 0. 1. ]]" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "fwd[\"mri_head_t\"]" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Using surface: C:\\Users\\victor.ferat\\mne_data\\MNE-testing-data\\subjects\\sample\\bem\\inner_skull.surf\n", + "Using surface: C:\\Users\\victor.ferat\\mne_data\\MNE-testing-data\\subjects\\sample\\bem\\outer_skull.surf\n", + "Using surface: C:\\Users\\victor.ferat\\mne_data\\MNE-testing-data\\subjects\\sample\\bem\\outer_skin.surf\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "419MB [65:05:16, 1.79kB/s]\n" + ] + }, + { + "ename": "ValueError", + "evalue": "Source space must be in MRI coordinates, got head", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[1;32mIn[12], line 10\u001b[0m\n\u001b[0;32m 1\u001b[0m plot_bem_kwargs \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mdict\u001b[39m(\n\u001b[0;32m 2\u001b[0m subject\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124msample\u001b[39m\u001b[38;5;124m\"\u001b[39m,\n\u001b[0;32m 3\u001b[0m subjects_dir\u001b[38;5;241m=\u001b[39msubjects_dir,\n\u001b[1;32m (...)\u001b[0m\n\u001b[0;32m 7\u001b[0m \n\u001b[0;32m 8\u001b[0m )\n\u001b[1;32m---> 10\u001b[0m \u001b[43mmne\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mviz\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mplot_bem\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mplot_bem_kwargs\u001b[49m\u001b[43m)\u001b[49m\n", + "File \u001b[1;32mc:\\Users\\victor.ferat\\Documents\\GitHub\\mne-python\\mne\\viz\\misc.py:722\u001b[0m, in \u001b[0;36mplot_bem\u001b[1;34m(subject, subjects_dir, orientation, slices, brain_surfaces, src, show, show_indices, mri, show_orientation)\u001b[0m\n\u001b[0;32m 716\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mOSError\u001b[39;00m(\n\u001b[0;32m 717\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mNo surface files found. Surface files must end with \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m 718\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124minner_skull.surf, outer_skull.surf or outer_skin.surf\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m 719\u001b[0m )\n\u001b[0;32m 721\u001b[0m \u001b[38;5;66;03m# Plot the contours\u001b[39;00m\n\u001b[1;32m--> 722\u001b[0m fig \u001b[38;5;241m=\u001b[39m \u001b[43m_plot_mri_contours\u001b[49m\u001b[43m(\u001b[49m\n\u001b[0;32m 723\u001b[0m \u001b[43m \u001b[49m\u001b[43mmri_fname\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mmri_fname\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m 724\u001b[0m \u001b[43m \u001b[49m\u001b[43msurfaces\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43msurfaces\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m 725\u001b[0m \u001b[43m \u001b[49m\u001b[43msrc\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43msrc\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m 726\u001b[0m \u001b[43m \u001b[49m\u001b[43morientation\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43morientation\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m 727\u001b[0m \u001b[43m \u001b[49m\u001b[43mslices\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mslices\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m 728\u001b[0m \u001b[43m \u001b[49m\u001b[43mshow\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mshow\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m 729\u001b[0m \u001b[43m \u001b[49m\u001b[43mshow_indices\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mshow_indices\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m 730\u001b[0m \u001b[43m \u001b[49m\u001b[43mshow_orientation\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mshow_orientation\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m 731\u001b[0m \u001b[43m \u001b[49m\u001b[43mslices_as_subplots\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43;01mTrue\u001b[39;49;00m\u001b[43m,\u001b[49m\n\u001b[0;32m 732\u001b[0m \u001b[43m\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m 733\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m fig\n", + "File \u001b[1;32mc:\\Users\\victor.ferat\\Documents\\GitHub\\mne-python\\mne\\viz\\misc.py:444\u001b[0m, in \u001b[0;36m_plot_mri_contours\u001b[1;34m(mri_fname, surfaces, src, orientation, slices, show, show_indices, show_orientation, width, slices_as_subplots)\u001b[0m\n\u001b[0;32m 442\u001b[0m \u001b[38;5;66;03m# Eventually we can relax this by allowing ``trans`` if need be\u001b[39;00m\n\u001b[0;32m 443\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m src[\u001b[38;5;241m0\u001b[39m][\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mcoord_frame\u001b[39m\u001b[38;5;124m\"\u001b[39m] \u001b[38;5;241m!=\u001b[39m FIFF\u001b[38;5;241m.\u001b[39mFIFFV_COORD_MRI:\n\u001b[1;32m--> 444\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\n\u001b[0;32m 445\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mSource space must be in MRI coordinates, got \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m 446\u001b[0m \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;132;01m{\u001b[39;00m_frame_to_str[src[\u001b[38;5;241m0\u001b[39m][\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mcoord_frame\u001b[39m\u001b[38;5;124m\"\u001b[39m]]\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m'\u001b[39m\n\u001b[0;32m 447\u001b[0m )\n\u001b[0;32m 448\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m src_ \u001b[38;5;129;01min\u001b[39;00m src:\n\u001b[0;32m 449\u001b[0m points \u001b[38;5;241m=\u001b[39m src_[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mrr\u001b[39m\u001b[38;5;124m\"\u001b[39m][src_[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124minuse\u001b[39m\u001b[38;5;124m\"\u001b[39m]\u001b[38;5;241m.\u001b[39mastype(\u001b[38;5;28mbool\u001b[39m)]\n", + "\u001b[1;31mValueError\u001b[0m: Source space must be in MRI coordinates, got head" + ] + } + ], + "source": [ + "plot_bem_kwargs = dict(\n", + " subject=\"sample\",\n", + " subjects_dir=subjects_dir,\n", + " brain_surfaces=\"white\",\n", + " orientation=\"coronal\",\n", + " src=src,\n", + "\n", + ")\n", + "\n", + "mne.viz.plot_bem(**plot_bem_kwargs)" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "'SourceEstimate' object is not subscriptable", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[1;32mIn[14], line 2\u001b[0m\n\u001b[0;32m 1\u001b[0m stc \u001b[38;5;241m=\u001b[39m mne\u001b[38;5;241m.\u001b[39mread_source_estimate(stc_fname)\n\u001b[1;32m----> 2\u001b[0m \u001b[43mstc\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43msrc\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m]\u001b[49m\n", + "\u001b[1;31mTypeError\u001b[0m: 'SourceEstimate' object is not subscriptable" + ] + } + ], + "source": [ + "stc = mne.read_source_estimate(stc_fname)\n", + "stc" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "mne_dev", + "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.4" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} From 45a4fa1e1d86b921d54d31e368045dbf57928d23 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Victor=20F=C3=A9rat?= Date: Mon, 10 Feb 2025 14:01:22 +0100 Subject: [PATCH 09/22] wip --- mne/report/report.py | 24 +++++++++- mne/viz/misc.py | 21 ++++++--- test.ipynb | 103 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 140 insertions(+), 8 deletions(-) create mode 100644 test.ipynb diff --git a/mne/report/report.py b/mne/report/report.py index d71902aaecf..531bdb09249 100644 --- a/mne/report/report.py +++ b/mne/report/report.py @@ -452,7 +452,7 @@ def _fig_to_img(fig, *, image_format="png", own_figure=True): def _get_bem_contour_figs_as_arrays( - *, sl, n_jobs, mri_fname, surfaces, orientation, src, show, show_orientation, width + *, sl, n_jobs, mri_fname, surfaces, orientation, src, trans, show, show_orientation, width ): """Render BEM surface contours on MRI slices. @@ -471,6 +471,7 @@ def _get_bem_contour_figs_as_arrays( surfaces=surfaces, orientation=orientation, src=src, + trans=trans, show=show, show_orientation=show_orientation, width=width, @@ -2470,6 +2471,7 @@ def add_bem( subject=subject, subjects_dir=subjects_dir, src=None, + trans=None, decim=decim, n_jobs=n_jobs, width=width, @@ -3060,6 +3062,7 @@ def _render_one_bem_axis( image_format, orientation, src=None, + trans=None, decim=2, n_jobs=None, width=512, @@ -3081,6 +3084,7 @@ def _render_one_bem_axis( surfaces=surfaces, orientation=orientation, src=src, + trans=trans, show=False, show_orientation="always", width=width, @@ -3425,6 +3429,22 @@ def _add_forward( replace=replace ) + self._add_bem( + subject=subject, + subjects_dir=subjects_dir, + src=src, + trans=trans, + decim=1, + n_jobs=1, + width=512, + image_format=image_format, + title="source space", + section=section, + tags=tags, + replace=replace, + ) + + def _add_inverse_operator( self, @@ -4275,6 +4295,7 @@ def _add_bem( subject, subjects_dir, src, + trans, decim, n_jobs, width=512, @@ -4308,6 +4329,7 @@ def _add_bem( surfaces=surfaces, orientation=orientation, src=src, + trans=trans, decim=decim, n_jobs=n_jobs, width=width, diff --git a/mne/viz/misc.py b/mne/viz/misc.py index af1345aa69d..b44ca2a2b66 100644 --- a/mne/viz/misc.py +++ b/mne/viz/misc.py @@ -360,6 +360,7 @@ def _plot_mri_contours( mri_fname, surfaces, src, + trans=None, orientation="coronal", slices=None, show=True, @@ -439,15 +440,16 @@ def _plot_mri_contours( sources = list() if src is not None: _ensure_src(src, extra=" or None") - # Eventually we can relax this by allowing ``trans`` if need be - if src[0]["coord_frame"] != FIFF.FIFFV_COORD_MRI: - raise ValueError( - "Source space must be in MRI coordinates, got " - f'{_frame_to_str[src[0]["coord_frame"]]}' - ) for src_ in src: points = src_["rr"][src_["inuse"].astype(bool)] - sources.append(apply_trans(mri_rasvox_t, points * 1e3)) + if src_["coord_frame"] != FIFF.FIFFV_COORD_MRI: + if trans is None: + raise ValueError( + "Source space must be in MRI coordinates, or " + "provide a trans.") + else: + points = apply_trans(np.linalg.inv(trans['trans']), points) + sources.append(apply_trans(mri_rasvox_t, points* 1e3)) sources = np.concatenate(sources, axis=0) # get the figure dimensions right @@ -600,6 +602,7 @@ def plot_bem( slices=None, brain_surfaces=None, src=None, + trans=None, show=True, show_indices=True, mri="T1.mgz", @@ -629,6 +632,9 @@ def plot_bem( .. versionchanged:: 0.20 All sources are shown on the nearest slice rather than some being omitted. + trans : TODO + TODO + .. versionadded:: 1.9 show : bool Show figure if True. show_indices : bool @@ -723,6 +729,7 @@ def plot_bem( mri_fname=mri_fname, surfaces=surfaces, src=src, + trans=trans, orientation=orientation, slices=slices, show=show, diff --git a/test.ipynb b/test.ipynb new file mode 100644 index 00000000000..61663da0087 --- /dev/null +++ b/test.ipynb @@ -0,0 +1,103 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Authors: The MNE-Python contributors.\n", + "# License: BSD-3-Clause\n", + "# Copyright the MNE-Python contributors.\n", + "\n", + "import base64\n", + "import glob\n", + "import os\n", + "import pickle\n", + "import re\n", + "import shutil\n", + "from io import BytesIO\n", + "from pathlib import Path\n", + "\n", + "import numpy as np\n", + "import pytest\n", + "from matplotlib import pyplot as plt\n", + "\n", + "from mne import (\n", + " Epochs,\n", + " create_info,\n", + " pick_channels_cov,\n", + " read_cov,\n", + " read_events,\n", + " read_evokeds,\n", + ")\n", + "from mne._fiff.write import DATE_NONE\n", + "from mne.datasets import testing\n", + "from mne.epochs import make_metadata\n", + "from mne.io import RawArray, read_info, read_raw_fif\n", + "from mne.preprocessing import ICA\n", + "from mne.report import Report, _ReportScraper, open_report, report\n", + "from mne.report import report as report_mod\n", + "from mne.report.report import (\n", + " _ALLOWED_IMAGE_FORMATS,\n", + " CONTENT_ORDER,\n", + ")\n", + "from mne.utils import Bunch, _record_warnings\n", + "from mne.utils._testing import assert_object_equal\n", + "from mne.viz import plot_alignment\n", + "\n", + "data_dir = testing.data_path(download=False)\n", + "subjects_dir = data_dir / \"subjects\"\n", + "sample_meg_dir = data_dir / \"MEG\" / \"sample\"\n", + "raw_fname = sample_meg_dir / \"sample_audvis_trunc_raw.fif\"\n", + "ms_fname = data_dir / \"SSS\" / \"test_move_anon_raw.fif\"\n", + "events_fname = sample_meg_dir / \"sample_audvis_trunc_raw-eve.fif\"\n", + "evoked_fname = sample_meg_dir / \"sample_audvis_trunc-ave.fif\"\n", + "cov_fname = sample_meg_dir / \"sample_audvis_trunc-cov.fif\"\n", + "ecg_proj_fname = sample_meg_dir / \"sample_audvis_ecg-proj.fif\"\n", + "eog_proj_fname = sample_meg_dir / \"sample_audvis_eog-proj.fif\"\n", + "fwd_fname = sample_meg_dir / \"sample_audvis_trunc-meg-eeg-oct-6-fwd.fif\"\n", + "trans_fname = sample_meg_dir / \"sample_audvis_trunc-trans.fif\"\n", + "inv_fname = sample_meg_dir / \"sample_audvis_trunc-meg-eeg-oct-6-meg-inv.fif\"\n", + "stc_fname = sample_meg_dir / \"sample_audvis_trunc-meg\"\n", + "mri_fname = subjects_dir / \"sample\" / \"mri\" / \"T1.mgz\"\n", + "nirs_fname = (\n", + " data_dir / \"SNIRF\" / \"NIRx\" / \"NIRSport2\" / \"1.0.3\" / \"2021-05-05_001.snirf\"\n", + ")\n", + "stc_plot_kwargs = dict( # for speed\n", + " smoothing_steps=1, size=(300, 300), views=\"lat\", hemi=\"lh\"\n", + ")\n", + "topomap_kwargs = dict(res=8, contours=0, sensors=False)\n", + "\n", + "\n", + "import mne\n", + "report = mne.Report(subject='sample', subjects_dir=subjects_dir)\n", + "report.add_forward(fwd_fname, title='Forward')\n", + "\n", + "\n", + "report.save('test_report.html', overwrite=True)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "mne-dev", + "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.6" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} From 175a622605f2e06af0eee479d35c58795fcd95f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Victor=20F=C3=A9rat?= Date: Mon, 10 Feb 2025 14:20:01 +0100 Subject: [PATCH 10/22] fix section --- mne/report/report.py | 1 - test.ipynb | 82 ++++++++++---------------------------------- 2 files changed, 19 insertions(+), 64 deletions(-) diff --git a/mne/report/report.py b/mne/report/report.py index 87a14359ba3..9eddb4b33d3 100644 --- a/mne/report/report.py +++ b/mne/report/report.py @@ -4498,7 +4498,6 @@ def _add_bem( title, section, tags, - section, replace, ): """Render mri+bem (only PNG).""" diff --git a/test.ipynb b/test.ipynb index 96420a505c9..3d528b0da89 100644 --- a/test.ipynb +++ b/test.ipynb @@ -2,33 +2,9 @@ "cells": [ { "cell_type": "code", - "execution_count": 8, + "execution_count": 2, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Dataset testing version 0.152 out of date, latest version is 0.156\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "Downloading file 'mne-testing-data-0.156.tar.gz' from 'https://codeload.github.com/mne-tools/mne-testing-data/tar.gz/0.156' to 'C:\\Users\\victor.ferat\\mne_data'.\n", - "0.00B [00:00, ?B/s] \n", - "Untarring contents of 'C:\\Users\\victor.ferat\\mne_data\\mne-testing-data-0.156.tar.gz' to 'C:\\Users\\victor.ferat\\mne_data'\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Download complete in 05m25s (1046.5 MB)\n" - ] - } - ], + "outputs": [], "source": [ "# Authors: The MNE-Python contributors.\n", "# License: BSD-3-Clause\n", @@ -97,14 +73,14 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "Reading forward solution from C:\\Users\\victor.ferat\\mne_data\\MNE-testing-data\\MEG\\sample\\sample_audvis_trunc-meg-eeg-oct-6-fwd.fif...\n", + "Reading forward solution from C:\\Users\\ferat\\mne_data\\MNE-testing-data\\MEG\\sample\\sample_audvis_trunc-meg-eeg-oct-6-fwd.fif...\n", " Reading a source space...\n", " Computing patch statistics...\n", " Patch information added...\n", @@ -129,39 +105,35 @@ "import mne\n", "\n", "fwd = mne.read_forward_solution(fwd_fname)\n", - "if fwd['coord_frame'] == 'head'\n", - "src = fwd[\"src\"]\n", - "src_" + "if fwd['coord_frame'] == 'head':\n", + " src = fwd[\"src\"]\n", + " src_" ] }, { "cell_type": "code", - "execution_count": 26, + "execution_count": null, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "Using outer_skin.surf for head surface.\n", - "Getting helmet for system 306m\n", - "Channel types::\tgrad: 203, mag: 102, eeg: 59\n", - "Projecting sensors to the head surface\n" + "Using default location ~/mne_data for sample...\n" ] }, { - "data": { - "text/plain": [ - "" - ] - }, - "execution_count": 26, - "metadata": {}, - "output_type": "execute_result" + "name": "stderr", + "output_type": "stream", + "text": [ + "Downloading file 'MNE-sample-data-processed.tar.gz' from 'https://osf.io/86qa2/download?version=6' to 'C:\\Users\\ferat\\mne_data'.\n", + " 33%|############5 | 544M/1.65G [00:26<00:54, 20.2MB/s]" + ] } ], "source": [ - "mne.viz.plot_alignment(subject=None, trans=fwd[\"mri_head_t\"], info=fwd[\"info\"], src=fwd[\"src\"], eeg=dict(original=0.2, projected=0.8))" + "SUBJECT_DIR = Path(mne.datasets.sample.data_path()) / \"subjects\"\n", + "mne.viz.plot_alignment(subject=None, trans=fwd[\"mri_head_t\"], info=fwd[\"info\"], src=fwd[\"src\"], eeg=dict(original=0.2, projected=0.8), subjects_dir=SUBJECT_DIR)" ] }, { @@ -184,15 +156,6 @@ " fwd[\"src\"]._subject" ] }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "info" - ] - }, { "cell_type": "code", "execution_count": 17, @@ -286,18 +249,11 @@ "stc = mne.read_source_estimate(stc_fname)\n", "stc" ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] } ], "metadata": { "kernelspec": { - "display_name": "mne_dev", + "display_name": "mne-dev", "language": "python", "name": "python3" }, @@ -311,7 +267,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.12.4" + "version": "3.12.6" } }, "nbformat": 4, From 9226b32e34de4038cd5290addde020c798d00f28 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 10 Feb 2025 13:20:23 +0000 Subject: [PATCH 11/22] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- mne/report/report.py | 40 ++++++++++++++++++++++++---------------- mne/viz/misc.py | 10 +++++----- 2 files changed, 29 insertions(+), 21 deletions(-) diff --git a/mne/report/report.py b/mne/report/report.py index 9eddb4b33d3..d498dc33a01 100644 --- a/mne/report/report.py +++ b/mne/report/report.py @@ -475,7 +475,17 @@ def _fig_to_img( def _get_bem_contour_figs_as_arrays( - *, sl, n_jobs, mri_fname, surfaces, orientation, src, trans, show, show_orientation, width + *, + sl, + n_jobs, + mri_fname, + surfaces, + orientation, + src, + trans, + show, + show_orientation, + width, ): """Render BEM surface contours on MRI slices. @@ -3626,21 +3636,19 @@ def _add_forward( ) self._add_bem( - subject=subject, - subjects_dir=subjects_dir, - src=src, - trans=trans, - decim=1, - n_jobs=1, - width=512, - image_format=image_format, - title="source space", - section=section, - tags=tags, - replace=replace, - ) - - + subject=subject, + subjects_dir=subjects_dir, + src=src, + trans=trans, + decim=1, + n_jobs=1, + width=512, + image_format=image_format, + title="source space", + section=section, + tags=tags, + replace=replace, + ) def _add_inverse_operator( self, diff --git a/mne/viz/misc.py b/mne/viz/misc.py index 2f4658b31ad..a08f4b60b14 100644 --- a/mne/viz/misc.py +++ b/mne/viz/misc.py @@ -32,7 +32,7 @@ from ..fixes import _safe_svd from ..rank import compute_rank from ..surface import read_surface -from ..transforms import _frame_to_str, apply_trans +from ..transforms import apply_trans from ..utils import ( _check_option, _mask_to_onsets_offsets, @@ -445,11 +445,11 @@ def _plot_mri_contours( if src_["coord_frame"] != FIFF.FIFFV_COORD_MRI: if trans is None: raise ValueError( - "Source space must be in MRI coordinates, or " - "provide a trans.") + "Source space must be in MRI coordinates, or provide a trans." + ) else: - points = apply_trans(np.linalg.inv(trans['trans']), points) - sources.append(apply_trans(mri_rasvox_t, points* 1e3)) + points = apply_trans(np.linalg.inv(trans["trans"]), points) + sources.append(apply_trans(mri_rasvox_t, points * 1e3)) sources = np.concatenate(sources, axis=0) # get the figure dimensions right From b7529b5fcceb8913e7353b3ee482588473b119ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Victor=20F=C3=A9rat?= Date: Mon, 10 Feb 2025 15:03:04 +0100 Subject: [PATCH 12/22] Update test_misc.py --- mne/viz/tests/test_misc.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mne/viz/tests/test_misc.py b/mne/viz/tests/test_misc.py index fe179756c53..79fd85ceddf 100644 --- a/mne/viz/tests/test_misc.py +++ b/mne/viz/tests/test_misc.py @@ -169,7 +169,7 @@ def test_plot_bem(): src=src_fname, ) assert len(fig.axes[0].collections) == 4 # 3 BEM surfaces + 1 src contour - with pytest.raises(ValueError, match="MRI coordinates, got head"): + with pytest.raises(ValueError, match="Source space must be in MRI coordinates"): plot_bem(subject="sample", subjects_dir=subjects_dir, src=inv_fname) From 3f80b8fd0553eae389c1e4b3e3104c3b9b12677e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Victor=20F=C3=A9rat?= Date: Mon, 10 Feb 2025 15:03:09 +0100 Subject: [PATCH 13/22] Create 12848.newfeature.rst --- doc/changes/devel/12848.newfeature.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 doc/changes/devel/12848.newfeature.rst diff --git a/doc/changes/devel/12848.newfeature.rst b/doc/changes/devel/12848.newfeature.rst new file mode 100644 index 00000000000..ebd0a791366 --- /dev/null +++ b/doc/changes/devel/12848.newfeature.rst @@ -0,0 +1 @@ +Add source space(s) visualization(s) in :func:`mne.Report._add_forward`, by `Victor Ferat`_. \ No newline at end of file From 5f38c725c3d9cf487c3abfda0dbe1ca715a8b546 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Victor=20F=C3=A9rat?= Date: Fri, 21 Feb 2025 17:19:52 +0100 Subject: [PATCH 14/22] wip --- mne/report/report.py | 30 +++++++++++++++--------------- mne/viz/misc.py | 4 +--- 2 files changed, 16 insertions(+), 18 deletions(-) diff --git a/mne/report/report.py b/mne/report/report.py index d498dc33a01..d62712e670c 100644 --- a/mne/report/report.py +++ b/mne/report/report.py @@ -2627,7 +2627,6 @@ def add_bem( subjects_dir=None, decim=2, width=512, - src=None, n_jobs=None, tags=("bem",), section=None, @@ -2674,7 +2673,7 @@ def add_bem( image_format=self.image_format, title=title, tags=tags, - section=None, # No nesting + section=section, replace=replace, ) @@ -3622,18 +3621,6 @@ def _add_forward( trans=trans, subjects_dir=subjects_dir, ) - img, caption = _iterate_alignment_views( - function=src.plot, alpha=0.5, **kwargs - ) - self._add_image( - img=img, - title="Source space", - section=section, - caption=caption, - image_format="png", - tags=tags, - replace=replace, - ) self._add_bem( subject=subject, @@ -3644,12 +3631,25 @@ def _add_forward( n_jobs=1, width=512, image_format=image_format, - title="source space", + title="Source space(s) (BEM view)", section=section, tags=tags, replace=replace, ) + img, caption = _iterate_alignment_views( + function=src.plot, alpha=0.5, **kwargs + ) + self._add_image( + img=img, + title="Source space(s) (3D view)", + section=section, + caption=None, + image_format="png", + tags=tags, + replace=replace, + ) + def _add_inverse_operator( self, *, diff --git a/mne/viz/misc.py b/mne/viz/misc.py index a08f4b60b14..21453e1e04b 100644 --- a/mne/viz/misc.py +++ b/mne/viz/misc.py @@ -631,9 +631,7 @@ def plot_bem( .. versionchanged:: 0.20 All sources are shown on the nearest slice rather than some - being omitted. - trans : TODO - TODO + %(trans)s .. versionadded:: 1.9 show : bool Show figure if True. From 6103859eaa9c7aa232af03acc9e51149984e8f4f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Victor=20F=C3=A9rat?= Date: Fri, 21 Feb 2025 17:54:19 +0100 Subject: [PATCH 15/22] Update 12848.newfeature.rst --- doc/changes/devel/12848.newfeature.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/changes/devel/12848.newfeature.rst b/doc/changes/devel/12848.newfeature.rst index ebd0a791366..7b0398c714f 100644 --- a/doc/changes/devel/12848.newfeature.rst +++ b/doc/changes/devel/12848.newfeature.rst @@ -1 +1 @@ -Add source space(s) visualization(s) in :func:`mne.Report._add_forward`, by `Victor Ferat`_. \ No newline at end of file +Add source space(s) visualization(s) in :func:`mne.Report.add_forward`, by `Victor Ferat`_. \ No newline at end of file From 8664b458e6f0d0aca1ecf80212243eb7d32b1947 Mon Sep 17 00:00:00 2001 From: vferat Date: Sun, 23 Feb 2025 00:55:33 +0100 Subject: [PATCH 16/22] Update 12848.newfeature.rst --- doc/changes/devel/12848.newfeature.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/changes/devel/12848.newfeature.rst b/doc/changes/devel/12848.newfeature.rst index ebd0a791366..7b0398c714f 100644 --- a/doc/changes/devel/12848.newfeature.rst +++ b/doc/changes/devel/12848.newfeature.rst @@ -1 +1 @@ -Add source space(s) visualization(s) in :func:`mne.Report._add_forward`, by `Victor Ferat`_. \ No newline at end of file +Add source space(s) visualization(s) in :func:`mne.Report.add_forward`, by `Victor Ferat`_. \ No newline at end of file From 64c1e1380cea6c9ace9fe2f4131b92d139e6f5ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Victor=20F=C3=A9rat?= Date: Mon, 17 Mar 2025 16:33:03 +0100 Subject: [PATCH 17/22] Change forward figures --- mne/report/report.py | 28 ++- test.ipynb | 587 ++++++++++++++++++++++++++++++++----------- 2 files changed, 461 insertions(+), 154 deletions(-) diff --git a/mne/report/report.py b/mne/report/report.py index d62712e670c..a12551edb06 100644 --- a/mne/report/report.py +++ b/mne/report/report.py @@ -3605,13 +3605,17 @@ def _add_forward( coord_frame="mri", ) img, caption = _iterate_trans_views( - function=plot_alignment, alpha=0.5, **kwargs + function=plot_alignment, + alpha=0.5, + max_width=self.img_max_width, + max_res=self.img_max_res, + **kwargs ) self._add_image( img=img, title="Alignment", section=section, - caption=caption, + caption=None, image_format="png", tags=tags, replace=replace, @@ -3637,8 +3641,26 @@ def _add_forward( replace=replace, ) + if src.kind == "surface" or src.kind == "mixed": + surfaces=dict(head=0.1, white=0.5) + else: + surfaces=dict(head=0.1) + + kwargs = dict( + trans=trans, + src=src, + subject=subject, + subjects_dir=subjects_dir, + show_axes=False, + coord_frame="mri", + surfaces=surfaces, + ) img, caption = _iterate_alignment_views( - function=src.plot, alpha=0.5, **kwargs + function=plot_alignment, + alpha=0.5, + max_width=self.img_max_width, + max_res=self.img_max_res, + **kwargs ) self._add_image( img=img, diff --git a/test.ipynb b/test.ipynb index 3d528b0da89..e4360035dce 100644 --- a/test.ipynb +++ b/test.ipynb @@ -2,9 +2,381 @@ "cells": [ { "cell_type": "code", - "execution_count": 2, + "execution_count": 1, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Reading forward solution from C:\\Users\\ferat\\mne_data\\MNE-testing-data\\MEG\\sample\\sample_audvis_trunc-meg-eeg-oct-6-fwd.fif...\n", + " Reading a source space...\n", + " Computing patch statistics...\n", + " Patch information added...\n", + " Distance information added...\n", + " [done]\n", + " Reading a source space...\n", + " Computing patch statistics...\n", + " Patch information added...\n", + " Distance information added...\n", + " [done]\n", + " 2 source spaces read\n", + " Desired named matrix (kind = 3523 (FIFF_MNE_FORWARD_SOLUTION_GRAD)) not available\n", + " Read MEG forward solution (7928 sources, 306 channels, free orientations)\n", + " Desired named matrix (kind = 3523 (FIFF_MNE_FORWARD_SOLUTION_GRAD)) not available\n", + " Read EEG forward solution (7928 sources, 60 channels, free orientations)\n", + " Forward solutions combined: MEG, EEG\n", + " Source spaces transformed to the forward solution coordinate frame\n" + ] + }, + { + "data": { + "text/html": [ + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + " \n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + " \n", + " \n", + "\n", + "\n", + "\n", + "\n", + " \n", + " \n", + " \n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + "\n", + " \n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + " \n", + " \n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + " \n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + " \n", + " \n", + "\n", + "\n", + "\n", + " \n", + "\n", + " \n", + " \n", + " \n", + "\n", + "\n", + " \n", + "\n", + " \n", + " \n", + " \n", + "\n", + "\n", + " \n", + "\n", + " \n", + " \n", + " \n", + "\n", + "\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + "\n", + " \n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + " \n", + " \n", + "\n", + "\n", + "\n", + "\n", + "\n", + "
\n", + " \n", + " \n", + " General\n", + "
MNE object typeInfo
Measurement dateUnknown
ParticipantUnknown
ExperimenterUnknown
\n", + " \n", + " \n", + " Acquisition\n", + "
\n", + " \n", + " \n", + " Channels\n", + "
Magnetometers\n", + " \n", + "\n", + " \n", + "
Gradiometers\n", + " \n", + "\n", + " \n", + " \n", + " and \n", + " \n", + "
EEG\n", + " \n", + "\n", + " \n", + " \n", + " and \n", + " \n", + "
Head & sensor digitization0 points
\n", + " \n", + " \n", + " Filters\n", + "
" + ], + "text/plain": [ + " head transform\n", + " meas_file: sample_audvis_trunc_raw.fif\n", + " mri_file: ./sample_audvis_trunc-trans.fif\n", + " mri_head_t: MRI (surface RAS) -> head transform\n", + " mri_id: 4 items (dict)\n", + " nchan: 366\n", + " working_dir: /home/larsoner/custombuilds/mne-python/examples/MNE-testing- ...\n", + ">" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Authors: The MNE-Python contributors.\n", "# License: BSD-3-Clause\n", @@ -23,6 +395,7 @@ "import pytest\n", "from matplotlib import pyplot as plt\n", "\n", + "import mne\n", "from mne import (\n", " Epochs,\n", " create_info,\n", @@ -50,205 +423,117 @@ "subjects_dir = data_dir / \"subjects\"\n", "sample_meg_dir = data_dir / \"MEG\" / \"sample\"\n", "raw_fname = sample_meg_dir / \"sample_audvis_trunc_raw.fif\"\n", - "ms_fname = data_dir / \"SSS\" / \"test_move_anon_raw.fif\"\n", - "events_fname = sample_meg_dir / \"sample_audvis_trunc_raw-eve.fif\"\n", - "evoked_fname = sample_meg_dir / \"sample_audvis_trunc-ave.fif\"\n", - "cov_fname = sample_meg_dir / \"sample_audvis_trunc-cov.fif\"\n", - "ecg_proj_fname = sample_meg_dir / \"sample_audvis_ecg-proj.fif\"\n", - "eog_proj_fname = sample_meg_dir / \"sample_audvis_eog-proj.fif\"\n", "fwd_fname = sample_meg_dir / \"sample_audvis_trunc-meg-eeg-oct-6-fwd.fif\"\n", "trans_fname = sample_meg_dir / \"sample_audvis_trunc-trans.fif\"\n", - "inv_fname = sample_meg_dir / \"sample_audvis_trunc-meg-eeg-oct-6-meg-inv.fif\"\n", - "stc_fname = sample_meg_dir / \"sample_audvis_trunc-meg\"\n", "mri_fname = subjects_dir / \"sample\" / \"mri\" / \"T1.mgz\"\n", - "nirs_fname = (\n", - " data_dir / \"SNIRF\" / \"NIRx\" / \"NIRSport2\" / \"1.0.3\" / \"2021-05-05_001.snirf\"\n", - ")\n", - "stc_plot_kwargs = dict( # for speed\n", - " smoothing_steps=1, size=(300, 300), views=\"lat\", hemi=\"lh\"\n", - ")\n", - "topomap_kwargs = dict(res=8, contours=0, sensors=False)\n", - "\n" + "bem = r\"C:\\Users\\ferat\\mne_data\\MNE-testing-data\\subjects\\sample\\bem\\sample-320-320-320-bem-sol.fif\"\n", + "\n", + "\n", + "data_dir = testing.data_path(download=True)\n", + "subjects_dir = data_dir / \"subjects\"\n", + "sample_meg_dir = data_dir / \"MEG\" / \"sample\"\n", + "fwd_fname = sample_meg_dir / \"sample_audvis_trunc-meg-eeg-oct-6-fwd.fif\"\n", + "fwd = mne.read_forward_solution(fwd_fname)\n", + "fwd[\"info\"]" ] }, { "cell_type": "code", - "execution_count": 4, + "execution_count": null, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "Reading forward solution from C:\\Users\\ferat\\mne_data\\MNE-testing-data\\MEG\\sample\\sample_audvis_trunc-meg-eeg-oct-6-fwd.fif...\n", - " Reading a source space...\n", - " Computing patch statistics...\n", - " Patch information added...\n", - " Distance information added...\n", - " [done]\n", - " Reading a source space...\n", - " Computing patch statistics...\n", - " Patch information added...\n", - " Distance information added...\n", - " [done]\n", - " 2 source spaces read\n", - " Desired named matrix (kind = 3523 (FIFF_MNE_FORWARD_SOLUTION_GRAD)) not available\n", - " Read MEG forward solution (7928 sources, 306 channels, free orientations)\n", - " Desired named matrix (kind = 3523 (FIFF_MNE_FORWARD_SOLUTION_GRAD)) not available\n", - " Read EEG forward solution (7928 sources, 60 channels, free orientations)\n", - " Forward solutions combined: MEG, EEG\n", - " Source spaces transformed to the forward solution coordinate frame\n" + "Embedding : jquery-3.6.0.min.js\n", + "Embedding : bootstrap.bundle.min.js\n", + "Embedding : bootstrap.min.css\n", + "Embedding : bootstrap-table/bootstrap-table.min.js\n", + "Embedding : bootstrap-table/bootstrap-table.min.css\n", + "Embedding : bootstrap-table/bootstrap-table-copy-rows.min.js\n", + "Embedding : bootstrap-table/bootstrap-table-export.min.js\n", + "Embedding : bootstrap-table/tableExport.min.js\n", + "Embedding : bootstrap-icons/bootstrap-icons.mne.min.css\n", + "Embedding : highlightjs/highlight.min.js\n", + "Embedding : highlightjs/atom-one-dark-reasonable.min.css\n", + "Using pyvistaqt 3d backend.\n", + "Using sample-head-dense.fif for head surface.\n", + " 1 BEM surfaces found\n", + " Reading a surface...\n", + "[done]\n", + " 1 BEM surfaces read\n", + "Getting helmet for system 306m\n", + "Channel types::\tgrad: 203, mag: 102, eeg: 59\n", + "Projecting sensors to the head surface\n", + "Using surface from C:\\Users\\ferat\\mne_data\\MNE-testing-data\\subjects\\sample\\bem\\sample-head-dense.fif.\n" ] - } - ], - "source": [ - "import mne\n", - "\n", - "fwd = mne.read_forward_solution(fwd_fname)\n", - "if fwd['coord_frame'] == 'head':\n", - " src = fwd[\"src\"]\n", - " src_" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [ + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "C:\\Users\\ferat\\AppData\\Local\\Temp\\ipykernel_30988\\2410195523.py:2: RuntimeWarning: Distances could not be calculated from digitized points: 'dig'\n", + " report.add_forward(fwd, title=\"Forward\")\n" + ] + }, { "name": "stdout", "output_type": "stream", "text": [ - "Using default location ~/mne_data for sample...\n" + "Using surface: C:\\Users\\ferat\\mne_data\\MNE-testing-data\\subjects\\sample\\bem\\inner_skull.surf\n", + "Using surface: C:\\Users\\ferat\\mne_data\\MNE-testing-data\\subjects\\sample\\bem\\outer_skull.surf\n", + "Using surface: C:\\Users\\ferat\\mne_data\\MNE-testing-data\\subjects\\sample\\bem\\outer_skin.surf\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ - "Downloading file 'MNE-sample-data-processed.tar.gz' from 'https://osf.io/86qa2/download?version=6' to 'C:\\Users\\ferat\\mne_data'.\n", - " 33%|############5 | 544M/1.65G [00:26<00:54, 20.2MB/s]" + "C:\\Users\\ferat\\AppData\\Local\\Temp\\ipykernel_30988\\2410195523.py:2: RuntimeWarning: Distances could not be calculated from digitized points: 'info'\n", + " report.add_forward(fwd, title=\"Forward\")\n" ] } ], "source": [ - "SUBJECT_DIR = Path(mne.datasets.sample.data_path()) / \"subjects\"\n", - "mne.viz.plot_alignment(subject=None, trans=fwd[\"mri_head_t\"], info=fwd[\"info\"], src=fwd[\"src\"], eeg=dict(original=0.2, projected=0.8), subjects_dir=SUBJECT_DIR)" + "report = mne.Report(verbose=True, subject=\"sample\", subjects_dir=subjects_dir)\n", + "report.add_forward(fwd, title=\"Forward\")" ] }, { "cell_type": "code", - "execution_count": 27, + "execution_count": 3, "metadata": {}, "outputs": [ { - "data": { - "text/plain": [ - "'sample'" - ] - }, - "execution_count": 27, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - " fwd[\"src\"]._subject" - ] - }, - { - "cell_type": "code", - "execution_count": 17, - "metadata": {}, - "outputs": [ + "name": "stdout", + "output_type": "stream", + "text": [ + "Overwriting existing file.\n", + "Saving report to : C:\\Users\\ferat\\Documents\\GitHub\\mne-python\\test_report.html\n" + ] + }, { "data": { "text/plain": [ - "head>\n", - "[[ 0.99930954 0.00998479 -0.03578702 -0.00316745]\n", - " [ 0.01275934 0.81240475 0.58295429 0.00685511]\n", - " [ 0.0348942 -0.58300853 0.81171638 0.02888404]\n", - " [ 0. 0. 0. 1. ]]" + "'C:\\\\Users\\\\ferat\\\\Documents\\\\GitHub\\\\mne-python\\\\test_report.html'" ] }, - "execution_count": 17, + "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "fwd[\"mri_head_t\"]" + "report.save(\"test_report.html\", overwrite=True)" ] }, { "cell_type": "code", - "execution_count": 12, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Using surface: C:\\Users\\victor.ferat\\mne_data\\MNE-testing-data\\subjects\\sample\\bem\\inner_skull.surf\n", - "Using surface: C:\\Users\\victor.ferat\\mne_data\\MNE-testing-data\\subjects\\sample\\bem\\outer_skull.surf\n", - "Using surface: C:\\Users\\victor.ferat\\mne_data\\MNE-testing-data\\subjects\\sample\\bem\\outer_skin.surf\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "419MB [65:05:16, 1.79kB/s]\n" - ] - }, - { - "ename": "ValueError", - "evalue": "Source space must be in MRI coordinates, got head", - "output_type": "error", - "traceback": [ - "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)", - "Cell \u001b[1;32mIn[12], line 10\u001b[0m\n\u001b[0;32m 1\u001b[0m plot_bem_kwargs \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mdict\u001b[39m(\n\u001b[0;32m 2\u001b[0m subject\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124msample\u001b[39m\u001b[38;5;124m\"\u001b[39m,\n\u001b[0;32m 3\u001b[0m subjects_dir\u001b[38;5;241m=\u001b[39msubjects_dir,\n\u001b[1;32m (...)\u001b[0m\n\u001b[0;32m 7\u001b[0m \n\u001b[0;32m 8\u001b[0m )\n\u001b[1;32m---> 10\u001b[0m \u001b[43mmne\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mviz\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mplot_bem\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mplot_bem_kwargs\u001b[49m\u001b[43m)\u001b[49m\n", - "File \u001b[1;32mc:\\Users\\victor.ferat\\Documents\\GitHub\\mne-python\\mne\\viz\\misc.py:722\u001b[0m, in \u001b[0;36mplot_bem\u001b[1;34m(subject, subjects_dir, orientation, slices, brain_surfaces, src, show, show_indices, mri, show_orientation)\u001b[0m\n\u001b[0;32m 716\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mOSError\u001b[39;00m(\n\u001b[0;32m 717\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mNo surface files found. Surface files must end with \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m 718\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124minner_skull.surf, outer_skull.surf or outer_skin.surf\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m 719\u001b[0m )\n\u001b[0;32m 721\u001b[0m \u001b[38;5;66;03m# Plot the contours\u001b[39;00m\n\u001b[1;32m--> 722\u001b[0m fig \u001b[38;5;241m=\u001b[39m \u001b[43m_plot_mri_contours\u001b[49m\u001b[43m(\u001b[49m\n\u001b[0;32m 723\u001b[0m \u001b[43m \u001b[49m\u001b[43mmri_fname\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mmri_fname\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m 724\u001b[0m \u001b[43m \u001b[49m\u001b[43msurfaces\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43msurfaces\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m 725\u001b[0m \u001b[43m \u001b[49m\u001b[43msrc\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43msrc\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m 726\u001b[0m \u001b[43m \u001b[49m\u001b[43morientation\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43morientation\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m 727\u001b[0m \u001b[43m \u001b[49m\u001b[43mslices\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mslices\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m 728\u001b[0m \u001b[43m \u001b[49m\u001b[43mshow\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mshow\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m 729\u001b[0m \u001b[43m \u001b[49m\u001b[43mshow_indices\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mshow_indices\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m 730\u001b[0m \u001b[43m \u001b[49m\u001b[43mshow_orientation\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mshow_orientation\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m 731\u001b[0m \u001b[43m \u001b[49m\u001b[43mslices_as_subplots\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43;01mTrue\u001b[39;49;00m\u001b[43m,\u001b[49m\n\u001b[0;32m 732\u001b[0m \u001b[43m\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m 733\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m fig\n", - "File \u001b[1;32mc:\\Users\\victor.ferat\\Documents\\GitHub\\mne-python\\mne\\viz\\misc.py:444\u001b[0m, in \u001b[0;36m_plot_mri_contours\u001b[1;34m(mri_fname, surfaces, src, orientation, slices, show, show_indices, show_orientation, width, slices_as_subplots)\u001b[0m\n\u001b[0;32m 442\u001b[0m \u001b[38;5;66;03m# Eventually we can relax this by allowing ``trans`` if need be\u001b[39;00m\n\u001b[0;32m 443\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m src[\u001b[38;5;241m0\u001b[39m][\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mcoord_frame\u001b[39m\u001b[38;5;124m\"\u001b[39m] \u001b[38;5;241m!=\u001b[39m FIFF\u001b[38;5;241m.\u001b[39mFIFFV_COORD_MRI:\n\u001b[1;32m--> 444\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\n\u001b[0;32m 445\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mSource space must be in MRI coordinates, got \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m 446\u001b[0m \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;132;01m{\u001b[39;00m_frame_to_str[src[\u001b[38;5;241m0\u001b[39m][\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mcoord_frame\u001b[39m\u001b[38;5;124m\"\u001b[39m]]\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m'\u001b[39m\n\u001b[0;32m 447\u001b[0m )\n\u001b[0;32m 448\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m src_ \u001b[38;5;129;01min\u001b[39;00m src:\n\u001b[0;32m 449\u001b[0m points \u001b[38;5;241m=\u001b[39m src_[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mrr\u001b[39m\u001b[38;5;124m\"\u001b[39m][src_[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124minuse\u001b[39m\u001b[38;5;124m\"\u001b[39m]\u001b[38;5;241m.\u001b[39mastype(\u001b[38;5;28mbool\u001b[39m)]\n", - "\u001b[1;31mValueError\u001b[0m: Source space must be in MRI coordinates, got head" - ] - } - ], - "source": [ - "plot_bem_kwargs = dict(\n", - " subject=\"sample\",\n", - " subjects_dir=subjects_dir,\n", - " brain_surfaces=\"white\",\n", - " orientation=\"coronal\",\n", - " src=src,\n", - "\n", - ")\n", - "\n", - "mne.viz.plot_bem(**plot_bem_kwargs)" - ] - }, - { - "cell_type": "code", - "execution_count": 14, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "ename": "TypeError", - "evalue": "'SourceEstimate' object is not subscriptable", - "output_type": "error", - "traceback": [ - "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", - "Cell \u001b[1;32mIn[14], line 2\u001b[0m\n\u001b[0;32m 1\u001b[0m stc \u001b[38;5;241m=\u001b[39m mne\u001b[38;5;241m.\u001b[39mread_source_estimate(stc_fname)\n\u001b[1;32m----> 2\u001b[0m \u001b[43mstc\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43msrc\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m]\u001b[49m\n", - "\u001b[1;31mTypeError\u001b[0m: 'SourceEstimate' object is not subscriptable" - ] - } - ], - "source": [ - "stc = mne.read_source_estimate(stc_fname)\n", - "stc" - ] + "outputs": [], + "source": [] } ], "metadata": { From aac99fbcbd0e6945a65dd49827bf6d24af347453 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 17 Mar 2025 15:35:05 +0000 Subject: [PATCH 18/22] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- mne/report/report.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/mne/report/report.py b/mne/report/report.py index a12551edb06..7929c970104 100644 --- a/mne/report/report.py +++ b/mne/report/report.py @@ -3609,7 +3609,7 @@ def _add_forward( alpha=0.5, max_width=self.img_max_width, max_res=self.img_max_res, - **kwargs + **kwargs, ) self._add_image( img=img, @@ -3642,9 +3642,9 @@ def _add_forward( ) if src.kind == "surface" or src.kind == "mixed": - surfaces=dict(head=0.1, white=0.5) + surfaces = dict(head=0.1, white=0.5) else: - surfaces=dict(head=0.1) + surfaces = dict(head=0.1) kwargs = dict( trans=trans, @@ -3660,7 +3660,7 @@ def _add_forward( alpha=0.5, max_width=self.img_max_width, max_res=self.img_max_res, - **kwargs + **kwargs, ) self._add_image( img=img, From 5d9ef5277cac7ef72ea5a4b76e787428c0c7b576 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Victor=20F=C3=A9rat?= Date: Mon, 14 Apr 2025 13:29:33 +0200 Subject: [PATCH 19/22] Edit views --- mne/report/report.py | 4 +- mne/viz/_brain/view.py | 21 +++- test.ipynb | 218 +++++++++++++++++++++++++++++------------ 3 files changed, 179 insertions(+), 64 deletions(-) diff --git a/mne/report/report.py b/mne/report/report.py index a12551edb06..90fcb773b94 100644 --- a/mne/report/report.py +++ b/mne/report/report.py @@ -556,7 +556,7 @@ def _itv(function, fig, *, max_width=MAX_IMG_WIDTH, max_res=MAX_IMG_RES, **kwarg function(fig=fig, **kwargs) - views = ("frontal", "lateral", "medial", "axial", "rostral", "coronal") + views = ("lateral_r", "frontlat_r", "frontal", "frontlat_l", "lateral_l", "top", "backlat_r", "back", "backlat_l", "bot") images = [] for view in views: @@ -569,7 +569,7 @@ def _itv(function, fig, *, max_width=MAX_IMG_WIDTH, max_res=MAX_IMG_RES, **kwarg images.append(im) images = np.concatenate( - [np.concatenate(images[:3], axis=1), np.concatenate(images[3:], axis=1)], axis=0 + [np.concatenate(images[:5], axis=1), np.concatenate(images[5:], axis=1)], axis=0 ) try: diff --git a/mne/viz/_brain/view.py b/mne/viz/_brain/view.py index 1550c3868fb..6ce3c48623b 100644 --- a/mne/viz/_brain/view.py +++ b/mne/viz/_brain/view.py @@ -35,6 +35,21 @@ azimuth=180.0, elevation=0.0, focalpoint=ORIGIN, roll=0, distance=DIST ), } + +_both_views_dict = { + "lateral_r": dict(azimuth=180.0, elevation=90.0, focalpoint=ORIGIN, distance=DIST), + "frontlat_r": dict(azimuth=120.0, elevation=90.0, focalpoint=ORIGIN, distance=DIST), + "frontal": dict(azimuth=90.0, elevation=90.0, focalpoint=ORIGIN, distance=DIST), + "frontlat_l": dict(azimuth=60.0, elevation=90.0, focalpoint=ORIGIN, distance=DIST), + "lateral_l": dict(azimuth=180.0, elevation=-90.0, focalpoint=ORIGIN, distance=DIST), + "backlat_r": dict(azimuth=-120.0, elevation=90.0, focalpoint=ORIGIN, distance=DIST), + "back": dict(azimuth=90.0, elevation=-90.0, focalpoint=ORIGIN, distance=DIST), + "backlat_l": dict(azimuth=-60.0, elevation=90.0, focalpoint=ORIGIN, distance=DIST), + "top": dict(azimuth=180.0, elevation=0.0, focalpoint=ORIGIN, distance=DIST), + "bot": dict(azimuth=180, elevation=180, focalpoint=ORIGIN, distance=DIST), + +} + # add short-size version entries into the dict lh_views_dict = _lh_views_dict.copy() for k, v in _lh_views_dict.items(): @@ -49,6 +64,10 @@ rh_views_dict["flat"] = dict( azimuth=0, elevation=0, focalpoint=ORIGIN, roll=0, distance=DIST ) + +both_views_dict = _both_views_dict.copy() + + views_dicts = dict( - lh=lh_views_dict, vol=lh_views_dict, both=lh_views_dict, rh=rh_views_dict + lh=lh_views_dict, vol=lh_views_dict, both=both_views_dict, rh=rh_views_dict ) diff --git a/test.ipynb b/test.ipynb index e4360035dce..f62ad765fca 100644 --- a/test.ipynb +++ b/test.ipynb @@ -2,7 +2,7 @@ "cells": [ { "cell_type": "code", - "execution_count": 1, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -179,9 +179,9 @@ "\n", "\n", "\n", - "\n", + " onclick=\"toggleVisibility('general-748d5219-9f6e-45c7-bfd9-548e186727ac')\">\n", " \n", "