Ejemplo n.º 1
0
def test_print_dir_tree():
    """Test printing a dir tree."""
    with pytest.raises(ValueError):
        print_dir_tree('i_dont_exist')

    tmp_dir = _TempDir()
    assert print_dir_tree(tmp_dir) is None
Ejemplo n.º 2
0
 def print_dir_tree(self):
     """Print a directory tree for this Bids dataset."""
     basepath = os.path.join(self.bids_root)
     print_dir_tree(basepath)
Ejemplo n.º 3
0
# Download the data for subject 1, for the 2 minutes of eyes closed rest task.
# From the online documentation of the data we know that run "2" corresponds
# to the "eyes closed" task.
subject = 1
run = 2
eegbci.load_data(subject=subject, runs=run, update_path=True)

###############################################################################
# Let's see whether the data has been downloaded using a quick visualization
# of the directory tree.

# get MNE directory with example data
mne_data_dir = mne.get_config('MNE_DATASETS_EEGBCI_PATH')
data_dir = os.path.join(mne_data_dir, 'MNE-eegbci-data')

print_dir_tree(data_dir)

###############################################################################
# The data are in the `European Data Format <https://www.edfplus.info/>`_ with
# the ``.edf`` extension, which is good for us because next to the
# `BrainVision format`_, EDF is one of the recommended file formats for EEG
# data in BIDS format.
#
# However, apart from the data format, we need to build a directory structure
# and supply meta data files to properly *bidsify* this data.
#
# We will do exactly that in the next step.

###############################################################################
# Convert to BIDS
# ---------------
Ejemplo n.º 4
0
    12
]  # This is run #2 of imagining to close left or right fist

# Download the data for subjects 1 and 2
for subj_idx in [1, 2]:
    eegbci.load_data(subject=subj_idx,
                     runs=tasks,
                     path=mne_dir,
                     update_path=True)

###############################################################################
# Let's see whether the data has been downloaded using a quick visualization
# of the directory tree.

data_dir = os.path.join(mne_dir, 'MNE-eegbci-data')
print_dir_tree(data_dir)

###############################################################################
# The data are in the `European Data Format <https://www.edfplus.info/>`_
# '.edf', which is good for us because next to the BrainVision format, EDF is
# one of the recommended file formats for EEG BIDS. However, apart from the
# data format, we need to build a directory structure and supply meta data
# files to properly *bidsify* this data.

###############################################################################
# Step 2: Formatting as BIDS
# --------------------------
#
# Let's start by formatting a single subject. We are reading the data using
# MNE-Python's io module and the `read_raw_edf` function. Note that we must
# use `preload=False`, the default in MNE-Python. It prevents the data from
Ejemplo n.º 5
0
    raw.save(tmp_fpath)
    raw = mne.io.read_raw_fif(tmp_fpath)

    # write `raw` to BIDS and anonymize it into BrainVision format
    write_raw_bids(raw,
                   bids_basename,
                   bids_root=bids_root,
                   anonymize=dict(daysback=30000),
                   overwrite=True)

###############################################################################
# Step 3: Check and compare with standard
# ---------------------------------------

# Now we have written our BIDS directory.
print_dir_tree(bids_root)

###############################################################################
# MNE-BIDS has created a suitable directory structure for us, and among other
# meta data files, it started an `events.tsv` and `channels.tsv` and made an
# initial `dataset_description.json` on top!
#
# Now it's time to manually check the BIDS directory and the meta files to add
# all the information that MNE-BIDS could not infer. For instance, you must
# describe iEEGReference and iEEGGround yourself. It's easy to find these by
# searching for "n/a" in the sidecar files.
#
# `$ grep -i 'n/a' <bids_root>`
#
# Remember that there is a convenient javascript tool to validate all your BIDS
# directories called the "BIDS-validator", available as a web version and a
Ejemplo n.º 6
0
output_path = op.join(data_path, '..', 'MNE-sample-data-bids')

###############################################################################
# Specify the raw_file and events_data and run the BIDS conversion.

raw = mne.io.read_raw_fif(raw_fname)
bids_basename = 'sub-01_ses-01_task-audiovisual_run-01'
write_raw_bids(raw,
               bids_basename,
               output_path,
               events_data=events_data,
               event_id=event_id,
               overwrite=True)

###############################################################################
# Specify some empty room data and run BIDS conversion on it.
er_raw_fname = op.join(data_path, 'MEG', 'sample', 'ernoise_raw.fif')
er_raw = mne.io.read_raw_fif(er_raw_fname)
# For empty room data we need to specify that the subject ID is
# 'emptyroom', and that the task is 'noise'.
# We also need to specify the recording date in the format YYYYMMDD for the
# session id.
er_date = datetime.fromtimestamp(
    er_raw.info['meas_date'][0]).strftime('%Y%m%d')
er_bids_basename = 'sub-emptyroom_ses-{0}_task-noise'.format(er_date)
write_raw_bids(er_raw, er_bids_basename, output_path, overwrite=True)

