def history(self,history): """Plot history as a function of epoch for model""" self.msg_svc.DEBUG("DL : Plotting loss as a function of epoch number.") ylabels = {'loss':'Loss','acc':'Accuracy'} for key in ['loss','acc']: fig,ax = plt.subplots() saveAs = "{0}/history_{1}".format(self.output_dir,key) csv = self.plot_history(history,ax=ax,key=key) filename = "{0}.csv".format(saveAs) util.to_csv(filename,csv) ax.set_xlabel('Epoch',fontsize=22,ha='right',va='top',position=(1,0)) ax.set_ylabel(ylabels[key],fontsize=22,ha='right',va='bottom',position=(0,1)) ax.set_xticklabels([self.formatter(i) for i in ax.get_xticks()],fontsize=20) ax.set_yticklabels(['']+[self.formatter(i) for i in ax.get_yticks()[1:-1]]+[''],fontsize=20) ## CMS/COM Energy Label self.stamp_cms(ax) self.stamp_energy(ax) leg = ax.legend(loc=0,numpoints=1,fontsize=12,ncol=1,columnspacing=0.3) leg.draw_frame(False) plt.savefig(self.output_dir+'/{0}_epochs.{1}'.format(key,self.image_format), format=self.image_format,bbox_inches='tight',dpi=200) plt.close() return
def ROC(self,fprs={},tprs={},roc_auc={}): """ Plot the ROC curve & save to text file @param fprs False Positive Rates. Dictionary with each key representing a different class ('signal','background1','background2', etc.). Only one key 'binary' for binary classification (defined in training.py). @param tprs True Positive Rates. Same structure as fprs. @param roc_auc Area under the ROC curves. Dictionary with same keys as fprs/tprs. """ self.msg_svc.DEBUG("DL : Plotting ROC curve.") saveAs = "{0}/roc_curve".format(self.output_dir) ## Use matplotlib directly fig,ax = plt.subplots() # Draw all of the ROC curves from the K-fold cross-validation ax.plot([0,1],[0,1],ls='--',label='No Discrimination',lw=2,c='gray') ax.axhline(y=1,lw=1,c='k',ls='-') # Plot ROC curve keys = fprs.keys() binary = (len(keys)==1) # Only draw one curve for binary classification for k,key in enumerate(keys): label = 'AUC={0:.2f}'.format(roc_auc[key]) if not binary: label = self.sample_labels[key].label+' {0}'.format(label) ax.plot(fprs[key],tprs[key],label=label,lw=2) # save ROC curve to CSV file (to plot later) csv = [ "{0},{1}".format(fp,tp) for fp,tp in zip(fprs[key],tprs[key]) ] util.to_csv("{0}_{1}.csv".format(saveAs,key),csv) ax.set_xlim([0.0, 1.0]) ax.set_ylim([0.0, 1.5]) ax.set_xlabel(r'Background $\epsilon$',ha='right',va='top',position=(1,0)) ax.set_ylabel(r'Signal $\epsilon$',ha='right',va='bottom',position=(0,1)) ax.set_xticklabels([self.formatter(i) for i in ax.get_xticks()],fontsize=20) ax.set_yticklabels([self.formatter(i) for i in ax.get_yticks()],fontsize=20) ## CMS/COM Energy Label self.stamp_cms(ax) self.stamp_energy(ax) leg = ax.legend(fontsize=12,ncol=2) leg.draw_frame(False) plt.savefig('{0}.{1}'.format(saveAs,self.image_format)) plt.close() return
def action_save(self): fname, _ = QFileDialog.getSaveFileName( self, "Save file as...", "/home/tech/workspace/python/magnetic-tools/downloads/", "Data (*.csv)") if fname: to_csv(fname, self.model.fetch_data()) self.status.showMessage(f"Save data to {fname}", 1000) else: self.status.showMessage(f"No save")
def main(q, qp, o): import datetime as dt ans = list() if q is not None and qp is None: pr = [q] elif qp is not None: with open(qp, "r") as f: k = f.read() pr = k.split("\n") else: raise Exception for kk in pr: answer = list() answer.append(util.to_csv(kk, str)) answer.append(util.to_csv(kk.count("0"))) start = dt.datetime.now() strategy = Solver(kk, 2) try: answer.append(util.to_csv(strategy.case_count())) strategy.remove_by_subgroup(strategy.values) answer.append(util.to_csv(strategy.case_count())) # for ii in range(2, 7): # strategy.x_wings(strategy.values, ii) # answer.append(util.to_csv(str(strategy.case_count()))) for ii in range(2, 5): strategy.remove_by_n_association(strategy.values, ii) answer.append(util.to_csv(strategy.case_count())) if strategy.brute_force(strategy.values): a = dt.datetime.now() - start print(a) print(strategy.dict_to_str()) answer.append(util.to_csv(strategy.dict_to_str(), str)) answer.append(util.to_csv(strategy.result_count, str)) print(strategy.result_count) answer.append(util.to_csv(a.microseconds)) ans.append(answer) # strategy.v.show() else: raise exception.NonAnswerException except exception.MultipleAnswerException: print("複数解あります") except exception.NonAnswerException: print("解がありません") with open(o + "ans_log_ans_1.csv", "w") as f: for a in ans: f.write(",".join(a)) f.write("\n")
def loss_history(self, history, kfold=0, val_loss=0.0): """Plot loss as a function of epoch for model""" self.msg_svc.INFO("DL : Plotting loss as a function of epoch number.") saveAs = "{0}/loss_epochs_{1}".format(self.output_dir, self.date) all_histories = type(history) == list # draw the loss curve fig, ax = plt.subplots() # also save the data to a CSV file if all_histories: for i, h in enumerate(history): csv = self.plot_loss_history(h, ax=ax, index=i) filename = "{0}_{1}.csv".format(saveAs, i) util.to_csv(filename, csv) else: csv = self.plot_loss_history(history, ax=ax) filename = "{0}.csv".format(saveAs) util.to_csv(filename, csv) ax.set_xlabel('Epoch', fontsize=22, ha='right', va='top', position=(1, 0)) ax.set_xticklabels(["{0:.1f}".format(i) for i in ax.get_xticks()], fontsize=22) ax.set_ylabel('Loss', fontsize=22, ha='right', va='bottom', position=(0, 1)) ax.set_yticklabels( [''] + ["{0:.1f}".format(i) for i in ax.get_yticks()[1:-1]] + [''], fontsize=22) ## CMS/COM Energy Label cms_stamp = hpl.CMSStamp(self.CMSlabelStatus) cms_stamp.coords = [0.03, 0.97] cms_stamp.fontsize = 18 ax.text(cms_stamp.coords[0], cms_stamp.coords[1], cms_stamp.text, fontsize=cms_stamp.fontsize, ha=cms_stamp.ha, va=cms_stamp.va, transform=ax.transAxes) energy_stamp = hpl.EnergyStamp() energy_stamp.coords = [0.03, 0.90] energy_stamp.fontsize = 18 ax.text(energy_stamp.coords[0], energy_stamp.coords[1], energy_stamp.text, fontsize=energy_stamp.fontsize, ha=energy_stamp.ha, va=energy_stamp.va, transform=ax.transAxes) text_args = { 'ha': 'left', 'va': 'top', 'fontsize': 18, 'transform': ax.transAxes } text = "Validation Loss = {0}; {1} K-folds".format( val_loss, len(history) ) if all_histories else "Validation Loss = {0}".format(val_loss) ax.text(0.03, 0.76, text, **text_args) leg = ax.legend(loc=1, numpoints=1, fontsize=12, ncol=1, columnspacing=0.3) leg.draw_frame(False) f = lambda x, pos: str(x).rstrip('0').rstrip('.') ax.xaxis.set_major_formatter(matplotlib.ticker.FuncFormatter(f)) plt.savefig('{0}.{1}'.format(saveAs, self.image_format), format=self.image_format, bbox_inches='tight', dpi=200) plt.close() return
def ROC(self, fprs=[], tprs=[], accuracy={}): """Plot the ROC curve & save to text file""" self.msg_svc.INFO("DL : Plotting ROC curve.") saveAs = "{0}/roc_curve_{1}".format(self.output_dir, self.date) ## Use matplotlib directly fig, ax = plt.subplots() # Draw all of the ROC curves from the K-fold cross-validation ax.plot([0, 1], [0, 1], ls='--', label='No Discrimination', lw=2, c='gray') ax.axhline(y=1, lw=1, c='lightgray', ls='--') for ft, (fpr, tpr) in enumerate(zip(fprs, tprs)): roc_auc = auc(fpr, tpr) ax.plot(fpr, tpr, label='K-fold {0} (AUC = {1:.2f})'.format(ft, roc_auc), lw=2) # save ROC curve to CSV file (to plot later) outfile_name = "{0}_{1}.csv".format(saveAs, ft) csv = ["{0},{1}".format(fp, tp) for fp, tp in zip(fpr, tpr)] util.to_csv(outfile_name, csv) ax.set_xlim([0.0, 1.0]) ax.set_ylim([0.0, 1.5]) ax.set_xlabel(r'$\epsilon$(anti-top)', fontsize=22, ha='right', va='top', position=(1, 0)) ax.set_xticklabels(["{0:.1f}".format(i) for i in ax.get_xticks()], fontsize=22) ax.set_ylabel(r'$\epsilon$(top)', fontsize=22, ha='right', va='bottom', position=(0, 1)) ax.set_yticklabels( [''] + ["{0:.1f}".format(i) for i in ax.get_yticks()[1:-1]] + [''], fontsize=22) ## CMS/COM Energy Label cms_stamp = hpl.CMSStamp(self.CMSlabelStatus) cms_stamp.coords = [0.03, 0.97] cms_stamp.fontsize = 16 ax.text(cms_stamp.coords[0], cms_stamp.coords[1], cms_stamp.text, fontsize=cms_stamp.fontsize, ha=cms_stamp.ha, va=cms_stamp.va, transform=ax.transAxes) energy_stamp = hpl.EnergyStamp() energy_stamp.coords = [0.03, 0.90] energy_stamp.fontsize = 16 ax.text(energy_stamp.coords[0], energy_stamp.coords[1], energy_stamp.text, fontsize=energy_stamp.fontsize, ha=energy_stamp.ha, va=energy_stamp.va, transform=ax.transAxes) text_args = { 'ha': 'left', 'va': 'top', 'fontsize': 18, 'transform': ax.transAxes } if self.processlabel: ax.text(0.03, 0.82, self.processlabel, **text_args) if accuracy: ax.text( 0.03, 0.75, r"Accuracy = {0:.2f}$\pm${1:.2f}".format( accuracy['mean'], accuracy['std']), **text_args) leg = ax.legend(loc=4, numpoints=1, fontsize=12, ncol=1, columnspacing=0.3) leg.draw_frame(False) plt.savefig('{0}.{1}'.format(saveAs, self.image_format), format=self.image_format, bbox_inches='tight', dpi=300) plt.close() return
def ROC(self, fprs=[], tprs=[], accuracy={}): """Plot the ROC curve & save to text file""" self.msg_svc.INFO("DL : Plotting ROC curve.") saveAs = "{0}/roc_curve_{1}".format(self.output_dir, self.date) ## Use matplotlib directly fig, ax = plt.subplots() # Draw all of the ROC curves from the K-fold cross-validation ax.plot([0, 1], [0, 1], ls='--', label='No Discrimination', lw=2, c='gray') ax.axhline(y=1, lw=1, c='k', ls='-') # Plot ROC curve if isinstance(fprs[0], list): for ft, (fpr, tpr) in enumerate(zip(fprs, tprs)): roc_auc = auc(fpr, tpr) ax.plot(fpr, tpr, label='K-fold {0} (AUC = {1:.2f})'.format(ft, roc_auc), lw=2) # save ROC curve to CSV file (to plot later) csv = ["{0},{1}".format(fp, tp) for fp, tp in zip(fpr, tpr)] util.to_csv("{0}_{1}.csv".format(saveAs, ft), csv) else: roc_auc = auc(fprs, tprs) ax.plot(fprs, tprs, label='AUC = {0:.2f}'.format(roc_auc), lw=2) # save ROC curve to CSV file (to plot later) csv = ["{0},{1}".format(fp, tp) for fp, tp in zip(fprs, tprs)] util.to_csv("{0}.csv".format(saveAs), csv) ax.set_xlim([0.0, 1.0]) ax.set_ylim([0.0, 1.5]) ax.set_xlabel(r'Background', ha='right', va='top', position=(1, 0)) ax.set_ylabel(r'Signal', ha='right', va='bottom', position=(0, 1)) ax.set_xticklabels([self.formatter(i) for i in ax.get_xticks()], fontsize=20) ax.set_yticklabels([self.formatter(i) for i in ax.get_yticks()], fontsize=20) ## CMS/COM Energy Label self.stamp_cms(ax) self.stamp_energy(ax) if accuracy: ax.text(0.97, 0.03, r"Accuracy = {0:.2f}$\pm${1:.2f}".format( accuracy['mean'], accuracy['std']), ha='right') leg = ax.legend() leg.draw_frame(False) plt.savefig('{0}.{1}'.format(saveAs, self.image_format)) plt.close() return