def phase_plot(self, starid, axis): from functions import phase star = NGC6633Star(starid) try: lc = star.lightcurve() lc.sigma_clip() lc.rebin(0.01) t = lc.hjd m = lc.mag except TypeError: print 'no data' return m -= np.mean(m) period = star['p_fin'] tp, yp = phase(t, m, period) plt.axvline(period, linestyle='-.', color='0.5') plt.axhline(0.0, linestyle='--', color='0.5') plt.scatter(tp, yp-np.mean(yp), edgecolor='none', facecolor='k', s=5) plt.scatter(tp+period, yp-np.mean(yp), edgecolor='none', facecolor='k', s=5) plt.xticks(np.arange(60)) plt.xlim(0, period*2) plt.yticks(np.arange(plt.ylim()[0],plt.ylim()[1],0.01)) plt.ylim(plt.ylim()[1], plt.ylim()[0]) if star['provisional']: startab = '(%d)' % star['tab'] else: startab = star['tab'] plt.text(0.01, 0.01, startab, fontsize=12, verticalalignment='bottom', transform=axis.transAxes)
def phase_plot(self): period = self.period tp, yp = phase(self.t,self.m, period) s1 = np.sin(2*np.pi*tp/period) c1 = np.cos(2*np.pi*tp/period) s2 = np.sin(4*np.pi*tp/period) c2 = np.cos(4*np.pi*tp/period) A = np.column_stack((np.ones(tp.size), s1, c1, s2, c2)) c, _,_,_ = np.linalg.lstsq(A,yp) plt.scatter(tp, yp-np.mean(yp), edgecolor='none', alpha=0.75) tp1 = np.linspace(0.0, period, 100) s1 = np.sin(2*np.pi*tp1/period) c1 = np.cos(2*np.pi*tp1/period) s2 = np.sin(4*np.pi*tp1/period) c2 = np.cos(4*np.pi*tp1/period) plt.plot(tp1,c[1]*s1+c[2]*c1+c[3]*s2+c[4]*c2, 'k', linestyle='--', linewidth=2) plt.plot(tp1,c[1]*s1, 'r', linewidth=0.5) plt.plot(tp1,c[2]*c1, 'g', linewidth=0.5) plt.plot(tp1,c[3]*s2, 'y', linewidth=0.5) plt.plot(tp1,c[4]*c2, 'm', linewidth=0.5) plt.xlim(0.0,period) plt.ylim(0.05,-0.05) plt.title(self.starid) plt.xlabel('period [days]') plt.ylabel('theta') plt.grid() #plt.show() plt.savefig('/work1/jwe/Dropbox/Documents/Talks/AIP2014a/phase.pdf') plt.close()
def plot_phase(self, period): from functions import phase tp, yp = phase(self.hjd, self.mag, period) s1 = np.sin(2 * np.pi * tp / period) c1 = np.cos(2 * np.pi * tp / period) s2 = np.sin(4 * np.pi * tp / period) c2 = np.cos(4 * np.pi * tp / period) A = np.column_stack((np.ones(tp.size), s1, c1, s2, c2)) c, _, _, _ = np.linalg.lstsq(A, yp) tp1 = np.linspace(0.0, 2 * period, 100) s1 = np.sin(2 * np.pi * tp1 / period) c1 = np.cos(2 * np.pi * tp1 / period) s2 = np.sin(4 * np.pi * tp1 / period) c2 = np.cos(4 * np.pi * tp1 / period) plt.scatter(tp, yp, edgecolor='none', alpha=0.75) plt.scatter(tp + period, yp, edgecolor='none', alpha=0.75) plt.plot(tp1, c[1] * s1 + c[2] * c1 + c[3] * s2 + c[4] * c2, 'k', linestyle='--', linewidth=2) plt.xlim(0.0, 2 * period) plt.ylim(max(yp - np.mean(yp)), min(yp - np.mean(yp))) plt.xlabel('P = %.4f' % period)
def phase(self, period): """ returnes the lightcurve phased at the given period """ from functions import phase tp, yp = phase(self.hjd, self.mag, period) return tp, yp
def plot_phase(t, x, period): from functions import phase tp, yp = phase(t, x, period) plt.scatter(tp,yp*1000, c='k') plt.scatter(tp+period,yp*1000, c='k') plt.xlim(0.0,period*2) plt.ylim(min(yp)*1000,max(yp)*1000) plt.xlabel('period = %.2f' % period) plt.axvline(period, linestyle='--', color='k') plt.minorticks_on() plt.ylabel('mmag') plt.ylim(plt.ylim()[::-1])
def phase_plot(self, show=False, save=False): period = self['p_fin'] tp, yp = phase(self.t, self.m, period) s1 = np.sin(2 * np.pi * tp / period) c1 = np.cos(2 * np.pi * tp / period) s2 = np.sin(4 * np.pi * tp / period) c2 = np.cos(4 * np.pi * tp / period) A = np.column_stack((np.ones(tp.size), s1, c1, s2, c2)) c, _, _, _ = np.linalg.lstsq(A, yp) plt.scatter(tp, yp - np.mean(yp), edgecolor='none', alpha=0.75) plt.scatter(tp + period, yp - np.mean(yp), edgecolor='none', alpha=0.75) tp1 = np.linspace(0.0, period * 2.0, 100) s1 = np.sin(2 * np.pi * tp1 / period) c1 = np.cos(2 * np.pi * tp1 / period) s2 = np.sin(4 * np.pi * tp1 / period) c2 = np.cos(4 * np.pi * tp1 / period) plt.axvline(period, linestyle='-.') plt.plot(tp1, c[1] * s1 + c[2] * c1 + c[3] * s2 + c[4] * c2, 'k', linestyle='--', linewidth=2) plt.plot(tp1, c[1] * s1, 'r', linewidth=0.5) plt.plot(tp1, c[2] * c1, 'g', linewidth=0.5) plt.plot(tp1, c[3] * s2, 'y', linewidth=0.5) plt.plot(tp1, c[4] * c2, 'm', linewidth=0.5) plt.xlim(0.0, period * 2) plt.ylim(max(yp - np.mean(yp)), min(yp - np.mean(yp))) plt.xlabel('period [days]') plt.ylabel('mag') plt.minorticks_on() plt.grid()
def phased(self, period): from functions import phase tp, yp = phase(self.hjd, self.mag, period) s1 = np.sin(2 * np.pi * tp / period) c1 = np.cos(2 * np.pi * tp / period) s2 = np.sin(4 * np.pi * tp / period) c2 = np.cos(4 * np.pi * tp / period) A = np.column_stack((np.ones(tp.size), s1, c1, s2, c2)) c, resid, _, _ = np.linalg.lstsq(A, yp) amp_err = resid[0] amp = max(c[1]*s1+c[2]*c1+c[3]*s2+c[4]*c2)-\ min(c[1]*s1+c[2]*c1+c[3]*s2+c[4]*c2) tp1 = np.linspace(0.0, period, 100) s1 = np.sin(2 * np.pi * tp1 / period) c1 = np.cos(2 * np.pi * tp1 / period) s2 = np.sin(4 * np.pi * tp1 / period) c2 = np.cos(4 * np.pi * tp1 / period) return amp, amp_err
def analysis(self, show=False): """perform a PDM analysis on each lightcurve""" from matplotlib import rcParams from functions import sigma_clip, phase print 'Analysis' fig_width = 18.3/2.54 # width in inches, was 7.48in fig_height = 23.3/2.54 # height in inches, was 25.5 fig_size = [fig_width,fig_height] #set plot attributes params = {'backend': 'Agg', 'axes.labelsize': 12, 'axes.titlesize': 12, 'font.size': 12, 'xtick.labelsize': 12, 'ytick.labelsize': 12, 'figure.figsize': fig_size, 'savefig.dpi' : 300, 'font.family': 'sans-serif', 'axes.linewidth' : 0.5, 'xtick.major.size' : 2, 'ytick.major.size' : 2, } rcParams.update(params) for starid in self.stars: star = NGC2236Star(starid) print '%-24s '% starid, lc = star.lightcurve() meanflux = np.nanmean(lc.flux) lc.flux /= meanflux lc.flux *= meanflux lc.rebin(0.0125) lc.interpolate() lc.normalize() raw_time, raw_flux = (lc.time, lc.flux) lc.medfilter() lc.jmpflt() lc.detrend() time, flux = (lc.time, lc.flux) time -= min(time) # convert to magnitudes mag = -2.512*np.log10(meanflux*flux) + 24 # remove the mean so we are around 0.0 magnitudes mag -= np.mean(mag) # perform a 3sigma clipping time, mag = sigma_clip(time, mag) # calculate fourier spectrum with zero padding n = len(mag) n2 = 8*n ft = np.fft.rfft(mag, n2) #amp = abs(ft)/n power = abs(ft)**2/n freq = np.fft.fftfreq(n2, d=lc.dt()) # apply a bessel filter to eliminate runlength artifacts from scipy import signal b, a = signal.butter(4, 2./max(time), 'high', analog=True) _, h = signal.freqs(b, a, worN=freq[1:n]) filt_pwr = abs(ft[1:n]*h)**2/n i = np.argmax(filt_pwr)+1 maxfreq = freq[i] period = 1./maxfreq from pdm import pdm #refine period using pdm pdm_periods, pdm_thetas = pdm(time, mag, period/1.5, period*1.5, 0.0125) i = np.argmin(pdm_thetas) period = pdm_periods[i] print 'P = %5.2f' % period, star['period'] = period periods = 1./freq[1:n] periods = periods[::-1] filt_pwr = filt_pwr[::-1] norm = np.mean(filt_pwr) print '%.1f' % (max(filt_pwr)/norm) power[1:n] /= norm filt_pwr /= norm star['amp'] = max(filt_pwr)/norm ph_time, ph_mag = phase(time, mag, period) num = len(ph_time)/round(max(time)/period) rph_mag, _ = signal.resample(ph_mag, num, t = ph_time) rph_time = np.linspace(min(ph_time),max(ph_time),num) bv = star['bv'] if bv is None: bv=-99 plt.subplot(411) ################################################## plt.title('%s (%d) B-V=%.2f' % (starid, star['corotid'], bv)) plt.scatter(raw_time-min(raw_time), raw_flux, edgecolor='none', alpha=0.5, s=3, color='k') plt.ylim(min(raw_flux),max(raw_flux)) plt.xlim(0,max(time)) plt.subplot(412) ################################################## plt.plot(time, -mag, 'k') plt.ylim(min(-mag),max(-mag)) plt.xlim(0,max(time)) plt.subplot(413) ################################################## plt.plot(1./freq[1:n], power[1:n], 'k') plt.plot(1./freq[1:n], filt_pwr, 'g') #plt.plot(1./w, 20 * np.log10(abs(h))) plt.axvline(period) #plt.axhline(np.mean(filt_pwr[1:n])) plt.xlim(0.1, max(time)) plt.subplot(414) ################################################## plt.plot(rph_time, -rph_mag,color='k') plt.plot(rph_time+period, -rph_mag,color='k') #plt.plot(phased_time, phased_mag, 'g') #plt.plot(phased_time+period, phased_mag, 'g') plt.axvline(period, linestyle='--') plt.xlabel('P = %.2f' % period) plt.xlim(0., period*2) #plt.grid() if show: plt.show() else: plt.savefig(config.plotpath+'%s(%d).pdf' % (starid,star['corotid'])) plt.close()
def lightcurve_overplot(self, show=False, save=False): fig_width = 8.9 / 2.54 # width in inches, was 7.48in fig_height = 0.75 * 8.9 / 2.54 # height in inches, was 25.5 fig_size = [fig_width, fig_height] #set plot attributes from matplotlib import rcParams params = { 'backend': 'Agg', 'axes.labelsize': 8, 'axes.titlesize': 10, 'font.size': 8, 'xtick.labelsize': 8, 'ytick.labelsize': 8, 'figure.figsize': fig_size, 'savefig.dpi': 300, 'font.family': 'sans-serif', 'axes.linewidth': 0.2, 'xtick.major.size': -2, 'ytick.major.size': -2, u'figure.subplot.bottom': 0.15, u'figure.subplot.left': 0.20, #u'figure.subplot.right' : 0.95, #u'figure.subplot.top' : 0.9 } rcParams.update(params) period = self['period'] tp, yp = phase(self.t, self.m, period) s1 = np.sin(2 * np.pi * tp / period) c1 = np.cos(2 * np.pi * tp / period) s2 = np.sin(4 * np.pi * tp / period) c2 = np.cos(4 * np.pi * tp / period) A = np.column_stack((np.ones(tp.size), s1, c1, s2, c2)) c, resid, rank, sigma = np.linalg.lstsq(A, yp) print self.starid, c, resid, rank, sigma plt.scatter(self.t, self.m - np.mean(self.m), edgecolor='none', alpha=0.75, c='red') tp1 = np.linspace(self.t[0], self.t[-1], 200) s1 = np.sin(2 * np.pi * tp1 / period) c1 = np.cos(2 * np.pi * tp1 / period) s2 = np.sin(4 * np.pi * tp1 / period) c2 = np.cos(4 * np.pi * tp1 / period) plt.plot(tp1, c[1] * s1 + c[2] * c1 + c[3] * s2 + c[4] * c2, 'k', linewidth=1) plt.xlim(0.0, tp1[-1]) plt.ylim(0.03, -0.03) plt.title('star #%d' % self['tab']) plt.xlabel('time [days]') plt.ylabel('V [mag] + %.2f' % np.mean(self.m)) plt.grid() if show: plt.show() elif save: plt.savefig(config.plotpath + '%s_lcsine.pdf' % self.starid) plt.savefig(config.plotpath + '%s_lcsine.eps' % self.starid) plt.close()
def analysis(self, show=False): """perform a PDM analysis on each lightcurve""" from pdm import pdm from psd import ppsd from matplotlib import rcParams from functions import sigma_clip, phase print 'Analysis' fig_width = 18.3 / 2.54 # width in inches, was 7.48in fig_height = 23.3 / 2.54 # height in inches, was 25.5 fig_size = [fig_width, fig_height] #set plot attributes params = { 'backend': 'Agg', 'axes.labelsize': 12, 'axes.titlesize': 12, 'font.size': 12, 'xtick.labelsize': 12, 'ytick.labelsize': 12, 'figure.figsize': fig_size, 'savefig.dpi': 300, 'font.family': 'sans-serif', 'axes.linewidth': 0.5, 'xtick.major.size': 2, 'ytick.major.size': 2, } rcParams.update(params) minperiod = 1.2 / 24 maxperiod = 15 for starid in self.stars: star = M48Star(starid) print '%-24s ' % starid, try: t, m, e = star.lightcurve() t -= min(t) except AttributeError: logger.error("Can't load lightcurve %s" % starid) print 'no lightcurve' continue if len(t) < 50: logger.warn("%s: not enough datapoints" % starid) print 'not enough datapoints' continue # perform a 3sigma clipping self.t, self.m, self.e = sigma_clip(t, m, e) # perform a power spectrum analysis tpsa, mpsa = self.t, self.m - np.mean(self.m) n = len(tpsa) # zero padded lightcurves t_padded = np.zeros(4 * n) t_padded[:n] = tpsa t_padded[n:] = np.linspace(max(tpsa), 4 * max(tpsa), 3 * n) m_padded = np.zeros(4 * n) m_padded[:n] = mpsa px, f = ppsd(t_padded, m_padded, lower=1. / maxperiod, upper=1. / minperiod, num=2000) px = np.sqrt(px) # look at 20 days or at most at the length of dataset pdm_periods, pdm_thetas = pdm(self.t, self.m, minperiod, maxperiod, 0.5 / 24) period = pdm_periods[np.argmin(pdm_thetas)] psd_period = 1. / f[np.argmax(px)] #psd_freq = f[np.argmax(px)] from scipy import interpolate i = np.argsort(1. / f) psd_periods = 1. / f[i] psd_power = px[i] psd_periods = np.insert(psd_periods, 0, 0.0) psd_power = np.insert(psd_power, 0, 0.0) psd_int = interpolate.interp1d(psd_periods, psd_power) # use interpolation function returned by `interp1d` sum_amp = psd_int(pdm_periods) * (1. - pdm_thetas) i = np.argmax(sum_amp) period = pdm_periods[i] theta = pdm_thetas[i] period = star['period'] period_err = star['period_err'] tp, yp = phase(self.t, self.m, period) s1 = np.sin(2 * np.pi * tp / period) c1 = np.cos(2 * np.pi * tp / period) s2 = np.sin(4 * np.pi * tp / period) c2 = np.cos(4 * np.pi * tp / period) A = np.column_stack((np.ones(tp.size), s1, c1, s2, c2)) c, resid, _, _ = np.linalg.lstsq(A, yp) amp_err = resid[0] amp = max(c[1]*s1+c[2]*c1+c[3]*s2+c[4]*c2)-\ min(c[1]*s1+c[2]*c1+c[3]*s2+c[4]*c2) tp1 = np.linspace(0.0, period, 100) s1 = np.sin(2 * np.pi * tp1 / period) c1 = np.cos(2 * np.pi * tp1 / period) s2 = np.sin(4 * np.pi * tp1 / period) c2 = np.cos(4 * np.pi * tp1 / period) plt.subplot( 411) ################################################## plt.title('%s (%d) B-V=%.2f' % (starid, star['tab'], star['bv'])) self.plot_lightcurve() plt.subplot( 412) ################################################## plt.axvline(x=psd_period, color='green', alpha=0.5) plt.axvline(x=period, color='red', alpha=0.5) plt.semilogx(1. / f, px * 1000, 'k') plt.xlim(0.1, 30) plt.grid() plt.subplot( 413) ################################################## plt.plot(pdm_periods, pdm_thetas, 'k') from functions import normalize plt.plot(pdm_periods, normalize(sum_amp), 'b') #plt.ylim(theta, 1.0) plt.axvline(x=psd_period, color='green') plt.axvline(x=period, color='red') plt.grid() plt.subplot( 414) ################################################## plt.scatter(tp, yp - np.mean(yp), edgecolor='none', alpha=0.75) plt.plot(tp1, c[1] * s1 + c[2] * c1 + c[3] * s2 + c[4] * c2, 'k', linestyle='--', linewidth=2) plt.xlim(0.0, period) plt.ylim(max(yp - np.mean(yp)), min(yp - np.mean(yp))) plt.xlabel('P = %.4f' % period) plt.grid() #plt.show() comment = 'P=%6.3f+-%.3f a=%.3f+-%.4f %.2f' % \ (period, period_err, amp, amp_err, theta) #plt.savefig(config.plotpath+'%s(%d).pdf' % (starid,star['tab'])) pdf.savefig() plt.close() logger.info(comment) print comment