###############################################################################
# Now let's see the structure of the BIDS folder we created.
print_dir_tree(output_path)
Ejemplo n.º 7
0
def _setup_main_eeg():
    """
    We copy some explicit example from MNE-BIDS and modify small details.

    Specifically, we will follow these steps:

    1. Download repository, and use the data in example directory:
        data/
            bids_layout/
                sourcedata/
                derivatives/
                sub-XXX/
                sub-XXY/
                ...

    2. Load the source raw data, extract information, preprocess certain things
    and save in a new BIDS directory

    3. Check the result and compare it with the standard
    """
    ###############################################################################
    # Step 1: Prepare the data
    # -------------------------
    #
    # First, we need some data to work with. We will use some sample simulated scalp and
    # iEEG data. For each subject, there are "seizure" events. For the present example, we will
    # show how to format the data for two modalities to comply with the Brain Imaging Data Structure
    # (`BIDS <http://bids.neuroimaging.io/>`_).
    #
    # The data are in the `European Data Format <https://www.edfplus.info/>`_
    # '.edf', which is good for us because next to the BrainVision format, EDF is
    # one of the recommended file formats for EEG BIDS. However, apart from the
    # data format, we need to build a directory structure and supply meta data
    # files to properly *bidsify* this data.
    #
    # Conveniently, there is already a data loading function available with
    # MNE-Python:

    DATADIR = os.getcwd()
    bids_root = os.path.join(DATADIR, "./data/bids_layout/")
    RUN_IEEG = False  # either run scalp, or iEEG
    line_freq = (
        60  # user should set the line frequency, since MNE-BIDS defaults to 50 Hz
    )
    test_subjectid = "0001"
    test_sessionid = "seizure"
    test_task = "monitor"
    authors = ["Adam Li", "Patrick Myers"]

    if RUN_IEEG:
        edf_fpaths = [
            os.path.join(bids_root, "sourcedata", "ieeg_ecog_test.edf")
        ]
        modality = "ecog"
    else:
        edf_fpath1 = os.path.join(bids_root, "sourcedata", "scalp_test.edf")
        edf_fpath2 = os.path.join(bids_root, "sourcedata", "scalp_test_2.edf")
        edf_fpaths = [edf_fpath1, edf_fpath2]
        modality = "eeg"
    ###############################################################################
    # Let's see whether the data has been downloaded using a quick visualization
    # of the directory tree.

    data_dir = os.path.join(bids_root, "sourcedata")
    print_dir_tree(data_dir)

    ###############################################################################
    # Step 2: Formatting as BIDS
    # --------------------------
    #
    # Let's start by formatting a single subject. We are reading the data using
    # MNE-Python's io module and the :func:`read_raw_edf` function. Note that we
    # must use `preload=False`, the default in MNE-Python. It prevents the data
    # from being loaded and modified when converting to BIDS.
    #
    # Note that kind and acquisition currently almost stand for the same thing.
    # Please read the BIDS docs to get acquainted.

    # create the BIDS directory structure
    if not os.path.exists(bids_root):
        print("Making bids root directory.")
        make_bids_folders(
            output_path=bids_root,
            session=test_sessionid,
            subject=test_subjectid,
            kind=modality,
        )

    for i, edf_fpath in enumerate(edf_fpaths):
        """ Write data file into BIDS format """
        test_runid = i

        # add a bids run
        bids_basename = make_bids_basename(
            subject=test_subjectid,
            session=test_sessionid,
            task=test_task,
            run=test_runid,
            acquisition=modality,
        )
        print("Loading filepath: ", edf_fpath)
        print("Writing to bidsroot: ", bids_root)
        print("Bids basenmae; ", bids_basename)
        # call bidsbuilder pipeline
        bids_deriv_root = BidsConverter.convert_to_bids(
            edf_fpath=edf_fpath,
            bids_root=bids_root,
            bids_basename=bids_basename,
            line_freq=line_freq,
            overwrite=True,
        )

    # currently write_raw_bids overwrites make_dataset_description
    # TODO: put this on the top when PR gets merged.
    make_dataset_description(os.path.join(bids_root),
                             name="test_bids_dataset",
                             authors=authors)

    ###############################################################################
    # What does our fresh BIDS directory look like?
    print_dir_tree(bids_root)

    ###############################################################################
    # Step 3: Check and compare and read in the data
    # ------------------------------------------------------------
    # Now we have written our BIDS directory.

    if modality in ["ecog", "seeg"]:
        kind = "ieeg"
    elif modality == "eeg":
        kind = "eeg"
    bids_fname = bids_basename + f"_{kind}.edf"

    print("Trying to read from: ", bids_fname)

    # use MNE-BIDS function to read in the data
    raw = read_raw_bids(bids_fname, bids_root)

    print("Read successfully using MNE-BIDS")

    # use BidsRun object, which just simply adds additional functionality
    # bidsrun = BidsRun(tmp_bids_root, bids_fname)
    # raw = bidsrun.load_data()

    print(raw)

    ###############################################################################
    # Step 4: Run BIDS-Validate
    # ------------------------------------------------------------
    # Now we have written our BIDS directory.
    # save a fif copy and reload it
    # TODO: re-check when pybids is updated.
    # currently, use the https://bids-standard.github.io/bids-validator/ and see that it is verified
    params = _parse_bids_filename(bids_basename, True)
    print(raw.info)
    fif_data_path = make_bids_folders(
        subject=params["sub"],
        session=params["ses"],
        kind=kind,
        output_path=bids_root,
        overwrite=False,
        verbose=True,
    )
    rel_bids_root = f"/sub-0001/ses-seizure/{kind}/"
    path = os.path.join(rel_bids_root, bids_fname)
    is_valid = BIDSValidator().is_bids(path)

    print(BIDSValidator().is_top_level(path))
    print(BIDSValidator().is_associated_data(path))
    print(BIDSValidator().is_session_level(path))
    print(BIDSValidator().is_subject_level(path))
    print(BIDSValidator().is_phenotypic(path))
    print(BIDSValidator().is_file(path))

    print("checked filepath: ", os.path.join(rel_bids_root, bids_fname))
    print(is_valid)
