Example #1
0
def test_update_sidecar_jsons(_get_bids_test_dir, _bids_validate,
                              _get_sidecar_json_update_file):
    """Test updating sidecar JSON files."""
    bids_path = BIDSPath(subject=subject_id,
                         session=session_id,
                         run=run,
                         acquisition=acq,
                         task=task,
                         suffix='meg',
                         root=_get_bids_test_dir)

    # expected key, original value, and expected value after update
    # Fields that are not `None` already are expected to exist
    # in this sidecar file. Fields that are `None` will get
    # written with the sidecar json value when update is called.
    expected_checks = [('InstitutionName', None, 'mne-bids'),
                       ('InstitutionAddress', None, 'Internet'),
                       ('MEGChannelCount', 306, 300),
                       ('MEGREFChannelCount', 0, 6), ('ECGChannelCount', 0, 0),
                       ('SEEGChannelCount', None, 0)]

    # get the sidecar json
    sidecar_path = bids_path.copy().update(extension='.json')
    sidecar_fpath = sidecar_path.fpath
    with open(sidecar_fpath, 'r', encoding='utf-8') as fin:
        sidecar_json = json.load(fin)
    for key, val, _ in expected_checks:
        assert sidecar_json.get(key) == val
    _bids_validate(bids_path.root)

    # update sidecars
    update_sidecar_json(sidecar_path, _get_sidecar_json_update_file)
    with open(sidecar_fpath, 'r', encoding='utf-8') as fin:
        sidecar_json = json.load(fin)
    for key, _, val in expected_checks:
        assert sidecar_json.get(key) == val
    _bids_validate(bids_path.root)

    # should result in error if you don't explicitly say
    # its a json file
    with pytest.raises(RuntimeError, match='Only works for ".json"'):
        update_sidecar_json(sidecar_path.copy().update(extension=None),
                            _get_sidecar_json_update_file)

    # error should raise if the file path doesn't exist
    error_bids_path = sidecar_path.copy().update(subject='02')
    with pytest.raises(RuntimeError, match='Sidecar file ' 'does not exist.'):
        update_sidecar_json(error_bids_path, _get_sidecar_json_update_file)
Example #2
0
# Define a sidecar update as a dictionary
entries = {
    'PowerLineFrequency': 60,
    'Manufacturer': "MEGIN",
    'InstitutionName': "Martinos Center"
}

# Note: ``update_sidecar_json`` will perform essentially a
# dictionary update to your sidecar json file, so be absolutely sure
# that the ``entries`` are defined with the proper fields specified
# by BIDS. For example, if you are updating the ``coordsystem.json``
# file, then you don't want to include ``PowerLineFrequency`` in
# ``entries``.
#
# Now update all sidecar fields according to our updating dictionary
update_sidecar_json(bids_path=sidecar_path, entries=entries)

###############################################################################
# Read the updated dataset
# ------------------------

# new line frequency is now 60 Hz
raw = read_raw_bids(bids_path=bids_path)
print(raw.info['line_freq'])

###############################################################################
# Generate a new report based on the updated metadata.

# The manufacturer was changed to ``MEGIN``
print(make_report(bids_root))