Source code for ddd_pylib.image_manip._resample_isotropic
from ._rescale_operator import RescaleOperator
import xarray as xr
[docs]
class ResampleIsotropicOperator(RescaleOperator):
"""
Artificially makes isotropic the voxels of a 3D image by resampling it.
The scale is determined automatically based on the main image's scale.
"""
def __init__(self):
super().__init__()
[docs]
def sanityCheck(self):
super().sanityCheck()
img = self.getMainImage()
if "scale" not in img.attrs:
raise ValueError("The main image must have a 'scale' attribute.")
if "Z" not in img.dims:
raise ValueError("The main image must have a 'Z' dimension.")
def _makeScaleFactors(self):
img = self.getMainImage()
z_scale = img.attrs["scale"].get("Z", 1.0)
yx_scale = img.attrs["scale"].get("Y", 1.0)
if yx_scale < z_scale:
factors = {"Z": z_scale / yx_scale, "Y": 1.0, "X": 1.0}
else:
factors = {"Z": 1.0, "Y": yx_scale / z_scale, "X": yx_scale / z_scale}
return factors
[docs]
def getPrefix(self) -> str:
return f"Isotropic[{self._interpolation}]-"
[docs]
def getScale(self) -> dict:
return self._makeScaleFactors()
[docs]
def setScale(self, scale):
raise ValueError(
"Scale cannot be set for ResampleIsotropic. It is determined automatically based on the main image's scale."
)
def _rescaleImage(
self, image: xr.DataArray, scale: dict, interpolation: str
) -> xr.DataArray:
scale = self._makeScaleFactors()
return super()._rescaleImage(image, scale, interpolation)