From 7b72302e8ed8a545ac3a58f64a5d456e7113cf73 Mon Sep 17 00:00:00 2001 From: Patrick Sadil Date: Mon, 8 Jul 2024 08:16:02 -0400 Subject: [PATCH] Add informative error when TotalReadoutTime not in metadata and in_file not provided --- sdcflows/utils/epimanip.py | 14 +++++++++--- sdcflows/utils/tests/test_epimanip.py | 33 +++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 3 deletions(-) create mode 100644 sdcflows/utils/tests/test_epimanip.py diff --git a/sdcflows/utils/epimanip.py b/sdcflows/utils/epimanip.py index 6af5be7f21..836f327b9e 100644 --- a/sdcflows/utils/epimanip.py +++ b/sdcflows/utils/epimanip.py @@ -188,10 +188,14 @@ def get_trt(in_meta, in_file=None): raise ValueError(f"'{trt}'") return trt + elif in_file is None: + msg = "Unable to find TotalReadoutTime in metadata and in_file \ + not defined." + raise AssertionError(msg) # npe = N voxels PE direction pe_index = "ijk".index(in_meta["PhaseEncodingDirection"][0]) - npe = nb.load(in_file).shape[pe_index] + npe = nb.loadsave.load(in_file).shape[pe_index] # Use case 2: EES is defined ees = in_meta.get("EffectiveEchoSpacing") @@ -252,7 +256,9 @@ def epi_mask(in_file, out_file=None): maxnorm = np.percentile(closed[closed > 0], 90) closed = np.clip(closed, a_min=0.0, a_max=maxnorm) # Calculate index of center of masses - cm = tuple(np.round(ndimage.measurements.center_of_mass(closed)).astype(int)) + cm = tuple( + np.round(ndimage.measurements.center_of_mass(closed)).astype(int) + ) # Erode the picture of the brain by a lot eroded = ndimage.grey_erosion(closed, structure=ball(5)) # Calculate the residual @@ -268,6 +274,8 @@ def epi_mask(in_file, out_file=None): hdr = img.header.copy() hdr.set_data_dtype("uint8") nb.Nifti1Image( - ndimage.binary_dilation(labels == 2, ball(2)).astype("uint8"), img.affine, hdr + ndimage.binary_dilation(labels == 2, ball(2)).astype("uint8"), + img.affine, + hdr, ).to_filename(out_file) return out_file diff --git a/sdcflows/utils/tests/test_epimanip.py b/sdcflows/utils/tests/test_epimanip.py new file mode 100644 index 0000000000..d6d08a7eea --- /dev/null +++ b/sdcflows/utils/tests/test_epimanip.py @@ -0,0 +1,33 @@ +# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*- +# vi: set ft=python sts=4 ts=4 sw=4 et: +# +# Copyright 2021 The NiPreps Developers +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# We support and encourage derived works from this project, please read +# about our expectations at +# +# https://www.nipreps.org/community/licensing/ +# +"""Test EPI manipulation routines.""" +import pytest +from sdcflows.utils.epimanip import get_trt + + +def test_get_trt_err_wo_trt_and_in_file(): + """Test that calling get_trt with dict that does not have TotalReadoutTime \ + and no in_file raises AssertionError. + """ + with pytest.raises(AssertionError): + get_trt(in_meta={})