@@ -273,7 +273,6 @@ def _list_outputs(self):
273273
274274class GunzipInputSpec (BaseInterfaceInputSpec ):
275275 in_file = File (exists = True , mandatory = True )
276- compress = traits .Bool (default_value = False )
277276
278277
279278class GunzipOutputSpec (TraitedSpec ):
@@ -288,10 +287,6 @@ class Gunzip(BaseInterface):
288287 >>> res = gunzip.run()
289288 >>> res.outputs.out_file # doctest: +ELLIPSIS
290289 '.../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'
295290
296291 .. testcleanup::
297292
@@ -303,35 +298,71 @@ class Gunzip(BaseInterface):
303298
304299 def _gen_output_file_name (self ):
305300 _ , 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 )
304+
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'
306336
307- if self .inputs .compress :
308- ext += ".gz"
337+ .. testcleanup::
338+
339+ >>> os.unlink('tpms_msk.nii')
340+ """
341+
342+ input_spec = GzipInputSpec
343+
344+ def _gen_output_file_name (self ):
345+ if mode == "decompress" :
346+ filename = super ()._gen_output_file_name ()
309347 else :
310- if ext [ - 3 :]. lower () == ".gz" :
311- ext = ext [: - 3 ]
348+ _ , base , ext = split_filename ( self . inputs . in_file )
349+ filename = os . path . abspath ( base + ext + ".gz" )
312350
313- return os . path . abspath ( base + ext )
351+ return filename
314352
315353 def _run_interface (self , runtime ):
316354 import gzip
317355 import shutil
318356
319- if self .inputs .compress :
357+ if mode == "decompress" :
358+ runtime = super ()._run_interface (runtime )
359+ else :
320360 with open (self .inputs .in_file , "rb" ) as in_file :
321361 with gzip .open (self ._gen_output_file_name (), "wb" ) as out_file :
322362 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 )
327363
328364 return runtime
329365
330- def _list_outputs (self ):
331- outputs = self ._outputs ().get ()
332- outputs ["out_file" ] = self ._gen_output_file_name ()
333- return outputs
334-
335366
336367def replaceext (in_list , ext ):
337368 out_list = list ()
0 commit comments