|
| 1 | +import numpy as np |
| 2 | + |
| 3 | +from few.tests.base import FewTest |
| 4 | +from few.utils.mappings.pn import Y_to_xI, xI_to_Y |
| 5 | + |
| 6 | + |
| 7 | +class PnMappingsTest(FewTest): |
| 8 | + @classmethod |
| 9 | + def name(self) -> str: |
| 10 | + return "PN Mappings" |
| 11 | + |
| 12 | + def test_back_and_forth_scalar_xI(self): |
| 13 | + # When a, p, and e are scalar values |
| 14 | + a = 0.5 |
| 15 | + p = 10.0 |
| 16 | + e = 0.0 |
| 17 | + # Given a xI value in the range [-1, 1] |
| 18 | + xI_inputs = np.linspace(-1.0, 1.0, 10, endpoint=True) |
| 19 | + self.logger.info(f"{xI_inputs=}") |
| 20 | + # Let's map xI values to corresponding Y |
| 21 | + Y_inputs = np.array([xI_to_Y(a, p, e, xI) for xI in xI_inputs]) |
| 22 | + self.logger.info(f"{Y_inputs=}") |
| 23 | + |
| 24 | + # Then map result Y back to xI |
| 25 | + xI_outputs = np.array([Y_to_xI(a, p, e, Y) for Y in Y_inputs]) |
| 26 | + self.logger.info(f"{xI_outputs=}") |
| 27 | + # And check that resulting values are close to input ones |
| 28 | + np.testing.assert_allclose(xI_outputs, xI_inputs, rtol=1e-4) |
| 29 | + |
| 30 | + # And map once again to Y |
| 31 | + Y_outputs = np.array([xI_to_Y(a, p, e, xI) for xI in xI_outputs]) |
| 32 | + # And check values match |
| 33 | + np.testing.assert_allclose(Y_outputs, Y_inputs, rtol=1e-4) |
0 commit comments