Exemple #1
0
def test_ctf(_bids_validate):
    """Test functionality of the write_raw_bids conversion for CTF data."""
    output_path = _TempDir()
    data_path = op.join(testing.data_path(download=False), 'CTF')
    raw_fname = op.join(data_path, 'testdata_ctf.ds')

    raw = mne.io.read_raw_ctf(raw_fname)
    with pytest.warns(UserWarning, match='No line frequency'):
        write_raw_bids(raw, bids_basename, output_path=output_path)

    _bids_validate(output_path)
    with pytest.warns(UserWarning, match='Did not find any events'):
        raw = read_raw_bids(bids_basename + '_meg.ds',
                            output_path,
                            extra_params=dict(clean_names=False))

    # test to check that running again with overwrite == False raises an error
    with pytest.raises(FileExistsError, match="already exists"):  # noqa: F821
        write_raw_bids(raw, bids_basename, output_path=output_path)

    assert op.exists(op.join(output_path, 'participants.tsv'))

    # test anonymize
    raw = mne.io.read_raw_ctf(raw_fname)
    with pytest.warns(UserWarning,
                      match='Converting to FIF for anonymization'):
        output_path = _test_anonymize(raw, bids_basename)
    _bids_validate(output_path)

    # XXX: change the next two lines once raw.set_meas_date() is
    # available.
    raw.info['meas_date'] = None
    raw.anonymize()
    with pytest.raises(ValueError, match='All measurement dates are None'):
        get_anonymization_daysback(raw)
Exemple #2
0
def test_bdf(_bids_validate):
    """Test write_raw_bids conversion for Biosemi data."""
    output_path = _TempDir()
    data_path = op.join(base_path, 'edf', 'tests', 'data')
    raw_fname = op.join(data_path, 'test.bdf')

    raw = mne.io.read_raw_bdf(raw_fname)
    with pytest.warns(UserWarning, match='No line frequency found'):
        write_raw_bids(raw, bids_basename, output_path, overwrite=False)
    _bids_validate(output_path)

    # Test also the reading of channel types from channels.tsv
    # the first channel in the raw data is not MISC right now
    test_ch_idx = 0
    assert coil_type(raw.info, test_ch_idx) != 'misc'

    # we will change the channel type to MISC and overwrite the channels file
    bids_fname = bids_basename + '_eeg.bdf'
    channels_fname = _find_matching_sidecar(bids_fname, output_path,
                                            'channels.tsv')
    channels_dict = _from_tsv(channels_fname)
    channels_dict['type'][test_ch_idx] = 'MISC'
    _to_tsv(channels_dict, channels_fname)

    # Now read the raw data back from BIDS, with the tampered TSV, to show
    # that the channels.tsv truly influences how read_raw_bids sets ch_types
    # in the raw data object
    raw = read_raw_bids(bids_fname, output_path)
    assert coil_type(raw.info, test_ch_idx) == 'misc'

    # Test cropped assertion error
    raw = mne.io.read_raw_bdf(raw_fname)
    raw.crop(0, raw.times[-2])
    with pytest.raises(AssertionError, match='cropped'):
        write_raw_bids(raw, bids_basename, output_path)
Exemple #3
0
def return_bids_test_dir(tmpdir_factory):
    """Return path to a written test BIDS dir."""
    bids_root = str(tmpdir_factory.mktemp('mnebids_utils_test_bids_ds'))
    data_path = testing.data_path()
    raw_fname = op.join(data_path, 'MEG', 'sample',
                        'sample_audvis_trunc_raw.fif')

    event_id = {
        'Auditory/Left': 1,
        'Auditory/Right': 2,
        'Visual/Left': 3,
        'Visual/Right': 4,
        'Smiley': 5,
        'Button': 32
    }
    events_fname = op.join(data_path, 'MEG', 'sample',
                           'sample_audvis_trunc_raw-eve.fif')

    raw = mne.io.read_raw_fif(raw_fname)
    # Write multiple runs for test_purposes
    bids_basename2 = bids_basename.replace('run-{}'.format(run), 'run-02')
    for name in [
            bids_basename,
            bids_basename2,
    ]:
        with pytest.warns(UserWarning, match='No line frequency'):
            write_raw_bids(raw,
                           name,
                           bids_root,
                           events_data=events_fname,
                           event_id=event_id,
                           overwrite=True)

    return bids_root
