Last active
January 6, 2023 14:09
-
-
Save oesteban/29247794a7d8a8a48b506ea1495208f1 to your computer and use it in GitHub Desktop.
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
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*- | |
# vi: set ft=python sts=4 ts=4 sw=4 et: | |
# | |
# Copyright 2023 Oscar Esteban <[email protected]> | |
# | |
# Licensed under the Apache License, Version 2.0 (the "License"); | |
# you may not use this file except in compliance with the License. | |
# You may obtain a copy of the License at | |
# | |
# http://www.apache.org/licenses/LICENSE-2.0 | |
# | |
# Unless required by applicable law or agreed to in writing, software | |
# distributed under the License is distributed on an "AS IS" BASIS, | |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
# See the License for the specific language governing permissions and | |
# limitations under the License. | |
# | |
# We support and encourage derived works from this project, please read | |
# about our expectations at | |
# | |
# https://www.nipreps.org/community/licensing/ | |
# | |
"""CLI.""" | |
import numpy as np | |
import click | |
import nibabel as nb | |
@click.group() | |
@click.version_option(message="Manipulate NIfTI files") | |
def cli(): | |
"""Manipulating NIfTI files.""" | |
@cli.command() | |
@click.option("-i", "--in_file", type=click.Path(exists=True)) | |
@click.option("-o", "--out_file", type=click.Path(), default=None) | |
def reorient(in_file, out_file): | |
"""Reorient the input to be RAS and have (0, 0, 0) [mm] at the center of the volume.""" | |
in_nii = nb.load(in_file) | |
# Get orientation codes of input | |
in_codes = nb.aff2axcodes(in_nii.affine) | |
# Transform to reorder axes so that voxels/coordinates are IJK/XYZ | |
ornt_xfm = nb.orientations.axcodes2ornt(in_codes) | |
# Do not flip any axes | |
ornt_xfm[:, 1] = 1.0 | |
# Data array will now be in the correct order | |
in_ijk_axes = in_nii.as_reoriented(ornt_xfm) | |
if out_file is None: | |
out_file = str(in_file).replace(".nii", "_reoriented.nii") | |
# Let NiBabel calculate the canonical (RAS) orientation | |
in_ras = nb.as_closest_canonical(in_ijk_axes) | |
# Fix input's eccentricity | |
ras_affine = in_ras.affine.copy() | |
ras_affine[:3, 3] = -1.0 * ( | |
ras_affine[:3, :3] @ ((np.array(in_ras.shape) - 1) * 0.5) | |
) | |
outnii = nb.Nifti1Image(in_ijk_axes.dataobj, ras_affine, in_ras.header) | |
outnii.header.set_sform(ras_affine, code=1) | |
outnii.header.set_qform(ras_affine, code=1) | |
outnii.to_filename(out_file) | |
if __name__ == "__main__": | |
""" Install entry-point """ | |
cli() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment