Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions src/nifreeze/model/dmri.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@
from dipy.core.gradients import gradient_table_from_bvals_bvecs
from joblib import Parallel, delayed

from nifreeze.data.dmri import DTI_MIN_ORIENTATIONS, DWI
from nifreeze.data.dmri import DEFAULT_LOWB_THRESHOLD, DEFAULT_MIN_S0, DTI_MIN_ORIENTATIONS, DWI
from nifreeze.data.filtering import BVAL_ATOL, dwi_select_shells, grand_mean_normalization
from nifreeze.model.base import BaseModel, ExpectationModel

S0_EPSILON = 1e-6
B_MIN = 50
DEFAULT_S0_CLIP_PERCENTILE = 98
"""Upper percentile threshold for non-diffusion-weighted signal estimation."""


def _exec_fit(model, data, chunk=None, **kwargs):
Expand Down Expand Up @@ -79,7 +79,7 @@ def __init__(self, dataset: DWI, max_b: float | int | None = None, **kwargs):
f"DWI dataset is too small ({dataset.gradients.shape[0]} directions)."
)

if max_b is not None and max_b > B_MIN:
if max_b is not None and max_b > DEFAULT_LOWB_THRESHOLD:
Copy link
Contributor Author

@jhlegarreta jhlegarreta Aug 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that as explained in cd9f6ca, the b0 lower bound is changed from 1e-6 to 1e-5.

self._max_b = max_b

self._data_mask = (
Expand All @@ -88,15 +88,17 @@ def __init__(self, dataset: DWI, max_b: float | int | None = None, **kwargs):
else np.ones(dataset.dataobj.shape[:3], dtype=bool)
)

# By default, set S0 to the 98% percentile of the DWI data within mask
# By default, set S0 to the q-th percentile of the DWI data within mask
self._S0 = np.full(
self._data_mask.sum(),
np.round(np.percentile(dataset.dataobj[self._data_mask, ...], 98)),
np.round(
np.percentile(dataset.dataobj[self._data_mask, ...], DEFAULT_S0_CLIP_PERCENTILE)
),
)

# If b=0 is present and not to be ignored, update brain mask and set
if not kwargs.pop("ignore_bzero", False) and dataset.bzero is not None:
self._data_mask[dataset.bzero < S0_EPSILON] = False
self._data_mask[dataset.bzero < DEFAULT_MIN_S0] = False
self._S0 = dataset.bzero[self._data_mask]

super().__init__(dataset, **kwargs)
Expand Down