-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLightField.py
More file actions
365 lines (317 loc) · 14.8 KB
/
Copy pathLightField.py
File metadata and controls
365 lines (317 loc) · 14.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
# -*- coding: utf-8 -*-
"""
Created on Thu May 21 10:31:21 2020
@author: Michael Schneider, Max Born Institut Berlin
"""
import ctypes
import sys
import os
import clr
sys.path.append(os.environ['LIGHTFIELD_ROOT'])
sys.path.append(os.environ['LIGHTFIELD_ROOT'] + '\\AddInViews')
clr.AddReference('PrincetonInstruments.LightFieldViewV5')
clr.AddReference('PrincetonInstruments.LightField.AutomationV5')
clr.AddReference('PrincetonInstruments.LightFieldAddInSupportServices')
clr.AddReference('System.Collections')
from PrincetonInstruments.LightField.Automation import Automation
from PrincetonInstruments.LightField.AddIns import CameraSettings as cs
from PrincetonInstruments.LightField.AddIns import ExperimentSettings as es
from PrincetonInstruments.LightField.AddIns import DeviceType
from PrincetonInstruments.LightField.AddIns import ImageDataFormat
from PrincetonInstruments.LightField.AddIns import RegionOfInterest
from System.Runtime.InteropServices import GCHandle, GCHandleType
from System import String
from System.Collections.Generic import List
import numpy as np
import tango
from tango import DevState, Attr, READ, READ_WRITE
from tango.server import Device, command, attribute
class LightFieldCamera(Device):
# list of simple scalar controls that can be created automatically
# required fields: name, access, dtype, lf
# `lf` is the LightField settingName
DYN_ATTRS = [
# camera settings
dict(name='temp_read', label='sensor temperature', access=READ,
dtype=tango.DevFloat, unit='degC', lf=cs.SensorTemperatureReading),
dict(name='temp_set', label='temperature setpoint', access=READ_WRITE,
dtype=tango.DevFloat, unit='degC', lf=cs.SensorTemperatureSetPoint),
# FIXME: this should be a DevEnum, which is currently bugged in
# dynamic creation: https://github.com/tango-controls/pytango/pull/348
dict(name='temp_status', label='temperature locked', access=READ,
dtype=tango.DevLong, lf=cs.SensorTemperatureStatus,
enum_labels=['invalid', 'unlocked', 'locked', 'fault']),
# FIXME: DevEnum
dict(name='shutter_mode', label='shutter mode', access=READ_WRITE,
dtype=tango.DevLong, lf=cs.ShutterTimingMode,
enum_labels=['invalid', 'normal', 'closed', 'open', 'trigger']),
dict(name='shutter_close', label='shutter closing time', access=READ_WRITE,
dtype=tango.DevFloat, unit='ms', lf=cs.ShutterTimingClosingDelay),
dict(name='exposure', label='exposure time', access=READ_WRITE,
dtype=tango.DevFloat, unit='ms', lf=cs.ShutterTimingExposureTime),
dict(name='n_ports', label='readout ports', access=READ_WRITE,
dtype=tango.DevLong, lf=cs.ReadoutControlPortsUsed),
dict(name='adc_speed', label='ADC speed', access=READ_WRITE,
dtype=tango.DevFloat, lf=cs.AdcSpeed, unit='MHz'),
# experiment settings
dict(name='accumulations', label='number of acquisitions per frame',
access=READ_WRITE, dtype=tango.DevLong,
lf=es.OnlineProcessingFrameCombinationFramesCombined),
dict(name='save_folder', label='data folder', access=READ_WRITE,
dtype=tango.DevString, lf=es.FileNameGenerationDirectory),
dict(name='save_base', label='base name', access=READ_WRITE,
dtype=tango.DevString, lf=es.FileNameGenerationBaseFileName),
dict(name='save_index', label='file index', access=READ_WRITE,
dtype=tango.DevLong, lf=es.FileNameGenerationIncrementNumber,
min_value='0'),
dict(name='save_digits', label='index length', access=READ_WRITE,
dtype=tango.DevLong, min_value='1', max_value='10',
lf=es.FileNameGenerationIncrementMinimumDigits),
dict(name='orient_on', label='apply image orientatiation',
access=READ_WRITE, dtype=tango.DevBoolean,
lf=es.OnlineCorrectionsOrientationCorrectionEnabled),
dict(name='orient_hor', label='flip horizontally',
access=READ_WRITE, dtype=tango.DevBoolean,
lf=es.OnlineCorrectionsOrientationCorrectionFlipHorizontally),
dict(name='orient_ver', label='flip vertically',
access=READ_WRITE, dtype=tango.DevBoolean,
lf=es.OnlineCorrectionsOrientationCorrectionFlipVertically),
dict(name='orient_rot', label='rotate 90 degree',
access=READ_WRITE, dtype=tango.DevLong,
lf=es.OnlineCorrectionsOrientationCorrectionRotateClockwise),
]
attr_keys = {d['name']: d['lf'] for d in DYN_ATTRS}
image = attribute(name='image', label='CCD image', max_dim_x=4096,
max_dim_y=4096, dtype=((tango.DevFloat,),), access=READ)
chip_shape = attribute(name='chip_shape', label='pixel size of the sensor',
access=READ, dtype=(int,), max_dim_x=4)
def init_device(self):
Device.init_device(self)
self.set_change_event('image', True, False)
self.set_state(DevState.INIT)
self.lf = Automation(True, List[String]()) # starts LF instance
self.exp = self.lf.LightFieldApplication.Experiment
self.device = self.get_camera_device()
if self.device is not None:
self.set_state(DevState.ON)
name, model, sn, shape = self.get_sensor_info()
print('Connected:', model, name, sn, file=self.log_info)
self._image = np.zeros(shape)
self._chip_shape = shape
self._sensorshape = shape
self._accum = 0
self.register_events()
self.setup_file_save()
else:
print('No camera found.', file=self.log_error)
self.set_state(DevState.FAULT)
def initialize_dynamic_attributes(self):
for d in self.DYN_ATTRS:
self.make_attribute(d)
def make_attribute(self, attr_dict):
'''Dynamically generate simple attributes for LightField settings.'''
baseprops = ['name', 'dtype', 'access', 'lf']
name, dtype, access, lf = [attr_dict.pop(k) for k in baseprops]
if self.exp.Exists(lf):
print('making attribute', name, file=self.log_debug)
new_attr = Attr(name, dtype, access)
prop = tango.UserDefaultAttrProp()
for k, v in attr_dict.items():
try:
property_setter = getattr(prop, 'set_' + k)
property_setter(v)
except AttributeError:
print("error setting attribute property:", name, k, v,
file=self.log_error)
new_attr.set_default_properties(prop)
self.add_attribute(
new_attr,
r_meth=self.read_general,
w_meth=self.write_general,
)
else:
print(f'Skipping attribute {name}: Does not exist on this device',
file=self.log_warn)
def setup_file_save(self):
'''Make sure that file save options are correct.'''
self.lightfield_set(es.FileNameGenerationAttachDate, False)
self.lightfield_set(es.FileNameGenerationAttachTime, False)
self.lightfield_set(es.FileNameGenerationAttachIncrement, True)
return
def get_sensor_info(self):
'''Query the sensor name, model, serial number and active area.'''
width = self.lightfield_get(cs.SensorInformationActiveAreaWidth)
height = self.lightfield_get(cs.SensorInformationActiveAreaHeight)
name = self.lightfield_get(cs.SensorInformationSensorName)
model = self.device.Model
serial = self.device.SerialNumber
return name, model, serial, (height, width)
def get_camera_device(self):
'''Returns the first registered camera device.'''
for device in self.exp.ExperimentDevices:
if device.Type == DeviceType.Camera:
return device
return None
def lightfield_set(self, key, value):
if not self.exp.IsRunning:
if self.exp.IsValid(key, value):
self.exp.SetValue(key, value)
print(f'set {key} -> {value}', file=self.log_debug)
else:
print(f'invalid setting: {key}->{value}', file=self.log_error)
else:
print(f'Cannot set {key}: acquiring', file=self.log_warn)
def lightfield_get(self, key):
val = self.exp.GetValue(key)
return val
def read_general(self, attr):
key = self.attr_keys[attr.get_name()]
print('reading', key, file=self.log_debug)
attr.set_value(self.lightfield_get(key))
def write_general(self, attr):
key = self.attr_keys[attr.get_name()]
val = attr.get_write_value()
print('setting', key, '->', val, file=self.log_debug)
self.lightfield_set(key, val)
def next_file_exists(self):
'''Check whether the next file name is available.'''
folder = self.lightfield_get(self.attr_keys['save_folder'])
fname = self.lightfield_get(es.FileNameGenerationExampleFileName)
fpath = os.path.join(folder, fname + '.spe')
return os.path.exists(fpath)
def increment_to_next_free(self):
'''
Make sure next file name is avilable by incrementing the file index.
'''
while self.next_file_exists():
index = self.lightfield_get(self.attr_keys['save_index'])
print('file exists! Incrementing index.', file=self.log_warn)
self.lightfield_set(self.attr_keys['save_index'], index + 1)
def read_image(self):
return self._image
def read_chip_shape(self):
return self._chip_shape
@command(dtype_in=int)
def set_binning(self, N):
'''Sets the camera to full chip binning mode.
Use the `set_roi` command to setup a region of interest with binning.
'''
if not self.exp.IsRunning:
if N > 1:
self.exp.SetBinnedSensorRegion(N, N)
self._imshape = [int(npx // N) for npx in self._sensorshape]
print(f'full chip binning {N}x{N}', file=self.log_debug)
else:
self.exp.SetFullSensorRegion()
self._imshape = self._sensorshape
print('full chip unbinned', file=self.log_debug)
@command(dtype_in=(int,), doc_in='list of ints [x0, x1, y0, y1, bin]',
dtype_out=bool, doc_out='True if successful')
def set_roi(self, roi):
'''Sets the camera to a (possibly binned) ROI.
input is a list of ints [x0, x1, y0, y1, binning]
binning will be set to one if not specified
'''
if not self.exp.IsRunning:
if len(roi) == 4:
x0, x1, y0, y1 = roi
N = 1
elif len(roi) > 4:
x0, x1, y0, y1, N = roi
else:
print('cannot understand ROI', file=self.log_error)
return False
region = RegionOfInterest(x0, y0, x1 - x0, y1 - y0, N, N)
self._imshape = [int((x1 - x0) // N), int((y1 - y0) // N)]
self.exp.SetCustomRegions((region,))
print('set custom ROI', file=self.log_debug)
return True
else:
print('Cannot set ROI during acquisition', file=self.log_error)
return False
@command(dtype_out=(int,),
doc_out='get width and height of the currently active ROI')
def get_roi_size(self):
'''Return image size for the current ROI settings.
As some hardware supports separate non-contiguous regions in a single
ROI, this returns the following numbers for each region in a flattened
list: `[offsetX, offsetY, width, height, binX, binY]`.'''
rois = self.exp.SelectedRegions
roi_size = []
for roi in rois:
roi_size += [roi.X, roi.Y, roi.Width, roi.Height, roi.XBinning, roi.YBinning]
return roi_size
@command
def acquire(self):
self.increment_to_next_free()
if self.exp.IsReadyToRun:
self._image = 0
self._accum = 0
self._preview = False
self.exp.Acquire()
@command
def stop(self):
self.exp.Stop()
@command
def preview(self):
self.increment_to_next_free()
if self.exp.IsReadyToRun:
self._preview = True
self.exp.Preview()
def handler_new_data(self, sender, event_args):
data = event_args.ImageDataSet
if data.Frames > 0:
frame = data.GetFrame(0, 0)
im = imageframe_to_numpy(frame).astype(np.float32)
if not self._preview:
self._image = ((self._image * self._accum) + im) / (self._accum + 1)
self._accum += 1
else:
self._image = im
dim_x, dim_y = self._image.shape
print('new image:', self._image.shape, file=self.log_info)
self.push_change_event('image', self._image, dim_y, dim_x)
else:
print('no frames:', data.Frames, file=self.log_error)
def handler_acq_finished(self, sender, event_args):
self.set_state(DevState.ON)
def handler_acq_start(self, sender, event_args):
self.set_state(DevState.RUNNING)
def handler_lightfield_close(self, sender, event_args):
self.set_state(DevState.OFF)
def register_events(self):
self.exp.ExperimentStarted += self.handler_acq_start
self.exp.ExperimentCompleted += self.handler_acq_finished
self.lf.LightFieldClosed += self.handler_lightfield_close
self.exp.ImageDataSetReceived += self.handler_new_data
def imageframe_to_numpy(frame):
'''
Retrieve data from LightField DisplaySource.
Parameters
----------
frame :
LightField display source. Could be the live view or a loaded file.
Returns
-------
data
numpy array.
'''
buffer = frame.GetData()
image_format = frame.Format
src_hndl = GCHandle.Alloc(buffer, GCHandleType.Pinned)
try:
src_ptr = src_hndl.AddrOfPinnedObject().ToInt64()
# Possible data types returned from acquisition
dtypes = {ImageDataFormat.MonochromeUnsigned16: ctypes.c_ushort,
ImageDataFormat.MonochromeUnsigned32: ctypes.c_uint,
ImageDataFormat.MonochromeFloating32: ctypes.c_float}
buf_type = dtypes[image_format] * len(buffer)
cbuf = buf_type.from_address(src_ptr)
image = np.frombuffer(cbuf, dtype=cbuf._type_).copy()
image = np.rot90(image.reshape(frame.Height, frame.Width), -1).T
finally:
if src_hndl.IsAllocated:
src_hndl.Free()
return image
if __name__ == '__main__':
LightFieldCamera.run_server()