|
| 1 | +from abc import ABC, abstractmethod |
| 2 | + |
| 3 | +import numpy as np |
| 4 | +from exceptions import InterpretationError |
| 5 | +from numpy._typing import ArrayLike |
| 6 | +from quantities.quantity import Quantity |
| 7 | +from util import is_increasing |
| 8 | + |
| 9 | + |
| 10 | +class Abscissa(ABC): |
| 11 | + |
| 12 | + def __init__(self, axes: list[Quantity]): |
| 13 | + self._axes = axes |
| 14 | + self._dimensionality = len(axes) |
| 15 | + |
| 16 | + @property |
| 17 | + def dimensionality(self) -> int: |
| 18 | + """ Dimensionality of this data """ |
| 19 | + return self._dimensionality |
| 20 | + |
| 21 | + @property |
| 22 | + @abstractmethod |
| 23 | + def is_grid(self) -> bool: |
| 24 | + """ Are these coordinates using a grid representation |
| 25 | + (as opposed to a general list representation) |
| 26 | +
|
| 27 | + is_grid = True: implies that the corresponding ordinate is n-dimensional tensor |
| 28 | + is_grid = False: implies that the corresponding ordinate is a 1D list |
| 29 | +
|
| 30 | + If the data is one dimensional, is_grid=True |
| 31 | + """ |
| 32 | + |
| 33 | + @property |
| 34 | + def axes(self) -> list[Quantity]: |
| 35 | + """ Axes of the data: |
| 36 | +
|
| 37 | + If it's an (n1-by-n2-by-n3...) grid (is_grid=True): give the values for each axis, returning a list like |
| 38 | + [Quantity(length n1), Quantity(length n2), Quantity(length n3) ... ] |
| 39 | +
|
| 40 | + If it is not grid data (is_grid=False), but n points on a general mesh, give one array for each dimension |
| 41 | + [Quantity(length n), Quantity(length n), Quantity(length n) ... ] |
| 42 | + """ |
| 43 | + |
| 44 | + return self._axes |
| 45 | + |
| 46 | + @staticmethod |
| 47 | + def _determine_error_message(axis_arrays: list[np.ndarray], ordinate_shape: tuple): |
| 48 | + """ Error message for the `.determine` function""" |
| 49 | + |
| 50 | + shape_string = ", ".join([str(axis.shape) for axis in axis_arrays]) |
| 51 | + |
| 52 | + return f"Cannot interpret array shapes axis: [{shape_string}], ordinate: {ordinate_shape}" |
| 53 | + |
| 54 | + @staticmethod |
| 55 | + def determine(axis_data: list[Quantity[ArrayLike]], ordinate_data: Quantity[ArrayLike]) -> "Abscissa": |
| 56 | + """ Get an Abscissa object that fits the combination of axes and data""" |
| 57 | + |
| 58 | + # Different posibilites: |
| 59 | + # 1: axes_data[i].shape == axes_data[j].shape == ordinate_data.shape |
| 60 | + # 1a: axis_data[i] is 1D => |
| 61 | + # 1a-i: len(axes_data) == 1 => Grid type or Scatter type depending on sortedness |
| 62 | + # 1a-ii: len(axes_data) != 1 => Scatter type |
| 63 | + # 1b: axis_data[i] dimensionality matches len(axis_data) => Meshgrid type |
| 64 | + # 1c: other => Error |
| 65 | + # 2: (len(axes_data[0]), len(axes_data[1])... ) == ordinate_data.shape => Grid type |
| 66 | + # 3: None of the above => Error |
| 67 | + |
| 68 | + ordinate_shape = np.array(ordinate_data.value).shape |
| 69 | + axis_arrays = [np.array(axis.value) for axis in axis_data] |
| 70 | + |
| 71 | + # 1: |
| 72 | + if all([axis.shape == ordinate_shape for axis in axis_arrays]): |
| 73 | + # 1a: |
| 74 | + if all([len(axis.shape)== 1 for axis in axis_arrays]): |
| 75 | + # 1a-i: |
| 76 | + if len(axis_arrays) == 1: |
| 77 | + # Is it sorted |
| 78 | + if is_increasing(axis_arrays[0]): |
| 79 | + return GridAbscissa(axis_data) |
| 80 | + else: |
| 81 | + return ScatterAbscissa(axis_data) |
| 82 | + # 1a-ii |
| 83 | + else: |
| 84 | + return ScatterAbscissa(axis_data) |
| 85 | + # 1b |
| 86 | + elif all([len(axis.shape) == len(axis_arrays) for axis in axis_arrays]): |
| 87 | + |
| 88 | + return MeshgridAbscissa(axis_data) |
| 89 | + |
| 90 | + else: |
| 91 | + raise InterpretationError(Abscissa._determine_error_message(axis_arrays, ordinate_shape)) |
| 92 | + |
| 93 | + elif all([len(axis.shape)== 1 for axis in axis_arrays]) and \ |
| 94 | + tuple([axis.shape[0] for axis in axis_arrays]) == ordinate_shape: |
| 95 | + |
| 96 | + # Require that they are sorted |
| 97 | + if all([is_increasing(axis) for axis in axis_arrays]): |
| 98 | + |
| 99 | + return GridAbscissa(axis_data) |
| 100 | + |
| 101 | + else: |
| 102 | + raise InterpretationError("Grid axes are not sorted") |
| 103 | + |
| 104 | + else: |
| 105 | + raise InterpretationError(Abscissa._determine_error_message(axis_arrays, ordinate_shape)) |
| 106 | + |
| 107 | +class GridAbscissa(Abscissa): |
| 108 | + |
| 109 | + @property |
| 110 | + def is_grid(self): |
| 111 | + return True |
| 112 | + |
| 113 | +class MeshgridAbscissa(Abscissa): |
| 114 | + |
| 115 | + @property |
| 116 | + def is_grid(self): |
| 117 | + return True |
| 118 | + |
| 119 | +class ScatterAbscissa(Abscissa): |
| 120 | + |
| 121 | + @property |
| 122 | + def is_grid(self): |
| 123 | + return False |
0 commit comments