def plot_polarization_angle(self, show=True, degree=False, **kwargs): """Plot the polarization angle as a function of energy. """ if self.fit_results == []: self.fit() _x = self.emean _dx = [self.emean - self.emin, self.emax - self.emean] if degree: _y = [numpy.degrees(r.phase) for r in self.fit_results] _dy = [numpy.degrees(r.phase_error) for r in self.fit_results] else: _y = [(r.phase) for r in self.fit_results] _dy = [(r.phase_error) for r in self.fit_results] # If there's more than one energy binning we also fit the entire # energy interval, but we don't want the corresponding data point to # appear in the plot, se we brutally get rid of it. if len(self.fit_results) > 1: _x = _x[:-1] _dx = [_x - self.emin[:-1], self.emax[:-1] - _x] _y = _y[:-1] _dy = _dy[:-1] plt.errorbar(_x, _y, _dy, _dx, fmt='o', **kwargs) plt.xlabel('Energy [keV]') if degree: plt.ylabel('Polarization angle [$^\circ$]') else: plt.ylabel('Polarization angle [rad]') if show: plt.show()
def view(): _mcube = xBinnedModulationCube(MCUBE_FILE_PATH) _mcube.fit() _fit_results = _mcube.fit_results[0] plt.figure('Polarization degree') _mcube.plot_polarization_degree(show=False, color='blue') pol_degree_spline.plot(color='lightgray',label='Spin %s'%spindegree, show=False) plt.figtext(0.2, 0.85,'XIPE %s ks'%(SIM_DURATION/1000.),size=18) #plt.errorbar(_energy_mean, _pol_deg, yerr=_pol_deg_err, color='blue',marker='o') plt.legend() plt.figure('Polarization angle') _mcube.plot_polarization_angle(show=False, color='blue', degree=False) pol_angle_spline.plot(color='lightgray',label='Spin %s'%spindegree, show=False) plt.figtext(0.2, 0.85,'XIPE %s ks'%(SIM_DURATION/1000.),size=18) #plt.errorbar(_energy_mean,_pol_angle, yerr= _pol_angle_err,color='blue',marker='o') plt.xlim([1,10]) plt.legend() plt.figure('MDP %s'%base_name) mdp = _mcube.mdp99[:-1] emean = _mcube.emean[:-1] emin = _mcube.emin[:-1] emax = _mcube.emax[:-1] width = (emax-emin)/2. plt.errorbar(emean,mdp,xerr=width, label='MDP99',marker='o',linestyle='--') plt.figtext(0.2, 0.85,'XIPE %s ks'%(SIM_DURATION/1000.),size=18) plt.xlim([1,10]) plt.ylabel('MPD 99\%') plt.xlabel('Energy (keV)') #plt.legend() plt.show()
def plotmdp(): spin00_pol_degree_spline = buildspline(0.5) spin00_mcube = xBinnedModulationCube(fetch_mcubepath(0.5)) spin998_pol_degree_spline = buildspline(0.998) spin998_mcube = xBinnedModulationCube(fetch_mcubepath(0.998)) spin00_mcube.fit() spin998_mcube.fit() spin00_fit_results = spin00_mcube.fit_results[0] spin998_fit_results = spin998_mcube.fit_results[0] plt.figure('MDP') spin00_mdp = spin00_mcube.mdp99[:-1] spin998_mdp = spin998_mcube.mdp99[:-1] emean = spin00_mcube.emean[:-1] emin = spin00_mcube.emin[:-1] emax = spin00_mcube.emax[:-1] width = (emax-emin)/2. plt.errorbar(emean,spin00_mdp,xerr=width, label='Spin 0.5',marker='o',linestyle='--') plt.errorbar(emean,spin998_mdp,xerr=width, label='Spin 0.998',marker='o',linestyle='--') plt.figtext(0.2, 0.85,'XIPE %s ks'%((SIM_DURATION*NUM_RUNS)/1000.),size=18) plt.xlim([1,10]) plt.ylabel('MPD 99\%') plt.xlabel('Energy (keV)') plt.legend() plt.show()
def makeMDPComparisonPlot(imaging_file_path): non_imaging_file_path = imaging_file_path.replace('_imaging.txt','_non_imaging.txt') print "Using %s for non imaging file path"%non_imaging_file_path _phase_ave, _phase_err, mdp_imaging = numpy.loadtxt(imaging_file_path, unpack=True) _phase_ave, _phase_err, mdp_nonimaging = numpy.loadtxt(non_imaging_file_path, unpack=True) scale_factor = 10. print "Improvement with imaging" for i, phase in enumerate(_phase_ave): imaging = 100*mdp_imaging[i]*(1/numpy.sqrt(scale_factor)) non_imaging = 100*mdp_nonimaging[i]*(1/numpy.sqrt(scale_factor)) print "%s\t Imaging (non):%s (%s) %s"%(phase,imaging,non_imaging,non_imaging/imaging) sim_label_imaging = 'XIPE %s ks\n Imaging 15"' % (SIM_DURATION*scale_factor/1000.) sim_label_nonimaging = 'Non imaging' lc_label = 'Light curve' plt.errorbar(_phase_ave, 100*mdp_imaging*(1/numpy.sqrt(scale_factor)),xerr=_phase_err, label=sim_label_imaging,fmt='o',markersize=6) plt.errorbar(_phase_ave, 100*mdp_nonimaging*(1/numpy.sqrt(scale_factor)),xerr=_phase_err, label=sim_label_nonimaging,fmt='v',markersize=6) pl_normalization_spline.plot(scale=10., show=False, color='darkgray',label=lc_label) #on_phase = 0.25, 0.45 #off_phase = 0.45,0.9 plt.axvspan(0.25, 0.45, color='r', alpha=0.45, lw=0) plt.axvspan(0.45, 0.9, color='gray', alpha=0.25, lw=0) plt.ylabel('MDP 99\%') plt.legend() plt.savefig('crab_complex_mdp_imaging_vs_nonimaging_%i_shaded.png'%(SIM_DURATION*scale_factor/1000.)) plt.show()
def plot(self, show=True): """Overloaded plot method. """ plt.errorbar(self.time, self.counts/self.timedel, yerr=self.error/self.timedel, fmt='o') plt.xlabel('Time [s]') plt.ylabel('Rate [Hz]') if show: plt.show()
def plot(self, show=True): """Overloaded plot method. """ plt.errorbar(self.channel, self.rate, yerr=self.error, fmt='o') plt.xlabel('PHA') plt.ylabel('Rate [Hz]') plt.yscale('log') if show: plt.show()
def plot(self, show=True): """Overloaded plot method. """ fig = plt.figure('Phasogram') plt.errorbar(self.phase, self.counts, yerr=self.error, fmt='o') plt.xlabel('Phase') plt.ylabel('Counts/bin') if show: plt.show()
def plot(self, show=True): """Overloaded plot method. """ fig = plt.figure('Phasogram') plt.errorbar(self.phase, self.counts, yerr=self.error, fmt='o') plt.xlabel('Phase') plt.ylabel('Counts/bin') if show: plt.show()
def plot(self, show=True): """Overloaded plot method. """ fig = plt.figure('Count spectrum') plt.errorbar(self.channel, self.rate, yerr=self.error, fmt='o') plt.xlabel('PHA') plt.ylabel('Rate [Hz]') plt.yscale('log') if show: plt.show()
def plot(self, show=True, **kwargs): """Overloaded plot method. """ if not kwargs.has_key('fmt'): kwargs['fmt'] = 'o' plt.errorbar(self.phase, self.counts, yerr=self.error, xerr=0.5*self.phase_delta, **kwargs) plt.xlabel('Phase') plt.ylabel('Counts/bin') if show: plt.show()
def plot(self, show=True): """Overloaded plot method. """ fig = plt.figure('Light curve') plt.errorbar(self.time, self.counts / self.timedel, yerr=self.error / self.timedel, fmt='o') plt.xlabel('Time [s]') plt.ylabel('Rate [Hz]') if show: plt.show()
def plot_polarization_degree(self, show=True, **kwargs): """Plot the polarization degree as a function of energy. """ if self.fit_results == []: self.fit() _x = self.emean _dx = [self.emean - self.emin, self.emax - self.emean] _y = [r.polarization_degree for r in self.fit_results] _dy = [r.polarization_degree_error for r in self.fit_results] plt.errorbar(_x, _y, _dy, _dx, fmt='o', **kwargs) plt.xlabel('Energy [keV]') plt.ylabel('Polarization degree') if show: plt.show()
def plot_polarization_degree(self, show=True, **kwargs): """Plot the polarization degree as a function of energy. """ if self.fit_results == []: self.fit() _x = self.emean _dx = [self.emean - self.emin, self.emax - self.emean] _y = [r.polarization_degree for r in self.fit_results] _dy = [r.polarization_degree_error for r in self.fit_results] plt.errorbar(_x, _y, _dy, _dx, fmt='o', **kwargs) plt.xlabel('Energy [keV]') plt.ylabel('Polarization degree') if show: plt.show()
def test_constant(self, num_events=1000000, polarization_degree=1., polarization_angle=numpy.radians(20.)): """Test the modulation factor as a random number generator when both the polarization angle and degrees are energy- and time-independent. """ poldegree = numpy.full(num_events, polarization_degree) polangle = numpy.full(num_events, polarization_angle) self.modf.generator.plot(show=False) save_current_figure('test_modulation_constant_generator.png', show=self.interactive) emin = self.modf.xmin() emax = self.modf.xmax() energy = numpy.random.uniform(emin, emax, num_events) phi = self.modf.rvs_phi(energy, poldegree, polangle) ebinning = numpy.linspace(emin, emax, 10) phi_binning = numpy.linspace(0, 2*numpy.pi, 100) fit_results = [] for i, (_emin, _emax) in enumerate(zip(ebinning[:-1], ebinning[1:])): _emean = 0.5*(_emin + _emax) _mask = (energy > _emin)*(energy < _emax) _phi = phi[_mask] _hist = plt.hist(_phi, bins=phi_binning, histtype='step') _fr = xAzimuthalResponseGenerator.fit_histogram(_hist) _fr.emean = _emean fit_results.append(_fr) _fr.plot(label='Energy: %.2f--%.2f keV' % (_emin, _emax)) plt.axis([0., 2*numpy.pi, 0., 1.2*_hist[0].max()]) overlay_tag() save_current_figure('test_modulation_constant_fit_slice%d.png' % i, show=self.interactive) _x = [_fr.emean for _fr in fit_results] _y = [_fr.phase for _fr in fit_results] _dy = [_fr.phase_error for _fr in fit_results] plt.errorbar(_x, _y, yerr=_dy, fmt='o') plt.plot(_x, numpy.array([polarization_angle]*len(_x))) plt.xlabel('Energy [keV]') plt.ylabel('Modulation angle [$^\circ$]') save_current_figure('test_modulation_constant_angle.png', show=self.interactive) _y = [_fr.visibility for _fr in fit_results] _dy = [_fr.visibility_error for _fr in fit_results] plt.errorbar(_x, _y, yerr=_dy, fmt='o') plt.axis([emin, emax, 0, 1]) self.modf.plot(show=False) plt.xlabel('Energy [keV]') plt.ylabel('Modulation visibility') save_current_figure('test_modulation_constant_visibility.png', show=self.interactive)
def test_constant(self, num_events=1000000, polarization_degree=1., polarization_angle=numpy.radians(20.)): """Test the modulation factor as a random number generator when both the polarization angle and degrees are energy- and time-independent. """ poldegree = numpy.full(num_events, polarization_degree) polangle = numpy.full(num_events, polarization_angle) self.modf.generator.plot(show=False) save_current_figure('test_modulation_constant_generator.png', show=self.interactive) emin = self.modf.xmin() emax = self.modf.xmax() energy = numpy.random.uniform(emin, emax, num_events) phi = self.modf.rvs_phi(energy, poldegree, polangle) ebinning = numpy.linspace(emin, emax, 10) phi_binning = numpy.linspace(0, 2*numpy.pi, 100) fit_results = [] for i, (_emin, _emax) in enumerate(zip(ebinning[:-1], ebinning[1:])): _emean = 0.5*(_emin + _emax) _mask = (energy > _emin)*(energy < _emax) _phi = phi[_mask] _hist = plt.hist(_phi, bins=phi_binning, histtype='step') _fr = xAzimuthalResponseGenerator.fit_histogram(_hist) _fr.emean = _emean fit_results.append(_fr) _fr.plot(label='Energy: %.2f--%.2f keV' % (_emin, _emax)) plt.axis([0., 2*numpy.pi, 0., 1.2*_hist[0].max()]) overlay_tag() save_current_figure('test_modulation_constant_fit_slice%d.png' % i, show=self.interactive) _x = [_fr.emean for _fr in fit_results] _y = [_fr.phase for _fr in fit_results] _dy = [_fr.phase_error for _fr in fit_results] plt.errorbar(_x, _y, yerr=_dy, fmt='o') plt.plot(_x, numpy.array([polarization_angle]*len(_x))) plt.xlabel('Energy [keV]') plt.ylabel('Modulation angle [$^\circ$]') save_current_figure('test_modulation_constant_angle.png', show=self.interactive) _y = [_fr.visibility for _fr in fit_results] _dy = [_fr.visibility_error for _fr in fit_results] plt.errorbar(_x, _y, yerr=_dy, fmt='o') plt.axis([emin, emax, 0, 1]) self.modf.plot(show=False) plt.xlabel('Energy [keV]') plt.ylabel('Modulation visibility') save_current_figure('test_modulation_constant_visibility.png', show=self.interactive)
def plot(save_plots=False): """ """ sim_label = 'XIPE %s ks' % (SIM_DURATION/1000.) mod_label = 'Input model' _phase, _phase_err, _pol_deg, _pol_deg_err, _pol_angle,\ _pol_angle_err = numpy.loadtxt(ANALYSIS_FILE_PATH, unpack=True) _colors = ['blue']*len(_pol_deg) plt.figure('Polarization degree') _good_fit = _pol_deg > 2*_pol_deg_err _bad_fit = numpy.logical_not(_good_fit) plt.errorbar(_phase[_good_fit], _pol_deg[_good_fit], xerr=_phase_err[_good_fit], yerr=_pol_deg_err[_good_fit], fmt='o', label=sim_label, color='blue') plt.errorbar(_phase[_bad_fit], _pol_deg[_bad_fit], xerr=_phase_err[_bad_fit], yerr=_pol_deg_err[_bad_fit], fmt='o', color='gray') pol_degree_spline.plot(show=False, label=mod_label, color='green') plt.axis([0., 1., 0., 0.1]) plt.legend(bbox_to_anchor=(0.37, 0.95)) plt.figtext(0.6, 0.8, '%.2f--%.2f keV' %\ (E_BINNING[0], E_BINNING[-1]), size=16) if save_plots: plt.savefig('gk_per_polarization_degree.png') plt.figure('Polarization angle') plt.errorbar(_phase[_good_fit], _pol_angle[_good_fit], xerr=_phase_err[_good_fit], yerr=_pol_angle_err[_good_fit], fmt='o', label=sim_label, color='blue') plt.errorbar(_phase[_bad_fit], _pol_angle[_bad_fit], xerr=_phase_err[_bad_fit], yerr=_pol_angle_err[_bad_fit], fmt='o', color='gray') pol_angle_spline.plot(show=False, label=mod_label, color='green', scale=numpy.radians(1.)) plt.axis([0., 1., -0.1, 1.5]) plt.xlabel('Rotational phase') plt.ylabel('Polarization angle [rad]') plt.legend(bbox_to_anchor=(0.37, 0.95)) plt.figtext(0.6, 0.8, '%.2f--%.2f keV' %\ (E_BINNING[0], E_BINNING[-1]), size=16) if save_plots: plt.savefig('gk_per_polarization_angle.png') _ebinning = zip(E_BINNING[:-1], E_BINNING[1:]) if len(_ebinning) > 1: _ebinning.append((E_BINNING[0], E_BINNING[-1])) for i, (_emin, _emax) in enumerate(_ebinning): plt.figure('Phasogram %d' % i) phasogram = xBinnedPhasogram(_phasg_file_path(i)) _scale = phasogram.counts.sum()/phasogram_spline.norm()/\ len(phasogram.counts) phasogram_spline.plot(show=False, label=mod_label, scale=_scale, color='green') phasogram.plot(show=False, color='blue', label=sim_label ) plt.legend(bbox_to_anchor=(0.37, 0.95)) plt.figtext(0.65, 0.8, '%.2f--%.2f keV' % (_emin, _emax), size=16) if save_plots: plt.savefig('gk_per_phasogram_%d.png' % i) plt.show()
def main(): """Test the script Retriving RA, Dec and Index for GRB 130427A """ from ximpol.utils.matplotlib_ import pyplot as plt grb_name = 'GRB 050219' from ximpol.config.grb_swift_download import download_swift_grb_lc_file file_path = download_swift_grb_lc_file(grb_name) if file_path is None: return grb_ra, grb_dec = get_grb_position(file_path) grb_index = get_grb_spec_index(file_path) logger.info('Retriving information for %s:'%grb_name) logger.info('\tPosition: RA = %fdeg, Dec = %fdeg'%(grb_ra,grb_dec)) logger.info('\tSpectral Index = %f '%grb_index) grb_lc = parse_light_curve(file_path) t, tp, tm, f, fp, fm = parse_light_curve_data(file_path) plt.errorbar(t, f, yerr=[fm, fp], fmt='o') grb_lc.plot(logx=True, logy=True)
def plot_polarization_angle(self, show=True, degree=True, **kwargs): """Plot the polarization angle as a function of energy. """ if self.fit_results == []: self.fit() _x = self.emean _dx = [self.emean - self.emin, self.emax - self.emean] if degree: _y = [numpy.degrees(r.phase) for r in self.fit_results] _dy = [numpy.degrees(r.phase_error) for r in self.fit_results] else: _y = [(r.phase) for r in self.fit_results] _dy = [(r.phase_error) for r in self.fit_results] plt.errorbar(_x, _y, _dy, _dx, fmt='o', **kwargs) plt.xlabel('Energy [keV]') plt.ylabel('Polarization angle [$^\circ$]') if show: plt.show()
def plot_polarization_angle(self, show=True, degree=True, **kwargs): """Plot the polarization angle as a function of energy. """ if self.fit_results == []: self.fit() _x = self.emean _dx = [self.emean - self.emin, self.emax - self.emean] if degree: _y = [numpy.degrees(r.phase) for r in self.fit_results] _dy = [numpy.degrees(r.phase_error) for r in self.fit_results] else: _y = [(r.phase) for r in self.fit_results] _dy = [(r.phase_error) for r in self.fit_results] plt.errorbar(_x, _y, _dy, _dx, fmt='o', **kwargs) plt.xlabel('Energy [keV]') plt.ylabel('Polarization angle [$^\circ$]') if show: plt.show()
def plot(save=False): """Plot the stuff in the analysis file. """ sim_label = 'XIPE %s ks' % (SIM_DURATION/1000.) sim_label = 'OBS: %s ks' % (SIM_DURATION/1000.) mod_label = 'Input model' lc_label = 'Light curve' _phase, _phase_err, _pol_deg, _pol_deg_err, _pol_angle,\ _pol_angle_err, _index, _index_err, _norm,\ _norm_err = numpy.loadtxt(ANALYSIS_FILE_PATH, unpack=True) plt.figure('Polarization degree') pl_normalization_spline.plot(scale=0.12, show=False, color='lightgray', label=lc_label) _pol_deg_err_p=numpy.where((_pol_deg+_pol_deg_err)<1.0,_pol_deg_err,1.0-_pol_deg) _pol_deg_err_m=numpy.where((_pol_deg-_pol_deg_err)>0.0,_pol_deg_err,_pol_deg) #if (_pol_deg+_pol_deg_err)>1.0: _pol_deg_err=1.0-yerr_p plt.errorbar(_phase, _pol_deg, xerr=_phase_err, yerr=[_pol_deg_err_m,_pol_deg_err_p], fmt='o', label=sim_label) pol_degree_spline.plot(show=False, label=mod_label) plt.axis([0., 1., 0., 0.5]) plt.legend(bbox_to_anchor=(0.45, 0.95)) if save: save_current_figure('crab_polarization_degree', OUTPUT_FOLDER, False) plt.figure('Polarization angle') pl_normalization_spline.plot(scale=0.4, offset=1.25, show=False, color='lightgray', label=lc_label) plt.errorbar(_phase, _pol_angle, xerr=_phase_err, yerr=_pol_angle_err, fmt='o', label=sim_label) pol_angle_spline.plot(show=False, label=mod_label) plt.axis([0., 1., 1.25, 3.]) plt.legend(bbox_to_anchor=(0.45, 0.95)) if save: save_current_figure('crab_polarization_angle', OUTPUT_FOLDER, False) plt.figure('PL normalization') plt.errorbar(_phase, _norm, xerr=_phase_err, yerr=_norm_err, fmt='o', label=sim_label) pl_normalization_spline.plot(show=False, label=mod_label) plt.axis([0., 1., None, None]) plt.legend(bbox_to_anchor=(0.45, 0.95)) if save: save_current_figure('crab_pl_norm', OUTPUT_FOLDER, False) plt.figure('PL index') pl_normalization_spline.plot(scale=0.18, offset=1.3, show=False, color='lightgray', label=lc_label) plt.errorbar(_phase, _index, xerr=_phase_err, yerr=_index_err, fmt='o', label=sim_label) pl_index_spline.plot(show=False, label=mod_label) plt.axis([0., 1., 1.3, 2.1]) plt.legend(bbox_to_anchor=(0.45, 0.95)) if save: save_current_figure('crab_pl_index', OUTPUT_FOLDER, False) plt.show()
def plot_bin(self, i, show=True, fit=True): """Plot the azimuthal distribution for the i-th energy slice. """ _emin = self.emin[i] _emax = self.emax[i] _emean = self.emean[i] label = '%.2f-%.2f $<$%.2f$>$ keV' % (_emin, _emax, _emean) plt.errorbar(self.phi_x, self.phi_y[i], yerr=numpy.sqrt(self.phi_y[i]), fmt='o') if fit: fit_results = self.fit_bin(i) fit_results.plot(label=label) plt.axis([0., 2*numpy.pi, 0.0, 1.2*self.phi_y[i].max()]) plt.xlabel('Azimuthal angle [rad]') plt.ylabel('Counts/bin') plt.text(0.02, 0.92, label, transform=plt.gca().transAxes, fontsize=15) if show: plt.show()
def makeMDP_fE_ComparisonPlot(file_path): scale_factor = 10. (_phase_ave, _phase_err, _energy_mean, pulsar_e_err, pulsar_e_err, mdp) =\ numpy.loadtxt(file_path, unpack=True) print "Phase ave:",_phase_ave print print "Energy mean", _energy_mean #phase_values = [0.025, 0.15, 0.35, 0.675, 0.95] phase_values = [0.35,0.675] on, on_phase_color = (0.35,'r') off, off_phase_color = (0.675,'gray') #for phase in phase_values: plt.errorbar(_energy_mean[_phase_ave==on], 100*mdp[_phase_ave==on]*(1/numpy.sqrt(scale_factor)),xerr=pulsar_e_err[_phase_ave==on], label='On Phase',fmt='o',markersize=6,ls='--',color=on_phase_color) plt.errorbar(_energy_mean[_phase_ave==off], 100*mdp[_phase_ave==off]*(1/numpy.sqrt(scale_factor)),xerr=pulsar_e_err[_phase_ave==off], label='Off Phase',fmt='o',markersize=6,ls='--',color=off_phase_color) plt.legend() plt.ylabel('MPD 99\%') plt.xlabel('Energy (keV)') plt.savefig('crab_complex_mdp_imaging_fE_%i.png'%(SIM_DURATION*scale_factor/1000.)) plt.show()
def plot_bin(self, i, show=True, fit=True): """Plot the azimuthal distribution for the i-th energy slice. """ _emin = self.emin[i] _emax = self.emax[i] _emean = self.emean[i] label = '%.2f-%.2f $<$%.2f$>$ keV' % (_emin, _emax, _emean) plt.errorbar(self.phi_x, self.phi_y[i], yerr=numpy.sqrt(self.phi_y[i]), fmt='o') if fit: fit_results = self.fit_bin(i) fit_results.plot(label=label) view_range = 1.5 * (self.phi_y[i].max() - self.phi_y[i].min()) view_ymin = self.phi_y[i].min() - view_range view_ymax = self.phi_y[i].max() + view_range plt.axis([0., 2 * numpy.pi, view_ymin, view_ymax]) plt.xlabel('Azimuthal angle [rad]') plt.ylabel('Counts/bin') plt.text(0.02, 0.92, label, transform=plt.gca().transAxes, fontsize=15) if show: plt.show()
def view(): #_energy_mean,_emin, _emax, _pol_deg, _pol_deg_err, _pol_angle, \ # _pol_angle_err = \ # # numpy.loadtxt(ANALYSIS_FILE_PATH, unpack=True) _mcube = xBinnedModulationCube(MCUBE_FILE_PATH) _mcube.fit() _fit_results = _mcube.fit_results[0] plt.figure('Polarization degree') _mcube.plot_polarization_degree(show=False, color='blue') pol_degree_spline.plot(color='lightgray',label='Model %s corona'%model_type, show=False) plt.figtext(0.2, 0.85,'XIPE %s ks'%(SIM_DURATION/1000.),size=18) #plt.errorbar(_energy_mean, _pol_deg, yerr=_pol_deg_err, color='blue',marker='o') plt.legend() plt.figure('Polarization angle') _mcube.plot_polarization_angle(show=False, color='blue', degree=False) pol_angle_spline.plot(color='lightgray',label='Model %s corona'%model_type, show=False) plt.figtext(0.2, 0.85,'XIPE %s ks'%(SIM_DURATION/1000.),size=18) #plt.errorbar(_energy_mean,_pol_angle, yerr= _pol_angle_err,color='blue',marker='o') plt.legend() plt.figure('MDP %s'%base_name) mdp = _mcube.mdp99 emean = _mcube.emean emin = _mcube.emin emax = _mcube.emax width = (emax-emin)/2. plt.errorbar(emean,mdp,xerr=width, label='MDP99',marker='o',linestyle='--') plt.figtext(0.2, 0.85,'XIPE %s ks'%(SIM_DURATION/1000.),size=18) plt.xlim([1,10]) plt.ylabel('MPD 99 %') plt.xlabel('Energy (keV)') #plt.legend() plt.show()
def plot(save=False): """Plot the stuff in the analysis file. """ for j in range(len(E_BINNING)): if (j==len(E_BINNING)-1): analysis_file = ANALYSIS_FILE_PATH emin=E_BINNING[0] emax=E_BINNING[-1] else: analysis_file = '%s_%d' % (ANALYSIS_FILE_PATH,j) emin=E_BINNING[j] emax=E_BINNING[j+1] sim_label = 'XIPE %s ks' % (SIM_DURATION/1000.) mod_label = 'Input model' lc_label = 'Light curve' _phase, _phase_err, _pol_deg, _pol_deg_err, _pol_angle,\ _pol_angle_err, _index, _index_err, _norm,\ _norm_err = numpy.loadtxt(ANALYSIS_FILE_PATH, unpack=True) plt.figure('Polarization degree (%g-%g keV)' % (emin,emax)) plt.title('Her X-1 (%g-%g keV)' % (emin,emax)) pl_normalization_spline.plot(scale=20, show=False, color='lightgray', label=lc_label) plt.errorbar(_phase, _pol_deg, xerr=_phase_err, yerr=_pol_deg_err, fmt='o', label=sim_label) pol_degree_spline.plot(show=False, label=mod_label) # pol_degree_spline_noqed.plot(show=False, label='No QED') plt.axis([0., 1., 0., 1.1]) plt.legend(bbox_to_anchor=(0.4, 0.5)) if save: save_current_figure('herx1_polarization_degree_%d' % j, OUTPUT_FOLDER, False) plt.figure('Polarization angle (%g-%g keV)' % (emin,emax)) plt.title('Her X-1 (%g-%g keV)' % (emin,emax)) pl_normalization_spline.plot(scale=60, offset=0, show=False, color='lightgray', label=lc_label) plt.errorbar(_phase, _pol_angle, xerr=_phase_err, yerr=_pol_angle_err, fmt='o', label=sim_label) pol_angle_spline.plot(show=False, label=mod_label) plt.axis([0., 1., 0, 3.15]) plt.legend(bbox_to_anchor=(0.4, 0.3)) if save: save_current_figure('herx1_polarization_angle_%d' %j, OUTPUT_FOLDER, False) plt.figure('Flux (%g-%g keV)' % (emin,emax)) plt.title('Her X-1 (%g-%g keV)' % (emin,emax)) plt.errorbar(_phase, 0.21*_norm/(2-_index)*(10.**(2.0-_index)-2.**(2.-_index)), xerr=_phase_err, yerr=0.21*_norm_err/(2-_index)*(10.**(2-_index)-2.**(2.-_index)), fmt='o', label=sim_label) pl_normalization_spline.plot(scale=1,show=False, label=mod_label) plt.axis([0., 1., None, None]) plt.legend(bbox_to_anchor=(0.75, 0.95)) if save: save_current_figure('herx1_flux_%d' % j, OUTPUT_FOLDER, False) plt.show()
def plot(save=False): """Plot the stuff in the analysis file. """ sim_label = 'XIPE %s ks' % (SIM_DURATION/1000.) mod_label = 'Input model' lc_label = 'Light curve' _phase, _phase_err, _pol_deg, _pol_deg_err, _pol_angle,\ _pol_angle_err, _index, _index_err, _norm,\ _norm_err = numpy.loadtxt(ANALYSIS_FILE_PATH, unpack=True) plt.figure('Polarization degree') pl_normalization_spline.plot(scale=20, show=False, color='lightgray', label=lc_label) plt.errorbar(_phase, _pol_deg, xerr=_phase_err, yerr=_pol_deg_err, fmt='o', label=sim_label) pol_degree_spline.plot(show=False, label='QED-On') pol_degree_spline_noqed.plot(show=False, offset=0.03, scale=1.06, label='QED-Off') plt.axis([0., 1., 0., 1.1]) plt.legend(bbox_to_anchor=(0.4, 0.6)) if save: save_current_figure('4u_polarization_degree', OUTPUT_FOLDER, False) plt.figure('Polarization angle') pl_normalization_spline.plot(scale=60, offset=0, show=False, color='lightgray', label=lc_label) plt.errorbar(_phase, _pol_angle, xerr=_phase_err, yerr=_pol_angle_err, fmt='o', label=sim_label) pol_angle_spline.plot(show=False, label=mod_label) plt.axis([0., 1., 0, 3.15]) plt.legend(bbox_to_anchor=(0.4, 0.3)) if save: save_current_figure('4u_polarization_angle', OUTPUT_FOLDER, False) plt.figure('Flux (2-10 keV)') plt.errorbar(_phase, 0.21*_norm/(2-_index)*(10.**(2.0-_index)-2.**(2.-_index)), xerr=_phase_err, yerr=0.21*_norm_err/(2-_index)*(10.**(2-_index)-2.**(2.-_index)), fmt='o', label=sim_label) pl_normalization_spline.plot(scale=1,show=False, label=mod_label) plt.axis([0., 1., None, None]) plt.legend(bbox_to_anchor=(0.75, 0.95)) if save: save_current_figure('4u_flux', OUTPUT_FOLDER, False) plt.show()
pol_deg = numpy.mean(pol_degree_array, axis=0) pol_deg_err = numpy.mean(pol_degree_error_array, axis=0) pol_ang = numpy.mean(pol_angle_array, axis=0) pol_ang_err = numpy.mean(pol_angle_error_array, axis=0) fig = plt.figure('Polarization degree') plt.xlabel('Energy [keV]') plt.ylabel('Polarization degree') bad = pol_deg < 3*pol_deg_err good = numpy.invert(bad) bad[len(E_BINNING)-1] = False good[len(E_BINNING)-1] = False _dx = numpy.array([mod_cube.emean - mod_cube.emin, mod_cube.emax - mod_cube.emean]) if bad.sum() > 0: plt.errorbar(mod_cube.emean[bad], pol_deg[bad], pol_deg_err[bad], _dx.T[bad].T, fmt='o', label='Data', color='gray') if good.sum() > 0: plt.errorbar(mod_cube.emean[good], pol_deg[good], pol_deg_err[good], _dx.T[good].T, fmt='o', label='Data', color='blue') pol_degree.plot(show=False, label='Model', linestyle='dashed', color='green') plt.legend(bbox_to_anchor=(0.30, 0.95)) plt.axis([1, 10, 0, 0.1]) #plt.savefig('/home/nicco/%s_0_0_10x%dMs_polarization_degree.png' %\ # (NAME_LIST[0:3], TIME/100000)) fig = plt.figure('Polarization angle') plt.xlabel('Energy [keV]') plt.ylabel('Polarization angle [$^\circ$]') if bad.sum() > 0: plt.errorbar(mod_cube.emean[bad], pol_ang[bad], pol_ang_err[bad], _dx.T[bad].T, fmt='o', label='Data', color='gray')
def calcMDP(plot=False): mdp_file = open(MDP_OUTPUT_FILE,'w') mdp_file.write('#Simulation time %s sec \n'%SIM_DURATION) mdp_file.write('#Phase Ave delta phase Mean energy delta energy mdp99\n') nebula_mcube_file = xBinnedModulationCube(NEBULA_MCUBE_FILE_PATH) nebula_counts = nebula_mcube_file.counts nebula_mdp = nebula_mcube_file.mdp99 txt = "Pulsar phase\t Emin - Emax\t Pulsar counts\t Nebula counts\t MDP\n" MDP99_PULSAR = [] phase = [] phase_err = [] for i, (_min, _max) in enumerate(PHASE_BINS): #zip(PHASE_BINS[:-1],PHASE_BINS[1:])): pulse_diff = numpy.fabs(_max -_min) _phase_ave = 0.5*(_min + _max) phase.append(_phase_ave) _phase_err = 0.5*(_max - _min) phase_err.append(_phase_err) pulsar_phase_mcube_file = xBinnedModulationCube(_mcube_file_path(i)) pulsar_emean = pulsar_phase_mcube_file.emean for j, _energy_mean in enumerate(pulsar_emean): pulsar_emin = pulsar_phase_mcube_file.emin[j] pulsar_emax = pulsar_phase_mcube_file.emax[j] pulsar_e_err = 0.5*(pulsar_emax-pulsar_emin) pulsar_phase_counts = pulsar_phase_mcube_file.counts[j] pulsar_mdp = pulsar_phase_mcube_file.mdp99[j] #scale the nebula counts for the time used for the pulsar phase scaled_nebula_counts = pulse_diff*nebula_counts[j] count_sqrt = numpy.sqrt(pulsar_phase_counts + scaled_nebula_counts) eff_mu_pulsar = pulsar_phase_mcube_file.effective_mu[j] mdp = 4.292/eff_mu_pulsar*count_sqrt/pulsar_phase_counts MDP99_PULSAR.append(100*mdp) _data = (_phase_ave, _phase_err, _energy_mean, pulsar_e_err, pulsar_e_err, mdp) _fmt = ('%.4e ' * len(_data)).strip() _fmt = '%s\n' % _fmt _line = _fmt % _data mdp_file.write(_line) #txt += "%s\t %s - %s\t %s\t %s\t %.3f\n"%(PHASE_BINS[i], pulsar_emin[0], pulsar_emax[0], pulsar_phase_counts[0], scaled_nebula_counts[0], 100*mdp) MDP99_PULSAR = numpy.array(MDP99_PULSAR) PHASE = numpy.array(phase) mdp_file.close() if plot: scale_factor = 10 sim_label = 'XIPE %s ks' % (SIM_DURATION*scale_factor/1000.) lc_label = 'Light curve' plt.errorbar(PHASE, MDP99_PULSAR*(1/numpy.sqrt(10)),xerr=phase_err, label=sim_label,fmt='o') pl_normalization_spline.plot(scale=10., show=False, color='lightgray',label=lc_label) plt.ylabel('MDP 99\%') plt.legend() plt.savefig('crab_complex_mdp_nonimaging_%i.png'%(SIM_DURATION*scale_factor/1000.)) #plt.show() print txt
def plot(save=False): """Plot the stuff in the analysis file. """ def draw_time_grid(color='blue'): """ """ times = [3600., 3600.*24., 3600.*24.*7.] labels = ['1 hour', '1 day', '1 week'] for t, l in zip(times, labels): plt.axvline(t, linestyle='dashed', color=color) ymin, ymax = plt.gca().get_ylim() y = ymin + 1.05*(ymax - ymin) plt.text(t, y, l, ha='center', color=color) sim_label = 'XIPE' mod_label = 'Input model' lc_label = 'Light curve' _time, _time_errp, _time_errm, _pol_deg, _pol_deg_err, _pol_angle,\ _pol_angle_err, _index, _index_err, _norm,\ _norm_err = numpy.loadtxt(ANALYSIS_FILE_PATH, unpack=True) logger.info(_time) logger.info((_time_errp + _time_errm)/3600.) _pol_angle = numpy.degrees(_pol_angle) _pol_angle_err = numpy.degrees(_pol_angle_err) plt.figure('Polarization degree') pol_degree_spline.plot(show=False, label=mod_label, logx=True) plt.errorbar(_time, _pol_deg, xerr=[_time_errm, _time_errp], yerr=_pol_deg_err, fmt='o', label=sim_label) plt.axis([100., 1e6, 0., 0.6]) plt.legend(bbox_to_anchor=(0.4, 0.95)) draw_time_grid() if save: save_current_figure('grb130427_swift_polarization_degree', OUTPUT_FOLDER, False) plt.figure('Polarization angle') _x = pol_degree_spline.x _y = numpy.full(len(_x), polarization_angle(_x, None, None, None)) _y = numpy.degrees(_y) fmt = dict(xname='Time', xunits='s', yname='Polarization angle', yunits=r'$^\circ$') _s = xInterpolatedUnivariateSpline(_x, _y, **fmt) _s.plot(logx=True, show=False, label=mod_label) plt.errorbar(_time, _pol_angle, xerr=[_time_errm, _time_errp], yerr=_pol_angle_err, fmt='o', label=sim_label) plt.axis([100., 1e6, None, None]) plt.legend(bbox_to_anchor=(0.4, 0.95)) draw_time_grid() if save: save_current_figure('grb130427_swift_polarization_angle', OUTPUT_FOLDER, False) plt.figure('PL index') _y = numpy.full(len(_x), PL_INDEX) fmt = dict(xname='Time', xunits='s', yname='PL index') _s = xInterpolatedUnivariateSpline(_x, _y, **fmt) _s.plot(logx=True, show=False, label=mod_label) plt.errorbar(_time, _index, xerr=[_time_errm, _time_errp], yerr=_index_err, fmt='o', label=sim_label) plt.axis([100., 1e6, None, None]) plt.legend(bbox_to_anchor=(0.4, 0.95)) draw_time_grid() if save: save_current_figure('grb130427_swift_pl_index', OUTPUT_FOLDER, False) #plt.figure('PL normalization') #plt.errorbar(_time, _norm, xerr=[_time_errm, _time_errp], yerr=_norm_err, # fmt='o', label=sim_label) #pl_normalization_spline.plot(show=False, label=mod_label) #plt.axis([100., 1e6, None, None]) #plt.legend(bbox_to_anchor=(0.4, 0.95)) #draw_time_grid() #if save: # save_current_figure('grb130427_swift_pl_norm', OUTPUT_FOLDER, False) plt.figure('Light curve') lc = xBinnedLightCurve(_lc_file_path()) lc.plot(show=False) # This should be implemented in the binned LC class. plt.xscale('log') plt.yscale('log') plt.axis([100., 1e6, None, None]) draw_time_grid() if save: save_current_figure('grb130427_swift_lc', OUTPUT_FOLDER, False) plt.show()
def plot(save=False): """Plot the stuff in the analysis file. """ def draw_time_grid(color='blue'): """ """ times = [3600., 3600. * 24., 3600. * 24. * 7.] labels = ['1 hour', '1 day', '1 week'] for t, l in zip(times, labels): plt.axvline(t, linestyle='dashed', color=color) ymin, ymax = plt.gca().get_ylim() y = ymin + 1.05 * (ymax - ymin) plt.text(t, y, l, ha='center', color=color) sim_label = 'XIPE' mod_label = 'Input model' lc_label = 'Light curve' _time, _time_errp, _time_errm, _pol_deg, _pol_deg_err, _pol_angle,\ _pol_angle_err, _index, _index_err, _norm,\ _norm_err = numpy.loadtxt(ANALYSIS_FILE_PATH, unpack=True) logger.info(_time) logger.info((_time_errp + _time_errm) / 3600.) _pol_angle = numpy.degrees(_pol_angle) _pol_angle_err = numpy.degrees(_pol_angle_err) plt.figure('Polarization degree') pol_degree_spline.plot(show=False, label=mod_label, logx=True) plt.errorbar(_time, _pol_deg, xerr=[_time_errm, _time_errp], yerr=_pol_deg_err, fmt='o', label=sim_label) plt.axis([100., 1e6, 0., 0.6]) plt.legend(bbox_to_anchor=(0.4, 0.95)) draw_time_grid() if save: save_current_figure('grb130427_swift_polarization_degree', OUTPUT_FOLDER, False) plt.figure('Polarization angle') _x = pol_degree_spline.x _y = numpy.full(len(_x), polarization_angle(_x, None, None, None)) _y = numpy.degrees(_y) fmt = dict(xname='Time', xunits='s', yname='Polarization angle', yunits=r'$^\circ$') _s = xInterpolatedUnivariateSpline(_x, _y, **fmt) _s.plot(logx=True, show=False, label=mod_label) plt.errorbar(_time, _pol_angle, xerr=[_time_errm, _time_errp], yerr=_pol_angle_err, fmt='o', label=sim_label) plt.axis([100., 1e6, None, None]) plt.legend(bbox_to_anchor=(0.4, 0.95)) draw_time_grid() if save: save_current_figure('grb130427_swift_polarization_angle', OUTPUT_FOLDER, False) plt.figure('PL index') _y = numpy.full(len(_x), PL_INDEX) fmt = dict(xname='Time', xunits='s', yname='PL index') _s = xInterpolatedUnivariateSpline(_x, _y, **fmt) _s.plot(logx=True, show=False, label=mod_label) plt.errorbar(_time, _index, xerr=[_time_errm, _time_errp], yerr=_index_err, fmt='o', label=sim_label) plt.axis([100., 1e6, None, None]) plt.legend(bbox_to_anchor=(0.4, 0.95)) draw_time_grid() if save: save_current_figure('grb130427_swift_pl_index', OUTPUT_FOLDER, False) #plt.figure('PL normalization') #plt.errorbar(_time, _norm, xerr=[_time_errm, _time_errp], yerr=_norm_err, # fmt='o', label=sim_label) #pl_normalization_spline.plot(show=False, label=mod_label) #plt.axis([100., 1e6, None, None]) #plt.legend(bbox_to_anchor=(0.4, 0.95)) #draw_time_grid() #if save: # save_current_figure('grb130427_swift_pl_norm', OUTPUT_FOLDER, False) plt.figure('Light curve') lc = xBinnedLightCurve(_lc_file_path()) lc.plot(show=False) # This should be implemented in the binned LC class. plt.xscale('log') plt.yscale('log') plt.axis([100., 1e6, None, None]) draw_time_grid() if save: save_current_figure('grb130427_swift_lc', OUTPUT_FOLDER, False) plt.show()