def get_LS(time, flux, freq): x = time y = flux LS = LombScargle(x, y, normalization='standard') power = LS.power(freq) max_NormLSP = np.max(power) LS_power = LombScargle(x, y, normalization='psd') LSP = LS_power.power(freq) # power_freq=np.max(LSP[np.where((1/freq-2492.73)<200)]) max_LSP = np.max(LSP) FP = LS.false_alarm_probability(power.max(), minimum_frequency=freq[0], maximum_frequency=freq[-1], method='baluev') plt.plot(freq, power) # plt.plot([1/2492,1/2492.],[0,np.max(power)],'--',linewidth=1) plt.semilogx() print(1. / freq[np.where(power == np.max(power))]) out_period = 1. / freq[np.where(power == np.max(power))][0] # plt.show() plt.close() # return [FP,out_period] return [FP, out_period, max_LSP, max_NormLSP]
def spike(time, flux): """ Function to calculate the metric called "spike" for K2 objects. When spike > 1.0, the object is said to have bad arc drift. args time : time reference for light curve (seconds) flux : flux of light curve returns spike : spike metric """ # get fit for below the 5 day noise floor freq, power = LS_PSD(time, flux, f=k2_freq) # noise floor are freqencies > X days, convert to Hz noise_floor_days = 5 noise_floor_mask = freq > (2 * np.pi / (noise_floor_days * 86400)) m_noise, b_noise = np.polyfit( np.log10(freq)[noise_floor_mask], np.log10(power)[noise_floor_mask], 1) freq_six_hour = (2 * np.pi / (0.5 * 86400) ) # frequency for 2*6 hour thurster fire # Compute the LS based power for single frequency model = LombScargle(time, flux) freq_temp = k2_freq[320] # a very particular frequency power_temp = model.power(freq_temp, method="fast", normalization="psd") power_temp /= len(time) spike = np.log10(power_temp / (10**(np.log10(freq_six_hour) * m_noise + b_noise))) return spike
def get_LS(time, flux,freq): def get_hist(t, len_bin): ###将输入的time信息,按照len_bin的长度输出为lc t_test = t - t[0] a = [0 for i in range(int(t_test[-1] / len_bin) + 1)] for i in range(len(t_test)): a[int(t_test[i] / len_bin)] += 1 a = np.array(a) x = np.arange(bin_len / 2., (time[-1] - time[0]) + bin_len / 2., bin_len) y = flux LS = LombScargle(x, y,normalization = 'standard') # LS = LombScargle(x, y, dy, normalization='psd') power = LS.power(freq) FP=LS.false_alarm_probability(power.max(),samples_per_peak=5, nyquist_factor=5,minimum_frequency = freq[0], maximum_frequency = freq[-1],method='baluev') FP_99 = LS.false_alarm_level(0.01, samples_per_peak=10, nyquist_factor=5,minimum_frequency = freq[0], maximum_frequency = freq[-1],method='naive') FP_90 = LS.false_alarm_level(0.1, samples_per_peak=10, nyquist_factor=5, minimum_frequency=freq[0], maximum_frequency=freq[-1], method='naive') FP_68 = LS.false_alarm_level(0.32, samples_per_peak=10, nyquist_factor=5, minimum_frequency=freq[0], maximum_frequency=freq[-1], method='naive') plt.title('{0},FP={1}'.format('test',FP)) plt.plot(freq, power) print(1./freq[np.where(power==np.max(power))]) plt.plot([freq[0], freq[-1]], [FP_99, FP_99], '--') plt.plot([freq[0], freq[-1]], [FP_90, FP_90], '--') plt.plot([freq[0], freq[-1]], [FP_68, FP_68], '--') plt.show()
def multiterm_periodogram(t, y, dy, omega, n_terms=3): """Perform a multiterm periodogram at each omega This calculates the chi2 for the best-fit least-squares solution for each frequency omega. Parameters ---------- t : array_like sequence of times y : array_like sequence of observations dy : array_like sequence of observational errors omega : float or array_like frequencies at which to evaluate p(omega) Returns ------- power : ndarray P = 1. - chi2 / chi2_0 where chi2_0 is the chi-square for a simple mean fit to the data """ # TODO: deprecate this ls = LombScargle(t, y, dy, nterms=n_terms) frequency = np.asarray(omega) / (2 * np.pi) return ls.power(frequency)
def windowfunction(self, width=None, oversampling=10): """Spectral window function. Parameters: width (float, optional): The width in Hz on either side of zero to calculate spectral window. oversampling (float, optional): Oversampling factor. Default=10. """ if width is None: width = 100 * self.df freq_cen = 0.5 * self.nyquist Nfreq = int(oversampling * width / self.df) freq = freq_cen + (self.df / oversampling) * np.arange( -Nfreq, Nfreq, 1) x = 0.5 * np.sin(2 * np.pi * freq_cen * self.ls.t) + 0.5 * np.cos( 2 * np.pi * freq_cen * self.ls.t) # Calculate power spectrum for the given frequency range: ls = LombScargle(self.ls.t, x, center_data=True, fit_mean=self.fit_mean) power = ls.power(freq, method='fast', normalization='psd', assume_regular_frequency=True) power /= power[int(len(power) / 2)] # Normalize to have maximum of one freq -= freq_cen freq *= 1e6 return freq, power
def amplitude_spectrum( self, t, y, fmin: float = None, fmax: float = None, oversample_factor: float = 5.0, ) -> tuple: """Calculates the amplitude spectrum at a given time and flux input Args: t (np.ndarray): Time values y (np.ndarray): Flux values fmin (float, optional): Minimum frequency. Defaults to None. fmax (float, optional): Maximum frequency. Defaults to None. oversample_factor (float, optional): Amount by which to oversample the light curve. Defaults to 5.0. Returns: tuple: Frequency and amplitude arrays """ # t, y = self.time, self.residual tmax = t.max() tmin = t.min() df = 1.0 / (tmax - tmin) if fmin is None: fmin = df if fmax is None: fmax = 0.5 / np.median(np.diff(t)) # *nyq_mult freq = np.arange(fmin, fmax, df / oversample_factor) model = LombScargle(t, y) sc = model.power(freq, method="fast", normalization="psd") fct = np.sqrt(4.0 / len(t)) amp = np.sqrt(sc) * fct return freq, amp
def LS_PSD(t, y, f=None): """ Normalized Lomb Scargle Power Spectrum Density args t : time array y : flux array f(optional) : frequency range returns f : frequency range with first and last entry removed power_ls: lomb-scargle power """ N = len(t) # number of datapoints #f = np.fft.rfftfreq(N**2, (t[-1] - t[0])) # frequency range (from Uttley et al. 2002) if f is None: f = np.fft.rfftfreq(N, (t[1] - t[0])) # frequency range # Compute the LS based power spectrum estimates model = LombScargle(t, y) power_ls = model.power(f[1:-1], method="fast", normalization="psd") # >>> To get the LS based PSD in the correct units, normalize by N <<< power_ls /= N return f[1:-1], power_ls
def funcPoint(iP, axP): siteNo = siteNoLst[iP] dfP1, dfObs = basins.loadSeq(outName, siteNo) t = dfPred.index.values.astype(np.datetime64) tBar = np.datetime64('2000-01-01') # Silica rmse, corr = waterQuality.calErrSeq(dfP1[code], dfObs[code]) axplot.plotTS(axP[0], t, [dfP1[code], dfObs[code]], tBar=tBar, legLst=['LSTM', 'obs'], styLst='-*', cLst='br') tStr = '{}, rmse [{:.2f} {:.2f}], corr [{:.2f} {:.2f}]'.format( siteNo, rmse[0], rmse[1], corr[0], corr[1]) axP[0].set_title('Silica ' + tStr) # rm outlier df = dfObs[dfObs['00955'].notna().values] y = df['00955'].values yV = y[y < np.percentile(y, 99)] yV = yV[yV > np.percentile(y, 1)] ul = np.mean(yV) + np.std(yV) * 5 dfObs[dfObs['00955'] > ul] = np.nan # fourier df = dfObs[dfObs['00955'].notna().values] # nt = len(dfObs) nt = 365 * 5 x = (df.index.values.astype('datetime64[D]') - np.datetime64('1979-01-01')).astype(np.float) y = df['00955'].values freq = np.fft.fftfreq(nt)[1:] ls = LombScargle(x, y) power = ls.power(freq) df2 = dfP1['00955'] x2 = (df2.index.values.astype('datetime64[D]') - np.datetime64('1979-01-01')).astype(np.float) y2 = df2.values ls2 = LombScargle(x2, y2) power2 = ls2.power(freq) axP[1].set_ylabel('normalize spectrum') indF = np.where(freq > 0)[0] axP[1].plot(1 / freq[indF], power2[indF], 'b', label='lstm') axP[1].plot(1 / freq[indF], power[indF], 'r', label='obs') axP[1].legend() axP[1].set_ylabel('power') axP[1].set_xlabel('period (day)')
def make_lsp(time, flux, flux_err, p_min=1/24.0, p_max=1.0, oversampling=5, lsp_kwargs={}, nterms=1, use_gatspy=False): """ Compute the Lomb-Scargle periodogram using the astropy LombScargle class. Parameters ---------- time : numpy.ndarray The time stamps of the time series **IN DAYS** flux : numpy.ndarray Flux measurements corresponding to the time stamps flux_err : numpy.ndarray The flux uncertainties corresponding to the data. p_min, p_max : float, float The minimum and maximum period to search, **IN DAYS** oversampling : int The oversampling factor; default is 5 lsp_kwargs : dict Optional keyword arguments for astropy.stats.LombScargle Returns ------- freq : numpy.ndarray The frequencies for which the Lomb-Scargle periodogram was computed power : numpy.ndarray Normalized Lomb-Scargle power """ # number of frequencies to sample nfreq = int(oversampling*(p_max - p_min)/p_min) # number of frequencies # make a list of frequencies freq = np.linspace(1/p_max, 1/p_min, nfreq) if use_gatspy: model = periodic.LombScargle(fit_period=True, center_data=True, fit_offset=True, Nterms=nterms) model.optimizer.period_range = (p_min, p_max) model.fit(time, flux, flux_err) periods = 1/freq power = model.periodogram(periods) power = power else: # initialize the lomb-scargle periodogram ls = LombScargle(time, flux, dy=flux_err, **lsp_kwargs, nterms=nterms) # compute the power at each given frequency power = ls.power(freq) return freq, power
def get_period(t, y, dy, frequencies=None): if frequencies == None: frequencies = get_range_freqs(t) ls = LombScargle(t, y, dy) power = ls.power(frequencies) best_freq = frequencies[np.argmax(power)] period = round(1 / best_freq, 3) return period
def get_LS(time, flux, freq, outpath=None, outname=None, save=False, show=True): x = time y = flux LS = LombScargle(x, y, normalization='standard') power = LS.power(freq) max_NormLSP = np.max(power) period_peak = 1. / freq[np.where(power == np.max(power))][0] FP = LS.false_alarm_probability(power.max(), minimum_frequency=freq[0], maximum_frequency=freq[-1], method='baluev') FP_99 = LS.false_alarm_level(0.0027, minimum_frequency=freq[0], maximum_frequency=freq[-1], method='baluev') FP_95 = LS.false_alarm_level(0.05, minimum_frequency=freq[0], maximum_frequency=freq[-1], method='baluev') FP_68 = LS.false_alarm_level(0.32, minimum_frequency=freq[0], maximum_frequency=freq[-1], method='baluev') plt.figure(1, (6, 6)) # plt.title('Period={0:.2f}'.format(period_peak), font1) plt.text(freq[np.where(power == np.max(power))][0] * 1.3, max_NormLSP * 0.95, 'P={0:.2f}s'.format(period_peak), fontsize=18, fontweight='semibold') plt.plot(freq, power) plt.semilogx() out_period = 1. / freq[np.where(power == np.max(power))][0] plt.plot([freq[0], freq[-1]], [FP_99, FP_99], '--') plt.plot([freq[0], freq[-1]], [FP_95, FP_95], '--') plt.plot([freq[0], freq[-1]], [FP_68, FP_68], '--') plt.text(freq[0], FP_99, '1-FAP 99.73%', font1) plt.text(freq[0], FP_95, '95%', font1) plt.text(freq[0], FP_68, '68%', font1) plt.xlabel('Frequency (Hz)', font1) plt.ylabel('Normalized LS Periodogram', font1) plt.tick_params(labelsize=16) if save: plt.savefig(outpath + outname + '_LS.pdf', bbox_inches='tight', pad_inches=0.01) if show: plt.show() else: plt.close() return [FP, out_period, max_NormLSP]
def get_LS(time, flux, freq, outpath, outname, save=False, show=True): x = time y = flux LS = LombScargle(x, y, normalization='standard') # LS = LombScargle(x, y, normalization='psd') power = LS.power(freq) max_NormLSP = np.max(power) period_peak = 1. / freq[np.where(power == np.max(power))][0] FP = LS.false_alarm_probability(power.max(), minimum_frequency=freq[0], maximum_frequency=freq[-1], method='baluev') FP_99 = LS.false_alarm_level(0.0027, minimum_frequency=freq[0], maximum_frequency=freq[-1], method='baluev') FP_95 = LS.false_alarm_level(0.05, minimum_frequency=freq[0], maximum_frequency=freq[-1], method='baluev') FP_68 = LS.false_alarm_level(0.32, minimum_frequency=freq[0], maximum_frequency=freq[-1], method='baluev') # if FP<0.01:print(dataname) # plt.title('Epoch {2}: XID={0},FAP={1}'.format(dataname,np.round(FP,4),k),font1) plt.figure(1, (15, 6)) plt.title('Period={0:.2f}'.format(period_peak), font1) # plt.semilogx() # print(freq) plt.plot(freq, power) plt.semilogx() out_period = 1. / freq[np.where(power == np.max(power))][0] # plt.plot([1/period_peak,1/period_peak],[0,np.max(power)],'--') plt.plot([freq[0], freq[-1]], [FP_99, FP_99], '--') plt.plot([freq[0], freq[-1]], [FP_95, FP_95], '--') plt.plot([freq[0], freq[-1]], [FP_68, FP_68], '--') plt.text(freq[0], FP_99, '1-FAP 99.73%', font1) plt.text(freq[0], FP_95, '95%', font1) plt.text(freq[0], FP_68, '68%', font1) plt.xlabel('Frequency (Hz)', font1) plt.ylabel('Normalized LS Periodogram', font1) plt.tick_params(labelsize=16) # plt.show() if save: plt.savefig(outpath + outname + '.eps', bbox_inches='tight', pad_inches=0.0) if show: plt.show() else: plt.close() return [FP, out_period, max_NormLSP]
def get_LS(time, flux,freq,dataname,k,save=0,show=0): x = time y = flux # dy=np.sqrt(y) # plt.scatter(x,y) # plt.show() FP=1.0 LS = LombScargle(x, y,dy=None,normalization = 'standard') # print(len(freq)) # LS = LombScargle(x, y, normalization='psd') power = LS.power(freq) # print(power.max()) FP=LS.false_alarm_probability(power.max(),minimum_frequency = freq[0],maximum_frequency = freq[-1],method='naive') FP_99 = LS.false_alarm_level(0.0027,minimum_frequency = freq[0], maximum_frequency = freq[-1],method='naive') FP_95 = LS.false_alarm_level(0.05, minimum_frequency=freq[0], maximum_frequency=freq[-1], method='naive') FP_68 = LS.false_alarm_level(0.32,minimum_frequency=freq[0], maximum_frequency=freq[-1], method='naive') # if FP<0.01:print(dataname) plt.figure(1, (10, 8)) # plt.title('Epoch {2}: XID={0},FAP={1}'.format(dataname,np.round(FP,4),k),font1) plt.title('Epoch {1}: XID={0}'.format(dataname, k), font1) # plt.title('XID={0},FAP={1}'.format(dataname, np.round(FP, 4)), font1) # plt.semilogx() plt.plot(freq, power) # plt.plot([1/2492.73,1/2492.73],[0,np.max(power)],'--',linewidth=1) plt.semilogx() # plt.ylim(0,0.00012) print(1. / freq[np.where(power == np.max(power))]) # print(np.where(power == np.max(power))) # if FP<0.01:print(1./freq[np.where(power==np.max(power))]);print(np.where(power==np.max(power))) out_period=1./freq[np.where(power==np.max(power))] plt.plot([freq[0], freq[-1]], [FP_99, FP_99], '--') plt.plot([freq[0], freq[-1]], [FP_95, FP_95], '--') plt.plot([freq[0], freq[-1]], [FP_68, FP_68], '--') plt.text(freq[int(len(freq)*0.32)], FP_99, '1-FAP 99.73%',font1) plt.text(freq[int(len(freq)*0.7)], FP_95, '95%',font1) plt.text(freq[int(len(freq)*0.7)], FP_68, '68%',font1) plt.xlabel('Frequency (Hz)',font1) plt.ylabel('Normalized LS Periodogram',font1) plt.tick_params(labelsize=16) # plt.savefig(func.figurepath + '643_epoch4.pdf', bbox_inches='tight', pad_inches=0.0) if save: plt.savefig(func.figurepath+'{0}_epoch{1}.pdf'.format(dataname,k),bbox_inches='tight', pad_inches=0.0) if show: plt.show() else: plt.close() # plt.savefig('/Users/baotong/Desktop/CDFS/fig_LS_ep{0}_ovsamp_5_baluev/{1}_bin250.eps'.format(k,dataname)) # plt.savefig('/Users/baotong/Desktop/CDFS/fig_LS_ep{0}_samp_1_baluev/{1}_bin250.eps'.format(k,dataname)) # plt.close() return [FP,out_period]
def get_LS(time, flux, freq, dataname='default', outpath='/Users/baotong/Desktop/'): x = time y = flux # dy=np.sqrt(y) # plt.scatter(x,y) # plt.show() FP = 1.0 # LS = LombScargle(x, y, dy = 1, normalization = 'standard', fit_mean = True, # center_data = True).power(freq, method = 'cython') LS = LombScargle(x, y, normalization='standard') # LS = LombScargle(x, y, normalization='psd') power = LS.power(freq) FP = LS.false_alarm_probability(power.max(), minimum_frequency=freq[0], maximum_frequency=freq[-1], method='baluev') FP_99 = LS.false_alarm_level(0.01, minimum_frequency=freq[0], maximum_frequency=freq[-1], method='baluev') FP_95 = LS.false_alarm_level(0.05, minimum_frequency=freq[0], maximum_frequency=freq[-1], method='baluev') FP_68 = LS.false_alarm_level(0.32, minimum_frequency=freq[0], maximum_frequency=freq[-1], method='baluev') # # if FP<0.01:print(dataname) plt.title('{0},FP={1}'.format(dataname, FP)) # plt.semilogx() plt.plot(freq, power) plt.semilogx() # print(1. / freq[np.where(power == np.max(power))]) # print(np.where(power == np.max(power))) # if FP<0.01:print(1./freq[np.where(power==np.max(power))]);print(np.where(power==np.max(power))) out_period = 1. / freq[np.where(power == np.max(power))[0]] plt.plot([freq[0], freq[-1]], [FP_99, FP_99], '--') plt.plot([freq[0], freq[-1]], [FP_95, FP_95], '--') plt.plot([freq[0], freq[-1]], [FP_68, FP_68], '--') plt.text(freq[0], FP_99, '99%') plt.text(freq[0], FP_95, '95%') plt.text(freq[0], FP_68, '68%') plt.show() # plt.savefig(outpath+'{0}_15sec.eps'.format(dataname)) # plt.close() return [FP, out_period]
def frequency_alarm(df_d: pd.DataFrame) -> float: """Function that returns the false alarm probability for a given detector, for use in groupbys Args: df_d: dataframe for single detector Returns: false alarm probability as float""" X = df_d.reset_index() X = X.drop(columns=["detector_id", "measurement_end_utc"]) x = X.index.to_numpy() y = X.to_numpy().flatten() ls = LombScargle(x, y) period = np.linspace(15, 30, 300) pmax = ls.power(1 / period).max() return ls.false_alarm_probability(pmax)
def LS_periodogram(times, signal, dy=None, f0=None, fn=None, oversample_factor=10.0, normalisation='amplitude'): """ Calculates the amplitude spectrum of a given signal Parameters ---------- t : `array` Time values y : `array` Flux or magnitude measurements This is not automatically mean-substracted!!! f0 : float (default None) Minimum frequency to calculate spectrum. Defaults to df fn : float Maximum frequency to calculate spectrum. Defaults to Nyquist. oversample_factor : float Amount by which to oversample the spectrum. Defaults to 10. normalisation : str (default amplitude) Normalise the periodogram. Options are 'distribution', 'ampliude', 'power_density', 'power' """ df = 1.0 / (times.max() - times.min()) if f0 is None: f0 = df if fn is None: fn = 0.5 / np.median(np.diff(times)) # *nyq_mult freq = np.arange(f0, fn, df / oversample_factor) ls_ = LombScargle(times, signal, dy=dy) sc = ls_.power(freq, method="fast", normalization="psd") if dy is None: dy = np.array([1.]) #np.ones_like(signal) w = (dy**-2.0).sum() power = (1. * sc) / w ## Normalised to return desired output units noramlise_str = 'normalise_{}'.format(normalisation) LS_out = eval(noramlise_str + '(power,times)') return freq, LS_out
def calPower(code, df): tt = df.index.values dfD = df[df[code].notna().values] t = dfD.index.values x = (t.astype('datetime64[D]') - np.datetime64('1979-01-01')).astype( np.float) y = dfD[code].values nt = len(tt) freq = np.fft.fftfreq(nt)[1:] ind = np.where((1 / freq >= 0) & (1 / freq < 1000))[0] freq = freq[ind] ls = LombScargle(x, y) power = ls.power(freq) p = ls.false_alarm_probability(power) return freq, power, 1 - p
def get_LS(time, flux, freq): ## Lomb-Scagrle周期图算法,参考VanderPlas J. T., 2018, ApJS, 236, 16 ## x = time y = flux plt.figure(1, (9, 6)) # LS = LombScargle(x, y, dy = 1, normalization = 'standard', fit_mean = True, # center_data = True).power(freq, method = 'cython') LS = LombScargle(x, y, normalization='standard') power = LS.power(freq) FP = LS.false_alarm_probability(power.max(), minimum_frequency=freq[0], maximum_frequency=freq[-1], method='baluev') FP_99 = LS.false_alarm_level(0.0027, minimum_frequency=freq[0], maximum_frequency=freq[-1], method='baluev') FP_90 = LS.false_alarm_level(0.05, minimum_frequency=freq[0], maximum_frequency=freq[-1], method='baluev') FP_68 = LS.false_alarm_level(0.32, minimum_frequency=freq[0], maximum_frequency=freq[-1], method='baluev') plt.plot([freq[0], freq[-1]], [FP_99, FP_99], '--') plt.plot([freq[0], freq[-1]], [FP_90, FP_90], '--') plt.plot([freq[0], freq[-1]], [FP_68, FP_68], '--') plt.title('FP={0}'.format(FP), font1) plt.semilogx() plt.xlabel('frequency (Hz)') plt.ylabel('Normalized Lomb-Scargle power') plt.plot(freq, power) print(1. / freq[np.where(power == np.max(power))]) plt.xlabel('frequency', font1) plt.ylabel('normalized LSP', font1) plt.tick_params(labelsize=16) plt.show() res = 1e5 * power res = np.round(res, 2) return [ FP, 1. / freq[np.where(power == np.max(power))], np.max(power), res ]
def get_LS(time, flux, error, freq): x = time y = flux dy = error FP = 1.0 LS = LombScargle(x, y, normalization='standard') # LS = LombScargle(x, y, normalization='psd') power = LS.power(freq) FP = LS.false_alarm_probability(power.max(), minimum_frequency=freq[0], maximum_frequency=freq[-1], method='baluev') FP_99 = LS.false_alarm_level(0.0027, minimum_frequency=freq[0], maximum_frequency=freq[-1], method='baluev') FP_95 = LS.false_alarm_level(0.05, minimum_frequency=freq[0], maximum_frequency=freq[-1], method='baluev') FP_68 = LS.false_alarm_level(0.32, minimum_frequency=freq[0], maximum_frequency=freq[-1], method='baluev') # if FP<0.01:print(dataname) plt.figure(1, (9, 6)) plt.title('Lomb-Scargle Periodogram') plt.plot(freq, power) plt.semilogx() # print(1. / freq[np.where(power == np.max(power))]) # print(np.where(power == np.max(power))) out_period = 1. / freq[np.where(power == np.max(power))] plt.plot([freq[0], freq[-1]], [FP_99, FP_99], '--') plt.plot([freq[0], freq[-1]], [FP_95, FP_95], '--') plt.plot([freq[0], freq[-1]], [FP_68, FP_68], '--') plt.text(freq[0], FP_99, '99%') plt.text(freq[0], FP_95, '95%') plt.text(freq[0], FP_68, '68%') plt.xlabel('Frequency (1/day)', font1) plt.ylabel('Normalized LSP', font1) plt.tick_params(labelsize=16) plt.show() return [FP, out_period]
def get_LS(time, flux,freq): path='/Users/baotong/xmm/0201290301/txt/' x = time y = flux # dy=np.sqrt(y) # plt.scatter(x,y) # plt.show() # LS = LombScargle(x, y, dy = 1, normalization = 'standard', fit_mean = True, # center_data = True).power(freq, method = 'cython') LS = LombScargle(x, y,normalization = 'standard') # LS = LombScargle(x, y, dy, normalization='psd') power = LS.power(freq) # print('freq_num={0}'.format(len(freq))) FP=LS.false_alarm_probability(power.max(),minimum_frequency = freq[0], maximum_frequency = freq[-1],method='baluev') FP_99 = LS.false_alarm_level(0.0027, minimum_frequency = freq[0], maximum_frequency = freq[-1],method='baluev') FP_90 = LS.false_alarm_level(0.05, minimum_frequency=freq[0], maximum_frequency=freq[-1], method='baluev') FP_68 = LS.false_alarm_level(0.32, minimum_frequency=freq[0], maximum_frequency=freq[-1], method='baluev') plt.title('FP={0}'.format(FP)) plt.semilogx() # plt.xlim(1000.,1500.) plt.plot(1/freq, power) print(1./freq[np.where(power==np.max(power))]) # plt.plot([freq[0], freq[-1]], [FP_99, FP_99], '--') # plt.plot([freq[0], freq[-1]], [FP_90, FP_90], '--') # plt.plot([freq[0], freq[-1]], [FP_68, FP_68], '--') # plt.plot([1/(20.3*60),1/(20.3*60)],[0,0.08],'--',color='green') # plt.plot([1 / (22.5 * 60), 1 / (22.5 * 60)], [0, 0.08], '--', color='yellow') # plt.plot([1 / (18.6 * 60), 1 / (18.6 * 60)], [0, 0.08], '--', color='blue') plt.plot([20.3*60,20.3*60],[0,0.08],'--',color='green') plt.plot([22.5 * 60, 22.5 * 60], [0, 0.08], '--', color='yellow') plt.plot([18.6 * 60, 18.6 * 60], [0, 0.08], '--', color='blue') plt.show() res=1e5*power res=np.round(res,2) # res=res[::smooth] # np.savetxt(path+'LS_simres_{0}.txt'.format(trial+1),res,fmt='%10.5f') return [FP, 1. / freq[np.where(power == np.max(power))],np.max(power),res]
def get_LS(time, flux,freq,dataname,k): x = time y = flux # dy=np.sqrt(y) # plt.scatter(x,y) # plt.show() FP=1.0 LS = LombScargle(x, y,dy=None,normalization = 'standard') # print(len(freq)) # LS = LombScargle(x, y, normalization='psd') power = LS.power(freq) # print(power.max()) FP=LS.false_alarm_probability(power.max(),minimum_frequency = freq[0],maximum_frequency = freq[-1],method='baluev') FP_99 = LS.false_alarm_level(0.01,minimum_frequency = freq[0], maximum_frequency = freq[-1],method='baluev') FP_95 = LS.false_alarm_level(0.05, minimum_frequency=freq[0], maximum_frequency=freq[-1], method='baluev') FP_68 = LS.false_alarm_level(0.32,minimum_frequency=freq[0], maximum_frequency=freq[-1], method='baluev') # if FP<0.01:print(dataname) plt.title('Epoch {2}: XID={0},FAP={1}'.format(dataname,np.round(FP,4),k),font1) # plt.semilogx() plt.plot(freq, power) # plt.plot([1/2492.73,1/2492.73],[0,np.max(power)],'--',linewidth=1) plt.semilogx() print(1. / freq[np.where(power == np.max(power))]) # print(np.where(power == np.max(power))) # if FP<0.01:print(1./freq[np.where(power==np.max(power))]);print(np.where(power==np.max(power))) out_period=1./freq[np.where(power==np.max(power))] plt.plot([freq[0], freq[-1]], [FP_99, FP_99], '--') plt.plot([freq[0], freq[-1]], [FP_95, FP_95], '--') plt.plot([freq[0], freq[-1]], [FP_68, FP_68], '--') plt.text(freq[0], FP_99, 'FAP 99%',font1) plt.text(freq[0], FP_95, '95%',font1) plt.text(freq[0], FP_68, '68%',font1) plt.xlabel('Frequency (Hz)',font1) plt.ylabel('Normalized LS Periodogram',font1) plt.tick_params(labelsize=16) plt.show() # plt.savefig('/Users/baotong/Desktop/CDFS/fig_LS_ep{0}_ovsamp_5_baluev/{1}.eps'.format(k,dataname)) # plt.savefig('/Users/baotong/Desktop/CDFS/fig_LS_ep{0}_samp_1_baluev/{1}.eps'.format(k,dataname)) # plt.close() return [FP,out_period]
def get_LS(time, flux, freq): x = time y = flux LS = LombScargle(x, y, normalization='standard') # LS = LombScargle(x, y, normalization='psd') power = LS.power(freq) max_NormLSP = np.max(power) FP = LS.false_alarm_probability(power.max(), minimum_frequency=freq[0], maximum_frequency=freq[-1], method='baluev') FP_99 = LS.false_alarm_level(0.0027, minimum_frequency=freq[0], maximum_frequency=freq[-1], method='baluev') FP_95 = LS.false_alarm_level(0.05, minimum_frequency=freq[0], maximum_frequency=freq[-1], method='baluev') FP_68 = LS.false_alarm_level(0.32, minimum_frequency=freq[0], maximum_frequency=freq[-1], method='baluev') # if FP<0.01:print(dataname) # plt.title('Epoch {2}: XID={0},FAP={1}'.format(dataname,np.round(FP,4),k),font1) # plt.semilogx() plt.plot(freq, power) plt.semilogx() out_period = 1. / freq[np.where(power == np.max(power))][0] plt.plot([freq[0], freq[-1]], [FP_99, FP_99], '--') plt.plot([freq[0], freq[-1]], [FP_95, FP_95], '--') plt.plot([freq[0], freq[-1]], [FP_68, FP_68], '--') plt.text(freq[0], FP_99, '1-FAP 99%', font1) plt.text(freq[0], FP_95, '95%', font1) plt.text(freq[0], FP_68, '68%', font1) plt.xlabel('Frequency (Hz)', font1) plt.ylabel('Normalized LS Periodogram', font1) plt.tick_params(labelsize=16) # plt.show() # plt.savefig('/Users/baotong/Desktop/CDFS/fig_LS_ep{0}_ovsamp_5_baluev/{1}.eps'.format(k,dataname)) plt.close() return [FP, out_period, max_NormLSP]
def get_LS(time, flux, freq): x = time y = flux # LS = LombScargle(x, y, dy = 1, normalization = 'standard', fit_mean = True, # center_data = True).power(freq, method = 'cython') LS = LombScargle(x, y, normalization='standard') FP = 0 power = LS.power(freq) FP = LS.false_alarm_probability(power.max(), minimum_frequency=freq[0], maximum_frequency=freq[-1], method='baluev') FP_99 = LS.false_alarm_level(0.0027, minimum_frequency=freq[0], maximum_frequency=freq[-1], method='baluev') FP_90 = LS.false_alarm_level(0.05, minimum_frequency=freq[0], maximum_frequency=freq[-1], method='baluev') FP_68 = LS.false_alarm_level(0.32, minimum_frequency=freq[0], maximum_frequency=freq[-1], method='baluev') plt.figure(1, (9, 6)) plt.plot([freq[0], freq[-1]], [FP_99, FP_99], '--') plt.plot([freq[0], freq[-1]], [FP_90, FP_90], '--') plt.plot([freq[0], freq[-1]], [FP_68, FP_68], '--') # plt.plot([1 /950.7, 1 / 950.7], [0, np.max(power)], '--', linewidth=1) plt.title('FP={0}'.format(FP)) plt.semilogx() # plt.xlim(1000.,1500.) plt.plot(freq, power) print(1. / freq[np.where(power == np.max(power))]) plt.show() res = 1e5 * power res = np.round(res, 2) return [ FP, 1. / freq[np.where(power == np.max(power))], np.max(power), res ]
def get_LS(time, flux, freq, dataname, k): x = time y = flux # dy=np.sqrt(y) # plt.scatter(x,y) # plt.show() # LS = LombScargle(x, y, dy = 1, normalization = 'standard', fit_mean = True, # center_data = True).power(freq, method = 'cython') LS = LombScargle(x, y, normalization='psd') # LS = LombScargle(x, y, dy, normalization='psd') power = LS.power(freq) FP = 1 # FP = LS.false_alarm_probability(power.max(), minimum_frequency=freq[0], maximum_frequency=freq[-1], # method='baluev') # FP_99 = LS.false_alarm_level(0.0027, minimum_frequency=freq[0], maximum_frequency=freq[-1], method='baluev') # FP_90 = LS.false_alarm_level(0.05, minimum_frequency=freq[0], # maximum_frequency=freq[-1], method='baluev') # FP_68 = LS.false_alarm_level(0.32, minimum_frequency=freq[0], # maximum_frequency=freq[-1], method='baluev') # if FP < 0.01: print(dataname) # plt.title('{0}, FP={1}%'.format(dataname, np.round(100*FP,3))) # plt.semilogx() plt.title(' XID={0}'.format(dataname), font1) plt.plot(freq, power) # plt.loglog() plt.xlabel('Frequency (Hz)', font1) plt.ylabel('LS Periodogram', font1) plt.tick_params(labelsize=16) plt.semilogx() # print(1. / freq[np.where(power == np.max(power))]) # if FP < 0.01: print(1. / freq[np.where(power == np.max(power))]) print(np.max(power)) out_period = 1. / freq[np.where(power == np.max(power))] # plt.plot([freq[0], freq[-1]], [FP_99, FP_99], '--') # plt.plot([freq[0], freq[-1]], [FP_90, FP_90], '--') # plt.plot([freq[0], freq[-1]], [FP_68, FP_68], '--') # plt.savefig('/Users/baotong/Desktop/CDFS/fig_LS_ep{0}_ovsamp_5_baluev/{1}.eps'.format(k,dataname)) # plt.close() return [FP, out_period]
def get_LS(time, flux, freq): x = time y = flux LS = LombScargle(x, y, normalization='standard') # LS = LombScargle(x, y, dy, normalization='psd') power = LS.power(freq) FP = LS.false_alarm_probability(power.max(), minimum_frequency=freq[0], maximum_frequency=freq[-1], method='baluev') FP_99 = LS.false_alarm_level(0.0027, minimum_frequency=freq[0], maximum_frequency=freq[-1], method='baluev') FP_90 = LS.false_alarm_level(0.05, minimum_frequency=freq[0], maximum_frequency=freq[-1], method='baluev') FP_68 = LS.false_alarm_level(0.32, minimum_frequency=freq[0], maximum_frequency=freq[-1], method='baluev') # if FP<0.01:print(dataname) # plt.title('{0},FP={1}'.format(dataname,FP)) # plt.semilogx() # plt.title('XID 19') plt.plot(freq, power) plt.semilogx() print(1. / freq[np.where(power == np.max(power))]) print(np.where(power == np.max(power))) # if FP<0.01:print(1./freq[np.where(power==np.max(power))]);print(np.where(power==np.max(power))) out_period = 1. / freq[np.where(power == np.max(power))] plt.plot([freq[0], freq[-1]], [FP_99, FP_99], '--') plt.plot([freq[0], freq[-1]], [FP_90, FP_90], '--') plt.plot([freq[0], freq[-1]], [FP_68, FP_68], '--') plt.show() # plt.savefig('/Users/baotong/Desktop/CDFS/fig_LS_ep{0}_ovsamp_5_baluev/{1}.eps'.format(k,dataname)) # plt.close() return [FP, out_period]
def get_LS(time, flux,freq,trial=1): x = time y = flux # dy=np.sqrt(y) # plt.scatter(x,y) # plt.show() # LS = LombScargle(x, y, dy = 1, normalization = 'standard', fit_mean = True, # center_data = True).power(freq, method = 'cython') LS = LombScargle(x, y,normalization = 'psd') # LS = LombScargle(x, y, dy, normalization='psd') power = LS.power(freq) # print('freq_num={0}'.format(len(freq))) # FP=LS.false_alarm_probability(power.max(),minimum_frequency = freq[0], maximum_frequency = freq[-1],method='baluev') # FP_99 = LS.false_alarm_level(0.0027, minimum_frequency = freq[0], maximum_frequency = freq[-1],method='baluev') # FP_90 = LS.false_alarm_level(0.05, minimum_frequency=freq[0], # maximum_frequency=freq[-1], method='baluev') # FP_68 = LS.false_alarm_level(0.32, minimum_frequency=freq[0], # maximum_frequency=freq[-1], method='baluev') # if FP<0.01:print(dataname) # plt.title('FP={0}'.format(FP)) plt.semilogx() plt.plot(freq, power) # print(1./freq[np.where(power==np.max(power))]) # plt.plot([freq[0], freq[-1]], [FP_99, FP_99], '--' # plt.plot([freq[0], freq[-1]], [FP_90, FP_90], '--') # plt.plot([freq[0], freq[-1]], [FP_68, FP_68], '--') # plt.show() res=1e2*power res=np.round(res,2) # res=res[::smooth] # np.savetxt(path+'LS_simres_{0}.txt'.format(trial+1),res,fmt='%10.5f') period=1. / freq[np.where(power == np.max(power))][0];LSP=np.max(power) period=np.round(period,2);LSP=np.round(1e2*LSP,2) if LSP<18.36: plt.close() return [period,LSP]
def fundamental_spacing_minimum(self): """Estimate fundamental spacing using the first minimum spectral window function.""" # Create "window" time series: freq_cen = 0.5 * self.nyquist x = 0.5 * np.sin(2 * np.pi * freq_cen * self.ls.t) + 0.5 * np.cos( 2 * np.pi * freq_cen * self.ls.t) # Calculate power spectrum for the given frequency range: ls = LombScargle(self.ls.t, x, center_data=True, fit_mean=self.fit_mean) # Minimize the window function around the first minimum: # Normalization is completely irrelevant window = lambda freq: ls.power( freq_cen + freq, normalization='psd', method='fast') res = minimize_scalar(window, [0.75 * self.df, self.df, 1.25 * self.df]) df = res.x return df
def get_LS(time, flux, freq): x = time y = flux LS = LombScargle(x, y, normalization='standard') # LS = LombScargle(x, y, dy, normalization='psd') power = LS.power(freq) FP = LS.false_alarm_probability(power.max(), samples_per_peak=5, nyquist_factor=5, minimum_frequency=freq[0], maximum_frequency=freq[-1], method='baluev') FP_99 = LS.false_alarm_level(0.01, samples_per_peak=10, nyquist_factor=5, minimum_frequency=freq[0], maximum_frequency=freq[-1], method='naive') FP_90 = LS.false_alarm_level(0.1, samples_per_peak=10, nyquist_factor=5, minimum_frequency=freq[0], maximum_frequency=freq[-1], method='naive') FP_68 = LS.false_alarm_level(0.32, samples_per_peak=10, nyquist_factor=5, minimum_frequency=freq[0], maximum_frequency=freq[-1], method='naive') plt.plot(freq, power) print(1. / freq[np.where(power == np.max(power))]) plt.plot([freq[0], freq[-1]], [FP_99, FP_99], '--') plt.plot([freq[0], freq[-1]], [FP_90, FP_90], '--') plt.plot([freq[0], freq[-1]], [FP_68, FP_68], '--') plt.show()
def get_LS(time, flux, freq, dataname): x = time y = flux # dy=np.sqrt(y) # plt.scatter(x,y) # plt.show() # LS = LombScargle(x, y, dy = 1, normalization = 'standard', fit_mean = True, # center_data = True).power(freq, method = 'cython') LS = LombScargle(x, y, normalization='standard') # LS = LombScargle(x, y, normalization='psd') power = LS.power(freq) power_freq = np.max(power[np.where((1 / freq - 2493.76) < 20)]) FP = LS.false_alarm_probability(power_freq, minimum_frequency=freq[0], maximum_frequency=freq[-1], method='baluev') # FP_99 = LS.false_alarm_level(0.0027,minimum_frequency = freq[0], maximum_frequency = freq[-1],method='baluev') # FP_90 = LS.false_alarm_level(0.05, minimum_frequency=freq[0], # maximum_frequency=freq[-1], method='baluev') # FP_68 = LS.false_alarm_level(0.32,minimum_frequency=freq[0], # maximum_frequency=freq[-1], method='baluev') # # if FP<0.01:print(dataname) plt.title('{0},FP={1}'.format(dataname, FP)) # plt.semilogx() plt.plot(freq, power) # plt.plot([1/2492,1/2492.],[0,np.max(power)],'--',linewidth=1) plt.semilogx() print(1. / freq[np.where(power == np.max(power))]) # if FP<0.01:print(1./freq[np.where(power==np.max(power))]);print(np.where(power==np.max(power))) out_period = 1. / freq[np.where(power == np.max(power))] # if np.abs(out_period-950.7317)>30:FP=1 # plt.savefig('/Users/baotong/Desktop/CDFS/fig_LS_ep{0}_ovsamp_5_baluev/{1}.eps'.format(k,dataname)) plt.close() return [FP, out_period]
def get_LS(time, flux,freq,trial=1): path='/Users/baotong/Desktop/CDFS/txt_all_obs_0.5_8_ep3/simulation/19_LS_sim_noQPO/' x = time y = flux # dy=np.sqrt(y) # plt.scatter(x,y) # plt.show() # LS = LombScargle(x, y, dy = 1, normalization = 'standard', fit_mean = True, # center_data = True).power(freq, method = 'cython') LS = LombScargle(x, y,normalization = 'standard') # LS = LombScargle(x, y, dy, normalization='psd') power = LS.power(freq) # print('freq_num={0}'.format(len(freq))) FP=LS.false_alarm_probability(power.max(),minimum_frequency = freq[0], maximum_frequency = freq[-1],method='baluev') # FP_99 = LS.false_alarm_level(0.0027, minimum_frequency = freq[0], maximum_frequency = freq[-1],method='baluev') # FP_90 = LS.false_alarm_level(0.05, minimum_frequency=freq[0], # maximum_frequency=freq[-1], method='baluev') # FP_68 = LS.false_alarm_level(0.32, minimum_frequency=freq[0], # maximum_frequency=freq[-1], method='baluev') # if FP<0.01:print(dataname) # plt.title('FP={0}'.format(FP)) # plt.semilogx() # plt.plot(freq, power) # print(1./freq[np.where(power==np.max(power))]) # plt.plot([freq[0], freq[-1]], [FP_99, FP_99], '--' # plt.plot([freq[0], freq[-1]], [FP_90, FP_90], '--') # plt.plot([freq[0], freq[-1]], [FP_68, FP_68], '--') # plt.show() res=1e2*power res=np.round(res,2) # res=res[::smooth] # np.savetxt(path+'LS_simres_{0}.txt'.format(trial+1),res,fmt='%10.5f') return [FP,1. / freq[np.where(power == np.max(power))],np.max(power),res]
def lomb_scargle(t, y, dy, omega, generalized=True, subtract_mean=True, significance=None): """ (Generalized) Lomb-Scargle Periodogram with Floating Mean Parameters ---------- t : array_like sequence of times y : array_like sequence of observations dy : array_like sequence of observational errors omega : array_like frequencies at which to evaluate p(omega) generalized : bool if True (default) use generalized lomb-scargle method otherwise, use classic lomb-scargle. subtract_mean : bool if True (default) subtract the sample mean from the data before computing the periodogram. Only referenced if generalized is False significance : None or float or ndarray if specified, then this is a list of significances to compute for the results. Returns ------- p : array_like Lomb-Scargle power associated with each frequency omega z : array_like if significance is specified, this gives the levels corresponding to the desired significance (using the Scargle 1982 formalism) Notes ----- The algorithm is based on reference [1]_. The result for generalized=False is given by equation 4 of this work, while the result for generalized=True is given by equation 20. Note that the normalization used in this reference is different from that used in other places in the literature (e.g. [2]_). For a discussion of normalization and false-alarm probability, see [1]_. To recover the normalization used in Scargle [3]_, the results should be multiplied by (N - 1) / 2 where N is the number of data points. References ---------- .. [1] M. Zechmeister and M. Kurster, A&A 496, 577-584 (2009) .. [2] W. Press et al, Numerical Recipes in C (2002) .. [3] Scargle, J.D. 1982, ApJ 263:835-853 """ # delegate to astropy's Lomb-Scargle ls = LombScargle(t, y, dy, fit_mean=generalized, center_data=subtract_mean) frequency = np.asarray(omega) / (2 * np.pi) p_omega = ls.power(frequency, method='cython') if significance is not None: N = t.size M = 2 * N z = (-2.0 / (N - 1.) * np.log(1 - (1 - np.asarray(significance)) ** (1. / M))) return p_omega, z else: return p_omega