def _get_func_and_confounds(fmriprep_folder, sourcedata_folder): from bids import BIDSLayout fmriprep_layout = BIDSLayout(fmriprep_folder) sourcedata_layout = BIDSLayout(sourcedata_folder) files = fmriprep_layout.get(extensions=['.nii', 'nii.gz'], modality='func', suffix='preproc') confounds = [] metadata = [] for f in files: kwargs = {} for key in ['subject', 'run', 'task', 'session']: if hasattr(f, key): kwargs[key] = getattr(f, key) c = fmriprep_layout.get(suffix='confounds', **kwargs) c = c[0] confounds.append(c) sourcedata_file = sourcedata_layout.get(modality='func', extensions='nii.gz', **kwargs) assert (len(sourcedata_file) == 1) md = sourcedata_layout.get_metadata(sourcedata_file[0].filename) metadata.append(md) return list(zip(files, confounds, metadata))
def intendedfor_nearest_fieldmap(bids_dir): """ :param bids_dir: str BIDS root directory :return: """ layout = BIDSLayout( bids_dir, absolute_paths=True, ignore=['sourcedata', 'work', 'derivatives', 'exclude']) for subj in layout.get_subjects(): # Find all JSON sidecars in bold and fmap folders bold_json = layout.get(return_type='file', extensions=['.json'], subject=subj, suffix='bold') fmap_json = layout.get(return_type='file', extensions=['.json'], subject=subj, suffix='json') print(bold_json) print(fmap_json)
def main(): parser = _build_arg_parser() args = parser.parse_args() assert_outputs_exist(parser, args, args.out_json) data = [] layout = BIDSLayout(args.in_bids, index_metadata=False) subjects = layout.get_subjects() if args.participants_label: subjects = [ nSub for nSub in args.participants_label if nSub in subjects ] for nSub in subjects: dwis = layout.get(subject=nSub, datatype='dwi', extension='nii.gz', suffix='dwi') t1s = layout.get(subject=nSub, datatype='anat', extension='nii.gz', suffix='T1w') fmaps = layout.get(subject=nSub, datatype='fmap', extension='nii.gz', suffix='epi') bvals = layout.get(subject=nSub, datatype='dwi', extension='bval', suffix='dwi') bvecs = layout.get(subject=nSub, datatype='dwi', extension='bvec', suffix='dwi') # Get associations relatives to DWIs associations = get_dwi_associations(fmaps, bvals, bvecs) # Get the data for each run of DWIs for dwi in dwis: data.append( get_data(nSub, dwi, t1s, associations, args.readout, args.clean)) if args.clean: data = [d for d in data if d] with open(args.out_json, 'w') as outfile: json.dump(data, outfile, indent=4, separators=(',', ': '), sort_keys=True) # Add trailing newline for POSIX compatibility outfile.write('\n')
def main(sourcedata, subject, session, length_epi, length_bold): print('Fixing subject {} in {} ({}. {})'.format(subject, sourcedata, length_epi, length_bold)) layout = BIDSLayout(sourcedata) LENGTH_EPI = length_epi LENGTH_BOLD = length_bold epis = layout.get(subject=subject, session=session, extensions='nii', suffix='epi') for epi in epis: epi_im = nb.load(epi.filename) if epi_im.shape[-1] == LENGTH_EPI: print('Correcting {}'.format(epi.filename)) index = np.zeros(LENGTH_EPI, dtype=bool) #index[:int(LENGTH_EPI/2)] = True index[::2] = True new_im = image.index_img(epi_im, index) print(new_im.shape) new_im.to_filename(epi.filename) bolds = layout.get(subject=subject, session=session, extensions='nii', suffix='bold') for bold in bolds: bold_im = nb.load(bold.filename) if bold_im.shape[-1] == LENGTH_BOLD: print('Correcting {}'.format(bold.filename)) index = np.zeros(LENGTH_BOLD, dtype=bool) #index[:int(LENGTH_BOLD/2)] = True index[::2] = True new_im = image.index_img(bold_im, index) print(new_im.shape) new_im.to_filename(bold.filename)
def _set_source_paths(self, source_paths): if source_paths is None: layout = BIDSLayout(self._data_dir) if self._modality == "all": self._source_paths = layout.get(scope='raw', extensions=[".nii", ".nii.gz"], return_type='file') else: self._source_paths = layout.get(scope='raw', extensions=[".nii", ".nii.gz"], suffix=self._modality, return_type='file') else: self._source_paths = source_paths
def collect_data(bids_dir, participant_label, task=None, echo=None, bids_validate=True): """ Uses pybids to retrieve the input data for a given participant >>> bids_root, _ = collect_data(str(datadir / 'ds054'), '100185', ... bids_validate=False) >>> bids_root['fmap'] # doctest: +ELLIPSIS ['.../ds054/sub-100185/fmap/sub-100185_magnitude1.nii.gz', \ '.../ds054/sub-100185/fmap/sub-100185_magnitude2.nii.gz', \ '.../ds054/sub-100185/fmap/sub-100185_phasediff.nii.gz'] >>> bids_root['bold'] # doctest: +ELLIPSIS ['.../ds054/sub-100185/func/sub-100185_task-machinegame_run-01_bold.nii.gz', \ '.../ds054/sub-100185/func/sub-100185_task-machinegame_run-02_bold.nii.gz', \ '.../ds054/sub-100185/func/sub-100185_task-machinegame_run-03_bold.nii.gz', \ '.../ds054/sub-100185/func/sub-100185_task-machinegame_run-04_bold.nii.gz', \ '.../ds054/sub-100185/func/sub-100185_task-machinegame_run-05_bold.nii.gz', \ '.../ds054/sub-100185/func/sub-100185_task-machinegame_run-06_bold.nii.gz'] >>> bids_root['sbref'] # doctest: +ELLIPSIS ['.../ds054/sub-100185/func/sub-100185_task-machinegame_run-01_sbref.nii.gz', \ '.../ds054/sub-100185/func/sub-100185_task-machinegame_run-02_sbref.nii.gz', \ '.../ds054/sub-100185/func/sub-100185_task-machinegame_run-03_sbref.nii.gz', \ '.../ds054/sub-100185/func/sub-100185_task-machinegame_run-04_sbref.nii.gz', \ '.../ds054/sub-100185/func/sub-100185_task-machinegame_run-05_sbref.nii.gz', \ '.../ds054/sub-100185/func/sub-100185_task-machinegame_run-06_sbref.nii.gz'] >>> bids_root['t1w'] # doctest: +ELLIPSIS ['.../ds054/sub-100185/anat/sub-100185_T1w.nii.gz'] >>> bids_root['t2w'] # doctest: +ELLIPSIS [] """ if isinstance(bids_dir, BIDSLayout): layout = bids_dir else: layout = BIDSLayout(str(bids_dir), validate=bids_validate) queries = { 'fmap': {'datatype': 'fmap'}, 'bold': {'datatype': 'func', 'suffix': 'bold'}, 'sbref': {'datatype': 'func', 'suffix': 'sbref'}, 'flair': {'datatype': 'anat', 'suffix': 'FLAIR'}, 't2w': {'datatype': 'anat', 'suffix': 'T2w'}, 't1w': {'datatype': 'anat', 'suffix': 'T1w'}, 'roi': {'datatype': 'anat', 'suffix': 'roi'}, } if task: queries['bold']['task'] = task if echo: queries['bold']['echo'] = echo subj_data = { dtype: sorted(layout.get(return_type='file', subject=participant_label, extensions=['nii', 'nii.gz'], **query)) for dtype, query in queries.items()} # Special case: multi-echo BOLD, grouping echos if any(['_echo-' in bold for bold in subj_data['bold']]): subj_data['bold'] = group_multiecho(subj_data['bold']) return subj_data, layout
def main(sourcedata, derivatives, subject, session, run, wf_dir): layout = BIDSLayout(sourcedata) bolds = layout.get(subject=subject, session=session, run=run, suffix='bold', return_type='file') bold = bolds for bold in bolds: print('Making reference image of {}'.format(bold)) inputnode = pe.Node(niu.IdentityInterface(fields=['bold']), name='inputnode') inputnode.inputs.bold = bolds wf = pe.Workflow(name='make_ref_{}_{}_{}'.format(subject, session, run)) wf.base_dir = wf_dir mc_wf_bold = create_motion_correction_workflow(name='mc_wf_bold', method='FSL', lightweight=True) wf.connect(inputnode, 'bold', mc_wf_bold, 'inputspec.in_files') wf.connect(inputnode, ('bold', pickfirst), mc_wf_bold, 'inputspec.which_file_is_EPI_space') mean_bold = pe.MapNode(fsl.MeanImage(dimension='T'), iterfield=['in_file'], name='mean_bold1') n4_correct = pe.MapNode(ants.N4BiasFieldCorrection(), iterfield=['input_image'], name='n4_correct') wf.connect(mean_bold, 'out_file', n4_correct, 'input_image') ds = pe.MapNode(DerivativesDataSink(out_path_base='simple_bold_ref', suffix='reference', base_directory=derivatives), iterfield=['in_file', 'source_file'], name='ds_reg_report') wf.connect(mc_wf_bold, 'outputspec.motion_corrected_files', mean_bold, 'in_file') wf.connect(n4_correct, 'output_image', ds, 'in_file') wf.connect(inputnode, 'bold', ds, 'source_file') wf.run()
def collect_data(bids_dir, participant_label, queries, filters=None, bids_validate=True): """ Uses pybids to retrieve the input data for a given participant """ if isinstance(bids_dir, BIDSLayout): layout = bids_dir else: layout = BIDSLayout(str(bids_dir), validate=bids_validate) bids_filters = filters or {} for acq, entities in bids_filters.items(): queries[acq].update(entities) subj_data = { dtype: sorted( layout.get( return_type="file", subject=participant_label, extension=["nii", "nii.gz"], **query ) ) for dtype, query in queries.items() } return subj_data, layout
def collect_data(bids_dir, participant_label, task=None, bids_validate=True): """ Uses pybids to retrieve the input data for a given participant """ if isinstance(bids_dir, BIDSLayout): layout = bids_dir else: layout = BIDSLayout(str(bids_dir), validate=bids_validate) queries = { 'fmap': {'datatype': 'fmap'}, 'sbref': {'datatype': 'func', 'suffix': 'sbref'}, 'flair': {'datatype': 'anat', 'suffix': 'FLAIR'}, 't2w': {'datatype': 'anat', 'suffix': 'T2w'}, 't1w': {'datatype': 'anat', 'suffix': 'T1w'}, 'roi': {'datatype': 'anat', 'suffix': 'roi'}, 'dwi': {'datatype': 'dwi', 'suffix': 'dwi'} } subj_data = { dtype: sorted(layout.get(return_type='file', subject=participant_label, extension=['nii', 'nii.gz'], **query)) for dtype, query in queries.items()} return subj_data, layout
def grab_eventorder(path, subject): # pybids processed = BIDSLayout(path) # Grab all event files eventfiles = processed.get(subject=subject, extension='.csv', return_type='filename') # design matrices design = pd.DataFrame(columns=['CSplus_shock', 'CSplus', 'CSminus']) for event in eventfiles: ev = pd.read_csv(event, header=None, names=['CSplus_shock', 'CSplus', 'CSminus']) design = design.append(ev, sort=False, ignore_index=True) # events are marked by 1, append their index to preserve order across runs CSplus_shock = design.index[design['CSplus_shock'] == 1] CSplus = design.index[design['CSplus'] == 1] CSminus = design.index[design['CSminus'] == 1] # group each index by CS type ev_order = [] for ty in [CSplus_shock, CSplus, CSminus]: for cs in ty: if ty is CSplus_shock: ev_order.append(['csplusshock', cs]) if ty is CSplus: ev_order.append(['csplus', cs]) if ty is CSminus: ev_order.append(['csminus', cs]) # reorder according to index event_ord = pd.DataFrame(ev_order, columns=['type', 'idx']) event_ord.sort_values(by=['idx'], inplace=True) event_ord.reset_index(inplace=True, drop=True) return event_ord, processed
def read_all_vhdr_filenames(BIDS_path): """ :return: files: list of all vhdr file paths in BIDS_path """ layout = BIDSLayout(BIDS_path) files = layout.get(extension='vhdr', return_type='filename') return files
def main(): parser = _build_args_parser() args = parser.parse_args() assert_outputs_exists(parser, args, args.output_json) data = [] layout = BIDSLayout(args.bids_folder, index_metadata=False) subjects = layout.get_subjects() for nSub in subjects: dwis = layout.get(subject=nSub, datatype='dwi', extension='nii.gz', suffix='dwi') t1s = layout.get(subject=nSub, datatype='anat', extension='nii.gz', suffix='T1w') fmaps = layout.get(subject=nSub, datatype='fmap', extension='nii.gz', suffix='epi') bvals = layout.get(subject=nSub, datatype='dwi', extension='bval', suffix='dwi') bvecs = layout.get(subject=nSub, datatype='dwi', extension='bvec', suffix='dwi') # Get associations relatives to DWIs associations = get_dwi_associations(fmaps, bvals, bvecs) # Get the data for each run of DWIs for nRun, dwi in enumerate(dwis): data.append( get_data(nSub, dwi, t1s, associations, nRun, args.readout)) with open(args.output_json, 'w') as outfile: json.dump(data, outfile, indent=4, separators=(',', ': '), sort_keys=True) # Add trailing newline for POSIX compatibility outfile.write('\n')
def main(subject, bids_folder): output_folder = op.join(bids_folder, 'derivatives', 'subject_image', f'sub-{subject}') if not op.exists(output_folder): os.makedirs(output_folder) layout = BIDSLayout(bids_folder, validate=False) t1w_3t = layout.get(subject=subject, suffix='T1w', session='3t1')[0].path t1w_7t = layout.get(subject=subject, suffix='T1w', session='7t1') t1w_7t = image.concat_imgs([im.path for im in t1w_7t]) t1w_7t = image.mean_img(t1w_7t) t2starw_7t = layout.get(subject=subject, suffix='T2starw', session='7t1', acquisition='average')[0].path print(t2starw_7t) def plot_img(im, label): if 'T2starw' in label: bounds = ((-65, 65), (-80, 80), (-20, 10)) else: bounds = ((-65, 65), (-65, 65), (-30, 60)) for bnd, display_mode in zip(bounds[2:], ['x', 'y', 'z'][2:]): print(bnd, display_mode) for coord in np.arange(bnd[0], bnd[1] + 5, 5): fn = op.join( output_folder, f'sub-{subject}_label-{label}_axis-{display_mode}_coord-{coord}.png' ) plotting.plot_img(im, cut_coords=[coord], display_mode=display_mode, cmap='gray') fig = plt.gcf() fig.set_dpi(500) fig.savefig(fn) # plot_img(t1w_3t, label='3T_t1w') plot_img(t1w_7t, label='7T_t1w')
def collect_data(bids_dir, participant_label, filters=None, bids_validate=True): """ Uses pybids to retrieve the input data for a given participant """ if isinstance(bids_dir, BIDSLayout): layout = bids_dir else: layout = BIDSLayout(str(bids_dir), validate=bids_validate) queries = { 'fmap': { 'datatype': 'fmap' }, 'sbref': { 'datatype': 'func', 'suffix': 'sbref' }, 'flair': { 'datatype': 'anat', 'suffix': 'FLAIR' }, 't2w': { 'datatype': 'anat', 'suffix': 'T2w' }, 't1w': { 'datatype': 'anat', 'suffix': 'T1w' }, 'roi': { 'datatype': 'anat', 'suffix': 'roi' }, 'dwi': { 'datatype': 'dwi', 'suffix': 'dwi' } } bids_filters = filters or {} for acq, entities in bids_filters.items(): queries[acq].update(entities) subj_data = { dtype: sorted( layout.get( return_type="file", subject=participant_label, extension=["nii", "nii.gz"], **query, )) for dtype, query in queries.items() } return subj_data, layout
def test_generate_bids_skeleton(tmp_path, test_id, json_layout, n_files, n_subjects, n_sessions): root = tmp_path / test_id generate_bids_skeleton(root, json_layout) datadesc = root / "dataset_description.json" assert datadesc.exists() assert "BIDSVersion" in datadesc.read_text() assert len([x for x in root.glob("**/*") if x.is_file()]) == n_files # ensure layout is valid layout = BIDSLayout(root) assert len(layout.get_subjects()) == n_subjects assert len(layout.get_sessions()) == n_sessions anat = layout.get(suffix="T1w", extension="nii.gz")[0] bold = layout.get(suffix="bold", extension="nii.gz")[0] assert anat.get_metadata() assert bold.get_metadata()
def reduce_sub_files(bids_dir, output_file, sub_file): df = pd.DataFrame([]) layout = BIDSLayout(bids_dir) files = layout.get(extension=sub_file) for file in [f.filename for f in files]: print(file) df_ = read_tsv(file) df = pd.concat((df, df_)) to_tsv(df, os.path.join(bids_dir, output_file))
def read_all_vhdr_filenames(BIDS_path): """list of all vhdr file paths in BIDS_path Args: BIDS_path (string): absolute path of BIDS folder Returns: list: all vhdr file in given BIDS path """ layout = BIDSLayout(BIDS_path) files = layout.get(extension='vhdr', return_type='filename') return files
def get_matching_images( image_to_match: BIDSImageFile, bids_dataset: BIDSLayout, matching_entities: list = None, required_entities: dict = None, ): """ Returns a list of images from the BIDS dataset that has the specified required_entities and has the same value for entities listed in matching_entities as the image_to_match. Example: for an image "sub-123_ses-1_T1w.nii" with matching_entities ['ses'] and required_entities {'suffix': 'FLAIR'}, the image "sub-123_ses-1_FLAIR.nii" would match, but "sub-123_ses-2_FLAIR.nii" would not. Parameters ---------- required_entities: dict Entity-value dictionary that are required. matching_entities: list List of entities that must match, if present, between the previous image and the one to fetch. image_to_match: BIDSImageFile Image to use as reference for matching_entities. bids_dataset: BIDSLayout BIDS dataset from which to fetch the new image. Returns ------- list [BIDSImageFile] BIDS image file matching the input specifications. Empty if there are no matches. """ if matching_entities is None: matching_entities = [] if required_entities is None: required_entities = {} ents_to_match = {} im_entities = image_to_match.get_entities() for k in matching_entities: if k in im_entities.keys(): ents_to_match[k] = im_entities[k] potential_matches = bids_dataset.get(**required_entities, **ents_to_match) # Go through each potential image; remove those that don't match potential_idx = [] for idx, potential_im in enumerate(potential_matches): potential_im_ents = potential_im.get_entities() for entity, value in ents_to_match.items(): if (entity not in potential_im_ents.keys() or value != potential_im_ents[entity]): continue else: if potential_im != image_to_match: potential_idx.append(idx) return [potential_matches[i] for i in potential_idx]
def main(args): analysis_params = json.load(open(args.config_file)) epleptic_windows = pd.read_csv(analysis_params['epleptic_windows_file']) epleptic_windows[['Start', 'End']] = (epleptic_windows[['Start', 'End']] * 1000).astype(int) epleptic_windows = epleptic_windows.groupby('subject_number') root_path = os.path.join(analysis_params['data_path']) layout = BIDSLayout(root_path) for subject in layout.get(target='subject', extension='edf'): subject_code = int(subject.entities['subject']) result_fname = os.path.join(analysis_params['output_path'], 'sub-{}_spectrum.pickle'.format(subject.entities['subject'])) montage_filename = os.path.join(subject.dirname, 'sub-{}_montage.tcsv'.format(subject.entities['subject'])) electrodes_filename = os.path.join(subject.dirname, 'sub-{}_electrodes.tcsv'.format(subject.entities['subject'])) data_filename = subject.path if not(os.path.exists(montage_filename) and os.path.exists(electrodes_filename) and os.path.exists(data_filename)): print('Cannot find data for subject {}'.format(subject.entities['subject'])) continue bipo = make_bipolar(data_filename, montage_filename, analysis_params['lowpass_filter'], analysis_params['pure_bipolar']) ref_mask = create_reference_mask(bipo).astype(int) electrodes_distance = get_electrode_distance(bipo.ch_names, electrodes_filename) if subject_code in epleptic_windows.groups: subject_ez_windows = epleptic_windows.get_group(subject_code) subject_ez_samples_mask = get_ez_samples_mask(subject_ez_windows, bipo._data) else: subject_ez_samples_mask = np.ones(bipo._data.shape[1], dtype=bool) n_chans = len(bipo.ch_names) frequencies = get_frequencies() cplv_spectrum = np.zeros((len(frequencies), n_chans, n_chans), dtype=np.complex) cplv_surrogate = np.zeros((len(frequencies), n_chans, n_chans), dtype=np.complex) for freq_idx, frequency in enumerate(tqdm.tqdm(frequencies, leave=False, desc='Subject {}'.format(subject.entities['subject']))): freq_cplv, freq_cplv_surr = routine_cpu(bipo, bipo.info['sfreq'], frequency, subject_ez_samples_mask) cplv_spectrum[freq_idx] = freq_cplv*ref_mask cplv_surrogate[freq_idx] = freq_cplv_surr*ref_mask res = {'frequencies': frequencies, 'cplv_spectrum': cplv_spectrum, 'surrogate_spectrum': cplv_surrogate, 'reference_mask': ref_mask, 'electrodes_distance': electrodes_distance, 'analysis_parameters': analysis_params} pickle.dump(res, open(result_fname, 'wb'))
def main(): parser = create_parser() args = parser.parse_args() n_cpus = args.jobs[0] if args.bids: if (args.bids_sub is None): # if bids folder is provided but no subject, raise error parser.error("--bids requires --bids_sub") else: # both bids and bids_sub layout = BIDSLayout(args.bids[0]) if args.bids_sub[0] not in layout.get_subjects(): parser.error("Unknown subject, not in BIDS structure") else: f = layout.get(subject=args.bids_sub[0], extension='gii.gz')[0] nib_surf, vertices, faces = io.open_gifti_surf(f) # hoping this f contains the file. TODO else: nib_surf, vertices, faces = io.open_gifti_surf(args.surface[0]) nib = nibabel.load(args.data[0]) if len(nib.darrays) > 1: cifti = np.array([n.data for n in nib.darrays]).transpose() else: cifti = nib.darrays[0].data if args.full_brain: print("Running full brain analyses") if args.mask is None: print("A mask file must be provided through the --label flag. See --help") quit() _, labels = io.open_gifti(args.mask[0]) cort_index = np.array(labels, np.bool) Z = np.array(cort_index, dtype=np.int) result = vb.vb_cluster(vertices, faces, n_cpus, cifti, Z, args.norm[0], args.output[0] + "." + args.norm[0], nib_surf) elif args.clusters is None: print("Running searchlight analyses") if args.mask is None: print("A mask file must be provided through the --label flag. See --help") quit() # Read labels _, labels = io.open_gifti(args.mask[0]) cort_index = np.array(labels, np.bool) result = vb.vb_index(vertices, faces, n_cpus, cifti, args.norm[0], cort_index, args.output[0] + "." + args.norm[0], nib_surf) else: print("Running ROI analyses") nib, Z = io.open_gifti(args.clusters[0]) Z = np.array(Z, dtype=np.int) result = vb.vb_cluster(vertices, faces, n_cpus, cifti, Z, args.norm[0], args.output[0] + "." + args.norm[0], nib_surf)
def get_entity_files(layout: BIDSLayout, include_no_aroma: bool, include_aroma: bool, entity: dict) -> tuple: """ Checks if all required files are present for single entity defined by subject, session and task labels. If include_aroma is True also checks for AROMA file. Note that session argument can be undefined. Args: Returns: (missing: Union[bool, dict], dict) """ filter_fmri = { 'extension': ['nii', 'nii.gz'], 'suffix': 'bold', 'desc': 'preproc', 'space': 'MNI152NLin2009cAsym' } filter_fmri_aroma = { 'extension': ['nii', 'nii.gz'], 'suffix': 'bold', 'desc': 'smoothAROMAnonaggr', # 'space': 'MNI152NLin2009cAsym' } filter_conf = { 'extension': 'tsv', 'suffix': ['regressors', 'timeseries'], 'desc': 'confounds', } filter_conf_json = { 'extension': 'json', 'suffix': ['regressors', 'timeseries'], 'desc': 'confounds', } filters_names = ['conf_raw', 'conf_json'] filters = [filter_conf, filter_conf_json] if include_no_aroma: filters.append(filter_fmri) filters_names.append('fmri_prep') if include_aroma: filters.append(filter_fmri_aroma) filters_names.append('fmri_prep_aroma') entity_files = {} for filter, filter_name in zip(filters, filters_names): files = layout.get(**entity, **filter) if len(files) != 1: return filter, entity_files entity_files[filter_name] = files[0] return False, entity_files
def get_subject_details(layout: BIDSLayout): """ Learn more about our subjects in the experiment """ # Get the subject questionaire questionaires = layout.get( extension='json', suffix='participants', ) assert len(questionaires) == 1, "Bad data read" questionaire = questionaires[0] # Get the subject answers answers = layout.get( extension='tsv', suffix='participants', ) assert len(answers) == 1, "Bad data read" answers = answers[0] return questionaire, answers
def collect_data( bids_dir: Union[BIDSLayout, Path, str], participant_label: str, dwi_identifier: dict, fmap_identifier: dict, t1w_identifier: dict, t2w_identifier: dict, bids_validate: bool = True, ): """ Collects processing-relevant files from a BIDS dataset Parameters ---------- bids_dir : Union[BIDSLayout, Path, str] Either BIDSLayout or path-like object representing an existing BIDS-compatible dataset. participant_label : str String representing a subject existing within *bids_dir*. bids_validate : bool, optional Whether to validate *bids_dir*`s compatibility with the BIDS format, by default True Returns ------- [type] [description] """ if isinstance(bids_dir, BIDSLayout): layout = bids_dir else: layout = BIDSLayout(str(bids_dir), bids_validate) queries = { "fmap": {"datatype": "fmap", **fmap_identifier}, "dwi": {"datatype": "dwi", "suffix": "dwi", **dwi_identifier}, "t2w": {"datatype": "anat", "suffix": "T2w", **t2w_identifier}, "t1w": {"datatype": "anat", "suffix": "T1w", **t1w_identifier}, } subj_data = { dtype: sorted( layout.get( return_type="file", subject=participant_label, extension=["nii", "nii.gz"], **query, ) ) for dtype, query in queries.items() } return subj_data, layout, queries
def bids_report(modality, data, ignore): print(data) print(ignore) layout = BIDSLayout(root=data, ignore=ignore) list_bids = layout.get(session=1, datatype=modality, extension='nii.gz') institutions = dict() all_keys = ['Modality', 'MagneticFieldStrength', 'ImagingFrequency', 'Manufacturer', 'ManufacturersModelName', 'InstitutionName', 'InstitutionalDepartmentName', 'InstitutionAddress', 'DeviceSerialNumber', 'StationName', 'PatientPosition', 'ProcedureStepDescription', 'SoftwareVersions', 'MRAcquisitionType', 'SeriesDescription', 'ProtocolName', 'ScanningSequence', 'SequenceVariant', 'ScanOptions', 'SequenceName', 'ImageType', 'SeriesNumber', 'AcquisitionTime', 'AcquisitionNumber', 'SliceThickness', 'SpacingBetweenSlices', 'SAR', 'EchoTime', 'RepetitionTime', 'FlipAngle', 'PartialFourier', 'BaseResolution', 'ShimSetting', 'TxRefAmp', 'PhaseResolution', 'ReceiveCoilName', 'CoilString', 'PulseSequenceDetails', 'RefLinesPE', 'ConsistencyInfo', 'PercentPhaseFOV', 'EchoTrainLength', 'PhaseEncodingSteps', 'AcquisitionMatrixPE', 'ReconMatrixPE', 'ParallelReductionFactorInPlane', 'PixelBandwidth', 'DwellTime', 'PhaseEncodingDirection', 'SliceTiming', 'ImageOrientationPatientDICOM', 'InPlanePhaseEncodingDirectionDICOM', 'ConversionSoftware', 'ConversionSoftwareVersion', 'Dcm2bidsVersion'] for bids_file in list_bids: print("####") niftii_file = bids_file.path metadata = bids_file.get_metadata() field_strength = "None" voxel_size = "None" echo_time = "None" repetition_time = "None" flip_angle = "None" if "InstitutionName" in metadata.keys(): #print(metadata["InstitutionName"]) if (metadata["InstitutionName"] == "IUGM"): print("-----------------------------") if "MagneticFieldStrength" in metadata.keys(): field_strength = metadata["MagneticFieldStrength"] if "SpacingBetweenSlices" in metadata.keys(): voxel_size = metadata["SpacingBetweenSlices"] if "EchoTime" in metadata.keys(): echo_time = metadata["EchoTime"] if "RepetitionTime" in metadata.keys(): repetition_time = metadata["RepetitionTime"] if "FlipAngle" in metadata.keys(): flip_angle = metadata["FlipAngle"] hdr = nib.load(niftii_file).header n_vol = hdr.get_data_shape()[-1] resolution = str(hdr.get_data_shape()[0]) + "x" + str(hdr.get_data_shape()[0]) slice_order = hdr.get_value_label('slice_code') scan_time = (n_vol * float(repetition_time))/60 institutions[metadata["InstitutionName"]] = {"field_strength":field_strength, "voxel_size": voxel_size, "resolution": resolution, "echo_time": echo_time, "repetition_time": repetition_time, "flip_angle": flip_angle, "num_vol": n_vol, "matrix_size": resolution, "slice_order": slice_order, "scan_time": scan_time, "filepath": niftii_file} title = ".. csv-table:: f-MRI sites parameters\n" header = " :header: \"Site\", \"Field Strength (T)\", \"Voxel size (mm3)\", \"Matrix size\", \"Flip Angle\", \"TE (s)\", \"TR (s)\", \"Volumes\", \"Scan slices order\", \"Scan time (min)\"\n" options = " :widths: 30, 10, 10, 10, 10, 10, 10, 10, 10, 10\n" content = "" for instit in institutions.keys(): dic = institutions[instit] content += " \"{}\", {}, {}, {}, {}, {}, {}, {}, \"{}\", {:.2f}\n".format(instit, dic["field_strength"], dic["voxel_size"], dic["matrix_size"], dic["flip_angle"], dic["echo_time"], dic["repetition_time"], dic["num_vol"], dic["slice_order"], dic["scan_time"]) print(title + header + options + "\n" + content)
class BidsDirectory(object): """BIDS directory querying, currently a wrapper for pybids.BIDSLayout """ def __init__(self, directory): self.layout = BIDSLayout(directory, derivatives=True) def get_preprocessed_subjects_ids(self): return self.layout.get(return_type='id', target='subject') def get_tasks_for_subject(self, subject): return self.layout.get(subject=subject, return_type='id', target='task') def get_sessions_for_task_and_subject(self, task, subject): return self.layout.get(subject=subject, task=task, return_type='id', target='session') def get_filepaths_bold_runs(self, subject, task, session): return self.layout.get(subject=subject, task=task, session=session, suffix='preproc', return_type='file') def get_filepaths_event_runs(self, subject, task, session): return self.layout.get(subject=subject, task=task, session=session, suffix='events', return_type='file') def get_metas_bold_runs(self, subject, task, session): runs = self.layout.get( subject=subject, task=task, session=session, suffix='bold' # get metadata from raw!? ) return [r.metadata for r in runs] def subject_id_from_number(self, sub_num): ids = self.layout.get(return_type='id', target='subject') for nzeros in [0, 2]: candidate = str(sub_num).zfill(nzeros) if candidate in ids: return candidate
def collect_data(bids_dir, participant_label, bids_validate=True): """Replacement for niworkflows' version.""" if isinstance(bids_dir, BIDSLayout): layout = bids_dir else: layout = BIDSLayout(str(bids_dir), validate=bids_validate) queries = { "fmap": { "datatype": "fmap" }, "dwi": { "datatype": "dwi", "suffix": "dwi" }, "flair": { "datatype": "anat", "suffix": "FLAIR" }, "t2w": { "datatype": "anat", "suffix": "T2w" }, "t1w": { "datatype": "anat", "suffix": "T1w" }, "roi": { "datatype": "anat", "suffix": "roi" }, } subj_data = { dtype: sorted( layout.get(return_type="file", subject=participant_label, extension=["nii", "nii.gz"], **query)) for dtype, query in queries.items() } return subj_data, layout
def get_experiment_details(layout: BIDSLayout): """ Learn more about our experiment """ description = layout.get_dataset_description() # layout.get_entities() # Get the metadata metadata = layout.get( extension='json', suffix='bold', task='passiveimageviewing', ) assert len(metadata) == 1, "Bad data read" metadata = metadata[0] # Get the subject answers return description, metadata
def get_bids_onsets(bids_folder): """ Get event onsets from a BIDS folder in a nideconv-ready format. Parameters ---------- bids_folder: str Folder containing fMRI dataset according to BIDS-standard. Returns ------- onsets: DataFrame Dataframe containing onsets, with subject and potentially session, task and run as indices. """ layout = BIDSLayout(bids_folder) events = layout.get(type='events', extensions='tsv') onsets = [] index_keys = [] for event in events: onsets_ = pd.read_table(event.filename) for key in ['subject', 'run', 'task', 'session']: if hasattr(event, key): onsets_[key] = getattr(event, key) if key not in index_keys: index_keys.append(key) onsets.append(onsets_) onsets = pd.concat(onsets).set_index(index_keys) return onsets
def main(bids_dir, subject, session): print(subject, bids_dir) layout = BIDSLayout(bids_dir) bolds = layout.get(subject=subject, session=session, extensions='nii', suffix='bold') for bold in bolds: print(bold.filename) im = nb.load(bold.filename) zooms = list(im.header.get_zooms()) units = list(im.header.get_xyzt_units()) if (zooms[-1] != layout.get_metadata(bold.filename)['RepetitionTime']) or (units[1] != 's'): im.header.set_xyzt_units(t=8) # 8 = seconds zooms[-1] = layout.get_metadata(bold.filename)['RepetitionTime'] im.header.set_zooms(zooms) print(im.get_filename()) nb.save(im, im.get_filename()+'.new.nii') shutil.move(bold.filename+'.new.nii', bold.filename)
def collect_data(bids_dir, participant_label, bids_validate=True): """Replacement for niworkflows' version.""" if isinstance(bids_dir, BIDSLayout): layout = bids_dir else: layout = BIDSLayout(str(bids_dir), validate=bids_validate) queries = { 'fmap': {'datatype': 'fmap'}, 'dwi': {'datatype': 'dwi', 'suffix': 'dwi'}, 'flair': {'datatype': 'anat', 'suffix': 'FLAIR'}, 't2w': {'datatype': 'anat', 'suffix': 'T2w'}, 't1w': {'datatype': 'anat', 'suffix': 'T1w'}, 'roi': {'datatype': 'anat', 'suffix': 'roi'}, } subj_data = { dtype: sorted(layout.get(return_type='file', subject=participant_label, extension=['nii', 'nii.gz'], **query)) for dtype, query in queries.items()} return subj_data, layout
def get_subject_sessions(bids_set: BIDSLayout, subject: str): ''' Returns the list of sessions in a BIDS directory for a given subject. Parameters ---------- bids_set : BIDSLayout BIDS dataset to check subject : str Subject ID to fetch sessions froms Returns ------- list [str] List of sessions that a particular subject has. ''' file_list = bids_set.get(subject=subject) session_list = set() for f in file_list: ents = f.entities if ('session' in ents): sess = f.entities['session'] session_list.add(sess) return list(session_list)