-
Notifications
You must be signed in to change notification settings - Fork 2
/
Scanner.py
987 lines (891 loc) · 32.9 KB
/
Scanner.py
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
from PyDAQmx import *
import numpy
import matplotlib.pyplot as plt
import random
import time
from pyflycam import *
from qupsi import *
#################################################################################
#container class for holding attributes
class EmptyObject:
pass
#helper class for callable list
class CallableList(list):
def __call__(self):
for entry in self:
entry()
#helper class for Sample size
class Size:
def __init__(self, height, width):
self.height = height
self.width = width
#override multiplication
def __mul__(self, other):
self.width *= other
self.height *= other
return self
#override division
def __div__(self, other):
self.width /=other
self.height /=other
return self
#Helper class for storing Lens informatios and to calculate the focal length corresponding to the NA
class Lens:
def __init__(self, NA, n):
self.NA = NA
self.n = n
def LensNumber(self):
return 1/numpy.tan(numpy.arcsin(self.NA/self.n))
#########################################################################################
################################################################
#Exceptions which occur while scanning
class AngleOutsideOfRangeException(Exception):
pass
class VoltageCannotBeNegativeException(Exception):
pass
class ConfigFileNotFoundException(Exception):
pass
#################################################################
def scannerObjects(dct):
if "_sample_size_" in dct:
return Size(dct["height"], dct["width"])
if "_eval_" in dct:
if "libraries" in dct:
for lib in dct["libraries"]:
exec("import " + str(lib))
return eval(dct["expression"])
return dct
class Scanner:
#scanner class: needs sampleSize (to calculate the max and min angles for the galvo) and
# the distance from the galvo to the lens
#sensitivity of the galvo in volt per rad
sensitivityRad = 90.0/numpy.pi
sensitivityDeg = 0.5
# arguments: all units in mm, devicePhi for Xtranslation, devicetheta for Ytranslation
def __init__(self, sampleSize = None,beamDiameter = 5, lens = Lens(1.3,1.5),inputDevice="Dev2/ai1", devicePhi = "Dev2/ao1", deviceTheta = "Dev2/ao0", configFile = "scanner_config.cfg"):
#local variables rerpresenting the sate of the scanner
self.testData = []
self.currentX = 0
self.currentY = 0
self.currentVoltagePhi = 0
self.currentVoltageTheta = 0
self.currentPiezoVoltage = 0
self.sampleSize = sampleSize
self.currentGalvoPhi = 0
self.currentGalvoTheta = 0
self.lens = lens
self.calibrationPhi = 0
self.calibrationTheta = 0
self.devicePhi = devicePhi
self.deviceTheta = deviceTheta
self.inputDevice = inputDevice
self.autoscale = True
self.hbtLoop = False
self.baseVoltage = 5
self.currentXCoord = 0
self.currentYCoord = 0
self.sigToBack = 0.5
self.doNormalization = False
self.autocorrection = False
self.quadSize = 3
self.noCheckForMax = True
self.startPoint = None
self.correctionFactor = (0,0)
#accept any device
TDC_init(-1)
#enable all channels
TDC_enableChannels(0xff)
#enable hbt
TDC_enableHbt(True)
#enable start stop
TDC_enableStartStop(True)
#exposure time in ms
self.exposureTime = 1
TDC_setExposureTime(self.exposureTime)
TDC_clearAllHistograms()
#the calibration values, read them from the config file
import json
import os.path
if os.path.isfile(configFile):
self._config = json.loads(open(configFile).read(), object_hook=scannerObjects)
else:
self._config = {}
self._config["settings"] = {}
if "settings" in self._config:
#foreach import config load the variables (same names get overwritten)
if "imports" in self._config:
for cfgfile in self._config["imports"]:
self.loadConfig(cfgfile)
for key in self._config["settings"]:
setattr(self, key, self._config["settings"][key])
#max and min x are half the sample size since we place the sample in such a way that it is centered arround the
#origin of lens
self.maxX = self.sampleSize.width/2.0
self.minX = -self.sampleSize.width/2.0
self.maxY = self.sampleSize.height / 2.0
self.minY = -self.sampleSize.height / 2.0
if not hasattr(self, 'xsteps'):
self.xsteps = numpy.linspace(0, 0.05, 500)
if not hasattr(self, 'ysteps'):
self.ysteps = numpy.linspace(0,0.05, 500)
self.dataArray = numpy.ones((len(self.ysteps),len(self.xsteps)), dtype=numpy.float64)
#prepare the output channels
try:
self.analog_output = Task()
self.analog_output.CreateAOVoltageChan(",".join([self.devicePhi, self.deviceTheta, self.piezoDevice]),"",-10.0,10.0,DAQmx_Val_Volts,None)
#self.analog_output.CreateAOVoltageChan(deviceTheta,"",-10.0,10.0,DAQmx_Val_Volts,None)
self.analog_output.CfgSampClkTiming("",10000.0,DAQmx_Val_Rising,DAQmx_Val_ContSamps,100)
self.analog_input = Task()
self.analog_input.CreateAIVoltageChan(self.inputDevice, "", DAQmx_Val_Cfg_Default, -10.0,10.0,DAQmx_Val_Volts, None)
self.analog_input.CfgSampClkTiming("",10000.0,DAQmx_Val_Rising,DAQmx_Val_ContSamps,100)
except(Exception):
print("Could not init DaqMX")
self.initCamera()
#if we have a focus point set it
#init the piezo to full focal
self.setFocus(0)
if hasattr(self, "focus"):
self.setFocus(self.focus)
if(hasattr(self, "imageSettings")):
self.setImageProperties(self.imageSettings['gain'], self.imageSettings['shutter'])
time.sleep(2)
#load config file
def loadConfig(self, configFile="scanner_config.cfg", focus=None):
import json
import os.path
if os.path.isfile(configFile):
self._config = json.loads(open(configFile).read(), object_hook=scannerObjects)
else:
raise(ConfigFileNotFoundException)
if "settings" in self._config:
for key in self._config["settings"]:
setattr(self, key, self._config["settings"][key])
if hasattr(self, "focus"):
self.setFocus(self.focus)
if focus is not None:
print("set focus")
focus.set(self.focus)
self.dataArray = numpy.ones((len(self.ysteps),len(self.xsteps)), dtype=numpy.float64)
#setImage properties
def setImageProperties(self, gain=0.0, shutter=10.0):
gainProp = fc2Property()
shutterProp = fc2Property()
gainProp.type = FC2_GAIN
shutterProp.type = FC2_SHUTTER
#retrieve current settings
fc2GetProperty(self._context, gainProp)
fc2GetProperty(self._context, shutterProp)
gainProp.absValue = gain
shutterProp.absValue = shutter
fc2SetProperty(self._context, gainProp)
fc2SetProperty(self._context, shutterProp)
def startScanhook(self, hook):
getattr(self, hook)()
def findMaximumX(self, oldMax, step=0.0002):
#we try to find the new maximum in all three dims
#first move in x
tmpB = c_int *19
tmpBuffer = tmpB()
tmpX = self.currentX
max = oldMax
self.setX(tmpX + step)
TDC_getCoincCounters(tmpBuffer)
countsA = numpy.sum(tmpBuffer)/0.032
#try to go a step back
self.setX(tmpX - step)
TDC_getCoincCounters(tmpBuffer)
countsB = numpy.sum(tmpBuffer)/0.032
diff = countsA-countsB
print("diff is: ", diff)
if abs(diff) > 1.5 *numpy.sqrt(oldMax):
#so go half the step size to the right
if diff < 0:
self.setX(tmpX - step/2.0)
else:
self.setX(tmpX + step / 2.0)
else:
return oldMax
time.sleep(0.032)
TDC_getCoincCounters(tmpBuffer)
counts = numpy.sum(tmpBuffer)/0.032
max = self.findMaximumX(counts, step=step/2.0)
#we did not find any maximum go back to origin
tmpB = None
tmpBuffer = None
return max
def findMaximumY(self, oldMax, step=0.0002):
#we try to find the new maximum in all three dims
#first move in x
tmpB = c_int *19
tmpBuffer = tmpB()
tmpY = self.currentY
max = oldMax
self.setY(tmpY + step)
TDC_getCoincCounters(tmpBuffer)
countsA = numpy.sum(tmpBuffer)/0.032
#try to go a step back
self.setY(tmpY - step)
TDC_getCoincCounters(tmpBuffer)
countsB = numpy.sum(tmpBuffer)/0.032
diff = countsA-countsB
if abs(diff) > 1.5 *numpy.sqrt(oldMax):
#so go half the step size to the right
if diff < 0:
self.setY(tmpY - step/2.0)
else:
self.setY(tmpY + step / 2.0)
else:
return oldMax
time.sleep(0.032)
TDC_getCoincCounters(tmpBuffer)
counts = numpy.sum(tmpBuffer)/0.032
max = self.findMaximumY(counts, step=step/2.0)
#we did not find any maximum go back to origin
tmpB = None
tmpBuffer = None
return max
#we did not find any maximum go back to origin
self.setY(tmpY)
print("max and oldmax are the same", oldMax, max)
tmpB = None
tmpBuffer = None
return oldMax
def findMax(self):
#lets start finding the maximum
tmpB = c_int *19
tmpBuffer = tmpB()
TDC_getCoincCounters(tmpBuffer)
counts = numpy.sum(tmpBuffer)
newmax = self.findMaximumX(counts)
newmax = self.findMaximumY(newmax)
tmpB = None
tmpBuffer = None
def callbackFactory(self, callback, args):
return lambda: getattr(self, callback.strip())(args)
def parseHook(self, hookFile):
keywords = { }
tmpHookName = hookFile
#compile a regular expression, so that we have fname(**args) and then call the appropriate function
#or match variable declarations var = value
import re
functionCallPattern = re.compile("\w+\s*\([\s\w\/\,\.\_\-\!\$\?]*\)")
assignPattern = re.compile("\w+\s*=\s*\w+")
tmpObject = EmptyObject()
body = CallableList()
for line in open(hookFile).readlines():
match = functionCallPattern.search(line)
if match is not None:
#we got a function call, so split at the first bracket
index = match.group().find("(")
if index > -1:
functionName = match.group()[:index]
if hasattr(self, functionName):
arguments = match.group()[index+1:-1]
#call function if it is a class funcion
if(arguments == ""):
args = None
else:
args = arguments.split(",")
#print(arguments)
if args is not None:
if functionName == "plot3dmap":
body += [self.callbackFactory(functionName, args)]
else:
body += [self.callbackFactory(functionName, *args)]
else:
body += [getattr(self, functionName)]
else:
#see if we have defined a own call
print(functionName)
if functionName in keywords:
print("custom function")
arguments = match.group()[index+1:-1]
#call function if it is a class funcion
args = ",".join(arguments)
body += [lambda: keywords[functionName](args)]
match = assignPattern.search(line)
print(match)
if match is not None:
#we have a assignment
name, value = match.group().split("=")
setattr(tmpObject, name.strip(), value.strip())
if hasattr(tmpObject, "name"):
tmpHookName = getattr(tmpObject, "name")
setattr(self, tmpHookName, body)
return tmpHookName
#get state of galvo -> return angle in degree
def getAnglePhiDegree(self):
return self.currentGalvoPhi * (180./numpy.pi)
def getAngleThetaDegree(self):
return self.currentGalvoTheta * (180./numpy.pi)
def stopScan(self):
self.interrupt = True
#set Voltage directly
def setVoltagePhi(voltage):
data = numpy.zeros((200,), dtype=numpy.float64)
data[:99] = voltage
data[99:] = self.currentVoltageTheta
#set the state of the object
self.currentVoltagePhi = voltage
self.currentGalvoPhi = phi
#write to the output channel
self.analog_output.WriteAnalogF64(100,False,-1,DAQmx_Val_GroupByChannel ,data,None,None)
self.analog_output.StartTask()
time.sleep(0.0001)
self.analog_output.StopTask()
def setVoltageTheta(voltage):
data = numpy.zeros((200,), dtype=numpy.float64)
data[:99] = self.currentVoltagePhi
data[99:] = voltage
#set the state of the object
self.currentVoltageTheta = voltage
self.currentGalvoTheta = theta
#write to the output channel
self.analog_output.WriteAnalogF64(100,False,-1,DAQmx_Val_GroupByChannel ,data,None,None)
self.analog_output.StartTask()
#time.sleep(0.0001)
self.analog_output.StopTask()
def calibrate():
self.calibrationPhi = self.currentVoltagePhi
self.currentVoltagePhi = 0
self.calibrationTheta = self.currentVoltageTheta
self.currentVoltageTheta = 0
self.currentGalvoTheta = 0
self.currentGalvoPhi = 0
self.currentX = 0
self.currentY = 0
def setFocus(self, voltage):
if self.baseVoltage - voltage < 0:
raise(VoltageCannotBeNegativeException)
v = self.baseVoltage - voltage
data = numpy.zeros((300,), dtype=numpy.float64)
data[:100] = self.currentVoltagePhi
data[100:200] = self.currentVoltageTheta
data[200:] = v
#set the state of the object
self.currentPiezoVoltage = v
#write to the output channel
self.analog_output.WriteAnalogF64(100,False,-1,DAQmx_Val_GroupByChannel ,data,None,None)
self.analog_output.StartTask()
self.analog_output.StopTask()
#setAngles for the galvo (enter values in degree), private: use setX and setY for public access
def __setPhi(self, phi):
voltage = self.sensitivityDeg * phi + self.calibrationPhi
data = numpy.zeros((300,), dtype=numpy.float64)
data[:100] = voltage
data[100:200] = self.currentVoltageTheta
data[200:] = self.currentPiezoVoltage
self.testData = data
#set the state of the object
self.currentVoltagePhi = voltage
self.currentGalvoPhi = phi
#write to the output channel
self.analog_output.WriteAnalogF64(100,False,-1,DAQmx_Val_GroupByChannel ,data,None,None)
self.analog_output.StartTask()
self.analog_output.StopTask()
def __setPhiRad(self, phiRad):
self.__setPhi(180./numpy.pi * phiRad)
def __setTheta(self, theta):
voltage = self.sensitivityDeg * theta + self.calibrationTheta
data = numpy.zeros((300,), dtype=numpy.float64)
data[:100] = self.currentVoltagePhi
data[100:200] = voltage
data[200:] = self.currentPiezoVoltage
self.testData = data
#set the state of the object
self.currentVoltageTheta = voltage
self.currentGalvoTheta = theta
#write to the output channel
self.analog_output.WriteAnalogF64(100,False,-1,DAQmx_Val_GroupByChannel ,data,None,None)
self.analog_output.StartTask()
#time.sleep(0.0001)
self.analog_output.StopTask()
def __setThetaRad(self, thetaRad):
self.__setTheta(180./numpy.pi * thetaRad)
def ReleaseObjects(self):
self.analog_output.StopTask()
self.analog_output.ClearTask()
self.analog_input.StopTask()
self.analog_input.ClearTask()
self.uninitCamera()
TDC_deInit()
#units are mm: set x and y according to angle and sampledistance x and y is relative to the sample, so it is the
#where the laser beam will hit the target
#-the incident angle on the lens will be phi as well
#-the formula for s(alpha_i) = (beamDiameter/2.) *
def setX(self, X, incremental=False):
#check if we are on the sample
if X < self.minX or X > self.maxX:
#raise(AngleOutsideOfRangeException)
pass
#set the angle of the galvo
if incremental:
self.setX(self.currentX + X)
else:
self.__setPhiRad(numpy.arctan(X * self.lens.LensNumber()))
#set the state of the situation
self.currentX = X
def setY(self, Y, incremental=False):
#check if we are on the sample
if Y < self.minY or Y > self.maxY:
#raise(AngleOutsideOfRangeException)
pass
#set the angle of the galvo
if incremental:
self.setY(self.currentY + Y)
else:
self.__setThetaRad(numpy.arctan(Y * self.lens.LensNumber()))
#set the state of the situation
self.currentY = Y
def setPoint(self, x, y, directly=False):
self.setX(x if not directly else x + self.correctionFactor[0])
self.setY(y if not directly else y + self.correctionFactor[1])
if self.correctionFactor is not None:
print("Correction factor: x -> %f, y -> %f"%(self.correctionFactor[0], self.correctionFactor[1]))
def saveState(self, name="tmpArray"):
numpy.save(name, self.dataArray)
numpy.savetxt(name+".csv", self.dataArray, delimiter=',')
if self.histoData is not None:
numpy.save(name+"_histo_", self.histoData)
numpy.savetxt(name+"_histo_"+".csv", self.histoData)
def goTo(self, x, y, directly=False):
self.currentXCoord = x
self.currentYCoord = y
self.setPoint( self.xsteps[int(x)], self.ysteps[int(y)], directly)
def getGoToX(self,x):
return self.xsteps[int(x)]
def getGoToY(self, y):
return self.ysteps[int(y)]
def showHistogram(self):
plt.clf()
plt.scatter(self.dataArray)
plt.hist2d(self.dataArray)
def plot3dmap(self, data, maskvalue=50000, multiple=False, fig=None):
plt.clf()
if len(data) <= 0:
return
zlayers = []
for entry in data:
#read each file and load it
zlayers += [numpy.load(entry)]
#prepare the x-axes (since we have a rectangle we have height times the same x value)
xdim = zlayers[0].shape[0]
ydim = zlayers[0].shape[1]
zdim = len(zlayers)
x_ = numpy.linspace(1,xdim, xdim)
y_ = numpy.linspace(1,ydim, ydim)
z_ = numpy.linspace(1,zdim, zdim)
x,y,z = numpy.meshgrid(x_,y_,z_, indexing='ij')
vol = xdim * ydim * zdim
x = x.reshape(vol,)
y = y.reshape(vol,)
z = z.reshape(vol,)
c = numpy.array(zlayers)
c = c.reshape(xdim*ydim*zdim,order='F')
c = numpy.ma.masked_less(c, maskvalue)
print("shapes", x.shape, y.shape,z.shape, c.shape)
#now we have prepared our data lets plot
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
#if not multiple we get a figure object so add our plot as a add_subplot
if not multiple:
fig = plt.figure(1)
ax = fig.add_subplot(111, projection='3d')
sc = ax.scatter(x,y,z,c=c, cmap=plt.hot())
plt.colorbar(sc)
#if we have multiple ones, we dont want to show the plot
if not multiple:
plt.show()
plt.savefig("3dplot.jpeg")
def processMouseClick(self, event):
print("Mouse clicked at, ", event.xdata, event.ydata)
if self.interrupt and event.xdata is not None and event.ydata is not None:
print(self.currentX)
self.goTo(int(event.xdata) if event.xdata > 0 else 0, int(event.ydata) if event.ydata > 0 else 0)
print(self.noCheckForMax)
ax = event.inaxes
canvas = event.canvas
xfrom = int(event.xdata)-self.quadSize
xto = xfrom +self.quadSize*2.0
yfrom = int(event.ydata)-self.quadSize
yto = yfrom +self.quadSize*2.0
if hasattr(self, "up"):
self.up.pop(0).remove()
del self.up
if hasattr(self, "down"):
self.down.pop(0).remove()
del self.down
self.up = ax.plot((xfrom, xto), ((yfrom+yto)/2., (yfrom+yto)/2.), "w-")
self.down = ax.plot(((xfrom + xto)/2.,(xfrom + xto)/2.), (yfrom,yto),"w-")
ax.set_xlim([0,len(self.xsteps)-1])
ax.set_ylim([len(self.ysteps)-1,0])
canvas.draw()
#don't do it!
if self.noCheckForMax and False:
xfrom = max(int(event.xdata)-self.quadSize,0)
xto = min(xfrom +self.quadSize*2, len(self.xsteps))
yfrom = max(int(event.ydata)-self.quadSize,0)
yto = min(yfrom +self.quadSize*2, len(self.ysteps))
subarray = self.dataArray[yfrom:yto, xfrom:xto]
print(subarray, xfrom, xto, yfrom, yto)
m = numpy.argmax(subarray)
y,x = numpy.unravel_index(m, subarray.shape)
print(m, x, y)
#calculate real index
#TODO check consistency of x and y throughout class
x = xfrom + x
y = yfrom + y
print("(%f,%f) -> (%d,%d)"%(self.currentX, self.currentY, x,y))
self.goTo(y,x)
def plotCurrentRate(self, master=None, refToMain=None):
if master is not None:
try:
import tkinter as Tk
except ImportError:
import Tkinter as Tk
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
from matplotlib.figure import Figure
f = Figure(figsize=(3,1.5), dpi=100)
f.subplots_adjust(left=0.2)
fplt = f.add_subplot(111)
for item in ([fplt.title, fplt.xaxis.label, fplt.yaxis.label] +fplt.get_xticklabels() + fplt.get_yticklabels()):
item.set_fontsize(8)
try:
import Tkinter as tk
except ImportError:
import tkinter as tk
if refToMain is not None:
toolbar_frame = refToMain.createFrame(master)
else:
toolbar_frame = tk.Frame(master)
toolbar_frame.grid(row=4,column=4, columnspan=3, rowspan=3)
if refToMain is not None:
ratePlot = refToMain.createCanvas(f, toolbar_frame)
else:
ratePlot = FigureCanvasTkAgg(f, master=toolbar_frame)
ratePlot.show()
ratePlotWidget =ratePlot.get_tk_widget()
#register mouse callback to be able to navigate to
#f.canvas.mpl_connect('pick_event', self.processMouseClick)
ratePlotWidget.pack(side=tk.BOTTOM, fill=tk.BOTH, expand=True)
#add the toolbar
currentRate = []
t = []
tmpB = c_int *19
tmpBuffer = tmpB()
ratep = fplt.plot(t, currentRate)
fplt.set_xlim([0,100])
i=0
filled = False
import threading
while True:
ret = TDC_getCoincCounters(tmpBuffer)
if len(currentRate) > 100:
currentRate = currentRate[1:]
filled = True
dataSet = numpy.array(tmpBuffer)
currentRate += [numpy.sum(dataSet/(self.exposureTime/1000))]
if not filled:
t += [i]
i+=1
ratep[0].set_data(t,currentRate)
if not self.autoscale:
fplt.set_ylim([0, 200000])
else:
fplt.set_ylim([0, numpy.max(currentRate)])
f.canvas.draw()
time.sleep(self.exposureTime/1000)
#ratep[0].set_clim(numpy.min(currentRate), numpy.max(currentRate))
def showHBT(self, binWidth=1, binCount=20, master=None, refToMain=None):
self.hbtRunning = True
self.hbtLoop = True
#its irritating, binwidth is actually the TDC_timeBase Resolution, that means binWidth corresponds to the time in ns
if master is not None:
#import according to python version (2 or 3)
try:
import tkinter as Tk
except ImportError:
import Tkinter as Tk
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
TDC_enableHbt(True)
#we need to set binWidth according to TDC_timeBase
timeBase = TDC_getTimebase()
#time base is the resolution in seconds, so
rightBinWidth = int((binWidth*1.0e-9) / timeBase)
#first set histogram parameter
print(timeBase, rightBinWidth, binCount)
TDC_setHbtParams(rightBinWidth,binCount)
#set up array with at least binCount elements
from matplotlib.figure import Figure
histFig = Figure(figsize=(9,3), dpi=100)
histFig.subplots_adjust(left=0.2)
histAx = histFig.add_subplot(111)
for item in ([histAx.title, histAx.xaxis.label, histAx.yaxis.label] +histAx.get_xticklabels() + histAx.get_yticklabels()):
item.set_fontsize(8)
dataArray = numpy.zeros((binCount*2-1,))
t = numpy.linspace(-(binCount), binCount-1, 2*binCount-1)
if master is None:
plt.clf()
plt.ion()
print("I WANT DATA", len(t), len(dataArray))
self.histo = histAx.bar(t,dataArray)#, norm=LogNorm(vmin=100, vmax=1000000))
else:
print("I WANT DATA", len(t), len(dataArray))
#if the canvas exists just set the data
self.histo = histAx.bar(t,dataArray)#, norm=LogNorm(vmin=100, vmax=1000000))
#self.imgplot.set_data(self.dataArray)
if master is not None and not hasattr(self,"histoCanvas"):
#if the canvas is not allready shown show it
try:
import Tkinter as tk
except ImportError:
import tkinter as tk
if refToMain is not None:
toolbar_frame = refToMain.createFrame(master)
else:
toolbar_frame = tk.Frame(master)
toolbar_frame.grid(row=7,column=2, columnspan=7, rowspan=3)
if refToMain is not None:
histoCanvas = refToMain.createCanvas(histFig, toolbar_frame)
else:
histoCanvas = FigureCanvasTkAgg(histFig, master=toolbar_frame)
histoCanvas.show()
histoWidget =histoCanvas.get_tk_widget()
histoWidget.pack(side=tk.BOTTOM, fill=tk.BOTH, expand=True)
#for normalization we need the integration time
startTime = time.time()
hbtFunction = TDC_createHbtFunction()
self.signalCorrection = False
while self.hbtLoop:
#retrieve histogram
print("in loop", self.hbtRunning)
if not self.hbtRunning:
#reset the histogram
print("reset TDC_getHbtCorrelations")
TDC_resetHbtCorrelations()
TDC_calcHbtG2(hbtFunction)
startTime = time.time()
#be sure to not have a time diff of 0 seconds... (otherwise we divide by zero)
endTime = time.time()+1
self.hbtRunning = True
print("clear data")
else:
TDC_calcHbtG2(hbtFunction)
endTime = time.time()
dataArray = numpy.array(hbtFunction[0][:], dtype=numpy.float64)
datalen = len(dataArray)
print(hbtFunction[0].indexOffset)
histAx.cla()
#normalize data (we assume to have a probabilty of one at large taus, so take the midvalue of the last 5 elements on each side)
#print(numpy.concatenate((dataArray[:5], dataArray[-5:])))
normConst = numpy.mean(numpy.concatenate((dataArray[:5], dataArray[-5:])))
if normConst > 0 and self.doNormalization:
dataArray /= normConst
#TODO make correction not static
#we assume a poor signal to background noise of 0.5
if self.signalCorrection:
dataArray = (dataArray-(1-self.sigToBack**2))/self.sigToBack**2
b = dataArray<0
dataArray[b] = 0
histAx.set_ylim([0, numpy.max(dataArray)])
self.histo = histAx.plot(t,dataArray)
histAx.plot((t[0], t[-1]), (1,1), 'r-')
histAx.plot((t[0],t[-1]), (0.5,0.5), 'r-')
histFig.canvas.draw()
#only update every second
time.sleep(1)
self.histoData = dataArray
dataArray = None
#histAx.cla()
TDC_releaseHbtFunction(hbtFunction)
def scanSample(self, master=None, refToMain=None):
#at start we clearly have no interrupt
self.interrupt = False
self.startPoint = None
self.correctionFactor = (0,0)
#clear data array
#self.dataArray = numpy.ones((len(self.ysteps),len(self.xsteps)), dtype=numpy.float64)
try:
import tkinter as Tk
except ImportError:
import Tkinter as Tk
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
countX = 0
countY = 0
from matplotlib.colors import LogNorm
from matplotlib.figure import Figure
f = Figure(figsize=(4,4), dpi=100)
f.subplots_adjust(left=0.2)
self.fplt = f.add_subplot(111)
#f.tight_layout()
self.imgplot = self.fplt.imshow(self.dataArray, animated=True)#, norm=LogNorm(vmin=100, vmax=1000000))
self.imgplot.set_interpolation('none')
if hasattr(self,"canvas"):
self.canvas.get_tk_widget().grid_forget()
self.canvas = None
#if the canvas is not allready shown show it
try:
import Tkinter as tk
except ImportError:
import tkinter as tk
if refToMain is not None:
toolbar_frame = refToMain.createFrame(master)
elif hasattr(self, "refToMain"):
toolbar_frame = self.refToMain.createFrame(master)
else:
toolbar_frame = tk.Frame(master)
toolbar_frame.grid(row=4,column=0, columnspan=2, rowspan=6)
#if we have a ref to main try to execute the gui generation on the main thread
if refToMain is not None:
self.canvas = refToMain.createCanvas(f, toolbar_frame)
elif hasattr(self, "refToMain"):
self.canvas = self.refToMain.createCanvas(f, toolbar_frame)
else:
self.canvas = FigureCanvasTkAgg(f, master=toolbar_frame)
self.canvas.show()
canvasWidget =self.canvas.get_tk_widget()
#register mouse callback to be able to navigate to
f.canvas.mpl_connect('button_press_event', self.processMouseClick)
canvasWidget.pack(side=tk.BOTTOM, fill=tk.BOTH, expand=True)
tmpB = c_int *19
tmpBuffer = tmpB()
#TDC_setExposureTime(self.exposureTime)
for i in self.ysteps:
countX = 0
for o in self.xsteps:
#navigate to location
self.setPoint( o, i)
#retrieve count rate from adp
ret = TDC_getCoincCounters(tmpBuffer)
#set the count rate (the value we get is the pure count number, so divide by exposure time)
self.dataArray[countY][countX] = numpy.sum(tmpBuffer) / (self.exposureTime/1000)
countX += 1
#set data and new limits for better color plotting
self.imgplot.set_data(self.dataArray)
self.imgplot.set_clim(numpy.min(self.dataArray), numpy.max(self.dataArray))
#update the canvas with the new data
f.canvas.draw()
time.sleep(self.exposureTime/1000)
if self.interrupt:
#if we have an interrupt stop scanning and clean the resources
#update the master (we only can get interrupts from the gui, so its save to assume that master is not None)
master.update()
#make sure the interrupt is set
self.interrupt = True
#clear the buffer, otherwise we get memory leaks and issues which let the python interpreter crash)
tmpBuffer = None
tmpB = None
#navigate back to origin
self.setPoint(0,0)
return
countY += 1
if master is None:
#only save the sample scan if we are not from gui (otherwise we see it there...)
plt.savefig("sampleScan.jpeg")
plt.ioff()
#same as for the interrupt
tmpB = None
tmpBuffer = None
def takePicture(self, name):
if not hasattr(self, "_context"):
self.initCamera()
fc2StartCapture(self._context)
#create the two pictures one for getting input the other to save
rawImage = fc2Image()
convertedImage = fc2Image()
fc2CreateImage(rawImage)
fc2CreateImage(convertedImage)
fc2RetrieveBuffer(self._context, rawImage)
self.savePicture(name, rawImage, convertedImage)
fc2DestroyImage(rawImage)
fc2DestroyImage(convertedImage)
fc2StopCapture(self._context)
def savePicture(self, name, rawImage, convertedImage):
fc2ConvertImageTo(FC2_PIXEL_FORMAT_BGR, rawImage, convertedImage)
fc2SaveImage(convertedImage, name.encode('utf-8'), 6)
def uninitCamera(self):
fc2StopCapture(self._context)
fc2DestroyContext(self._context)
def initCamera(self):
error = fc2Error()
self._context = fc2Context()
self._guid = fc2PGRGuid()
self._numCameras = c_uint()
error = fc2CreateContext(self._context)
if error != FC2_ERROR_OK.value:
print("Error in fc2CreateContext: " + str(error))
error = fc2GetNumOfCameras(self._context, self._numCameras)
if error != FC2_ERROR_OK.value:
print("Error in fc2GetNumOfCameras: " + str(error))
if self._numCameras == 0:
print("No Cameras detected")
#get the first camera
error = fc2GetCameraFromIndex(self._context, 0, self._guid)
if error != FC2_ERROR_OK.value:
print("Error in fc2GetCameraFromIndex: " + str(error))
error = fc2Connect(self._context, self._guid)
if error!= FC2_ERROR_OK.value:
print("Error in fc2Connect: " + str(error))
def checkForMax(self, textBoxReference):
if not hasattr(self, "interrupt") or not self.interrupt or not hasattr(self, "noCheckForMax") or not self.noCheckForMax:
print("scan has not lunched yet, or is running")
self.startPoint = None
self.correctionFactor = (0,0)
return
TDC_freezeBuffers(True)
#the scan is not running, so check if we are on the maximum in a 6x6 px array
#assume that currentXCoord and currentYCoord are set to the right spot
xfrom = max(self.currentXCoord-self.quadSize,0)
xto = min(self.currentXCoord + self.quadSize, len(self.xsteps))
yfrom = max(self.currentYCoord-self.quadSize,0)
yto = min(self.currentYCoord+self.quadSize, len(self.ysteps))
tmpData = numpy.ones((self.quadSize*2,self.quadSize*2), dtype=numpy.float64)
tmpB = c_int *19
tmpBuffer = tmpB()
sleepTime = 0.01
tmpIntens = 0.0
tmpLocX = 0.0
tmpLocY = 0.0
xStart = self.currentX
yStart = self.currentY
for x in numpy.linspace(0, xto-xfrom-1, xto-xfrom):
for y in numpy.linspace(0, yto-yfrom-1, yto-yfrom):
#get count rate
self.goTo(x+xfrom,y+yfrom, directly=True)
ret = TDC_getCoincCounters(tmpBuffer)
#set the count rate (the value we get is the pure count number, so divide by exposure time)
tmpData[y][x] = numpy.sum(tmpBuffer) / (self.exposureTime/1000)
tmpLocX += tmpData[y][x] * self.getGoToX(x+xfrom)
tmpLocY += tmpData[y][x] * self.getGoToY(y+yfrom)
time.sleep(self.exposureTime/1000)
#same as for the interrupt
subarray = tmpData
tmpIntens = numpy.sum(tmpData)
tmpLocX = tmpLocX/tmpIntens
tmpLocY = tmpLocY/tmpIntens
print(subarray, xfrom, xto, yfrom, yto)
m = numpy.argmax(subarray)
maximum = numpy.max(subarray)
minimum = numpy.min(subarray)
if self.autocorrection:
self.sigToBack = (maximum-minimum)/maximum
textBoxReference.set(self.sigToBack)
y,x = numpy.unravel_index(m, subarray.shape)
#print(m, x, y)
#calculate real index
#TODO check consistency of x and y throughout class
x = xfrom + x
y = yfrom + y
print("(%f,%f) -> (%f,%f)"%(xStart, yStart, tmpLocX,tmpLocY))
self.currentXCoord = x
self.currentYCoord = y
self.setPoint(tmpLocX, tmpLocY, directly=True)
if self.startPoint is not None:
self.correctionFactor = (tmpLocX - self.startPoint[0], tmpLocY - self.startPoint[1])
else:
self.startPoint = (tmpLocX, tmpLocY)
#clean up
tmpB = None
tmpBuffer = None
TDC_freezeBuffers(False)