bec-lib 4.7.0__tar.gz → 4.8.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {bec_lib-4.7.0 → bec_lib-4.8.0}/PKG-INFO +1 -1
- {bec_lib-4.7.0 → bec_lib-4.8.0}/bec_lib/scan_data_container.py +153 -1
- {bec_lib-4.7.0 → bec_lib-4.8.0}/bec_lib/scan_history.py +33 -1
- {bec_lib-4.7.0 → bec_lib-4.8.0}/pyproject.toml +1 -1
- bec_lib-4.8.0/tests/test_data_container.py +443 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/tests/test_scan_history.py +61 -1
- bec_lib-4.7.0/tests/test_data_container.py +0 -198
- {bec_lib-4.7.0 → bec_lib-4.8.0}/.gitignore +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/README.md +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/bec_lib/__init__.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/bec_lib/_print_versions.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/bec_lib/acl_login.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/bec_lib/actors.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/bec_lib/alarm_handler.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/bec_lib/atlas_models.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/bec_lib/bec_errors.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/bec_lib/bec_service.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/bec_lib/bec_yaml_loader.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/bec_lib/bl_state_manager.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/bec_lib/bl_states.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/bec_lib/builtin_actor_hli.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/bec_lib/callback_handler.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/bec_lib/channel_monitor.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/bec_lib/client.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/bec_lib/codecs.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/bec_lib/config_helper.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/bec_lib/config_values.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/bec_lib/configs/__init__.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/bec_lib/configs/demo_config.yaml +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/bec_lib/configs/openapi_schema.json +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/bec_lib/connector.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/bec_lib/dap_plugin_objects.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/bec_lib/dap_plugins.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/bec_lib/device.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/bec_lib/devicemanager.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/bec_lib/endpoints.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/bec_lib/file_utils.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/bec_lib/live_scan_data.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/bec_lib/lmfit_serializer.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/bec_lib/logger.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/bec_lib/macro_update_handler.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/bec_lib/messages.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/bec_lib/messaging_hooks.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/bec_lib/messaging_services.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/bec_lib/metadata_schema.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/bec_lib/numpy_encoder.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/bec_lib/observer.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/bec_lib/path_optimization.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/bec_lib/pdf_writer.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/bec_lib/plugin_helper.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/bec_lib/position_generators.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/bec_lib/procedures/__init__.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/bec_lib/procedures/helper.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/bec_lib/procedures/hli.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/bec_lib/procedures/status.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/bec_lib/queue_items.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/bec_lib/redis_connector/__init__.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/bec_lib/redis_connector/constants.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/bec_lib/redis_connector/hli.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/bec_lib/redis_connector/managed_redis_connection.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/bec_lib/redis_connector/streams.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/bec_lib/redis_connector/validation.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/bec_lib/request_context.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/bec_lib/request_items.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/bec_lib/scan_args.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/bec_lib/scan_input_validator.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/bec_lib/scan_items.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/bec_lib/scan_manager.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/bec_lib/scan_number_container.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/bec_lib/scan_preview.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/bec_lib/scan_repeat.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/bec_lib/scan_report.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/bec_lib/scans.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/bec_lib/scibec_validator.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/bec_lib/script_executor.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/bec_lib/serialization.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/bec_lib/serialization_registry.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/bec_lib/service_config.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/bec_lib/signature_serializer.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/bec_lib/tests/__init__.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/bec_lib/tests/fixtures.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/bec_lib/tests/test_config.yaml +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/bec_lib/tests/test_service_config.yaml +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/bec_lib/tests/utils.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/bec_lib/user_macros.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/bec_lib/utils/__init__.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/bec_lib/utils/copier_jinja_filters.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/bec_lib/utils/deprecation.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/bec_lib/utils/import_utils.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/bec_lib/utils/json_extended.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/bec_lib/utils/plugin_manager/__init__.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/bec_lib/utils/plugin_manager/_constants.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/bec_lib/utils/plugin_manager/_util.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/bec_lib/utils/plugin_manager/create/__init__.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/bec_lib/utils/plugin_manager/create/device.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/bec_lib/utils/plugin_manager/create/scan.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/bec_lib/utils/plugin_manager/create/templates/scan.py.jinja +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/bec_lib/utils/plugin_manager/main.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/bec_lib/utils/proxy.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/bec_lib/utils/pydantic_pretty_print.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/bec_lib/utils/rpc_utils.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/bec_lib/utils/scan_utils.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/bec_lib/utils/threading_utils.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/bec_lib/utils/user_acls_test.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/bec_lib/zmq_transports/__init__.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/bec_lib/zmq_transports/adzmq.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/bec_lib/zmq_transports/protocol.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/tests/__init__.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/tests/_additional_for_signature_serializer_test.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/tests/conftest.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/tests/test_acl_login.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/tests/test_alarm_handler.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/tests/test_atlas_models.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/tests/test_beamline_states.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/tests/test_bec_logger.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/tests/test_bec_messages.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/tests/test_bec_service.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/tests/test_callback_handler.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/tests/test_channel_monitor.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/tests/test_client.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/tests/test_config_helper.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/tests/test_core_utils.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/tests/test_dap_plugins.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/tests/test_deprecation.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/tests/test_device_hashing.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/tests/test_device_manager.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/tests/test_devices.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/tests/test_file_utils.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/tests/test_hli_connector_fakeredis.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/tests/test_hli_redis_connector.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/tests/test_import_utils.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/tests/test_jinja_plugins.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/tests/test_lmfit_serializer.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/tests/test_managed_config_values.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/tests/test_managed_redis_connection.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/tests/test_messaging_hooks.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/tests/test_messaging_service.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/tests/test_metadata_schema.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/tests/test_observer.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/tests/test_path_optimization.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/tests/test_pdf_writer.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/tests/test_plugin_helper.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/tests/test_plugin_manager.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/tests/test_plugin_system.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/tests/test_position_generators.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/tests/test_procedure_submission.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/tests/test_pydantic_pretty_print.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/tests/test_queue_items.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/tests/test_redis_connector_fakeredis.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/tests/test_roi_endpoints.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/tests/test_roi_messages.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/tests/test_scan_context.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/tests/test_scan_data.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/tests/test_scan_items.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/tests/test_scan_manager.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/tests/test_scan_object.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/tests/test_scan_preview.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/tests/test_scan_repeat.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/tests/test_scan_report.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/tests/test_scibec_validator.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/tests/test_script_executor.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/tests/test_serializer.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/tests/test_service_config.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/tests/test_signature_serializer.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/tests/test_signature_serializer_with_future_import.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/tests/test_stream_subs.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/tests/test_user_macros.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/tests/test_utils.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/tests/test_yaml_loader.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/tests/test_zmq_transports.py +0 -0
- {bec_lib-4.7.0 → bec_lib-4.8.0}/util_scripts/init_config.py +0 -0
|
@@ -10,12 +10,13 @@ import functools
|
|
|
10
10
|
import importlib
|
|
11
11
|
import time
|
|
12
12
|
from collections import deque, namedtuple
|
|
13
|
-
from collections.abc import Iterable
|
|
13
|
+
from collections.abc import Iterable, Iterator
|
|
14
14
|
from functools import wraps
|
|
15
15
|
from typing import TYPE_CHECKING, Any, Dict, Literal, NamedTuple, Tuple
|
|
16
16
|
|
|
17
17
|
import h5py
|
|
18
18
|
import hdf5plugin # Required to ensure compatibility with HDF5 files using plugins
|
|
19
|
+
import numpy as np
|
|
19
20
|
from _collections_abc import dict_items, dict_keys
|
|
20
21
|
from prettytable import PrettyTable
|
|
21
22
|
|
|
@@ -27,6 +28,53 @@ if TYPE_CHECKING: # pragma: no cover
|
|
|
27
28
|
logger = bec_logger.logger
|
|
28
29
|
|
|
29
30
|
|
|
31
|
+
class SignalSlab(NamedTuple):
|
|
32
|
+
"""
|
|
33
|
+
A block of consecutive rows of a stored signal, as handed out by
|
|
34
|
+
:meth:`SignalDataReference.read_slabs`.
|
|
35
|
+
|
|
36
|
+
``data`` holds the rows ``start:stop`` of the value and of every other dataset of
|
|
37
|
+
the signal that has one entry per row (the timestamps). ``total`` is the number of
|
|
38
|
+
rows of the signal. ``other`` holds, in the first slab only, the datasets of another
|
|
39
|
+
length, whole.
|
|
40
|
+
"""
|
|
41
|
+
|
|
42
|
+
start: int
|
|
43
|
+
stop: int
|
|
44
|
+
total: int
|
|
45
|
+
data: dict
|
|
46
|
+
other: dict = {}
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
class SlabIterator(Iterator[SignalSlab]):
|
|
50
|
+
"""
|
|
51
|
+
The slabs of a stored signal, as returned by :meth:`SignalDataReference.read_slabs`.
|
|
52
|
+
|
|
53
|
+
It is an iterator and a context manager, like the result of :func:`os.scandir`. The
|
|
54
|
+
file is opened for the first slab. It is closed after the last slab, by :meth:`close`,
|
|
55
|
+
and at the end of a ``with`` block.
|
|
56
|
+
"""
|
|
57
|
+
|
|
58
|
+
def __init__(self, slabs: Iterator[SignalSlab]) -> None:
|
|
59
|
+
self._slabs = slabs
|
|
60
|
+
|
|
61
|
+
def __iter__(self) -> SlabIterator:
|
|
62
|
+
return self
|
|
63
|
+
|
|
64
|
+
def __next__(self) -> SignalSlab:
|
|
65
|
+
return next(self._slabs)
|
|
66
|
+
|
|
67
|
+
def close(self) -> None:
|
|
68
|
+
"""Close the file. No further slabs are handed out."""
|
|
69
|
+
self._slabs.close()
|
|
70
|
+
|
|
71
|
+
def __enter__(self) -> SlabIterator:
|
|
72
|
+
return self
|
|
73
|
+
|
|
74
|
+
def __exit__(self, *exc_info) -> None:
|
|
75
|
+
self.close()
|
|
76
|
+
|
|
77
|
+
|
|
30
78
|
class DataCache:
|
|
31
79
|
"""
|
|
32
80
|
Data cache for repeated file reads, implementing a least-recently-used cache.
|
|
@@ -307,6 +355,12 @@ class SignalDataReference:
|
|
|
307
355
|
else:
|
|
308
356
|
self._dict_entry = dict_entry if isinstance(dict_entry, list) else [dict_entry]
|
|
309
357
|
|
|
358
|
+
#: Elements of the value in one slab of :meth:`read_slabs`.
|
|
359
|
+
#: The number was chosen empirically and is not derived from the file: 8 million
|
|
360
|
+
#: elements are 64 MB of float64, which keeps a slab small in memory and large enough
|
|
361
|
+
#: that the time is spent reading and not on the calls per slab.
|
|
362
|
+
_SLAB_ELEMENTS = 8_000_000
|
|
363
|
+
|
|
310
364
|
def read(self) -> dict[Literal["value", "timestamp"], Any]:
|
|
311
365
|
"""
|
|
312
366
|
Read the data from the HDF5 file.
|
|
@@ -316,6 +370,104 @@ class SignalDataReference:
|
|
|
316
370
|
"""
|
|
317
371
|
return self._get_entry()
|
|
318
372
|
|
|
373
|
+
def read_slabs(self) -> SlabIterator:
|
|
374
|
+
"""
|
|
375
|
+
Read the signal block by block along its first axis, without the cache.
|
|
376
|
+
|
|
377
|
+
Each :class:`SignalSlab` holds the next rows of the value, together with the same
|
|
378
|
+
rows of the datasets that have one entry per row. A dataset of another length
|
|
379
|
+
comes whole, in ``other`` of the first slab. Only one slab is in memory at a time,
|
|
380
|
+
so the caller decides how much of a large signal it keeps. A slab has about
|
|
381
|
+
``_SLAB_ELEMENTS`` elements and, for a dataset that is chunked in the file, a
|
|
382
|
+
whole number of its HDF5 chunks.
|
|
383
|
+
|
|
384
|
+
The rows of a ragged signal (a variable-length dataset, as the async writer stores
|
|
385
|
+
a signal whose rows differ in length) do not show their length before they are
|
|
386
|
+
read. Its first slab is a single row. Every later slab holds at most twice the
|
|
387
|
+
rows of the slab before, and at most as many as give ``_SLAB_ELEMENTS`` elements
|
|
388
|
+
at the length of the longest row of that slab. A slab is larger than
|
|
389
|
+
``_SLAB_ELEMENTS`` only by the factor by which its rows are longer than the rows
|
|
390
|
+
of the slab before, and never smaller than one row.
|
|
391
|
+
|
|
392
|
+
The file is open from the first slab until the last one was read. A loop that may
|
|
393
|
+
end earlier should run in a ``with`` block, which closes the file when it is left::
|
|
394
|
+
|
|
395
|
+
with signal.read_slabs() as slabs:
|
|
396
|
+
for slab in slabs:
|
|
397
|
+
if cancelled():
|
|
398
|
+
break
|
|
399
|
+
consume(slab)
|
|
400
|
+
|
|
401
|
+
Returns:
|
|
402
|
+
SlabIterator: Hands out the rows ``start:stop`` of ``total`` as
|
|
403
|
+
:class:`SignalSlab`. A signal without rows gives one slab with
|
|
404
|
+
``start == stop == total == 0``.
|
|
405
|
+
"""
|
|
406
|
+
return SlabIterator(self._iter_slabs())
|
|
407
|
+
|
|
408
|
+
def _iter_slabs(self) -> Iterator[SignalSlab]:
|
|
409
|
+
"""The generator behind :meth:`read_slabs`; the file is open while it is suspended."""
|
|
410
|
+
with h5py.File(self._file_reference.file_path, "r") as f:
|
|
411
|
+
datasets = self._datasets(f)
|
|
412
|
+
value = datasets.get("value")
|
|
413
|
+
total = value.shape[0] if value is not None and value.shape else 0
|
|
414
|
+
if total == 0:
|
|
415
|
+
yield SignalSlab(0, 0, 0, {key: dset[()] for key, dset in datasets.items()})
|
|
416
|
+
return
|
|
417
|
+
per_row = {
|
|
418
|
+
key: dset for key, dset in datasets.items() if dset.shape and dset.shape[0] == total
|
|
419
|
+
}
|
|
420
|
+
ragged = self._is_ragged(value)
|
|
421
|
+
rows_per_slab = 1 if ragged else self._rows_per_slab(value)
|
|
422
|
+
start = 0
|
|
423
|
+
while start < total:
|
|
424
|
+
stop = min(start + rows_per_slab, total)
|
|
425
|
+
data = {key: dset[start:stop] for key, dset in per_row.items()}
|
|
426
|
+
other = {}
|
|
427
|
+
if start == 0:
|
|
428
|
+
other = {key: dset[()] for key, dset in datasets.items() if key not in per_row}
|
|
429
|
+
yield SignalSlab(start, stop, total, data, other)
|
|
430
|
+
if ragged:
|
|
431
|
+
# The rows read so far are all that is known about the rows to come:
|
|
432
|
+
# size the next slab by the longest of them, and let it grow to at
|
|
433
|
+
# most twice the rows, so that one short row does not open the gate.
|
|
434
|
+
longest = max((np.size(row) for row in data["value"]), default=1)
|
|
435
|
+
by_length = self._SLAB_ELEMENTS // max(1, longest)
|
|
436
|
+
rows_per_slab = max(1, min(2 * rows_per_slab, by_length))
|
|
437
|
+
start = stop
|
|
438
|
+
|
|
439
|
+
def _datasets(self, f: h5py.File) -> dict[str, h5py.Dataset]:
|
|
440
|
+
"""The datasets of this signal in the open file ``f``, by name."""
|
|
441
|
+
node = f[self._entry_path]
|
|
442
|
+
for key in self._dict_entry or []:
|
|
443
|
+
node = node[key]
|
|
444
|
+
if isinstance(node, h5py.Dataset):
|
|
445
|
+
return {"value": node}
|
|
446
|
+
return {key: node[key] for key in node.keys() if isinstance(node[key], h5py.Dataset)}
|
|
447
|
+
|
|
448
|
+
@staticmethod
|
|
449
|
+
def _is_ragged(dset: h5py.Dataset) -> bool:
|
|
450
|
+
"""
|
|
451
|
+
Whether the rows of ``dset`` are arrays of variable length. Its shape then says
|
|
452
|
+
nothing about the number of elements in a row. Variable-length strings do not
|
|
453
|
+
count: a string is one small element.
|
|
454
|
+
"""
|
|
455
|
+
base = h5py.check_vlen_dtype(dset.dtype)
|
|
456
|
+
return base is not None and base not in (str, bytes)
|
|
457
|
+
|
|
458
|
+
@classmethod
|
|
459
|
+
def _rows_per_slab(cls, dset: h5py.Dataset) -> int:
|
|
460
|
+
"""
|
|
461
|
+
Rows of ``dset`` in one slab: about ``_SLAB_ELEMENTS`` elements, and a whole
|
|
462
|
+
number of HDF5 chunks when the dataset is chunked, so that no chunk is read twice.
|
|
463
|
+
"""
|
|
464
|
+
row_elements = max(1, int(np.prod(dset.shape[1:], dtype=np.int64)))
|
|
465
|
+
rows = max(1, cls._SLAB_ELEMENTS // row_elements)
|
|
466
|
+
chunk_rows = dset.chunks[0] if dset.chunks else 0
|
|
467
|
+
if chunk_rows:
|
|
468
|
+
rows = max(chunk_rows, rows - rows % chunk_rows)
|
|
469
|
+
return rows
|
|
470
|
+
|
|
319
471
|
def _get_entry(self) -> dict:
|
|
320
472
|
data = self._file_reference.read(self._entry_path, entry_filter=self._dict_entry)
|
|
321
473
|
return data
|
|
@@ -95,11 +95,43 @@ class ScanHistory:
|
|
|
95
95
|
if not os.access(msg.file_path, os.R_OK):
|
|
96
96
|
# If the file is not readable, we skip adding it to the history
|
|
97
97
|
return
|
|
98
|
-
|
|
98
|
+
# Store first: consumers of the SCAN_HISTORY_UPDATE event may read
|
|
99
|
+
# the registry (e.g. get_by_scan_id) from the callback.
|
|
99
100
|
self._scan_data[msg.scan_id] = msg
|
|
100
101
|
self._scan_ids.append(msg.scan_id)
|
|
101
102
|
self._scan_numbers.append(msg.scan_number)
|
|
102
103
|
self._remove_oldest_scan()
|
|
104
|
+
self._client.callbacks.run(event_type=EventType.SCAN_HISTORY_UPDATE, history_msg=msg)
|
|
105
|
+
|
|
106
|
+
def get_message(self, scan_id: str) -> messages.ScanHistoryMessage | None:
|
|
107
|
+
"""
|
|
108
|
+
Return the history message of a scan without opening its file.
|
|
109
|
+
|
|
110
|
+
Lock-free on purpose: the history-update callback holds the registry
|
|
111
|
+
lock while it dispatches, so a reader that also waits on another lock
|
|
112
|
+
must not take it. A single dict read is atomic.
|
|
113
|
+
|
|
114
|
+
Args:
|
|
115
|
+
scan_id (str): Scan ID.
|
|
116
|
+
|
|
117
|
+
Returns:
|
|
118
|
+
messages.ScanHistoryMessage | None: The registry entry, or None.
|
|
119
|
+
"""
|
|
120
|
+
return self._scan_data.get(scan_id)
|
|
121
|
+
|
|
122
|
+
def latest_message(self) -> messages.ScanHistoryMessage | None:
|
|
123
|
+
"""
|
|
124
|
+
Return the history message of the most recent scan without opening its file.
|
|
125
|
+
|
|
126
|
+
Lock-free for the same reason as :meth:`get_message`.
|
|
127
|
+
|
|
128
|
+
Returns:
|
|
129
|
+
messages.ScanHistoryMessage | None: The newest registry entry, or None.
|
|
130
|
+
"""
|
|
131
|
+
try:
|
|
132
|
+
return self._scan_data.get(self._scan_ids[-1])
|
|
133
|
+
except IndexError:
|
|
134
|
+
return None
|
|
103
135
|
|
|
104
136
|
def get_by_scan_id(self, scan_id: str) -> ScanDataContainer | None:
|
|
105
137
|
"""Get the scan data by scan ID."""
|
|
@@ -0,0 +1,443 @@
|
|
|
1
|
+
from unittest import mock
|
|
2
|
+
|
|
3
|
+
import h5py
|
|
4
|
+
import numpy as np
|
|
5
|
+
import pytest
|
|
6
|
+
|
|
7
|
+
from bec_lib.scan_data_container import (
|
|
8
|
+
FileReference,
|
|
9
|
+
ScanDataContainer,
|
|
10
|
+
SignalDataReference,
|
|
11
|
+
SignalSlab,
|
|
12
|
+
_file_cache,
|
|
13
|
+
)
|
|
14
|
+
|
|
15
|
+
# pylint: disable=protected-access
|
|
16
|
+
# pylint: disable=missing-function-docstring
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
@pytest.fixture
|
|
20
|
+
def file_cache():
|
|
21
|
+
_file_cache.clear_cache()
|
|
22
|
+
yield _file_cache
|
|
23
|
+
_file_cache.clear_cache()
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def test_file_cache(mock_file, file_cache):
|
|
27
|
+
"""
|
|
28
|
+
Test that the file access is cached and repeated access does not trigger a reload.
|
|
29
|
+
"""
|
|
30
|
+
reference = FileReference(file_path=mock_file)
|
|
31
|
+
entry_path = "entry/collection/devices/samx/samx/value"
|
|
32
|
+
reference.read(entry_path=entry_path)
|
|
33
|
+
assert file_cache._cache[0][0] == f"{reference.file_path}::{entry_path}"
|
|
34
|
+
|
|
35
|
+
with mock.patch.object(file_cache, "add_item") as add_item:
|
|
36
|
+
reference.read(entry_path=entry_path)
|
|
37
|
+
add_item.assert_not_called()
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def test_file_read_groups(mock_file):
|
|
41
|
+
"""
|
|
42
|
+
Test that the file reference can read groups.
|
|
43
|
+
"""
|
|
44
|
+
reference = FileReference(file_path=mock_file)
|
|
45
|
+
groups = reference.get_hdf5_structure()
|
|
46
|
+
assert groups["entry"]["collection"]["devices"]["samx"]["samx"]["value"] == {
|
|
47
|
+
"type": "dataset",
|
|
48
|
+
"shape": (3,),
|
|
49
|
+
"dtype": int,
|
|
50
|
+
"mem_size": 24,
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def test_data_container(mock_file):
|
|
55
|
+
|
|
56
|
+
container = ScanDataContainer(file_path=mock_file)
|
|
57
|
+
assert "samx" in container.devices.keys()
|
|
58
|
+
assert "samx" in dir(container.devices)
|
|
59
|
+
|
|
60
|
+
assert all(container.devices.samx.read()["samx"]["value"] == np.array([1, 2, 3]))
|
|
61
|
+
assert all(container.devices.samx["samx"].read()["timestamp"] == np.array([1, 2, 3]))
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def test_data_container_raises_without_file():
|
|
65
|
+
with pytest.raises(RuntimeError):
|
|
66
|
+
container = ScanDataContainer(file_path="does_not_exist.h5")
|
|
67
|
+
container._load_devices(timeout_time=0.3)
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
def test_data_container_readout_group_access(mock_file):
|
|
71
|
+
container = ScanDataContainer(file_path=mock_file)
|
|
72
|
+
|
|
73
|
+
assert container.readout_groups.baseline_devices.samz.read()["samz"]["value"] == 1
|
|
74
|
+
assert container.readout_groups.baseline_devices.samz["samz"].read()["timestamp"] == 1
|
|
75
|
+
|
|
76
|
+
assert "samz" in container.readout_groups.baseline_devices.read()
|
|
77
|
+
assert "samx" not in container.readout_groups.baseline_devices.read()
|
|
78
|
+
assert "samx" in container.readout_groups.monitored_devices.read()
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
def test_data_container_read_metadata(mock_file):
|
|
82
|
+
container = ScanDataContainer(file_path=mock_file)
|
|
83
|
+
assert container.metadata.sample_name == "test_sample"
|
|
84
|
+
assert container.metadata.bec["scan_id"] == "scan_id_1"
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
def test_data_container_repr_without_msg(mock_file):
|
|
88
|
+
container = ScanDataContainer(file_path=mock_file)
|
|
89
|
+
assert repr(container) == f"ScanDataContainer: {mock_file}"
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
def test_data_container_repr_with_msg(mock_file, file_history_messages):
|
|
93
|
+
container = ScanDataContainer(file_path=mock_file, msg=file_history_messages[0])
|
|
94
|
+
out = repr(container)
|
|
95
|
+
assert "ScanDataContainer" in out
|
|
96
|
+
assert "Scan number: 1" in out
|
|
97
|
+
assert "Start time" in out
|
|
98
|
+
assert "End time" in out
|
|
99
|
+
assert "Scan ID: scan_id_1" in out
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
def test_data_container_devices_repr(mock_file):
|
|
103
|
+
container = ScanDataContainer(file_path=mock_file)
|
|
104
|
+
assert "samx" in repr(container.devices)
|
|
105
|
+
assert "samz" in repr(container.devices)
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
def test_data_container_readout_groups_repr(mock_file):
|
|
109
|
+
container = ScanDataContainer(file_path=mock_file)
|
|
110
|
+
assert "samz" in repr(container.readout_groups.baseline_devices)
|
|
111
|
+
assert "samx" in repr(container.readout_groups.monitored_devices)
|
|
112
|
+
assert "waveform" in repr(container.readout_groups.async_devices)
|
|
113
|
+
assert "samx" not in repr(container.readout_groups.baseline_devices)
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
def test_data_container_single_device_repr(mock_file):
|
|
117
|
+
container = ScanDataContainer(file_path=mock_file)
|
|
118
|
+
assert "samx" in repr(container.devices.samx)
|
|
119
|
+
assert "samz" not in repr(container.devices.samx)
|
|
120
|
+
assert "(3,)" in repr(container.devices.samx)
|
|
121
|
+
assert "0.00 MB" in repr(container.devices.samx)
|
|
122
|
+
assert "int64" in repr(container.devices.samx)
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
def test_data_container_raises_if_no_file():
|
|
126
|
+
with pytest.raises(ValueError):
|
|
127
|
+
container = ScanDataContainer()
|
|
128
|
+
container.devices.samx.read()
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
def test_data_container_to_pandas(mock_file):
|
|
132
|
+
container = ScanDataContainer(file_path=mock_file)
|
|
133
|
+
df = container.readout_groups.monitored_devices.to_pandas()
|
|
134
|
+
assert df["samx"]["samx"]["value"].tolist() == [1, 2, 3]
|
|
135
|
+
assert df["samx"]["samx"]["timestamp"].tolist() == [1, 2, 3]
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
def test_data_container_get_device(mock_file):
|
|
139
|
+
container = ScanDataContainer(file_path=mock_file)
|
|
140
|
+
assert container.devices.get("samx") == container.devices.samx
|
|
141
|
+
assert container.devices.get("samz") == container.devices.samz
|
|
142
|
+
assert container.devices.get("doesn't exist") is None
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
def test_data_container_get_signal(mock_file):
|
|
146
|
+
container = ScanDataContainer(file_path=mock_file)
|
|
147
|
+
assert container.devices.samx.get("samx") == container.devices.samx["samx"]
|
|
148
|
+
assert container.devices.samx.get("doesn't exist") is None
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
def test_data_container_get_device_from_readout_group(mock_file):
|
|
152
|
+
container = ScanDataContainer(file_path=mock_file)
|
|
153
|
+
assert (
|
|
154
|
+
container.readout_groups.monitored_devices.get("samx")
|
|
155
|
+
== container.readout_groups.monitored_devices.samx
|
|
156
|
+
)
|
|
157
|
+
assert (
|
|
158
|
+
container.readout_groups.monitored_devices.get("samz")
|
|
159
|
+
== container.readout_groups.monitored_devices.samz
|
|
160
|
+
)
|
|
161
|
+
assert container.readout_groups.monitored_devices.get("doesn't exist") is None
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
def test_data_container_get_on_signal_directly(mock_file):
|
|
165
|
+
container = ScanDataContainer(file_path=mock_file)
|
|
166
|
+
assert all(container.devices.samx.samx.get() == container.devices.samx.samx.read()["value"])
|
|
167
|
+
assert all(
|
|
168
|
+
container.readout_groups.monitored_devices.samx.samx.get()
|
|
169
|
+
== container.devices.samx.samx.read()["value"]
|
|
170
|
+
)
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
def test_data_container_get_on_device_directly(mock_file):
|
|
174
|
+
container = ScanDataContainer(file_path=mock_file)
|
|
175
|
+
out = container.devices.samx.get()
|
|
176
|
+
assert all(out.samx == container.devices.samx.samx.get())
|
|
177
|
+
assert all(out.samx_setpoint == container.devices.samx.samx_setpoint.get())
|
|
178
|
+
|
|
179
|
+
out = container.readout_groups.monitored_devices.samx.get()
|
|
180
|
+
assert all(out.samx == container.devices.samx.samx.get())
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+
def test_data_container_read_device_from_data(mock_file):
|
|
184
|
+
container = ScanDataContainer(file_path=mock_file)
|
|
185
|
+
assert all(container.data.samx.read()["value"] == container.devices.samx.samx.read()["value"])
|
|
186
|
+
assert all(
|
|
187
|
+
container.data.samx_setpoint.read()["value"]
|
|
188
|
+
== container.devices.samx.samx_setpoint.read()["value"]
|
|
189
|
+
)
|
|
190
|
+
|
|
191
|
+
|
|
192
|
+
def test_data_container_read_monitored_pandas(mock_file):
|
|
193
|
+
container = ScanDataContainer(file_path=mock_file)
|
|
194
|
+
df = container.readout_groups.monitored_devices.to_pandas()
|
|
195
|
+
assert df["samx"]["samx"]["value"].tolist() == [1, 2, 3]
|
|
196
|
+
assert df["samx"]["samx"]["timestamp"].tolist() == [1, 2, 3]
|
|
197
|
+
assert df["samx"]["samx_setpoint"]["value"].tolist() == [1, 2, 3]
|
|
198
|
+
assert df["samx"]["samx_setpoint"]["timestamp"].tolist() == [1, 2, 3]
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
def test_data_container_read_baseline_pandas(mock_file):
|
|
202
|
+
container = ScanDataContainer(file_path=mock_file)
|
|
203
|
+
df = container.readout_groups.baseline_devices.to_pandas()
|
|
204
|
+
assert df["samz"]["samz"]["value"].tolist() == [1]
|
|
205
|
+
assert df["samz"]["samz"]["timestamp"].tolist() == [1]
|
|
206
|
+
|
|
207
|
+
|
|
208
|
+
def test_signal_read_slabs_of_the_default_size(tmp_path):
|
|
209
|
+
"""A dataset larger than one slab comes in several slabs that add up to the plain read."""
|
|
210
|
+
file_path = tmp_path / "scan.h5"
|
|
211
|
+
values = np.arange(20_000_000, dtype=np.uint16) # > _SLAB_ELEMENTS
|
|
212
|
+
timestamps = np.arange(2_000, dtype=float)
|
|
213
|
+
with h5py.File(file_path, "w") as f:
|
|
214
|
+
group = f.create_group("entry/collection/devices/waveform/waveform_waveform")
|
|
215
|
+
group.create_dataset("value", data=values)
|
|
216
|
+
group.create_dataset("timestamp", data=timestamps)
|
|
217
|
+
reference = SignalDataReference(
|
|
218
|
+
str(file_path), "entry/collection/devices/waveform/waveform_waveform"
|
|
219
|
+
)
|
|
220
|
+
|
|
221
|
+
slabs = list(reference.read_slabs())
|
|
222
|
+
|
|
223
|
+
assert [(slab.start, slab.stop) for slab in slabs] == [
|
|
224
|
+
(0, 8_000_000),
|
|
225
|
+
(8_000_000, 16_000_000),
|
|
226
|
+
(16_000_000, 20_000_000),
|
|
227
|
+
]
|
|
228
|
+
assert all(slab.total == 20_000_000 for slab in slabs)
|
|
229
|
+
np.testing.assert_array_equal(np.concatenate([slab.data["value"] for slab in slabs]), values)
|
|
230
|
+
np.testing.assert_array_equal(slabs[0].other["timestamp"], timestamps)
|
|
231
|
+
np.testing.assert_array_equal(reference.read()["value"], values)
|
|
232
|
+
|
|
233
|
+
|
|
234
|
+
def test_signal_read_slabs_cover_the_signal_in_order(mock_file, monkeypatch):
|
|
235
|
+
reference = SignalDataReference(str(mock_file), "entry/collection/devices/samx/samx")
|
|
236
|
+
monkeypatch.setattr(SignalDataReference, "_SLAB_ELEMENTS", 1)
|
|
237
|
+
|
|
238
|
+
slabs = list(reference.read_slabs())
|
|
239
|
+
|
|
240
|
+
assert all(isinstance(slab, SignalSlab) for slab in slabs)
|
|
241
|
+
assert [(slab.start, slab.stop, slab.total) for slab in slabs] == [
|
|
242
|
+
(0, 1, 3),
|
|
243
|
+
(1, 2, 3),
|
|
244
|
+
(2, 3, 3),
|
|
245
|
+
]
|
|
246
|
+
plain = reference.read()
|
|
247
|
+
for key in ("value", "timestamp"):
|
|
248
|
+
joined = np.concatenate([slab.data[key] for slab in slabs])
|
|
249
|
+
np.testing.assert_array_equal(joined, plain[key])
|
|
250
|
+
|
|
251
|
+
|
|
252
|
+
def test_signal_read_slabs_are_whole_hdf5_chunks(tmp_path, monkeypatch):
|
|
253
|
+
"""Slabs of a chunked dataset end on chunk borders; other lengths come with the first slab."""
|
|
254
|
+
file_path = tmp_path / "scan.h5"
|
|
255
|
+
values = np.arange(1000 * 4, dtype=np.int32).reshape(1000, 4)
|
|
256
|
+
with h5py.File(file_path, "w") as f:
|
|
257
|
+
group = f.create_group("entry/collection/devices/cam/cam_frames")
|
|
258
|
+
group.create_dataset("value", data=values, chunks=(64, 4), maxshape=(None, 4))
|
|
259
|
+
group.create_dataset("timestamp", data=np.arange(7, dtype=float))
|
|
260
|
+
reference = SignalDataReference(str(file_path), "entry/collection/devices/cam/cam_frames")
|
|
261
|
+
monkeypatch.setattr(SignalDataReference, "_SLAB_ELEMENTS", 4 * 100) # 100 rows
|
|
262
|
+
|
|
263
|
+
slabs = list(reference.read_slabs())
|
|
264
|
+
|
|
265
|
+
assert [slab.stop - slab.start for slab in slabs[:-1]] == [64] * (len(slabs) - 1)
|
|
266
|
+
assert slabs[-1].stop == 1000
|
|
267
|
+
np.testing.assert_array_equal(np.concatenate([slab.data["value"] for slab in slabs]), values)
|
|
268
|
+
# the timestamps are not one per row here: they come whole, once, apart from the rows
|
|
269
|
+
assert all(set(slab.data) == {"value"} for slab in slabs)
|
|
270
|
+
assert [set(slab.other) for slab in slabs] == [{"timestamp"}] + [set()] * (len(slabs) - 1)
|
|
271
|
+
np.testing.assert_array_equal(slabs[0].other["timestamp"], np.arange(7, dtype=float))
|
|
272
|
+
|
|
273
|
+
|
|
274
|
+
def test_signal_read_slabs_bound_the_elements_of_ragged_rows(tmp_path, monkeypatch):
|
|
275
|
+
"""
|
|
276
|
+
The async writer stores a signal whose rows differ in length as a variable-length
|
|
277
|
+
dataset of shape (rows,). Its slabs are sized by the elements in the rows, not by the
|
|
278
|
+
number of rows.
|
|
279
|
+
"""
|
|
280
|
+
file_path = tmp_path / "scan.h5"
|
|
281
|
+
rows = [np.full(1000, float(index)) for index in range(10)]
|
|
282
|
+
with h5py.File(file_path, "w") as f:
|
|
283
|
+
group = f.create_group("entry/collection/devices/det/det_wave")
|
|
284
|
+
value = group.create_dataset(
|
|
285
|
+
"value", shape=(10,), maxshape=(None,), dtype=h5py.vlen_dtype(np.dtype("float64"))
|
|
286
|
+
)
|
|
287
|
+
for index, row in enumerate(rows):
|
|
288
|
+
value[index] = row
|
|
289
|
+
group.create_dataset("timestamp", data=np.arange(10, dtype=float), maxshape=(None,))
|
|
290
|
+
reference = SignalDataReference(str(file_path), "entry/collection/devices/det/det_wave")
|
|
291
|
+
monkeypatch.setattr(SignalDataReference, "_SLAB_ELEMENTS", 2000) # two rows
|
|
292
|
+
|
|
293
|
+
slabs = list(reference.read_slabs())
|
|
294
|
+
|
|
295
|
+
# The first slab is one row: nothing is known about the rows before one is read.
|
|
296
|
+
assert [(slab.start, slab.stop) for slab in slabs] == [
|
|
297
|
+
(0, 1),
|
|
298
|
+
(1, 3),
|
|
299
|
+
(3, 5),
|
|
300
|
+
(5, 7),
|
|
301
|
+
(7, 9),
|
|
302
|
+
(9, 10),
|
|
303
|
+
]
|
|
304
|
+
assert all(sum(len(row) for row in slab.data["value"]) <= 2000 for slab in slabs)
|
|
305
|
+
for slab in slabs:
|
|
306
|
+
for index, row in zip(range(slab.start, slab.stop), slab.data["value"]):
|
|
307
|
+
np.testing.assert_array_equal(row, rows[index])
|
|
308
|
+
np.testing.assert_array_equal(
|
|
309
|
+
slab.data["timestamp"], np.arange(slab.start, slab.stop, dtype=float)
|
|
310
|
+
)
|
|
311
|
+
|
|
312
|
+
|
|
313
|
+
def test_signal_read_slabs_follow_the_length_of_ragged_rows(tmp_path, monkeypatch):
|
|
314
|
+
"""Short rows are read many at a time; when the rows get longer the slabs get shorter."""
|
|
315
|
+
file_path = tmp_path / "scan.h5"
|
|
316
|
+
lengths = [10] * 6 + [100] * 6
|
|
317
|
+
with h5py.File(file_path, "w") as f:
|
|
318
|
+
group = f.create_group("entry/collection/devices/det/det_wave")
|
|
319
|
+
value = group.create_dataset(
|
|
320
|
+
"value", shape=(len(lengths),), dtype=h5py.vlen_dtype(np.dtype("int32"))
|
|
321
|
+
)
|
|
322
|
+
for index, length in enumerate(lengths):
|
|
323
|
+
value[index] = np.arange(length, dtype=np.int32)
|
|
324
|
+
reference = SignalDataReference(str(file_path), "entry/collection/devices/det/det_wave")
|
|
325
|
+
monkeypatch.setattr(SignalDataReference, "_SLAB_ELEMENTS", 50)
|
|
326
|
+
|
|
327
|
+
slabs = list(reference.read_slabs())
|
|
328
|
+
|
|
329
|
+
# one row, two rows, four rows; the slab of four meets the first long row, after
|
|
330
|
+
# which every row is a slab of its own
|
|
331
|
+
assert [(slab.start, slab.stop) for slab in slabs] == [
|
|
332
|
+
(0, 1),
|
|
333
|
+
(1, 3),
|
|
334
|
+
(3, 7),
|
|
335
|
+
(7, 8),
|
|
336
|
+
(8, 9),
|
|
337
|
+
(9, 10),
|
|
338
|
+
(10, 11),
|
|
339
|
+
(11, 12),
|
|
340
|
+
]
|
|
341
|
+
assert [len(row) for slab in slabs for row in slab.data["value"]] == lengths
|
|
342
|
+
|
|
343
|
+
|
|
344
|
+
@pytest.mark.parametrize("first_row", [0, 3])
|
|
345
|
+
def test_signal_read_slabs_grow_slowly_after_a_short_ragged_row(tmp_path, monkeypatch, first_row):
|
|
346
|
+
"""
|
|
347
|
+
A short or empty first row says little about the rows after it. The slabs double in
|
|
348
|
+
rows from there, so the long rows that follow are not read in one piece.
|
|
349
|
+
"""
|
|
350
|
+
file_path = tmp_path / "scan.h5"
|
|
351
|
+
lengths = [first_row] + [1000] * 9
|
|
352
|
+
with h5py.File(file_path, "w") as f:
|
|
353
|
+
group = f.create_group("entry/collection/devices/det/det_wave")
|
|
354
|
+
value = group.create_dataset(
|
|
355
|
+
"value", shape=(len(lengths),), dtype=h5py.vlen_dtype(np.dtype("float64"))
|
|
356
|
+
)
|
|
357
|
+
for index, length in enumerate(lengths):
|
|
358
|
+
value[index] = np.full(length, float(index))
|
|
359
|
+
reference = SignalDataReference(str(file_path), "entry/collection/devices/det/det_wave")
|
|
360
|
+
monkeypatch.setattr(SignalDataReference, "_SLAB_ELEMENTS", 2000) # two long rows
|
|
361
|
+
|
|
362
|
+
slabs = list(reference.read_slabs())
|
|
363
|
+
|
|
364
|
+
assert [(slab.start, slab.stop) for slab in slabs] == [
|
|
365
|
+
(0, 1),
|
|
366
|
+
(1, 3),
|
|
367
|
+
(3, 5),
|
|
368
|
+
(5, 7),
|
|
369
|
+
(7, 9),
|
|
370
|
+
(9, 10),
|
|
371
|
+
]
|
|
372
|
+
assert all(sum(len(row) for row in slab.data["value"]) <= 2000 for slab in slabs)
|
|
373
|
+
assert [len(row) for slab in slabs for row in slab.data["value"]] == lengths
|
|
374
|
+
|
|
375
|
+
|
|
376
|
+
def test_signal_read_slabs_count_a_string_as_one_element(tmp_path, monkeypatch):
|
|
377
|
+
file_path = tmp_path / "scan.h5"
|
|
378
|
+
with h5py.File(file_path, "w") as f:
|
|
379
|
+
group = f.create_group("entry/collection/devices/det/det_label")
|
|
380
|
+
group.create_dataset("value", data=[f"label {index}" for index in range(10)])
|
|
381
|
+
reference = SignalDataReference(str(file_path), "entry/collection/devices/det/det_label")
|
|
382
|
+
monkeypatch.setattr(SignalDataReference, "_SLAB_ELEMENTS", 4)
|
|
383
|
+
|
|
384
|
+
slabs = list(reference.read_slabs())
|
|
385
|
+
|
|
386
|
+
assert [(slab.start, slab.stop) for slab in slabs] == [(0, 4), (4, 8), (8, 10)]
|
|
387
|
+
|
|
388
|
+
|
|
389
|
+
def test_signal_read_slabs_of_an_empty_signal(tmp_path):
|
|
390
|
+
file_path = tmp_path / "scan.h5"
|
|
391
|
+
with h5py.File(file_path, "w") as f:
|
|
392
|
+
group = f.create_group("entry/collection/devices/cam/cam_frames")
|
|
393
|
+
group.create_dataset("value", shape=(0, 4), maxshape=(None, 4), dtype=np.int32)
|
|
394
|
+
reference = SignalDataReference(str(file_path), "entry/collection/devices/cam/cam_frames")
|
|
395
|
+
|
|
396
|
+
slabs = list(reference.read_slabs())
|
|
397
|
+
|
|
398
|
+
assert [(slab.start, slab.stop, slab.total) for slab in slabs] == [(0, 0, 0)]
|
|
399
|
+
assert slabs[0].data["value"].shape == (0, 4)
|
|
400
|
+
|
|
401
|
+
|
|
402
|
+
def test_signal_read_slabs_close_the_file_at_the_end_of_a_with_block(mock_file, monkeypatch):
|
|
403
|
+
"""A loop that ends early inside ``with`` leaves no open file behind."""
|
|
404
|
+
reference = SignalDataReference(str(mock_file), "entry/collection/devices/samx/samx")
|
|
405
|
+
monkeypatch.setattr(SignalDataReference, "_SLAB_ELEMENTS", 1)
|
|
406
|
+
|
|
407
|
+
with reference.read_slabs() as slabs:
|
|
408
|
+
for slab in slabs:
|
|
409
|
+
assert slab.start == 0
|
|
410
|
+
break
|
|
411
|
+
# the reader still holds the file: it is suspended between two slabs
|
|
412
|
+
with pytest.raises(OSError):
|
|
413
|
+
h5py.File(mock_file, "r+")
|
|
414
|
+
|
|
415
|
+
with h5py.File(mock_file, "r+"):
|
|
416
|
+
pass
|
|
417
|
+
assert list(slabs) == []
|
|
418
|
+
|
|
419
|
+
|
|
420
|
+
def test_signal_read_slabs_close_the_file_when_the_with_block_raises(mock_file, monkeypatch):
|
|
421
|
+
reference = SignalDataReference(str(mock_file), "entry/collection/devices/samx/samx")
|
|
422
|
+
monkeypatch.setattr(SignalDataReference, "_SLAB_ELEMENTS", 1)
|
|
423
|
+
|
|
424
|
+
with pytest.raises(RuntimeError, match="consumer failed"):
|
|
425
|
+
with reference.read_slabs() as slabs:
|
|
426
|
+
next(slabs)
|
|
427
|
+
raise RuntimeError("consumer failed")
|
|
428
|
+
|
|
429
|
+
with h5py.File(mock_file, "r+"):
|
|
430
|
+
pass
|
|
431
|
+
|
|
432
|
+
|
|
433
|
+
def test_signal_read_slabs_close_the_file_when_abandoned(mock_file, monkeypatch):
|
|
434
|
+
reference = SignalDataReference(str(mock_file), "entry/collection/devices/samx/samx")
|
|
435
|
+
monkeypatch.setattr(SignalDataReference, "_SLAB_ELEMENTS", 1)
|
|
436
|
+
|
|
437
|
+
slabs = reference.read_slabs()
|
|
438
|
+
next(slabs)
|
|
439
|
+
slabs.close()
|
|
440
|
+
|
|
441
|
+
# the file can be opened for writing again, so the reader let go of it
|
|
442
|
+
with h5py.File(mock_file, "r+"):
|
|
443
|
+
pass
|