Skip to main content

A CLI wrapper for TorchIO and ANTsPyX for NIfTI image processing

Project description

niiprep

A CLI wrapper for TorchIO and ANTsPyX for NIfTI image processing.

Features

  • Resample NIfTI images to specified resolution
  • Register NIfTI images using ANTsPyX
  • Convert NIfTI images to MP4 videos
  • Round NIfTI pixel values
  • Denoise MP2RAGE images for improved T1w contrast
  • Register images using VoxelMorph (deep learning-based registration) with multiple pre-trained models included

Installation

pip install niiprep

With VoxelMorph support:

pip install niiprep[voxelmorph]

For development:

git clone https://github.com/yourusername/niiprep.git
cd niiprep
pip install -e .

Usage

Resampling

resample -i input.nii.gz -o output.nii.gz -s 1.0 1.0 1.0 --interpolation linear

Registration

registernii -f fixed.nii.gz -m moving.nii.gz -o registered.nii.gz -t syn --interpolation linear

Convert NIfTI to MP4

nii2mp4 -i input.nii.gz -o output.mp4 -d 2 --fps 10

Round NIfTI pixel values

roundnii -i input.nii.gz

Denoise MP2RAGE images

denoiseMP2RAGE --uni uni.nii.gz --inv1 inv1.nii.gz --inv2 inv2.nii.gz --output denoised.nii.gz --regularization 1.0

VoxelMorph Registration

Requires optional dependency: pip install niiprep[voxelmorph]

# Using the default pre-trained model (T1_BRAIN)
vxmreg -m moving.nii.gz -f fixed.nii.gz -o moved.nii.gz

# Using a specific predefined model
vxmreg -m moving.nii.gz -f fixed.nii.gz -o moved.nii.gz --model-type BRAINS_DICE

# List all available predefined models
vxmreg --list-models

# Or specify a custom model file path
vxmreg -m moving.nii.gz -f fixed.nii.gz -o moved.nii.gz --model path/to/custom/model.pt --warp warp.nii.gz

Included VoxelMorph Models

The package includes the following pre-trained VoxelMorph models:

  1. T1_BRAIN: Default model for T1-weighted brain MRI registration with MSE similarity metric. Small and fast.
  2. BRAINS_DICE: Specialized brain registration model with Dice similarity and velocity field regularization. Larger but more accurate for brain images.
  3. SHAPES_DICE: Generic shapes registration with Dice similarity and velocity field. Versatile for different types of images.

Note: The PyTorch version requires models in .pt format. The package will try to create placeholder models automatically. If this fails, you can create them manually with:

python -m niiprep.convert_models

Note that these are placeholder models without trained weights. The registration will still work, but may not be optimal.

Python API

You can also use niiprep as a Python package:

from niiprep import resample, register, nii_to_mp4

# Resample a NIfTI image
resample(
    input_path="input.nii.gz",
    output_path="resampled.nii.gz",
    target_spacing=(1.0, 1.0, 1.0)
)

# Register a moving image to a fixed image
register(
    fixed_path="fixed.nii.gz",
    moving_path="moving.nii.gz",
    output_path="registered.nii.gz",
    reg_type="syn"
)

# Convert a NIfTI image to MP4
nii_to_mp4(
    input_path="input.nii.gz",
    output_path="output.mp4",
    dimension=2,
    fps=10
)

# Register using VoxelMorph (requires voxelmorph package)
try:
    from niiprep import register_voxelmorph, ModelType
    
    # Using the default pre-trained model (T1_BRAIN)
    register_voxelmorph(
        moving_path="moving.nii.gz",
        fixed_path="fixed.nii.gz",
        moved_path="moved.nii.gz"
    )
    
    # Using a specific predefined model
    register_voxelmorph(
        moving_path="moving.nii.gz",
        fixed_path="fixed.nii.gz",
        moved_path="moved.nii.gz",
        model_type=ModelType.BRAINS_DICE  # Or use the string 'BRAINS_DICE'
    )
    
    # Or specify a custom model
    register_voxelmorph(
        moving_path="moving.nii.gz",
        fixed_path="fixed.nii.gz",
        moved_path="moved.nii.gz",
        model_path="path/to/custom/model.pt",
        warp_path="warp.nii.gz"  # Optional
    )
    
    # List available models
    from niiprep import list_available_models
    available_models = list_available_models()
    for name, desc in available_models.items():
        print(f"{name}: {desc}")
        
except ImportError:
    print("VoxelMorph not installed. Install with: pip install voxelmorph")

License

MIT

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

niiprep-0.1.1.tar.gz (1.2 MB view details)

Uploaded Source

Built Distributions

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

niiprep-0.1.1-py3-none-any.whl (9.6 kB view details)

Uploaded Python 3

niiprep-0.1.1-py2.py3-none-any.whl (1.2 MB view details)

Uploaded Python 2Python 3

File details

Details for the file niiprep-0.1.1.tar.gz.

File metadata

  • Download URL: niiprep-0.1.1.tar.gz
  • Upload date:
  • Size: 1.2 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.16

File hashes

Hashes for niiprep-0.1.1.tar.gz
Algorithm Hash digest
SHA256 e4cbdff83e94c9c04d3dc309dbe24d0cb525cb13d2de10916c908323988a5121
MD5 a05c1b5a642226f7af426ae1c7536b74
BLAKE2b-256 25ef8139816b18ae70fca7f92ac4d606f9aa20c6f8e41993c017dbd822fa5e7a

See more details on using hashes here.

File details

Details for the file niiprep-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: niiprep-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 9.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.16

File hashes

Hashes for niiprep-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 0cede3c28f774b45c672a0fa08934d0e80d7582e0b3562698949b893e1d011e3
MD5 e61b79726907524da1dcbdb2a71199c4
BLAKE2b-256 30b5595e759399ae1a98d6ad9314dc927f4a629264b9012fdc2b5b81ace5d532

See more details on using hashes here.

File details

Details for the file niiprep-0.1.1-py2.py3-none-any.whl.

File metadata

  • Download URL: niiprep-0.1.1-py2.py3-none-any.whl
  • Upload date:
  • Size: 1.2 MB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.16

File hashes

Hashes for niiprep-0.1.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 67b7e1fb3bf36e9a3c0d685b9b31da0c7384f60a65f580f6dbe50f350e67bcf9
MD5 e5631ba78c61613fcb10fd4ee20327b3
BLAKE2b-256 26b6670450655c3693cc7db4ea2317ccf30ae2d71ca4b00ebd55553f72c6c89f

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