1
-
2
1
#!/usr/bin/env python
2
+ # emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
3
+ # vi: set ft=python sts=4 ts=4 sw=4 et:
3
4
"""
4
5
====================================
5
6
rsfMRI: ANTS, FS, FSL, SPM, aCompCor
40
41
41
42
- `Joint Fusion Atlas <http://mindboggle.info/data/atlases/jointfusion/OASIS-TRT-20_jointfusion_DKT31_CMA_labels_in_MNI152_2mm_v2.nii.gz>`_
42
43
- `MNI template <http://mindboggle.info/data/templates/ants/OASIS-30_Atropos_template_in_MNI152_2mm.nii.gz>`_
43
-
44
44
"""
45
45
46
+
46
47
import os
47
48
48
49
from nipype .interfaces .base import CommandLine
49
50
CommandLine .set_default_terminal_output ('allatonce' )
50
51
51
- from dcmstack . extract import default_extractor
52
+
52
53
from dicom import read_file
53
54
54
55
from nipype .interfaces import (spm , fsl , Function , ants , freesurfer )
83
84
84
85
85
86
def get_info (dicom_files ):
87
+ from dcmstack .extract import default_extractor
86
88
"""Given a Siemens dicom file return metadata
87
89
88
90
Returns
@@ -111,6 +113,8 @@ def median(in_files):
111
113
112
114
out_file: a 3D Nifti file
113
115
"""
116
+ import numpy as np
117
+ import nibabel as nb
114
118
average = None
115
119
for idx , filename in enumerate (filename_to_list (in_files )):
116
120
img = nb .load (filename )
@@ -136,6 +140,9 @@ def bandpass_filter(files, lowpass_freq, highpass_freq, fs):
136
140
highpass_freq: cutoff frequency for the high pass filter (in Hz)
137
141
fs: sampling rate (in Hz)
138
142
"""
143
+ from nipype .utils .filemanip import split_filename , list_to_filename
144
+ import numpy as np
145
+ import nibabel as nb
139
146
out_files = []
140
147
for filename in filename_to_list (files ):
141
148
path , name , ext = split_filename (filename )
@@ -168,6 +175,7 @@ def motion_regressors(motion_params, order=0, derivatives=1):
168
175
169
176
motion + d(motion)/dt + d2(motion)/dt2 (linear + quadratic)
170
177
"""
178
+ import numpy as np
171
179
out_files = []
172
180
for idx , filename in enumerate (filename_to_list (motion_params )):
173
181
params = np .genfromtxt (filename )
@@ -204,6 +212,9 @@ def build_filter1(motion_params, comp_norm, outliers, detrend_poly=None):
204
212
-------
205
213
components_file: a text file containing all the regressors
206
214
"""
215
+ import numpy as np
216
+ import nibabel as nb
217
+ from scipy .special import legendre
207
218
out_files = []
208
219
for idx , filename in enumerate (filename_to_list (motion_params )):
209
220
params = np .genfromtxt (filename )
@@ -245,6 +256,10 @@ def extract_noise_components(realigned_file, mask_file, num_components=5,
245
256
-------
246
257
components_file: a text file containing the noise components
247
258
"""
259
+ from scipy .linalg .decomp_svd import svd
260
+ import numpy as np
261
+ import nibabel as nb
262
+ import os
248
263
imgseries = nb .load (realigned_file )
249
264
components = None
250
265
for filename in filename_to_list (mask_file ):
@@ -261,7 +276,7 @@ def extract_noise_components(realigned_file, mask_file, num_components=5,
261
276
stdX [np .isnan (stdX )] = 1.
262
277
stdX [np .isinf (stdX )] = 1.
263
278
X = (X - np .mean (X , axis = 0 ))/ stdX
264
- u , _ , _ = sp . linalg . svd (X , full_matrices = False )
279
+ u , _ , _ = svd (X , full_matrices = False )
265
280
if components is None :
266
281
components = u [:, :num_components ]
267
282
else :
@@ -311,6 +326,9 @@ def extract_subrois(timeseries_file, label_file, indices):
311
326
The first four columns are: freesurfer index, i, j, k positions in the
312
327
label file
313
328
"""
329
+ from nipype .utils .filemanip import split_filename
330
+ import nibabel as nb
331
+ import os
314
332
img = nb .load (timeseries_file )
315
333
data = img .get_data ()
316
334
roiimg = nb .load (label_file )
@@ -331,6 +349,8 @@ def extract_subrois(timeseries_file, label_file, indices):
331
349
def combine_hemi (left , right ):
332
350
"""Combine left and right hemisphere time series into a single text file
333
351
"""
352
+ import os
353
+ import numpy as np
334
354
lh_data = nb .load (left ).get_data ()
335
355
rh_data = nb .load (right ).get_data ()
336
356
@@ -367,10 +387,6 @@ def create_reg_workflow(name='registration'):
367
387
outputspec.anat2target_transform : FLIRT+FNIRT transform
368
388
outputspec.transformed_files : transformed files in target space
369
389
outputspec.transformed_mean : mean image in target space
370
-
371
- Example
372
- -------
373
-
374
390
"""
375
391
376
392
register = Workflow (name = name )
@@ -437,6 +453,7 @@ def create_reg_workflow(name='registration'):
437
453
"""
438
454
Apply inverse transform to take segmentations to functional space
439
455
"""
456
+
440
457
applyxfm = MapNode (freesurfer .ApplyVolTransform (inverse = True ,
441
458
interp = 'nearest' ),
442
459
iterfield = ['target_file' ],
@@ -449,6 +466,7 @@ def create_reg_workflow(name='registration'):
449
466
"""
450
467
Apply inverse transform to aparc file
451
468
"""
469
+
452
470
aparcxfm = Node (freesurfer .ApplyVolTransform (inverse = True ,
453
471
interp = 'nearest' ),
454
472
name = 'aparc_inverse_transform' )
@@ -508,7 +526,6 @@ def create_reg_workflow(name='registration'):
508
526
register .connect (stripper , 'out_file' , reg , 'moving_image' )
509
527
register .connect (inputnode ,'target_image' , reg ,'fixed_image' )
510
528
511
-
512
529
"""
513
530
Concatenate the affine and ants transforms into a list
514
531
"""
@@ -519,10 +536,10 @@ def create_reg_workflow(name='registration'):
519
536
register .connect (convert2itk , 'itk_transform' , merge , 'in2' )
520
537
register .connect (reg , ('composite_transform' , pickfirst ), merge , 'in1' )
521
538
522
-
523
539
"""
524
540
Transform the mean image. First to anatomical and then to target
525
541
"""
542
+
526
543
warpmean = Node (ants .ApplyTransforms (), name = 'warpmean' )
527
544
warpmean .inputs .input_image_type = 3
528
545
warpmean .inputs .interpolation = 'BSpline'
@@ -535,7 +552,6 @@ def create_reg_workflow(name='registration'):
535
552
register .connect (inputnode , 'mean_image' , warpmean , 'input_image' )
536
553
register .connect (merge , 'out' , warpmean , 'transforms' )
537
554
538
-
539
555
"""
540
556
Assign all the output files
541
557
"""
@@ -613,6 +629,7 @@ def create_workflow(files,
613
629
614
630
"""Segment and Register
615
631
"""
632
+
616
633
registration = create_reg_workflow (name = 'registration' )
617
634
wf .connect (calc_median , 'median_file' , registration , 'inputspec.mean_image' )
618
635
registration .inputs .inputspec .subject_id = subject_id
0 commit comments