Skip to content

Commit 795d277

Browse files
authored
Merge pull request #3472 from ghisvail/enh/gunzip-compression
ENH: Enable compression with Gunzip interface
2 parents 8b21e4b + c5b1fe3 commit 795d277

File tree

3 files changed

+85
-14
lines changed

3 files changed

+85
-14
lines changed

nipype/algorithms/misc.py

Lines changed: 50 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -271,43 +271,58 @@ def _list_outputs(self):
271271
return outputs
272272

273273

274-
class GunzipInputSpec(BaseInterfaceInputSpec):
275-
in_file = File(exists=True, mandatory=True)
274+
class GzipInputSpec(TraitedSpec):
275+
in_file = File(exists=True, mandatory=True, desc="file to (de)compress")
276+
mode = traits.Enum(
277+
"compress", "decompress", usedefault=True, desc="compress or decompress"
278+
)
276279

277280

278-
class GunzipOutputSpec(TraitedSpec):
279-
out_file = File(exists=True)
281+
class GzipOutputSpec(TraitedSpec):
282+
out_file = File()
280283

281284

282-
class Gunzip(BaseInterface):
283-
"""Gunzip wrapper
285+
class Gzip(BaseInterface):
286+
"""Gzip wrapper
284287
285-
>>> from nipype.algorithms.misc import Gunzip
286-
>>> gunzip = Gunzip(in_file='tpms_msk.nii.gz')
287-
>>> res = gunzip.run()
288+
>>> from nipype.algorithms.misc import Gzip
289+
>>> gzip = Gzip(in_file='tpms_msk.nii.gz', mode="decompress")
290+
>>> res = gzip.run()
288291
>>> res.outputs.out_file # doctest: +ELLIPSIS
289292
'.../tpms_msk.nii'
290293
294+
>>> gzip = Gzip(in_file='tpms_msk.nii')
295+
>>> res = gzip.run()
296+
>>> res.outputs.out_file # doctest: +ELLIPSIS
297+
'.../tpms_msk.nii.gz'
298+
291299
.. testcleanup::
292300
293301
>>> os.unlink('tpms_msk.nii')
294302
"""
295303

296-
input_spec = GunzipInputSpec
297-
output_spec = GunzipOutputSpec
304+
input_spec = GzipInputSpec
305+
output_spec = GzipOutputSpec
298306

299307
def _gen_output_file_name(self):
300308
_, base, ext = split_filename(self.inputs.in_file)
301-
if ext[-3:].lower() == ".gz":
309+
if self.inputs.mode == "decompress" and ext[-3:].lower() == ".gz":
302310
ext = ext[:-3]
311+
elif self.inputs.mode == "compress":
312+
ext = f"{ext}.gz"
303313
return os.path.abspath(base + ext)
304314

305315
def _run_interface(self, runtime):
306316
import gzip
307317
import shutil
308318

309-
with gzip.open(self.inputs.in_file, "rb") as in_file:
310-
with open(self._gen_output_file_name(), "wb") as out_file:
319+
if self.inputs.mode == "compress":
320+
open_input, open_output = open, gzip.open
321+
else:
322+
open_input, open_output = gzip.open, open
323+
324+
with open_input(self.inputs.in_file, "rb") as in_file:
325+
with open_output(self._gen_output_file_name(), "wb") as out_file:
311326
shutil.copyfileobj(in_file, out_file)
312327
return runtime
313328

@@ -317,6 +332,27 @@ def _list_outputs(self):
317332
return outputs
318333

319334

335+
class GunzipInputSpec(GzipInputSpec):
336+
mode = traits.Enum("decompress", usedefault=True, desc="decompress or compress")
337+
338+
339+
class Gunzip(Gzip):
340+
"""Gunzip wrapper
341+
342+
>>> from nipype.algorithms.misc import Gunzip
343+
>>> gunzip = Gunzip(in_file='tpms_msk.nii.gz')
344+
>>> res = gunzip.run()
345+
>>> res.outputs.out_file # doctest: +ELLIPSIS
346+
'.../tpms_msk.nii'
347+
348+
.. testcleanup::
349+
350+
>>> os.unlink('tpms_msk.nii')
351+
"""
352+
353+
input_spec = GunzipInputSpec
354+
355+
320356
def replaceext(in_list, ext):
321357
out_list = list()
322358
for filename in in_list:

nipype/algorithms/tests/test_auto_Gunzip.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ def test_Gunzip_inputs():
88
extensions=None,
99
mandatory=True,
1010
),
11+
mode=dict(
12+
usedefault=True,
13+
),
1114
)
1215
inputs = Gunzip.input_spec()
1316

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)