|
23 | 23 | import os |
24 | 24 | import time |
25 | 25 | import unittest |
| 26 | +from concurrent.futures import Future |
26 | 27 | from concurrent.futures._base import CancelledError |
27 | 28 | from unittest import mock |
28 | 29 |
|
|
33 | 34 | import odemis.acq.stream as stream |
34 | 35 | from odemis import model |
35 | 36 | from odemis.acq import acqmng |
36 | | -from odemis.acq.acqmng import SettingsObserver, acquireZStack |
| 37 | +from odemis.acq.acqmng import SettingsObserver, ZStackAcquisitionTask, acquireZStack |
37 | 38 | from odemis.acq.leech import ProbeCurrentAcquirer |
38 | 39 | from odemis.acq.move import MicroscopePostureManager, FM_IMAGING, SEM_IMAGING, LOADING |
39 | 40 | from odemis.driver import xt_client |
40 | 41 | from odemis.driver.test.xt_client_test import CONFIG_FIB_SEM, CONFIG_FIB_SCANNER, CONFIG_DETECTOR |
41 | 42 | from odemis.util import testing |
42 | 43 | from odemis.util.comp import generate_zlevels |
| 44 | +from odemis.dataio import tiff |
43 | 45 |
|
44 | 46 | logging.getLogger().setLevel(logging.DEBUG) |
45 | 47 |
|
@@ -856,5 +858,153 @@ def test_settings_observer_metadata_with_zstack(self): |
856 | 858 | self.assertEqual(data[0].metadata[model.MD_EXTRA_SETTINGS] |
857 | 859 | ["Camera"]["exposureTime"], [0.023, "s"]) |
858 | 860 |
|
| 861 | + |
| 862 | +def _make_sim_future(result=None): |
| 863 | + """ |
| 864 | + Return a stdlib Future already completed with result. |
| 865 | +
|
| 866 | + :param result: value to store in the future |
| 867 | + :return: completed concurrent.futures.Future |
| 868 | + """ |
| 869 | + f = Future() |
| 870 | + f.set_result(result) |
| 871 | + return f |
| 872 | + |
| 873 | + |
| 874 | +def _make_sim_data_array(shape=(64, 64), dtype=numpy.uint16): |
| 875 | + """ |
| 876 | + Return a minimal 2-D DataArray suitable as a z-level image. |
| 877 | +
|
| 878 | + :param shape: 2-tuple (height, width) |
| 879 | + :param dtype: NumPy dtype for the pixel data |
| 880 | + :return: model.DataArray with pixel-size and position metadata |
| 881 | + """ |
| 882 | + md = { |
| 883 | + model.MD_DIMS: "YX", |
| 884 | + model.MD_PIXEL_SIZE: (1e-7, 1e-7), |
| 885 | + model.MD_POS: (0.0, 0.0), |
| 886 | + } |
| 887 | + return model.DataArray(numpy.zeros(shape, dtype=dtype), md) |
| 888 | + |
| 889 | + |
| 890 | +def _make_sim_stream(name="mock_stream"): |
| 891 | + """ |
| 892 | + Build a MagicMock that satisfies the interface used by ZStackAcquisitionTask. |
| 893 | +
|
| 894 | + :param name: human-readable name for the stream mock |
| 895 | + :return: unittest.mock.MagicMock mimicking a Stream |
| 896 | + """ |
| 897 | + s = mock.MagicMock() |
| 898 | + s.name.value = name |
| 899 | + s.estimateAcquisitionTime.return_value = 0.0 |
| 900 | + s.focuser.moveAbs.return_value = _make_sim_future(None) |
| 901 | + return s |
| 902 | + |
| 903 | + |
| 904 | +def _make_sim_task(stream_mock, zlevels): |
| 905 | + """ |
| 906 | + Construct a ZStackAcquisitionTask with a mock ProgressiveFuture. |
| 907 | +
|
| 908 | + Both guessActuatorMoveDuration (called in __init__) and |
| 909 | + estimate_total_duration (called inside run()) are patched to avoid |
| 910 | + the need for real actuator hardware. |
| 911 | +
|
| 912 | + :param stream_mock: mock Stream object |
| 913 | + :param zlevels: dict mapping stream_mock to list of z positions |
| 914 | + :return: (task, mock_future) tuple ready to call task.run() on |
| 915 | + """ |
| 916 | + future = mock.MagicMock() |
| 917 | + with mock.patch("odemis.acq.acqmng.guessActuatorMoveDuration", return_value=0.0): |
| 918 | + task = ZStackAcquisitionTask(future, [stream_mock], zlevels, settings_obs=None) |
| 919 | + task.estimate_total_duration = mock.MagicMock(return_value=1.0) |
| 920 | + return task, future |
| 921 | + |
| 922 | + |
| 923 | +class TestZStackPartialFailureSim(unittest.TestCase): |
| 924 | + """ |
| 925 | + Simulation tests (no hardware) for the fix that saves partial z-stack data |
| 926 | + when a camera error occurs during an acquisition. |
| 927 | +
|
| 928 | + Root cause of the original bug: a camera communication error could return |
| 929 | + an image with the wrong shape. On NumPy < 1.24, numpy.array() on |
| 930 | + mixed-shape arrays silently produces an object-dtype array, which cannot |
| 931 | + be written to TIFF (WriteDirectory() → AssertionError: 0). |
| 932 | +
|
| 933 | + Shape validation in assembleZCube() and ZStackAcquisitionTask.run(), |
| 934 | + plus partial z-stack assembly is implemented instead of discarding data on failure. |
| 935 | + """ |
| 936 | + |
| 937 | + def test_full_success_returns_zcube(self): |
| 938 | + """ |
| 939 | + When all z-levels succeed, run() returns a single ZYX DataArray and no exception. |
| 940 | + """ |
| 941 | + n = 3 |
| 942 | + zlevels_list = [i * 1e-6 for i in range(n)] |
| 943 | + s = _make_sim_stream("fluo") |
| 944 | + task, _ = _make_sim_task(s, {s: zlevels_list}) |
| 945 | + |
| 946 | + good_img = _make_sim_data_array((64, 64)) |
| 947 | + acq_futures = [_make_sim_future(([good_img], None)) for _ in range(n)] |
| 948 | + |
| 949 | + with mock.patch("odemis.acq.acqmng.acquire", side_effect=acq_futures): |
| 950 | + data, exp = task.run() |
| 951 | + |
| 952 | + self.assertIsNone(exp) |
| 953 | + self.assertEqual(len(data), 1) |
| 954 | + self.assertEqual(data[0].shape, (n, 64, 64)) |
| 955 | + self.assertNotEqual(data[0].dtype, object) |
| 956 | + |
| 957 | + def test_wrong_shape_mid_zstack_saves_partial(self): |
| 958 | + """ |
| 959 | + When a z-level image has a wrong spatial shape (truncated camera read), |
| 960 | + run() must stop, assemble only the valid z-levels, and report the error. |
| 961 | +
|
| 962 | + This is the primary regression test for the TIFF-crash bug. |
| 963 | + """ |
| 964 | + zlevels_list = [0.0e-6, 1.0e-6, 2.0e-6] |
| 965 | + s = _make_sim_stream("fluo") |
| 966 | + task, _ = _make_sim_task(s, {s: zlevels_list}) |
| 967 | + |
| 968 | + good_img = _make_sim_data_array((64, 64)) |
| 969 | + bad_img = _make_sim_data_array((32, 64)) # truncated height — simulates camera error |
| 970 | + |
| 971 | + acq_futures = [ |
| 972 | + _make_sim_future(([good_img], None)), |
| 973 | + _make_sim_future(([good_img], None)), |
| 974 | + _make_sim_future(([bad_img], None)), # 3rd level: wrong shape |
| 975 | + ] |
| 976 | + |
| 977 | + with mock.patch("odemis.acq.acqmng.acquire", side_effect=acq_futures): |
| 978 | + data, exp = task.run() |
| 979 | + |
| 980 | + # Partial data must be saved |
| 981 | + self.assertEqual(len(data), 1) |
| 982 | + zcube = data[0] |
| 983 | + # Must NOT be an object-dtype array (the original bug) |
| 984 | + self.assertNotEqual(zcube.dtype, object) |
| 985 | + # Only the 2 valid levels are included |
| 986 | + self.assertEqual(zcube.shape, (2, 64, 64)) |
| 987 | + # Error must be reported |
| 988 | + self.assertIsNotNone(exp) |
| 989 | + |
| 990 | + def test_first_zlevel_fails_returns_empty_data(self): |
| 991 | + """ |
| 992 | + When the very first z-level fails, no z-cube can be assembled. |
| 993 | + run() must return an empty data list and the exception. |
| 994 | + """ |
| 995 | + zlevels_list = [0.0e-6, 1.0e-6] |
| 996 | + s = _make_sim_stream("fluo") |
| 997 | + task, _ = _make_sim_task(s, {s: zlevels_list}) |
| 998 | + |
| 999 | + hw_error = IOError("Camera connection lost") |
| 1000 | + |
| 1001 | + with mock.patch("odemis.acq.acqmng.acquire", |
| 1002 | + return_value=_make_sim_future(([], hw_error))): |
| 1003 | + data, exp = task.run() |
| 1004 | + |
| 1005 | + self.assertEqual(len(data), 0) |
| 1006 | + self.assertIs(exp, hw_error) |
| 1007 | + |
| 1008 | + |
859 | 1009 | if __name__ == "__main__": |
860 | 1010 | unittest.main() |
0 commit comments