Skip to content

Commit e9da30c

Browse files
committed
ENH: Move compression to separate Gzip interface
Rationales: Keep the Gunzip interface consistent with the behavior of `gunzip` on the CLI, which is basically `gzip` with decompression mode set nowadays.
1 parent 3b61513 commit e9da30c

File tree

3 files changed

+83
-21
lines changed

3 files changed

+83
-21
lines changed

nipype/algorithms/misc.py

Lines changed: 51 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,6 @@ def _list_outputs(self):
273273

274274
class GunzipInputSpec(BaseInterfaceInputSpec):
275275
in_file = File(exists=True, mandatory=True)
276-
compress = traits.Bool(default_value=False)
277276

278277

279278
class 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
@@ -304,27 +299,17 @@ class Gunzip(BaseInterface):
304299
def _gen_output_file_name(self):
305300
_, base, ext = split_filename(self.inputs.in_file)
306301

307-
if self.inputs.compress:
308-
ext += ".gz"
309-
else:
310-
if ext[-3:].lower() == ".gz":
311-
ext = ext[:-3]
312-
302+
if ext[-3:].lower() == ".gz":
303+
ext = ext[:-3]
313304
return os.path.abspath(base + ext)
314305

315306
def _run_interface(self, runtime):
316307
import gzip
317308
import shutil
318309

319-
if self.inputs.compress:
320-
with open(self.inputs.in_file, "rb") as in_file:
321-
with gzip.open(self._gen_output_file_name(), "wb") as out_file:
322-
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-
310+
with gzip.open(self.inputs.in_file, "rb") as in_file:
311+
with open(self._gen_output_file_name(), "wb") as out_file:
312+
shutil.copyfileobj(in_file, out_file)
328313
return runtime
329314

330315
def _list_outputs(self):
@@ -333,6 +318,52 @@ def _list_outputs(self):
333318
return outputs
334319

335320

321+
class GzipInputSpec(GunzipInputSpec):
322+
mode = traits.Enum("compress", "decompress", usedefault=True)
323+
324+
325+
class Gzip(Gunzip):
326+
"""Gzip wrapper supporting both compression and decompression.
327+
328+
>>> from nipype.algorithms.misc import Gzip
329+
>>> gzip = Gzip(in_file='tpms_msk.nii.gz', mode="decompress")
330+
>>> res = gzip.run()
331+
>>> res.outputs.out_file # doctest: +ELLIPSIS
332+
'.../tpms_msk.nii'
333+
>>> gzip = Gzip(in_file='tpms_msk.nii')
334+
>>> res = gzip.run()
335+
>>> res.outputs.out_file # doctest: +ELLIPSIS
336+
'.../tpms_msk.nii.gz'
337+
338+
.. testcleanup::
339+
340+
>>> os.unlink('tpms_msk.nii')
341+
"""
342+
input_spec = GzipInputSpec
343+
344+
def _gen_output_file_name(self):
345+
if mode == "decompress":
346+
filename = super()._gen_output_file_name()
347+
else:
348+
_, base, ext = split_filename(self.inputs.in_file)
349+
filename = os.path.abspath(base + ext + ".gz")
350+
351+
return filename
352+
353+
def _run_interface(self, runtime):
354+
import gzip
355+
import shutil
356+
357+
if mode == "decompress":
358+
runtime = super()._run_interface(runtime)
359+
else:
360+
with open(self.inputs.in_file, "rb") as in_file:
361+
with gzip.open(self._gen_output_file_name(), "wb") as out_file:
362+
shutil.copyfileobj(in_file, out_file)
363+
364+
return runtime
365+
366+
336367
def replaceext(in_list, ext):
337368
out_list = list()
338369
for filename in in_list:

nipype/algorithms/tests/test_auto_Gunzip.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
def test_Gunzip_inputs():
66
input_map = dict(
7-
compress=dict(),
87
in_file=dict(
98
extensions=None,
109
mandatory=True,
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# AUTO-GENERATED by tools/checkspecs.py - DO NOT EDIT
2+
from ..misc import Gzip
3+
4+
5+
def test_Gzip_inputs():
6+
input_map = dict(
7+
in_file=dict(
8+
extensions=None,
9+
mandatory=True,
10+
),
11+
mode=dict(
12+
usedefault=True,
13+
),
14+
)
15+
inputs = Gzip.input_spec()
16+
17+
for key, metadata in list(input_map.items()):
18+
for metakey, value in list(metadata.items()):
19+
assert getattr(inputs.traits()[key], metakey) == value
20+
21+
22+
def test_Gzip_outputs():
23+
output_map = dict(
24+
out_file=dict(
25+
extensions=None,
26+
),
27+
)
28+
outputs = Gzip.output_spec()
29+
30+
for key, metadata in list(output_map.items()):
31+
for metakey, value in list(metadata.items()):
32+
assert getattr(outputs.traits()[key], metakey) == value

0 commit comments

Comments
 (0)