Exemple #4
0
def test_find_emptyroom_ties(tmpdir):
    """Test that we receive a warning on a date tie."""
    data_path = testing.data_path()
    raw_fname = op.join(data_path, 'MEG', 'sample',
                        'sample_audvis_trunc_raw.fif')

    bids_root = str(tmpdir)
    bids_path.update(root=bids_root)
    session = '20010101'
    er_dir_path = BIDSPath(subject='emptyroom', session=session,
                           datatype='meg', root=bids_root)
    er_dir = er_dir_path.mkdir().directory

    meas_date = (datetime
                 .strptime(session, '%Y%m%d')
                 .replace(tzinfo=timezone.utc))

    raw = _read_raw_fif(raw_fname)

    er_raw_fname = op.join(data_path, 'MEG', 'sample', 'ernoise_raw.fif')
    raw.copy().crop(0, 10).save(er_raw_fname, overwrite=True)
    er_raw = _read_raw_fif(er_raw_fname)
    raw.set_meas_date(meas_date)
    er_raw.set_meas_date(meas_date)

    write_raw_bids(raw, bids_path, overwrite=True)
    er_bids_path = BIDSPath(subject='emptyroom', session=session)
    er_basename_1 = er_bids_path.basename
    er_basename_2 = BIDSPath(subject='emptyroom', session=session,
                             task='noise').basename
    er_raw.save(op.join(er_dir, f'{er_basename_1}_meg.fif'))
    er_raw.save(op.join(er_dir, f'{er_basename_2}_meg.fif'))

    with pytest.warns(RuntimeWarning, match='Found more than one'):
        bids_path.find_empty_room()
Exemple #5
0
def return_bids_test_dir(tmpdir_factory):
    """Return path to a written test BIDS dir."""
    bids_root = str(tmpdir_factory.mktemp('mnebids_utils_test_bids_ds'))
    data_path = testing.data_path()
    raw_fname = op.join(data_path, 'MEG', 'sample',
                        'sample_audvis_trunc_raw.fif')

    event_id = {
        'Auditory/Left': 1,
        'Auditory/Right': 2,
        'Visual/Left': 3,
        'Visual/Right': 4,
        'Smiley': 5,
        'Button': 32
    }
    events_fname = op.join(data_path, 'MEG', 'sample',
                           'sample_audvis_trunc_raw-eve.fif')
    cal_fname = op.join(data_path, 'SSS', 'sss_cal_mgh.dat')
    crosstalk_fname = op.join(data_path, 'SSS', 'ct_sparse.fif')

    raw = mne.io.read_raw_fif(raw_fname)
    raw.info['line_freq'] = 60
    bids_path.update(root=bids_root)
    # Write multiple runs for test_purposes
    for run_idx in [run, '02']:
        name = bids_path.copy().update(run=run_idx)
        write_raw_bids(raw,
                       name,
                       events_data=events_fname,
                       event_id=event_id,
                       overwrite=True)

    write_meg_calibration(cal_fname, bids_path=bids_path)
    write_meg_crosstalk(crosstalk_fname, bids_path=bids_path)
    return bids_root
Exemple #6
0
def test_ctf():
    """Test functionality of the write_raw_bids conversion for CTF data."""
    output_path = _TempDir()
    data_path = op.join(testing.data_path(download=False), 'CTF')
    raw_fname = op.join(data_path, 'testdata_ctf.ds')

    raw = mne.io.read_raw_ctf(raw_fname)
    folder = write_raw_bids(raw, bids_basename, output_path=output_path)

    # XXX: hack to be removed once the empty file issue is solved in validator
    # https://github.com/bids-standard/bids-validator/issues/651
    for root, dirs, files in os.walk(folder):
        for file in files:
            fpath = op.join(root, file)
            if os.stat(fpath).st_size == 0:
                with open(fpath, 'w') as f:
                    f.write('***Empty-File-Filler***')

    cmd = ['bids-validator', output_path]
    run_subprocess(cmd, shell=shell)

    # test to check that running again with overwrite == False raises an error
    with pytest.raises(FileExistsError, match="already exists"):
        write_raw_bids(raw, bids_basename, output_path=output_path)

    assert op.exists(op.join(output_path, 'participants.tsv'))
Exemple #7
0
def test_inspect(tmp_path):
    """Test mne_bids inspect."""

    # Check that help is printed
    check_usage(mne_bids_inspect)

    # Create test dataset.
    bids_root = str(tmp_path)
    data_path = testing.data_path()
    subject = '01'
    task = 'test'
    datatype = 'meg'
    raw_fname = op.join(data_path, 'MEG', 'sample',
                        'sample_audvis_trunc_raw.fif')

    raw = mne.io.read_raw(raw_fname)
    raw.info['line_freq'] = 60.

    bids_path = BIDSPath(subject=subject, task=task, datatype=datatype,
                         root=bids_root)
    write_raw_bids(raw, bids_path, overwrite=True, verbose=False)

    import matplotlib
    matplotlib.use('agg')

    h_freqs = (30.0, 30, '30')
    for h_freq in h_freqs:
        args = ('--bids_root', bids_root, '--h_freq', h_freq,
                '--find_flat', 0)
        with ArgvSetter(args):
            mne_bids_inspect.run()
