Exemple #1
0
    parser.add_option("-i", "--info", dest="info_fname",
                      help="File from which info dictionary is to be read",
                      metavar="FILE")
    parser.add_option("-d", "--subjects-dir", dest="subjects_dir",
                      help="The subjects directory")
    parser.add_option("-s", "--subject", dest="subject",
                      help="The subject name")
    parser.add_option("-v", "--verbose", dest="verbose",
                      action='store_true', help="run in verbose mode")
    parser.add_option("--no-browser", dest="no_browser", action='store_false',
                      help="Do not open MNE-Report in browser")
    parser.add_option("--overwrite", dest="overwrite", action='store_false',
                      help="Overwrite html report if it already exists")
    parser.add_option("-j", "--jobs", dest="n_jobs", help="Number of jobs to"
                      " run in parallel")

    options, args = parser.parse_args()
    path = options.path
    info_fname = options.info_fname
    subjects_dir = options.subjects_dir
    subject = options.subject
    verbose = True if options.verbose is not None else False
    open_browser = False if options.no_browser is not None else True
    overwrite = True if options.overwrite is not None else False
    n_jobs = int(options.n_jobs) if options.n_jobs is not None else 1

    report = Report(info_fname, subjects_dir=subjects_dir, subject=subject,
                    verbose=verbose)
    report.parse_folder(path, verbose=verbose, n_jobs=n_jobs)
    report.save(open_browser=open_browser, overwrite=overwrite)
Exemple #2
0
def test_render_add_sections():
    """Test adding figures/images to section.
    """
    from PIL import Image
    tempdir = _TempDir()
    import matplotlib.pyplot as plt
    report = Report(subjects_dir=subjects_dir)
    # Check add_figs_to_section functionality
    fig = plt.plot([1, 2], [1, 2])[0].figure
    report.add_figs_to_section(
        figs=fig,  # test non-list input
        captions=['evoked response'],
        scale=1.2,
        image_format='svg')
    assert_raises(ValueError,
                  report.add_figs_to_section,
                  figs=[fig, fig],
                  captions='H')
    assert_raises(ValueError,
                  report.add_figs_to_section,
                  figs=fig,
                  captions=['foo'],
                  scale=0,
                  image_format='svg')
    assert_raises(ValueError,
                  report.add_figs_to_section,
                  figs=fig,
                  captions=['foo'],
                  scale=1e-10,
                  image_format='svg')
    # need to recreate because calls above change size
    fig = plt.plot([1, 2], [1, 2])[0].figure

    # Check add_images_to_section with png and then gif
    img_fname = op.join(tempdir, 'testimage.png')
    fig.savefig(img_fname)
    report.add_images_to_section(fnames=[img_fname],
                                 captions=['evoked response'])

    im = Image.open(img_fname)
    op.join(tempdir, 'testimage.gif')
    im.save(img_fname)  # matplotlib does not support gif
    report.add_images_to_section(fnames=[img_fname],
                                 captions=['evoked response'])

    assert_raises(ValueError,
                  report.add_images_to_section,
                  fnames=[img_fname, img_fname],
                  captions='H')

    assert_raises(ValueError,
                  report.add_images_to_section,
                  fnames=['foobar.xxx'],
                  captions='H')

    evoked = read_evokeds(evoked_fname,
                          condition='Left Auditory',
                          baseline=(-0.2, 0.0))
    fig = plot_trans(evoked.info,
                     trans_fname,
                     subject='sample',
                     subjects_dir=subjects_dir)

    report.add_figs_to_section(
        figs=fig,  # test non-list input
        captions='random image',
        scale=1.2)
    assert_true(repr(report))
Exemple #3
0
def test_render_report():
    """Test rendering -*.fif files for mne report.
    """
    tempdir = _TempDir()
    raw_fname_new = op.join(tempdir, 'temp_raw.fif')
    event_fname_new = op.join(tempdir, 'temp_raw-eve.fif')
    cov_fname_new = op.join(tempdir, 'temp_raw-cov.fif')
    fwd_fname_new = op.join(tempdir, 'temp_raw-fwd.fif')
    inv_fname_new = op.join(tempdir, 'temp_raw-inv.fif')
    for a, b in [[raw_fname, raw_fname_new],
                 [event_fname, event_fname_new],
                 [cov_fname, cov_fname_new],
                 [fwd_fname, fwd_fname_new],
                 [inv_fname, inv_fname_new]]:
        shutil.copyfile(a, b)

    # create and add -epo.fif and -ave.fif files
    epochs_fname = op.join(tempdir, 'temp-epo.fif')
    evoked_fname = op.join(tempdir, 'temp-ave.fif')
    raw = Raw(raw_fname_new)
    picks = pick_types(raw.info, meg='mag', eeg=False)  # faster with one type
    epochs = Epochs(raw, read_events(event_fname), 1, -0.2, 0.2, picks=picks)
    epochs.save(epochs_fname)
    epochs.average().save(evoked_fname)

    report = Report(info_fname=raw_fname_new, subjects_dir=subjects_dir)
    with warnings.catch_warnings(record=True) as w:
        warnings.simplefilter('always')
        report.parse_folder(data_path=tempdir)
    assert_true(len(w) >= 1)

    # Check correct paths and filenames
    fnames = glob.glob(op.join(tempdir, '*.fif'))
    for fname in fnames:
        assert_true(op.basename(fname) in
                    [op.basename(x) for x in report.fnames])
        assert_true(''.join(report.html).find(op.basename(fname)) != -1)

    assert_equal(len(report.fnames), len(fnames))
    assert_equal(len(report.html), len(report.fnames))

    # Check saving functionality
    report.data_path = tempdir
    report.save(fname=op.join(tempdir, 'report.html'), open_browser=False)
    assert_true(op.isfile(op.join(tempdir, 'report.html')))

    assert_equal(len(report.html), len(fnames))
    assert_equal(len(report.html), len(report.fnames))

    # Check saving same report to new filename
    report.save(fname=op.join(tempdir, 'report2.html'), open_browser=False)
    assert_true(op.isfile(op.join(tempdir, 'report2.html')))

    # Check overwriting file
    report.save(fname=op.join(tempdir, 'report.html'), open_browser=False,
                overwrite=True)
    assert_true(op.isfile(op.join(tempdir, 'report.html')))

    # Check pattern matching with multiple patterns
    pattern = ['*raw.fif', '*eve.fif']
    with warnings.catch_warnings(record=True) as w:
        warnings.simplefilter('always')
        report.parse_folder(data_path=tempdir, pattern=pattern)
    assert_true(len(w) >= 1)

    fnames = glob.glob(op.join(tempdir, '*.raw')) + \
        glob.glob(op.join(tempdir, '*.raw'))
    for fname in fnames:
        assert_true(op.basename(fname) in
                    [op.basename(x) for x in report.fnames])
        assert_true(''.join(report.html).find(op.basename(fname)) != -1)
Exemple #4
0
from mne.report import Report
from mne.datasets import sample
from mne import read_evokeds
from matplotlib import pyplot as plt

data_path = sample.data_path()
meg_path = data_path + '/MEG/sample'
subjects_dir = data_path + '/subjects'
evoked_fname = meg_path + '/sample_audvis-ave.fif'

###############################################################################
# Do standard folder parsing (this can take a couple of minutes):

report = Report(image_format='png',
                subjects_dir=subjects_dir,
                info_fname=evoked_fname,
                subject='sample',
                raw_psd=False)  # use False for speed here
report.parse_folder(meg_path, on_error='ignore', mri_decim=10)

###############################################################################
# Add a custom section with an evoked slider:

# Load the evoked data
evoked = read_evokeds(evoked_fname,
                      condition='Left Auditory',
                      baseline=(None, 0),
                      verbose=False)
evoked.crop(0, .2)
times = evoked.times[::4]
# Create a list of figs for the slider
Exemple #5
0
def test_render_add_sections(renderer, tmpdir):
    """Test adding figures/images to section."""
    tempdir = str(tmpdir)
    report = Report(subjects_dir=subjects_dir)
    # Check add_figs_to_section functionality
    fig = plt.plot([1, 2], [1, 2])[0].figure
    report.add_figs_to_section(
        figs=fig,  # test non-list input
        captions=['evoked response'],
        scale=1.2,
        image_format='svg')
    pytest.raises(ValueError,
                  report.add_figs_to_section,
                  figs=[fig, fig],
                  captions='H')
    pytest.raises(ValueError,
                  report.add_figs_to_section,
                  figs=fig,
                  captions=['foo'],
                  scale=0,
                  image_format='svg')
    pytest.raises(ValueError,
                  report.add_figs_to_section,
                  figs=fig,
                  captions=['foo'],
                  scale=1e-10,
                  image_format='svg')
    # need to recreate because calls above change size
    fig = plt.plot([1, 2], [1, 2])[0].figure

    # Check add_images_to_section with png
    img_fname = op.join(tempdir, 'testimage.png')
    fig.savefig(img_fname)
    report.add_images_to_section(fnames=[img_fname],
                                 captions=['evoked response'])

    report.add_images_to_section(fnames=[img_fname],
                                 captions=['evoked response'])

    pytest.raises(ValueError,
                  report.add_images_to_section,
                  fnames=[img_fname, img_fname],
                  captions='H')

    pytest.raises(ValueError,
                  report.add_images_to_section,
                  fnames=['foobar.xxx'],
                  captions='H')

    evoked = read_evokeds(evoked_fname,
                          condition='Left Auditory',
                          baseline=(-0.2, 0.0))
    fig = plot_alignment(evoked.info,
                         trans_fname,
                         subject='sample',
                         subjects_dir=subjects_dir)

    report.add_figs_to_section(
        figs=fig,  # test non-list input
        captions='random image',
        scale=1.2)
    assert (repr(report))
    fname = op.join(str(tmpdir), 'test.html')
    report.save(fname, open_browser=False)
    with open(fname, 'r') as fid:
        html = fid.read()
    assert html.count('<li class="report_custom"') == 8  # several
def check_apply_filter(raw,
                       subject,
                       filter_params=None,
                       notch_filter_params=None,
                       plot_fmin=None,
                       plot_fmax=None,
                       n_jobs=1,
                       figsize=None,
                       report=None,
                       img_scale=1.0):
    """Apply filtering and save diagnostic plots

    Parameters
    ----------
    raw : instance of Raw
        Raw measurements to be decomposed.
    subject : str
        The name of the subject.
    filter_params : dict | list of dict | None
        The parametrs passed to raw.filter. If list, raw.filter will be
        invoked len(filter_params) times. Defaults to None. If None, expands
        to:

        dict(l_freq=0.5, h_freq=200, n_jobs=n_jobs,
             method='fft', l_trans_bandwidth=0.1, h_trans_bandwidth=0.5)
    notch_filter_params : dict | list of dict | None
        The parametrs passed to raw.notch_filter. Defaults to None.
        If None, expands to:
    n_jobs : int
        The number of CPUs to use in parallel.
    figsize : tuple of int
        The figsize in inches. See matplotlib documentation.
    scale_img : float
        The scaling factor for the report. Defaults to 1.0.
    report : instance of Report | None
        The report object. If None, a new report will be generated.
    """
    _default_filter_params = dict(l_freq=0.5,
                                  h_freq=200,
                                  n_jobs=n_jobs,
                                  method='fft',
                                  l_trans_bandwidth=0.1,
                                  h_trans_bandwidth=0.5)
    if filter_params is None:
        filter_params = _default_filter_params
    if not isinstance(filter_params, (list, tuple)):
        filter_params = [filter_params]
    if notch_filter_params is None:
        notch_filter_params = dict(freqs=(
            50,
            100,
            150,
            200,
            250,
        ),
                                   method='fft')
    if report is None:
        report = Report(subject)

    notch_filter_params.update(n_jobs=n_jobs)
    picks_list, n_rows, fig, axes = _prepare_filter_plot(raw, figsize)

    iter_plot = zip(axes, picks_list)
    fmin, fmax = plot_fmin or 0, plot_fmax or raw.info['lowpass'] + 20

    ############################################################################
    # plot before filter
    for ax, (picks, ch_type) in iter_plot:

        raw.plot_psds(fmin=fmin, fmax=fmax, ax=ax, picks=picks, color='black')
        first_line = ax.get_lines()[0]
        first_line.set_label('{} - raw'.format(ch_type))
        ax.set_ylabel('Power (dB)')
        ax.grid(True)
        ax.set_title(ch_type)

    ############################################################################
    # filter
    for filter_params_ in filter_params:
        final_filter_params_ = deepcopy(_default_filter_params)
        final_filter_params_.update(filter_params_)
        final_filter_params_.update({'n_jobs': n_jobs})
        raw.filter(**final_filter_params_)

    raw.notch_filter(**notch_filter_params)

    ############################################################################
    # plot after filter
    for ax, (picks, ch_type) in iter_plot:

        raw.plot_psds(fmin=fmin, fmax=fmax, ax=ax, picks=picks, color='red')
        second_line = ax.get_lines()[1]
        second_line.set_label('{} - filtered'.format(ch_type))
        ax.legend(loc='best')

    fig.suptitle('Multitaper PSD')
    report.add_figs_to_section(fig,
                               'filter PSD spectra {}'.format(subject),
                               'FILTER',
                               scale=img_scale)
    return fig, report
task = 'OcularLDT'
bids_root = op.join('/', 'Volumes', 'teon-backup', 'Experiments', task)
derivative = 'pca'

redo = True
reject = dict(mag=3e-12)
baseline = (-.2, -.1)
tmin, tmax = -.5, 1
ylim = dict(mag=[-200, 200])

