Skip to content

Commit 6682859

Browse files
committed
Merge pull request #649 from satra/fix/nipydev
FIX: for nipy dev version and other example improvements
2 parents c274d4a + 719ed94 commit 6682859

File tree

2 files changed

+52
-23
lines changed

2 files changed

+52
-23
lines changed

examples/rsfmri_preprocessing.py

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
#!/usr/bin/env python
22
"""
3+
================================================================
4+
rsfMRI: AFNI, ANTS, DicomStack, FreeSurfer, FSL, Nipy, aCompCorr
5+
================================================================
6+
7+
38
A preprocessing workflow for Siemens resting state data.
49
510
This workflow makes use of:
@@ -12,15 +17,42 @@
1217
- FSL
1318
- NiPy
1419
15-
For example:
20+
For example::
1621
17-
python rsfmri_preprocessing.py -d /data/12345-34-1.dcm -f /data/Resting.nii
22+
python rsfmri_preprocessing.py -d /data/12345-34-1.dcm -f /data/Resting.nii
1823
-s subj001 -n 2 --despike -o output
1924
-p PBS --plugin_args "dict(qsub_args='-q many')"
25+
26+
This workflow takes resting timeseries and a Siemens dicom file corresponding
27+
to it and preprocesses it to produce timeseries coordinates or grayordinates.
28+
29+
This workflow also requires 2mm subcortical atlas and templates that are
30+
available from:
31+
32+
http://mindboggle.info/data.html
33+
34+
specifically the 2mm versions of:
35+
36+
- `Joint Fusion Atlas <http://mindboggle.info/data/atlases/jointfusion/OASIS-TRT-20_DKT31_CMA_jointfusion_labels_in_MNI152_2mm.nii.gz>`_
37+
- `MNI template <http://mindboggle.info/data/templates/ants/OASIS-TRT-20_template_in_MNI152_2mm.nii.gz>`_
38+
39+
The 2mm version was generated with::
40+
41+
>>> from nipype import freesurfer as fs
42+
>>> rs = fs.Resample()
43+
>>> rs.inputs.in_file = 'OASIS-TRT-20_DKT31_CMA_jointfusion_labels_in_MNI152.nii.gz'
44+
>>> rs.inputs.resampled_file = 'OASIS-TRT-20_DKT31_CMA_jointfusion_labels_in_MNI152_2mm.nii.gz'
45+
>>> rs.inputs.voxel_size = (2., 2., 2.)
46+
>>> rs.inputs.args = '-rt nearest -ns 1'
47+
>>> res = rs.run()
48+
2049
"""
2150

2251
import os
2352

53+
from nipype.interfaces.base import CommandLine
54+
CommandLine.set_default_terminal_output('file')
55+
2456
from nipype import (ants, afni, fsl, freesurfer, nipy, Function, DataSink)
2557
from nipype import Workflow, Node, MapNode
2658

