def imshow(self,err=False,cbar=False,ax=None,show=True,border=False,ZORDER=0,cmap=cm.binary,alpha=True): """ Basic plotting of the dynamic spectrum """ if err: spec = self.errdata else: spec = self.data T=self.T if self.T is None: if self.Tcenter is None: T = np.arange(len(spec)) else: T = self.Tcenter F=self.F if self.F is None: if self.Fcenter is None: F = np.arange(len(spec[0])) else: F = self.Fcenter #cmap = cm.binary#jet if alpha: cmap.set_bad(alpha=0.0) if alpha: #do this? for i in range(len(spec)): for j in range(len(spec[0])): if spec[i][j]<=0.0:# or self.errdata[i][j]>3*sigma: spec[i][j]=np.nan minT = T[0] maxT = T[-1] minF = F[0] maxF = F[-1] # print inds # raise SystemExit # spec[inds] = np.nan im=u.imshow(spec,ax=ax,extent = [minT,maxT,minF,maxF],cmap=cmap,zorder=ZORDER) #border here? if border:# and self.extras['name']!='EFF I': plt.plot([T[0],T[-1]],[F[0],F[0]],'0.50',zorder=ZORDER+0.1) plt.plot([T[0],T[-1]],[F[-1],F[-1]],'0.50',zorder=ZORDER+0.1) plt.plot([T[0],T[0]],[F[0],F[-1]],'0.50',zorder=ZORDER+0.1) plt.plot([T[-1],T[-1]],[F[0],F[-1]],'0.50',zorder=ZORDER+0.1) if cbar: plt.colorbar() #im.set_clim(0.0001,None) if show: plt.show() return ax
def plotSecondarySpectrum(self,ax=None,fast=True,minutes=False,labels=False, ss= None, SS_xaxis = None, SS_yaxis = None): """ Plot the secondary spectrum, useful if on a specific axis """ if np.ndim(self.data) <= 2: return doshow = False if ax is None: doshow = True fig = figure() ax = fig.add_subplot(111) if ss is None: ss = self.getSecondarySpectrum(fast=fast) if None in (SS_xaxis,SS_yaxis): #WHAT IF ONE AXIS GIVEN? SS_xaxis,SS_yaxis = self.getSecondarySpectrumAxes() if minutes: SS_xaxis = SS_xaxis*60 extent = [SS_xaxis[0],SS_xaxis[-1],SS_yaxis[0],SS_yaxis[-1]] im=u.imshow(ss, ax=ax, extent = extent) ax.ticklabel_format(style='sci', axis='x', scilimits=(0,0)) if labels: ax_right = ax.twinx() ax_right.set_yticks([]) ax.axes.set_ylabel('Conjugate Frequency (1/MHz)') ax_right.axes.set_ylabel("Secondary Spectrum") if minutes: ax.axes.set_xlabel('Conjugate Time (1/Minutes)') else: ax.axes.set_xlabel('Conjugate Time (1/Seconds)') if doshow: show() return ax
def plotAcf2d(self,ax=None,fast=True,minutes=False,labels=False,acf2d=None,tlags=None,flags=None): """ Plot the acf2d, useful if on a specific axis """ if np.ndim(self.data) <= 2: return doshow = False if ax is None: doshow = True fig = figure() ax = fig.add_subplot(111) if None in (acf2d, tlags, flags): tlags, flags = self.getAcf2dAxes() acf2d = self.getAcf2d(fast=fast) if minutes: extent = [tlags[0]/60.0,tlags[-1]/60.0,flags[0],flags[-1]] else: extent = [tlags[0],tlags[-1],flags[0],flags[-1]] if labels: ax_right = ax.twinx() ax_right.set_yticks([]) ax_right.axes.set_ylabel("ACF2D") ax.axes.set_ylabel('Lag (MHz)') #input dynamic units here if minutes: ax.axes.set_xlabel('Lag (Minutes)') else: ax.axes.set_xlabel('Lag (Seconds)') im=u.imshow(acf2d,ax=ax,extent=extent) if doshow: show() return ax
def scintillation_parameters(self,plotbound=1.0): if self.acf is None: self.acf2d() if self.dT is None: dT = 1 else: dT = self.dT if self.dF is None: dF = 1 else: dF = self.dF acfshape = np.shape(self.acf) centerrind = acfshape[0]//2 centercind = acfshape[1]//2 # Look for the central peak in the ACF MIN = np.min(self.acf) if MIN < 0: #The min value is approximately from a gaussian distribution MIN = np.abs(MIN) else: #center,hist = u.histogram(acf.flatten(),interval=0.001) #relies on 0.001 #MIN = center[np.argmax(hist)] MIN = u.RMS(self.acf.flatten()) rslice = self.acf[centerrind:,centercind] maxr = np.where(rslice<=MIN)[0][0] cslice = self.acf[centerrind,centercind:] maxc = np.where(cslice<=MIN)[0][0] plotacf = self.acf[centerrind-plotbound*maxr+1:centerrind+plotbound*maxr,centercind-plotbound*maxc+1:centercind+plotbound*maxc+1] params, pcov = ffit.fitgaussian2d(plotacf) fit = ffit.gaussian2d(*params) amplitude,center_x,center_y,width_x,width_y,rotation,baseline = params if self.verbose: paramnames = ["amplitude","center_x","center_y","width_x","width_y","rotation","baseline"] if pcov is not None: paramerrors = np.array(list(map(np.sqrt,np.diagonal(pcov)))) #multiply by s_sq!!!!! else: paramerrors = np.zeros_like(params) for i,param in enumerate(params): print("%s: %0.2e+/-%0.2e"%(paramnames[i],param,paramerrors[i])) #Solve for scintillation parameters numerically SHAPE = np.shape(plotacf) try: delta_t_d = (optimize.brentq(lambda y: fit(SHAPE[0]//2,y)-baseline-amplitude/np.e,(SHAPE[1]-1)//2,SHAPE[1]*2)-(SHAPE[1]-1)//2)*dT #FWHM test if self.verbose: print("delta_t_d %0.3f minutes"%delta_t_d) except ValueError: if self.verbose: print("ERROR in delta_t_d") delta_t_d = SHAPE[1]*dT try: delta_nu_d = (optimize.brentq(lambda x: fit(x,SHAPE[1]//2)-baseline-amplitude/2.0,(SHAPE[0]-1)//2,SHAPE[0])-(SHAPE[0]-1)//2)*dF if self.verbose: print("delta_nu_d %0.3f MHz"%delta_nu_d) except ValueError: if self.verbose: print("ERROR in delta_nu_d") delta_nu_d = SHAPE[0]*dF if self.verbose: print("dnu/dt %0.3f MHz/min" % ((dF/dT)*np.tan(rotation)))#((dF/dT)*np.tan(rotation)) fig = plt.figure() ax = fig.add_subplot(211) u.imshow(self.data) ax = fig.add_subplot(212) u.imshow(plotacf) plt.colorbar() levels = (amplitude*np.array([1.0,0.5,1.0/np.e]))+baseline levels = (amplitude*np.array([0.5]))+baseline print(levels) ax.contour(fit(*np.indices(plotacf.shape)),levels, colors='k') #ax.set_xlim(len(xs)-20,len(xs)+20) #ax.set_ylim(len(ys)-10,len(ys)+10) plt.show() return delta_t_d,delta_nu_d,rotation #need to report errors
def imshow(self, err=False, cbar=False, ax=None, show=True, border=False, ZORDER=0, cmap=cm.binary, alpha=True, cdf=True): """ Basic plotting of the dynamic spectrum """ if err: spec = self.errdata else: spec = self.data T = self.T if self.T is None: if self.Tcenter is None: T = np.arange(len(spec)) else: T = self.Tcenter F = self.F if self.F is None: if self.Fcenter is None: F = np.arange(len(spec[0])) else: F = self.Fcenter #cmap = cm.binary#jet if alpha: cmap.set_bad(alpha=0.0) if alpha: #do this? for i in range(len(spec)): for j in range(len(spec[0])): if spec[i, j] <= 0.0: # or self.errdata[i][j]>3*sigma: spec[i, j] = np.nan minT = T[0] maxT = T[-1] minF = F[0] maxF = F[-1] if cdf: xcdf, ycdf = u.ecdf(spec.flatten()) low, high = u.likelihood_evaluator(xcdf, ycdf, cdf=True, values=[0.01, 0.99]) for i in range(len(spec)): for j in range(len(spec[0])): if spec[i, j] <= low: spec[i, j] = low elif spec[i, j] >= high: spec[i, j] = high # print inds # raise SystemExit # spec[inds] = np.nan cax = u.imshow(spec, ax=ax, extent=[minT, maxT, minF, maxF], cmap=cmap, zorder=ZORDER) #border here? if border: # and self.extras['name']!='EFF I': plt.plot([T[0], T[-1]], [F[0], F[0]], '0.50', zorder=ZORDER + 0.1) plt.plot([T[0], T[-1]], [F[-1], F[-1]], '0.50', zorder=ZORDER + 0.1) plt.plot([T[0], T[0]], [F[0], F[-1]], '0.50', zorder=ZORDER + 0.1) plt.plot([T[-1], T[-1]], [F[0], F[-1]], '0.50', zorder=ZORDER + 0.1) if cbar: plt.colorbar(cax) #im.set_clim(0.0001,None) if show: plt.show() return ax
def scintillation_parameters(self, plotbound=1.0, maxr=None, maxc=None, savefig=None, show=True, full_output=False): if self.acf is None: self.acf2d() if self.dT is None: dT = 1 else: dT = self.dT if self.dF is None: dF = 1 else: dF = self.dF acfshape = np.shape(self.acf) centerrind = acfshape[0] // 2 centercind = acfshape[1] // 2 # Look for the central peak in the ACF MIN = np.min(self.acf) if MIN < 0: #The min value is approximately from a gaussian distribution MIN = np.abs(MIN) else: #center,hist = u.histogram(acf.flatten(),interval=0.001) #relies on 0.001 #MIN = center[np.argmax(hist)] MIN = u.RMS(self.acf.flatten()) if maxr is None: rslice = self.acf[centerrind:, centercind] maxr = np.where(rslice <= MIN)[0][0] if maxc is None: cslice = self.acf[centerrind, centercind:] maxc = np.where(cslice <= MIN)[0][0] plotacf = self.acf[centerrind - plotbound * maxr + 1:centerrind + plotbound * maxr, centercind - plotbound * maxc + 1:centercind + plotbound * maxc + 1] params, pcov = ffit.fitgaussian2d( plotacf) #pcov already takes into account s_sq issue SHAPE = np.shape(plotacf) fit = ffit.gaussian2d(*params) amplitude, center_x, center_y, width_x, width_y, rotation, baseline = params if self.verbose: paramnames = [ "amplitude", "center_x", "center_y", "width_x", "width_y", "rotation", "baseline" ] if pcov is not None: paramerrors = np.sqrt(np.diagonal(pcov)) else: paramerrors = np.zeros_like(params) for i, param in enumerate(params): print("%s: %0.2e+/-%0.2e" % (paramnames[i], param, paramerrors[i])) #Solve for scintillation parameters numerically try: delta_t_d = (optimize.brentq( lambda y: fit(SHAPE[0] // 2, y) - baseline - amplitude / np.e, (SHAPE[1] - 1) // 2, SHAPE[1] * 2) - (SHAPE[1] - 1) // 2) * dT #FWHM test if self.verbose: print("delta_t_d %0.3f minutes" % delta_t_d) except ValueError: if self.verbose: print("ERROR in delta_t_d") delta_t_d = SHAPE[1] * dT if pcov is not None: err_t_d = paramerrors[3] * dT #assume no rotaton for now else: err_t_d = None try: delta_nu_d = (optimize.brentq( lambda x: fit(x, SHAPE[1] // 2) - baseline - amplitude / 2.0, (SHAPE[0] - 1) // 2, SHAPE[0]) - (SHAPE[0] - 1) // 2) * dF if self.verbose: print("delta_nu_d %0.3f MHz" % delta_nu_d) except ValueError: if self.verbose: print("ERROR in delta_nu_d") delta_nu_d = SHAPE[0] * dF if pcov is not None: err_nu_d = paramerrors[4] * dF #assume no rotaton for now else: err_nu_d = None err_rot = paramerrors[5] if self.verbose: print("dnu/dt %0.3f MHz/min" % ((dF / dT) * np.tan(rotation))) #((dF/dT)*np.tan(rotation)) if show or savefig is not None: fig = plt.figure() ax = fig.add_subplot(211) u.imshow(self.data) ax = fig.add_subplot(212) u.imshow(plotacf) plt.colorbar() levels = (amplitude * np.array([1.0, 0.5, 1.0 / np.e])) + baseline levels = (amplitude * np.array([0.5])) + baseline print(levels) ax.contour(fit(*np.indices(plotacf.shape)), levels, colors='k') #ax.set_xlim(len(xs)-20,len(xs)+20) #ax.set_ylim(len(ys)-10,len(ys)+10) if savefig is not None: plt.savefig(savefig) if show: plt.show() if full_output: return delta_t_d, err_t_d, delta_nu_d, err_nu_d, rotation, err_rot return delta_t_d, delta_nu_d, rotation
def imshow(self, err=False, cbar=False, ax=None, show=True, border=False, zorder=0, cmap=cm.binary, alpha=True, cdf=True, savefig=None, acf=False, ss=False, extent=None, log=False, xlim=None, ylim=None): """ Basic plotting of the dynamic spectrum """ if acf: if self.acf is None: self.acf2d() data = self.acf elif ss: if self.ss is None: self.secondary_spectrum(log=log) data = self.ss elif err: data = self.errdata else: data = self.getData() if extent is None: if acf: T = self.acfT F = self.acfF elif ss: T = self.ssconjT F = self.ssconjF else: if self.Tcenter is None: T = self.T else: T = self.Tcenter if self.Fcenter is None: F = self.F else: F = self.Fcenter if log and not ss: data = np.log10(data) if alpha and not (acf or ss): #or just ignore? cmap.set_bad(alpha=0.0) for i in range(len(data)): for j in range(len(data[0])): if data[i, j] <= 0.0: # or self.errdata[i][j]>3*sigma: data[i, j] = np.nan if cdf: xcdf, ycdf = u.ecdf(data.flatten()) low, high = u.likelihood_evaluator(xcdf, ycdf, cdf=True, values=[0.01, 0.99]) for i in range(len(data)): for j in range(len(data[0])): if data[i, j] <= low: data[i, j] = low elif data[i, j] >= high: data[i, j] = high if extent is None: minT = T[0] maxT = T[-1] minF = F[0] maxF = F[-1] extent = [minT, maxT, minF, maxF] #return np.shape(data) #raise SystemExit # print inds # raise SystemExit # spec[inds] = np.nan if ax is None: fig = plt.figure() ax = fig.add_subplot(111) cax = u.imshow(data, ax=ax, extent=extent, cmap=cmap, zorder=zorder) if xlim is not None: plt.xlim(xlim) if ylim is not None: plt.ylim(ylim) #border here? if border: # and self.extras['name']!='EFF I': plt.plot([T[0], T[-1]], [F[0], F[0]], '0.50', zorder=zorder + 0.1) plt.plot([T[0], T[-1]], [F[-1], F[-1]], '0.50', zorder=zorder + 0.1) plt.plot([T[0], T[0]], [F[0], F[-1]], '0.50', zorder=zorder + 0.1) plt.plot([T[-1], T[-1]], [F[0], F[-1]], '0.50', zorder=zorder + 0.1) if acf: plt.xlabel('Time Lag (%s)' % self.Tunit) plt.ylabel('Frequency Lag (%s)' % self.Funit) elif ss: plt.xlabel('Conjugate Time (1/%s)' % self.Tunit) plt.ylabel('Conjugate Frequency (1/%s)' % self.Funit) else: plt.xlabel('Time (%s)' % self.Tunit) plt.ylabel('Frequency (%s)' % self.Funit) if cbar: plt.colorbar(cax) #im.set_clim(0.0001,None) if savefig is not None: plt.savefig(savefig) if show: plt.show() return ax
def scintillation_parameters(self, plotbound=1.0, maxr=None, maxc=None, savefig=None, show=True, full_output=False, simple=False, eta=0.2, cmap=cm.binary): if self.acf is None: self.acf2d() if self.dT is None: dT = 1 else: dT = self.dT if self.dF is None: dF = 1 else: dF = self.dF acfshape = np.shape(self.acf) centerrind = acfshape[0] // 2 centercind = acfshape[1] // 2 if simple: # Do 1D slices NF = len(self.F) Faxis = (np.arange(-(NF - 1), NF, dtype=np.float) * np.abs(dF) ) #why abs? NT = len(self.T) Taxis = (np.arange(-(NT - 1), NT, dtype=np.float) * np.abs(dT))[1:-1] #??? #try: pout, errs = ffit.gaussianfit(Taxis[NT / 2:3 * NT / 2], self.acf[centerrind, NT / 2:3 * NT / 2], baseline=True) f = interpolate.interp1d( Taxis, ffit.funcgaussian(pout, Taxis, baseline=True) - (pout[3] + pout[0] / np.e)) delta_t_d = optimize.brentq(f, 0, Taxis[-1]) #except: # delta_t_d = 0.0 # print "dtd",pout # plt.plot(Taxis,self.acf[centerrind,:]) # plt.plot(Taxis,f(Taxis)) # plt.show() #try: pout, errs = ffit.gaussianfit(Faxis[NF / 2:3 * NF / 2], self.acf[NF / 2:3 * NF / 2, centercind], baseline=True) f = interpolate.interp1d( Faxis, ffit.funcgaussian(pout, Faxis, baseline=True) - (pout[3] + pout[0] / 2)) delta_nu_d = optimize.brentq(f, 0, Faxis[-1]) #except: # delta_nu_d = 0 #print "dnud",pout,Faxis[-1],dF #print Faxis #raise SystemExit #plt.plot(Faxis,self.acf[:,centercind]) #plt.plot(Faxis,f(Faxis)) #plt.show() #following Glenn's code and Cordes 1986 (Space Velocities...) # Errors from finite scintle effect: bw = self.getBandwidth() T = self.getTspan() if delta_t_d == 0.0: N_d = (1 + eta * bw / delta_nu_d) elif delta_nu_d == 0.0: N_d = (1 + eta * T / delta_t_d) else: N_d = (1 + eta * bw / delta_nu_d) * (1 + eta * T / delta_t_d) fse_nu_d = delta_nu_d / (2 * np.log(2) * np.sqrt(N_d) ) #log because of FWHM? fse_t_d = delta_t_d / (2 * np.sqrt(N_d)) err_nu_d = fse_nu_d err_t_d = fse_t_d #need to add in fitting errors if full_output: return delta_t_d, err_t_d, delta_nu_d, err_nu_d return delta_t_d, delta_nu_d # Look for the central peak in the ACF MIN = np.min(self.acf) if MIN < 0: #The min value is approximately from a gaussian distribution MIN = np.abs(MIN) else: #center,hist = u.histogram(acf.flatten(),interval=0.001) #relies on 0.001 #MIN = center[np.argmax(hist)] MIN = u.RMS(self.acf.flatten()) if maxr is None: rslice = self.acf[centerrind:, centercind] maxr = np.where(rslice <= MIN)[0][0] if maxc is None: cslice = self.acf[centerrind, centercind:] maxc = np.where(cslice <= MIN)[0][0] plotacf = self.acf[int(centerrind - plotbound * maxr + 1):int(centerrind + plotbound * maxr), int(centercind - plotbound * maxc + 1):int(centercind + plotbound * maxc + 1)] params, pcov = ffit.fitgaussian2d( plotacf) #pcov already takes into account s_sq issue SHAPE = np.shape(plotacf) fit = ffit.gaussian2d(*params) amplitude, center_x, center_y, width_x, width_y, rotation, baseline = params paramnames = [ "amplitude", "center_x", "center_y", "width_x", "width_y", "rotation", "baseline" ] if pcov is not None: paramerrors = np.sqrt(np.diagonal(pcov)) else: paramerrors = np.zeros_like(params) if self.verbose: for i, param in enumerate(params): print("%s: %0.2e+/-%0.2e" % (paramnames[i], param, paramerrors[i])) #Solve for scintillation parameters numerically try: delta_t_d = (optimize.brentq( lambda y: fit(SHAPE[0] // 2, y) - baseline - amplitude / np.e, (SHAPE[1] - 1) // 2, SHAPE[1] * 2) - (SHAPE[1] - 1) // 2) * dT #FWHM test if self.verbose: print("delta_t_d %0.3f %s" % (delta_t_d, self.Tunit)) except ValueError: if self.verbose: print("ERROR in delta_t_d") delta_t_d = SHAPE[1] * dT if pcov is not None: err_t_d = paramerrors[3] * dT #assume no rotaton for now else: err_t_d = None try: delta_nu_d = (optimize.brentq( lambda x: fit(x, SHAPE[1] // 2) - baseline - amplitude / 2.0, (SHAPE[0] - 1) // 2, SHAPE[0]) - (SHAPE[0] - 1) // 2) * dF if self.verbose: print("delta_nu_d %0.3f %s" % (delta_nu_d, self.Funit)) except ValueError: if self.verbose: print("ERROR in delta_nu_d") delta_nu_d = SHAPE[0] * dF if pcov is not None: err_nu_d = paramerrors[4] * dF #assume no rotaton for now else: err_nu_d = None err_rot = paramerrors[5] #finite-scintle errors if self.verbose: f = (dF / dT) * np.tan(rotation) df = (dF / dT) * np.cos(rotation)**2 * err_rot print("dnu/dt %0.3e+/-%0.3e %s/%s" % (f, df, self.Funit, self.Tunit)) #((dF/dT)*np.tan(rotation)) if show or savefig is not None: fig = plt.figure() ax = fig.add_subplot(211) u.imshow(self.data, cmap=cmap) ax = fig.add_subplot(212) u.imshow(plotacf, cmap=cmap) plt.colorbar() levels = (amplitude * np.array([1.0, 0.5, 1.0 / np.e])) + baseline levels = (amplitude * np.array([0.5])) + baseline #print(levels) ax.contour(fit(*np.indices(plotacf.shape)), levels, colors='k') #ax.set_xlim(len(xs)-20,len(xs)+20) #ax.set_ylim(len(ys)-10,len(ys)+10) if savefig is not None: plt.savefig(savefig) if show: plt.show() if full_output: return delta_t_d, err_t_d, delta_nu_d, err_nu_d, rotation, err_rot return delta_t_d, delta_nu_d, rotation