lima2.client 1.0.9__py3-none-any.whl
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.
- lima2/client/__init__.py +18 -0
- lima2/client/channel.py +30 -0
- lima2/client/cli.py +321 -0
- lima2/client/convert.py +46 -0
- lima2/client/detector.py +722 -0
- lima2/client/devencoded/__init__.py +6 -0
- lima2/client/devencoded/dense_frame.py +154 -0
- lima2/client/devencoded/exception.py +10 -0
- lima2/client/devencoded/sparse_frame.py +87 -0
- lima2/client/devencoded/sparse_frame_with_background.py +118 -0
- lima2/client/devencoded/structured_array.py +81 -0
- lima2/client/pipelines/__init__.py +6 -0
- lima2/client/pipelines/legacy.py +111 -0
- lima2/client/pipelines/smx.py +103 -0
- lima2/client/pipelines/xpcs.py +137 -0
- lima2/client/processing.py +180 -0
- lima2/client/state_machine.py +255 -0
- lima2/server/__init__.py +9 -0
- lima2/server/control.py +182 -0
- lima2/server/receiver.py +149 -0
- lima2/server/timer.py +28 -0
- lima2.client-1.0.9.dist-info/LICENCE +7 -0
- lima2.client-1.0.9.dist-info/METADATA +37 -0
- lima2.client-1.0.9.dist-info/RECORD +27 -0
- lima2.client-1.0.9.dist-info/WHEEL +5 -0
- lima2.client-1.0.9.dist-info/entry_points.txt +2 -0
- lima2.client-1.0.9.dist-info/top_level.txt +1 -0
lima2/client/__init__.py
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
#
|
|
3
|
+
# This file is part of the Lima2 project
|
|
4
|
+
#
|
|
5
|
+
# Copyright (c) 2020-2024 Beamline Control Unit, ESRF
|
|
6
|
+
# Distributed under the MIT licence. See LICENSE for more info.
|
|
7
|
+
|
|
8
|
+
"""Lima2.Client package
|
|
9
|
+
|
|
10
|
+
.. autosummary::
|
|
11
|
+
|
|
12
|
+
detector.Detector
|
|
13
|
+
state_machine.State
|
|
14
|
+
"""
|
|
15
|
+
|
|
16
|
+
from lima2.client.detector import Detector, CommError
|
|
17
|
+
from lima2.client.state_machine import State
|
|
18
|
+
from lima2.client.processing import Processing
|
lima2/client/channel.py
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
#
|
|
3
|
+
# This file is part of the Lima2 project
|
|
4
|
+
#
|
|
5
|
+
# Copyright (c) 2020-2024 Beamline Control Unit, ESRF
|
|
6
|
+
# Distributed under the MIT licence. See LICENSE for more info.
|
|
7
|
+
|
|
8
|
+
import numpy as np
|
|
9
|
+
from abc import ABC, abstractmethod
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class Channel(ABC):
|
|
13
|
+
"""A base class for channel to retrive data from processing"""
|
|
14
|
+
|
|
15
|
+
def __init__(self, name, shape, dtype):
|
|
16
|
+
self._name = name
|
|
17
|
+
self._shape = shape
|
|
18
|
+
self._dtype = dtype
|
|
19
|
+
|
|
20
|
+
@abstractmethod
|
|
21
|
+
def progress(self):
|
|
22
|
+
pass
|
|
23
|
+
|
|
24
|
+
@abstractmethod
|
|
25
|
+
def __get_item__(self, slice):
|
|
26
|
+
pass
|
|
27
|
+
|
|
28
|
+
@abstractmethod
|
|
29
|
+
def __len__(self):
|
|
30
|
+
pass
|
lima2/client/cli.py
ADDED
|
@@ -0,0 +1,321 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
#
|
|
3
|
+
# This file is part of the Lima2 project
|
|
4
|
+
#
|
|
5
|
+
# Copyright (c) 2020-2024 Beamline Control Unit, ESRF
|
|
6
|
+
# Distributed under the MIT licence. See LICENSE for more info.
|
|
7
|
+
|
|
8
|
+
import logging
|
|
9
|
+
import gevent
|
|
10
|
+
import gevent.event
|
|
11
|
+
import json
|
|
12
|
+
import argparse
|
|
13
|
+
from functools import partial
|
|
14
|
+
from contextlib import ExitStack
|
|
15
|
+
from uuid import uuid1
|
|
16
|
+
from tango.gevent import DeviceProxy
|
|
17
|
+
|
|
18
|
+
from lima2.client import Detector, State
|
|
19
|
+
from lima2.client.pipelines.legacy import Processing
|
|
20
|
+
|
|
21
|
+
# Create a logger
|
|
22
|
+
_logger = logging.getLogger(__name__)
|
|
23
|
+
_logger.setLevel(logging.DEBUG)
|
|
24
|
+
|
|
25
|
+
# Enable logging
|
|
26
|
+
logging.basicConfig()
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def get_saving_groups(proc_params):
|
|
30
|
+
return [g for n, g in proc_params.items() if n.startswith("sav")]
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def run_acquisition(device, uuid, params: dict, cb={}):
|
|
34
|
+
if uuid is None:
|
|
35
|
+
uuid = uuid1()
|
|
36
|
+
|
|
37
|
+
with ExitStack() as stack:
|
|
38
|
+
not_running_event = gevent.event.Event()
|
|
39
|
+
idle_event = gevent.event.Event()
|
|
40
|
+
|
|
41
|
+
def state_cb(state):
|
|
42
|
+
_logger.debug(f"State change to {state=}")
|
|
43
|
+
state_cb = cb.get("state", None)
|
|
44
|
+
if state_cb:
|
|
45
|
+
state_cb(state)
|
|
46
|
+
if state in [State.IDLE, State.FAULT]:
|
|
47
|
+
_logger.debug("Setting not_running_event")
|
|
48
|
+
not_running_event.set()
|
|
49
|
+
if state == State.IDLE:
|
|
50
|
+
_logger.debug("Setting not_running_event")
|
|
51
|
+
idle_event.set()
|
|
52
|
+
|
|
53
|
+
device.register_transition_logger(state_cb)
|
|
54
|
+
stack.callback(partial(device.unregister_transition_logger, state_cb))
|
|
55
|
+
|
|
56
|
+
device.prepare_acq(
|
|
57
|
+
uuid, params["ctrl_params"], params["recv_params"], params["proc_params"]
|
|
58
|
+
)
|
|
59
|
+
# stack.callback(partial(device.erase_pipeline, uuid))
|
|
60
|
+
# proc = stack.enter_context(device.get_pipeline(uuid))
|
|
61
|
+
proc = device.get_pipeline(uuid)
|
|
62
|
+
prepare_cb = cb.get("prepare", None)
|
|
63
|
+
if prepare_cb:
|
|
64
|
+
prepare_cb(uuid, proc)
|
|
65
|
+
|
|
66
|
+
end_cb = cb.get("end", None)
|
|
67
|
+
if end_cb:
|
|
68
|
+
stack.callback(end_cb)
|
|
69
|
+
|
|
70
|
+
for e in [not_running_event, idle_event]:
|
|
71
|
+
e.clear()
|
|
72
|
+
|
|
73
|
+
device.start_acq()
|
|
74
|
+
start_cb = cb.get("start", None)
|
|
75
|
+
if start_cb:
|
|
76
|
+
start_cb()
|
|
77
|
+
|
|
78
|
+
def stop_wait_proc():
|
|
79
|
+
# Stop if running
|
|
80
|
+
if device.state == State.RUNNING:
|
|
81
|
+
_logger.info("Stopping running acquisition ...")
|
|
82
|
+
device.stop_acq()
|
|
83
|
+
if device.state == State.CLOSING:
|
|
84
|
+
while not idle_event.ready():
|
|
85
|
+
idle_event.wait(1.0)
|
|
86
|
+
# Signal that acq has stopped
|
|
87
|
+
stop_cb = cb.get("stop", None)
|
|
88
|
+
if stop_cb:
|
|
89
|
+
stop_cb(device.nb_frames_xferred)
|
|
90
|
+
# Wait for the end of the processing
|
|
91
|
+
while not proc.is_finished:
|
|
92
|
+
gevent.sleep(0.1)
|
|
93
|
+
|
|
94
|
+
stack.callback(stop_wait_proc)
|
|
95
|
+
|
|
96
|
+
while not not_running_event.ready():
|
|
97
|
+
xfer_cb = cb.get("xfer", None)
|
|
98
|
+
if xfer_cb:
|
|
99
|
+
xfer_cb(device.nb_frames_xferred)
|
|
100
|
+
not_running_event.wait(1.0)
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
class Acquisition:
|
|
104
|
+
def __init__(self, device, args, params):
|
|
105
|
+
self.device = device
|
|
106
|
+
self.args = args
|
|
107
|
+
|
|
108
|
+
self.acq_params = params["acq_params"]
|
|
109
|
+
self.proc_params = params["proc_params"]
|
|
110
|
+
|
|
111
|
+
self.start_event = gevent.event.Event()
|
|
112
|
+
self.end_event = gevent.event.Event()
|
|
113
|
+
self.xferred_frames = gevent.event.AsyncResult()
|
|
114
|
+
self.uuid = self.args.get("uuid", None)
|
|
115
|
+
self.proc = None
|
|
116
|
+
|
|
117
|
+
def run(self):
|
|
118
|
+
self.show_state("Run")
|
|
119
|
+
|
|
120
|
+
params = {
|
|
121
|
+
"ctrl_params": self.acq_params,
|
|
122
|
+
"recv_params": [self.acq_params for _ in self.device.recvs],
|
|
123
|
+
"proc_params": [self.proc_params for _ in self.device.recvs],
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
cb = dict(
|
|
127
|
+
state=self.state_cb,
|
|
128
|
+
prepare=self.prepare_cb,
|
|
129
|
+
start=self.start_cb,
|
|
130
|
+
xfer=self.xfer_cb,
|
|
131
|
+
stop=self.stop_cb,
|
|
132
|
+
end=self.end_cb,
|
|
133
|
+
)
|
|
134
|
+
|
|
135
|
+
return run_acquisition(self.device, self.uuid, params, cb)
|
|
136
|
+
|
|
137
|
+
def show_state(self, name):
|
|
138
|
+
_logger.info(f'{name + ":":8s} state={self.device.state}')
|
|
139
|
+
|
|
140
|
+
def state_cb(self, state):
|
|
141
|
+
if state is not None:
|
|
142
|
+
_logger.info(f"Monitor: state={state}")
|
|
143
|
+
else:
|
|
144
|
+
_logger.info("\nMonitor finished!")
|
|
145
|
+
|
|
146
|
+
def reading_loop(self, proc):
|
|
147
|
+
self.start_event.wait()
|
|
148
|
+
|
|
149
|
+
roi_counters = []
|
|
150
|
+
|
|
151
|
+
def pop_roi_counters(progress_counters):
|
|
152
|
+
# ROI counter acquisition
|
|
153
|
+
n = min([c["nb_frames_counters"] for c in progress_counters])
|
|
154
|
+
avail_frames = max(0, n - len(roi_counters))
|
|
155
|
+
if avail_frames > 0:
|
|
156
|
+
rc = proc.popRoiCounters(avail_frames)
|
|
157
|
+
if rc:
|
|
158
|
+
roi_counters.append(rc)
|
|
159
|
+
return avail_frames
|
|
160
|
+
|
|
161
|
+
# Read the ROI statistics if enabled
|
|
162
|
+
if self.proc_params["statistics"]["enabled"]:
|
|
163
|
+
while not (self.end_event.ready() or proc.is_finished):
|
|
164
|
+
progress_counters = proc.progress_counters
|
|
165
|
+
_logger.info(f"Progress={progress_counters}")
|
|
166
|
+
if self.proc_params["statistics"]["enabled"]:
|
|
167
|
+
pop_roi_counters(progress_counters)
|
|
168
|
+
gevent.sleep(1)
|
|
169
|
+
progress_counters = proc.progress_counters
|
|
170
|
+
_logger.info(f"Final counters={progress_counters}")
|
|
171
|
+
# Finish ROI counter download
|
|
172
|
+
while pop_roi_counters(progress_counters):
|
|
173
|
+
progress_counters = proc.progress_counters
|
|
174
|
+
_logger.info(f"ROI Statistics={roi_counters}")
|
|
175
|
+
|
|
176
|
+
# nb_frames_xferred = self.xferred_frames.get()
|
|
177
|
+
# while True:
|
|
178
|
+
# try:
|
|
179
|
+
# frame = proc.getFrame(nb_frames_xferred - 1)
|
|
180
|
+
# break
|
|
181
|
+
# except:
|
|
182
|
+
# gevent.sleep(0.1)
|
|
183
|
+
# print(f'Frame={frame}')
|
|
184
|
+
|
|
185
|
+
def prepare_cb(self, uuid, proc):
|
|
186
|
+
self.uuid = uuid
|
|
187
|
+
_logger.info(f"UUID={uuid}")
|
|
188
|
+
self.show_state("Prepare")
|
|
189
|
+
self.proc = gevent.spawn(self.reading_loop, proc)
|
|
190
|
+
|
|
191
|
+
def start_cb(self):
|
|
192
|
+
self.show_state("Start")
|
|
193
|
+
self.start_event.set()
|
|
194
|
+
|
|
195
|
+
def xfer_cb(self, nb_frames_xferred):
|
|
196
|
+
state = self.device.state
|
|
197
|
+
_logger.info(f"Curr. {nb_frames_xferred=} {state=}")
|
|
198
|
+
|
|
199
|
+
# Simulate an asynchronous Stop, if requested
|
|
200
|
+
stop_frames = self.args.get("stop_after_nb_frames", 0)
|
|
201
|
+
if (
|
|
202
|
+
stop_frames
|
|
203
|
+
and nb_frames_xferred >= stop_frames
|
|
204
|
+
and self.device.state == State.RUNNING
|
|
205
|
+
):
|
|
206
|
+
_logger.info("##### STOP #####")
|
|
207
|
+
self.device.stopAcq()
|
|
208
|
+
|
|
209
|
+
def stop_cb(self, nb_frames_xferred):
|
|
210
|
+
self.show_state("Stop")
|
|
211
|
+
_logger.info(f"Total {nb_frames_xferred=}")
|
|
212
|
+
self.xferred_frames.set(nb_frames_xferred)
|
|
213
|
+
|
|
214
|
+
def end_cb(self):
|
|
215
|
+
self.show_state("End")
|
|
216
|
+
self.end_event.set()
|
|
217
|
+
self.proc.join()
|
|
218
|
+
|
|
219
|
+
|
|
220
|
+
def test_acquisition(device, args, params):
|
|
221
|
+
print("*** Running acquisition ***")
|
|
222
|
+
test = Acquisition(device, args.__dict__, params)
|
|
223
|
+
test.run()
|
|
224
|
+
|
|
225
|
+
|
|
226
|
+
def pretty_print_json(json_str, desc=None):
|
|
227
|
+
if desc:
|
|
228
|
+
print(f"{desc}:")
|
|
229
|
+
parsed = json.loads(json_str)
|
|
230
|
+
print(json.dumps(parsed, indent=4, sort_keys=True))
|
|
231
|
+
|
|
232
|
+
|
|
233
|
+
def main():
|
|
234
|
+
parser = argparse.ArgumentParser(description="Lima2 Client test program.")
|
|
235
|
+
parser.add_argument("tango_ctrl_url", nargs=1, help="control Tango device name")
|
|
236
|
+
parser.add_argument(
|
|
237
|
+
"tango_recv_urls", nargs="+", help="receiver Tango device name(s)"
|
|
238
|
+
)
|
|
239
|
+
parser.add_argument("--acq_params", help="JSON string with acq_params")
|
|
240
|
+
parser.add_argument("--uuid", help="Acquisition UUID")
|
|
241
|
+
parser.add_argument(
|
|
242
|
+
"--acq_params_file", help="file containg JSON string with acq_params"
|
|
243
|
+
)
|
|
244
|
+
parser.add_argument("--proc_params", help="JSON string with proc_params")
|
|
245
|
+
parser.add_argument(
|
|
246
|
+
"--proc_params_file", help="file containg JSON string with proc_params"
|
|
247
|
+
)
|
|
248
|
+
parser.add_argument(
|
|
249
|
+
"--stop_after_nb_frames",
|
|
250
|
+
type=int,
|
|
251
|
+
help="Stop after a number of frames have been xferred",
|
|
252
|
+
)
|
|
253
|
+
parser.add_argument(
|
|
254
|
+
"-v",
|
|
255
|
+
"--verbose",
|
|
256
|
+
help="Print extra information useful for debbuging",
|
|
257
|
+
action="store_true",
|
|
258
|
+
)
|
|
259
|
+
|
|
260
|
+
args = parser.parse_args()
|
|
261
|
+
|
|
262
|
+
# Note that nargs=1 produces a list of one item
|
|
263
|
+
tango_ctrl_url = args.tango_ctrl_url[0]
|
|
264
|
+
tango_recv_urls = args.tango_recv_urls
|
|
265
|
+
|
|
266
|
+
tango_ctrl_dev = DeviceProxy(tango_ctrl_url)
|
|
267
|
+
tango_recv_devs = [DeviceProxy(url) for url in tango_recv_urls]
|
|
268
|
+
device = Detector(tango_ctrl_dev, *tango_recv_devs)
|
|
269
|
+
|
|
270
|
+
# Create the user data structures to set detector params
|
|
271
|
+
acq_params = device.params_default[tango_ctrl_url]["acq_params"]
|
|
272
|
+
|
|
273
|
+
proc_params = Processing.params_default
|
|
274
|
+
proc_params["class_name"] = Processing.tango_class
|
|
275
|
+
|
|
276
|
+
# Fill the params with the default params
|
|
277
|
+
params = {"acq_params": acq_params, "proc_params": proc_params}
|
|
278
|
+
|
|
279
|
+
def merge(a: dict, b: dict, path=[]):
|
|
280
|
+
for key in b:
|
|
281
|
+
if key in a:
|
|
282
|
+
if isinstance(a[key], dict) and isinstance(b[key], dict):
|
|
283
|
+
merge(a[key], b[key], path + [str(key)])
|
|
284
|
+
elif a[key] == b[key]:
|
|
285
|
+
pass
|
|
286
|
+
else:
|
|
287
|
+
a[key] = b[key]
|
|
288
|
+
else:
|
|
289
|
+
a[key] = b[key]
|
|
290
|
+
return a
|
|
291
|
+
|
|
292
|
+
# Fill the params either from command line or file
|
|
293
|
+
params_keys = ["acq_params", "proc_params"]
|
|
294
|
+
for key in params_keys:
|
|
295
|
+
val = None
|
|
296
|
+
val_str = getattr(args, key)
|
|
297
|
+
if val_str:
|
|
298
|
+
val = val_str
|
|
299
|
+
fname = getattr(args, f"{key}_file")
|
|
300
|
+
if fname:
|
|
301
|
+
with open(fname) as f:
|
|
302
|
+
val = f.read()
|
|
303
|
+
if val:
|
|
304
|
+
# Merge the default and command line / file args
|
|
305
|
+
merge(params[key], json.loads(val))
|
|
306
|
+
|
|
307
|
+
# Pretty print params
|
|
308
|
+
if args.verbose:
|
|
309
|
+
import pprint
|
|
310
|
+
|
|
311
|
+
pprint.pprint(params)
|
|
312
|
+
|
|
313
|
+
test_acquisition(device, args, params)
|
|
314
|
+
|
|
315
|
+
input("Press Enter to close")
|
|
316
|
+
|
|
317
|
+
|
|
318
|
+
if __name__ == "__main__":
|
|
319
|
+
t = gevent.spawn(main)
|
|
320
|
+
t.join()
|
|
321
|
+
print("Good bye!")
|
lima2/client/convert.py
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
#
|
|
3
|
+
# This file is part of the Lima2 project
|
|
4
|
+
#
|
|
5
|
+
# Copyright (c) 2020-2024 Beamline Control Unit, ESRF
|
|
6
|
+
# Distributed under the MIT licence. See LICENSE for more info.
|
|
7
|
+
|
|
8
|
+
"""Convertion routines.
|
|
9
|
+
|
|
10
|
+
Frame info is serialized as {'dimensions': {'x': 2068, 'y': 512}, 'nb_channels': 2, 'pixel_type': 'gray16'}
|
|
11
|
+
"""
|
|
12
|
+
|
|
13
|
+
import numpy as np
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def pixel_type_to_np_dtype(pixel_type):
|
|
17
|
+
"""convert from pixel enum type to numpy type"""
|
|
18
|
+
if pixel_type == "gray8s":
|
|
19
|
+
return np.int8
|
|
20
|
+
elif pixel_type == "gray8":
|
|
21
|
+
return np.uint8
|
|
22
|
+
elif pixel_type == "gray16s":
|
|
23
|
+
return np.int16
|
|
24
|
+
elif pixel_type == "gray16":
|
|
25
|
+
return np.uint16
|
|
26
|
+
elif pixel_type == "gray32s":
|
|
27
|
+
return np.int32
|
|
28
|
+
elif pixel_type == "gray32":
|
|
29
|
+
return np.uint32
|
|
30
|
+
elif pixel_type == "gray32f":
|
|
31
|
+
return np.float32
|
|
32
|
+
elif pixel_type == "gray64f":
|
|
33
|
+
return np.float64
|
|
34
|
+
else:
|
|
35
|
+
raise RuntimeError(f"Unsupported pixel type '{pixel_type}'")
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def frame_info_to_shape_dtype(frame_info):
|
|
39
|
+
return dict(
|
|
40
|
+
shape=(
|
|
41
|
+
frame_info["nb_channels"],
|
|
42
|
+
frame_info["dimensions"]["y"],
|
|
43
|
+
frame_info["dimensions"]["x"],
|
|
44
|
+
),
|
|
45
|
+
dtype=pixel_type_to_np_dtype(frame_info["pixel_type"]),
|
|
46
|
+
)
|