Skip to content

Commit 3e4128e

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 3e4128e

File tree

3 files changed

+82
-21
lines changed

3 files changed

+82
-21
lines changed

nipype/algorithms/misc.py

Lines changed: 50 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
@@ -303,35 +298,70 @@ 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)
306304

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()
309346
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")
312349

313-
return os.path.abspath(base + ext)
350+
return filename
314351

315352
def _run_interface(self, runtime):
316353
import gzip
317354
import shutil
318355

319-
if self.inputs.compress:
356+
if mode == "decompress":
357+
runtime = super()._run_interface(runtime)
358+
else:
320359
with open(self.inputs.in_file, "rb") as in_file:
321360
with gzip.open(self._gen_output_file_name(), "wb") as out_file:
322361
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)
327362

328363
return runtime
329364

330-
def _list_outputs(self):
331-
outputs = self._outputs().get()
332-
outputs["out_file"] = self._gen_output_file_name()
333-
return outputs
334-
335365

336366
def replaceext(in_list, ext):
337367
out_list = 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)