Exemple #8
0
def test_bti(_bids_validate):
    """Test functionality of the write_raw_bids conversion for BTi data."""
    output_path = _TempDir()
    data_path = op.join(base_path, 'bti', 'tests', 'data')
    raw_fname = op.join(data_path, 'test_pdf_linux')
    config_fname = op.join(data_path, 'test_config_linux')
    headshape_fname = op.join(data_path, 'test_hs_linux')

    raw = mne.io.read_raw_bti(raw_fname,
                              config_fname=config_fname,
                              head_shape_fname=headshape_fname)

    write_raw_bids(raw, bids_basename, output_path, verbose=True)

    assert op.exists(op.join(output_path, 'participants.tsv'))
    _bids_validate(output_path)

    raw = read_raw_bids(bids_basename + '_meg', output_path)

    with pytest.raises(TypeError, match="unexpected keyword argument 'foo'"):
        read_raw_bids(bids_basename + '_meg',
                      output_path,
                      extra_params=dict(foo='bar'))

    # test anonymize
    raw = mne.io.read_raw_bti(raw_fname,
                              config_fname=config_fname,
                              head_shape_fname=headshape_fname)
    with pytest.warns(UserWarning,
                      match='Converting to FIF for anonymization'):
        output_path = _test_anonymize(raw, bids_basename)
    _bids_validate(output_path)
Exemple #9
0
def test_count_events(tmpdir):
    """Test mne_bids count_events."""

    # Check that help is printed
    check_usage(mne_bids_count_events)

    # Create test dataset.
    output_path = str(tmpdir)
    data_path = testing.data_path()
    raw_fname = op.join(data_path, 'MEG', 'sample',
                        'sample_audvis_trunc_raw.fif')

    raw = mne.io.read_raw(raw_fname)
    raw.info['line_freq'] = 60.
    events = mne.find_events(raw)
    event_id = {'auditory/left': 1, 'auditory/right': 2, 'visual/left': 3,
                'visual/right': 4, 'face': 5, 'button': 32}

    bids_path = BIDSPath(subject='01', root=output_path)
    write_raw_bids(raw, bids_path, events, event_id, overwrite=True,
                   verbose=False)

    with ArgvSetter(('--bids_root', output_path)):
        mne_bids_count_events.run()

    with ArgvSetter(('--bids_root', output_path, '--describe')):
        mne_bids_count_events.run()
Exemple #10
0
def test_find_emptyroom_no_meas_date(tmpdir):
    """Test that we warn if measurement date can be read or inferred."""
    data_path = testing.data_path()
    raw_fname = op.join(data_path, 'MEG', 'sample',
                        'sample_audvis_trunc_raw.fif')

    bids_root = str(tmpdir)
    bids_path.update(root=bids_root)
    er_session = 'mysession'
    er_meas_date = None

    er_dir_path = BIDSPath(subject='emptyroom', session=er_session,
                           datatype='meg', root=bids_root)
    er_dir = er_dir_path.mkdir().directory

    er_bids_path = BIDSPath(subject='emptyroom', session=er_session,
                            task='noise', check=False)
    er_basename = er_bids_path.basename
    raw = _read_raw_fif(raw_fname)

    er_raw_fname = op.join(data_path, 'MEG', 'sample', 'ernoise_raw.fif')
    raw.copy().crop(0, 10).save(er_raw_fname, overwrite=True)
    er_raw = _read_raw_fif(er_raw_fname)
    er_raw.set_meas_date(er_meas_date)
    er_raw.save(op.join(er_dir, f'{er_basename}_meg.fif'), overwrite=True)

    # Write raw file data using mne-bids, and remove participants.tsv
    # as it's incomplete (doesn't contain the emptyroom subject we wrote
    # manually using MNE's Raw.save() above)
    raw = _read_raw_fif(raw_fname)
    write_raw_bids(raw, bids_path, overwrite=True)
    os.remove(op.join(bids_root, 'participants.tsv'))

    with pytest.warns(RuntimeWarning, match='Could not retrieve .* date'):
        bids_path.find_empty_room()
Exemple #11
0
def _make_dataset(subjects, tasks=(None, ), runs=(None, ), sessions=(None, )):
    data_path = testing.data_path()
    raw_fname = \
        Path(data_path) / 'MEG' / 'sample' / 'sample_audvis_trunc_raw.fif'
    raw = mne.io.read_raw(raw_fname)
    raw.info['line_freq'] = 60.
    root = _TempDir()
    events = mne.find_events(raw)
    event_id = {
        'auditory/left': 1,
        'auditory/right': 2,
        'visual/left': 3,
        'visual/right': 4,
        'face': 5,
        'button': 32
    }

    for subject, session, task, run in \
            itertools.product(subjects, sessions, tasks, runs):
        bids_path = BIDSPath(
            subject=subject,
            session=session,
            run=run,
            task=task,
            root=root,
        )
        write_raw_bids(raw,
                       bids_path,
                       events,
                       event_id,
                       overwrite=True,
                       verbose=False)

    return root, events, event_id
