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.
Files changed (73) hide show
  1. mosca/__init__.py +11 -0
  2. mosca/common/__init__.py +9 -0
  3. mosca/common/enums.py +79 -0
  4. mosca/common/logger.py +139 -0
  5. mosca/common/threads.py +199 -0
  6. mosca/common/utils.py +70 -0
  7. mosca/control/__init__.py +4 -0
  8. mosca/control/buffer.py +526 -0
  9. mosca/control/controller.py +278 -0
  10. mosca/control/datahandler.py +261 -0
  11. mosca/control/detectors.py +116 -0
  12. mosca/control/mosca.py +693 -0
  13. mosca/control/spectra.py +36 -0
  14. mosca/control/stats.py +43 -0
  15. mosca/control/tasks/__init__.py +3 -0
  16. mosca/control/tasks/background.py +118 -0
  17. mosca/control/tasks/rois.py +466 -0
  18. mosca/control/tasks/saving.py +391 -0
  19. mosca/control/tasks/spectrasums.py +185 -0
  20. mosca/control/tasks/task.py +201 -0
  21. mosca/control/tasks/taskmanager.py +133 -0
  22. mosca/devices/__init__.py +28 -0
  23. mosca/devices/hamamatsu/__init__.py +0 -0
  24. mosca/devices/hamamatsu/hamamatsu.py +88 -0
  25. mosca/devices/hamamatsu/hamamatsuhw.py +1298 -0
  26. mosca/devices/oceanoptics/__init__.py +0 -0
  27. mosca/devices/oceanoptics/oceanoptics.py +59 -0
  28. mosca/devices/oceanoptics/oceanopticshw.py +379 -0
  29. mosca/devices/simulation/__init__.py +0 -0
  30. mosca/devices/simulation/dante/__init__.py +1 -0
  31. mosca/devices/simulation/dante/simdante.py +33 -0
  32. mosca/devices/simulation/dante/simdantehw.py +506 -0
  33. mosca/devices/simulation/dummy/__init__.py +1 -0
  34. mosca/devices/simulation/dummy/dummy.py +299 -0
  35. mosca/devices/simulation/generator.py +607 -0
  36. mosca/devices/simulation/xia/__init__.py +1 -0
  37. mosca/devices/simulation/xia/iniparser.py +99 -0
  38. mosca/devices/simulation/xia/simfalconx.py +26 -0
  39. mosca/devices/simulation/xia/simfalconxhw.py +599 -0
  40. mosca/devices/xglab/__init__.py +0 -0
  41. mosca/devices/xglab/dante.py +126 -0
  42. mosca/devices/xglab/dantehw.py +1178 -0
  43. mosca/devices/xia/__init__.py +0 -0
  44. mosca/devices/xia/falconx.py +232 -0
  45. mosca/devices/xia/handel/__init__.py +14 -0
  46. mosca/devices/xia/handel/_cffi.py +62 -0
  47. mosca/devices/xia/handel/error.py +272 -0
  48. mosca/devices/xia/handel/handel_inc/handel_constants.h +127 -0
  49. mosca/devices/xia/handel/handel_inc/handel_errors.h +254 -0
  50. mosca/devices/xia/handel/handel_inc/handel_generic.h +66 -0
  51. mosca/devices/xia/handel/interface.py +1025 -0
  52. mosca/devices/xia/handel/mapping.py +107 -0
  53. mosca/devices/xia/handel/parser.py +62 -0
  54. mosca/devices/xia/handel/stats.py +85 -0
  55. mosca/devices/xia/mercury.py +160 -0
  56. mosca/devices/xia/utils.py +47 -0
  57. mosca/devices/xia/xia.py +575 -0
  58. mosca/devices/xia/xiads.py +161 -0
  59. mosca/mosca.py +0 -0
  60. mosca/tango/__init__.py +2 -0
  61. mosca/tango/controller.py +80 -0
  62. mosca/tango/main.py +163 -0
  63. mosca/tango/mosca.py +513 -0
  64. mosca/tango/moscav1.py +408 -0
  65. mosca/tango/shared.py +23 -0
  66. mosca/tango/tasks/__init__.py +0 -0
  67. mosca/tango/tasks/background.py +55 -0
  68. mosca/tango/utils.py +109 -0
  69. mosca-2.0.2.dist-info/METADATA +32 -0
  70. mosca-2.0.2.dist-info/RECORD +73 -0
  71. mosca-2.0.2.dist-info/WHEEL +4 -0
  72. mosca-2.0.2.dist-info/entry_points.txt +2 -0
  73. mosca-2.0.2.dist-info/licenses/LICENSE +166 -0
