9
9
"""
10
10
11
11
from __future__ import absolute_import
12
- import os , string
13
- from os import path
12
+ import os
13
+ import string
14
+ import errno
15
+ from os import path as op
14
16
from glob import glob
15
17
from nipype .interfaces .base import (TraitedSpec ,
16
18
DynamicTraitedSpec ,
33
35
except ImportError :
34
36
have_dcmstack = False
35
37
38
+
36
39
def sanitize_path_comp (path_comp ):
37
40
result = []
38
41
for char in path_comp :
@@ -48,8 +51,7 @@ class NiftiGeneratorBaseInputSpec(TraitedSpec):
48
51
"meta data to create the output filename(s)" )
49
52
out_ext = traits .Str ('.nii.gz' , usedefault = True ,
50
53
desc = "Determines output file type" )
51
- use_cwd = traits .Bool (True , usedefault = True ,
52
- desc = 'use interface\' s current working directory' )
54
+ out_path = Directory (desc = 'output path, current working directory if not set' )
53
55
54
56
55
57
class NiftiGeneratorBase (BaseInterface ):
@@ -77,10 +79,20 @@ def _get_out_path(self, meta, idx=None):
77
79
out_fn = (out_fmt % meta ) + self .inputs .out_ext
78
80
out_fn = sanitize_path_comp (out_fn )
79
81
80
- if self .inputs .use_cwd :
81
- return path .join (os .getcwd (), out_fn )
82
- else :
83
- return path .abspath (out_fn )
82
+ out_path = os .getcwd ()
83
+ if isdefined (self .inputs .out_path ):
84
+ out_path = op .abspath (self .inputs .out_path )
85
+
86
+ # now, mkdir -p $out_path
87
+ try :
88
+ os .makedirs (out_path )
89
+ except OSError as exc : # Python >2.5
90
+ if exc .errno == errno .EEXIST and op .isdir (out_path ):
91
+ pass
92
+ else :
93
+ raise
94
+
95
+ return op .join (out_path , out_fn )
84
96
85
97
86
98
class DcmStackInputSpec (NiftiGeneratorBaseInputSpec ):
@@ -100,6 +112,7 @@ class DcmStackInputSpec(NiftiGeneratorBaseInputSpec):
100
112
class DcmStackOutputSpec (TraitedSpec ):
101
113
out_file = File (exists = True )
102
114
115
+
103
116
class DcmStack (NiftiGeneratorBase ):
104
117
'''Create one Nifti file from a set of DICOM files. Can optionally embed
105
118
meta data.
@@ -119,8 +132,8 @@ class DcmStack(NiftiGeneratorBase):
119
132
120
133
def _get_filelist (self , trait_input ):
121
134
if isinstance (trait_input , six .string_types ):
122
- if path .isdir (trait_input ):
123
- return glob (path .join (trait_input , '*.dcm' ))
135
+ if op .isdir (trait_input ):
136
+ return glob (op .join (trait_input , '*.dcm' ))
124
137
else :
125
138
return glob (trait_input )
126
139
@@ -155,9 +168,11 @@ def _list_outputs(self):
155
168
outputs ["out_file" ] = self .out_path
156
169
return outputs
157
170
171
+
158
172
class GroupAndStackOutputSpec (TraitedSpec ):
159
173
out_list = traits .List (desc = "List of output nifti files" )
160
174
175
+
161
176
class GroupAndStack (DcmStack ):
162
177
'''Create (potentially) multiple Nifti files for a set of DICOM files.
163
178
'''
@@ -185,6 +200,7 @@ def _list_outputs(self):
185
200
outputs ["out_list" ] = self .out_list
186
201
return outputs
187
202
203
+
188
204
class LookupMetaInputSpec (TraitedSpec ):
189
205
in_file = File (mandatory = True ,
190
206
exists = True ,
@@ -197,6 +213,7 @@ class LookupMetaInputSpec(TraitedSpec):
197
213
"lookup and the values specify the output names" )
198
214
)
199
215
216
+
200
217
class LookupMeta (BaseInterface ):
201
218
'''Lookup meta data values from a Nifti with embeded meta data.
202
219
@@ -233,13 +250,13 @@ def _outputs(self):
233
250
outputs .add_trait (out_name , traits .Any )
234
251
undefined_traits [out_name ] = Undefined
235
252
outputs .trait_set (trait_change_notify = False , ** undefined_traits )
236
- #Not sure why this is needed
253
+ # Not sure why this is needed
237
254
for out_name in self ._meta_keys .values ():
238
255
_ = getattr (outputs , out_name )
239
256
return outputs
240
257
241
258
def _run_interface (self , runtime ):
242
- #If the 'meta_keys' input is a list, covert it to a dict
259
+ # If the 'meta_keys' input is a list, covert it to a dict
243
260
self ._make_name_map ()
244
261
nw = NiftiWrapper .from_filename (self .inputs .in_file )
245
262
self .result = {}
@@ -253,6 +270,7 @@ def _list_outputs(self):
253
270
outputs .update (self .result )
254
271
return outputs
255
272
273
+
256
274
class CopyMetaInputSpec (TraitedSpec ):
257
275
src_file = File (mandatory = True , exists = True )
258
276
dest_file = File (mandatory = True , exists = True )
@@ -262,9 +280,11 @@ class CopyMetaInputSpec(TraitedSpec):
262
280
exclude_classes = traits .List (desc = "List of meta data "
263
281
"classifications to exclude" )
264
282
283
+
265
284
class CopyMetaOutputSpec (TraitedSpec ):
266
285
dest_file = File (exists = True )
267
286
287
+
268
288
class CopyMeta (BaseInterface ):
269
289
'''Copy meta data from one Nifti file to another. Useful for preserving
270
290
meta data after some processing steps.'''
@@ -296,8 +316,8 @@ def _run_interface(self, runtime):
296
316
dest .meta_ext .slice_dim = src .meta_ext .slice_dim
297
317
dest .meta_ext .shape = src .meta_ext .shape
298
318
299
- self .out_path = path .join (os .getcwd (),
300
- path .basename (self .inputs .dest_file ))
319
+ self .out_path = op .join (os .getcwd (),
320
+ op .basename (self .inputs .dest_file ))
301
321
dest .to_filename (self .out_path )
302
322
303
323
return runtime
@@ -307,6 +327,7 @@ def _list_outputs(self):
307
327
outputs ['dest_file' ] = self .out_path
308
328
return outputs
309
329
330
+
310
331
class MergeNiftiInputSpec (NiftiGeneratorBaseInputSpec ):
311
332
in_files = traits .List (mandatory = True ,
312
333
desc = "List of Nifti files to merge" )
@@ -318,16 +339,19 @@ class MergeNiftiInputSpec(NiftiGeneratorBaseInputSpec):
318
339
"specified, the last singular or "
319
340
"non-existant dimension is used." )
320
341
342
+
321
343
class MergeNiftiOutputSpec (TraitedSpec ):
322
344
out_file = File (exists = True , desc = "Merged Nifti file" )
323
345
346
+
324
347
def make_key_func (meta_keys , index = None ):
325
348
def key_func (src_nii ):
326
349
result = [src_nii .get_meta (key , index ) for key in meta_keys ]
327
350
return result
328
351
329
352
return key_func
330
353
354
+
331
355
class MergeNifti (NiftiGeneratorBase ):
332
356
'''Merge multiple Nifti files into one. Merges together meta data
333
357
extensions as well.'''
@@ -361,18 +385,23 @@ def _list_outputs(self):
361
385
outputs ['out_file' ] = self .out_path
362
386
return outputs
363
387
388
+
364
389
class SplitNiftiInputSpec (NiftiGeneratorBaseInputSpec ):
365
390
in_file = File (exists = True , mandatory = True , desc = "Nifti file to split" )
366
391
split_dim = traits .Int (desc = "Dimension to split along. If not "
367
392
"specified, the last dimension is used." )
368
393
394
+
369
395
class SplitNiftiOutputSpec (TraitedSpec ):
370
396
out_list = traits .List (File (exists = True ),
371
397
desc = "Split Nifti files" )
372
398
399
+
373
400
class SplitNifti (NiftiGeneratorBase ):
374
- '''Split one Nifti file into many along the specified dimension. Each
375
- result has an updated meta data extension as well.'''
401
+ '''
402
+ Split one Nifti file into many along the specified dimension. Each
403
+ result has an updated meta data extension as well.
404
+ '''
376
405
input_spec = SplitNiftiInputSpec
377
406
output_spec = SplitNiftiOutputSpec
378
407
0 commit comments