-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscan3D_image_wells.py
986 lines (772 loc) · 38.4 KB
/
scan3D_image_wells.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
#!/usr/bin/python
"""
LSM scanning code
# Adam Glaser 07/19
# Edited by Kevin Bishop 5/22
# Edited by Rob Serafin 9/22
"""
import numpy as np
import math
import h5py
import os.path
import shutil
import skimage.transform
import pco
# Tiger or MS2000 are imported below based on stage model param
import hardware.ni as ni
import hardware.fw102c as fw102c
import hardware.skyra as skyra
from hardware.opto import Opto
import time as timer
import shutil
import hivex_puck as puck
class experiment(object):
"""
A descriptive sentence
Parameters
----------
drive
fname
xMin
xMax
yMin
YMax
zMin
zMax
wavelengths
powers
attenuations
theta
overlap
"""
def __init__(self, experiment_dict):
self.drive = experiment_dict['drive']
self.fname = experiment_dict['fname']
self.xWidth = experiment_dict['xWidth']
self.yWidth = experiment_dict['yWidth']
self.zWidth = experiment_dict['zWidth']
self.wavelengths = experiment_dict['wavelengths']
# self.powers = powers
self.attenuations = experiment_dict['attenuations']
self.theta = experiment_dict['theta']
self.overlapY = experiment_dict['overlapY']
self.overlapZ = experiment_dict['overlapZ']
## If imaging pre-defined coordinates for hivex well, these keys will not be defined until lsmfx is opened
check_for_keys = 'xMin', 'xMax', 'yMin', 'yMax', 'zMin', 'zMax'
if check_for_keys in experiment_dict:
self.xMin = experiment_dict['xMin']
self.xMax = experiment_dict['xMax']
self.yMin = experiment_dict['yMin']
self.yMax = experiment_dict['yMax']
self.zMin = experiment_dict['zMin']
self.zMax = experiment_dict['zMax']
print('defined all experiment_dict keys')
else:
print('not all keys are defined yet')
class scan(object):
def __init__(self, experiment, camera):
self.xLength = experiment.xMax - experiment.xMin # mm
self.yLength = round((experiment.yMax - experiment.yMin) /
experiment.yWidth) * experiment.yWidth # mm
self.zLength = round((experiment.zMax - experiment.zMin) /
experiment.zWidth) * experiment.zWidth # mm
self.xOff = experiment.xMax - self.xLength/2
self.yOff = experiment.yMax - self.yLength/2
self.zOff = experiment.zMin
self.nFrames = int(np.floor(self.xLength/(experiment.xWidth/1000.0)))
self.nWavelengths = len(experiment.wavelengths)
self.yTiles = int(round(self.yLength/experiment.yWidth))
self.zTiles = int(round(self.zLength/experiment.zWidth))
# setup scan speed and chunk sizes
self.scanSpeed = self.setScanSpeed(experiment.xWidth, camera.expTime)
self.chunkSize1 = 256
if self.chunkSize1 >= self.nFrames/8:
self.chunkSize1 = np.floor(self.nFrames/8)
self.chunkSize2 = 16
if self.chunkSize2 >= camera.Y/8:
self.chunkSize2 = np.floor(camera.Y/8)
self.chunkSize3 = 256
if self.chunkSize3 >= camera.X/8:
self.chunkSize3 = np.floor(camera.X/8)
self.blockSize = int(2*self.chunkSize1)
def setScanSpeed(self, xWidth, expTime):
speed = xWidth/(1.0/(1.0/((expTime + 10.0e-3)/1000.0))*1000.0)
return speed
# TODO: Change name to camera_settings
class camera(object):
def __init__(self,
camera_dict):
self.number = camera_dict['number']
self.X = camera_dict['X']
self.Y = camera_dict['Y']
self.sampling = camera_dict['sampling']
self.expTime = camera_dict['expTime']
self.triggerMode = camera_dict['triggerMode']
self.acquireMode = camera_dict['acquireMode']
self.shutterMode = camera_dict['shutterMode']
self.compressionMode = camera_dict['compressionMode']
self.B3Denv = camera_dict['B3Denv']
self.quantSigma = camera_dict['quantSigma']
class daq(object):
def __init__(self,
daq_dict):
self.rate = daq_dict['rate']
self.board = daq_dict['board']
# self.name = daq_dict['name']
self.num_channels = daq_dict['num_channels']
self.names_to_channels = daq_dict['names_to_channels']
self.xmin = daq_dict['xmin']
self.xmax = daq_dict['xmax']
self.xpp = daq_dict['xpp']
self.ymin = daq_dict['ymin']
self.ymax = daq_dict['ymax']
self.ypp = daq_dict['ypp']
self.econst = daq_dict['econst']
class laser(object):
def __init__(self,
laser_dict):
self.port = laser_dict['port']
self.rate = laser_dict['rate']
self.names_to_channels = laser_dict['names_to_channels']
self.max_powers = laser_dict['max_powers']
self.skyra_system_name = laser_dict['skyra_system_name']
self.min_currents = laser_dict['min_currents']
self.max_currents = laser_dict['max_currents']
self.strobing = laser_dict['strobing']
def initialize(self, experiment, scan):
print('initializing laser')
print('System_name=' + self.skyra_system_name)
input('If this is NOT correct, press CTRL+C to exit and avoid damage' +
' to the laser. If this correct, press Enter to continue.')
min_currents_sk_num = {}
max_currents_sk_num = {}
max_powers_sk_num = {}
for ch in experiment.wavelengths: # ch is wavelength as a string
min_currents_sk_num[self.names_to_channels[ch]] = \
self.min_currents[ch]
max_currents_sk_num[self.names_to_channels[ch]] = \
self.max_currents[ch]
max_powers_sk_num[self.names_to_channels[ch]] = \
self.max_powers[ch]
skyraLaser = skyra.Skyra(baudrate=self.rate,
port=self.port)
skyraLaser.setMinCurrents(min_currents_sk_num)
skyraLaser.setMaxCurrents(max_currents_sk_num)
for ch in list(self.names_to_channels):
skyraLaser.setModulationOn(self.names_to_channels[ch])
skyraLaser.setDigitalModulation(self.names_to_channels[ch], 1)
# new, to ensure analog mod is not active
skyraLaser.setAnalogModulation(self.names_to_channels[ch], 0)
for ch in list(experiment.wavelengths):
skyraLaser.setModulationHighCurrent(self.names_to_channels[ch],
experiment.wavelengths[ch])
skyraLaser.turnOn(self.names_to_channels[ch])
for ch in list(experiment.wavelengths):
skyraLaser.setModulationLowCurrent(self.names_to_channels[ch], 0)
highest_current = ((experiment.wavelengths[ch] - self.min_currents[ch]) / \
np.exp(-scan.zTiles * experiment.zWidth / experiment.attenuations[ch])) + self.min_currents[ch]
# old attenuation equation
# highest_current = experiment.wavelengths[ch] / \
# np.exp(-scan.zTiles * experiment.zWidth / experiment.attenuations[ch])
print(highest_current)
print(scan.zTiles)
print(experiment.zWidth)
maxCurrent = self.max_currents[ch]
if highest_current > maxCurrent:
raise Exception('Current will be out of range at final Z ' +
'position. Adjust current or attenuation.\n')
print('finished initializing laser')
return skyraLaser
class etl(object):
def __init__(self,
etl_dict):
self.port = etl_dict['port']
class wheel(object):
def __init__(self,
wheel_dict):
self.port = wheel_dict['port']
self.rate = wheel_dict['rate']
self.names_to_channels = wheel_dict['names_to_channels']
class stage(object):
def __init__(self,
stage_dict):
self.port = stage_dict['port']
self.rate = stage_dict['rate']
self.model = stage_dict['model']
# Should check the velocity and acceration, I think this
# really shouldn't be part of the init since it's set to different
# values in various places
self.settings = {'backlash': 0.0,
'velocity': 1.0,
'acceleration': 100
}
self.axes = ('X', 'Y', 'Z')
def initialize(self):
if self.model == 'tiger':
print('initializing stage: Tiger')
import hardware.tiger as tiger
xyzStage = tiger.TIGER(baudrate=self.rate, port=self.port)
xyzStage.setPLCPreset(6, 52)
elif self.model == 'ms2000':
print('initializing stage: MS2000')
import hardware.ms2000 as ms2000
xyzStage = ms2000.MS2000(baudrate=self.rate, port=self.port)
xyzStage.setTTL('Y', 3)
else:
raise Exception('invalid stage type!')
initialPos = xyzStage.getPosition()
xyzStage.setScanF(1)
for ax in self.axes:
xyzStage.setBacklash(ax, self.settings['backlash'])
xyzStage.setVelocity(ax, self.settings['velocity'])
xyzStage.setAcceleration(ax, self.settings['acceleration'])
print('stage initialized', initialPos)
return xyzStage, initialPos
# TODO: break apart into smaller pieces:
# initialize hardware
# scan tiles
def scan3D_image_wells(experiment, camera, daq, laser, wheel, etl, stage, image_wells):
if image_wells['option'] == 'yes':
experiment.fname += '_well_N' ## re-format file name for well-based imaging
## Check to make sure that well numbers were defined by the user
try:
image_wells['well_numbers']
except NameError:
print('Well numbers are not defined')
for well_number in image_wells['well_numbers']:
experiment = puck.well(well_number, experiment) ## Define imaging coordinates for this well
fname_end = str(experiment.fname).split('_')[-1]
experiment.fname = experiment.fname.replace(fname_end, str(well_number)) ## adjust fname
# ROUND SCAN DIMENSIONS & SETUP IMAGING SESSION
session = scan(experiment, camera)
# SETUP DATA DIRECTORY
## Check if drive already exists. If so, provide option to delete
if os.path.exists(experiment.drive + ':\\' + experiment.fname):
userinput = input('this file directory already exists! permanently delete? [y/n]')
if userinput == 'y':
shutil.rmtree(experiment.drive + ':\\' + experiment.fname, ignore_errors=True)
if userinput== 'n':
sys.exit('--Terminating-- re-name write directory and try again')
os.makedirs(experiment.drive + ':\\' + experiment.fname)
dest = experiment.drive + ':\\' + experiment.fname + '\\data.h5'
# # Save a copy of all files in the current directory, i.e. so user can refer to experiment settings and could reproduce experiment entirely
# src = os.getcwd()
# settings_rxiv = experiment.drive + ':\\' + experiment.fname + '\\settings and code archive\\'
# shutil.copytree(src,dst=settings_rxiv)
# CONNECT XYZ STAGE
xyzStage, initialPos = stage.initialize()
print(xyzStage)
# INITIALIZE H5 FILE
h5init(dest, camera, session, experiment)
write_xml(experiment=experiment, camera=camera, scan=session)
# CONNECT NIDAQ
waveformGenerator = ni.waveformGenerator(daq=daq,
camera=camera,
session=session,
triggered=True)
# CONNECT LASER
# according to the manual, you should wait for 2min after setting
# laser 1 (561) to mod mode for power to stabalize. Consider adding this in
# TODO: disentangle laser and experiment attributes
skyraLaser = laser.initialize(experiment, session)
print(skyraLaser)
# CONNECT FILTER WHEEL
fWheel = fw102c.FW102C(baudrate=wheel.rate, port=wheel.port)
print(fWheel)
# CONNECT TUNBALE LENS
etl = Opto(port=etl.port)
etl.connect()
etl.mode('analog')
print(etl)
# CONNECT CAMERA
# TODO: setup separate hardware initialization method within camera
cam = pco.Camera(camera_number=camera.number)
cam.configuration = {'exposure time': camera.expTime*1.0e-3,
'roi': (1,
1023-round(camera.Y/2),
2060,
1026+round(camera.Y/2)),
'trigger': camera.triggerMode,
'acquire': camera.acquireMode,
'pixel rate': 272250000}
cam.record(number_of_images=session.nFrames, mode='sequence non blocking')
# possibly change mode to ring buffer??
# IMAGING LOOP
ring_buffer = np.zeros((session.blockSize,
camera.Y,
camera.X),
dtype=np.uint16)
# print('made ring buffer')
tile = 0
previous_tile_time = 0
previous_ram = 0
start_time = timer.time()
xPos = session.xLength/2.0 - session.xOff
for j in range(session.zTiles):
zPos = j*experiment.zWidth + session.zOff
xyzStage.setVelocity('Z', 0.1)
xyzStage.goAbsolute('Z', zPos, False)
for k in range(session.yTiles):
yPos = session.yOff - session.yLength / 2.0 + \
k*experiment.yWidth + experiment.yWidth / 2.0
xyzStage.setVelocity('Y', 1.0)
xyzStage.goAbsolute('Y', yPos, False)
for ch in range(session.nWavelengths):
wave_str = list(experiment.wavelengths)[ch]
# wave_str is wavelength in nm as a string, e.g. '488'
# ch is order of wavelenghts in main (an integer 0 -> X)
# (NOT necessarily Skyra channel number)
xyzStage.setVelocity('X', 1.0)
xPos = session.xLength/2.0 - session.xOff
xyzStage.goAbsolute('X', -xPos, False)
# CHANGE FILTER
fWheel.setPosition(wheel.names_to_channels[wave_str])
# START SCAN
skyraLaser.setModulationHighCurrent(
laser.names_to_channels[wave_str],
experiment.wavelengths[wave_str] /
np.exp(-j*experiment.zWidth /
experiment.attenuations[wave_str])
)
voltages, rep_time = write_voltages(daq=daq,
laser=laser,
camera=camera,
experiment=experiment,
ch=ch)
waveformGenerator.ao_task.write(voltages)
print('Starting tile ' + str((tile)*session.nWavelengths+ch+1),
'/',
str(session.nWavelengths*session.zTiles*session.yTiles))
print('y position: ' + str(yPos) + ' mm')
print('z position: ' + str(zPos) + ' mm')
tile_start_time = timer.time()
xyzStage.setScanR(-xPos, -xPos + session.xLength)
xyzStage.setScanV(yPos)
response = xyzStage.getMotorStatus()
while response[0] == 'B':
response = xyzStage.getMotorStatus()
xyzStage.setVelocity('X', session.scanSpeed)
xyzStage.setVelocity('Y', session.scanSpeed)
xyzStage.setVelocity('Z', session.scanSpeed)
waveformGenerator.ao_task.start()
cam.start()
xyzStage.scan(False)
skyraLaser.turnOn(laser.names_to_channels[
list(experiment.wavelengths)[ch]])
# START IMAGING LOOP
num_acquired = 0
num_acquired_counter = 0
num_acquired_previous = 0
# while num_acquired < scan.nFrames: #original version.
# for some reason code frequently (but not always)
# gets stuck on cam.wait_for_next_image(num_acquired),
# like cam is a frame or two ahead of code
while num_acquired < session.nFrames - 100:
# print('you\'ve got an image!', num_acquired, 'of',
# session.nFrames, 'total')
cam.wait_for_next_image(num_acquired)
if num_acquired_counter == int(session.blockSize):
print('Saving frames: ',
str(num_acquired_previous),
' - ',
str(num_acquired))
print('Tile: ' + str(tile))
h5write(dest,
ring_buffer,
tile + session.zTiles*session.yTiles*ch,
num_acquired_previous, num_acquired)
num_acquired_counter = 0
num_acquired_previous = num_acquired
temp = cam.image(num_acquired)[0]
ring_buffer[num_acquired_counter] = \
temp[2:camera.Y + 2, 1024 - int(camera.X / 2):1024
- int(camera.X / 2) + camera.X]
else:
temp = cam.image(num_acquired)[0]
ring_buffer[num_acquired_counter] = \
temp[2:camera.Y + 2, 1024 - int(camera.X / 2):1024
- int(camera.X / 2) + camera.X]
if num_acquired == session.nFrames-1:
print('Saving frames: ',
str(num_acquired_previous),
' - ',
str(session.nFrames))
h5write(dest,
ring_buffer[0:num_acquired_counter+1],
tile + session.zTiles*session.yTiles*ch,
num_acquired_previous,
session.nFrames)
num_acquired += 1
num_acquired_counter += 1
waveformGenerator.ao_task.stop()
waveformGenerator.write_zeros(daq=daq)
# For some reason this write_zeros works but the above doesn't?
# laser stops and starts appropriately with this one active
# and the top write_zeros() commented out
skyraLaser.turnOff(laser.names_to_channels[list(experiment.wavelengths)[ch]])
cam.stop()
tile_end_time = timer.time()
tile_time = tile_end_time - tile_start_time
print('Tile time: ' + str(round((tile_time/60), 3)) + " min")
tiles_remaining = session.nWavelengths * session.zTiles * \
session.yTiles - (tile * session.nWavelengths + ch + 1)
if tiles_remaining != 0:
print('Estimated time remaining: ',
str(round((tile_time*tiles_remaining/3600), 3)),
" hrs")
tile += 1
end_time = timer.time()
print("Total time = ",
str(round((end_time - start_time)/3600, 3)),
" hrs")
response = xyzStage.getMotorStatus()
while response[0] == 'B':
response = xyzStage.getMotorStatus()
cam.close()
etl.close(soft_close=True)
# waveformGenerator.counter_task.close()
waveformGenerator.ao_task.close()
xyzStage.shutDown()
def write_voltages(daq,
laser,
camera,
experiment,
ch):
print('writing voltages')
n2c = daq.names_to_channels
wave_key = list(experiment.wavelengths)[ch] # wavelength as a string
# convert max / min / peak-to-peak (DAQExpress convention)
# to offset / amplitude
xoffset = (daq.xmax[wave_key] + daq.xmin[wave_key]) / 2
xamplitude = daq.xpp[wave_key] / 2
yoffset = (daq.ymax[wave_key] + daq.ymin[wave_key]) / 2
yamplitude = daq.ypp[wave_key] / 2
eoffset = daq.econst[wave_key]
samples = int(daq.rate*camera.expTime/1e3) # number of samples for DAQ
line_time = 9.76/1.0e6 # seconds, constant for pco.edge camera
roll_time = line_time*camera.Y/2.0 # chip rolling time in seconds
roll_samples = int(np.floor(roll_time*daq.rate)) # rolling samples
on_time = camera.expTime/1e3 - roll_time # ON time for strobing laser
on_samples = int(np.floor(on_time*daq.rate)) # ON samples
galvo_time = 365/1.0e6 # galvo delay time
galvo_samples = int(np.floor(galvo_time*daq.rate))
buffer_time = 50/1.0e6
buffer_samples = int(np.floor(buffer_time*daq.rate))
voltages = np.zeros((daq.num_channels, samples)) # create voltages array
# X Galvo scanning:
period_samples = np.linspace(0,
2 * math.pi, on_samples + 2 * buffer_samples)
snap_back = np.linspace(xoffset + xamplitude,
xoffset - xamplitude,
samples - on_samples - 2 * buffer_samples)
voltages[n2c['xgalvo'], :] = xoffset
voltages[n2c['xgalvo'],
roll_samples - galvo_samples - buffer_samples:
roll_samples + on_samples - galvo_samples + buffer_samples] = \
-2 * (xamplitude / math.pi) * \
np.arctan(1.0 / (np.tan(period_samples / 2.0))) + xoffset
voltages[n2c['xgalvo'],
roll_samples + on_samples - galvo_samples + buffer_samples:
samples] = \
snap_back[0:samples - (roll_samples + on_samples - galvo_samples +
buffer_samples)]
voltages[n2c['xgalvo'],
0:roll_samples - galvo_samples - buffer_samples] = \
snap_back[samples - (roll_samples + on_samples - galvo_samples +
buffer_samples):samples]
# Y Galvo scanning:
period_samples = np.linspace(0,
2 * math.pi, on_samples + 2 * buffer_samples)
snap_back = np.linspace(yoffset + yamplitude,
yoffset - yamplitude,
samples - on_samples - 2 * buffer_samples)
voltages[n2c['ygalvo'], :] = yoffset
voltages[n2c['ygalvo'],
roll_samples - galvo_samples - buffer_samples:
roll_samples + on_samples-galvo_samples + buffer_samples] = \
-2 * (yamplitude / math.pi) * \
np.arctan(1.0 / (np.tan(period_samples / 2.0))) + yoffset
voltages[n2c['ygalvo'],
roll_samples + on_samples - galvo_samples + buffer_samples:
samples] = \
snap_back[0:samples - (roll_samples + on_samples - galvo_samples +
buffer_samples)]
voltages[n2c['ygalvo'],
0:roll_samples - galvo_samples - buffer_samples] = \
snap_back[samples - (roll_samples + on_samples - galvo_samples +
buffer_samples):samples]
# Laser modulation:
if laser.strobing == 'ON':
voltages[n2c[wave_key],
roll_samples + 50:roll_samples + on_samples - 50] = 5.0
voltages[n2c[wave_key], 0] = 0.0
voltages[n2c[wave_key], -1] = 0.0
elif laser.strobing == 'OFF':
voltages[n2c[wave_key], :] = 5.0
else:
raise Exception('laser.strobing invalid, must be \'ON\' or \'OFF\'')
# ETL scanning:
voltages[n2c['etl'], :] = eoffset
# NI playing:
voltages[n2c['daq_active'], :] = 3.0
# for c in range(12):
# plt.plot(voltages[c, :])
# plt.legend(loc='upper right')
# plt.show()
# Check final voltages for sanity
# Assert that voltages are safe
assert (1.0/(1.0/on_time)) <= 800.0
assert np.max(voltages[n2c['xgalvo'], :]) <= 5.0
assert np.min(voltages[n2c['xgalvo'], :]) >= -5.0
assert np.max(voltages[n2c['ygalvo'], :]) <= 5.0
assert np.min(voltages[n2c['ygalvo'], :]) >= -5.0
assert np.max(voltages[n2c['etl'], :]) <= 5.0
assert np.min(voltages[n2c['etl'], :]) >= 0.0
assert np.max(voltages[n2c['daq_active'], :]) <= 5.0
assert np.min(voltages[n2c['daq_active'], :]) >= 0.0
assert np.max(voltages[n2c[wave_key], :]) <= 5.0
assert np.min(voltages[n2c[wave_key], :]) >= 0.0
print('wrote voltages')
return voltages, (camera.expTime/1e3-2*roll_time)*1000
def zero_voltages(daq, camera):
# samples = int(daq.rate*camera.expTime/1e3) # number of samples for DAQ
voltages = np.zeros((daq.num_channels, 2)) # create voltages array
return voltages
def h5init(dest, camera, scan, experiment):
f = h5py.File(dest, 'a')
res_list = [1, 2, 4, 8]
res_np = np.zeros((len(res_list), 3), dtype='float64')
res_np[:, 0] = res_list
res_np[:, 1] = res_list
res_np[:, 2] = res_list
subdiv_np = np.zeros((len(res_list), 3), dtype='uint32')
subdiv_np[:, 0] = scan.chunkSize1
subdiv_np[:, 1] = scan.chunkSize2
subdiv_np[:, 2] = scan.chunkSize3
tgroup = f.create_group('/t00000')
tile = 0
for j in range(scan.zTiles):
for k in range(scan.yTiles):
for ch in range(scan.nWavelengths):
idx = tile + scan.zTiles*scan.yTiles*ch
sgroup = f.create_group('/s' + str(idx).zfill(2))
resolutions = f.require_dataset('/s' + str(idx).zfill(2) + '/resolutions',
chunks=(res_np.shape),
dtype='float64',
shape=(res_np.shape),
data=res_np)
subdivisions = f.require_dataset('/s' + str(idx).zfill(2) + '/subdivisions',
chunks=(res_np.shape),
dtype='uint32',
shape=(subdiv_np.shape),
data=subdiv_np)
for z in range(len(res_list)-1, -1, -1):
res = res_list[z]
resgroup = f.create_group('/t00000/s' + str(idx).zfill(2) + '/' + str(z))
if camera.quantSigma[list(experiment.wavelengths)[ch]] == 0:
data = f.require_dataset('/t00000/s' + str(idx).zfill(2) + '/' + str(z) + '/cells',
chunks=(scan.chunkSize1,
scan.chunkSize2,
scan.chunkSize3),
dtype='int16',
shape=np.ceil(np.divide([scan.nFrames,
camera.Y,
camera.X],
res)
)
)
else:
if ((camera.B3Denv != '') and
(camera.B3Denv != os.environ['CONDA_DEFAULT_ENV'])
):
print('Warning: B3D is active but the ' +
'current conda environment is: ' +
os.environ['CONDA_DEFAULT_ENV'])
print('Press CTRL + C to exit and run \'conda' +
' activate ' + camera.B3Denv + '\' before ' +
'running lsm-python-main.py')
input('Press Enter to override this warning' +
' and continue anyways')
data = f.require_dataset('/t00000/s' + str(idx).zfill(2) + '/' + str(z) + '/cells',
chunks=(scan.chunkSize1,
scan.chunkSize2,
scan.chunkSize3),
dtype='int16',
shape=np.ceil(np.divide([scan.nFrames,
camera.Y,
camera.X],
res)),
compression=32016,
compression_opts=(round(camera.quantSigma[list(experiment.wavelengths)[ch]]*1000),
camera.compressionMode,
round(2.1845*1000),
0,
round(1.5*1000))
)
tile += 1
f.close()
def h5write(dest, img_3d, idx, ind1, ind2):
f = h5py.File(dest, 'a')
res_list = [1, 2, 4, 8]
for z in range(len(res_list)):
res = res_list[z]
if res > 1:
img_3d = skimage.transform.downscale_local_mean(img_3d,
(2, 2, 2)
).astype('uint16')
if ind1 == 0:
ind1_r = ind1
else:
ind1_r = np.ceil((ind1 + 1)/res - 1)
data = f['/t00000/s' + str(idx).zfill(2) + '/' + str(z) + '/cells']
data[int(ind1_r):int(ind1_r+img_3d.shape[0])] = img_3d.astype('int16')
f.close()
def write_xml(experiment, camera, scan):
print("Writing BigDataViewer XML file...")
c = scan.nWavelengths # number of channels
tx = scan.yTiles # number of lateral x tiles
ty = scan.zTiles # number of vertical y tiles
t = tx*ty # total tiles
ox = experiment.yWidth*1000 # offset along x in um
oy = experiment.zWidth*1000 # offset along y in um
sx = camera.sampling # effective pixel size in x direction
# effective pixel size in y direction
sy = camera.sampling*np.cos(experiment.theta*np.pi/180.0)
# effective pixel size in z direction (scan direction)
sz = experiment.xWidth
scale_x = sx/sy # normalized scaling in x
scale_y = sy/sy # normalized scaling in y
scale_z = sz/sy # normalized scaning in z
# shearing based on theta and y/z pixel sizes
shear = -np.tan(experiment.theta*np.pi/180.0)*sy/sz
f = open(experiment.drive + ':\\' + experiment.fname + '\\data.xml', 'w')
f.write('<?xml version="1.0" encoding="UTF-8"?>\n')
f.write('<SpimData version="0.2">\n')
f.write('\t<BasePath type="relative">.</BasePath>\n')
f.write('\t<SequenceDescription>\n')
f.write('\t\t<ImageLoader format="bdv.hdf5">\n')
f.write('\t\t\t<hdf5 type="relative">data.h5</hdf5>\n')
f.write('\t\t</ImageLoader>\n')
f.write('\t\t<ViewSetups>\n')
for i in range(0, c):
for j in range(0, t):
ind = j+i*t
if ind <= scan.yTiles*scan.zTiles*scan.nWavelengths:
f.write('\t\t\t<ViewSetup>\n')
f.write('\t\t\t\t<id>' + str(t*i+j) + '</id>\n')
f.write('\t\t\t\t<name>' + str(t*i+j) + '</name>\n')
f.write('\t\t\t\t<size>' + str(camera.X) + ' ' + str(camera.Y)
+ ' ' + str(scan.nFrames) + '</size>\n')
f.write('\t\t\t\t<voxelSize>\n')
f.write('\t\t\t\t\t<unit>um</unit>\n')
f.write('\t\t\t\t\t<size>' + str(sx) + ' ' + str(sy) + ' '
+ str(sz) + '</size>\n')
f.write('\t\t\t\t</voxelSize>\n')
f.write('\t\t\t\t<attributes>\n')
f.write('\t\t\t\t\t<illumination>0</illumination>\n')
f.write('\t\t\t\t\t<channel>' + str(i) + '</channel>\n')
f.write('\t\t\t\t\t<tile>' + str(j) + '</tile>\n')
f.write('\t\t\t\t\t<angle>0</angle>\n')
f.write('\t\t\t\t</attributes>\n')
f.write('\t\t\t</ViewSetup>\n')
f.write('\t\t\t<Attributes name="illumination">\n')
f.write('\t\t\t\t<Illumination>\n')
f.write('\t\t\t\t\t<id>0</id>\n')
f.write('\t\t\t\t\t<name>0</name>\n')
f.write('\t\t\t\t</Illumination>\n')
f.write('\t\t\t</Attributes>\n')
f.write('\t\t\t<Attributes name="channel">\n')
for i in range(0, c):
ind = i
if ind <= scan.nWavelengths:
f.write('\t\t\t\t<Channel>\n')
f.write('\t\t\t\t\t<id>' + str(i) + '</id>\n')
f.write('\t\t\t\t\t<name>' + str(i) + '</name>\n')
f.write('\t\t\t\t</Channel>\n')
f.write('\t\t\t</Attributes>\n')
f.write('\t\t\t<Attributes name="tile">\n')
for i in range(0, t):
ind = i
if ind <= scan.yTiles*scan.zTiles:
f.write('\t\t\t\t<Tile>\n')
f.write('\t\t\t\t\t<id>' + str(i) + '</id>\n')
f.write('\t\t\t\t\t<name>' + str(i) + '</name>\n')
f.write('\t\t\t\t</Tile>\n')
f.write('\t\t\t</Attributes>\n')
f.write('\t\t\t<Attributes name="angle">\n')
f.write('\t\t\t\t<Illumination>\n')
f.write('\t\t\t\t\t<id>0</id>\n')
f.write('\t\t\t\t\t<name>0</name>\n')
f.write('\t\t\t\t</Illumination>\n')
f.write('\t\t\t</Attributes>\n')
f.write('\t\t</ViewSetups>\n')
f.write('\t\t<Timepoints type="pattern">\n')
f.write('\t\t\t<integerpattern>0</integerpattern>')
f.write('\t\t</Timepoints>\n')
f.write('\t\t<MissingViews />\n')
f.write('\t</SequenceDescription>\n')
f.write('\t<ViewRegistrations>\n')
for i in range(0, c):
for j in range(0, ty):
for k in range(0, tx):
ind = i*ty*tx + j*tx + k
if ind <= scan.yTiles*scan.zTiles*scan.nWavelengths:
shiftx = scale_x*(ox/sx)*k # shift tile in x, unit pixels
shifty = -scale_y*(oy/sy)*j # shift tile in y, unit pixels
f.write('\t\t<ViewRegistration timepoint="0" setup="'
+ str(ind) + '">\n')
# affine matrix for translation of
# tiles into correct positions
f.write('\t\t\t<ViewTransform type="affine">\n')
f.write('\t\t\t\t<Name>Overlap</Name>\n')
f.write('\t\t\t\t<affine>1.0 0.0 0.0 ' + str(shiftx)
+ ' 0.0 1.0 0.0 ' + str(shifty)
+ ' 0.0 0.0 1.0 0.0</affine>\n')
f.write('\t\t\t</ViewTransform>\n')
# affine matrix for scaling of tiles in orthogonal
# XYZ directions, accounting for theta and
# inter-frame spacing
f.write('\t\t\t<ViewTransform type="affine">\n')
f.write('\t\t\t\t<Name>Scale</Name>\n')
f.write('\t\t\t\t<affine>' + str(scale_x)
+ ' 0.0 0.0 0.0 0.0 ' + str(scale_y)
+ ' 0.0 0.0 0.0 0.0 ' + str(scale_z)
+ ' 0.0</affine>\n')
f.write('\t\t\t</ViewTransform>\n')
# affine matrix for shearing of data within each tile
f.write('\t\t\t<ViewTransform type="affine">\n')
f.write('\t\t\t\t<Name>Deskew</Name>\n')
f.write('\t\t\t\t<affine>1.0 0.0 0.0 0.0 0.0 1.0 '
+ str(0.0) + ' 0.0 0.0 ' + str(shear)
+ ' 1.0 0.0</affine>\n')
f.write('\t\t\t</ViewTransform>\n')
f.write('\t\t</ViewRegistration>\n')
f.write('\t</ViewRegistrations>\n')
f.write('\t<ViewInterestPoints />\n')
f.write('\t<BoundingBoxes />\n')
f.write('\t<PointSpreadFunctions />\n')
f.write('\t<StitchingResults />\n')
f.write('\t<IntensityAdjustments />\n')
f.write('</SpimData>')
f.close()
# The MIT License
#
# Copyright (c) 2020 Adam Glaser, University of Washington
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.