-
Notifications
You must be signed in to change notification settings - Fork 521
/
utils.py
528 lines (434 loc) · 15.8 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
# -*- coding: utf-8 -*-
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
# vi: set ft=python sts=4 ts=4 sw=4 et:
import os
import numpy as np
from ...utils.filemanip import (
split_filename,
fname_presuffix,
ensure_list,
simplify_list,
)
from ..base import TraitedSpec, isdefined, File, traits, OutputMultiPath, InputMultiPath
from .base import SPMCommandInputSpec, SPMCommand, scans_for_fnames, scans_for_fname
class Analyze2niiInputSpec(SPMCommandInputSpec):
analyze_file = File(exists=True, mandatory=True)
class Analyze2niiOutputSpec(SPMCommandInputSpec):
nifti_file = File(exists=True)
class Analyze2nii(SPMCommand):
input_spec = Analyze2niiInputSpec
output_spec = Analyze2niiOutputSpec
def _make_matlab_command(self, _):
script = "V = spm_vol('%s');\n" % self.inputs.analyze_file
_, name, _ = split_filename(self.inputs.analyze_file)
self.output_name = os.path.join(os.getcwd(), name + ".nii")
script += "[Y, XYZ] = spm_read_vols(V);\n"
script += "V.fname = '%s';\n" % self.output_name
script += "spm_write_vol(V, Y);\n"
return script
def _list_outputs(self):
outputs = self._outputs().get()
outputs["nifti_file"] = self.output_name
return outputs
class CalcCoregAffineInputSpec(SPMCommandInputSpec):
target = File(
exists=True, mandatory=True, desc="target for generating affine transform"
)
moving = File(
exists=True,
mandatory=True,
copyfile=False,
desc=("volume transform can be applied to register with " "target"),
)
mat = File(desc="Filename used to store affine matrix")
invmat = File(desc="Filename used to store inverse affine matrix")
class CalcCoregAffineOutputSpec(TraitedSpec):
mat = File(exists=True, desc="Matlab file holding transform")
invmat = File(desc="Matlab file holding inverse transform")
class CalcCoregAffine(SPMCommand):
""" Uses SPM (spm_coreg) to calculate the transform mapping
moving to target. Saves Transform in mat (matlab binary file)
Also saves inverse transform
Examples
--------
>>> import nipype.interfaces.spm.utils as spmu
>>> coreg = spmu.CalcCoregAffine(matlab_cmd='matlab-spm8')
>>> coreg.inputs.target = 'structural.nii'
>>> coreg.inputs.moving = 'functional.nii'
>>> coreg.inputs.mat = 'func_to_struct.mat'
>>> coreg.run() # doctest: +SKIP
.. note::
* the output file mat is saves as a matlab binary file
* calculating the transforms does NOT change either input image
it does not **move** the moving image, only calculates the transform
that can be used to move it
"""
input_spec = CalcCoregAffineInputSpec
output_spec = CalcCoregAffineOutputSpec
def _make_inv_file(self):
""" makes filename to hold inverse transform if not specified"""
invmat = fname_presuffix(self.inputs.mat, prefix="inverse_")
return invmat
def _make_mat_file(self):
""" makes name for matfile if doesn exist"""
pth, mv, _ = split_filename(self.inputs.moving)
_, tgt, _ = split_filename(self.inputs.target)
mat = os.path.join(pth, "%s_to_%s.mat" % (mv, tgt))
return mat
def _make_matlab_command(self, _):
"""checks for SPM, generates script"""
if not isdefined(self.inputs.mat):
self.inputs.mat = self._make_mat_file()
if not isdefined(self.inputs.invmat):
self.inputs.invmat = self._make_inv_file()
script = """
target = '%s';
moving = '%s';
targetv = spm_vol(target);
movingv = spm_vol(moving);
x = spm_coreg(targetv, movingv);
M = spm_matrix(x);
save('%s' , 'M' );
M = inv(M);
save('%s','M')
""" % (
self.inputs.target,
self.inputs.moving,
self.inputs.mat,
self.inputs.invmat,
)
return script
def _list_outputs(self):
outputs = self._outputs().get()
outputs["mat"] = os.path.abspath(self.inputs.mat)
outputs["invmat"] = os.path.abspath(self.inputs.invmat)
return outputs
class ApplyTransformInputSpec(SPMCommandInputSpec):
in_file = File(
exists=True,
mandatory=True,
copyfile=True,
desc="file to apply transform to, (only updates header)",
)
mat = File(exists=True, mandatory=True, desc="file holding transform to apply")
out_file = File(desc="output file name for transformed data", genfile=True)
class ApplyTransformOutputSpec(TraitedSpec):
out_file = File(exists=True, desc="Transformed image file")
class ApplyTransform(SPMCommand):
""" Uses SPM to apply transform stored in a .mat file to given file
Examples
--------
>>> import nipype.interfaces.spm.utils as spmu
>>> applymat = spmu.ApplyTransform()
>>> applymat.inputs.in_file = 'functional.nii'
>>> applymat.inputs.mat = 'func_to_struct.mat'
>>> applymat.run() # doctest: +SKIP
"""
input_spec = ApplyTransformInputSpec
output_spec = ApplyTransformOutputSpec
def _make_matlab_command(self, _):
"""checks for SPM, generates script"""
outputs = self._list_outputs()
self.inputs.out_file = outputs["out_file"]
script = """
infile = '%s';
outfile = '%s'
transform = load('%s');
V = spm_vol(infile);
X = spm_read_vols(V);
[p n e v] = spm_fileparts(V.fname);
V.mat = transform.M * V.mat;
V.fname = fullfile(outfile);
spm_write_vol(V,X);
""" % (
self.inputs.in_file,
self.inputs.out_file,
self.inputs.mat,
)
# img_space = spm_get_space(infile);
# spm_get_space(infile, transform.M * img_space);
return script
def _list_outputs(self):
outputs = self.output_spec().get()
if not isdefined(self.inputs.out_file):
outputs["out_file"] = os.path.abspath(self._gen_outfilename())
else:
outputs["out_file"] = os.path.abspath(self.inputs.out_file)
return outputs
def _gen_outfilename(self):
_, name, _ = split_filename(self.inputs.in_file)
return name + "_trans.nii"
class ResliceInputSpec(SPMCommandInputSpec):
in_file = File(
exists=True,
mandatory=True,
desc="file to apply transform to, (only updates header)",
)
space_defining = File(
exists=True, mandatory=True, desc="Volume defining space to slice in_file into"
)
interp = traits.Range(
low=0,
high=7,
usedefault=True,
desc="degree of b-spline used for interpolation"
"0 is nearest neighbor (default)",
)
out_file = File(desc="Optional file to save resliced volume")
class ResliceOutputSpec(TraitedSpec):
out_file = File(exists=True, desc="resliced volume")
class Reslice(SPMCommand):
""" uses spm_reslice to resample in_file into space of space_defining"""
input_spec = ResliceInputSpec
output_spec = ResliceOutputSpec
def _make_matlab_command(self, _):
""" generates script"""
if not isdefined(self.inputs.out_file):
self.inputs.out_file = fname_presuffix(self.inputs.in_file, prefix="r")
script = """
flags.mean = 0;
flags.which = 1;
flags.mask = 0;
flags.interp = %d;
infiles = strvcat(\'%s\', \'%s\');
invols = spm_vol(infiles);
spm_reslice(invols, flags);
""" % (
self.inputs.interp,
self.inputs.space_defining,
self.inputs.in_file,
)
return script
def _list_outputs(self):
outputs = self._outputs().get()
outputs["out_file"] = os.path.abspath(self.inputs.out_file)
return outputs
class ApplyInverseDeformationInput(SPMCommandInputSpec):
in_files = InputMultiPath(
File(exists=True),
mandatory=True,
field="fnames",
desc="Files on which deformation is applied",
)
target = File(
exists=True, field="comp{1}.inv.space", desc="File defining target space"
)
deformation = File(
exists=True,
field="comp{1}.inv.comp{1}.sn2def.matname",
desc="SN SPM deformation file",
xor=["deformation_field"],
)
deformation_field = File(
exists=True,
field="comp{1}.inv.comp{1}.def",
desc="SN SPM deformation file",
xor=["deformation"],
)
interpolation = traits.Range(
low=0, high=7, field="interp", desc="degree of b-spline used for interpolation"
)
bounding_box = traits.List(
traits.Float(),
field="comp{1}.inv.comp{1}.sn2def.bb",
minlen=6,
maxlen=6,
desc="6-element list (opt)",
)
voxel_sizes = traits.List(
traits.Float(),
field="comp{1}.inv.comp{1}.sn2def.vox",
minlen=3,
maxlen=3,
desc="3-element list (opt)",
)
class ApplyInverseDeformationOutput(TraitedSpec):
out_files = OutputMultiPath(File(exists=True), desc="Transformed files")
class ApplyInverseDeformation(SPMCommand):
""" Uses spm to apply inverse deformation stored in a .mat file or a
deformation field to a given file
Examples
--------
>>> import nipype.interfaces.spm.utils as spmu
>>> inv = spmu.ApplyInverseDeformation()
>>> inv.inputs.in_files = 'functional.nii'
>>> inv.inputs.deformation = 'struct_to_func.mat'
>>> inv.inputs.target = 'structural.nii'
>>> inv.run() # doctest: +SKIP
"""
input_spec = ApplyInverseDeformationInput
output_spec = ApplyInverseDeformationOutput
_jobtype = "util"
_jobname = "defs"
def _format_arg(self, opt, spec, val):
"""Convert input to appropriate format for spm
"""
if opt == "in_files":
return scans_for_fnames(ensure_list(val))
if opt == "target":
return scans_for_fname(ensure_list(val))
if opt == "deformation":
return np.array([simplify_list(val)], dtype=object)
if opt == "deformation_field":
return np.array([simplify_list(val)], dtype=object)
return val
def _list_outputs(self):
outputs = self._outputs().get()
outputs["out_files"] = []
for filename in self.inputs.in_files:
_, fname = os.path.split(filename)
outputs["out_files"].append(os.path.realpath("w%s" % fname))
return outputs
class ResliceToReferenceInput(SPMCommandInputSpec):
in_files = InputMultiPath(
File(exists=True),
mandatory=True,
field="fnames",
desc="Files on which deformation is applied",
)
target = File(
exists=True, field="comp{1}.id.space", desc="File defining target space"
)
interpolation = traits.Range(
low=0, high=7, field="interp", desc="degree of b-spline used for interpolation"
)
bounding_box = traits.List(
traits.Float(),
field="comp{2}.idbbvox.bb",
minlen=6,
maxlen=6,
desc="6-element list (opt)",
)
voxel_sizes = traits.List(
traits.Float(),
field="comp{2}.idbbvox.vox",
minlen=3,
maxlen=3,
desc="3-element list (opt)",
)
class ResliceToReferenceOutput(TraitedSpec):
out_files = OutputMultiPath(File(exists=True), desc="Transformed files")
class ResliceToReference(SPMCommand):
"""Uses spm to reslice a volume to a target image space or to a provided
voxel size and bounding box
Examples
--------
>>> import nipype.interfaces.spm.utils as spmu
>>> r2ref = spmu.ResliceToReference()
>>> r2ref.inputs.in_files = 'functional.nii'
>>> r2ref.inputs.target = 'structural.nii'
>>> r2ref.run() # doctest: +SKIP
"""
input_spec = ResliceToReferenceInput
output_spec = ResliceToReferenceOutput
_jobtype = "util"
_jobname = "defs"
def _format_arg(self, opt, spec, val):
"""Convert input to appropriate format for spm
"""
if opt == "in_files":
return scans_for_fnames(ensure_list(val))
if opt == "target":
return scans_for_fname(ensure_list(val))
if opt == "deformation":
return np.array([simplify_list(val)], dtype=object)
if opt == "deformation_field":
return np.array([simplify_list(val)], dtype=object)
return val
def _list_outputs(self):
outputs = self._outputs().get()
outputs["out_files"] = []
for filename in self.inputs.in_files:
_, fname = os.path.split(filename)
outputs["out_files"].append(os.path.realpath("w%s" % fname))
return outputs
class DicomImportInputSpec(SPMCommandInputSpec):
in_files = InputMultiPath(
File(exists=True),
mandatory=True,
field="data",
desc="dicom files to be converted",
)
output_dir_struct = traits.Enum(
"flat",
"series",
"patname",
"patid_date",
"patid",
"date_time",
field="root",
usedefault=True,
desc="directory structure for the output.",
)
output_dir = traits.Str(
"./converted_dicom", field="outdir", usedefault=True, desc="output directory."
)
format = traits.Enum(
"nii", "img", field="convopts.format", usedefault=True, desc="output format."
)
icedims = traits.Bool(
False,
field="convopts.icedims",
usedefault=True,
desc=(
"If image sorting fails, one can try using "
"the additional SIEMENS ICEDims information "
"to create unique filenames. Use this only if "
"there would be multiple volumes with exactly "
"the same file names."
),
)
class DicomImportOutputSpec(TraitedSpec):
out_files = OutputMultiPath(File(exists=True), desc="converted files")
class DicomImport(SPMCommand):
""" Uses spm to convert DICOM files to nii or img+hdr.
Examples
--------
>>> import nipype.interfaces.spm.utils as spmu
>>> di = spmu.DicomImport()
>>> di.inputs.in_files = ['functional_1.dcm', 'functional_2.dcm']
>>> di.run() # doctest: +SKIP
"""
input_spec = DicomImportInputSpec
output_spec = DicomImportOutputSpec
_jobtype = "util"
_jobname = "dicom"
def _format_arg(self, opt, spec, val):
"""Convert input to appropriate format for spm
"""
if opt == "in_files":
return np.array(val, dtype=object)
if opt == "output_dir":
return np.array([val], dtype=object)
if opt == "output_dir":
return os.path.abspath(val)
if opt == "icedims":
if val:
return 1
return 0
return super(DicomImport, self)._format_arg(opt, spec, val)
def _run_interface(self, runtime):
od = os.path.abspath(self.inputs.output_dir)
if not os.path.isdir(od):
os.mkdir(od)
return super(DicomImport, self)._run_interface(runtime)
def _list_outputs(self):
from glob import glob
outputs = self._outputs().get()
od = os.path.abspath(self.inputs.output_dir)
ext = self.inputs.format
if self.inputs.output_dir_struct == "flat":
outputs["out_files"] = glob(os.path.join(od, "*.%s" % ext))
elif self.inputs.output_dir_struct == "series":
outputs["out_files"] = glob(
os.path.join(od, os.path.join("*", "*.%s" % ext))
)
elif self.inputs.output_dir_struct in ["patid", "date_time", "patname"]:
outputs["out_files"] = glob(
os.path.join(od, os.path.join("*", "*", "*.%s" % ext))
)
elif self.inputs.output_dir_struct == "patid_date":
outputs["out_files"] = glob(
os.path.join(od, os.path.join("*", "*", "*", "*.%s" % ext))
)
return outputs