From 5767188a4b889e68d7d5ba4085b01b7c8281dea4 Mon Sep 17 00:00:00 2001 From: nordme Date: Fri, 31 Mar 2023 12:34:40 -0700 Subject: [PATCH 01/11] remove mne.fixes --- mnefun/_report.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/mnefun/_report.py b/mnefun/_report.py index 4d305ba..31eeccc 100644 --- a/mnefun/_report.py +++ b/mnefun/_report.py @@ -375,9 +375,6 @@ def gen_html_report(p, subjects, structurals, run_indices=None): import matplotlib.pyplot as plt if run_indices is None: run_indices = [None] * len(subjects) - time_kwargs = dict() - if 'time_unit' in mne.fixes._get_args(mne.viz.plot_evoked): - time_kwargs['time_unit'] = 's' known_keys = { 'good_hpi_count', 'chpi_snr', 'head_movement', 'raw_segments', 'psd', 'ssp_topomaps', 'source_alignment', 'drop_log', 'bem', 'covariance', From 425fd56865c645b37135c2872c0cb4f8556395af Mon Sep 17 00:00:00 2001 From: nordme Date: Fri, 31 Mar 2023 13:54:04 -0700 Subject: [PATCH 02/11] psd fix --- mnefun/_report.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/mnefun/_report.py b/mnefun/_report.py index 31eeccc..8b2a828 100644 --- a/mnefun/_report.py +++ b/mnefun/_report.py @@ -267,6 +267,17 @@ def _report_raw_segments(report, raw, lowpass=None): print('%5.1f sec' % ((time.time() - t0),)) +def _gen_psd_plot(raw, fmax, n_fft, ax): + try: + from mne.time_frequency import Spectrum + except ImportError: + plot = raw.plot_psd(fmax=fmax, n_fft=n_fft, show=False, ax=ax) + else: + plot = raw.compute_psd(fmax=fmax, n_fft=n_fft).plot(show=False, + axes=ax) + return plot + + def _report_raw_psd(report, raw, raw_pca=None, raw_erm=None, raw_erm_pca=None, p=None): t0 = time.time() @@ -283,7 +294,7 @@ def _report_raw_psd(report, raw, raw_pca=None, raw_erm=None, raw_erm_pca=None, fmax = raw.info['lowpass'] n_ax = sum(key in raw for key in ('mag', 'grad', 'eeg')) _, ax = plt.subplots(n_ax, figsize=(10, 8)) - figs = [raw.plot_psd(fmax=fmax, n_fft=n_fft, show=False, ax=ax)] + figs = [_gen_psd_plot(raw, fmax=fmax, n_fft=n_fft, ax=ax)] captions = ['%s: Raw' % section] fmax = lp_cut + 2 * lp_trans for this_raw, caption in [ @@ -293,8 +304,7 @@ def _report_raw_psd(report, raw, raw_pca=None, raw_erm=None, raw_erm_pca=None, (raw_erm_pca, f'{section}: ERM processed (zoomed)')]: _, ax = plt.subplots(n_ax, figsize=(10, 8)) if this_raw is not None: - figs.append( - this_raw.plot_psd(fmax=fmax, n_fft=n_fft, show=False, ax=ax)) + figs.append(_gen_psd_plot(this_raw, fmax=fmax, n_fft=n_fft, ax=ax)) captions.append(caption) # shared y limits n = len(figs[0].axes) // 2 From e7a1ee72aa64a8f807976b370320b97f8b035bb2 Mon Sep 17 00:00:00 2001 From: nordme Date: Fri, 31 Mar 2023 14:28:12 -0700 Subject: [PATCH 03/11] flake fix --- mnefun/_report.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mnefun/_report.py b/mnefun/_report.py index 8b2a828..f9cb241 100644 --- a/mnefun/_report.py +++ b/mnefun/_report.py @@ -273,7 +273,7 @@ def _gen_psd_plot(raw, fmax, n_fft, ax): except ImportError: plot = raw.plot_psd(fmax=fmax, n_fft=n_fft, show=False, ax=ax) else: - plot = raw.compute_psd(fmax=fmax, n_fft=n_fft).plot(show=False, + plot = raw.compute_psd(fmax=fmax, n_fft=n_fft).plot(show=False, axes=ax) return plot From c100cdc47a457dbc59a6b4a27df168c6b5989265 Mon Sep 17 00:00:00 2001 From: nordme Date: Fri, 31 Mar 2023 14:48:44 -0700 Subject: [PATCH 04/11] fix mne-python version check for flake --- mnefun/_report.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/mnefun/_report.py b/mnefun/_report.py index f9cb241..3f0ec8a 100644 --- a/mnefun/_report.py +++ b/mnefun/_report.py @@ -268,13 +268,11 @@ def _report_raw_segments(report, raw, lowpass=None): def _gen_psd_plot(raw, fmax, n_fft, ax): - try: - from mne.time_frequency import Spectrum - except ImportError: - plot = raw.plot_psd(fmax=fmax, n_fft=n_fft, show=False, ax=ax) - else: + if hasattr(mne.time_frequency.Spectrum, 'plot'): plot = raw.compute_psd(fmax=fmax, n_fft=n_fft).plot(show=False, axes=ax) + else: + plot = raw.plot_psd(fmax=fmax, n_fft=n_fft, show=False, ax=ax) return plot From bb9b0f53c3356b82415233e8b89e341887b656ca Mon Sep 17 00:00:00 2001 From: nordme Date: Fri, 31 Mar 2023 18:18:08 -0700 Subject: [PATCH 05/11] use matplotlib backend --- mnefun/_report.py | 1 + 1 file changed, 1 insertion(+) diff --git a/mnefun/_report.py b/mnefun/_report.py index 3f0ec8a..a93e3ad 100644 --- a/mnefun/_report.py +++ b/mnefun/_report.py @@ -40,6 +40,7 @@ @contextmanager def report_context(): """Create a context for making plt and mlab figures.""" + mne.viz.set_browser_backend('matplotlib') import matplotlib import matplotlib.pyplot as plt style = {'axes.spines.right': 'off', 'axes.spines.top': 'off', From 935448bd3248e358f4cde29c4a9c64f470ad628d Mon Sep 17 00:00:00 2001 From: nordme Date: Sun, 2 Apr 2023 15:34:32 -0700 Subject: [PATCH 06/11] temporarily set backend --- mnefun/_report.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/mnefun/_report.py b/mnefun/_report.py index a93e3ad..44a7d37 100644 --- a/mnefun/_report.py +++ b/mnefun/_report.py @@ -40,7 +40,6 @@ @contextmanager def report_context(): """Create a context for making plt and mlab figures.""" - mne.viz.set_browser_backend('matplotlib') import matplotlib import matplotlib.pyplot as plt style = {'axes.spines.right': 'off', 'axes.spines.top': 'off', @@ -49,13 +48,14 @@ def report_context(): plt.ioff() old_backend = matplotlib.get_backend() matplotlib.use('Agg', force=True) - try: - with plt.style.context(style): - yield - except Exception: - matplotlib.use(old_backend, force=True) - plt.interactive(is_interactive) - raise + with mne.viz.use_browser_backend('matplotlib'): + try: + with plt.style.context(style): + yield + except Exception: + matplotlib.use(old_backend, force=True) + plt.interactive(is_interactive) + raise # Backward compat wrappers for MNE 1.0+ From aa259c36946bab492a2ebc90988fb34771268911 Mon Sep 17 00:00:00 2001 From: nordme Date: Mon, 3 Apr 2023 12:09:43 -0700 Subject: [PATCH 07/11] 1.4 fixes: posix path and vlim arg format --- mnefun/_forward.py | 2 +- mnefun/_report.py | 23 ++++++++++++++++------- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/mnefun/_forward.py b/mnefun/_forward.py index 7300bc5..c3cbdb2 100644 --- a/mnefun/_forward.py +++ b/mnefun/_forward.py @@ -74,7 +74,7 @@ def gen_forwards(p, subjects, structurals, run_indices): def _get_bem_src_trans(p, info, subj, struc): - subjects_dir = get_subjects_dir(p.subjects_dir, raise_error=True) + subjects_dir = str(get_subjects_dir(p.subjects_dir, raise_error=True)) assert isinstance(subjects_dir, str) if struc is None: # spherical case bem, src, trans = _spherical_conductor(info, subj, p.src_pos) diff --git a/mnefun/_report.py b/mnefun/_report.py index 44a7d37..4275bf2 100644 --- a/mnefun/_report.py +++ b/mnefun/_report.py @@ -974,13 +974,22 @@ def gen_html_report(p, subjects, structurals, run_indices=None): this_evoked.add_proj(all_proj) if this_evoked.nave > 0: with mne.utils.use_log_level('error'): - fig = this_evoked.plot_joint( - times, show=False, picks=picks, - ts_args=dict(proj=proj), - topomap_args=dict( - outlines='head', - vmin=min_, vmax=max_, - cmap=cmap, proj=proj)) + try: + fig = this_evoked.plot_joint( + times, show=False, picks=picks, + ts_args=dict(proj=proj), + topomap_args=dict( + outlines='head', + vlim=(min_, max_), + cmap=cmap, proj=proj)) + except TypeError: + fig = this_evoked.plot_joint( + times, show=False, picks=picks, + ts_args=dict(proj=proj), + topomap_args=dict( + outlines='head', + vmin=min_, vmax=max_, + cmap=cmap, proj=proj)) assert isinstance(fig, plt.Figure) fig.axes[0].set(ylim=(-max_, max_)) t = fig.axes[-1].texts[0] From 0224406d92e47556a5ae1eb10b032bb35cc1bf46 Mon Sep 17 00:00:00 2001 From: nordme Date: Mon, 3 Apr 2023 12:58:23 -0700 Subject: [PATCH 08/11] use png --- mnefun/_report.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mnefun/_report.py b/mnefun/_report.py index 4275bf2..d239150 100644 --- a/mnefun/_report.py +++ b/mnefun/_report.py @@ -660,7 +660,7 @@ def gen_html_report(p, subjects, structurals, run_indices=None): figs = [epo.plot_drop_log(subject=subj, show=False)] captions = [repr(epo)] _add_figs_to_section( - report, figs, captions, section, image_format='svg') + report, figs, captions, section, image_format='png') print('%5.1f sec' % ((time.time() - t0),)) else: print(' %s skipped' % section) From 2ab3c9c50570c95a8d27c0772b589fe04515275b Mon Sep 17 00:00:00 2001 From: nordme Date: Mon, 3 Apr 2023 15:26:18 -0700 Subject: [PATCH 09/11] set default image format using mne.Report attribute --- mnefun/_report.py | 67 +++++++++++++++++++++++++++-------------------- 1 file changed, 39 insertions(+), 28 deletions(-) diff --git a/mnefun/_report.py b/mnefun/_report.py index d239150..a5dbcb7 100644 --- a/mnefun/_report.py +++ b/mnefun/_report.py @@ -18,7 +18,7 @@ plot_events) from mne.viz._3d import plot_head_positions from mne.report import Report -from mne.utils import _pl, use_log_level +from mne.utils import _pl, use_log_level, logger from mne.cov import whiten_evoked from mne.viz.utils import _triage_rank_sss @@ -59,7 +59,7 @@ def report_context(): # Backward compat wrappers for MNE 1.0+ -def _add_figs_to_section(report, figs, captions, section, image_format='png'): +def _add_figs_to_section(report, figs, captions, section, image_format='webp'): try: report.add_figure except AttributeError: @@ -83,7 +83,7 @@ def _add_figs_to_section(report, figs, captions, section, image_format='png'): def _add_slider_to_section(report, figs, captions, section, title, - image_format='png'): + image_format='webp'): try: report.add_figure except AttributeError: @@ -114,7 +114,7 @@ def _check_fname_raw(fname, p, subj): return fname, raw -def _report_good_hpi(report, fnames, p=None, subj=None): +def _report_good_hpi(report, fnames, p=None, subj=None, img_form='webp'): t0 = time.time() section = 'Good HPI count' print((' %s ... ' % section).ljust(LJUST), end='') @@ -133,11 +133,11 @@ def _report_good_hpi(report, fnames, p=None, subj=None): figs.append(fig) captions.append('%s: %s' % (section, op.basename(fname))) _add_figs_to_section( - report, figs, captions, section, image_format='png') + report, figs, captions, section, image_format=img_form) print('%5.1f sec' % ((time.time() - t0),)) -def _report_chpi_snr(report, fnames, p=None, subj=None): +def _report_chpi_snr(report, fnames, p=None, subj=None, img_form='webp'): t0 = time.time() section = 'cHPI SNR' print((' %s ... ' % section).ljust(LJUST), end='') @@ -156,11 +156,12 @@ def _report_chpi_snr(report, fnames, p=None, subj=None): figs.append(fig) captions.append('%s: %s' % (section, op.basename(fname))) _add_figs_to_section( - report, figs, captions, section, image_format='png') # svd too slow + report, figs, captions, section, image_format=img_form) # svd too slow print('%5.1f sec' % ((time.time() - t0),)) -def _report_head_movement(report, fnames, p=None, subj=None, run_indices=None): +def _report_head_movement(report, fnames, p=None, subj=None, run_indices=None, + img_form='webp'): section = 'Head movement' print((' %s ... ' % section).ljust(LJUST), end='') t0 = time.time() @@ -201,11 +202,12 @@ def _report_head_movement(report, fnames, p=None, subj=None, run_indices=None): figs.append(fig) captions.append('%s: %s' % (section, op.basename(fname))) del trans_to - _add_figs_to_section(report, figs, captions, section, image_format='png') + _add_figs_to_section(report, figs, captions, section, + image_format=img_form) print('%5.1f sec' % ((time.time() - t0),)) -def _report_events(report, fnames, p=None, subj=None): +def _report_events(report, fnames, p=None, subj=None, img_form='webp'): t0 = time.time() section = 'Events' print((' %s ... ' % section).ljust(LJUST), end='') @@ -223,11 +225,11 @@ def _report_events(report, fnames, p=None, subj=None): captions.append('%s: %s' % (section, op.basename(fname))) if len(figs): _add_figs_to_section( - report, figs, captions, section, image_format='png') + report, figs, captions, section, image_format=img_form) print('%5.1f sec' % ((time.time() - t0),)) -def _report_raw_segments(report, raw, lowpass=None): +def _report_raw_segments(report, raw, lowpass=None, img_form='webp'): t0 = time.time() section = 'Raw segments' print((' %s ... ' % section).ljust(LJUST), end='') @@ -264,7 +266,7 @@ def _report_raw_segments(report, raw, lowpass=None): fig.set(figheight=(fig.axes[0].get_yticks() != 0).sum(), figwidth=12) fig.subplots_adjust(0.025, 0.0, 1, 1, 0, 0) - _add_figs_to_section(report, fig, section, section, image_format='png') + _add_figs_to_section(report, fig, section, section, image_format=img_form) print('%5.1f sec' % ((time.time() - t0),)) @@ -278,7 +280,7 @@ def _gen_psd_plot(raw, fmax, n_fft, ax): def _report_raw_psd(report, raw, raw_pca=None, raw_erm=None, raw_erm_pca=None, - p=None): + p=None, img_form='webp'): t0 = time.time() section = 'PSD' import matplotlib.pyplot as plt @@ -315,7 +317,7 @@ def _report_raw_psd(report, raw, raw_pca=None, raw_erm=None, raw_erm_pca=None, ax.set_ylim(ylims) ax.set(title='') _add_figs_to_section( - report, figs, captions, section, image_format='png') + report, figs, captions, section, image_format=img_form) print('%5.1f sec' % ((time.time() - t0),)) @@ -451,6 +453,12 @@ def gen_html_report(p, subjects, structurals, run_indices=None): subj + p.inv_tag + '-fwd.fif')) with report_context(): + # + # Set report image format + # + img_form = report.image_format + logger.info(f'Setting default image format to {img_form}.') + # # Custom pre-fun # @@ -465,7 +473,7 @@ def gen_html_report(p, subjects, structurals, run_indices=None): # Head coils # if p.report_params.get('good_hpi_count', True) and p.movecomp: - _report_good_hpi(report, fnames, p, subj) + _report_good_hpi(report, fnames, p, subj, img_form=img_form) else: print(' HPI count skipped') @@ -473,7 +481,7 @@ def gen_html_report(p, subjects, structurals, run_indices=None): # cHPI SNR # if p.report_params.get('chpi_snr', True) and p.movecomp: - _report_chpi_snr(report, fnames, p, subj) + _report_chpi_snr(report, fnames, p, subj, img_form=img_form) else: print(' cHPI SNR skipped') @@ -481,7 +489,8 @@ def gen_html_report(p, subjects, structurals, run_indices=None): # Head movement # if p.report_params.get('head_movement', True) and p.movecomp: - _report_head_movement(report, fnames, p, subj, run_indices[si]) + _report_head_movement(report, fnames, p, subj, run_indices[si], + img_form=img_form) else: print(' Head movement skipped') @@ -490,7 +499,7 @@ def gen_html_report(p, subjects, structurals, run_indices=None): # if p.report_params.get('raw_segments', True) and \ raw_pca is not None: - _report_raw_segments(report, raw_pca) + _report_raw_segments(report, raw_pca, img_form=img_form) else: print(' Raw segments skipped') @@ -498,7 +507,8 @@ def gen_html_report(p, subjects, structurals, run_indices=None): # PSD # if p.report_params.get('psd', True): - _report_raw_psd(report, raw, raw_pca, raw_erm, raw_erm_pca, p) + _report_raw_psd(report, raw, raw_pca, raw_erm, raw_erm_pca, p, + img_form=img_form) else: print(' PSD skipped') @@ -559,7 +569,7 @@ def gen_html_report(p, subjects, structurals, run_indices=None): duration)) captions = ['SSP epochs: %s' % c for c in comments] _add_figs_to_section( - report, figs, captions, section, image_format='png') + report, figs, captions, section, image_format=img_form) print('%5.1f sec' % ((time.time() - t0),)) else: print(' %s skipped' % section) @@ -644,7 +654,8 @@ def gen_html_report(p, subjects, structurals, run_indices=None): from mayavi import mlab mlab.close(fig) view = trim_bg(np.concatenate(view, axis=1), 0) - _add_figs_to_section(report, [view], captions, section) + _add_figs_to_section(report, [view], captions, section, + image_format=img_form) print('%5.1f sec' % ((time.time() - t0),)) else: print(' %s skipped' % section) @@ -660,7 +671,7 @@ def gen_html_report(p, subjects, structurals, run_indices=None): figs = [epo.plot_drop_log(subject=subj, show=False)] captions = [repr(epo)] _add_figs_to_section( - report, figs, captions, section, image_format='png') + report, figs, captions, section, image_format=img_form) print('%5.1f sec' % ((time.time() - t0),)) else: print(' %s skipped' % section) @@ -717,7 +728,7 @@ def gen_html_report(p, subjects, structurals, run_indices=None): for kind in ('images', 'SVDs')] _add_figs_to_section( report, figs, captions, section=section, - image_format='png') + image_format=img_form) print('%5.1f sec' % ((time.time() - t0),)) else: print(' %s skipped' % section) @@ -819,7 +830,7 @@ def gen_html_report(p, subjects, structurals, run_indices=None): figs = [figs] _add_figs_to_section( report, figs, captions, section=section, - image_format='png') + image_format=img_form) print('%5.1f sec' % ((time.time() - t0),)) else: print(' %s skipped' % section) @@ -891,7 +902,7 @@ def gen_html_report(p, subjects, structurals, run_indices=None): figs.tight_layout() _add_figs_to_section( report, figs, captions, section=section, - image_format='png') + image_format=img_form) print('%5.1f sec' % ((time.time() - t0),)) # @@ -1008,7 +1019,7 @@ def gen_html_report(p, subjects, structurals, run_indices=None): title += ' : SSP on' _add_slider_to_section( report, all_figs, all_captions, section=section, - title=title, image_format='png') + title=title, image_format=img_form) del this_evoked, all_evoked print('%5.1f sec' % ((time.time() - t0),)) @@ -1154,7 +1165,7 @@ def gen_html_report(p, subjects, structurals, run_indices=None): print(repr(captions)) _add_slider_to_section( report, figs, captions=captions, section=section, - title=title, image_format='png') + title=title, image_format=img_form) print('%5.1f sec' % ((time.time() - t0),)) else: From bad12c6971baa81cd2be07ea206151e17b941c1d Mon Sep 17 00:00:00 2001 From: Daniel McCloy Date: Tue, 4 Apr 2023 09:15:07 -0500 Subject: [PATCH 10/11] Apply suggestions from code review --- mnefun/_report.py | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/mnefun/_report.py b/mnefun/_report.py index a5dbcb7..b804486 100644 --- a/mnefun/_report.py +++ b/mnefun/_report.py @@ -271,7 +271,7 @@ def _report_raw_segments(report, raw, lowpass=None, img_form='webp'): def _gen_psd_plot(raw, fmax, n_fft, ax): - if hasattr(mne.time_frequency.Spectrum, 'plot'): + if hasattr(raw, 'compute_psd'): plot = raw.compute_psd(fmax=fmax, n_fft=n_fft).plot(show=False, axes=ax) else: @@ -985,22 +985,22 @@ def gen_html_report(p, subjects, structurals, run_indices=None): this_evoked.add_proj(all_proj) if this_evoked.nave > 0: with mne.utils.use_log_level('error'): + topomap_args = dict(outlines='head', + vlim=(min_, max_), + cmap=cmap, proj=proj) + kwargs = dict(show=False, picks=picks, + ts_args=dict(proj=proj), + topomap_args=topomap_args) try: fig = this_evoked.plot_joint( - times, show=False, picks=picks, - ts_args=dict(proj=proj), - topomap_args=dict( - outlines='head', - vlim=(min_, max_), - cmap=cmap, proj=proj)) + times, **kwargs) except TypeError: + # old MNE had separate vmin, vmax + topomap_args.update( + zip(('vmin', 'vmax'), + topomap_args.pop('vlim'))) fig = this_evoked.plot_joint( - times, show=False, picks=picks, - ts_args=dict(proj=proj), - topomap_args=dict( - outlines='head', - vmin=min_, vmax=max_, - cmap=cmap, proj=proj)) + times, **kwargs) assert isinstance(fig, plt.Figure) fig.axes[0].set(ylim=(-max_, max_)) t = fig.axes[-1].texts[0] From 73a3a3c0b0c91c11ce3964404bd02276cefc581e Mon Sep 17 00:00:00 2001 From: Daniel McCloy Date: Tue, 4 Apr 2023 09:21:28 -0500 Subject: [PATCH 11/11] fix whitespace --- mnefun/_report.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mnefun/_report.py b/mnefun/_report.py index b804486..ebb97b0 100644 --- a/mnefun/_report.py +++ b/mnefun/_report.py @@ -998,7 +998,7 @@ def gen_html_report(p, subjects, structurals, run_indices=None): # old MNE had separate vmin, vmax topomap_args.update( zip(('vmin', 'vmax'), - topomap_args.pop('vlim'))) + topomap_args.pop('vlim'))) fig = this_evoked.plot_joint( times, **kwargs) assert isinstance(fig, plt.Figure)