Add data access helpers
We need an API that looks the same for online and offline data analysis of Bliss data.
Proposal: streamline API on top of Bliss or HDF5
from numbers import Integral, Number
from typing import Callable, Dict, Iterable, Optional, Sequence
def get_reader(url) -> "DataReader":
pass
class DataReader:
def __init__(
self,
point_multipliers: Optional[Dict[str, Integral]],
time_multipliers: Optional[Dict[str, Integral]]
) -> None:
"""Some channels produce 2*n, 3*n, ... points"""
self._point_multipliers = point_multipliers
self._time_multipliers = time_multipliers
def iter_data(
self,
period: Optional[Number] = None,
npoints: Optional[Integral] = None,
channel_filter: Optional[Callable[[str], bool]] = None,
) -> Iterable[Dict[str, Sequence]]:
"""Yield data every `period` seconds or every `npoints` points.
Returns a dictionary with a sequence of data points per channel.
"""
raise NotImplementedError
def get_channel_metadata(
self, channel_filter: Optional[Callable[[str], bool]]
) -> Dict:
"""Return metadata of the channels (e.g. detector pixel size)"""
raise NotImplementedError
def get_metadata(self) -> Dict:
"""Return non-channel metadata (e.g. SR current)"""
raise NotImplementedError
class BlissReader(DataReader):
"""Data comes from Redis, Lima server or HDF5 files"""
def __init__(self, url: str, **kwargs) -> None:
"""For example
url="redis://localhost:25002/1?key=demo_session:tmp:scans:inhouse:id002205:sample:sample_0001:1_loopscan"
"""
super().__init__(**kwargs)
class HDF5Reader(DataReader):
"""Data comes from the HDF5 file"""
def __init__(self, url: str, **kwargs) -> None:
"""For example
url="hdf5:///tmp/scans/inhouse/id002205/id00/20220501/sample/sample_0001/sample_0001.h5"
"""
super().__init__(**kwargs)
Usage:
reader = get_reader("redis://localhost:25002/1?key=demo_session:tmp:scans:inhouse:id002205:sample:sample_0001:1_loopscan")
for data in reader.iter_data(period=5):
pass
Edited by Wout De Nolf