-
Notifications
You must be signed in to change notification settings - Fork 0
/
contrast.py
1307 lines (1169 loc) · 51.3 KB
/
contrast.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
# -*-coding:utf-8 -*
import numpy as np
import pickle
import pyfftw
import matplotlib.pyplot as plt
import matplotlib.colors as colors
from scipy.ndimage import gaussian_filter
from skimage.restoration import unwrap_phase
from skimage import filters, measure, morphology
from skimage.segmentation import clear_border, flood
from scipy import optimize
from scipy import constants as cst
from scipy import interpolate
import cupy as cp
from numbalsoda import lsoda_sig, lsoda
from simus.nlse import NLSE_1d
import numba
from numba import cuda
import cmath
import math
import multiprocessing
pyfftw.interfaces.cache.enable()
pyfftw.config.NUM_THREADS = multiprocessing.cpu_count()//2
# try to load previous fftw wisdom
try:
with open("fft.wisdom", "rb") as file:
wisdom = pickle.load(file)
pyfftw.import_wisdom(wisdom)
except FileNotFoundError:
print("No FFT wisdom found, starting over ...")
def gauss_fit(x, waist, mean):
"""Gaussian BEAM intensity fitting
Attention !!! Different convention as for a regular gaussian
Args:
x (float): Position
waist (float): Waist
mean (float): center
Returns:
float: Gaussian
"""
return np.exp(-2*(x-mean)**2/waist**2)
@numba.njit(parallel=True, cache=True, fastmath=True)
def az_avg(image: np.ndarray, center: tuple) -> np.ndarray:
"""Calculates the azimuthally averaged radial profile.
Args:
image (np.ndarray): The 2D image
center (tuple, optional): The [x,y] pixel coordinates used as the center. Defaults to None,
which then uses the center of the image (including fractional pixels).
Returns:
np.ndarray: prof the radially averaged profile
"""
# Calculate the indices from the image
max_r = max([np.hypot(center[0], center[1]),
np.hypot(center[0]-image.shape[1], center[1]),
np.hypot(center[0]-image.shape[1],
center[1]-image.shape[0]),
np.hypot(center[0], center[1]-image.shape[0])])
r = np.arange(1, int(max_r)+1, 1)
prof = np.zeros_like(r, dtype=np.float64)
prof_counts = np.zeros_like(r)
for i in numba.prange(image.shape[0]):
for j in range(image.shape[1]):
dist = round(np.hypot(i-center[1], j-center[0]))
prof[dist] += image[i, j]
prof_counts[dist] += 1
prof /= prof_counts
return prof
@cuda.jit(fastmath=True)
def _az_avg_cp(image: cp.ndarray, prof: cp.ndarray, prof_counts: cp.ndarray, center: tuple):
"""Kernel for azimuthal average calculation
Args:
image (cp.ndarray): The image from which to calculate the azimuthal average
prof (cp.ndarray): A vector containing the bins
prof_counts (cp.ndarray): A vector of same size as prof to count each bin
"""
i, j = numba.cuda.grid(2)
if i < image.shape[0] and j < image.shape[1]:
dist = round(math.sqrt((i-center[1])**2+(j-center[0])**2))
prof[dist] += image[i, j]
prof_counts[dist] += 1
def az_avg_cp(image: cp.ndarray, center: tuple) -> cp.ndarray:
"""Calculates the azimuthally averaged radial profile.
Args:
image (cp.ndarray): The 2D image
center (tuple): The [x,y] pixel coordinates used as the center. Defaults to None,
which then uses the center of the image (including fractional pixels).
Returns:
cp.ndarray: prof the radially averaged profile
"""
# Calculate the indices from the image
R = cp.empty_like(image)
max_r = max([cp.hypot(center[0], center[1]),
cp.hypot(center[0]-image.shape[1], center[1]),
cp.hypot(center[0]-image.shape[1],
center[1]-image.shape[0]),
cp.hypot(center[0], center[1]-image.shape[0])])
r = cp.arange(1, int(max_r)+1, 1)
prof = cp.zeros_like(r, dtype=np.float32)
prof_counts = cp.zeros_like(r, dtype=np.float32)
tpb = 16
bpgx = math.ceil(image.shape[0]/tpb)
bpgy = math.ceil(image.shape[1]/tpb)
_az_avg_cp[(bpgx, bpgy), (tpb, tpb)](image, prof, prof_counts, center)
prof /= prof_counts
return prof
@numba.njit(numba.float32[:, :](numba.complex64[:, :]), fastmath=True, cache=True, parallel=True)
def angle_fast(x: np.ndarray) -> np.ndarray:
"""Accelerates a smidge angle by using fastmath
Args:
x (np.ndarray): The complex field
Returns:
np.ndarray: the argument of the complex field
"""
out = np.empty_like(x, dtype=np.float32)
for i in numba.prange(x.shape[0]):
for j in range(x.shape[1]):
out[i, j] = cmath.phase(x[i, j])
return out
@cuda.jit((numba.complex64[:, :], numba.float32[:, :]), fastmath=True)
def angle_fast_cp(x: cp.ndarray, out: cp.ndarray):
"""Accelerates a smidge angle by using fastmath
Args:
x (np.ndarray): The complex field
Returns:
np.ndarray: the argument of the complex field
"""
i, j = cuda.grid(2)
if i < x.shape[0]:
if j < x.shape[1]:
out[i, j] = cmath.phase(x[i, j])
def centre(im, truncate: bool = True):
"""Fits the center of the image using gaussian fitting
Args:
im (np.ndarray): The image to fit
Returns:
Tuple(int): The coordinates of the fitted center.
"""
out_x = np.sum(im, axis=0)
out_x = out_x/np.max(out_x)
out_y = np.sum(im, axis=1)
out_y = out_y/np.max(out_y)
absc = np.linspace(0, im.shape[1]-1, im.shape[1])
ordo = np.linspace(0, im.shape[0]-1, im.shape[0])
p0x = np.argmax(out_x)
p0y = np.argmax(out_y)
ptot, pcov = optimize.curve_fit(gauss_fit, absc, out_x, p0=[
p0x, len(absc)//2], maxfev=3200)
centre_x = ptot[1]
ptot, pcov = optimize.curve_fit(gauss_fit, ordo, out_y, p0=[
p0y, len(ordo)//2], maxfev=3200)
centre_y = ptot[1]
if truncate:
centre_x = int(centre_x)
centre_y = int(centre_y)
return centre_x, centre_y
def waist(im, plot=False):
"""Fits the waist of the image using gaussian fitting
Args:
im (np.ndarray): The image to fit
Returns:
Tuple(int): The coordinates of the fitted waists.
"""
out_x = np.sum(im, axis=0)
out_x = out_x/np.max(out_x)
out_y = np.sum(im, axis=1)
out_y = out_y/np.max(out_y)
absc = np.linspace(0, im.shape[1]-1, im.shape[1])
ordo = np.linspace(0, im.shape[0]-1, im.shape[0])
poptx, pcov = optimize.curve_fit(gauss_fit, absc, out_x, p0=[
100, len(absc)//2], maxfev=3200)
waist_x = poptx[0]
perrx = np.sqrt(np.diag(pcov))[0]
popty, pcov = optimize.curve_fit(gauss_fit, ordo, out_y, p0=[
100, len(ordo)//2], maxfev=3200)
waist_y = popty[0]
perry = np.sqrt(np.diag(pcov))[0]
if plot:
fig, ax = plt.subplots(1, 2)
ax[0].plot(absc, out_x)
tex = r"$w_x$"
pm = r'$\pm$'
lab = f'{tex} = {waist_x:.1f} {pm} {perrx:.1f}'
ax[0].plot(absc, gauss_fit(absc, *poptx), ls='--', label=lab)
ax[1].plot(ordo, out_y)
tex = r"$w_y$"
lab = f'{tex} = {waist_y:.1f} {pm} {perry:.1f}'
ax[1].plot(ordo, gauss_fit(ordo, *popty), ls='--', label=lab)
ax[0].legend()
ax[1].legend()
plt.show(block=False)
return waist_x, waist_y
def cache(radius: int, center: tuple = (1024, 1024), out: bool = True,
nb_pix: tuple = (2048, 2048)) -> np.ndarray:
"""Defines a circular mask
Args:
radius (int): Radius of the mask
center (tuple, optional): Center of the mask. Defaults to (1024, 1024).
out (bool, optional): Masks the outside of the disk. Defaults to True.
nb_pix (tuple, optional): Shape of the mask. Defaults to (2048, 2048).
Returns:
np.ndarray: The array of booleans defining the mask
"""
Y, X = np.ogrid[:nb_pix[0], :nb_pix[1]]
dist_from_center = np.hypot(X - center[0], Y-center[1])
if out:
mask = dist_from_center <= radius
else:
mask = dist_from_center > radius
return mask
def cache_cp(radius: int, center: tuple = (1024, 1024), out: bool = True,
nb_pix: tuple = (2048, 2048)) -> np.ndarray:
"""Defines a circular mask
Args:
radius (int): Radius of the mask
center (tuple, optional): Center of the mask. Defaults to (1024, 1024).
out (bool, optional): Masks the outside of the disk. Defaults to True.
nb_pix (tuple, optional): Shape of the mask. Defaults to (2048, 2048).
Returns:
np.ndarray: The array of booleans defining the mask
"""
Y, X = cp.ogrid[:nb_pix[0], :nb_pix[1]]
dist_from_center = cp.hypot(X - center[0], Y-center[1])
if out:
mask = dist_from_center <= radius
else:
mask = dist_from_center > radius
return mask
def im_osc(im: np.ndarray, cont: bool = False, plot: bool = False, return_mask: bool = False, big: bool = False) -> tuple:
"""Separates the continuous and oscillating components of an image using
Fourier filtering.
:param np.ndarray im: Description of parameter `im`.
:param bool cont: Returns or not the continuons component
:param bool plot: Plots a visualization of the analysis result
:return np.ndarray: The oscillating component of the image, or both
components
"""
im = im.astype(np.float32)
im_fft = pyfftw.interfaces.numpy_fft.rfft2(im)
im_fft_orig = im_fft.copy()
im_fft_fringe = pyfftw.zeros_aligned(
(im.shape[0], im.shape[1]), dtype=np.complex64)
im_fft_cont = im_fft.copy()
fft_filt = gaussian_filter(np.abs(im_fft), 1e-3*im_fft.shape[0])
cont_size = im.shape[0]//4
mask_cont = cache(cont_size, out=False, center=(0, 0),
nb_pix=im_fft_cont.shape)
mask_cont = np.logical_xor(mask_cont, cache(cont_size, out=False,
center=(
0, im_fft_cont.shape[0]),
nb_pix=im_fft_cont.shape))
im_fft_cont[np.logical_not(mask_cont)] = 0
im_cont = pyfftw.interfaces.numpy_fft.irfft2(im_fft_cont)
dbl_gradient = np.log(np.abs(np.gradient(fft_filt, axis=0)) +
np.abs(np.gradient(fft_filt, axis=1)))
m_value = np.nanmean(dbl_gradient[dbl_gradient != -np.infty])
dbl_gradient[mask_cont] = m_value
dbl_gradient_int = (
2**16*(dbl_gradient/np.nanmax(dbl_gradient))).astype(np.uint16)
threshold = filters.threshold_otsu(dbl_gradient_int)
mask = dbl_gradient_int > threshold
mask = morphology.remove_small_objects(mask, 1)
mask = morphology.remove_small_holes(mask, 1)
mask = clear_border(mask)
mask = morphology.remove_small_holes(mask, 1, connectivity=2)
labels = measure.label(mask)
props = measure.regionprops(labels, dbl_gradient_int)
# takes the spot with the maximum area
areas = [prop.area for prop in props]
maxi_area = np.where(areas == max(areas))[0][0]
label_osc = props[maxi_area].label
center_osc = np.round(props[maxi_area].centroid).astype(int)
contour_osc = measure.find_contours(labels == label_osc, 0.5)[0]
y, x = contour_osc.T
y = y.astype(int)
x = x.astype(int)
mask_osc = np.zeros(im_fft.shape)
mask_osc[y, x] = 1
mask_osc_flood = flood(mask_osc, (y[0]+1, x[0]+1), connectivity=1)
if big:
# r_osc = min(center_osc)
r_osc = 1.9*np.max([[np.hypot(x[i]-x[j], y[i]-y[j])
for j in range(len(x))] for i in range(len(x))])
mask_osc_flood = cache(r_osc, out=False, center=(
center_osc[1], center_osc[0]), nb_pix=im_fft.shape)
im_fft[mask_osc_flood] = 0
# bring osc part to center to remove tilt
im_fft = np.roll(im_fft,
(im_fft.shape[0]//2-center_osc[0],
im_fft.shape[1]//2-center_osc[1]),
axis=(-2, -1))
im_fft_fringe[:, im_fft.shape[1] //
2:im_fft_fringe.shape[1]//2+im_fft.shape[1]//2+1] = im_fft
im_fringe = pyfftw.interfaces.numpy_fft.ifft2(
np.fft.fftshift(im_fft_fringe), s=im.shape, axes=(-1, -2))
# save FFT wisdom
with open("fft.wisdom", "wb") as file:
wisdom = pyfftw.export_wisdom()
pickle.dump(wisdom, file)
if plot:
fig, ax = plt.subplots(1, 4)
im0 = ax[0].imshow(im, cmap='gray')
ax[0].set_title("Real space")
fig.colorbar(im0, ax=ax[0])
im = ax[1].imshow(np.log10(np.abs(im_fft_orig)+1e-15))
fig.colorbar(im, ax=ax[1])
ax[1].plot(x, y, color='r', ls='--')
if big:
circle_big = plt.Circle((center_osc[1], center_osc[0]), r_osc, color='r',
fill=False)
ax[1].add_patch(circle_big)
ax[1].set_title("Fourier space")
ax[1].legend(["Oscillating", "Continuous"])
im = ax[2].imshow(np.log10(np.abs(im_fft)+1e-15))
fig.colorbar(im, ax=ax[2])
ax[2].set_title("Filtered Fourier signal")
im = ax[3].imshow(np.angle(im_fringe), cmap="twilight")
fig.colorbar(im, ax=ax[3])
ax[3].set_title("Phase of filtered signal")
plt.show()
if cont:
if return_mask:
return im_cont, im_fringe, mask_cont_flood, mask_osc_flood, center_osc
return im_cont, im_fringe
if return_mask:
return im_fringe, mask_cont_flood, mask_osc_flood, center_osc
return im_fringe
def im_osc_center(im: np.ndarray, center: tuple, mask_osc_flood: np.ndarray = None, cont: bool = False, plot: bool = False, big: bool = False) -> tuple:
"""Separates the continuous and oscillating components of an image using
Fourier filtering.
:param np.ndarray im: Description of parameter `im`.
:param tuple center: i,j position of the 1st order
:param np.ndarray mask_osc_flood: mask for the 1st order
:param bool cont: Returns or not the continuons component
:param bool plot: Plots a visualization of the analysis result
:return np.ndarray: The oscillating component of the image, or both
components
"""
im_fft = np.fft.fftshift(pyfftw.interfaces.numpy_fft.fft2(im))
im_fft_fringe = im_fft.copy()
im_fft_cont = im_fft.copy()
fft_filt = gaussian_filter(np.abs(im_fft), 1e-3*im_fft.shape[0])
cont_size = 20
mask_cont_flood = cache(cont_size, out=False, center=(im.shape[0]//2, im.shape[1]//2),
nb_pix=im.shape)
if mask_osc_flood is None:
dbl_gradient = np.log(np.abs(np.gradient(fft_filt, axis=0)) +
np.abs(np.gradient(fft_filt, axis=1)))
m_value = np.nanmean(dbl_gradient[dbl_gradient != -np.infty])
dbl_gradient[np.bitwise_not(mask_cont_flood)] = m_value
dbl_gradient_int = (dbl_gradient*(dbl_gradient > 0.8 *
np.nanmax(dbl_gradient)))
dbl_gradient_int /= np.nanmax(dbl_gradient_int)
dbl_gradient_int = (255*dbl_gradient_int).astype(np.uint8)
threshold = filters.threshold_otsu(dbl_gradient_int)
mask = dbl_gradient_int > threshold
mask = morphology.remove_small_objects(mask, 1)
mask = morphology.remove_small_holes(mask, 1)
mask = clear_border(mask)
mask = morphology.remove_small_holes(mask, 1, connectivity=2)
labels = measure.label(mask)
props = measure.regionprops(labels, dbl_gradient_int)
# takes the spot with the maximum area
areas = [prop.area for prop in props]
maxi_area = np.where(areas == max(areas))[0][0]
label_osc = props[maxi_area].label
center_osc = center
contour_osc = measure.find_contours(labels == label_osc, 0.5)[0]
y, x = contour_osc.T
y = y.astype(int)
x = x.astype(int)
mask_osc = np.zeros(im_fft.shape)
mask_osc[y, x] = 1
mask_osc_flood = flood(mask_osc, (y[0]+1, x[0]+1), connectivity=1)
if big:
r_osc = np.max([[np.hypot(x[i]-x[j], y[i]-y[j])
for j in range(len(x))] for i in range(len(x))])
mask_osc_flood = cache(r_osc, out=False, center=(
center, center))
# mask_osc_flood = np.zeros(mask_cont_flood.shape, dtype=bool)
# mask_osc_flood[0:mask_osc_flood.shape[0] //
# 2, 0:mask_osc_flood.shape[1]//2] = True
# mask_osc_flood = np.logical_not(np.logical_and(
# mask_osc_flood, mask_cont_flood))
im_fft_fringe[mask_osc_flood] = 0
im_fft_cont[mask_cont_flood] = 0
# bring osc part to center to remove tilt
im_fft_fringe = np.roll(im_fft_fringe,
(im_fft_fringe.shape[0]//2-center[0],
im_fft_fringe.shape[1]//2-center[1]),
axis=(-2, -1))
im_fringe = pyfftw.interfaces.numpy_fft.ifft2(
np.fft.fftshift(im_fft_fringe))
im_cont = pyfftw.interfaces.numpy_fft.ifft2(np.fft.fftshift(im_fft_cont))
# save FFT wisdom
with open("fft.wisdom", "wb") as file:
wisdom = pyfftw.export_wisdom()
pickle.dump(wisdom, file)
if plot:
circle = plt.Circle((im.shape[1]//2, im.shape[0]//2), cont_size//2, color='b',
fill=False)
fig, ax = plt.subplots(1, 4)
im0 = ax[0].imshow(im, cmap='gray')
ax[0].set_title("Real space")
fig.colorbar(im0, ax=ax[0])
im = ax[1].imshow(np.abs(im_fft),
norm=colors.SymLogNorm(linthresh=0.03, linscale=0.03,
vmin=np.min(np.abs(im_fft)),
vmax=np.max(np.abs(im_fft)),
base=10))
fig.colorbar(im, ax=ax[1])
if mask_osc_flood is None:
ax[1].plot(x, y, color='r', ls='--')
else:
ax[1].imshow(mask_osc_flood, alpha=0.35, cmap='gray')
ax[1].add_patch(circle)
if big and mask_osc_flood is None:
circle_big = plt.Circle((center[1], center[0]), r_osc, color='r',
fill=False)
ax[1].add_patch(circle_big)
ax[1].set_title("Fourier space")
ax[1].legend(["Oscillating", "Continuous"])
im = ax[2].imshow(np.abs(im_fft_fringe),
norm=colors.SymLogNorm(linthresh=0.03, linscale=0.03,
vmin=np.min(np.abs(im_fft)),
vmax=np.max(np.abs(im_fft)),
base=10))
fig.colorbar(im, ax=ax[2])
ax[2].set_title("Filtered Fourier signal")
im = ax[3].imshow(np.angle(im_fringe), cmap="twilight")
fig.colorbar(im, ax=ax[3])
ax[3].set_title("Phase of filtered signal")
plt.show()
if cont:
return im_cont, im_fringe
return im_fringe
def im_osc_mask(im: np.ndarray, masks: tuple, cont: bool = True, plot: bool = False) -> tuple:
"""Separates the continuous and oscillating components of an image using
Fourier filtering.
:param np.ndarray im: Description of parameter `im`.
:param tuple masks: Continuous and oscillating masks
:param bool cont: Returns or not the continuons component
:param bool plot: Plots a visualization of the analysis result
:return np.ndarray: The oscillating component of the image, or both
components
"""
mask_cont_flood, mask_osc_flood, center_osc = masks
center_osc[0] = int(center_osc[0])
center_osc[1] = int(center_osc[1])
im_fft = np.fft.fftshift(pyfftw.interfaces.numpy_fft.fft2(im))
im_fft_fringe = im_fft.copy()
im_fft_cont = im_fft.copy()
im_fft_fringe[mask_osc_flood] = 0
im_fft_cont[mask_cont_flood] = 0
# bring osc part to center to remove tilt
im_fft_fringe = np.roll(im_fft_fringe,
(im_fft_fringe.shape[0]//2-center_osc[0],
im_fft_fringe.shape[1]//2-center_osc[1]),
axis=(-2, -1))
im_fringe = pyfftw.interfaces.numpy_fft.ifft2(
np.fft.fftshift(im_fft_fringe))
im_cont = pyfftw.interfaces.numpy_fft.ifft2(np.fft.fftshift(im_fft_cont))
# save FFT wisdom
with open("fft.wisdom", "wb") as file:
wisdom = pyfftw.export_wisdom()
pickle.dump(wisdom, file)
if plot:
fig, ax = plt.subplots(1, 4)
im0 = ax[0].imshow(im, cmap='gray')
ax[0].set_title("Real space")
fig.colorbar(im0, ax=ax[0])
im = ax[1].imshow(np.abs(im_fft),
norm=colors.SymLogNorm(linthresh=0.03, linscale=0.03,
vmin=np.min(np.abs(im_fft)),
vmax=np.max(np.abs(im_fft)),
base=10))
ax[1].scatter(center_osc[1], center_osc[0], color='red')
fig.colorbar(im, ax=ax[1])
ax[1].set_title("Fourier space")
im = ax[2].imshow(np.abs(im_fft_fringe),
norm=colors.SymLogNorm(linthresh=0.03, linscale=0.03,
vmin=np.min(np.abs(im_fft)),
vmax=np.max(np.abs(im_fft)),
base=10))
fig.colorbar(im, ax=ax[2])
ax[2].set_title("Filtered Fourier signal")
im = ax[3].imshow(np.angle(im_fringe), cmap="twilight")
fig.colorbar(im, ax=ax[3])
ax[3].set_title("Phase of filtered signal")
plt.show()
if cont:
return im_cont, im_fringe
return im_fringe
# objective function for Dn vs I fitting
@numba.cfunc(lsoda_sig, cache=True)
def dI_dz(z: float, I: np.ndarray, dI: np.ndarray, p: np.ndarray) -> None:
"""RHS to solve the intensity evolution
Args:
z (float): Position in the cell
I (np.ndarray): Intensity
dI (np.ndarray): Intensity derivative
p (np.ndarray): Parameters
"""
alpha = p[0]
Isat = p[1]
dI[0] = -alpha*I[0]/(1+I[0]/Isat)
@numba.cfunc(lsoda_sig, cache=True)
def dphi_dz(z: float, y: np.ndarray, dy: np.ndarray, p: np.ndarray) -> None:
"""RHS to solve the non linear phase evolution
Args:
z (float): Position in the cell
y (np.ndarray): The state vector (I, phi)
dy (np.ndarray): The derivative of the state vector (dI, dphi)
p (np.ndarray): The parameters
"""
I = y[0]
k = p[0]
alpha = p[1]
n2 = p[2]
Isat = p[3]
dy[0] = -alpha*I/(1+I/Isat)
dy[1] = k*n2*I/(1+I/Isat)
dI_dz_ptr = dI_dz.address
dphi_dz_ptr = dphi_dz.address
@numba.njit(parallel=True, fastmath=True)
def I_z(z: float, I0: np.ndarray, alpha: float, Isat: float) -> np.ndarray:
"""Returns the intensity after a propagation of z in the cell
Args:
z (float): Position in the cell
I0 (np.ndarray): Initial intensity
alpha (float): Linear losses coeff
Isat (float): Saturation intensity in W/m^2
Returns:
np.ndarray: The final intensity
"""
Iz = np.empty_like(I0)
t_eval = np.array([0, z], dtype=np.float64)
p = np.array([alpha, Isat], dtype=np.float64)
for i in numba.prange(I0.shape[0]):
usol, success = lsoda(
dI_dz_ptr, np.array([I0[i]], dtype=np.float64), t_eval, rtol=1e-6, atol=1e-6, data=p)
Iz[i] = usol[-1, 0]
return Iz
@numba.njit(parallel=True, fastmath=True)
def phi_z(z: float, I0: np.ndarray, k: float, alpha: float, n2: float, Isat: float) -> np.ndarray:
"""Returns the nonlinear dephasing after the a length z
Args:
z (float): The length in m
I0 (np.ndarray): Initial intensity profile in W/m^2
k (float): Wavenumber in m^-1
alpha (float): Linear losses coeff in m^-1
n2 (float): Non linear coeff in m^2/W
Isat (float): Saturation intensity in W/m^2
Returns:
np.ndarray: Final non-linear phase profile
"""
phi = np.empty_like(I0)
t_eval = np.array([0, z], dtype=np.float64)
p = np.array([k, alpha, n2, Isat], dtype=np.float64)
for i in numba.prange(I0.shape[0]):
usol, success = lsoda(
dphi_dz_ptr, np.array([I0[i], 0], dtype=np.float64), t_eval, rtol=1e-6, atol=1e-6, data=p)
phi[i] = usol[-1, 1]
return phi
def delta_n(im0: np.ndarray, I0: float, Pf: float, w0: float, d: float,
k: float, L: float, alpha: float = 50, plot: bool = False, err: bool = False):
"""Computes the total dephasing of an interferogram and fits the linear
loss coefficient alpha, the nonlinear coefficient n2 and the saturation intensity
from a single interferogram.
:param np.ndarray im0: Image to extract Dn
:param float I0: Initial intensity
:param float Pf: Final power
:param w0: initial waist
:param float d: Pixel pitch of the image
:param float k: wavenumber
:param float L: length of the cell in m
:param bool plot: Plots a visualization of the analysis result
:param bool err: Returns the error
:return tuple: phi_tot, (n2, Isat, alpha) with the errors if err is True.
"""
# im = im/np.max(im)
im_fringe = im_osc_fast_t(im0, cont=False)
# ATTENTION : Because im_osc_fast_t truncates the image,
# d needs to become 2d in the rest of the function
im_cont = np.abs(im_fringe)**2
im_cont /= np.max(im_cont)
wx, wy = waist(im_cont, plot=False)
wx *= 2*d
wy *= 2*d
# ratio of camera sensor surface over whole beam if waist is bigger than the whole camera
If = Pf/(np.sum(im_cont)*(2*d)**2) * \
(np.pi*(wx**2+wy**2))/(np.prod(im_cont.shape)*(2*d)**2)
# fit Isat
def fit_function_Isat(I, alpha, Isat):
return I_z(L, I, alpha, Isat)
phase_raw = angle_fast(im_fringe)
im_cont *= If
centre_x, centre_y = centre(im_cont)
cont_avg = az_avg(im_cont, center=(centre_x, centre_y))
phase = unwrap_phase(phase_raw, wrap_around=False)
phi_avg = az_avg(phase, center=(centre_x, centre_y))
phi_avg = gaussian_filter(phi_avg, 25)
cont_avg = gaussian_filter(cont_avg, 25)
phi_avg -= np.max(phi_avg)
# fit input intensity using waist
x = np.linspace(0, len(cont_avg)-1, len(cont_avg))*2*d
selec = x < max(im_cont.shape)*d
cont_fit = cont_avg[selec]
phi_fit = phi_avg[selec]
phi_fit -= np.max(phi_fit)
x = x[selec]
dphi = abs(np.max(phi_fit)-np.min(phi_fit))
dn_guess = dphi/(k*L)
n2_guess = dn_guess/I0
I_in = I0*np.exp(-2*x**2/w0**2)
(alpha, Isat), cov = optimize.curve_fit(fit_function_Isat, I_in,
cont_fit, p0=(alpha, 1e4), bounds=[(0, 1e2), (-np.log(1e-9)/L, 5e6)],
maxfev=3200)
alpha_err, Isat_err = np.sqrt(np.diag(cov))
def fit_phi_vs_I(I: np.ndarray, n2: float):
return phi_z(L, I, k, alpha, n2, Isat)-phi_z(L, np.array([np.min(I)]), k, alpha, n2, Isat)
(n2,), pcov = optimize.curve_fit(fit_phi_vs_I, I_in,
phi_fit,
bounds=[(-1e-6,),
(0,)],
p0=(-n2_guess),
maxfev=3200)
# gets fitting covariance/error for each parameter
n2_err = np.sqrt(np.diag(pcov))[0]
phase_tot = np.abs(phi_z(L, np.array([np.max(I_in)]), k, alpha,
n2, Isat)-phi_z(L, np.array([1e-10]), k, alpha, n2, Isat))
if plot:
fig = plt.figure()
ax = fig.add_subplot(2, 3, 1)
i00 = ax.imshow(im_cont*1e-4)
ax.set_title("Dc intensity")
fig.colorbar(i00, ax=ax, label=r'Intensity ($W/cm^2$)')
ax = fig.add_subplot(2, 3, 3)
i01 = ax.imshow(phase_raw, cmap='twilight_shifted')
ax.set_title("Phase")
fig.colorbar(i01, ax=ax, label=r'$\phi$ (rad)')
ax = fig.add_subplot(2, 3, 4)
i10 = ax.imshow(phase, cmap="viridis")
ax.set_title(r"Unwrapped $\phi$")
fig.colorbar(i10, ax=ax, label='Phase (rad)')
ax = fig.add_subplot(2, 3, 5)
lab = f"n2 = {n2:.2e} +/- {n2_err:.2e}\n"
lab += f"alpha = {alpha:.2f} +/- {alpha_err:.2f}\n"
lab += f"Isat = {Isat*1e-4:.2f} +/- {Isat_err*1e-4:.2f} W/cm²\n"
ax.plot(x*1e3, fit_phi_vs_I(I_in, n2),
label=lab)
ax.plot(x*1e3, phi_fit, label=r"$\phi_{fit}$")
ax.set_title("Azimuthal average")
ax.set_xlabel(r"Position in mm")
ax.set_ylabel(r"$\phi$ in rad")
ax.legend()
ax = fig.add_subplot(2, 3, 6)
ax.plot(I_in*1e-4, cont_fit*1e-4, label=r'$I_{out}$')
lab = f"alpha = {alpha:.2f} +/- {alpha_err:.2f}\n"
lab += f"Isat = {Isat*1e-4:.2f} +/- {Isat_err*1e-4:.2f} W/cm²\n"
ax.plot(I_in*1e-4, fit_function_Isat(I_in, alpha, Isat)*1e-4, label=lab)
ax.set_title(r"$I_{sat}$ and $\alpha$ fit")
ax.set_xlabel("Fitted input intensity in $W/cm^2$")
ax.set_ylabel("Output intensity in $W/cm^2$")
ax.legend()
plt.show()
# plt.tight_layout()
plt.show()
if err:
return phase_tot, (n2, Isat, alpha), (n2_err, Isat_err, alpha_err)
else:
return phase_tot, (n2, Isat, alpha)
def delta_n_sim(im0: np.ndarray, Pi: float, Pf: float, w0: float, d: float,
k: float, L: float, alpha: float = 50, plot: bool = False):
"""Computes the total dephasing of an interferogram and fits the linear
loss coefficient alpha, the nonlinear coefficient n2 and the saturation intensity
from a single interferogram.
:param np.ndarray im0: Image to extract Dn
:param float I0: Initial intensity
:param float Pf: Final power
:param float d: Pixel pitch of the image
:param float k: wavenumber
:param bool plot: Plots a visualization of the analysis result
:param bool err: Returns the error
:return tuple: phi_tot, (n2, Isat, alpha) with the errors if err is True.
"""
I0 = Pi/(np.pi*w0**2)
# im = im/np.max(im)
im_fringe = im_osc_fast_t(im0, cont=False)
# ATTENTION : Because im_osc_fast_t truncates the image,
# d needs to become 2d in the rest of the function
im_cont = np.abs(im_fringe)**2
im_cont /= np.max(im_cont)
wx, wy = waist(im_cont, plot=False)
wx *= 2*d
wy *= 2*d
# ratio of camera sensor surface over whole beam if waist is bigger than the whole camera
If = Pf/(np.sum(im_cont)*(2*d)**2) * \
(np.pi*(wx**2+wy**2))/(np.prod(im_cont.shape)*(2*d)**2)
phase_raw = angle_fast(im_fringe)
im_cont *= If
centre_x, centre_y = centre(im_cont)
cont_avg = az_avg(im_cont, center=(centre_x, centre_y))
phase = unwrap_phase(phase_raw, wrap_around=False)
phi_avg = az_avg(phase, center=(centre_x, centre_y))
phi_avg = gaussian_filter(phi_avg, 25)
cont_avg = gaussian_filter(cont_avg, 25)
phi_avg -= np.max(phi_avg)
x = np.linspace(0, len(cont_avg)-1, len(cont_avg))*2*d
selec = x < max(im_cont.shape)*d
cont_fit = np.zeros(2*np.sum(selec))
phi_fit = np.zeros(2*np.sum(selec))
x = np.linspace(-len(cont_fit)//2, len(cont_fit)//2, len(cont_fit))*2*d
window = 2*d*len(cont_fit)
cont_fit[:len(cont_fit)//2] = np.flip(cont_avg[selec])
phi_fit[:len(cont_fit)//2] = np.flip(phi_avg[selec])
cont_fit[len(cont_fit)//2:] = cont_avg[selec]
phi_fit[len(cont_fit)//2:] = phi_avg[selec]
phi_fit -= np.max(phi_fit)
dphi = abs(np.max(phi_fit)-np.min(phi_fit))
dn_guess = dphi/(k*L)
n2_guess = dn_guess/I0
Isat_guess = 1e5
alpha_guess = -np.log(Pf/Pi)/L
# define simulation for fitting
N = 512
simu = NLSE_1d(alpha_guess, Pi, w0, window, n2_guess, None,
L, NX=N)
simu.I_sat = Isat_guess
simu.delta_z = L/500
E_in_0 = cp.ones((simu.NX), dtype=cp.complex64) * \
cp.exp(-2*(cp.asarray(simu.X)**2)/simu.waist**2)
cont_interp_func = interpolate.interp1d(x, cont_fit)
phi_interp_func = interpolate.interp1d(x, phi_fit)
cont_interp = cont_interp_func(simu.X)
phi_interp = phi_interp_func(simu.X)
# define fitting functions
def fit_function(x, n2, Isat, alpha):
simu.n2 = n2
simu.I_sat = Isat
simu.alpha = alpha
simu.delta_z = L/500
out = simu.out_field(E_in_0, L, plot=False, verbose=False)
out_a = (cp.abs(out)**2*cst.c*cst.epsilon_0/2).get()
out_p = cp.unwrap(cp.angle(out)).get()
return np.hstack([out_a, out_p])
def error_function(params: tuple, x: np.ndarray, y: np.ndarray) -> float:
return np.sum((y-fit_function(x, *params))**2)
bds_ = ((-2e-8, -1e-10), (1e3, 2e5), (1, 50))
init_params = optimize.brute(error_function, bds_, args=(
simu.X, np.hstack([cont_interp, phi_interp])), Ns=20, disp=True)
A_fit = fit_function(simu.X, *init_params)
A_fit = A_fit[0:A_fit.shape[0]//2]*np.exp(1j*A_fit[A_fit.shape[0]//2:])
n2, Isat, alpha_fit = init_params
print(init_params)
if plot:
fig, ax = plt.subplots(1, 2)
ax[0].plot(x, phi_fit, label=r'$\phi$')
ax[0].plot(simu.X, phi_interp, label=r'$\phi$ interp')
ax[0].plot(simu.X, np.unwrap(np.angle(A_fit)), label='Fit')
ax[1].plot(x, cont_fit, label='Amp')
ax[1].plot(simu.X, cont_interp, label='Interpolated')
ax[1].plot(simu.X, np.abs(A_fit), label='Fit')
ax[0].legend()
ax[1].legend()
plt.show()
# fig = plt.figure()
# ax = fig.add_subplot(2, 3, 1)
# i00 = ax.imshow(im_cont*1e-4)
# ax.set_title("Dc intensity")
# fig.colorbar(i00, ax=ax, label=r'Intensity ($W/cm^2$)')
# ax = fig.add_subplot(2, 3, 3)
# i01 = ax.imshow(phase_raw, cmap='twilight_shifted')
# ax.set_title("Phase")
# fig.colorbar(i01, ax=ax, label=r'$\phi$ (rad)')
# ax = fig.add_subplot(2, 3, 4)
# i10 = ax.imshow(phase, cmap="viridis")
# ax.set_title(r"Unwrapped $\phi$")
# fig.colorbar(i10, ax=ax, label='Phase (rad)')
# ax = fig.add_subplot(2, 3, 5)
# lab = f"n2 = {n2:.2e} +/- {n2_err:.2e}\n"
# lab += f"alpha = {alpha:.2f} +/- {alpha_err:.2f}\n"
# lab += f"Isat = {Isat*1e-4:.2f} +/- {Isat_err*1e-4:.2f} W/cm²\n"
# ax.plot(simu.X*1e3, np.unwrap(np.angle(A_fit)),
# label=lab)
# ax.plot(x*1e3, phi_fit, label=r"$\phi_{fit}$")
# ax.set_title("Azimuthal average")
# ax.set_xlabel(r"Position in mm")
# ax.set_ylabel(r"$\phi$ in rad")
# ax.legend()
# ax = fig.add_subplot(2, 3, 6)
# ax.plot(x*1e3, cont_fit*1e-4, label=r'$I_{out}$')
# lab = f"alpha = {alpha:.2f} +/- {alpha_err:.2f}\n"
# lab += f"Isat = {Isat*1e-4:.2f} +/- {Isat_err*1e-4:.2f} W/cm²\n"
# ax.plot(simu.X*1e3, np.abs(A_fit)**2 * 1e-4, label=lab)
# ax.set_title(r"$I_{sat}$ and $\alpha$ fit")
# ax.set_xlabel(r"Position in mm")
# ax.set_ylabel("Output intensity in $W/cm^2$")
# ax.legend()
# plt.show()
# # plt.tight_layout()
# plt.show()
else:
return (n2, Isat, alpha)
def delta_n_cp(im0: cp.ndarray, I0: float, Pf: float, w0: float, d: float,
k: float, L: float, alpha: float = 50, plot: bool = False, err: bool = False):
"""Computes the total dephasing of an interferogram and fits the linear
loss coefficient alpha, the nonlinear coefficient n2 and the saturation intensity
from a single interferogram.
:param np.ndarray im0: Image to extract Dn
:param float I0: Initial intensity
:param float Pf: Final power
:param float d: Pixel pitch of the image
:param float k: wavenumber
:param bool plot: Plots a visualization of the analysis result
:param bool err: Returns the error
:return tuple: phi_tot, (n2, Isat, alpha) with the errors if err is True.
"""
im = cp.copy(im0)
im = im/cp.max(im)
im_fringe = im_osc_fast_cp(im, cont=False)
im_cont = cp.abs(im_fringe)
im_cont /= cp.max(im_cont)
# ratio of camera sensor surface over whole beam if waist is bigger than the whole camera
If = Pf/(cp.sum(im_cont)*d**2) * \
(np.pi*w0**2)/(np.prod(im0.shape)*d**2)
# fit Isat
def fit_function_Isat(I, alpha, Isat):
return I_z(L, I, alpha, Isat)
phase = cp.empty_like(im_fringe, dtype=cp.float32)
tpb = 16
bpg = math.ceil(phase.shape[0]/tpb)
angle_fast_cp[(bpg, bpg), (tpb, tpb)](im_fringe, phase)
im_cont *= If
im_cont = im_cont.get()
phase = phase.get()
centre_x, centre_y = centre(im_cont)
cont_avg = az_avg(im_cont, center=(centre_x, centre_y))
phase = unwrap_phase(phase, wrap_around=True)
phi_avg = az_avg(phase, center=(centre_x, centre_y))
phi_avg = gaussian_filter(phi_avg, 50)
phi_avg = -phi_avg
phi_avg -= np.max(phi_avg)
cont_fit = cont_avg[np.linspace(
0, len(cont_avg)-1, 100, dtype=int)]
phi_fit = phi_avg[np.linspace(
0, len(phi_avg)-1, 100, dtype=int)]
dphi = abs(np.max(phi_fit)-np.min(phi_fit))
dn_guess = dphi/(k*L)
n2_guess = dn_guess/I0
# fit input intensity using waist
x = np.linspace(0, len(cont_avg)-1, len(cont_avg))*d
x = x[np.linspace(0, len(x)-1, 100, dtype=int)]
I_in = I0*np.exp(-x**2/w0**2)
(alpha, Isat), cov = optimize.curve_fit(fit_function_Isat, I_in,
cont_fit, p0=(alpha, 1e4), bounds=[(0, 1e2), (-np.log(1e-9)/L, 1e7)])
alpha_err, Isat_err = np.sqrt(np.diag(cov))
def fit_phi_vs_I(I: np.ndarray, n2):
return phi_z(L, I, k, alpha, n2, Isat)
(n2,), pcov = optimize.curve_fit(fit_phi_vs_I, I_in,
phi_fit,
bounds=[(-1e-6,),
(0,)],
p0=(-n2_guess),
maxfev=3200)
# gets fitting covariance/error for each parameter
n2_err = np.sqrt(np.diag(pcov))[0]
phase_tot = np.abs(phi_z(L, np.array([np.max(I_in)]), k, alpha,
n2, Isat)-phi_z(L, np.array([1e-10]), k, alpha, n2, Isat))
if plot:
fig, ax = plt.subplots(1, 3)
i0 = ax[0].imshow(np.abs(im_cont))
ax[0].set_title("Dc intensity")
fig.colorbar(i0, ax=ax[0])
i1 = ax[1].imshow(phase, cmap='viridis')
ax[1].set_title("Unwrapped phase")
fig.colorbar(i1, ax=ax[1])
ax[2].plot(cont_avg*1e-4, phi_avg, label="Unwrapped phase")
lab = f"n2 = {n2:.2e} +/- {n2_err:.2e}\n"
lab += f"alpha = {alpha:.2f} +/- {alpha_err:.2f}\n"
lab += f"Isat = {Isat*1e-4:.2f} +/- {Isat_err*1e-4:.2f} W/cm²\n"
ax[2].plot(I_z(L, I_in, alpha, Isat)*1e-4, fit_phi_vs_I(I_in, n2),
label=lab)
ax[2].plot(cont_avg*1e-4, phi_avg,
label="Unwrapped phase filtered")
ax[2].set_title("Azimuthal average")
ax[2].set_xlabel(r"Az avg output intensity $W/cm^2$")
ax[2].set_ylabel("Phase in rad")
ax[2].legend()
# plt.tight_layout()
plt.show()
if err:
return phase_tot, (n2, Isat, alpha), (n2_err, Isat_err, alpha_err)
else:
return phase_tot, (n2, Isat, alpha)
def contr(im: np.ndarray) -> np.ndarray:
"""Computes the contrast of an interferogram
Args:
im (np.ndarray): The interferogram
Returns:
np.ndarray: The contrast map
"""