|
| 1 | +from abc import ABC, abstractmethod |
| 2 | + |
| 3 | +import numpy as np |
| 4 | +from numpy._typing import ArrayLike |
| 5 | + |
| 6 | +from quantities.quantity import Quantity |
| 7 | + |
| 8 | + |
| 9 | +class Abscissa(ABC): |
| 10 | + |
| 11 | + def __init__(self, axes: list[Quantity]): |
| 12 | + self._axes = axes |
| 13 | + self._dimensionality = len(axes) |
| 14 | + @property |
| 15 | + def dimensionality(self) -> int: |
| 16 | + """ Dimensionality of this data """ |
| 17 | + return self._dimensionality |
| 18 | + |
| 19 | + @property |
| 20 | + @abstractmethod |
| 21 | + def is_grid(self) -> bool: |
| 22 | + """ Are these coordinates using a grid representation |
| 23 | + ( as opposed to a general list representation) |
| 24 | +
|
| 25 | + is_grid = True: implies that the corresponding ordinate is n-dimensional tensor |
| 26 | + is_grid = False: implies that the corresponding ordinate is a 1D list |
| 27 | +
|
| 28 | + If the data is one dimensional, is_grid=True |
| 29 | +
|
| 30 | + """ |
| 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 | + |
| 45 | + return self._axes |
| 46 | + |
| 47 | + @staticmethod |
| 48 | + def determine(axes_data: list[Quantity[ArrayLike]], ordinate_data: Quantity[ArrayLike]) -> "Abscissa": |
| 49 | + """ Get an Abscissa object that fits the combination of axes and data""" |
| 50 | + |
| 51 | + # Different possibilites: |
| 52 | + # 1: axes_data[i].shape == axes_data[j].shape == ordinate_data.shape |
| 53 | + # 1a: axis_data[i] is 1D => |
| 54 | + # 1a-i: len(axes_data) == 1 => Grid type (trivially) |
| 55 | + # 1a-ii: len(axes_data) != 1 => Mesh type |
| 56 | + # 1b: axis_data[i] dimensionality matches len(axis_data) => Grid type |
| 57 | + # 1c: other => Error |
| 58 | + # 2: (len(axes_data[0]), len(axes_data[1])... ) == ordinate_data.shape => Grid type |
| 59 | + # 3: None of the above => Error |
| 60 | + |
| 61 | + ordinate_shape = np.array(ordinate_data.value).shape |
| 62 | + |
| 63 | + if len(ordinate_shape) == 1 and all([np.array(axis.value).shape == ordinate_shape for axis in axes_data]): |
| 64 | + return ScatterAbscissa(axes_data) |
| 65 | + |
| 66 | + elif |
| 67 | +class GridAbscissa(Abscissa): |
| 68 | + |
| 69 | + @property |
| 70 | + def is_grid(self): |
| 71 | + return True |
| 72 | + |
| 73 | +class ScatterAbscissa(Abscissa): |
| 74 | + |
| 75 | + @property |
| 76 | + def is_grid(self): |
| 77 | + return False |
| 78 | + |
0 commit comments