Skip to content
Snippets Groups Projects
Commit 57cbc79e authored by Paul McCarthy's avatar Paul McCarthy :mountain_bicyclist:
Browse files

Merge branch 'bf/imcp_paths' into 'master'

Bf/imcp paths

See merge request fsl/fslpy!78
parents 5dacef11 3520c334
No related branches found
No related tags found
No related merge requests found
Pipeline #2887 passed
......@@ -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
^^^^^^^
......
......@@ -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.
......
......@@ -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,
......
......@@ -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,
......
......@@ -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')
......
......@@ -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')
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment