Skip to content

Commit 7b72302

Browse files
committed
Add informative error when TotalReadoutTime not in metadata and in_file not provided
1 parent 1a11859 commit 7b72302

File tree

2 files changed

+44
-3
lines changed

2 files changed

+44
-3
lines changed

sdcflows/utils/epimanip.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -188,10 +188,14 @@ def get_trt(in_meta, in_file=None):
188188
raise ValueError(f"'{trt}'")
189189

190190
return trt
191+
elif in_file is None:
192+
msg = "Unable to find TotalReadoutTime in metadata and in_file \
193+
not defined."
194+
raise AssertionError(msg)
191195

192196
# npe = N voxels PE direction
193197
pe_index = "ijk".index(in_meta["PhaseEncodingDirection"][0])
194-
npe = nb.load(in_file).shape[pe_index]
198+
npe = nb.loadsave.load(in_file).shape[pe_index]
195199

196200
# Use case 2: EES is defined
197201
ees = in_meta.get("EffectiveEchoSpacing")
@@ -252,7 +256,9 @@ def epi_mask(in_file, out_file=None):
252256
maxnorm = np.percentile(closed[closed > 0], 90)
253257
closed = np.clip(closed, a_min=0.0, a_max=maxnorm)
254258
# Calculate index of center of masses
255-
cm = tuple(np.round(ndimage.measurements.center_of_mass(closed)).astype(int))
259+
cm = tuple(
260+
np.round(ndimage.measurements.center_of_mass(closed)).astype(int)
261+
)
256262
# Erode the picture of the brain by a lot
257263
eroded = ndimage.grey_erosion(closed, structure=ball(5))
258264
# Calculate the residual
@@ -268,6 +274,8 @@ def epi_mask(in_file, out_file=None):
268274
hdr = img.header.copy()
269275
hdr.set_data_dtype("uint8")
270276
nb.Nifti1Image(
271-
ndimage.binary_dilation(labels == 2, ball(2)).astype("uint8"), img.affine, hdr
277+
ndimage.binary_dilation(labels == 2, ball(2)).astype("uint8"),
278+
img.affine,
279+
hdr,
272280
).to_filename(out_file)
273281
return out_file

sdcflows/utils/tests/test_epimanip.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
2+
# vi: set ft=python sts=4 ts=4 sw=4 et:
3+
#
4+
# Copyright 2021 The NiPreps Developers <[email protected]>
5+
#
6+
# Licensed under the Apache License, Version 2.0 (the "License");
7+
# you may not use this file except in compliance with the License.
8+
# You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
#
18+
# We support and encourage derived works from this project, please read
19+
# about our expectations at
20+
#
21+
# https://www.nipreps.org/community/licensing/
22+
#
23+
"""Test EPI manipulation routines."""
24+
import pytest
25+
from sdcflows.utils.epimanip import get_trt
26+
27+
28+
def test_get_trt_err_wo_trt_and_in_file():
29+
"""Test that calling get_trt with dict that does not have TotalReadoutTime \
30+
and no in_file raises AssertionError.
31+
"""
32+
with pytest.raises(AssertionError):
33+
get_trt(in_meta={})

0 commit comments

Comments
 (0)