Skip to content

Commit c3d8d19

Browse files
committed
STY: Annotate DWI optional data class parameters as such
Annotate DWI optional data class parameters as such: add `None` to the type annotation of `bzero` and `eddy_xfms` attributes. Ignore the `bzero` indexing static checking error when serializing the data to NIfTI: the indexing operation is protected by a conditional `no_bzero` statement, so at that point the class instance is bound to have a non-`None` `bzero` attribute value. Assert that `bzero` is not `None` in tests to avoid static type checking errors.
1 parent d27ba75 commit c3d8d19

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

src/nifreeze/data/dmri.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,13 @@
6868
class DWI(BaseDataset[np.ndarray]):
6969
"""Data representation structure for dMRI data."""
7070

71-
bzero: np.ndarray = attrs.field(default=None, repr=_data_repr, eq=attrs.cmp_using(eq=_cmp))
71+
bzero: np.ndarray | None = attrs.field(
72+
default=None, repr=_data_repr, eq=attrs.cmp_using(eq=_cmp)
73+
)
7274
"""A *b=0* reference map, preferably obtained by some smart averaging."""
7375
gradients: np.ndarray = attrs.field(default=None, repr=_data_repr, eq=attrs.cmp_using(eq=_cmp))
7476
"""A 2D numpy array of the gradient table (4xN)."""
75-
eddy_xfms: list = attrs.field(default=None)
77+
eddy_xfms: list | None = attrs.field(default=None)
7678
"""List of transforms to correct for estimated eddy current distortions."""
7779

7880
def _getextra(self, idx: int | slice | tuple | np.ndarray) -> tuple[np.ndarray]:
@@ -257,7 +259,7 @@ def to_nifti(
257259
stacklevel=2,
258260
)
259261
else:
260-
data = np.concatenate((self.bzero[..., np.newaxis], self.dataobj), axis=-1)
262+
data = np.concatenate((self.bzero[..., np.newaxis], self.dataobj), axis=-1) # type: ignore
261263
nii = nb.Nifti1Image(data, nii.affine, nii.header)
262264

263265
if filename is not None:

test/test_data_dmri.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ def test_load(datadir, tmp_path, insert_b0, rotate_bvecs): # noqa: C901
122122
)
123123

124124
if insert_b0:
125+
assert dwi_h5.bzero is not None
126+
assert dwi_from_nifti1.bzero is not None
125127
assert np.allclose(dwi_h5.bzero, dwi_from_nifti1.bzero)
126128

127129
assert np.allclose(dwi_h5.bvals, dwi_from_nifti1.bvals, atol=1e-3)
@@ -146,6 +148,8 @@ def test_load(datadir, tmp_path, insert_b0, rotate_bvecs): # noqa: C901
146148
if not rotate_bvecs: # If we set motion_affines, data WILL change
147149
assert np.allclose(dwi_h5.dataobj, dwi_from_nifti2.dataobj)
148150
if insert_b0:
151+
assert dwi_h5.bzero is not None
152+
assert dwi_from_nifti2.bzero is not None
149153
assert np.allclose(dwi_h5.bzero, dwi_from_nifti2.bzero)
150154

151155
assert np.allclose(dwi_h5.gradients, dwi_from_nifti2.gradients)
@@ -173,6 +177,8 @@ def test_load(datadir, tmp_path, insert_b0, rotate_bvecs): # noqa: C901
173177
if not rotate_bvecs: # If we set motion_affines, data WILL change
174178
assert np.allclose(dwi_h5.dataobj, dwi_from_nifti3.dataobj)
175179

180+
assert dwi_h5.bzero is not None
181+
assert dwi_from_nifti3.bzero is not None
176182
assert np.allclose(dwi_h5.bzero, dwi_from_nifti3.bzero)
177183
assert np.allclose(dwi_h5.gradients, dwi_from_nifti3.gradients, atol=1e-6)
178184
assert np.allclose(dwi_h5.bvals, dwi_from_nifti3.bvals, atol=1e-6)
@@ -184,6 +190,7 @@ def test_load(datadir, tmp_path, insert_b0, rotate_bvecs): # noqa: C901
184190
if not rotate_bvecs: # If we set motion_affines, data WILL change
185191
assert np.allclose(dwi_h5.dataobj, dwi_from_nifti4.dataobj)
186192

193+
assert dwi_from_nifti4.bzero is not None
187194
assert np.allclose(dwi_h5.bzero, dwi_from_nifti4.bzero)
188195
assert np.allclose(dwi_h5.gradients, dwi_from_nifti4.gradients)
189196
assert np.allclose(dwi_h5.bvals, dwi_from_nifti4.bvals, atol=1e-6)

0 commit comments

Comments
 (0)