NiiMetric
A Python CLI tool for evaluating image quality metrics between NIfTI (.nii/.nii.gz) medical images.
Features
- PSNR - Peak Signal-to-Noise Ratio
- SSIM - Structural Similarity Index (slice-based with configurable dimension)
- MAE - Mean Absolute Error
- LPIPS - Learned Perceptual Image Patch Similarity (slice-based)
- Image Augmentations - Apply realistic MRI noise (coil-based), standard Rician noise, blur, bias field, motion, ghosting, and GRAPPA artifacts.
- Auto-cropping - Automatically crops to brain region based on reference image (30% mean threshold)
- Foreground masking - Evaluates only on non-air regions (excludes background)
- Configurable slice dimension - Evaluate on sagittal, coronal, or axial slices
- CSV output - Save results to CSV file
- Mask export - Optionally save the foreground mask as NIfTI
Installation
pip install niimetric
Usage
Basic Usage
# Single metric
niimetric -a reference.nii.gz -b image.nii.gz --ssim -o output.csv
niimetric -a reference.nii.gz -b image.nii.gz --psnr -o output.csv
niimetric -a reference.nii.gz -b image.nii.gz --mae -o output.csv
niimetric -a reference.nii.gz -b image.nii.gz --lpips -o output.csv
# All metrics at once
niimetric -a reference.nii.gz -b image.nii.gz --all -o output.csv
Slice Dimension Selection
# Evaluate on axial slices (default)
niimetric -a reference.nii.gz -b image.nii.gz --all --dim 2 -o output.csv
# Evaluate on sagittal slices
niimetric -a reference.nii.gz -b image.nii.gz --all --dim 0 -o output.csv
# Evaluate on coronal slices
niimetric -a reference.nii.gz -b image.nii.gz --all --dim 1 -o output.csv
Save Foreground Mask
niimetric -a reference.nii.gz -b image.nii.gz --all -o output.csv --save-mask mask.nii.gz
Image Augmentations (Artifact Generation)
You can generate degraded NIfTI images using included augmentation scripts powered by TorchIO. All augmentations accept an input -i, output -o, and severity -s (1 to 5).
# Add random noise (mostly on brain)
noise -i input.nii.gz -o noise_1.nii.gz -s 1
# Add realistic coil-based noise (supports --coils and --dir)
noise_robust -i input.nii.gz -o robust.nii.gz -s 3
# Add GRAPPA reconstruction artifacts
grappa -i input.nii.gz -o grappy.nii.gz -s 3
# Add blur artifact
blur -i input.nii.gz -o blur_3.nii.gz -s 3
# Add bias field (inhomogeneity) artifact
bias -i input.nii.gz -o bias_5.nii.gz -s 5
# Add motion artifact
motion -i input.nii.gz -o motion_2.nii.gz -s 2
# Add ghosting artifact
ghost -i input.nii.gz -o ghost_4.nii.gz -s 4
Arguments
| Argument | Description |
|---|---|
-a, --reference |
Path to reference NIfTI image (used for cropping boundaries) |
-b, --image |
Path to comparison NIfTI image |
-o, --output |
Path to output CSV file |
--ssim |
Calculate Structural Similarity Index |
--psnr |
Calculate Peak Signal-to-Noise Ratio |
--mae |
Calculate Mean Absolute Error |
--lpips |
Calculate Learned Perceptual Image Patch Similarity |
--all |
Calculate all metrics |
--dim |
Dimension for slice-based evaluation: 0=sagittal, 1=coronal, 2=axial (default: 2) |
--save-mask |
Path to save the foreground mask as NIfTI file |
Output Format
Results are saved in CSV format:
reference,image,metric,value
reference.nii.gz,image.nii.gz,PSNR,25.432100
reference.nii.gz,image.nii.gz,SSIM,0.921500
reference.nii.gz,image.nii.gz,MAE,0.045200
reference.nii.gz,image.nii.gz,LPIPS,0.032100
How It Works
- Load both NIfTI images
- Auto-crop based on reference image (30% mean threshold per slice)
- Normalize both images to 0-1 range
- Create foreground mask from reference (10% intensity threshold)
- Compute metrics only on foreground regions
- Save results to CSV
Dependencies
nibabel- NIfTI file I/Onumpy- Array operationsscikit-image- SSIM, PSNR calculationstorch- LPIPS backendlpips- Perceptual similarity metric
Python API
You can also use niimetric as a Python library:
from niimetric import load_nifti, compute_ssim, compute_psnr, compute_mae, compute_lpips
from niimetric import auto_crop_volumes
from niimetric.metrics import create_foreground_mask
from niimetric.utils import normalize_to_range
# Load images
ref = load_nifti("reference.nii.gz")
img = load_nifti("image.nii.gz")
# Auto-crop
ref_cropped, img_cropped, bbox = auto_crop_volumes(ref, img)
# Normalize
ref_norm = normalize_to_range(ref_cropped, 0, 1)
img_norm = normalize_to_range(img_cropped, 0, 1)
# Create mask
mask = create_foreground_mask(ref_norm)
# Compute metrics
psnr = compute_psnr(ref_norm, img_norm, mask=mask)
ssim = compute_ssim(ref_norm, img_norm, mask=mask, dim=2)
mae = compute_mae(ref_norm, img_norm, mask=mask)
lpips = compute_lpips(ref_norm, img_norm, mask=mask, dim=2)
License
MIT License - see LICENSE for details.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Metadata
Release files for niimetric 0.1.11
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| niimetric-0.1.11.tar.gz | 28.3 MB | Without provenance | Details for niimetric-0.1.11.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| niimetric-0.1.11-py3-none-any.whl | Python 3 | none | any | Details for niimetric-0.1.11-py3-none-any.whl |
Total release size: 56.7 MB
Release files / niimetric-0.1.11.tar.gz
| Download URL | niimetric-0.1.11.tar.gz |
|---|---|
| Size | 28.3 MB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
bd1a5c98d1a092e3f84c0bf829246621b0d31adbc5009868fd5b19e62454aba9
|
|
BLAKE2b-256 checksum How to use checksums |
9d03cf637bd5f4fab202483d6f755de249966d6752f600a91e88e901810a97aa
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.9.23
|
Release files / niimetric-0.1.11-py3-none-any.whl
| Download URL | niimetric-0.1.11-py3-none-any.whl |
|---|---|
| Size | 28.3 MB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
94b14921e9cb5c3d837160527d3c72eeaef1fbb8db4cb46ed421c8306af93832
|
|
BLAKE2b-256 checksum How to use checksums |
f0efade6920a35acae4150338ce7ca6e7db341451e012f15cd45086298ec13e0
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.9.23
|
Security for niimetric 0.1.11
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.