def plot_deviance(sol, save=False, draw=True, save_as_png=True, fig_dpi=144):
if save_as_png:
save_as = 'png'
else:
save_as = 'pdf'
filename = sol.filename.replace("\\", "/").split("/")[-1].split(".")[0]
model = get_model_type(sol)
if draw or save:
fig, ax = plt.subplots(figsize=(4,3))
deviance = sol.MDL.trace('deviance')[:]
sampler_state = sol.MDL.get_state()["sampler"]
x = np.arange(sampler_state["_burn"]+1, sampler_state["_iter"]+1, sampler_state["_thin"])
plt.plot(x, deviance, "-", color="C3", label="Model deviance\nDIC = %.2f\nBPIC = %.2f" %(sol.MDL.DIC,sol.MDL.BPIC))
plt.xlabel("Iteration")
plt.ylabel("Model deviance")
plt.legend(numpoints=1, loc="best")
plt.grid('on')
if sampler_state["_burn"] == 0:
plt.xscale('log')
else:
plt.ticklabel_format(style='sci', axis='x', scilimits=(0,0))
ax.yaxis.set_major_locator(MaxNLocator(integer=True))
fig.tight_layout()
if save:
save_where = '/Figures/ModelDeviance/'
working_path = getcwd().replace("\\", "/")+"/"
save_path = working_path+save_where
print("\nSaving model deviance figure in:\n", save_path)
if not path.exists(save_path):
makedirs(save_path)
fig.savefig(save_path+'ModelDeviance-%s-%s.%s'%(model,filename,save_as), dpi=fig_dpi, bbox_inches='tight')
try: plt.close(fig)
except: pass
if draw: return fig
else: return None
评论列表
文章目录