Skip to content

Commit 2b6921e

Browse files
committed
dcmstack create directory if does not exist. pep8 fixes
1 parent 18dfc5d commit 2b6921e

File tree

1 file changed

+39
-10
lines changed

1 file changed

+39
-10
lines changed

nipype/interfaces/dcmstack.py

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@
99
"""
1010

1111
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
1416
from glob import glob
1517
from nipype.interfaces.base import (TraitedSpec,
1618
DynamicTraitedSpec,
@@ -33,6 +35,7 @@
3335
except ImportError:
3436
have_dcmstack = False
3537

38+
3639
def sanitize_path_comp(path_comp):
3740
result = []
3841
for char in path_comp:
@@ -78,9 +81,18 @@ def _get_out_path(self, meta, idx=None):
7881

7982
out_path = os.getcwd()
8083
if isdefined(self.inputs.out_path):
81-
out_path = path.abspath(self.inputs.out_path)
82-
return path.join(out_path, out_fn)
84+
out_path = op.abspath(self.inputs.out_path)
85+
86+
# now, mkdir -p $out_path
87+
try:
88+
op.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
8394

95+
return op.join(out_path, out_fn)
8496

8597

8698
class DcmStackInputSpec(NiftiGeneratorBaseInputSpec):
@@ -100,6 +112,7 @@ class DcmStackInputSpec(NiftiGeneratorBaseInputSpec):
100112
class DcmStackOutputSpec(TraitedSpec):
101113
out_file = File(exists=True)
102114

115+
103116
class DcmStack(NiftiGeneratorBase):
104117
'''Create one Nifti file from a set of DICOM files. Can optionally embed
105118
meta data.
@@ -119,8 +132,8 @@ class DcmStack(NiftiGeneratorBase):
119132

120133
def _get_filelist(self, trait_input):
121134
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'))
124137
else:
125138
return glob(trait_input)
126139

@@ -155,9 +168,11 @@ def _list_outputs(self):
155168
outputs["out_file"] = self.out_path
156169
return outputs
157170

171+
158172
class GroupAndStackOutputSpec(TraitedSpec):
159173
out_list = traits.List(desc="List of output nifti files")
160174

175+
161176
class GroupAndStack(DcmStack):
162177
'''Create (potentially) multiple Nifti files for a set of DICOM files.
163178
'''
@@ -185,6 +200,7 @@ def _list_outputs(self):
185200
outputs["out_list"] = self.out_list
186201
return outputs
187202

203+
188204
class LookupMetaInputSpec(TraitedSpec):
189205
in_file = File(mandatory=True,
190206
exists=True,
@@ -197,6 +213,7 @@ class LookupMetaInputSpec(TraitedSpec):
197213
"lookup and the values specify the output names")
198214
)
199215

216+
200217
class LookupMeta(BaseInterface):
201218
'''Lookup meta data values from a Nifti with embeded meta data.
202219
@@ -253,6 +270,7 @@ def _list_outputs(self):
253270
outputs.update(self.result)
254271
return outputs
255272

273+
256274
class CopyMetaInputSpec(TraitedSpec):
257275
src_file = File(mandatory=True, exists=True)
258276
dest_file = File(mandatory=True, exists=True)
@@ -262,9 +280,11 @@ class CopyMetaInputSpec(TraitedSpec):
262280
exclude_classes = traits.List(desc="List of meta data "
263281
"classifications to exclude")
264282

283+
265284
class CopyMetaOutputSpec(TraitedSpec):
266285
dest_file = File(exists=True)
267286

287+
268288
class CopyMeta(BaseInterface):
269289
'''Copy meta data from one Nifti file to another. Useful for preserving
270290
meta data after some processing steps.'''
@@ -296,8 +316,8 @@ def _run_interface(self, runtime):
296316
dest.meta_ext.slice_dim = src.meta_ext.slice_dim
297317
dest.meta_ext.shape = src.meta_ext.shape
298318

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))
301321
dest.to_filename(self.out_path)
302322

303323
return runtime
@@ -307,6 +327,7 @@ def _list_outputs(self):
307327
outputs['dest_file'] = self.out_path
308328
return outputs
309329

330+
310331
class MergeNiftiInputSpec(NiftiGeneratorBaseInputSpec):
311332
in_files = traits.List(mandatory=True,
312333
desc="List of Nifti files to merge")
@@ -318,16 +339,19 @@ class MergeNiftiInputSpec(NiftiGeneratorBaseInputSpec):
318339
"specified, the last singular or "
319340
"non-existant dimension is used.")
320341

342+
321343
class MergeNiftiOutputSpec(TraitedSpec):
322344
out_file = File(exists=True, desc="Merged Nifti file")
323345

346+
324347
def make_key_func(meta_keys, index=None):
325348
def key_func(src_nii):
326349
result = [src_nii.get_meta(key, index) for key in meta_keys]
327350
return result
328351

329352
return key_func
330353

354+
331355
class MergeNifti(NiftiGeneratorBase):
332356
'''Merge multiple Nifti files into one. Merges together meta data
333357
extensions as well.'''
@@ -361,18 +385,23 @@ def _list_outputs(self):
361385
outputs['out_file'] = self.out_path
362386
return outputs
363387

388+
364389
class SplitNiftiInputSpec(NiftiGeneratorBaseInputSpec):
365390
in_file = File(exists=True, mandatory=True, desc="Nifti file to split")
366391
split_dim = traits.Int(desc="Dimension to split along. If not "
367392
"specified, the last dimension is used.")
368393

394+
369395
class SplitNiftiOutputSpec(TraitedSpec):
370396
out_list = traits.List(File(exists=True),
371397
desc="Split Nifti files")
372398

399+
373400
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+
'''
376405
input_spec = SplitNiftiInputSpec
377406
output_spec = SplitNiftiOutputSpec
378407

0 commit comments

Comments
 (0)