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 1/7] 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 2/7] [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 3/7] 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 4/7] 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 5/7] 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 6/7] 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 7/7] [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, *,