def test_calibration_inverse(): """Test calibrations with and without inverse expression.""" fname = os.path.join(TEST_OUTPUTS, "calibration__inverse.h5") # cal1 has an explicit inverse expression, cal2 does not cal1 = Calibration( "p[0] + p[1] * x", [5.0, 4.0], inv_expression="(y - p[0]) / p[1]" ) cal2 = Calibration(cal1.expression, [5.0, 4.0]) assert cal1 == cal2 # evaluate the inverse for a scalar y = 100.0 x1 = cal1.inverse(y) x2 = cal2.inverse(y) assert np.isclose(x1, (y - 5.0) / 4.0) assert np.isclose(x1, x2) # evaluate the inverse for a scalar with initial guess x1 = cal1.inverse(y, x0=25.0) x2 = cal2.inverse(y, x0=25.0) assert np.isclose(x1, (y - 5.0) / 4.0) assert np.isclose(x1, x2) # evaluate the inverse for an array y = np.linspace(20.0, 500.0, num=100) x1 = cal1.inverse(y) x2 = cal2.inverse(y) assert np.allclose(x1, (y - 5.0) / 4.0) assert np.allclose(x1, x2) # evaluate the inverse for an array with initial guesses y = np.linspace(20.0, 500.0, num=100) x0 = np.arange(len(y)) / 4.0 x1 = cal1.inverse(y, x0=x0) x2 = cal2.inverse(y, x0=x0) assert np.allclose(x1, (y - 5.0) / 4.0) assert np.allclose(x1, x2) # evaluate the inverse for a value outside the range with pytest.raises(CalibrationError): cal1.inverse(-10.0) with pytest.raises(CalibrationError): cal1.inverse(2e6) # test __str__() and __repr__() str(cal1) repr(cal1) # test write() and read() cal1.write(fname) cal3 = Calibration.read(fname) assert cal3.inv_expression is not None assert cal3.inv_expression == cal1.inv_expression
def test_calibration_read_failures(): """Test miscellaneous HDF5 reading failures.""" fname = os.path.join(TEST_OUTPUTS, "calibration__read_failures.h5") cal = Calibration.from_linear([2.0, 3.0]) cal.add_points([0, 1000, 2000], [0, 1000, 2000]) # remove datasets from the file and show that read raises an error for dset_name in ["params", "expression", "domain", "range"]: cal.write(fname) with h5.open_h5(fname, "r+") as f: del f[dset_name] with pytest.raises(CalibrationError): Calibration.read(fname) # remove datasets from the file and show that read does *not* raise error for dset_name in ["points_x", "points_y"]: cal.write(fname) with h5.open_h5(fname, "r+") as f: del f[dset_name] Calibration.read(fname) # add unexpected dataset to the file and show that read raises an error cal.write(fname) with h5.open_h5(fname, "r+") as f: f.create_dataset("unexpected", data=[0, 1, 2]) with pytest.raises(CalibrationError): Calibration.read(fname)
def test_calibration_set_add_points(name, args): """Test Calibration.set_points and add_points methods.""" fname = os.path.join(TEST_OUTPUTS, f"calibration__add_points__{name}.h5") cal = make_calibration(name, args) # test set_points cal.set_points() cal.set_points(1000, 1000) cal.set_points((0, 1000), (0, 1000)) cal.set_points([], []) # test add_points cal.add_points() # does nothing for px, py in [[(), ()], [1000, 1000], [(0, 1000), (0, 1000)]]: cal.add_points(px, py) # test __str__() and __repr__() str(cal) repr(cal) # test write() cal.write(fname) # test read() cal2 = Calibration.read(fname) # test __eq__() assert cal2 == cal # points_x is not 1D with pytest.raises(CalibrationError): points_1d = np.array([0, 1000, 2000]) points_2d = np.reshape(points_1d, (1, 3)) cal.add_points(points_2d, points_1d) # points_y is not 1D with pytest.raises(CalibrationError): points_1d = np.array([0, 1000, 2000]) points_2d = np.reshape(points_1d, (1, 3)) cal.add_points(points_1d, points_2d) # points have different lengths with pytest.raises(CalibrationError): points_1d = np.array([0, 1000, 2000]) points_2d = np.reshape(points_1d, (1, 3)) cal.add_points([0, 1000, 2000], [0, 2000]) # points_x contains negative values with pytest.raises(CalibrationError): cal.add_points([0, -2000], [0, 2000]) # points_y contains negative values with pytest.raises(CalibrationError): cal.add_points([0, 2000], [0, -2000])
def test_calibration(name, args): """Test the Calibration class.""" fname = os.path.join(TEST_OUTPUTS, f"calibration__init__{name}.h5") # test __init__() cal = make_calibration(name, args) # test protections on setting parameters with pytest.raises(CalibrationError): cal.params = None with pytest.raises(CalibrationError): cal.params = np.ones((1, 2)) # test write() cal.write(fname) # test read() cal2 = Calibration.read(fname) # test __eq__() assert cal2 == cal # test copy() cal3 = cal.copy() assert cal3 == cal # test __call__() cal(100.0) str(cal) repr(cal)