evts_labels = ['word/prime/unprimed', 'word/prime/primed', 'nonword/prime']
subjects_list = get_entity_vals(bids_root, entity_key='sub')

fname_rep_group = op.join('..', '..', 'output', 'preprocessing',
                          f'group_{task}_{derivative}-report.html')
rep_group = Report()

for subject in subjects_list:
    print("#" * 9 + f"\n# {subject} #\n" + "#" * 9)

    # define filenames
    path = op.join(bids_root, f"sub-{subject}", 'meg')
    events_fname = op.join(path, f"sub-{subject}_task-{task}_events.tsv")
    fname_raw = op.join(path, f"sub-{subject}_task-{task}_meg.fif")
    fname_mp_raw = op.join(path, f"sub-{subject}_task-{task}_split-01_meg.fif")
    fname_proj = op.join(path, f"sub-{subject}_task-{task}_proj.fif")

    if not op.exists(fname_proj) or redo:
        # pca input is from fixation cross to three hashes
        # no language involved
        # TODO: replace path with basename
Exemple #8
0
def test_render_report(renderer_pyvistaqt, tmp_path, invisible_fig):
    """Test rendering *.fif files for mne report."""
    tempdir = str(tmp_path)
    raw_fname_new = op.join(tempdir, 'temp_raw.fif')
    raw_fname_new_bids = op.join(tempdir, 'temp_meg.fif')
    ms_fname_new = op.join(tempdir, 'temp_ms_raw.fif')
    event_fname_new = op.join(tempdir, 'temp_raw-eve.fif')
    cov_fname_new = op.join(tempdir, 'temp_raw-cov.fif')
    proj_fname_new = op.join(tempdir, 'temp_ecg-proj.fif')
    fwd_fname_new = op.join(tempdir, 'temp_raw-fwd.fif')
    inv_fname_new = op.join(tempdir, 'temp_raw-inv.fif')
    nirs_fname_new = op.join(tempdir, 'temp_raw-nirs.snirf')
    for a, b in [[raw_fname, raw_fname_new], [raw_fname, raw_fname_new_bids],
                 [ms_fname, ms_fname_new], [events_fname, event_fname_new],
                 [cov_fname, cov_fname_new], [ecg_proj_fname, proj_fname_new],
                 [fwd_fname, fwd_fname_new], [inv_fname, inv_fname_new],
                 [nirs_fname, nirs_fname_new]]:
        shutil.copyfile(a, b)

    # create and add -epo.fif and -ave.fif files
    epochs_fname = op.join(tempdir, 'temp-epo.fif')
    evoked_fname = op.join(tempdir, 'temp-ave.fif')
    # Speed it up by picking channels
    raw = read_raw_fif(raw_fname_new)
    raw.pick_channels(['MEG 0111', 'MEG 0121', 'EEG 001', 'EEG 002'])
    raw.del_proj()
    raw.set_eeg_reference(projection=True).load_data()
    epochs = Epochs(raw, read_events(events_fname), 1, -0.2, 0.2)
    epochs.save(epochs_fname, overwrite=True)
    # This can take forever, so let's make it fast
    # Also, make sure crop range is wide enough to avoid rendering bug
    evoked = epochs.average()
    with pytest.warns(RuntimeWarning, match='tmax is not in Evoked'):
        evoked.crop(0.1, 0.2)
    evoked.save(evoked_fname)

    report = Report(info_fname=raw_fname_new,
                    subjects_dir=subjects_dir,
                    projs=False,
                    image_format='png')
    with pytest.warns(RuntimeWarning, match='Cannot render MRI'):
        report.parse_folder(data_path=tempdir,
                            on_error='raise',
                            n_time_points_evokeds=2,
                            raw_butterfly=False,
                            stc_plot_kwargs=stc_plot_kwargs,
                            topomap_kwargs=topomap_kwargs)
    assert repr(report)

    # Check correct paths and filenames
    fnames = glob.glob(op.join(tempdir, '*.fif'))
    fnames.extend(glob.glob(op.join(tempdir, '*.snirf')))

    titles = [op.basename(x) for x in fnames if not x.endswith('-ave.fif')]
    titles.append(f'{op.basename(evoked_fname)}: {evoked.comment}')

    content_names = [element.name for element in report._content]
    for title in titles:
        assert title in content_names
        assert (''.join(report.html).find(title) != -1)

    assert len(report._content) == len(fnames)

    # Check saving functionality
    report.data_path = tempdir
    fname = op.join(tempdir, 'report.html')
    report.save(fname=fname, open_browser=False)
    assert (op.isfile(fname))
    html = Path(fname).read_text(encoding='utf-8')
    # Evoked in `evoked_fname`
    assert f'{op.basename(evoked_fname)}: {evoked.comment}' in html
    assert 'Topographies' in html
    assert 'Global field power' in html

    assert len(report._content) == len(fnames)

    # Check saving same report to new filename
    report.save(fname=op.join(tempdir, 'report2.html'), open_browser=False)
    assert (op.isfile(op.join(tempdir, 'report2.html')))

    # Check overwriting file
    report.save(fname=op.join(tempdir, 'report.html'),
                open_browser=False,
                overwrite=True)
    assert (op.isfile(op.join(tempdir, 'report.html')))

    # Check pattern matching with multiple patterns
    pattern = ['*proj.fif', '*eve.fif']
    with pytest.warns(RuntimeWarning, match='Cannot render MRI'):
        report.parse_folder(data_path=tempdir,
                            pattern=pattern,
                            raw_butterfly=False)
    assert (repr(report))

    fnames = glob.glob(op.join(tempdir, '*.raw')) + \
        glob.glob(op.join(tempdir, '*.raw'))

    content_names = [element.name for element in report._content]
    for fname in fnames:
        assert (op.basename(fname) in [op.basename(x) for x in content_names])
        assert (''.join(report.html).find(op.basename(fname)) != -1)

    with pytest.raises(ValueError, match='Invalid value'):
        Report(image_format='foo')
    with pytest.raises(ValueError, match='Invalid value'):
        Report(image_format=None)

    # ndarray support smoke test
    report.add_figure(fig=np.zeros((2, 3, 3)), title='title')

    with pytest.raises(TypeError, match='It seems you passed a path'):
        report.add_figure(fig='foo', title='title')
    with pytest.raises(TypeError, match='.*MNEQtBrowser.*Figure3D.*got.*'):
        report.add_figure(fig=1., title='title')
def run(fnconfig=None,
        basedir=None,
        data_meg=None,
        data_train=None,
        pattern='-raw.fif',
        verbose=False,
        do_label_ica=False,
        do_label_check=False,
        log2file=False):

    # ToDo run for list of files or search in subdirs

    # -- init config CLS
    cfg = DCNN_CONFIG(verbose=verbose)
    cfg.load(fname=fnconfig)

    #-- init dcnn CLS
    dcnn = DCNN(**cfg.config)  # init object with config details

    #---
    if basedir:  # FB test
        dcnn.path.basedir = basedir
    if data_meg:
        dcnn.path.data_meg = data_meg  # input directory
    if data_train:
        dcnn.path.data_train = data_train  # input directory

    dcnn.verbose = True
    dcnn.get_info()

    # ==========================================================
    # run ICA auto labelling
    # ==========================================================
    if do_label_ica:
        #path_in = os.path.join(cfg.config['path']['basedir'],cfg.config['path']['data_meg'])
        path_in = dcnn.path.data_meg

        # -- looper catch error via try/exception setup log2file
        for fnraw in file_looper(rootdir=path_in,
                                 pattern=pattern,
                                 version=__version__,
                                 verbose=verbose,
                                 logoverwrite=True,
                                 log2file=log2file):
            #logger.info(fnraw)
            # -- read raw data and apply noise reduction
            dcnn.meg.update(fname=fnraw)

            # -- apply ICA on chops, label ICs save results to disk
            # ToDo store chop-times, ECG,EOG in raw.annotations
            #   - chop:  make use of annotations in get_chop_times_indices()
            fgdcnn = dcnn.label_ica(save=True)

            if verbose:
                dcnn.get_info()

    # ==========================================================
    # check ICA auto labelling
    # ==========================================================
    npz_pattern = pattern.split(".", -1)[0] + "-gdcnn.npz"

    if do_label_check:
        path_in = dcnn.path.data_train

        from mne.report import Report
        report = Report(title='Check IC labels')

        for fname in file_looper(rootdir=path_in,
                                 pattern=npz_pattern,
                                 version=__version__,
                                 verbose=verbose,
                                 log2file=log2file,
                                 logoverwrite=False):

            dcnn.load_gdcnn(fname)
            # check IC labels (and apply corrections)
            #dcnn.check_labels(save=True)

            fnreport = "test_report"

            # check IC labels (and apply corrections)
            name = os.path.basename(fname[:-4])
            print('>>> working on %s' % name)
            figs, captions = dcnn.plot_ica_traces(fname)
            report.add_figs_to_section(figs,
                                       captions=captions,
                                       section=name,
                                       replace=True)
        report.save(fnreport + '.h5', overwrite=True)
        report.save(fnreport + '.html', overwrite=True, open_browser=True)

        #dcnn.check_labels(save=True, path_out=cfg.config['path']['data_train'])


#if verbose: # ToDo set verbose level  True,2,3
#   logger.debug("dcnn ica chop  dump.\n{}\n{}\n\n".format( dcnn.ica.chop, dict2str(dcnn.ica.chop.dump())  ))
#   logger.debug("dcnn ica n_chop.\n{}\n\n".format(dcnn.ica.chop.n_chop))
#   logger.debug("dcnn ica topo data.\n{}\n\n".format(dcnn.ica.topo.data))
#   logger.debug("dcnn ica topo img.\n{}\n\n".format(dcnn.ica.topo.images))

# ==========================================================
# ICA performance plot
# ==========================================================
    if do_performance_check:
        path_in = cfg.config['path']['data_train']

        for fname in file_looper(rootdir=path_in,
                                 pattern=npz_pattern,
                                 version=__version__,
                                 verbose=verbose,
                                 log2file=log2file,
                                 logoverwrite=False):

            dcnn.load_gdcnn(fname)
def apply_ica(subject, run, session):
    deriv_path = config.get_subject_deriv_path(subject=subject,
                                               session=session,
                                               kind=config.get_kind())

    bids_basename = BIDSPath(subject=subject,
                             session=session,
                             task=config.get_task(),
                             acquisition=config.acq,
                             run=None,
                             recording=config.rec,
                             space=config.space,
                             prefix=deriv_path)

    fname_in = bids_basename.copy().update(
        kind='epo', extension='.fif')
    fname_out = bids_basename.copy().update(
        kind='epo', processing='clean', extension='.fif')

    # load epochs to reject ICA components
    epochs = mne.read_epochs(fname_in, preload=True)

    msg = f'Input: {fname_in}, Output: {fname_out}'
    logger.info(gen_log_message(message=msg, step=5, subject=subject,
                                session=session))

    # load first run of raw data for ecg / eog epochs
    msg = 'Loading first run from raw data'
    logger.debug(gen_log_message(message=msg, step=5, subject=subject,
                                 session=session))

    bids_basename = BIDSPath(subject=subject,
                             session=session,
                             task=config.get_task(),
                             acquisition=config.acq,
                             run=config.get_runs()[0],
                             processing=config.proc,
                             recording=config.rec,
                             space=config.space)

    # XXX : do we want to always take the filt?
    if config.use_maxwell_filter:
        raw_fname_in = (bids_basename.copy()
                        .update(run=config.get_runs()[0],
                                processing='sss',
                                kind=config.get_kind(),
                                extension='.fif'))
    else:
        raw_fname_in = (bids_basename.copy()
                        .update(run=config.get_runs()[0],
                                processing='filt',
                                kind=config.get_kind(),
                                extension='.fif'))

    raw = mne.io.read_raw_fif(raw_fname_in, preload=True)

    for ch_type in config.ch_types:
        report = None

        # Load ICA
        fname_ica = bids_basename.copy().update(
          run=None, kind=f'{ch_type}-ica', extension='.fif')

        msg = f'Reading ICA: {fname_ica}'
        logger.debug(gen_log_message(message=msg, step=5, subject=subject,
                                     session=session))
        ica = read_ica(fname=fname_ica)

        # ECG
        # either needs an ecg channel, or avg of the mags (i.e. MEG data)
        ecg_inds = list()
        if 'ecg' in raw.get_channel_types() or ch_type in ('meg', 'mag'):
            # Create ecg epochs
            if ch_type == 'meg':
                reject = {'mag': config.reject['mag'],
                          'grad': config.reject['grad']}
            elif ch_type == 'mag':
                reject = {'mag': config.reject['mag']}
            elif ch_type == 'eeg':
                reject = {'eeg': config.reject['eeg']}

            ecg_epochs = create_ecg_epochs(raw, reject=reject,
                                           baseline=(None, 0),
                                           tmin=-0.5, tmax=0.5)

            ecg_average = ecg_epochs.average()

            ecg_inds, scores = \
                ica.find_bads_ecg(ecg_epochs, method='ctps',
                                  threshold=config.ica_ctps_ecg_threshold)
            del ecg_epochs

            report_fname = bids_basename.copy().update(
                run=None, kind=f'{ch_type}-reject_ica', extension='.html')

            report = Report(report_fname, verbose=False)

            # Plot r score
            report.add_figs_to_section(
                ica.plot_scores(scores, exclude=ecg_inds,
                                show=config.interactive),
                captions=ch_type.upper() + ' - ECG - R scores')

            # Plot source time course
            report.add_figs_to_section(
                ica.plot_sources(ecg_average, exclude=ecg_inds,
                                 show=config.interactive),
                captions=ch_type.upper() + ' - ECG - Sources time course')

            # Plot source time course
            report.add_figs_to_section(
                ica.plot_overlay(ecg_average, exclude=ecg_inds,
                                 show=config.interactive),
                captions=ch_type.upper() + ' - ECG - Corrections')

        else:
            # XXX : to check when EEG only is processed
            msg = ('No ECG channel is present. Cannot automate IC detection '
                   'for ECG')
            logger.info(gen_log_message(message=msg, step=5, subject=subject,
                                        session=session))

        # EOG
        pick_eog = mne.pick_types(raw.info, meg=False, eeg=False,
                                  ecg=False, eog=True)
        eog_inds = list()
        if pick_eog.any():
            msg = 'Using EOG channel'
            logger.debug(gen_log_message(message=msg, step=5, subject=subject,
                                         session=session))

            # Create eog epochs
            eog_epochs = create_eog_epochs(raw, reject=None,
                                           baseline=(None, 0),
                                           tmin=-0.5, tmax=0.5)

            eog_average = eog_epochs.average()
            eog_inds, scores = ica.find_bads_eog(eog_epochs, threshold=3.0)
            del eog_epochs

            params = dict(exclude=eog_inds, show=config.interactive)

            # Plot r score
            report.add_figs_to_section(ica.plot_scores(scores, **params),
                                       captions=ch_type.upper() + ' - EOG - ' +
                                       'R scores')

            # Plot source time course
            report.add_figs_to_section(ica.plot_sources(eog_average, **params),
                                       captions=ch_type.upper() + ' - EOG - ' +
                                       'Sources time course')

            # Plot source time course
            report.add_figs_to_section(ica.plot_overlay(eog_average, **params),
                                       captions=ch_type.upper() + ' - EOG - ' +
                                       'Corrections')

            report.save(report_fname, overwrite=True, open_browser=False)

        else:
            msg = ('No EOG channel is present. Cannot automate IC detection '
                   'for EOG')
            logger.info(gen_log_message(message=msg, step=5, subject=subject,
                                        session=session))

        ica_reject = (list(ecg_inds) + list(eog_inds) +
                      list(config.rejcomps_man[subject][ch_type]))

        # now reject the components
        msg = f'Rejecting from {ch_type}: {ica_reject}'
        logger.info(gen_log_message(message=msg, step=5, subject=subject,
                                    session=session))
        epochs = ica.apply(epochs, exclude=ica_reject)

        msg = 'Saving cleaned epochs'
        logger.info(gen_log_message(message=msg, step=5, subject=subject,
                                    session=session))
        epochs.save(fname_out)

        if report is not None:
            fig = ica.plot_overlay(raw, exclude=ica_reject,
                                   show=config.interactive)
            report.add_figs_to_section(fig, captions=ch_type.upper() +
                                       ' - ALL(epochs) - Corrections')

        if config.interactive:
            epochs.plot_image(combine='gfp', group_by='type', sigma=2.,
                              cmap="YlGnBu_r", show=config.interactive)
