Skip to main content

Medical Image Mask Compression and Processing Library

Project description

MedMask - Medical Image Mask Processing Library

Python License

A specialized library for efficient compression, storage, and processing of medical image segmentation masks. Designed to dramatically improve medical image analysis workflow through advanced compression and semantic design.

🚀 Key Benefits

  • Ultimate Compression: 50+ compression ratio using Zstandard
  • File Management: 117 files → 1 archive file
  • Performance: 16x faster read operations
  • Semantic Mapping: Built-in name-to-label conversion
  • Overlapping Masks: Multi-granularity organ combinations
  • Lazy Loading: Memory-efficient on-demand construction

📦 Installation

pip install medmask

Dependencies: Python 3.8+, numpy, spacetransformer, zstandard

⚡ Usage

Basic Operations

import numpy as np
from medmask import SegmentationMask, MaskArchive, save_mask, load_mask
from spacetransformer import Space

# Create spatial information 
space = Space(shape=(64, 192, 192), spacing=(2.5, 1.0, 1.0))

# -----------------------------
# Create mask – two approaches
# -----------------------------
# 1. Complete initialization
liver_data = np.zeros((64, 192, 192), dtype=np.uint8)  # (Z,Y,X)
liver_data[20:40, 50:150, 60:140] = 1
mask = SegmentationMask(liver_data, {"liver": 1}, space=space)

# 2. Lazy loading (multiple organs)
combined_mask = SegmentationMask.lazy_init(bit_depth=8, space=space)
combined_mask.add_label(liver_data > 0, label=1, name="liver")
combined_mask.add_label(spleen_data > 0, label=2, name="spleen")

# Query masks
liver_region = mask.get_binary_mask_by_names("liver")
multiple_organs = combined_mask.get_binary_mask_by_names(["liver", "spleen"])

File Operations

# Single mask files (.msk)
medmask.save_mask(mask, "liver.msk")
loaded_mask = medmask.load_mask("liver.msk")

# Equivalent
mask.save('liver.msk')
loaded_mask = SegmentationMask.load('liver.msk')

# Multi-mask archives (.mska) - for collections
archive = MaskArchive("organs.mska", mode="w", space=space)
archive.add_segmask(liver_mask, "liver")
archive.add_segmask(heart_mask, "heart")

# Read from archive
archive = MaskArchive("organs.mska", mode="r")
liver = archive.load_segmask("liver")
all_names = archive.all_names()

Use Cases

Single Masks: Individual organ processing, simple storage Combined Masks: Multiple organs in one mask, unified label management
Archives: Collections of related masks, overlapping hierarchies (e.g., individual ribs + combined rib groups)

📊 Performance Comparison

Metric Traditional MedMask Improvement
File Count 117 .nii.gz files 1 .mska file 117:1
Storage Size 5.12 MB 92 KB 56.7:1
Read Time 1.869s 0.117s 16.0x
File Management Complex Simple

Test results based on TotalSegmentator 117-organ segmentation data

📁 Project Structure

medmask/
├── core/
│   ├── segmask.py       # SegmentationMask class
│   └── mapping.py       # LabelMapping class
├── storage/
│   ├── archivefile.py   # MaskArchive class
│   └── maskfile.py      # MaskFile class
├── compression/
│   └── zstd_codec.py    # Compression implementation
└── utils/
    └── utils.py         # Utility functions

🧪 Testing

python -m pytest tests/

📝 License

MIT License - see LICENSE file for details.

🤝 Contributing

Issues and Pull Requests are welcome! Please follow the standard GitHub workflow.


MedMask - Making medical image segmentation mask processing simpler and more efficient!

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

medmask-0.1.2.tar.gz (329.6 kB view details)

Uploaded Source

Built Distribution

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

medmask-0.1.2-py3-none-any.whl (19.1 kB view details)

Uploaded Python 3

File details

Details for the file medmask-0.1.2.tar.gz.

File metadata

  • Download URL: medmask-0.1.2.tar.gz
  • Upload date:
  • Size: 329.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for medmask-0.1.2.tar.gz
Algorithm Hash digest
SHA256 cfd4c4a225ce12a47596edcc62f88232411fb271e1047b7116e6404752e1a309
MD5 88be8a50d06b4e6d2fc435dffea54b13
BLAKE2b-256 e5bf6396bd79ab12b6041dc2dfb78b791c46e0a6a2b3d5208fa00b5f63e71952

See more details on using hashes here.

Provenance

The following attestation bundles were made for medmask-0.1.2.tar.gz:

Publisher: ci.yml on fastdiag-toolbox/medmask

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file medmask-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: medmask-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 19.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for medmask-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 665f6a97b55c173b33d72d74a7880b5d5a212347f12cbaa0854107f026568207
MD5 7da82dbe0a15f395b5d08894689406ae
BLAKE2b-256 a9b981c1bde5b0ca39062bb99701bfe175f53e2d3e69a3321d5e6ad25bb10e38

See more details on using hashes here.

Provenance

The following attestation bundles were made for medmask-0.1.2-py3-none-any.whl:

Publisher: ci.yml on fastdiag-toolbox/medmask

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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