Exemple #12
0
def test_vhdr(_bids_validate):
    """Test write_raw_bids conversion for BrainVision data."""
    output_path = _TempDir()
    data_path = op.join(base_path, 'brainvision', 'tests', 'data')
    raw_fname = op.join(data_path, 'test.vhdr')

    raw = mne.io.read_raw_brainvision(raw_fname)

    # inject a bad channel
    assert not raw.info['bads']
    injected_bad = ['FP1']
    raw.info['bads'] = injected_bad

    # write with injected bad channels
    write_raw_bids(raw, bids_basename_minimal, output_path, overwrite=False)
    _bids_validate(output_path)

    # read and also get the bad channels
    raw = read_raw_bids(bids_basename_minimal + '_eeg.vhdr', output_path)

    with pytest.raises(TypeError, match="unexpected keyword argument 'foo'"):
        read_raw_bids(bids_basename_minimal + '_eeg.vhdr', output_path,
                      extra_params=dict(foo='bar'))

    # Check that injected bad channel shows up in raw after reading
    np.testing.assert_array_equal(np.asarray(raw.info['bads']),
                                  np.asarray(injected_bad))

    # Test that correct channel units are written ... and that bad channel
    # is in channels.tsv
    channels_tsv_name = op.join(output_path, 'sub-{}'.format(subject_id),
                                'eeg', bids_basename_minimal + '_channels.tsv')
    data = _from_tsv(channels_tsv_name)
    assert data['units'][data['name'].index('FP1')] == 'µV'
    assert data['units'][data['name'].index('CP5')] == 'n/a'
    assert data['status'][data['name'].index(injected_bad[0])] == 'bad'

    # check events.tsv is written
    events_tsv_fname = channels_tsv_name.replace('channels', 'events')
    assert op.exists(events_tsv_fname)

    # create another bids folder with the overwrite command and check
    # no files are in the folder
    data_path = make_bids_folders(subject=subject_id, kind='eeg',
                                  output_path=output_path, overwrite=True)
    assert len([f for f in os.listdir(data_path) if op.isfile(f)]) == 0

    # test anonymize and convert
    raw = mne.io.read_raw_brainvision(raw_fname)
    _test_anonymize(raw, bids_basename)

    # Also cover iEEG
    # We use the same data and pretend that eeg channels are ecog
    raw = mne.io.read_raw_brainvision(raw_fname)
    raw.set_channel_types({raw.ch_names[i]: 'ecog'
                           for i in mne.pick_types(raw.info, eeg=True)})
    output_path = _TempDir()
    write_raw_bids(raw, bids_basename, output_path, overwrite=False)
    _bids_validate(output_path)
Exemple #13
0
def save_bids_data(raw, subject, bidsdir, figdir, ctfile, fcfile, behav_dir):
    """
    Saves BIDS-structured data subject-wise
    :param raw: raw fif data
    :param subject: str, subject identifier
    :param bidsdir: str, path to where a bids-conform output dir shall exist
    :param figdir: str, path to diagnostic plot directory
    :param ctfile: str, path to crosstalk file
    :param fcfile: str, path to fine cal file
    :return:
    """

    bids_path = _get_BIDSPath(subject, bidsdir)
    logging.info(f"Saving BIDS-compliant raw data from subject "
                 f"{subject} into "
                 f"{bids_path}")
    # a workaround of https://github.com/mne-tools/mne-bids/issues/718
    if subject in ["001", "005"]:
        # these subjects can't be written directly - mne-bids would perform an
        # internal check that fails on the basis that the hand-concatenated
        # files are longer than the single split file that would be read-in
        # automatically.
        from tempfile import NamedTemporaryFile

        with NamedTemporaryFile(suffix="_raw.fif", delete=False) as f:
            fname = f.name
            raw.save(fname, overwrite=True)
            raw = mne.io.read_raw_fif(fname, preload=False)

    # save raw fif data and events
    # get log files, but don't yet write them out as the path doesn't exist yet
    df = write_to_df(
        participant=subject,
        behav_dir=behav_dir,
        bids_dir=bids_path.directory,
        write_out=False,
    )
    events_data, event_dict = _events(raw, subject, figdir, df)
    write_raw_bids(raw,
                   bids_path,
                   events_data=events_data,
                   event_id=event_dict,
                   overwrite=True)

    # save crosstalk and calibration file
    _elektas_extras(crosstalk_file=ctfile,
                    fine_cal_file=fcfile,
                    bids_path=bids_path)
    # write out log files
    df = write_to_df(
        participant=subject,
        behav_dir=behav_dir,
        bids_dir=bids_path.directory,
        write_out=True,
    )
Exemple #14
0
def mne_write_bids(filename, subject, bids_folder='/bids', task='rest'):
    raw = mne.io.read_raw_brainvision(str(pathlib.Path(filename)))
    print(raw.annotations)
    raw.annotations.duration[raw.annotations.description ==
                             'DC Correction/'] = 6
    events, event_id = mne.events_from_annotations(
        raw)  # this needs to be checked
    write_raw_bids(raw,
                   make_bids_basename(subject=subject, task=task),
                   bids_folder,
                   overwrite=True)
