@@ -93,6 +93,7 @@ def _run_interface(self, runtime):
93
93
94
94
class _FourToThreeInputSpec (BaseInterfaceInputSpec ):
95
95
in_file = File (exists = True , mandatory = True , desc = 'input 4d image' )
96
+ accept_3D = traits .Bool (False , usedefault = True , desc = 'do not fail if a 3D volume is passed in' )
96
97
97
98
98
99
class _FourToThreeOutputSpec (TraitedSpec ):
@@ -108,14 +109,25 @@ class SplitSeries(SimpleInterface):
108
109
output_spec = _FourToThreeOutputSpec
109
110
110
111
def _run_interface (self , runtime ):
111
- filenii = nb .load (self .inputs .in_file )
112
- if len (filenii .shape ) != 4 :
113
- raise RuntimeError ('Input image (%s) is not 4D.' % filenii )
112
+ filenii = nb .squeeze_image (nb .load (self .inputs .in_file ))
113
+ ndim = filenii .dataobj .ndim
114
+ if ndim != 4 :
115
+ if self .inputs .accept_3D and ndim == 3 :
116
+ out_file = str (
117
+ Path (fname_presuffix (self .inputs .in_file , suffix = f"_idx-000" )).absolute ()
118
+ )
119
+ self ._results ['out_files' ] = out_file
120
+ filenii .to_filename (out_file )
121
+ return runtime
122
+ raise RuntimeError (f"Input image image is { ndim } D." )
114
123
115
124
files_3d = nb .four_to_three (filenii )
116
125
self ._results ['out_files' ] = []
126
+ in_file = self .inputs .in_file
117
127
for i , file_3d in enumerate (files_3d ):
118
- out_file = fname_presuffix (in_file , suffix = f"_idx-{ i :03} " )
128
+ out_file = str (
129
+ Path (fname_presuffix (in_file , suffix = f"_idx-{ i :03} " )).absolute ()
130
+ )
119
131
file_3d .to_filename (out_file )
120
132
self ._results ['out_files' ].append (out_file )
121
133
@@ -125,6 +137,7 @@ def _run_interface(self, runtime):
125
137
class _MergeSeriesInputSpec (BaseInterfaceInputSpec ):
126
138
in_files = InputMultiObject (File (exists = True , mandatory = True ,
127
139
desc = 'input list of 3d images' ))
140
+ allow_4D = traits .Bool (True , usedefault = True , desc = 'whether 4D images are allowed to be concatenated' )
128
141
129
142
130
143
class _MergeSeriesOutputSpec (TraitedSpec ):
@@ -142,11 +155,14 @@ def _run_interface(self, runtime):
142
155
for f in self .inputs .in_files :
143
156
filenii = nb .load (f )
144
157
filenii = nb .squeeze_image (filenii )
145
- if len (filenii .shape ) != 3 :
146
- raise RuntimeError ('Input image (%s) is not 3D.' % f )
147
- else :
158
+ if filenii .dataobj .ndim == 3 :
148
159
nii_list .append (filenii )
149
- img_4d = nb .funcs .concat_images (nii_list )
160
+ elif self .inputs .allow_4D and filenii .dataobj .ndim == 4 :
161
+ nii_list += nb .four_to_three (filenii )
162
+ else :
163
+ raise ValueError ("Input image has an incorrect number of dimensions"
164
+ f" ({ filenii .dataobj .ndim } )." )
165
+ img_4d = nb .concat_images (nii_list )
150
166
out_file = fname_presuffix (self .inputs .in_files [0 ], suffix = "_merged" )
151
167
img_4d .to_filename (out_file )
152
168
0 commit comments