Skip to content

Commit

Permalink
Fix verification of header
Browse files Browse the repository at this point in the history
  • Loading branch information
EmmaRenauld committed Apr 26, 2024
1 parent 44b096c commit 9ee46ff
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
7 changes: 3 additions & 4 deletions scilpy/image/volume_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ def _interp_code_to_order(interp_code):
return orders[interp_code]


def resample_volume(img, ref=None, res=None, iso_min=False, zoom=None,
def resample_volume(img, ref_img=None, res=None, iso_min=False, zoom=None,
interp='lin', enforce_dimensions=False):
"""
Function to resample a dataset to match the resolution of another
Expand All @@ -463,7 +463,7 @@ def resample_volume(img, ref=None, res=None, iso_min=False, zoom=None,
----------
img: nib.Nifti1Image
Image to resample.
ref: nib.Nifti1Image
ref_img: nib.Nifti1Image, optional
Reference volume to resample to. This method is used only if ref is not
None. (default: None)
res: tuple, shape (3,) or int, optional
Expand Down Expand Up @@ -492,11 +492,10 @@ def resample_volume(img, ref=None, res=None, iso_min=False, zoom=None,
affine = img.affine
original_zooms = img.header.get_zooms()[:3]

if ref is not None:
if ref_img is not None:
if iso_min or zoom or res:
raise ValueError('Please only provide one option amongst ref, res '
', zoom or iso_min.')
ref_img = nib.load(ref)
new_zooms = ref_img.header.get_zooms()[:3]
elif res is not None:
if iso_min or zoom:
Expand Down
14 changes: 11 additions & 3 deletions scripts/scil_volume_resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import logging

import nibabel as nib
import numpy as np

from scilpy.io.utils import (add_verbose_arg, add_overwrite_arg,
assert_inputs_exist, assert_outputs_exist,
Expand Down Expand Up @@ -67,8 +68,6 @@ def main():
# Checking args
assert_inputs_exist(parser, args.in_image, args.ref)
assert_outputs_exist(parser, args, args.out_image)
assert_headers_compatible(parser, args.in_image, args.ref)
assert_headers_compatible(parser, args.in_image, args.ref)

if args.enforce_dimensions and not args.ref:
parser.error("Cannot enforce dimensions without a reference image")
Expand All @@ -85,8 +84,17 @@ def main():

img = nib.load(args.in_image)

ref_img = None
if args.ref:
ref_img = nib.load(args.ref)
# Must not verify that headers are compatible. But can verify that, at
# least, that the last column of their affine are compatible.
if not np.array_equal(img.affine[:, -1], ref_img.affine[:, -1]):
parser.error("The --ref image should have the same affine as the "
"input image (but with a different sampling).")

# Resampling volume
resampled_img = resample_volume(img, ref=args.ref, res=args.volume_size,
resampled_img = resample_volume(img, ref_img=ref_img, res=args.volume_size,
iso_min=args.iso_min, zoom=args.voxel_size,
interp=args.interp,
enforce_dimensions=args.enforce_dimensions)
Expand Down

0 comments on commit 9ee46ff

Please sign in to comment.