brain_extraction

brain_extraction.brain_extractor

class brainles_preprocessing.brain_extraction.brain_extractor.BrainExtractor

Bases: object

apply_mask(input_image_path: str | Path, mask_path: str | Path, bet_image_path: str | Path) None

Apply a brain mask to an input image.

Parameters:
  • input_image_path (str or Path) – Path to the input image (NIfTI format).

  • mask_path (str or Path) – Path to the brain mask image (NIfTI format).

  • bet_image_path (str or Path) – Path to save the resulting masked image (NIfTI format).

abstract extract(input_image_path: str | Path, masked_image_path: str | Path, brain_mask_path: str | Path, **kwargs) None

Abstract method to extract the brain from an input image.

Parameters:
  • input_image_path (str or Path) – Path to the input image.

  • masked_image_path (str or Path) – Path where the brain-extracted image will be saved.

  • brain_mask_path (str or Path) – Path where the brain mask will be saved.

  • mode (str or Mode) – Extraction mode.

  • **kwargs – Additional keyword arguments.

class brainles_preprocessing.brain_extraction.brain_extractor.HDBetExtractor

Bases: BrainExtractor

extract(input_image_path: str | Path, masked_image_path: str | Path, brain_mask_path: str | Path, mode: str | Mode = Mode.ACCURATE, device: int | str | None = 0, do_tta: bool = True, **kwargs) None

Skull-strips images with HD-BET and generates a skull-stripped file and mask.

Parameters:
  • input_image_path (str or Path) – Path to the input image.

  • masked_image_path (str or Path) – Path where the brain-extracted image will be saved.

  • brain_mask_path (str or Path) – Path where the brain mask will be saved.

  • mode (str or Mode) – Extraction mode (‘fast’ or ‘accurate’).

  • device (str or int) – Device to use for computation (e.g., 0 for GPU 0, ‘cpu’ for CPU).

  • do_tta (bool) – whether to do test time data augmentation by mirroring along all axes.

class brainles_preprocessing.brain_extraction.brain_extractor.Mode(value)

Bases: Enum

An enumeration.

ACCURATE = 'accurate'
FAST = 'fast'

brain_extraction.synthstrip

class brainles_preprocessing.brain_extraction.synthstrip.SynthStripExtractor(border: int = 1)

Bases: BrainExtractor

_conform(input_nii: Nifti1Image) Nifti1Image

Resample the input image to match SynthStrip’s expected input space.

Parameters:

input_nii (Nifti1Image) – Input NIfTI image to conform.

Raises:

ValueError – If the input NIfTI image does not have a valid affine.

Returns:

A new NIfTI image with conformed shape and affine.

_resample_like(image: Nifti1Image, target: Nifti1Image, output_dtype: dtype | None = None, cval: int | float = 0) Nifti1Image

Resample the input image to match the target’s grid using an identity transform.

Parameters:
  • image – The image to be resampled.

  • target – The reference image.

  • output_dtype – Output data type.

  • cval – Value to use for constant padding.

Returns:

A resampled NIfTI image.

_setup_model(device: device) StripModel

Load SynthStrip model and prepare it for inference on the specified device.

Parameters:

device – Device to load the model onto.

Returns:

A configured and ready-to-use StripModel.

extract(input_image_path: str | Path, masked_image_path: str | Path, brain_mask_path: str | Path, device: device | str = 'cuda', num_threads: int = 1, **kwargs) None

Extract the brain from an input image using SynthStrip.

Parameters:
  • input_image_path (Union[str, Path]) – Path to the input image.

  • masked_image_path (Union[str, Path]) – Path to the output masked image.

  • brain_mask_path (Union[str, Path]) – Path to the output brain mask.

  • device (Union[torch.device, str], optional) – Device to use for computation. Defaults to “cuda”.

  • num_threads (int, optional) – Number of threads to use for computation in CPU mode. Defaults to 1.

Returns:

The function saves the masked image and brain mask to the specified paths.

Return type:

None