Skip to main content

Lightweight research utilities for BraTS-style brain tumor segmentation

Project description

brainseg-utils

A lightweight Python library for BraTS-style brain tumor segmentation research. Provides utilities for loading NIfTI volumes, extracting 2D slices, computing segmentation metrics, and building PyTorch datasets — without requiring Docker or heavy dependencies.


Motivation

When working on brain tumor segmentation projects using the BraTS dataset, researchers repeatedly write the same boilerplate code for every experiment: loading .nii.gz files, normalizing intensities, extracting non-empty slices, avoiding data leakage from slice-level splitting, and computing region-wise Dice scores. This library consolidates those utilities into a clean, minimal API.


Installation

pip install brainseg-utils

Features

  • NIfTI volume loading and intensity normalization (z-score, minmax)
  • 2D slice extraction along any axis with empty slice filtering
  • Patient-level train/val/test splitting to prevent data leakage
  • Dice, IoU, and Hausdorff distance metrics
  • BraTS region-wise Dice (WT, TC, ET)
  • Matplotlib overlay visualization
  • PyTorch Dataset class for drop-in DataLoader integration

Usage

from brainseg_utils import load_brats_subject, normalize, extract_slices, brats_dice

# load all modalities for a subject
subject = load_brats_subject("path/to/brats/", "BraTS20_Training_001")

# normalize
vol = normalize(subject["flair"], method="zscore")

# extract non-empty axial slices
slices = extract_slices(vol, subject["seg"], skip_empty=True)

# compute region-wise dice
scores = brats_dice(pred, subject["seg"])
print(scores)  # {'WT': 0.89, 'TC': 0.76, 'ET': 0.71}

Patient-level split

from brainseg_utils import patient_split

train_ids, val_ids, test_ids = patient_split(all_subject_ids, train=0.7, val=0.15, test=0.15)

PyTorch Dataset

from brainseg_utils.dataset import BraTSSliceDataset
from torch.utils.data import DataLoader

dataset = BraTSSliceDataset(root="path/to/brats", subject_ids=train_ids, modality="flair")
loader = DataLoader(dataset, batch_size=8, shuffle=True)

Visualization

from brainseg_utils import show_slice

show_slice(vol[:, :, 80], mask[:, :, 80])

Dependencies

  • nibabel
  • numpy
  • matplotlib
  • scipy (optional, for Hausdorff distance)
  • torch (optional, for Dataset class)

Author

Dipendra Thapa GitHub: https://github.com/Dipendra367 Portfolio: https://dipendra367.com.np


License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

brainseg_utils-0.1.3.tar.gz (6.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

brainseg_utils-0.1.3-py3-none-any.whl (8.3 kB view details)

Uploaded Python 3

File details

Details for the file brainseg_utils-0.1.3.tar.gz.

File metadata

  • Download URL: brainseg_utils-0.1.3.tar.gz
  • Upload date:
  • Size: 6.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for brainseg_utils-0.1.3.tar.gz
Algorithm Hash digest
SHA256 67867cd1655dc231b429793dda3574c60969940371c5e948ec8ca65e05867953
MD5 407f91002677bbeeb13940bebf0e8b49
BLAKE2b-256 e98dd862770894253c3f568cf204642dc02b01b764e7907d32521ed8f65a1127

See more details on using hashes here.

File details

Details for the file brainseg_utils-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: brainseg_utils-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 8.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for brainseg_utils-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 9e2f20624b3be92b48a8f8f87a6a0f1a218a01e71e13a29aa5cfd768bf6220dd
MD5 fca156117e618ef01eb3b9bdf50e9a6f
BLAKE2b-256 d454dc663ff48d9e5031e66393ebb845c9e51c22b3d40cc0a6299dffd2c12787

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page