def lombscargle(self): print(self.length) model = LombScargleFast(silence_warnings=True).fit(self.d, self.m, 0.1) model.optimizer.period_range = (0.02, min(10, self.length)) period = model.best_period print "period is ", model.best_period sco = model.score(period, ) print "the score is ", sco periods, power = model.periodogram_auto( nyquist_factor=2000) ##Depend the highest fequency of the model. #f=open("tet.txt",'wb') #f.write('period is:'+str(period)) #f.close x = periods y = power fig, axes = plt.subplots(1, 2, figsize=(8, 8)) ax1, ax2 = axes.flatten() ax1.plot(x, y, color="red", linewidth=2) ax1.set_xlim(0, 10) ax1.set_xlabel("period (days)") ax1.set_ylabel("Lomb-Scargle Power") ax1.legend() phase = self.d / (1 * period) phase = phase % 1 * 1 ax2.plot(phase, self.m, 'ro') ax2.set_xlabel("phase") ax2.set_ylabel("Mag") #ax2.set_ylim(0,1) ax2.legend() # plt.savefig('/home/yf/ptftry/pic/Lomb-Scargle.png') plt.show() return period
def getPeriod(HJD, TMag, TdMag, p_range=None, makeGraph=None, giveP=None): if p_range is None: p_min = .01 p_max = 2 else: p_min, p_max = p_range if makeGraph is None: makeGraph = False if giveP is None: giveP = False lsf = LombScargleFast() lsf.optimizer.period_range = (p_min, p_max) lsf.fit(HJD, TMag, TdMag) period = lsf.best_period if giveP: print("\n") print("Period: " + str(period)) print("\n") if makeGraph: periods = np.linspace(p_min, p_max, 1000) scores = lsf.score(periods) plt.plot(periods, scores) plt.xlabel("Period") plt.ylabel("Probability") plt.show() return period
def fit_lomb_scargle(self): from gatspy.periodic import LombScargleFast period_range = (0.005 * (max(self.times) - min(self.times)), 0.95 * (max(self.times) - min(self.times))) model_gat = LombScargleFast(fit_period=True, silence_warnings=True, optimizer_kwds={ 'period_range': period_range, 'quiet': True }) model_gat.fit(self.times, self.measurements, self.errors) self.best_period = model_gat.best_period self.best_score = model_gat.score(model_gat.best_period).item()
def ellc_gatspy_sim(j, t_zero, period, a, q, f_c, f_s, ld_1, ld_2, Teff1, Teff2, logg1, logg2, M_H, R_1, R_2, incl, sbratio, shape_1, shape_2, appmag, bnum, n_band, n_base, return_dict, plot_dict, db, dbID_OpSim): #this is the general simulation - ellc light curves and gatspy periodograms #global filters, sigma_sys, totaltime, cadence print("here in ellc_gatspy_sim") totalt = [] totalmag = [] totaldmag = [] totalfilts = [] if (verbose): print(j, 't_zero = ', t_zero) print(j, 'period = ', period) print(j, 'semimajor = ', a) print(j, 'massrat = ', q) print(j, 'f_c = ', f_c) print(j, 'f_s = ', f_s) print(j, 'ld_1 = ', ld_1) print(j, 'ld_2 = ', ld_2) print(j, 'radius_1 = ', R_1) print(j, 'radius_2 = ', R_2) print(j, 'incl = ', incl) print(j, 'sbratio = ', sbratio) print(j, 'Teff1 = ', Teff1) print(j, 'Teff2 = ', Teff2) if (do_plot): # for plotting the periodograms for_plotting = dict() pds = np.linspace(0.2, 2. * period, 10000) for_plotting['periods'] = pds for_plotting['seed'] = bnum #set the random seed #np.random.seed(seed = seed) delta_mag = 0. if (opsim): cursor = getSummaryCursor(db, dbID_OpSim) for i, filt in enumerate(filters): if (opsim): print("I'm running code with OpSim!") time = getexpDate(cursor, dbID_OpSim, filt) nobs = len(time) print("time_OpSim = ", time) else: print("I'm NOT running code with OpSim!") nobs = int(round(totaltime / (cadence * len(filters)))) time = np.sort(totaltime * np.random.random(size=nobs)) #time.sort() #maybe not needed and taking time drng = max(time) - min(time) # if (time_OpSim[0] != None): if (time[0] != None): filtellc = filt if (filt == 'y_'): filtellc = 'z_' #because we don't have limb darkening for y_ ########################## #Can we find limb darkening coefficients for y band?? (Then we wouldn't need this trick) ########################## ldy_filt = ellc.ldy.LimbGravityDarkeningCoeffs(filtellc) a1_1, a2_1, a3_1, a4_1, y = ldy_filt(Teff1, logg1, M_H) a1_2, a2_2, a3_2, a4_2, y = ldy_filt(Teff2, logg2, M_H) ldc_1 = [a1_1, a2_1, a3_1, a4_1] ldc_2 = [a1_2, a2_2, a3_2, a4_2] lc = ellc.lc(time, t_zero=t_zero, period=period, a=a, q=q, f_c=f_c, f_s=f_s, ld_1=ld_1, ldc_1=ldc_1, ld_2=ld_2, ldc_2=ldc_2, radius_1=R_1, radius_2=R_2, incl=incl, sbratio=sbratio, shape_1=shape_1, shape_2=shape_2, grid_1='default', grid_2='default') if (verbose): print(j, 'ldc_1 = ', ldc_1) print(j, 'ldc_2 = ', ldc_2) print(j, 'time = ', time[0:10]) print(j, 'lc = ', lc[0:10]) if (min(lc) >= 0): magn = -2.5 * np.log10(lc) magnitude = appmag + magn #Ivezic 2008, https://arxiv.org/pdf/0805.2366.pdf , Table 2 sigma2_rand = getSig2Rand(filt, magnitude) #random photometric error sigma = ((sigma_sys**2.) + (sigma2_rand))**(1. / 2.) #now add the uncertaintly onto the magnitude #magnitude_obs = magnitude magnitude_obs = [ np.random.normal(loc=x, scale=sig) for (x, sig) in zip(magnitude, sigma) ] delta_mag = max( [delta_mag, abs(min(magnitude_obs) - max(magnitude_obs))]) if (verbose): print(j, 'magn = ', magn[0:10]) print(j, 'magnitude = ', magnitude[0:10]) print(j, 'magnitude_obs = ', magnitude_obs[0:10]) print(j, "sigma2_rand = ", sigma2_rand[0:10]) print(j, "sigma = ", sigma[0:10]) print(j, "delta_mag = ", delta_mag) t = np.array(time) totalt.extend(t) mag = np.array(magnitude_obs) totalmag.extend(mag) dmag = np.array(sigma) totaldmag.extend(dmag) filts = np.array(filt) specfilt = nobs * [filt] totalfilts.extend(specfilt) #model = LombScargle(fit_period = True) model = LombScargleFast(fit_period=True) model.optimizer.period_range = (0.2, drng) model.fit(t, mag, dmag) LSS = model.best_period print('LSS running') return_dict[j] = return_dict[j] + [LSS] print('LSS in return_dict') if (verbose): print(j, "LSS = ", LSS) if (do_plot): if (i == 0): for_plotting['phase_obs'] = dict() for_plotting['mag_obs'] = dict() for_plotting['mag'] = dict() for_plotting['scores'] = dict() for_plotting['LSS'] = dict() phase_obs = np.array([(tt % period) / period for tt in time]) scores = model.score(pds) for_plotting['phase_obs'][filt] = phase_obs for_plotting['mag_obs'][filt] = magnitude_obs for_plotting['mag'][filt] = magnitude for_plotting['scores'][filt] = scores for_plotting['LSS'][filt] = LSS else: return_dict[j] = return_dict[j] + [-999.] if (verbose): print(j, "bad fluxes", filt) #raise Exception('stopping') if (len(totalmag) > 0): t = np.array(totalt) mag = np.array(totalmag) dmag = np.array(totaldmag) filts = np.array(totalfilts) drng = max(t) - min(t) print("drng", drng) model = LombScargleMultiband(Nterms_band=n_band, Nterms_base=n_base, fit_period=True) #model = LombScargleMultibandFast(Nterms=2, fit_period = True) #this is not working in parallel for some reason model.optimizer.period_range = (0.2, drng) model.fit(t, mag, dmag, filts) LSM = model.best_period print('LSM running') return_dict[j] = return_dict[j] + [LSM] #n_totalrun += 1 print('LSM in return_dict') return_dict[j] = return_dict[j] + [delta_mag] else: return_dict[j] = return_dict[j] + [-999.] return_dict[j] = return_dict[j] + [-999.]