A simple transform that takes and arbitrary function for the forward and inverse transform.
| 231 | |
| 232 | |
| 233 | class FuncTransform(Transform): |
| 234 | """ |
| 235 | A simple transform that takes and arbitrary function for the |
| 236 | forward and inverse transform. |
| 237 | """ |
| 238 | |
| 239 | input_dims = output_dims = 1 |
| 240 | |
| 241 | def __init__(self, forward, inverse): |
| 242 | """ |
| 243 | Parameters |
| 244 | ---------- |
| 245 | forward : callable |
| 246 | The forward function for the transform. This function must have |
| 247 | an inverse and, for best behavior, be monotonic. |
| 248 | It must have the signature:: |
| 249 | |
| 250 | def forward(values: array-like) -> array-like |
| 251 | |
| 252 | inverse : callable |
| 253 | The inverse of the forward function. Signature as ``forward``. |
| 254 | """ |
| 255 | super().__init__() |
| 256 | if callable(forward) and callable(inverse): |
| 257 | self._forward = forward |
| 258 | self._inverse = inverse |
| 259 | else: |
| 260 | raise ValueError('arguments to FuncTransform must be functions') |
| 261 | |
| 262 | def transform_non_affine(self, values): |
| 263 | return self._forward(values) |
| 264 | |
| 265 | def inverted(self): |
| 266 | return FuncTransform(self._inverse, self._forward) |
| 267 | |
| 268 | |
| 269 | class FuncScale(ScaleBase): |