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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file brainseg_utils-0.1.2.tar.gz.
File metadata
- Download URL: brainseg_utils-0.1.2.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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
26d5356869a5c20e3355998f48b3a767f1d5a8752529c848b7ff33dc76e53dbe
|
|
| MD5 |
d6c10fff82cb4c570f22d146f8848e97
|
|
| BLAKE2b-256 |
57b7b58eb0a97588b64e82e3c0472f5797657898fe6a853c43ff4a53864210e6
|
File details
Details for the file brainseg_utils-0.1.2-py3-none-any.whl.
File metadata
- Download URL: brainseg_utils-0.1.2-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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
046cba2930aa8e6889588d5922942e2aa0f0b23a5fb0064fdfe9bb5b1277f209
|
|
| MD5 |
2f98d4a46761bd30922d31cd16457d92
|
|
| BLAKE2b-256 |
42c6256e4e6f508622774746a2f6b47110d950a8349d2724228f4fca4e98dc54
|