Skip to content

Commit 5f0c848

Browse files
committed
BUG: Fix the non-weighted DWI data serialization
WIP: Re issue #79
1 parent 45839b8 commit 5f0c848

File tree

2 files changed

+26
-18
lines changed

2 files changed

+26
-18
lines changed

src/nifreeze/data/dmri.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,18 +210,24 @@ def to_nifti(
210210
filename : :obj:`os.pathlike`
211211
The output NIfTI file path.
212212
insert_b0 : :obj:`bool`, optional
213-
Insert a :math:`b=0` at the front of the output NIfTI.
213+
Insert a :math:`b=0` at the front of the output NIfTI and add the corresponding
214+
null gradient value to the output bval/bvec files.
214215
bvals_dec_places : :obj:`int`, optional
215216
Decimal places to use when serializing b-values.
216217
bvecs_dec_places : :obj:`int`, optional
217218
Decimal places to use when serializing b-vectors.
218219
219220
"""
221+
bvecs = self.bvecs
222+
bvals = self.bvals
223+
220224
if not insert_b0:
221225
# Parent's to_nifti to handle the primary NIfTI export.
222226
super().to_nifti(filename)
223227
else:
224228
data = np.concatenate((self.bzero[..., np.newaxis], self.dataobj), axis=-1)
229+
bvecs = np.concatenate((np.zeros(3)[:, np.newaxis], bvecs), axis=-1)
230+
bvals = np.concatenate((np.zeros(1), bvals))
225231
nii = nb.Nifti1Image(data, self.affine, self.datahdr)
226232
if self.datahdr is None:
227233
nii.header.set_xyzt_units("mm")

test/test_data_dmri.py

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -110,37 +110,39 @@ def test_main(datadir):
110110
assert isinstance(load(input_file), DWI)
111111

112112

113-
def test_load(datadir, tmp_path):
113+
@pytest.mark.parametrize("insert_b0", (False, True))
114+
def test_load(datadir, tmp_path, insert_b0):
114115
dwi_h5 = DWI.from_filename(datadir / "dwi.h5")
115116
dwi_nifti_path = tmp_path / "dwi.nii.gz"
116117
gradients_path = tmp_path / "dwi.tsv"
117-
bvecs_path = tmp_path / "dwi.bvecs"
118-
bvals_path = tmp_path / "dwi.bvals"
119118

120-
grad_table = np.hstack((np.zeros((4, 1)), dwi_h5.gradients))
121-
122-
dwi_h5.to_nifti(dwi_nifti_path, insert_b0=True)
123-
np.savetxt(str(gradients_path), grad_table.T)
124-
np.savetxt(str(bvecs_path), grad_table[:3])
125-
np.savetxt(str(bvals_path), grad_table[-1])
119+
dwi_h5.to_nifti(dwi_nifti_path, insert_b0=insert_b0)
126120

127121
with pytest.raises(RuntimeError):
128122
from_nii(dwi_nifti_path)
129123

130-
# Try loading NIfTI + gradients table
131-
dwi_from_nifti1 = from_nii(dwi_nifti_path, gradients_file=gradients_path)
132-
133-
assert np.allclose(dwi_h5.dataobj, dwi_from_nifti1.dataobj)
134-
assert np.allclose(dwi_h5.bzero, dwi_from_nifti1.bzero)
135-
assert np.allclose(dwi_h5.gradients, dwi_from_nifti1.gradients)
136-
137124
# Try loading NIfTI + b-vecs/vals
138-
dwi_from_nifti2 = from_nii(
125+
out_root = dwi_nifti_path.parent / dwi_nifti_path.name.replace(
126+
"".join(dwi_nifti_path.suffixes), ""
127+
)
128+
bvecs_path = out_root.with_suffix(".bvec")
129+
bvals_path = out_root.with_suffix(".bval")
130+
dwi_from_nifti1 = from_nii(
139131
dwi_nifti_path,
140132
bvec_file=bvecs_path,
141133
bval_file=bvals_path,
142134
)
143135

136+
assert np.allclose(dwi_h5.dataobj, dwi_from_nifti1.dataobj)
137+
assert np.allclose(dwi_h5.bzero, dwi_from_nifti1.bzero)
138+
assert np.allclose(dwi_h5.gradients, dwi_from_nifti1.gradients, atol=1e-6)
139+
140+
grad_table = np.hstack((np.zeros((4, 1)), dwi_h5.gradients))
141+
np.savetxt(str(gradients_path), grad_table.T)
142+
143+
# Try loading NIfTI + gradients table
144+
dwi_from_nifti2 = from_nii(dwi_nifti_path, gradients_file=gradients_path)
145+
144146
assert np.allclose(dwi_h5.dataobj, dwi_from_nifti2.dataobj)
145147
assert np.allclose(dwi_h5.bzero, dwi_from_nifti2.bzero)
146148
assert np.allclose(dwi_h5.gradients, dwi_from_nifti2.gradients)

0 commit comments

Comments
 (0)