Exemple #11
0
def test_manual_report_2d(tmp_path, invisible_fig):
    """Simulate user manually creating report by adding one file at a time."""
    from sklearn.exceptions import ConvergenceWarning

    r = Report(title='My Report')
    raw = read_raw_fif(raw_fname)
    raw.pick_channels(raw.ch_names[:6]).crop(10, None)
    raw.info.normalize_proj()
    cov = read_cov(cov_fname)
    cov = pick_channels_cov(cov, raw.ch_names)
    events = read_events(events_fname)
    event_id = {
        'auditory/left': 1,
        'auditory/right': 2,
        'visual/left': 3,
        'visual/right': 4,
        'face': 5,
        'buttonpress': 32
    }
    metadata, metadata_events, metadata_event_id = make_metadata(
        events=events,
        event_id=event_id,
        tmin=-0.2,
        tmax=0.5,
        sfreq=raw.info['sfreq'])
    epochs_without_metadata = Epochs(raw=raw,
                                     events=events,
                                     event_id=event_id,
                                     baseline=None)
    epochs_with_metadata = Epochs(raw=raw,
                                  events=metadata_events,
                                  event_id=metadata_event_id,
                                  baseline=None,
                                  metadata=metadata)
    evokeds = read_evokeds(evoked_fname)
    evoked = evokeds[0].pick('eeg')

    with pytest.warns(ConvergenceWarning, match='did not converge'):
        ica = (ICA(n_components=2, max_iter=1,
                   random_state=42).fit(inst=raw.copy().crop(tmax=1)))
    ica_ecg_scores = ica_eog_scores = np.array([3, 0])
    ica_ecg_evoked = ica_eog_evoked = epochs_without_metadata.average()

    r.add_raw(raw=raw,
              title='my raw data',
              tags=('raw', ),
              psd=True,
              projs=False)
    r.add_raw(raw=raw,
              title='my raw data 2',
              psd=False,
              projs=False,
              butterfly=1)
    r.add_events(events=events_fname,
                 title='my events',
                 sfreq=raw.info['sfreq'])
    r.add_epochs(epochs=epochs_without_metadata,
                 title='my epochs',
                 tags=('epochs', ),
                 psd=False,
                 projs=False)
    r.add_epochs(epochs=epochs_without_metadata,
                 title='my epochs 2',
                 psd=1,
                 projs=False)
    r.add_epochs(epochs=epochs_without_metadata,
                 title='my epochs 2',
                 psd=True,
                 projs=False)
    assert 'Metadata' not in r.html[-1]

    # Try with metadata
    r.add_epochs(epochs=epochs_with_metadata,
                 title='my epochs with metadata',
                 psd=False,
                 projs=False)
    assert 'Metadata' in r.html[-1]

    with pytest.raises(ValueError,
                       match='requested to calculate PSD on a duration'):
        r.add_epochs(epochs=epochs_with_metadata,
                     title='my epochs 2',
                     psd=100000000,
                     projs=False)

    r.add_evokeds(evokeds=evoked,
                  noise_cov=cov_fname,
                  titles=['my evoked 1'],
                  tags=('evoked', ),
                  projs=False,
                  n_time_points=2)
    r.add_projs(info=raw_fname,
                projs=ecg_proj_fname,
                title='my proj',
                tags=('ssp', 'ecg'))
    r.add_ica(ica=ica, title='my ica', inst=None)
    with pytest.raises(RuntimeError, match='not preloaded'):
        r.add_ica(ica=ica, title='ica', inst=raw)
    r.add_ica(ica=ica,
              title='my ica with inst',
              inst=raw.copy().load_data(),
              picks=[0],
              ecg_evoked=ica_ecg_evoked,
              eog_evoked=ica_eog_evoked,
              ecg_scores=ica_ecg_scores,
              eog_scores=ica_eog_scores)
    r.add_covariance(cov=cov, info=raw_fname, title='my cov')
    r.add_forward(forward=fwd_fname,
                  title='my forward',
                  subject='sample',
                  subjects_dir=subjects_dir)
    r.add_html(html='<strong>Hello</strong>', title='Bold')
    r.add_code(code=__file__, title='my code')
    r.add_sys_info(title='my sysinfo')

    # drop locations (only EEG channels in `evoked`)
    evoked_no_ch_locs = evoked.copy()
    for ch in evoked_no_ch_locs.info['chs']:
        ch['loc'][:3] = np.nan

    with pytest.warns(RuntimeWarning, match='No EEG channel locations'):
        r.add_evokeds(evokeds=evoked_no_ch_locs,
                      titles=['evoked no chan locs'],
                      tags=('evoked', ),
                      projs=True,
                      n_time_points=1)
    assert 'Time course' not in r._content[-1].html
    assert 'Topographies' not in r._content[-1].html
    assert evoked.info['projs']  # only then the following test makes sense
    assert 'SSP' not in r._content[-1].html
    assert 'Global field power' in r._content[-1].html

    # Drop locations from Info used for projs
    info_no_ch_locs = raw.info.copy()
    for ch in info_no_ch_locs['chs']:
        ch['loc'][:3] = np.nan

    with pytest.warns(RuntimeWarning, match='No channel locations found'):
        r.add_projs(info=info_no_ch_locs, title='Projs no chan locs')

    # Drop locations from ICA
    ica_no_ch_locs = ica.copy()
    for ch in ica_no_ch_locs.info['chs']:
        ch['loc'][:3] = np.nan

    with pytest.warns(RuntimeWarning,
                      match='No Magnetometers channel locations'):
        r.add_ica(ica=ica_no_ch_locs,
                  picks=[0],
                  inst=raw.copy().load_data(),
                  title='ICA')
    assert 'ICA component properties' not in r._content[-1].html
    assert 'ICA component topographies' not in r._content[-1].html
    assert 'Original and cleaned signal' in r._content[-1].html

    fname = op.join(tmp_path, 'report.html')
    r.save(fname=fname, open_browser=False)
