From 0a53adc1a38d906eb1460db8de472fdf8d19aa80 Mon Sep 17 00:00:00 2001 From: Philippe Karan Date: Mon, 23 Oct 2023 12:04:36 -0400 Subject: [PATCH 1/3] Moving load_img to io.utils --- scilpy/io/utils.py | 24 ++++++++++++++++++++++++ scripts/scil_image_math.py | 26 ++------------------------ 2 files changed, 26 insertions(+), 24 deletions(-) diff --git a/scilpy/io/utils.py b/scilpy/io/utils.py index 843daba24..30f774bb5 100644 --- a/scilpy/io/utils.py +++ b/scilpy/io/utils.py @@ -20,6 +20,7 @@ from scilpy.io.streamlines import load_tractogram_with_reference from scilpy.utils.bvec_bval_tools import DEFAULT_B0_THRESHOLD from scilpy.utils.filenames import split_name_with_nii +from scilpy.utils.util import is_float eddy_options = ["mb", "mb_offs", "slspec", "mporder", "s2v_lambda", "field", "field_mat", "flm", "slm", "fwhm", "niter", "s2v_niter", @@ -35,6 +36,29 @@ axis_name_choices = ["axial", "coronal", "sagittal"] +def load_img(arg): + if is_float(arg): + img = float(arg) + dtype = np.float64 + else: + if not os.path.isfile(arg): + raise ValueError('Input file {} does not exist.'.format(arg)) + img = nib.load(arg) + shape = img.header.get_data_shape() + dtype = img.header.get_data_dtype() + logging.info('Loaded {} of shape {} and data_type {}.'.format( + arg, shape, dtype)) + + if len(shape) > 3: + logging.warning('{} has {} dimensions, be careful.'.format( + arg, len(shape))) + elif len(shape) < 3: + raise ValueError('{} has {} dimensions, not valid.'.format( + arg, len(shape))) + + return img, dtype + + def link_bundles_and_reference(parser, args, input_tractogram_list): """ Associate the bundle to their reference (if they require a reference). diff --git a/scripts/scil_image_math.py b/scripts/scil_image_math.py index 4819280c7..ef5feecf1 100755 --- a/scripts/scil_image_math.py +++ b/scripts/scil_image_math.py @@ -24,7 +24,8 @@ from scilpy.image.volume_math import (get_image_ops, get_operations_doc) from scilpy.io.utils import (add_overwrite_arg, add_verbose_arg, - assert_outputs_exist) + assert_outputs_exist, + load_img) from scilpy.utils.util import is_float OPERATIONS = get_image_ops() @@ -59,29 +60,6 @@ def _build_arg_parser(): return p -def load_img(arg): - if is_float(arg): - img = float(arg) - dtype = np.float64 - else: - if not os.path.isfile(arg): - raise ValueError('Input file {} does not exist.'.format(arg)) - img = nib.load(arg) - shape = img.header.get_data_shape() - dtype = img.header.get_data_dtype() - logging.info('Loaded {} of shape {} and data_type {}.'.format( - arg, shape, dtype)) - - if len(shape) > 3: - logging.warning('{} has {} dimensions, be careful.'.format( - arg, len(shape))) - elif len(shape) < 3: - raise ValueError('{} has {} dimensions, not valid.'.format( - arg, len(shape))) - - return img, dtype - - def main(): parser = _build_arg_parser() args = parser.parse_args() From d5784d7ec57482e757ded15a9266292f270eb8ee Mon Sep 17 00:00:00 2001 From: Philippe Karan Date: Mon, 23 Oct 2023 12:10:42 -0400 Subject: [PATCH 2/3] Moving it to io.image instead --- scilpy/io/image.py | 25 +++++++++++++++++++++++++ scilpy/io/utils.py | 24 ------------------------ scripts/scil_image_math.py | 4 ++-- 3 files changed, 27 insertions(+), 26 deletions(-) diff --git a/scilpy/io/image.py b/scilpy/io/image.py index 727e9b4a2..bcccc73e7 100644 --- a/scilpy/io/image.py +++ b/scilpy/io/image.py @@ -5,6 +5,31 @@ import numpy as np import os +from scilpy.utils.util import is_float + + +def load_img(arg): + if is_float(arg): + img = float(arg) + dtype = np.float64 + else: + if not os.path.isfile(arg): + raise ValueError('Input file {} does not exist.'.format(arg)) + img = nib.load(arg) + shape = img.header.get_data_shape() + dtype = img.header.get_data_dtype() + logging.info('Loaded {} of shape {} and data_type {}.'.format( + arg, shape, dtype)) + + if len(shape) > 3: + logging.warning('{} has {} dimensions, be careful.'.format( + arg, len(shape))) + elif len(shape) < 3: + raise ValueError('{} has {} dimensions, not valid.'.format( + arg, len(shape))) + + return img, dtype + def merge_labels_into_mask(atlas, filtering_args): """ diff --git a/scilpy/io/utils.py b/scilpy/io/utils.py index 69176363d..d29f9907c 100644 --- a/scilpy/io/utils.py +++ b/scilpy/io/utils.py @@ -20,7 +20,6 @@ from scilpy.io.streamlines import load_tractogram_with_reference from scilpy.utils.bvec_bval_tools import DEFAULT_B0_THRESHOLD from scilpy.utils.filenames import split_name_with_nii -from scilpy.utils.util import is_float eddy_options = ["mb", "mb_offs", "slspec", "mporder", "s2v_lambda", "field", "field_mat", "flm", "slm", "fwhm", "niter", "s2v_niter", @@ -36,29 +35,6 @@ axis_name_choices = ["axial", "coronal", "sagittal"] -def load_img(arg): - if is_float(arg): - img = float(arg) - dtype = np.float64 - else: - if not os.path.isfile(arg): - raise ValueError('Input file {} does not exist.'.format(arg)) - img = nib.load(arg) - shape = img.header.get_data_shape() - dtype = img.header.get_data_dtype() - logging.info('Loaded {} of shape {} and data_type {}.'.format( - arg, shape, dtype)) - - if len(shape) > 3: - logging.warning('{} has {} dimensions, be careful.'.format( - arg, len(shape))) - elif len(shape) < 3: - raise ValueError('{} has {} dimensions, not valid.'.format( - arg, len(shape))) - - return img, dtype - - def link_bundles_and_reference(parser, args, input_tractogram_list): """ Associate the bundle to their reference (if they require a reference). diff --git a/scripts/scil_image_math.py b/scripts/scil_image_math.py index ef5feecf1..508380a66 100755 --- a/scripts/scil_image_math.py +++ b/scripts/scil_image_math.py @@ -22,10 +22,10 @@ import numpy as np from scilpy.image.volume_math import (get_image_ops, get_operations_doc) +from scilpy.io.image import load_img from scilpy.io.utils import (add_overwrite_arg, add_verbose_arg, - assert_outputs_exist, - load_img) + assert_outputs_exist) from scilpy.utils.util import is_float OPERATIONS = get_image_ops() From dc7c2bf4c31a93443a1ea83c98ae1a607c50c4e3 Mon Sep 17 00:00:00 2001 From: Philippe Karan Date: Mon, 23 Oct 2023 12:12:23 -0400 Subject: [PATCH 3/3] Fixing imports --- scilpy/io/image.py | 1 + 1 file changed, 1 insertion(+) diff --git a/scilpy/io/image.py b/scilpy/io/image.py index bcccc73e7..f2739d38d 100644 --- a/scilpy/io/image.py +++ b/scilpy/io/image.py @@ -2,6 +2,7 @@ from dipy.io.utils import is_header_compatible import logging +import nibabel as nib import numpy as np import os