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()
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()
def test_find_empty_room(return_bids_test_dir): """Test reading of empty room data.""" data_path = testing.data_path() raw_fname = op.join(data_path, 'MEG', 'sample', 'sample_audvis_trunc_raw.fif') bids_root = _TempDir() tmp_dir = _TempDir() raw = _read_raw_fif(raw_fname) bids_path = BIDSPath(subject='01', session='01', task='audiovisual', run='01', root=bids_root, suffix='meg') write_raw_bids(raw, bids_path, overwrite=True) # No empty-room data present. er_basename = bids_path.find_empty_room() assert er_basename is None # Now create data resembling an empty-room recording. # The testing data has no "noise" recording, so save the actual data # as named as if it were noise. We first need to write the FIFF file # before reading it back in. er_raw_fname = op.join(tmp_dir, 'ernoise_raw.fif') raw.copy().crop(0, 10).save(er_raw_fname, overwrite=True) er_raw = _read_raw_fif(er_raw_fname) if not isinstance(er_raw.info['meas_date'], datetime): # mne < v0.20 er_date = datetime.fromtimestamp(er_raw.info['meas_date'][0]) else: er_date = er_raw.info['meas_date'] er_date = er_date.strftime('%Y%m%d') er_bids_path = BIDSPath(subject='emptyroom', task='noise', session=er_date, suffix='meg', root=bids_root) write_raw_bids(er_raw, er_bids_path, overwrite=True) recovered_er_bids_path = bids_path.find_empty_room() assert er_bids_path == recovered_er_bids_path # assert that we get best emptyroom if there are multiple available sh.rmtree(op.join(bids_root, 'sub-emptyroom')) dates = ['20021204', '20021201', '20021001'] for date in dates: er_bids_path.update(session=date) er_meas_date = datetime.strptime(date, '%Y%m%d') er_meas_date = er_meas_date.replace(tzinfo=timezone.utc) if check_version('mne', '0.20'): er_raw.set_meas_date(er_meas_date) else: er_raw.info['meas_date'] = (er_meas_date.timestamp(), 0) write_raw_bids(er_raw, er_bids_path) best_er_basename = bids_path.find_empty_room() assert best_er_basename.session == '20021204' with pytest.raises(ValueError, match='The root of the "bids_path" must be set'): bids_path.copy().update(root=None).find_empty_room() # assert that we get error if meas_date is not available. raw = read_raw_bids(bids_path=bids_path) if check_version('mne', '0.20'): raw.set_meas_date(None) else: raw.info['meas_date'] = None raw.annotations.orig_time = None anonymize_info(raw.info) write_raw_bids(raw, bids_path, overwrite=True) with pytest.raises(ValueError, match='The provided recording does not ' 'have a measurement date set'): bids_path.find_empty_room()
def test_find_empty_room(return_bids_test_dir, tmpdir): """Test reading of empty room data.""" data_path = testing.data_path() raw_fname = op.join(data_path, 'MEG', 'sample', 'sample_audvis_trunc_raw.fif') bids_root = tmpdir.mkdir("bids") tmp_dir = tmpdir.mkdir("tmp") raw = _read_raw_fif(raw_fname) bids_path = BIDSPath(subject='01', session='01', task='audiovisual', run='01', root=bids_root, suffix='meg') write_raw_bids(raw, bids_path, overwrite=True) # No empty-room data present. er_basename = bids_path.find_empty_room() assert er_basename is None # Now create data resembling an empty-room recording. # The testing data has no "noise" recording, so save the actual data # as named as if it were noise. We first need to write the FIFF file # before reading it back in. er_raw_fname = op.join(tmp_dir, 'ernoise_raw.fif') raw.copy().crop(0, 10).save(er_raw_fname, overwrite=True) er_raw = _read_raw_fif(er_raw_fname) if not isinstance(er_raw.info['meas_date'], datetime): # pragma: no cover # mne < v0.20 er_date = datetime.fromtimestamp(er_raw.info['meas_date'][0]) else: er_date = er_raw.info['meas_date'] er_date = er_date.strftime('%Y%m%d') er_bids_path = BIDSPath(subject='emptyroom', task='noise', session=er_date, suffix='meg', root=bids_root) write_raw_bids(er_raw, er_bids_path, overwrite=True) recovered_er_bids_path = bids_path.find_empty_room() assert er_bids_path == recovered_er_bids_path # assert that we get best emptyroom if there are multiple available sh.rmtree(op.join(bids_root, 'sub-emptyroom')) dates = ['20021204', '20021201', '20021001'] for date in dates: er_bids_path.update(session=date) er_meas_date = datetime.strptime(date, '%Y%m%d') er_meas_date = er_meas_date.replace(tzinfo=timezone.utc) er_raw.set_meas_date(er_meas_date) write_raw_bids(er_raw, er_bids_path) best_er_basename = bids_path.find_empty_room() assert best_er_basename.session == '20021204' with pytest.raises(ValueError, match='The root of the "bids_path" must be set'): bids_path.copy().update(root=None).find_empty_room() # assert that we get an error if meas_date is not available. raw = read_raw_bids(bids_path=bids_path) raw.set_meas_date(None) anonymize_info(raw.info) write_raw_bids(raw, bids_path, overwrite=True) with pytest.raises(ValueError, match='The provided recording does not ' 'have a measurement date set'): bids_path.find_empty_room() # test that the `AssociatedEmptyRoom` key in MEG sidecar is respected bids_root = tmpdir.mkdir('associated-empty-room') raw = _read_raw_fif(raw_fname) meas_date = datetime(year=2020, month=1, day=10, tzinfo=timezone.utc) er_date = datetime(year=2010, month=1, day=1, tzinfo=timezone.utc) raw.set_meas_date(meas_date) er_raw_matching_date = er_raw.copy().set_meas_date(meas_date) er_raw_associated = er_raw.copy().set_meas_date(er_date) # First write empty-room data # We write two empty-room recordings: one with a date matching exactly the # experimental measurement date, and one dated approx. 10 years earlier # We will want to enforce using the older recording via # `AssociatedEmptyRoom` (without AssociatedEmptyRoom, find_empty_room() # would return the recording with the matching date instead) er_matching_date_bids_path = BIDSPath(subject='emptyroom', session='20200110', task='noise', root=bids_root, datatype='meg', suffix='meg', extension='.fif') write_raw_bids(er_raw_matching_date, bids_path=er_matching_date_bids_path) er_associated_bids_path = (er_matching_date_bids_path.copy().update( session='20100101')) write_raw_bids(er_raw_associated, bids_path=er_associated_bids_path) # Now we write experimental data and associate it with the earlier # empty-room recording bids_path = (er_matching_date_bids_path.copy().update(subject='01', session=None, task='task')) write_raw_bids(raw, bids_path=bids_path, empty_room=er_associated_bids_path) # Retrieve empty-room BIDSPath assert bids_path.find_empty_room() == er_associated_bids_path # Should only work for MEG with pytest.raises(ValueError, match='only supported for MEG'): bids_path.copy().update(datatype='eeg').find_empty_room() # Don't create `AssociatedEmptyRoom` entry in sidecar – we should now # retrieve the empty-room recording closer in time write_raw_bids(raw, bids_path=bids_path, empty_room=None, overwrite=True) assert bids_path.find_empty_room() == er_matching_date_bids_path