Exemple #15
0
def test_kit():
    """Test functionality of the write_raw_bids conversion for KIT data."""
    output_path = _TempDir()
    data_path = op.join(base_path, 'kit', 'tests', 'data')
    raw_fname = op.join(data_path, 'test.sqd')
    events_fname = op.join(data_path, 'test-eve.txt')
    hpi_fname = op.join(data_path, 'test_mrk.sqd')
    electrode_fname = op.join(data_path, 'test_elp.txt')
    headshape_fname = op.join(data_path, 'test_hsp.txt')
    event_id = dict(cond=1)

    raw = mne.io.read_raw_kit(raw_fname,
                              mrk=hpi_fname,
                              elp=electrode_fname,
                              hsp=headshape_fname)
    write_raw_bids(raw,
                   bids_basename,
                   output_path,
                   events_data=events_fname,
                   event_id=event_id,
                   overwrite=False)
    cmd = ['bids-validator', output_path]
    run_subprocess(cmd, shell=shell)
    assert op.exists(op.join(output_path, 'participants.tsv'))

    # ensure the channels file has no STI 014 channel:
    channels_tsv = make_bids_basename(subject=subject_id,
                                      session=session_id,
                                      task=task,
                                      run=run,
                                      suffix='channels.tsv',
                                      acquisition=acq,
                                      prefix=op.join(output_path,
                                                     'sub-01/ses-01/meg'))
    df = pd.read_csv(channels_tsv, sep='\t')
    assert not ('STI 014' in df['name'].values)

    # ensure the marker file is produced in the right place
    raw_folder = make_bids_basename(subject=subject_id,
                                    session=session_id,
                                    task=task,
                                    run=run,
                                    acquisition=acq,
                                    suffix='%s' % 'meg')
    marker_fname = make_bids_basename(subject=subject_id,
                                      session=session_id,
                                      task=task,
                                      run=run,
                                      acquisition=acq,
                                      suffix='markers.sqd',
                                      prefix=os.path.join(
                                          output_path, 'sub-01/ses-01/meg',
                                          raw_folder))
    assert op.exists(marker_fname)
Exemple #16
0
def fif_to_bids(path: Path):
    raw = mne.io.read_raw_fif(path)

    bids_path = mne_bids.BIDSPath(
        subject="01",
        session="01",
        task="testing",
        acquisition="01",
        run="01",
        root=data_dir / "BIDS",
    )
    mne_bids.write_raw_bids(raw, bids_path, overwrite=True)
Exemple #17
0
def _test_anonymize(raw, bids_basename, events_fname=None, event_id=None):
    bids_root = _TempDir()
    write_raw_bids(raw, bids_basename, bids_root,
                   events_data=events_fname,
                   event_id=event_id, anonymize=dict(daysback=33000),
                   overwrite=False)
    scans_tsv = make_bids_basename(
        subject=subject_id, session=session_id, suffix='scans.tsv',
        prefix=op.join(bids_root, 'sub-01', 'ses-01'))
    data = _from_tsv(scans_tsv)
    if data['acq_time'] is not None and data['acq_time'][0] != 'n/a':
        assert datetime.strptime(data['acq_time'][0],
                                 '%Y-%m-%dT%H:%M:%S').year < 1925
    return bids_root
Exemple #18
0
def test_bdf():
    """Test write_raw_bids conversion for Biosemi data."""
    output_path = _TempDir()
    data_path = op.join(base_path, 'edf', 'tests', 'data')
    raw_fname = op.join(data_path, 'test.bdf')

    raw = mne.io.read_raw_edf(raw_fname)
    write_raw_bids(raw, bids_basename, output_path, overwrite=False)

    cmd = ['bids-validator', '--bep006', output_path]
    run_subprocess(cmd, shell=shell)

    raw.crop(0, raw.times[-2])
    with pytest.raises(AssertionError, match='cropped'):
        write_raw_bids(raw, bids_basename, output_path)
Exemple #19
0
def test_set(_bids_validate):
    """Test write_raw_bids conversion for EEGLAB data."""
    # standalone .set file
    output_path = _TempDir()
    data_path = op.join(testing.data_path(), 'EEGLAB')

    # .set with associated .fdt
    output_path = _TempDir()
    data_path = op.join(testing.data_path(), 'EEGLAB')
    raw_fname = op.join(data_path, 'test_raw.set')

    raw = mne.io.read_raw_eeglab(raw_fname)

    # embedded - test mne-version assertion
    tmp_version = mne.__version__
    mne.__version__ = '0.16'
    with pytest.raises(ValueError, match='Your version of MNE is too old.'):
        write_raw_bids(raw, bids_basename, output_path)
    mne.__version__ = tmp_version

    # proceed with the actual test for EEGLAB data
    write_raw_bids(raw, bids_basename, output_path, overwrite=False)
    read_raw_bids(bids_basename + '_eeg.set', output_path)

    with pytest.raises(TypeError, match="unexpected keyword argument 'foo'"):
        read_raw_bids(bids_basename + '_eeg.set',
                      output_path,
                      extra_params=dict(foo='bar'))

    with pytest.raises(FileExistsError, match="already exists"):  # noqa: F821
        write_raw_bids(raw,
                       bids_basename,
                       output_path=output_path,
                       overwrite=False)
    _bids_validate(output_path)

    # check events.tsv is written
    # XXX: only from 0.18 onwards because events_from_annotations
    # is broken for earlier versions
    events_tsv_fname = op.join(output_path, 'sub-' + subject_id,
                               'ses-' + session_id, 'eeg',
                               bids_basename + '_events.tsv')
    if check_version('mne', '0.18'):
        assert op.exists(events_tsv_fname)

    # Also cover iEEG
    # We use the same data and pretend that eeg channels are ecog
    raw.set_channel_types(
        {raw.ch_names[i]: 'ecog'
         for i in mne.pick_types(raw.info, eeg=True)})
    output_path = _TempDir()
    write_raw_bids(raw, bids_basename, output_path)
    _bids_validate(output_path)

    # test anonymize and convert
    if check_version('mne', '0.20') and check_version('pybv', '0.2.0'):
        output_path = _test_anonymize(raw, bids_basename)
        _bids_validate(output_path)
