|
| 1 | +import json |
| 2 | +from typing import TYPE_CHECKING, Any, Dict, List, Optional, Union |
| 3 | + |
| 4 | +__all__ = ["CoordinateSystem"] |
| 5 | + |
| 6 | +Axis_t = Dict[str, str] |
| 7 | +CoordSystem_t = Dict[str, Union[str, List[Dict[str, str]]]] |
| 8 | +AXIS_ORDER = ["t", "c", "z", "y", "x"] |
| 9 | + |
| 10 | + |
| 11 | +class Axis: |
| 12 | + name: str |
| 13 | + type: str |
| 14 | + unit: Optional[str] |
| 15 | + |
| 16 | + def __init__(self, name: str, type: str, unit: Optional[str] = None): |
| 17 | + self.name = name |
| 18 | + self.type = type |
| 19 | + self.unit = unit |
| 20 | + |
| 21 | + def to_dict(self) -> Axis_t: |
| 22 | + d = {"name": self.name, "type": self.type} |
| 23 | + if self.unit is not None: |
| 24 | + d["unit"] = self.unit |
| 25 | + return d |
| 26 | + |
| 27 | + |
| 28 | +class CoordinateSystem: |
| 29 | + def __init__(self, name: Optional[str] = None, axes: Optional[List[Axis]] = None): |
| 30 | + self._name = name |
| 31 | + self._axes = axes if axes is not None else [] |
| 32 | + |
| 33 | + @staticmethod |
| 34 | + def from_dict(coord_sys: CoordSystem_t) -> "CoordinateSystem": |
| 35 | + if "name" not in coord_sys.keys(): |
| 36 | + raise ValueError("`coordinate_system` MUST have a name.") |
| 37 | + if "axes" not in coord_sys.keys(): |
| 38 | + raise ValueError("`coordinate_system` MUST have axes.") |
| 39 | + |
| 40 | + if TYPE_CHECKING: |
| 41 | + assert isinstance(coord_sys["name"], str) |
| 42 | + assert isinstance(coord_sys["axes"], list) |
| 43 | + name = coord_sys["name"] |
| 44 | + |
| 45 | + # sorted_axes = sorted(coord_sys["axes"], key=lambda x: AXIS_ORDER.index(x["name"])) |
| 46 | + axes = [] |
| 47 | + for axis in coord_sys["axes"]: |
| 48 | + # for axis in sorted_axes: |
| 49 | + if "name" not in axis.keys(): |
| 50 | + raise ValueError("Each axis MUST have a name.") |
| 51 | + if "type" not in axis.keys(): |
| 52 | + raise ValueError("Each axis MUST have a type.") |
| 53 | + if "unit" not in axis.keys(): |
| 54 | + if not axis["type"] in ["channel", "array"]: |
| 55 | + raise ValueError("Each axis is either of type channel either MUST have a unit.") |
| 56 | + kw = {} |
| 57 | + if "unit" in axis.keys(): |
| 58 | + kw = {"unit": axis["unit"]} |
| 59 | + axes.append(Axis(name=axis["name"], type=axis["type"], **kw)) |
| 60 | + return CoordinateSystem(name=name, axes=axes) |
| 61 | + |
| 62 | + def to_dict(self) -> CoordSystem_t: |
| 63 | + out: Dict[str, Any] = {"name": self.name, "axes": [axis.to_dict() for axis in self._axes]} |
| 64 | + # if TYPE_CHECKING: |
| 65 | + # assert isinstance(out["axes"], list) |
| 66 | + return out |
| 67 | + |
| 68 | + def from_array(self, array: Any) -> None: |
| 69 | + raise NotImplementedError() |
| 70 | + |
| 71 | + @staticmethod |
| 72 | + def from_json(data: Union[str, bytes]) -> "CoordinateSystem": |
| 73 | + coord_sys = json.loads(data) |
| 74 | + return CoordinateSystem.from_dict(coord_sys) |
| 75 | + |
| 76 | + def to_json(self, **kwargs: Any) -> str: |
| 77 | + out = self.to_dict() |
| 78 | + return json.dumps(out, **kwargs) |
| 79 | + |
| 80 | + def __eq__(self, other: Any) -> bool: |
| 81 | + if not isinstance(other, CoordinateSystem): |
| 82 | + return False |
| 83 | + return self.to_dict() == other.to_dict() |
| 84 | + |
| 85 | + @property |
| 86 | + def name(self) -> Optional[str]: |
| 87 | + return self._name |
| 88 | + |
| 89 | + @property |
| 90 | + def axes_names(self) -> List[str]: |
| 91 | + return [ax.name for ax in self._axes] |
| 92 | + |
| 93 | + @property |
| 94 | + def axes_types(self) -> List[str]: |
| 95 | + return [ax.type for ax in self._axes] |
| 96 | + |
| 97 | + def __hash__(self) -> int: |
| 98 | + return hash(frozenset(self.to_dict())) |
0 commit comments