|
1 | 1 | # MedVol |
2 | 2 |
|
3 | | -`medvol` is a small wrapper around `SimpleITK`, `nibabel`, and `pynrrd` that |
4 | | -provides one interface for reading and writing 2D, 3D, and 4D NIfTI and NRRD |
5 | | -images. |
6 | | - |
7 | | -## Features |
8 | | - |
9 | | -- One `MedVol` API across `SimpleITK`, `nibabel`, and `pynrrd` |
10 | | -- Automatic backend selection by extension |
11 | | - - NIfTI defaults to `nibabel` |
12 | | - - NRRD defaults to `pynrrd` |
13 | | -- Explicit backend override with `backend=...` |
14 | | -- Canonical `RAS+` orientation by default across backends with `canonicalize=True` |
15 | | -- Optional geometry-only deobliquing with `remove_obliqueness=True` |
16 | | -- `affine` as the single source of truth |
17 | | -- Derived geometry properties: |
18 | | - - `spacing` |
19 | | - - `origin` |
20 | | - - `direction` |
21 | | - - `translation` |
22 | | - - `rotation` |
23 | | - - `scale` |
24 | | - - `shear` |
25 | | - - `coordinate_system` |
26 | | -- Raw backend-native header access via `header` |
27 | | - |
28 | | -## Installation |
| 3 | +[](https://pypi.org/project/medvol/) |
| 4 | +[](https://pypi.org/project/medvol/) |
| 5 | +[](https://github.com/MIC-DKFZ/MedVol/actions) |
| 6 | +[](https://github.com/MIC-DKFZ/MedVol/blob/main/LICENSE) |
| 7 | + |
| 8 | +A lightweight Python wrapper that unifies **SimpleITK**, **nibabel**, and **pynrrd** under a single, simple API for reading and writing 2‑D, 3‑D, and 4‑D medical images in NIfTI (`.nii/.nii.gz`) and NRRD (`.nrrd`) formats. |
| 9 | + |
| 10 | + |
| 11 | +## ✨ Features |
| 12 | + |
| 13 | +- **Unified API** – one `MedVol` class works with all three back‑ends. |
| 14 | +- **Automatic backend selection** based on file extension: |
| 15 | + - `.nii/.nii.gz` → `nibabel` |
| 16 | + - `.nrrd` → `pynrrd` |
| 17 | +- **Explicit backend override** via the `backend=` argument. |
| 18 | +- **Canonical `RAS+` orientation** by default (no interpolation). |
| 19 | +- Optional **de‑obliquing** via `get_geometry(deoblique=True)`. |
| 20 | +- Geometry is stored in a single source of truth – the affine matrix. |
| 21 | +- Convenient derived properties: `spacing`, `origin`, `direction`, `rotation`, `shear`, `coordinate_system`. |
| 22 | +- Direct access to the raw backend header through `header`. |
| 23 | + |
| 24 | + |
| 25 | +## 📦 Installation |
29 | 26 |
|
30 | 27 | ```bash |
31 | 28 | pip install medvol |
32 | 29 | ``` |
33 | 30 |
|
34 | | -## Example |
| 31 | + |
| 32 | +## 🚀 Quick start |
35 | 33 |
|
36 | 34 | ```python |
37 | 35 | from medvol import MedVol |
38 | 36 |
|
39 | | -# Uses the bundled 3D NIfTI example. |
40 | | -image = MedVol("examples/data/3d_img.nii.gz") |
41 | | - |
42 | | -print("Backend:", image.backend) |
43 | | -print("Shape:", image.array.shape) |
44 | | -print("Coordinate system:", image.coordinate_system) |
45 | | -print("Spacing:", image.spacing) |
46 | | -print("Origin:", image.origin) |
47 | | -print("Direction:\n", image.direction) |
48 | | -print("Affine:\n", image.affine) |
49 | | -print("Rotation:\n", image.rotation) |
50 | | -print("Header type:", type(image.header).__name__) |
51 | | -print("Center voxel:", image.array[tuple(size // 2 for size in image.array.shape)]) |
52 | | - |
53 | | -# Opt out of canonicalization to inspect backend-native geometry. |
54 | | -native_image = MedVol("examples/data/3d_img.nii.gz", backend="simpleitk", canonicalize=False) |
55 | | -print("Native coordinate system:", native_image.coordinate_system) |
| 37 | +# Load the bundled 3‑D example image (NIfTI). |
| 38 | +img = MedVol("examples/data/3d_img.nii.gz") |
| 39 | + |
| 40 | +print("Backend:", img.backend) |
| 41 | +print("Shape:", img.array.shape) |
| 42 | +print("Coordinate system:", img.coordinate_system) |
| 43 | +print("Spacing:", img.spacing) |
| 44 | +print("Origin:", img.origin) |
| 45 | +print("Direction:\n", img.direction) |
| 46 | +print("Affine:\n", img.affine) |
| 47 | +print("Rotation:\n", img.rotation) |
| 48 | +print("Header type:", type(img.header).__name__) |
| 49 | + |
| 50 | +# Access the centre voxel value. |
| 51 | +center = tuple(s // 2 for s in img.array.shape) |
| 52 | +print("Center voxel:", img.array[center]) |
56 | 53 | ``` |
57 | 54 |
|
58 | | -See [example_showcase_3d_nifti.py](/home/k539i/Documents/projects/medvol/examples/example_showcase_3d_nifti.py) for a runnable version. |
| 55 | +To inspect the native geometry without canonicalisation: |
| 56 | + |
| 57 | +```python |
| 58 | +native = MedVol( |
| 59 | + "examples/data/3d_img.nii.gz", |
| 60 | + backend="simpleitk", |
| 61 | + canonicalize=False, |
| 62 | +) |
| 63 | +print("Native coordinate system:", native.coordinate_system) |
| 64 | +``` |
59 | 65 |
|
60 | | -## Notes |
| 66 | +See the runnable demo at `examples/example_showcase_3d_nifti.py`. |
61 | 67 |
|
62 | | -- By default, MedVol canonicalizes loaded images to the closest array-aligned |
63 | | - `RAS+` orientation without interpolating voxel values. |
64 | | -- Use `canonicalize=False` to preserve backend-native array order and geometry. |
65 | | -- Use `remove_obliqueness=True` together with canonicalization to strip |
66 | | - obliqueness from the affine while leaving voxel values untouched. |
67 | | -- For 4D NIfTI, `nibabel` and the `SimpleITK` NIfTI path only support |
68 | | - block-separable affines where the spatial axes do not couple to the 4th axis. |
69 | | - Unsupported 5x5 affines raise a `ValueError`. |
70 | 68 |
|
71 | | -## Development |
| 69 | +## Contributing |
72 | 70 |
|
73 | | -Run the test suite with: |
| 71 | +Contributions are welcome! Please open a pull request with clear changes and add tests when appropriate. |
74 | 72 |
|
75 | | -```bash |
76 | | -pytest -q |
77 | | -``` |
| 73 | +## Acknowledgments |
78 | 74 |
|
79 | | -## License |
| 75 | +<p align="left"> |
| 76 | + <img src="https://github.com/MIC-DKFZ/vidata/raw/main/imgs/Logos/HI_Logo.png" width="150"> |
| 77 | + <img src="https://github.com/MIC-DKFZ/vidata/raw/main/imgs/Logos/DKFZ_Logo.png" width="500"> |
| 78 | +</p> |
80 | 79 |
|
81 | | -Distributed under the terms of the Apache Software License 2.0. |
| 80 | +This repository is developed and maintained by the Applied Computer Vision Lab (ACVL) |
| 81 | +of [Helmholtz Imaging](https://www.helmholtz-imaging.de/) and the |
| 82 | +[Division of Medical Image Computing](https://www.dkfz.de/en/medical-image-computing) at DKFZ. |
0 commit comments