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

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

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