diff --git a/CHANGELOG.rst b/CHANGELOG.rst index f9d9dcbe5f8e8a975a6b7bb057df60281bd71e97..e4da31014521e5daff86a632a3679db3fda7b31b 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -11,7 +11,7 @@ Added * New :mod:`fsl.utils.filetree` package for defining and working with file/directory templates. -* Simple built-in `.deprecated` decorator. +* Simple built-in :mod:`.deprecated` decorator. Changed @@ -23,6 +23,14 @@ Changed file handles. +Fixed +^^^^^ + + +* The ``immv`` and ``imcp`` scripts now accept incorrect file extensions + on input arguments. + + Removed ^^^^^^^ diff --git a/fsl/data/image.py b/fsl/data/image.py index 1b407a3f7c773066c13e75b5bb207de03986f9c3..a1e695b59bd81a36a845d3f64167efbf6222ecc5 100644 --- a/fsl/data/image.py +++ b/fsl/data/image.py @@ -1376,6 +1376,20 @@ def removeExt(filename): return fslpath.removeExt(filename, ALLOWED_EXTENSIONS) +def fixExt(filename): + """Fix the extension of ``filename``. + + For example, if a file name is passed in as ``file.nii.gz``, but the + file is actually ``file.nii``, this function will fix the file name. + + If ``filename`` already exists, it is returned unchanged. + """ + if op.exists(filename): + return filename + else: + return addExt(removeExt(filename)) + + def defaultExt(): """Returns the default NIFTI file extension that should be used. diff --git a/fsl/scripts/imcp.py b/fsl/scripts/imcp.py index 77482a28cb2ac7ff9710d796bc01faea466733ce..81f53a83d5fdd682c80f41a208641c3273a8985d 100755 --- a/fsl/scripts/imcp.py +++ b/fsl/scripts/imcp.py @@ -60,6 +60,7 @@ def main(argv=None): return 1 try: + srcs = [fslimage.fixExt(s) for s in srcs] srcs = fslpath.removeDuplicates( srcs, allowedExts=fslimage.ALLOWED_EXTENSIONS, diff --git a/fsl/scripts/immv.py b/fsl/scripts/immv.py index 94c9674099497f7aaa2bea30945af7df15afa92a..c25a99e04c535f3cd26690f5600213e4d3d23ebe 100755 --- a/fsl/scripts/immv.py +++ b/fsl/scripts/immv.py @@ -61,6 +61,7 @@ def main(argv=None): return 1 try: + srcs = [fslimage.fixExt(s) for s in srcs] srcs = fslpath.removeDuplicates( srcs, allowedExts=fslimage.ALLOWED_EXTENSIONS, diff --git a/tests/test_image.py b/tests/test_image.py index d468b4b76a2216b8045b7f2b15b947d4136c6510..2786cc501e3a3eee2bb387cd78c33fd303d79b27 100644 --- a/tests/test_image.py +++ b/tests/test_image.py @@ -504,6 +504,31 @@ def test_defaultExt(): assert fslimage.defaultExt() == e +def test_fixExt(): + with tempdir(): + + # error if if file doesn't exist + with pytest.raises(fslpath.PathError): + fslimage.fixExt('file.nii.gz') + + with open('file.nii', 'w') as f: + f.write('1') + assert fslimage.fixExt('file.nii.gz') == 'file.nii' + assert fslimage.fixExt('file.nii') == 'file.nii' + + with open('file.nii.gz', 'w') as f: + f.write('1') + + assert fslimage.fixExt('file.nii.gz') == 'file.nii.gz' + assert fslimage.fixExt('file.nii') == 'file.nii' + + os.remove('file.nii') + os.remove('file.nii.gz') + with open('file.nii.gz', 'w') as f: + f.write('1') + assert fslimage.fixExt('file.nii') == 'file.nii.gz' + + def test_Image_orientation_analyze_neuro(): _test_Image_orientation(0, 'neuro') def test_Image_orientation_analyze_radio(): _test_Image_orientation(0, 'radio') def test_Image_orientation_nifti1_neuro(): _test_Image_orientation(1, 'neuro') diff --git a/tests/test_immv_imcp.py b/tests/test_immv_imcp.py index 36e9ddae17d2ad99269420606f857c496b22bb40..215b4dc4062245786d4b37af190d626928cefc1c 100644 --- a/tests/test_immv_imcp.py +++ b/tests/test_immv_imcp.py @@ -20,10 +20,11 @@ import pytest import nibabel as nib -import fsl.utils.imcp as imcp -import fsl.scripts.imcp as imcp_script -import fsl.scripts.immv as immv_script -import fsl.data.image as fslimage +from fsl.utils.tempdir import tempdir +import fsl.utils.imcp as imcp +import fsl.scripts.imcp as imcp_script +import fsl.scripts.immv as immv_script +import fsl.data.image as fslimage from . import make_random_image from . import make_dummy_file @@ -730,3 +731,27 @@ def test_imcp_shouldPass(move=False): def test_immv_shouldPass(): test_imcp_shouldPass(move=True) + +def test_imcp_badExt(): + with tempdir(): + + with open('file.nii.gz', 'wt') as f: + f.write('1') + + result = imcp_script.main(['file.nii', 'dest']) + + assert result == 0 + assert op.exists('dest.nii.gz') + + + +def test_immv_badExt(): + with tempdir(): + + with open('file.nii.gz', 'wt') as f: + f.write('1') + + result = immv_script.main(['file.nii', 'dest']) + + assert result == 0 + assert op.exists('dest.nii.gz')