Skip to content

Commit

Permalink
Add informative error when TotalReadoutTime not in metadata and in_fi…
Browse files Browse the repository at this point in the history
…le not provided
  • Loading branch information
psadil committed Jul 8, 2024
1 parent 1a11859 commit 7b72302
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
14 changes: 11 additions & 3 deletions sdcflows/utils/epimanip.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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
Expand All @@ -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
33 changes: 33 additions & 0 deletions sdcflows/utils/tests/test_epimanip.py
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>
#
# 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={})

0 comments on commit 7b72302

Please sign in to comment.