Last active
March 17, 2017 16:59
-
-
Save ofgulban/4c775726b8cc7ac06ca5f736f3a7c120 to your computer and use it in GitHub Desktop.
Sort 4D nifti files to get minimum/maximum intensity volume projections.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""Example for sorting 4D nifti data.""" | |
import numpy as np | |
import os | |
from nibabel import load, save, Nifti1Image | |
from __future__ import division | |
nii = load('/path/to/your/4D_nifti_file.nii.gz') | |
basename = nii.get_filename().split(os.extsep, 1)[0] | |
niiHeader = nii.header | |
niiAffine = nii.affine | |
data = nii.get_data() | |
data = np.sort(data, axis=3) | |
# for only saving min and max | |
data_min = data[:, :, :, 0] | |
data_max = data[:, :, :, 3] | |
# save | |
out = Nifti1Image(data, header=niiHeader, affine=niiAffine) | |
save(out, basename + '_sorted.nii.gz') | |
out = Nifti1Image(data_min, header=niiHeader, affine=niiAffine) | |
save(out, basename + '_min.nii.gz') | |
out = Nifti1Image(data_max, header=niiHeader, affine=niiAffine) | |
save(out, basename + '_max.nii.gz') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment