Skip to main content

Evaluate image quality metrics (SSIM, MAE, LPIPS, PSNR) and add realistic artifacts/augmentations for NIfTI images

Project description

NiiMetric

A Python CLI tool for evaluating image quality metrics between NIfTI (.nii/.nii.gz) medical images.

PyPI version License: MIT

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

  1. Load both NIfTI images
  2. Auto-crop based on reference image (30% mean threshold per slice)
  3. Normalize both images to 0-1 range
  4. Create foreground mask from reference (10% intensity threshold)
  5. Compute metrics only on foreground regions
  6. Save results to CSV

Dependencies

  • nibabel - NIfTI file I/O
  • numpy - Array operations
  • scikit-image - SSIM, PSNR calculations
  • torch - LPIPS backend
  • lpips - 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.

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

niimetric-0.1.11.tar.gz (28.3 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

niimetric-0.1.11-py3-none-any.whl (28.3 MB view details)

Uploaded Python 3

File details

Details for the file niimetric-0.1.11.tar.gz.

File metadata

  • Download URL: niimetric-0.1.11.tar.gz
  • Upload date:
  • Size: 28.3 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.23

File hashes

Hashes for niimetric-0.1.11.tar.gz
Algorithm Hash digest
SHA256 bd1a5c98d1a092e3f84c0bf829246621b0d31adbc5009868fd5b19e62454aba9
MD5 552423130c66fbb31607fd6b0540bea1
BLAKE2b-256 9d03cf637bd5f4fab202483d6f755de249966d6752f600a91e88e901810a97aa

See more details on using hashes here.

File details

Details for the file niimetric-0.1.11-py3-none-any.whl.

File metadata

  • Download URL: niimetric-0.1.11-py3-none-any.whl
  • Upload date:
  • Size: 28.3 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.23

File hashes

Hashes for niimetric-0.1.11-py3-none-any.whl
Algorithm Hash digest
SHA256 94b14921e9cb5c3d837160527d3c72eeaef1fbb8db4cb46ed421c8306af93832
MD5 8a32536467d94985c233254f3603855c
BLAKE2b-256 f0efade6920a35acae4150338ce7ca6e7db341451e012f15cd45086298ec13e0

See more details on using hashes here.

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