def correlations(): from data_load import gen_models, combine_models models = gen_models(cc = .95, quick = True) c, h, e = combine_models(models) fig, axes = plt.subplots(2, 2, figsize = (12,10), sharex = True, sharey = True) phases = ['lanina', 'neutneg', 'neutpos', 'elnino'] cmap = mpl.cm.get_cmap('viridis') rgba = [cmap(0.2), cmap(0.5), cmap(0.9)] for phase, ax in zip(phases, axes.ravel()): ax.scatter(models[phase].clim_data, models[phase].hindcast, s = 30, color = cmap(0.0)) ax.axis([0,550,0,550]) if phase == 'allyears': title = 'All Years' if phase == 'lanina': title = 'La Nina' if phase == 'elnino': title = 'El Nino' if phase == 'neutpos': title = 'Neutral-Positive' if phase == 'neutneg': title = 'Neutral-Negative' ax.set_title(title, fontweight= 'bold') ax.text(400,20, 'r = %.2f' % models[phase].correlation) fig.text(0.5, 0.04, 'Observed MAMJ Prcp (mm)', ha='center', va = 'center', fontsize = 20) fig.text(0.06, 0.5, 'Hindcast MAMJ Prcp (mm)', ha='center', va = 'center', fontsize = 20, rotation = 'vertical') fig.suptitle('Hindcast Correlations by Phase', fontsize = 24, fontweight = 'bold') #fig.tight_layout() fig.savefig(EV['HOME']+'/Desktop/Feb20Response/images/phase_corr') plt.close(fig) fig, (ax1, ax2) = plt.subplots(1,2, figsize = (12,8), sharey = True) fig = plt.figure(figsize = (12,6)) ax1 = fig.add_axes([.15, .2, .35, 0.6]) ax2 = fig.add_axes([0.55, 0.2, 0.35, 0.6]) ax1.scatter(c, h, s = 30, color = cmap(0.0)) ax1.axis([0, 550, 0, 550]) ax1.set_title('NIPA', fontweight = 'bold') ax1.text(400,20, 'r = %.2f' % np.corrcoef(c, h)[0,1]) ax2.scatter(models['allyears'].clim_data, models['allyears'].hindcast, s= 30, color = cmap(0.0)) ax2.axis([0, 550, 0, 550]) ax2.set_yticks([]) ax2.set_title('All-years', fontweight = 'bold') ax2.text(400, 20, 'r = %.2f' % models['allyears'].correlation) fig.text(0.5, 0.04, 'Observed MAMJ Prcp (mm)', ha='center', va = 'center', fontsize = 20) fig.text(0.06, 0.5, 'Hindcast MAMJ Prcp (mm)', ha='center', va = 'center', fontsize = 20, rotation = 'vertical') fig.suptitle('NIPA vs One-Phase', fontsize = 24, fontweight = 'bold') fig.savefig(EV['HOME'] + '/Desktop/Feb20Response/aycorr') plt.close(fig) return
def nmmecompare(): from nmme import gen_ensembles from data_load import gen_models, combine_models, RPSS, HSS from scipy.stats import gaussian_kde as gk models = gen_models(cc = 0.95, quick = True) clim_data, hindcast, ensemble = combine_models(models) df = gen_ensembles() cmap = mpl.cm.get_cmap('viridis') rgba = [cmap(0.25), cmap(0.7)] idx = np.arange(1921,2011)>=1982 x = 16 dNIPA = gk(ensemble[:,idx][x], bw_method = 0.5) dNMME = gk(df.values[x], bw_method = 0.5) p_x = np.linspace(0, 600, 1000) fig, (ax1, ax2) = plt.subplots(1,2,sharey = True, figsize = (14,10)) ax1.plot(p_x, dNMME.pdf(p_x)*1000, color = rgba[0], label = 'NMME', linewidth = 2) ax1.plot(p_x, dNIPA.pdf(p_x)*1000, color = rgba[1], label = 'NIPA', linewidth = 2) ax1.fill_between(p_x, 0, dNIPA.pdf(p_x)*1000, color = rgba[1], alpha = 0.5) ax1.fill_between(p_x, 0, dNMME.pdf(p_x)*1000, color = rgba[0],alpha = 0.5) ax1.vlines(clim_data[idx][x], 0, 6, linewidth = 4, color = 'k') ax1.set_title(str(clim_data.index.year[idx][x]),fontsize = 18, fontweight='bold') ax1.set_ylabel('Probability Density, $10^{-3}$', fontweight = 'bold') h, l = ax1.get_legend_handles_labels() ax1.legend(h, l) x = 25 dNIPA = gk(ensemble[:,idx][x], bw_method = 0.5) dNMME = gk(df.values[x], bw_method = 0.5) p_x = np.linspace(0, 600, 1000) ax2.plot(p_x, dNMME.pdf(p_x)*1000, color = rgba[0], label = 'NMME', linewidth = 2) ax2.plot(p_x, dNIPA.pdf(p_x)*1000, color = rgba[1], label = 'NIPA', linewidth = 2) ax2.fill_between(p_x, 0, dNIPA.pdf(p_x)*1000, color = rgba[1], alpha = 0.5) ax2.fill_between(p_x, 0, dNMME.pdf(p_x)*1000, color = rgba[0],alpha = 0.5) ax2.vlines(clim_data[idx][x], 0, 6, linewidth = 4, color = 'k') ax2.set_title(str(clim_data.index.year[idx][x]), fontsize = 18, fontweight='bold') fig.text(0.33, 0.015, 'Total MAMJ Precipitation, mm') fig.savefig(EV['HOME'] + '/Desktop/Feb20Response/images/nmmecompare') plt.close(fig) return
def timecompare(): from data_load import gen_models, combine_models models = gen_models(cc = .95, quick = True) c, h, e = combine_models(models) cmap = mpl.cm.get_cmap('viridis') rgba = [cmap(0.0), cmap(0.5), cmap(0.8)] plt.plot(c, label = 'Observations', linewidth = 3, color = 'black') plt.plot(h, label = 'Hindcast', linewidth = 3, color = rgba[1]) plt.plot(models['allyears'].hindcast, label = 'One-Phase', linewidth = 3, color = rgba[2]) plt.legend() plt.ylim(0,600) plt.ylabel('Total MAMJ Precipitaton, mm', fontweight = 'bold') plt.savefig(EV['HOME'] + '/Desktop/Feb20Response/images/time_series') return
def rpssexmp(): from data_load import gen_models, combine_models from scipy.stats import gaussian_kde as gk cmap = mpl.cm.get_cmap('viridis') rgba = [cmap(0.8), cmap(0.2)] models = gen_models() c, h, e = combine_models(models) x, x2 = 15, 20 fig, ax = plt.subplots(1, 1, figsize = (8,6)) ax.set_ylabel('Probability Density, $10^{-3}$') ax.set_xlabel('Total MAMJ Precipitation, mm') dNIPA = gk(e[:, x], bw_method = 0.5) p_x = np.linspace(0,600,1000) ax.plot(p_x, dNIPA.pdf(p_x)*1000, linewidth = 2, color = rgba[0], label = 'Bad') ax.fill_between(p_x, 0, dNIPA.pdf(p_x)*1000, color = rgba[0], alpha = 0.5) dNIPA = gk(e[:, x2], bw_method = 0.5) ax.plot(p_x, dNIPA.pdf(p_x)*1000, linewidth = 2, color = rgba[1], label = 'Worse') ax.fill_between(p_x, 0, dNIPA.pdf(p_x)*1000, color =rgba[1] , alpha = 0.5) ax.vlines(100, 0,8, linewidth = 2, label = 'obs') plt.legend() fig.savefig(EV['HOME'] + '/Desktop/Feb20Response/images/rpss_exmp') return
from data_load import gen_models, combine_models, RPSS, HSS import numpy as np from matplotlib import pyplot as plt models = gen_models(cc = 0.95, bc = 0.80, quick = True) clim_data, hindcast, ensemble = combine_models(models) for x in [10, 20, 30]: print RPSS(clim_data, ensemble, x, x) print HSS(clim_data, hindcast, x, x), '\n'
def terciles(): from data_load import gen_models, combine_models from scipy.stats import gaussian_kde as gk models = gen_models(cc = 0.95, quick = True) c, h, e = combine_models(models) cmap = mpl.cm.get_cmap('viridis') rgba = [cmap(0.0), cmap(0.35), cmap(0.8)] idx = np.argsort(c) B = c[idx][24] A = c[idx][66] x = 20 dNIPA = gk(e[:,x], bw_method = 0.5) dCLIM = gk(c, bw_method = 0.5) bclim = round(dCLIM.integrate_box(0,B),2)* 100 aclim = round(dCLIM.integrate_box(A, 600),2)* 100 nclim = round(dCLIM.integrate_box(B,A),2)* 100 bnipa = round(dNIPA.integrate_box(0,B),2)* 100 anipa = round(dNIPA.integrate_box(A, 600),2)* 100 nnipa = round(dNIPA.integrate_box(B,A),2)* 100 p_x = np.linspace(0,600,1000) below = p_x[p_x<B] middle = p_x[(p_x>=B) & (p_x<=A)] above = p_x[p_x>A] fig, (ax1, ax2) = plt.subplots(1,2,figsize = (12,6), sharey = True) ax1.plot(p_x, dCLIM.pdf(p_x)*1000, linewidth = 2, color = 'k') ax1.fill_between(below, 0, dCLIM.pdf(below)*1000, color = rgba[2], alpha = 0.5) ax1.fill_between(above, 0, dCLIM.pdf(above)*1000, color = rgba[0], alpha = 0.5) ax1.fill_between(middle, 0, dCLIM.pdf(middle)*1000, color = rgba[1], alpha = 0.5) line = '\n\nB: %.0f%%\nN: %.0f%%\nA: %.0f%%' % (bclim, nclim, aclim) ax1.text(400,3,line) ax1.set_title('Climatological PDF', fontsize = 22, fontweight = 'bold') ax1.set_ylabel('Probability Density, $10^{-3}$', fontweight = 'bold') ax2.plot(p_x, dCLIM.pdf(p_x)*1000, linewidth = 2, color = 'grey', alpha = 0.7) ax2.fill_between(p_x, dCLIM.pdf(p_x)*1000, label = 'Climatology', color = 'grey', alpha = 0.2) ax2.plot(p_x, dNIPA.pdf(p_x)*1000, linewidth = 2, color = 'k') ax2.fill_between(below, 0, dNIPA.pdf(below)*1000, color = rgba[2], label = 'Below', alpha = 0.5) ax2.fill_between(middle, 0, dNIPA.pdf(middle)*1000, color = rgba[1], label = 'Normal', alpha = 0.5) ax2.fill_between(above, 0, dNIPA.pdf(above)*1000, color = rgba[0], label = 'Above', alpha = 0.5) line = '\n\nB: %.0f%%\nN: %.0f%%\nA: %.0f%%' % (bnipa, nnipa, anipa) ax2.text(400,3,line) ax2.set_title('Forecast PDF', fontsize = 22, fontweight = 'bold') h, l = ax2.get_legend_handles_labels() fig.legend(h,l, loc = (0.395, 0.55)) fig.text(0.33, 0.015, 'Total MAMJ Precipitation, mm') fig.savefig(EV['HOME'] + '/Desktop/Feb20Response/images/tercile') return