Skip to content

Commit a516c84

Browse files
committed
ENH: Add a shell data property to DWI data class
Add a shell data property to `DWI` data class that returns a list of pairs consisting of the estimated b-value and the associated DWI data.
1 parent 7237ecf commit a516c84

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

src/nifreeze/data/dmri.py

+40
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,46 @@ def to_nifti(self, filename: Path | str, insert_b0: bool = False) -> None:
241241
np.savetxt(bvecs_file, self.gradients[:3, ...].T, fmt="%.6f")
242242
np.savetxt(bvals_file, self.gradients[:3, ...], fmt="%.6f")
243243

244+
@property
245+
def shells(
246+
self,
247+
num_bins: int = DEFAULT_NUM_BINS,
248+
multishell_nonempty_bin_count_thr: int = DEFAULT_MULTISHELL_BIN_COUNT_THR,
249+
bval_cap: int = DEFAULT_HIGHB_THRESHOLD,
250+
):
251+
"""Get the shell data according to the b-value groups.
252+
253+
Bin the shell data according to the b-value groups found by `~find_shelling_scheme`.
254+
255+
Parameters
256+
----------
257+
num_bins : :obj:`int`, optional
258+
Number of bins.
259+
multishell_nonempty_bin_count_thr : :obj:`int`, optional
260+
Bin count to consider a multi-shell scheme.
261+
bval_cap : :obj:`int`, optional
262+
Maximum b-value to be considered in a multi-shell scheme.
263+
264+
Returns
265+
-------
266+
:obj:`list`
267+
Tuples of binned b-values and corresponding shell data.
268+
"""
269+
270+
_, bval_groups, bval_estimated = find_shelling_scheme(
271+
self.gradients[-1, ...],
272+
num_bins=num_bins,
273+
multishell_nonempty_bin_count_thr=multishell_nonempty_bin_count_thr,
274+
bval_cap=bval_cap,
275+
)
276+
indices = [
277+
np.hstack(np.where(np.isin(self.gradients[-1, ...], bvals))) for bvals in bval_groups
278+
]
279+
return [
280+
(bval_estimated[idx], self.dataobj[indices, ...])
281+
for idx, indices in enumerate(indices)
282+
]
283+
244284

245285
def load(
246286
filename: Path | str,

test/test_data_dmri.py

+23
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,29 @@ def test_equality_operator(tmp_path):
182182
assert round_trip_dwi_obj == dwi_obj
183183

184184

185+
def test_shells(datadir):
186+
187+
dwi_h5 = load(datadir / "dwi.h5")
188+
num_bins = 3
189+
190+
_, expected_bval_groups, expected_bval_est = find_shelling_scheme(
191+
dwi_h5.gradients[-1, ...], num_bins=num_bins
192+
)
193+
194+
indices = [
195+
np.hstack(np.where(np.isin(dwi_h5.gradients[-1, ...], bvals)))
196+
for bvals in expected_bval_groups
197+
]
198+
expected_shell_data = [dwi_h5.dataobj[indices, ...] for indices in indices]
199+
200+
shells = dwi_h5.shells(num_bins=num_bins)
201+
obtained_bval_est, obtaine_shell_data = zip(*shells, strict=True)
202+
203+
assert len(shells) == num_bins
204+
assert obtained_bval_est == expected_bval_est
205+
assert np.allclose(obtaine_shell_data, expected_shell_data)
206+
207+
185208
@pytest.mark.parametrize(
186209
("bvals", "exp_scheme", "exp_bval_groups", "exp_bval_estimated"),
187210
[

0 commit comments

Comments
 (0)