/
maths.py
606 lines (483 loc) · 14.3 KB
/
maths.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
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
# vi: set ft=python sts=4 ts=4 sw=4 et:
"""
The maths module provides higher-level interfaces to some of the operations
that can be performed with the fslmaths command-line program.
"""
import os
import numpy as np
from ..base import TraitedSpec, File, traits, InputMultiPath, isdefined
from .base import FSLCommand, FSLCommandInputSpec
class MathsInput(FSLCommandInputSpec):
in_file = File(
position=2, argstr="%s", exists=True, mandatory=True, desc="image to operate on"
)
out_file = File(
genfile=True, position=-2, argstr="%s", desc="image to write", hash_files=False
)
_dtypes = ["float", "char", "int", "short", "double", "input"]
internal_datatype = traits.Enum(
*_dtypes,
position=1,
argstr="-dt %s",
desc=("datatype to use for calculations (default is float)")
)
output_datatype = traits.Enum(
*_dtypes,
position=-1,
argstr="-odt %s",
desc=("datatype to use for output (default uses input type)")
)
nan2zeros = traits.Bool(
position=3, argstr="-nan", desc="change NaNs to zeros before doing anything"
)
class MathsOutput(TraitedSpec):
out_file = File(desc="image written after calculations")
class MathsCommand(FSLCommand):
_cmd = "fslmaths"
input_spec = MathsInput
output_spec = MathsOutput
_suffix = "_maths"
def _list_outputs(self):
outputs = self.output_spec().get()
outputs["out_file"] = self.inputs.out_file
if not isdefined(self.inputs.out_file):
outputs["out_file"] = self._gen_fname(
self.inputs.in_file, suffix=self._suffix
)
outputs["out_file"] = os.path.abspath(outputs["out_file"])
return outputs
def _gen_filename(self, name):
if name == "out_file":
return self._list_outputs()["out_file"]
return None
class ChangeDataTypeInput(MathsInput):
_dtypes = ["float", "char", "int", "short", "double", "input"]
output_datatype = traits.Enum(
*_dtypes, position=-1, argstr="-odt %s", mandatory=True, desc="output data type"
)
class ChangeDataType(MathsCommand):
"""Use fslmaths to change the datatype of an image."""
input_spec = ChangeDataTypeInput
_suffix = "_chdt"
class ThresholdInputSpec(MathsInput):
thresh = traits.Float(
mandatory=True, position=4, argstr="%s", desc="threshold value"
)
direction = traits.Enum(
"below",
"above",
usedefault=True,
desc="zero-out either below or above thresh value",
)
use_robust_range = traits.Bool(
desc="interpret thresh as percentage (0-100) of robust range"
)
use_nonzero_voxels = traits.Bool(
desc="use nonzero voxels to calculate robust range",
requires=["use_robust_range"],
)
class Threshold(MathsCommand):
"""Use fslmaths to apply a threshold to an image in a variety of ways."""
input_spec = ThresholdInputSpec
_suffix = "_thresh"
def _format_arg(self, name, spec, value):
if name == "thresh":
arg = "-"
_si = self.inputs
if self.inputs.direction == "above":
arg += "u"
arg += "thr"
if isdefined(_si.use_robust_range) and _si.use_robust_range:
if isdefined(_si.use_nonzero_voxels) and _si.use_nonzero_voxels:
arg += "P"
else:
arg += "p"
arg += " %.10f" % value
return arg
return super()._format_arg(name, spec, value)
class StdImageInput(MathsInput):
dimension = traits.Enum(
"T",
"X",
"Y",
"Z",
usedefault=True,
argstr="-%sstd",
position=4,
desc="dimension to standard deviate across",
)
class StdImage(MathsCommand):
"""Use fslmaths to generate a standard deviation in an image across a given
dimension.
"""
input_spec = StdImageInput
_suffix = "_std"
class MeanImageInput(MathsInput):
dimension = traits.Enum(
"T",
"X",
"Y",
"Z",
usedefault=True,
argstr="-%smean",
position=4,
desc="dimension to mean across",
)
class MeanImage(MathsCommand):
"""Use fslmaths to generate a mean image across a given dimension."""
input_spec = MeanImageInput
_suffix = "_mean"
class MaxImageInput(MathsInput):
dimension = traits.Enum(
"T",
"X",
"Y",
"Z",
usedefault=True,
argstr="-%smax",
position=4,
desc="dimension to max across",
)
class MaxImage(MathsCommand):
"""Use fslmaths to generate a max image across a given dimension.
Examples
--------
>>> from nipype.interfaces.fsl.maths import MaxImage
>>> maxer = MaxImage()
>>> maxer.inputs.in_file = "functional.nii" # doctest: +SKIP
>>> maxer.dimension = "T"
>>> maxer.cmdline # doctest: +SKIP
'fslmaths functional.nii -Tmax functional_max.nii'
"""
input_spec = MaxImageInput
_suffix = "_max"
class PercentileImageInput(MathsInput):
dimension = traits.Enum(
"T",
"X",
"Y",
"Z",
usedefault=True,
argstr="-%sperc",
position=4,
desc="dimension to percentile across",
)
perc = traits.Range(
low=0,
high=100,
argstr="%f",
position=5,
desc=("nth percentile (0-100) of FULL RANGE across dimension"),
)
class PercentileImage(MathsCommand):
"""Use fslmaths to generate a percentile image across a given dimension.
Examples
--------
>>> from nipype.interfaces.fsl.maths import MaxImage
>>> percer = PercentileImage()
>>> percer.inputs.in_file = "functional.nii" # doctest: +SKIP
>>> percer.dimension = "T"
>>> percer.perc = 90
>>> percer.cmdline # doctest: +SKIP
'fslmaths functional.nii -Tperc 90 functional_perc.nii'
"""
input_spec = PercentileImageInput
_suffix = "_perc"
class MaxnImageInput(MathsInput):
dimension = traits.Enum(
"T",
"X",
"Y",
"Z",
usedefault=True,
argstr="-%smaxn",
position=4,
desc="dimension to index max across",
)
class MaxnImage(MathsCommand):
"""Use fslmaths to generate an image of index of max across
a given dimension.
"""
input_spec = MaxnImageInput
_suffix = "_maxn"
class MinImageInput(MathsInput):
dimension = traits.Enum(
"T",
"X",
"Y",
"Z",
usedefault=True,
argstr="-%smin",
position=4,
desc="dimension to min across",
)
class MinImage(MathsCommand):
"""Use fslmaths to generate a minimum image across a given dimension."""
input_spec = MinImageInput
_suffix = "_min"
class MedianImageInput(MathsInput):
dimension = traits.Enum(
"T",
"X",
"Y",
"Z",
usedefault=True,
argstr="-%smedian",
position=4,
desc="dimension to median across",
)
class MedianImage(MathsCommand):
"""Use fslmaths to generate a median image across a given dimension."""
input_spec = MedianImageInput
_suffix = "_median"
class AR1ImageInput(MathsInput):
dimension = traits.Enum(
"T",
"X",
"Y",
"Z",
usedefault=True,
argstr="-%sar1",
position=4,
desc=("dimension to find AR(1) coefficient across"),
)
class AR1Image(MathsCommand):
"""Use fslmaths to generate an AR1 coefficient image across a
given dimension. (Should use -odt float and probably demean first)
"""
input_spec = AR1ImageInput
_suffix = "_ar1"
class IsotropicSmoothInput(MathsInput):
fwhm = traits.Float(
mandatory=True,
xor=["sigma"],
position=4,
argstr="-s %.5f",
desc="fwhm of smoothing kernel [mm]",
)
sigma = traits.Float(
mandatory=True,
xor=["fwhm"],
position=4,
argstr="-s %.5f",
desc="sigma of smoothing kernel [mm]",
)
class IsotropicSmooth(MathsCommand):
"""Use fslmaths to spatially smooth an image with a gaussian kernel."""
input_spec = IsotropicSmoothInput
_suffix = "_smooth"
def _format_arg(self, name, spec, value):
if name == "fwhm":
sigma = float(value) / np.sqrt(8 * np.log(2))
return spec.argstr % sigma
return super()._format_arg(name, spec, value)
class ApplyMaskInput(MathsInput):
mask_file = File(
exists=True,
mandatory=True,
argstr="-mas %s",
position=4,
desc="binary image defining mask space",
)
class ApplyMask(MathsCommand):
"""Use fslmaths to apply a binary mask to another image."""
input_spec = ApplyMaskInput
_suffix = "_masked"
class KernelInput(MathsInput):
kernel_shape = traits.Enum(
"3D",
"2D",
"box",
"boxv",
"gauss",
"sphere",
"file",
argstr="-kernel %s",
position=4,
desc="kernel shape to use",
)
kernel_size = traits.Float(
argstr="%.4f",
position=5,
xor=["kernel_file"],
desc=("kernel size - voxels for box/boxv, mm for sphere, mm sigma for gauss"),
)
kernel_file = File(
exists=True,
argstr="%s",
position=5,
xor=["kernel_size"],
desc="use external file for kernel",
)
class DilateInput(KernelInput):
operation = traits.Enum(
"mean",
"modal",
"max",
argstr="-dil%s",
position=6,
mandatory=True,
desc="filtering operation to perform in dilation",
)
class DilateImage(MathsCommand):
"""Use fslmaths to perform a spatial dilation of an image."""
input_spec = DilateInput
_suffix = "_dil"
def _format_arg(self, name, spec, value):
if name == "operation":
return spec.argstr % dict(mean="M", modal="D", max="F")[value]
return super()._format_arg(name, spec, value)
class ErodeInput(KernelInput):
minimum_filter = traits.Bool(
argstr="%s",
position=6,
usedefault=True,
default_value=False,
desc=("if true, minimum filter rather than erosion by zeroing-out"),
)
class ErodeImage(MathsCommand):
"""Use fslmaths to perform a spatial erosion of an image."""
input_spec = ErodeInput
_suffix = "_ero"
def _format_arg(self, name, spec, value):
if name == "minimum_filter":
if value:
return "-eroF"
return "-ero"
return super()._format_arg(name, spec, value)
class SpatialFilterInput(KernelInput):
operation = traits.Enum(
"mean",
"median",
"meanu",
argstr="-f%s",
position=6,
mandatory=True,
desc="operation to filter with",
)
class SpatialFilter(MathsCommand):
"""Use fslmaths to spatially filter an image."""
input_spec = SpatialFilterInput
_suffix = "_filt"
class UnaryMathsInput(MathsInput):
operation = traits.Enum(
"exp",
"log",
"sin",
"cos",
"tan",
"asin",
"acos",
"atan",
"sqr",
"sqrt",
"recip",
"abs",
"bin",
"binv",
"fillh",
"fillh26",
"index",
"edge",
"nan",
"nanm",
"rand",
"randn",
"range",
argstr="-%s",
position=4,
mandatory=True,
desc="operation to perform",
)
class UnaryMaths(MathsCommand):
"""Use fslmaths to perorm a variety of mathematical operations on an image."""
input_spec = UnaryMathsInput
def _list_outputs(self):
self._suffix = "_" + self.inputs.operation
return super()._list_outputs()
class BinaryMathsInput(MathsInput):
operation = traits.Enum(
"add",
"sub",
"mul",
"div",
"rem",
"max",
"min",
mandatory=True,
argstr="-%s",
position=4,
desc="operation to perform",
)
operand_file = File(
exists=True,
argstr="%s",
mandatory=True,
position=5,
xor=["operand_value"],
desc="second image to perform operation with",
)
operand_value = traits.Float(
argstr="%.8f",
mandatory=True,
position=5,
xor=["operand_file"],
desc="value to perform operation with",
)
class BinaryMaths(MathsCommand):
"""Use fslmaths to perform mathematical operations using a second image or
a numeric value.
"""
input_spec = BinaryMathsInput
class MultiImageMathsInput(MathsInput):
op_string = traits.String(
position=4,
argstr="%s",
mandatory=True,
desc=("python formatted string of operations to perform"),
)
operand_files = InputMultiPath(
File(exists=True),
mandatory=True,
desc=("list of file names to plug into op string"),
)
class MultiImageMaths(MathsCommand):
"""Use fslmaths to perform a sequence of mathematical operations.
Examples
--------
>>> from nipype.interfaces.fsl import MultiImageMaths
>>> maths = MultiImageMaths()
>>> maths.inputs.in_file = "functional.nii"
>>> maths.inputs.op_string = "-add %s -mul -1 -div %s"
>>> maths.inputs.operand_files = ["functional2.nii", "functional3.nii"]
>>> maths.inputs.out_file = "functional4.nii"
>>> maths.cmdline
'fslmaths functional.nii -add functional2.nii -mul -1 -div functional3.nii functional4.nii'
"""
input_spec = MultiImageMathsInput
def _format_arg(self, name, spec, value):
if name == "op_string":
return value % tuple(self.inputs.operand_files)
return super()._format_arg(name, spec, value)
class TemporalFilterInput(MathsInput):
lowpass_sigma = traits.Float(
-1,
argstr="%.6f",
position=5,
usedefault=True,
desc="lowpass filter sigma (in volumes)",
)
highpass_sigma = traits.Float(
-1,
argstr="-bptf %.6f",
position=4,
usedefault=True,
desc="highpass filter sigma (in volumes)",
)
class TemporalFilter(MathsCommand):
"""Use fslmaths to apply a low, high, or bandpass temporal filter to a
timeseries.
"""
input_spec = TemporalFilterInput
_suffix = "_filt"