@@ -0,0 +1,526 @@
1
+ import logging
2
+ logger = logging.getLogger(__name__)
3
+
4
+ import copy
5
+ import time
6
+ import typing
7
+
8
+ import numpy as np
9
+
10
+
11
+ # dont forget to update the tests!
12
+ # TODO: check available memory
13
+ # TODO: add a property to the DS
14
+ DEFAULT_BUFFER_SIZE = 15000
15
+ DEFAULT_N_ITEMS = 1
16
+ DEFAULT_DATA_SIZE = 0
17
+ DEFAULT_DTYPE = np.float64
18
+
19
+
20
+ # TODO : needs optimisation and/or complete rewriting
21
+ # temporarily disabling this until this is used somewhere
22
+ class AccumulationBuffer:
23
+ pass
24
+ # """
25
+ # Accumulation buffer.
26
+ # Warning! The only thing stored in an instance of AccumulationBuffer
27
+ # is the incomplete accumulated buffer.
28
+ # The accumulation result, if any, is returned when the accumulation
29
+ # function is called, but not stored in this class.
30
+ # See AccumulationBuffer.accumulate documentation.
31
+ # """
32
+
33
+ # # TODO: update to uint64?
34
+ # DTYPE = np.uint32
35
+
36
+ # n_items = property(lambda self: self._data.shape[0])
37
+ # """ Number of data items (1st dimension) in the buffer spectra. """
38
+ # spectra_size = property(lambda self: self._data.shape[1])
39
+ # """ Spectra size. """
40
+ # accumulated = property(lambda self: self._accumulated)
41
+ # """ Number of currently accumulated points. """
42
+ # empty = property(lambda self: self.accumulated == 0)
43
+ # n_accum_frames = property(lambda self: self._n_accum_frames)
44
+
45
+ # def __init__(self, n_items, spectra_size, n_accum_frames):
46
+ # """
47
+ # Creates a buffer that will accumulate n_accum_frames points.
48
+ # Expects data of shape (n_items, :, spectra_size).
49
+ # Accumulated buffer is of dtype numpy.uint32.
50
+ # """
51
+ # self._data = np.ndarray((1, 1))
52
+ # self._n_accum_frames = n_accum_frames
53
+ # self._accumulated = 0
54
+ # self._data = np.zeros((n_items, spectra_size), dtype=self.DTYPE)
55
+
56
+ # def __call__(self, *args, **kwargs):
57
+ # """
58
+ # Just calls self.accumulate with the given arguments.
59
+ # """
60
+ # return self.accumulate(*args, **kwargs)
61
+
62
+ # def accumulate(self, spectra, n_points=None):
63
+ # """
64
+ # Accumulates spectra.
65
+ # Returns the completed accumulation(s), if any, or None.
66
+ # Expects an array that can be reshaped to (nb_modules, :, spectra_size).
67
+ # where the 2nd dimension is the number of points.
68
+ # If enough points are provided to complete an accumulation, the accumulated
69
+ # result will be returned as a numpy array of shape (nb_modules, :, spectra_size),
70
+ # where the 2nd dimension is the number of completed accumulations.
71
+
72
+ # Example:
73
+ # >> buf = AccumulationBuffer(4, 1024, 5)
74
+ # # This creates a buffer that will accumulated 5 points.
75
+ # >> spectra = np.ndarray((4, 3, 1024))
76
+ # >> result = buf(spectra)
77
+ # # adding 3 points, not enough to complete an accumulation,
78
+ # # result is then None
79
+ # spectra = np.ndarray((4, 6, 1024))
80
+ # result = buf(spectra)
81
+ # # Now adding 6 points to the previous 3
82
+ # # this completes 1 accumulation of 5, and starts a new one of 4
83
+ # # (3 + 6 == 5 + 4)
84
+ # # The completed accumulation is returned: result.shape == (8, 1, 1024)
85
+ # spectra = np.ndarray((4, 11, 1024))
86
+ # result = buf(spectra)
87
+ # # adding 11 points to the previous 4, this completes 3 accumulations
88
+ # # (4 + 11 == 3 * 5)
89
+ # # 3 accumulations are returned: result.shape == (8, 3, 1024)
90
+ # """
91
+
92
+ # # TODO: is it necessary?
93
+ # # n_points is just used to check the array shape
94
+ # # if n_points is None:
95
+ # # assert s_n_points == n_points
96
+ # # else:
97
+ # # n_points = -1
98
+
99
+ # if n_points is None:
100
+ # n_points = -1
101
+
102
+ # # TODO: make sure this is a View and not a copy.
103
+ # spectra = spectra.reshape(self.n_items, n_points, self.spectra_size)
104
+ # # number of points in the given array
105
+ # s_n_points = spectra.shape[1]
106
+
107
+ # # already accumulated frames
108
+ # accumulated = self._accumulated
109
+ # # max frames to accumulate
110
+ # n_accum_frames = self._n_accum_frames
111
+ # # current point index in the input buffer
112
+ # point_idx = 0
113
+
114
+ # # number of expected complete accumulations from this new data
115
+ # n_results = (s_n_points + accumulated) // n_accum_frames
116
+ # if n_results == 0:
117
+ # result = None
118
+ # else:
119
+ # result = np.ndarray(
120
+ # (self.n_items, n_results, self.spectra_size), dtype=self.DTYPE
121
+ # )
122
+ # # pointer to the next accumulated data in the output arry
123
+ # result_idx = 0
124
+
125
+ # # accumulating frames
126
+ # while point_idx < s_n_points:
127
+ # # remaining number of points to add to
128
+ # # the current accumulation buffer
129
+ # remaining = min(s_n_points - point_idx, n_accum_frames - accumulated)
130
+
131
+ # # starting a new accumulation, or adding to the previous one
132
+ # if accumulated == 0:
133
+ # np.sum(
134
+ # spectra[:, point_idx : point_idx + remaining, :],
135
+ # axis=1,
136
+ # dtype=self.DTYPE,
137
+ # out=self._data,
138
+ # )
139
+ # else:
140
+ # self._data += np.sum(
141
+ # spectra[:, point_idx : point_idx + remaining, :],
142
+ # axis=1,
143
+ # dtype=self.DTYPE,
144
+ # )
145
+
146
+ # # moving up the input buffer
147
+ # point_idx += remaining
148
+ # accumulated += remaining
149
+ # # completed an accumulation, copying result to output buffer
150
+ # if accumulated == n_accum_frames:
151
+ # result[:, result_idx, :] = self._data
152
+ # result_idx += 1
153
+ # remaining = 0
154
+ # accumulated = 0
155
+
156
+ # self._accumulated = accumulated
157
+ # return result
158
+
159
+
160
+ class _FlattenView:
161
+ __slots__ = ("_buffer",)
162
+
163
+ def __init__(self, buffer):
164
+ self._buffer = buffer
165
+
166
+ def __getitem__(self, index):
167
+ return self._buffer[index].flatten()
168
+
169
+
170
+ class CircularBuffer:
171
+ # TODO: implement direct access (will need locking).
172
+ """
173
+ Class managing a circular buffer.
174
+ """
175
+
176
+ n_items = property(lambda self: self._data.shape[0])
177
+ """ Number of detector modules.
178
+ (1st dim of the buffer) """
179
+
180
+ n_points = property(lambda self: self._n_points_set)
181
+ """ Total number of points that have been added to this buffer,
182
+ including ones that may have been overwritten
183
+ (i.e: NOT the number of points currently stored,
184
+ which can't be greater than buffer_size) """
185
+
186
+ data_size = property(lambda self: self._data.shape[2])
187
+ """ Data size (3d dimension of the buffer). """
188
+
189
+ buffer_size = property(lambda self: self._data.shape[1])
190
+ """ Maximum number of points that can be stored in the buffer
191
+ at any given time (2nd dimension).
192
+ Once the limit is reached the older points will be overwritten. """
193
+
194
+ first_point_time = property(lambda self: self._metadata["first_point_time"])
195
+ """ Timestamp of the first added point. """
196
+
197
+ latest_point_time = property(lambda self: self._metadata["latest_point_time"])
198
+ """ Timestamp of the latest added point. """
199
+
200
+ dtype = property(lambda self: self._data.dtype)
201
+
202
+ locked = property(lambda self: self._locked)
203
+
204
+ def __init__(self,
205
+ n_items=DEFAULT_N_ITEMS,
206
+ buffer_size=DEFAULT_BUFFER_SIZE,
207
+ data_size=DEFAULT_DATA_SIZE,
208
+ dtype=DEFAULT_DTYPE):
209
+ self._data = np.zeros(
210
+ (n_items, buffer_size, data_size),
211
+ dtype=dtype
212
+ )
213
+ self._n_points_set = 0
214
+ self._first_point_num = -1
215
+ self._last_idx = -1
216
+ self._metadata = {"first_point_time": None, "latest_point_time": None}
217
+ self._locked = False
218
+
219
+ def lock(self):
220
+ self._locked = True
221
+
222
+ def unlock(self):
223
+ self._locked = False
224
+
225
+ def _check_lock(self):
226
+ if self.locked:
227
+ raise RuntimeError("Buffer is locked. Can't modify its properties.")
228
+
229
+ @property
230
+ def latest_point_num(self):
231
+ """
232
+ Latest stored point number.
233
+ """
234
+ # TODO: raise if no points yet?
235
+ return self.n_points - 1
236
+
237
+ @property
238
+ def oldest_point_num(self):
239
+ """
240
+ Oldest stored point number.
241
+ """
242
+ if self.n_points <= 0:
243
+ raise RuntimeError("Buffer contains no point yet.")
244
+ if self.n_points < self.buffer_size:
245
+ return 0
246
+ return self.n_points - self.buffer_size
247
+
248
+ @property
249
+ def _first_point_idx(self):
250
+ if self.n_points <= 0:
251
+ raise RuntimeError("Buffer contains no point yet.")
252
+ if self.n_points <= self.buffer_size:
253
+ return 0
254
+ return (self._last_idx + 1) % self.buffer_size
255
+
256
+ def clear(self):
257
+ """
258
+ Resets the point counters.
259
+ (sets stored points list to -1 and last_point_num to -1)
260
+ """
261
+ self._check_lock()
262
+ self._last_idx = -1
263
+ self._n_points_set = 0
264
+ self._first_point_num = -1
265
+ self._metadata["first_point_time"] = None
266
+ self._metadata["latest_point_time"] = None
267
+
268
+ def prepare(
269
+ self,
270
+ n_items=None,
271
+ buffer_size=None,
272
+ data_size=None,
273
+ dtype=None
274
+ ):
275
+ """
276
+ TODO: hide counter size
277
+ Allocate the buffers.
278
+ n_items: number of items (1st dim of the buffer)
279
+ buffer_size: max number of points to store (2nd dim of the buffer)
280
+ data_size: size of data (3rd dim of the buffer)
281
+ """
282
+ self._check_lock()
283
+
284
+ if n_items is None:
285
+ n_items = self.n_items
286
+ if buffer_size is None:
287
+ buffer_size = self.buffer_size
288
+ if data_size is None:
289
+ data_size = self.data_size
290
+ if dtype is None:
291
+ dtype = self._data.dtype
292
+
293
+ alloc = (
294
+ self._data.shape != (n_items, buffer_size, data_size) or dtype != self._data.dtype
295
+ )
296
+
297
+ if alloc:
298
+ self._data = np.zeros(
299
+ (n_items, buffer_size, data_size), dtype=dtype
300
+ )
301
+ self.clear()
302
+
303
+ def _get_point_index(self, pix_num, clip_idx=False, raise_ex=False):
304
+ """
305
+ Checks if a point is still present in the buffers and return its index.
306
+ Returns -1 if the point is not present anymore.
307
+
308
+ IMPORTANT! If pix_num is < 0 we consider that the requested point is relative to the newest point
309
+
310
+ pix_num: point number to find. If negative then the point with be last_point + pix_num.
311
+ raise_ex: if set to True, the function will raise an exception
312
+ instead of returning -1
313
+ """
314
+ try:
315
+ if self._last_idx < 0:
316
+ raise RuntimeError("Buffer contains no point yet.")
317
+ # the latest point stored
318
+ newest = self.latest_point_num
319
+ # the older point stored
320
+ oldest = self.oldest_point_num
321
+
322
+ # if pix_num is < 0 we consider that the requested point
323
+ # is relative to the end of the buffer,
324
+ # just like any python array
325
+ if pix_num < 0:
326
+ # last index
327
+ pix_num = newest + pix_num + 1
328
+
329
+ if clip_idx:
330
+ if pix_num > newest:
331
+ pix_num = newest
332
+ elif pix_num < oldest:
333
+ pix_num = oldest
334
+ elif pix_num > newest:
335
+ raise RuntimeError(f"point {pix_num} is not available yet.")
336
+ elif pix_num < oldest:
337
+ raise RuntimeError(f"point {pix_num} has already been discarded.")
338
+
339
+ pix_idx = (
340
+ self._first_point_idx + (pix_num - oldest)
341
+ ) % self.buffer_size
342
+ except:
343
+ if raise_ex:
344
+ raise
345
+ return -1
346
+ return pix_idx
347
+
348
+ def _get_points_index_range(
349
+ self, first_pix_num=None, last_pix_num=None, clip_idx=False, raise_ex=True
350
+ ):
351
+ """
352
+ Returns the indices of a point range in the buffers.
353
+ Returns an empty array if one or both limits are not found
354
+ in the stored points.
355
+ first_pix_num: point number stored in the buffer. If None, the older point is selected.
356
+ last_pix_num: point number stored in the buffer. If None, the latest point is selected.
357
+ clip_idx: if true the given point range will be clipped to the available points.
358
+ raise_ex: if set to True, the function will raise an exception
359
+ instead of returning an empty array.
360
+ """
361
+ if first_pix_num is not None and first_pix_num == last_pix_num:
362
+ return np.array([], dtype=int)
363
+
364
+ # resolve negative point numbers to absolute point numbers *before*
365
+ # converting to buffer indices, so a backward range (first >= last)
366
+ # can be detected even when both individually refer to valid stored
367
+ # points (buffer indices wrap around and lose that information).
368
+ newest = self.latest_point_num
369
+ if first_pix_num is not None and first_pix_num < 0:
370
+ first_pix_num = newest + first_pix_num + 1
371
+ if last_pix_num is not None and last_pix_num < 0:
372
+ last_pix_num = newest + last_pix_num + 1
373
+
374
+ if last_pix_num is not None and last_pix_num == 0:
375
+ # common python indexing behaviour
376
+ return np.array([], dtype=int)
377
+
378
+ if (first_pix_num is not None and last_pix_num is not None
379
+ and first_pix_num >= last_pix_num):
380
+ return np.array([], dtype=int)
381
+
382
+ if first_pix_num is None:
383
+ first = self._first_point_idx
384
+ else:
385
+ first = self._get_point_index(
386
+ first_pix_num, clip_idx=clip_idx, raise_ex=raise_ex
387
+ )
388
+ if last_pix_num is None:# or last_pix_num == -1:
389
+ last = self._last_idx
390
+ else:
391
+ last = self._get_point_index(
392
+ last_pix_num - 1, clip_idx=clip_idx, raise_ex=raise_ex
393
+ )
394
+
395
+ if first == -1 or last == -1:
396
+ return np.array([], dtype=int)
397
+
398
+ if first == last:
399
+ point_range = np.array([first], dtype=int)
400
+ elif first < last:
401
+ # the range is inside the buffer
402
+ # [... first >>>> last ...]
403
+ point_range = np.arange(first, last + 1)
404
+ else:
405
+ # the range "crosses" over the end of the buffer
406
+ # [>>>> last ... first >>>>]
407
+ point_range = np.arange(
408
+ first, first + self.buffer_size - (first - last) + 1
409
+ )
410
+ point_range %= self.buffer_size
411
+ return point_range
412
+
413
+ def add_data(self, data):
414
+ """
415
+ Add data to the buffers.
416
+ point_nums: 1D array containing the point numbers
417
+ (0 being the first point of the SCAN)
418
+ data: array (see below)
419
+
420
+ Returns: the number of new points
421
+
422
+ IMPORTANT!
423
+ Arrays shape: it is the CALLER's resposability to provide correctly
424
+ aligned data.
425
+ -> data will be reshaped to (n_items, n_points, data_size)
426
+ """
427
+ n_items = self.n_items
428
+ data_size = self.data_size
429
+
430
+ data = data.reshape(n_items, -1, data_size)
431
+ npoints = data.shape[1]
432
+
433
+ if npoints == 0:
434
+ return 0
435
+
436
+ if npoints > self.buffer_size:
437
+ raise RuntimeError("Buffer is too small.")
438
+
439
+ if self.n_points != 0:
440
+ # positions of the saved elements
441
+ idx = (
442
+ np.arange(self._last_idx + 1, self._last_idx + 1 + npoints)
443
+ % self.buffer_size
444
+ )
445
+ # making sure we have the right shape
446
+ # nb: ndarray.reshape returns a view if possible, no copy.
447
+ self._data[:, idx, :] = data
448
+ last_idx = idx[-1]
449
+ else:
450
+ self._data[:, 0:npoints, :] = data
451
+ last_idx = npoints - 1
452
+
453
+ now = time.time()
454
+ if self._n_points_set == 0:
455
+ self._metadata["first_point_time"] = now
456
+ self._metadata["latest_point_time"] = now
457
+
458
+ self._last_idx = last_idx
459
+ self._n_points_set += npoints
460
+
461
+ return npoints
462
+
463
+ def get_point(self, pix_num):
464
+ """
465
+ Returns the given point.
466
+ pix_num: point number (0 is the first point of the SCAN)
467
+ """
468
+ idx = self._get_point_index(pix_num, raise_ex=True)
469
+ return self._data[:, idx, :]
470
+
471
+ def get_data(
472
+ self,
473
+ first=None,
474
+ last=None,
475
+ strip_first_axis=False,
476
+ raise_ex=True,
477
+ clip_idx=False,
478
+ flatten=False
479
+ ):
480
+ """
481
+ WARNING!! like any python array indexing, the last point
482
+ returned will be the one found at index last-1.
483
+ e.g: get_data(0, 34) will return points 0 to 33 included.
484
+
485
+ Returns data from a range of points
486
+ first: point number stored in the buffer. If None, the older point is selected.
487
+ last: point number stored in the buffer. If None, the latest point is selected.
488
+ strip_first_axis: if the first axis (nb points) size is 1, the arrays will be reshaped to 2D (points, data)
489
+ clip_idx: if true the given point range will be clipped to the available points.
490
+ flatten: reshape the arrays to 1D.
491
+ raise_ex: if set to True, the function will raise an exception
492
+ instead of returning empty arrays.
493
+ """
494
+ indices = self._get_points_index_range(
495
+ first, last, clip_idx=clip_idx, raise_ex=raise_ex
496
+ )
497
+ data = self._data[:, indices, :]
498
+ if flatten:
499
+ data = data.flatten()
500
+ elif strip_first_axis and self.n_items == 1:
501
+ data = data.reshape(len(indices), -1)
502
+ # TODO: fix first/last if clipped = True
503
+ return data
504
+
505
+ def __getitem__(self, index):
506
+ stop = None
507
+ if isinstance(index, (tuple, list)):
508
+ if len(index) > 1:
509
+ raise ValueError("Invalid index, max 1 dimensions (points).")
510
+ start = index[0]
511
+ elif isinstance(index, (slice,)):
512
+ start = index.start
513
+ stop = index.stop
514
+ if index.step is not None and index.step != 1:
515
+ raise ValueError(f"Slicing on the buffer only supports step == 1.")
516
+ if start is not None and stop is not None and stop > 0 and start > stop:
517
+ raise ValueError(f"Slicing on the buffer only supports start <= stop.")
518
+ else:
519
+ return self.get_point(index)
520
+
521
+ indices = self._get_points_index_range(start, stop)
522
+ return self._data[:, indices]
523
+
524
+ @property
525
+ def flatten(self):
526
+ return _FlattenView(self)