def set_xspec(obsid, model = None, rmffile='/Users/corcoran/Dropbox/nicer_cal/nicer_v0.06.rmf', arffile='/Users/corcoran/Dropbox/nicer_cal/ni_xrcall_onaxis_v0.06.arf', workdir='/Users/corcoran/research/WR140/NICER/work/', ignore='0.0-0.45, 5.-**', showplot=False, showmodel=False ): """ sets the pha and model instances for a given obsid :param obsid: NICER observation id (integer) :param rmffile: NICER response file :param arffile: NICER effective area file :param workdir: NICER dataset working directory :param ignore: energy range in keV to ignore when fitting :param showplot: if True plot the model and spectrum vs. energy :param showmodel: if True print the model parameters :return: """ import xspec import matplotlib as plt obs = str(obsid) xspec.AllData.clear() xspec.AllModels.clear() xspecdir = os.path.join(workdir, obs) phaname = os.path.join(xspecdir, 'ni{obs}_0mpu7_cl.pha'.format(obs=obsid)) print "phaname = {phaname}".format(phaname=phaname) try: pha = xspec.Spectrum(phaname) except: print "Can't find {phaname}; returning".format(phaname=phaname) return 0, 0 pha.response = rmffile pha.response.arf = arffile pha.ignore(ignore) # # Define a model # if not model: model = set_model() if showmodel: with sys_pipes(): model.show() if showplot: xspec.Plot.setRebin(minSig=3, maxBins=30) xspec.Plot.device = "/null" xspec.Plot.xAxis = "keV" xspec.Plot.yLog = "True" xspec.Plot("data") plt.figure(figsize=[10, 6]) plt.yscale('log') plt.xscale('log') plt.xlabel('Energy (keV)', fontsize=16) plt.step(xspec.Plot.x(), xspec.Plot.y()) plt.step(xspec.Plot.x(), xspec.Plot.model()) return pha, model
def plot_gti_lc(obsid, rootdir, flagtype="slow", binwidth=2, ymax=30, save=False, chanmin=None, chanmax=None, figdir='/Users/corcoran/research/WR140/NICER/plots', figsize=[15, 4]): """ plots the lightcurve for the specified obsid for all the gtis """ nobs = nicerObs(obsid, rootdir) gti = nobs.get_gti() fig = plt.figure(figsize=figsize) nrows = int(len(gti) / 2) for i in range(2 * nrows): tstart = gti['START'][i] tstop = gti['STOP'][i] # print tstart,tstop subplot(nrows, 2, i + 1) fig.tight_layout(pad=1.2) ylim(0, ymax) plt.xlabel('Time - {0} (seconds)'.format(tstart), fontsize=16) plt.ylabel('XTI Counts s$^{-1}$', fontsize=16) if chanmin: titl = "{0} {1}-{2}".format(obsid, chanmin, chanmax) else: titl = obsid plt.title(titl) ctsbin, bincen, binwidths = nobs.get_lc(gtinum=i, binwidth=binwidth, flagtype=flagtype, chanmin=chanmin, chanmax=chanmax) plt.step(bincen - gti['START'][i], ctsbin / binwidth) if save: figname = os.path.join(figdir, "{0}_plot_gti.pdf".format(obsid)) plt.savefig(figname)
def save_controls(control_name, fname, action): steps = [ i * environment_configs.get_interval_width() for i in range(action.shape[0]) ] plt.clf() plt.step(steps, action) plt.ylabel(control_name) plt.xlabel('t') plt.savefig(fname + '_' + control_name)
def plot_ecdf(data, name): # sort the values sorted_data = np.sort(data) # eval y n = sorted_data.size F_x = [sorted_data[sorted_data <= x].size / n for x in sorted_data] # plot the plot plt.step(sorted_data, F_x, linewidth=4, label=name) return
def fit_spectrum(obsid, pha, model, writexcm=True, workdir='/Users/corcoran/research/WR140/NICER/work/', statMethod='cstat' ): import xspec from wurlitzer import sys_pipes import pylab as plt from heasarc.utils import xspec_utils as xu if type(pha) != int: # # do fit # xspec.Fit.statMethod = statMethod xspec.Fit.perform() print("Best Fit Model is\n") with sys_pipes(): model.show() pha.notice("0.4-10.0") xspec.Plot.setRebin(minSig=3, maxBins=30) xspec.Plot.device = "/null" xspec.Plot.xAxis = "keV" xspec.Plot.yLog = "True" xspec.Plot("data") plt.figure(figsize=[10, 6]) plt.yscale('log') plt.xscale('log') plt.xlabel('Energy (keV)', fontsize=16) plt.title("{obs}".format(obs=obsid), fontsize=16) plt.errorbar(xspec.Plot.x(), xspec.Plot.y(), xerr=xspec.Plot.xErr(), yerr=xspec.Plot.yErr(), fmt='.') plt.step(xspec.Plot.x(), xspec.Plot.model(), where="mid") band = "0.5-10 keV" xspec.AllModels.calcFlux(band.replace(' keV', '').replace('-',' ')) print "Flux is {0:.3e} in the {1} band".format(pha.flux[0], band) if writexcm: xcmfile = os.path.join(workdir, str(obsid), 'ni{obsid}_0mpu7_cl'.format(obsid=obsid)) xu.write_xcm(xcmfile, pha, model=model) else: print "Can't fit OBSID {obs}".format(obs=obsid) return pha, model
def scree_plot(self): total = sum(self.eig_vals) explained_var = [(i / total) * 100 for i in sorted(self.eig_vals, reverse=True)] cum_var = np.cumsum(explained_var) with plt.style.context('seaborn-darkgrid'): plt.bar(range(len(explained_var)), explained_var, align='center', label='individual explained variance') plt.step(range(len(explained_var)), cum_var, where='mid', label='cumulative explained variance', color="red") plt.ylabel("Cumulative variance") plt.xlabel("Principal components") plt.tight_layout() plt.legend(loc='best') plt.show()
classifyReports[j] = report preRecFSupports[j] = prfs microAverageList = [v["micro"] for v in APList.values()] microAverage = np.mean(microAverageList) print('Average precision score, micro-averaged over all classes: {0:0.2f}'. format(microAverage)) # %% print(classifyReports[2]) # %% print(preRecFSupports[4]) # %% plt.figure() plt.step(recall['micro'], precision['micro'], where='post') plt.xlabel('Recall') plt.ylabel('Precision') plt.ylim([0.0, 1.05]) plt.xlim([0.0, 1.0]) plt.title( 'Average precision score, micro-averaged over all classes: AP={0:0.2f}'. format(average_precision["micro"])) # %% svc.decision_function # %% from itertools import cycle # setup plot details n_classes = 3
# ---------------- plot PRC ---------------- average_precision = average_precision_score(test_label_trans, lp_probs_trans) precision, recall, thresholds = precision_recall_curve(test_label_trans, lp_probs_trans) pos_rate = sum(test_label_trans)/len(test_label_trans) f=plt.figure() matplotlib.rcParams.update({'font.size': 20}) # In matplotlib < 1.5, plt.fill_between does not have a 'step' argument step_kwargs = ({'step': 'post'} if 'step' in signature(plt.fill_between).parameters else {}) plt.step(recall, precision, color='b', alpha=0.2, where='post', label='AP = %0.2f' % average_precision) plt.fill_between(recall, precision, alpha=0.2, color='b', **step_kwargs) plt.plot([0, 1], [pos_rate, pos_rate], 'r--') plt.legend(loc='upper right') plt.xlabel('Recall') plt.ylabel('Precision') plt.ylim([0.0, 1.05]) plt.xlim([0.0, 1.0]) # plt.title('PRC for skip-chain CRF with threads \n AP={0:0.2f}'.format(average_precision)) f.savefig("data_cmv/figures/exp-skip-thread-prc-large-6.pdf", bbox_inches='tight') print(precision) print(recall)