def _MakePhasebin(self): """document me """ self.time_array = np.zeros(self.Nbin*2) self.config['time']['type'] = 'MJD' T0 = self.config['FoldedLC']['epoch'] Period = self.config['FoldedLC']['Period'] t1 = utils.met_to_MJD(self.config['time']['tmin']) t2 = utils.met_to_MJD(self.config['time']['tmax']) if T0==0: T0=t1 elif t1 < T0: T0 -= np.ceil((T0-t1)/Period)*Period # find orbit numbers covered by range (t1,t2) norbt1 = int(np.floor((t1-T0)/Period)) norbt2 = int(np.ceil((t2-T0)/Period)) phase = np.linspace(0,1,self.Nbin+1) self.gtifile=[] #reset gtifiles for i in range(self.Nbin): self.time_array[2*i] = self.config['time']['tmin'] self.time_array[2*i+1] = self.config['time']['tmax'] gtifn = os.path.join(self.LCfolder,"TimeSelection_{0:02.0f}.dat".format(i)) ints=[] for norb in range(norbt1,norbt2+1): ints.append(((T0+(norb+phase[i])*Period),(T0+(norb+phase[i+1])*Period))) tsel = np.array(ints) np.savetxt(gtifn,tsel) self.gtifile.append(gtifn)
def _PlotLC(self,folded=False): self.info("Reading files produced by enrico") LcOutPath = self.LCfolder + self.config['target']['name'] #Result are stored into list. This allow to get rid of the bin which failled Time = [] TimeErr = [] Flux = [] FluxErr = [] # FluxErrChi2 = [] Index = [] IndexErr = [] Cutoff = [] CutoffErr = [] FluxForNpred = [] # FluxErrForNpred = [] Npred = [] Npred_detected_indices = [] TS = [] uplim = [] # Find name used for index parameter if (self.config['target']['spectrum'] == 'PowerLaw' or self.config['target']['spectrum'] == 'PowerLaw2'): IndexName = 'Index' CutoffName = None elif (self.config['target']['spectrum'] == 'PLExpCutoff' or self.config['target']['spectrum'] == 'PLSuperExpCutoff'): IndexName = 'Index1' CutoffName = 'Cutoff' CutoffErrName = 'dCutoff' else: IndexName = 'alpha' CutoffName = None IndexErrName = 'd' + IndexName Nfail = 0 for i in xrange(self.Nbin): CurConfig = get_config(self.configfile[i]) #Read the result. If it fails, it means that the bins has not bin computed. A warning message is printed try : ResultDic = utils.ReadResult(CurConfig) if ResultDic == {}: raise(ValueError) except : self._errorReading("Fail reading config file",i) Nfail+=1 continue #Update the time and time error array Time.append((ResultDic.get("tmax")+ResultDic.get("tmin"))/2.) TimeErr.append((ResultDic.get("tmax")-ResultDic.get("tmin"))/2.) #Check is an ul have been computed. The error is set to zero for the TGraph. if ResultDic.has_key('Ulvalue') : uplim.append(1) Flux.append(ResultDic.get("Ulvalue")) # FluxErr.append(0) # FluxErrChi2.append(ResultDic.get("dFlux")) # Index.append(ResultDic.get(IndexName)) # IndexErr.append(0) else : uplim.append(0) Flux.append(ResultDic.get("Flux")) FluxErr.append(ResultDic.get("dFlux")) # FluxErrChi2.append(ResultDic.get("dFlux")) Index.append(ResultDic.get(IndexName)) IndexErr.append(ResultDic.get(IndexErrName)) # if CutoffName is not None: # Cutoff.append(ResultDic.get(CutoffName)) # CutoffErr.append(ResultDic.get(CutoffErrName)) # FluxErrForNpred.append(ResultDic.get("dFlux")) FluxForNpred.append(ResultDic.get("Flux")) #Get the Npred and TS values Npred.append(ResultDic.get("Npred")) TS.append(ResultDic.get("TS")) if (CurConfig['LightCurve']['TSLightCurve']<float(ResultDic.get("TS"))): Npred_detected_indices.append(i-Nfail) # #change the list into np array # TS = np.array(TS) Npred = np.asarray(Npred) Npred_detected = np.asarray(Npred[Npred_detected_indices]) Time = np.asarray(Time) TimeErr = np.asarray(TimeErr) Flux = np.asarray(Flux) FluxErr = np.asarray(FluxErr) # Index = np.array(Index) # IndexErr = np.array(IndexErr) # Cutoff = np.array(Cutoff) # CutoffErr = np.array(CutoffErr) FluxForNpred = np.asarray(FluxForNpred) # FluxErrForNpred = np.array(FluxErrForNpred) uplim = np.asarray(uplim,dtype=bool) #Plots the diagnostic plots is asked # Plots are : Npred vs flux # TS vs Time if self.config['LightCurve']['DiagnosticPlots'] == 'yes' and len(Npred)>0: #plot Npred vs flux plt.figure() NdN = np.asarray(Npred) /np.sqrt(Npred) FdF = np.asarray(FluxForNpred) / (np.asarray(FluxErr) + 1e-20) plt.errorbar(NdN, FdF,fmt='+',color='black') if len(Npred_detected)>0: NdN = np.asarray(Npred_detected) /np.sqrt(Npred_detected) FdF = np.asarray(FluxForNpred[Npred_detected_indices]) / (np.asarray(FluxErr[Npred_detected_indices]) + 1e-20) plt.errorbar(NdN, FdF,fmt='+',color='red') popt,_ = scipy.optimize.curve_fit(pol1, NdN, FdF, p0=[0,1])#, sigma=dydata) for i in xrange(len(FluxForNpred)): if FluxForNpred[i]/FluxErr[i]>2*pol1(sqrt(Npred[i]),popt[0],popt[1]): self._errorReading("problem in errors calculation for",i) print "Flux +/- error = ",FluxForNpred[i]," +/- ",FluxErr[i] print "V(Npred) = ",sqrt(Npred[i]) print plt.plot(np.array([0,max(NdN)]),pol1(np.array([0,max(NdN)]),popt[0],popt[1]),'--',color='black') plt.xlabel(r"${\rm Npred/\sqrt{Npred}}$") plt.ylabel(r"${\rm Flux/\Delta Flux}$") plt.savefig(LcOutPath+"_Npred.png", dpi=150, facecolor='w', edgecolor='w', orientation='portrait', papertype=None, format=None, transparent=False, bbox_inches=None, pad_inches=0.1, frameon=None) else : print "No Npred Plot produced" #plot TS vs Time plt.figure() plt.xlabel(r"Time (s)") plt.ylabel(r"Test Statistic") plt.errorbar(x=Time,y=TS,xerr=TimeErr,fmt='+',color='black',ls='None') plt.ylim(ymin=min(TS)*0.8,ymax=max(TS)*1.2) plt.xlim(xmin=max(plt.xlim()[0],1.02*min(Time)-0.02*max(Time)),xmax=min(plt.xlim()[1],1.02*max(Time)-0.02*min(Time))) # Move the offset to the axis label ax = plt.gca() ax.get_yaxis().get_major_formatter().set_useOffset(False) offset_factor = int(np.mean(np.log10(np.abs(ax.get_ylim())))) if (offset_factor != 0): ax.set_yticklabels([float(round(k,5)) for k in ax.get_yticks()*10**(-offset_factor)]) ax.yaxis.set_label_text(ax.yaxis.get_label_text() + r" [${\times 10^{%d}}$]" %offset_factor) # Secondary axis with MJD mjdaxis = plt.twiny() mjdaxis.set_xlim([utils.met_to_MJD(k) for k in ax.get_xlim()]) mjdaxis.set_xlabel(r"Time (MJD)") plt.tight_layout() plt.savefig(LcOutPath+"_TS.png", dpi=150, facecolor='w', edgecolor='w', orientation='portrait', papertype=None, format=None, transparent=False, bbox_inches=None, pad_inches=0.1, frameon=None) # Plot the LC itself. This function return a TH2F for a nice plot # a TGraph and a list of TArrow for the ULs # if folded: # phase = np.linspace(0,1,self.Nbin+1) # Time = (phase[1:]+phase[:-1])/2. # TimeErr = (phase[1:]-phase[:-1])/2. # gTHLC,TgrLC,ArrowLC = plotting.PlotFoldedLC(Time,TimeErr,Flux,FluxErr) # gTHIndex,TgrIndex,ArrowIndex = plotting.PlotFoldedLC(Time,TimeErr,Index,IndexErr) # if CutoffName is not None: # gTHCutoff,TgrCutoff,ArrowCutoff = plotting.PlotFoldedLC(Time,TimeErr,Cutoff,CutoffErr) # else : # gTHLC,TgrLC,ArrowLC = plotting.PlotLC(Time,TimeErr,Flux,FluxErr) # gTHIndex,TgrIndex,ArrowIndex = plotting.PlotLC(Time,TimeErr,Index,IndexErr) # if CutoffName is not None: # gTHCutoff,TgrCutoff,ArrowCutoff = plotting.PlotFoldedLC(Time,TimeErr,Cutoff,CutoffErr) # xmin = min(Time) - max(TimeErr) * 10 # xmax = max(Time) + max(TimeErr) * 10 # ymin = min(Flux) - max(FluxErr) * 1.3 # ymax = max(Flux) + max(FluxErr) * 1.3 plt.figure() plt.xlabel(r"Time (s)") plt.ylabel(r"${\rm Flux\ (photon\ cm^{-2}\ s^{-1})}$") # plt.ylim(ymin=ymin,ymax=ymax) # plt.xlim(xmin=xmin,xmax=xmax) #plt.errorbar(Time,Flux,xerr=TimeErr,yerr=FluxErr,fmt='o',color='black',ls='None',uplims=uplim) plot_errorbar_withuls(Time,TimeErr,TimeErr,Flux,FluxErr,FluxErr,uplim,bblocks=True) plt.ylim(ymin=max(plt.ylim()[0],np.percentile(Flux[~uplim],1)*0.1), ymax=min(plt.ylim()[1],np.percentile(Flux[~uplim],99)*2.0)) plt.xlim(xmin=max(plt.xlim()[0],1.02*min(Time)-0.02*max(Time)), xmax=min(plt.xlim()[1],1.02*max(Time)-0.02*min(Time))) # Move the offset to the axis label ax = plt.gca() ax.get_yaxis().get_major_formatter().set_useOffset(False) offset_factor = int(np.mean(np.log10(np.abs(ax.get_ylim())))) if (offset_factor != 0): ax.set_yticklabels([float(round(k,5)) for k in ax.get_yticks()*10**(-offset_factor)]) ax.yaxis.set_label_text(ax.yaxis.get_label_text() + r" [${\times 10^{%d}}$]" %offset_factor) # Secondary axis with MJD mjdaxis = plt.twiny() mjdaxis.set_xlim([utils.met_to_MJD(k) for k in ax.get_xlim()]) mjdaxis.set_xlabel(r"Time (MJD)") plt.tight_layout() plt.savefig(LcOutPath+"_LC.png", dpi=150, facecolor='w', edgecolor='w', orientation='portrait', papertype=None, format=None, transparent=False, bbox_inches=None, pad_inches=0.1, frameon=None) if self.config["LightCurve"]["SpectralIndex"] == 0 : plt.figure() plt.xlabel(r"Time (s)") plt.ylabel(r"${\rm Index}$") Index = np.asarray(Index) IndexErr = np.asarray(IndexErr) uplimIndex = uplim + Index<0.55 plot_errorbar_withuls(Time[~uplimIndex],TimeErr[~uplimIndex],TimeErr[~uplimIndex], Index[~uplimIndex],IndexErr[~uplimIndex],IndexErr[~uplimIndex], uplimIndex[~uplimIndex],bblocks=True) plt.ylim(ymin=max(plt.ylim()[0],np.percentile(Index[~uplimIndex],1)*0.1),ymax=min(plt.ylim()[1],np.percentile(Index[~uplimIndex],99)*2.0)) plt.xlim(xmin=max(plt.xlim()[0],1.02*min(Time)-0.02*max(Time)),xmax=min(plt.xlim()[1],1.02*max(Time)-0.02*min(Time))) plt.savefig(LcOutPath+"_Index.png", dpi=150, facecolor='w', edgecolor='w', orientation='portrait', papertype=None, format=None, transparent=False, bbox_inches=None, pad_inches=0.1, frameon=None) # compute Fvar and probability of being cst self.info("Flux vs Time: infos") self.FitWithCst(Time,Flux,FluxErr) self.Fvar(Flux,FluxErr) # ### plot and save the Index LC # CanvIndex = ROOT.TCanvas() # gTHIndex.Draw() # TgrIndex.Draw('zP') # #plot the ul as arrow # for i in xrange(len(ArrowIndex)): # ArrowIndex[i].Draw() # #Save the canvas in the LightCurve subfolder # if self.config["LightCurve"]["SpectralIndex"] == 0 : # self.info("Index vs Time") # self.FitWithCst(Time,Index,IndexErr) # CanvIndex.Print(LcOutPath+'_Index.png') # CanvIndex.Print(LcOutPath+'_Index.eps') # CanvIndex.Print(LcOutPath+'_Index.C') #Dump into ascii lcfilename = LcOutPath+"_results.dat" self.info("Write to Ascii file : "+lcfilename) WriteToAscii(Time,TimeErr,Flux,FluxErr,Index,IndexErr,Cutoff,CutoffErr,TS,Npred,lcfilename) if self.config["LightCurve"]['ComputeVarIndex'] == 'yes': self.VariabilityIndex()
def VariabilityIndex(self): """Compute the variability index as in the 2FGL catalogue. (see Nolan et al, 2012)""" LcOutPath = self.LCfolder + self.config['target']['name'] utils._log('Computing Variability index ') self.config['Spectrum']['FitsGeneration'] = 'no' try : ResultDicDC = utils.ReadResult(self.generalconfig) except : self.warning("No results file found; please run enrico_sed first.") return LogL1 = [] LogL0 = [] Time = [] for i in xrange(self.Nbin): CurConfig = get_config(self.configfile[i]) #Read the result. If it fails, it means that the bins has not bin computed. A warning message is printed try : ResultDic = utils.ReadResult(CurConfig) except : self._errorReading("Fail reading the config file ",i) continue # LogL1.append(ResultDic.get("log_like")) #Update the time and time error array Time.append((ResultDic.get("tmax")+ResultDic.get("tmin"))/2.) ############################################################## # Compute the loglike value using the DC flux or prefactor ############################################################## # Create one obs instance CurConfig['Spectrum']['FitsGeneration'] = 'no' _,Fit = GenAnalysisObjects(CurConfig,verbose=0)#be quiet Fit.ftol = float(self.config['fitting']['ftol']) #Spectral index management! parameters = dict() parameters['Index'] = -2. parameters['alpha'] = +2. parameters['Index1'] = -2. parameters['beta'] = 0 parameters['Index2'] = 2. parameters['Cutoff'] = 30000. # set the cutoff to be high for key in parameters.keys(): try: utils.FreezeParams(Fit, self.srcname, key, parameters[key]) except: continue LogL1.append(-Fit.fit(0,optimizer=CurConfig['fitting']['optimizer'])) for key in ["norm","Prefactor","Integral"]: try: utils.FreezeParams(Fit,self.srcname,key, utils.fluxNorm(ResultsDicDC[key])) except: continue LogL0.append(-Fit.fit(0,optimizer=CurConfig['fitting']['optimizer'])) del Fit #Clean memory plt.figure() plt.xlabel("Time") plt.ylabel("Log(Like) Variability") plt.errorbar(Time,LogL0,fmt='o',color='black',ls='None') plt.xlim(xmin=max(plt.xlim()[0],1.02*min(Time)-0.02*max(Time)), xmax=min(plt.xlim()[1],1.02*max(Time)-0.02*min(Time))) # Move the offset to the axis label ax = plt.gca() ax.get_yaxis().get_major_formatter().set_useOffset(False) offset_factor = int(np.mean(np.log10(np.abs(ax.get_ylim())))) if (offset_factor != 0): ax.set_yticklabels([float(round(k,5)) \ for k in ax.get_yticks()*10**(-offset_factor)]) ax.yaxis.set_label_text(ax.yaxis.get_label_text() +\ r" [${\times 10^{%d}}$]" %offset_factor) # Secondary axis with MJD mjdaxis = ax.twiny() mjdaxis.set_xlim([utils.met_to_MJD(k) for k in ax.get_xlim()]) mjdaxis.set_xlabel(r"Time (MJD)") mjdaxis.xaxis.set_major_formatter(matplotlib.ticker.ScalarFormatter(useOffset=False)) plt.setp( mjdaxis.xaxis.get_majorticklabels(), rotation=15 ) plt.tight_layout() plt.savefig(LcOutPath+"_VarIndex.png", dpi=150, facecolor='w', edgecolor='w', orientation='portrait', papertype=None, format=None, transparent=False, bbox_inches=None, pad_inches=0.1, frameon=None) self.info("Variability index calculation") print "\t TSvar = ",2*(sum(LogL1)-sum(LogL0)) print "\t NDF = ",len(LogL0)-1 print "\t Chi2 prob = ",1 - chi2.cdf(2*(sum(LogL1)-sum(LogL0)),len(LogL0)-1) print
def _PlotLC(self,folded=False): self.info("Reading files produced by enrico") LcOutPath = self.LCfolder + self.config['target']['name'] #Result are stored into list. This allow to get rid of the bin which failled Time = [] TimeErr = [] Flux = [] FluxErr = [] # FluxErrChi2 = [] Index = [] IndexErr = [] Cutoff = [] CutoffErr = [] FluxForNpred = [] # FluxErrForNpred = [] Npred = [] Npred_detected_indices = [] TS = [] uplim = [] # Find name used for index parameter if ((self.config['target']['spectrum'] == 'PowerLaw' or self.config['target']['spectrum'] == 'PowerLaw2') and self.config['target']['redshift'] == 0): IndexName = 'Index' CutoffName = None elif (self.config['target']['spectrum'] == 'PLExpCutoff' or self.config['target']['spectrum'] == 'PLSuperExpCutoff'): IndexName = 'Index1' CutoffName = 'Cutoff' CutoffErrName = 'dCutoff' else: IndexName = 'alpha' CutoffName = None IndexErrName = 'd' + IndexName Nfail = 0 for i in xrange(self.Nbin): CurConfig = get_config(self.configfile[i]) #Read the result. If it fails, it means that the bins has not bin computed. A warning message is printed try : ResultDic = utils.ReadResult(CurConfig) if ResultDic == {}: raise(ValueError) except : self._errorReading("Fail reading config file",i) Nfail+=1 continue #Update the time and time error array Time.append((ResultDic.get("tmax")+ResultDic.get("tmin"))/2.) TimeErr.append((ResultDic.get("tmax")-ResultDic.get("tmin"))/2.) #Check is an ul have been computed. The error is set to zero for the TGraph. if ResultDic.has_key('Ulvalue') : uplim.append(1) Flux.append(ResultDic.get("Ulvalue")) # FluxErr.append(0) # FluxErrChi2.append(ResultDic.get("dFlux")) # Index.append(ResultDic.get(IndexName)) # IndexErr.append(0) else : uplim.append(0) Flux.append(ResultDic.get("Flux")) FluxErr.append(ResultDic.get("dFlux")) # FluxErrChi2.append(ResultDic.get("dFlux")) Index.append(ResultDic.get(IndexName)) IndexErr.append(ResultDic.get(IndexErrName)) # if CutoffName is not None: # Cutoff.append(ResultDic.get(CutoffName)) # CutoffErr.append(ResultDic.get(CutoffErrName)) # FluxErrForNpred.append(ResultDic.get("dFlux")) FluxForNpred.append(ResultDic.get("Flux")) #Get the Npred and TS values Npred.append(ResultDic.get("Npred")) TS.append(ResultDic.get("TS")) if (CurConfig['LightCurve']['TSLightCurve']<float(ResultDic.get("TS"))): Npred_detected_indices.append(i-Nfail) # #change the list into np array # TS = np.array(TS) Npred = np.asarray(Npred) Npred_detected = np.asarray(Npred[Npred_detected_indices]) Time = np.asarray(Time) TimeErr = np.asarray(TimeErr) Flux = np.asarray(Flux) FluxErr = np.asarray(FluxErr) # Index = np.array(Index) # IndexErr = np.array(IndexErr) # Cutoff = np.array(Cutoff) # CutoffErr = np.array(CutoffErr) FluxForNpred = np.asarray(FluxForNpred) # FluxErrForNpred = np.array(FluxErrForNpred) uplim = np.asarray(uplim,dtype=bool) #Plots the diagnostic plots is asked # Plots are : Npred vs flux # TS vs Time if self.config['LightCurve']['DiagnosticPlots'] == 'yes' and len(Npred)>0: #plot Npred vs flux plt.figure() NdN = np.asarray(Npred) /np.sqrt(Npred) FdF = np.asarray(FluxForNpred) / (np.asarray(FluxErr) + 1e-20) plt.errorbar(NdN, FdF,fmt='+',color='black') if len(Npred_detected)>2: NdN = np.asarray(Npred_detected) /np.sqrt(Npred_detected) FdF = np.asarray(FluxForNpred[Npred_detected_indices]) / (np.asarray(FluxErr[Npred_detected_indices]) + 1e-20) plt.errorbar(NdN, FdF,fmt='+',color='red') popt,_ = scipy.optimize.curve_fit(pol1, NdN, FdF, p0=[0,1])#, sigma=dydata) for i in xrange(len(FluxForNpred)): if FluxForNpred[i]/FluxErr[i]>2*pol1(sqrt(Npred[i]),popt[0],popt[1]): self._errorReading("problem in errors calculation for",i) print "Flux +/- error = ",FluxForNpred[i]," +/- ",FluxErr[i] print "V(Npred) = ",sqrt(Npred[i]) print plt.plot(np.array([0,max(NdN)]),pol1(np.array([0,max(NdN)]),popt[0],popt[1]),'--',color='black') plt.xlabel(r"${\rm Npred/\sqrt{Npred}}$") plt.ylabel(r"${\rm Flux/\Delta Flux}$") plt.savefig(LcOutPath+"_Npred.png", dpi=150, facecolor='w', edgecolor='w', orientation='portrait', papertype=None, format=None, transparent=False, bbox_inches=None, pad_inches=0.1, frameon=None) else : print "No Npred Plot produced" #plot TS vs Time plt.figure() plt.xlabel(r"Time (s)") plt.ylabel(r"Test Statistic") plt.errorbar(x=Time,y=TS,xerr=TimeErr,fmt='+',color='black',ls='None') plt.ylim(ymin=min(TS)*0.8,ymax=max(TS)*1.2) plt.xlim(xmin=max(plt.xlim()[0],1.02*min(Time)-0.02*max(Time)),xmax=min(plt.xlim()[1],1.02*max(Time)-0.02*min(Time))) # Move the offset to the axis label ax = plt.gca() ax.get_yaxis().get_major_formatter().set_useOffset(False) offset_factor = int(np.mean(np.log10(np.abs(ax.get_ylim())))) if (offset_factor != 0): ax.set_yticklabels([float(round(k,5)) for k in ax.get_yticks()*10**(-offset_factor)]) ax.yaxis.set_label_text(ax.yaxis.get_label_text() + r" [${\times 10^{%d}}$]" %offset_factor) # Secondary axis with MJD mjdaxis = ax.twiny() mjdaxis.set_xlim([utils.met_to_MJD(k) for k in ax.get_xlim()]) mjdaxis.set_xlabel(r"Time (MJD)") mjdaxis.xaxis.set_major_formatter(matplotlib.ticker.ScalarFormatter(useOffset=False)) plt.setp( mjdaxis.xaxis.get_majorticklabels(), rotation=15 ) plt.tight_layout() plt.savefig(LcOutPath+"_TS.png", dpi=150, facecolor='w', edgecolor='w', orientation='portrait', papertype=None, format=None, transparent=False, bbox_inches=None, pad_inches=0.1, frameon=None) # Plot the LC itself. This function return a TH2F for a nice plot # a TGraph and a list of TArrow for the ULs # if folded: # phase = np.linspace(0,1,self.Nbin+1) # Time = (phase[1:]+phase[:-1])/2. # TimeErr = (phase[1:]-phase[:-1])/2. # gTHLC,TgrLC,ArrowLC = plotting.PlotFoldedLC(Time,TimeErr,Flux,FluxErr) # gTHIndex,TgrIndex,ArrowIndex = plotting.PlotFoldedLC(Time,TimeErr,Index,IndexErr) # if CutoffName is not None: # gTHCutoff,TgrCutoff,ArrowCutoff = plotting.PlotFoldedLC(Time,TimeErr,Cutoff,CutoffErr) # else : # gTHLC,TgrLC,ArrowLC = plotting.PlotLC(Time,TimeErr,Flux,FluxErr) # gTHIndex,TgrIndex,ArrowIndex = plotting.PlotLC(Time,TimeErr,Index,IndexErr) # if CutoffName is not None: # gTHCutoff,TgrCutoff,ArrowCutoff = plotting.PlotFoldedLC(Time,TimeErr,Cutoff,CutoffErr) # xmin = min(Time) - max(TimeErr) * 10 # xmax = max(Time) + max(TimeErr) * 10 # ymin = min(Flux) - max(FluxErr) * 1.3 # ymax = max(Flux) + max(FluxErr) * 1.3 plt.figure() plt.xlabel(r"Time (s)") plt.ylabel(r"${\rm Flux\ (photon\ cm^{-2}\ s^{-1})}$") # plt.ylim(ymin=ymin,ymax=ymax) # plt.xlim(xmin=xmin,xmax=xmax) #plt.errorbar(Time,Flux,xerr=TimeErr,yerr=FluxErr,i # fmt='o',color='black',ls='None',uplims=uplim) plot_errorbar_withuls(Time,TimeErr,TimeErr,Flux,FluxErr,FluxErr, uplim,bblocks=True) plt.ylim(ymin=max(plt.ylim()[0],np.percentile(Flux[~uplim],1)*0.1), ymax=min(plt.ylim()[1],np.percentile(Flux[~uplim],99)*2.0)) plt.xlim(xmin=max(plt.xlim()[0],1.02*min(Time)-0.02*max(Time)), xmax=min(plt.xlim()[1],1.02*max(Time)-0.02*min(Time))) # Move the offset to the axis label ax = plt.gca() ax.get_yaxis().get_major_formatter().set_useOffset(False) offset_factor = int(np.mean(np.log10(np.abs(ax.get_ylim())))) if (offset_factor != 0): ax.set_yticklabels([float(round(k,5)) \ for k in ax.get_yticks()*10**(-offset_factor)]) ax.yaxis.set_label_text(ax.yaxis.get_label_text() +\ r" [${\times 10^{%d}}$]" %offset_factor) # Secondary axis with MJD mjdaxis = ax.twiny() mjdaxis.set_xlim([utils.met_to_MJD(k) for k in ax.get_xlim()]) mjdaxis.set_xlabel(r"Time (MJD)") mjdaxis.xaxis.set_major_formatter(matplotlib.ticker.ScalarFormatter(useOffset=False)) plt.setp( mjdaxis.xaxis.get_majorticklabels(), rotation=15 ) plt.tight_layout() plt.savefig(LcOutPath+"_LC.png", dpi=150, facecolor='w', edgecolor='w', orientation='portrait', papertype=None, format=None, transparent=False, bbox_inches=None, pad_inches=0.1, frameon=None) if self.config["LightCurve"]["SpectralIndex"] == 0 : plt.figure() plt.xlabel(r"Time (s)") plt.ylabel(r"${\rm Index}$") Index = np.asarray(Index) IndexErr = np.asarray(IndexErr) uplimIndex = uplim #+ Index<0.55 plot_errorbar_withuls(Time[~uplimIndex], TimeErr[~uplimIndex], TimeErr[~uplimIndex], Index[~uplimIndex], IndexErr[~uplimIndex], IndexErr[~uplimIndex], uplimIndex[~uplimIndex], bblocks=True) plt.ylim(ymin=max(plt.ylim()[0],np.percentile(Index[~uplimIndex],1)*0.1), ymax=min(plt.ylim()[1],np.percentile(Index[~uplimIndex],99)*2.0)) plt.xlim(xmin=max(plt.xlim()[0],1.02*min(Time)-0.02*max(Time)), xmax=min(plt.xlim()[1],1.02*max(Time)-0.02*min(Time))) # Move the offset to the axis label ax = plt.gca() ax.get_yaxis().get_major_formatter().set_useOffset(False) offset_factor = int(np.mean(np.log10(np.abs(ax.get_ylim())))) if (offset_factor != 0): ax.set_yticklabels([float(round(k,5)) \ for k in ax.get_yticks()*10**(-offset_factor)]) ax.yaxis.set_label_text(ax.yaxis.get_label_text() +\ r" [${\times 10^{%d}}$]" %offset_factor) # Secondary axis with MJD mjdaxis = ax.twiny() mjdaxis.set_xlim([utils.met_to_MJD(k) for k in ax.get_xlim()]) mjdaxis.set_xlabel(r"Time (MJD)") mjdaxis.xaxis.set_major_formatter(matplotlib.ticker.ScalarFormatter(useOffset=False)) plt.setp( mjdaxis.xaxis.get_majorticklabels(), rotation=15 ) plt.tight_layout() plt.savefig(LcOutPath+"_Index.png", dpi=150, facecolor='w', edgecolor='w', orientation='portrait', papertype=None, format=None, transparent=False, bbox_inches=None, pad_inches=0.1, frameon=None) # compute Fvar and probability of being cst self.info("Flux vs Time: infos") self.FitWithCst(Time,Flux,FluxErr) self.Fvar(Flux,FluxErr) # ### plot and save the Index LC # CanvIndex = ROOT.TCanvas() # gTHIndex.Draw() # TgrIndex.Draw('zP') # #plot the ul as arrow # for i in xrange(len(ArrowIndex)): # ArrowIndex[i].Draw() # #Save the canvas in the LightCurve subfolder # if self.config["LightCurve"]["SpectralIndex"] == 0 : # self.info("Index vs Time") # self.FitWithCst(Time,Index,IndexErr) # CanvIndex.Print(LcOutPath+'_Index.png') # CanvIndex.Print(LcOutPath+'_Index.eps') # CanvIndex.Print(LcOutPath+'_Index.C') #Dump into ascii lcfilename = LcOutPath+"_results.dat" self.info("Write to Ascii file : "+lcfilename) WriteToAscii(Time,TimeErr,Flux,FluxErr,Index,IndexErr, Cutoff,CutoffErr,TS,Npred,lcfilename) if self.config["LightCurve"]['ComputeVarIndex'] == 'yes': self.VariabilityIndex()
def PlotAppLC(Nbins,LCoutfolder,FITSfile): ROOT.gStyle.SetOptStat(0) spfile=pyfits.open(FITSfile) Time = utils.met_to_MJD(spfile[1].data.field(0)[:-1])#mdj_ref+(spfile[1].data.field(0)[:-1]-met_ref)/DAY_IN_SECOND dTime = (spfile[1].data.field(1)[:-1])/DAY_IN_SECOND Counts = (spfile[1].data.field(2)[:-1]) Exposure = (spfile[1].data.field(4)[:-1]) count_histo =ROOT.TH1F("count","count",Nbins,Time[0],Time[-1]) expo_histo =ROOT.TH1F("exposure","exposure",Nbins,Time[0],Time[-1]) for i in xrange(len(Time)): if Counts[i]>0 and Exposure[i] : count_histo.Fill(Time[i],Counts[i]) expo_histo.Fill(Time[i],Exposure[i]) flux_histo =ROOT.TH1F() count_histo.Copy(flux_histo) #correct for exposure flux_histo.Divide(expo_histo) for i in xrange(Nbins):#Correct error for exposure if expo_histo.GetBinContent(i+1)>0: flux_histo.SetBinError(i+1,count_histo.GetBinError(i+1)/expo_histo.GetBinContent(i+1)) ###################################################################################### #Save event time exposure and count file_evt = open(LCoutfolder+'/TimeExposureCount.txt',"w") #Write into file file_evt.write("Time\tdTime\tExposure\tCounts\n") for i in xrange(len(Time)): file_evt.write(str(Time[i])+"\t"+str(dTime[i])+"\t"+str(Exposure[i])+"\t"+str(Counts[i])+"\n") file_evt.close() ###################################################################################### file_lc = open(LCoutfolder+'/AppLC.txt',"w") for i in xrange(Nbins): file_lc.write(str(flux_histo.GetBinCenter(i))+"\t"+str(flux_histo.GetBinContent(i))+"\t"+str(expo_histo.GetBinContent(i))+"\n") file_lc.close() CanvCount = ROOT.TCanvas() count_histo.SetMarkerStyle(20) count_histo.SetLineColor(1) count_histo.SetMarkerColor(1) count_histo.SetXTitle("Time (MJD)") count_histo.Draw("ep") #Save the canvas in the Apperture LightCurve subfolder CanvCount.Print(LCoutfolder+'/Counts.eps') CanvCount.Print(LCoutfolder+'/Counts.C') CanvExposure = ROOT.TCanvas() CanvExposure.SetGridx() CanvExposure.SetGridy() expo_histo.SetMarkerStyle(20) expo_histo.SetLineColor(1) expo_histo.SetMarkerColor(1) expo_histo.SetXTitle("Time (MJD)") expo_histo.Draw("ep") #Save the canvas in the Apperture LightCurve subfolder CanvExposure.Print(LCoutfolder+'/Exposure.eps') CanvExposure.Print(LCoutfolder+'/Exposure.C') CanvFlux = ROOT.TCanvas() flux_histo.SetNameTitle("Apperture_Photometry_Flux","Apperture_Photometry_Flux") flux_histo.SetYTitle("Flux") flux_histo.SetXTitle("Time (MJD)") flux_histo.SetMarkerStyle(20) flux_histo.SetLineColor(1) flux_histo.SetMarkerColor(1) flux_histo.Draw("ep") #Save the canvas in the Apperture LightCurve subfolder CanvFlux.Print(LCoutfolder+'/AppLC.eps') CanvFlux.Print(LCoutfolder+'/AppLC.C')
def PlotAppLC(Nbins, LCoutfolder, FITSfile): ROOT.gStyle.SetOptStat(0) spfile = pyfits.open(FITSfile) Time = utils.met_to_MJD(spfile[1].data.field( 0)[:-1]) #mdj_ref+(spfile[1].data.field(0)[:-1]-met_ref)/DAY_IN_SECOND dTime = (spfile[1].data.field(1)[:-1]) / DAY_IN_SECOND Counts = (spfile[1].data.field(2)[:-1]) Exposure = (spfile[1].data.field(4)[:-1]) count_histo = ROOT.TH1F("count", "count", Nbins, Time[0], Time[-1]) expo_histo = ROOT.TH1F("exposure", "exposure", Nbins, Time[0], Time[-1]) for i in xrange(len(Time)): if Counts[i] > 0 and Exposure[i]: count_histo.Fill(Time[i], Counts[i]) expo_histo.Fill(Time[i], Exposure[i]) flux_histo = ROOT.TH1F() count_histo.Copy(flux_histo) #correct for exposure flux_histo.Divide(expo_histo) for i in xrange(Nbins): #Correct error for exposure if expo_histo.GetBinContent(i + 1) > 0: flux_histo.SetBinError( i + 1, count_histo.GetBinError(i + 1) / expo_histo.GetBinContent(i + 1)) ###################################################################################### #Save event time exposure and count file_evt = open(LCoutfolder + '/TimeExposureCount.txt', "w") #Write into file file_evt.write("Time\tdTime\tExposure\tCounts\n") for i in xrange(len(Time)): file_evt.write( str(Time[i]) + "\t" + str(dTime[i]) + "\t" + str(Exposure[i]) + "\t" + str(Counts[i]) + "\n") file_evt.close() ###################################################################################### file_lc = open(LCoutfolder + '/AppLC.txt', "w") for i in xrange(Nbins): file_lc.write( str(flux_histo.GetBinCenter(i)) + "\t" + str(flux_histo.GetBinContent(i)) + "\t" + str(expo_histo.GetBinContent(i)) + "\n") file_lc.close() CanvCount = ROOT.TCanvas() count_histo.SetMarkerStyle(20) count_histo.SetLineColor(1) count_histo.SetMarkerColor(1) count_histo.SetXTitle("Time (MJD)") count_histo.Draw("ep") #Save the canvas in the Apperture LightCurve subfolder CanvCount.Print(LCoutfolder + '/Counts.eps') CanvCount.Print(LCoutfolder + '/Counts.C') CanvExposure = ROOT.TCanvas() CanvExposure.SetGridx() CanvExposure.SetGridy() expo_histo.SetMarkerStyle(20) expo_histo.SetLineColor(1) expo_histo.SetMarkerColor(1) expo_histo.SetXTitle("Time (MJD)") expo_histo.Draw("ep") #Save the canvas in the Apperture LightCurve subfolder CanvExposure.Print(LCoutfolder + '/Exposure.eps') CanvExposure.Print(LCoutfolder + '/Exposure.C') CanvFlux = ROOT.TCanvas() flux_histo.SetNameTitle("Apperture_Photometry_Flux", "Apperture_Photometry_Flux") flux_histo.SetYTitle("Flux") flux_histo.SetXTitle("Time (MJD)") flux_histo.SetMarkerStyle(20) flux_histo.SetLineColor(1) flux_histo.SetMarkerColor(1) flux_histo.Draw("ep") #Save the canvas in the Apperture LightCurve subfolder CanvFlux.Print(LCoutfolder + '/AppLC.eps') CanvFlux.Print(LCoutfolder + '/AppLC.C')
def PlotAppLC(Nbins,LCoutfolder,FITSfile): spfile=fits.open(FITSfile) spfile[1].data.sort(order='TIME') Time = utils.met_to_MJD(spfile[1].data.field(0)[:-1])#mdj_ref+(spfile[1].data.field(0)[:-1]-met_ref)/DAY_IN_SECOND dTime = (spfile[1].data.field(1)[:-1])/DAY_IN_SECOND Counts = (spfile[1].data.field(2)[:-1]) Exposure = (spfile[1].data.field(4)[:-1]) count_histo,count_edges = np.histogram(Time,Nbins,weights=Counts) expo_histo,expo_edges = np.histogram(Time,Nbins,weights=Exposure) flux_histo,flux_edges = np.histogram(Time,Nbins,weights=Counts/Exposure) dCounts = np.sqrt(Counts) dflux_histo,_ = np.histogram(Time,Nbins,weights=dCounts/Exposure) ###################################################################################### #Save event time exposure and count file_evt = open(LCoutfolder+'/TimeExposureCount.txt',"w") #Write into file file_evt.write("Time\tdTime\tExposure\tCounts\n") for i in xrange(len(Time)): file_evt.write(str(Time[i])+"\t"+str(dTime[i])+"\t"+str(Exposure[i])+"\t"+str(Counts[i])+"\n") file_evt.close() ###################################################################################### file_lc = open(LCoutfolder+'/AppLC.txt',"w") for i in xrange(len(Time)): file_lc.write(str(Time[i])+"\t"+str(Counts[i]/Exposure[i])+"\t"+str(dCounts[i]/Exposure[i])+"\n") file_lc.close() plt.figure() print count_edges print count_histo plt.errorbar((count_edges[:1]+count_edges[1:])/2.,count_histo,fmt="o") plt.xlabel("Time (MJD)") #Save the canvas in the Apperture LightCurve subfolder plt.savefig(LCoutfolder+'/Counts.png', dpi=150, facecolor='w', edgecolor='w', orientation='portrait', papertype=None, format=None, transparent=False, bbox_inches=None, pad_inches=0.1, frameon=None) plt.figure() plt.errorbar((expo_edges[:1]+expo_edges[1:])/2.,expo_histo,fmt="o") plt.xlabel("Time (MJD)") #Save the canvas in the Apperture LightCurve subfolder plt.savefig(LCoutfolder+'/Exposure.png', dpi=150, facecolor='w', edgecolor='w', orientation='portrait', papertype=None, format=None, transparent=False, bbox_inches=None, pad_inches=0.1, frameon=None) plt.figure() plt.errorbar((flux_edges[:1]+flux_edges[1:])/2.,flux_histo,yerr=dflux_histo,fmt="o") plt.ylabel("Flux") plt.xlabel("Time (MJD)") #Save the canvas in the Apperture LightCurve subfolder plt.savefig(LCoutfolder+'/AppLC.png', dpi=150, facecolor='w', edgecolor='w', orientation='portrait', papertype=None, format=None, transparent=False, bbox_inches=None, pad_inches=0.1, frameon=None)
def _MakeTimeBins(self): from astropy.stats import bayesian_blocks from astropy.table import Table evtfile = str("%s/AppertureLightCurve/%s_%s_MkTime.fits" % (self.folder, self.srcname, self.Tag)) evtlist = Table.read(evtfile, hdu='EVENTS')['TIME'].data expfile = str("%s/AppertureLightCurve/%s_%s_applc.fits" % (self.folder, self.srcname, self.Tag)) expbins = Table.read(expfile, hdu='RATE') meanRate = float(len(evtlist)) / float(self.tmax - self.tmin) print("Mean photon rate %s s^-1" % meanRate) print("Mean photon rate %s day^-1" % (meanRate * 3600. * 24)) #Sort table in function of time just to be sure evtlist.sort() evtlistExpCorrected = np.empty_like(evtlist) expbins[expbins.argsort('TIME')] # Calculate relative exposure time and time correction associated for each exposure bins j = 0 surfaceFermi = 10000 # in cm^2 timeCorrection = np.zeros((len(expbins) + 1, 2)) exposure = np.zeros(len(expbins)) timeCorrection[j, 0] = expbins['TIME'][j] - 0.5 * expbins['TIMEDEL'][j] timeCorrection[j, 1] = 0. exposure[j] = expbins['EXPOSURE'][j] / (expbins['TIMEDEL'][j] * surfaceFermi) for j in range(1, len(expbins)): exposure[j] = expbins['EXPOSURE'][j] / (expbins['TIMEDEL'][j] * surfaceFermi) timeCorrection[ j, 0] = expbins['TIME'][j] - 0.5 * expbins['TIMEDEL'][j] timeCorrection[j, 1] = timeCorrection[ j - 1, 1] + exposure[j - 1] * expbins['TIMEDEL'][j - 1] timeCorrection[j + 1, 0] = expbins['TIME'][j] + 0.5 * expbins['TIMEDEL'][j] timeCorrection[ j + 1, 1] = timeCorrection[j, 1] + exposure[j] * expbins['TIMEDEL'][j] #Apply exposure time correction evtlistcorrected = np.interp(evtlist, timeCorrection[:, 0], timeCorrection[:, 1]) meanRateCorrected = float( len(evtlistcorrected)) / float(timeCorrection[-1, 1] - timeCorrection[0, 1]) print("Mean photon rate exposure corrected %s s^-1" % meanRateCorrected) print("Mean photon rate exposure corrected %s day^-1" % (meanRateCorrected * 3600. * 24)) #Calculate bayesian block edgesCorrected = bayesian_blocks(evtlistcorrected, fitness='events', p0=self.p0) edgesCorrected[0] = timeCorrection[0, 1] edgesCorrected[-1] = timeCorrection[-1, 1] #Calculate bin event for apperture photometry count, tmp = np.histogram(evtlistcorrected, bins=edgesCorrected) errcount = np.sqrt(count) #Correct edges from exposure edges = np.interp(edgesCorrected, timeCorrection[:, 1], timeCorrection[:, 0]) edges[0] = self.tmin edges[-1] = self.tmax #Calculate apperture phtometry flux flux = np.array(count / (edgesCorrected[1:] - edgesCorrected[:-1])) errflux = np.array(errcount / (edgesCorrected[1:] - edgesCorrected[:-1])) self.Nbin = len(edges) - 1 self.time_array = np.zeros(self.Nbin * 2) self.gtifile = [] for i in xrange(self.Nbin): self.time_array[2 * i] = edges[i] self.time_array[2 * i + 1] = edges[i + 1] self.info("Running LC with " + str(self.Nbin) + " bins") for i in xrange(self.Nbin): print "Bin ", i, " Start=", self.time_array[ 2 * i], " Stop=", self.time_array[ 2 * i + 1], 'Apperture Photometry=', flux[ i], '+/-', errflux[i], 'ph.s^-1' #Dump into ascii bbfile = str("%s/BayesianBlocks/%s_bb.dat" % (self.folder, self.srcname)) np.savetxt(bbfile, np.transpose( np.array([ np.array(edges[:-1]), np.array(edges[1:]), np.array(edgesCorrected[1:] - edgesCorrected[:-1]), np.array(count) ])), header='tstart tend dt_exposure_corrected count') #Load apperture flux point time_pt, dTime_pt, flux_pt, errflux_pt = self.readApperturePhotometryPoint( ) plt.figure() plt.xlabel(r"Time (s)") plt.ylabel(r"${\rm Flux\ (photon\ cm^{-2}\ s^{-1})}$") plot_bayesianblocks(np.array(edges[:-1]), np.array(edges[1:]), flux / surfaceFermi, errflux / surfaceFermi, errflux / surfaceFermi, np.zeros(flux.shape).astype(np.bool)) plt.errorbar(time_pt, flux_pt / surfaceFermi, yerr=errflux_pt / surfaceFermi, xerr=dTime_pt / 2., color='k', ls='None') plt.ylim(ymin=max(plt.ylim()[0], np.percentile(flux / surfaceFermi, 1) * 0.1), ymax=min(plt.ylim()[1], np.percentile(flux / surfaceFermi, 99) * 2.0)) plt.xlim(xmin=max( plt.xlim()[0], 1.02 * min(np.array(edges[:-1])) - 0.02 * max(np.array(edges[1:]))), xmax=min( plt.xlim()[1], 1.02 * max(np.array(edges[1:])) - 0.02 * min(np.array(edges[:-1])))) # Move the offset to the axis label ax = plt.gca() ax.get_yaxis().get_major_formatter().set_useOffset(False) offset_factor = int(np.mean(np.log10(np.abs(ax.get_ylim())))) if (offset_factor != 0): ax.set_yticklabels([float(round(k,5)) \ for k in ax.get_yticks()*10**(-offset_factor)]) ax.yaxis.set_label_text(ax.yaxis.get_label_text() +\ r" [${\times 10^{%d}}$]" %offset_factor) # Secondary axis with MJD mjdaxis = ax.twiny() mjdaxis.set_xlim([utils.met_to_MJD(k) for k in ax.get_xlim()]) mjdaxis.set_xlabel(r"Time (MJD)") mjdaxis.xaxis.set_major_formatter( matplotlib.ticker.ScalarFormatter(useOffset=False)) plt.setp(mjdaxis.xaxis.get_majorticklabels(), rotation=15) plt.tight_layout() LcOutPath = self.LCfolder + self.config['target']['name'] plt.savefig(LcOutPath + "_AP.png", dpi=150, facecolor='w', edgecolor='w', orientation='portrait', papertype=None, format=None, transparent=False, bbox_inches=None, pad_inches=0.1, frameon=None)
def _PlotLC(self, folded=False): self.info("Reading files produced by enrico") LcOutPath = self.LCfolder + self.config['target']['name'] #Result are stored into list. This allow to get rid of the bin which failled Time = [] TimeErr = [] Flux = [] FluxErr = [] # FluxErrChi2 = [] Index = [] IndexErr = [] Cutoff = [] CutoffErr = [] FluxForNpred = [] # FluxErrForNpred = [] Npred = [] Npred_detected_indices = [] TS = [] uplim = [] # Find name used for index parameter if ((self.config['target']['spectrum'] == 'PowerLaw' or self.config['target']['spectrum'] == 'PowerLaw2') and self.config['target']['redshift'] == 0): IndexName = 'Index' CutoffName = None elif (self.config['target']['spectrum'] == 'PLExpCutoff' or self.config['target']['spectrum'] == 'PLSuperExpCutoff'): IndexName = 'Index1' CutoffName = 'Cutoff' CutoffErrName = 'dCutoff' else: IndexName = 'alpha' CutoffName = None IndexErrName = 'd' + IndexName Nfail = 0 for i in xrange(self.Nbin): CurConfig = get_config(self.configfile[i]) #Read the result. If it fails, it means that the bins has not bin computed. A warning message is printed try: ResultDic = utils.ReadResult(CurConfig) if ResultDic == {}: raise (ValueError) except: self._errorReading("Fail reading config file", i) Nfail += 1 continue #Update the time and time error array Time.append((ResultDic.get("tmax") + ResultDic.get("tmin")) / 2.) TimeErr.append( (ResultDic.get("tmax") - ResultDic.get("tmin")) / 2.) #Check is an ul have been computed. The error is set to zero for the TGraph. if ResultDic.has_key('Ulvalue'): uplim.append(1) Flux.append(ResultDic.get("Ulvalue")) # FluxErr.append(0) # FluxErrChi2.append(ResultDic.get("dFlux")) # Index.append(ResultDic.get(IndexName)) # IndexErr.append(0) else: uplim.append(0) Flux.append(ResultDic.get("Flux")) FluxErr.append(ResultDic.get("dFlux")) # FluxErrChi2.append(ResultDic.get("dFlux")) Index.append(ResultDic.get(IndexName)) IndexErr.append(ResultDic.get(IndexErrName)) # if CutoffName is not None: # Cutoff.append(ResultDic.get(CutoffName)) # CutoffErr.append(ResultDic.get(CutoffErrName)) # FluxErrForNpred.append(ResultDic.get("dFlux")) FluxForNpred.append(ResultDic.get("Flux")) #Get the Npred and TS values Npred.append(ResultDic.get("Npred")) TS.append(ResultDic.get("TS")) if (CurConfig['BayesianBlocks']['TSLightCurve'] < float( ResultDic.get("TS"))): Npred_detected_indices.append(i - Nfail) # #change the list into np array # TS = np.array(TS) Npred = np.asarray(Npred) Npred_detected = np.asarray(Npred[Npred_detected_indices]) Time = np.asarray(Time) TimeErr = np.asarray(TimeErr) Flux = np.asarray(Flux) FluxErr = np.asarray(FluxErr) # Index = np.array(Index) # IndexErr = np.array(IndexErr) # Cutoff = np.array(Cutoff) # CutoffErr = np.array(CutoffErr) FluxForNpred = np.asarray(FluxForNpred) # FluxErrForNpred = np.array(FluxErrForNpred) uplim = np.asarray(uplim, dtype=bool) #Plots the diagnostic plots is asked # Plots are : Npred vs flux # TS vs Time if self.config['BayesianBlocks']['DiagnosticPlots'] == 'yes' and len( Npred) > 0: #plot Npred vs flux plt.figure() NdN = np.asarray(Npred) / np.sqrt(Npred) FdF = np.asarray(FluxForNpred) / (np.asarray(FluxErr) + 1e-20) plt.errorbar(NdN, FdF, fmt='+', color='black') if len(Npred_detected) > 2: NdN = np.asarray(Npred_detected) / np.sqrt(Npred_detected) FdF = np.asarray(FluxForNpred[Npred_detected_indices]) / ( np.asarray(FluxErr[Npred_detected_indices]) + 1e-20) plt.errorbar(NdN, FdF, fmt='+', color='red') popt, _ = scipy.optimize.curve_fit(pol1, NdN, FdF, p0=[0, 1]) #, sigma=dydata) for i in xrange(len(FluxForNpred)): if FluxForNpred[i] / FluxErr[i] > 2 * pol1( sqrt(Npred[i]), popt[0], popt[1]): self._errorReading("problem in errors calculation for", i) print "Flux +/- error = ", FluxForNpred[ i], " +/- ", FluxErr[i] print "V(Npred) = ", sqrt(Npred[i]) print plt.plot(np.array([0, max(NdN)]), pol1(np.array([0, max(NdN)]), popt[0], popt[1]), '--', color='black') plt.xlabel(r"${\rm Npred/\sqrt{Npred}}$") plt.ylabel(r"${\rm Flux/\Delta Flux}$") plt.savefig(LcOutPath + "_Npred.png", dpi=150, facecolor='w', edgecolor='w', orientation='portrait', papertype=None, format=None, transparent=False, bbox_inches=None, pad_inches=0.1, frameon=None) else: print "No Npred Plot produced" #plot TS vs Time plt.figure() plt.xlabel(r"Time (s)") plt.ylabel(r"Test Statistic") plt.errorbar(x=Time, y=TS, xerr=TimeErr, fmt='+', color='black', ls='None') plt.ylim(ymin=min(TS) * 0.8, ymax=max(TS) * 1.2) plt.xlim(xmin=max(plt.xlim()[0], 1.02 * min(Time) - 0.02 * max(Time)), xmax=min(plt.xlim()[1], 1.02 * max(Time) - 0.02 * min(Time))) # Move the offset to the axis label ax = plt.gca() ax.get_yaxis().get_major_formatter().set_useOffset(False) offset_factor = int(np.mean(np.log10(np.abs(ax.get_ylim())))) if (offset_factor != 0): ax.set_yticklabels([ float(round(k, 5)) for k in ax.get_yticks() * 10**(-offset_factor) ]) ax.yaxis.set_label_text(ax.yaxis.get_label_text() + r" [${\times 10^{%d}}$]" % offset_factor) # Secondary axis with MJD mjdaxis = ax.twiny() mjdaxis.set_xlim([utils.met_to_MJD(k) for k in ax.get_xlim()]) mjdaxis.set_xlabel(r"Time (MJD)") mjdaxis.xaxis.set_major_formatter( matplotlib.ticker.ScalarFormatter(useOffset=False)) plt.setp(mjdaxis.xaxis.get_majorticklabels(), rotation=15) plt.tight_layout() plt.savefig(LcOutPath + "_TS.png", dpi=150, facecolor='w', edgecolor='w', orientation='portrait', papertype=None, format=None, transparent=False, bbox_inches=None, pad_inches=0.1, frameon=None) if len(Time) > 0: plt.figure() plt.xlabel(r"Time (s)") plt.ylabel(r"${\rm Flux\ (photon\ cm^{-2}\ s^{-1})}$") plot_bayesianblocks(Time - TimeErr, Time + TimeErr, Flux, FluxErr, FluxErr, uplim) plt.ylim(ymin=max(plt.ylim()[0], np.percentile(Flux[~uplim], 1) * 0.1), ymax=min(plt.ylim()[1], np.percentile(Flux[~uplim], 99) * 2.0)) plt.xlim(xmin=max( plt.xlim()[0], 1.02 * min(Time - TimeErr) - 0.02 * max(Time + TimeErr)), xmax=min( plt.xlim()[1], 1.02 * max(Time + TimeErr) - 0.02 * min(Time - TimeErr))) # Move the offset to the axis label ax = plt.gca() ax.get_yaxis().get_major_formatter().set_useOffset(False) offset_factor = int(np.mean(np.log10(np.abs(ax.get_ylim())))) if (offset_factor != 0): ax.set_yticklabels([float(round(k,5)) \ for k in ax.get_yticks()*10**(-offset_factor)]) ax.yaxis.set_label_text(ax.yaxis.get_label_text() +\ r" [${\times 10^{%d}}$]" %offset_factor) # Secondary axis with MJD mjdaxis = ax.twiny() mjdaxis.set_xlim([utils.met_to_MJD(k) for k in ax.get_xlim()]) mjdaxis.set_xlabel(r"Time (MJD)") mjdaxis.xaxis.set_major_formatter( matplotlib.ticker.ScalarFormatter(useOffset=False)) plt.setp(mjdaxis.xaxis.get_majorticklabels(), rotation=15) plt.tight_layout() plt.savefig(LcOutPath + "_LC.png", dpi=150, facecolor='w', edgecolor='w', orientation='portrait', papertype=None, format=None, transparent=False, bbox_inches=None, pad_inches=0.1, frameon=None) else: print "[BayesianBlocks] Warning : No valid data" if self.config["BayesianBlocks"]["SpectralIndex"] == 0: if len(Time[~uplimIndex]) > 0: plt.figure() plt.xlabel(r"Time (s)") plt.ylabel(r"${\rm Index}$") Index = np.asarray(Index) IndexErr = np.asarray(IndexErr) uplimIndex = uplim #+ Index<0.55 plot_bayesianblocks(Time[~uplimIndex] - TimeErr[~uplimIndex], Time[~uplimIndex] + TimeErr[~uplimIndex], Index[~uplimIndex], IndexErr[~uplimIndex], IndexErr[~uplimIndex], uplimIndex[~uplimIndex]) plt.ylim(ymin=max(plt.ylim()[0], np.percentile(Index[~uplimIndex], 1) * 0.1), ymax=min(plt.ylim()[1], np.percentile(Index[~uplimIndex], 99) * 2.0)) plt.xlim(xmin=max( plt.xlim()[0], 1.02 * min(Time - TimeErr) - 0.02 * max(Time + TimeErr)), xmax=min( plt.xlim()[1], 1.02 * max(Time + TimeErr) - 0.02 * min(Time - TimeErr))) # Move the offset to the axis label ax = plt.gca() ax.get_yaxis().get_major_formatter().set_useOffset(False) offset_factor = int(np.mean(np.log10(np.abs(ax.get_ylim())))) if (offset_factor != 0): ax.set_yticklabels([float(round(k,5)) \ for k in ax.get_yticks()*10**(-offset_factor)]) ax.yaxis.set_label_text(ax.yaxis.get_label_text() +\ r" [${\times 10^{%d}}$]" %offset_factor) # Secondary axis with MJD mjdaxis = ax.twiny() mjdaxis.set_xlim([utils.met_to_MJD(k) for k in ax.get_xlim()]) mjdaxis.set_xlabel(r"Time (MJD)") mjdaxis.xaxis.set_major_formatter( matplotlib.ticker.ScalarFormatter(useOffset=False)) plt.setp(mjdaxis.xaxis.get_majorticklabels(), rotation=15) plt.tight_layout() plt.savefig(LcOutPath + "_Index.png", dpi=150, facecolor='w', edgecolor='w', orientation='portrait', papertype=None, format=None, transparent=False, bbox_inches=None, pad_inches=0.1, frameon=None) else: print "[BayesianBlocks] Warning : No valid data" #Dump into ascii lcfilename = LcOutPath + "_results.dat" self.info("Write to Ascii file : " + lcfilename) lightcurve.WriteToAscii(Time, TimeErr, Flux, FluxErr, Index, IndexErr, Cutoff, CutoffErr, TS, Npred, lcfilename)
def VariabilityIndex(self): """Compute the variability index as in the 2FGL catalogue. (see Nolan et al, 2012)""" LcOutPath = self.LCfolder + self.config['target']['name'] utils._log('Computing Variability index ') self.config['Spectrum']['FitsGeneration'] = 'no' try : ResultDicDC = utils.ReadResult(self.generalconfig) except : self.warning("No results file found; please run enrico_sed first.") return LogL1 = [] LogL0 = [] Time = [] for i in xrange(self.Nbin): CurConfig = get_config(self.configfile[i]) #Read the result. If it fails, it means that the bins has not bin computed. A warning message is printed try : ResultDic = utils.ReadResult(CurConfig) except : self._errorReading("Fail reading the config file ",i) continue # LogL1.append(ResultDic.get("log_like")) #Update the time and time error array Time.append((ResultDic.get("tmax")+ResultDic.get("tmin"))/2.) ############################################################## # Compute the loglike value using the DC flux or prefactor ############################################################## # Create one obs instance CurConfig['Spectrum']['FitsGeneration'] = 'no' _,Fit = GenAnalysisObjects(CurConfig,verbose=0)#be quiet Fit.ftol = float(self.config['fitting']['ftol']) #Spectral index management! parameters = dict() parameters['Index'] = -2. parameters['alpha'] = +2. parameters['Index1'] = -2. parameters['beta'] = 0 parameters['Index2'] = 2. parameters['Cutoff'] = 100000. # set the cutoff to be high for key in parameters.keys(): try: utils.FreezeParams(Fit, self.srcname, key, parameters[key]) except: continue LogL1.append(-Fit.fit(0,optimizer=CurConfig['fitting']['optimizer'])) for key in ["norm","Prefactor","Integral"]: try: utils.FreezeParams(Fit,self.srcname,key, utils.fluxNorm(ResultsDicDC[key])) except: continue LogL0.append(-Fit.fit(0,optimizer=CurConfig['fitting']['optimizer'])) del Fit #Clean memory plt.figure() plt.xlabel("Time") plt.ylabel("Log(Like) Variability") plt.errorbar(Time,LogL0,fmt='o',color='black',ls='None') plt.xlim(xmin=max(plt.xlim()[0],1.02*min(Time)-0.02*max(Time)), xmax=min(plt.xlim()[1],1.02*max(Time)-0.02*min(Time))) # Move the offset to the axis label ax = plt.gca() ax.get_yaxis().get_major_formatter().set_useOffset(False) offset_factor = int(np.mean(np.log10(np.abs(ax.get_ylim())))) if (offset_factor != 0): ax.set_yticklabels([float(round(k,5)) \ for k in ax.get_yticks()*10**(-offset_factor)]) ax.yaxis.set_label_text(ax.yaxis.get_label_text() +\ r" [${\times 10^{%d}}$]" %offset_factor) # Secondary axis with MJD mjdaxis = ax.twiny() mjdaxis.set_xlim([utils.met_to_MJD(k) for k in ax.get_xlim()]) mjdaxis.set_xlabel(r"Time (MJD)") mjdaxis.xaxis.set_major_formatter(matplotlib.ticker.ScalarFormatter(useOffset=False)) plt.setp( mjdaxis.xaxis.get_majorticklabels(), rotation=15 ) plt.tight_layout() plt.savefig(LcOutPath+"_VarIndex.png", dpi=150, facecolor='w', edgecolor='w', orientation='portrait', papertype=None, format=None, transparent=False, bbox_inches=None, pad_inches=0.1, frameon=None) self.info("Variability index calculation") print "\t TSvar = ",2*(sum(LogL1)-sum(LogL0)) print "\t NDF = ",len(LogL0)-1 print "\t Chi2 prob = ",1 - chi2.cdf(2*(sum(LogL1)-sum(LogL0)),len(LogL0)-1) print
def PlotAppLC(Nbins, LCoutfolder, FITSfile): spfile = fits.open(FITSfile) spfile[1].data.sort(order='TIME') Time = utils.met_to_MJD(spfile[1].data.field( 0)[:-1]) #mdj_ref+(spfile[1].data.field(0)[:-1]-met_ref)/DAY_IN_SECOND dTime = (spfile[1].data.field(1)[:-1]) / DAY_IN_SECOND Counts = (spfile[1].data.field(2)[:-1]) Exposure = (spfile[1].data.field(4)[:-1]) count_histo, count_edges = np.histogram(Time, Nbins, weights=Counts) expo_histo, expo_edges = np.histogram(Time, Nbins, weights=Exposure) flux_histo, flux_edges = np.histogram(Time, Nbins, weights=Counts / Exposure) dCounts = np.sqrt(Counts) dflux_histo, _ = np.histogram(Time, Nbins, weights=dCounts / Exposure) ###################################################################################### #Save event time exposure and count file_evt = open(LCoutfolder + '/TimeExposureCount.txt', "w") #Write into file file_evt.write("Time\tdTime\tExposure\tCounts\n") for i in xrange(len(Time)): file_evt.write( str(Time[i]) + "\t" + str(dTime[i]) + "\t" + str(Exposure[i]) + "\t" + str(Counts[i]) + "\n") file_evt.close() ###################################################################################### file_lc = open(LCoutfolder + '/AppLC.txt', "w") for i in xrange(len(Time)): file_lc.write( str(Time[i]) + "\t" + str(Counts[i] / Exposure[i]) + "\t" + str(dCounts[i] / Exposure[i]) + "\n") file_lc.close() plt.figure() print count_edges print count_histo plt.errorbar((count_edges[:1] + count_edges[1:]) / 2., count_histo, fmt="o") plt.xlabel("Time (MJD)") #Save the canvas in the Apperture LightCurve subfolder plt.savefig(LCoutfolder + '/Counts.png', dpi=150, facecolor='w', edgecolor='w', orientation='portrait', papertype=None, format=None, transparent=False, bbox_inches=None, pad_inches=0.1, frameon=None) plt.figure() plt.errorbar((expo_edges[:1] + expo_edges[1:]) / 2., expo_histo, fmt="o") plt.xlabel("Time (MJD)") #Save the canvas in the Apperture LightCurve subfolder plt.savefig(LCoutfolder + '/Exposure.png', dpi=150, facecolor='w', edgecolor='w', orientation='portrait', papertype=None, format=None, transparent=False, bbox_inches=None, pad_inches=0.1, frameon=None) plt.figure() plt.errorbar((flux_edges[:1] + flux_edges[1:]) / 2., flux_histo, yerr=dflux_histo, fmt="o") plt.ylabel("Flux") plt.xlabel("Time (MJD)") #Save the canvas in the Apperture LightCurve subfolder plt.savefig(LCoutfolder + '/AppLC.png', dpi=150, facecolor='w', edgecolor='w', orientation='portrait', papertype=None, format=None, transparent=False, bbox_inches=None, pad_inches=0.1, frameon=None)