Skip to main content

A python library for neuroimaging transformations

Project description

warpkit

Build

A python library for neuroimaging transforms

If you've landed here, you're probably interested in the Multi-Echo DIstortion Correction (MEDIC) algorithm, which this library implements.

You can find the pre-print for MEDIC here: https://www.biorxiv.org/content/10.1101/2023.11.28.568744v1.

See below for usage details.

Installation

Installing through conda (recommended)

The easiest way to install warpkit is through a conda environment. Currently, warpkit is not uploaded to any conda channels, so you will need to build it from source.

[!NOTE] For advanced users only A meta.yaml file is present in the conda folder in the root of the repo. If you are familiar with conda-build you should be able to build this with your existing conda install.

Fortunately, there is an automated script that will download micromamba (a conda variant), create a new environment, and build/install warpkit for you automagically. To do this, clone this repo and run the following:

# switch to conda directory in repo
cd conda
# run the install script (use -p or --path to specify an install prefix)
# by default this is set to ~/micromamba
./warpkit-conda-install.sh

# in your shell profile add the following lines
# or just execute them in your current shell
# but it won't be permanent
export MAMBA_ROOT_PREFIX=/whatever/path/you/installed/micromamba/prefix/above
eval "$(/whatever/path/you/installed/micromamba/prefix/above/bin/micromamba shell hook -s posix)"

# if you added the above to your profile
# restart your shell then activate the environment
#
# unless you have already executed the above in your current shell
# then you don't need to restart it and can just activate the environment
micromamba activate base

# you should now be able to import/use warpkit
python -c "import warpkit"  # should return no errors
medic --help  # should return help

After installing and configuring everything, you must type micromamba activate base each time you open a new terminal to get back into the conda environment to use warpkit. To make this permanent, you can add the above line to your shell's profile file.

Installing through pip

To install, clone this repo and run the following in the repo directory. Due to the current developmental nature of this package, I highly recommend installing it in editable mode (with the strict option, see here):

pip install -e ./[dev] -v --config-settings editable_mode=strict

You will need a C++ compiler with C++17 support, as well as Julia pre-installed on your system. For the Julia install, ensure that you have the julia executable in your path, and the julia libraries correctly setup in your ld.so.conf. If you installed julia via a package manager, this should be done for you (most of the time) already. However, if you installed Julia manually, you may need to tell ldconfig where the julia libraries are. For example, on debian based systems you can do this with:

# /path to julia installation (the lib folder will have libjulia.so)
echo /path/to/julia/lib > /etc/ld.so.conf.d/julia.conf
ldconfig

If you have done this correctly, you should see libjulia.so in your ldconfig:

ldconfig -p | grep julia                                                                                        
	libjulia.so.1 (libc6,x86-64) => /usr/lib/libjulia.so.1
	libjulia.so (libc6,x86-64) => /usr/lib/libjulia.so

The above may require root privileges. The alternative to the above is to set the LD_LIBRARY_PATH environment variable to the path of the julia libraries.

# /path to julia installation (the lib folder will have libjulia.so)
export LD_LIBRARY_PATH=/path/to/julia/lib:$LD_LIBRARY_PATH

Note however, that you must type the above each time you open a new terminal. To make this permanent, you can add the above line to your shell's profile file.

The build process uses CMake to build the C++/Python Extension. If you encounter an error during the build process, please report the full logs of the build process using the -v flag to the pip command above.

What is MEDIC?

MEDIC takes your ME-EPI phase data from this:

phase

to this:

field map

for each frame of your data. You can then use these field maps to distortion correct your data.

MEDIC Usage

The warpkit library is meant for integration into a larger neuroimaging pipeline/software package.

An example of how to call MEDIC from python is provided below:

import nibabel as nib
from warpkit.distortion import medic
from warpkit.utilities import displacement_map_to_warp

# load phase and magnitude images into lists
# each element in list is a different echo
phases = [nib.load(p) for p in phases_paths]
magnitudes = [nib.load(p) for p in magnitude_paths]
TEs = [TE1, TE2, ...] # in milliseconds
effective_echo_spacing = ... # in seconds
phase_encoding_direction = either i, j, k, i-, j-, k-, x , y, z, x-, y-, z- 

# call the medic function
field_maps_native, displacement_maps, field_maps = medic(
    phases, magnitudes, TEs, effective_echo_spacing, phase_encoding_direction)

# field_maps_native are returned in the distorted space (Hz) (mainly for reference/debugging purposes)
# you shouldn't need to use these probably???
# displacement_maps are returned in the undistorted space (mm) (see below for usage)
# field_maps are returned in the undistorted space (Hz) (same field map output as topup/fugue, but framewise)

