Source code for allensdk.brain_observatory.behavior.image_api
import SimpleITK as sitk
import numpy as np
from typing import NamedTuple
[docs]class Image(NamedTuple):
''' Describes a 2D Image
data : np.ndarray
Image data points
spacing : tuple
Spacing describes the physical size of each pixel
unit : str
Physical unit of the spacing (currently constrained to be isotropic)
'''
data: np.ndarray
spacing: tuple
unit: int = 0
def __eq__(self, other):
a = np.array_equal(self.data, other.data)
b = self.spacing == other.spacing
c = self.unit == other.unit
return a and b and c
def __array__(self):
return np.array(self.data)
[docs]class ImageApi:
[docs] @staticmethod
def serialize(data, spacing, unit):
img = sitk.GetImageFromArray(data)
img.SetSpacing(np.array(spacing, dtype=np.double))
img.SetMetaData('unit', unit)
return img
[docs] @staticmethod
def deserialize(img):
data = sitk.GetArrayFromImage(img)
spacing = img.GetSpacing()
unit = img.GetMetaData('unit')
return Image(data, spacing, unit)