Exemple #1
0
def test_add_slider_to_section():
    """Test adding a slider with a series of images to mne report."""
    tempdir = _TempDir()
    from matplotlib import pyplot as plt
    report = Report(info_fname=raw_fname,
                    subject='sample', subjects_dir=subjects_dir)
    section = 'slider_section'
    figs = list()
    figs.append(plt.figure())
    plt.plot([1, 2, 3])
    plt.close('all')
    figs.append(plt.figure())
    plt.plot([3, 2, 1])
    plt.close('all')
    report.add_slider_to_section(figs, section=section)
    report.save(op.join(tempdir, 'report.html'), open_browser=False)

    assert_raises(NotImplementedError, report.add_slider_to_section,
                  [figs, figs])
    assert_raises(ValueError, report.add_slider_to_section, figs, ['wug'])
    assert_raises(TypeError, report.add_slider_to_section, figs, 'wug')
    # need at least 2
    assert_raises(ValueError, report.add_slider_to_section, figs[:1], 'wug')

    # Smoke test that SVG w/unicode can be added
    report = Report()
    fig, ax = plt.subplots()
    ax.set_xlabel(u'μ')
    report.add_slider_to_section([fig] * 2, image_format='svg')
Exemple #2
0
    def copy_surfaces(sbj_id, mesh_files):
        import os
        import os.path as op
        from smri_params import sbj_dir
        from mne.report import Report

        report = Report()

        surf_names = ["brain_surface", "inner_skull_surface", "outer_skull_surface", "outer_skin_surface"]
        new_surf_names = ["brain.surf", "inner_skull.surf", "outer_skull.surf", "outer_skin.surf"]

        bem_dir = op.join(sbj_dir, sbj_id, "bem")
        surface_dir = op.join(sbj_dir, sbj_id, "bem/watershed")

        for i in range(len(surf_names)):
            os.system(
                "cp %s %s" % (op.join(surface_dir, sbj_id + "_" + surf_names[i]), op.join(bem_dir, new_surf_names[i]))
            )
            # op.join(bem_dir,sbj_id + '-' + new_surf_names[i])))

        report.add_bem_to_section(subject=sbj_id, subjects_dir=sbj_dir)
        report_filename = op.join(bem_dir, "BEM_report.html")
        print "*** REPORT file %s written ***" % report_filename
        print report_filename
        report.save(report_filename, open_browser=False, overwrite=True)

        return sbj_id
Exemple #3
0
def test_render_mri_without_bem():
    """Test rendering MRI without BEM for mne report."""
    tempdir = _TempDir()
    os.mkdir(op.join(tempdir, 'sample'))
    os.mkdir(op.join(tempdir, 'sample', 'mri'))
    shutil.copyfile(mri_fname, op.join(tempdir, 'sample', 'mri', 'T1.mgz'))
    report = Report(info_fname=raw_fname,
                    subject='sample', subjects_dir=tempdir)
    report.parse_folder(tempdir, render_bem=False)
    report.save(op.join(tempdir, 'report.html'), open_browser=False)
Exemple #4
0
def test_render_mri():
    """Test rendering MRI for mne report."""
    tempdir = _TempDir()
    trans_fname_new = op.join(tempdir, 'temp-trans.fif')
    for a, b in [[trans_fname, trans_fname_new]]:
        shutil.copyfile(a, b)
    report = Report(info_fname=raw_fname,
                    subject='sample', subjects_dir=subjects_dir)
    report.parse_folder(data_path=tempdir, mri_decim=30, pattern='*')
    report.save(op.join(tempdir, 'report.html'), open_browser=False)
    assert repr(report)
def test_render_mri():
    """Test rendering MRI for mne report.
    """
    tempdir = _TempDir()
    trans_fname_new = op.join(tempdir, "temp-trans.fif")
    for a, b in [[trans_fname, trans_fname_new]]:
        shutil.copyfile(a, b)
    report = Report(info_fname=raw_fname, subject="sample", subjects_dir=subjects_dir)
    with warnings.catch_warnings(record=True):
        warnings.simplefilter("always")
        report.parse_folder(data_path=tempdir, mri_decim=30, pattern="*", n_jobs=2)
    report.save(op.join(tempdir, "report.html"), open_browser=False)
def create_bem_sol(sbj_dir, sbj_id):
    import os.path as op
    import mne

    from mne.bem import make_watershed_bem
    from mne.report import Report

    report = Report()

    bem_dir = op.join(sbj_dir, sbj_id, 'bem')

    surf_name = 'inner_skull.surf'
    sbj_inner_skull_fname = op.join(bem_dir, sbj_id + '-' + surf_name)
    inner_skull_fname = op.join(bem_dir, surf_name)

    # check if bem-sol was created, if not creates the bem sol using C MNE
    bem_fname = op.join(bem_dir, '%s-5120-bem-sol.fif' % sbj_id)
    model_fname = op.join(bem_dir, '%s-5120-bem.fif' % sbj_id)

    if not op.isfile(bem_fname):
        # chek if inner_skull surf exists, if not BEM computation is
        # performed by MNE python functions mne.bem.make_watershed_bem
        if not (op.isfile(sbj_inner_skull_fname) or
                op.isfile(inner_skull_fname)):
            print inner_skull_fname + '---> FILE NOT FOUND!!!---> BEM computed'
            make_watershed_bem(sbj_id, sbj_dir, overwrite=True)
        else:
            print '\n*** inner skull %s surface exists!!!\n' % inner_skull_fname

        # Create a BEM model for a subject
        surfaces = mne.make_bem_model(sbj_id, ico=4, conductivity=[0.3],
                                      subjects_dir=sbj_dir)

        # Write BEM surfaces to a fiff file
        mne.write_bem_surfaces(model_fname, surfaces)

        # Create a BEM solution using the linear collocation approach
        bem = mne.make_bem_solution(surfaces)
        mne.write_bem_solution(bem_fname, bem)

        print '\n*** BEM solution file %s written ***\n' % bem_fname

        # add BEM figures to a Report
        report.add_bem_to_section(subject=sbj_id, subjects_dir=sbj_dir)
        report_filename = op.join(bem_dir, "BEM_report.html")
        print '\n*** REPORT file %s written ***\n' % report_filename
        print report_filename
        report.save(report_filename, open_browser=False, overwrite=True)
    else:
        bem = bem_fname
        print '\n*** BEM solution file %s exists!!! ***\n' % bem_fname

    return bem
Exemple #7
0
def test_render_mri_without_bem():
    """Test rendering MRI without BEM for mne report."""
    tempdir = _TempDir()
    os.mkdir(op.join(tempdir, 'sample'))
    os.mkdir(op.join(tempdir, 'sample', 'mri'))
    shutil.copyfile(mri_fname, op.join(tempdir, 'sample', 'mri', 'T1.mgz'))
    report = Report(info_fname=raw_fname,
                    subject='sample', subjects_dir=tempdir)
    with warnings.catch_warnings(record=True) as w:
        warnings.simplefilter('always')
        report.parse_folder(tempdir)
    assert_true(len(w) >= 1)
    report.save(op.join(tempdir, 'report.html'), open_browser=False)
Exemple #8
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)
Exemple #9
0
def test_add_slider_to_section():
    """Test adding a slider with a series of images to mne report."""
    tempdir = _TempDir()
    from matplotlib import pyplot as plt
    report = Report(info_fname=raw_fname,
                    subject='sample', subjects_dir=subjects_dir)
    section = 'slider_section'
    figs = list()
    figs.append(plt.figure())
    plt.plot([1, 2, 3])
    plt.close('all')
    figs.append(plt.figure())
    plt.plot([3, 2, 1])
    plt.close('all')
    report.add_slider_to_section(figs, section=section)
    report.save(op.join(tempdir, 'report.html'), open_browser=False)

    assert_raises(NotImplementedError, report.add_slider_to_section,
                  [figs, figs])
    assert_raises(ValueError, report.add_slider_to_section, figs, ['wug'])
    assert_raises(TypeError, report.add_slider_to_section, figs, 'wug')
Exemple #10
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 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')))
Exemple #11
0
def test_add_slider_to_section():
    """Test adding a slider with a series of images to mne report."""
    tempdir = _TempDir()
    report = Report(info_fname=raw_fname,
                    subject='sample', subjects_dir=subjects_dir)
    section = 'slider_section'
    figs = _get_example_figures()
    report.add_slider_to_section(figs, section=section)
    report.save(op.join(tempdir, 'report.html'), open_browser=False)

    pytest.raises(NotImplementedError, report.add_slider_to_section,
                  [figs, figs])
    pytest.raises(ValueError, report.add_slider_to_section, figs, ['wug'])
    pytest.raises(TypeError, report.add_slider_to_section, figs, 'wug')
    # need at least 2
    pytest.raises(ValueError, report.add_slider_to_section, figs[:1], 'wug')

    # Smoke test that SVG w/unicode can be added
    report = Report()
    fig, ax = plt.subplots()
    ax.set_xlabel(u'μ')
    report.add_slider_to_section([fig] * 2, image_format='svg')
Exemple #12
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')))
Exemple #13
0
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 #14
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
Exemple #15
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')
def split_fif_into_epochs(fif_file, ep_length, isRemoveMuscleEpochs=False):
  """
  Split fif_file into epochs of ep_length. Removes epochs
  with muscle activity if isRemoveMuscleEpochs is set to True.
  Args:
          fif_file
          ep_length
          isRemoveMuscleEpochs
  """
  # ---------------- Imports ---------------------------------#
  import os
  import numpy as np
  import mne
  from mne.io import Raw
  from nipype.utils.filemanip import split_filename as split_f
  # --------------------------------------------------------- #
  ep_length = float(ep_length)
  subj_path, basename, ext = split_f(fif_file)
  # path = '/media/karim/79DE901F11ABED4C/Dimitriy/resting_data/MEG/K0017/'
  raw = Raw(fif_file, preload=True)
  sfreq = raw.info['sfreq']
  half_ep_length = ep_length / 2.
  first_time = half_ep_length
  last_time = raw.times[-1]
  event_times = np.arange(first_time, last_time, ep_length)
  event_idx = raw.time_as_index(event_times) + raw.first_samp
  events = np.array([event_idx, np.zeros(event_idx.shape),
                     np.ones(event_idx.shape)], dtype=np.int)
  picks = mne.pick_types(
      raw.info, meg=True, eeg=False, stim=False, eog=False)

  # Remove epoch if its amplitude goes more then stdThreshold STDs above
  # baseline
  bad_epochs_count = 0
  if isRemoveMuscleEpochs:
    from split_data import generate_figures_for_report
    from mne.report import Report
    report = Report()
    report_filename = basename + '_muscle_' + '.html'
    rawHpass = raw.copy()
    rawHpass.filter(l_freq=100, h_freq=None)
    rawHpassPicks = mne.pick_types(
        rawHpass.info, meg='mag', eeg=False, stim=False, eog=False)
    stdThreshold = 4.
    maxThreshold = 6.
    # print("Before epochs")

    epochsHpass = mne.Epochs(rawHpass, events.T, picks=rawHpassPicks,
                             event_id=1, tmin=-half_ep_length, tmax=half_ep_length,
                             reject=None, baseline=None, flat=dict(mag=1e-14), preload=True)
    # print("After epochs")

    ep_data = epochsHpass.get_data()
    baseline_std = 5e-14
    badChThreshold_std = 15
    badChThreshold_max = 50

    for iEp in range(len(ep_data[:, -1, -1])):
      std_ep = ep_data[iEp, :, :].std(axis=1)
      # import pdb; pdb.set_trace()
      max_ep = ep_data[iEp, :, :].max(axis=1)
      if len(std_ep[std_ep > baseline_std * stdThreshold]) > badChThreshold_std or len(max_ep[max_ep > baseline_std * maxThreshold]) > badChThreshold_max:

          # print std_ep[std_ep > baseline_std * std_ep]
          bad_epochs_count += 1
          events[2, iEp] = 0
          # import pdb; pdb.set_trace()
          # print len(std_ep[std_ep > baseline_std * std_ep])
          print "Number of contaminated channels: ", len(std_ep[std_ep > baseline_std * stdThreshold]) + len(max_ep[max_ep > baseline_std * maxThreshold])
          generate_figures_for_report(
              report, epochsHpass, rawHpass, events, half_ep_length, ep_length, iEp, picks)

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

  # Set events for epochs
  # Need to add raw.first_samp because otherwise it drops out
  # epochs with times less than raw.first_samp (see epochs.py: _get_epoch_from_disk)
  # import pdb; pdb.set_trace()
  epochs = mne.Epochs(raw, events.T, picks=picks, proj=False, event_id=1,
                      tmin=-half_ep_length, tmax=half_ep_length,
                      reject=None, baseline=None, preload=True)
  np_epochs = epochs.get_data()
  epochs_file = os.path.abspath(basename + '_epochs.npy')
  np.save(epochs_file, np_epochs)
  return epochs, epochs_file, np_epochs, bad_epochs_count
