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, Meta

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]],
    meta=Meta(image={"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.meta.image)  # {"patient_id": "123", "modality": "CT"}

image.spacing[1] = 5.3
image.meta.image["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.5.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.5-py3-none-any.whl (13.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: med_blosc2-0.0.5.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.5.tar.gz
Algorithm Hash digest
SHA256 f8d6bb3f72392ad75911dd0daa6dee00f79d10f8db79182dd18b0f1829bfcd48
MD5 0f27833fa23f02086418a5b7b171ed4b
BLAKE2b-256 da462c7441e16e7414f17ca9648da2c2c8312cfb3d2dc82dc1d8ac5dc2210925

See more details on using hashes here.

Provenance

The following attestation bundles were made for med_blosc2-0.0.5.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.5-py3-none-any.whl.

File metadata

  • Download URL: med_blosc2-0.0.5-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.5-py3-none-any.whl
Algorithm Hash digest
SHA256 3966c4e92eddcd4b018cb522c50940a0869bc5343fa2522a2d764d34cab0fc0a
MD5 66afab08da7583f354c9b59f2748dc4c
BLAKE2b-256 f26151b70e3599e1fa8f11fb2e6d1bc896a0a9f7bf62801f4511f42318181407

See more details on using hashes here.

Provenance

The following attestation bundles were made for med_blosc2-0.0.5-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