etho-python 0.22.0__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 (47) hide show
  1. etho/__init__.py +9 -0
  2. etho/app.py +569 -0
  3. etho/assets/etho_icon1_pipeline_dark.svg +23 -0
  4. etho/assets/etho_icon1_pipeline_light.svg +30 -0
  5. etho/cli.py +215 -0
  6. etho/client.py +384 -0
  7. etho/services/ANAZeroService.py +123 -0
  8. etho/services/BLTZeroService.py +108 -0
  9. etho/services/DAQZeroService.py +326 -0
  10. etho/services/DLPZeroService.py +240 -0
  11. etho/services/GCMZeroService.py +271 -0
  12. etho/services/GOVZeroService.py +231 -0
  13. etho/services/NICounterZeroService.py +92 -0
  14. etho/services/NITriggerZeroService.py +120 -0
  15. etho/services/TemplateZeroService.py +98 -0
  16. etho/services/ZeroService.py +272 -0
  17. etho/services/__init__.py +0 -0
  18. etho/services/callbacks/__init__.py +19 -0
  19. etho/services/callbacks/_base.py +92 -0
  20. etho/services/callbacks/_image.py +798 -0
  21. etho/services/callbacks/_trace.py +456 -0
  22. etho/services/camera/__init__.py +13 -0
  23. etho/services/camera/base.py +164 -0
  24. etho/services/camera/basler.py +201 -0
  25. etho/services/camera/dummy.py +139 -0
  26. etho/services/camera/hamamatsu.py +133 -0
  27. etho/services/camera/spinnaker.py +317 -0
  28. etho/services/camera/ximea.py +167 -0
  29. etho/services/daq/IOTask.py +311 -0
  30. etho/services/daq/ai.py +174 -0
  31. etho/services/dlp/dlp_make_warpfiles.py +131 -0
  32. etho/services/dlp/dlp_runners.py +238 -0
  33. etho/services/utils/__init__.py +0 -0
  34. etho/services/utils/concurrent_task.py +236 -0
  35. etho/services/utils/log_exceptions.py +39 -0
  36. etho/utils/__init__.py +0 -0
  37. etho/utils/calibration.py +88 -0
  38. etho/utils/config.py +41 -0
  39. etho/utils/runner.py +121 -0
  40. etho/utils/shuffled_cycle.py +45 -0
  41. etho/utils/sound.py +336 -0
  42. etho/utils/tui.py +105 -0
  43. etho/utils/zeroclient.py +95 -0
  44. etho_python-0.22.0.dist-info/METADATA +49 -0
  45. etho_python-0.22.0.dist-info/RECORD +47 -0
  46. etho_python-0.22.0.dist-info/WHEEL +4 -0
  47. etho_python-0.22.0.dist-info/entry_points.txt +3 -0
