def discretize(X, method="ef", nbins=None): """ Discretize `X` Parameters ---------- bins : int, optional Number of bins. Default is floor(sqrt(N)) method : string "ef" is equal-frequency binning "ew" is equal-width binning Examples -------- """ nobs = len(X) if nbins == None: nbins = np.floor(np.sqrt(nobs)) if method == "ef": discrete = np.ceil(nbins * stats.rankdata(X)/nobs) if method == "ew": width = np.max(X) - np.min(X) width = np.floor(width/nbins) svec, ivec = stats.fastsort(X) discrete = np.zeros(nobs) binnum = 1 base = svec[0] discrete[ivec[0]] = binnum for i in range(1,nobs): if svec[i] < base + width: discrete[ivec[i]] = binnum else: base = svec[i] binnum += 1 discrete[ivec[i]] = binnum return discrete
def discretize(X, method="ef", nbins=None): """ Discretize `X` Parameters ---------- bins : int, optional Number of bins. Default is floor(sqrt(N)) method : string "ef" is equal-frequency binning "ew" is equal-width binning Examples -------- """ nobs = len(X) if nbins is None: nbins = np.floor(np.sqrt(nobs)) if method == "ef": discrete = np.ceil(nbins * stats.rankdata(X)/nobs) if method == "ew": width = np.max(X) - np.min(X) width = np.floor(width/nbins) svec, ivec = stats.fastsort(X) discrete = np.zeros(nobs) binnum = 1 base = svec[0] discrete[ivec[0]] = binnum for i in range(1,nobs): if svec[i] < base + width: discrete[ivec[i]] = binnum else: base = svec[i] binnum += 1 discrete[ivec[i]] = binnum return discrete
print phasetemp phase.append(phasetemp) print '...Done!\n' head = pf.getheader(ff[0]) start = head['CRVAL1'] step = head['CDELT1'] length = head['NAXIS1'] x = start + pl.arange(0,length)*step hi = x > 6500 low = x < 6625 xx = hi*low # now get the phases, sort them and find the order of numbers. phase,argsort = stats.fastsort(pl.array(phase)) im = pl.array([pf.getdata(ff[i])[xx]-pl.median(pf.getdata(ff[i])[xx]) for i in argsort]) print im #im = pl.array([allspec[i].count for i in range(len(allspec))]) # phases are not equispaced, need to interpolate im on to regular phase grid. #l,w = pl.shape(im) #x = pl.linspace(0.0,1.0,l) #im2 = 1.0*pl.zeros((l,w))
# start with lpDNO @ 829 c/d. check if this is the actual frequency # calculate phase for every spectrum and assign number to it #period = 1.0/3627.965 #t0 = float(pf.getheader(ff[0])['HJD']) print 'Calculating phase...\n' for i in range(len(ff[:-2])): temp = ((float(pf.getheader(ff[i])['HJD']) - T0)/P) phase.append(temp) print '...Done!\n' # now get the phases, sort them and find the order of numbers. phase = pl.array(phase) phase2 = (pl.array(phase) - phase[0]) / P2 phase2,argsort = stats.fastsort(pl.array(phase2)) # speed of light in km/s c = 2.99792458e5 v = 1500.0 for i in ff: # subtract average from spectrum data = pf.getdata(i)# - ave head = pf.getheader(i) # write average subtracted spectrum to new fits file #pf.writeto('avesub%s'%i,data=data,header=head)
pl.subplot(221) pl.plot(pl.array(t-int(t[0])),pl.array(big)) ylo,yhi = pl.ylim() pl.ylim(yhi,ylo) #pl.xlim(0.3,0.6) pl.title('Spectrum lightcurve') pl.xlabel('HJD') pl.ylabel('Total spectrum Mag') pl.subplot(222) f,a = ast.signal.dft(t,big,3000,3934,0.1) pl.title('Spectrum periodogram') pl.xlabel('Frequency [Cycles/Day]') pl.ylabel('Amplitude [mag]') pl.plot(f,a,'g-') sorted,argsort = sci.fastsort(a) print 'Max amplitude of %s at %s cycles/day' % (sorted[-1],f[argsort[-1]]) pl.subplot(223) pl.plot(x,y,'.') pl.title('Photometry lightcurve') ylo,yhi = pl.ylim() pl.ylim(yhi,ylo) pl.xlabel('HJD') pl.ylabel('Relative Magnitude') pl.subplot(224) f,a = ast.signal.dft(x,y,0,3934,0.1) pl.title('Photometry periodogram') pl.xlabel('Frequency [Cycles/Day]') pl.ylabel('Amplitude [mag]') pl.plot(f,a,'g-')
temp.append(date) temp.append(phase) temp.append(flux) pl.save('HeIIlightcurve.dat',pl.array(temp).transpose()) lt = phase < 7.9 date = date[lt] flux = pl.array(flux)[lt] phase = pl.array(phase)[lt] # flatten lightcurve using fourier method fft = sci.fft(flux) fft[0:4] = 0.0 fft[-4:] = 0.0 flux = sci.ifft(fft) f,a = ast.signal.dft(date,flux,0,4000,1) pl.figure() pl.subplot(211) pl.plot(phase,flux) #pl.ylim(-5e-8,5e-8) pl.subplot(212) pl.plot(f,a) sort,argsort = stats.fastsort(a) print 'Max amplitude of %s at %s' % (a[argsort[-1]],f[argsort[-1]]) #print 'Second highest amplitude of %s at %s' % (a[argsort[-2]],f[argsort[-2]]) pl.show()
cof = pl.polyfit(x_bins[i]-xave,y_bins[i],3) z_bins.append(pl.polyval(cof,x_bins[i]-xave)) f = string.split(raw_input('Start End freq : ')) f0 = float(f[0]) f1 = float(f[1]) print f0,f1 # now do fourier transform on every bins and give stats ft_bins = [ast.signal.dft(x_bins[i],y_bins[i]-z_bins[i],f0,f1,1) for i in range(n)] #print pl.shape(ft_bins) print '\nBin\t','Amp\t\t\t','F c/d\t\t','F(s)\n\n' for i in range(n): sort, argsort = stats.fastsort(ft_bins[i][1]) amp = ft_bins[i][1][argsort[-1]] freq = ft_bins[i][0][argsort[-1]] print i,'\t',amp,'\t',freq,'\t',86400.0/freq print '\n\n\n' # make plots pl.figure(figsize=(9,12)) for i in range(n): plots = '%s1%s' % (n,i+1) pl.subplot(plots) pl.plot(ft_bins[i][0],ft_bins[i][1],'k') pl.figure(figsize=(9,12))