def Plot_Lorenz96(savepath, tag='', picformat='png'): """Generates and saves the result figure for Lorenz in the paper.""" # first the random phase model plt.rc('font', family='serif', size=9) tfs = 10 MyColors = ['#377eb8', '#d95f02'] # funcion that designates the position of axes def CreateAxes(i, j, myfig): # i,j are the row column indices of axes from top left # xleft, ybottom h, w, dw, dh = .2, .2, .07, .09 x0, y0 = 0.17, .68 xl = x0 + (j - 1) * (w + dw) yl = y0 - (i - 1) * (h + dh) my_axes = myfig.add_axes([xl, yl, w, h]) plt.tick_params(direction='in') return my_axes # truth, random phase model and SDE model data = sio.loadmat('./results/Lorenz_RPM') x, x_qp, dt = np.squeeze(data['x']), np.squeeze(data['x_qp']), np.squeeze( data['dt']) data2 = sio.loadmat('./results/LorenzModel_r3_t1000_ns1.mat') x_train, x_model, t_model = data2['x_train'], data2['x_model'], data2[ 't_model'] # we use the training data as truth x = x_train[:, 0] x_mean = np.mean(x) nt = 201 t = np.arange(0, nt) * dt fig = plt.figure(figsize=[6.5, 6.5 * .66]) yls = [-12, 14] # ylim for signals pls = [-11, 14] # signals ax1 = CreateAxes(1, 1, fig) ax1.plot(t, x[2000:2000 + nt], color=MyColors[0]) ax1.set_xticks(np.arange(0, 21, 5)) ax1.set_xlim(0, 20), ax1.set_ylim(yls) ax1.set_title(r'signal', fontsize=tfs) ax4 = CreateAxes(2, 1, fig) ax4.plot(t, x_model[0:nt, 0], color=MyColors[0]) ax4.set_xticks(np.arange(0, 21, 5)) ax4.set_xlim(0, 20), ax4.set_ylim(yls) ax7 = CreateAxes(3, 1, fig) ax7.plot(t, x_qp[2000:2000 + nt], color=MyColors[0]) ax7.set_xticks(np.arange(0, 21, 5)) ax7.set_xlim(0, 20), ax7.set_ylim(yls) ax7.set_xlabel(r'$t$') # tags xt = -10 ax1.text(xt, 1, r'truth', horizontalalignment='center', verticalalignment='center', FontSize=tfs) ax4.text(xt, 1, r'SDE model', horizontalalignment='center', verticalalignment='center', FontSize=tfs) ax7.text(xt, 1, r'phase model', horizontalalignment='center', verticalalignment='center', FontSize=tfs) # PSD ax2 = CreateAxes(1, 2, fig) wr, pr = sa.Welch_estimator(x - x_mean, M=100, L=200, fs=1 / dt) ax2.plot(wr, pr, 'k', label=r'truth') ax2.set_xlim(0, np.pi / dt) ax2.set_title(r'PSD', fontsize=tfs) ax2.set_xticks([0, 15, 30]) ax2 = CreateAxes(2, 2, fig) wm, pm = sa.Welch_estimator(x_model[:, 0] - x_mean, M=100, L=200, fs=1 / dt) ax2.plot(wm, pm, 'k') ax2.plot(wr, pr, '--', color='gray', label='truth') ax2.set_xlim(0, np.pi / dt) ax2.set_xticks([0, 15, 30]) legend = plt.legend(fontsize=tfs - 1, bbox_to_anchor=(1.03, .84), loc='center right', ncol=1, fancybox=True, framealpha=0) legend.get_frame().set_linewidth(0) ax2 = CreateAxes(3, 2, fig) wm, pm = sa.Welch_estimator(x_qp, M=100, L=200, fs=1 / dt) ax2.plot(wm, pm, 'k') ax2.plot(wr, pr, '--', color='gray') ax2.set_xlim(0, np.pi / dt) ax2.set_xticks([0, 15, 30]) ax2.set_xlabel(r'$\omega$') # PDF ax3 = CreateAxes(1, 3, fig) [xp, pp] = pt.pdf_1d(x, nx=100, smoothing_sigma=2) ax3.plot(xp, pp, 'k') ax3.set_xlim(pls), ax3.set_ylim(0, .11) ax3.set_yticks(np.arange(0, .11, .1)), ax3.set_xticks([-10, 0, 10]) ax3.set_title(r'PDF', fontsize=tfs) ax3 = CreateAxes(2, 3, fig) [xm, pm] = pt.pdf_1d(x_model[:, 0], nx=100, smoothing_sigma=2) ax3.plot(xm, pm, 'k') ax3.plot(xp, pp, '--', color='gray') ax3.set_xlim(pls), ax3.set_ylim(0, .11) ax3.set_yticks(np.arange(0, .11, .1)), ax3.set_xticks([-10, 0, 10]) ax3 = CreateAxes(3, 3, fig) [xm, pm] = pt.pdf_1d(x_qp + x_mean, nx=100, smoothing_sigma=2) ax3.plot(xm, pm, 'k') ax3.plot(xp, pp, '--', color='gray') ax3.set_xlim(pls), ax3.set_ylim(0, .11) ax3.set_yticks(np.arange(0, .11, .1)), ax3.set_xticks([-10, 0, 10]) plt.savefig(savepath + 'Lorenz96.' + picformat, dpi=400)
def Plot_tails_wCI(savepath, tag='', picformat='png'): plt.rc('font', family='serif', size=9) tfs = 10 MyColors = ['#377eb8', '#d95f02'] lw0, lw1, lw2 = 1.5, 1.2, 1.2 # smoothing for pdfs? F = 2 #std of gaussian kernel in convolution # axes sizes myfig = plt.figure(figsize=[6.5, 2.4]) w = .16 h = .3 dh, dw = .11, .02 x0, y0 = .11, .89 - h # data tags Setup, years_train, polynomial_order, start_year = 21, 2, 2, 0 # this relates to how correlated variables are chosen ############# # temperature idx_target = 2 tdata = sio.loadmat('./results/NorthPole_var' + str(idx_target) + '_r' + str(polynomial_order) + '_tyrs' + str(years_train) + '_syrs' + str(start_year)) y_truth, y_train, y_model = np.squeeze(tdata['y_truth']), np.squeeze( tdata['y_train']), tdata['ys_model'] r_truth, p_truth, p_truth_u, p_truth_l = pt.pdf_1d_wCI(y_truth, nx=100, smoothing_sigma=F) r_train, p_train, p_train_u, p_train_l = pt.pdf_1d_wCI(y_train, nx=50, smoothing_sigma=F) for k in range(0, 5): ax6 = myfig.add_axes([x0 + k * (w + dw), y0 - h - dh, w, h]) ax6.tick_params(direction='in') plt.yticks([]), plt.xticks([]) ax6.set_yscale('log') plt.yticks([1e-1, 1e-3, 1e-5]) plt.ylim(1e-6, .8) plt.xlim(-10, 10) plt.xticks([-5, 5]) if k == 0: plt.ylabel(r'$T_{NP}$', rotation=0) ax6.yaxis.set_label_coords(-.52, .5) else: ax6.set_yticklabels([]) r_model, p_model = pt.pdf_1d(y_model[:, k], nx=100, smoothing_sigma=F, MyRange=[-20, 20]) plt.plot(r_model, p_model, color=MyColors[0], linewidth=lw0, label='model (74 years)') plt.plot(r_train, p_train, 'k--', linewidth=lw1, label='train (2 years)') plt.plot(r_truth, p_truth, '-.', linewidth=lw2, color=MyColors[1], label='truth (37 years)') # confidence intervalo plt.fill_between(r_truth, p_truth_l, p_truth_u, alpha=0.35, color=MyColors[1], linewidth=0) plt.fill_between(r_train, p_train_l, p_train_u, alpha=0.35, color='k', linewidth=0) if k == 2: legend = plt.legend(fontsize=tfs, bbox_to_anchor=(.5, -.43), loc='center', ncol=3, fancybox=True, framealpha=0) legend.get_frame().set_linewidth(0) legend.get_frame().set_edgecolor("black") # u-velocity idx_target = 0 tdata = sio.loadmat('./results/NorthPole_var' + str(idx_target) + '_r' + str(polynomial_order) + '_tyrs' + str(years_train) + '_syrs' + str(start_year)) y_truth, y_train, y_model = np.squeeze(tdata['y_truth']), np.squeeze( tdata['y_train']), tdata['ys_model'] r_truth, p_truth, p_truth_u, p_truth_l = pt.pdf_1d_wCI(y_truth, nx=100, smoothing_sigma=F) r_train, p_train, p_train_u, p_train_l = pt.pdf_1d_wCI(y_train, nx=50, smoothing_sigma=F) for k in range(0, 5): ax6 = myfig.add_axes([x0 + k * (w + dw), y0, w, h]) ax6.tick_params(direction='in') plt.yticks([]) ax6.set_yscale('log') plt.yticks([1e-1, 1e-3, 1e-5]) plt.ylim(1e-6, .8) plt.xlim(-12, 12) plt.xticks([-10, 10]) if k == 0: plt.ylabel(r'$U_{NP}$', rotation=0) ax6.yaxis.set_label_coords(-.52, .5) else: ax6.set_yticklabels([]) r_model, p_model = pt.pdf_1d(y_model[:, k], nx=100, smoothing_sigma=F, MyRange=[-20, 20]) plt.plot(r_model, p_model, color=MyColors[0], linewidth=lw0) plt.plot(r_train, p_train, 'k--', linewidth=lw1) plt.plot(r_truth, p_truth, '-.', linewidth=lw2, color=MyColors[1]) # confidence intervalo - shaded enevelope method plt.fill_between(r_truth, p_truth_l, p_truth_u, alpha=0.35, color=MyColors[1], linewidth=0) plt.fill_between(r_train, p_train_l, p_train_u, alpha=0.35, color='k', linewidth=0) plt.title(r'$n=' + str(k) + '$', fontsize=tfs) plt.savefig(savepath + 'climate_tails.' + picformat, dpi=400)
def Plot_NorthPole_signals(savepath, tag='', picformat='png'): plt.rc('font', family='serif', size=9) tfs = 10 MyColors = ['#377eb8', '#d95f02'] lw2 = .8 # data matters BaseData = sio.loadmat('./thehood/NorthPole_data.mat') t, y, yp, yc = np.squeeze( BaseData['t']), BaseData['y'], BaseData['yp'], BaseData['yc'] # smoothing for pdfs? F = 2 # axes sizes myfig = plt.figure(figsize=[6.5, 2.4]) w1, w2, w3 = .29, .29, .16 h = .3 dh, dw = .11, .07 x0, y0 = .08, .89 - h xlims = [1980, 1995] # u-vel time series ax1 = myfig.add_axes([x0, y0, w1, h]) ax1.tick_params(direction='in') ax1.plot(t, y[:, 0], color=MyColors[0]) plt.plot(t, yp[:, 0], '--', label=r'periodic part', color=MyColors[1]) plt.xlim(xlims), plt.ylim(-10, 25) ax1.set_xticklabels([]) plt.title(r'time series', fontsize=tfs) plt.ylabel(r'$U_{NP}$~(m/s)') ax1.yaxis.set_label_coords(-.17, .5) legend = plt.legend(fontsize=tfs - 2, bbox_to_anchor=(.38, .14), loc='center left', ncol=1, fancybox=True, framealpha=0.0) legend.get_frame().set_linewidth(0) legend.get_frame().set_edgecolor("black") ax2 = myfig.add_axes([x0 + w1 + dw, y0, w2, h]) ax2.tick_params(direction='in') ax2.plot(t, yc[:, 0], color=MyColors[0]) plt.xlim(xlims) ax2.set_xticklabels([]) plt.title(r'chaotic part', fontsize=tfs) ax3 = myfig.add_axes([x0 + w1 + w2 + 2 * dw + .02, y0, w3, h]) ax3.tick_params(direction='in') [rr, pr] = pt.pdf_1d(yc[:, 0], nx=100, smoothing_sigma=F) rg, pg = pt.Gaussian_fit(yc[:, 0], r=rr) plt.plot(rg, pg, 'k--', label=r'Gaussian fit', linewidth=lw2) plt.plot(rr, pr, color=MyColors[0]) ax3.set_yscale('log') plt.ylim(1e-6, .5) plt.yticks([1e-1, 1e-3, 1e-5]) plt.xlim(-12, 12), plt.xticks([-10, 10]) plt.title(r'PDF of chaotic part', fontsize=tfs) legend = plt.legend(fontsize=tfs - 2, bbox_to_anchor=(-.05, -1.75), loc='center left', ncol=1, fancybox=True, framealpha=0) legend.get_frame().set_linewidth(0) legend.get_frame().set_edgecolor("black") # temperature time series ax4 = myfig.add_axes([x0, y0 - h - dh, w1, h]) plt.tick_params(direction='in') plt.plot(t, y[:, 2]) plt.plot(t, yp[:, 2], '--', color='#d95f02') plt.xlim(xlims) plt.xlabel(r'$t$ (year)') plt.ylabel(r'$T_{NP}$~(K)') plt.ylim(135, 155) ax5 = myfig.add_axes([x0 + w1 + dw, y0 - h - dh, w2, h]) ax5.tick_params(direction='in') ax5.plot(t, yc[:, 2]) plt.xlim(xlims) plt.xlabel(r'$t$ (year)') ax6 = myfig.add_axes([x0 + w1 + w2 + 2 * dw + .02, y0 - h - dh, w3, h]) ax6.tick_params(direction='in') [rr, pr] = pt.pdf_1d(yc[:, 2], nx=100, smoothing_sigma=F) rg, pg = pt.Gaussian_fit(yc[:, 2], r=rr) plt.plot(rg, pg, 'k--', linewidth=lw2) plt.plot(rr, pr, color=MyColors[0]) ax6.set_yscale('log') plt.yticks([1e-1, 1e-3, 1e-5]), plt.ylim(1e-6, .8) plt.xlim(-10, 10), plt.xticks([-5, 5]) plt.savefig(savepath + 'NorthPole_UT.' + picformat, dpi=400)
def Plot_pointwise_stat(savepath, tag='', picformat='png'): plt.rc('font', family='serif', size=9) tfs = 10 PointWiseData = sio.loadmat('./results/Cavity_pointwise_' + tag + '.mat') CavityField = sio.loadmat('./thehood/CavitySensors.mat') # what sensors to look at s = [12, 22, 23, 47] # out of 0-48 SensorIndex = np.squeeze(CavityField['SensorIndex']) ns = SensorIndex.shape[0] # SensorIndex=SensorIndex[s] # -1 is for python vs MATLAB indexing # print(SensorIndex) uv_model = PointWiseData['uv_model'] uv_truth = PointWiseData['uv_truth'] print(uv_model.shape) print(uv_truth.shape) # funcion that designates the position of axes apr = 1 / 2 # aspect ratio myfig = plt.figure(figsize=[6.5, 6.5 * apr]) def CreateAxes(i, j, myfig): # i,j are the row column indices of axes from top left # xleft, ybottom h, w, dw, dh = .24, .24 * apr, .03, .15 x0, y0 = 0.34, .6 xl = x0 + (j - 1) * (w + dw) yl = y0 - (i - 1) * (h + dh) my_axes = myfig.add_axes([xl, yl, w, h]) plt.tick_params(direction='in') plt.yticks([]) # plt.xticks([]) return my_axes xtiko = np.array([[-1, 0, 1], [-.2, 0, .2], [-1, 0, 1], [-.6, 0, .6], [-1, 0, 1], [-.25, 0, .25], [-.5, 0, .5], [-2, 0, 2]]) for j in range(4): ax2 = CreateAxes(1, j + 1, myfig) # u-velocity y = uv_truth[s[j], :] x0, p0 = pt.pdf_1d(y, nx=100, smoothing_sigma=3) xl = [np.amin(x0), np.amax(x0)] # model y = uv_model[s[j], :] x, p = pt.pdf_1d(y, nx=100, smoothing_sigma=3, MyRange=1.5 * np.array(xl)) plt.plot(x, p, 'k', label='SDE model PDF') plt.plot(x0, p0, 'k-.', color='gray', label='true PDF') plt.xticks(xtiko[j, :]) plt.title(r'$u_' + str(j + 1) + '$', fontsize=tfs) # ax2.set_yscale('log') if j == 0: legend = plt.legend(fontsize=tfs, bbox_to_anchor=(4.5, -1.9), ncol=2, fancybox=True) legend.get_frame().set_linewidth(0) legend.get_frame().set_edgecolor("black") ax2 = CreateAxes(2, j + 1, myfig) # v-velocity y = uv_truth[s[j] + ns, :] x0, p0 = pt.pdf_1d(y, nx=100, smoothing_sigma=3) xl = [np.amin(x0), np.amax(x0)] # model y = uv_model[s[j] + ns, :] x, p = pt.pdf_1d(y, nx=100, smoothing_sigma=3, MyRange=1.5 * np.array(xl)) plt.plot(x, p, 'k') plt.plot(x0, p0, 'k-.', color='gray') plt.xticks(xtiko[j + 4, :]) plt.title(r'$v_' + str(j + 1) + '$', fontsize=tfs) # cavity snapshot x, y, vort, cm = CavityField['x'], CavityField['y'], CavityField[ 'vort'], CavityField['colormap_vort'] X, Y = np.squeeze(CavityField['X']), np.squeeze(CavityField['Y']) cm = np.concatenate((cm, np.ones((cm.shape[0], 1))), axis=1) cm = ListedColormap(cm) ax1 = myfig.add_axes([.05, .28, .45 * apr, .45]) plt.contourf(x, y, vort, 100) plt.set_cmap(cm) plt.xticks([]), plt.yticks([]) xpa, ypa = [.07, .08, -.08, .1], [.06, .06, -.24, -.14] for j in range(4): xp, yp = X[SensorIndex[s[j]] - 1], Y[SensorIndex[s[j]] - 1] ax1.plot(xp, yp, 'kx') ax1.text(xp + xpa[j], yp + ypa[j], r'' + str(j + 1), fontsize=tfs - 1) plt.savefig(savepath + 'cavity_pointwise.' + picformat, dpi=400)
def Cavity_analysis_plot(): """Analyze convergence and generate the anlaysis figure [6(a)] in the paper. """ nbins = 65 smoothing_sigma = None Ts = [100, 250, 500, 2500, 5000] errors_sample_size = [] for tmax in Ts: filename = SavePath + 'Cavity_pointwise_analysis_T' + str( tmax) + '_Dim=10_ns10_po2' Data = sio.loadmat(filename) uv_model, uv_truth = Data['uv_model'], Data['uv_truth'] Sensors = np.squeeze(Data['Sensors']) S = list(range(Sensors.shape[0])) sum_sensor_error = 0 for sensor in S: grid, p_truth = pt.pdf_1d(uv_truth[sensor, :], nx=nbins, smoothing_sigma=smoothing_sigma) dx0 = grid[1] - grid[0] _, p_model = pt.pdf_1d(uv_model[sensor, :], nx=nbins, smoothing_sigma=smoothing_sigma, MyRange=[np.amin(grid), np.amax(grid)]) sum_sensor_error = sum_sensor_error + compute_var_diag( p_truth, p_model, dx0, trim_zeros=True) errors_sample_size.append(sum_sensor_error / len(S)) poly_orders = [1, 2, 3, 4, 5] errors_porder = [] for poly_order in poly_orders: filename = SavePath + 'Cavity_pointwise_analysis_T2500_Dim=5_ns10_po' + str( poly_order) Data = sio.loadmat(filename) uv_model, uv_truth = Data['uv_model'], Data['uv_truth'] Sensors = np.squeeze(Data['Sensors']) S = list(range(Sensors.shape[0])) sum_sensor_error = 0 for sensor in S: grid, p_truth = pt.pdf_1d(uv_truth[sensor, :], nx=nbins, smoothing_sigma=smoothing_sigma) dx0 = grid[1] - grid[0] _, p_model = pt.pdf_1d(uv_model[sensor, :], nx=nbins, smoothing_sigma=smoothing_sigma, MyRange=[np.amin(grid), np.amax(grid)]) sum_sensor_error = sum_sensor_error + compute_var_diag( p_truth, p_model, dx0, trim_zeros=True) errors_porder.append(sum_sensor_error / len(S)) myfig = plt.figure(figsize=[5.5, 2.2]) axw, axx0, axy0 = .33, .15, .2 ax1 = myfig.add_axes([axx0, axy0, axw, .7]) no_samples = [t_train * 10 for t_train in Ts] plt.plot(no_samples, errors_sample_size, '-s', color=MyColors[0]) ax1.tick_params(direction='in') plt.yscale('log'), plt.xscale('log') plt.yticks([1e-2, 3e-2, 1e-1], [r'$10^{-2}$', r'$3 \times 10^{-2}$', r'$10^{-1}$']) plt.minorticks_off() plt.xlabel(r'sample size') plt.title(r'$\tilde{e}$', fontsize=tfs) ax2 = myfig.add_axes([axx0 + .15 + axw, axy0, axw, .7]) ax2.tick_params(direction='in') plt.plot(poly_orders, errors_porder, '-s', color=MyColors[0]) plt.yscale('log') plt.yticks([1e-2, 5e-2], [r'$ 10^{-2}$', r'$5\times 10^{-2}$']) plt.xticks([1, 2, 3, 4, 5]) plt.minorticks_off() plt.xlabel(r'polynomial degree') plt.title(r'$\tilde{e}$', fontsize=tfs) plt.subplots_adjust(bottom=0.35) plt.savefig('cavity_analysis.pdf')
def Lorenz_analysis_plot(): """Analyze convergence and generate the anlaysis figure [6(b)] in the paper. """ x_truth = sio.loadmat('./mat files/Lorenz96Data.mat')['y'][:, 0] nbins = 66 smoothing_sigma = None grid, p_truth = pt.pdf_1d(x_truth, nx=nbins, smoothing_sigma=smoothing_sigma) xl = [np.amin(grid), np.amax(grid)] dx0 = grid[1] - grid[0] Ts = [1, 10, 100, 1000, 5000] Ns, error_ss = [], [] for T in Ts: filename = SavePath + 'LorenzModel_r3_t{}_ns1.mat'.format(T) data = sio.loadmat(filename) x_model = data['x_model'].squeeze() _, p_model = pt.pdf_1d(x_model, nx=nbins, smoothing_sigma=smoothing_sigma, MyRange=xl) Ns.append(data['x_train'].shape[0]) error_ss.append( compute_var_diag(p_truth, p_model, dx0, trim_zeros=True)) rs = [1, 2, 3, 4, 5] error_r = [] for r in rs: filename = SavePath + 'LorenzModel_r{}_t1000_ns1.mat'.format(r) data = sio.loadmat(filename) x_model = data['x_model'].squeeze() _, p_model = pt.pdf_1d(x_model, nx=nbins, smoothing_sigma=smoothing_sigma, MyRange=xl) error_r.append(compute_var_diag(p_truth, p_model, dx0, trim_zeros=True)) myfig = plt.figure(figsize=[5.5, 2.2]) axw, axx0, axy0 = .33, .15, .2 ax1 = myfig.add_axes([axx0, axy0, axw, .7]) ax1.tick_params(direction='in') plt.plot(Ns, error_ss, '-s', color=MyColors[0]) plt.yscale('log'), plt.xscale('log') plt.xlabel(r'sample size') plt.minorticks_off() plt.title(r'$e$', fontsize=tfs) ax2 = myfig.add_axes([axx0 + .15 + axw, axy0, axw, .7]) ax2.tick_params(direction='in') plt.plot(rs, error_r, '-s', color=MyColors[0]) plt.yscale('log') plt.xticks(rs) plt.xlabel(r'polynomial degree') plt.yticks([1e-2, 5e-3], [r'$ 10^{-2}$', r'$5\times 10^{-3}$']) plt.xticks(rs) plt.minorticks_off() plt.title(r'$e$', fontsize=tfs) plt.subplots_adjust(bottom=0.35) plt.savefig('lorenz_analysis.pdf', dpi=450)