Exemple #20
0
    def _create_bidsrun(self, data_fpath):
        """
        Create a BIDS Run if it does not exist.

        Parameters
        ----------
        data_fpath : str
            A filepath to the dataset, and it will create a BIDS-like run using MNE_Bids.

        """
        if pathlib.Path(data_fpath).suffix == ".fif":
            raw = mne.io.read_raw_fif(data_fpath)
        elif pathlib.Path(data_fpath).suffix == ".edf":
            raw = mne.io.read_raw_edf(data_fpath)
        else:
            raise RuntimeError(
                "Can't automatically create bids run using this extension. "
            )
        bids_basename = self.loader.datafile_fpath
        bids_dir = self.bids_root

        # copy over bids run to where it should be within the bids directory
        output_path = mne_bids.write_raw_bids(
            raw, bids_basename, output_path=bids_dir, overwrite=True, verbose=False
        )
        return output_path
Exemple #21
0
def test_inspect_multiple_files(return_bids_test_dir):
    import matplotlib
    matplotlib.use('Agg')

    bids_path = _bids_path.copy().update(root=return_bids_test_dir)

    # Create a second subject
    raw = read_raw_bids(bids_path=bids_path, verbose='error')
    write_raw_bids(raw, bids_path.copy().update(subject='02'))
    del raw

    # Inspection should end with the second subject.
    inspect_dataset(bids_path.copy().update(subject=None))
    raw_fig = mne_bids.inspect._global_vars['raw_fig']
    assert raw_fig.mne.info['subject_info']['participant_id'] == 'sub-02'
    raw_fig.canvas.key_press_event(raw_fig.mne.close_key)
Exemple #22
0
def test_count_no_events_file(tmpdir):
    """Test count_events with no event present."""
    data_path = testing.data_path()
    raw_fname = \
        Path(data_path) / 'MEG' / 'sample' / 'sample_audvis_trunc_raw.fif'
    raw = mne.io.read_raw(raw_fname)
    raw.info['line_freq'] = 60.
    root = str(tmpdir)

    bids_path = BIDSPath(
        subject='01', task='task1', root=root,
    )
    write_raw_bids(raw, bids_path, overwrite=True, verbose=False)

    with pytest.raises(ValueError, match='No events files found.'):
        count_events(root)
Exemple #23
0
def test_bti():
    """Test functionality of the write_raw_bids conversion for BTi data."""
    output_path = _TempDir()
    data_path = op.join(base_path, 'bti', 'tests', 'data')
    raw_fname = op.join(data_path, 'test_pdf_linux')
    config_fname = op.join(data_path, 'test_config_linux')
    headshape_fname = op.join(data_path, 'test_hs_linux')

    raw = mne.io.read_raw_bti(raw_fname,
                              config_fname=config_fname,
                              head_shape_fname=headshape_fname)
    write_raw_bids(raw, bids_basename, output_path, verbose=True)

    assert op.exists(op.join(output_path, 'participants.tsv'))

    cmd = ['bids-validator', output_path]
    run_subprocess(cmd, shell=shell)
def write_BIDS():
    # write Brainvision format
    l_session_path = get_used_sessions()
    for patient_idx, patient_l in enumerate(l_session_path):

        for sess_idx, sess_path in enumerate(patient_l):
            data, channel_names = read_raw_session(
                l_session_path[patient_idx][sess_idx])
            pybv.write_brainvision(data,
                                   sfreq=1000,
                                   ch_names=channel_names,
                                   fname_base=str(sess_idx),
                                   folder_out='BrainVision',
                                   events=None,
                                   resolution=1e-7,
                                   scale_data=True,
                                   fmt='binary_float32',
                                   meas_date=None)

            # BIDS Filename
            if patient_idx < 10:
                subject_id = str('00') + str(patient_idx)
            else:
                subject_id = str('0') + str(patient_idx)

            raw = mne.io.read_raw_brainvision('BrainVision/' + str(sess_idx) +
                                              '.vhdr')
            run_ = sess_idx
            print(str(subject_id))

            if 'LEFT' in channel_names[0]:
                laterality = 'left'
            else:
                laterality = 'right'

            bids_basename = make_bids_basename(subject=str(subject_id),
                                               session=laterality,
                                               task='force',
                                               run=str(run_))
            # BIDS schreiben
            output_path = '/Users/hi/Documents/workshop_ML/thesis_plots/BIDS_new/'
            write_raw_bids(raw,
                           bids_basename,
                           output_path,
                           event_id=0,
                           overwrite=True)
