diff --git a/src/tests/problem.py b/src/tests/problem.py new file mode 100644 index 0000000000000000000000000000000000000000..97c2ca2cbb25310492561d267a482147c0751ce9 --- /dev/null +++ b/src/tests/problem.py @@ -0,0 +1,20 @@ +from calng import utils + + +calls = 0 + +@utils.threadsafe_cache +def will_raise_once(argument): + global calls + calls += 1 + if calls == 1: + raise Exception("That's just what I do") + return argument + 1 + +try: + will_raise_once(0) +except Exception as ex: + print("As expected, firs call raised:", ex) + +print("Now calling again:") +print(will_raise_once(0)) diff --git a/src/tests/test_agipd_kernels.py b/src/tests/test_agipd_kernels.py index 4a5aa10db181738274ebbd2c21eb90e44c2b39c1..73b776aba85acf5e34bb5a671f3b484e3c57b8b9 100644 --- a/src/tests/test_agipd_kernels.py +++ b/src/tests/test_agipd_kernels.py @@ -3,7 +3,7 @@ import numpy as np import pathlib import pytest -from calng import agipd_gpu +from calng import AgipdCorrection input_dtype = np.uint16 output_dtype = np.float16 @@ -30,7 +30,7 @@ with h5py.File(caldb_prefix / "cal.1619543664.1545036.h5", "r") as fd: with h5py.File(caldb_prefix / "cal.1615377705.8904035.h5", "r") as fd: slopes_pc_map = np.array(fd["/AGIPD_SIV1_AGIPDV11_M305/SlopesPC/0/data"]) -kernel_runner = agipd_gpu.AgipdGpuRunner( +kernel_runner = AgipdCorrection.AgipdGpuRunner( pixels_x, pixels_y, memory_cells, @@ -85,7 +85,7 @@ def test_thresholding(): kernel_runner.load_cell_table(cell_table) kernel_runner.load_data(raw_data) kernel_runner.load_thresholds(thresholds) - kernel_runner.correct(agipd_gpu.CorrectionFlags.THRESHOLD) + kernel_runner.correct(AgipdCorrection.CorrectionFlags.THRESHOLD) gpu_res = kernel_runner.gain_map_gpu.get() assert np.allclose(gpu_res, gain_map_cpu) @@ -97,7 +97,8 @@ def test_offset(): kernel_runner.load_offset_map(offset_map) # have to do thresholding, otherwise all is treated as high gain kernel_runner.correct( - agipd_gpu.CorrectionFlags.THRESHOLD | agipd_gpu.CorrectionFlags.OFFSET + AgipdCorrection.CorrectionFlags.THRESHOLD + | AgipdCorrection.CorrectionFlags.OFFSET ) cpu_res = corr_offset_cpu(raw_data, cell_table, gain_map_cpu, offset_map) gpu_res = kernel_runner.processed_data_gpu.get() @@ -110,7 +111,8 @@ def test_rel_gain_pc(): kernel_runner.load_thresholds(thresholds) kernel_runner.load_rel_gain_pc_map(slopes_pc_map) kernel_runner.correct( - agipd_gpu.CorrectionFlags.THRESHOLD | agipd_gpu.CorrectionFlags.REL_GAIN_PC + AgipdCorrection.CorrectionFlags.THRESHOLD + | AgipdCorrection.CorrectionFlags.REL_GAIN_PC ) cpu_res = corr_rel_gain_pc_cpu(raw_data, cell_table, gain_map_cpu, slopes_pc_map) gpu_res = kernel_runner.processed_data_gpu.get() diff --git a/src/tests/test_calcat_utils.py b/src/tests/test_calcat_utils.py index 81a866a55c3abdc42c9083086f3dcca3739bb218..4b647c805876766ccc9fc66a36576788f3f216e5 100644 --- a/src/tests/test_calcat_utils.py +++ b/src/tests/test_calcat_utils.py @@ -1,9 +1,10 @@ import pathlib import timeit -from calng import calcat_utils +from calng import AgipdCorrection, DsscCorrection from calng.utils import Stopwatch from karabo.bound import Hash, Schema +import pytest # TODO: secrets management _test_dir = pathlib.Path(__file__).absolute().parent @@ -39,13 +40,13 @@ class DummyAgipdDevice(DummyBaseDevice): @staticmethod def expectedParameters(expected): - calcat_utils.AgipdCalcatFriend.add_schema( + AgipdCorrection.AgipdCalcatFriend.add_schema( expected, DummyAgipdDevice.managed_keys ) def __init__(self, config): self.schema = config - self.calibration_constant_manager = calcat_utils.AgipdCalcatFriend( + self.calibration_constant_manager = AgipdCorrection.AgipdCalcatFriend( self, _test_calcat_secrets_fn, ) @@ -53,8 +54,6 @@ class DummyAgipdDevice(DummyBaseDevice): DummyAgipdDevice.expectedParameters(DummyAgipdDevice.device_class_schema) -print(DummyAgipdDevice.device_class_schema) -print(DummyAgipdDevice.managed_keys) class DummyDsscDevice(DummyBaseDevice): @@ -63,13 +62,14 @@ class DummyDsscDevice(DummyBaseDevice): @staticmethod def expectedParameters(expected): - # super(DummyDsscDevice, DummyDsscDevice).expectedParameters(expected) - calcat_utils.DsscCalcatFriend.add_schema(expected, DummyDsscDevice.managed_keys) + DsscCorrection.DsscCalcatFriend.add_schema( + expected, DummyDsscDevice.managed_keys + ) def __init__(self, config): # TODO: check config against schema (as Karabo would) self.schema = config - self.calibration_constant_manager = calcat_utils.DsscCalcatFriend( + self.calibration_constant_manager = DsscCorrection.DsscCalcatFriend( self, _test_calcat_secrets_fn, ) @@ -78,6 +78,7 @@ class DummyDsscDevice(DummyBaseDevice): DummyDsscDevice.expectedParameters(DummyDsscDevice.device_class_schema) +@pytest.mark.skip(reason="Async currently behind lock, so no concurrent funt") def test_agipd_constants_and_caching_and_async(): # def test_agipd_constants(): conf = Hash() @@ -101,7 +102,7 @@ def test_agipd_constants_and_caching_and_async(): with Stopwatch() as timer_async_cold: # TODO: put this sort of thing in BaseCalcatFriend threads = [] - for constant in calcat_utils.AgipdConstants: + for constant in AgipdCorrection.AgipdConstants: thread = device.calibration_constant_manager.get_constant_version_and_call_me_back( constant, backcall ) @@ -111,7 +112,7 @@ def test_agipd_constants_and_caching_and_async(): with Stopwatch() as timer_async_warm: threads = [] - for constant in calcat_utils.AgipdConstants: + for constant in AgipdCorrection.AgipdConstants: thread = device.calibration_constant_manager.get_constant_version_and_call_me_back( constant, backcall ) @@ -120,7 +121,7 @@ def test_agipd_constants_and_caching_and_async(): thread.join() with Stopwatch() as timer_sync_warm: - for constant in calcat_utils.AgipdConstants: + for constant in AgipdCorrection.AgipdConstants: data = device.calibration_constant_manager.get_constant_version( constant, ) diff --git a/src/tests/test_dssc_kernels.py b/src/tests/test_dssc_kernels.py index ecd7c0f0c00c54be0b066eb9b24be520bac2aa7e..95c3ec55c580aa216eef871006955109c0a31ee4 100644 --- a/src/tests/test_dssc_kernels.py +++ b/src/tests/test_dssc_kernels.py @@ -1,7 +1,7 @@ import numpy as np import pytest -from calng import dssc_gpu +from calng import DsscCorrection input_dtype = np.uint16 output_dtype = np.float16 @@ -31,7 +31,7 @@ corrected_data = correct_cpu(raw_data, cell_table, offset_map) only_cast_data = np.squeeze(raw_data).astype(output_dtype) -kernel_runner = dssc_gpu.DsscGpuRunner( +kernel_runner = DsscCorrection.DsscGpuRunner( pixels_x, pixels_y, memory_cells, @@ -43,7 +43,7 @@ kernel_runner = dssc_gpu.DsscGpuRunner( def test_only_cast(): kernel_runner.load_data(raw_data) - kernel_runner.correct(dssc_gpu.CorrectionFlags.NONE) + kernel_runner.correct(DsscCorrection.CorrectionFlags.NONE) assert np.allclose( kernel_runner.processed_data_gpu.get(), raw_data.astype(output_dtype) ) @@ -53,7 +53,7 @@ def test_correct(): kernel_runner.load_offset_map(offset_map) kernel_runner.load_data(raw_data) kernel_runner.load_cell_table(cell_table) - kernel_runner.correct(dssc_gpu.CorrectionFlags.OFFSET) + kernel_runner.correct(DsscCorrection.CorrectionFlags.OFFSET) assert np.allclose(kernel_runner.processed_data_gpu.get(), corrected_data) @@ -64,7 +64,7 @@ def test_correct_oob_cells(): wild_cell_table = cell_table * 2 kernel_runner.load_cell_table(wild_cell_table) # should not crash - kernel_runner.correct(dssc_gpu.CorrectionFlags.OFFSET) + kernel_runner.correct(DsscCorrection.CorrectionFlags.OFFSET) # should correct as much as possible assert np.allclose( kernel_runner.processed_data_gpu.get(),