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 fif_to_bids(path: Path): raw = mne.io.read_raw_fif(path) bids_path = mne_bids.BIDSPath( subject="01", session="01", task="testing", acquisition="01", run="01", root=data_dir / "BIDS", ) mne_bids.write_raw_bids(raw, bids_path, overwrite=True)
def subj_files(avail_subj, root): # create a dictionary of subjects and corresponding files subjects = {} for subject in avail_subj: bids_path = mne_bids.BIDSPath(subject=subject, root=root) files = bids_path.match() scans_file = [f for f in files if "scans" in f.suffix][0] scans_info = pd.read_csv(scans_file, sep='\t') filenames = scans_info["filename"].to_list() cleaned_filenames = [] for file in filenames: _, tail = os.path.split(file) cleaned_filenames.append(tail) subjects[subject] = cleaned_filenames return subjects
def _filter_exceptions(subjects, tasks, runs, files, root, ch_type): """Remove exceptions as defined by user_params Parameters ---------- subjects, tasks, runs : list a list of subjects, tasks, and runs to be cartesian multiplied to get omitted files files: list list of partially filtered files according to subject and tasks root: str root of BIDS dataset ch_type: str type of BIDS dataset Returns ---------- files: list a list of fully filtered BIDS paths according to exceptions """ # get cartesian product of subjects, tasks, and runs exceptions = list(product(subjects, tasks, runs)) bids_root = pathlib.Path(root) type_exten = ALLOWED_DATATYPE_EXTENSIONS[ch_type] # turn each exception into a BIDS path for i in range(len(exceptions)): file = exceptions[i] sub = file[0] task = file[1] run = file[2] bids_path = mne_bids.BIDSPath(subject=sub, task=task, run=run, datatype=ch_type, root=bids_root) e_files = bids_path.match() e_files_eeg = [f for f in e_files if f.extension.lower() in type_exten] exceptions[i] = e_files_eeg[0] # remove any file in files that shows up in exceptions and return return [f for f in files if f not in exceptions]
ch_type = 'eeg' for subject in subjects: frontalEOGChannel = 'E22' # Load preprocessed mne data from Guillaume fname = os.path.join(data_path, subject, 'RestingState_Blinks_epo.fif') epochs = mne.read_epochs(fname, proj=True, preload=True, verbose=None) subject = subject.lower() + "01" #eo.plot_image(picks=[frontalEOGChannel]) ## Do some preprocessing on the epochs - jitter the center so the model doesn't learn the position epochs_preprocessed = epochs.crop(tmin=-0.4 + np.random.random() * 0.1, tmax=0.40 + np.random.random() * 0.1, include_tmax=True) # Check if resampling is needed epochs_preprocessed = epochs_preprocessed.resample(100) # Create bids folder bids_path = mne_bids.BIDSPath(subject=subject, root=export_folder) # Use the raw object that the trials came from in order to build the BIDS tree mne_bids.write_raw_bids(fake_raw, bids_path, overwrite=True, verbose=True) # Export trials into .nii files export_epoch_to_nifti_small.run_export(epochs_preprocessed, ch_type, annotated_event_for_gt, bids_path)
if subj_fullname in sub_id_name.keys(): subj_id = str(sub_id_name[subj_fullname]) task_name = raw_file_path.parts[-1].split('.')[0][-4:] # vid% if subj_id in bad_subjects.keys( ) and task_name in bad_subjects[subj_id]: continue raw_meg = make_annotations_from_events( raw_meg) # make readable annotations else: subj_id = 'emptyroom' task_name = 'noise' date_record = str(raw_meg.info['meas_date']).split(' ')[0].replace('-', '') meg_bids_path = mne_bids.BIDSPath(subject=subj_id, session=date_record, task=task_name, root=data_bids_dir) # mri_bids_path = mne_bids.BIDSPath(subject=subj_id, session=date_record, # root=data_bids_dir) # mne_bids.write_raw_bids(raw=raw_meg, bids_path=meg_bids_path, anonymize={'daysback': 40000}, overwrite=True, verbose=True) if subj_id != 'emptyroom': mne_bids.write_meg_crosstalk(ct_file, meg_bids_path) if int(date_record[:4]) < 2020:
#epochs_heartbeat['999'].plot_image(picks='meg') export_folder = '/home/nas/Desktop/test_BIDS' # Select channel type to create the topographies on ch_type = 'grad' for iSubject in range(1, 15): annotated_event_for_gt = '998' # This is the event that will be used to create the derivatives # 999 Heartbeats # 998 Blinks #epochs_ = epochs_heartbeat[(iSubject-1)*3:iSubject*3] epochs_ = epochs_blink[(iSubject - 1) * 3:iSubject * 3] # Create bids folder bids_path = mne_bids.BIDSPath(subject='IVADOMEDSubjTest' + str(iSubject), session='IVADOMEDSession1', task='testing', acquisition='01', run='01', root=export_folder) # Use the raw object that the trials came from in order to build the BIDS tree mne_bids.write_raw_bids(raw, bids_path, overwrite=True, verbose=True) # Export trials into .nii files export_epoch_to_nifti_small.run_export(epochs_, ch_type, annotated_event_for_gt, bids_path)