Exemple #25
0
def BrainSenseTimeDomain_to_bids(filename,
                                 subject,
                                 bids_folder='/bids',
                                 task='BrainSenseTimeDomain'):
    data = read_file(filename)
    opath, fname, ext = tb.fileparts(filename)
    if subject.find('-') > 0:
        subject = subject[subject.find('-') + 1:]
    session = data['SessionDate'][:-1].replace('-', '').replace(':', '')
    basename = make_bids_basename(subject=subject, task=task, session=session)
    bpath = make_bids_folders(subject=subject, session=session, make_dir=False)
    sourcename = make_bids_basename(subject=subject, session=session)
    raw = import_BrainSenseTimeDomain(filename)
    if raw is not None:
        rfig = raw.plot(color='k')
        rfig.savefig(
            pathlib.Path(bids_folder, bpath, 'eeg', 'sourcedata', 'figures',
                         basename + '.png'))
        if os.path.exists('tmp.edf'):
            os.remove('tmp.edf')
        ephys.mne_write_edf(raw, 'tmp.edf')
        raw = mne.io.read_raw_edf('tmp.edf')
        write_raw_bids(raw,
                       bids_basename=basename,
                       output_path=bids_folder,
                       overwrite=True)
        if not os.path.isdir(
                pathlib.Path(bids_folder, bpath, 'eeg', 'sourcedata')):
            os.makedirs(pathlib.Path(bids_folder, bpath, 'eeg', 'sourcedata'))
        shutil.copyfile(
            filename,
            pathlib.Path(bids_folder, bpath, 'eeg', 'sourcedata',
                         basename + ext))
        plot_wavelet_spectra(pathlib.Path(bids_folder, bpath, 'eeg',
                                          'sourcedata', basename + ext),
                             typefield=task)
        plot_BrainSenseLfp(
            pathlib.Path(bids_folder, bpath, 'eeg', 'sourcedata',
                         basename + ext))
        os.remove('tmp.edf')
        shutil.move(
            pathlib.Path(bids_folder, bpath, 'eeg', 'sourcedata',
                         basename + ext),
            pathlib.Path(bids_folder, bpath, 'eeg', 'sourcedata',
                         sourcename + ext))
Exemple #26
0
def test_bti(_bids_validate):
    """Test functionality of the write_raw_bids conversion for BTi data."""
    output_path = _TempDir()
    data_path = op.join(base_path, 'bti', 'tests', 'data')
    raw_fname = op.join(data_path, 'test_pdf_linux')
    config_fname = op.join(data_path, 'test_config_linux')
    headshape_fname = op.join(data_path, 'test_hs_linux')

    raw = mne.io.read_raw_bti(raw_fname,
                              config_fname=config_fname,
                              head_shape_fname=headshape_fname)

    write_raw_bids(raw, bids_basename, output_path, verbose=True)

    assert op.exists(op.join(output_path, 'participants.tsv'))
    _bids_validate(output_path)

    raw = read_raw_bids(bids_basename + '_meg', output_path)
Exemple #27
0
def test_ctf(_bids_validate):
    """Test functionality of the write_raw_bids conversion for CTF data."""
    output_path = _TempDir()
    data_path = op.join(testing.data_path(download=False), 'CTF')
    raw_fname = op.join(data_path, 'testdata_ctf.ds')

    raw = mne.io.read_raw_ctf(raw_fname)
    with pytest.warns(UserWarning, match='No line frequency'):
        write_raw_bids(raw, bids_basename, output_path=output_path)

    _bids_validate(output_path)
    with pytest.warns(UserWarning, match='Did not find any events'):
        raw = read_raw_bids(bids_basename + '_meg.ds', output_path)

    # test to check that running again with overwrite == False raises an error
    with pytest.raises(FileExistsError, match="already exists"):  # noqa: F821
        write_raw_bids(raw, bids_basename, output_path=output_path)

    assert op.exists(op.join(output_path, 'participants.tsv'))
