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

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.2.tar.gz (13.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.2-py3-none-any.whl (10.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: med_blosc2-0.0.2.tar.gz
  • Upload date:
  • Size: 13.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.2.tar.gz
Algorithm Hash digest
SHA256 870c862d320503a42fc28826c33f11a14b419e0e4bbb44d21e3ef9cbb9c8243c
MD5 6ba2dc7100db18eada94bd02e1edd344
BLAKE2b-256 0c0333f379e88ad2bf43b07e635c71853d5e77aabe9186f3f24de153b5b7051f

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: med_blosc2-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 10.4 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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 33767337403cadd126b687f9dc47ce038be2c8787115bd5b900279290ee89b47
MD5 2ac65ba81e0f32864fedc75ea42bb41f
BLAKE2b-256 96d5187ba750a7685fa5306bde62b1b30f26aa06f20104fe2e1dc8b516a01419

See more details on using hashes here.

Provenance

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