Skip to content

csttool.tracking

Whole-brain deterministic tractography. The CLI counterpart is track.

from csttool.tracking.modules import run_tracking

run_tracking(
    nifti="preproc/dti_preproc.nii.gz",
    out="./tracking",
    fa_thr=0.2,
    seed_density=1,
    rng_seed=42,
)

csttool.tracking

Whole-brain deterministic tractography (CSA-ODF).

Functions:

resolve_brain_mask(data, gtab, affine, brain_mask_path=None, verbose=False)

Return the brain mask for this run, external if given, automatic if not.

Args: data (ndarray): 4D DWI array (X, Y, Z, N). gtab (GradientTable): Gradient table (used only by the automatic path). affine (ndarray): 4x4 affine of data, for validating an external mask. brain_mask_path (str | Path | None): Optional external brain mask. When None, background_segmentation is used exactly as before. verbose (bool): Print the mask source and statistics.

Returns: tuple: (masked_data, brain_mask, mask_info) where mask_info is a dict with brain_mask_source ('automatic_background_segmentation' or 'external') and brain_mask_path (str or None).

Source code in src/csttool/tracking/modules/brain_mask.py
def resolve_brain_mask(data, gtab, affine, brain_mask_path=None, verbose=False):
    """Return the brain mask for this run, external if given, automatic if not.

    Args:
        data (ndarray): 4D DWI array (X, Y, Z, N).
        gtab (GradientTable): Gradient table (used only by the automatic path).
        affine (ndarray): 4x4 affine of ``data``, for validating an external mask.
        brain_mask_path (str | Path | None): Optional external brain mask. When
            None, ``background_segmentation`` is used exactly as before.
        verbose (bool): Print the mask source and statistics.

    Returns:
        tuple: (masked_data, brain_mask, mask_info) where ``mask_info`` is a
            dict with ``brain_mask_source`` ('automatic_background_segmentation'
            or 'external') and ``brain_mask_path`` (str or None).
    """
    if brain_mask_path is None:
        # Import inside the call so the automatic path stays patchable at its
        # source module, as the existing tracking tests expect.
        from csttool.preprocess.modules.background_segmentation import (
            background_segmentation,
        )

        if verbose:
            print("  → Brain mask source: automatic background segmentation")

        masked_data, brain_mask = background_segmentation(data, gtab)
        mask_info = {
            'brain_mask_source': 'automatic_background_segmentation',
            'brain_mask_path': None,
        }
    else:
        if verbose:
            print(f"  → Brain mask source: external brain mask: {brain_mask_path}")

        brain_mask = load_external_brain_mask(
            brain_mask_path,
            dwi_shape=np.asarray(data).shape,
            dwi_affine=affine,
            verbose=verbose,
        )
        masked_data = apply_brain_mask(data, brain_mask)
        mask_info = {
            'brain_mask_source': 'external',
            'brain_mask_path': str(brain_mask_path),
        }

    if verbose:
        coverage = brain_mask.sum() / brain_mask.size * 100
        print(f"    • Brain mask: {brain_mask.sum():,} voxels ({coverage:.1f}%)")

    return masked_data, brain_mask, mask_info