def test_file_not_found(tmp_path): """Check behavior if the requested file cannot be found.""" # First a path with a filename extension. bp = BIDSPath( root=tmp_path, subject='foo', task='bar', datatype='eeg', suffix='eeg', extension='.fif' ) bp.fpath.parent.mkdir(parents=True) with pytest.raises(FileNotFoundError, match='File does not exist'): read_raw_bids(bids_path=bp) # Now without an extension bp.extension = None with pytest.raises(FileNotFoundError, match='File does not exist'): read_raw_bids(bids_path=bp)
def test_find_matching_sidecar(return_bids_test_dir, tmp_path): """Test finding a sidecar file from a BIDS dir.""" bids_root = return_bids_test_dir bids_path = _bids_path.copy().update(root=bids_root) # Now find a sidecar sidecar_fname = _find_matching_sidecar(bids_path, suffix='coordsystem', extension='.json') expected_file = op.join('sub-01', 'ses-01', 'meg', 'sub-01_ses-01_coordsystem.json') assert sidecar_fname.endswith(expected_file) # Find multiple sidecars, tied in score, triggering an error with pytest.raises(RuntimeError, match='Expected to find a single'): open(sidecar_fname.replace('coordsystem.json', '2coordsystem.json'), 'w').close() print_dir_tree(bids_root) _find_matching_sidecar(bids_path, suffix='coordsystem', extension='.json') # Find nothing and raise. with pytest.raises(RuntimeError, match='Did not find any'): fname = _find_matching_sidecar(bids_path, suffix='foo', extension='.bogus') # Find nothing and receive None and a warning. on_error = 'warn' with pytest.warns(RuntimeWarning, match='Did not find any'): fname = _find_matching_sidecar(bids_path, suffix='foo', extension='.bogus', on_error=on_error) assert fname is None # Find nothing and receive None. on_error = 'ignore' fname = _find_matching_sidecar(bids_path, suffix='foo', extension='.bogus', on_error=on_error) assert fname is None # Invalid on_error. on_error = 'hello' with pytest.raises(ValueError, match='Acceptable values for on_error are'): _find_matching_sidecar(bids_path, suffix='coordsystem', extension='.json', on_error=on_error) # Test behavior of suffix and extension params when suffix and extension # are also (not) present in the passed BIDSPath bids_path = BIDSPath( subject='test', task='task', datatype='eeg', root=tmp_path ) bids_path.mkdir() for suffix, extension in zip( ['eeg', 'eeg', 'events', 'events'], ['.fif', '.json', '.tsv', '.json'] ): bids_path.suffix = suffix bids_path.extension = extension bids_path.fpath.touch() # suffix parameter should always override BIDSPath.suffix bids_path.extension = '.json' for bp_suffix in (None, 'eeg'): bids_path.suffix = bp_suffix s = _find_matching_sidecar(bids_path=bids_path, suffix='events') assert Path(s).name == 'sub-test_task-task_events.json' # extension parameter should always override BIDSPath.extension bids_path.suffix = 'events' for bp_extension in (None, '.json'): bids_path.extension = bp_extension s = _find_matching_sidecar(bids_path=bids_path, extension='.tsv') assert Path(s).name == 'sub-test_task-task_events.tsv' # If suffix and extension parameters are not passed, use BIDSPath # attributes bids_path.suffix = 'events' bids_path.extension = '.tsv' s = _find_matching_sidecar(bids_path=bids_path) assert Path(s).name == 'sub-test_task-task_events.tsv'