def test_get_entity_vals(entity, expected_vals, kwargs, return_bids_test_dir): """Test getting a list of entities.""" bids_root = return_bids_test_dir if kwargs is None: kwargs = dict() if entity == 'bogus': with pytest.raises(ValueError, match='`key` must be one of'): get_entity_vals(root=bids_root, entity_key=entity, **kwargs) else: vals = get_entity_vals(root=bids_root, entity_key=entity, **kwargs) assert vals == expected_vals # test using ``with_key`` kwarg entities = get_entity_vals(root=bids_root, entity_key=entity, with_key=True, **kwargs) entity_long_to_short = { val: key for key, val in ALLOWED_PATH_ENTITIES_SHORT.items() } assert entities == [ f'{entity_long_to_short[entity]}-{val}' for val in expected_vals ]
def test_get_entity_vals(entity, expected_vals, kwargs, return_bids_test_dir): """Test getting a list of entities.""" bids_root = return_bids_test_dir if kwargs is None: kwargs = dict() if entity == 'bogus': with pytest.raises(ValueError, match='`key` must be one of'): get_entity_vals(root=bids_root, entity_key=entity, **kwargs) else: vals = get_entity_vals(root=bids_root, entity_key=entity, **kwargs) assert vals == expected_vals
def _init_subjects(filter_sub, root, ch_type): """Initialize collection of files by loading selected subjects Parameters ---------- filter_sub : list a list of subjects to select from all BIDS files root : str root of BIDS dataset ch_type: str type of BIDS dataset Returns ---------- files: list a list of partially filtered BIDS paths according to subjects """ if filter_sub == ["*"]: filter_sub = mne_bids.get_entity_vals(root, 'subject') filered_subjects = [] bids_root = pathlib.Path(root) type_exten = ALLOWED_DATATYPE_EXTENSIONS[ch_type] for subject in filter_sub: bids_path = mne_bids.BIDSPath(subject=subject, datatype=ch_type, root=bids_root) files = bids_path.match() files_eeg = [f for f in files if f.extension.lower() in type_exten] filered_subjects += files_eeg return filered_subjects
def get_bids_paths(root, task=None, acquisition=None, ext=".vhdr"): """ Get a list of all the bids_paths in a certain dataset. Once you have these paths, you can easily use the eeg data with read_raw_bids. Parameters ---------- root: str The root path where your bids directory is stored. Returns ------- bids_fpaths: list List of all bids_paths in the root directory. """ bids_fpaths = [] subjects = get_entity_vals(root, 'subject') for subject in subjects: ignore_subjects = [sub for sub in subjects if sub != subject] sessions = get_entity_vals(root, "session", ignore_subjects=ignore_subjects) for session in sessions: ignore_sessions = [ses for ses in sessions if ses != session] runs = get_entity_vals(root, "run", ignore_subjects=ignore_subjects, ignore_sessions=ignore_sessions) for run in runs: # The other entities here are likely all standard across your dataset bids_path = BIDSPath(subject=subject, session=session, task=task, acquisition=acquisition, datatype=acquisition, run=run, suffix=acquisition, extension=ext, root=root) # Finds all the runs that match the above params add_paths = bids_path.match() bids_fpaths.extend(add_paths) return bids_fpaths
def test_get_entity_vals(entity, expected_vals, kwargs, return_bids_test_dir): """Test getting a list of entities.""" bids_root = return_bids_test_dir # Add some derivative data that should be ignored by get_entity_vals() deriv_path = Path(bids_root) / 'derivatives' deriv_meg_dir = deriv_path / 'pipeline' / 'sub-deriv' / 'ses-deriv' / 'meg' deriv_meg_dir.mkdir(parents=True) (deriv_meg_dir / 'sub-deriv_ses-deriv_task-deriv_meg.fif').touch() (deriv_meg_dir / 'sub-deriv_ses-deriv_task-deriv_meg.json').touch() if kwargs is None: kwargs = dict() if entity == 'bogus': with pytest.raises(ValueError, match='`key` must be one of'): get_entity_vals(root=bids_root, entity_key=entity, **kwargs) else: vals = get_entity_vals(root=bids_root, entity_key=entity, **kwargs) assert vals == expected_vals # test using ``with_key`` kwarg entities = get_entity_vals(root=bids_root, entity_key=entity, with_key=True, **kwargs) entity_long_to_short = { val: key for key, val in ALLOWED_PATH_ENTITIES_SHORT.items() } assert entities == [ f'{entity_long_to_short[entity]}-{val}' for val in expected_vals ] # Test without ignoring the derivatives dir entities = get_entity_vals(root=bids_root, entity_key=entity, **kwargs, ignore_dirs=None) if entity not in ('acquisition', 'run'): assert 'deriv' in entities # Clean up shutil.rmtree(deriv_path)
# skip to the next section. # # First we will define where the raw data is stored. We will analyse a # BIDS dataset. This ensures we have all the metadata we require # without manually specifying the trigger names etc. # We first define where the root directory of our dataset is. # In this example we use the example dataset ``audio_or_visual_speech``. root = data_path() dataset = BIDSPath(root=root, suffix="nirs", extension=".snirf", session="01", task="AudioVisualBroadVsRestricted", datatype="nirs") subjects = get_entity_vals(root, 'subject') # %% # Define individual analysis # -------------------------- # # More details on the epoching analysis can be found # at :ref:`Waveform individual analysis <tut-fnirs-processing>`. # A minimal processing pipeline is demonstrated here, as the focus # of this tutorial is to demonstrate the decoding pipeline. # In this example only the epochs for the two conditions we wish to decode # between are retained. def epoch_preprocessing(bids_path):
delete_all = False save_dir = "./derivatives/leadfields" subfolders = ["ico"] BIDS_ROOT = "/storage/store/data/camcan/BIDSsep/passive" kind = "passive" # can be "smt" age_max = 30 n_subjects = 3 # n_subjects = 20 # subjects_dir = cfg.get_subjects_dir(dataset_name) subjects_dir = '/storage/store/data/camcan-mne/freesurfer' # subjects = cfg.get_subjects_list(dataset_name, 0, age_max)[:n_subjects] subjects = get_entity_vals(BIDS_ROOT, entity_key='subject') subjects = subjects[:n_subjects] # take one only os.environ['SUBJECTS_DIR'] = subjects_dir dataset_name = 'camcan' trans_fnames = [cfg.get_trans_fname(dataset_name, subject) for subject in subjects] raw_fnames = [cfg.get_raw_fname(dataset_name, subject) for subject in subjects] bem_fnames = [cfg.get_bem_fname(dataset_name, subject) for subject in subjects] resolution = 4 spacing = "ico%d" % resolution
# read in the actual file raw = read_raw_bids(bids_path) # get the channels ch_names = raw.ch_names # save this to a output file elec_dict = {"elec_name": np.array(ch_names).astype(np.object)} Path(output_fpath).parent.mkdir(exist_ok=True, parents=True) savemat(output_fpath, elec_dict) if __name__ == "__main__": root = Path("/Users/adam2392/Dropbox/epilepsy_bids/") # get the runs for this subject subjects = get_entity_vals(root, "subject") session = "presurgery" extension = ".vhdr" output_path = root / "sourcedata" / "electrodes localized" / "setup" for subject in subjects: if not any([x in subject for x in ["la", "nl", "tvb"]]): continue if subject in ["la00"]: continue # create the BIDS path bids_path = BIDSPath(subject=subject, session=session, root=root,
import os.path as op import mne from mne.report import Report from mne_bids import get_entity_vals layout = mne.channels.read_layout('KIT-AD.lout') task = 'OcularLDT' bids_root = op.join('/', 'Volumes', 'teon-backup', 'Experiments', task) mri_subjects_dir = op.join('/', 'Volumes', 'teon-backup', 'Experiments', task + '_MRI') derivative = 'fwd' redo = False subjects_list = get_entity_vals(bids_root, entity_key='subject') for subject in subjects_list: print("#" * 9 + f"\n# {subject} #\n" + "#" * 9) # define filenames path = op.join(bids_root, f"sub-{subject}", 'meg') fname_raw = op.join(path, f"sub-{subject}_task-{task}_meg.fif") fname_mp_raw = op.join(path, f"sub-{subject}_task-{task}_split-01_meg.fif") fname_fwd = op.join(path, f"sub-{subject}_task-{task}_{derivative}.fif") fname_trans = op.join(path, f"sub-{subject}_task-{task}_trans.fif") bem_sol = op.join(mri_subjects_dir, f"sub-{subject}", 'bem', f'sub-{subject}-inner_skull-bem-sol.fif') fname_src = op.join(mri_subjects_dir, f"sub-{subject}", 'bem', f'sub-{subject}-ico-4-src.fif') if not op.exists(fname_fwd) or redo:
def avail_subj(root): avail_subj = mne_bids.get_entity_vals(root, 'subject') return avail_subj