-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdfws_solver.py
1366 lines (1087 loc) · 51.7 KB
/
dfws_solver.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
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Deconvolution from Wavefront Sensing Solver!
Created by Bas de Bruijne, Sep 09, 2020
For questions, contact [email protected]
-------------------------------------------------------------------------------------------------------------------------------------------------------
EXAMPLE OF HOW TO USE THIS FILE
import DFWS_Simulator as sim
import dfws_solver as solver
# Fist, use DFWS_Simulator to generate a DFWS class object:
dfws = sim.DFWS(1, 6, 680, 680, 0, 0, .6) # Initialise system with diameter
of 1, 6x6 shack-hartmann sensor and 680x680 pixels of imaging resolution
dfws.random_object() # Load Object
dfws.wavefront_kolmogorov(0.2) # Make D/r0 = 5 turbulent phase
screen
dfws.make_psf() # Adjust the wavefront to the right
size and generate the point spread
functions
dfws.make_image() # Generate the output images
# Now, interact with dfws using the solver:
solver.get_wavefront_DLWFS(dfws) # Use my developed deep learning
wavefront sensing method to
retrieve the wavefront
objest_est = solver.deconvolve(dfws) # Estimate object using PSF loaded
in dfws (solver.get_wavefront_
DLWFS(dfws) loads the estimated
psf automatically)
-------------------------------------------------------------------------------------------------------------------------------------------------------
"""
import aotools
import matplotlib.pyplot as plt
import numpy
import warnings
import numpy as np
from numpy import fft
from scipy.ndimage import rotate
import cupy as cp
import threading
from copy import copy
import tensorflow as tf
from aotools.functions.pupil import circle
from aotools.functions.zernike import zernikeArray
cupy = 0
def free_gpu_memory():
"""
Frees up GPU memory by making reserved blocks available
Inputs:
None, all variables come from globals
Outputs:
None
"""
if cupy:
mempool.free_all_blocks()
pinned_mempool.free_all_blocks()
def tonumpy(x):
"""
Returns a variable from either cupy or numpy to numpy
cupy is the CUDA accelerated version of numpy and will
be used by this class if supported by the hardware
Input:
x: either a numpy or cupy array
Output:
x: a numpy array
"""
try:
return cp.asnumpy(x)
except:
return x
def convolve2(a, b):
"""
Fourier domain convolution between two matrices
Input:
a, b: two square matrices to be convoluted with each other
Output:
c: the result of the convolution between a and b
"""
# Make sure matrices are square
if a.shape[0] != a.shape[1] or b.shape[0] != b.shape[1]:
raise Exception('Please enter square matrices')
# Add padding to the matrices
a_pad = np.pad(np.array(a), [0, b.shape[0]-1], mode='constant')
b_pad = np.pad(np.array(b), [0, a.shape[0]-1], mode='constant')
# Convolve the image and crop the edges
edge = np.minimum(a.shape[0], b.shape[0])/2
c = np.fft.ifft2(np.fft.fft2(a_pad)
*np.fft.fft2(b_pad))[int(np.floor(edge))
:-int(np.ceil(edge))+1,
int(np.floor(edge))
:-int(np.ceil(edge))+1]
return c
def numpy_or_cupy(cupy_req):
"""
Check if numpy or cupy should be used and load right libraries
Inputs:
cupy_req [bool]: is cupy required?
Outputs:
None
"""
global np, fft, rotate, cupy, mempool, pinned_mempool
if cupy_req:
import cupy as np
from cupy import fft
from cupyx.scipy.ndimage import rotate
cupy = 1
mempool = np.get_default_memory_pool()
pinned_mempool = np.get_default_pinned_memory_pool()
else:
import numpy as np
from numpy import fft
from scipy.ndimage import rotate
cupy = 0
def Downres_Image(setup):
"""
Reduce the resolution of the image, as to be used for the sh-sensor
deconvolutoin
TO BE REMOVED IN FUTURE VERSION
Inputs:
Setup: DFWS class
Outputs:
object_est: Downsampled image
"""
numpy_or_cupy(setup.cupy_req)
# Downsizes the main image from sensor to the size of the subaperture
# images
downres = np.around(np.arange(0, setup.res,
setup.res/(setup.res_SH
/setup.N))).astype('int')
object_est = setup.image[downres, :]
object_est = object_est[:, downres]
return object_est
def Run_SH_TIP(setup, iterations = 3, o_0 = None, psf_0 = None,
pupil = None, crop_index = None):
"""
Performs the TIP-alogirithm on the Shack-Hartmann image of setup in
order to find the Shack-Hartmann PSF
Based on Wilding et all (2017)
Inputs:
o_0 [optional]: initial estimate of object
psf_0 [optional]: initial estimate of PSF
pupil [optional]: mask for the containment of the estimated object
Outputs:
psf_est: Estimated shack-hartmann point-spread-function
o: Estimated SH object
"""
numpy_or_cupy(setup.cupy_req)
# free_gpu_memory()
# If no inital estimate is provided, use circular function with ones
if o_0 is None:
object_est = np.array(circle(setup.res_SH/setup.N/5,
int(np.ceil(setup.res_SH/setup.N)),
circle_centre=(0, 0), origin='middle'))
else:
object_est = o_0
if not psf_0 is None and not o_0 is None:
warnings.warn("Initial estimates of both object and psf are provided."
+" Only psf estimate will be used")
if not hasattr(setup, 'TIP_pad_o'):
setup.TIP_pad_o = [int(np.floor((setup.image_sh.shape[0]+1)/2)),
int(np.ceil((setup.image_sh.shape[0]+1)/2))]
# Initialize function for deconvolution
o_zero = fft.fftshift((np.pad(np.array(object_est),
setup.TIP_pad_o, mode='constant')))
i_F = fft.fft2((np.pad(np.array(setup.image_sh),
[0, int(object_est.shape[0]+1)], mode='constant')))
o_F = fft.fft2(o_zero)
# free_gpu_memory()
# Generate tip_pupil if not yet loaded into setup.
# This function limits the extent of the estimated object
if not hasattr(setup, 'tip_pupil'):
if pupil is None:
setup.tip_pupil = fft.fftshift(np.array(circle(setup.res_subap/2,
o_F.shape[0],
circle_centre=(0,
0),
origin='middle')))
setup.tip_pupil = setup.tip_pupil.astype('float16')
setup.tip_pupil += .1*(setup.tip_pupil == 0)
else:
setup.tip_pupil = pupil
# Run the TIP-algorithm
for n in range(0, iterations):
if n != 0 or psf_0 is None: # If not initial psf_estimate provided
# Estimate psf by deconvolution
psf_est_F = i_F/(o_F+(1)*(np.abs(o_F)<(1)))
psf_est = np.real(fft.ifft2(psf_est_F))
psf_est *= (psf_est>0)
# In all iterations except last, increase the contrast of the image
if n < iterations-1:
psf_est -= np.min(psf_est)
psf_est /= np.max(psf_est)
psf_est **= (iterations-n)/1
# psf_est[psf_est < (iterations-n)/10] = 0
# Normalize the psf and convert to frequency domain
psf_est -= np.min(psf_est)
psf_est /= np.sum(psf_est)
psf_est_F = fft.fft2(psf_est)
else: # If psf_estimate is provided
psf_0 /= np.sum(psf_0)
psf_est_F = fft.fft2(np.pad(psf_0,
[0, int(i_F.shape[0]-psf_0.shape[0])],
mode='constant'))
# In all iterations exeot last, find object by deconvolution
if n < iterations-1:
conj = np.conj(psf_est_F)
o_F = (conj*i_F)/(conj*psf_est_F+1e-9)
o = np.abs(fft.ifft2(o_F))
o *= setup.tip_pupil
o -= np.min(o)
o /= np.max(o)
o_F = fft.fft2(o)
# Crop the estimated PSF and normalize it
psf_est = np.real(psf_est)
psf_est = convert_680_to_128(setup, psf_est, crop_index)
psf_est -= np.min(psf_est)
psf_est /= np.max(psf_est)
setup.psf_sh_128 = psf_est
return psf_est, fft.fftshift(o)
def plot(image, figure = None):
"""
Show grayscale image, but without the hassle of making sure that
the data type and memory is right
Inputs:
image: 2d array representing gray-scale image
Outputs:
None
"""
if figure is None:
plt.figure()
else:
plt.figure(figure)
try:
plt.imshow(image.astype('float32'), cmap='gray')
except:
plt.imshow(image.astype('float32').get(), cmap='gray')
def plot_phi(phi, figure = None, vmin = None, vmax = None):
"""
Show wavefront image image, but without the hassle of making sure that
the data type and memory is right
Inputs:
image: 2d array representing the wavefront
Outputs:
None
"""
cmap = copy(plt.cm.get_cmap('RdYlGn'))
cmap.set_bad(alpha = 0)
image = copy(phi)
image[image == 0] = np.nan
if figure is None:
plt.figure()
else:
plt.figure(figure)
try:
plt.imshow(image.astype('float32'), cmap=cmap, vmin = vmin,
vmax = vmax)
except:
plt.imshow(image.astype('float32').get(), cmap=cmap, vmin = vmin,
vmax = vmax)
plt.axis('off')
def deconvolve_CPU(start, end, iterations):
# Function used for parallel deconvolution of image, do not call directly!
global o_F, i_F, psf_F, psf_mirror_F
tau = 1.5
for n in range(iterations):
o_F[start:end,] += tau*((i_F[start:end,]-psf_F[start:end,]
*o_F[start:end,])*psf_mirror_F[start:end,])
def deconvolve(setup, mode = 'LR', iterations = 10,
regularization_term = 1e-1, force_cuda = False, min_iterations = 20, max_iterations = 80):
"""
Deconvolves the image based on the loaded psf
Inputs:
mode: mode of deconvolution, options are: LR (lucy richardson),
LR2 (accelerated LR), CPU_LR (LR but parallel), Regularization,
Regularization_Filter, Regularization_Filter2
TIP, LR_Steepest_Descent and LR_preconditioning
Iterations: amount of iterations used for the iterative methods, set
iterations = 0 to stop when a stopping criterium is met
regularization_term: regularization used for the methods that are use it
force_cuda [bool]: use cuda eventhough cuda is not yet loaded
Outputs:
setup: DFWS class object with estimated object loaded
o_est: Estimated object
"""
global o_F, i_F, psf_F, psf_mirror_F
# Load numpy or cupy depending on the mode and hardware requirements:
if mode == 'CPU_LR':
numpy_or_cupy(0)
elif force_cuda:
numpy_or_cupy(1)
else:
numpy_or_cupy(setup.cupy_req)
# free_gpu_memory()
# Check if the setup class has an image loaded
if not hasattr(setup, 'image'):
raise Exception("Setup does not have an image loaded,"
+ " deconvolution cannot proceed")
# Initialize the variables and make sure the psf is normalized
setup.psf /= np.sum(setup.psf)
# Define the Fourier transform counterparts of the variables
pad_i = (setup.psf.shape[0]-1)/2
pad_psf = setup.image.shape[0]-1
i_F = fft.fft2((np.pad(np.array(setup.image), [int(np.floor(pad_i)),
int(np.ceil(pad_i))],
mode='reflect')))
psf_F = fft.fft2((np.pad(np.array(setup.psf), [0, pad_psf],
mode='constant')))
o_F = copy(i_F)
# If the deconvolution mode requires a mirrord psf, generate it
if 'LR' in mode or mode == 'Steepest_Descent':
psf_mirror_F = fft.fft2((np.pad(np.array(setup.psf[::-1, ::-1]),
[pad_psf, 0], mode='constant')))
# Lucy richardson: the deconvolution used is actually the landweber method,
# which is nearly identical to the lucy richardson deconvolution except
# that it is fully in the frequency domain, making it significantly faster
if mode == 'LR':
if iterations:
tau = 1.5
a = tau*i_F*psf_mirror_F
b = -1*tau*psf_F*psf_mirror_F
for n in range(iterations):
o_F += a + o_F*b
else:
tau = 1.5
a = tau*i_F*psf_mirror_F
b = -1*tau*psf_F*psf_mirror_F
for n in range(min_iterations-1):
o_F += a + o_F*b
score1 = np.sum((a + o_F*b)**2)
iterations = min_iterations
increment = a + o_F*b
o_F += increment
score = np.sum((increment)**2)
while score1*(1+1e-3) > score:
iterations += 1
if iterations >= max_iterations:
break
score1 = copy(score)
increment = a + o_F*b
o_F += increment
score = np.sum((increment)**2)
print(iterations, ' iterations completed')
# Lucy richardson V2: same as LR, except that the accelation parameter
# tau is sceduled
elif mode == 'LR2':
tau = 2.5
a = i_F*psf_mirror_F
b = -1*psf_F*psf_mirror_F
for n in range(iterations):
tau -= 1/iterations
o_F += tau*(a + o_F*b)
# Lucy richardson CPU: same as LR, but run parallel on the CPU. This
# may be nice if the graphics card does not support cuda
elif mode == 'CPU_LR':
threads = 4
steps = np.around(np.arange(0, i_F.shape[0]+.1, i_F.shape[0]/threads))
t = []
for i in range(threads):
t.append(threading.Thread(target=deconvolve_CPU,
args=(int(steps[i]),
int(steps[i+1]),iterations)))
t[i].start()
for i in range(threads):
t[i].join()
# Regularization method
elif mode == 'Regularization':
if iterations != 10:
print('Regularization mode does not work iteratively')
o_F = i_F/(psf_F+regularization_term*(psf_F<regularization_term))
# Regularization method but with added frequency domain filter
elif mode == 'Regularization_Filter':
if iterations != 10:
print('Regularization mode does not work iteratively')
psf_F = (psf_F+regularization_term*(psf_F<regularization_term))
o_F = i_F/(psf_F)
o_F_2 = np.log(1+np.abs(o_F))
o_F_2 -= np.min(o_F_2)
o_F_2 /= np.max(o_F_2)
if not hasattr(setup, 'Regularization_Filter'):
temp = -1*np.array(zernikeArray([4], o_F_2.shape[0]),
dtype='float32')[0,]
temp -= np.min(temp)
temp /= np.max(temp)
temp[np.where(temp==temp[0,0])] = 0
temp[np.where(np.fft.fftshift(np.array(circle(20,
o_F_2.shape[0]))))] = 1
temp **= 2
setup.Regularization_Filter = temp*.2+.8
o_F = o_F + (np.mean(o_F) - o_F)*(o_F_2>setup.Regularization_Filter)
# Regularization method but with a different added frequency domain filter
elif mode == 'Regularization_Filter2':
if iterations != 10:
print('Regularization mode does not work iteratively')
psf_F = (psf_F+regularization_term)#*(psf_F<regularization_term))
o_F = i_F/(psf_F)
if 1:#not hasattr(setup, 'G1'):
setup.a1 = np.pad(np.ones([3,3]),[2,2])
setup.a2 = np.ones([7,7])-setup.a1
setup.a2 /= np.sum(setup.a2)
setup.a2 = np.fft.fft2(np.pad(setup.a2, [0,
o_F.shape[0]
-setup.a2.shape[0]]))
setup.d = np.fft.fftshift(np.array(circle(20, o_F.shape[0])))
setup.e = (setup.d==0)
G1_shape = 5
setup.G1 = np.ones([G1_shape, G1_shape])
for i in range(G1_shape):
for j in range(G1_shape):
i1 = np.abs(int(np.floor(G1_shape/2))-i)
i2 = np.abs(int(np.floor(G1_shape/2))-j)
setup.G1[i, j] = 1 * np.exp(-1/4*(i1**2+i2**2))
setup.G1 = np.fft.fft2(np.pad(setup.G1,
[0, o_F.shape[0]-setup.G1.shape[0]]))
setup.threshold = 1.25
o_F_norm = np.abs(np.fft.fft2(o_F))
c2 = np.abs(np.fft.ifft2(o_F_norm*setup.a2))
b = ((c2/np.abs(o_F))>=setup.threshold)
b = np.fft.ifft2(np.fft.fft2(b)*(setup.G1))
b = ((1-b*setup.e))
b *= (b>0)
o_F *= b
# Tangantial iterative propogations. This method assumes that the PSF
# is just an estimate and is able to change it. Since there are not
# multiple images available, the performance is not great.
elif mode == 'TIP':
pupil = fft.fftshift(np.array(circle(60, o_F.shape[0],
circle_centre=(0, 0),
origin='middle'))).astype('float16')
for n in range(iterations):
o_F = i_F/(psf_F+regularization_term*(psf_F<regularization_term))
o = np.real(fft.ifft2(o_F))
o -= np.min(o)
o = o/np.max(o)
o_F = fft.fft2(o)
psf_F = i_F/(o_F+regularization_term*(o_F<regularization_term))
psf = np.abs(fft.ifft2(psf_F))
psf /= np.sum(psf)
psf *= pupil
psf_F = fft.fft2(psf)
# Steepest descent. Same as LR, but the acceleation parameter is sceduled
# based on the direction of steepest descent. Due to the calculation of
# norms not actually faster than the normal LR.
elif mode == 'LR_Steepest_Descent':
score = np.abs(np.sum((i_F-o_F*psf_F)**2))
r = i_F - psf_F * o_F
if iterations:
for n in range(iterations):
d = psf_mirror_F*r
w = psf_F*d
t = np.sqrt(np.sum(d**2))/np.sqrt(np.sum(w**2))
o_F += t*d
r -= t*w
else:
for n in range(min_iterations):
d = psf_mirror_F*r
w = psf_F*d
t = np.sqrt(np.sum(d**2))/np.sqrt(np.sum(w**2))
o_F += t*d
if not n:
score = np.mean((t*d)**2)
r -= t*w
iterations = min_iterations
score1 = np.mean((t*d)**2)
while score1 < score:
iterations += 1
if iterations > max_iterations:
break
score = copy(score1)
d = psf_mirror_F*r
w = psf_F*d
t = np.sqrt(np.sum(d**2))/np.sqrt(np.sum(w**2))
o_F += t*d
r -= t*w
score1 = np.mean((t*d)**2)
print(iterations, ' iterations completed')
elif mode == 'LR_Hybrid':
score = np.sqrt(np.sum((i_F-o_F*psf_F)**2))
r = i_F - psf_F * o_F
for n in range(5):
d = psf_mirror_F*r
w = psf_F*d
t = np.sqrt(np.sum(d**2))/np.sqrt(np.sum(w**2))
# numpy.linalg.norm(d,2)/numpy.linalg.norm(w,2)
o_F += t*d
r -= t*w
tau = 1.5
a = tau*i_F*psf_mirror_F
b = -1*tau*psf_F*psf_mirror_F
if iterations:
for n in range(iterations-5):
o_F += a + o_F*b
else:
for n in range(10):
o_F += a + o_F*b
score1 = np.sqrt(np.sum((i_F-o_F*psf_F)**2))
while score1 < score:
score = copy(score1)
o_F += a + o_F*b
score1 = np.sqrt(np.sum((i_F-o_F*psf_F)**2))
# LR with preconditioning. Uses less iterations than LR to converge,
# but the iterations take longer. This makes it in practice not actually
# faster
elif mode == 'LR_preconditioning':
tau = 1
r = i_F - psf_F * o_F
M = np.diag(np.dot(psf_F.T, psf_F)) + tau * np.tril(np.dot(psf_F.T,
psf_F))
v = np.linalg.inv(M)*r
d = psf_mirror_F*v
for n in range(iterations):
o_F += tau*d
r = i_F - psf_F * o_F
M = np.diag(np.dot(psf_F.T, psf_F)) + tau * np.tril(np.dot(psf_F.T,
psf_F))
v = np.linalg.inv(M)*r
d = psf_mirror_F*v
else:
raise Exception('Mode of deconvolution not recognized, options are: LR'
+ '(lucy richardson), LR2 (accelerated LR), CPU_LR'
+' (LR but parallel), Regularization, Regularization_'
+ 'Filter, Regularization_Filter2, TIP, LR_Steepest_'
+'Descent and LR_preconditioning')
# Calculate the output and normalize it
output = np.abs(fft.ifft2(o_F))
output -= np.min(output)
output /= np.max(output)
# free_gpu_memory()
return setup, tonumpy(output[0:setup.image.shape[0],
0:setup.image.shape[0]]).astype('float16')
def convert_680_to_256(setup, psf_est):
"""
WILL BE CHANGED TO CONVERT_X_TO_Y IN FUTURE VERSIONS IN ORDER TO
COMBINE THESE SIMILAR FUNCTIONS
Reduce black space within SH image to 256 pixels
Input:
psf_est: shack-hartmann patterns, estimated or true
Output:
psf_est: cropped input
"""
begin = numpy.array(numpy.round((setup.res_SH/setup.N
*numpy.arange(1,setup.N+1)
-setup.res_SH/(setup.N*2))
-256/(setup.N*2)), dtype='int16')
end = numpy.array(numpy.round((setup.res_SH/setup.N
*numpy.arange(1,setup.N+1)
-setup.res_SH/(setup.N*2))
+256/(setup.N*2)), dtype='int16')
index = numpy.array(numpy.r_[int(begin[0]):int(end[0])], dtype='int16')
for i in range(1, setup.N):
index = numpy.r_[index, int(begin[i]):int(end[i])]
index = numpy.r_[index, int(end[setup.N-1]+1)]
index = np.array(index)
return psf_est[index][:,index]
def convert_680_to_128(setup, psf_est, index = None):
"""
WILL BE CHANGED TO CONVERT_X_TO_Y IN FUTURE VERSIONS IN ORDER TO
COMBINE THESE SIMILAR FUNCTIONS
Reduce black space within SH image to 128 pixels
Input:
psf_est: shack-hartmann patterns, estimated or true
index [optional]: cropping index that overrides the standard cropping.
this can be usefull if the spacing of the SH-sensor is
a little off
Output:
psf_est: cropped input
"""
if index is None:
if not hasattr(setup, 'setup.SH_crop_index'):
if setup.N == 10:
begin = numpy.array(numpy.round((setup.res_SH/setup.N
*numpy.arange(1,setup.N+1)
-setup.res_SH/(setup.N*2))-7),
dtype='int16')
end = numpy.array(numpy.round((setup.res_SH/setup.N
*numpy.arange(1,setup.N+1)
-setup.res_SH/(setup.N*2))+6),
dtype='int16')
else:
begin = numpy.array(numpy.round((setup.res_SH/setup.N
*numpy.arange(1,setup.N+1)
-setup.res_SH/(setup.N*2))
-128/(setup.N*2)), dtype='int16')
end = numpy.array(numpy.round((setup.res_SH/setup.N
*numpy.arange(1,setup.N+1)
-setup.res_SH/(setup.N*2))
+128/(setup.N*2)), dtype='int16')
index = numpy.array(numpy.r_[int(begin[0]):int(end[0])],
dtype='int16')
for i in range(1, setup.N):
index = numpy.r_[index, int(begin[i]):int(end[i])]
index = np.array(index)
setup.SH_crop_index = index
else:
index = setup.SH_crop_index
out = np.array(psf_est)[index][:,index]
else:
psf_est = psf_est[index,][:, index]
out = convert_256_to_128(setup, psf_est)
return out
def convert_680_to_128_test(setup, psf_est):
"""
WILL BE CHANGED TO CONVERT_X_TO_Y IN FUTURE VERSIONS IN ORDER TO
COMBINE THESE SIMILAR FUNCTIONS
Reduce black space within SH image to 128 pixels
Rather than cropping, it overlaps the subaperture PSFs, which avoids
the PSFs
being cut off. The noise is, however, also overlapped, which can cause
problems
Input:
psf_est: shack-hartmann patterns, estimated or true
Output:
psf_est: cropped input
"""
begin = numpy.array(numpy.round((setup.res_SH/setup.N
*numpy.arange(1,setup.N+1)
-setup.res_SH/(setup.N*2))
-128/(setup.N*2)), dtype='int16')
end = numpy.array(numpy.round((setup.res_SH/setup.N
*numpy.arange(1,setup.N+1)
-setup.res_SH/(setup.N*2))
+128/(setup.N*2)), dtype='int16')
border = min(begin[0], 680-end[-1])
out = numpy.zeros([128+2*border, 128+2*border])
subap_size = end-begin
for i in range(setup.N):
for j in range(setup.N):
out[np.sum(subap_size[0:i])
:np.sum(subap_size[0:i+1])
+2*border, np.sum(subap_size[0:j])
:np.sum(subap_size[0:j+1])+2*border] += psf_est[begin[i]-border:
end[i]+border,
begin[j]-border
:end[j]+border]
return out[border:-border, border:-border]
def convert_256_to_128(setup, psf_est, offset = 0):
"""
WILL BE CHANGED TO CONVERT_X_TO_Y IN FUTURE VERSIONS IN ORDER TO COMBINE
THESE SIMILAR FUNCTIONS
Reduce black space within SH image to 128 pixels from 256 pixels
Input:
psf_est: shack-hartmann patterns, estimated or true
Output:
psf_est: cropped input
"""
res = 256+2*offset
res_new = 128
begin = -offset+numpy.array(numpy.floor((res/setup.N
*numpy.arange(1,setup.N+1)
-res/(setup.N*2))
-res_new/(setup.N*2)),dtype='int32')
end = -offset+numpy.array(numpy.floor((res/setup.N
*numpy.arange(1,setup.N+1)
-res/(setup.N*2))
+res_new/(setup.N*2)), dtype='int32')
index = numpy.array(numpy.r_[int(begin[0]):int(end[0])], dtype='int16')
for i in range(1, setup.N):
index = numpy.r_[index, int(begin[i]):int(end[i])]
index = numpy.r_[index, int(end[setup.N-1]+1)]
index = np.array(index)
return psf_est[index][:,index]
def Allign_Shack_Hartmann(setup):
"""
Allign SH image based on rotation and shift data in 'rotate_shift_data.npz'
Inputs:
setup: DFWS class object
Outputs:
setup: DFWS class object but with alligned shack-hartmann image
"""
numpy_or_cupy(setup.cupy_req)
# load allignment data from file
if not hasattr(setup, 'alpha'):
data = np.load('rotate_shift_data.npz')
setup.angle = data['name1']
setup.y0 = data['name2']
setup.y1 = data['name3']
setup.x0 = data['name4']
setup.x1 = data['name5']
# rotate and crop the shack-hartmann image
setup.image_sh = rotate(setup.image_sh, setup.angle)[setup.y0:setup.y1,
setup.x0:setup.x1]
return setup
def get_Wavefront_slopes(setup, obj_est = None):
"""
Returns the slopes of the wavefront estimated from a correlation algorithm
i.e. conventional way of extended scene wavefront sensing
Input:
setup: DFWS class object
obj_est [optional]: estimated object (to be used as reference image)
Output:
Slopes: 3d array containing all the x and y slopes of the
"""
# setup the size of the reference image and retrieve the reference image
border = 20
if obj_est is None:
ref = np.zeros([2*border, 2*border])
# for i in range(2,4):
# for j in range(2,4):
i = int(setup.N/2); j = int(setup.N/2)
x = int(setup.res/(setup.N*2)+setup.res/setup.N*i)
y = int(setup.res/(setup.N*2)+setup.res/setup.N*j)
ref += setup.image_sh[x-border:x+border, y-border:y+border]
ref /= np.max(ref)
else:
ref = obj_est[int((obj_est.shape[0]-2*border)/2)
:int((obj_est.shape[0]-2*border)/2)+2*border,
int((obj_est.shape[0]-2*border)/2)
:int((obj_est.shape[0]-2*border)/2)+2*border]
# run the cross correlation algorithm in order to find the shifts
corr = np.zeros([setup.res_SH-2*border, setup.res_SH-2*border])
for i in (range(setup.res_SH-2*border)):
for j in range(setup.res_SH-2*border):
corr[i,j] = np.sum(np.abs(setup.image_sh[i:i+2*border,
j:j+2*border]-ref)**2)
# convert the cross correlation matrix to wavefront slopes
centers_int = (setup.res/(setup.N*2)+setup.res/setup.N
*np.arange(0, setup.N)-border).astype('uint64')
slopes = np.zeros([setup.N, setup.N, 2])
for i in range(setup.N):
for j in range(setup.N):
# skip some subapertures, for now done manually
if min(setup.N-1-i, i)+min(setup.N-1-j, j) < 2 and setup.N == 6:
continue
# normalize image
im = corr[int(centers_int[i]-border):int(centers_int[i]+border),
int(centers_int[j]-border):int(centers_int[j]+border)]
im -= np.min(im)
im /= np.max(im)
im += .1
# find the minimum value of image
x = int(np.median(np.where(im==np.min(im))[0]))
y = int(np.median(np.where(im==np.min(im))[1]))
# interpole the location of the minimum value for sub-pixel accuray
if im[x+1,y] < im[x-1,y]:
slopes[i, j, 1] = (x-.5*(im[x+1,y]-im[x-1, y])
/(im[x+1,y]-2*im[x,y]+im[x-1,y])
- border)
elif im[x+1,y] > im[x-1,y]:
slopes[i, j, 1] = (x+.5*(im[x-1,y]-im[x+1, y])
/(im[x-1,y]-2*im[x,y]+im[x+1,y])
- border)
else:
slopes[i, j, 1] = x
if im[x,y+1] < im[x,y-1]:
slopes[i, j, 0] = (y-.5*(im[x,y+1]-im[x,y-1])
/(im[x,y+1]-2*im[x,y]+im[x,y-1])
- border)
elif im[x,y+1] > im[x,y-1]:
slopes[i, j, 0] = (y+.5*(im[x,y-1]-im[x,y+1])/
(im[x,y-1]-2*im[x,y]+im[x,y+1])
- border)
else:
slopes[i, j, 0] = y
# Errors from slopes, for now done manually
# to find this matrix, load a flat wavefront and retrieve the slopes,
# these slopes are removed here
if setup.N == 8:
slopes -= np.array([[[ 0, 0], [ 0, 0], [ 0, 0], [ 2.49214765e-01, 6.06005467e-02], [-2.43867886e-03, 6.51673048e-02], [ 0, 0], [ 0, 0], [ 0, 0]], [[ 0, 0], [ 1.26021137e-01, 1.40449116e-01], [ 1.61539538e-01, 7.53880468e-02], [ 2.31480458e-01, 6.46776544e-02], [ 4.46428560e-04, 7.37487723e-02], [ 7.85162090e-02, 8.24028726e-02], [ 9.18516793e-02, 1.53844554e-01], [ 0, 0]], [[ 0, 0], [ 7.07881886e-02, 1.61335372e-01], [ 1.55958663e-01, 1.54818575e-01], [ 2.32523066e-01, 1.52663273e-01], [ 6.86735013e-04, 1.57221381e-01], [ 8.12891502e-02, 1.57301272e-01], [ 1.61515288e-01, 1.53466387e-01], [ 0, 0]], [[ 6.41848990e-02, 2.49900357e-01], [ 6.40621324e-02, 2.45348770e-01], [ 1.45220300e-01, 2.41369259e-01], [ 2.33756896e-01, 2.33699576e-01], [-7.12976917e-03, 2.36579861e-01], [ 7.54330625e-02, 2.40466783e-01], [ 1.58218462e-01, 2.48129523e-01], [ 1.58871532e-01, 2.56868945e-01]], [[ 6.31023923e-02, 4.24469389e-03], [ 7.05156546e-02, -4.91059888e-04], [ 1.52168511e-01, -2.50537007e-03], [ 2.43723214e-01, -1.59453223e-03], [-1.84971705e-04, -8.23751100e-04], [ 8.25983526e-02, 3.90210599e-03], [ 1.70600227e-01, -1.94522574e-03], [ 1.62476694e-01, -1.63321967e-02]], [[ 0, 0], [ 7.37600329e-02, 8.06012454e-02], [ 1.46580219e-01, 8.51030727e-02], [ 2.34301007e-01, 7.74258101e-02], [-4.54505208e-03, 8.85571013e-02], [ 8.34133964e-02, 8.46985159e-02], [ 1.69218255e-01, 7.71526022e-02], [ 0, 0]], [[ 0, 0], [ 1.66599356e-01, 1.14567227e-01], [ 1.48682772e-01, 1.62169471e-01], [ 2.36729042e-01, 1.65530106e-01], [-7.18202858e-03, 1.76395115e-01], [ 6.82716761e-02, 1.65037399e-01], [ 7.52139982e-02, 9.77784254e-02], [ 0, 0]], [[ 0, 0], [ 0, 0], [ 0, 0], [ 2.60798165e-01, 1.88154443e-01], [-1.56122997e-02, 1.75102584e-01], [ 0, 0], [ 0, 0], [ 0, 0]]])
if setup.N == 6:
slopes -= np.array([[[ 0, 0], [ 0, 0], [ 8.24572859e-03, -1.06432543e-01], [ 5.99529896e-03, -9.56992552e-03], [ 0, 0], [ 0, 0]], [[ 0, 0], [-5.83200145e-01, -5.82559357e-01], [-5.67643573e-01, -1.17170839e-01], [-5.71454886e-01, -8.44801997e-03], [-5.75496409e-01, -5.72065171e-01], [ 0, 0]], [[-1.08936067e-01, 1.25730858e-02], [-1.18221616e-01, -5.68024875e-01], [-1.06968880e-01, -1.07323550e-01], [-1.09466605e-01, 2.63584106e-03], [-1.16971681e-01, -5.54453151e-01], [-1.06919368e-01, -1.28305822e-01]], [[-5.03460532e-03, 7.68953472e-03], [-1.08713759e-02, -5.72399052e-01], [ 4.45833122e-03, -1.09633420e-01], [ 2.11680675e-05, -2.39525975e-06], [-5.06640915e-03, -5.56343821e-01], [-1.81281834e-03, -1.27903215e-01]], [[ 0, 0], [-5.67888652e-01, -5.75334994e-01], [-5.51749824e-01, -1.17642141e-01], [-5.58120205e-01, -1.00138714e-02], [-5.72177928e-01, -5.77601013e-01], [ 0, 0]], [[ 0, 0], [ 0, 0], [-1.25290046e-01, -1.13314551e-01], [-1.19016532e-01, -4.28573584e-03], [ 0, 0], [ 0, 0]]])
slopes[:, :, 0]-=np.mean(slopes[:,:,0])
slopes[:, :, 1]-=np.mean(slopes[:,:,1])
slopes[0:2, 0, ] = 0
slopes[4:, 0, ] = 0
slopes[0, 0:2, ] = 0
slopes[0, 4:, ] = 0
slopes[0:2, 5, ] = 0
slopes[4:, 5, ] = 0
slopes[5, 0:2, ] = 0
slopes[5, 4:, ] = 0
slopes *= -1.7
return slopes
def Get_Slopes_From_Wavefront(setup):
"""
return slopes of the wavefront, based on the currently loaded wavefront,
not on the SH image!
TO BE REMOVED IN FUTURE VERSION
"""
warnings.warn("This function returns slopes directly from wavefront,"
+"it accesses information normally not available")
basis = np.zeros([2, setup.res_subap**2])
basis[0,] = np.tile(np.arange(0, setup.res_subap),
(setup.res_subap, 1)).reshape([setup.res_subap**2])
basis[1,] = np.tile(np.arange(0, setup.res_subap),
(setup.res_subap, 1)).T.reshape([setup.res_subap**2])
basis /= np.max(basis)
basis_inv = np.linalg.pinv(basis)
slopes = np.zeros([setup.N, setup.N, 2])
for i in range(setup.N):
for ii in range(setup.N):
if min(setup.N-1-i, i)+min(setup.N-1-ii, ii) < 2:
continue
curr = setup.wavefront[int(np.floor(setup.res_SH/setup.N*i))
:int(np.floor(setup.res_SH/setup.N*i)
+setup.res_subap),
int(np.floor(setup.res_SH/setup.N*ii))
:int(np.floor(setup.res_SH/setup.N*ii)
+setup.res_subap)]
curr = curr.reshape([setup.res_subap**2])
slopes[i, ii, ] = np.dot(curr, basis_inv)
return slopes
def Get_Wavefront_From_Slopes(setup, slopes):
"""
Returns a fit of zernike polynomials from the given slopes
Inputs:
setup: DFWS class object
slopes: retrieved wavefront slopes
Outputs: