Skip to main content

A standardized blosc2 image reader and writer for medical images.

Project description

MedBlosc2

PyPI Python Version Tests Copier Template License

A standardized Blosc2 image reader and writer for medical images. The MedBlosc2 file format (".mb2nd") is a Blosc2-compressed container with standardized metadata support for N-dimensional medical images. Plain ".b2nd" files are also supported, but they do not participate in the MedBlosc2 metadata standard.

Installation

You can install med-blosc2 via pip:

pip install med-blosc2

API

See API.md for the full MedBlosc2 api, including argument descriptions and types.

Metadata schema

See SCHEMA.md for the full MedBlosc2 metadata schema, including field descriptions and types.

Usage

Below are common usage patterns for loading, saving, and working with metadata.

Default usage

import numpy as np
from med_blosc2 import MedBlosc2

array = np.random.random((128, 256, 256)).astype(np.float32)
image = MedBlosc2(array)
image.save("sample.mb2nd")

Memory-mapped loading

from med_blosc2 import MedBlosc2

image = MedBlosc2("sample.mb2nd", mmap=True)
# image.array is a blosc2.ndarray.NDArray when mmap=True, otherwise a np.ndarray

Loading and saving

from med_blosc2 import MedBlosc2

image = MedBlosc2("sample.mb2nd")
image.save("copy.mb2nd")

Metadata inspection and manipulation

import numpy as np
from med_blosc2 import MedBlosc2

array = np.random.random((64, 128, 128)).astype(np.float32)
image = MedBlosc2(
    array,
    spacing=(1.0, 1.0, 1.5),
    origin=(10.0, 10.0, 30.0),
    direction=[[1, 0, 0], [0, 1, 0], [0, 0, 1]],
    header={"patient_id": "123", "modality": "CT"},
    is_seg=False,
)

print(image.spacing)  # [1.0, 1.0, 1.5]
print(image.origin)  # [10.0, 10.0, 30.0]
print(image.header)  # {"patient_id": "123", "modality": "CT"}

image.spacing[1] = 5.3
image.header["study_id"] = "study-001"
image.save("with-metadata.mb2nd")

Copy metadata with overrides

import numpy as np
from med_blosc2 import MedBlosc2

base = MedBlosc2("sample.mb2nd")
array = np.random.random(base.shape).astype(np.float32)

image = MedBlosc2(
    array,
    spacing=(0.8, 0.8, 1.0),
    copy=base,  # Copies all non-explicitly set arguments from base
)

image.save("copied-metadata.mb2nd")

Patch size variants

Default patch size (192):

from med_blosc2 import MedBlosc2

image = MedBlosc2("sample.mb2nd")
image.save("default-patch.mb2nd")

Custom isotropic patch size (512):

from med_blosc2 import MedBlosc2

image = MedBlosc2("sample.mb2nd")
image.save("patch-512.mb2nd", patch_size=512)

Custom non-isotropic patch size:

from med_blosc2 import MedBlosc2

image = MedBlosc2("sample.mb2nd")
image.save("patch-non-iso.mb2nd", patch_size=(128, 192, 256))

Manual chunk/block size:

from med_blosc2 import MedBlosc2

image = MedBlosc2("sample.mb2nd")
image.save("manual-chunk-block.mb2nd", patch_size=None,
           chunk_size=(1, 128, 128), block_size=(1, 32, 32))

Contributing

Contributions are welcome! Please open a pull request with clear changes and add tests when appropriate.

Issues

Found a bug or have a request? Open an issue at https://github.com/Karol-G/med-blosc2/issues.

License

Distributed under the MIT license. See LICENSE for details.

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

med_blosc2-0.0.4.tar.gz (18.2 kB view details)

Uploaded Source

Built Distribution

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

med_blosc2-0.0.4-py3-none-any.whl (13.8 kB view details)

Uploaded Python 3

File details

Details for the file med_blosc2-0.0.4.tar.gz.

File metadata

  • Download URL: med_blosc2-0.0.4.tar.gz
  • Upload date:
  • Size: 18.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for med_blosc2-0.0.4.tar.gz
Algorithm Hash digest
SHA256 3264c4b2db6293a07a5ad015a4032b3da3ab59f2c698df66f377ac56a4677331
MD5 65c6c8000863ce50c23c322556084975
BLAKE2b-256 1d628203baab9ab790b8cce1ca29aea0fe9cd397b67f1b20a147540066fa55fd

See more details on using hashes here.

Provenance

The following attestation bundles were made for med_blosc2-0.0.4.tar.gz:

Publisher: workflow.yml on Karol-G/med-blosc2

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

File details

Details for the file med_blosc2-0.0.4-py3-none-any.whl.

File metadata

  • Download URL: med_blosc2-0.0.4-py3-none-any.whl
  • Upload date:
  • Size: 13.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for med_blosc2-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 0f3460dc93a76bd1762f35de10abad525f3119a0865871235660b288ff379276
MD5 8917eb1334a5ba5a7f4ce667f0a18f5b
BLAKE2b-256 7ebeb1a90a06b2eb87a25ef5340febe24b148f843fdd22be79d4635d576ca8e9

See more details on using hashes here.

Provenance

The following attestation bundles were made for med_blosc2-0.0.4-py3-none-any.whl:

Publisher: workflow.yml on Karol-G/med-blosc2

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