Skip to content
This repository was archived by the owner on Dec 27, 2022. It is now read-only.

Commit c2829ff

Browse files
committed
Merge remote-tracking branch 'origin/master'
2 parents ed09f75 + 6b6d8a0 commit c2829ff

File tree

4 files changed

+79
-13
lines changed

4 files changed

+79
-13
lines changed

dmriprep/cli.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,13 @@ def main(participant_label, bids_dir, output_dir, analysis_level="participant"):
4141
if analysis_level is not 'participant':
4242
raise NotImplementedError('The only valid analysis level for dmriprep is participant at the moment.')
4343

44-
inputs = io.get_BIDS_files(participant_label, bids_dir)
44+
inputs = io.get_bids_files(participant_label, bids_dir)
45+
46+
for subject_inputs in inputs:
47+
run.run_dmriprep_pe(**subject_inputs,
48+
working_dir=os.path.join(output_dir, 'scratch'),
49+
out_dir=output_dir)
4550

46-
# for subject_id, subject_inputs in inputs:
47-
# run.run_dmriprep_pe(**subject_inputs,
48-
# working_dir=os.path.join(output_dir, 'scratch'),
49-
# out_dir=output_dir)
50-
click.echo('doing stuff {} {} {} {}'.format(participant_label, bids_dir, output_dir, analysis_level))
5151
return 0
5252

5353

dmriprep/io.py

Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,72 @@
44
"""
55
from glob import glob
66
import os
7+
import os.path as op
8+
import bids
79

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+
"""
970
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)]

dmriprep/run.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ def rang(b):
227227
return dmri_corrected, bvec_rotated, art_file, motion_file, outlier_file
228228

229229

230-
def run_dmriprep_pe(dwi_file, dwi_file_AP, dwi_file_PA, bvec_file, bval_file,
230+
def run_dmriprep_pe(subject_id, dwi_file, dwi_file_AP, dwi_file_PA, bvec_file, bval_file,
231231
subjects_dir, working_dir, out_dir):
232232
"""
233233
This assumes that there are scans with phase-encode directions AP/PA for
@@ -245,7 +245,7 @@ def run_dmriprep_pe(dwi_file, dwi_file_AP, dwi_file_PA, bvec_file, bval_file,
245245

246246
# some bookkeeping (getting the filename, gettings the BIDS subject name)
247247
dwi_fname = op.split(dwi_file)[1].split(".nii.gz")[0]
248-
bids_sub_name = dwi_fname.split("_")[0]
248+
bids_sub_name = subject_id
249249
assert bids_sub_name.startswith("sub-")
250250

251251
# Grab the preprocessing all_fsl_pipeline
@@ -256,10 +256,9 @@ def run_dmriprep_pe(dwi_file, dwi_file_AP, dwi_file_PA, bvec_file, bval_file,
256256

257257
# initialize an overall workflow
258258
wf = pe.Workflow(name="dmriprep")
259-
wf.base_dir = op.abspath(working_dir)
259+
wf.base_dir = op.join(op.abspath(working_dir), subject_id)
260260

261261
prep.inputs.inputnode.in_file = dwi_file
262-
# prep.inputs.inputnode.alt_file = dwi_file_PA
263262
prep.inputs.inputnode.in_bvec = bvec_file
264263
prep.inputs.inputnode.in_bval = bval_file
265264
eddy = prep.get_node('fsl_eddy')

docker/environment.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,7 @@ dependencies:
55
- python=3.6
66
- dipy
77
- boto3
8+
- graphviz
89
- pip:
9-
- "--editable=git+https://github.com/nipy/nipype@fa0a101fec2d010dcb68910000f66d7c64e5d03e#egg=nipype"
10+
- "--editable=git+https://github.com/nipy/nipype@fa0a101fec2d010dcb68910000f66d7c64e5d03e#egg=nipype"
11+
- bids

0 commit comments

Comments
 (0)