|
4 | 4 | """
|
5 | 5 | from glob import glob
|
6 | 6 | import os
|
| 7 | +import os.path as op |
| 8 | +import bids |
7 | 9 |
|
8 |
| -def get_BIDS_files(subject_id, bids_input_directory): |
| 10 | + |
| 11 | +def get_bids_subject_input_files(subject_id, bids_input_directory): |
| 12 | + """ |
| 13 | + Function to return the needed files for dmriprep based on subject id and a bids directory. |
| 14 | +
|
| 15 | + :param subject_id: string |
| 16 | + :param bids_input_directory: string to bids dir |
| 17 | + :return: dict of inputs |
| 18 | + """ |
| 19 | + layout = bids.layout.BIDSLayout(bids_input_directory) |
| 20 | + subjects = layout.get_subjects() |
| 21 | + assert subject_id in subjects, "subject {} is not in the bids folder".format(subject_id) |
| 22 | + |
| 23 | + ap_file = layout.get(subject=subject_id, fmap="epi", modality="fmap", dir="AP") |
| 24 | + assert len(ap_file) == 1, 'found {} ap fieldmap files and we need just 1'.format(len(ap_file)) |
| 25 | + |
| 26 | + pa_file = layout.get(subject=subject_id, fmap="epi", modality="fmap", dir="PA") |
| 27 | + assert len(pa_file) == 1, 'found {} pa fieldmap files and we need just 1'.format(len(pa_file)) |
| 28 | + |
| 29 | + dwi_files = layout.get(subject=subject_id, modality="dwi") |
| 30 | + valid_dwi_files = [] |
| 31 | + |
| 32 | + for d in dwi_files: |
| 33 | + if d.filename.startswith(op.abspath(op.join(bids_input_directory, 'sub-' + subject_id))): |
| 34 | + valid_dwi_files.append(d.filename) |
| 35 | + |
| 36 | + dwi_file = [d for d in valid_dwi_files if d.endswith('.nii.gz') and not "TRACE" in d] |
| 37 | + assert len(dwi_file) == 1, 'found {} dwi files and we need just 1'.format(len(dwi_file)) |
| 38 | + |
| 39 | + bval_file = [d for d in valid_dwi_files if d.endswith('.bval')] |
| 40 | + assert len(bval_file) == 1, 'found {} bval files and we need just 1'.format(len(bval_file)) |
| 41 | + |
| 42 | + bvec_file = [d for d in valid_dwi_files if d.endswith('.bvec')] |
| 43 | + assert len(bvec_file) == 1, 'found {} bvec files and we need just 1'.format(len(bvec_file)) |
| 44 | + |
| 45 | + subjects_dir = op.join(bids_input_directory, 'derivatives', 'sub-'+subject_id) |
| 46 | + |
| 47 | + if not op.exists(op.join(subjects_dir, 'freesurfer')): |
| 48 | + raise NotImplementedError('we have not yet implemented a version of dmriprep that runs freesurfer for you.' |
| 49 | + 'please run freesurfer and try again' |
| 50 | + ) |
| 51 | + |
| 52 | + outputs = dict(subject_id="sub-"+subject_id, |
| 53 | + dwi_file=dwi_file[0], |
| 54 | + dwi_file_AP=ap_file[0].filename, |
| 55 | + dwi_file_PA=pa_file[0].filename, |
| 56 | + bvec_file=bvec_file[0], |
| 57 | + bval_file=bval_file[0], |
| 58 | + subjects_dir=op.abspath(subjects_dir)) |
| 59 | + return outputs |
| 60 | + |
| 61 | + |
| 62 | +def get_bids_files(subject_id, bids_input_directory): |
| 63 | + """ |
| 64 | + subject to get all bids files for am optional subject id and bids dir. if subject id is blank then all subjects |
| 65 | + are used. |
| 66 | + :param subject_id: |
| 67 | + :param bids_input_directory: |
| 68 | + :return: |
| 69 | + """ |
9 | 70 | if not subject_id:
|
10 |
| - subjects = glob(os.path.join(bids_input_directory, "*")) |
| 71 | + subjects = [s.split("/")[-1].replace("sub-", "") for s in glob(os.path.join(bids_input_directory, "sub-*"))] |
| 72 | + assert len(subjects), "No subject files found in bids directory" |
| 73 | + return [get_bids_subject_input_files(sub, bids_input_directory) for sub in subjects] |
| 74 | + else: |
| 75 | + return [get_bids_subject_input_files(subject_id, bids_input_directory)] |
0 commit comments