@@ -75,7 +107,6 @@ def median(in_files):
75107
76108
out_file: a 3D Nifti file
77109
"""
78-
79110
average = None
80111
for idx, filename in enumerate(filename_to_list(in_files)):
81112
img = nb.load(filename)
@@ -138,7 +169,6 @@ def build_filter1(motion_params, comp_norm, outliers):
138169
Returns
139170
-------
140171
components_file: a text file containing all the regressors
141-
142172
"""
143173
out_files = []
144174
for idx, filename in enumerate(filename_to_list(motion_params)):
@@ -191,8 +221,7 @@ def extract_subrois(timeseries_file, label_file, indices):
191221
192222
timeseries_file: a 4D Nifti file
193223
label_file: a 3D file containing rois in the same space/size of the 4D file
194-
indices: a list of indices for ROIs to extract. Currently a dictionary
195-
mapping freesurfer indices to CMA/Label Fusion indices are being used
224+
indices: a list of indices for ROIs to extract.
196225
197226
Returns
198227
-------
@@ -206,8 +235,8 @@ def extract_subrois(timeseries_file, label_file, indices):
206235
rois = roiimg.get_data()
207236
out_ts_file = os.path.join(os.getcwd(), 'subcortical_timeseries.txt')
208237
with open(out_ts_file, 'wt') as fp:
209-
for fsindex, cmaindex in sorted(indices.items()):
210-
ijk = np.nonzero(rois == cmaindex)
238+
for fsindex in indices:
239+
ijk = np.nonzero(rois == fsindex)
211240
ts = data[ijk]
212241
for i0, row in enumerate(ts):
213242
fp.write('%d,%d,%d,%d,' % (fsindex, ijk[0][i0],
@@ -530,7 +559,7 @@ def create_workflow(files,
530559
# registration can be made significantly more accurate for cortical
531560
# structures by increasing the number of iterations
532561
# All parameters are set using the example from:
533-
#
562+
# https://github.com/stnava/ANTs/blob/master/Scripts/newAntsExample.sh
534563
reg = Node(ants.Registration(), name='antsRegister')
535564
reg.inputs.output_transform_prefix = "output_"
536565
reg.inputs.transforms = ['Translation', 'Rigid', 'Affine', 'SyN']
@@ -557,7 +586,6 @@ def create_workflow(files,
557586
reg.inputs.fixed_image = \
558587
os.path.abspath('OASIS-TRT-20_template_to_MNI152_2mm.nii.gz')
559588
reg.inputs.num_threads = 4
560-
reg.inputs.terminal_output = 'file'
561589
reg.plugin_args = {'qsub_args': '-l nodes=1:ppn=4'}
562590

563591
# Convert T1.mgz to nifti for using with ANTS
@@ -610,10 +638,8 @@ def create_workflow(files,
610638
imports=imports),
611639
iterfield=['timeseries_file'],
612640
name='getsubcortts')
613-
ts2txt.inputs.indices = dict(zip([8] + range(10, 14) + [17, 18, 26, 47] +
614-
range(49, 55) + [58],
615-
[39, 60, 37, 58, 56, 48, 32, 30,
616-
38, 59, 36, 57, 55, 47, 31, 23]))
641+
ts2txt.inputs.indices = [8] + range(10, 14) + [17, 18, 26, 47] +\
642+
range(49, 55) + [58]
617643
ts2txt.inputs.label_file = \
618644
os.path.abspath(('OASIS-TRT-20_DKT31_CMA_jointfusion_labels_in_MNI152'
619645
'_2mm.nii.gz'))

nipype/interfaces/nipy/preprocess.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,9 @@
2121
except Exception, e:
2222
warnings.warn('nipy not installed')
2323
else:
24-
from nipy.labs.mask import compute_mask
25-
from nipy.algorithms.registration import FmriRealign4d as FR4d
24+
import nipy
2625
from nipy import save_image, load_image
27-
from nipy.algorithms.registration import SpaceTimeRealign
28-
from nipy.algorithms.registration.groupwise_registration import SpaceRealign
26+
nipy_version = nipy.__version__
2927

3028
from ..base import (TraitedSpec, BaseInterface, traits,
3129
BaseInterfaceInputSpec, isdefined, File,
@@ -52,7 +50,7 @@ class ComputeMask(BaseInterface):
5250
output_spec = ComputeMaskOutputSpec
5351

5452
def _run_interface(self, runtime):
55-
53+
from nipy.labs.mask import compute_mask
5654
args = {}
5755
for key in [k for k, _ in self.inputs.items()
5856
if k not in BaseInterfaceInputSpec().trait_names()]:
@@ -119,7 +117,6 @@ class FmriRealign4dOutputSpec(TraitedSpec):
119117
desc="Motion parameter files")
120118

121119

122-
@np.deprecate_with_doc('Please use SpaceTimeRealign instead')
123120
class FmriRealign4d(BaseInterface):
124121
"""Simultaneous motion and slice timing correction algorithm
125122
@@ -149,7 +146,7 @@ class FmriRealign4d(BaseInterface):
149146
keywords = ['slice timing', 'motion correction']
150147

151148
def _run_interface(self, runtime):
152-
149+
from nipy.algorithms.registration import FmriRealign4d as FR4d
153150
all_ims = [load_image(fname) for fname in self.inputs.in_file]
154151

155152
if not isdefined(self.inputs.tr_slices):
@@ -202,7 +199,7 @@ def _list_outputs(self):
202199
class SpaceTimeRealignerInputSpec(BaseInterfaceInputSpec):
203200

204201
in_file = InputMultiPath(exists=True,
205-
mandatory=True,
202+
mandatory=True, min_ver='0.4.0.dev',
206203
desc="File to realign")
207204
tr = traits.Float(desc="TR in seconds", requires=['slice_times'])
208205
slice_times = traits.Either(traits.List(traits.Float()),
@@ -277,13 +274,19 @@ class SpaceTimeRealigner(BaseInterface):
277274
output_spec = SpaceTimeRealignerOutputSpec
278275
keywords = ['slice timing', 'motion correction']
279276

280-
def _run_interface(self, runtime):
277+
@property
278+
def version(self):
279+
return nipy_version
281280

281+
def _run_interface(self, runtime):
282282
all_ims = [load_image(fname) for fname in self.inputs.in_file]
283283

284284
if not isdefined(self.inputs.slice_times):
285+
from nipy.algorithms.registration.groupwise_registration import \
286+
SpaceRealign
285287
R = SpaceRealign(all_ims)
286288
else:
289+
from nipy.algorithms.registration import SpaceTimeRealign
287290
R = SpaceTimeRealign(all_ims,
288291
tr=self.inputs.tr,
289292
slice_times=self.inputs.slice_times,

0 commit comments

Comments
 (0)