mosca 2.0.2__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.
- mosca/__init__.py +11 -0
- mosca/common/__init__.py +9 -0
- mosca/common/enums.py +79 -0
- mosca/common/logger.py +139 -0
- mosca/common/threads.py +199 -0
- mosca/common/utils.py +70 -0
- mosca/control/__init__.py +4 -0
- mosca/control/buffer.py +526 -0
- mosca/control/controller.py +278 -0
- mosca/control/datahandler.py +261 -0
- mosca/control/detectors.py +116 -0
- mosca/control/mosca.py +693 -0
- mosca/control/spectra.py +36 -0
- mosca/control/stats.py +43 -0
- mosca/control/tasks/__init__.py +3 -0
- mosca/control/tasks/background.py +118 -0
- mosca/control/tasks/rois.py +466 -0
- mosca/control/tasks/saving.py +391 -0
- mosca/control/tasks/spectrasums.py +185 -0
- mosca/control/tasks/task.py +201 -0
- mosca/control/tasks/taskmanager.py +133 -0
- mosca/devices/__init__.py +28 -0
- mosca/devices/hamamatsu/__init__.py +0 -0
- mosca/devices/hamamatsu/hamamatsu.py +88 -0
- mosca/devices/hamamatsu/hamamatsuhw.py +1298 -0
- mosca/devices/oceanoptics/__init__.py +0 -0
- mosca/devices/oceanoptics/oceanoptics.py +59 -0
- mosca/devices/oceanoptics/oceanopticshw.py +379 -0
- mosca/devices/simulation/__init__.py +0 -0
- mosca/devices/simulation/dante/__init__.py +1 -0
- mosca/devices/simulation/dante/simdante.py +33 -0
- mosca/devices/simulation/dante/simdantehw.py +506 -0
- mosca/devices/simulation/dummy/__init__.py +1 -0
- mosca/devices/simulation/dummy/dummy.py +299 -0
- mosca/devices/simulation/generator.py +607 -0
- mosca/devices/simulation/xia/__init__.py +1 -0
- mosca/devices/simulation/xia/iniparser.py +99 -0
- mosca/devices/simulation/xia/simfalconx.py +26 -0
- mosca/devices/simulation/xia/simfalconxhw.py +599 -0
- mosca/devices/xglab/__init__.py +0 -0
- mosca/devices/xglab/dante.py +126 -0
- mosca/devices/xglab/dantehw.py +1178 -0
- mosca/devices/xia/__init__.py +0 -0
- mosca/devices/xia/falconx.py +232 -0
- mosca/devices/xia/handel/__init__.py +14 -0
- mosca/devices/xia/handel/_cffi.py +62 -0
- mosca/devices/xia/handel/error.py +272 -0
- mosca/devices/xia/handel/handel_inc/handel_constants.h +127 -0
- mosca/devices/xia/handel/handel_inc/handel_errors.h +254 -0
- mosca/devices/xia/handel/handel_inc/handel_generic.h +66 -0
- mosca/devices/xia/handel/interface.py +1025 -0
- mosca/devices/xia/handel/mapping.py +107 -0
- mosca/devices/xia/handel/parser.py +62 -0
- mosca/devices/xia/handel/stats.py +85 -0
- mosca/devices/xia/mercury.py +160 -0
- mosca/devices/xia/utils.py +47 -0
- mosca/devices/xia/xia.py +575 -0
- mosca/devices/xia/xiads.py +161 -0
- mosca/mosca.py +0 -0
- mosca/tango/__init__.py +2 -0
- mosca/tango/controller.py +80 -0
- mosca/tango/main.py +163 -0
- mosca/tango/mosca.py +513 -0
- mosca/tango/moscav1.py +408 -0
- mosca/tango/shared.py +23 -0
- mosca/tango/tasks/__init__.py +0 -0
- mosca/tango/tasks/background.py +55 -0
- mosca/tango/utils.py +109 -0
- mosca-2.0.2.dist-info/METADATA +32 -0
- mosca-2.0.2.dist-info/RECORD +73 -0
- mosca-2.0.2.dist-info/WHEEL +4 -0
- mosca-2.0.2.dist-info/entry_points.txt +2 -0
- mosca-2.0.2.dist-info/licenses/LICENSE +166 -0
|
@@ -0,0 +1,278 @@
|
|
|
1
|
+
import typing
|
|
2
|
+
import numpy as np
|
|
3
|
+
|
|
4
|
+
from ..common import to_enum, Status, TriggerMode, GateMode, Callback
|
|
5
|
+
|
|
6
|
+
import logging
|
|
7
|
+
logger = logging.getLogger(__name__)
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class mca_property(property):
|
|
12
|
+
def __set__(self, obj, *args):
|
|
13
|
+
if obj.mca_status() in (Status.RUNNING, Status.UNINITIALIZED, Status.INITIALIZING):
|
|
14
|
+
raise RuntimeError(f"Can't set parameters while MCA state is {obj.mca_status().name}.")
|
|
15
|
+
super().__set__(obj, *args)
|
|
16
|
+
|
|
17
|
+
def __get__(self, obj, *args):
|
|
18
|
+
status = obj.mca_status()
|
|
19
|
+
if status in (Status.UNINITIALIZED, Status.INITIALIZING, Status.FAULT):
|
|
20
|
+
raise RuntimeError(f"Can't get parameters while MCA state is {status.name}.")
|
|
21
|
+
return super().__get__(obj, *args)
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def ready_locked(func):
|
|
25
|
+
def _inner(self, *args, **kwargs):
|
|
26
|
+
mca_status = self.mca_status()
|
|
27
|
+
if mca_status != Status.READY:
|
|
28
|
+
raise RuntimeError(f"Can't call {func.__name__} while MCA state is {mca_status.name}.")
|
|
29
|
+
return func(self, *args, **kwargs)
|
|
30
|
+
return _inner
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
class PollResult:
|
|
34
|
+
""" Class containing data and acquisition status
|
|
35
|
+
of an acquisition when calling `Controller.poll_acq`
|
|
36
|
+
"""
|
|
37
|
+
def __init__(self,
|
|
38
|
+
finished: bool = False,
|
|
39
|
+
n_read: typing.Optional[int] = 0,
|
|
40
|
+
spectra: typing.Optional[np.ndarray] = None,
|
|
41
|
+
stats: typing.Optional[np.ndarray] = None,
|
|
42
|
+
total_triggered: typing.Optional[int] = None):
|
|
43
|
+
"""
|
|
44
|
+
Args:
|
|
45
|
+
finished: True if the acquisiton is finished (i.e: detector stopped)
|
|
46
|
+
n_read: number of data points stored in spectra and/or stats, in case the spectra / stats buffers are larger than the number of read pixels
|
|
47
|
+
spectra: numpy array of shape (n_detectors, n_points, spectrum_size), or None if there is not available data, with n_points >= n_read.
|
|
48
|
+
stats: numpy array of shape (n_detectors, n_points, stats_size), or None if there is not available data, with n_points >= n_read.
|
|
49
|
+
total_triggered: optional, total number of data points read by the detector since the beginning of the acquisition. Some controllers allow waiting
|
|
50
|
+
for a certain number of points to be acquired on the hardware before requesting a download.
|
|
51
|
+
|
|
52
|
+
"""
|
|
53
|
+
self.finished = finished
|
|
54
|
+
self.n_read = n_read
|
|
55
|
+
self.spectra = spectra
|
|
56
|
+
self.stats = stats
|
|
57
|
+
self.total_triggered = total_triggered
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
class Controller:
|
|
61
|
+
"""Base class for hardware controller.
|
|
62
|
+
"""
|
|
63
|
+
name = property(lambda self: self._name)
|
|
64
|
+
|
|
65
|
+
def __init__(self, name=None, on_changed=None):
|
|
66
|
+
if name is None:
|
|
67
|
+
name = f"{self.__class__.__name__}"
|
|
68
|
+
self._name = name
|
|
69
|
+
super().__init__()
|
|
70
|
+
self._callback = Callback(on_changed)
|
|
71
|
+
|
|
72
|
+
def unconfig(self):
|
|
73
|
+
raise NotImplementedError
|
|
74
|
+
|
|
75
|
+
@property
|
|
76
|
+
def callback(self):
|
|
77
|
+
return self._callback
|
|
78
|
+
|
|
79
|
+
def changed(self):
|
|
80
|
+
self._callback()
|
|
81
|
+
|
|
82
|
+
def is_running(self) -> bool:
|
|
83
|
+
return self.mca_status() == Status.RUNNING
|
|
84
|
+
|
|
85
|
+
def unprepare(self):
|
|
86
|
+
pass
|
|
87
|
+
|
|
88
|
+
def metadata(self):
|
|
89
|
+
return {}
|
|
90
|
+
|
|
91
|
+
def mca_status(self) -> Status:
|
|
92
|
+
raise NotImplementedError
|
|
93
|
+
|
|
94
|
+
def apply_mosca(self,
|
|
95
|
+
acq_nb_points=None,
|
|
96
|
+
acq_realtime=None,
|
|
97
|
+
acq_nb_triggers=None,
|
|
98
|
+
spectrum_size=None,
|
|
99
|
+
trigger_mode=None,
|
|
100
|
+
gate_mode=None,
|
|
101
|
+
start_trigger=None,
|
|
102
|
+
stop_trigger=None,
|
|
103
|
+
block_size=None,
|
|
104
|
+
resume=False):
|
|
105
|
+
"""
|
|
106
|
+
Pass Mosca parameters to the controller. It's up to the controller
|
|
107
|
+
to decide what to do with those parameters and how to convert them
|
|
108
|
+
to actual hardware settings.
|
|
109
|
+
"""
|
|
110
|
+
raise NotImplementedError
|
|
111
|
+
|
|
112
|
+
def prepare_acq(self, resume=False):
|
|
113
|
+
"""Signals that an acquisition is about to be started, and that the controller
|
|
114
|
+
should be ready (e.g: apply settings).
|
|
115
|
+
|
|
116
|
+
Args:
|
|
117
|
+
resume (bool): not used at the moment.
|
|
118
|
+
|
|
119
|
+
Note:
|
|
120
|
+
after a call to that `prepare_acq` everything should be set up so that
|
|
121
|
+
subsequent calls to `available_detectors` and `get_stats_name` return values
|
|
122
|
+
consistent with the applied settings.
|
|
123
|
+
"""
|
|
124
|
+
raise NotImplementedError
|
|
125
|
+
|
|
126
|
+
def start_acq(self, resume=None):
|
|
127
|
+
""" Starts the acquisitions.
|
|
128
|
+
|
|
129
|
+
Args:
|
|
130
|
+
resume (bool): not used at the moment.
|
|
131
|
+
"""
|
|
132
|
+
raise NotImplementedError
|
|
133
|
+
|
|
134
|
+
def stop_acq(self):
|
|
135
|
+
""" Stops the acquisitions. """
|
|
136
|
+
raise NotImplementedError
|
|
137
|
+
|
|
138
|
+
def abort_acq(self):
|
|
139
|
+
""" Aborts the acquisitions.
|
|
140
|
+
|
|
141
|
+
Note:
|
|
142
|
+
optional
|
|
143
|
+
"""
|
|
144
|
+
raise NotImplementedError
|
|
145
|
+
|
|
146
|
+
def poll_acq(self) -> PollResult:
|
|
147
|
+
""" After an acquisition is started, this method can be called to poll the
|
|
148
|
+
controller and retrieve data. See the documentation for the `PollResult`
|
|
149
|
+
class.
|
|
150
|
+
|
|
151
|
+
Returns:
|
|
152
|
+
an instance of PollResult.
|
|
153
|
+
"""
|
|
154
|
+
raise NotImplementedError
|
|
155
|
+
|
|
156
|
+
def enabled_detectors(self) -> list[int]:
|
|
157
|
+
""" Returns detectors enabled by the hardware.
|
|
158
|
+
|
|
159
|
+
Default implementation raises NotImplemented, in which
|
|
160
|
+
case called can assume that disabling detectors at the hardware
|
|
161
|
+
level is not supported.
|
|
162
|
+
|
|
163
|
+
Returns:
|
|
164
|
+
list of integers.
|
|
165
|
+
"""
|
|
166
|
+
raise NotImplementedError
|
|
167
|
+
|
|
168
|
+
def disabled_detectors(self) -> list[int]:
|
|
169
|
+
""" Returns detectors disabled by the hardware.
|
|
170
|
+
|
|
171
|
+
Default implementation just returns the difference
|
|
172
|
+
between available_detectors and enabled_detectors.
|
|
173
|
+
|
|
174
|
+
Returns:
|
|
175
|
+
list of integers.
|
|
176
|
+
"""
|
|
177
|
+
enab = self.enabled_detectors()
|
|
178
|
+
avail = self.available_detectors()
|
|
179
|
+
return list(set(avail) - set(enab))
|
|
180
|
+
|
|
181
|
+
def enable_detector(self, detector, enable=True):
|
|
182
|
+
""" Enables the given detector.
|
|
183
|
+
|
|
184
|
+
Default implementation raises NotImplemented, in which
|
|
185
|
+
case called can assume that enabling/disabling detectors at the hardware
|
|
186
|
+
level is not supported.
|
|
187
|
+
|
|
188
|
+
Returns:
|
|
189
|
+
list of integers.
|
|
190
|
+
"""
|
|
191
|
+
raise NotImplementedError
|
|
192
|
+
|
|
193
|
+
def disable_detector(self, detector):
|
|
194
|
+
""" Disables the given detector.
|
|
195
|
+
|
|
196
|
+
Default implementation calls enable_detector(detector, enable=False).
|
|
197
|
+
|
|
198
|
+
Returns:
|
|
199
|
+
list of integers.
|
|
200
|
+
"""
|
|
201
|
+
self.enable_detector(detector, enable=False)
|
|
202
|
+
|
|
203
|
+
def available_detectors(self) -> list[int]:
|
|
204
|
+
""" Returns all the available detector numbers (i.e: aliases),
|
|
205
|
+
whether enabled or not. The detector order should be coherent with
|
|
206
|
+
the acquired data.
|
|
207
|
+
|
|
208
|
+
Returns:
|
|
209
|
+
list of integers.
|
|
210
|
+
"""
|
|
211
|
+
raise NotImplementedError
|
|
212
|
+
|
|
213
|
+
def spectrum_size(self):
|
|
214
|
+
raise NotImplementedError
|
|
215
|
+
|
|
216
|
+
def get_stats_names(self) -> list[str]:
|
|
217
|
+
""" Used to get the names of statistics returned during the acquisition.
|
|
218
|
+
|
|
219
|
+
Returns:
|
|
220
|
+
names of statistics returned by the polling.
|
|
221
|
+
"""
|
|
222
|
+
return []
|
|
223
|
+
|
|
224
|
+
def get_supported_spectrum_sizes(self) -> list[int]:
|
|
225
|
+
""" Supported spectrum sizes.
|
|
226
|
+
|
|
227
|
+
Returns:
|
|
228
|
+
a list of supported spectrum sizes
|
|
229
|
+
"""
|
|
230
|
+
raise NotImplementedError
|
|
231
|
+
|
|
232
|
+
def get_supported_trigger_modes(self) -> list[TriggerMode]:
|
|
233
|
+
""" Supported trigger modes.
|
|
234
|
+
|
|
235
|
+
Returns:
|
|
236
|
+
a list of supported trigger modes.
|
|
237
|
+
"""
|
|
238
|
+
raise NotImplementedError
|
|
239
|
+
|
|
240
|
+
def get_supported_gate_modes(self) -> list[GateMode]:
|
|
241
|
+
""" Supported gate modes.
|
|
242
|
+
|
|
243
|
+
Returns:
|
|
244
|
+
a list of supported gate modes.
|
|
245
|
+
"""
|
|
246
|
+
raise NotImplementedError
|
|
247
|
+
|
|
248
|
+
def timeout_start(self) -> float:
|
|
249
|
+
""" Maximum expected time when calling start_acq.
|
|
250
|
+
|
|
251
|
+
Returns:
|
|
252
|
+
timeout in s, default 5s.
|
|
253
|
+
"""
|
|
254
|
+
return 5.
|
|
255
|
+
|
|
256
|
+
def timeout_stop(self) -> float:
|
|
257
|
+
""" Maximum expected time when calling stop_acq.
|
|
258
|
+
|
|
259
|
+
Returns:
|
|
260
|
+
timeout in s, default 5s.
|
|
261
|
+
"""
|
|
262
|
+
return 5.
|
|
263
|
+
|
|
264
|
+
def timeout_setup(self) -> float:
|
|
265
|
+
""" Maximum expected time when calling setup_acq.
|
|
266
|
+
|
|
267
|
+
Returns:
|
|
268
|
+
timeout in s, default 5s.
|
|
269
|
+
"""
|
|
270
|
+
return 5.
|
|
271
|
+
|
|
272
|
+
def timeout_prepare(self) -> float:
|
|
273
|
+
""" Maximum expected time when calling prepare_acq.
|
|
274
|
+
|
|
275
|
+
Returns:
|
|
276
|
+
timeout in s, default 5s.
|
|
277
|
+
"""
|
|
278
|
+
return 5.
|
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
import logging
|
|
2
|
+
logger = logging.getLogger(__name__)
|
|
3
|
+
|
|
4
|
+
import numpy as np
|
|
5
|
+
import copy
|
|
6
|
+
|
|
7
|
+
from collections import deque
|
|
8
|
+
|
|
9
|
+
from ..common import AsyncTask
|
|
10
|
+
from .buffer import DEFAULT_BUFFER_SIZE
|
|
11
|
+
from .spectra import Spectra
|
|
12
|
+
from .stats import Stats
|
|
13
|
+
from .tasks.taskmanager import TaskManager
|
|
14
|
+
from .detectors import Detectors
|
|
15
|
+
from ..common.utils import Blocker
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
# FUGLY
|
|
19
|
+
SPECTRA_DTYPE = np.uint32
|
|
20
|
+
SPECTRASUM_DTYPE = np.uint32
|
|
21
|
+
STATS_DTYPE = np.float64
|
|
22
|
+
ROIS_DTYPE = np.int64
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
# todo: LOCK PARAMETERS ON acq ruNNING
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
class DataHandler:
|
|
29
|
+
"""
|
|
30
|
+
Class acting as a link between the spectrometer, buffers and saving manager.
|
|
31
|
+
Methods that need to be called to
|
|
32
|
+
"""
|
|
33
|
+
|
|
34
|
+
spectra = property(lambda self: self._spectra)
|
|
35
|
+
""" Spectra """
|
|
36
|
+
|
|
37
|
+
stats = property(lambda self: self._stats)
|
|
38
|
+
""" Statistics """
|
|
39
|
+
|
|
40
|
+
rois = property(lambda self: self._task_mgr.get_task("rois"))
|
|
41
|
+
""" ROIs """
|
|
42
|
+
|
|
43
|
+
# spectra_sums = property(lambda self: self._task_mgr.get_task("spectrasums"))
|
|
44
|
+
# """ Sums of spectra """
|
|
45
|
+
|
|
46
|
+
saving = property(lambda self: self._task_mgr.get_task("saving"))
|
|
47
|
+
""" Saving manager """
|
|
48
|
+
|
|
49
|
+
# accum_buffer = property(lambda self: self._accum_buffer)
|
|
50
|
+
# """ Accumulation buffer """
|
|
51
|
+
|
|
52
|
+
detectors = property(lambda self: self._detectors)
|
|
53
|
+
|
|
54
|
+
tasks = property(lambda self: self._task_mgr)
|
|
55
|
+
|
|
56
|
+
def __init__(self):
|
|
57
|
+
super().__init__()
|
|
58
|
+
self._buffer_point_size = DEFAULT_BUFFER_SIZE
|
|
59
|
+
|
|
60
|
+
self._detectors = Detectors()
|
|
61
|
+
self._spectrum_size = 0
|
|
62
|
+
self._stats_names = tuple()
|
|
63
|
+
|
|
64
|
+
self._spectra = Spectra(buffer_size=self._buffer_point_size, dtype=SPECTRA_DTYPE)
|
|
65
|
+
self._stats = Stats(buffer_size=self._buffer_point_size, dtype=STATS_DTYPE)
|
|
66
|
+
self._newdata_task = None
|
|
67
|
+
|
|
68
|
+
self._data_queue = deque()
|
|
69
|
+
self._task_mgr = TaskManager(self._buffer_point_size,
|
|
70
|
+
SPECTRA_DTYPE)
|
|
71
|
+
|
|
72
|
+
self._blocker = Blocker(self.reconfig)
|
|
73
|
+
|
|
74
|
+
@property
|
|
75
|
+
def delayed_reconfig(self):
|
|
76
|
+
return self._blocker
|
|
77
|
+
|
|
78
|
+
def metadata(self):
|
|
79
|
+
rois = self.rois.metadata()
|
|
80
|
+
saving = self.saving.metadata()
|
|
81
|
+
return {"spectrum_size": self.spectrum_size,
|
|
82
|
+
"stats_names": self.stats_names,
|
|
83
|
+
"rois": rois,
|
|
84
|
+
"saving": saving}
|
|
85
|
+
|
|
86
|
+
@property
|
|
87
|
+
def spectrum_size(self):
|
|
88
|
+
return self._spectrum_size
|
|
89
|
+
|
|
90
|
+
@property
|
|
91
|
+
def stats_names(self):
|
|
92
|
+
return self._stats_names
|
|
93
|
+
|
|
94
|
+
@property
|
|
95
|
+
def task_names(self):
|
|
96
|
+
return tuple(self._task_mgr.task_names)
|
|
97
|
+
|
|
98
|
+
def get_task(self, task_name):
|
|
99
|
+
return self._task_mgr.get_task(task_name)
|
|
100
|
+
|
|
101
|
+
@property
|
|
102
|
+
def n_available_points(self):
|
|
103
|
+
"""
|
|
104
|
+
Number of points processed (all tasks BUT saving)
|
|
105
|
+
"""
|
|
106
|
+
# tasks_points is None if no tasks are active
|
|
107
|
+
tasks_points = self._task_mgr.n_points
|
|
108
|
+
if tasks_points is not None:
|
|
109
|
+
# TODO: ??? if no tasks just return n_read
|
|
110
|
+
return min(self.n_read_points, tasks_points)
|
|
111
|
+
return self.n_read_points
|
|
112
|
+
|
|
113
|
+
@property
|
|
114
|
+
def n_read_points(self):
|
|
115
|
+
""" Number of spectra points read from the hardware. """
|
|
116
|
+
return self.spectra.n_points
|
|
117
|
+
|
|
118
|
+
def clear(self):
|
|
119
|
+
logger.info("Clearing data...")
|
|
120
|
+
self.spectra.clear()
|
|
121
|
+
self.stats.clear()
|
|
122
|
+
self.tasks.clear()
|
|
123
|
+
|
|
124
|
+
def reconfig(self):
|
|
125
|
+
self.clear()
|
|
126
|
+
tasksman = self.tasks
|
|
127
|
+
with tasksman.delayed_reconfig:
|
|
128
|
+
tasksman.set_detectors(copy.deepcopy(self.detectors))
|
|
129
|
+
tasksman.set_spectrum_size(self.spectrum_size)
|
|
130
|
+
tasksman.set_stats_names(tuple(self.stats_names[:]))
|
|
131
|
+
|
|
132
|
+
def set_detectors(self, detectors: Detectors):
|
|
133
|
+
""" set detector detectors information """
|
|
134
|
+
self._detectors = copy.deepcopy(detectors)
|
|
135
|
+
self.reconfig()
|
|
136
|
+
|
|
137
|
+
def set_spectrum_size(self, spectrum_size: int):
|
|
138
|
+
""" set detector spectrum_size """
|
|
139
|
+
self._spectrum_size = spectrum_size
|
|
140
|
+
self.reconfig()
|
|
141
|
+
|
|
142
|
+
def set_stats_names(self, stats_names: int):
|
|
143
|
+
""" set detector stats names """
|
|
144
|
+
self._stats_names = stats_names
|
|
145
|
+
self.reconfig()
|
|
146
|
+
|
|
147
|
+
# =================================
|
|
148
|
+
# New data task
|
|
149
|
+
# =================================
|
|
150
|
+
def _wait_data(self, task, *args, **kwargs):
|
|
151
|
+
logger.info("Wait loop started.")
|
|
152
|
+
for n_read in iter(task.queue.get, None):
|
|
153
|
+
logger.info("Wait loop: n_read=%s", n_read)
|
|
154
|
+
self._notify_new_data(n_read)
|
|
155
|
+
if n_read <= 0:
|
|
156
|
+
logger.info("Wait loop done.")
|
|
157
|
+
break
|
|
158
|
+
# if n_read > 0:
|
|
159
|
+
# self._notify_new_data(n_read)
|
|
160
|
+
# else:
|
|
161
|
+
# logger.info("Wait loop done.")
|
|
162
|
+
# break
|
|
163
|
+
|
|
164
|
+
def _cleanup_poll(self, task, *args, **kwargs):
|
|
165
|
+
logger.warning("Cleanup.")
|
|
166
|
+
self._newdata_task = None
|
|
167
|
+
|
|
168
|
+
def notify_start(self):
|
|
169
|
+
logger.info("Notify start in.")
|
|
170
|
+
if self._newdata_task and not self._newdata_task.ready():
|
|
171
|
+
raise RuntimeError("For some reason the previous reading task is still running...")
|
|
172
|
+
self.lock()
|
|
173
|
+
self._task_mgr.notify_start()
|
|
174
|
+
self._newdata_task = AsyncTask(run_func=self._wait_data, cleanup_func=self._cleanup_poll)
|
|
175
|
+
logger.info("Notify start out.")
|
|
176
|
+
|
|
177
|
+
def notify_end(self):
|
|
178
|
+
logger.info("Notify end in.")
|
|
179
|
+
if self._newdata_task:
|
|
180
|
+
self._newdata_task.put(-1)
|
|
181
|
+
self.wait_done()
|
|
182
|
+
self.unlock()
|
|
183
|
+
logger.info("Notify end out.")
|
|
184
|
+
|
|
185
|
+
def lock(self):
|
|
186
|
+
self.spectra.lock()
|
|
187
|
+
self.stats.lock()
|
|
188
|
+
self._task_mgr.lock()
|
|
189
|
+
|
|
190
|
+
def unlock(self):
|
|
191
|
+
self.spectra.unlock()
|
|
192
|
+
self.stats.unlock()
|
|
193
|
+
self._task_mgr.unlock()
|
|
194
|
+
|
|
195
|
+
def wait_done(self):
|
|
196
|
+
if self._newdata_task:
|
|
197
|
+
logger.info("Waiting for data tasks to end.")
|
|
198
|
+
try:
|
|
199
|
+
self._newdata_task.join()
|
|
200
|
+
self._newdata_task = None
|
|
201
|
+
except AttributeError:
|
|
202
|
+
pass
|
|
203
|
+
logger.info("Data tasks done!")
|
|
204
|
+
else:
|
|
205
|
+
logger.info("No data tasks running!")
|
|
206
|
+
|
|
207
|
+
def is_running(self):
|
|
208
|
+
return self._newdata_task is not None
|
|
209
|
+
|
|
210
|
+
def prepare(self):
|
|
211
|
+
n_detectors = len(self.detectors.detectors)
|
|
212
|
+
self.spectra.prepare(n_items=n_detectors,
|
|
213
|
+
spectrum_size=self.spectrum_size)
|
|
214
|
+
self.stats.prepare(n_items=n_detectors,
|
|
215
|
+
names=self.stats_names)
|
|
216
|
+
self._task_mgr.prepare()
|
|
217
|
+
|
|
218
|
+
def add_spectra(self, spectra, stats=None):
|
|
219
|
+
"""
|
|
220
|
+
Adds spectra to the data buffer or
|
|
221
|
+
to the accumulation buffer.
|
|
222
|
+
It notifies the saving manager if saving mode is AUTO.
|
|
223
|
+
"""
|
|
224
|
+
self._data_queue.appendleft((spectra, stats))
|
|
225
|
+
if self._newdata_task:
|
|
226
|
+
self._newdata_task.put(spectra.shape[1])
|
|
227
|
+
else:
|
|
228
|
+
self._notify_new_data(spectra.shape[1])
|
|
229
|
+
|
|
230
|
+
def _notify_new_data(self, n_new):
|
|
231
|
+
if not self._newdata_task:
|
|
232
|
+
raise RuntimeError("Data task not started. Did you call notify_start?")
|
|
233
|
+
|
|
234
|
+
if n_new > 0:
|
|
235
|
+
spectra, stats = self._data_queue.pop()
|
|
236
|
+
|
|
237
|
+
data_idx = self.detectors.data_indices()
|
|
238
|
+
|
|
239
|
+
if data_idx:
|
|
240
|
+
stats = stats[data_idx] if stats is not None else None
|
|
241
|
+
spectra, stats = self._task_mgr.new_spectra(spectra[data_idx], stats)
|
|
242
|
+
else:
|
|
243
|
+
spectra, stats = self._task_mgr.new_spectra(spectra, stats)
|
|
244
|
+
|
|
245
|
+
self.spectra.add_spectra(spectra)
|
|
246
|
+
if stats is not None:
|
|
247
|
+
self.stats.add_stats(stats)
|
|
248
|
+
else:
|
|
249
|
+
self._task_mgr.notify_end()
|
|
250
|
+
|
|
251
|
+
def get_spectra(self, from_point=None, to_point=None, flatten=False):
|
|
252
|
+
if flatten:
|
|
253
|
+
return self.spectra.flatten[from_point:to_point]
|
|
254
|
+
else:
|
|
255
|
+
return self.spectra[from_point:to_point]
|
|
256
|
+
|
|
257
|
+
def get_stats(self, from_point=None, to_point=None, flatten=False):
|
|
258
|
+
if flatten:
|
|
259
|
+
return self.stats.flatten[from_point:to_point]
|
|
260
|
+
else:
|
|
261
|
+
return self.stats[from_point:to_point]
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import logging
|
|
2
|
+
logger = logging.getLogger(__name__)
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
class Detectors:
|
|
6
|
+
"""
|
|
7
|
+
Managing detectors states:
|
|
8
|
+
- available
|
|
9
|
+
- sw disabled
|
|
10
|
+
- hw disabled
|
|
11
|
+
|
|
12
|
+
This class doesn't actually perform anything to disable channels
|
|
13
|
+
on the hardware, this is the responsibility of the caller. But this class
|
|
14
|
+
is used for keeping track of which channels are disabled
|
|
15
|
+
and enabled, and matching detector names to index in the returned
|
|
16
|
+
data (spectra, stats).
|
|
17
|
+
"""
|
|
18
|
+
|
|
19
|
+
def __init__(self, available_detectors=None):
|
|
20
|
+
# tuple, to keep order
|
|
21
|
+
self._available_detectors = tuple(available_detectors or [])
|
|
22
|
+
self._sw_disabled_detectors = set()
|
|
23
|
+
self._hw_disabled_detectors = set()
|
|
24
|
+
|
|
25
|
+
def _check_detector(self, chan: int):
|
|
26
|
+
if chan not in self.available_detectors:
|
|
27
|
+
logger.warning("Detector %s is not available. Ignoring.", chan)
|
|
28
|
+
return False
|
|
29
|
+
return True
|
|
30
|
+
|
|
31
|
+
@property
|
|
32
|
+
def available_detectors(self) -> tuple[int, ...]:
|
|
33
|
+
return self._available_detectors
|
|
34
|
+
|
|
35
|
+
@property
|
|
36
|
+
def detectors(self) -> tuple[int, ...]:
|
|
37
|
+
""" Detectors that are not disabled (neither hw or sw). """
|
|
38
|
+
# not using a full set difference to keep order
|
|
39
|
+
return tuple([detector for detector in self.available_detectors
|
|
40
|
+
if detector not in self.disabled_detectors])
|
|
41
|
+
|
|
42
|
+
@property
|
|
43
|
+
def hw_disabled_detectors(self) -> tuple[int, ...]:
|
|
44
|
+
return tuple(self._hw_disabled_detectors)
|
|
45
|
+
|
|
46
|
+
@property
|
|
47
|
+
def sw_disabled_detectors(self) -> tuple[int, ...]:
|
|
48
|
+
return tuple(self._sw_disabled_detectors)
|
|
49
|
+
|
|
50
|
+
@property
|
|
51
|
+
def disabled_detectors(self) -> tuple[int, ...]:
|
|
52
|
+
""" All disabled detectors (either hw or sw)"""
|
|
53
|
+
return tuple(self._hw_disabled_detectors | self._sw_disabled_detectors)
|
|
54
|
+
|
|
55
|
+
def set_enabled(self, chan: int) -> None:
|
|
56
|
+
logger.info(f"Flagging detector {chan} as enabled.")
|
|
57
|
+
self._hw_disabled_detectors.discard(chan)
|
|
58
|
+
self._sw_disabled_detectors.discard(chan)
|
|
59
|
+
|
|
60
|
+
def set_hw_disabled(self, chan: int):
|
|
61
|
+
# removing it from the sw disabled list
|
|
62
|
+
# no reason it should be in there tho
|
|
63
|
+
logger.info(f"Flagging detector {chan} as hard disabled.")
|
|
64
|
+
try:
|
|
65
|
+
self._sw_disabled_detectors.remove(chan)
|
|
66
|
+
logger.error("Detector %s was in the soft disabled list."
|
|
67
|
+
"This needs to be investigated...",
|
|
68
|
+
chan)
|
|
69
|
+
except KeyError:
|
|
70
|
+
pass
|
|
71
|
+
|
|
72
|
+
if self._check_detector(chan):
|
|
73
|
+
self._hw_disabled_detectors.add(chan)
|
|
74
|
+
|
|
75
|
+
def set_sw_disabled(self, chan: int):
|
|
76
|
+
logger.info(f"Flagging detector {chan} as soft disabled.")
|
|
77
|
+
if chan in self._hw_disabled_detectors:
|
|
78
|
+
raise RuntimeError(f"Can't mute detector {chan}, "
|
|
79
|
+
"it is already hardware disabled.")
|
|
80
|
+
if self._check_detector(chan):
|
|
81
|
+
self._sw_disabled_detectors.add(chan)
|
|
82
|
+
|
|
83
|
+
def set_all_enabled(self):
|
|
84
|
+
logger.info("Enabling all detectors.")
|
|
85
|
+
for chan in self.available_detectors:
|
|
86
|
+
self.set_enabled(chan)
|
|
87
|
+
|
|
88
|
+
@property
|
|
89
|
+
def active_detectors(self) -> tuple[int, ...]:
|
|
90
|
+
""" Returns the detectors that will acquire and
|
|
91
|
+
return data when the detector is started.
|
|
92
|
+
Those are the detectors that are not hardware disabled.
|
|
93
|
+
"""
|
|
94
|
+
disab = set(self.hw_disabled_detectors)
|
|
95
|
+
# iterating over the ordered available_detectors tuple to keep order
|
|
96
|
+
return tuple(det for det in self.available_detectors if det not in disab)
|
|
97
|
+
|
|
98
|
+
def data_indices(self):
|
|
99
|
+
""" Returns the indices of non muted detectors within the
|
|
100
|
+
list of active (i.e: acquiring) detectors.
|
|
101
|
+
Returns None if no detectors are disabled.
|
|
102
|
+
|
|
103
|
+
e.g:
|
|
104
|
+
* available detectors: 0, 1, 2, 3, hw disabled detector: 2
|
|
105
|
+
-> data_indices = 0, 1, 2
|
|
106
|
+
* available detectors: 0, 1, 2, 3, sw disabled detector: 2
|
|
107
|
+
-> data_indices = 0, 1, 3
|
|
108
|
+
"""
|
|
109
|
+
muted = self.sw_disabled_detectors
|
|
110
|
+
if not muted:
|
|
111
|
+
return None
|
|
112
|
+
active = self.active_detectors
|
|
113
|
+
return [i for i, chan in enumerate(active)
|
|
114
|
+
if chan not in muted]
|
|
115
|
+
|
|
116
|
+
|