-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathccf.py
More file actions
1534 lines (1362 loc) · 79.2 KB
/
ccf.py
File metadata and controls
1534 lines (1362 loc) · 79.2 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
#A code to measure the time lag in reverberation mapping
#Last modified on 6/20/2022
#Auther: Hengxiao Guo (UCI), Aaron Barth (UCI)
#Email: hengxiaoguo AT gmail DOT com, barth AT uci DOT edu
#version 1.3
#Main function: measure the time lag (including lag uncertainty and significance) between
#AGN continuum and emission line in reverberation mapping project. All the CARMA procedures
#are from Kelly Brandon's carma_pack.
#We thanks for the helpful discussion with Mouyuan Sun, Jennifer I. Li.
import numpy as np
import carmcmc as cm
import scipy.stats as sst
from astropy.io import fits
from scipy import interpolate
import matplotlib.pyplot as plt
from scipy.signal import find_peaks
from contextlib import contextmanager
import glob, os, sys, timeit, warnings
from sklearn.neighbors import KernelDensity
from multiprocessing import Lock, Pool, Manager
warnings.filterwarnings("ignore")
# setup the styles of output figures
plt.rcParams['figure.figsize'] = 8, 6
plt.rcParams['axes.labelsize'] = 20
plt.rcParams['xtick.labelsize'] = 20
plt.rcParams['ytick.labelsize'] = 20
plt.rcParams['axes.titlesize'] = 20
plt.rcParams['axes.linewidth'] = 2
plt.rcParams['xtick.major.size'] = 5
plt.rcParams['xtick.minor.size'] = 3
plt.rcParams['xtick.direction'] = 'in'
plt.rcParams['ytick.direction'] = 'in'
plt.rcParams['font.family'] = 'serif'
plt.rcParams['lines.linewidth'] = 2
@contextmanager
def quiet():
"""Disable print in carma code."""
with open(os.devnull, "w") as devnull:
old_stdout = sys.stdout
sys.stdout = devnull
try:
yield
finally:
sys.stdout = old_stdout
class CCF():
def __init__(self):
pass
def ICCF(self, t1, y1, e1, t2, y2, e2, tau_min = -100, tau_max = 100, step = 0.2, detrend = 0, interp = 'linear', mcmc_nsamples = 20000, auto_pq = False, p1 = 1, q1 = 0, p2 = 1, q2 = 0, carma_model = 'random', sig_cut = 0.8, imode = 0, MC_ntrials = 1000, FR_RSS = 0, sigmode = 0.2, weight = False, nsmooth_wgts = 1, sim_ntrials = 1000, sim_mode = 0, sim_var_range = 0.2, Nmodel = 2, MP = True, name = 'results', plotLC = True, shift = 'centroid', plotCCF = True, save = True, lite = True, path = './'):
'''
Main function will call other functions. This function is able to detrend the light curve, measure the time lag,
evaluate the lag uncertianties with Flux Randomization (FR) and Random Subset Sampling (RSS) and the significance with simulated CARMA Light Curves (LC).
Input:
t1, y1, e1: array_like
time, flux and uncertainty for continuum light curve.
t2, y2, e2: array_like
time, flux and uncertainty for emission-line light curve.
tau_min, tau_max: float
maximum and minimun range to calculate the time delay.
step: float
tau shifting step.
detrend: interger
detrend the data with a polynomial function np.polyfit(t, y, detrend). Not detrend if 0.
interp: 'linear' or 'carma'
linear interpolation for all procedures (measuring lag, lag uncertainties and significance) if 'linear', CARMA-model interpolation for all procedures if 'carma', i.e., interpolate light curve with Damped Random Walk model (CARMA(p, q) with p = 1, q = 0). See details in Kelly et al. 2009, ApJ, 698, 895 and Kelly et al. 2014, ApJ, 788, 33.
mcmc_nsamples: interger
N sampeles in CARMA MCMC process.
auto_pq: bool
choose the best p and q automatically by CARMA model for continuum and emission line LCs if True. We search the all the grids with p <= 6 and q <= 5. This step will take much time.
p1, q1, p2, q2: interger
p1, q1 for continuum LC and p2, q2 for emission line LC. p > q required. DRW model if p = 1 and q = 0.
carma_model: 'mean' or 'random'
interpolate data with the sample mean carma model if 'mean', else we randomly draw a realization, which is more close to the reality.
sig_cut: float
0 < sig_cut <1. The required significance of the correlation coefficient r_max to calculate the CCF centroid, i.e., r_max * sig_cut
FR_RSS: 0, 1 or 2
use both FR and RSS to calculate the uncertainties if 0. Only use FR (RSS) if 1 (2).
imode: 0, 1 or 2
0: cross-correlation mode (two ways); 1: interpolate continuum LC; 2: interpolate emission-line LC.
sigmode: float
the required CCF r_max to calculate the lag uncertainties, 0<sigmode<1. r_max < sigmode will be considered as failed CCFs.
MC_ntrials: interger
n (>= 0) trials of Monte Carlo process to evaluate the lag uncertainties.
sim_ntrials: interger
n (>= 0) trials of light curve simulation. Only applied when SIM is True.
weight: Bool
apply the weights to lag posterier if True. Weights are decided by ACF and overlapping points. Our method is similar to Grier+19. See the details in Grier et al. 2019, ApJ, 887, 38
nsmooth_wgts: > 0
a scale factor multiply on the Gaussain bandwidth according to Scott's Rule n^(-0.2) [sigma], n is number of data points, for the lag posterier to decide the region (or local minimum around the primary peak) to calculate the lag and its uncertainties.
sim_ntrials: interger
n (even number, >2) trials of simulation to evaluate the lag significance. 0.5n trials are calculating the CCF between real continuum LC and simulated emission line LC, and the rest 0.5 is opposite.
Nmodel: interger
n (even number, >=2) models to use to produce the simulated LCs. If n =2, one model to produce the simulated continuum LCs and another one is for the simulated emission-line LCs.
sim_var_range: float
the maximum fraction of the variability of the mock LCs can deviate from the actual variability in the real LCs. E.g., the standard deviation of LC 1 is 0.5, then the simulated variability should be between 0.5*(1-x) and 0.5*(1+x).
sim_mode: 0 or 1
if 0, significance test will calculate CCF with real y1 and simualted y2, then real y2 and simulated y1. If 1, CCF will be based on both simulated y1 and y2.
MP: Bool
Multiprocessing with all CPUs if true.
name: string
name of saved figures and fits file.
plotLC: bool
plot orignial LC (and CARMA model) if True.
shift: 'peak' or 'centroid'
shift the emission line LC with peak or centroid lag.
plotCCF: bool
pllt CCF results if True.
save: Bool
save figures and CCF results into fits if ture. MC_ntrial>1, sim_ntrials >2 are needed.
lite: Bool
save a lite version of the fits file.
path: string
path of the saved fits and figures.
Retrun:
t1, y1, e1, t2, y2, e2:
orignial or detrended (if detrend = True) light curves.
lag, r:
time lag array and averaged CCF pearson r array with two ways.
lag_cen, lag_peak:
actual CCF results of lag centroid and peak values.
lag_1, r_1, lag_2, r_2:
one-way time lag and CCF results. 1: interpolate y1. 2: interpolate y2.
npt, npt_1, npt_2:
overlapping points in CCF, npt is averaged and npt_1 (npt_2) is one-way result.
p1, q1, p2, q2:
the assumed CARMA model. The best p and q if auto_pq is True, 1 for continuum and 2 for emission line.
var_sn1, var_sn2:
sginal-to-noise ratio of the variability for continuum and emission-line light curves sqrt(chi^2-DOF), chi^2 = SUM [(Xobs-Xmedian)^2/sig^2] and DOF = N_lightcuve-1.
sample1, sample2:
CARMA MCMC sample for y1 (sample1) and y2 (sample2). see details in Kelly Brandon's carma_pack.
failed_CCF_MC:
number of CCF result hits the tau boundaries (i.e., tau_min or tau_max).
peak_pack_MC, cen_pack_MC:
lag and its error (1 sigma) from Monte Carlo method, i.e., [lower_error, best, upper_error].
lag_peak_MC, lag_cen_MC:
lag peak and centroid in flux randomization with Monte Carlo method. Failed CCF results have been
removed.
p_positive:
fraction of positive simulated CCF peak larger than real-data CCF peak at tau >0, i.e., N1(tau>0&r>r_max_data)/N0(tau>0)
p_all:
fraction of simulated CCF peak larger than real-data CCF peak at any tau, i.e., N2(r>r_max_data)/N0
p_peak:
fraction of simulated CCF peak larger than real-data CCF peak at peak bin.
lag_sim, lag_sim_cen, lag_sim_peak, r_sim, r_sim_cen, r_sim_peak
lag_sim and r_sim saved all the simlated CCF curves. lag_sim_cen, lag_sim_peak, r_sim_cen and r_sim_peak are the corresponding
peak/centroid lag and r.
peak_left, peak_right, cen_left, cen_right:
left and right boundaries of the effctive region used to calculated lag peak/centroid when weights applied.
peak_kde, cen_ked:
smoothed peak/centroid lag MC posterior profile.
'''
self.t1 = np.asarray(t1, dtype = np.float64)
self.y1 = np.asarray(y1, dtype = np.float64)
self.e1 = np.asarray(e1, dtype = np.float64)
self.t2 = np.asarray(t2, dtype = np.float64)
self.y2 = np.asarray(y2, dtype = np.float64)
self.e2 = np.asarray(e2, dtype = np.float64)
self.tau_min = tau_min
self.tau_max = tau_max
self.step = step
self.detrend = detrend
self.interp = interp
self.p1, self.q1, self.p2, self.q2 = p1, q1, p2, q2
self.carma_model = carma_model
self.imode = imode
self.mcmc_nsamples = mcmc_nsamples
self.sig_cut = sig_cut
self.MC_ntrials = MC_ntrials
self.sim_ntrials = sim_ntrials
self.weight = weight
self.sim_var_range = sim_var_range
self.MP = MP
self.plotLC = plotLC
self.plotCCF = plotCCF
self.name = name
self.save = save
self.path = path
self.FR_RSS = FR_RSS
self.sigmode = sigmode
self.lite = lite
self.Nmodel = Nmodel
self.nsmooth_wgts = nsmooth_wgts
self.shift = shift
self.sim_mode = sim_mode
# check parameters
if self.Nmodel <2 and self.Nmodel > self.sim_ntrials:
raise Exception("Number of models should be between 2 and sim_ntrials!")
if self.interp != 'linear' and self.interp != 'carma':
raise Exception("interp must be 'linear' or 'carma'!")
# check the light curve
if t1.shape[0] <10 or t2.shape[0] <10:
raise Exception("The light curve should contain at least 10 data points!")
# avoid nan, inf in data
sm = np.sum(np.isnan(self.t1)) + np.sum(np.isnan(self.t2)) + np.sum(np.isnan(self.y1)) + np.sum(np.isnan(self.y2))+ np.sum(np.isnan(self.e1)) + np.sum(np.isnan(self.e2))+ np.sum(np.isinf(self.t1)) + np.sum(np.isinf(self.t2))+ np.sum(np.isinf(self.y1)) + np.sum(np.isinf(self.y2)) + np.sum(np.isinf(self.e1)) + np.sum(np.isinf(self.e2))
if sm >0:
raise Exception("The light curve should not contain nan or inf data points!")
# avoid zero in errors
if 0. in self.e1:
self.e1 = self.e1 + self.y1*1e-6
if 0. in self.e2:
self.e2 = self.e2 + self.y2*1e-6
# sort the data according to date if they are not inceasing
if np.sum(np.diff(self.t1)<0.0)>0 or np.sum(np.diff(self.t2)<0.0)>0:
ind1 = np.argsort(self.t1)
ind2 = np.argsort(self.t2)
self.t1, self.y1, self.e1 = self.t1[ind1], self.y1[ind1], self.e1[ind1]
self.t2, self.y2, self.e2 = self.t2[ind2], self.y2[ind2], self.e2[ind2]
# calculate the variability SNR
self.var_sn1 = np.sqrt(np.sum(((self.y1-np.median(self.y1))**2/self.e1**2)) - (len(self.y1)-1))
self.var_sn2 = np.sqrt(np.sum(((self.y2-np.median(self.y2))**2/self.e2**2)) - (len(self.y2)-1))
if np.isnan(self.var_sn1):
self.var_sn1 = 0.0
if np.isnan(self.var_sn2):
self.var_sn2 = 0.0
# choose the best p&q in carma model automatically for the LCs
if auto_pq == True:
# continuum
with quiet():
model = cm.CarmaModel(self.t1, self.y1, self.e1)
sample = model.run_mcmc(self.mcmc_nsamples)
best_MLE, pqlist, AICc = model.choose_order(6, njobs = -1, ntrials = 50)
ind_finite = np.where(~np.isnan(AICc) & np.isfinite(AICc))
self.p1, self.q1 = np.array(pqlist)[ind_finite][np.argmin(np.array(AICc)[ind_finite])]
# for emssion-line
with quiet():
model = cm.CarmaModel(self.t2, self.y2, self.e2)
sample = model.run_mcmc(self.mcmc_nsamples)
best_MLE, pqlist, AICc = model.choose_order(5, njobs = -1, ntrials = 50)
ind_finite = np.where(~np.isnan(AICc) & np.isfinite(AICc))
self.p2, self.q2 = np.array(pqlist)[ind_finite][np.argmin(np.array(AICc)[ind_finite])]
# calculate ICCF with linear or CARMA model
global N
N = 0 # for actual data
self.lag, self.r, self.npt = self.ICCF_CARMA(self.t1, self.y1, self.e1, self.t2, self.y2, self.e2, tau_min = self.tau_min, tau_max = self.tau_max,
step = self.step, interp = self.interp, mcmc_nsamples = self.mcmc_nsamples, p1 = self.p1, q1 = self.q1,
p2 = self.p2, q2 = self.q2, carma_model = self.carma_model, imode = self.imode)
N = 1 # for MC or simulations
# do MC to estimate the lag error
if self.MC_ntrials>0:
# apply the weights to lag posterier
if self.weight == True:
self.lag_ACF, self.r_ACF, self.overlap_npt, self.weighting = self.Weight()
# estimate the lag error
self.peak_pack_MC, self.cen_pack_MC, self.lag_peak_MC, self.lag_cen_MC = self.MonteCarlo(self.MC_ntrials)
# do the LC simulation to calculate the lag significance
if self.sim_ntrials>1:
self.lag_sim, self.r_sim = self.CalSig(self.sim_ntrials)
# plot figures
if self.plotLC == True:
self.PlotLC()
if self.plotCCF == True:
self.PlotCCF()
# save results
if self.save == True:
self.SaveResults()
def Detrend(self, t1, y1, t2, y2):
# detrend with nth polynomial
T = np.linspace(t1.min(), t1.max(), 1000)
cont = np.poly1d(np.polyfit(t1, y1, self.detrend))
cont_new = np.interp(t1, T, cont(T))
y1 = y1-cont_new+cont_new.mean()
T = np.linspace(t2.min(), t2.max(), 1000)
line = np.poly1d(np.polyfit(t2, y2, self.detrend))
line_new = np.interp(t2, T, line(T))
y2 = y2-line_new+line_new.mean()
return t1, y1, t2, y2
def ICCF_CARMA(self, t1, y1, e1, t2, y2, e2, tau_min = -100, tau_max = 100, step = 0.2, interp = 'linear',
mcmc_nsamples = 20000, p1 = 1, q1 = 0, p2 = 1, q2 = 0, carma_model = 'random', imode = 0):
"""
Calculate the time lag between continuum and emission-line LCs.
This function returns the lag, CCF r and number of data points used in CCF
"""
np.random.seed()
# If detrend > 0 do detrend for both LCs
if self.detrend > 0 :
t1, y1, t2, y2 = self.Detrend(t1, y1, t2, y2)
# calculate ICCF
# one bug: in carma p and q need to be reset
lag_1, r_1, npt_1, lag_2, r_2, npt_2, = ([] for i in range(6))
#prepare carma mcmc samplers
if interp == 'carma':
# setup the samples for y1, y2 with carma model
with quiet():
model2 = cm.CarmaModel(t2, y2, e2, p = self.p2, q = self.q2) #use self.p self.q since the carma bug
sample2 = model2.run_mcmc(mcmc_nsamples)
model1 = cm.CarmaModel(t1, y1, e1, p = self.p1, q = self.q1)
sample1 = model1.run_mcmc(mcmc_nsamples)
if N == 0:
# for actual data save samples
self.sample2 = sample2
self.sample1 = sample1
# interpolate y2
if imode!= 1: #imode = 0, 2
if interp == 'carma':
time = np.linspace(t2.min(), t2.max(), int((t2.max()-t2.min())/step))
if carma_model == 'mean':
model_mean, model_var = sample2.predict(time, bestfit = 'map')
t2_tmp, y2_tmp = time, model_mean
if carma_model == 'random':
ysim = sample2.simulate(time, bestfit = 'map')
t2_tmp, y2_tmp = time, ysim
else:
# for linear interpolation
t2_tmp, y2_tmp = t2, y2
# calculate pearson r
tau = tau_min
while tau < tau_max:
lag_2.append(tau)
t2_new = t2_tmp-tau
ind_t1 = np.where( (t1 >= t2_new.min()) & (t1 <= t2_new.max()), True, False)
npt_2.append(np.sum(ind_t1)) # "real" data points used in iccf
if np.sum(ind_t1) > 0:
y2_new = np.interp(t1[ind_t1], t2_new, y2_tmp)
r, p = sst.pearsonr(y1[ind_t1], y2_new)
r_2.append(r)
tau = tau+step
else:
raise Exception("No overlaping data in two light curves! Reset the search range!")
lag_2 = np.array(lag_2)
r_2 = np.array(r_2)
npt_2 = np.array(npt_2)
if N == 0:
self.lag_2 = lag_2
self.r_2 = r_2
self.npt_2 = npt_2
# interpolate y1
if imode != 2: # imode = 0, 1
if interp == 'carma':
time = np.linspace(t1.min(), t1.max(), int((t1.max()-t1.min())/step))
if carma_model == 'mean':
model_mean, model_var = sample1.predict(time, bestfit = 'map')
t1_tmp, y1_tmp = time, model_mean
if carma_model == 'random':
ysim = sample1.simulate(time, bestfit = 'map')
t1_tmp, y1_tmp = time, ysim
else:
t1_tmp, y1_tmp = t1, y1
tau = tau_min
while tau <tau_max:
lag_1.append(tau)
t1_new = t1_tmp+tau
ind_t2 = np.where( (t2 >= t1_new.min()) & (t2 <= t1_new.max()), True, False)
npt_1.append(np.sum(ind_t2))
if np.sum(ind_t2) >0:
y1_new = np.interp(t2[ind_t2], t1_new, y1_tmp)
r, p = sst.pearsonr(y2[ind_t2], y1_new)
r_1.append(r)
tau = tau+step
else:
raise Exception("No overlaping data in two light curves! Reset the search range!")
lag_1 = np.array(lag_1)
r_1 = np.array(r_1)
npt_1 = np.array(npt_1)
if N == 0:
self.lag_1 = lag_1
self.r_1 = r_1
self.npt_1 = npt_1
# for different cases
if imode == 0:
lag = (lag_1+lag_2)*0.5
r = (r_1+r_2)*0.5
npt = (npt_1+npt_2)*0.5
elif imode == 1:
lag = lag_1
r = r_1
npt = npt_1
elif imode == 2:
lag = lag_2
r = r_2
npt = npt_2
else:
raise Exception("Please select imode = 0, 1 or 2!")
# get the peak and centroid of data-based CCF
if N == 0:
self.lag_peak, self.lag_cen, self.r_peak, self.r_cen = self.Lag_center(lag, r)
return lag, r, npt
def Lag_center(self, lag, r):
"""
Calculate the peak and centroid of lag and r according to sig_cut.
"""
#peak
np.random.seed()
r_peak = max(r)
if N == 0:
self.rmax = r_peak
r_peak_ind = np.argmax(r)
lag_peak = lag[r_peak_ind]
# centroid, we use the region contains the peak
try:
spline = interpolate.UnivariateSpline(lag, r-self.sig_cut*r.max(), s = 0.)
left = spline.roots()[np.where(lag_peak>spline.roots(), True, False)].max()
right = spline.roots()[np.where(lag_peak<spline.roots(), True, False)].min()
if N == 0:
self.left, self.right = left, right
ind = np.where( (lag <= right.min()) & (lag >= left.max()), True, False)
lag_cen = np.sum(r[ind]*lag[ind])/np.sum(r[ind])
r_cen = r[np.where(lag == lag[min(range(len(lag)), key = lambda i: abs(lag[i]-lag_cen))], True, False)][0]
except:
try:
spline = interpolate.UnivariateSpline(lag, r-0.95*r.max(), s = 0.)
left = spline.roots()[np.where(lag_peak>spline.roots(), True, False)].max()
right = spline.roots()[np.where(lag_peak<spline.roots(), True, False)].min()
if N == 0:
self.left, self.right = left, right
ind = np.where( (lag <= right.min()) & (lag >= left.max()), True, False)
lag_cen = np.sum(r[ind]*lag[ind])/np.sum(r[ind])
r_cen = r[np.where(lag == lag[min(range(len(lag)), key = lambda i: abs(lag[i]-lag_cen))], True, False)][0]
except:
# failed CCF
lag_cen, r_cen = -9999., -9999.
if N == 0:
self.right, self.left = 0., 0.
return lag_peak, lag_cen, r_peak, r_cen
def Weight(self):
"""
Calculate the weights for lag posterier with ACF of contiuum LC and overlaping points following Grier+19.
This function returns the profiles of ACF, overlapping points, and final convoloved weighting.
"""
# calculate ACF for continuum LC
lag_ACF, r_ACF, npt_ACF = self.ICCF_CARMA(self.t1, self.y1, self.e1, self.t1, self.y1, self.e1, tau_min = self.tau_min,
tau_max = self.tau_max, step = self.step, interp = self.interp, mcmc_nsamples = self.mcmc_nsamples,
p1 = self.p1, q1 = self.q1, p2 = self.p2, q2 = self.q2, carma_model = self.carma_model, imode = self.imode)
# calculate overlapping points see Grier+ 2019 P = (N(tau)/N(0))^2
npt0 = float(self.npt[(np.abs(self.lag-0.)).argmin()])
overlap_npt = ((self.npt/npt0))**2 # here a little different from Grier, they use npt_12
overlap_npt = overlap_npt/overlap_npt.max() # normalized to 1
# convolve ACF and overlapping points
weight_unnorm = np.convolve(r_ACF, overlap_npt, 'same')
weighting = weight_unnorm/weight_unnorm.max()
# reset the negtive values to 0
weighting = np.clip(weighting, 0, np.inf)
return lag_ACF, r_ACF, overlap_npt, weighting
def MonteCarlo(self, MC_ntrials):
"""
Run Monte Carlo for error estimation with FR/RSS method.
"""
if self.MP == False:
# no multiprocess
lag_peak_MC = []
lag_cen_MC = []
r_peak_MC = []
for n in range(MC_ntrials):
if self.interp == 'linear':
if self.FR_RSS == 0:
# both FR and RSS
# RSS
id1 = np.hstack((np.random.randint(0, len(self.t1), len(self.t1)), [0,len(self.t1)-1]))
un1, ct1 = np.unique(id1, return_counts = True)
id2 = np.hstack((np.random.randint(0, len(self.t2), len(self.t2)), [0,len(self.t2)-1]))
un2, ct2 = np.unique(id2, return_counts = True)
# FR
y1 = np.random.normal(self.y1[un1], self.e1[un1]/np.sqrt(ct1))
y2 = np.random.normal(self.y2[un2], self.e2[un2]/np.sqrt(ct2))
t1, e1 = self.t1[un1], self.e1[un1]/np.sqrt(ct1)
t2, e2 = self.t2[un2], self.e2[un2]/np.sqrt(ct2)
if self.FR_RSS == 1:
# only FR
y1 = np.random.normal(self.y1, self.e1)
y2 = np.random.normal(self.y2, self.e2)
t1, t2, e1, e2 = self.t1, self.t2, self.e1, self.e2
if self.FR_RSS == 2:
# only RSS
id1 = np.hstack((np.random.randint(0, len(self.t1), len(self.t1)), [0,len(self.t1)-1]))
un1, ct1 = np.unique(id1, return_counts = True)
id2 = np.hstack((np.random.randint(0, len(self.t2), len(self.t2)), [0,len(self.t2)-1]))
un2, ct2 = np.unique(id2, return_counts = True)
t1, y1, e1 = self.t1[un1], self.y1[un1], self.e1[un1]/np.sqrt(ct1)
t2, y2, e2 = self.t2[un2], self.y2[un2], self.e2[un2]/np.sqrt(ct2)
if self.interp == 'carma':
# only FR for carma
t1_tmp = np.linspace(self.t1.min(), self.t1.max(), 1000)
t2_tmp = np.linspace(self.t2.min(), self.t2.max(), 1000)
y1_tmp = self.sample1.simulate(t1_tmp, bestfit = 'map')
y2_tmp = self.sample2.simulate(t2_tmp, bestfit = 'map')
y1 = np.random.normal(np.interp(self.t1, t1_tmp, y1_tmp), self.e1)
y2 = np.random.normal(np.interp(self.t2, t2_tmp, y2_tmp), self.e2)
t1, t2, e1, e2 = self.t1, self.t2, self.e1, self.e2
# do CCF for each MC
lag, r, npt = self.ICCF_CARMA(t1, y1, e1, t2, y2, e2, tau_min = self.tau_min, tau_max = self.tau_max, step = self.step, interp = self.interp, mcmc_nsamples = self.mcmc_nsamples, p1 = self.p1, q1 = self.q1, p2 = self.p2, q2 = self.q2, carma_model = self.carma_model, imode = self.imode)
# find the peak and centroid
lag_peak, lag_cen, r_peak, r_cen = self.Lag_center(lag, r)
#
lag_peak_MC.append(lag_peak)
lag_cen_MC.append(lag_cen)
r_peak_MC.append(r_peak)
# we do not need r_cen_MC
else:
# multiprocessing
global loop, lock, manager, lag_peak_a, lag_cen_a, r_peak_a
loop = 1 # call job1
manager = Manager()
lag_peak_a = manager.list([])
lag_cen_a = manager.list([])
r_peak_a = manager.list([])
lock = Lock() # make sure the right order for appending
p1 = Pool() # use all cores
n = range(MC_ntrials)
p1.map(self, n)
p1.close()
p1.join()
lag_peak_MC = list(lag_peak_a)
lag_cen_MC = list(lag_cen_a)
r_peak_MC = list(r_peak_a)
lag_peak_MC = np.array(lag_peak_MC)
lag_cen_MC = np.array(lag_cen_MC)
r_peak_MC = np.array(r_peak_MC)
# remove failed CCF results
ind = np.where( (lag_peak_MC < self.tau_max) & (lag_peak_MC> self.tau_min) & (r_peak_MC > self.sigmode) & (lag_cen_MC!= -9999.), True, False)
self.failed_CCF_MC = len(lag_peak_MC)-np.sum(ind)
#----------------------------------------------------------------------------
# calculate the centers and 1 sigma error for weighted/unweighted cases.
if np.sum(ind)>0:
if self.weight == False:
lag_peak_low = np.round(np.percentile(lag_peak_MC[ind], 50)-np.percentile(lag_peak_MC[ind], 16), 2)
lag_peak_high = np.round(np.percentile(lag_peak_MC[ind], 84)-np.percentile(lag_peak_MC[ind], 50), 2)
lag_peak = np.round(np.percentile(lag_peak_MC[ind], 50), 2)
peak_pack_MC = np.array([lag_peak_low, lag_peak, lag_peak_high])
lag_cen_low = np.round(np.percentile(lag_cen_MC[ind], 50)-np.percentile(lag_cen_MC[ind], 16), 2)
lag_cen_high = np.round(np.percentile(lag_cen_MC[ind], 84)-np.percentile(lag_cen_MC[ind], 50), 2)
lag_cen = np.round(np.percentile(lag_cen_MC[ind], 50), 2)
cen_pack_MC = np.array([lag_cen_low, lag_cen, lag_cen_high])
else:
# find weighted peak distribution
wgts = np.interp(lag_peak_MC[ind], self.lag, self.weighting)
hist = np.histogram(lag_peak_MC[ind], bins = 50, weights = wgts)
# smooth the weighted posterier histogram to find the local minimum and primary peak
if np.sum(wgts) == 0:
wgts = np.ones(len(wgts))
print("weights is not applied due to the zero weights!")
kde = sst.gaussian_kde(lag_peak_MC[ind], weights = wgts)
kde.set_bandwidth(bw_method = kde.factor * self.nsmooth_wgts)
tt = np.linspace(hist[1].min(),hist[1].max(),1000)
self.lag_peak_kde = tt
peak_kde = kde.evaluate(tt)
self.peak_kde = peak_kde
lag_peak_wgts = tt[np.argmax(peak_kde)]
# find every local minimum in 1D array
loc_min = peak_kde[np.r_[True, peak_kde[1:] < peak_kde[:-1]] & np.r_[peak_kde[:-1] < peak_kde[1:], True]]
loc_min_ind = np.r_[True, peak_kde[1:] < peak_kde[:-1]] & np.r_[peak_kde[:-1] < peak_kde[1:], True]
# find the nearest left and right minimum around peak
try:
self.peak_left = np.max(tt[loc_min_ind][np.where(tt[loc_min_ind]<lag_peak_wgts)])
except:
self.peak_left = self.tau_min
try:
self.peak_right = np.min(tt[loc_min_ind][np.where(tt[loc_min_ind]>lag_peak_wgts)])
except:
self.peak_right = self.tau_max
region = np.where( (lag_peak_MC[ind] >self.peak_left) & (lag_peak_MC[ind]<self.peak_right), True, False)
lag_peak_low = np.round(np.percentile(lag_peak_MC[ind][region], 50)-np.percentile(lag_peak_MC[ind][region], 16), 2)
lag_peak_high = np.round(np.percentile(lag_peak_MC[ind][region], 84)-np.percentile(lag_peak_MC[ind][region], 50), 2)
lag_peak = np.round(np.percentile(lag_peak_MC[ind][region], 50), 2)
peak_pack_MC = np.array([lag_peak_low, lag_peak, lag_peak_high])
self.lag_peak_MC_weighted = lag_peak_MC[ind][region]
#------------------------------------------------------------------------
# find weighted cen distribution
wgts = np.interp(lag_cen_MC[ind], self.lag, self.weighting)
hist = np.histogram(lag_cen_MC[ind], bins = 50, weights = wgts)
# smooth the weighted posterier histogram
if np.sum(wgts) == 0:
wgts = np.ones(len(wgts))
print("weights is not applied due to the zero weights!")
kde = sst.gaussian_kde(lag_cen_MC[ind], weights = wgts)
kde.set_bandwidth(bw_method = kde.factor * self.nsmooth_wgts)
tt = np.linspace(hist[1].min(),hist[1].max(),1000)
self.lag_cen_kde = tt
cen_kde = kde.evaluate(tt)
self.cen_kde = cen_kde
lag_cen_wgts = tt[np.argmax(cen_kde)]
#find every local minimum in 1D array
loc_min = cen_kde[np.r_[True, cen_kde[1:] < cen_kde[:-1]] & np.r_[cen_kde[:-1] < cen_kde[1:], True]]
loc_min_ind = np.r_[True, cen_kde[1:] < cen_kde[:-1]] & np.r_[cen_kde[:-1] < cen_kde[1:], True]
try:
self.cen_left = np.max(tt[loc_min_ind][np.where(tt[loc_min_ind]<lag_cen_wgts)])
except:
self.cen_left = self.tau_min
try:
self.cen_right = np.min(tt[loc_min_ind][np.where(tt[loc_min_ind]>lag_cen_wgts)])
except:
self.cen_right = self.tau_max
region = np.where( (lag_cen_MC[ind] >self.cen_left) & (lag_cen_MC[ind]<self.cen_right), True, False)
lag_cen_low = np.round(np.percentile(lag_cen_MC[ind][region], 50)-np.percentile(lag_cen_MC[ind][region], 16), 2)
lag_cen_high = np.round(np.percentile(lag_cen_MC[ind][region], 84)-np.percentile(lag_cen_MC[ind][region], 50), 2)
lag_cen = np.round(np.percentile(lag_cen_MC[ind][region], 50), 2)
cen_pack_MC = np.array([lag_cen_low, lag_cen, lag_cen_high])
self.lag_cen_MC_weighted = lag_cen_MC[ind][region]
return peak_pack_MC, cen_pack_MC, lag_peak_MC[ind], lag_cen_MC[ind]
else:
return np.array([0., 0., 0.]), np.array([0., 0., 0.]), np.array([]), np.array([])
def weighted_percentile(self, data, percents, wgts = None):
''' calculate the weighted percentile for lag posterior. This function is not used in this code, but could be useful if you want to directly apply weight on the lag posterior.
'''
if wgts is None:
return np.percentile(data, percents)
ind = np.argsort(data)
d = data[ind]
w = wgts[ind]
p = 1.*w.cumsum()/w.sum()*100
y = np.interp(percents, p, d)
return y
def __call__(self, n):
#make job function callable in python2
if loop == 1:
return self._job1(n)
if loop == 2:
return self._job2(n)
if loop == 3:
return self._job3(n)
if loop == 4:
return self._job4(n)
def _job1(self, n):
# multiprocess job function for MC
np.random.seed() # make it random
if self.interp == 'linear':
if self.FR_RSS == 0:
# both FR and RSS
# RSS
id1 = np.hstack(( np.random.randint(0, len(self.t1), len(self.t1)),[0,len(self.t1)-1]))
un1, ct1 = np.unique(id1, return_counts = True)
id2 = np.hstack(( np.random.randint(0, len(self.t2), len(self.t2)),[0,len(self.t2)-1]))
un2, ct2 = np.unique(id2, return_counts = True)
#FR
y1 = np.random.normal(self.y1[un1], self.e1[un1]/np.sqrt(ct1))
y2 = np.random.normal(self.y2[un2], self.e2[un2]/np.sqrt(ct2))
t1, e1 = self.t1[un1], self.e1[un1]/np.sqrt(ct1)
t2, e2 = self.t2[un2], self.e2[un2]/np.sqrt(ct2)
if self.FR_RSS == 1:
# only FR
y1 = np.random.normal(self.y1, self.e1)
y2 = np.random.normal(self.y2, self.e2)
t1, t2, e1, e2 = self.t1, self.t2, self.e1, self.e2
if self.FR_RSS == 2:
#only RSS
id1 = np.hstack(( np.random.randint(0, len(self.t1), len(self.t1)),[0,len(self.t1-1)]))
un1, ct1 = np.unique(id1, return_counts = True)
id2 = np.hstack(( np.random.randint(0, len(self.t2), len(self.t2)),[0,len(self.t2-1)]))
un2, ct2 = np.unique(id2, return_counts = True)
t1, y1, e1 = self.t1[un1], self.y1[un1], self.e1[un1]/np.sqrt(ct1)
t2, y2, e2 = self.t2[un2], self.y2[un2], self.e2[un2]/np.sqrt(ct2)
if self.interp == 'carma':
t1_tmp = np.linspace(self.t1.min(), self.t1.max(), 1000)
t2_tmp = np.linspace(self.t2.min(), self.t2.max(), 1000)
y1_tmp = self.sample1.simulate(t1_tmp, bestfit = 'map')
y2_tmp = self.sample2.simulate(t2_tmp, bestfit = 'map')
y1 = np.random.normal(np.interp(self.t1, t1_tmp, y1_tmp), self.e1)
y2 = np.random.normal(np.interp(self.t2, t2_tmp, y2_tmp), self.e2)
t1, t2, e1, e2 = self.t1, self.t2, self.e1, self.e2
lag, r, npt = self.ICCF_CARMA(t1, y1, e1, t2, y2, e2, tau_min = self.tau_min, tau_max = self.tau_max, step = self.step, interp = self.interp, mcmc_nsamples = self.mcmc_nsamples, p1 = self.p1, q1 = self.q1, p2 = self.p2, q2 = self.q2, carma_model = self.carma_model, imode = self.imode)
lag_peak, lag_cen, r_peak, r_cen = self.Lag_center(lag, r)
lock.acquire()
lag_peak_a.append(lag_peak)
lag_cen_a.append(lag_cen)
r_peak_a.append(r_peak)
lock.release()
def _job2(self, nn2_ysim_t2_cadence):
np.random.seed()
n, ysim_t2_cadence = nn2_ysim_t2_cadence
# multiprocess job function for CalSig, simulated y2
lagsim, rsim, nptsim = self.ICCF_CARMA(self.t1, self.y1, self.e1, self.t2, ysim_t2_cadence, self.e2, tau_min = self.tau_min,
tau_max = self.tau_max, step = self.step,
interp = self.interp, mcmc_nsamples = self.mcmc_nsamples, p1 = self.p1,
q1 = self.q1, p2 = self.p2, q2 = self.q2, carma_model = self.carma_model, imode = self.imode)
lock.acquire()
nn2.append(n)
lag_sim2_a.append(lagsim)
r_sim2_a.append(rsim)
lock.release()
def _job3(self, nn1_ysim_t1_cadence):
np.random.seed()
n, ysim_t1_cadence = nn1_ysim_t1_cadence
# multiprocess job function for CalSig, simulated y1
lagsim, rsim, nptsim = self.ICCF_CARMA(self.t1, ysim_t1_cadence, self.e1, self.t2, self.y2, self.e2, tau_min = self.tau_min,
tau_max = self.tau_max, step = self.step,
interp = self.interp, mcmc_nsamples = self.mcmc_nsamples, p1 = self.p1,
q1 = self.q1, p2 = self.p2, q2 = self.q2, carma_model = self.carma_model, imode = self.imode)
lock.acquire()
nn1.append(n)
lag_sim1_a.append(lagsim)
r_sim1_a.append(rsim)
lock.release()
def _job4(self, nn0_ysim_t12_cadence):
np.random.seed()
n, ysim_t1_cadence, ysim_t2_cadence = nn0_ysim_t12_cadence
# multiprocess job function for CalSig, simulated y1 and y2
lagsim, rsim, nptsim = self.ICCF_CARMA(self.t1, ysim_t1_cadence, self.e1, self.t2, ysim_t2_cadence, self.e2, tau_min = self.tau_min,
tau_max = self.tau_max, step = self.step,
interp = self.interp, mcmc_nsamples = self.mcmc_nsamples, p1 = self.p1,
q1 = self.q1, p2 = self.p2, q2 = self.q2, carma_model = self.carma_model, imode = self.imode)
lock.acquire()
nn0.append(n)
lag_sim0_a.append(lagsim)
r_sim0_a.append(rsim)
lock.release()
def CalSig(self, sim_ntrials):
"""
calculate the CCF between real (simulated) continuum LC and simulated (real) emission-line LC. The simulated continuum/emission-line LCs are produced according to the feature (i.e., p and q) of orignial LC. If interp = 'linear', we use DRW model to produce the simulated LCs.
"""
# prepare for simulated y2
ysim_t2_cadence = []
if self.Nmodel == 2:
#one model to produce all simulated LCs
if self.interp == 'linear' or self.imode == 2:
with quiet():
model = cm.CarmaModel(self.t2, self.y2, self.e2, p = self.p2, q = self.q2)
self.sample2 = model.run_mcmc(self.mcmc_nsamples)
if self.sim_mode == 1:
# produce sim_ntrials simulated LC for both continuum and emission lines
sim_ntrials = sim_ntrials * 2
# produce 100x long LC and randomly select a segment from 10 to 100 and impose the same cadence and errors
n1 = 0
acceptance_limit = 0.001
acceptance_rate_y2 = []
for i in range(sim_ntrials/2):
#print i
nn = np.random.randint(10, 100)
tsim = np.arange(nn*self.t2.max(), nn*self.t2.max()+self.t2.max()-self.t2.min(),
(self.t2.max()-self.t2.min())/self.t2.shape[0])
if self.Nmodel>2:
if (i%(sim_ntrials/self.Nmodel) == 0) and (n1<self.Nmodel/2):
n1 = n1+1
with quiet():
model = cm.CarmaModel(self.t2, self.y2, self.e2, p = self.p2, q = self.q2)
sample = model.run_mcmc(self.mcmc_nsamples)
ysim = sample.simulate(tsim, bestfit = 'map')
Tsim = tsim-tsim.min()+self.t2.min()
ysim_t2 = np.interp(self.t2, Tsim, ysim)
# let variability amp is between + - 20% of orignial LC
nt = 0
good = 0.
bad = 0.
while nt <1:
if (good/(good+bad) < acceptance_limit) and (good + bad > 1./acceptance_limit):
raise Exception("Your acceptance rate is < 1% for producing mock light curve, you can set a higher MCMC_nsamples or loose the variability range (sim_var_range).")
if (np.random.normal(ysim_t2, self.e2).std() < self.y2.std()*(1.+self.sim_var_range)) and (np.random.normal(ysim_t2,self.e2).std() > self.y2.std()*(1.-self.sim_var_range)):
nt = 1
good = good + 1
else:
ysim = sample.simulate(tsim, bestfit = 'map')
ysim_t2 = np.interp(self.t2, Tsim, ysim)
bad = bad +1
else:
ysim = self.sample2.simulate(tsim, bestfit = 'map')
Tsim = tsim-tsim.min()+self.t2.min()
ysim_t2 = np.interp(self.t2, Tsim, ysim)
nt=0
good = 0.
bad = 0.
while nt <1:
if (good/(good + bad + 1.) < acceptance_limit) and (good + bad > 1./acceptance_limit):
raise Exception("Your acceptance rate is < 0.1% for producing mock light curve, you can set a higher MCMC_nsamples or loose the variability range (sim_var_range).")
if (np.random.normal(ysim_t2, self.e2).std() < self.y2.std()*(1.+self.sim_var_range)) and (np.random.normal(ysim_t2,self.e2).std() > self.y2.std()*(1.-self.sim_var_range)):
nt = 1
good = good + 1
else:
ysim = self.sample2.simulate(tsim, bestfit = 'map')
ysim_t2 = np.interp(self.t2, Tsim, ysim)
bad = bad + 1
acceptance_rate_y2.append(good/(good+bad+1.))
y2_sim = ysim_t2
ysim_t2_cadence.append(y2_sim)
self.acceptance_rate_y2 = np.array(acceptance_rate_y2).mean()
ysim_t2_cadence = np.array(ysim_t2_cadence).reshape(-1, len(self.t2))
self.ysim_t2_cadence = np.array(ysim_t2_cadence)
# prepare for simulated y1
ysim_t1_cadence = []
if self.Nmodel == 2:
if self.interp == 'linear' or self.imode == 1:
with quiet():
model = cm.CarmaModel(self.t1, self.y1, self.e1, p = self.p1, q = self.q1)
self.sample1 = model.run_mcmc(self.mcmc_nsamples)
n2 = 0
acceptance_rate_y1 = []
for j in range(sim_ntrials/2):
#print j
nn = np.random.randint(10, 100)
tsim = np.arange(nn*self.t1.max(), nn*self.t1.max()+self.t1.max()-self.t1.min(),
(self.t1.max()-self.t1.min())/self.t1.shape[0])
if self.Nmodel>2:
if (j%(sim_ntrials/self.Nmodel) == 0) and (n2<self.Nmodel/2):
n2 = n2+1
with quiet():
model = cm.CarmaModel(self.t1, self.y1, self.e1, p = self.p1, q = self.q1)
sample = model.run_mcmc(self.mcmc_nsamples)
ysim = sample.simulate(tsim, bestfit = 'map')
Tsim = tsim-tsim.min()+self.t1.min()
ysim_t1 = np.interp(self.t1, Tsim, ysim)
# let variability amp is between + - 20% of orignial LC
nt = 0
good = 0.
bad = 0.
while nt <1:
if (good/(good + bad +1.) < acceptance_limit) and (good + bad > 1./acceptance_limit):
raise Exception("Your acceptance rate is < 1% for producing mock light curve, you can set a higher MCMC_nsamples or loose the variability range (sim_var_range).")
if (np.random.normal(ysim_t1, self.e1).std() < self.y1.std()*(1.+self.sim_var_range)) and (np.random.normal(ysim_t1,self.e1).std() > self.y1.std()*(1.-self.sim_var_range)):
nt=1
good = good +1
else:
ysim = sample.simulate(tsim, bestfit = 'map')
ysim_t1 = np.interp(self.t1, Tsim, ysim)
bad = bad +1
else:
ysim = self.sample1.simulate(tsim, bestfit = 'map')
Tsim = tsim-tsim.min()+self.t1.min()
ysim_t1 = np.interp(self.t1, Tsim, ysim)
nt=0
good = 0
bad = 0
while nt <1:
if (good/(good + bad + 1.) < acceptance_limit) and (good + bad > 1./acceptance_limit):
raise Exception("Your acceptance rate is < 1% for producing mock light curve, you can set a higher MCMC_nsamples or loose the variability range (sim_var_range).")
if (np.random.normal(ysim_t1, self.e1).std() < self.y1.std()*(1.+self.sim_var_range)) and (np.random.normal(ysim_t1,self.e1).std() > self.y1.std()*(1.-self.sim_var_range)):
nt=1
good = good +1
else:
ysim = self.sample1.simulate(tsim, bestfit = 'map')
ysim_t1 = np.interp(self.t1, Tsim, ysim)
bad = bad +1
acceptance_rate_y1.append(good/(good+bad+1.))
y1_sim = ysim_t1
ysim_t1_cadence.append(y1_sim)
self.acceptance_rate_y1 = np.array(acceptance_rate_y1).mean()
ysim_t1_cadence = np.array(ysim_t1_cadence).reshape(-1, len(self.t1))
self.ysim_t1_cadence = np.array(ysim_t1_cadence)
#-------multiprocessing or not-----------------
if self.MP == False:
#-----single simulated LC or both-----
if self.sim_mode == 0:
#for simulated y2
lag_sim2_all = []
r_sim2_all = []
# measure the CCF for the simulated and real LCs
for i in range(len(ysim_t2_cadence)):
lagsim, rsim, nptsim = self.ICCF_CARMA(self.t1, self.y1, self.e1, self.t2, ysim_t2_cadence[i], self.e2, tau_min = self.tau_min,
tau_max = self.tau_max, step = self.step,
interp = self.interp, mcmc_nsamples = self.mcmc_nsamples, p1 = self.p1,
q1 = self.q1, p2 = self.p2, q2 = self.q2, carma_model = self.carma_model, imode = self.imode)
lag_sim2_all.append(lagsim)
r_sim2_all.append(rsim)
lag_sim2_all = np.array(lag_sim2_all)
r_sim2_all = np.array(r_sim2_all)
#for simulated y1
lag_sim1_all = []
r_sim1_all = []
plt.figure(figsize = (8, 6))
for j in range(len(ysim_t1_cadence)):
lagsim, rsim, nptsim = self.ICCF_CARMA(self.t1, ysim_t1_cadence[j], self.e1, self.t2, self.y2, self.e2, tau_min = self.tau_min,
tau_max = self.tau_max, step = self.step,
interp = self.interp, mcmc_nsamples = self.mcmc_nsamples, p1 = self.p1,
q1 = self.q1, p2 = self.p2, q2 = self.q2, carma_model = self.carma_model, imode = self.imode)
lag_sim1_all.append(lagsim)
r_sim1_all.append(rsim)
lag_sim1_all = np.array(lag_sim1_all)
r_sim1_all = np.array(r_sim1_all)
else:
lag_sim0_all = []
r_sim0_all = []
# measure the CCF for the simulated and real LCs
for i in range(len(ysim_t1_cadence)):
lagsim, rsim, nptsim = self.ICCF_CARMA(self.t1, self.y1, ysim_t1_cadence[i], self.t2, ysim_t2_cadence[i], self.e2, tau_min = self.tau_min,
tau_max = self.tau_max, step = self.step,
interp = self.interp, mcmc_nsamples = self.mcmc_nsamples, p1 = self.p1,
q1 = self.q1, p2 = self.p2, q2 = self.q2, carma_model = self.carma_model, imode = self.imode)
lag_sim0_all.append(lagsim)
r_sim0_all.append(rsim)
lag_sim0_all = np.array(lag_sim0_all)
r_sim0_all = np.array(r_sim0_all)
else:
#multiprocessing
global loop, lag_sim1_a, lag_sim2_a, r_sim1_a, r_sim2_a, nn1, nn2, nn0, lag_sim0_a, r_sim0_a
loop = 2 # go job2
lag_sim1_a = manager.list([])
lag_sim2_a = manager.list([])
r_sim1_a = manager.list([])
r_sim2_a = manager.list([])
nn1 = manager.list([])
nn2 = manager.list([])
nn0 = manager.list([])
lag_sim0_a = manager.list([])
r_sim0_a = manager.list([])
p2 = Pool()
p2.map(self, zip(range(len(ysim_t2_cadence)), ysim_t2_cadence))
p2.close()
p2.join()
lag_sim2_all = np.array(list(lag_sim2_a))[np.argsort(nn2)]