Exemple #17
0
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
figs = list()
for t in times:
    figs.append(
        evoked.plot_topomap(t, vmin=-300, vmax=300, res=100, show=False))
    plt.close(figs[-1])
report.add_slider_to_section(figs,
                             times,
                             'Evoked Response',
                             image_format='png')  # can also use 'svg'

# Save the report
report.save('my_report.html', overwrite=True)
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():
    """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("-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
    cov_fname = options.cov_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

    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)
    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)
def apply_ica(subject, run, session):
    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)

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

    fpath_deriv = op.join(config.bids_root, 'derivatives',
                          config.PIPELINE_NAME, subject_path)
    fname_in = \
        op.join(fpath_deriv, bids_basename + '-epo.fif')

    fname_out = \
        op.join(fpath_deriv, bids_basename + '_cleaned-epo.fif')

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

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

    # 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=config.runs[0],
                                       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')

    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 config.ch_types:
        report = None
        print(ch_type)
        picks = all_picks[ch_type]

        # Load ICA
        fname_ica = \
            op.join(fpath_deriv, bids_basename + '_%s-ica.fif' % 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)
        ecg_inds = list()
        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)
            del ecg_epochs

            report_fname = \
                op.join(fpath_deriv,
                        bids_basename + '_%s-reject_ica.html' % ch_type)

            report = Report(report_fname, verbose=False)

            # Plot r score
            report.add_figs_to_section(ica.plot_scores(scores,
                                                       exclude=ecg_inds,
                                                       show=config.plot),
                                       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.plot),
                                       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.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 ECG!')

        # EOG
        pick_eog = mne.pick_types(raw.info, meg=False, eeg=False,
                                  ecg=False, eog=True)
        eog_inds = list()
        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_ctps_eog_threshold)
            del eog_epochs

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

            # 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:
            print('no EOG channel is present. Cannot automate ICAs component '
                  'detection for EOG!')

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

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

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

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

        if config.plot:
            epochs.plot_image(combine='gfp', group_by='type', sigma=2.,
                              cmap="YlGnBu_r", show=config.plot)
 for subject in tqdm(files):
     native_fid      = op.join('G:\OpenSource_data\CAM-CAN\cc700\mri\pipeline\\release004\\fiducials','fid-native-' + subject.replace("sub-","") +'.mat')
     raw_path        = op.join(rest_dir, subject, 'meg\\transdef_mf2pt2_rest_raw.fif')
     clean_dir       = op.join(rest_dir, subject, 'meg_clean')
     if not os.path.exists(clean_dir):
         os.makedirs(clean_dir)
     raw_NosePtsOut  = op.join(clean_dir,'nose-pts-out_raw.fif')
     trans_dst       = op.join(clean_dir,'_trans.fif')
     
     if not os.path.exists(raw_path):
         logFile.write("Subject " + subject + " doesn't have MEG data\n")
         continue
     
     if os.path.exists(native_fid):
         flag_fid = True
         coreg_head2mri(subjects_dir, subject, native_fid, raw_path, raw_NosePtsOut, trans_dst, flag_fid)
     else:
         logFile.write("Subject " + subject + " doesn't have fiducials\n")
         coreg_head2mri(subjects_dir, subject, native_fid, raw_path, raw_NosePtsOut, trans_dst)
     
     raw = read_raw_fif(raw_NosePtsOut, preload=True)
     fig = mne.viz.plot_alignment(raw.info, trans=trans_dst, subject=subject, dig=True, meg=True, subjects_dir=subjects_dir, surfaces=['head','inner_skull'], coord_frame='meg', show_axes=True)
     report.add_figs_to_section(fig, captions=subject, section='Co-registration')
     fig = mne.viz.plot_alignment(raw.info, trans=trans_dst, subject=subject, dig=True, meg=True, subjects_dir=subjects_dir, surfaces=['head','inner_skull'], coord_frame='meg', show_axes=True)
     with warnings.catch_warnings(record=True):
         from mayavi import mlab
         mlab.view(175, 90, distance=0.6, focalpoint=(0., 0., 0.))
     report.add_figs_to_section(fig, captions=subject, section='Co-registration')
     report.save('G:\OpenSource_data\CAM-CAN\cc700\mri\pipeline\\release004\BIDSsep\megmax_rest_reports\\Coregistration_sub-CC6.html', overwrite=True, open_browser=False) 
 
 logFile.close()
Exemple #22
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 #23
0
def generateReport(raw, ica, subj_name, subj_path, basename,
                   ecg_evoked, ecg_scores, ecg_inds, ECG_ch_name,
                   eog_evoked, eog_scores, eog_inds, EoG_ch_name):
    from mne.report import Report
    import numpy as np
    import os
    import HTML
    report = Report()

    ICA_title = 'Sources related to %s artifacts (red)'
    is_show = False # visualization

    File_length = str(round(raw.times[-1], 0))
    # report.add_htmls_to_section(htmls=name_html, captions='File path', section='General')
    name_html =  '<h4 style="text-align:left;"> Path:  ' + subj_path + '/' + basename + '.fif' + '</h4>'
    ex_comps_table = [['', 'ICs to exclude'],['ECG', ecg_inds], ['EOG', eog_inds]]
    ex_comps_html = '<h4>' + HTML.table(ex_comps_table) + '</h4>'
    File_length_html = '<h4 style="text-align:left;">' + 'File length: ' + File_length + ' seconds' + '</h4>'
    report.add_htmls_to_section(htmls=name_html + File_length_html + ex_comps_html, captions='General info', section='General info')
    # --------------------- Generate report for ECG ---------------------------------------- #
    fig1 = 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.

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

    

    # plot ECG sources + selection
    fig3 = ica.plot_sources(ecg_evoked, exclude=ecg_inds, show=is_show)
    fig = [fig1, fig2, fig3]
    report.add_figs_to_section(fig, captions=['Scores of ICs related to 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'])):
        fig4 = ica.plot_scores(
            eog_scores, exclude=eog_inds, title=ICA_title % 'eog', show=is_show)
        report.add_figs_to_section(fig4, captions=['Scores of ICs related to EOG'],
                                   section='ICA - EOG')
        rs = np.shape(eog_scores)
        if len(rs) > 1:
            rr = rs[0]
            show_picks = [np.abs(eog_scores[i][:]).argsort()[::-1][:5]
                          for i in range(rr)]
            for i in range(rr):
                fig5 = ica.plot_components(show_picks[i][:], title=ICA_title % 'eog',
                                           colorbar=True, show=is_show)  # ICA nel tempo
                fig = [fig5]
                report.add_figs_to_section(fig, captions=['Scores of ICs related to EOG'],
                                           section='ICA - EOG')
        else:
            show_picks = np.abs(eog_scores).argsort()[::-1][:5]
            fig5 = ica.plot_components(
                show_picks, title=ICA_title % 'eog', colorbar=True, show=is_show)
            fig = [fig5]
            report.add_figs_to_section(fig, captions=['TopoMap of ICs (EOG)', ],
                                       section='ICA - EOG')
        fig9 = ica.plot_sources(eog_evoked, exclude=eog_inds, show=is_show)  # plot EOG sources + selection
        # fig10 = ica.plot_overlay(eog_evoked, exclude=eog_inds, show=is_show)  # plot EOG cleaning

        # fig = [fig9, fig10]
        fig = [fig9]
        report.add_figs_to_section(fig, captions=['Time-locked EOG sources'], section = 'ICA - EOG')
   # -------------------- end generate report for EoG -------------------------------------------------------- #
    # import ipdb; ipdb.set_trace()
    IC_nums = 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')

    psds = []
    captions_psd = []
    ica_src = ica.get_sources(raw)
    for iIC in IC_nums:
        fig = ica_src.plot_psd(tmax=None, picks=[iIC], fmax=140, show=False)
        fig.set_figheight(3)
        fig.set_figwidth(5)
        psds.append(fig)
        captions_psd.append('IC #' + str(iIC))
    # report.add_slider_to_section(figs=psds, captions=captions_psd, title='', section='ICA - muscles')
    report.add_figs_to_section(figs=psds, captions=captions_psd, section='ICA - muscles')
    report_filename = os.path.join(subj_path, basename + "-report.html")
    print '******* ' + report_filename
    report.save(report_filename, open_browser=False, overwrite=True)
Exemple #24
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
Exemple #25
0
def gen_html_report(p, subjects, structurals, run_indices=None):
    """Generates HTML reports"""
    import matplotlib.pyplot as plt
    from ._mnefun import (_load_trans_to, plot_good_coils, _head_pos_annot,
                          _get_bem_src_trans, safe_inserter, _prebad,
                          _load_meg_bads, mlab_offscreen, _fix_raw_eog_cals,
                          _handle_dict, _get_t_window, plot_chpi_snr_raw)
    if run_indices is None:
        run_indices = [None] * len(subjects)
    style = {'axes.spines.right': 'off', 'axes.spines.top': 'off',
             'axes.grid': True}
    time_kwargs = dict()
    if 'time_unit' in mne.fixes._get_args(mne.viz.plot_evoked):
        time_kwargs['time_unit'] = 's'
    for si, subj in enumerate(subjects):
        struc = structurals[si]
        report = Report(verbose=False)
        print('  Processing subject %s/%s (%s)'
              % (si + 1, len(subjects), subj))

        # raw
        fnames = get_raw_fnames(p, subj, 'raw', erm=False, add_splits=False,
                                run_indices=run_indices[si])
        for fname in fnames:
            if not op.isfile(fname):
                raise RuntimeError('Cannot create reports until raw data '
                                   'exist, missing:\n%s' % fname)
        raw = [read_raw_fif(fname, allow_maxshield='yes')
               for fname in fnames]
        _fix_raw_eog_cals(raw)
        prebad_file = _prebad(p, subj)
        for r in raw:
            _load_meg_bads(r, prebad_file, disp=False)
        raw = mne.concatenate_raws(raw)

        # sss
        sss_fnames = get_raw_fnames(p, subj, 'sss', False, False,
                                    run_indices[si])
        has_sss = all(op.isfile(fname) for fname in sss_fnames)
        sss_info = mne.io.read_raw_fif(sss_fnames[0]) if has_sss else None
        bad_file = get_bad_fname(p, subj)
        if bad_file is not None:
            sss_info.load_bad_channels(bad_file)
        sss_info = sss_info.info

        # pca
        pca_fnames = get_raw_fnames(p, subj, 'pca', False, False,
                                    run_indices[si])
        has_pca = all(op.isfile(fname) for fname in pca_fnames)

        # whitening and source localization
        inv_dir = op.join(p.work_dir, subj, p.inverse_dir)

        has_fwd = op.isfile(op.join(p.work_dir, subj, p.forward_dir,
                                    subj + p.inv_tag + '-fwd.fif'))

        with plt.style.context(style):
            ljust = 25
            #
            # Head coils
            #
            section = 'Good HPI count'
            if p.report_params.get('good_hpi_count', True) and p.movecomp:
                t0 = time.time()
                print(('    %s ... ' % section).ljust(ljust), end='')
                figs = list()
                captions = list()
                for fname in fnames:
                    _, _, fit_data = _head_pos_annot(p, fname, prefix='      ')
                    assert fit_data is not None
                    fig = plot_good_coils(fit_data, show=False)
                    fig.set_size_inches(10, 2)
                    fig.tight_layout()
                    figs.append(fig)
                    captions.append('%s: %s' % (section, op.split(fname)[-1]))
                report.add_figs_to_section(figs, captions, section,
                                           image_format='svg')
                print('%5.1f sec' % ((time.time() - t0),))
            else:
                print('    %s skipped' % section)

            #
            # cHPI SNR
            #
            section = 'cHPI SNR'
            if p.report_params.get('chpi_snr', True) and p.movecomp:
                t0 = time.time()
                print(('    %s ... ' % section).ljust(ljust), end='')
                figs = list()
                captions = list()
                for fname in fnames:
                    raw = mne.io.read_raw_fif(fname, allow_maxshield='yes')
                    t_window = _get_t_window(p, raw)
                    fig = plot_chpi_snr_raw(raw, t_window, show=False,
                                            verbose=False)
                    fig.set_size_inches(10, 5)
                    fig.subplots_adjust(0.1, 0.1, 0.8, 0.95,
                                        wspace=0, hspace=0.5)
                    figs.append(fig)
                    captions.append('%s: %s' % (section, op.split(fname)[-1]))
                report.add_figs_to_section(figs, captions, section,
                                           image_format='png')  # svd too slow
                print('%5.1f sec' % ((time.time() - t0),))
            else:
                print('    %s skipped' % section)

            #
            # Head movement
            #
            section = 'Head movement'
            if p.report_params.get('head_movement', True) and p.movecomp:
                print(('    %s ... ' % section).ljust(ljust), end='')
                t0 = time.time()
                trans_to = _load_trans_to(p, subj, run_indices[si], raw)
                figs = list()
                captions = list()
                for fname in fnames:
                    pos, _, _ = _head_pos_annot(p, fname, prefix='      ')
                    fig = plot_head_positions(pos=pos, destination=trans_to,
                                              info=raw.info, show=False)
                    for ax in fig.axes[::2]:
                        """
                        # tighten to the sensor limits
                        assert ax.lines[0].get_color() == (0., 0., 0., 1.)
                        mn, mx = np.inf, -np.inf
                        for line in ax.lines:
                            ydata = line.get_ydata()
                            if np.isfinite(ydata).any():
                                mn = min(np.nanmin(ydata), mn)
                                mx = max(np.nanmax(line.get_ydata()), mx)
                        """
                        # always show at least 10cm span, and use tight limits
                        # if greater than that
                        coord = ax.lines[0].get_ydata()
                        for line in ax.lines:
                            if line.get_color() == 'r':
                                extra = line.get_ydata()[0]
                        mn, mx = coord.min(), coord.max()
                        md = (mn + mx) / 2.
                        mn = min([mn, md - 50., extra])
                        mx = max([mx, md + 50., extra])
                        assert (mn <= coord).all()
                        assert (mx >= coord).all()
                        ax.set_ylim(mn, mx)
                    fig.set_size_inches(10, 6)
                    fig.tight_layout()
                    figs.append(fig)
                    captions.append('%s: %s' % (section, op.split(fname)[-1]))
                del trans_to
                report.add_figs_to_section(figs, captions, section,
                                           image_format='svg')
                print('%5.1f sec' % ((time.time() - t0),))
            else:
                print('    %s skipped' % section)

            #
            # Raw segments
            #
            if op.isfile(pca_fnames[0]):
                raw_pca = mne.concatenate_raws(
                    [mne.io.read_raw_fif(fname) for fname in pca_fnames])
            section = 'Raw segments'
            if p.report_params.get('raw_segments', True) and has_pca:
                times = np.linspace(raw.times[0], raw.times[-1], 12)[1:-1]
                raw_plot = list()
                for t in times:
                    this_raw = raw_pca.copy().crop(t - 0.5, t + 0.5)
                    this_raw.load_data()
                    this_raw._data[:] -= np.mean(this_raw._data, axis=-1,
                                                 keepdims=True)
                    raw_plot.append(this_raw)
                raw_plot = mne.concatenate_raws(raw_plot)
                for key in ('BAD boundary', 'EDGE boundary'):
                    raw_plot.annotations.delete(
                        np.where(raw_plot.annotations.description == key)[0])
                new_events = np.linspace(
                    0, int(round(10 * raw.info['sfreq'])) - 1, 11).astype(int)
                new_events += raw_plot.first_samp
                new_events = np.array([new_events,
                                       np.zeros_like(new_events),
                                       np.ones_like(new_events)]).T
                fig = raw_plot.plot(group_by='selection', butterfly=True,
                                    events=new_events)
                fig.axes[0].lines[-1].set_zorder(10)  # events
                fig.axes[0].set(xticks=np.arange(0, len(times)) + 0.5)
                xticklabels = ['%0.1f' % t for t in times]
                fig.axes[0].set(xticklabels=xticklabels)
                fig.axes[0].set(xlabel='Center of 1-second segments')
                fig.axes[0].grid(False)
                for _ in range(len(fig.axes) - 1):
                    fig.delaxes(fig.axes[-1])
                fig.set(figheight=(fig.axes[0].get_yticks() != 0).sum(),
                        figwidth=12)
                fig.subplots_adjust(0.0, 0.0, 1, 1, 0, 0)
                report.add_figs_to_section(fig, 'Processed', section,
                                           image_format='png')  # svg too slow

            #
            # PSD
            #
            section = 'PSD'
            if p.report_params.get('psd', True) and has_pca:
                t0 = time.time()
                print(('    %s ... ' % section).ljust(ljust), end='')
                if p.lp_trans == 'auto':
                    lp_trans = 0.25 * p.lp_cut
                else:
                    lp_trans = p.lp_trans
                n_fft = 8192
                fmax = raw.info['lowpass']
                figs = [raw.plot_psd(fmax=fmax, n_fft=n_fft, show=False)]
                captions = ['%s: Raw' % section]
                fmax = p.lp_cut + 2 * lp_trans
                figs.append(raw.plot_psd(fmax=fmax, n_fft=n_fft, show=False))
                captions.append('%s: Raw (zoomed)' % section)
                if op.isfile(pca_fnames[0]):
                    figs.append(raw_pca.plot_psd(fmax=fmax, n_fft=n_fft,
                                                 show=False))
                    captions.append('%s: Processed' % section)
                # shared y limits
                n = len(figs[0].axes) // 2
                for ai, axes in enumerate(list(zip(
                        *[f.axes for f in figs]))[:n]):
                    ylims = np.array([ax.get_ylim() for ax in axes])
                    ylims = [np.min(ylims[:, 0]), np.max(ylims[:, 1])]
                    for ax in axes:
                        ax.set_ylim(ylims)
                        ax.set(title='')
                for fig in figs:
                    fig.set_size_inches(8, 8)
                    with warnings.catch_warnings(record=True):
                        fig.tight_layout()
                report.add_figs_to_section(figs, captions, section,
                                           image_format='svg')
                print('%5.1f sec' % ((time.time() - t0),))
            else:
                print('    %s skipped' % section)

            #
            # SSP
            #
            section = 'SSP topomaps'

            proj_nums = _handle_dict(p.proj_nums, subj)
            if p.report_params.get('ssp_topomaps', True) and has_pca and \
                    np.sum(proj_nums) > 0:
                assert sss_info is not None
                t0 = time.time()
                print(('    %s ... ' % section).ljust(ljust), end='')
                figs = []
                comments = []
                proj_files = get_proj_fnames(p, subj)
                if p.proj_extra is not None:
                    comments.append('Custom')
                    projs = read_proj(op.join(p.work_dir, subj, p.pca_dir,
                                              p.proj_extra))
                    figs.append(plot_projs_topomap(projs, info=sss_info,
                                                   show=False))
                if any(proj_nums[0]):  # ECG
                    if 'preproc_ecg-proj.fif' in proj_files:
                        comments.append('ECG')
                        figs.append(_proj_fig(op.join(
                            p.work_dir, subj, p.pca_dir,
                            'preproc_ecg-proj.fif'), sss_info,
                            proj_nums[0], p.proj_meg, 'ECG'))
                if any(proj_nums[1]):  # EOG
                    if 'preproc_blink-proj.fif' in proj_files:
                        comments.append('Blink')
                        figs.append(_proj_fig(op.join(
                            p.work_dir, subj, p.pca_dir,
                            'preproc_blink-proj.fif'), sss_info,
                            proj_nums[1], p.proj_meg, 'EOG'))
                if any(proj_nums[2]):  # ERM
                    if 'preproc_cont-proj.fif' in proj_files:
                        comments.append('Continuous')
                        figs.append(_proj_fig(op.join(
                            p.work_dir, subj, p.pca_dir,
                            'preproc_cont-proj.fif'), sss_info,
                            proj_nums[2], p.proj_meg, 'ERM'))
                captions = [section] + [None] * (len(comments) - 1)
                report.add_figs_to_section(
                     figs, captions, section, image_format='svg',
                     comments=comments)
                print('%5.1f sec' % ((time.time() - t0),))
            else:
                print('    %s skipped' % section)

            #
            # Source alignment
            #
            section = 'Source alignment'
            source_alignment = p.report_params.get('source_alignment', True)
            if source_alignment is True or isinstance(source_alignment, dict) \
                    and has_sss and has_fwd:
                assert sss_info is not None
                kwargs = source_alignment
                if isinstance(source_alignment, dict):
                    kwargs = dict(**source_alignment)
                else:
                    assert source_alignment is True
                    kwargs = dict()
                t0 = time.time()
                print(('    %s ... ' % section).ljust(ljust), end='')
                captions = [section]
                try:
                    from mayavi import mlab
                except ImportError:
                    warnings.warn('Cannot plot alignment in Report, mayavi '
                                  'could not be imported')
                else:
                    subjects_dir = mne.utils.get_subjects_dir(
                        p.subjects_dir, raise_error=True)
                    bem, src, trans, _ = _get_bem_src_trans(
                        p, sss_info, subj, struc)
                    if len(mne.pick_types(sss_info)):
                        coord_frame = 'meg'
                    else:
                        coord_frame = 'head'
                    with mlab_offscreen():
                        fig = mlab.figure(bgcolor=(0., 0., 0.),
                                          size=(1000, 1000))
                        for key, val in (
                                ('info', sss_info),
                                ('subjects_dir', subjects_dir), ('bem', bem),
                                ('dig', True), ('coord_frame', coord_frame),
                                ('show_axes', True), ('fig', fig),
                                ('trans', trans), ('src', src)):
                            kwargs[key] = kwargs.get(key, val)
                        try_surfs = ['head-dense', 'head', 'inner_skull']
                        for surf in try_surfs:
                            try:
                                mne.viz.plot_alignment(surfaces=surf, **kwargs)
                            except Exception:
                                pass
                            else:
                                break
                        else:
                            raise RuntimeError('Could not plot any surface '
                                               'for alignment:\n%s'
                                               % (try_surfs,))
                        fig.scene.parallel_projection = True
                        view = list()
                        for ai, angle in enumerate([180, 90, 0]):
                            mlab.view(angle, 90, focalpoint=(0., 0., 0.),
                                      distance=0.6, figure=fig)
                            view.append(mlab.screenshot(figure=fig))
                        mlab.close(fig)
                    view = trim_bg(np.concatenate(view, axis=1), 0)
                    report.add_figs_to_section(view, captions, section)
                print('%5.1f sec' % ((time.time() - t0),))
            else:
                print('    %s skipped' % section)
            #
            # SNR
            #
            section = 'SNR'
            if p.report_params.get('snr', None) is not None:
                t0 = time.time()
                print(('    %s ... ' % section).ljust(ljust), end='')
                snrs = p.report_params['snr']
                if not isinstance(snrs, (list, tuple)):
                    snrs = [snrs]
                for snr in snrs:
                    assert isinstance(snr, dict)
                    analysis = snr['analysis']
                    name = snr['name']
                    times = snr.get('times', [0.1])
                    inv_dir = op.join(p.work_dir, subj, p.inverse_dir)
                    fname_inv = op.join(inv_dir,
                                        safe_inserter(snr['inv'], subj))
                    fname_evoked = op.join(inv_dir, '%s_%d%s_%s_%s-ave.fif'
                                           % (analysis, p.lp_cut, p.inv_tag,
                                              p.eq_tag, subj))
                    if not op.isfile(fname_inv):
                        print('    Missing inv: %s'
                              % op.basename(fname_inv), end='')
                    elif not op.isfile(fname_evoked):
                        print('    Missing evoked: %s'
                              % op.basename(fname_evoked), end='')
                    else:
                        inv = mne.minimum_norm.read_inverse_operator(fname_inv)
                        this_evoked = mne.read_evokeds(fname_evoked, name)
                        title = ('%s<br>%s["%s"] (N=%d)'
                                 % (section, analysis, name, this_evoked.nave))
                        figs = plot_snr_estimate(this_evoked, inv,
                                                 verbose=False)
                        figs.axes[0].set_ylim(auto=True)
                        captions = ('%s<br>%s["%s"] (N=%d)'
                                    % (section, analysis, name,
                                       this_evoked.nave))
                        report.add_figs_to_section(
                            figs, captions, section=section,
                            image_format='svg')
                print('%5.1f sec' % ((time.time() - t0),))
            #
            # BEM
            #
            section = 'BEM'
            if p.report_params.get('bem', True) and has_fwd:
                caption = '%s<br>%s' % (section, struc)
                bem, src, trans, _ = _get_bem_src_trans(
                    p, raw.info, subj, struc)
                if not bem['is_sphere']:
                    subjects_dir = mne.utils.get_subjects_dir(
                        p.subjects_dir, raise_error=True)
                    mri_fname = op.join(subjects_dir, struc, 'mri', 'T1.mgz')
                    if not op.isfile(mri_fname):
                        warnings.warn(
                            'Could not find MRI:\n%s\nIf using surrogate '
                            'subjects, use '
                            'params.report_params["bem"] = False to avoid '
                            'this warning', stacklevel=2)
                    else:
                        t0 = time.time()
                        print(('    %s ... ' % section).ljust(ljust), end='')
                        report.add_bem_to_section(struc, caption, section,
                                                  decim=10, n_jobs=1,
                                                  subjects_dir=subjects_dir)
                        print('%5.1f sec' % ((time.time() - t0),))
                else:
                    print('    %s skipped (sphere)' % section)
            else:
                print('    %s skipped' % section)

            #
            # Whitening
            #
            section = 'Whitening'
            if p.report_params.get('whitening', False):
                t0 = time.time()
                print(('    %s ... ' % section).ljust(ljust), end='')

                whitenings = p.report_params['whitening']
                if not isinstance(whitenings, (list, tuple)):
                    whitenings = [whitenings]
                for whitening in whitenings:
                    assert isinstance(whitening, dict)
                    analysis = whitening['analysis']
                    name = whitening['name']
                    cov_name = op.join(p.work_dir, subj, p.cov_dir,
                                       safe_inserter(whitening['cov'], subj))
                    # Load the inverse
                    fname_evoked = op.join(inv_dir, '%s_%d%s_%s_%s-ave.fif'
                                           % (analysis, p.lp_cut, p.inv_tag,
                                              p.eq_tag, subj))
                    if not op.isfile(cov_name):
                        print('    Missing cov: %s'
                              % op.basename(cov_name), end='')
                    elif not op.isfile(fname_evoked):
                        print('    Missing evoked: %s'
                              % op.basename(fname_evoked), end='')
                    else:
                        noise_cov = mne.read_cov(cov_name)
                        evo = mne.read_evokeds(fname_evoked, name)
                        captions = ('%s<br>%s["%s"] (N=%d)'
                                    % (section, analysis, name, evo.nave))
                        fig = evo.plot_white(noise_cov, **time_kwargs)
                        report.add_figs_to_section(
                            fig, captions, section=section, image_format='png')
                print('%5.1f sec' % ((time.time() - t0),))
            else:
                print('    %s skipped' % section)

            #
            # Sensor space plots
            #
            section = 'Responses'
            if p.report_params.get('sensor', False):
                t0 = time.time()
                print(('    %s ... ' % section).ljust(ljust), end='')
                sensors = p.report_params['sensor']
                if not isinstance(sensors, (list, tuple)):
                    sensors = [sensors]
                for sensor in sensors:
                    assert isinstance(sensor, dict)
                    analysis = sensor['analysis']
                    name = sensor['name']
                    times = sensor.get('times', [0.1, 0.2])
                    fname_evoked = op.join(inv_dir, '%s_%d%s_%s_%s-ave.fif'
                                           % (analysis, p.lp_cut, p.inv_tag,
                                              p.eq_tag, subj))
                    if not op.isfile(fname_evoked):
                        print('    Missing evoked: %s'
                              % op.basename(fname_evoked), end='')
                    else:
                        this_evoked = mne.read_evokeds(fname_evoked, name)
                        figs = this_evoked.plot_joint(
                            times, show=False, ts_args=dict(**time_kwargs),
                            topomap_args=dict(outlines='head', **time_kwargs))
                        if not isinstance(figs, (list, tuple)):
                            figs = [figs]
                        captions = ('%s<br>%s["%s"] (N=%d)'
                                    % (section, analysis, name,
                                       this_evoked.nave))
                        captions = [captions] + [None] * (len(figs) - 1)
                        report.add_figs_to_section(
                            figs, captions, section=section,
                            image_format='png')
                print('%5.1f sec' % ((time.time() - t0),))

            #
            # Source estimation
            #
            section = 'Source estimation'
            if p.report_params.get('source', False):
                t0 = time.time()
                print(('    %s ... ' % section).ljust(ljust), end='')
                sources = p.report_params['source']
                if not isinstance(sources, (list, tuple)):
                    sources = [sources]
                for source in sources:
                    assert isinstance(source, dict)
                    analysis = source['analysis']
                    name = source['name']
                    times = source.get('times', [0.1, 0.2])
                    # Load the inverse
                    inv_dir = op.join(p.work_dir, subj, p.inverse_dir)
                    fname_inv = op.join(inv_dir,
                                        safe_inserter(source['inv'], subj))
                    fname_evoked = op.join(inv_dir, '%s_%d%s_%s_%s-ave.fif'
                                           % (analysis, p.lp_cut, p.inv_tag,
                                              p.eq_tag, subj))
                    if not op.isfile(fname_inv):
                        print('    Missing inv: %s'
                              % op.basename(fname_inv), end='')
                    elif not op.isfile(fname_evoked):
                        print('    Missing evoked: %s'
                              % op.basename(fname_evoked), end='')
                    else:
                        inv = mne.minimum_norm.read_inverse_operator(fname_inv)
                        this_evoked = mne.read_evokeds(fname_evoked, name)
                        title = ('%s<br>%s["%s"] (N=%d)'
                                 % (section, analysis, name, this_evoked.nave))
                        stc = mne.minimum_norm.apply_inverse(
                            this_evoked, inv,
                            lambda2=source.get('lambda2', 1. / 9.),
                            method=source.get('method', 'dSPM'))
                        stc = abs(stc)
                        # get clim using the reject_tmin <->reject_tmax
                        stc_crop = stc.copy().crop(
                            p.reject_tmin, p.reject_tmax)
                        clim = source.get('clim', dict(kind='percent',
                                                       lims=[82, 90, 98]))
                        out = mne.viz._3d._limits_to_control_points(
                             clim, stc_crop.data, 'viridis',
                             transparent=True)  # dummy cmap
                        if isinstance(out[0], (list, tuple, np.ndarray)):
                            clim = out[0]  # old MNE
                        else:
                            clim = out[1]  # new MNE (0.17+)
                        clim = dict(kind='value', lims=clim)
                        if not isinstance(stc, mne.SourceEstimate):
                            print('Only surface source estimates currently '
                                  'supported')
                        else:
                            subjects_dir = mne.utils.get_subjects_dir(
                                p.subjects_dir, raise_error=True)
                            with mlab_offscreen():
                                brain = stc.plot(
                                    hemi=source.get('hemi', 'split'),
                                    views=source.get('views', ['lat', 'med']),
                                    size=source.get('size', (800, 600)),
                                    colormap=source.get('colormap', 'viridis'),
                                    transparent=source.get('transparent',
                                                           True),
                                    foreground='k', background='w',
                                    clim=clim, subjects_dir=subjects_dir,
                                    )
                                imgs = list()
                                for t in times:
                                    brain.set_time(t)
                                    imgs.append(
                                        trim_bg(brain.screenshot(), 255))
                                brain.close()
                            captions = ['%2.3f sec' % t for t in times]
                            report.add_slider_to_section(
                                imgs, captions=captions, section=section,
                                title=title, image_format='png')
                print('%5.1f sec' % ((time.time() - t0),))
            else:
                print('    %s skipped' % section)

        report_fname = get_report_fnames(p, subj)[0]
        report.save(report_fname, open_browser=False, overwrite=True)
def preprocess_set_ICA_comp_fif_to_ts(fif_file, n_comp_exclude, l_freq, h_freq,
                                      down_sfreq, is_sensor_space):
    import os
    import numpy as np
    import sys

    import mne
    from mne.io import Raw
    from mne.preprocessing import read_ica
    from mne.report import Report

    from nipype.utils.filemanip import split_filename as split_f

    report = Report()

    subj_path, basename, ext = split_f(fif_file)
    (data_path,  sbj_name) = os.path.split(subj_path)

    print '*** SBJ %s' % sbj_name + '***'

#    n_session = int(filter(str.isdigit, basename))
#    print '*** n session = %d' % n_session + '***'

    # Read raw
    raw = Raw(fif_file, preload=True)

    # select sensors
    select_sensors = mne.pick_types(raw.info, meg=True, ref_meg=False,
                                    exclude='bads')
    picks_meeg = mne.pick_types(raw.info, meg=True, eeg=True,
                                exclude='bads')

    # save electrode locations
    sens_loc = [raw.info['chs'][i]['loc'][:3] for i in select_sensors]
    sens_loc = np.array(sens_loc)

    channel_coords_file = os.path.abspath("correct_channel_coords.txt")
    np.savetxt(channel_coords_file, sens_loc, fmt='%s')

    # save electrode names
    sens_names = np.array([raw.ch_names[pos] for pos in select_sensors],
                          dtype="str")

    channel_names_file = os.path.abspath("correct_channel_names.txt")
    np.savetxt(channel_names_file, sens_names, fmt='%s')

    # filtering + downsampling
    # TODO n_jobs=8
    raw.filter(l_freq=l_freq, h_freq=h_freq, picks=picks_meeg,
               method='iir',n_jobs=8)
#    raw.resample(sfreq=down_sfreq, npad=0)

    # load ICA
    is_show = False  # visualization
    ica_filename = os.path.join(subj_path, basename + '-ica.fif')
    if os.path.exists(ica_filename) is False:
        print "$$$ Warning, no %s found" % ica_filename
        sys.exit()
    else:
        ica = read_ica(ica_filename)

    # AP 210316
    '''
    print '*** ica.exclude before set components= ', ica.exclude
    if n_comp_exclude.has_key(sbj_name):
        print '*** ICA to be excluded for sbj %s ' % sbj_name + ' ' + str(n_comp_exclude[sbj_name]) + '***'
        matrix_c_ICA = n_comp_exclude[sbj_name]

        if not matrix_c_ICA[n_session-1]:
            print 'no ICA'
        else:
            print '*** ICA to be excluded for session %d ' %n_session + ' ' + str(matrix_c_ICA[n_session-1]) + '***'        
    ica.exclude = matrix_c_ICA[n_session-1]
    '''
    # AP new dict
    print '*** ica.exclude before set components= ', ica.exclude
    if n_comp_exclude.has_key(sbj_name):
        print '*** ICA to be excluded for sbj %s ' % sbj_name + ' ' + str(n_comp_exclude[sbj_name]) + '***'
        session_dict = n_comp_exclude[sbj_name]
        session_names = session_dict.keys()

        componentes = []
        for s in session_names:
            if basename.find(s) > -1:
                componentes = session_dict[s]
                break

        if len(componentes) == 0:
            print '\n no ICA to be excluded \n'
        else:
            print '\n *** ICA to be excluded for session %s ' % s + \
                    ' ' + str(componentes) + ' *** \n'

    ica.exclude = componentes

    print '\n *** ica.exclude after set components = ', ica.exclude

    fig1 = ica.plot_overlay(raw, show=is_show)
    report.add_figs_to_section(fig1, captions=['Signal'],
                               section='Signal quality')
    report_filename = os.path.join(subj_path, basename + "-report_NEW.html")
    print report_filename
    report.save(report_filename, open_browser=False, overwrite=True)

    # 3) apply ICA to raw data and save solution and report
    # check the amplitudes do not change
#    raw_ica_file = os.path.abspath(basename[:i_raw] + 'ica-raw.fif')
    raw_ica_file = os.path.join(subj_path, basename + '-preproc-raw.fif')
    raw_ica = ica.apply(raw)
    
    raw_ica.resample(sfreq=down_sfreq, npad=0)
    
    raw_ica.save(raw_ica_file, overwrite=True)

    # save ICA solution
    print ica_filename
    ica.save(ica_filename)

    # 4) save data
    data_noIca, times = raw[select_sensors, :]
    data, times = raw_ica[select_sensors, :]

    print data.shape
    print raw.info['sfreq']

    ts_file = os.path.abspath(basename + '_ica.npy')
    np.save(ts_file, data)
    print '*** TS FILE ' + ts_file + '***'
    print '*** raw.info[sfreq] = ' + str(raw.info['sfreq'])

    if is_sensor_space:
        return ts_file, channel_coords_file, channel_names_file, raw.info['sfreq']
    else:
        return raw_ica_file, channel_coords_file, channel_names_file, raw.info['sfreq']
Exemple #27
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}

    if config.eeg:
        ch_types = ['meg', 'eeg']
    else:
        ch_types = ['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()

            # 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

            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.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=3.0)
            del eog_epochs

            # 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)

        report.add_figs_to_section(ica.plot_overlay(raw.copy(),
                                                    exclude=ica_reject),
                                   captions=ch_type.upper() +
                                   ' - ALL(epochs) - ' + 'Corrections')

        if config.plot:
            epochs.plot_image(combine='gfp',
                              group_by='type',
                              sigma=2.,
                              cmap="YlGnBu_r")
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")
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 #30
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)

    assert_raises(ValueError, Report, image_format='foo')
    assert_raises(ValueError, Report, image_format=None)

    # SVG rendering
    report = Report(info_fname=raw_fname_new, subjects_dir=subjects_dir,
                    image_format='svg')
    with warnings.catch_warnings(record=True) as w:
        warnings.simplefilter('always')
        report.parse_folder(data_path=tempdir, on_error='raise')
Exemple #31
0
    # Baseline to plot
    inducedshock.apply_baseline(mode=param['tfr_baseline_mode'],
                                baseline=param['tfr_baseline_time'])

    # Plot in report
    chans_to_plot = ['Cz', 'Pz', 'CPz', 'Oz', 'Fz', 'FCz']
    figs_tfr = []
    for c in chans_to_plot:
        pick = inducedshock.ch_names.index(c)
        figs_tfr.append(
            inducedshock.plot(picks=[pick],
                              tmin=param['erpbaseline'],
                              tmax=param['erpepochend'],
                              show=False))
    report.add_slider_to_section(figs_tfr,
                                 captions=chans_to_plot,
                                 section='TFR_shock',
                                 title='TFR shocks')

    # Save report
    report.save(opj(outdir, p + '_task-fearcond' + '_tfr.html'),
                open_browser=False,
                overwrite=True)

    plt.close('all')

# Save rejection stats (should be the same as erps)
removed_frame['percleft_cue'] = percleft_cue
removed_frame['percleft_shock'] = percleft_shock

removed_frame.to_csv('/data/derivatives/task-fearcond_tfr_rejectionstats.csv')
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, on_error='raise')
    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 #33
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()
    events_list = list()
    print("  Loading raw data")

    for run in config.runs:
        extension = run + '_sss_raw'
        raw_fname_in = op.join(meg_subject_dir,
                               config.base_fname.format(**locals()))
        eve_fname = op.splitext(raw_fname_in)[0] + '-eve.fif'
        print("Input: ", raw_fname_in, eve_fname)

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

        events = mne.read_events(eve_fname)
        events_list.append(events)

        # XXX mark bads from any run – is it a problem for ICA
        # if we just exclude the bads shared by all runs ?
        if run:
            bads = set(chain(*config.bads[subject].values()))
        else:
            bads = config.bads[subject]

        raw.info['bads'] = bads
        print("added bads: ", raw.info['bads'])

        raw_list.append(raw)

    print('  Concatenating runs')
    raw, events = mne.concatenate_raws(raw_list, events_list=events_list)
    if config.eeg:
        raw.set_eeg_reference(projection=True)
    del raw_list

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

    # 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}

    if config.eeg:
        ch_types = ['meg', 'eeg']
    else:
        ch_types = ['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(epochs_for_ica, picks=picks, decim=decim)

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

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

        if config.plot:
            # plot ICA components to html report
            from mne.report import Report
            report_name = op.join(
                meg_subject_dir,
                '{0}_{1}_{2}-ica.html'.format(subject, config.study_name,
                                              ch_type))
            report = Report(report_name, verbose=False)

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

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

                # XXX how to close each figure within the loop to avoid
                # runtime error: > 20 figures opened

            report.save(report_name, overwrite=True, open_browser=False)
Exemple #34
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 #35
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()
    events_list = list()
    print("  Loading raw data")

    for run in config.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()))
        eve_fname = op.splitext(raw_fname_in)[0] + '-eve.fif'
        print("Input: ", raw_fname_in, eve_fname)

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

        events = mne.read_events(eve_fname)
        events_list.append(events)

        raw_list.append(raw)

    print('  Concatenating runs')
    raw, events = mne.concatenate_raws(raw_list, events_list=events_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...")
    epochs_for_ica = mne.Epochs(raw_ica,
                                events,
                                config.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:
        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(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]))

        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.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(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 #36
0
        raw.info['bads'] = bad_MEG[nip]

        #######################################################################
        ############################ Maxfilter data ###########################
        #######################################################################
        sss_file_path = data_sss_directory + '/' + nip + '/' + raw_file_path.split(
            '/')[-1]
        sss_file_path = '_sss_raw'.join(sss_file_path.split('_raw'))

        sss_raw = maxwell_filter(raw,
                                 calibration=cal_path,
                                 cross_talk=ct_path,
                                 verbose=False,
                                 destination=refrun_path,
                                 bad_condition='warning')
        sss_raw.save(sss_file_path, overwrite=True, verbose=False)
        report.add_figs_to_section(sss_raw.plot(),
                                   captions=raw_file_path,
                                   section=nip)

        #######################################################################
        ######################## Close and save report ########################
        #######################################################################
        # Delete variables
        del raw, sss_raw

    # Save report
    report.save(os.getcwd() + '/1_maxFilter.html',
                overwrite=True,
                open_browser=False)
Exemple #37
0
def check_ICA_Cleaning(subject, runlist, badEEG, badICA):

    import mne
    import os.path as op
    import os
    from mne.report import Report
    from mne.io.pick import _picks_by_type as picks_by_type
    from mne.preprocessing import read_ica
    from mne.io import Raw
    from mne.preprocessing import create_ecg_epochs, create_eog_epochs

    data_path = '/neurospin/meg/meg_tmp/MTT_MEG_Baptiste/MEG/'
    report = Report(subject)

    raw_fnames = []
    for run in runlist:
        raw_fnames.append(data_path + subject + '/' + run + '_trans_sss.fif')
    # read raw data
    raw = Raw(raw_fnames, preload=True)
    raw.info['bads'] = badEEG
    # Highpass filter 1Hz on EOG/ECG channels
    picks = mne.pick_types(raw.info, meg=False, eeg=False, eog=True, ecg=False)
    raw.filter(l_freq=1, h_freq=2, picks=picks)
    picks = mne.pick_types(raw.info, meg=True, eeg=True, eog=True, ecg=True)
    raw.filter(l_freq=None, h_freq=30, picks=picks, n_jobs=4)

    results_dir = op.join(data_path, subject, 'artefactICA')
    if not op.exists(results_dir):
        os.makedirs(results_dir)

    # Create ICA file for each data type (MEG and EEG)
    for ch_type, picks in picks_by_type(
            raw.info, meg_combined=True):  # bad EEG channels are excluded
        # Read ICA file
        ica = []
        ica = read_ica(data_path + subject + '/mne_python/ICA/ICA_' + ch_type +
                       '_allRuns')

        for badICAsel in badICA[ch_type]:
            # Select bad component and assess artefact rejection quality
            ica_exclude = badICAsel

            ecg_evoked = create_ecg_epochs(raw, tmin=-.5, tmax=.5,
                                           picks=picks).average()
            fig = ica.plot_overlay(ecg_evoked,
                                   exclude=ica_exclude)  # plot ECG cleaning
            report.add_figs_to_section(
                fig,
                str(badICAsel) + ' Rejection overlay (%s)' % ch_type, 'ECG')

            eog_evoked = create_eog_epochs(raw, tmin=-.5, tmax=.5,
                                           picks=picks).average()
            fig = ica.plot_overlay(eog_evoked,
                                   exclude=ica_exclude)  # plot EOG cleaning
            report.add_figs_to_section(
                fig,
                str(badICAsel) + ' Rejection overlay (%s)' % ch_type, 'EOG')

            fig = ica.plot_overlay(raw)
            report.add_figs_to_section(
                fig,
                str(badICAsel) + ' Rejection overlay (%s)' % ch_type,
                'Workroad artefact')

    report.save((results_dir + '/AllRunsChecked.html'),
                open_browser=False,
                overwrite=True)
def ICA_denoising_full(subject, runlist, badEEG):

    import mne
    import os.path as op
    import os
    import matplotlib
    matplotlib.use('Agg')

    from mne.report import Report
    from mne.io import Raw
    from meeg_preprocessing import compute_ica
    from mne.io.pick import _picks_by_type as picks_by_type

    ###########################################################################
    # jobs and runtime performance
    n_components = 0.99
    decim = 5  # decimation
    n_max_ecg, n_max_eog = 5, 5  # limit components detected due to ECG / EOG
    ica_reject = {'mag': 5e-12, 'grad': 5000e-13, 'eeg': 500e-6}

    ###########################################################################
    data_path = '/neurospin/meg/meg_tmp/MTT_MEG_Baptiste/MEG/'
    raw_fnames = []
    for run in runlist:
        raw_fnames.append(data_path + subject + '/' + run + '_trans_sss.fif')
    report = Report(subject)
    # read raw data
    raw = Raw(raw_fnames, preload=True)
    raw.info['bads'] = badEEG
    # Highpass filter 1Hz on EOG/ECG channels
    picks = mne.pick_types(raw.info, meg=False, eeg=False, eog=False, ecg=True)
    raw.filter(l_freq=1, h_freq=30, picks=picks)
    picks = mne.pick_types(raw.info, meg=False, eeg=False, eog=True, ecg=False)
    raw.filter(l_freq=1, h_freq=2, picks=picks)
    picks = mne.pick_types(raw.info, meg=True, eeg=True, eog=True, ecg=True)
    raw.filter(l_freq=None, h_freq=30, picks=picks, n_jobs=4)

    ###########################################################################
    # Create the report directory if it doesn't exist
    results_dir = op.join(data_path, subject, 'artefactICA')
    if not op.exists(results_dir):
        os.makedirs(results_dir)

    # Create the results directory if it doesn't exist
    ica_dir = op.join(data_path, subject, 'mne_python/ICA')
    if not op.exists(ica_dir):
        os.makedirs(ica_dir)

    report = mne.report.Report(subject)

    ###########################################################################
    for ch_type, picks in picks_by_type(
            raw.info, meg_combined=True):  # bad EEG channels are excluded
        print 'loul'
        ica, _ = compute_ica(raw,
                             picks=picks,
                             subject=subject,
                             n_components=n_components,
                             n_max_ecg=n_max_ecg,
                             n_max_eog=n_max_eog,
                             reject=ica_reject,
                             random_state=666,
                             decim=decim,
                             report=report)

        # when pb with eeg
        #ica = ICA(n_components=0.99, method='fastica')
        #ica.fit(raw, picks=picks, decim=5, reject=dict(eeg=300e-6))
        ica.save((ica_dir + '/ICA_' + ch_type + '_allRuns'))

    report.save((results_dir + '/AllRuns.html'),
                open_browser=False,
                overwrite=True)
    del raw
Exemple #39
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 found, cannot create joint plot'):
        r.add_evokeds(evokeds=evoked_no_ch_locs,
                      titles=['evoked no chan locs'],
                      tags=('evoked', ),
                      projs=False,
                      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 'Projectors' 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.raises(ValueError,
                       match='does not contain.*channel locations'):
        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)
Exemple #40
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 #41
0
def run_ica(*, cfg, subject, session=None):
    """Run ICA."""
    bids_basename = BIDSPath(subject=subject,
                             session=session,
                             task=cfg.task,
                             acquisition=cfg.acq,
                             recording=cfg.rec,
                             space=cfg.space,
                             datatype=cfg.datatype,
                             root=cfg.deriv_root,
                             check=False)

    raw_fname = bids_basename.copy().update(processing='filt', suffix='raw')
    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+components',
                                               suffix='report',
                                               extension='.html')

    # Generate a list of raw data paths (i.e., paths of individual runs)
    # we want to create epochs from.
    raw_fnames = []
    for run in cfg.runs:
        raw_fname.update(run=run)
        if raw_fname.copy().update(split='01').fpath.exists():
            raw_fname.update(split='01')

        raw_fnames.append(raw_fname.copy())

    # Generate a unique event name -> event code mapping that can be used
    # across all runs.
    event_name_to_code_map = annotations_to_events(raw_paths=raw_fnames)

    # Now, generate epochs from each individual run
    epochs_all_runs = []
    eog_epochs_all_runs = []
    ecg_epochs_all_runs = []

    for run, raw_fname in zip(cfg.runs, raw_fnames):
        msg = f'Loading filtered raw data from {raw_fname} and creating epochs'
        logger.info(**gen_log_kwargs(
            message=msg, subject=subject, session=session, run=run))
        raw = mne.io.read_raw_fif(raw_fname, preload=True)

        # EOG epochs
        eog_epochs = make_eog_epochs(raw=raw,
                                     eog_channels=cfg.eog_channels,
                                     subject=subject,
                                     session=session,
                                     run=run)
        if eog_epochs is not None:
            eog_epochs_all_runs.append(eog_epochs)

        # ECG epochs
        ecg_epochs = make_ecg_epochs(cfg=cfg,
                                     raw=raw,
                                     subject=subject,
                                     session=session,
                                     run=run)
        if ecg_epochs is not None:
            ecg_epochs_all_runs.append(ecg_epochs)

        # Produce high-pass filtered version of the data for ICA.
        # Sanity check – make sure we're using the correct data!
        if cfg.resample_sfreq is not None:
            assert np.allclose(raw.info['sfreq'], cfg.resample_sfreq)
        if cfg.l_freq is not None:
            assert np.allclose(raw.info['highpass'], cfg.l_freq)

        filter_for_ica(cfg=cfg,
                       raw=raw,
                       subject=subject,
                       session=session,
                       run=run)

        # Only keep the subset of the mapping that applies to the current run
        event_id = event_name_to_code_map.copy()
        for event_name in event_id.copy().keys():
            if event_name not in raw.annotations.description:
                del event_id[event_name]

        msg = 'Creating task-related epochs …'
        logger.info(**gen_log_kwargs(
            message=msg, subject=subject, session=session, run=run))
        epochs = make_epochs(raw=raw,
                             event_id=event_id,
                             tmin=cfg.epochs_tmin,
                             tmax=cfg.epochs_tmax,
                             event_repeated=cfg.event_repeated,
                             decim=cfg.decim)

        epochs_all_runs.append(epochs)
        del raw, epochs, eog_epochs, ecg_epochs  # free memory

    # Lastly, we can concatenate the epochs and set an EEG reference
    epochs = mne.concatenate_epochs(epochs_all_runs, on_mismatch='warn')

    if eog_epochs_all_runs:
        epochs_eog = mne.concatenate_epochs(eog_epochs_all_runs,
                                            on_mismatch='warn')
    else:
        epochs_eog = None

    if ecg_epochs_all_runs:
        epochs_ecg = mne.concatenate_epochs(ecg_epochs_all_runs,
                                            on_mismatch='warn')
    else:
        epochs_ecg = None

    del epochs_all_runs, eog_epochs_all_runs, ecg_epochs_all_runs

    epochs.load_data()
    if 'eeg' in cfg.ch_types:
        projection = True if cfg.eeg_reference == 'average' else False
        epochs.set_eeg_reference(cfg.eeg_reference, projection=projection)

    # Reject epochs based on peak-to-peak rejection thresholds
    msg = f'Using PTP rejection thresholds: {cfg.ica_reject}'
    logger.info(
        **gen_log_kwargs(message=msg, subject=subject, session=session))

    # Reject epochs based on peak-to-peak amplitude
    epochs.drop_bad(reject=cfg.ica_reject)
    if epochs_eog is not None:
        epochs_eog.drop_bad(reject=cfg.ica_reject)
    if epochs_ecg is not None:
        epochs_ecg.drop_bad(reject=cfg.ica_reject)

    # Now actually perform ICA.
    msg = 'Calculating ICA solution.'
    logger.info(
        **gen_log_kwargs(message=msg, subject=subject, session=session))
    ica = fit_ica(cfg=cfg, epochs=epochs, subject=subject, session=session)

    # Start a report
    title = f'ICA – sub-{subject}'
    if session is not None:
        title += f', ses-{session}'
    if cfg.task is not None:
        title += f', task-{cfg.task}'

    report = Report(info_fname=epochs, title=title, verbose=False)

    # ECG and EOG component detection
    if epochs_ecg:
        ecg_ics = detect_bad_components(cfg=cfg,
                                        which='ecg',
                                        epochs=epochs_ecg,
                                        ica=ica,
                                        subject=subject,
                                        session=session,
                                        report=report)
    else:
        ecg_ics = []

    if epochs_eog:
        eog_ics = detect_bad_components(cfg=cfg,
                                        which='eog',
                                        epochs=epochs_eog,
                                        ica=ica,
                                        subject=subject,
                                        session=session,
                                        report=report)
    else:
        eog_ics = []

    # 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_kwargs(message=msg, 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, add info about the epochs used for the ICA fit, and plot all ICs
    # for manual inspection.
    fig = epochs.plot_drop_log(subject=f'sub-{subject}', show=cfg.interactive)
    caption = 'Dropped epochs before fit'
    report.add_figs_to_section(fig, section=f'sub-{subject}', captions=caption)

    msg = 'Adding diagnostic plots for all ICs to the HTML report …'
    logger.info(
        **gen_log_kwargs(message=msg, 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 cfg.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_kwargs(message=msg, subject=subject, session=session))
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 = read_raw_fif(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)
    if sys.version.startswith('3.5'):  # XXX Some strange MPL/3.5 error...
        raise SkipTest('Python 3.5 and mpl have unresolved issues')
    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)
def compute_LF_matrix(sbj_id, sbj_dir, raw_info, aseg, spacing, labels):
    import os.path as op
    import mne

    from mne.bem import make_watershed_bem
    from mne.report import Report

    from nipype.utils.filemanip import split_filename as split_f

    from neuropype_ephy.compute_fwd_problem import create_mixed_source_space

    report = Report()

    bem_dir = op.join(sbj_dir, sbj_id, 'bem')

    surf_name = 'inner_skull.surf'
    sbj_inner_skull_fname = op.join(bem_dir, sbj_id + '-' + surf_name)
    inner_skull_fname = op.join(bem_dir, surf_name)

    data_path, raw_fname, ext = split_f(raw_info['filename'])

    if aseg:
        fwd_filename = op.join(data_path, '%s-%s-aseg-fwd.fif'
                               % (raw_fname, spacing))
    else:
        fwd_filename = op.join(data_path, '%s-%s-fwd.fif'
                               % (raw_fname, spacing))

    # check if we have just created the fwd matrix
    if not op.isfile(fwd_filename):
        # check if bem-sol was created, if not creates the bem sol using C MNE
        bem_fname = op.join(bem_dir, '%s-5120-bem-sol.fif' % sbj_id)
        model_fname = op.join(bem_dir, '%s-5120-bem.fif' % sbj_id)
        if not op.isfile(bem_fname):
            # chek if inner_skull surf exists, if not BEM computation is
            # performed by MNE python functions mne.bem.make_watershed_bem
            if not (op.isfile(sbj_inner_skull_fname) or
                    op.isfile(inner_skull_fname)):
                print sbj_inner_skull_fname + '---> FILE NOT FOUND!!! ---> BEM is computed'                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
                make_watershed_bem(sbj_id, sbj_dir, overwrite=True)
            else:
                print '*** inner skull surface exists!!!'

            # Create a BEM model for a subject
            surfaces = mne.make_bem_model(sbj_id, ico=4, conductivity=[0.3],
                                          subjects_dir=sbj_dir)
            # Write BEM surfaces to a fiff file
            mne.write_bem_surfaces(model_fname, surfaces)

            # Create a BEM solution using the linear collocation approach
            bem = mne.make_bem_solution(surfaces)
            mne.write_bem_solution(bem_fname, bem)

            print '*** BEM solution file %s written ***' % bem_fname
            # add BEM figures to a Report
            report.add_bem_to_section(subject=sbj_id, subjects_dir=sbj_dir)
            report_filename = op.join(bem_dir, "BEM_report.html")
            print report_filename
            report.save(report_filename, open_browser=False, overwrite=True)
        else:
            bem = bem_fname
            print '*** BEM solution file %s exists!!!' % bem_fname

        # check if source space exists, if not it creates using mne-python fun
        # we have to create the cortical surface source space even when aseg is
        # True
        src_fname = op.join(bem_dir, '%s-%s-src.fif' % (sbj_id, spacing))
        if not op.isfile(src_fname):
            src = mne.setup_source_space(sbj_id, subjects_dir=sbj_dir,
                                         fname=True,
                                         spacing=spacing.replace('-', ''),
                                         add_dist=False, overwrite=True,
                                         n_jobs=2)
            print '*** source space file %s written ***' % src_fname         
        else:
            print '*** source space file %s exists!!!' % src_fname
            src = mne.read_source_spaces(src_fname)

        if aseg:
            src = create_mixed_source_space(sbj_dir, sbj_id, spacing,
                                            labels, src)

        n = sum(src[i]['nuse'] for i in range(len(src)))
        print('il src space contiene %d spaces e %d vertici' % (len(src), n))

        # check if the co-registration file was created
        # if not raise an runtime error
        trans_fname = op.join(data_path, '%s-trans.fif' % raw_fname)
        if not op.isfile(trans_fname):
            raise RuntimeError('coregistration file %s NOT found!!!'
                               % trans_fname)

        # if all is ok creates the fwd matrix
        mne.make_forward_solution(raw_info, trans_fname, src, bem,
                                  fwd_filename,
                                  mindist=5.0, # ignore sources <= 0mm from inner skull
                                  meg=True, eeg=False,
                                  n_jobs=2,
                                  overwrite=True)

    else:
        print '*** FWD file %s exists!!!' % fwd_filename

    return fwd_filename
def preprocess_ICA_fif_to_ts(fif_file, ECG_ch_name, EoG_ch_name, l_freq, h_freq, down_sfreq, variance, is_sensor_space, data_type):
    import os
    import numpy as np

    import mne
    from mne.io import Raw
    from mne.preprocessing import ICA, read_ica
    from mne.preprocessing import create_ecg_epochs, create_eog_epochs
    from mne.report import Report

    from nipype.utils.filemanip import split_filename as split_f

    report = Report()

    subj_path, basename, ext = split_f(fif_file)
    (data_path, sbj_name) = os.path.split(subj_path)
    print data_path

    # Read raw
    # If None the compensation in the data is not modified.
    # If set to n, e.g. 3, apply gradient compensation of grade n as for
    # CTF systems (compensation=3)
    raw = Raw(fif_file, preload=True)

    # select sensors
    select_sensors = mne.pick_types(raw.info, meg=True, ref_meg=False,
                                    exclude='bads')
    picks_meeg = mne.pick_types(raw.info, meg=True, eeg=True, exclude='bads')

    # save electrode locations
    sens_loc = [raw.info['chs'][i]['loc'][:3] for i in select_sensors]
    sens_loc = np.array(sens_loc)

    channel_coords_file = os.path.abspath("correct_channel_coords.txt")
    print '*** ' + channel_coords_file + '***'
    np.savetxt(channel_coords_file, sens_loc, fmt='%s')

    # save electrode names
    sens_names = np.array([raw.ch_names[pos] for pos in select_sensors],dtype = "str")

    # AP 21032016 
#    channel_names_file = os.path.join(data_path, "correct_channel_names.txt") 
    channel_names_file = os.path.abspath("correct_channel_names.txt")
    np.savetxt(channel_names_file,sens_names , fmt = '%s')
 
    ### filtering + downsampling
    raw.filter(l_freq=l_freq, h_freq=h_freq, picks=picks_meeg,
               method='iir', n_jobs=8)
#    raw.filter(l_freq = l_freq, h_freq = h_freq, picks = picks_meeg,
#               method='iir')
#    raw.resample(sfreq=down_sfreq, npad=0)

    ### 1) Fit ICA model using the FastICA algorithm
    # Other available choices are `infomax` or `extended-infomax`
    # We pass a float value between 0 and 1 to select n_components based on the
    # percentage of variance explained by the PCA components.
    ICA_title = 'Sources related to %s artifacts (red)'
    is_show = False # visualization
    reject = dict(mag=4e-12, grad=4000e-13)

    # check if we have an ICA, if yes, we load it
    ica_filename = os.path.join(subj_path,basename + "-ica.fif")  
    if os.path.exists(ica_filename) is False:
        ica = ICA(n_components=variance, method='fastica', max_iter=500) # , max_iter=500
        ica.fit(raw, picks=select_sensors, reject=reject) # decim = 3, 

        has_ICA = False
    else:
        has_ICA = True
        print ica_filename + '   exists!!!'
        ica = read_ica(ica_filename)
        ica.exclude = []

    # 2) identify bad components by analyzing latent sources.
    # generate ECG epochs use detection via phase statistics

    # if we just have exclude channels we jump these steps
#    if len(ica.exclude)==0:
    n_max_ecg = 3
    n_max_eog = 2

    # check if ECG_ch_name is in the raw channels
    if ECG_ch_name in raw.info['ch_names']:
        ecg_epochs = create_ecg_epochs(raw, tmin=-.5, tmax=.5,
                                       picks=select_sensors,
                                       ch_name=ECG_ch_name)
    # if not  a synthetic ECG channel is created from cross channel average
    else:
        ecg_epochs = create_ecg_epochs(raw, tmin=-.5, tmax=.5,
                                       picks=select_sensors)

    # ICA for ECG artifact
    # threshold=0.25 come default
    ecg_inds, scores = ica.find_bads_ecg(ecg_epochs, method='ctps')
    print scores
    print '\n len ecg_inds *** ' + str(len(ecg_inds)) + '***\n'
    if len(ecg_inds) > 0:
        ecg_evoked = ecg_epochs.average()

        fig1 = ica.plot_scores(scores, exclude=ecg_inds,
                               title=ICA_title % 'ecg', show=is_show)

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

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

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

        ecg_inds = ecg_inds[:n_max_ecg]
        ica.exclude += ecg_inds
    
        fig4 = ica.plot_sources(ecg_evoked, exclude=ecg_inds, show=is_show)  # plot ECG sources + selection
        fig5 = ica.plot_overlay(ecg_evoked, exclude=ecg_inds, show=is_show)  # plot ECG cleaning
    
        fig = [fig1, fig2, fig3, fig4, fig5]
        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', 
                                                  'ECG overlay'], section = 'ICA - ECG')    
    
    # check if EoG_ch_name is in the raw channels
    # if EoG_ch_name is empty if data_type is fif, ICA routine automatically looks for EEG61, EEG62
    # otherwise if data_type is ds we jump this step
    if not EoG_ch_name and data_type=='ds':
        eog_inds = []
    else:
        if EoG_ch_name in raw.info['ch_names']:        
            ### ICA for eye blink artifact - detect EOG by correlation
            eog_inds, scores = ica.find_bads_eog(raw, ch_name = EoG_ch_name)
        else:
            eog_inds, scores = ica.find_bads_eog(raw)

    if len(eog_inds) > 0:  
        
        fig6 = ica.plot_scores(scores, exclude=eog_inds, title=ICA_title % 'eog', show=is_show)
        report.add_figs_to_section(fig6, captions=['Scores of ICs related to EOG'], 
                           section = 'ICA - EOG')
                           
        # check how many EoG ch we have
        rs = np.shape(scores)
        if len(rs)>1:
            rr = rs[0]
            show_picks = [np.abs(scores[i][:]).argsort()[::-1][:5] for i in range(rr)]
            for i in range(rr):
                fig7 = ica.plot_sources(raw, show_picks[i][:], exclude=eog_inds, 
                                    start = raw.times[0], stop  = raw.times[-1], 
                                    title=ICA_title % 'eog',show=is_show)       
                                    
                fig8 = ica.plot_components(show_picks[i][:], title=ICA_title % 'eog', colorbar=True, show=is_show) # ICA nel tempo

                fig = [fig7, fig8]
                report.add_figs_to_section(fig, captions=['Scores of ICs related to EOG', 
                                                 'Time Series plots of ICs (EOG)'],
                                            section = 'ICA - EOG')    
        else:
            show_picks = np.abs(scores).argsort()[::-1][:5]
            fig7 = ica.plot_sources(raw, show_picks, exclude=eog_inds, title=ICA_title % 'eog', show=is_show)                                    
            fig8 = ica.plot_components(show_picks, title=ICA_title % 'eog', colorbar=True, show=is_show) 
            fig = [fig7, fig8]            
            report.add_figs_to_section(fig, captions=['Time Series plots of ICs (EOG)',
                                                      'TopoMap of ICs (EOG)',],
                                            section = 'ICA - EOG') 
        
        eog_inds = eog_inds[:n_max_eog]
        ica.exclude += eog_inds
        
        if EoG_ch_name in raw.info['ch_names']:
            eog_evoked = create_eog_epochs(raw, tmin=-.5, tmax=.5, picks=select_sensors, 
                                   ch_name=EoG_ch_name).average()
        else:
            eog_evoked = create_eog_epochs(raw, tmin=-.5, tmax=.5, picks=select_sensors).average()               
       
        fig9 = ica.plot_sources(eog_evoked, exclude=eog_inds, show=is_show)  # plot EOG sources + selection
        fig10 = ica.plot_overlay(eog_evoked, exclude=eog_inds, show=is_show)  # plot EOG cleaning

        fig = [fig9, fig10]
        report.add_figs_to_section(fig, captions=['Time-locked EOG sources',
                                                  'EOG overlay'], section = 'ICA - EOG')

    fig11 = ica.plot_overlay(raw, show=is_show)
    report.add_figs_to_section(fig11, captions=['Signal'], section = 'Signal quality') 
   
    ### plot all topographies and time seris of the ICA components
    n_ica_components = ica.mixing_matrix_.shape[1]
    
    n_topo = 10;
    n_fig  = n_ica_components/n_topo;
    n_plot = n_ica_components%n_topo;

    print '*************** n_fig = ' + str(n_fig) + ' n_plot = ' + str(n_plot) + '********************'
    fig = []
    t_start = 0
    t_stop = None # 60 if we want to take the fist 60s
    for n in range(0,n_fig):
        fig_tmp = ica.plot_components(range(n_topo*n,n_topo*(n+1)),title='ICA components', show=is_show)    
        fig.append(fig_tmp)
        fig_tmp = ica.plot_sources(raw, range(n_topo*n,n_topo*(n+1)), 
                                    start = t_start, stop  = t_stop, 
                                    title='ICA components')     
        fig.append(fig_tmp)
    
#    if n_plot > 0:
#        fig_tmp = ica.plot_components(range(n_fig*n_topo,n_ica_components), title='ICA components', show=is_show)    
#        fig.append(fig_tmp)
#        fig_tmp = ica.plot_sources(raw, range(n_fig*n_topo,n_ica_components), 
#                                        start = t_start, stop  = t_stop,
#                                        title='ICA components')     
#        fig.append(fig_tmp)   
#   
#    for n in range(0, len(fig)):
#        report.add_figs_to_section(fig[n], captions=['TOPO'], section = 'ICA Topo Maps')   
     
    if n_plot > 5:
        n_fig_l  = n_plot//5
                
        print '*************************** ' + str(n_fig_l) + ' *********************************'
        for n in range(0,n_fig_l):
            print range(n_fig*n_topo+5*n, n_fig*n_topo+5*(n+1))
            fig_tmp = ica.plot_components(range(n_fig*n_topo+5*n, n_fig*n_topo+5*(n+1)),title='ICA components')    
            fig.append(fig_tmp)
            fig_tmp = ica.plot_sources(raw, range(n_fig*n_topo+5*n, n_fig*n_topo+5*(n+1)), 
                                    start = t_start, stop  = t_stop, 
                                    title='ICA components')     
            fig.append(fig_tmp)
        
        print range(n_fig*n_topo+5*(n+1),n_ica_components)
        fig_tmp = ica.plot_components(range(n_fig*n_topo+5*(n+1),n_ica_components), title='ICA components')    
        fig.append(fig_tmp)
        fig_tmp = ica.plot_sources(raw, range(n_fig*n_topo+5*(n+1),n_ica_components), 
                                        start = t_start, stop  = t_stop, 
                                        title='ICA components')     
        fig.append(fig_tmp)   
        
    for n in range(0, len(fig)):
        report.add_figs_to_section(fig[n], captions=['TOPO'], section = 'ICA Topo Maps')       
    
    report_filename = os.path.join(subj_path,basename + "-report.html")
    print '******* ' + report_filename
    report.save(report_filename, open_browser=False, overwrite=True)
        
        
    # 3) apply ICA to raw data and save solution and report
    # check the amplitudes do not change
#    raw_ica_file = os.path.abspath(basename[:i_raw] + 'ica-raw.fif')
    raw_ica_file = os.path.join(subj_path, basename + '-preproc-raw.fif')
    raw_ica = ica.apply(raw)

    raw_ica.resample(sfreq=down_sfreq, npad=0)

    raw_ica.save(raw_ica_file, overwrite=True)

    # save ICA solution
    print ica_filename
    if has_ICA is False:
        ica.save(ica_filename)

    # 4) save data
    data_noIca, times = raw[select_sensors, :]
    data, times = raw_ica[select_sensors, :]

    print data.shape
    print raw.info['sfreq']

    ts_file = os.path.abspath(basename + "_ica.npy")
    np.save(ts_file, data)
    print '***** TS FILE ' + ts_file + '*****'

    if is_sensor_space:
        return ts_file, channel_coords_file, channel_names_file, raw.info['sfreq']
    else:
#        return raw_ica, channel_coords_file, channel_names_file, raw.info['sfreq']
        return raw_ica_file, channel_coords_file, channel_names_file, raw.info['sfreq']
Exemple #45
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)
def test_compute_LF_matrix():
    import os
    import os.path as op
    import nipype.pipeline.engine as pe
    from nipype.interfaces.mne import WatershedBEM
    import mne
    import mne.io as io
    from mne.minimum_norm import make_inverse_operator, apply_inverse_raw
    from mne.report import Report
    from nipype.utils.filemanip import split_filename as split_f
    main_path = '/home/karim/Documents/pasca/data/resting_state/'
    sbj_id = 'K0002'
    sbj_dir = op.join(main_path, 'FSF')
    bem_dir = op.join(sbj_dir, sbj_id, 'bem')
    surface_dir = op.join(sbj_dir, sbj_id, 'bem/watershed')
    data_dir = op.join(main_path, 'MEG')
    raw_fname = op.join(data_dir, '%s/%s_rest_tsss_mc.fif' % (sbj_id, sbj_id))
    raw = io.Raw(raw_fname, preload=True)
    picks = mne.pick_types(raw.info, meg=True, ref_meg=False, exclude='bads')
    raw.filter(l_freq=0.1, h_freq=300, picks=picks, method='iir', n_jobs=2)
    raw.resample(sfreq=300, npad=0)
    report = Report()
    surfaces = [sbj_id + '_brain_surface',
     sbj_id + '_inner_skull_surface',
     sbj_id + '_outer_skull_surface',
     sbj_id + '_outer_skin_surface']
    new_surfaces = ['brain.surf',
     'inner_skull.surf',
     'outer_skull.surf',
     'outer_skin.surf']
    sbj_inner_skull_fname = op.join(bem_dir, sbj_id + '-' + new_surfaces[1])
    inner_skull_fname = op.join(bem_dir, new_surfaces[1])
    if not (op.isfile(sbj_inner_skull_fname) or op.isfile(inner_skull_fname)):
        bem_IF = WatershedBEM()
        bem_IF.inputs.subject_id = sbj_id
        bem_IF.inputs.subjects_dir = sbj_dir
        bem_IF.inputs.atlas_mode = True
        bem_IF.run()
        for i in range(len(surfaces)):
            os.system('cp %s %s' % (op.join(surface_dir, surfaces[i]), op.join(bem_dir, sbj_id + '-' + new_surfaces[i])))

    else:
        print '*** inner skull surface exists!!!'
    bem = op.join(bem_dir, '%s-5120-bem-sol.fif' % sbj_id)
    if not op.isfile(bem):
        os.system('$MNE_ROOT/bin/mne_setup_forward_model --subject ' + sbj_id + ' --homog --surf --ico 4')
    else:
        print '*** BEM solution file exists!!!'
    src_fname = op.join(bem_dir, '%s-ico-5-src.fif' % sbj_id)
    if not op.isfile(src_fname):
        src = mne.setup_source_space(sbj_id, fname=True, spacing='ico5', subjects_dir=sbj_dir, overwrite=True, n_jobs=2)
    else:
        print '*** source space file exists!!!'
        src = mne.read_source_spaces(src_fname)
    trans_fname = op.join(data_dir, '%s/%s-trans.fif' % (sbj_id, sbj_id))
    data_path, basename, ext = split_f(raw_fname)
    fwd_filename = op.join(data_path, '%s-fwd.fif' % basename)
    forward = mne.make_forward_solution(raw_fname, trans_fname, src, bem, fwd_filename, n_jobs=2, overwrite=True)
    forward = mne.convert_forward_solution(forward, surf_ori=True)
    snr = 1.0
    lambda2 = 1.0 / snr ** 2
    method = 'MNE'
    reject = dict(mag=4e-12, grad=4e-10, eog=0.00025)
    noise_cov = mne.compute_raw_data_covariance(raw, picks=picks, reject=reject)
    inverse_operator = make_inverse_operator(raw.info, forward, noise_cov, loose=0.2, depth=0.8)
    start, stop = raw.time_as_index([0, 30])
    stc = apply_inverse_raw(raw, inverse_operator, lambda2, method, label=None, start=start, stop=stop, pick_ori=None)
    print '***'
    stc.shape
    print '***'
    subj_path, basename, ext = split_f(raw_fname)
    stc_filename = op.join(subj_path, basename)
    stc.save(stc_filename)
    report_filename = op.join(subj_path, basename + '-BEM-report.html')
    print report_filename
    report.save(report_filename, open_browser=False, overwrite=True)
    return
Exemple #47
0
def autodenoise_MEG(subject, run):

    ###############################################################################
    import numpy as np

    import mne
    import os.path as op
    import os
    import matplotlib
    matplotlib.use('Agg')

    from mne.report import Report
    from mne.io import Raw
    from mne.preprocessing import ICA
    from mne.preprocessing import create_ecg_epochs, create_eog_epochs

    data_path = '/neurospin/meg/meg_tmp/MTT_MEG_Baptiste/MEG/'
    raw_fname = data_path + subject + '/' + run + '_trans_sss.fif'
    # ICA
    report = Report(subject)

    raw = Raw(raw_fname, preload=True)
    raw.filter(None, 30, n_jobs=4)

    picks = mne.pick_types(raw.info,
                           meg=True,
                           eeg=False,
                           eog=False,
                           stim=False,
                           exclude='bads')

    ica = ICA(n_components=0.95, method='fastica')
    ica.fit(raw, picks=picks, decim=4)

    # maximum number of components to reject
    n_max_ecg, n_max_eogv, n_max_eogh = 3, 3, 3

    title = 'Sources related to %s artifacts (red)'

    # Create the results directory if it doesn't exist
    results_dir = op.join(data_path, subject, 'artefactICA')
    if not op.exists(results_dir):
        os.makedirs(results_dir)
    #ica_fname = '%s_ica' % (subject)

    ###############################################################################
    # generate ECG epochs use detection via phase statistics
    ecg_epochs = create_ecg_epochs(raw, tmin=-.5, tmax=.5, picks=picks)

    ecg_inds, scores = ica.find_bads_ecg(ecg_epochs, method='ctps')
    fig1 = ica.plot_scores(scores,
                           exclude=ecg_inds,
                           title=title % 'ecg',
                           labels='ecg')
    report.add_figs_to_section(fig1, captions='ECGcomp', section='ICA')

    show_picks = np.abs(scores).argsort()[::-1][:5]

    fig2 = ica.plot_sources(raw,
                            show_picks,
                            exclude=ecg_inds,
                            title=title % 'ecg')
    report.add_figs_to_section(fig2, captions='ECGcomptime', section='ICA')
    fig3 = ica.plot_components(ecg_inds, title=title % 'ecg', colorbar=True)
    report.add_figs_to_section(fig3, captions='ECGcomptopo', section='ICA')

    ecg_inds = ecg_inds[:n_max_ecg]

    report.save((results_dir + '/' + run + '.html'),
                open_browser=False,
                overwrite=True)
    ###############################################################################
    # detect vertical EOG by correlation

    eogv_inds = []
    eogv_inds, scores = ica.find_bads_eog(raw, ch_name='EOG061')
    fig4 = ica.plot_scores(scores,
                           exclude=eogv_inds,
                           title=title % 'eogv',
                           labels='ecg')
    report.add_figs_to_section(fig4, captions='EOGvcomp', section='ICA')

    show_picks = np.abs(scores).argsort()[::-1][:5]

    fig5 = ica.plot_sources(raw,
                            show_picks,
                            exclude=eogv_inds,
                            title=title % 'eogv')
    report.add_figs_to_section(fig5, captions='EOGvcomptime', section='ICA')
    fig6 = ica.plot_components(eogv_inds, title=title % 'eogv', colorbar=True)
    report.add_figs_to_section(fig6, captions='EOGvcomptopo', section='ICA')

    eogv_inds = eogv_inds[:n_max_eogv]

    report.save((results_dir + '/' + run + '.html'),
                open_browser=False,
                overwrite=True)
    ###############################################################################
    # detect horizontal EOG by correlation

    eogh_inds = []
    eogh_inds, scores = ica.find_bads_eog(raw, ch_name='EOG062')
    fig7 = ica.plot_scores(scores,
                           exclude=eogh_inds,
                           title=title % 'eogh',
                           labels='ecg')
    report.add_figs_to_section(fig7, captions='EOGhcomp', section='ICA')

    show_picks = np.abs(scores).argsort()[::-1][:5]

    fig8 = ica.plot_sources(raw,
                            show_picks,
                            exclude=eogh_inds,
                            title=title % 'eogh')
    report.add_figs_to_section(fig8, captions='EOGhcomptime', section='ICA')
    fig9 = ica.plot_components(eogh_inds, title=title % 'eogh', colorbar=True)
    report.add_figs_to_section(fig9, captions='EOGhcomptopo', section='ICA')

    eogh_inds = eogh_inds[:n_max_eogh]

    report.save((results_dir + '/' + run + '.html'),
                open_browser=False,
                overwrite=True)
    ###############################################################################
    # estimate average artifact
    ecg_evoked = ecg_epochs.average()
    fig10 = ica.plot_sources(ecg_evoked,
                             exclude=ecg_inds)  # plot ECG sources + selection
    fig11 = ica.plot_overlay(ecg_evoked, exclude=ecg_inds)  # plot ECG cleaning
    report.add_figs_to_section(fig10, captions='ECG sources', section='ICA')
    report.add_figs_to_section(fig11, captions='ECG clean', section='ICA')

    eogv_evoked = create_eog_epochs(raw,
                                    tmin=-.5,
                                    ch_name='EOG061',
                                    tmax=.5,
                                    picks=picks).average()
    fig12 = ica.plot_sources(eogv_evoked,
                             exclude=eogv_inds)  # plot EOG sources + selection
    fig13 = ica.plot_overlay(eogv_evoked,
                             exclude=eogv_inds)  # plot EOG cleaning
    report.add_figs_to_section(fig12, captions='EOGv sources', section='ICA')
    report.add_figs_to_section(fig13, captions='EOGv clean', section='ICA')

    eogh_evoked = create_eog_epochs(raw,
                                    tmin=-.5,
                                    ch_name='EOG062',
                                    tmax=.5,
                                    picks=picks).average()
    fig14 = ica.plot_sources(eogh_evoked,
                             exclude=eogh_inds)  # plot EOG sources + selection
    fig15 = ica.plot_overlay(eogh_evoked,
                             exclude=eogh_inds)  # plot EOG cleaning
    report.add_figs_to_section(fig14, captions='EOGh sources', section='ICA')
    report.add_figs_to_section(fig15, captions='EOGh clean', section='ICA')

    # check the amplitudes do not change
    fig16 = ica.plot_overlay(raw)  # EOG artifacts remain
    report.add_figs_to_section(fig16, captions='avg', section='ICA')

    ###############################################################################
    # Create the results directory if it doesn't exist
    ica_dir = op.join(data_path, subject, 'mne_python/ICA')
    if not op.exists(ica_dir):
        os.makedirs(ica_dir)

    # mark bad components and save ica
    artifact_inds = list(np.unique(eogv_inds + eogh_inds + ecg_inds))
    ica.exclude = []
    ica.exclude = artifact_inds
    ica.save((ica_dir + '/ICA_MEG' + run))
            for bad_component in ica_rejects[nip][ch_type]['eog1']:
                # Plots                            
                report.add_figs_to_section(ica.plot_properties(eog_epochs, picks=[bad_component]), 
                                           captions=ch_type.upper() + ' - EOG061 - ICA n. ' + str(bad_component),
                                           section=nip)

                report.add_figs_to_section(ica.plot_overlay(eog_average, exclude=[bad_component]),
                                           captions=' ',
                                           section=nip)
    
            report.add_figs_to_section(ica.plot_overlay(eog_average, exclude=ica_rejects[nip][ch_type]['eog1']), 
                                       captions=ch_type.upper() + ' - EOG061 - All bad components',
                                       section=nip)   
    
            # Save HTML report                   
            report.save(os.getcwd() + '/4_rejectArtifactsManually.html', overwrite=True, 
                        open_browser=False)


        #######################################################################
        ######################### Check EOG062 artifacts ######################
        #######################################################################
        # Choose good channels (ch_types + EOG062)
        picks_eog2 = np.concatenate([picks, np.array([eog_channels[1]])]) # 383 = EOG062
        # Create eog-based epochs 
        eog_epochs = create_eog_epochs(raw, reject=reject, picks=picks_eog2, 
                                       baseline = (None, 0), ch_name='EOG062')
        eog_average = eog_epochs.average()
        
        if len(eog_epochs) > 0:
            for bad_component in ica_rejects[nip][ch_type]['eog2']:
                # Plots                            
            ecg_inds = returnBadICA()[nip]['meg']['ecg']

            # Plot source time course
            report.add_figs_to_section(ica.plot_overlay(raw,
                                                        exclude=ecg_inds,
                                                        start=20000,
                                                        stop=25000),
                                       captions=ch_type.upper() + ' - ECG - ' +
                                       'Corrections',
                                       section=nip)

        # Reject bad ICA
        raw = ica.apply(raw, exclude=(ecg_inds))
        # Save HTML report
        report.save(os.getcwd() + '/3_rejectArtifactsAutomatically.html',
                    overwrite=True,
                    open_browser=False)

        #######################################################################
        ########################## Reject EOG artifacts #######################
        #######################################################################
        if 'EOG062' in raw.ch_names:
            # Choose good channels (ch_types + eog)
            picks_eog = np.concatenate([
                picks,
                mne.pick_types(raw.info,
                               meg=False,
                               eeg=False,
                               ecg=False,
                               eog=True)
            ])
Exemple #50
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)
    group_evoked_dict_aud['Auditory'].append(auditory_evoked_dict['Auditory'])
    group_evoked_dict_vis['Causality'].append(visual_evoked_dict['Causality'])
    group_evoked_dict_vis['Temporal'].append(visual_evoked_dict['Temporal'])
    group_evoked_dict_vis['Visual'].append(visual_evoked_dict['Visual']) 

    # Plot to compare different conditions
    colors = dict(Causality="b", Temporal="r", Auditory="g")
    comparativePlot(auditory_evoked_dict, section=nip, colors=colors,
                    captions='Multisensory/Unisensory - Auditory ERF')
    colors = dict(Causality="b", Temporal="r", Visual="m")
    comparativePlot(visual_evoked_dict, section=nip, colors=colors, 
                    captions='Multisensory/Unisensory - Visual ERF')

                     
    # Save HTML report                   
    report.save(os.getcwd() + '/1_mutlisensoryUnisensoryERF.html', overwrite=True, 
                open_browser=False)



###############################################################################
############################### GROUP ANALYSIS ################################
###############################################################################

# Open html report 
report = Report('Mutlisensory vs Unisensory evoked response - group', verbose=False)
'''
blocs = ['Causality', 'Temporal', 'Auditory']
    
times = np.arange(-0.1, 1., 0.1)
    
for bloc in blocs:
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")
        rep_group.add_figs_to_section(fig, '%s: Evoked, Before and After PCA'
                                      % subject, 'Evoked, Before and After')

        # 2. plot PCA topos
        p = mne.viz.plot_projs_topomap(projs, evoked.info, show=False,
                                       extrapolate='head')
        rep_group.add_figs_to_section(p, '%s: PCA topos' % subject,
                                      'Topos')

        # 3. plot evoked - each proj
        for ii, ev in enumerate(evokeds):
            exp_var = ev.info['projs'][0]['explained_var'] * 100
            title = 'PC %d: %2.2f%% Explained Variance' % (ii, exp_var)
            tab = 'PC %d' % ii
            fig = plt.figure(figsize=(12, 6))
            gs = gridspec.GridSpec(1, 2, width_ratios=[3, 1])
            axes = [plt.subplot(gs[0]), plt.subplot(gs[1])]
            e = ev.plot(titles={'mag': title},
                        show=False, axes=axes[0])
            p = mne.viz.plot_projs_topomap(ev.info['projs'], ev.info,
                                           show=False, axes=axes[1],
                                           extrapolate='head')
            rep_group.add_figs_to_section(fig, '%s: Evoked w/o PC %d'
                                          % (subject, ii), tab)

        # save projs
        mne.write_proj(fname_proj, projs)
        # cleanup
        del epochs
    rep_group.save(fname_rep_group, open_browser=False, overwrite=True)
Exemple #54
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')
                                    reject_tmax=rej_tmax) # Check rejection settings
                    ica_check_evoked = \
                                epochs[ica_check_eves[trial_type]].average()

                    fig = ica.plot_overlay(ica_check_evoked, exclude=ica_excludes)  # plot EOG cleaning
                    #fig.savefig(ica_check_img_folder + '/' +trial_type + session_no + '-savgol.png')
                    report.add_figs_to_section(fig, trial_type + session_no,
                            section=filter_string, scale=None, image_format='png')
                    plt.close(fig)

                    ica.exclude = ica_excludes

                    ica.apply(epochs, copy=False)

                    print('Resampling epochs...')
                    epochs.resample(epoch_params['rsl'],
                                    n_jobs=1, verbose=False)
                                    # Trust the defaults here

                    epochs.save(opj(epochs_folder,
                                    trial_type + session_no + '-epo.fif'))

                # Try if deleting the raw object helps here!
                # not sure it was these or the addition of
                # OMP_NUM_THREADS=1 to the invokation that did the trick...
                del raw
                del ica
                del epochs

    report.save(fname=report_folder + '/' + subj + '.html', open_browser = False, overwrite = CLOBBER)