Exemple #28
0
def test_inspect_multiple_files(tmp_path):
    """Test inspecting a dataset consisting of more than one file."""
    import matplotlib
    import matplotlib.pyplot as plt
    matplotlib.use('Agg')
    plt.close('all')

    bids_root = setup_bids_test_dir(tmp_path)
    bids_path = _bids_path.copy().update(root=bids_root)

    # Create a second subject
    raw = read_raw_bids(bids_path=bids_path, verbose='error')
    write_raw_bids(raw, bids_path.copy().update(subject='02'))
    del raw

    # Inspection should end with the second subject.
    inspect_dataset(bids_path.copy().update(subject=None))
    raw_fig = mne_bids.inspect._global_vars['raw_fig']
    assert raw_fig.mne.info['subject_info']['participant_id'] == 'sub-02'
    raw_fig.canvas.key_press_event(raw_fig.mne.close_key)
def fieldtrip_to_BIDS(filename, BIDS_path, subject_id, session, task, run):
    """
    Write BIDS entry from a fieldtrip format
    The function has to temporarily save the output file as brainvision to set the filenames attribute in the mne RawArray 
    Args:
        filename (string): .mat fieldtrip name
        BIDS_path (string): BIDS_path
        subject_id (string): BIDS subject id
        session (string): BIDS session
        task (string): BIDS session
        run (string): BIDS session
    """

    raw = mne.io.read_raw_fieldtrip(filename, None)
    ieegdata = raw.get_data()
    info = mne.create_info(raw.ch_names, raw.info['sfreq'], ch_types='ecog')
    raw = mne.io.RawArray(ieegdata, info)

    bids_basename = mne_bids.make_bids_basename(subject=subject_id,
                                                session=session,
                                                task=task,
                                                run=run)

    pybv.write_brainvision(raw.get_data(), raw.info['sfreq'], raw.ch_names,
                           'dummy_write', os.getcwd())

    bv_raw = mne.io.read_raw_brainvision('dummy_write.vhdr')

    # set all channel types to ECOG for iEEG - BIDS does not allow more than one channel type
    mapping = {}
    for ch in range(len(bv_raw.info['ch_names'])):
        mapping[bv_raw.info['ch_names'][ch]] = 'ecog'
    bv_raw.set_channel_types(mapping)

    mne_bids.write_raw_bids(bv_raw, bids_basename, BIDS_path, overwrite=True)

    #  remove dummy file
    os.remove('dummy_write.vhdr')
    os.remove('dummy_write.eeg')
    os.remove('dummy_write.vmrk')
Exemple #30
0
def test_set():
    """Test write_raw_bids conversion for EEGLAB data."""
    # standalone .set file
    output_path = _TempDir()
    data_path = op.join(testing.data_path(), 'EEGLAB')

    # .set with associated .fdt
    output_path = _TempDir()
    data_path = op.join(testing.data_path(), 'EEGLAB')
    raw_fname = op.join(data_path, 'test_raw.set')

    raw = mne.io.read_raw_eeglab(raw_fname)
    write_raw_bids(raw, bids_basename, output_path, overwrite=False)

    with pytest.raises(FileExistsError, match="already exists"):
        write_raw_bids(raw,
                       bids_basename,
                       output_path=output_path,
                       overwrite=False)

    cmd = ['bids-validator', '--bep006', output_path]
    run_subprocess(cmd, shell=shell)

    # Also cover iEEG
    # We use the same data and pretend that eeg channels are ecog
    raw.set_channel_types(
        {raw.ch_names[i]: 'ecog'
         for i in mne.pick_types(raw.info, eeg=True)})
    output_path = _TempDir()
    write_raw_bids(raw, bids_basename, output_path)

    cmd = ['bids-validator', '--bep010', output_path]
    run_subprocess(cmd, shell=shell)
if elp_file is None:
    sys.stderr.write('No _Points.txt file found in raw directory')
    sys.exit(3)
if hsp_file is None:
    sys.stderr.write('No _HS.txt file found in raw directory')
    sys.exit(3)

stim_dir = os.path.join(bids_path, 'stimuli')

raws = mne_read_raw_kit(sqd_files, mrk_files[0], elp_file, hsp_file, subject=sub)

for ii in range(len(raws)):
    bname = os.path.split(raws[ii].filenames[0])[1];
    bname = '.'.join(bname.split('.')[:-1])
    bdata = {kk[0]:kk[1] for pp in bname.split('_') for kk in [pp.split('-')] if len(kk) == 2}
    mne_bids.write_raw_bids(raws[ii], bname, bids_path,
                            events_data=None, overwrite=True, verbose=False)

    # since mne_bids doesn't correctly write out our events data...
    pnm = mne_bids.make_bids_folders(subject=bdata['sub'], kind='meg', session=bdata['ses'],
                                     make_dir=False, output_path=bids_path)
    edata_in  = os.path.join(meta_path, bname + '.tsv')
    edata_out = os.path.join(pnm, bname + '_events.tsv')
    edata = pandas.read_csv(edata_in, sep='\t')
    ts = triggers(raws[ii])
    wh = np.where(ts > 0)[0]
    edata['onset'] = wh / 1000.0
    edata.to_csv(edata_out, sep='\t', index=False)

    # we need to make the stimulus directory also...
    if not os.path.isdir(stim_dir): os.makedirs(stim_dir)
    mdata_in = os.path.join(meta_path, bname + '.mat')