-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathglint_fitting_functions6.py
More file actions
1933 lines (1467 loc) · 70.1 KB
/
glint_fitting_functions6.py
File metadata and controls
1933 lines (1467 loc) · 70.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Library of the ``glint_fitting_gpu6.py``.
"""
import numpy as np
import cupy as cp
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit, leastsq, least_squares
from timeit import default_timer as time
import h5py
import os
from cupyx.scipy.special.statistics import ndtr
import scipy.special as sp
from scipy.stats import norm
from scipy.linalg import svd
import warnings
from scipy.optimize import OptimizeWarning, minimize
interpolate_kernel = cp.ElementwiseKernel(
'float32 x_new, raw float32 xp, int32 xp_size, raw float32 yp',
'raw float32 y_new',
'''
int high = xp_size - 1;
int low = 0;
int mid = 0;
while(high - low > 1)
{
mid = (high + low)/2;
if (xp[mid] <= x_new)
{
low = mid;
}
else
{
high = mid;
}
}
y_new[i] = yp[low] + (x_new - xp[low]) * (yp[low+1] - yp[low]) / (xp[low+1] - xp[low]);
if (x_new < xp[0])
{
y_new[i] = yp[0];
}
else if (x_new > xp[xp_size-1])
{
y_new[i] = yp[xp_size-1];
}
'''
)
computeCdfCuda = cp.ElementwiseKernel(
'float32 x_axis, raw float32 rv, float32 rv_sz',
'raw float32 cdf',
'''
int low = 0;
int high = rv_sz;
int mid = 0;
while(low < high){
mid = (low + high) / 2;
if(rv[mid] <= x_axis){
low = mid + 1;
}
else{
high = mid;
}
}
cdf[i] = high;
'''
)
def computeCdf(absc, data, mode, normed):
"""
Compute the empirical cumulative density function (CDF) on GPU with CUDA.
:Parameters:
**absc**: array
Abscissa of the CDF.
**data**: array
Data used to create the CDF.
**mode**: string
If ``ccdf``, the survival function (complementary of the CDF) is calculated instead.
**normed**: bool
If ``True``, the CDF is normed so that the maximum is equal to 1.
:Returns:
**cdf**: CDF of **data**.
"""
cdf = cp.zeros(absc.shape, dtype=cp.float32)
data = cp.asarray(data, dtype=cp.float32)
absc = cp.asarray(absc, dtype=cp.float32)
data = cp.sort(data)
computeCdfCuda(absc, data, data.size, cdf)
if mode == 'ccdf':
cdf = data.size - cdf
if normed:
cdf = cdf/data.size
return cdf
def rv_generator_wPDF(bins_cent, pdf, nsamp):
"""
Random values generator based on the PDF.
:Parameters:
**bins_cent**: array
Centered bins of the PDF.
**pdf**: array
Normalized arbitrary PDF to use to generate rv.
**nsamp**: integer
Number of values to generate.
:Returns:
**output_samples**: array
Array of random values generated from the PDF.
"""
bin_width = bins_cent[1] - bins_cent[0]
cdf = cp.cumsum(pdf, dtype=cp.float32) * bin_width
cdf, mask = cp.unique(cdf, True)
cdf_bins_cent = bins_cent[mask]
cdf_bins_cent = cdf_bins_cent + bin_width/2.
rv_uniform = cp.random.rand(nsamp, dtype=cp.float32)
output_samples = cp.zeros(rv_uniform.shape, dtype=cp.float32)
interpolate_kernel(rv_uniform, cdf, cdf.size, cdf_bins_cent, output_samples)
return output_samples
def rv_generator(absc, cdf, nsamp):
"""
Random values generator based on the CDF.
:Parameters:
**absc**: array
Abscissa of the CDF;
**cdf**: array
Normalized arbitrary CDF to use to generate rv.
**nsamp**: integer
Number of values to generate.
:Returns:
**output_samples**: array
Array of random values generated from the CDF.
"""
cdf, mask = cp.unique(cdf, True)
cdf_absc = absc[mask]
rv_uniform = cp.random.rand(nsamp, dtype=cp.float32)
output_samples = cp.zeros(rv_uniform.shape, dtype=cp.float32)
interpolate_kernel(rv_uniform, cdf, cdf.size, cdf_absc, output_samples)
return output_samples
def computeCdfCpu(rv, x_axis, normed=True):
"""
Compute the empirical cumulative density function (CDF) on CPU.
:Parameters:
**rv**: array
data used to compute the CDF.
**x_axis**: array
Abscissa of the CDF.
**normed**: bool
If ``True``, the CDF is normed so that the maximum is equal to 1.
:Returns:
**cdf**: array
CDF of the **data**.
**mask**: array
Indexes of cumulated values.
"""
cdf = np.ones(x_axis.size)*rv.size
temp = np.sort(rv)
idx = 0
for i in range(x_axis.size):
# idx = idx + len(np.where(temp[idx:] <= x_axis[i])[0])
mask = np.where(temp <= x_axis[i])[0]
idx = len(mask)
if len(temp[idx:]) != 0:
cdf[i] = idx
else:
print('pb', i, idx)
break
if normed:
cdf /= float(rv.size)
return cdf
else:
return cdf, mask
def computeCdfCupy(rv, x_axis):
"""
Compute the empirical cumulative density function (CDF) on GPU with cupy.
:Parameters:
**rv**: array
Data used to compute the CDF.
**x_axis**: array
Abscissa of the CDF.
:Returns:
**cdf**: array
CDF of **data**.
"""
cdf = cp.ones(x_axis.size, dtype=cp.float32)*rv.size
temp = cp.asarray(rv, dtype=cp.float32)
temp = cp.sort(rv)
idx = 0
for i in range(x_axis.size):
idx = idx + len(cp.where(temp[idx:] <= x_axis[i])[0])
if len(temp[idx:]) != 0:
cdf[i] = idx
else:
break
cdf = cdf / rv.size
return cdf
def load_data(data, wl_edges, null_key, nulls_to_invert, *args, **kwargs):
"""
Load data from data file to create the histograms of the null depths and do Monte-Carlo.
:Parameters:
**data**: array
List of data files.
**wl_edges**: 2-tuple
Lower and upper bounds of the spectrum to load.
**null_key**: string
Baseline to load.
**nulls_to_invert**: list
List of nulls to invert because their null and antinull outputs are swapped.
**args**: extra arguments
Use dark data to get the error on the null depth.
**kwargs**: extra keyword arguments
Performs temporal binning of frames.
:Returns:
**out**: dictionary
Includes data to use for the fit: flux in (anti-)null and phtometric outputs, errors, wavelengths.
"""
# Null table for getting the null and associated photometries in the intermediate data
# Structure = Chosen null:[number of null, photometry A and photometry B]
null_table = {'null1':[1,1,2], 'null2':[2,2,3], 'null3':[3,1,4], \
'null4':[4,3,4], 'null5':[5,3,1], 'null6':[6,4,2]}
indexes = null_table[null_key]
null_data = []
Iminus_data = []
Iplus_data = []
photo_data = [[],[]]
photo_err_data = [[],[]]
wl_scale = []
for d in data:
with h5py.File(d, 'r') as data_file:
wl_scale.append(np.array(data_file['wl_scale']))
# null_data.append(np.array(data_file['null%s'%(indexes[0])]))
Iminus_data.append(np.array(data_file['Iminus%s'%(indexes[0])]))
Iplus_data.append(np.array(data_file['Iplus%s'%(indexes[0])]))
photo_data[0].append(np.array(data_file['p%s'%(indexes[1])])) # Fill with beam A intensity
photo_data[1].append(np.array(data_file['p%s'%(indexes[2])])) # Fill with beam B intensity
photo_err_data[0].append(np.array(data_file['p%serr'%(indexes[1])])) # Fill with beam A error
photo_err_data[1].append(np.array(data_file['p%serr'%(indexes[2])])) # Fill with beam B error
if 'null%s'%(indexes[0]) in nulls_to_invert:
n = np.array(data_file['Iplus%s'%(indexes[0])]) / np.array(data_file['Iminus%s'%(indexes[0])])
else:
n = np.array(data_file['Iminus%s'%(indexes[0])]) / np.array(data_file['Iplus%s'%(indexes[0])])
null_data.append(n)
# Merge data along frame axis
null_data = [selt for elt in null_data for selt in elt]
Iminus_data = [selt for elt in Iminus_data for selt in elt]
Iplus_data = [selt for elt in Iplus_data for selt in elt]
for i in range(2):
photo_data[i] = [selt for elt in photo_data[i] for selt in elt]
photo_err_data[i] = [selt for elt in photo_err_data[i] for selt in elt]
null_data = np.array(null_data)
Iminus_data = np.array(Iminus_data)
Iplus_data = np.array(Iplus_data)
photo_data = np.array(photo_data)
photo_err_data = np.array(photo_err_data)
wl_scale = wl_scale[0] #All the wl scale are supposed to be the same, just pick up the first of the list
mask = np.arange(wl_scale.size)
wl_min, wl_max = wl_edges
mask = mask[(wl_scale>=wl_min)&(wl_scale <= wl_max)]
if 'flag' in kwargs:
flags = kwargs['flag']
mask = mask[flags]
null_data = null_data[:,mask]
Iminus_data = Iminus_data[:,mask]
Iplus_data = Iplus_data[:,mask]
photo_data = photo_data[:,:,mask]
wl_scale = wl_scale[mask]
null_data = np.transpose(null_data)
photo_data = np.transpose(photo_data, axes=(0,2,1))
Iminus_data = np.transpose(Iminus_data)
Iplus_data = np.transpose(Iplus_data)
if 'frame_binning' in kwargs:
if not kwargs['frame_binning'] is None:
if kwargs['frame_binning'] > 1:
nb_frames_to_bin = int(kwargs['frame_binning'])
null_data, dummy = binning(null_data, nb_frames_to_bin, axis=1, avg=True)
photo_data, dummy = binning(photo_data, nb_frames_to_bin, axis=2, avg=True)
Iminus_data, dummy = binning(Iminus_data, nb_frames_to_bin, axis=1, avg=True)
Iplus_data, dummy = binning(Iplus_data, nb_frames_to_bin, axis=1, avg=True)
out = {'null':null_data, 'photo':photo_data, 'wl_scale':wl_scale,\
'photo_err':photo_err_data, 'wl_idx':mask, 'Iminus':Iminus_data, 'Iplus':Iplus_data}
if len(args) > 0:
null_err_data = getErrorNull(out, args[0])
else:
null_err_data = np.zeros(null_data.shape)
out['null_err'] = null_err_data
return out
def getErrorNull(data_dic, dark_dic):
"""
Compute the error of the null depth.
:Parameters:
**data_dic**: dictionary
Dictionary of the data from ``load_data``.
**dark_dic**: dictionary
Dictionary of the dark from ``load_data``.
:Returns:
**std_null** : array
Array of the error on the null depths.
"""
var_Iminus = dark_dic['Iminus'].var(axis=-1)[:,None]
var_Iplus = dark_dic['Iplus'].var(axis=-1)[:,None]
Iminus = data_dic['Iminus']
Iplus = data_dic['Iplus']
null = data_dic['null']
std_null = (null**2 * (var_Iminus/Iminus**2 + var_Iplus/Iplus**2))**0.5
return std_null
def getHistogram(data, bins, density, target='cpu'):
"""
**DISCARDED**
Compute the histogram of the data.
:Parameters:
**data**: array
Data from which we want the histogram.
**bins**: array
Left-edge of the bins of the histogram but the last value which is the right-edge of the last bin.
**density**: bool
If ``True``, the histogram is normalized as described in documentation of ``np.histogram``.
**target**: string, optional
Indicates what creates the histograms: the ``cpu`` or the ``gpu``. The default is 'cpu'.
:Returns:
**pdf**: array
PDF of the data.
**bins_cent**: array
Centered bins of the histogram.
"""
pdf, bin_edges = np.histogram(data, bins=bins, density=density)
bins_cent = bin_edges[:-1] + np.diff(bin_edges[:2])/2.
if target == 'gpu':
pdf, bins_cent = cp.asarray(pdf, dtype=cp.float32), cp.asarray(bins_cent, dtype=cp.float32)
return pdf, bins_cent
def getHistogramOfIntensities(data, bins, split, target='cpu'):
"""
**DISCARDED**
Compute the histograms of the photometric outputs.
:Parameters:
**data**: array
Data from which we want the histogram.
**bins**: array
Left-edge of the bins of the histogram but the last value which is the right-edge of the last bin.
**split**: ????
**target**: string, optional
Indicates what creates the histograms: the ``cpu`` or the ``gpu``. The default is 'cpu'.
:Returns:
**pdf_I_interf**: array
PDF of the intensities.
**bins_cent**: array
Centered bins of the histogram.
"""
pdf_I = [[np.histogram(selt, bins) for selt in elt] for elt in data]
bin_edges = np.array([[selt[1] for selt in elt] for elt in pdf_I])
pdf_I = np.array([[selt[0] for selt in elt] for elt in pdf_I])
bin_edges_interf = bin_edges[:,None,:] * split[:,:,:,None]
pdf_I_interf = pdf_I[:,None,:] / np.sum(pdf_I[:,None,:] * np.diff(bin_edges_interf), axis=-1, keepdims=True)
bins_cent = bin_edges_interf[:,:,:,:-1] + np.diff(bin_edges_interf[:,:,:,:2])/2.
if target=='gpu':
pdf_I_interf, bins_cent = cp.asarray(pdf_I_interf, dtype=cp.float32), cp.asarray(bins_cent, dtype=cp.float32)
return pdf_I_interf, bins_cent
def computeNullDepth(na, IA, IB, wavelength, opd, phase_bias, dphase_bias, dark_null, dark_antinull,
zeta_minus_A, zeta_minus_B, zeta_plus_A, zeta_plus_B, spec_chan_width, oversampling_switch, switch_invert_null):
"""
Compute the null depth from generated random values of photometries, detector noise and OPD.
The estimator is the ratio of the null over the antinull fluxes.
:Parameters:
**na**: float
Astrophysical null depth.
**IA**: array
Values of intensity of beam A in the fringe pattern.
**IB**: array
Values of intensity of beam B in the fringe pattern.
**wavelength** : float
Wavelength of the fringe pattern.
**opd**: array
Value of OPD in nm.
**phase_bias**: float
Achromatic phase offset in radian.
**dphase_bias**: float
Achromatic phase offset complement in radian (originally supposed to be fitted but now set to 0).
**dark_null**: array
Synthetic values of detector noise in the null output.
**dark_antinull**: array
Synthetic values of detector noise in the antinull output.
**zeta_minus_A**: float
Value of the zeta coefficient between null and photometric outputs for beam B.
**zeta_minus_B**: float
Value of the zeta coefficient between null and photometric outputs for beam B.
**zeta_plus_A**: float
Value of the zeta coefficient between antinull and photometric outputs for beam A.
**zeta_plus_B**: float
Value of the zeta coefficient between antinull and photometric outputs for beam B.
**spec_chan_width**: float
Width of a spectral channel in nm.
**oversampling_switch**: bool
If ``True``, the spectral channel is oversampled and averaged to take into account the loss of temporal coherence.
**switch_invert_null**: bool
If ``True``, the null and antinull sequences are swapped because they are swapped on real data.
:Returns:
**null**: array
Synthetic sequence of null dephts.
**Iminus**: array
Synthetic sequence of flux in the null output.
**Iplus**: array
Synthetic sequence of flux in the antinull output.
"""
visibility = (1 - na) / (1 + na)
wave_number = 1./wavelength
sine = cp.sin(2*np.pi*wave_number*(opd) + phase_bias + dphase_bias)
if oversampling_switch:
delta_wave_number = abs(1/(wavelength + spec_chan_width/2) - 1/(wavelength - spec_chan_width/2))
arg = np.pi*delta_wave_number * (opd)
sinc = cp.sin(arg) / arg
sine = sine * sinc
if switch_invert_null: # Data was recorded with a constant pi shift
Iminus = IA*zeta_minus_A + IB*zeta_minus_B + \
2 * np.sqrt(IA * IB) * np.sqrt(zeta_minus_A*zeta_minus_B) * visibility * sine #+ dark_null
Iplus = IA*zeta_plus_A + IB*zeta_plus_B - \
2 * np.sqrt(IA * IB) * np.sqrt(zeta_plus_A*zeta_plus_B) * visibility * sine #+ dark_antinull
# Iminus = cp.random.normal(Iminus, Iminus**0.5, size=Iminus.shape)
# Iplus = cp.random.normal(Iplus, Iplus**0.5, size=Iplus.shape)
Iminus = Iminus + dark_null
Iplus = Iplus + dark_antinull
null = Iplus / Iminus
else:
Iminus = IA*zeta_minus_A + IB*zeta_minus_B - \
2 * np.sqrt(IA * IB) * np.sqrt(zeta_minus_A*zeta_minus_B) * visibility * sine #+ dark_null
Iplus = IA*zeta_plus_A + IB*zeta_plus_B + \
2 * np.sqrt(IA * IB) * np.sqrt(zeta_plus_A*zeta_plus_B) * visibility * sine #+ dark_antinull
# Iminus = cp.random.normal(Iminus, Iminus**0.5, size=Iplus.shape)
# Iplus = cp.random.normal(Iplus, Iplus**0.5, size=Iplus.shape)
Iminus = Iminus + dark_null
Iplus = Iplus + dark_antinull
null = Iminus / Iplus
return null, Iminus, Iplus
def computeNullDepthNoAntinull(IA, IB, wavelength, opd, dark_null, dark_antinull,
zeta_minus_A, zeta_minus_B, zeta_plus_A, zeta_plus_B, spec_chan_width, oversampling_switch, switch_invert_null):
"""
Compute the null depth from generated random values of photometries, detector noise and OPD.
The estimator is the ratio of the null over the antinull fluxes.
The antinull flux is considered as a pure constructive fringe.
:Parameters:
**IA**: array
Values of intensity of beam A in the fringe pattern.
**IB**: array
Values of intensity of beam B in the fringe pattern.
**wavelength** : float
Wavelength of the fringe pattern.
**opd**: array
Value of OPD in nm.
**dark_null**: array
Synthetic values of detector noise in the null output.
**dark_antinull**: array
Synthetic values of detector noise in the antinull output.
**zeta_minus_A**: float
Value of the zeta coefficient between null and photometric outputs for beam B.
**zeta_minus_B**: float
Value of the zeta coefficient between null and photometric outputs for beam B.
**zeta_plus_A**: float
Value of the zeta coefficient between antinull and photometric outputs for beam A.
**zeta_plus_B**: float
Value of the zeta coefficient between antinull and photometric outputs for beam B.
**spec_chan_width**: float
Width of a spectral channel in nm.
**oversampling_switch**: bool
If ``True``, the spectral channel is oversampled and averaged to take into account the loss of temporal coherence.
**switch_invert_null**: bool
If ``True``, the null and antinull sequences are swapped because they are swapped on real data.
:Returns:
**Iminus**: array
Synthetic sequence of flux in the null output.
**Iplus**: array
Synthetic sequence of flux in the antinull output.
"""
wave_number = 1./wavelength
sine = cp.sin(2*np.pi*wave_number*(opd))
if oversampling_switch:
delta_wave_number = abs(1/(wavelength + spec_chan_width/2) - 1/(wavelength - spec_chan_width/2))
arg = np.pi*delta_wave_number * (opd)
sinc = cp.sin(arg) / arg
sine = sine * sinc
if switch_invert_null: # Data was recorded with a constant pi shift
Iminus = IA*zeta_minus_A + IB*zeta_minus_B + \
2 * np.sqrt(IA * IB) * np.sqrt(zeta_minus_A*zeta_minus_B)
Iplus = IA*zeta_plus_A + IB*zeta_plus_B - \
2 * np.sqrt(IA * IB) * np.sqrt(zeta_plus_A*zeta_plus_B) * sine + \
dark_antinull
else:
Iminus = IA*zeta_minus_A + IB*zeta_minus_B - \
2 * np.sqrt(IA * IB) * np.sqrt(zeta_minus_A*zeta_minus_B) * sine + \
dark_null
Iplus = IA*zeta_plus_A + IB*zeta_plus_B + \
2 * np.sqrt(IA * IB) * np.sqrt(zeta_plus_A*zeta_plus_B)
return Iminus, Iplus
def computeNullDepth2(na, IA, IB, wavelength, opd, phase_bias, dphase_bias, dark_null, dark_antinull,
zeta_minus_A, zeta_minus_B, zeta_plus_A, zeta_plus_B, spec_chan_width, oversampling_switch, switch_invert_null, sig_opd):
"""
**DISCARDED**
Compute the null depth from generated random values of photometries, detector noise and OPD.
The interferometric term is weighted by the loss of coherence expressed as the exponential form :math:`e^{-(2\pi/\lambda \sigma_{OPD})^2 / 2}`.
:Parameters:
**na**: float
Astrophysical null depth.
**IA**: array
Values of intensity of beam A in the fringe pattern.
**IB**: array
Values of intensity of beam B in the fringe pattern.
**wavelength** : float
Wavelength of the fringe pattern.
**opd**: array
Value of OPD in nm.
**dark_null**: array
Synthetic values of detector noise in the null output.
**dark_antinull**: array
Synthetic values of detector noise in the antinull output.
**zeta_minus_A**: float
Value of the zeta coefficient between null and photometric outputs for beam B.
**zeta_minus_B**: float
Value of the zeta coefficient between null and photometric outputs for beam B.
**zeta_plus_A**: float
Value of the zeta coefficient between antinull and photometric outputs for beam A.
**zeta_plus_B**: float
Value of the zeta coefficient between antinull and photometric outputs for beam B.
**spec_chan_width**: float
Width of a spectral channel in nm.
**oversampling_switch**: bool
If ``True``, the spectral channel is oversampled and averaged to take into account the loss of temporal coherence.
**switch_invert_null**: bool
If ``True``, the null and antinull sequences are swapped because they are swapped on real data.
**sig_opd**: float
Standard deviation of the fluctuations of OPD.
:Returns:
**null**: array
Synthetic sequence of null dephts.
**Iminus**: array
Synthetic sequence of flux in the null output.
**Iplus**: array
Synthetic sequence of flux in the antinull output.
"""
visibility = (1 - na) / (1 + na)
wave_number = 1./wavelength
sine = cp.sin(2*np.pi*wave_number*(opd) + phase_bias + dphase_bias)
if oversampling_switch:
delta_wave_number = abs(1/(wavelength + spec_chan_width/2) - 1/(wavelength - spec_chan_width/2))
arg = np.pi*delta_wave_number * (opd)
sinc = cp.sin(arg) / arg
sine = sine * sinc
if switch_invert_null:
Iminus = IA*zeta_minus_A + IB*zeta_minus_B + \
2 * np.sqrt(IA * IB) * np.sqrt(zeta_minus_A*zeta_minus_B) * visibility * sine * np.exp(-(2*np.pi/wavelength*sig_opd)**2/2) + \
dark_null
Iplus = IA*zeta_plus_A + IB*zeta_plus_B - \
2 * np.sqrt(IA * IB) * np.sqrt(zeta_plus_A*zeta_plus_B) * visibility *sine * np.exp(-(2*np.pi/wavelength*sig_opd)**2/2) + \
dark_antinull
null = Iplus / Iminus
else:
Iminus = IA*zeta_minus_A + IB*zeta_minus_B - \
2 * np.sqrt(IA * IB) * np.sqrt(zeta_minus_A*zeta_minus_B) * visibility * sine + \
dark_null
Iplus = IA*zeta_plus_A + IB*zeta_plus_B + \
2 * np.sqrt(IA * IB) * np.sqrt(zeta_plus_A*zeta_plus_B) * visibility *sine + \
dark_antinull
null = Iminus / Iplus
return null, Iminus, Iplus
def computeNullDepthLinear(na, IA, IB, wavelength, opd, phase_bias, dphase_bias, dark_null, dark_antinull,
zeta_minus_A, zeta_minus_B, zeta_plus_A, zeta_plus_B, spec_chan_width, oversampling_switch, switch_invert_null):
"""
Compute the null depth from generated random values of photometries, detector noise and OPD.
The estimator is the linear expression :math:`N = N_a + N_{instr}`.
:Parameters:
**na**: float
Astrophysical null depth.
**IA**: array
Values of intensity of beam A in the fringe pattern.
**IB**: array
Values of intensity of beam B in the fringe pattern.
**wavelength** : float
Wavelength of the fringe pattern.
**opd**: array
Value of OPD in nm.
**phase_bias**: float
Achromatic phase offset in radian.
**dphase_bias**: float
Achromatic phase offset complement in radian (originally supposed to be fitted but now set to 0).
**dark_null**: array
Synthetic values of detector noise in the null output.
**dark_antinull**: array
Synthetic values of detector noise in the antinull output.
**zeta_minus_A**: float
Value of the zeta coefficient between null and photometric outputs for beam B.
**zeta_minus_B**: float
Value of the zeta coefficient between null and photometric outputs for beam B.
**zeta_plus_A**: float
Value of the zeta coefficient between antinull and photometric outputs for beam A.
**zeta_plus_B**: float
Value of the zeta coefficient between antinull and photometric outputs for beam B.
**spec_chan_width**: float
Width of a spectral channel in nm.
**oversampling_switch**: bool
If ``True``, the spectral channel is oversampled and averaged to take into account the loss of temporal coherence.
**switch_invert_null**: bool
If ``True``, the null and antinull sequences are swapped because they are swapped on real data.
:Returns:
**null**: array
Synthetic sequence of null dephts.
**Iminus**: array
Synthetic sequence of flux in the null output.
**Iplus**: array
Synthetic sequence of flux in the antinull output.
"""
astroNull = na
wave_number = 1./wavelength
sine = cp.sin(2*np.pi*wave_number*(opd) + phase_bias + dphase_bias)
if oversampling_switch:
delta_wave_number = abs(1/(wavelength + spec_chan_width/2) - 1/(wavelength - spec_chan_width/2))
arg = np.pi*delta_wave_number * (opd)
sinc = cp.sin(arg) / arg
sine = sine * sinc
if switch_invert_null:
Iminus = IA*zeta_minus_A + IB*zeta_minus_B + 2 * np.sqrt(IA * IB) * np.sqrt(zeta_minus_A*zeta_minus_B) * sine + dark_null
Iplus = IA*zeta_plus_A + IB*zeta_plus_B - 2 * np.sqrt(IA * IB) * np.sqrt(zeta_plus_A*zeta_plus_B) * sine + dark_antinull
null = Iplus / Iminus
else:
Iminus = IA*zeta_minus_A + IB*zeta_minus_B - 2 * np.sqrt(IA * IB) * np.sqrt(zeta_minus_A*zeta_minus_B) * sine + dark_null
Iplus = IA*zeta_plus_A + IB*zeta_plus_B + 2 * np.sqrt(IA * IB) * np.sqrt(zeta_plus_A*zeta_plus_B) * sine + dark_antinull
null = Iminus / Iplus
return null + astroNull, Iminus, Iplus
def computeHanot(na, IA, IB, wavelength, opd, phase_bias, dphase_bias, dark_null, dark_antinull,
zeta_minus_A, zeta_minus_B, zeta_plus_A, zeta_plus_B, spec_chan_width, oversampling_switch, switch_invert_null):
"""
Compute the null depth from generated random values of photometries, detector noise and OPD.
The estimator is the one used in Hanot et al. (2011)(https://ui.adsabs.harvard.edu/abs/2011ApJ...729..110H/abstract).
:Parameters:
**na**: float
Astrophysical null depth.
**IA**: array
Values of intensity of beam A in the fringe pattern.
**IB**: array
Values of intensity of beam B in the fringe pattern.
**wavelength** : float
Wavelength of the fringe pattern.
**opd**: array
Value of OPD in nm.
**phase_bias**: float
Achromatic phase offset in radian.
**dphase_bias**: float
Achromatic phase offset complement in radian (originally supposed to be fitted but now set to 0).
**dark_null**: array
Synthetic values of detector noise in the null output.
**dark_antinull**: array
Synthetic values of detector noise in the antinull output.
**zeta_minus_A**: float
Value of the zeta coefficient between null and photometric outputs for beam B.
**zeta_minus_B**: float
Value of the zeta coefficient between null and photometric outputs for beam B.
**zeta_plus_A**: float
Value of the zeta coefficient between antinull and photometric outputs for beam A.
**zeta_plus_B**: float
Value of the zeta coefficient between antinull and photometric outputs for beam B.
**spec_chan_width**: float
Width of a spectral channel in nm.
**oversampling_switch**: bool
If ``True``, the spectral channel is oversampled and averaged to take into account the loss of temporal coherence.
**switch_invert_null**: bool
If ``True``, the null and antinull sequences are swapped because they are swapped on real data.
:Returns:
**null**: array
Synthetic sequence of null dephts.
**Iminus**: array
Synthetic sequence of flux in the null output.
**Iplus**: array
Synthetic sequence of flux in the antinull output.
"""
astroNull = na
wave_number = 1./wavelength
DeltaPhi = 2*np.pi*wave_number*(opd) + phase_bias + dphase_bias
if switch_invert_null:
dI = (IA*zeta_plus_A - IB*zeta_plus_B) / (IA*zeta_plus_A + IB*zeta_plus_B)
Nb = dark_antinull / (IA*zeta_plus_A + IB*zeta_plus_B)
else:
dI = (IA*zeta_minus_A - IB*zeta_minus_B) / (IA*zeta_minus_A + IB*zeta_minus_B)
Nb = dark_null / (IA*zeta_minus_A + IB*zeta_minus_B)
null = 0.25 * (dI**2 + DeltaPhi**2)
return null + astroNull + Nb
def computeNullDepthCos(IA, IB, wavelength, offset_opd, dopd, phase_bias, dphase_bias, na, dark_null, dark_antinull,
zeta_minus_A, zeta_minus_B, zeta_plus_A, zeta_plus_B, spec_chan_width, oversampling_switch):
"""
Compute the null depth from generated random values of photometries, detector noise and OPD.
The estimator is the ratio of the null over the antinull fluxes.
The interferometric term uses a cosine and not a sine function.
:Parameters:
**na**: float
Astrophysical null depth.
**IA**: array
Values of intensity of beam A in the fringe pattern.
**IB**: array
Values of intensity of beam B in the fringe pattern.
**wavelength** : float
Wavelength of the fringe pattern.
**opd**: array
Value of OPD in nm.
**phase_bias**: float
Achromatic phase offset in radian.
**dphase_bias**: float
Achromatic phase offset complement in radian (originally supposed to be fitted but now set to 0).
**dark_null**: array
Synthetic values of detector noise in the null output.
**dark_antinull**: array
Synthetic values of detector noise in the antinull output.
**zeta_minus_A**: float
Value of the zeta coefficient between null and photometric outputs for beam B.
**zeta_minus_B**: float
Value of the zeta coefficient between null and photometric outputs for beam B.
**zeta_plus_A**: float
Value of the zeta coefficient between antinull and photometric outputs for beam A.
**zeta_plus_B**: float
Value of the zeta coefficient between antinull and photometric outputs for beam B.
**spec_chan_width**: float
Width of a spectral channel in nm.
**oversampling_switch**: bool
If ``True``, the spectral channel is oversampled and averaged to take into account the loss of temporal coherence.
**switch_invert_null**: bool
If ``True``, the null and antinull sequences are swapped because they are swapped on real data.
:Returns:
**null**: array
Synthetic sequence of null dephts.
"""
visibility = (1 - na) / (1 + na)
wave_number = 1./wavelength
sine = cp.cos(2*np.pi*wave_number*(offset_opd + dopd) + phase_bias + dphase_bias)
if oversampling_switch:
delta_wave_number = abs(1/(wavelength + spec_chan_width/2) - 1/(wavelength - spec_chan_width/2))
arg = np.pi*delta_wave_number * (offset_opd + dopd)
sinc = cp.sin(arg) / arg
sine = sine * sinc
Iminus = IA*zeta_minus_A + IB*zeta_minus_B - \
2 * np.sqrt(IA * IB) * np.sqrt(zeta_minus_A*zeta_minus_B) * visibility * sine + \
dark_null
Iplus = IA*zeta_plus_A + IB*zeta_plus_B + \
2 * np.sqrt(IA * IB) * np.sqrt(zeta_plus_A*zeta_plus_B) * visibility *sine + \
dark_antinull
null = Iminus / Iplus
return null
def computeNullDepthLinearCos(na, IA, IB, wavelength, opd, phase_bias, dphase_bias, dark_null, dark_antinull,
zeta_minus_A, zeta_minus_B, zeta_plus_A, zeta_plus_B, spec_chan_width, oversampling_switch):
"""
Compute the null depth from generated random values of photometries, detector noise and OPD.