# returns are nibabel Nifti1Image objects, so they can be saved to file by:

# displacement_maps to file
displacement_maps.to_filename("/path/to/save.nii.gz")

# these should be converted to displacement fields
# by the displacement_map_to_field function
# and specifying the appropriate type (i.e. itk, ants, afni, fsl)

displacement_field = displacement_map_to_field(displacement_maps, axis="y", format="itk", frame=0)

# where axis specifies the phase encoding direction, format is the desired output format, and frame is the index of
# displacement map to convert to a displacement field

# the displacement field can then be saved to file by the to_filename method
# Each file can be applied with the respective software's displacement field application tool:
# itk: the internal format warpkit uses. See utilities.resample_image
# ants: antsApplyTransforms (note that even though ants also uses itk, warpkit's itk warp format is NOT equivalent)
# afni: 3dNwarpApply
# fsl: applywarp

# if you are using fsl and instead want to use fugue to distortion correction, you can use the field_maps outputs
# (these are the equivalent field maps of that you would get from fugue, but with multiple frames)

You can also use the provided CLI script medic to run MEDIC from the command line. The script is installed to your PATH when you install the package. medic takes the following arguments:

usage: medic [-h] --magnitude MAGNITUDE [MAGNITUDE ...] --phase PHASE
             [PHASE ...] --metadata METADATA [METADATA ...]
             [--out_prefix OUT_PREFIX] [-f NOISEFRAMES] [-n N_CPUS]
             [--debug] [--wrap_limit]

Multi-Echo DIstortion Correction

options:
  -h, --help            show this help message and exit
  --magnitude MAGNITUDE [MAGNITUDE ...]
                        Magnitude data
  --phase PHASE [PHASE ...]
                        Phase data
  --metadata METADATA [METADATA ...]
                        JSON sidecar for each echo
  --out_prefix OUT_PREFIX
                        Prefix to output field maps and displacment maps.
  -f NOISEFRAMES, --noiseframes NOISEFRAMES
                        Number of noise frames
  -n N_CPUS, --n_cpus N_CPUS
                        Number of CPUs to use.
  --debug               Debug mode
  --wrap_limit          Turns off some heuristics for phase unwrapping

Vahdeta Suljic <suljic@wustl.edu>, Andrew Van <vanandrew@wustl.edu>
12/09/2022

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

warpkit-0.0.1.tar.gz (154.5 kB view details)

Uploaded Source

Built Distributions

warpkit-0.0.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.3 MB view details)

Uploaded CPython 3.12 manylinux: glibc 2.17+ x86-64

warpkit-0.0.1-cp312-cp312-macosx_10_9_universal2.whl (2.2 MB view details)

Uploaded CPython 3.12 macOS 10.9+ universal2 (ARM64, x86-64)

warpkit-0.0.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.3 MB view details)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64

warpkit-0.0.1-cp311-cp311-macosx_10_9_universal2.whl (2.2 MB view details)

Uploaded CPython 3.11 macOS 10.9+ universal2 (ARM64, x86-64)

warpkit-0.0.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.3 MB view details)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

warpkit-0.0.1-cp310-cp310-macosx_10_9_universal2.whl (2.2 MB view details)

Uploaded CPython 3.10 macOS 10.9+ universal2 (ARM64, x86-64)

warpkit-0.0.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.3 MB view details)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

warpkit-0.0.1-cp39-cp39-macosx_10_9_universal2.whl (2.2 MB view details)

Uploaded CPython 3.9 macOS 10.9+ universal2 (ARM64, x86-64)

warpkit-0.0.1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.3 MB view details)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

warpkit-0.0.1-cp38-cp38-macosx_10_9_universal2.whl (2.2 MB view details)

Uploaded CPython 3.8 macOS 10.9+ universal2 (ARM64, x86-64)

File details

Details for the file warpkit-0.0.1.tar.gz.

File metadata

  • Download URL: warpkit-0.0.1.tar.gz
  • Upload date:
  • Size: 154.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.2 CPython/3.11.7

File hashes

Hashes for warpkit-0.0.1.tar.gz
Algorithm Hash digest
SHA256 5e4d75a13441307586e162381082f8b793ea94344506744146585b2579af9ecd
MD5 211cbe04e2691b20956437934de97805
BLAKE2b-256 6d18d37edee3e136cc9e52a9984317c6589914b140dfbaf4476cadfb09cd4596

See more details on using hashes here.

File details

