|
| 1 | +import numpy as np |
| 2 | +from keras.saving import register_keras_serializable as serializable |
| 3 | + |
| 4 | +from .elementwise_transform import ElementwiseTransform |
| 5 | + |
| 6 | + |
| 7 | +@serializable(package="bayesflow.adapters") |
| 8 | +class NumpyTransform(ElementwiseTransform): |
| 9 | + """ |
| 10 | + A class to apply element-wise transformations using plain NumPy functions. |
| 11 | +
|
| 12 | + Attributes: |
| 13 | + ---------- |
| 14 | + _forward : str |
| 15 | + The name of the NumPy function to apply in the forward transformation. |
| 16 | + _inverse : str |
| 17 | + The name of the NumPy function to apply in the inverse transformation. |
| 18 | + """ |
| 19 | + |
| 20 | + INVERSE_METHODS = { |
| 21 | + np.arctan: np.tan, |
| 22 | + np.exp: np.log, |
| 23 | + np.expm1: np.log1p, |
| 24 | + np.square: np.sqrt, |
| 25 | + np.reciprocal: np.reciprocal, |
| 26 | + } |
| 27 | + # ensure the map is symmetric |
| 28 | + INVERSE_METHODS |= {v: k for k, v in INVERSE_METHODS.items()} |
| 29 | + |
| 30 | + def __init__(self, forward: str, inverse: str = None): |
| 31 | + """ |
| 32 | + Initializes the NumpyTransform with specified forward and inverse functions. |
| 33 | +
|
| 34 | + Parameters: |
| 35 | + ---------- |
| 36 | + forward: str |
| 37 | + The name of the NumPy function to use for the forward transformation. |
| 38 | + inverse: str, optional |
| 39 | + The name of the NumPy function to use for the inverse transformation. |
| 40 | + By default, the inverse is inferred from the forward argument for supported methods. |
| 41 | + """ |
| 42 | + super().__init__() |
| 43 | + |
| 44 | + if isinstance(forward, str): |
| 45 | + forward = getattr(np, forward) |
| 46 | + |
| 47 | + if not isinstance(forward, np.ufunc): |
| 48 | + raise ValueError("Forward transformation must be a NumPy Universal Function (ufunc).") |
| 49 | + |
| 50 | + if inverse is None: |
| 51 | + if forward not in self.INVERSE_METHODS: |
| 52 | + raise ValueError(f"Cannot infer inverse for method {forward!r}") |
| 53 | + |
| 54 | + inverse = self.INVERSE_METHODS[forward] |
| 55 | + |
| 56 | + if isinstance(inverse, str): |
| 57 | + inverse = getattr(np, inverse) |
| 58 | + |
| 59 | + if not isinstance(inverse, np.ufunc): |
| 60 | + raise ValueError("Inverse transformation must be a NumPy Universal Function (ufunc).") |
| 61 | + |
| 62 | + self._forward = forward |
| 63 | + self._inverse = inverse |
| 64 | + |
| 65 | + @classmethod |
| 66 | + def from_config(cls, config: dict, custom_objects=None) -> "ElementwiseTransform": |
| 67 | + return cls( |
| 68 | + forward=config["forward"], |
| 69 | + inverse=config["inverse"], |
| 70 | + ) |
| 71 | + |
| 72 | + def get_config(self) -> dict: |
| 73 | + return {"forward": self._forward.__name__, "inverse": self._inverse.__name__} |
| 74 | + |
| 75 | + def forward(self, data: dict[str, any], **kwargs) -> dict[str, any]: |
| 76 | + return self._forward(data) |
| 77 | + |
| 78 | + def inverse(self, data: np.ndarray, **kwargs) -> np.ndarray: |
| 79 | + return self._inverse(data) |
0 commit comments