Skip to content

Commit 3d21ad1

Browse files
committed
enh: compressing more into functions
1 parent 7b501ed commit 3d21ad1

File tree

1 file changed

+49
-66
lines changed

1 file changed

+49
-66
lines changed

examples/rsfmri_preprocessing.py

Lines changed: 49 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,15 @@
3131
from nipype.utils.filemanip import filename_to_list
3232

3333
import numpy as np
34+
import scipy as sp
35+
import nibabel as nb
3436

37+
imports = ['import os',
38+
'import nibabel as nb',
39+
'import numpy as np',
40+
'import scipy as sp',
41+
'from nipype.utils.filemanip import filename_to_list'
42+
]
3543

3644
def median(in_files):
3745
"""Computes an average of the median of each realigned timeseries
@@ -47,10 +55,6 @@ def median(in_files):
4755
out_file: a 3D Nifti file
4856
"""
4957

50-
import os
51-
import nibabel as nb
52-
import numpy as np
53-
from nipype.utils.filemanip import filename_to_list
5458
average = None
5559
for idx, filename in enumerate(filename_to_list(in_files)):
5660
img = nb.load(filename)
@@ -98,9 +102,6 @@ def motion_regressors(motion_params, order=2, derivatives=2):
98102
99103
motion + d(motion)/dt + d2(motion)/dt2 (linear + quadratic)
100104
"""
101-
from nipype.utils.filemanip import filename_to_list
102-
import numpy as np
103-
import os
104105
out_files = []
105106
for idx, filename in enumerate(filename_to_list(motion_params)):
106107
params = np.genfromtxt(filename)
@@ -137,9 +138,6 @@ def build_filter1(motion_params, comp_norm, outliers):
137138
components_file: a text file containing all the regressors
138139
139140
"""
140-
from nipype.utils.filemanip import filename_to_list
141-
import numpy as np
142-
import os
143141
out_files = []
144142
for idx, filename in enumerate(filename_to_list(motion_params)):
145143
params = np.genfromtxt(filename)
@@ -172,14 +170,8 @@ def extract_noise_components(realigned_file, mask_file, num_components=6):
172170
-------
173171
components_file: a text file containing the noise components
174172
"""
175-
176-
import os
177-
from nibabel import load
178-
import numpy as np
179-
import scipy as sp
180-
181-
imgseries = load(realigned_file)
182-
noise_mask = load(mask_file)
173+
imgseries = nb.load(realigned_file)
174+
noise_mask = nb.load(mask_file)
183175
voxel_timecourses = imgseries.get_data()[np.nonzero(noise_mask.get_data())]
184176
voxel_timecourses = voxel_timecourses.byteswap().newbyteorder()
185177
voxel_timecourses[np.isnan(np.sum(voxel_timecourses, axis=1)), :] = 0
@@ -206,9 +198,6 @@ def extract_subrois(timeseries_file, label_file, indices):
206198
The first four columns are: freesurfer index, i, j, k positions in the
207199
label file
208200
"""
209-
import os
210-
import nibabel as nb
211-
import numpy as np
212201
img = nb.load(timeseries_file)
213202
data = img.get_data()
214203
roiimg = nb.load(label_file)
@@ -228,11 +217,8 @@ def extract_subrois(timeseries_file, label_file, indices):
228217
def combine_hemi(left, right):
229218
"""Combine left and right hemisphere time series into a single text file
230219
"""
231-
import os
232-
from nibabel import load
233-
import numpy as np
234-
lh_data = load(left).get_data()
235-
rh_data = load(right).get_data()
220+
lh_data = nb.load(left).get_data()
221+
rh_data = nb.load(right).get_data()
236222

237223
indices = np.vstack((1000000 + np.arange(0, lh_data.shape[0])[:, None],
238224
2000000 + np.arange(0, rh_data.shape[0])[:, None]))
@@ -675,6 +661,42 @@ def create_workflow(files,
675661
datasink2, 'resting.parcellations.grayo.@surface')
676662
return wf
677663

664+
665+
"""
666+
Creates the full workflow including getting information from dicom files
667+
"""
668+
669+
def create_resting_workflow(args):
670+
TR = args.TR
671+
slice_times = args.slice_times
672+
slice_thickness = None
673+
if args.dicom_file:
674+
TR, slice_times, slice_thickness = get_info(args.dicom_file)
675+
676+
if slice_thickness is None:
677+
from nibabel import load
678+
img = load(args.files[0])
679+
slice_thickness = max(img.get_header().get_zooms()[:3])
680+
681+
kwargs = dict(files =[os.path.abspath(filename) for
682+
filename in args.files],
683+
subject_id=args.subject_id,
684+
n_vol=args.n_vol,
685+
despike=args.despike,
686+
TR=TR,
687+
slice_times=slice_times,
688+
slice_thickness=slice_thickness,
689+
lowpass_freq=args.lowpass_freq,
690+
highpass_freq=args.highpass_freq,
691+
sink_directory=os.path.abspath(args.sink))
692+
if args.field_maps:
693+
kwargs.update(**dict(fieldmap_images=args.field_maps,
694+
FM_TEdiff=args.TE_diff,
695+
FM_echo_spacing=args.echo_spacing,
696+
FM_sigma=args.sigma))
697+
wf = create_workflow(**kwargs)
698+
return wf
699+
678700
if __name__ == "__main__":
679701
from argparse import ArgumentParser
680702
parser = ArgumentParser(description=__doc__)
@@ -714,52 +736,13 @@ def create_workflow(files,
714736
help="field map sigma value")
715737
args = parser.parse_args()
716738

717-
TR = args.TR
718-
slice_times = args.slice_times
719-
slice_thickness = None
720-
if args.dicom_file:
721-
TR, slice_times, slice_thickness = get_info(args.dicom_file)
722-
723-
if slice_thickness is None:
724-
from nibabel import load
725-
img = load(args.files[0])
726-
slice_thickness = max(img.get_header().get_zooms()[:3])
727-
728-
if args.field_maps:
729-
wf = create_workflow([os.path.abspath(filename) for
730-
filename in args.files],
731-
subject_id=args.subject_id,
732-
n_vol=args.n_vol,
733-
despike=args.despike,
734-
TR=TR,
735-
slice_times=slice_times,
736-
slice_thickness=slice_thickness,
737-
lowpass_freq=args.lowpass_freq,
738-
highpass_freq=args.highpass_freq,
739-
sink_directory=os.path.abspath(args.sink),
740-
fieldmap_images=args.field_maps,
741-
FM_TEdiff=args.TE_diff,
742-
FM_echo_spacing=args.echo_spacing,
743-
FM_sigma=args.sigma)
744-
else:
745-
wf = create_workflow([os.path.abspath(filename) for
746-
filename in args.files],
747-
subject_id=args.subject_id,
748-
n_vol=args.n_vol,
749-
despike=args.despike,
750-
TR=TR,
751-
slice_times=slice_times,
752-
slice_thickness=slice_thickness,
753-
lowpass_freq=args.lowpass_freq,
754-
highpass_freq=args.highpass_freq,
755-
sink_directory=os.path.abspath(args.sink))
739+
wf = create_resting_workflow(args)
756740

757741
if args.work_dir:
758742
work_dir = os.path.abspath(args.work_dir)
759743
else:
760744
work_dir = os.getcwd()
761745

762-
wf.config['execution'].update(**{'remove_unnecessary_outputs': False})
763746
wf.base_dir = work_dir
764747
exec args.plugin_args
765748
wf.run(**plugin_args)

0 commit comments

Comments
 (0)