77from nipype import logging
88from nipype .utils .filemanip import fname_presuffix
99from 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 ,
1217)
1318
14- IFLOGGER = logging .getLogger (' nipype.interface' )
19+ IFLOGGER = logging .getLogger (" nipype.interface" )
1520
1621
1722class _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+ )
2228
2329
2430class _ApplyMaskOutputSpec (TraitedSpec ):
25- out_file = File (exists = True , desc = ' masked file' )
31+ out_file = File (exists = True , desc = " masked file" )
2632
2733
2834class ApplyMask (SimpleInterface ):
@@ -36,8 +42,9 @@ def _run_interface(self, runtime):
3642 msknii = nb .load (self .inputs .in_mask )
3743 msk = msknii .get_fdata () > self .inputs .threshold
3844
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+ )
4148
4249 if img .dataobj .shape [:3 ] != msk .shape :
4350 raise ValueError ("Image and mask sizes do not match." )
@@ -49,19 +56,18 @@ def _run_interface(self, runtime):
4956 msk = msk [..., np .newaxis ]
5057
5158 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" ])
5360 return runtime
5461
5562
5663class _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" )
6066
6167
6268class _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" )
6571
6672
6773class Binarize (SimpleInterface ):
@@ -73,33 +79,35 @@ class Binarize(SimpleInterface):
7379 def _run_interface (self , runtime ):
7480 img = nb .load (self .inputs .in_file )
7581
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+ )
8088
8189 data = img .get_fdata ()
8290 mask = data > self .inputs .thresh_low
8391 data [~ mask ] = 0.0
8492 masked = img .__class__ (data , img .affine , img .header )
85- masked .to_filename (self ._results [' out_file' ])
93+ masked .to_filename (self ._results [" out_file" ])
8694
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" ])
9198
9299 return runtime
93100
94101
95102class _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+ )
98107
99108
100109class _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" )
103111
104112
105113class SplitSeries (SimpleInterface ):
@@ -117,9 +125,11 @@ def _run_interface(self, runtime):
117125 if ndim != 4 :
118126 if self .inputs .allow_3D and ndim == 3 :
119127 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 ()
121131 )
122- self ._results [' out_files' ] = out_file
132+ self ._results [" out_files" ] = out_file
123133 filenii .to_filename (out_file )
124134 return runtime
125135 raise RuntimeError (
@@ -128,27 +138,29 @@ def _run_interface(self, runtime):
128138 )
129139
130140 files_3d = nb .four_to_three (filenii )
131- self ._results [' out_files' ] = []
141+ self ._results [" out_files" ] = []
132142 in_file = self .inputs .in_file
133143 for i , file_3d in enumerate (files_3d ):
134144 out_file = str (
135145 Path (fname_presuffix (in_file , suffix = f"_idx-{ i :03} " )).absolute ()
136146 )
137147 file_3d .to_filename (out_file )
138- self ._results [' out_files' ].append (out_file )
148+ self ._results [" out_files" ].append (out_file )
139149
140150 return runtime
141151
142152
143153class _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+ )
148160
149161
150162class _MergeSeriesOutputSpec (TraitedSpec ):
151- out_file = File (exists = True , desc = ' output 4d image' )
163+ out_file = File (exists = True , desc = " output 4d image" )
152164
153165
154166class MergeSeries (SimpleInterface ):
@@ -169,12 +181,13 @@ def _run_interface(self, runtime):
169181 nii_list += nb .four_to_three (filenii )
170182 continue
171183 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+ )
174187
175188 img_4d = nb .concat_images (nii_list )
176189 out_file = fname_presuffix (self .inputs .in_files [0 ], suffix = "_merged" )
177190 img_4d .to_filename (out_file )
178191
179- self ._results [' out_file' ] = out_file
192+ self ._results [" out_file" ] = out_file
180193 return runtime
0 commit comments