Skip to content

Instantly share code, notes, and snippets.

@lebedov
Created October 16, 2016 16:14
Show Gist options
  • Save lebedov/d52672be50e65ab1175be4941a6ec3c8 to your computer and use it in GitHub Desktop.
Save lebedov/d52672be50e65ab1175be4941a6ec3c8 to your computer and use it in GitHub Desktop.
Ensure Reorient2Std output is compressed or uncompressed as per output_type trait.
#!/usr/bin/env python
"""
Ensure Reorient2Std output is compressed or uncompressed as per output_type trait.
Notes
-----
Works around possible fslreorient2std bug: https://github.com/nipy/nipype/issues/1683
"""
import logging
import os
from nipype.interfaces.fsl import Reorient2Std
iflogger = logging.getLogger('interface')
class FixedReorient2Std(Reorient2Std):
def _run_wrapper(self, runtime):
runtime = super(FixedReorient2Std, self)._run_wrapper(runtime)
# The absence of the output file is a sign that the input may have been oriented and
# the requested output type differs from that of the actually generated output file:
out_file = self._list_outputs()['out_file']
if not os.path.exists(out_file):
# Input oriented and uncompressed, but compressed output requested:
if self._output_type == 'NIFTI_GZ' and os.path.exists(os.path.splitext(out_file)[0]):
iflogger.debug('explicitly compressing %s' % os.path.splitext(out_file)[0])
os.system('gzip %s' % os.path.splitext(out_file)[0])
# Input oriented and compressed, but uncompressed output requested:
elif self._output_type == 'NIFTI' and os.path.exists(out_file+'.gz'):
iflogger.debug('explicitly uncompressing %s' % (out_file+'.gz'))
os.system('gunzip %s' % (out_file+'.gz'))
return runtime
if __name__ == '__main__':
# To test, obtain MNI152_T1_2mm.nii.gz from FSL distro or some other source,
# uncompress with gunzip -k, and rename the uncompressed and compressed files to
# oriented1.nii and oriented2.nii.gz.
# This should still generate a compressed file ..
r = FixedReorient2Std(in_file='oriented1.nii', output_type='NIFTI_GZ')
r.run()
# .. and this should generate an uncompressed file:
r = FixedReorient2Std(in_file='oriented2.nii.gz', output_type='NIFTI')
r.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment