Skip to content

Commit

Permalink
Add CoordinateTranform reprs
Browse files Browse the repository at this point in the history
  • Loading branch information
jp-dark committed Oct 10, 2024
1 parent 15cbe86 commit a50600d
Showing 1 changed file with 35 additions and 1 deletion.
36 changes: 35 additions & 1 deletion python-spec/src/somacore/coordinates.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

import abc
import collections.abc
from typing import Optional, Sequence, Tuple, Union
import itertools
from typing import Iterable, Optional, Sequence, Tuple, Union

import attrs
import numpy as np
Expand Down Expand Up @@ -100,6 +101,25 @@ def _check_rmatmul_inner_axes(self, other: "CoordinateTransform"):
f"{type(self).__name__}."
)

@abc.abstractmethod
def _contents_lines(self) -> Iterable[str]:
return
yield

def _my_repr(self) -> Iterable[str]:
yield f"{type(self).__name__}"
yield f" input axes: {self._input_axes}"
yield f" output axes: {self._output_axes}"

def __repr__(self) -> str:
content = self._contents_lines
lines = (
self._my_repr()
if content is None
else itertools.chain(self._my_repr(), self._contents_lines())
)
return "<" + "\n".join(lines) + ">"

@abc.abstractmethod
def __matmul__(self, other: object) -> "CoordinateTransform":
raise NotImplementedError()
Expand Down Expand Up @@ -190,6 +210,10 @@ def __init__(
f"Unexpected shape {self._matrix.shape} for the input affine matrix."
)

def _contents_lines(self) -> Iterable[str]:
yield " augmented matrix:"
yield " " + str(self._matrix).replace("\n", "\n ")

def __matmul__(self, other: object) -> CoordinateTransform:
if not isinstance(other, CoordinateTransform):
raise NotImplementedError(
Expand Down Expand Up @@ -264,6 +288,9 @@ def __init__(

super().__init__(input_axes, output_axes, np.diag(self._scale_factors))

def _contents_lines(self) -> Iterable[str]:
yield f" scales: {self._scale_factors}"

def __matmul__(self, other: object) -> CoordinateTransform:
if not isinstance(other, CoordinateTransform):
raise NotImplementedError(
Expand Down Expand Up @@ -317,6 +344,9 @@ def __init__(
rank = str_or_seq_length(input_axes)
super().__init__(input_axes, output_axes, rank * [self._scale])

def _contents_lines(self) -> Iterable[str]:
yield f" scale: {self._scale}"

def __matmul__(self, other: object) -> CoordinateTransform:
if not isinstance(other, CoordinateTransform):
raise NotImplementedError(
Expand Down Expand Up @@ -366,6 +396,10 @@ def __init__(
):
super().__init__(input_axes, output_axes, 1)

def _contents_lines(self) -> Iterable[str]:
return
yield

def __matmul__(self, other: object) -> CoordinateTransform:
if not isinstance(other, CoordinateTransform):
raise NotImplementedError(
Expand Down

0 comments on commit a50600d

Please sign in to comment.