Details for the file warpkit-0.0.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for warpkit-0.0.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 c5f7dd22ab9fc38ddcbf7495bb7ce41657b7320e750b0e18b3834ef1452dc669
MD5 0988d26a3ab525af8096c9fa03fe036d
BLAKE2b-256 0c30bcb6b87de3893231cf0bb0dd579f3d40e0a69c6ca7b5eb5cd109a9abe21e

See more details on using hashes here.

File details

Details for the file warpkit-0.0.1-cp312-cp312-macosx_10_9_universal2.whl.

File metadata

File hashes

Hashes for warpkit-0.0.1-cp312-cp312-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 c57caa3ab0d0152ee97c9603ecd073a5b38dda6f8498399e4813e6914dbd7eb7
MD5 e498bf39b0fb907f04cdc458cc51327f
BLAKE2b-256 17a32ad024b13f1c80fbd952d0d5f2eb8d61d627e91a7a650544aa30466b6b04

See more details on using hashes here.

File details

Details for the file warpkit-0.0.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for warpkit-0.0.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 e25483f026e4185d867569dc94c70f8b8d50c2b71d5e705384adce670a2e4b2b
MD5 572730d4d8ce8608d7220095205d0c3a
BLAKE2b-256 52d2263829dc94904c30ec1689c5eb6fe16f39ff8bbbe09eccd9b5d46b89bfdd

See more details on using hashes here.

File details

Details for the file warpkit-0.0.1-cp311-cp311-macosx_10_9_universal2.whl.

File metadata

File hashes

Hashes for warpkit-0.0.1-cp311-cp311-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 144cb13d4d220903c53b00c652c24ee053c254518bedd1ebfcb528fa4597d327
MD5 cdf99c4c565b91e25d4987c79854d795
BLAKE2b-256 ae5a1cc9496fd453243d854ab2ec8fde3ed986949ba275c0c13b085987bb6b0f

See more details on using hashes here.

File details

Details for the file warpkit-0.0.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for warpkit-0.0.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 586c79556a356f32ab49bd89630a36f4d7b79c31176417781940505b2cdc0eab
MD5 c2e3ed5892ff1027ed096cfd84630383
BLAKE2b-256 6ff8834171844beeecef847ed63da983f86d9c6df9da967b028e8e0377e37b0b

See more details on using hashes here.

File details

Details for the file warpkit-0.0.1-cp310-cp310-macosx_10_9_universal2.whl.

File metadata

File hashes

Hashes for warpkit-0.0.1-cp310-cp310-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 a830a5c2f471d0ccdc2da3e4446e3e0a78e982eb764b159306c22ed0ace48cdb
MD5 8044fb28fa8850d4a038c003d3878a17
BLAKE2b-256 b6d55741b8844c7810a3e90335e0b6fa7366c0566217e300d458df2c0c6687d6

See more details on using hashes here.

File details

Details for the file warpkit-0.0.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for warpkit-0.0.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 50458de63e781e37b784b7645f7a26ac614544daceb909d88393c093a2a192d7
MD5 6618b85e311b3e84cc51542e59ea80a2
BLAKE2b-256 320d814391a9c61bfcdec67633df3f649c5a92b67691e9a53cdca85f252c95ea

See more details on using hashes here.

File details

Details for the file warpkit-0.0.1-cp39-cp39-macosx_10_9_universal2.whl.

File metadata

File hashes

Hashes for warpkit-0.0.1-cp39-cp39-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 03fbae48de0eeecda7a3992d64c534c3b7d9e62eaa6c21a35b4f1fba11ed958b
MD5 8538e6bc0c10e8368646bbbde2b7caea
BLAKE2b-256 f252858f6c95f944989cdbc60926a0be7dd88873b6f20b2474e355cde94aa45f

See more details on using hashes here.

File details

Details for the file warpkit-0.0.1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for warpkit-0.0.1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 4dc716490788e9b3986991bc351b8cbbd4903e058d5c993b05061de89eb1f41e
MD5 5581b8c88ff535b2842ec3b9ddd9b0b1
BLAKE2b-256 fdcb5fb30805a43fa0a525f28492e4ff6259f046bf04a9d2c82c974f33f399e7

See more details on using hashes here.

File details

Details for the file warpkit-0.0.1-cp38-cp38-macosx_10_9_universal2.whl.

File metadata

File hashes

Hashes for warpkit-0.0.1-cp38-cp38-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 3e7f6abfddd7bd445c61bd5170115b0e448890d2b5f1535512fa3ba0de909b13
MD5 0f35ddcdb485671e9796ed4959ab5464
BLAKE2b-256 235e2c5b0a6bfb8bf1037528880055721b47b623c0f4bff6df966738d7a0393e

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page