From c3245f023d757d2afcb240fb53f0a0f61ac8e722 Mon Sep 17 00:00:00 2001 From: Paul McCarthy <pauldmccarthy@gmail.com> Date: Fri, 20 Aug 2021 16:13:08 +0100 Subject: [PATCH] MNT: Apply same minor optimisations to other im* scripts - avoid expensive imports --- fsl/scripts/imglob.py | 3 +-- fsl/scripts/imln.py | 31 +++++++++++++++++-------------- fsl/scripts/imrm.py | 20 ++++++++++---------- fsl/scripts/imtest.py | 26 +++++++++++++++----------- fsl/scripts/remove_ext.py | 20 ++++++++++---------- 5 files changed, 53 insertions(+), 47 deletions(-) diff --git a/fsl/scripts/imglob.py b/fsl/scripts/imglob.py index 8c8c631e2..87314e8fb 100644 --- a/fsl/scripts/imglob.py +++ b/fsl/scripts/imglob.py @@ -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). """ diff --git a/fsl/scripts/imln.py b/fsl/scripts/imln.py index 88955f3cb..74bcaf395 100644 --- a/fsl/scripts/imln.py +++ b/fsl/scripts/imln.py @@ -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: diff --git a/fsl/scripts/imrm.py b/fsl/scripts/imrm.py index bd8110332..7d8287b6e 100644 --- a/fsl/scripts/imrm.py +++ b/fsl/scripts/imrm.py @@ -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}' diff --git a/fsl/scripts/imtest.py b/fsl/scripts/imtest.py index f03c28442..b0fed7c27 100644 --- a/fsl/scripts/imtest.py +++ b/fsl/scripts/imtest.py @@ -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: diff --git a/fsl/scripts/remove_ext.py b/fsl/scripts/remove_ext.py index fc21094ff..54e62f573 100644 --- a/fsl/scripts/remove_ext.py +++ b/fsl/scripts/remove_ext.py @@ -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)) -- GitLab