def acorr(trace): if len(trace) > 50: pl.acorr(trace, normed=True, detrend=pl.mlab.detrend_mean, maxlags=50) pl.xticks([]) pl.yticks([]) l,r,b,t = pl.axis() pl.axis([-10, r, -.1, 1.1])
def autocorrelation(x, y): # normalise so range is 2 - no idea if this is the right thing to do... y = 2*y/(max(y)-min(y)) y = y-np.median(y) # calculate autocorrelation fn lags, acf, lines, axis = pb.acorr(y, maxlags = len(y)/2.) # halve acf and find peaks acf = acf[len(acf)/2.:] pks = find_peaks_cwt(acf, np.arange(10, 20)) # play with these params, #they define peak widths peaks = pks[1:] # lose the first peak period = peaks[0]*cadence print 'acf period = ', period pl.clf() pl.subplot(2,1,1) pl.plot(x[:4000], y[:4000], 'k.') pl.title('Period = %s' %period) pl.subplot(2,1,2) pl.plot(np.arange(5000)*cadence, acf[:5000]) # pl.plot(np.arange(len(acf))*cadence, acf) [pl.axvline(peak*cadence, linestyle = '--', color = 'r') for peak in peaks] pl.xlim(0, 5000*cadence) pl.savefig('/Users/angusr/Python/george/acf/%sacf' %int(KID)) # np.savetxt('/Users/angusr/Python/george/acf/%sacf_per.txt'%int(KID), period) return period
def AcfPeriodogram(x, dt = 1, norm = False, doplot = False, \ smooth = False, box = 0.01): ''' Compute periodogram (power spectrum of ACF) of a 1-D vector x, assumed to be sampled regularly with sampling interval dt, and the corresponding frequency array in physical units (postive frequencies only). The ACF is computed up to lags of N/4 where N is the length of the input array. If smooth is True, the ACF is multiplied by sinc(pi/box) before taking the power spectrum. This is equivalent to smoothing the power specturm by convolving it a top-hat function of width (box/dt) in the frequency domain. ''' maxl = min(len(x), max(len(x)/4, 50)) f = plt.figure() lag, corr, line, ax = plt.acorr(x, maxlags = maxl, normed = True) plt.close(f) if smooth == True: corr *= np.sinc(np.pi/box) pgram, freq = DftPowerSpectrum(corr, dt, doplot = False) if doplot == True: plt.figure(figsize = (6,7.5)) plt.subplot(311) plt.title('ACF periodogram') t = np.arange(len(x))*dt plt.plot(t, x, 'k-') plt.xlabel('time') plt.ylabel('data') plt.xlim(0,len(x)*dt) plt.subplot(312) l = lag >= 0 plt.plot(lag[l]*dt, corr[l], 'k-') plt.ylabel('ACF') plt.xlabel('lag (time units)') plt.xlim(0,lag.max()*dt) plt.subplot(313) plt.plot(freq, pgram, 'k-') plt.xlabel('frequency') if smooth == True: plt.ylabel('amplitude (smoothed)') else: plt.ylabel('amplitude') plt.xlim(freq.min(),freq.max()) return (corr, lags*dt), (pgram, freq)
def AcfPeriodogram(x, dt = 1, norm = False, doplot = False, \ smooth = False, box = 0.01): ''' Compute periodogram (power spectrum of ACF) of a 1-D vector x, assumed to be sampled regularly with sampling interval dt, and the corresponding frequency array in physical units (postive frequencies only). The ACF is computed up to lags of N/4 where N is the length of the input array. If smooth is True, the ACF is multiplied by sinc(pi/box) before taking the power spectrum. This is equivalent to smoothing the power specturm by convolving it a top-hat function of width (box/dt) in the frequency domain. ''' maxl = min(len(x), max(len(x) / 4, 50)) f = plt.figure() lag, corr, line, ax = plt.acorr(x, maxlags=maxl, normed=True) plt.close(f) if smooth == True: corr *= np.sinc(np.pi / box) pgram, freq = DftPowerSpectrum(corr, dt, doplot=False) if doplot == True: plt.figure(figsize=(6, 7.5)) plt.subplot(311) plt.title('ACF periodogram') t = np.arange(len(x)) * dt plt.plot(t, x, 'k-') plt.xlabel('time') plt.ylabel('data') plt.xlim(0, len(x) * dt) plt.subplot(312) l = lag >= 0 plt.plot(lag[l] * dt, corr[l], 'k-') plt.ylabel('ACF') plt.xlabel('lag (time units)') plt.xlim(0, lag.max() * dt) plt.subplot(313) plt.plot(freq, pgram, 'k-') plt.xlabel('frequency') if smooth == True: plt.ylabel('amplitude (smoothed)') else: plt.ylabel('amplitude') plt.xlim(freq.min(), freq.max()) return (corr, lag * dt), (pgram, freq)
def visualize_single_step(mod, i, alpha=0., description_str=''): """ Show how a random walk in a two dimensional space has progressed up to step i""" X = mod.X.trace() pl.clf() sq_size = .3 # show 2d trace pl.axes([.05, .05, sq_size, sq_size]) pl.plot(X[:i, 0], X[:i, 1], 'b.-', alpha=.1) Y = alpha * X[i, :] + (1 - alpha) * X[i - 1, :] pl.plot([Y[0], Y[0]], [Y[1], 2.], 'k-', alpha=.5) pl.plot([Y[0], 2], [Y[1], Y[1]], 'k-', alpha=.5) pl.plot(Y[0], Y[1], 'go') if hasattr(mod, 'shape'): pl.fill(mod.shape[:, 0], mod.shape[:, 1], color='b', alpha=.2) if hasattr(mod, 'plot_distribution'): mod.plot_distribution() pl.axis([-1.1, 1.1, -1.1, 1.1]) pl.xticks([]) pl.yticks([]) # show 1d marginals ## X[0] is horizontal position pl.axes([.05, .05 + sq_size, sq_size, 1. - .1 - sq_size]) pl.plot(X[:(i + 1), 0], i + 1 - pl.arange(i + 1), 'k-') pl.axis([-1.1, 1.1, 0, 1000]) pl.xticks([]) pl.yticks([]) pl.text(-1, .1, '$X_0$') ## X[1] is vertical position pl.axes([.05 + sq_size, .05, 1. - .1 - sq_size, sq_size]) pl.plot(i + 1 - pl.arange(i + 1), X[:(i + 1), 1], 'k-') pl.axis([0, 1000, -1.1, 1.1]) pl.xticks([]) pl.yticks([]) pl.text(10, -1., '$X_1$') ## show X[i, j] acorr N, D = X.shape if i > 250: for j in range(D): pl.axes([ 1 - .1 - 1.5 * sq_size * (1 - j * D**-1.), 1. - .1 - 1.5 * sq_size * D**-1, 1.5 * sq_size * D**-1., 1.5 * sq_size * D**-1. ]) pl.acorr(X[(i / 2.):i:10, j], detrend=pl.mlab.detrend_mean) pl.xlabel('$X_%d$' % j) if j == 0: pl.ylabel('autocorr') pl.xticks([]) pl.yticks([]) pl.axis([-10, 10, -.1, 1]) ## show X[1] acorr ## textual information str = '' str += 't = %d\n' % i str += 'acceptance rate = %.2f\n\n' % ( 1. - pl.mean(pl.diff(X[(i / 2.):i, 0]) == 0.)) str += 'mean(X) = %s' % pretty_array(X[(i / 2.):i, :].mean(0)) if hasattr(mod, 'true_mean'): str += ' / true mean = %s\n' % pretty_array(mod.true_mean) else: str += '\n' if i > 10: iqr = pl.sort(X[(i / 2.):i, :], axis=0)[[.25 * (i / 2.), .75 * (i / 2.)], :].T for j in range(D): str += 'IQR(X[%d]) = (%.2f, %.2f)' % (j, iqr[j, 0], iqr[j, 1]) if hasattr(mod, 'true_iqr'): str += ' / true IQR = %s\n' % mod.true_iqr[j] else: str += '\n' pl.figtext(.05 + .01 + sq_size, .05 + .01 + sq_size, str, va='bottom', ha='left') pl.figtext(sq_size + .5 * (1. - sq_size), .96, description_str, va='top', ha='center', size=32) pl.figtext(.95, .05, 'healthyalgorithms.wordpress.com', ha='right')
def autocorr(x): x = np.squeeze(x) import pylab return pylab.acorr( x - np.mean(x))
def visualize_single_step(mod, i, alpha=0.0, description_str=""): """ Show how a random walk in a two dimensional space has progressed up to step i""" X = mod.X.trace() pl.clf() sq_size = 0.3 # show 2d trace pl.axes([0.05, 0.05, sq_size, sq_size]) pl.plot(X[:i, 0], X[:i, 1], "b.-", alpha=0.1) Y = alpha * X[i, :] + (1 - alpha) * X[i - 1, :] pl.plot([Y[0], Y[0]], [Y[1], 2.0], "k-", alpha=0.5) pl.plot([Y[0], 2], [Y[1], Y[1]], "k-", alpha=0.5) pl.plot(Y[0], Y[1], "go") if hasattr(mod, "shape"): pl.fill(mod.shape[:, 0], mod.shape[:, 1], color="b", alpha=0.2) if hasattr(mod, "plot_distribution"): mod.plot_distribution() pl.axis([-1.1, 1.1, -1.1, 1.1]) pl.xticks([]) pl.yticks([]) # show 1d marginals ## X[0] is horizontal position pl.axes([0.05, 0.05 + sq_size, sq_size, 1.0 - 0.1 - sq_size]) pl.plot(X[: (i + 1), 0], i + 1 - pl.arange(i + 1), "k-") pl.axis([-1.1, 1.1, 0, 1000]) pl.xticks([]) pl.yticks([]) pl.text(-1, 0.1, "$X_0$") ## X[1] is vertical position pl.axes([0.05 + sq_size, 0.05, 1.0 - 0.1 - sq_size, sq_size]) pl.plot(i + 1 - pl.arange(i + 1), X[: (i + 1), 1], "k-") pl.axis([0, 1000, -1.1, 1.1]) pl.xticks([]) pl.yticks([]) pl.text(10, -1.0, "$X_1$") ## show X[i, j] acorr N, D = X.shape if i > 250: for j in range(D): pl.axes( [ 1 - 0.1 - 1.5 * sq_size * (1 - j * D ** -1.0), 1.0 - 0.1 - 1.5 * sq_size * D ** -1, 1.5 * sq_size * D ** -1.0, 1.5 * sq_size * D ** -1.0, ] ) pl.acorr(X[(i / 2.0) : i : 10, j], detrend=pl.mlab.detrend_mean) pl.xlabel("$X_%d$" % j) if j == 0: pl.ylabel("autocorr") pl.xticks([]) pl.yticks([]) pl.axis([-10, 10, -0.1, 1]) ## show X[1] acorr ## textual information str = "" str += "t = %d\n" % i str += "acceptance rate = %.2f\n\n" % (1.0 - pl.mean(pl.diff(X[(i / 2.0) : i, 0]) == 0.0)) str += "mean(X) = %s" % pretty_array(X[(i / 2.0) : i, :].mean(0)) if hasattr(mod, "true_mean"): str += " / true mean = %s\n" % pretty_array(mod.true_mean) else: str += "\n" if i > 10: iqr = pl.sort(X[(i / 2.0) : i, :], axis=0)[[0.25 * (i / 2.0), 0.75 * (i / 2.0)], :].T for j in range(D): str += "IQR(X[%d]) = (%.2f, %.2f)" % (j, iqr[j, 0], iqr[j, 1]) if hasattr(mod, "true_iqr"): str += " / true IQR = %s\n" % mod.true_iqr[j] else: str += "\n" pl.figtext(0.05 + 0.01 + sq_size, 0.05 + 0.01 + sq_size, str, va="bottom", ha="left") pl.figtext(sq_size + 0.5 * (1.0 - sq_size), 0.96, description_str, va="top", ha="center", size=32) pl.figtext(0.95, 0.05, "healthyalgorithms.wordpress.com", ha="right")
def PlotParameters(process, acorr = False): """docstring for PlotRMSE""" mapping = { 'IGOU': ['Inverse Gaussian-OU', ['\\gamma', 'a', 'b', '\\lambda_0'] ], 'GOU' : [ 'Gamma-OU', ['\\gamma', 'a', 'b', '\\lambda_0'] ], 'CIR' : ['CIR', ['\\kappa', '\\nu', '\\gamma', '\\lambda_0'] ], } dates, dynamic_parameters = GetParameters(process, True) dates, static_parameters = GetParameters(process, False) # print dates # print dynamic_parameters parameter_names = mapping[process][1] process_name = mapping[process][0] pylab.clf() pylab.figure(1) for i, param in enumerate(parameter_names): dynamic_values = dynamic_parameters[i] static_values = static_parameters[i] pylab.subplot(2,2,i) lag = 5 usevlines = False from matplotlib.patches import Rectangle if acorr: if AUTOCOLOR: dyn = pylab.acorr(dynamic_values, label = "Dynamic", color = AUTOCOLOR_COLORS[0], maxlags = lag, usevlines = usevlines) stat = pylab.acorr(static_values, label = "Static", color = AUTOCOLOR_COLORS[1], maxlags = lag, usevlines = usevlines) p = Rectangle((0, 0), 1, 1, fc=AUTOCOLOR_COLORS[0]) q = Rectangle((0, 0), 1, 1, fc=AUTOCOLOR_COLORS[1]) pylab.ylim([0,1]) pylab.legend((p, q), ("Dynamic", "Static")) else: dyn = pylab.acorr(dates, dynamic_values, label = "Dynamic", color = AUTOCOLOR_COLORS[0]) stat = pylab.acorr(dates, static_values, label = "Static", color = AUTOCOLOR_COLORS[1]) else: if AUTOCOLOR: dyn = pylab.plot(dynamic_values, label = "Dynamic", color = AUTOCOLOR_COLORS[0]) stat = pylab.plot(static_values, label = "Static", color = AUTOCOLOR_COLORS[1]) pylab.legend() else: dyn = pylab.plot(dates, dynamic_values, label = "Dynamic", color = AUTOCOLOR_COLORS[0]) stat = pylab.plot(dates, static_values, label = "Static", color = AUTOCOLOR_COLORS[1]) pylab.legend() # pylab.title('Stability of $' + param + '$') # pylab .xlabel('Year') pylab.ylabel('$' + param + '$') # loc = mpl.dates.MonthLocator(1) # dateFmt = mpl.dates.DateFormatter('%b %y') # pylab.gca().xaxis.set_major_formatter(dateFmt) # # # pylab.gca().xaxis.set_major_locator(loc) # # pylab.subplots_adjust(bottom=0.15) pylab.subplots_adjust(wspace=0.4) pylab.suptitle(process_name, fontsize = 10) pdf_file = "../../Diagrams/ParameterStability/" + process + "Parameters.pdf" if acorr: pdf_file = "../../Diagrams/ParameterStability/" + process + "ParametersAutoCorr.pdf" pylab.savefig(pdf_file)
def acf_calc(quarter_acf, time, flux, interval, kid, max_psearch_len): ''' Calculate ACF, calls error calc function''' # ACF calculation in pylab, close fig when finished pylab.figure(50) pylab.clf() lags, acf, lines, axis = pylab.acorr(flux, maxlags = max_psearch_len) pylab.close(50) #convolve smoothing window with Gaussian kernel gauss_func = lambda x,sig: 1./numpy.sqrt(2*numpy.pi*sig**2) * numpy.exp(-0.5*(x**2)/(sig**2)) #define a Gaussian conv_func = gauss_func(numpy.arange(-28,28,1.),9.) #create the smoothing kernel acf_smooth = numpy.convolve(acf,conv_func,mode='same') #and convolve lenlag = len(lags) lags = lags[int(lenlag/2.0):lenlag][:-1] * interval acf = acf[int(lenlag/2.0): lenlag][0:-1] acf_smooth = acf_smooth[int(lenlag/2.0): lenlag][1:] print 'plotting raw acf' pylab.clf() pylab.plot(acf) #pylab.xlim(numpy.median(acf), 1000) #pylab.xlim(8900,9200) #pylab.ylim(0.95, 1.0) pylab.title('Raw acf') #raw_input('enter') # find max using usmoothed acf (for plot only) max_ind_us, max_val_us = extrema(acf, max = True, min = False) # find max/min using smoothed acf max_ind_s, max_val_s = extrema(acf_smooth, max = True, min = False) min_ind_s, min_val_s = extrema(acf_smooth, max = False, min = True) maxmin_ind_s, maxmin_val_s = extrema(acf_smooth, max = True, min = True) if len(max_ind_s) > 0 and len(min_ind_s) > 0: # ensure no duplicate peaks are detected t_max_s = atpy.Table() t_max_s.add_column('ind', max_ind_s) t_max_s.add_column('val', max_val_s) t_min_s = atpy.Table() t_min_s.add_column('ind', min_ind_s) t_min_s.add_column('val', min_val_s) t_maxmin_s = atpy.Table() t_maxmin_s.add_column('ind', maxmin_ind_s) t_maxmin_s.add_column('val', maxmin_val_s) ma_i = collections.Counter(t_max_s.ind) dup_arr = [i for i in ma_i if ma_i[i]>1] if len(dup_arr) > 0: for j in scipy.arange(len(dup_arr)): tin = t_max_s.where(t_max_s.ind != dup_arr[j]) tout = t_max_s.where(t_max_s.ind == dup_arr[j]) tout = tout.rows([0]) tin.append(tout) t_max_s = copy.deepcopy(tin) ma_i = collections.Counter(t_min_s.ind) dup_arr = [i for i in ma_i if ma_i[i]>1] if len(dup_arr) > 0: for j in scipy.arange(len(dup_arr)): tin = t_min_s.where(t_min_s.ind != dup_arr[j]) tout = t_min_s.where(t_min_s.ind == dup_arr[j]) tout = tout.rows([0]) tin.append(tout) t_min_s = copy.deepcopy(tin) ma_i = collections.Counter(t_maxmin_s.ind) dup_arr = [i for i in ma_i if ma_i[i]>1] if len(dup_arr) > 0: for j in scipy.arange(len(dup_arr)): tin = t_maxmin_s.where(t_maxmin_s.ind != dup_arr[j]) tout = t_maxmin_s.where(t_maxmin_s.ind == dup_arr[j]) tout = tout.rows([0]) tin.append(tout) t_maxmin_s = copy.deepcopy(tin) t_max_s.sort('ind') t_min_s.sort('ind') t_maxmin_s.sort('ind') # relate max inds to lags maxnum = len(t_max_s.ind) acf_per_pos = lags[t_max_s.ind] acf_per_height = acf[t_max_s.ind] print 'Calculating errors and asymmetries...' # Calculate peak widths, asymmetries etc acf_per_err, locheight, asym= \ calc_err(quarter_acf, kid = kid, lags = lags, acf = acf, inds = t_maxmin_s.ind, vals = t_maxmin_s.val, maxnum = maxnum) else: acf_per_pos = scipy.array([-9999]) acf_per_height = scipy.array([-9999]) acf_per_err = scipy.array([-9999]) locheight = scipy.array([-9999]) asym = scipy.array([-9999]) # save corrected LC and ACF t_lc = atpy.Table() t_lc.add_column('time', time) t_lc.add_column('flux', flux) t_lc.write('%s/PDCQ%s_output/dat_files/%s_LC.ipac' % (dir, quarter_acf, kid), overwrite = True) t_acf = atpy.Table() t_acf.add_column('lags_days', lags) t_acf.add_column('acf', acf) t_acf.add_column('acf_smooth', acf_smooth) t_acf.write('%s/PDCQ%s_output/dat_files/%s_ACF.ipac' % (dir, quarter_acf, kid), overwrite = True) pylab.figure(6,(10, 5.5)) pylab.clf() pylab.plot(t_acf.lags_days, t_acf.acf, 'k-') pylab.plot(t_acf.lags_days, t_acf.acf_smooth, 'r-') for j in scipy.arange(len(max_ind_us)): pylab.axvline(t_acf.lags_days[max_ind_us[j]], ls = '--', c = 'k', lw=1) for i in scipy.arange(len(acf_per_pos)): pylab.axvline(acf_per_pos[i], ls = '--', c = 'r', lw = 2) if t_acf.lags_days.max() > 10 * acf_per_pos[0]: pylab.xlim(0,10 * acf_per_pos[0]) else: pylab.xlim(0,max(t_acf.lags_days)) pylab.xlabel('Period (days)') pylab.ylabel('ACF') pylab.title('ID: %s, Smoothed \& Unsmoothed ACF' %(kid)) pylab.savefig('%s/PDCQ%s_output/plots_sm/%s_sm.png' % (dir, quarter_acf, kid)) return t_acf, acf_per_pos, acf_per_height, acf_per_err, locheight, asym
fnoise = sqrt(2*dt/tau)*lfilter(b, a, noise) return fnoise if __name__ == '__main__': tau = 10*ms dt = .1*ms duration = 1000*ms noise = white_noise(dt, duration) cnoise = colored_noise(tau, dt, duration) from numpy import linspace t = linspace(0*ms, duration, len(noise)) from pylab import acorr, show, subplot, plot, ylabel, xlim subplot(221) plot(t, noise) ylabel('white noise') subplot(222) acorr(noise, maxlags=200) xlim(-200,200) subplot(223) plot(t, cnoise) ylabel('colored noise') subplot(224) acorr(cnoise, maxlags=200) xlim(-200,200) show()
print('Length of StaLta, samprate, wfm:', len(StaLta), samprate, len(wfm)) fw, = plt.plot(t[0:len(StaLta)],StaLta[0:len(StaLta)]) label = fm.format('{0} - Filtered',station) plt.title(label) plt.subplot(313,sharex=ax1) #StaLta = gy.CompStaLta(L_ta, S_ta, samprate, fltwfm) t0=time.time() print("Time before pylab autocorrelation:",t0) lags,cross,linecol,b = pylab.acorr(fltwfm, normed=True, maxlags=None, usevlines=True , detrend=mlab.detrend_none) print("Time elapsed for pylab autocorrelation:",time.time()-t0) plt.ylabel("Lag in seconds") #plt.subplot(414,sharex=ax1) #Pxx,freqs,bins,filt_im = pylab.specgram(fltwfm,NFFT=512,Fs=samprate,noverlap=510) plt.xlabel(label) plt.ylabel("Normalized Correlation") #plt.subplot(414,sharex=ax1) #mycrossco = PPAcross(fltwfm,len(fltwfm)) #myt = array('f',[]) #for i in range(len(mycrossco)):
@author: TH """ import pylab as pl import pandas as pd import numpy as np from statsmodels.graphics.tsaplots import plot_acf, plot_pacf def wgn(x, snr): snr = 10**(snr / 10.0) xpower = np.sum(x**2) / len(x) npower = xpower / snr return np.random.randn(len(x)) * np.sqrt(npower) t = np.arange(0, 1000000) * 0.1 x = np.sin(t) n = wgn(x, 6) xn = x + n # 增加了6dBz信噪比噪声的信号 pl.subplot(211) pl.hist(n, bins=10, normed=True) pl.subplot(212) pl.psd(n) pl.show() pl.acorr(np.ones(20)) pl.show() plot_acf(np.ones(20))
def acf_calc(time, flux, interval, kid, max_psearch_len): ''' Calculate ACF, calls error calc function''' lags, acf, lines, axis = pylab.acorr(flux, maxlags = max_psearch_len) #convolve smoothing window with Gaussian kernel gauss_func = lambda x,sig: 1./np.sqrt(2*np.pi*sig**2) * \ np.exp(-0.5*(x**2)/(sig**2)) #define a Gaussian #create the smoothing kernel conv_func = gauss_func(np.arange(-28,28,1.),9.) acf_smooth = np.convolve(acf,conv_func,mode='same') #and convolve lenlag = len(lags) lags = lags[int(lenlag/2.0):lenlag][:-1] * interval acf = acf[int(lenlag/2.0): lenlag][0:-1] acf_smooth = acf_smooth[int(lenlag/2.0): lenlag][1:] # find max using usmoothed acf (for plot only) max_ind_us, max_val_us = extrema(acf, max = True, min = False) # find max/min using smoothed acf max_ind_s, max_val_s = extrema(acf_smooth, max = True, min = False) min_ind_s, min_val_s = extrema(acf_smooth, max = False, min = True) maxmin_ind_s, maxmin_val_s = extrema(acf_smooth, max = True, min = True) if len(max_ind_s) > 0 and len(min_ind_s) > 0: # ensure no duplicate peaks are detected t_max_s = atpy.Table() t_max_s.add_column('ind', max_ind_s) t_max_s.add_column('val', max_val_s) t_min_s = atpy.Table() t_min_s.add_column('ind', min_ind_s) t_min_s.add_column('val', min_val_s) t_maxmin_s = atpy.Table() t_maxmin_s.add_column('ind', maxmin_ind_s) t_maxmin_s.add_column('val', maxmin_val_s) ma_i = collections.Counter(t_max_s.ind) dup_arr = [i for i in ma_i if ma_i[i]>1] if len(dup_arr) > 0: for j in scipy.arange(len(dup_arr)): tin = t_max_s.where(t_max_s.ind != dup_arr[j]) tout = t_max_s.where(t_max_s.ind == dup_arr[j]) tout = tout.rows([0]) tin.append(tout) t_max_s = copy.deepcopy(tin) ma_i = collections.Counter(t_min_s.ind) dup_arr = [i for i in ma_i if ma_i[i]>1] if len(dup_arr) > 0: for j in scipy.arange(len(dup_arr)): tin = t_min_s.where(t_min_s.ind != dup_arr[j]) tout = t_min_s.where(t_min_s.ind == dup_arr[j]) tout = tout.rows([0]) tin.append(tout) t_min_s = copy.deepcopy(tin) ma_i = collections.Counter(t_maxmin_s.ind) dup_arr = [i for i in ma_i if ma_i[i]>1] if len(dup_arr) > 0: for j in scipy.arange(len(dup_arr)): tin = t_maxmin_s.where(t_maxmin_s.ind != dup_arr[j]) tout = t_maxmin_s.where(t_maxmin_s.ind == dup_arr[j]) tout = tout.rows([0]) tin.append(tout) t_maxmin_s = copy.deepcopy(tin) t_max_s.sort('ind') t_min_s.sort('ind') t_maxmin_s.sort('ind') # relate max inds to lags maxnum = len(t_max_s.ind) acf_per_pos = lags[t_max_s.ind] acf_per_height = acf[t_max_s.ind] # Calculate peak widths, asymmetries etc acf_per_err, locheight, asym= \ calc_err(kid = kid, lags = lags, acf = acf, inds = \ t_maxmin_s.ind, vals = t_maxmin_s.val, maxnum = maxnum) else: acf_per_pos = scipy.array([-9999]) acf_per_height = scipy.array([-9999]) acf_per_err = scipy.array([-9999]) locheight = scipy.array([-9999]) asym = scipy.array([-9999]) # save corrected LC and ACF t_lc = atpy.Table() t_lc.add_column('time', time) t_lc.add_column('flux', flux) t_acf = atpy.Table() t_acf.add_column('lags_days', lags) t_acf.add_column('acf', acf) t_acf.add_column('acf_smooth', acf_smooth) return t_acf, acf_per_pos, acf_per_height, acf_per_err, locheight, asym
def acf_calc(time, flux, interval, kid, max_psearch_len): ''' Calculate ACF, calls error calc function''' # ACF calculation in pylab, close fig when finished pylab.figure(50) pylab.clf() lags, acf, lines, axis = pylab.acorr(flux, maxlags = max_psearch_len) pylab.close(50) lenlag = len(lags) lags = lags[int(lenlag/2.0):lenlag][:-1] * interval acf = acf[int(lenlag/2.0): lenlag][0:-1] ptout = pd.peakdetect(acf, lookahead = 100*interval, delta = 0.02) if len(ptout[0]) == 0: t_acf = atpy.Table() t_acf.add_column('lags_days', lags) t_acf.add_column('acf', acf) t_acf.write('%s/ACF_output/dat_files/%s_ACF.ipac' % (dir, kid), overwrite = True) acf_per_pos = scipy.array([-9999]) acf_per_height = scipy.array([-9999]) acf_per_err = scipy.array([-9999]) locheight = scipy.array([-9999]) asym = scipy.array([-9999]) return t_acf, acf_per_pos, acf_per_height, acf_per_err, locheight, asym max_inds = scipy.array(scipy.array(ptout[0])[:,0], dtype = 'int') max_height = scipy.array(ptout[0])[:,1] min_inds = scipy.array(ptout[1])[:,0] min_height = scipy.array(ptout[1])[:,1] maxnum = len(max_inds) if maxnum > 0: t = atpy.Table() t.add_column('ind', scipy.append(max_inds, min_inds)) t.add_column('val', scipy.append(max_height, min_height)) t.sort('ind') acf_per_pos = lags[max_inds] acf_per_height = max_height print 'Calculating errors and asymmetries...' # Calculate peak widths, asymmetries etc acf_per_err, locheight, asym= \ calc_err(kid = kid, lags = lags, acf = acf, inds = t.ind, vals = t.val, maxnum = maxnum) else: acf_per_pos = scipy.array([-9999]) acf_per_height = scipy.array([-9999]) acf_per_err = scipy.array([-9999]) locheight = scipy.array([-9999]) asym = scipy.array([-9999]) # save corrected LC and ACF t_lc = atpy.Table() t_lc.add_column('time', time) t_lc.add_column('flux', flux) t_lc.write('%s/ACF_output/dat_files/%s_LC.ipac' % (dir, kid), overwrite = True) t_acf = atpy.Table() t_acf.add_column('lags_days', lags) t_acf.add_column('acf', acf) t_acf.write('%s/ACF_output/dat_files/%s_ACF.ipac' % (dir, kid), overwrite = True) return t_acf, acf_per_pos, acf_per_height, acf_per_err, locheight, asym
return fnoise if __name__ == '__main__': tau = 10 * ms dt = .1 * ms duration = 1000 * ms noise = white_noise(dt, duration) cnoise = colored_noise(tau, dt, duration) from numpy import linspace t = linspace(0 * ms, duration, len(noise)) from pylab import acorr, show, subplot, plot, ylabel, xlim subplot(221) plot(t, noise) ylabel('white noise') subplot(222) acorr(noise, maxlags=200) xlim(-200, 200) subplot(223) plot(t, cnoise) ylabel('colored noise') subplot(224) acorr(cnoise, maxlags=200) xlim(-200, 200) show()
def PlotParameters(process, acorr=False): """docstring for PlotRMSE""" mapping = { 'IGOU': ['Inverse Gaussian-OU', ['\\gamma', 'a', 'b', '\\lambda_0']], 'GOU': ['Gamma-OU', ['\\gamma', 'a', 'b', '\\lambda_0']], 'CIR': ['CIR', ['\\kappa', '\\nu', '\\gamma', '\\lambda_0']], } dates, dynamic_parameters = GetParameters(process, True) dates, static_parameters = GetParameters(process, False) # print dates # print dynamic_parameters parameter_names = mapping[process][1] process_name = mapping[process][0] pylab.clf() pylab.figure(1) for i, param in enumerate(parameter_names): dynamic_values = dynamic_parameters[i] static_values = static_parameters[i] pylab.subplot(2, 2, i) lag = 5 usevlines = False from matplotlib.patches import Rectangle if acorr: if AUTOCOLOR: dyn = pylab.acorr(dynamic_values, label="Dynamic", color=AUTOCOLOR_COLORS[0], maxlags=lag, usevlines=usevlines) stat = pylab.acorr(static_values, label="Static", color=AUTOCOLOR_COLORS[1], maxlags=lag, usevlines=usevlines) p = Rectangle((0, 0), 1, 1, fc=AUTOCOLOR_COLORS[0]) q = Rectangle((0, 0), 1, 1, fc=AUTOCOLOR_COLORS[1]) pylab.ylim([0, 1]) pylab.legend((p, q), ("Dynamic", "Static")) else: dyn = pylab.acorr(dates, dynamic_values, label="Dynamic", color=AUTOCOLOR_COLORS[0]) stat = pylab.acorr(dates, static_values, label="Static", color=AUTOCOLOR_COLORS[1]) else: if AUTOCOLOR: dyn = pylab.plot(dynamic_values, label="Dynamic", color=AUTOCOLOR_COLORS[0]) stat = pylab.plot(static_values, label="Static", color=AUTOCOLOR_COLORS[1]) pylab.legend() else: dyn = pylab.plot(dates, dynamic_values, label="Dynamic", color=AUTOCOLOR_COLORS[0]) stat = pylab.plot(dates, static_values, label="Static", color=AUTOCOLOR_COLORS[1]) pylab.legend() # pylab.title('Stability of $' + param + '$') # pylab .xlabel('Year') pylab.ylabel('$' + param + '$') # loc = mpl.dates.MonthLocator(1) # dateFmt = mpl.dates.DateFormatter('%b %y') # pylab.gca().xaxis.set_major_formatter(dateFmt) # # # pylab.gca().xaxis.set_major_locator(loc) # # pylab.subplots_adjust(bottom=0.15) pylab.subplots_adjust(wspace=0.4) pylab.suptitle(process_name, fontsize=10) pdf_file = "../../Diagrams/ParameterStability/" + process + "Parameters.pdf" if acorr: pdf_file = "../../Diagrams/ParameterStability/" + process + "ParametersAutoCorr.pdf" pylab.savefig(pdf_file)
def autocorrelation( data, name, maxlags=100, format='png', reflected=False, suffix='-acf', path='./', fontmap=None, new=True, last=True, rows=1, columns=1, num=1, verbose=1): """ Generate bar plot of the autocorrelation function for a series (usually an MCMC trace). :Arguments: data: PyMC object, trace or array A trace from an MCMC sample or a PyMC object with one or more traces. name: string The name of the object. maxlags (optional): int The largest discrete value for the autocorrelation to be calculated (defaults to 100). format (optional): string Graphic output format (defaults to png). suffix (optional): string Filename suffix. path (optional): string Specifies location for saving plots (defaults to local directory). fontmap (optional): dict Font mapping for plot labels; most users should not specify this. verbose (optional): int Level of output verbosity. """ # Internal plotting specification for handling nested arrays if fontmap is None: fontmap = {1: 10, 2: 8, 3: 6, 4: 5, 5: 4} # Stand-alone plot or subplot? standalone = rows == 1 and columns == 1 and num == 1 if standalone: if verbose > 0: print_('Plotting', name) figure() subplot(rows, columns, num) if ndim(data) == 1: maxlags = min(len(data) - 1, maxlags) try: acorr(data, detrend=mlab.detrend_mean, maxlags=maxlags) except: print_('Cannot plot autocorrelation for %s' % name) return # Set axis bounds ylim(-.1, 1.1) xlim(-maxlags * reflected or 0, maxlags) # Plot options title( '\n\n %s acorr' % name, x=0., y=1., ha='left', va='top', fontsize='small') # Smaller tick labels tlabels = gca().get_xticklabels() setp(tlabels, 'fontsize', fontmap[1]) tlabels = gca().get_yticklabels() setp(tlabels, 'fontsize', fontmap[1]) elif ndim(data) == 2: # generate acorr plot for each dimension rows = data.shape[1] for j in range(rows): autocorrelation( data[:, j], '%s_%d' % (name, j), maxlags, fontmap=fontmap, rows=rows, columns=1, num=j + 1) else: raise ValueError( 'Only 1- and 2- dimensional functions can be displayed') if standalone: if not os.path.exists(path): os.mkdir(path) if not path.endswith('/'): path += '/' # Save to fiel savefig("%s%s%s.%s" % (path, name, suffix, format))
def auto_correlation(values, lags=100): """ Calculate auto correlation a given List of values """ lags, corr, line, x = pl.acorr( values, maxlags=lags, usevlines=False, marker=None) return lags, corr