Ejemplo n.º 8
0
# dates. Here, they will all contain the same data but in your study, they
# will be different on different days.
dates = ['20021204', '20021201', '20021001']

for date in dates:
    er_bids_basename = make_bids_basename(subject='emptyroom', session=date,
                                          task='noise')
    er_meas_date = datetime.strptime(date, '%Y%m%d')
    er_raw.set_meas_date(er_meas_date.replace(tzinfo=timezone.utc))
    write_raw_bids(er_raw, er_bids_basename, bids_path, overwrite=True)

###############################################################################
# Let us look at the directory structure
from mne_bids.utils import print_dir_tree # noqa

print_dir_tree(bids_path)

###############################################################################
# To get an accurate estimate of the noise, it is important that the empty
# room recording be as close in date as the raw data.
# We can retrieve the filename corresponding to the empty room
# file that is closest in time to the measurement file using MNE-BIDS.
from mne_bids import get_matched_empty_room # noqa

bids_fname = bids_basename + '_meg.fif'
best_er_fname = get_matched_empty_room(bids_fname, bids_path)
print(best_er_fname)

###############################################################################
# Finally, we can read the empty room file using
raw = read_raw_bids(best_er_fname, bids_path)
Ejemplo n.º 9
0
def test_print_dir_tree(capsys):
    """Test printing a dir tree."""
    with pytest.raises(ValueError, match='Directory does not exist'):
        print_dir_tree('i_dont_exist')

    # We check the testing directory
    test_dir = op.dirname(__file__)
    with pytest.raises(ValueError, match='must be a positive integer'):
        print_dir_tree(test_dir, max_depth=-1)
    with pytest.raises(ValueError, match='must be a positive integer'):
        print_dir_tree(test_dir, max_depth='bad')

    # Do not limit depth
    print_dir_tree(test_dir)
    captured = capsys.readouterr()
    assert '|--- test_utils.py' in captured.out.split('\n')
    assert '|--- __pycache__{}'.format(os.sep) in captured.out.split('\n')
    assert '.pyc' in captured.out

    # Now limit depth ... we should not descend into pycache
    print_dir_tree(test_dir, max_depth=1)
    captured = capsys.readouterr()
    assert '|--- test_utils.py' in captured.out.split('\n')
    assert '|--- __pycache__{}'.format(os.sep) in captured.out.split('\n')
    assert '.pyc' not in captured.out

    # Limit depth even more
    print_dir_tree(test_dir, max_depth=0)
    captured = capsys.readouterr()
    assert captured.out == '|tests{}\n'.format(os.sep)
Ejemplo n.º 10
0
                           'pip install {0}>={1}'.format(pkg, ver))

# Check that FreeSurfer is initialized
FREESURFER_HOME = os.getenv('FREESURFER_HOME')
if FREESURFER_HOME is None:
    raise RuntimeError('You need to define a FREESURFER_HOME environment '
                       'variable pointing to your installation of FreeSurfer.'
                       'Also make sure you have initialized FreeSurfer by '
                       'running: `source $FREESURFER_HOME/SetUpFreeSurfer.sh`')

# Somato data prior to conversion
somato_path = somato.data_path()

# Print the directory tree
print('\nSomato data before conversion: {}\n'.format(somato_path))
print_dir_tree(somato_path, max_depth=3)
print('\n\n')

# Convert to BIDS
# Path to the raw data of somato dataset
fif_path = op.join(somato_path, 'MEG', 'somato')

# Load the raw data file
raw = mne.io.read_raw_fif(op.join(fif_path, 'sef_raw_sss.fif'))

# Set the sex of the participant to male
# Subject sex (0=unknown, 1=male, 2=female).
raw.info['subject_info']['sex'] = raw.info['subject_info'].get('sex', 1)

# Get the events, and assign an ID
events = mne.find_events(raw)