for i, p in zip(range(C.shape[0]), perm): f.write("%d, %d, %g, %g\n" % (i + 1, p + 1, C[i, perm[i]], OC[i, perm[i]])) print("Rendering images") for i in range(len(perm)): if perm[i] >= 0: f = plt.figure(figsize=(12, 6)) gs = matplotlib.gridspec.GridSpec(2, 2, width_ratios=[5, 5], height_ratios=[6, 4]) plt.subplot(gs[0, 0]) plot_data_robinson(np.reshape(comps1[:, i], (Nlats1, Nlons1)), lats1, lons1, subplot=True, euro_centered=True) plt.title('Component %d' % (i + 1)) plt.subplot(gs[0, 1]) plot_data_robinson(np.reshape(comps2[:, perm[i]] * sign_flip[i], (Nlats2, Nlons2)), lats2, lons2, subplot=True, euro_centered=True) plt.title( 'Component %d%s' % (perm[i] + 1, " (sign fliped)" if sign_flip[i] == -1 else "")) plt.subplot(gs[1, :]) plt.plot(ts1[i, :].T, 'b-')
# we must match by time coss = subsample_component_2x2(comp_orig, oNlats, oNlons) dots = np.dot(coss, compsi) comp_ndx = np.argmax(np.abs(dots)) sign_flip = -1.0 if dots[comp_ndx] < 0 else 1.0 time_series[:, i] = tsi[comp_ndx, :] / np.var( tsi[comp_ndx, :]) * sign_flip - i * 0.4 comp = compsi[:, comp_ndx] * sign_flip print("i = %d comp_ndx = %d\n" % (i, comp_ndx)) plt.subplot(Ncomps + 1, 1, i + 1) plot_data_robinson(np.reshape(comp, (Nlats, Nlons)), lats, lons, subplot=True, euro_centered=euro_centered) plt.text(0.0, 0.99 * plt.ylim()[1], '(%s)' % ("abcdefgh"[i])) plt.subplot(Ncomps + 1, 1, Ncomps + 1) plt.plot(time_series, linewidth=0.5) plt.yticks([]) year_step = 8 years = range(1948, 2013, year_step) sy = [str(y) for y in years] plt.xticks(range(0, time_series.shape[0], 12 * year_step), sy, rotation=90) plt.axis('tight') plt.savefig(output_file)
ts_all[:,0] = standardize(tsi) ts_all[:,1] = standardize(chop_series(ts1, ymin, ymax)) - 4.0 ts_all[:,2] = standardize(chop_series(ts2, ymin, ymax)) - 2*4.0 cc = np.corrcoef(ts_all, rowvar = 0) # all time series data # load data from component sets fig = plt.figure(figsize = (12, 6)) plt.subplots_adjust(left = 0.025, right = 0.975, top = 0.97, wspace = 0.4, hspace = 0.3) ax = plt.gca() plt.subplot2grid((2,2), (0,0)) plot_data_robinson(np.reshape(U1, (len(lats1), len(lons1))), lats1, lons1, subplot = True, add_colorbar = False, parallel_labels = 'left', euro_centered = euro_centered) plt.subplot2grid((2,2), (0,1)) plot_data_robinson(np.reshape(U2, (len(lats2), len(lons2))), lats2, lons2, subplot = True, add_colorbar = False, parallel_labels = 'right', euro_centered = euro_centered) plt.subplot2grid((2,2), (1,0), colspan = 2) plt.plot(ts_all, linewidth = 0.5) plt.yticks([]) year_step = 8 years = range(1948, 2013, year_step) sy = [str(y) for y in years] plt.xticks(range(0, ts_all.shape[0], 12 * year_step), sy, rotation = 90)
# all time series data # load data from component sets fig = plt.figure(figsize=(12, 6)) plt.subplots_adjust(left=0.025, right=0.975, top=0.97, wspace=0.4, hspace=0.3) ax = plt.gca() plt.subplot2grid((2, 2), (0, 0)) plot_data_robinson(np.reshape(U1, (len(lats1), len(lons1))), lats1, lons1, subplot=True, add_colorbar=False, parallel_labels='left', euro_centered=euro_centered) plt.subplot2grid((2, 2), (0, 1)) plot_data_robinson(np.reshape(U2, (len(lats2), len(lons2))), lats2, lons2, subplot=True, add_colorbar=False, parallel_labels='right', euro_centered=euro_centered) plt.subplot2grid((2, 2), (1, 0), colspan=2) plt.plot(ts_all, linewidth=0.5)
ts2 /= np.var(ts2, axis = 1) with open(os.path.join(out_dir, 'matching'), 'w') as f: f.write("# matching from %s (left) to %s (right) in %s\n" % (sys.argv[1], sys.argv[2], sys.argv[3])) f.write("# third column - match similarity (correlation in time, dot product in space)\n") f.write("# fourth column - complementary similarity (time -> dot product in space, space -> correlation in time\n") for i, p in zip(range(C.shape[0]), perm): f.write("%d, %d, %g, %g\n" % (i+1, p+1, C[i,perm[i]], OC[i,perm[i]])) print("Rendering images") for i in range(len(perm)): if perm[i] >= 0: f = plt.figure(figsize = (12, 6)) gs = matplotlib.gridspec.GridSpec(2, 2, width_ratios=[5, 5],height_ratios=[6,4]) plt.subplot(gs[0,0]) plot_data_robinson(np.reshape(comps1[:, i], (Nlats1, Nlons1)), lats1, lons1, subplot = True, euro_centered = True) plt.title('Component %d' % (i+1)) plt.subplot(gs[0,1]) plot_data_robinson(np.reshape(comps2[:, perm[i]] * sign_flip[i], (Nlats2, Nlons2)), lats2, lons2, subplot = True, euro_centered = True) plt.title('Component %d%s' % (perm[i]+1, " (sign fliped)" if sign_flip[i] == -1 else "")) plt.subplot(gs[1,:]) plt.plot(ts1[i,:].T, 'b-') plt.plot(ts2[perm[i],:].T * sign_flip[i], 'g-') if sys.argv[3] == 'time': plt.figtext(0.25, 0.43, 'match by timeseries Pearson corr %g, components dot %g' % (C[i,perm[i]], OC[i,perm[i]])) else: plt.figtext(0.25, 0.43, 'match by component dot %g, time series Pearson corr %g' % (C[i,perm[i]], OC[i,perm[i]])) plt.savefig(os.path.join(out_dir, "match_%d_to_%d.png" % (i+1, perm[i]+1))) plt.clf() sys.stdout.write("*")
gf = load_monthly_sat_all() gf.detrend() d = gf.data() d = np.reshape(d, (768, 71*144)) # compute correlation of slp time series with sat index Uc = np.zeros(U1.shape) for i in range(Uc.shape[0]): Uc[i] = np.corrcoef(ts1, d[:,i])[0,1] plt.figure(figsize = (6, 3 * 4)) plt.subplots_adjust(left = 0.02, right = 0.96, hspace = 0.5) plt.subplot(311) plot_data_robinson(np.reshape(U1, (len(lats1), len(lons1))), lats1, lons1, subplot = True, euro_centered = euro_centered) plt.subplot(312) plot_data_robinson(np.reshape(U2, (len(lats2), len(lons2))), lats2, lons2, subplot = True, euro_centered = euro_centered) plt.subplot(313) plot_data_robinson(np.reshape(Uc, (len(lats2), len(lons2))), lats2, lons2, subplot = True, euro_centered = euro_centered) plt.savefig('slp_%02d_sat_%02d.eps' % (slp_ndx, best_match+1))
comp_ndx = np.argmax(np.abs(dots)) sign_flip = -1.0 if dots[comp_ndx] < 0 else 1.0 else: # we must match by time coss = subsample_component_2x2(comp_orig, oNlats, oNlons) dots = np.dot(coss, compsi) comp_ndx = np.argmax(np.abs(dots)) sign_flip = -1.0 if dots[comp_ndx] < 0 else 1.0 time_series[:, i] = tsi[comp_ndx, :] / np.var(tsi[comp_ndx, :]) * sign_flip - i*0.4 comp = compsi[:, comp_ndx] * sign_flip print("i = %d comp_ndx = %d\n" % (i, comp_ndx)) plt.subplot(Ncomps+1, 1, i+1) plot_data_robinson(np.reshape(comp, (Nlats, Nlons)), lats, lons, subplot = True, euro_centered = euro_centered) plt.text(0.0, 0.99*plt.ylim()[1], '(%s)' % ("abcdefgh"[i])) plt.subplot(Ncomps+1, 1, Ncomps+1) plt.plot(time_series, linewidth = 0.5) plt.yticks([]) year_step = 8 years = range(1948,2013,year_step) sy = [str(y) for y in years] plt.xticks(range(0, time_series.shape[0], 12*year_step),sy,rotation = 90) plt.axis('tight') plt.savefig(output_file)