Commit c3245f02 authored by Paul McCarthy's avatar Paul McCarthy 🚵
Browse files

MNT: Apply same minor optimisations to other im* scripts - avoid expensive imports

parent d652c88e
......@@ -11,7 +11,6 @@ NIFTI/ANALYZE image files.
import sys
import glob
import itertools as it
import fsl.utils.path as fslpath
......@@ -30,7 +29,7 @@ exts = ['.nii.gz', '.nii', '.img', '.hdr', '.img.gz', '.hdr.gz']
"""List of supported image file extensions. """
groups = [('.hdr', '.img'), ('.hdr.gz', '.img.gz')]
groups = [('.hdr', '.img'), ('.hdr.gz', '.img.gz')]
"""List of known image file groups (image/header file pairs). """
......
......@@ -17,19 +17,22 @@ to NIFTI image files.
import os.path as op
import os
import sys
import warnings
import fsl.utils.path as fslpath
# See atlasq.py for explanation
with warnings.catch_warnings():
warnings.filterwarnings("ignore", category=FutureWarning)
import fsl.data.image as fslimage
# The lists below are defined in the
# fsl.data.image class, but are duplicated
# here for performance (to avoid import of
# nibabel/numpy/etc).
exts = ['.nii.gz', '.nii',
'.img', '.hdr',
'.img.gz', '.hdr.gz',
'.mnc', '.mnc.gz']
"""List of file extensions that are supported by ``imtest``.
"""
ALLOWED_EXTENSIONS = fslimage.ALLOWED_EXTENSIONS + ['.mnc', '.mnc.gz']
"""List of file extensions that are supported by ``imln``. """
groups = [('.hdr', '.img'), ('.hdr.gz', '.img.gz')]
"""List of known image file groups (image/header file pairs). """
usage = """
......@@ -50,8 +53,8 @@ def main(argv=None):
return 1
target, linkbase = argv
target = fslpath.removeExt(target, ALLOWED_EXTENSIONS)
linkbase = fslpath.removeExt(linkbase, ALLOWED_EXTENSIONS)
target = fslpath.removeExt(target, exts)
linkbase = fslpath.removeExt(linkbase, exts)
# Target must exist, so we can
# infer the correct extension(s).
......@@ -59,8 +62,8 @@ def main(argv=None):
# (e.g. a.img without a.hdr).
try:
targets = fslpath.getFileGroup(target,
allowedExts=ALLOWED_EXTENSIONS,
fileGroups=fslimage.FILE_GROUPS,
allowedExts=exts,
fileGroups=groups,
unambiguous=True)
except Exception as e:
print(f'Error: {e}')
......@@ -70,7 +73,7 @@ def main(argv=None):
if not op.exists(target):
continue
ext = fslpath.getExt(target, ALLOWED_EXTENSIONS)
ext = fslpath.getExt(target, exts)
link = f'{linkbase}{ext}'
try:
......
......@@ -13,22 +13,22 @@ import itertools as it
import os.path as op
import os
import sys
import warnings
import fsl.utils.path as fslpath
# See atlasq.py for explanation
with warnings.catch_warnings():
warnings.filterwarnings("ignore", category=FutureWarning)
import fsl.data.image as fslimage
usage = """Usage: imrm <list of image names to remove>
NB: filenames can be basenames or not
""".strip()
ALLOWED_EXTENSIONS = fslimage.ALLOWED_EXTENSIONS + ['.mnc', '.mnc.gz']
# This list is defined in the
# fsl.data.image class, but are duplicated
# here for performance (to avoid import of
# nibabel/numpy/etc).
exts = ['.nii.gz', '.nii',
'.img', '.hdr',
'.img.gz', '.hdr.gz',
'.mnc', '.mnc.gz']
"""List of file extensions that are removed by ``imrm``. """
......@@ -42,9 +42,9 @@ def main(argv=None):
print(usage)
return 1
prefixes = [fslpath.removeExt(p, ALLOWED_EXTENSIONS) for p in argv]
prefixes = [fslpath.removeExt(p, exts) for p in argv]
for prefix, ext in it.product(prefixes, ALLOWED_EXTENSIONS):
for prefix, ext in it.product(prefixes, exts):
path = f'{prefix}{ext}'
......
......@@ -11,18 +11,22 @@ not, without having to know the file suffix (.nii, .nii.gz, etc).
import os.path as op
import sys
import warnings
import fsl.utils.path as fslpath
# See atlasq.py for explanation
with warnings.catch_warnings():
warnings.filterwarnings("ignore", category=FutureWarning)
import fsl.data.image as fslimage
# The lists below are defined in the
# fsl.data.image class, but are duplicated
# here for performance (to avoid import of
# nibabel/numpy/etc).
exts = ['.nii.gz', '.nii',
'.img', '.hdr',
'.img.gz', '.hdr.gz',
'.mnc', '.mnc.gz']
"""List of file extensions that are supported by ``imtest``.
"""
ALLOWED_EXTENSIONS = fslimage.ALLOWED_EXTENSIONS + ['.mnc', '.mnc.gz']
"""List of file extensions that are supported by ``imln``. """
groups = [('.hdr', '.img'), ('.hdr.gz', '.img.gz')]
"""List of known image file groups (image/header file pairs). """
def main(argv=None):
......@@ -38,7 +42,7 @@ def main(argv=None):
print('0')
return 0
path = fslpath.removeExt(argv[0], ALLOWED_EXTENSIONS)
path = fslpath.removeExt(argv[0], exts)
path = op.realpath(path)
# getFileGroup will raise an error
......@@ -47,8 +51,8 @@ def main(argv=None):
# image) does not exist
try:
fslpath.getFileGroup(path,
allowedExts=ALLOWED_EXTENSIONS,
fileGroups=fslimage.FILE_GROUPS,
allowedExts=exts,
fileGroups=groups,
unambiguous=True)
print('1')
except fslpath.PathError:
......
......@@ -6,22 +6,22 @@
#
import sys
import warnings
import sys
import fsl.utils.path as fslpath
# See atlasq.py for explanation
with warnings.catch_warnings():
warnings.filterwarnings("ignore", category=FutureWarning)
import fsl.data.image as fslimage
usage = """Usage: remove_ext <list of image paths to remove extension from>
""".strip()
ALLOWED_EXTENSIONS = fslimage.ALLOWED_EXTENSIONS + ['.mnc', '.mnc.gz']
# This list is defined in the
# fsl.data.image class, but are duplicated
# here for performance (to avoid import of
# nibabel/numpy/etc).
exts = ['.nii.gz', '.nii',
'.img', '.hdr',
'.img.gz', '.hdr.gz',
'.mnc', '.mnc.gz']
"""List of file extensions that are removed by ``remove_ext``. """
......@@ -40,7 +40,7 @@ def main(argv=None):
removed = []
for path in argv:
removed.append(fslpath.removeExt(path, ALLOWED_EXTENSIONS))
removed.append(fslpath.removeExt(path, exts))
print(' '.join(removed))
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment