read arbitrary medical images in python
Project description
Read arbitrary medical images in Python with various backends for scientific computing.
Basically, this package is just a modified version of torchio [1] and dicom-numpy which eagerly loads images (depending on the backend and settings) and returns them as NumPy arrays (instead of PyTorch tensors, as is the case in torchio).
There are also various factory functions to load and work with images from memory buffers instead of from disk which is preferable in certain environments, e.g., AWS Lambda.
The main motivation for yet another medical image reader is that I wanted the flexibility of torchio in opening almost any medical image type, without requiring PyTorch and without the automatic casting of images to arrays of 32-bit floating point precision, which is an unnecessary and memory-intensive operation in many scientific computing use cases.
Free software: MIT license
Documentation: https://pymedio.readthedocs.io.
Install
The easiest way to install the package is through the following command:
pip install pymedio
Alternatively, this can be done via conda:
conda install -c conda-forge pymedio
To install from the source directory, clone the repo and run:
python setup.py install
To make the package as portable as possible, the package only comes with numpy installed by default.
If you want to load only DICOM images (using pydicom) install with:
pip install "pymedio[dicom]"
If you want to load DICOM and non-DICOM images (using SimpleITK or nibabel as a backend), install with:
pip install "pymedio[all]"
Basic Usage
Say you have a directory of DICOM images at the path dicom_dir, and you installed the package with dicom extras, then you can open it with:
import pymedio.dicom as miod
image = miod.DICOMImage.from_path("dicom_dir")
This uses pydicom as a backend to open the image à la dicom-numpy.
If you have a NIfTI image at image.nii, and you installed the package with all extras, you can open it with:
import pymedio.image as mioi
image = mioi.Image.from_path("image.nii")
In either case, you can proceed to work with the image data like a normal numpy array, e.g.,
image += 1.0
image *= image
Note that the image will have a affine attribute which stores the (affine) coordinate transformation matrix to/from scanner coordinates/voxels (even after applying numpy functions/operations to the image).
You can convert an either image class to a torch tensor—if you have it installed—like:
import torch
tensor = torch.as_tensor(image.torch_compatible())
If you want to save an image as a non-standard data type (e.g., a 16-bit floating point number), do so with:
# optionally convert the image to the desired type
f16_image = image.astype(np.float16)
f16_image.to_npz("image-f16.npz")
Then, in a more resource-constrained environment, you can install pymedio without any extras and run, e.g.,
from pymedio.base import ImageBase as Image
f16_image = Image.from_npz("image-f16.npz")
print(f16_image.affine)
To view the image in a standard reader following some processing (assuming all extras installed), you can then do, e.g.,
import pymedio.image as mioi
image = mioi.Image.from_npz("image-f16.npz")
image.astype(np.float32).save("image.nii.gz")
References
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
File details
Details for the file pymedio-0.2.14.tar.gz
.
File metadata
- Download URL: pymedio-0.2.14.tar.gz
- Upload date:
- Size: 34.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.11.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7fa1f7277c5b729a6d46e15ccb0249805a8d08a916378a5e658663aba4d295af |
|
MD5 | 0780c600bf004de946d4b02601599635 |
|
BLAKE2b-256 | e733e4ee93fc3a7738691cbcfc3709843ab0cfafb1195d01a9c717070c63426f |
File details
Details for the file pymedio-0.2.14-py3-none-any.whl
.
File metadata
- Download URL: pymedio-0.2.14-py3-none-any.whl
- Upload date:
- Size: 26.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.11.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 93b832cb636b9122400c5d59fba283ec472cd3956a429c7458f59dfbbe65d750 |
|
MD5 | bfb59b417f9285ba8adfc87cf54aad3d |
|
BLAKE2b-256 | 4ad87afd79c6a0b695a6f70e0fcf86a78451443e1b00e2c36a545cc804d75148 |