@author: Alba
"""

import os
import os.path as op
import mne
from mne.report import Report
from IPython import get_ipython

## ~~~~~~~~  PARAMETERS
rest_dir = 'G:\OpenSource_data\CAM-CAN\cc700\mri\pipeline\\release004\BIDSsep\megmax_rest'
report_path = 'G:\OpenSource_data\CAM-CAN\cc700\mri\pipeline\\release004\BIDSsep\megmax_rest_reports'
logFile = open(
    'G:\OpenSource_data\CAM-CAN\cc700\mri\pipeline\\release004\BIDSsep\megmax_rest_reports\preproc_manual_logFile.txt',
    'a')
report = Report()
## ~~~~~~~~  PARAMETERS


def preproc_manual(subject):

    global report
    from mne.preprocessing import ICA
    from mne.io import read_raw_fif

    get_ipython().run_line_magic('matplotlib', 'inline')

    raw_path = op.join(rest_dir, subject, 'meg_clean', 'clean_raw.fif')
    raw = read_raw_fif(raw_path, preload=True)

    pick_meg = mne.pick_types(raw.info,
Exemple #13
0
def run_evoked(subject):
    print("Processing subject: %s" % subject)
    meg_subject_dir = op.join(config.meg_dir, subject)

    # load epochs to reject ICA components
    extension = '-epo'
    fname_in = op.join(meg_subject_dir, config.base_fname.format(**locals()))
    epochs = mne.read_epochs(fname_in, preload=True)

    extension = 'cleaned-epo'
    fname_out = op.join(meg_subject_dir, config.base_fname.format(**locals()))

    print("Input: ", fname_in)
    print("Output: ", fname_out)

    # load first run of raw data for ecg /eog epochs
    raw_list = list()
    print("  Loading one run from raw data")
    extension = config.runs[0] + '_sss_raw'
    raw_fname_in = op.join(meg_subject_dir,
                           config.base_fname.format(**locals()))
    raw = mne.io.read_raw_fif(raw_fname_in, preload=True)

    # run ICA on MEG and EEG
    picks_meg = mne.pick_types(raw.info,
                               meg=True,
                               eeg=False,
                               eog=False,
                               stim=False,
                               exclude='bads')
    picks_eeg = mne.pick_types(raw.info,
                               meg=False,
                               eeg=True,
                               eog=False,
                               stim=False,
                               exclude='bads')
    all_picks = {'meg': picks_meg, 'eeg': picks_eeg}

    for ch_type in ['meg', 'eeg']:
        print(ch_type)
        picks = all_picks[ch_type]

        # Load ICA
        fname_ica = op.join(
            meg_subject_dir,
            '{0}_{1}_{2}-ica.fif'.format(subject, config.study_name, ch_type))
        print('Reading ICA: ' + fname_ica)
        ica = read_ica(fname=fname_ica)

        pick_ecg = mne.pick_types(raw.info,
                                  meg=False,
                                  eeg=False,
                                  ecg=True,
                                  eog=False)

        # ECG
        # either needs an ecg channel, or avg of the mags (i.e. MEG data)
        if pick_ecg or ch_type == 'meg':

            picks_ecg = np.concatenate([picks, pick_ecg])

            # Create ecg epochs
            if ch_type == 'meg':
                reject = {
                    'mag': config.reject['mag'],
                    'grad': config.reject['grad']
                }
            elif ch_type == 'eeg':
                reject = {'eeg': config.reject['eeg']}

            ecg_epochs = create_ecg_epochs(raw,
                                           picks=picks_ecg,
                                           reject=reject,
                                           baseline=(None, 0),
                                           tmin=-0.5,
                                           tmax=0.5)

            ecg_average = ecg_epochs.average()

            # XXX I had to lower the threshold for ctps (default 0.25), otherwise it does not
            # find any components
            # check how this behaves on other data
            ecg_inds, scores = ica.find_bads_ecg(ecg_epochs,
                                                 method='ctps',
                                                 threshold=0.1)
            del ecg_epochs

            if config.plot:

                report_name = op.join(
                    meg_subject_dir, '{0}_{1}_{2}-reject_ica.html'.format(
                        subject, config.study_name, ch_type))
                report = Report(report_name, verbose=False)

                # Plot r score
                report.add_figs_to_section(ica.plot_scores(scores,
                                                           exclude=ecg_inds),
                                           captions=ch_type.upper() +
                                           ' - ECG - ' + 'R scores')

                # Plot source time course
                report.add_figs_to_section(ica.plot_sources(ecg_average,
                                                            exclude=ecg_inds),
                                           captions=ch_type.upper() +
                                           ' - ECG - ' + 'Sources time course')

                # Plot source time course
                report.add_figs_to_section(ica.plot_overlay(ecg_average,
                                                            exclude=ecg_inds),
                                           captions=ch_type.upper() +
                                           ' - ECG - ' + 'Corrections')

        else:
            print('no ECG channel!')

        # EOG
        pick_eog = mne.pick_types(raw.info,
                                  meg=False,
                                  eeg=False,
                                  ecg=False,
                                  eog=True)

        if pick_eog:
            print('using EOG channel')
            picks_eog = np.concatenate([picks, pick_eog])
            # Create eog epochs
            eog_epochs = create_eog_epochs(raw,
                                           picks=picks_eog,
                                           reject=None,
                                           baseline=(None, 0),
                                           tmin=-0.5,
                                           tmax=0.5)

            eog_average = eog_epochs.average()
            eog_inds, scores = ica.find_bads_eog(eog_epochs)
            del eog_epochs

            if config.plot:
                # Plot r score
                report.add_figs_to_section(ica.plot_scores(scores,
                                                           exclude=eog_inds),
                                           captions=ch_type.upper() +
                                           ' - EOG - ' + 'R scores')

                # Plot source time course
                report.add_figs_to_section(ica.plot_sources(eog_average,
                                                            exclude=eog_inds),
                                           captions=ch_type.upper() +
                                           ' - EOG - ' + 'Sources time course')

                # Plot source time course
                report.add_figs_to_section(ica.plot_overlay(eog_average,
                                                            exclude=eog_inds),
                                           captions=ch_type.upper() +
                                           ' - EOG - ' + 'Corrections')

                report.save(report_name, overwrite=True, open_browser=False)

        else:
            print('no EOG channel!')

        ica_reject = (list(ecg_inds) + list(eog_inds) +
                      list(config.rejcomps_man[subject][ch_type]))

        # now reject the components
        print('Rejecting from ' + ch_type + ': ' + str(ica_reject))
        epochs = ica.apply(epochs, exclude=ica_reject)

        print('Saving epochs')
        epochs.save(fname_out)

        if config.plot:
            report.add_figs_to_section(ica.plot_overlay(raw.copy(),
                                                        exclude=ica_reject),
                                       captions=ch_type.upper() +
                                       ' - ALL(epochs) - ' + 'Corrections')
Exemple #14
0
def test_render_report():
    """Test rendering -*.fif files for mne report.
    """

    report = Report(info_fname=raw_fname)
    with warnings.catch_warnings(record=True) as w:
        warnings.simplefilter('always')
        report.parse_folder(data_path=base_dir)
    assert_true(len(w) == 1)

    # Check correct paths and filenames
    assert_true(raw_fname in report.fnames)
    assert_true(event_name in report.fnames)
    assert_true(report.data_path == base_dir)

    # Check if raw repr is printed correctly
    raw = Raw(raw_fname)
    raw_idx = [
        ii for ii, fname in enumerate(report.fnames) if fname == raw_fname
    ][0]
    raw_html = report.html[raw_idx]
    assert_true(raw_html.find(repr(raw)[1:-1]) != -1)
    assert_true(raw_html.find(str(raw.info['sfreq'])) != -1)
    assert_true(raw_html.find('class="raw"') != -1)
    assert_true(raw_html.find(raw_fname) != -1)

    # Check if all files were rendered in the report
    fnames = glob.glob(op.join(base_dir, '*.fif'))
    bad_name = 'test_ctf_comp_raw-eve.fif'
    decrement = any(fname.endswith(bad_name) for fname in fnames)
    fnames = [
        fname for fname in fnames
        if fname.endswith(('-eve.fif', '-ave.fif', '-cov.fif', '-sol.fif',
                           '-fwd.fif', '-inv.fif', '-src.fif', '-trans.fif',
                           'raw.fif', 'sss.fif',
                           '-epo.fif')) and not fname.endswith(bad_name)
    ]
    # last file above gets created by another test, and it shouldn't be there

    for fname in fnames:
        assert_true(''.join(report.html).find(op.basename(fname)) != -1)

    assert_equal(len(report.fnames), len(fnames))
    assert_equal(len(report.html), len(report.fnames))

    evoked1 = read_evokeds(evoked1_fname)
    evoked2 = read_evokeds(evoked2_fname)
    assert_equal(
        len(report.fnames) + len(evoked1) + len(evoked2) - 2,
        report.initial_id - decrement)

    # Check saving functionality
    report.data_path = tempdir
    report.save(fname=op.join(tempdir, 'report.html'), open_browser=False)
    assert_true(op.isfile(op.join(tempdir, 'report.html')))

    # Check add_section functionality
    fig = evoked1[0].plot(show=False)
    report.add_section(
        figs=fig,  # test non-list input
        captions=['evoked response'])
    assert_equal(len(report.html), len(fnames) + 1)
    assert_equal(len(report.html), len(report.fnames))
    assert_raises(ValueError,
                  report.add_section,
                  figs=[fig, fig],
                  captions='H')

    # Check saving same report to new filename
    report.save(fname=op.join(tempdir, 'report2.html'), open_browser=False)
    assert_true(op.isfile(op.join(tempdir, 'report2.html')))

    # Check overwriting file
    report.save(fname=op.join(tempdir, 'report.html'),
                open_browser=False,
                overwrite=True)
    assert_true(op.isfile(op.join(tempdir, 'report.html')))
def run_ica(subject, session=None):
    """Run ICA."""
    bids_basename = BIDSPath(subject=subject,
                             session=session,
                             task=config.get_task(),
                             acquisition=config.acq,
                             recording=config.rec,
                             space=config.space,
                             datatype=config.get_datatype(),
                             root=config.deriv_root,
                             check=False)

    ica_fname = bids_basename.copy().update(suffix='ica', extension='.fif')
    ica_components_fname = bids_basename.copy().update(processing='ica',
                                                       suffix='components',
                                                       extension='.tsv')
    report_fname = bids_basename.copy().update(processing='ica',
                                               suffix='report',
                                               extension='.html')

    msg = 'Loading and concatenating filtered continuous "raw" data'
    logger.info(
        gen_log_message(message=msg, step=4, subject=subject, session=session))
    raw = load_and_concatenate_raws(bids_basename.copy().update(
        processing='filt', suffix='raw', extension='.fif'))

    # Sanity check – make sure we're using the correct data!
    if config.resample_sfreq is not None:
        np.testing.assert_allclose(raw.info['sfreq'], config.resample_sfreq)
    if config.l_freq is not None:
        np.testing.assert_allclose(raw.info['highpass'], config.l_freq)

    # Produce high-pass filtered version of the data for ICA.
    # filter_for_ica will concatenate all runs of our raw data.
    # We don't have to worry about edge artifacts due to raw concatenation as
    # we'll be epoching the data in the next step.
    raw = filter_for_ica(raw, subject=subject, session=session)
    epochs = make_epochs_for_ica(raw, subject=subject, session=session)

    # Now actually perform ICA.
    msg = 'Calculating ICA solution.'
    logger.info(
        gen_log_message(message=msg, step=4, subject=subject, session=session))
    report = Report(info_fname=raw,
                    title='Independent Component Analysis (ICA)',
                    verbose=False)
    ica = fit_ica(epochs, subject=subject, session=session)
    ecg_ics = detect_ecg_artifacts(ica=ica,
                                   raw=raw,
                                   subject=subject,
                                   session=session,
                                   report=report)
    eog_ics = detect_eog_artifacts(ica=ica,
                                   raw=raw,
                                   subject=subject,
                                   session=session,
                                   report=report)

    # Save ICA to disk.
    # We also store the automatically identified ECG- and EOG-related ICs.
    msg = 'Saving ICA solution and detected artifacts to disk.'
    logger.info(
        gen_log_message(message=msg, step=4, subject=subject, session=session))
    ica.exclude = sorted(set(ecg_ics + eog_ics))
    ica.save(ica_fname)

    # Create TSV.
    tsv_data = pd.DataFrame(
        dict(component=list(range(ica.n_components_)),
             type=['ica'] * ica.n_components_,
             description=['Independent Component'] * ica.n_components_,
             status=['good'] * ica.n_components_,
             status_description=['n/a'] * ica.n_components_))

    for component in ecg_ics:
        row_idx = tsv_data['component'] == component
        tsv_data.loc[row_idx, 'status'] = 'bad'
        tsv_data.loc[row_idx,
                     'status_description'] = 'Auto-detected ECG artifact'

    for component in eog_ics:
        row_idx = tsv_data['component'] == component
        tsv_data.loc[row_idx, 'status'] = 'bad'
        tsv_data.loc[row_idx,
                     'status_description'] = 'Auto-detected EOG artifact'

    tsv_data.to_csv(ica_components_fname, sep='\t', index=False)

    # Lastly, plot all ICs, and add them to the report for manual inspection.
    msg = 'Adding diagnostic plots for all ICs to the HTML report …'
    logger.info(
        gen_log_message(message=msg, step=4, subject=subject, session=session))
    for component_num in tqdm(range(ica.n_components_)):
        fig = ica.plot_properties(epochs,
                                  picks=component_num,
                                  psd_args={'fmax': 60},
                                  show=False)

        caption = f'IC {component_num}'
        if component_num in eog_ics and component_num in ecg_ics:
            caption += ' (EOG & ECG)'
        elif component_num in eog_ics:
            caption += ' (EOG)'
        elif component_num in ecg_ics:
            caption += ' (ECG)'
        report.add_figs_to_section(fig,
                                   section=f'sub-{subject}',
                                   captions=caption)

    open_browser = True if config.interactive else False
    report.save(report_fname, overwrite=True, open_browser=open_browser)

    msg = (f"ICA completed. Please carefully review the extracted ICs in the "
           f"report {report_fname.basename}, and mark all components you wish "
           f"to reject as 'bad' in {ica_components_fname.basename}")
    logger.info(
        gen_log_message(message=msg, step=4, subject=subject, session=session))
Exemple #16
0
from mne.report import Report
from mne.datasets import sample
from mne import read_evokeds
from matplotlib import pyplot as plt

data_path = sample.data_path()
meg_path = data_path + '/MEG/sample'
subjects_dir = data_path + '/subjects'
evoked_fname = meg_path + '/sample_audvis-ave.fif'

###############################################################################
# Do standard folder parsing (this can take a couple of minutes):

report = Report(image_format='png',
                subjects_dir=subjects_dir,
                info_fname=evoked_fname,
                subject='sample',
                raw_psd=True)
report.parse_folder(meg_path)

###############################################################################
# Add a custom section with an evoked slider:

# Load the evoked data
evoked = read_evokeds(evoked_fname,
                      condition='Left Auditory',
                      baseline=(None, 0),
                      verbose=False)
evoked.crop(0, .2)
times = evoked.times[::4]
# Create a list of figs for the slider
def compute_ica(raw,
                subject,
                n_components=0.99,
                picks=None,
                decim=None,
                reject=None,
                ecg_tmin=-0.5,
                ecg_tmax=0.5,
                eog_tmin=-0.5,
                eog_tmax=0.5,
                n_max_ecg=3,
                n_max_eog=1,
                n_max_ecg_epochs=200,
                img_scale=1.0,
                report=None):
    """Run ICA in raw data

    Parameters
    ----------,
    raw : instance of Raw
        Raw measurements to be decomposed.
    subject : str
        The name of the subject.
    picks : array-like of int, shape(n_channels, ) | None
        Channels to be included. This selection remains throughout the
        initialized ICA solution. If None only good data channels are used.
        Defaults to None.
    n_components : int | float | None | 'rank'
        The number of components used for ICA decomposition. If int, it must be
        smaller then max_pca_components. If None, all PCA components will be
        used. If float between 0 and 1 components can will be selected by the
        cumulative percentage of explained variance.
        If 'rank', the number of components equals the rank estimate.
        Defaults to 0.99.
    decim : int | None
        Increment for selecting each nth time slice. If None, all samples
        within ``start`` and ``stop`` are used. Defalts to None.
    reject : dict | None
        Rejection parameters based on peak to peak amplitude.
        Valid keys are 'grad' | 'mag' | 'eeg' | 'eog' | 'ecg'.
        If reject is None then no rejection is done. You should
        use such parameters to reject big measurement artifacts
        and not EOG for example. It only applies if `inst` is of type Raw.
        Defaults to {'mag': 5e-12}
    ecg_tmin : float
        Start time before ECG event. Defaults to -0.5.
    ecg_tmax : float
        End time after ECG event. Defaults to 0.5.
    eog_tmin : float
        Start time before rog event. Defaults to -0.5.
    eog_tmax : float
        End time after rog event. Defaults to 0.5.
    n_max_ecg : int | None
        The maximum number of ECG components to exclude. Defaults to 3.
    n_max_eog : int | None
        The maximum number of EOG components to exclude. Defaults to 1.
    n_max_ecg_epochs : int
        The maximum number of ECG epochs to use for phase-consistency
        estimation. Defaults to 200.
    scale_img : float
        The scaling factor for the report. Defaults to 1.0.
    report : instance of Report | None
        The report object. If None, a new report will be generated.

    Returns
    -------
    ica : isntance of ICA
        The ICA solution.
    report : instance of Report
        The report object.
    """
    if report is None:
        report = Report(subject=subject, title='ICA preprocessing')
    if n_components == 'rank':
        n_components = raw.estimate_rank(picks=picks)
    ica = ICA(n_components=n_components, max_pca_components=None, max_iter=256)
    ica.fit(raw, picks=picks, decim=decim, reject=reject)

    comment = []
    for ch in ('mag', 'grad', 'eeg'):
        if ch in ica:
            comment += [ch.upper()]
    if len(comment) > 0:
        comment = '+'.join(comment) + ' '
    else:
        comment = ''

    topo_ch_type = 'mag'
    if 'GRAD' in comment and 'MAG' not in comment:
        topo_ch_type = 'grad'
    elif 'EEG' in comment:
        topo_ch_type = 'eeg'

    ############################################################################
    # 2) identify bad components by analyzing latent sources.

    title = '%s related to %s artifacts (red) ({})'.format(subject)

    # generate ECG epochs use detection via phase statistics

    ecg_epochs = create_ecg_epochs(raw,
                                   tmin=ecg_tmin,
                                   tmax=ecg_tmax,
                                   picks=None,
                                   reject={'mag': 5e-12})
    n_ecg_epochs_found = len(ecg_epochs.events)
    n_max_ecg_epochs = min(n_max_ecg_epochs, n_ecg_epochs_found)
    sel_ecg_epochs = np.arange(n_ecg_epochs_found)
    rng = np.random.RandomState(42)
    rng.shuffle(sel_ecg_epochs)
    ecg_epochs = ecg_epochs[sel_ecg_epochs[:n_max_ecg_epochs]]

    ecg_inds, scores = ica.find_bads_ecg(ecg_epochs, method='ctps')
    if len(ecg_inds) > 0:
        ecg_evoked = ecg_epochs.average(picks=picks)
        del ecg_epochs
        fig = ica.plot_scores(scores,
                              exclude=ecg_inds,
                              title=title % ('scores', 'ecg'))
        report.add_figs_to_section(fig,
                                   'scores ({})'.format(subject),
                                   section=comment + 'ECG',
                                   scale=img_scale)

        fig = ica.plot_sources(raw,
                               ecg_inds,
                               exclude=ecg_inds,
                               title=title % ('components', 'ecg'))
        report.add_figs_to_section(fig,
                                   'sources ({})'.format(subject),
                                   section=comment + 'ECG',
                                   scale=img_scale)

        fig = ica.plot_components(ecg_inds,
                                  ch_type=topo_ch_type,
                                  title='',
                                  colorbar=True)
        report.add_figs_to_section(fig,
                                   title % ('sources', 'ecg'),
                                   section=comment + 'ECG',
                                   scale=img_scale)

        ecg_inds = ecg_inds[:n_max_ecg]
        ica.exclude += ecg_inds

        fig = ica.plot_sources(ecg_evoked, exclude=ecg_inds)
        report.add_figs_to_section(fig,
                                   'evoked sources ({})'.format(subject),
                                   section=comment + 'ECG',
                                   scale=img_scale)

        fig = ica.plot_overlay(ecg_evoked, exclude=ecg_inds)
        report.add_figs_to_section(fig,
                                   'rejection overlay ({})'.format(subject),
                                   section=comment + 'ECG',
                                   scale=img_scale)

    # detect EOG by correlation
    eog_inds, scores = ica.find_bads_eog(raw)
    if len(eog_inds) > 0:
        fig = ica.plot_scores(scores,
                              exclude=eog_inds,
                              title=title % ('scores', 'eog'))
        report.add_figs_to_section(fig,
                                   'scores ({})'.format(subject),
                                   section=comment + 'EOG',
                                   scale=img_scale)

        fig = ica.plot_sources(raw,
                               eog_inds,
                               exclude=ecg_inds,
                               title=title % ('sources', 'eog'))
        report.add_figs_to_section(fig,
                                   'sources',
                                   section=comment + 'EOG',
                                   scale=img_scale)

        fig = ica.plot_components(eog_inds,
                                  ch_type=topo_ch_type,
                                  title='',
                                  colorbar=True)
        report.add_figs_to_section(fig,
                                   title % ('components', 'eog'),
                                   section=comment + 'EOG',
                                   scale=img_scale)

        eog_inds = eog_inds[:n_max_eog]
        ica.exclude += eog_inds

        # estimate average artifact
        eog_evoked = create_eog_epochs(raw,
                                       tmin=eog_tmin,
                                       tmax=eog_tmax,
                                       picks=None).average(picks=picks)
        fig = ica.plot_sources(eog_evoked, exclude=eog_inds)
        report.add_figs_to_section(fig,
                                   'evoked sources ({})'.format(subject),
                                   section=comment + 'EOG',
                                   scale=img_scale)

        fig = ica.plot_overlay(eog_evoked, exclude=eog_inds)
        report.add_figs_to_section(fig,
                                   'rejection overlay({})'.format(subject),
                                   section=comment + 'EOG',
                                   scale=img_scale)

    # check the amplitudes do not change
    if len(ica.exclude) > 0:
        fig = ica.plot_overlay(raw)  # EOG artifacts remain
        report.add_figs_to_section(fig,
                                   'rejection overlay({})'.format(subject),
                                   section=comment + 'RAW',
                                   scale=img_scale)

    return ica, report
def run_ica(subject, session=None):
    """Run ICA."""

    deriv_path = config.get_subject_deriv_path(subject=subject,
                                               session=session,
                                               kind=config.get_kind())

    raw_list = list()
    msg = 'Loading filtered raw data'
    logger.info(
        gen_log_message(message=msg, step=4, subject=subject, session=session))

    for run in config.get_runs():
        bids_basename = make_bids_basename(subject=subject,
                                           session=session,
                                           task=config.get_task(),
                                           acquisition=config.acq,
                                           run=run,
                                           processing=config.proc,
                                           recording=config.rec,
                                           space=config.space)

        raw_fname_in = \
            op.join(deriv_path, bids_basename + '_filt_raw.fif')

        raw = mne.io.read_raw_fif(raw_fname_in, preload=True)
        raw_list.append(raw)

    msg = 'Concatenating runs'
    logger.info(
        gen_log_message(message=msg, step=4, subject=subject, session=session))
    raw = mne.concatenate_raws(raw_list)

    events, event_id = mne.events_from_annotations(raw)

    if config.get_kind() == 'eeg':
        raw.set_eeg_reference(projection=True)
    del raw_list

    # don't reject based on EOG to keep blink artifacts
    # in the ICA computation.
    reject_ica = config.get_reject()
    if reject_ica and 'eog' in reject_ica:
        reject_ica = dict(reject_ica)
        del reject_ica['eog']

    # produce high-pass filtered version of the data for ICA
    raw_ica = raw.copy().filter(l_freq=1., h_freq=None)
    epochs_for_ica = mne.Epochs(raw_ica,
                                events,
                                event_id,
                                config.tmin,
                                config.tmax,
                                proj=True,
                                baseline=config.baseline,
                                preload=True,
                                decim=config.decim,
                                reject=reject_ica)

    # run ICA on MEG and EEG
    picks_meg = mne.pick_types(epochs_for_ica.info,
                               meg=True,
                               eeg=False,
                               eog=False,
                               stim=False,
                               exclude='bads')
    picks_eeg = mne.pick_types(epochs_for_ica.info,
                               meg=False,
                               eeg=True,
                               eog=False,
                               stim=False,
                               exclude='bads')
    all_picks = {'meg': picks_meg, 'eeg': picks_eeg}

    # get number of components for ICA
    # compute_rank requires 0.18
    # n_components_meg = (mne.compute_rank(epochs_for_ica.copy()
    #                        .pick_types(meg=True)))['meg']

    n_components_meg = 0.999

    n_components = {'meg': n_components_meg, 'eeg': 0.999}

    kind = config.get_kind()
    msg = f'Running ICA for {kind}'
    logger.info(
        gen_log_message(message=msg, step=4, subject=subject, session=session))

    if config.ica_algorithm == 'picard':
        fit_params = dict(fastica_it=5)
    elif config.ica_algorithm == 'extended_infomax':
        fit_params = dict(extended=True)
    elif config.ica_algorithm == 'fastica':
        fit_params = None

    ica = ICA(method=config.ica_algorithm,
              random_state=config.random_state,
              n_components=n_components[kind],
              fit_params=fit_params,
              max_iter=config.ica_max_iterations)

    picks = all_picks[kind]
    if picks.size == 0:
        ica.fit(epochs_for_ica, decim=config.ica_decim)
    else:
        ica.fit(epochs_for_ica, picks=picks, decim=config.ica_decim)

    msg = (f'Fit {ica.n_components_} components (explaining at least '
           f'{100*n_components[kind]:.1f}% of the variance)')
    logger.info(
        gen_log_message(message=msg, step=4, subject=subject, session=session))

    # Load ICA
    ica_fname = \
        op.join(deriv_path, bids_basename + '_%s-ica.fif' % kind)

    ica.save(ica_fname)

    if config.interactive:
        # plot ICA components to html report
        report_fname = \
            op.join(deriv_path, f'bids_basename_{kind}-ica.html')

        report = Report(report_fname, verbose=False)

        for idx in range(0, ica.n_components_):
            figure = ica.plot_properties(epochs_for_ica,
                                         picks=idx,
                                         psd_args={'fmax': 60},
                                         show=False)

            report.add_figs_to_section(figure,
                                       section=subject,
                                       captions=(kind.upper() +
                                                 ' - ICA Components'))

        report.save(report_fname, overwrite=True, open_browser=False)
Exemple #19
0
def run_ica(subject, tsss=config.mf_st_duration):
    print("Processing subject: %s" % subject)

    meg_subject_dir = op.join(config.meg_dir, subject)

    raw_list = list()
    print("  Loading raw data")
    runs = config.runs_dict[subject]

    for run in runs[-4:-1]:  # load four last runs
        if config.use_maxwell_filter:
            extension = run + '_sss_raw'
        else:
            extension = run + '_filt_raw'

        raw_fname_in = op.join(meg_subject_dir,
                               config.base_fname.format(**locals()))

        raw = mne.io.read_raw_fif(raw_fname_in, preload=True)
        raw_list.append(raw)

    print('  Concatenating runs')
    raw = mne.concatenate_raws(raw_list)
    if "eeg" in config.ch_types:
        raw.set_eeg_reference(projection=True)
    del raw_list

    # don't reject based on EOG to keep blink artifacts
    # in the ICA computation.
    reject_ica = config.reject
    if reject_ica and 'eog' in reject_ica:
        reject_ica = dict(reject_ica)
        del reject_ica['eog']

    # produce high-pass filtered version of the data for ICA
    raw_ica = raw.copy().filter(l_freq=1., h_freq=None)

    print("  Running ICA...")

    picks_meg = mne.pick_types(raw.info,
                               meg=True,
                               eeg=False,
                               eog=False,
                               stim=False,
                               exclude='bads')
    picks_eeg = mne.pick_types(raw.info,
                               meg=False,
                               eeg=True,
                               eog=False,
                               stim=False,
                               exclude='bads')
    all_picks = {'meg': picks_meg, 'eeg': picks_eeg}

    n_components = {'meg': 0.999, 'eeg': 0.999}

    ch_types = []
    if 'eeg' in config.ch_types:
        ch_types.append('eeg')
    if set(config.ch_types).intersection(('meg', 'grad', 'mag')):
        ch_types.append('meg')

    for ch_type in ch_types:
        print('Running ICA for ' + ch_type)

        ica = ICA(method='fastica',
                  random_state=config.random_state,
                  n_components=n_components[ch_type])

        picks = all_picks[ch_type]

        ica.fit(raw, picks=picks, decim=config.ica_decim)

        print('  Fit %d components (explaining at least %0.1f%% of the'
              ' variance)' % (ica.n_components_, 100 * n_components[ch_type]))

        ica_fname = \
            '{0}_{1}_{2}-ica.fif'.format(subject, config.study_name, ch_type)
        ica_fname = op.join(meg_subject_dir, ica_fname)
        ica.save(ica_fname)

        # if config.plot:

        # plot ICA components to html report
        report_fname = \
            '{0}_{1}_{2}-ica.h5'.format(subject, config.study_name,
                                          ch_type)
        report_fname = op.join(meg_subject_dir, report_fname)
        report_fname_html = \
            '{0}_{1}_{2}-ica.html'.format(subject, config.study_name,
                                          ch_type)
        report_fname = op.join(meg_subject_dir, report_fname)
        report = Report(report_fname, verbose=False)

        for idx in range(0, ica.n_components_):
            figure = ica.plot_properties(raw,
                                         picks=idx,
                                         psd_args={'fmax': 60},
                                         show=False)

            report.add_figs_to_section(figure,
                                       section=subject,
                                       captions=(ch_type.upper() +
                                                 ' - ICA Components'))

        report.save(report_fname, overwrite=True)
        report.save(report_fname_html, overwrite=True)
def apply_ica(*, cfg, subject, session):
    bids_basename = BIDSPath(subject=subject,
                             session=session,
                             task=cfg.task,
                             acquisition=cfg.acq,
                             run=None,
                             recording=cfg.rec,
                             space=cfg.space,
                             datatype=cfg.datatype,
                             root=cfg.deriv_root,
                             check=False)

    fname_epo_in = bids_basename.copy().update(suffix='epo', extension='.fif')
    fname_epo_out = bids_basename.copy().update(
        processing='ica', suffix='epo', extension='.fif')
    fname_ica = bids_basename.copy().update(suffix='ica', extension='.fif')
    fname_ica_components = bids_basename.copy().update(
        processing='ica', suffix='components', extension='.tsv')

    report_fname = (bids_basename.copy()
                    .update(processing='ica', suffix='report',
                            extension='.html'))

    title = f'ICA artifact removal – sub-{subject}'
    if session is not None:
        title += f', ses-{session}'
    if cfg.task is not None:
        title += f', task-{cfg.task}'
    report = Report(report_fname, title=title, verbose=False)

    # Load ICA.
    msg = f'Reading ICA: {fname_ica}'
    logger.debug(**gen_log_kwargs(message=msg, subject=subject,
                                  session=session))
    ica = read_ica(fname=fname_ica)

    # Select ICs to remove.
    tsv_data = pd.read_csv(fname_ica_components, sep='\t')
    ica.exclude = (tsv_data
                   .loc[tsv_data['status'] == 'bad', 'component']
                   .to_list())

    # Load epochs to reject ICA components.
    msg = f'Input: {fname_epo_in}, Output: {fname_epo_out}'
    logger.info(**gen_log_kwargs(message=msg, subject=subject,
                                 session=session))

    epochs = mne.read_epochs(fname_epo_in, preload=True)
    epochs.drop_bad(cfg.ica_reject)

    # Compare ERP/ERF before and after ICA artifact rejection. The evoked
    # response is calculated across ALL epochs, just like ICA was run on
    # all epochs, regardless of their respective experimental condition.
    #
    # Note that up until now, we haven't actually rejected any ICs from the
    # epochs.
    #
    # We apply baseline correction here to (hopefully!) make the effects of
    # ICA easier to see. Otherwise, individual channels might just have
    # arbitrary DC shifts, and we wouldn't be able to easily decipher what's
    # going on!
    evoked = epochs.average().apply_baseline(cfg.baseline)

    # Plot source time course
    fig = ica.plot_sources(evoked, show=cfg.interactive)
    report.add_figs_to_section(figs=fig,
                               captions='All ICs - Source time course')

    # Plot original & corrected data
    fig = ica.plot_overlay(evoked, show=cfg.interactive)
    report.add_figs_to_section(figs=fig,
                               captions=f'Evoked response (across all epochs) '
                                        f'before and after cleaning via ICA '
                                        f'({len(ica.exclude)} ICs removed)')
    report.save(report_fname, overwrite=True, open_browser=False)

    # Now actually reject the components.
    msg = f'Rejecting ICs: {", ".join([str(ic) for ic in ica.exclude])}'
    logger.info(**gen_log_kwargs(message=msg, subject=subject,
                                 session=session))
    epochs_cleaned = ica.apply(epochs.copy())  # Copy b/c works in-place!

    msg = 'Saving reconstructed epochs after ICA.'
    logger.info(**gen_log_kwargs(message=msg, subject=subject,
                                 session=session))
    epochs_cleaned.save(fname_epo_out, overwrite=True)

    if cfg.interactive:
        epochs_cleaned.plot_image(combine='gfp', sigma=2., cmap="YlGnBu_r")
Exemple #21
0
def _generate_report(raw, ica, subj_name, basename, ecg_evoked, ecg_scores,
                     ecg_inds, ecg_ch_name, eog_evoked, eog_scores, eog_inds,
                     eog_ch_name):
    """Generate report for ica solution."""
    import matplotlib.pyplot as plt

    report = Report()

    ica_title = 'Sources related to %s artifacts (red)'
    is_show = False

    # ------------------- Generate report for ECG ------------------------ #
    fig_ecg_scores = ica.plot_scores(ecg_scores,
                                     exclude=ecg_inds,
                                     title=ica_title % 'ecg',
                                     show=is_show)

    # Pick the five largest ecg_scores and plot them
    show_picks = np.abs(ecg_scores).argsort()[::-1][:5]

    # Plot estimated latent sources given the unmixing matrix.
    fig_ecg_ts = ica.plot_sources(raw,
                                  show_picks,
                                  exclude=ecg_inds,
                                  title=ica_title % 'ecg' + ' in 30s',
                                  start=0,
                                  stop=30,
                                  show=is_show)

    # topoplot of unmixing matrix columns
    fig_ecg_comp = ica.plot_components(show_picks,
                                       title=ica_title % 'ecg',
                                       colorbar=True,
                                       show=is_show)

    # plot ECG sources + selection
    fig_ecg_src = ica.plot_sources(ecg_evoked, exclude=ecg_inds, show=is_show)
    fig = [fig_ecg_scores, fig_ecg_ts, fig_ecg_comp, fig_ecg_src]
    report.add_figs_to_section(fig,
                               captions=[
                                   'Scores of ICs related to ECG',
                                   'Time Series plots of ICs (ECG)',
                                   'TopoMap of ICs (ECG)',
                                   'Time-locked ECG sources'
                               ],
                               section='ICA - ECG')
    # -------------------- end generate report for ECG ---------------------- #

    # -------------------------- Generate report for EoG -------------------- #
    # check how many EoG ch we have
    if set(eog_ch_name.split(',')).issubset(set(raw.info['ch_names'])):
        fig_eog_scores = ica.plot_scores(eog_scores,
                                         exclude=eog_inds,
                                         title=ica_title % 'eog',
                                         show=is_show)

        report.add_figs_to_section(fig_eog_scores,
                                   captions=['Scores of ICs related to EOG'],
                                   section='ICA - EOG')

        n_eogs = np.shape(eog_scores)
        if len(n_eogs) > 1:
            n_eog0 = n_eogs[0]
            show_picks = [
                np.abs(eog_scores[i][:]).argsort()[::-1][:5]
                for i in range(n_eog0)
            ]
            for i in range(n_eog0):
                fig_eog_comp = ica.plot_components(show_picks[i][:],
                                                   title=ica_title % 'eog',
                                                   colorbar=True,
                                                   show=is_show)

                fig = [fig_eog_comp]
                report.add_figs_to_section(fig,
                                           captions=['Scores of EoG ICs'],
                                           section='ICA - EOG')
        else:
            show_picks = np.abs(eog_scores).argsort()[::-1][:5]
            fig_eog_comp = ica.plot_components(show_picks,
                                               title=ica_title % 'eog',
                                               colorbar=True,
                                               show=is_show)

            fig = [fig_eog_comp]
            report.add_figs_to_section(fig,
                                       captions=['TopoMap of ICs (EOG)'],
                                       section='ICA - EOG')

        fig_eog_src = ica.plot_sources(eog_evoked,
                                       exclude=eog_inds,
                                       show=is_show)

        fig = [fig_eog_src]
        report.add_figs_to_section(fig,
                                   captions=['Time-locked EOG sources'],
                                   section='ICA - EOG')
    # ----------------- end generate report for EoG ---------- #
    ic_nums = list(range(ica.n_components_))
    fig = ica.plot_components(picks=ic_nums, show=False)
    report.add_figs_to_section(fig,
                               captions=['All IC topographies'],
                               section='ICA - muscles')

    fig = ica.plot_sources(raw,
                           start=0,
                           stop=None,
                           show=False,
                           title='All IC time series')
    report.add_figs_to_section(fig,
                               captions=['All IC time series'],
                               section='ICA - muscles')

    psds_fig = []
    captions_psd = []
    ica_src = ica.get_sources(raw)
    for i_ic in ic_nums:

        psds, freqs = psd_multitaper(ica_src, picks=i_ic, fmax=140, tmax=60)
        psds = np.squeeze(psds)

        f, ax = plt.subplots()
        psds = 10 * np.log10(psds)

        ax.plot(freqs, psds, color='k')
        ax.set(title='PSD',
               xlabel='Frequency',
               ylabel='Power Spectral Density (dB)')

        psds_fig.append(f)
        captions_psd.append('IC #' + str(i_ic))

    report.add_figs_to_section(figs=psds_fig,
                               captions=captions_psd,
                               section='ICA - muscles')

    report_filename = os.path.join(basename + "-report.html")
    print(('******* ' + report_filename))
    report.save(report_filename, open_browser=False, overwrite=True)
    return report_filename
def apply_ica(subject, session):
    bids_basename = BIDSPath(subject=subject,
                             session=session,
                             task=config.get_task(),
                             acquisition=config.acq,
                             run=None,
                             recording=config.rec,
                             space=config.space,
                             datatype=config.get_datatype(),
                             root=config.deriv_root,
                             check=False)

    fname_epo_in = bids_basename.copy().update(suffix='epo', extension='.fif')
    fname_epo_out = bids_basename.copy().update(suffix='epo',
                                                processing='clean',
                                                extension='.fif')
    fname_ica = bids_basename.copy().update(suffix='ica', extension='.fif')
    fname_ica_components = bids_basename.copy().update(processing='ica',
                                                       suffix='components',
                                                       extension='.tsv')

    # Load epochs to reject ICA components.
    epochs = mne.read_epochs(fname_epo_in, preload=True)

    msg = f'Input: {fname_epo_in}, Output: {fname_epo_out}'
    logger.info(
        gen_log_message(message=msg, step=5, subject=subject, session=session))

    report_fname = (bids_basename.copy().update(processing='clean',
                                                suffix='report',
                                                extension='.html'))
    report = Report(report_fname, verbose=False)

    # Load ICA.
    msg = f'Reading ICA: {fname_ica}'
    logger.debug(
        gen_log_message(message=msg, step=5, subject=subject, session=session))
    ica = read_ica(fname=fname_ica)

    # Select ICs to remove.
    tsv_data = pd.read_csv(fname_ica_components, sep='\t')
    ica.exclude = (tsv_data.loc[tsv_data['status'] == 'bad',
                                'component'].to_list())

    # Compare ERP/ERF before and after ICA artifact rejection. The evoked
    # response is calculated across ALL epochs, just like ICA was run on
    # all epochs, regardless of their respective experimental condition.
    #
    # Note that up until now, we haven't actually rejected any ICs from the
    # epochs.
    evoked = epochs.average()

    # Plot source time course
    fig = ica.plot_sources(evoked, show=config.interactive)
    report.add_figs_to_section(figs=fig,
                               captions='All ICs - Source time course')

    # Plot original & corrected data
    fig = ica.plot_overlay(evoked, show=config.interactive)
    report.add_figs_to_section(figs=fig,
                               captions=f'Evoked response (across all epochs) '
                               f'before and after cleaning via ICA '
                               f'({len(ica.exclude)} ICs removed)')
    report.save(report_fname, overwrite=True, open_browser=False)

    # Now actually reject the components.
    msg = f'Rejecting ICs: {ica.exclude}'
    logger.info(
        gen_log_message(message=msg, step=5, subject=subject, session=session))
    epochs_cleaned = ica.apply(epochs.copy())  # Copy b/c works in-place!
    epochs_cleaned.apply_baseline(config.baseline)

    msg = 'Saving cleaned epochs.'
    logger.info(
        gen_log_message(message=msg, step=5, subject=subject, session=session))
    epochs_cleaned.save(fname_epo_out, overwrite=True)

    if config.interactive:
        epochs_cleaned.plot_image(combine='gfp', sigma=2., cmap="YlGnBu_r")
def run():
    """Run command."""
    from mne.commands.utils import get_optparser

    parser = get_optparser(__file__)

    parser.add_option("-p", "--path", dest="path",
                      help="Path to folder who MNE-Report must be created")
    parser.add_option("-i", "--info", dest="info_fname",
                      help="File from which info dictionary is to be read",
                      metavar="FILE")
    parser.add_option("-c", "--cov", dest="cov_fname",
                      help="File from which noise covariance is to be read",
                      metavar="FILE")
    parser.add_option("--bmin", dest="bmin",
                      help="Time at which baseline correction starts for "
                      "evokeds", default=None)
    parser.add_option("--bmax", dest="bmax",
                      help="Time at which baseline correction stops for "
                      "evokeds", default=None)
    parser.add_option("-d", "--subjects-dir", dest="subjects_dir",
                      help="The subjects directory")
    parser.add_option("-s", "--subject", dest="subject",
                      help="The subject name")
    parser.add_option("--no-browser", dest="no_browser", action='store_false',
                      help="Do not open MNE-Report in browser")
    parser.add_option("--overwrite", dest="overwrite", action='store_false',
                      help="Overwrite html report if it already exists")
    parser.add_option("-j", "--jobs", dest="n_jobs", help="Number of jobs to"
                      " run in parallel")
    parser.add_option("-m", "--mri-decim", type="int", dest="mri_decim",
                      default=2, help="Integer factor used to decimate "
                      "BEM plots")
    parser.add_option("--image-format", type="str", dest="image_format",
                      default='png', help="Image format to use "
                      "(can be 'png' or 'svg')")
    parser.add_option("-v", "--verbose", dest="verbose",
                      action='store_true', help="run in verbose mode")

    options, args = parser.parse_args()
    path = options.path
    if path is None:
        parser.print_help()
        sys.exit(1)
    info_fname = options.info_fname
    cov_fname = options.cov_fname
    subjects_dir = options.subjects_dir
    subject = options.subject
    image_format = options.image_format
    mri_decim = int(options.mri_decim)
    verbose = True if options.verbose is not None else False
    open_browser = False if options.no_browser is not None else True
    overwrite = True if options.overwrite is not None else False
    n_jobs = int(options.n_jobs) if options.n_jobs is not None else 1

    bmin = float(options.bmin) if options.bmin is not None else None
    bmax = float(options.bmax) if options.bmax is not None else None
    # XXX: this means (None, None) cannot be specified through command line
    if bmin is None and bmax is None:
        baseline = None
    else:
        baseline = (bmin, bmax)

    t0 = time.time()
    report = Report(info_fname, subjects_dir=subjects_dir,
                    subject=subject, baseline=baseline,
                    cov_fname=cov_fname, verbose=verbose,
                    image_format=image_format)
    report.parse_folder(path, verbose=verbose, n_jobs=n_jobs,
                        mri_decim=mri_decim)
    log_elapsed(time.time() - t0, verbose=verbose)
    report.save(open_browser=open_browser, overwrite=overwrite)
Exemple #24
0
def run_ica(subject, session=None):
    """Run ICA."""
    print("Processing subject: %s" % subject)

    # Construct the search path for the data file. `sub` is mandatory
    subject_path = op.join('sub-{}'.format(subject))
    # `session` is optional
    if session is not None:
        subject_path = op.join(subject_path, 'ses-{}'.format(session))

    subject_path = op.join(subject_path, config.kind)

    fpath_deriv = op.join(config.bids_root, 'derivatives',
                          config.PIPELINE_NAME, subject_path)

    raw_list = list()
    print("  Loading raw data")

    for run in config.runs:
        # load first run of raw data for ecg /eog epochs
        print("  Loading one run from raw data")

        bids_basename = make_bids_basename(subject=subject,
                                           session=session,
                                           task=config.task,
                                           acquisition=config.acq,
                                           run=run,
                                           processing=config.proc,
                                           recording=config.rec,
                                           space=config.space)

        if config.use_maxwell_filter:
            raw_fname_in = \
                op.join(fpath_deriv, bids_basename + '_sss_raw.fif')
        else:
            raw_fname_in = \
                op.join(fpath_deriv, bids_basename + '_filt_raw.fif')

        eve_fname = \
            op.join(fpath_deriv, bids_basename + '-eve.fif')

        print("Input: ", raw_fname_in, eve_fname)

        raw = mne.io.read_raw_fif(raw_fname_in, preload=True)

        raw_list.append(raw)

    print('  Concatenating runs')
    raw = mne.concatenate_raws(raw_list)

    events, event_id = mne.events_from_annotations(raw)

    if "eeg" in config.ch_types or config.kind == 'eeg':
        raw.set_eeg_reference(projection=True)
    del raw_list

    # don't reject based on EOG to keep blink artifacts
    # in the ICA computation.
    reject_ica = config.reject
    if reject_ica and 'eog' in reject_ica:
        reject_ica = dict(reject_ica)
        del reject_ica['eog']

    # produce high-pass filtered version of the data for ICA
    raw_ica = raw.copy().filter(l_freq=1., h_freq=None)

    print("  Running ICA...")
    epochs_for_ica = mne.Epochs(raw_ica,
                                events,
                                event_id,
                                config.tmin,
                                config.tmax,
                                proj=True,
                                baseline=config.baseline,
                                preload=True,
                                decim=config.decim,
                                reject=reject_ica)

    # run ICA on MEG and EEG
    picks_meg = mne.pick_types(epochs_for_ica.info,
                               meg=True,
                               eeg=False,
                               eog=False,
                               stim=False,
                               exclude='bads')
    picks_eeg = mne.pick_types(epochs_for_ica.info,
                               meg=False,
                               eeg=True,
                               eog=False,
                               stim=False,
                               exclude='bads')
    all_picks = {'meg': picks_meg, 'eeg': picks_eeg}

    # get number of components for ICA
    # compute_rank requires 0.18
    # n_components_meg = (mne.compute_rank(epochs_for_ica.copy()
    #                        .pick_types(meg=True)))['meg']

    n_components_meg = 0.999

    n_components = {'meg': n_components_meg, 'eeg': 0.999}

    ch_types = []
    if 'eeg' in config.ch_types or config.kind == 'eeg':
        ch_types.append('eeg')
    if set(config.ch_types).intersection(('meg', 'grad', 'mag')):
        ch_types.append('meg')

    for ch_type in ch_types:
        print('Running ICA for ' + ch_type)

        ica = ICA(method='fastica',
                  random_state=config.random_state,
                  n_components=n_components[ch_type])

        picks = all_picks[ch_type]
        if picks.size == 0:
            ica.fit(epochs_for_ica, decim=config.ica_decim)
        else:
            ica.fit(epochs_for_ica, picks=picks, decim=config.ica_decim)

        print('  Fit %d components (explaining at least %0.1f%% of the'
              ' variance)' % (ica.n_components_, 100 * n_components[ch_type]))

        # Load ICA
        ica_fname = \
            op.join(fpath_deriv, bids_basename + '_%s-ica.fif' % ch_type)

        ica.save(ica_fname)

        if config.plot:
            # plot ICA components to html report
            report_fname = \
                op.join(fpath_deriv,
                        bids_basename + '_%s-ica.html' % ch_type)

            report = Report(report_fname, verbose=False)

            for idx in range(0, ica.n_components_):
                figure = ica.plot_properties(epochs_for_ica,
                                             picks=idx,
                                             psd_args={'fmax': 60},
                                             show=False)

                report.add_figs_to_section(figure,
                                           section=subject,
                                           captions=(ch_type.upper() +
                                                     ' - ICA Components'))

            report.save(report_fname, overwrite=True, open_browser=False)
Exemple #25
0
def test_render_report(renderer, tmpdir):
    """Test rendering *.fif files for mne report."""
    tempdir = str(tmpdir)
    raw_fname_new = op.join(tempdir, 'temp_raw.fif')
    raw_fname_new_bids = op.join(tempdir, 'temp_meg.fif')
    ms_fname_new = op.join(tempdir, 'temp_ms_raw.fif')
    event_fname_new = op.join(tempdir, 'temp_raw-eve.fif')
    cov_fname_new = op.join(tempdir, 'temp_raw-cov.fif')
    proj_fname_new = op.join(tempdir, 'temp_ecg-proj.fif')
    fwd_fname_new = op.join(tempdir, 'temp_raw-fwd.fif')
    inv_fname_new = op.join(tempdir, 'temp_raw-inv.fif')
    for a, b in [[raw_fname, raw_fname_new], [raw_fname, raw_fname_new_bids],
                 [ms_fname, ms_fname_new], [event_fname, event_fname_new],
                 [cov_fname, cov_fname_new], [proj_fname, proj_fname_new],
                 [fwd_fname, fwd_fname_new], [inv_fname, inv_fname_new]]:
        shutil.copyfile(a, b)

    # create and add -epo.fif and -ave.fif files
    epochs_fname = op.join(tempdir, 'temp-epo.fif')
    evoked_fname = op.join(tempdir, 'temp-ave.fif')
    # Speed it up by picking channels
    raw = read_raw_fif(raw_fname_new, preload=True)
    raw.pick_channels(['MEG 0111', 'MEG 0121', 'EEG 001', 'EEG 002'])
    raw.del_proj()
    raw.set_eeg_reference(projection=True)
    epochs = Epochs(raw, read_events(event_fname), 1, -0.2, 0.2)
    epochs.save(epochs_fname, overwrite=True)
    # This can take forever, so let's make it fast
    # Also, make sure crop range is wide enough to avoid rendering bug
    evoked = epochs.average()
    with pytest.warns(RuntimeWarning, match='tmax is not in Evoked'):
        evoked.crop(0.1, 0.2)
    evoked.save(evoked_fname)

    report = Report(info_fname=raw_fname_new,
                    subjects_dir=subjects_dir,
                    projs=True)
    with pytest.warns(RuntimeWarning, match='Cannot render MRI'):
        report.parse_folder(data_path=tempdir, on_error='raise')
    assert repr(report)

    # Check correct paths and filenames
    fnames = glob.glob(op.join(tempdir, '*.fif'))
    for fname in fnames:
        assert (op.basename(fname) in [op.basename(x) for x in report.fnames])
        assert (''.join(report.html).find(op.basename(fname)) != -1)

    assert len(report.fnames) == len(fnames)
    assert len(report.html) == len(report.fnames)
    assert len(report.fnames) == len(report)

    # Check saving functionality
    report.data_path = tempdir
    fname = op.join(tempdir, 'report.html')
    report.save(fname=fname, open_browser=False)
    assert (op.isfile(fname))
    with open(fname, 'rb') as fid:
        html = fid.read().decode('utf-8')
    assert '(MaxShield on)' in html
    # Projectors in Raw.info
    assert '<h4>SSP Projectors</h4>' in html
    # Projectors in `proj_fname_new`
    assert f'SSP Projectors: {op.basename(proj_fname_new)}' in html
    # Evoked in `evoked_fname`
    assert f'Evoked: {op.basename(evoked_fname)} ({evoked.comment})' in html
    assert 'Topomap (ch_type =' in html
    assert f'Evoked: {op.basename(evoked_fname)} (GFPs)' in html

    assert len(report.html) == len(fnames)
    assert len(report.html) == len(report.fnames)

    # Check saving same report to new filename
    report.save(fname=op.join(tempdir, 'report2.html'), open_browser=False)
    assert (op.isfile(op.join(tempdir, 'report2.html')))

    # Check overwriting file
    report.save(fname=op.join(tempdir, 'report.html'),
                open_browser=False,
                overwrite=True)
    assert (op.isfile(op.join(tempdir, 'report.html')))

    # Check pattern matching with multiple patterns
    pattern = ['*raw.fif', '*eve.fif']
    with pytest.warns(RuntimeWarning, match='Cannot render MRI'):
        report.parse_folder(data_path=tempdir, pattern=pattern)
    assert (repr(report))

    fnames = glob.glob(op.join(tempdir, '*.raw')) + \
        glob.glob(op.join(tempdir, '*.raw'))
    for fname in fnames:
        assert (op.basename(fname) in [op.basename(x) for x in report.fnames])
        assert (''.join(report.html).find(op.basename(fname)) != -1)

    pytest.raises(ValueError, Report, image_format='foo')
    pytest.raises(ValueError, Report, image_format=None)

    # SVG rendering
    report = Report(info_fname=raw_fname_new,
                    subjects_dir=subjects_dir,
                    image_format='svg')
    tempdir = pathlib.Path(tempdir)  # test using pathlib.Path
    with pytest.warns(RuntimeWarning, match='Cannot render MRI'):
        report.parse_folder(data_path=tempdir, on_error='raise')

    # ndarray support smoke test
    report.add_figs_to_section(np.zeros((2, 3, 3)), 'caption', 'section')

    with pytest.raises(TypeError, match='figure must be a'):
        report.add_figs_to_section('foo', 'caption', 'section')
    with pytest.raises(TypeError, match='figure must be a'):
        report.add_figs_to_section(['foo'], 'caption', 'section')
Exemple #26
0
def apply_ica(subject):
    print("Processing subject: %s" % subject)
    meg_subject_dir = op.join(config.meg_dir, subject)

    # load epochs to reject ICA components
    extension = '-epo'
    fname_in = op.join(meg_subject_dir, config.base_fname.format(**locals()))
    epochs = mne.read_epochs(fname_in, preload=True)

    extension = '_cleaned-epo'
    fname_out = op.join(meg_subject_dir, config.base_fname.format(**locals()))

    print("Input: ", fname_in)
    print("Output: ", fname_out)

    # load first run of raw data for ecg /eog epochs
    raw_list = list()
    print("  Loading one run from raw data")
    extension = config.runs[0] + '_sss_raw'
    raw_fname_in = op.join(meg_subject_dir,
                           config.base_fname.format(**locals()))
    raw = mne.io.read_raw_fif(raw_fname_in, preload=True)

    # run ICA on MEG and EEG
    picks_meg = mne.pick_types(raw.info,
                               meg=True,
                               eeg=False,
                               eog=False,
                               stim=False,
                               exclude='bads')
    picks_eeg = mne.pick_types(raw.info,
                               meg=False,
                               eeg=True,
                               eog=False,
                               stim=False,
                               exclude='bads')
    all_picks = {'meg': picks_meg, 'eeg': picks_eeg}

    ch_types = []
    if 'eeg' in config.ch_types:
        ch_types.append('eeg')
    if set(config.ch_types).intersection(('meg', 'grad', 'mag')):
        ch_types.append('meg')

    for ch_type in ch_types:
        print(ch_type)
        picks = all_picks[ch_type]

        # Load ICA
        fname_ica = op.join(
            meg_subject_dir,
            '{0}_{1}_{2}-ica.fif'.format(subject, config.study_name, ch_type))
        print('Reading ICA: ' + fname_ica)
        ica = read_ica(fname=fname_ica)

        pick_ecg = mne.pick_types(raw.info,
                                  meg=False,
                                  eeg=False,
                                  ecg=True,
                                  eog=False)

        # ECG
        # either needs an ecg channel, or avg of the mags (i.e. MEG data)
        if pick_ecg or ch_type == 'meg':

            picks_ecg = np.concatenate([picks, pick_ecg])

            # Create ecg epochs
            if ch_type == 'meg':
                reject = {
                    'mag': config.reject['mag'],
                    'grad': config.reject['grad']
                }
            elif ch_type == 'eeg':
                reject = {'eeg': config.reject['eeg']}

            ecg_epochs = create_ecg_epochs(raw,
                                           picks=picks_ecg,
                                           reject=reject,
                                           baseline=(None, 0),
                                           tmin=-0.5,
                                           tmax=0.5)

            ecg_average = ecg_epochs.average()

            ecg_inds, scores = \
                ica.find_bads_ecg(ecg_epochs, method='ctps',
                                  threshold=config.ica_ctps_ecg_threshold)
            ica.exclude.extend(ecg_inds)

            del ecg_epochs

            report_fname = \
                '{0}_{1}_{2}-reject_ica.html'.format(subject,
                                                     config.study_name,
                                                     ch_type)
            report_fname = op.join(meg_subject_dir, report_fname)
            report = Report(report_fname, verbose=False)

            # Plot r score
            report.add_figs_to_section(ica.plot_scores(scores,
                                                       show=config.plot),
                                       captions=ch_type.upper() + ' - ECG - ' +
                                       'R scores')

            # Plot source time course
            report.add_figs_to_section(ica.plot_sources(ecg_average,
                                                        show=config.plot),
                                       captions=ch_type.upper() + ' - ECG - ' +
                                       'Sources time course')

            # Plot source time course
            report.add_figs_to_section(ica.plot_overlay(ecg_average,
                                                        show=config.plot),
                                       captions=ch_type.upper() + ' - ECG - ' +
                                       'Corrections')

        else:
            # XXX : to check when EEG only is processed
            print('no ECG channel is present. Cannot automate ICAs component '
                  'detection for EOG!')

        # EOG
        pick_eog = mne.pick_types(raw.info,
                                  meg=False,
                                  eeg=False,
                                  ecg=False,
                                  eog=True)

        if pick_eog.any():
            print('using EOG channel')
            picks_eog = np.concatenate([picks, pick_eog])
            # Create eog epochs
            eog_epochs = create_eog_epochs(raw,
                                           picks=picks_eog,
                                           reject=None,
                                           baseline=(None, 0),
                                           tmin=-0.5,
                                           tmax=0.5)

            eog_average = eog_epochs.average()
            eog_inds, scores = \
                ica.find_bads_eog(eog_epochs,
                                  threshold=config.ica_correlation_eog_threshold)

            ica.exclude.extend(eog_inds)

            del eog_epochs

            # Plot r score
            report.add_figs_to_section(ica.plot_scores(scores,
                                                       show=config.plot),
                                       captions=ch_type.upper() + ' - EOG - ' +
                                       'R scores')

            # Plot source time course
            report.add_figs_to_section(ica.plot_sources(eog_average,
                                                        show=config.plot),
                                       captions=ch_type.upper() + ' - EOG - ' +
                                       'Sources time course')

            # Plot source time course
            report.add_figs_to_section(ica.plot_overlay(eog_average,
                                                        show=config.plot),
                                       captions=ch_type.upper() + ' - EOG - ' +
                                       'Corrections')

            report.save(report_fname, overwrite=True, open_browser=False)

        else:
            print('no EOG channel is present. Cannot automate ICAs component '
                  'detection for EOG!')

        rej_man = list(config.rejcomps_man[subject][ch_type])
        ica.exclude.extend(rej_man)

        # now reject the components
        print('Rejecting from %s: %s' % (ch_type, ica.exclude))
        epochs = ica.apply(epochs)

        print('Saving cleaned epochs')
        epochs.save(fname_out)

        fig = ica.plot_overlay(raw, show=config.plot)
        report.add_figs_to_section(fig,
                                   captions=ch_type.upper() +
                                   ' - ALL(epochs) - Corrections')

        report.save(report_fname, overwrite=True, open_browser=False)

        if config.plot:
            epochs.plot_image(combine='gfp',
                              sigma=2.,
                              cmap="YlGnBu_r",
                              show=config.plot)
Exemple #27
0
def test_render_report():
    """Test rendering -*.fif files for mne report."""
    tempdir = _TempDir()
    raw_fname_new = op.join(tempdir, 'temp_raw.fif')
    ms_fname_new = op.join(tempdir, 'temp_ms_raw.fif')
    event_fname_new = op.join(tempdir, 'temp_raw-eve.fif')
    cov_fname_new = op.join(tempdir, 'temp_raw-cov.fif')
    fwd_fname_new = op.join(tempdir, 'temp_raw-fwd.fif')
    inv_fname_new = op.join(tempdir, 'temp_raw-inv.fif')
    for a, b in [[raw_fname, raw_fname_new],
                 [ms_fname, ms_fname_new],
                 [event_fname, event_fname_new],
                 [cov_fname, cov_fname_new],
                 [fwd_fname, fwd_fname_new],
                 [inv_fname, inv_fname_new]]:
        shutil.copyfile(a, b)

    # create and add -epo.fif and -ave.fif files
    epochs_fname = op.join(tempdir, 'temp-epo.fif')
    evoked_fname = op.join(tempdir, 'temp-ave.fif')
    # Speed it up by picking channels
    raw = read_raw_fif(raw_fname_new, preload=True)
    raw.pick_channels(['MEG 0111', 'MEG 0121'])
    raw.del_proj()
    epochs = Epochs(raw, read_events(event_fname), 1, -0.2, 0.2)
    epochs.save(epochs_fname)
    # This can take forever (stall Travis), so let's make it fast
    # Also, make sure crop range is wide enough to avoid rendering bug
    epochs.average().crop(0.1, 0.2).save(evoked_fname)

    report = Report(info_fname=raw_fname_new, subjects_dir=subjects_dir)
    with pytest.warns(RuntimeWarning, match='Cannot render MRI'):
        report.parse_folder(data_path=tempdir, on_error='raise')
    assert repr(report)

    # Check correct paths and filenames
    fnames = glob.glob(op.join(tempdir, '*.fif'))
    for fname in fnames:
        assert (op.basename(fname) in
                [op.basename(x) for x in report.fnames])
        assert (''.join(report.html).find(op.basename(fname)) != -1)

    assert_equal(len(report.fnames), len(fnames))
    assert_equal(len(report.html), len(report.fnames))
    assert_equal(len(report.fnames), len(report))

    # Check saving functionality
    report.data_path = tempdir
    fname = op.join(tempdir, 'report.html')
    report.save(fname=fname, open_browser=False)
    assert (op.isfile(fname))
    with open(fname, 'rb') as fid:
        html = fid.read().decode('utf-8')
    assert '(MaxShield on)' in html

    assert_equal(len(report.html), len(fnames))
    assert_equal(len(report.html), len(report.fnames))

    # Check saving same report to new filename
    report.save(fname=op.join(tempdir, 'report2.html'), open_browser=False)
    assert (op.isfile(op.join(tempdir, 'report2.html')))

    # Check overwriting file
    report.save(fname=op.join(tempdir, 'report.html'), open_browser=False,
                overwrite=True)
    assert (op.isfile(op.join(tempdir, 'report.html')))

    # Check pattern matching with multiple patterns
    pattern = ['*raw.fif', '*eve.fif']
    with pytest.warns(RuntimeWarning, match='Cannot render MRI'):
        report.parse_folder(data_path=tempdir, pattern=pattern)
    assert (repr(report))

    fnames = glob.glob(op.join(tempdir, '*.raw')) + \
        glob.glob(op.join(tempdir, '*.raw'))
    for fname in fnames:
        assert (op.basename(fname) in
                [op.basename(x) for x in report.fnames])
        assert (''.join(report.html).find(op.basename(fname)) != -1)

    pytest.raises(ValueError, Report, image_format='foo')
    pytest.raises(ValueError, Report, image_format=None)

    # SVG rendering
    report = Report(info_fname=raw_fname_new, subjects_dir=subjects_dir,
                    image_format='svg')
    with pytest.warns(RuntimeWarning, match='Cannot render MRI'):
        report.parse_folder(data_path=tempdir, on_error='raise')

    # ndarray support smoke test
    report.add_figs_to_section(np.zeros((2, 3, 3)), 'caption', 'section')
Exemple #28
0
removed_frame = pd.DataFrame(index=part)
removed_frame['percleft_cue'] = 999
removed_frame['percleft_shock'] = 999
percleft_cue = []
percleft_shock = []

for p in part:
    # ______________________________________________________
    # Make out dir
    outdir = opj('/data/derivatives', p, 'eeg')
    if not os.path.exists(outdir):
        os.mkdir(outdir)

    # _______________________________________________________
    # Initialise MNE report
    report = Report(verbose=False, subject=p, title='TFR report for part ' + p)

    report.add_htmls_to_section(htmls=pprint.pformat(param),
                                captions='Parameters',
                                section='Parameters')

    # ______________________________________________________
    # Load cleaned raw file
    raw = mne.io.read_raw_fif(opj(outdir,
                                  p + '_task-fearcond_cleanedeeg_raw.fif'),
                              preload=True)

    # ______________________________________________________
    # Additional filter
    raw = raw.filter(l_freq=None, h_freq=200)
Exemple #29
0
def test_render_report():
    """Test rendering -*.fif files for mne report."""
    tempdir = _TempDir()
    raw_fname_new = op.join(tempdir, 'temp_raw.fif')
    event_fname_new = op.join(tempdir, 'temp_raw-eve.fif')
    cov_fname_new = op.join(tempdir, 'temp_raw-cov.fif')
    fwd_fname_new = op.join(tempdir, 'temp_raw-fwd.fif')
    inv_fname_new = op.join(tempdir, 'temp_raw-inv.fif')
    for a, b in [[raw_fname, raw_fname_new],
                 [event_fname, event_fname_new],
                 [cov_fname, cov_fname_new],
                 [fwd_fname, fwd_fname_new],
                 [inv_fname, inv_fname_new]]:
        shutil.copyfile(a, b)

    # create and add -epo.fif and -ave.fif files
    epochs_fname = op.join(tempdir, 'temp-epo.fif')
    evoked_fname = op.join(tempdir, 'temp-ave.fif')
    # Speed it up by picking channels
    raw = read_raw_fif(raw_fname_new, preload=True)
    raw.pick_channels(['MEG 0111', 'MEG 0121'])
    epochs = Epochs(raw, read_events(event_fname), 1, -0.2, 0.2)
    epochs.save(epochs_fname)
    # This can take forever (stall Travis), so let's make it fast
    # Also, make sure crop range is wide enough to avoid rendering bug
    epochs.average().crop(0.1, 0.2).save(evoked_fname)

    report = Report(info_fname=raw_fname_new, subjects_dir=subjects_dir)
    with warnings.catch_warnings(record=True) as w:
        warnings.simplefilter('always')
        report.parse_folder(data_path=tempdir, on_error='raise')
    assert_true(len(w) >= 1)
    assert_true(repr(report))

    # Check correct paths and filenames
    fnames = glob.glob(op.join(tempdir, '*.fif'))
    for fname in fnames:
        assert_true(op.basename(fname) in
                    [op.basename(x) for x in report.fnames])
        assert_true(''.join(report.html).find(op.basename(fname)) != -1)

    assert_equal(len(report.fnames), len(fnames))
    assert_equal(len(report.html), len(report.fnames))
    assert_equal(len(report.fnames), len(report))

    # Check saving functionality
    report.data_path = tempdir
    report.save(fname=op.join(tempdir, 'report.html'), open_browser=False)
    assert_true(op.isfile(op.join(tempdir, 'report.html')))

    assert_equal(len(report.html), len(fnames))
    assert_equal(len(report.html), len(report.fnames))

    # Check saving same report to new filename
    report.save(fname=op.join(tempdir, 'report2.html'), open_browser=False)
    assert_true(op.isfile(op.join(tempdir, 'report2.html')))

    # Check overwriting file
    report.save(fname=op.join(tempdir, 'report.html'), open_browser=False,
                overwrite=True)
    assert_true(op.isfile(op.join(tempdir, 'report.html')))

    # Check pattern matching with multiple patterns
    pattern = ['*raw.fif', '*eve.fif']
    with warnings.catch_warnings(record=True) as w:
        warnings.simplefilter('always')
        report.parse_folder(data_path=tempdir, pattern=pattern)
    assert_true(len(w) >= 1)
    assert_true(repr(report))

    fnames = glob.glob(op.join(tempdir, '*.raw')) + \
        glob.glob(op.join(tempdir, '*.raw'))
    for fname in fnames:
        assert_true(op.basename(fname) in
                    [op.basename(x) for x in report.fnames])
        assert_true(''.join(report.html).find(op.basename(fname)) != -1)
Exemple #30
0
def run():
    from mne.commands.utils import get_optparser

    parser = get_optparser(__file__)

    parser.add_option("-p",
                      "--path",
                      dest="path",
                      help="Path to folder who MNE-Report must be created")
    parser.add_option("-i",
                      "--info",
                      dest="info_fname",
                      help="File from which info dictionary is to be read",
                      metavar="FILE")
    parser.add_option("-d",
                      "--subjects-dir",
                      dest="subjects_dir",
                      help="The subjects directory")
    parser.add_option("-s",
                      "--subject",
                      dest="subject",
                      help="The subject name")
    parser.add_option("-v",
                      "--verbose",
                      dest="verbose",
                      action='store_true',
                      help="run in verbose mode")
    parser.add_option("--no-browser",
                      dest="no_browser",
                      action='store_false',
                      help="Do not open MNE-Report in browser")
    parser.add_option("--overwrite",
                      dest="overwrite",
                      action='store_false',
                      help="Overwrite html report if it already exists")
    parser.add_option("-j",
                      "--jobs",
                      dest="n_jobs",
                      help="Number of jobs to"
                      " run in parallel")
    parser.add_option("-m",
                      "--mri-decim",
                      type="int",
                      dest="mri_decim",
                      default=2,
                      help="Integer factor used to decimate "
                      "BEM plots")

    options, args = parser.parse_args()
    path = options.path
    if path is None:
        parser.print_help()
        sys.exit(1)
    info_fname = options.info_fname
    subjects_dir = options.subjects_dir
    subject = options.subject
    mri_decim = int(options.mri_decim)
    verbose = True if options.verbose is not None else False
    open_browser = False if options.no_browser is not None else True
    overwrite = True if options.overwrite is not None else False
    n_jobs = int(options.n_jobs) if options.n_jobs is not None else 1

    t0 = time.time()
    report = Report(info_fname,
                    subjects_dir=subjects_dir,
                    subject=subject,
                    verbose=verbose)
    report.parse_folder(path,
                        verbose=verbose,
                        n_jobs=n_jobs,
                        mri_decim=mri_decim)
    log_elapsed(time.time() - t0, verbose=verbose)
    report.save(open_browser=open_browser, overwrite=overwrite)