def test_index_associated_false(testvalidator): testvalidator = BIDSValidator(index_associated=False) target_list = [ "/code/", "/derivatives/", "/sourcedata/", "/stimuli/", "/.git/" ] for item in target_list: result = testvalidator.is_associated_data(item) assert not result
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 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 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 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 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
def testvalidator(): return BIDSValidator()