7
7
from nipype import logging
8
8
from nipype .utils .filemanip import fname_presuffix
9
9
from nipype .interfaces .base import (
10
- traits , TraitedSpec , BaseInterfaceInputSpec , File ,
11
- SimpleInterface , OutputMultiObject , InputMultiObject
10
+ traits ,
11
+ TraitedSpec ,
12
+ BaseInterfaceInputSpec ,
13
+ File ,
14
+ SimpleInterface ,
15
+ OutputMultiObject ,
16
+ InputMultiObject ,
12
17
)
13
18
14
- IFLOGGER = logging .getLogger (' nipype.interface' )
19
+ IFLOGGER = logging .getLogger (" nipype.interface" )
15
20
16
21
17
22
class _ApplyMaskInputSpec (BaseInterfaceInputSpec ):
18
- in_file = File (exists = True , mandatory = True , desc = 'an image' )
19
- in_mask = File (exists = True , mandatory = True , desc = 'a mask' )
20
- threshold = traits .Float (0.5 , usedefault = True ,
21
- desc = 'a threshold to the mask, if it is nonbinary' )
23
+ in_file = File (exists = True , mandatory = True , desc = "an image" )
24
+ in_mask = File (exists = True , mandatory = True , desc = "a mask" )
25
+ threshold = traits .Float (
26
+ 0.5 , usedefault = True , desc = "a threshold to the mask, if it is nonbinary"
27
+ )
22
28
23
29
24
30
class _ApplyMaskOutputSpec (TraitedSpec ):
25
- out_file = File (exists = True , desc = ' masked file' )
31
+ out_file = File (exists = True , desc = " masked file" )
26
32
27
33
28
34
class ApplyMask (SimpleInterface ):
@@ -36,8 +42,9 @@ def _run_interface(self, runtime):
36
42
msknii = nb .load (self .inputs .in_mask )
37
43
msk = msknii .get_fdata () > self .inputs .threshold
38
44
39
- self ._results ['out_file' ] = fname_presuffix (
40
- self .inputs .in_file , suffix = '_masked' , newpath = runtime .cwd )
45
+ self ._results ["out_file" ] = fname_presuffix (
46
+ self .inputs .in_file , suffix = "_masked" , newpath = runtime .cwd
47
+ )
41
48
42
49
if img .dataobj .shape [:3 ] != msk .shape :
43
50
raise ValueError ("Image and mask sizes do not match." )
@@ -49,19 +56,18 @@ def _run_interface(self, runtime):
49
56
msk = msk [..., np .newaxis ]
50
57
51
58
masked = img .__class__ (img .dataobj * msk , None , img .header )
52
- masked .to_filename (self ._results [' out_file' ])
59
+ masked .to_filename (self ._results [" out_file" ])
53
60
return runtime
54
61
55
62
56
63
class _BinarizeInputSpec (BaseInterfaceInputSpec ):
57
- in_file = File (exists = True , mandatory = True , desc = 'input image' )
58
- thresh_low = traits .Float (mandatory = True ,
59
- desc = 'non-inclusive lower threshold' )
64
+ in_file = File (exists = True , mandatory = True , desc = "input image" )
65
+ thresh_low = traits .Float (mandatory = True , desc = "non-inclusive lower threshold" )
60
66
61
67
62
68
class _BinarizeOutputSpec (TraitedSpec ):
63
- out_file = File (exists = True , desc = ' masked file' )
64
- out_mask = File (exists = True , desc = ' output mask' )
69
+ out_file = File (exists = True , desc = " masked file" )
70
+ out_mask = File (exists = True , desc = " output mask" )
65
71
66
72
67
73
class Binarize (SimpleInterface ):
@@ -73,33 +79,35 @@ class Binarize(SimpleInterface):
73
79
def _run_interface (self , runtime ):
74
80
img = nb .load (self .inputs .in_file )
75
81
76
- self ._results ['out_file' ] = fname_presuffix (
77
- self .inputs .in_file , suffix = '_masked' , newpath = runtime .cwd )
78
- self ._results ['out_mask' ] = fname_presuffix (
79
- self .inputs .in_file , suffix = '_mask' , newpath = runtime .cwd )
82
+ self ._results ["out_file" ] = fname_presuffix (
83
+ self .inputs .in_file , suffix = "_masked" , newpath = runtime .cwd
84
+ )
85
+ self ._results ["out_mask" ] = fname_presuffix (
86
+ self .inputs .in_file , suffix = "_mask" , newpath = runtime .cwd
87
+ )
80
88
81
89
data = img .get_fdata ()
82
90
mask = data > self .inputs .thresh_low
83
91
data [~ mask ] = 0.0
84
92
masked = img .__class__ (data , img .affine , img .header )
85
- masked .to_filename (self ._results [' out_file' ])
93
+ masked .to_filename (self ._results [" out_file" ])
86
94
87
- img .header .set_data_dtype ('uint8' )
88
- maskimg = img .__class__ (mask .astype ('uint8' ), img .affine ,
89
- img .header )
90
- maskimg .to_filename (self ._results ['out_mask' ])
95
+ img .header .set_data_dtype ("uint8" )
96
+ maskimg = img .__class__ (mask .astype ("uint8" ), img .affine , img .header )
97
+ maskimg .to_filename (self ._results ["out_mask" ])
91
98
92
99
return runtime
93
100
94
101
95
102
class _FourToThreeInputSpec (BaseInterfaceInputSpec ):
96
- in_file = File (exists = True , mandatory = True , desc = 'input 4d image' )
97
- allow_3D = traits .Bool (False , usedefault = True , desc = 'do not fail if a 3D volume is passed in' )
103
+ in_file = File (exists = True , mandatory = True , desc = "input 4d image" )
104
+ allow_3D = traits .Bool (
105
+ False , usedefault = True , desc = "do not fail if a 3D volume is passed in"
106
+ )
98
107
99
108
100
109
class _FourToThreeOutputSpec (TraitedSpec ):
101
- out_files = OutputMultiObject (File (exists = True ),
102
- desc = 'output list of 3d images' )
110
+ out_files = OutputMultiObject (File (exists = True ), desc = "output list of 3d images" )
103
111
104
112
105
113
class SplitSeries (SimpleInterface ):
@@ -117,9 +125,11 @@ def _run_interface(self, runtime):
117
125
if ndim != 4 :
118
126
if self .inputs .allow_3D and ndim == 3 :
119
127
out_file = str (
120
- Path (fname_presuffix (self .inputs .in_file , suffix = f"_idx-000" )).absolute ()
128
+ Path (
129
+ fname_presuffix (self .inputs .in_file , suffix = f"_idx-000" )
130
+ ).absolute ()
121
131
)
122
- self ._results [' out_files' ] = out_file
132
+ self ._results [" out_files" ] = out_file
123
133
filenii .to_filename (out_file )
124
134
return runtime
125
135
raise RuntimeError (
@@ -128,27 +138,29 @@ def _run_interface(self, runtime):
128
138
)
129
139
130
140
files_3d = nb .four_to_three (filenii )
131
- self ._results [' out_files' ] = []
141
+ self ._results [" out_files" ] = []
132
142
in_file = self .inputs .in_file
133
143
for i , file_3d in enumerate (files_3d ):
134
144
out_file = str (
135
145
Path (fname_presuffix (in_file , suffix = f"_idx-{ i :03} " )).absolute ()
136
146
)
137
147
file_3d .to_filename (out_file )
138
- self ._results [' out_files' ].append (out_file )
148
+ self ._results [" out_files" ].append (out_file )
139
149
140
150
return runtime
141
151
142
152
143
153
class _MergeSeriesInputSpec (BaseInterfaceInputSpec ):
144
- in_files = InputMultiObject (File (exists = True , mandatory = True ,
145
- desc = 'input list of 3d images' ))
146
- allow_4D = traits .Bool (True , usedefault = True ,
147
- desc = 'whether 4D images are allowed to be concatenated' )
154
+ in_files = InputMultiObject (
155
+ File (exists = True , mandatory = True , desc = "input list of 3d images" )
156
+ )
157
+ allow_4D = traits .Bool (
158
+ True , usedefault = True , desc = "whether 4D images are allowed to be concatenated"
159
+ )
148
160
149
161
150
162
class _MergeSeriesOutputSpec (TraitedSpec ):
151
- out_file = File (exists = True , desc = ' output 4d image' )
163
+ out_file = File (exists = True , desc = " output 4d image" )
152
164
153
165
154
166
class MergeSeries (SimpleInterface ):
@@ -169,12 +181,13 @@ def _run_interface(self, runtime):
169
181
nii_list += nb .four_to_three (filenii )
170
182
continue
171
183
else :
172
- raise ValueError ("Input image has an incorrect number of dimensions"
173
- f" ({ ndim } )." )
184
+ raise ValueError (
185
+ "Input image has an incorrect number of dimensions" f" ({ ndim } )."
186
+ )
174
187
175
188
img_4d = nb .concat_images (nii_list )
176
189
out_file = fname_presuffix (self .inputs .in_files [0 ], suffix = "_merged" )
177
190
img_4d .to_filename (out_file )
178
191
179
- self ._results [' out_file' ] = out_file
192
+ self ._results [" out_file" ] = out_file
180
193
return runtime
0 commit comments