def main(sourcedata, derivatives, subject, session, tmp_dir): sourcedata_layout = BIDSLayout(sourcedata) sourcedata_df = sourcedata_layout.as_data_frame() events = sourcedata_df[(sourcedata_df['type'] == 'events') & (sourcedata_df['subject'] == subject) & (sourcedata_df['session'] == session)] derivatives_layout = BIDSLayout(os.path.join(derivatives, 'spynoza')) derivatives_df = derivatives_layout.as_data_frame() bold = derivatives_df[(derivatives_df['type'] == 'preproc') & (derivatives_df['subject'] == subject) & (derivatives_df['session'] == session)] mask = derivatives_layout.get(subject=subject, session=session, type='mask', return_type='file')[0] mask = image.math_img('(im > .5).astype(int)', im=mask) print(mask) row = bold.iloc[0] results_dir = os.path.join(derivatives, 'modelfitting_av', 'glm2', 'sub-{}'.format(row['subject'])) os.makedirs(results_dir, exist_ok=True) av_bold_fn = os.path.join( results_dir, 'sub-{}_ses-{}_bold_average.nii.gz'.format(row['subject'], row['session'])) av_bold = average_over_runs(bold.path.tolist(), output_filename=av_bold_fn) av_bold = image.math_img('(av_bold / av_bold.mean(-1)[..., np.newaxis])', av_bold=av_bold) av_bold.to_filename(av_bold_fn) model = FirstLevelModel(t_r=4, mask=mask, drift_model=None) paradigm = pd.read_table(events.iloc[0]['path']) model.fit(av_bold, paradigm) left_right = model.compute_contrast('eye_L - eye_R', output_type='z_score') left_right.to_filename( os.path.join( results_dir, 'sub-{}_ses-{}_left_over_right_zmap.nii.gz'.format( row['subject'], row['session'], ))) left_right = model.compute_contrast('eye_L - eye_R', output_type='effect_size') left_right.to_filename( os.path.join( results_dir, 'sub-{}_ses-{}_left_over_right_psc.nii.gz'.format( row['subject'], row['session'])))
def get_T1w(fpath): """ Returns list of anatomical (T1w) files from ``fpath`` Parameters ---------- fpath : str Filepath to BIDS dataset (or loaded BIDSLayout object) Returns ------- imgs : list-of-str Filepaths of T1w images """ # exclude everything that isn't the subject data itself data = BIDSLayout(op.abspath(fpath), exclude=EXCLUDE) # TODO: it'd be great to do this without needing pandas... t1s = data.as_data_frame(modality='anat', type='T1w', extensions='.nii.gz') # try to get most "minimal" (i.e., fewest tags) T1w image for each subject cols = [f for f in TAGS if f in t1s.columns] if len(cols) > 0: t1s = t1s.sort_values(cols, na_position='first').reset_index(drop=True) imgs = t1s.groupby('subject').nth(0).get('path').tolist() if len(imgs) == 0: raise ValueError(f'There are no viable T1w images in {fpath}. ' 'Check inputs and try again.') return imgs
def prepare_bids_df(sourcedata_dir, subject_ids=None, session_ids=None): layout = BIDSLayout(sourcedata_dir) bids_df = layout.as_data_frame() bids_df.dropna(subset=["subject"], inplace=True) # remove study level info nii_df = bids_df[bids_df.path.str.endswith(NII_EXT)] nii_df = nii_df.sort_values( by=["subject", "session", "modality", "type", "run"]) # reduce to requested sessions if subject_ids: nii_df = nii_df[nii_df.subject.isin(subject_ids)] if session_ids: nii_df = nii_df[nii_df.session.isin(session_ids)] return nii_df
def drp_seed_fc(): import numpy as np from os import path #from labbookdb.report.tracking import treatment_group, append_external_identifiers from samri.plotting.overview import multiplot_matrix, multipage_plot from samri.utilities import bids_substitution_iterator from samri.analysis import fc from samri.utilities import N_PROCS N_PROCS = max(N_PROCS - 8, 2) from bids.grabbids import BIDSLayout from bids.grabbids import BIDSValidator import os base = '~/ni_data/ofM.dr/bids/preprocessing/generic/' base = os.path.abspath(os.path.expanduser(base)) validate = BIDSValidator() for x in os.walk(base): print(x[0]) print(validate.is_bids(x[0])) layout = BIDSLayout(base) df = layout.as_data_frame() df = df[df.type.isin(['cbv'])] print(df) #substitutions = bids_substitution_iterator( # list(df['session'].unique()), # all_subjects, # ["CogB",], # "~/ni_data/ofM.dr/", # "composite", # acquisitions=["EPI",], # check_file_format='~/ni_data/ofM.dr/preprocessing/{preprocessing_dir}/sub-{subject}/ses-{session}/func/sub-{subject}_ses-{session}_acq-{acquisition}_task-{task}_cbv.nii.gz') #substitutions = df.T.to_dict().values()[:2] substitutions = df.T.to_dict().values() print(substitutions) fc_results = fc.seed_based( substitutions, "~/ni_data/templates/roi/DSURQEc_drp.nii.gz", "/usr/share/mouse-brain-atlases/dsurqec_200micron_mask.nii", ts_file_template= '~/ni_data/ofM.dr/bids/preprocessing/generic/sub-{subject}/ses-{session}/func/sub-{subject}_ses-{session}_acq-{acquisition}_task-{task}_cbv.nii.gz', save_results= "~/ni_data/ofM.dr/bids/fc/DSURQEc_drp/sub-{subject}/ses-{session}/func/sub-{subject}_ses-{session}_acq-{acquisition}_task-{task}_cbv_zstat.nii.gz", n_procs=N_PROCS, cachedir='/mnt/data/joblib')
def get_bids_df(bids_dir, scans_only=None, keep_defaced=False): if isinstance(bids_dir, Path): bids_dir = bids_dir.as_posix() layout = BIDSLayout(bids_dir) df_pybids = layout.as_data_frame() if not keep_defaced: df_pybids = df_pybids.query('~path.str.contains("defaced")') if scans_only: df_pybids = (df_pybids.loc[df_pybids.path.str.contains('nii.gz'), :]. query("~path.str.contains('.git')")) df_pybids['json_path'] = \ (df_pybids.path.apply( lambda x: Path(''.join([*x.split('.')[:-2], '.json'])))) return df_pybids
def drp_seed_fc(): import numpy as np from os import path #from labbookdb.report.tracking import treatment_group, append_external_identifiers from samri.plotting.overview import multiplot_matrix, multipage_plot from samri.utilities import bids_substitution_iterator from samri.analysis import fc from samri.utilities import N_PROCS N_PROCS=max(N_PROCS-8, 2) from bids.grabbids import BIDSLayout from bids.grabbids import BIDSValidator import os base = '~/ni_data/ofM.dr/bids/preprocessing/generic/' base = os.path.abspath(os.path.expanduser(base)) validate = BIDSValidator() for x in os.walk(base): print(x[0]) print(validate.is_bids(x[0])) layout = BIDSLayout(base) df = layout.as_data_frame() df = df[df.type.isin(['cbv'])] print(df) #substitutions = bids_substitution_iterator( # list(df['session'].unique()), # all_subjects, # ["CogB",], # "~/ni_data/ofM.dr/", # "composite", # acquisitions=["EPI",], # check_file_format='~/ni_data/ofM.dr/preprocessing/{preprocessing_dir}/sub-{subject}/ses-{session}/func/sub-{subject}_ses-{session}_acq-{acquisition}_task-{task}_cbv.nii.gz') #substitutions = df.T.to_dict().values()[:2] substitutions = df.T.to_dict().values() print(substitutions) fc_results = fc.seed_based(substitutions, "~/ni_data/templates/roi/DSURQEc_drp.nii.gz", "/usr/share/mouse-brain-atlases/dsurqec_200micron_mask.nii", ts_file_template='~/ni_data/ofM.dr/bids/preprocessing/generic/sub-{subject}/ses-{session}/func/sub-{subject}_ses-{session}_acq-{acquisition}_task-{task}_cbv.nii.gz', save_results="~/ni_data/ofM.dr/bids/fc/DSURQEc_drp/sub-{subject}/ses-{session}/func/sub-{subject}_ses-{session}_acq-{acquisition}_task-{task}_cbv_zstat.nii.gz", n_procs=N_PROCS, cachedir='/mnt/data/joblib')
def check_fs_subjects(sourcedata_dir, fs_dir): "checks that for every subject and session with T1w there is a finished cross and long fs subject" from bids.grabbids import BIDSLayout layout = BIDSLayout(sourcedata_dir) df = layout.as_data_frame() df = df[["subject", "session", "type"]].drop_duplicates().query("type=='T1w'") df.reset_index(inplace=True, drop=True) # fs os.chdir(fs_dir) df["cross"] = None df["long"] = None for r in df.itertuples(index=True): print(r) cross = True if glob("sub-{s}_ses-{ses}/scripts/recon-all.done".format(s=r.subject, ses=r.session)) else False long = True if glob( "sub-{s}_ses-{ses}.long.sub-*/scripts/recon-all.done".format(s=r.subject, ses=r.session)) else False df.loc[r.Index, "cross"] = cross df.loc[r.Index, "long"] = long missing = df[df.cross == False] raise_ex = False if len(missing) > 0: print("*** CROSS missing for {} entries".format(len(missing))) print(missing) raise_ex = True missing = df[df.long == False] if len(missing) > 0: print("*** LONG missing for {} entries".format(len(missing))) print(missing) raise_ex = True print("*** Found {} cross and {} long sessions from {} subjects".format(df.cross.sum(), df.long.sum(), len(df.subject.unique()))) if raise_ex: raise Exception("SOME FS SUBJECTS ARE MISSING") else: print("Everything looks good")
def bids_data_selection( base, structural_match, functional_match, subjects, sessions, verbose=False, joint_conditions=True, ): validate = BIDSValidator() if verbose: for x in os.walk(base): print(x[0]) if validate.is_bids(x[0]): print("Is not BIDS-formatted.") else: print("Detected!") layout = BIDSLayout(base) df = layout.as_data_frame() # drop event files df = df[df.type != 'events'] # rm .json df = df.loc[df.path.str.contains('.nii')] # generate scan types for later df['scan_type'] = "" #print(df.path.str.startswith('task', beg=0,end=len('task'))) beg = df.path.str.find('task-') end = df.path.str.find('.') #df.loc[df.modality == 'func', 'scan_type'] = 'acq-'+df['acq']+'_task-'+ df.path.str.partition('task-')[2].str.partition('.')[0] #df.loc[df.modality == 'anat', 'scan_type'] = 'acq-'+df['acq']+'_' + df['type'] #TODO: fix task!=type df.loc[df.modality == 'func', 'task'] = df.path.str.partition('task-')[2].str.partition('_')[0] df.loc[df.modality == 'func', 'scan_type'] = 'task-' + df['task'] + '_acq-' + df['acq'] df.loc[df.modality == 'anat', 'scan_type'] = 'acq-' + df['acq'] + '_' + df['type'] #TODO: The following should be collapsed into one criterion category if functional_match or structural_match: res_df = pd.DataFrame() if functional_match: _df = deepcopy(df) try: if joint_conditions: for match in functional_match.keys(): _df = _df.loc[_df[match].isin(functional_match[match])] res_df = res_df.append(_df) else: for match in structural_match.keys(): _df = filter_data(_df, match, functional_match[match]) res_df = res_df.append(_df) except: pass if structural_match: _df = deepcopy(df) try: if joint_conditions: for match in structural_match.keys(): _df = _df.loc[_df[match].isin(structural_match[match])] res_df = res_df.append(_df) else: for match in structural_match.keys(): _df = filter_data(_df, match, structural_match[match]) res_df = res_df.append(_df) except: pass df = res_df if (subjects): df = filter_data(df, 'subject', subjects) if (sessions): df = filter_data(df, 'session', sessions) return df
def main(sourcedata, derivatives, subject, session, tmp_dir): sourcedata_layout = BIDSLayout(sourcedata) sourcedata_df = sourcedata_layout.as_data_frame() events = sourcedata_df[(sourcedata_df['type'] == 'events') & (sourcedata_df['subject'] == subject) & (sourcedata_df['session'] == session)] derivatives_layout = BIDSLayout( os.path.join(derivatives, 'spynoza_mc_mutualinfo')) derivatives_df = derivatives_layout.as_data_frame() bold = derivatives_df[(derivatives_df['type'] == 'preproc') & (derivatives_df['subject'] == subject) & (derivatives_df['session'] == session)] confounds = derivatives_df[(derivatives_df['type'] == 'confounds') & (derivatives_df['subject'] == subject) & (derivatives_df['session'] == session)] print(derivatives_df.type.unique()) mask = derivatives_layout.get(subject=subject, session=session, type='mask', return_type='file')[0] df = events.merge(bold, on=['subject', 'session', 'run'], suffixes=('_events', '_bold')) confounds = confounds.rename(columns={'path': 'confounds'}) df = df.merge(confounds[['subject', 'session', 'run', 'confounds']]) models = [] for ix, row in df.iterrows(): results_dir = os.path.join(derivatives, 'modelfitting', 'glm4', 'sub-{}'.format(row['subject'])) if 'session' in row: results_dir = os.path.join(results_dir, 'ses-{}'.format(row['session'])) os.makedirs(results_dir, exist_ok=True) confounds = pd.read_table(row.confounds).fillna(method='bfill') print('Fitting {}'.format(row['path_bold'])) model = FirstLevelModel(t_r=4, mask=mask) paradigm = pd.read_table(row['path_events']) model.fit(row['path_bold'], paradigm, confounds=confounds) left_right = model.compute_contrast('eye_L - eye_R', output_type='z_score') left_right.to_filename( os.path.join( results_dir, 'sub-{}_ses-{}_run-{}_left_over_right_zmap.nii.gz'.format( row['subject'], row['session'], row['run']))) left_right = model.compute_contrast('eye_L - eye_R', output_type='effect_size') left_right.to_filename( os.path.join( results_dir, 'sub-{}_ses-{}_run-{}_left_over_right_psc.nii.gz'.format( row['subject'], row['session'], row['run']))) models.append(model) second_level_model = SecondLevelModel(mask=mask) second_level_model.fit(models) left_right_group = second_level_model.compute_contrast( first_level_contrast='eye_L - eye_R', output_type='z_score') left_right_group.to_filename( os.path.join( results_dir, 'sub-{}_ses-{}_left_over_right_zmap.nii.gz'.format( row['subject'], row['session']))) left_right_group = second_level_model.compute_contrast( first_level_contrast='eye_L - eye_R', output_type='effect_size') left_right_group.to_filename( os.path.join( results_dir, 'sub-{}_ses-{}_left_over_right_effect_size.nii.gz'.format( row['subject'], row['session']))) left_right_group = second_level_model.compute_contrast( first_level_contrast='eye_L - eye_R', output_type='z_score') left_right_group.to_filename( os.path.join( results_dir, 'sub-{}_ses-{}_left_over_right_zmap.nii.gz'.format( row['subject'], row['session'])))
def bids_autograb(bids_dir): bids_dir = path.abspath(path.expanduser(bids_dir)) validate = BIDSValidator() layout = BIDSLayout(bids_dir) df = layout.as_data_frame() return df
def main(sourcedata, derivatives, subject, session, tmp_dir): sourcedata_layout = BIDSLayout(sourcedata) sourcedata_df = sourcedata_layout.as_data_frame() events = sourcedata_df[(sourcedata_df['type'] == 'events') & (sourcedata_df['subject'] == subject) & (sourcedata_df['session'] == session)] derivatives_layout = BIDSLayout(os.path.join(derivatives, 'spynoza')) derivatives_df = derivatives_layout.as_data_frame() bold = derivatives_df[(derivatives_df['type'] == 'preproc') & (derivatives_df['subject'] == subject) & (derivatives_df['session'] == session)] mask = derivatives_layout.get(subject=subject, session=session, type='mask', return_type='file')[0] mask = image.math_img('(im > .5).astype(int)', im=mask) print(mask) df = events.merge(bold, on=['subject', 'session', 'run'], suffixes=('_events', '_bold')) models = [] for ix, row in df.iterrows(): results_dir = os.path.join(derivatives, 'modelfitting', 'sub-{}'.format(row['subject'])) if 'session' in row: results_dir = os.path.join(results_dir, 'ses-{}'.format(row['session'])) os.makedirs(results_dir, exist_ok=True) print('Fitting {}'.format(row['path_bold'])) model = FirstLevelModel(t_r=4, mask=mask) paradigm = pd.read_table(row['path_events']) paradigm_short = paradigm.copy() paradigm_short['duration'] = 1 paradigm_short['trial_type'] = paradigm_short['trial_type'].map( lambda x: '{}_instant'.format(x)) paradigm = pd.concat((paradigm, paradigm_short)) model.fit(row['path_bold'], paradigm) left_right = model.compute_contrast('eye_L - eye_R', output_type='z_score') left_right.to_filename( os.path.join( results_dir, 'sub-{}_ses-{}_run-{}_left_over_right_zmap.nii.gz'.format( row['subject'], row['session'], row['run']))) left_right = model.compute_contrast('eye_L - eye_R', output_type='effect_size') left_right.to_filename( os.path.join( results_dir, 'sub-{}_ses-{}_run-{}_left_over_right_psc.nii.gz'.format( row['subject'], row['session'], row['run']))) eye_l_instant = model.compute_contrast('eye_L_instant', output_type='z_score') eye_l_instant.to_filename( os.path.join( results_dir, 'sub-{}_ses-{}_run-{}_eye_l_instant_zmap.nii.gz'.format( row['subject'], row['session'], row['run']))) eye_l_instant = model.compute_contrast('eye_L_instant', output_type='effect_size') eye_l_instant.to_filename( os.path.join( results_dir, 'sub-{}_ses-{}_run-{}_eye_l_instant_effect_size.nii.gz'.format( row['subject'], row['session'], row['run']))) eye_r_instant = model.compute_contrast('eye_R_instant', output_type='z_score') eye_r_instant.to_filename( os.path.join( results_dir, 'sub-{}_ses-{}_run-{}_eye_r_instant_zmap.nii.gz'.format( row['subject'], row['session'], row['run']))) eye_r_instant = model.compute_contrast('eye_R_instant', output_type='effect_size') eye_r_instant.to_filename( os.path.join( results_dir, 'sub-{}_ses-{}_run-{}_eye_R_instant_effect_size.nii.gz'.format( row['subject'], row['session'], row['run']))) models.append(model) second_level_model = SecondLevelModel(mask=mask) second_level_model.fit(models) left_right_group = second_level_model.compute_contrast( first_level_contrast='eye_L - eye_R', output_type='z_score') left_right_group.to_filename( os.path.join( results_dir, 'sub-{}_ses-{}_left_over_right_zmap.nii.gz'.format( row['subject'], row['session']))) left_right_group = second_level_model.compute_contrast( first_level_contrast='eye_L - eye_R', output_type='effect_size') left_right_group.to_filename( os.path.join( results_dir, 'sub-{}_ses-{}_left_over_right_effect_size.nii.gz'.format( row['subject'], row['session'])))
"participant_id"] participants_site["participant_id"] = participants_site[ "site"] + "x" + participants_site["orig_participant_id"] participants = pd.concat((participants, participants_site)) else: participants_site = pd.DataFrame({ "site": [site], "participant_id": "participants file missing" }) participants_missing = pd.concat( (participants_missing, participants_site)) # get file df layout = BIDSLayout(site_dir) df = layout.as_data_frame() # copy_files = df[df.path.str.endswith(".nii.gz") | df.path.str.endswith("_sessions.tsv")] nii_of_int = df[df.path.str.endswith(".nii.gz") & df.modality.isin(["anat", "func"])] session_files = df[df.path.str.endswith("_sessions.tsv")] copy_files = pd.concat((nii_of_int, session_files)) def get_new_file(source_file, site_dir, out_dir, old_subject, site): new_file = os.path.relpath(source_file, site_dir).replace( old_subject, site + "x" + old_subject) dest_file = os.path.join(out_dir, new_file) dest_dir = os.path.dirname(dest_file) mkdir(dest_dir) return dest_file
def bids_data_selection( base, structural_match, functional_match, subjects, sessions, verbose=False, joint_conditions=True, ): validate = BIDSValidator() if verbose: for x in os.walk(base): print(x[0]) if validate.is_bids(x[0]): print("Is not BIDS-formatted.") else: print("Detected!") layout = BIDSLayout(base) df = layout.as_data_frame() # Not crashing if the run field is not present try: # Run is for some reason recorded as float df.loc[df['run'].notna(), 'run'] = df.loc[df['run'].notnull(), 'run'].apply(int).apply(str) except KeyError: pass # drop event files df = df[df.type != 'events'] # rm .json df = df.loc[df.path.str.contains('.nii')] # generate scan types for later df['scan_type'] = "" #print(df.path.str.startswith('task', beg=0,end=len('task'))) beg = df.path.str.find('task-') end = df.path.str.find('.') #df.loc[df.modality == 'func', 'scan_type'] = 'acq-'+df['acq']+'_task-'+ df.path.str.partition('task-')[2].str.partition('.')[0] #df.loc[df.modality == 'anat', 'scan_type'] = 'acq-'+df['acq']+'_' + df['type'] #TODO: fix task!=type if 'func' in df.columns: df.loc[df.modality == 'func', 'task'] = df.path.str.partition( 'task-')[2].str.partition('_')[0] df.loc[df.modality == 'func', 'scan_type'] = 'task-' + df['task'] + '_acq-' + df['acq'] if 'anat' in df.columns: df.loc[df.modality == 'anat', 'scan_type'] = 'acq-' + df['acq'] + '_' + df['type'] # Unclear in current BIDS specification, we refer to BOLD/CBV as modalities and func/anat as types df = df.rename(columns={'modality': 'type', 'type': 'modality'}) #TODO: The following should be collapsed into one criterion category if functional_match or structural_match: res_df = pd.DataFrame() if functional_match: _df = deepcopy(df) try: if joint_conditions: for match in functional_match.keys(): _df = _df.loc[_df[match].isin(functional_match[match])] res_df = res_df.append(_df) else: for match in structural_match.keys(): _df = filter_data(_df, match, functional_match[match]) res_df = res_df.append(_df) except: pass if structural_match: _df = deepcopy(df) try: if joint_conditions: for match in structural_match.keys(): _df = _df.loc[_df[match].isin(structural_match[match])] res_df = res_df.append(_df) else: for match in structural_match.keys(): _df = filter_data(_df, match, structural_match[match]) res_df = res_df.append(_df) except: pass df = res_df if subjects: df = filter_data(df, 'subject', subjects) if sessions: df = filter_data(df, 'session', sessions) return df
def bids_data_selection( base, structural_match, functional_match, subjects, sessions, verbose=False, joint_conditions=True, ): """ Creates a Pandas Dataframe descriptor from a BIDS datapath, optionally filtering out conditions. Parameters ---------- base : str path specifying the root directory of the BIDS data structural_match : dict or bool Dictionary specifying a whitelist of BIDS field identifiers. False if no whitelist is specified. functional_match : dict or bool Dictionary specifying a whitelist of BIDS field identifiers. False if no whitelist is specified. subjects: list or bool A list of subjects which may be present in the 'subjects' column of the created Pandas DataFrame, 'df'. False if user does not want to filter DataFrame by subjects. sessions: list or bool A list of session names which may be present in the 'sessions' column of the created Pandas DataFrame, 'df'. False if user does not want to filter DataFrame by sessions. Returns ------- df : pandas.DataFrame A Pandas DataFrame with information corresponding to the whitelisted BIDS identifiers and optionally filtered by subjects and/or sessions. """ validate = BIDSValidator() if verbose: for x in os.walk(base): print(x[0]) if validate.is_bids(x[0]): print("Is not BIDS-formatted.") else: print("Detected!") #layout = BIDSLayout(base, validate=False) layout = BIDSLayout(base, validate=False, derivatives=True) try: df = layout.as_data_frame() except AttributeError: df = layout.to_df() # Not crashing if the run field is not present try: # Run is for some reason recorded as float df.loc[df['run'].notna(), 'run'] = df.loc[df['run'].notnull(), 'run'].apply(int).apply(str) except KeyError: pass if verbose: print(df) print(df.columns) # drop event files # PyBIDS 0.6.5 and 0.10.2 compatibility try: df = df[df.type != 'events'] except AttributeError: df = df[df.suffix != 'events'] # rm .json df = df.loc[df.path.str.contains('.nii')] # generate scan types for later df['scan_type'] = "" #print(df.path.str.startswith('task', beg=0,end=len('task'))) beg = df.path.str.find('task-') end = df.path.str.find('.') #df.loc[df.modality == 'func', 'scan_type'] = 'acq-'+df['acq']+'_task-'+ df.path.str.partition('task-')[2].str.partition('.')[0] #df.loc[df.modality == 'anat', 'scan_type'] = 'acq-'+df['acq']+'_' + df['type'] #TODO: fix task!=type if 'func' in df.columns: df.loc[df.modality == 'func', 'task'] = df.path.str.partition( 'task-')[2].str.partition('_')[0] df.loc[df.modality == 'func', 'scan_type'] = 'task-' + df['task'] + '_acq-' + df['acq'] if 'anat' in df.columns: df.loc[df.modality == 'anat', 'scan_type'] = 'acq-' + df['acq'] + '_' + df['type'] # Unclear in current BIDS specification, we refer to BOLD/CBV as modalities and func/anat as types # Can be removed after Pybids 0.10.2 migration df = df.rename(columns={'modality': 'type', 'type': 'modality'}) #TODO: The following should be collapsed into one criterion category if functional_match or structural_match: res_df = pd.DataFrame() if functional_match: _df = deepcopy(df) try: if joint_conditions: for match in functional_match.keys(): _df = _df.loc[_df[match].isin(functional_match[match])] res_df = res_df.append(_df) else: for match in structural_match.keys(): _df = filter_data(_df, match, functional_match[match]) res_df = res_df.append(_df) except: pass if structural_match: _df = deepcopy(df) try: if joint_conditions: for match in structural_match.keys(): _df = _df.loc[_df[match].isin(structural_match[match])] res_df = res_df.append(_df) else: for match in structural_match.keys(): _df = filter_data(_df, match, structural_match[match]) res_df = res_df.append(_df) except: pass df = res_df if subjects: df = filter_data(df, 'subject', subjects) if sessions: df = filter_data(df, 'session', sessions) return df
def bids_data_selection(base, structural_match, functional_match, subjects, sessions, verbose=False, joint_conditions=True, ): validate = BIDSValidator() if verbose: for x in os.walk(base): print(x[0]) if validate.is_bids(x[0]): print("Is not BIDS-formatted.") else: print("Detected!") layout = BIDSLayout(base) df = layout.as_data_frame() # Run is for some reason recorded as float df.loc[df['run'].notna(),'run'] = df.loc[df['run'].notnull(),'run'].apply(int).apply(str) #df['run'] = df['run'].astype(int) # drop event files df = df[df.type != 'events'] # rm .json df = df.loc[df.path.str.contains('.nii')] # generate scan types for later df['scan_type'] = "" #print(df.path.str.startswith('task', beg=0,end=len('task'))) beg = df.path.str.find('task-') end = df.path.str.find('.') #df.loc[df.modality == 'func', 'scan_type'] = 'acq-'+df['acq']+'_task-'+ df.path.str.partition('task-')[2].str.partition('.')[0] #df.loc[df.modality == 'anat', 'scan_type'] = 'acq-'+df['acq']+'_' + df['type'] #TODO: fix task!=type if 'func' in df.columns: df.loc[df.modality == 'func', 'task'] = df.path.str.partition('task-')[2].str.partition('_')[0] df.loc[df.modality == 'func', 'scan_type'] = 'task-' + df['task'] + '_acq-'+ df['acq'] if 'anat' in df.columns: df.loc[df.modality == 'anat', 'scan_type'] = 'acq-'+df['acq'] +'_' + df['type'] #TODO: The following should be collapsed into one criterion category if functional_match or structural_match: res_df = pd.DataFrame() if functional_match: _df = deepcopy(df) try: if joint_conditions: for match in functional_match.keys(): _df = _df.loc[_df[match].isin(functional_match[match])] res_df = res_df.append(_df) else: for match in structural_match.keys(): _df = filter_data(_df, match, functional_match[match]) res_df = res_df.append(_df) except: pass if structural_match: _df = deepcopy(df) try: if joint_conditions: for match in structural_match.keys(): _df = _df.loc[_df[match].isin(structural_match[match])] res_df = res_df.append(_df) else: for match in structural_match.keys(): _df = filter_data(_df, match, structural_match[match]) res_df = res_df.append(_df) except: pass df = res_df if subjects: df = filter_data(df, 'subject', subjects) if sessions: df = filter_data(df, 'session', sessions) # Unclear in current BIDS specification, we refer to BOLD/CBV as modalities and func/anat as types df = df.rename(columns={'modality': 'type', 'type': 'modality'}) return df