@@ -273,7 +273,6 @@ def _list_outputs(self):
273
273
274
274
class GunzipInputSpec (BaseInterfaceInputSpec ):
275
275
in_file = File (exists = True , mandatory = True )
276
- compress = traits .Bool (default_value = False )
277
276
278
277
279
278
class GunzipOutputSpec (TraitedSpec ):
@@ -288,10 +287,6 @@ class Gunzip(BaseInterface):
288
287
>>> res = gunzip.run()
289
288
>>> res.outputs.out_file # doctest: +ELLIPSIS
290
289
'.../tpms_msk.nii'
291
- >>> gunzip = Gunzip(in_file='tpms_msk.nii', compress=True)
292
- >>> res = gunzip.run()
293
- >>> res.outputs.out_file # doctest: +ELLIPSIS
294
- '.../tpms_msk.nii.gz'
295
290
296
291
.. testcleanup::
297
292
@@ -303,35 +298,70 @@ class Gunzip(BaseInterface):
303
298
304
299
def _gen_output_file_name (self ):
305
300
_ , base , ext = split_filename (self .inputs .in_file )
301
+ if ext [- 3 :].lower () == ".gz" :
302
+ ext = ext [:- 3 ]
303
+ return os .path .abspath (base + ext )
306
304
307
- if self .inputs .compress :
308
- ext += ".gz"
305
+ def _run_interface (self , runtime ):
306
+ import gzip
307
+ import shutil
308
+
309
+ with gzip .open (self .inputs .in_file , "rb" ) as in_file :
310
+ with open (self ._gen_output_file_name (), "wb" ) as out_file :
311
+ shutil .copyfileobj (in_file , out_file )
312
+ return runtime
313
+
314
+ def _list_outputs (self ):
315
+ outputs = self ._outputs ().get ()
316
+ outputs ["out_file" ] = self ._gen_output_file_name ()
317
+ return outputs
318
+
319
+
320
+ class GzipInputSpec (GunzipInputSpec ):
321
+ mode = traits .Enum ("compress" , "decompress" , usedefault = True )
322
+
323
+
324
+ class Gzip (Gunzip ):
325
+ """Gzip wrapper supporting both compression and decompression.
326
+
327
+ >>> from nipype.algorithms.misc import Gzip
328
+ >>> gzip = Gzip(in_file='tpms_msk.nii.gz', mode="decompress")
329
+ >>> res = gzip.run()
330
+ >>> res.outputs.out_file # doctest: +ELLIPSIS
331
+ '.../tpms_msk.nii'
332
+ >>> gzip = Gzip(in_file='tpms_msk.nii')
333
+ >>> res = gzip.run()
334
+ >>> res.outputs.out_file # doctest: +ELLIPSIS
335
+ '.../tpms_msk.nii.gz'
336
+
337
+ .. testcleanup::
338
+
339
+ >>> os.unlink('tpms_msk.nii')
340
+ """
341
+ input_spec = GzipInputSpec
342
+
343
+ def _gen_output_file_name (self ):
344
+ if mode == "decompress" :
345
+ filename = super ()._gen_output_file_name ()
309
346
else :
310
- if ext [ - 3 :]. lower () == ".gz" :
311
- ext = ext [: - 3 ]
347
+ _ , base , ext = split_filename ( self . inputs . in_file )
348
+ filename = os . path . abspath ( base + ext + ".gz" )
312
349
313
- return os . path . abspath ( base + ext )
350
+ return filename
314
351
315
352
def _run_interface (self , runtime ):
316
353
import gzip
317
354
import shutil
318
355
319
- if self .inputs .compress :
356
+ if mode == "decompress" :
357
+ runtime = super ()._run_interface (runtime )
358
+ else :
320
359
with open (self .inputs .in_file , "rb" ) as in_file :
321
360
with gzip .open (self ._gen_output_file_name (), "wb" ) as out_file :
322
361
shutil .copyfileobj (in_file , out_file )
323
- else :
324
- with gzip .open (self .inputs .in_file , "rb" ) as in_file :
325
- with open (self ._gen_output_file_name (), "wb" ) as out_file :
326
- shutil .copyfileobj (in_file , out_file )
327
362
328
363
return runtime
329
364
330
- def _list_outputs (self ):
331
- outputs = self ._outputs ().get ()
332
- outputs ["out_file" ] = self ._gen_output_file_name ()
333
- return outputs
334
-
335
365
336
366
def replaceext (in_list , ext ):
337
367
out_list = list ()
0 commit comments