@@ -85,6 +85,11 @@ def validate(self, object, name, value):
8585 return validated_value
8686 elif os .path .isfile (value ):
8787 return validated_value
88+ else :
89+ raise TraitError (
90+ args = 'The trait \' {}\' of {} instance is {}, but the path '
91+ ' \' {}\' does not exist.' .format (name , class_of (object ),
92+ self .info_text , value ))
8893
8994 self .error (object , name , value )
9095
@@ -218,37 +223,67 @@ def __init__(self, value='', auto_set=False, entries=0,
218223 super (Directory , self ).__init__ (value , auto_set , entries , exists ,
219224 ** metadata )
220225
226+ # lists of tuples
227+ # each element consists of :
228+ # - uncompressed (tuple[0]) extension
229+ # - compressed (tuple[1]) extension
230+ img_fmt_types = {
231+ 'nifti1' : [('.nii' , '.nii.gz' ),
232+ (('.hdr' , '.img' ), ('.hdr' , '.img.gz' ))],
233+ 'mgh' : [('.mgh' , '.mgz' ), ('.mgh' , '.mgh.gz' )],
234+ 'nifti2' : [('.nii' , '.nii.gz' ),
235+ (('.hdr' , '.img' ), ('.hdr' , '.img.gz' ))],
236+ 'cifti2' : [('.nii' , '.nii.gz' ),
237+ (('.hdr' , '.img' ), ('.hdr' , '.img.gz' ))],
238+ 'DICOM' : [('.dcm' , '.dcm' ), ('.IMA' , '.IMA' ), ('.tar' , '.tar.gz' )],
239+ 'nrrd' : [('.nrrd' , 'nrrd' ), ('nhdr' , 'nhdr' )],
240+ 'afni' : [('.HEAD' , '.HEAD' ), ('.BRIK' , '.BRIK' )]
241+ }
242+
221243class ImageFile (File ):
222244 """ Defines a trait of specific neuroimaging files """
223245
224246 def __init__ (self , value = '' , filter = None , auto_set = False , entries = 0 ,
225- exists = False , types = ['nii' , 'hdr' , 'img' ], compressed = True ,
247+ exists = False , types = [], compressed = True , extensions = [] ,
226248 ** metadata ):
227249 """ Trait handles neuroimaging files.
228250
229251 Parameters
230252 ----------
231253 types : list
232- The accepted file- types
254+ Strings of file format types accepted
233255 compressed : boolean
234- Indicates whether the file-type can compressed
256+ Indicates whether the file format can compressed
235257 """
236258 self .types = types
237259 self .compressed = compressed
260+ self .exts = extensions
238261 super (ImageFile , self ).__init__ (value , filter , auto_set , entries ,
239262 exists , ** metadata )
240263
264+ def grab_exts (self , exts = []):
265+ for fmt in self .types :
266+ if fmt in img_fmt_types :
267+ exts .extend (sum ([[u for u in y [0 ]] if isinstance (y [0 ], tuple )
268+ else [y [0 ]] for y in img_fmt_types [fmt ]], []))
269+ if self .compressed :
270+ exts .extend (sum ([[u for u in y [- 1 ]] if isinstance (y [- 1 ], tuple )
271+ else [y [- 1 ]] for y in img_fmt_types [fmt ]], []))
272+ else :
273+ raise AttributeError ('Information has not been added for format'
274+ 'type {} yet.' .format (fmt ))
275+ return list (set (exts ))
276+
241277 def validate (self , object , name , value ):
242278 """ Validates that a specified value is valid for this trait.
243279 """
244280 validated_value = super (ImageFile , self ).validate (object , name , value )
245281 if validated_value and self .types :
246- if self .compressed :
247- self .types .extend ([x + '.gz' for x in self .types ])
248- if not any (validated_value .endswith (x ) for x in self .types ):
282+ self .exts = self .grab_exts (self .exts )
283+ if not any (validated_value .endswith (x ) for x in self .exts ):
249284 raise TraitError (
250285 args = "{} is not included in allowed types: {}" .format (
251- validated_value , ',' .join (self .types )))
286+ validated_value , ',' .join (self .exts )))
252287 return validated_value
253288
254289"""
0 commit comments