@@ -0,0 +1,317 @@
1
+ import time
2
+ import numpy as np
3
+ from typing import Tuple, Union
4
+ from .base import BaseCam
5
+ import cv2
6
+
7
+
8
+ try:
9
+ import PySpin
10
+ pyspin_error = None
11
+ except ImportError as pyspin_error:
12
+ pass
13
+
14
+ class Spinnaker(BaseCam):
15
+
16
+ NAME = 'SPN'
17
+
18
+
19
+ def __init__(self, serialnumber):
20
+ if pyspin_error is not None:
21
+ raise pyspin_error
22
+ self.serialnumber = serialnumber
23
+
24
+ def init(self):
25
+ self.cam_system = PySpin.System_GetInstance()
26
+ self.cam_list = self.cam_system.GetCameras()
27
+ self.c = self.cam_list.GetBySerial(self.serialnumber)
28
+ self.c.Init()
29
+ self.c.PixelFormat.SetValue(PySpin.PixelFormat_Mono8)
30
+ self._generate_attrs()
31
+
32
+ # enable embedding of frame TIME STAMP
33
+ self.c.ChunkModeActive.SetValue(True)
34
+ self.c.ChunkSelector.SetValue(PySpin.ChunkSelector_Timestamp)
35
+ self.c.ChunkEnable.SetValue(True)
36
+ self.timestamp_offset = self._estimate_timestamp_offset()
37
+
38
+ # set pixel format
39
+ self.c.PixelFormat.SetValue(PySpin.PixelFormat_Mono8)
40
+
41
+ # set continuous acquisition
42
+ self.c.AcquisitionMode.SetValue(PySpin.AcquisitionMode_Continuous)
43
+
44
+ # trigger overlap -> ReadOut - for faster frame rates
45
+ self.c.TriggerOverlap.SetValue(PySpin.TriggerOverlap_ReadOut)
46
+
47
+ self.timeout = PySpin.EVENT_TIMEOUT_INFINITE
48
+
49
+ # initialize image processor for converting mono8 to bgr8 images - faster than cv2 or np
50
+ self.processor = PySpin.ImageProcessor()
51
+ self.processor.SetColorProcessing(PySpin.SPINNAKER_COLOR_PROCESSING_ALGORITHM_HQ_LINEAR)
52
+
53
+ def get(self, timeout=None):
54
+ im = self.c.GetNextImage(self.timeout) # get image
55
+ system_stimestamp = time.time()
56
+
57
+ if im.IsIncomplete():
58
+ im.Release()
59
+ raise ValueError(f"Image incomplete with image status {im.GetImageStatus()}")
60
+ else:
61
+ image_timestamp = im.GetTimeStamp()
62
+ im_converted = self.processor.Convert(im, PySpin.PixelFormat_BGR8) # convert mono to rgb
63
+ im.Release() # release memory (and camera buffer?)
64
+
65
+ BGR = im_converted.GetNDArray() # get the converted image array
66
+ image_timestamp = image_timestamp / 1e9 + self.timestamp_offset # convert ts to seconds
67
+ return BGR, image_timestamp, system_stimestamp
68
+
69
+ @property
70
+ def roi(self) -> Tuple[int, int, int, int]:
71
+ return self.c.OffsetX.GetValue(), self.c.OffsetY.GetValue(), self.c.Width.GetValue(), self.c.Height.GetValue()
72
+
73
+ @roi.setter
74
+ def roi(self, x0_y0_x_y: Tuple[int, int, int, int]):
75
+ try:
76
+ x0, y0, x, y = x0_y0_x_y
77
+ except ValueError:
78
+ raise ValueError('Need 4-tuple with x0_y0_x_y')
79
+ else:
80
+ self._min_max_inc(self.c.Width, int(x))
81
+ self._min_max_inc(self.c.Height, int(y))
82
+ self._min_max_inc(self.c.OffsetX, int(x0))
83
+ self._min_max_inc(self.c.OffsetY, int(y0))
84
+
85
+ @property
86
+ def framerate(self):
87
+ return self.c.AcquisitionResultingFrameRate.GetValue()
88
+
89
+ @framerate.setter
90
+ def framerate(self, value: float):
91
+ self.c.AcquisitionFrameRateEnable.SetValue(True)
92
+ self.c.AcquisitionFrameRate.SetValue(float(value))
93
+
94
+ @property
95
+ def gamma(self):
96
+ return self.c.GammaEnable.GetValue()
97
+
98
+ @gamma.setter
99
+ def gamma(self, value: bool):
100
+ self.c.GammaEnable.SetValue(bool(value))
101
+
102
+ @property
103
+ def brightness(self) -> float:
104
+ return self.c.BlackLevel.GetValue()
105
+
106
+ @brightness.setter
107
+ def brightness(self, value: float):
108
+ self.c.BlackLevelSelector.SetValue(PySpin.BlackLevelSelector_All)
109
+ self.c.BlackLevel.SetValue(float(value))
110
+
111
+ @property
112
+ def exposure(self):
113
+ return self.c.ExposureTime.GetValue()
114
+
115
+ @exposure.setter
116
+ def exposure(self, value: Union[float, str]):
117
+ if value == 'AUTO':
118
+ self.c.ExposureAuto.SetValue(PySpin.ExposureAuto_Continuous)
119
+ else:
120
+ self.c.ExposureAuto.SetValue(PySpin.ExposureAuto_Off)
121
+ self.c.ExposureMode.SetValue(PySpin.ExposureMode_Timed)
122
+ self.c.ExposureTime.SetValue(float(value))
123
+
124
+ @property
125
+ def gain(self):
126
+ self.c.Gain.GetValue()
127
+
128
+ @gain.setter
129
+ def gain(self, value: Union[float, str]):
130
+ if value == 'AUTO':
131
+ self.c.GainAuto.SetValue(PySpin.GainAuto_Continuous)
132
+ else:
133
+ self.c.GainAuto.SetValue(PySpin.GainAuto_Off)
134
+ self.c.Gain.SetValue(float(value))
135
+
136
+ def optimize_auto_exposure(self):
137
+ self.setattr('AutoExposureControlLoopDamping', 0.1)
138
+ self.setattr('AutoExposureLightingMode_Val', 2) # Frontlight
139
+ self.setattr('AutoExposureExposureTimeLowerLimit', 6)
140
+ self.setattr('AutoExposureExposureTimeUpperLimit', 30000)
141
+ self.setattr('AutoExposureEVCompensation', 3)
142
+
143
+ @property
144
+ def external_trigger(self):
145
+ return self.c.TriggerMode.GetValue()
146
+
147
+ @external_trigger.setter
148
+ def external_trigger(self, value: bool = False):
149
+ self.c.TriggerMode.SetValue(PySpin.TriggerMode_Off)
150
+
151
+ if value:
152
+ self.c.TriggerSource.SetValue(PySpin.TriggerSource_Line3)
153
+ self.c.TriggerOverlap.SetValue(PySpin.TriggerOverlap_ReadOut)
154
+ self.c.TriggerMode.SetValue(PySpin.TriggerMode_On)
155
+
156
+ def _generate_attrs(self):
157
+
158
+ self._rw_modes = {
159
+ PySpin.RO: "read only",
160
+ PySpin.RW: "read/write",
161
+ PySpin.WO: "write only",
162
+ PySpin.NA: "not available"
163
+ }
164
+
165
+ self._attr_types = {
166
+ PySpin.intfIFloat: PySpin.CFloatPtr,
167
+ PySpin.intfIBoolean: PySpin.CBooleanPtr,
168
+ PySpin.intfIInteger: PySpin.CIntegerPtr,
169
+ PySpin.intfIEnumeration: PySpin.CEnumerationPtr,
170
+ PySpin.intfIString: PySpin.CStringPtr,
171
+ }
172
+
173
+ self._attr_type_names = {
174
+ PySpin.intfIFloat: 'float',
175
+ PySpin.intfIBoolean: 'bool',
176
+ PySpin.intfIInteger: 'int',
177
+ PySpin.intfIEnumeration: 'enum',
178
+ PySpin.intfIString: 'string',
179
+ PySpin.intfICommand: 'command',
180
+ }
181
+
182
+ self.camera_attributes = {}
183
+ self.camera_methods = {}
184
+ self.camera_node_types = {}
185
+
186
+ for node in self.c.GetNodeMap().GetNodes():
187
+ pit = node.GetPrincipalInterfaceType()
188
+ name = node.GetName()
189
+ self.camera_node_types[name] = self._attr_type_names.get(pit, pit)
190
+ if pit == PySpin.intfICommand:
191
+ self.camera_methods[name] = PySpin.CCommandPtr(node)
192
+ if pit in self._attr_types:
193
+ self.camera_attributes[name] = self._attr_types[pit](node)
194
+ self.initialized = True
195
+
196
+ def getattr(self, attr):
197
+ if attr in self.camera_attributes:
198
+
199
+ prop = self.camera_attributes[attr]
200
+ if not PySpin.IsReadable(prop):
201
+ raise AttributeError("Camera property '%s' is not readable" % attr)
202
+
203
+ if hasattr(prop, "GetValue"):
204
+ return prop.GetValue()
205
+ elif hasattr(prop, "ToString"):
206
+ return prop.ToString()
207
+ else:
208
+ raise AttributeError("Camera property '%s' is not readable" % attr)
209
+ elif attr in self.camera_methods:
210
+ return self.camera_methods[attr].Execute
211
+ else:
212
+ raise AttributeError(attr)
213
+
214
+ def setattr(self, attr, val):
215
+ if attr in self.camera_attributes:
216
+
217
+ prop = self.camera_attributes[attr]
218
+ if not PySpin.IsWritable(prop):
219
+ raise AttributeError("Property '%s' is not currently writable!" % attr)
220
+
221
+ if hasattr(prop, 'SetValue'):
222
+ prop.SetValue(val)
223
+ else:
224
+ prop.FromString(val)
225
+
226
+ elif attr in self.camera_methods:
227
+ raise AttributeError("Camera method '%s' is a function -- you can't assign it a value!" % attr)
228
+ else:
229
+ if attr not in self.__dict__ and self.lock and self.initialized:
230
+ raise AttributeError("Unknown property '%s'." % attr)
231
+ else:
232
+ raise AttributeError(attr)
233
+
234
+ def _min_max_inc(self, prop, value=None, set_value=True):
235
+ min_val, max_val, inc = prop.GetMin(), prop.GetMax(), prop.GetInc()
236
+
237
+ prop_type = type(prop.GetValue())
238
+ if value is not None:
239
+ value = np.clip(value, min_val, max_val)
240
+ value = np.round(value / inc) * inc
241
+ if set_value:
242
+ value = prop_type(value)
243
+ prop.SetValue(value)
244
+ value = prop.GetValue()
245
+ return value
246
+
247
+ def _estimate_timestamp_offset(self, timestamp_offset_iterations=10):
248
+ """Gets offset between system time and timestamps."""
249
+ # This method is required because the timestamp stored in the camera is relative to when it was powered on, so an
250
+ # offset needs to be applied to get it into epoch time; from tests I’ve done, this appears to be accurate to ~1e-3
251
+ # seconds.
252
+ timestamp_offsets = []
253
+ for i in range(timestamp_offset_iterations):
254
+ # Latch timestamp. This basically “freezes” the current camera timer into a variable that can be read with
255
+ self.c.TimestampLatch.Execute()
256
+ system_time = time.time()
257
+ # Compute timestamp offset in seconds; note that timestamp latch value is in nanoseconds
258
+ timestamp_offset = system_time - self.c.TimestampLatchValue.GetValue() / 1e9
259
+ timestamp_offsets.append(timestamp_offset)
260
+ # Return the median value
261
+ return np.median(timestamp_offsets)
262
+
263
+ def enable_gpio_strobe(self):
264
+ self._set_gpio_strobe(enable=True)
265
+
266
+ def disable_gpio_strobe(self):
267
+ self._set_gpio_strobe(enable=False)
268
+
269
+ def set_node_and_entry(self, node_name: str, entry_name: str):
270
+ nodemap = self.c.GetNodeMap()
271
+
272
+ node = PySpin.CEnumerationPtr(nodemap.GetNode(node_name))
273
+ if not PySpin.IsAvailable(node) or not PySpin.IsWritable(node):
274
+ raise ValueError(f"Cannot retrieve node {node_name}.")
275
+
276
+ entry = node.GetEntryByName(entry_name)
277
+ if not PySpin.IsAvailable(entry) or not PySpin.IsReadable(entry):
278
+ raise ValueError(f"Cannot retrieve entry {entry_name} from node {node_name}.")
279
+
280
+ node.SetIntValue(entry.GetValue())
281
+
282
+ def _set_gpio_strobe(self, line: str = "Line2", enable: bool = True):
283
+ # from PySpin/Examples/Python3/CounterAndTimer.py
284
+ # from https://github.com/CapAI/misc/blob/a22cd50e90018c03cde3c1339aa622185502bb05/spinnaker/pyspin/Examples/Python3/CounterAndTimer.py#L193
285
+ if enable:
286
+ line_mode = "Output"
287
+ else:
288
+ line_mode = "Input"
289
+
290
+ # Select line to control
291
+ self.set_node_and_entry("LineSelector", line)
292
+ self.set_node_and_entry("LineMode", line_mode)
293
+ self.set_node_and_entry("LineSource", "ExposureActive")
294
+
295
+ def reset(self, sleep: float = 10.0):
296
+ device_reset = PySpin.CCommandPtr(self.c.GetNodeMap().GetNode("DeviceReset"))
297
+ device_reset.Execute()
298
+ self.close()
299
+ time.sleep(sleep)
300
+
301
+ def start(self):
302
+ self.c.BeginAcquisition()
303
+
304
+ def stop(self):
305
+ try:
306
+ self.c.EndAcquisition() # not sure this works if BeginAcquistion has not been called
307
+ except:
308
+ pass
309
+
310
+ def close(self):
311
+ self.stop() # should check if cam started
312
+ self.c.DeInit()
313
+ del self.c
314
+ self.cam_list.Clear()
315
+ del self.cam_list
316
+ # self.cam_system.ReleaseInstance()
317
+ # del self.cam_system
@@ -0,0 +1,167 @@
1
+ import time
2
+ import numpy as np
3
+ from typing import Tuple
4
+ from .base import BaseCam, gray2rgb
5
+
6
+ try:
7
+ from ximea import xiapi
8
+ ximea_error = None
9
+ except ImportError as ximea_error:
10
+ pass
11
+
12
+
13
+ class Ximea(BaseCam):
14
+
15
+ NAME = "XIM"
16
+
17
+ def __init__(self, serialnumber):
18
+ if ximea_error is not None:
19
+ raise ximea_error
20
+ self.serialnumber = serialnumber
21
+ self.timestamp_offset = 0
22
+ self.im = xiapi.Image()
23
+ # create instance for first connected camera
24
+
25
+ def init(self):
26
+ self.c = xiapi.Camera()
27
+ self.c.open_device_by_SN(self.serialnumber)
28
+ # HARDCODED
29
+ self.c.set_downsampling("XI_DWN_2x2")
30
+ self.c.set_downsampling_type("XI_SKIPPING")
31
+
32
+ self.c.set_imgdataformat("XI_MONO8")
33
+ self.c.set_limit_bandwidth(self.c.get_limit_bandwidth_maximum())
34
+ self.timestamp_offset = self._estimate_timestamp_offset()
35
+
36
+ def get(self, timeout=None):
37
+ self.c.get_image(self.im) # get buffer - this blocks until an image is acquired
38
+ system_timestamp = time.time()
39
+ image_timestamp = self.im.tsSec * 1e9 + self.im.tsUSec * 1e3
40
+ image_timestamp = image_timestamp / 1e9 + self.timestamp_offset
41
+
42
+ image = self.im.get_image_data_numpy()
43
+ image = gray2rgb(image)
44
+
45
+ return image, image_timestamp, system_timestamp
46
+
47
+ def _min_max_inc(self, prop, value=None, set_value=True):
48
+ min_val, max_val, inc = (
49
+ self.c.get_param(prop + ":min"),
50
+ self.c.get_param(prop + ":max"),
51
+ self.c.get_param(prop + ":inc"),
52
+ )
53
+
54
+ prop_type = type(self.c.get_param(prop))
55
+ if value is not None:
56
+ value = np.clip(value, min_val, max_val)
57
+ value = np.round(value / inc) * inc
58
+ if set_value:
59
+ value = prop_type(value)
60
+ self.c.set_param(prop, value)
61
+ value = self.c.get_param(prop)
62
+ return value
63
+
64
+ def _estimate_timestamp_offset(self, timestamp_offset_iterations=30):
65
+ """Gets offset between system time and timestamps."""
66
+ # This method is required because the timestamp stored in the camera is relative to when it was powered on, so an
67
+ # offset needs to be applied to get it into epoch time; from tests I’ve done, this appears to be accurate to ~1e-3
68
+ # seconds.
69
+ timestamp_offsets = []
70
+ tmp = self.framerate
71
+ self.framerate = 30
72
+ self.start()
73
+ for _ in range(timestamp_offset_iterations):
74
+ # timestamp = self.c.get_timestamp() # does not work for some reason
75
+ # system_time = time.time()
76
+ self.c.get_image(self.im) # get buffer - this blocks until an image is acquired
77
+ system_ts = time.time()
78
+ image_ts = self.im.tsSec * 1e9 + self.im.tsUSec * 1e3
79
+ image_ts = image_ts / 1e9
80
+ timestamp_offset = system_ts - image_ts
81
+ timestamp_offsets.append(timestamp_offset)
82
+ self.stop()
83
+ self.framerate = tmp
84
+ # Return the median value
85
+ return np.median(timestamp_offsets)
86
+
87
+ @property
88
+ def roi(self):
89
+ return self.c.get_offsetX(), self.c.get_offsetY(), self.c.get_width(), self.c.get_height()
90
+
91
+ @roi.setter
92
+ def roi(self, x0_y0_x_y: Tuple[int, int, int, int]):
93
+ try:
94
+ x0, y0, x, y = x0_y0_x_y
95
+ self.c.set_width(16)
96
+ self.c.set_height(16)
97
+ self.c.set_offsetX(x0)
98
+ self.c.set_offsetY(y0)
99
+ self.c.set_width(x)
100
+ self.c.set_height(y)
101
+ except ValueError:
102
+ raise ValueError("Need 4-tuple with x0_y0_x_y")
103
+ else:
104
+ self._min_max_inc("offsetX", x0)
105
+ self._min_max_inc("offsetY", y0)
106
+ self._min_max_inc("width", x)
107
+ self._min_max_inc("height", y)
108
+
109
+ @property
110
+ def exposure(self):
111
+ return self.c.get_exposure()
112
+
113
+ @exposure.setter
114
+ def exposure(self, value: float):
115
+ """Set exposure/shutter time in WHICH UNITS? ns or ms?."""
116
+ self.c.disable_aeag()
117
+ self.c.set_exposure(float(value))
118
+
119
+ @property
120
+ def framerate(self):
121
+ return self.c.get_framerate()
122
+
123
+ @framerate.setter
124
+ def framerate(self, value: float):
125
+ self.c.set_acq_timing_mode("XI_ACQ_TIMING_MODE_FRAME_RATE")
126
+ self.c.set_framerate(float(value))
127
+
128
+ @property
129
+ def gamma(self):
130
+ return self.c.get_gammaY()
131
+
132
+ @gamma.setter
133
+ def gamma(self, value: float):
134
+ self.c.set_gammaY(float(value))
135
+
136
+ @property
137
+ def gain(self):
138
+ return self.c.get_gain()
139
+
140
+ @gain.setter
141
+ def gain(self, value: float):
142
+ self.c.disable_aeag()
143
+ self.c.set_gain(float(value))
144
+
145
+ @property
146
+ def brightness(self):
147
+ return None
148
+
149
+ @brightness.setter
150
+ def brightness(self, value: float):
151
+ pass
152
+
153
+ def start(self):
154
+ self.c.start_acquisition()
155
+
156
+ def stop(self):
157
+ try:
158
+ self.c.stop_acquisition()
159
+ except:
160
+ pass
161
+
162
+ def close(self):
163
+ self.stop()
164
+ self.c.close_device()
165
+
166
+ def reset(self, sleep=None):
167
+ pass