def run_report_source(*, cfg: SimpleNamespace, subject: str, session: Optional[str] = None, report: mne.Report) -> mne.Report: import matplotlib.pyplot as plt msg = 'Generating source-space analysis report …' logger.info( **gen_log_kwargs(message=msg, subject=subject, session=session)) if report is None: report = _gen_empty_report(cfg=cfg, subject=subject, session=session) bids_path = BIDSPath(subject=subject, session=session, task=cfg.task, acquisition=cfg.acq, run=None, recording=cfg.rec, space=cfg.space, extension='.fif', datatype=cfg.datatype, root=cfg.deriv_root, check=False) # Use this as a source for the Info dictionary fname_info = bids_path.copy().update(processing='clean', suffix='epo') fname_trans = bids_path.copy().update(suffix='trans') if not fname_trans.fpath.exists(): msg = 'No coregistration found, skipping source space report.' logger.info( **gen_log_kwargs(message=msg, subject=subject, session=session)) return report ########################################################################### # # Visualize the coregistration & inverse solutions. # if cfg.conditions is None: conditions = [] elif isinstance(cfg.conditions, dict): conditions = list(cfg.conditions.keys()) else: conditions = cfg.conditions.copy() conditions.extend(cfg.contrasts) msg = 'Rendering MRI slices with BEM contours.' logger.info( **gen_log_kwargs(message=msg, subject=subject, session=session)) report.add_bem(subject=cfg.fs_subject, subjects_dir=cfg.fs_subjects_dir, title='BEM', width=256, decim=8) msg = 'Rendering sensor alignment (coregistration).' logger.info( **gen_log_kwargs(message=msg, subject=subject, session=session)) report.add_trans( trans=fname_trans, info=fname_info, title='Sensor alignment', subject=cfg.fs_subject, subjects_dir=cfg.fs_subjects_dir, ) for condition in conditions: msg = f'Rendering inverse solution for {condition}' logger.info( **gen_log_kwargs(message=msg, subject=subject, session=session)) if condition in cfg.conditions: title = f'Source: {config.sanitize_cond_name(condition)}' else: # It's a contrast of two conditions. # XXX Will change once we process contrasts here too continue method = cfg.inverse_method cond_str = config.sanitize_cond_name(condition) inverse_str = method hemi_str = 'hemi' # MNE will auto-append '-lh' and '-rh'. fname_stc = bids_path.copy().update( suffix=f'{cond_str}+{inverse_str}+{hemi_str}', extension=None) tags = ('source-estimate', condition.lower().replace(' ', '-')) if Path(f'{fname_stc.fpath}-lh.stc').exists(): report.add_stc(stc=fname_stc, title=title, subject=cfg.fs_subject, subjects_dir=cfg.fs_subjects_dir, tags=tags) plt.close('all') # close all figures to save memory return report