def ks_plots(plots, flavour, species): pvals = [] for plotname in plots: if plotname in ignore: continue plot = plots[plotname] L, U, N, T, x, plotfile = plot['attrs'] sw, aw = abb_init_kernels(L, U, N, flavour, plotname) nsw, naw = plot['nsw'], plot['naw'] for j, t in enumerate(T): if t in sw.years: if j > 0: if species == 'sw': ccdf, dcdf, d, p = ks1(sw.meas['SW'][t], nsw[j], x) z = np.sort(sw.meas['SW'][t]) elif species == 'aw': ccdf, dcdf, d, p = ks1(aw.meas['AW'][t], naw[j], x) z = np.sort(aw.meas['AW'][t]) pvals.append(p) # # histogram of pvals # plt.figure() # plt.hist(pvals, bins=21) # plt.xlabel('p-value') # plt.ylabel('frequency') # plt.savefig('plots/kshist_%s_%s.png' % (flavour, species)) # plt.close() # sorted scatter plot plt.figure() x = range(len(pvals)) plt.plot(x, sorted(pvals), '.k') plt.plot(x, len(x)*[0.05], '-r') plt.xlabel('index') plt.ylabel('p-value') plt.savefig('plots/ksscatter_%s_%s.png' % (flavour, species)) plt.close() pvals = np.asarray(pvals) n = len(pvals) nsig = np.count_nonzero(pvals > 0.05) print flavour, species, n, nsig, n - nsig return pvals
def ks_compare(plots1, plots2, flavour, species): pvals1 = [] pvals2 = [] for plotname in plots1: if plotname in ignore: continue plot1 = plots1[plotname] plot2 = plots2[plotname] L, U, N, T, x, plotfile = plot1['attrs'] sw, aw = abb_init_kernels(L, U, N, flavour, plotname) nsw1, naw1 = plot1['nsw'], plot1['naw'] nsw2, naw2 = plot2['nsw'], plot2['naw'] for j, t in enumerate(T): if t in sw.years: if j > 0: if species == 'sw': ccdf, dcdf, d, p = ks1(sw.meas['SW'][t], nsw1[j], x) # pvals1.append(p) pvals1.append(d) ccdf, dcdf, d, p = ks1(sw.meas['SW'][t], nsw2[j], x) # pvals2.append(p) pvals2.append(d) elif species == 'aw': ccdf, dcdf, d, p = ks1(aw.meas['AW'][t], naw1[j], x) # pvals1.append(p) pvals1.append(d) ccdf, dcdf, d, p = ks1(aw.meas['AW'][t], naw2[j], x) # pvals2.append(p) pvals2.append(d) # pmax = 1.0 pmax = 0.6 # p-val vs p-val (actually d-val vs d-val for now...) plt.figure() plt.plot(pvals1, pvals2, 'ok', alpha=0.5) # plt.axhline(y=0.05, color='black', linestyle='--') # plt.axvline(x=0.05, color='black', linestyle='--') plt.plot([0.0, pmax], [0.0, pmax], color='black') # plt.xlabel('p-value (without competition)') # plt.ylabel('p-value (with competition)') plt.xlabel('KS statistic (without competition)') plt.ylabel('KS statistic (with competition)') dvals1 = np.asarray(pvals1) dvals2 = np.asarray(pvals2) print 'species', species print 'npoints', len(dvals1) print 'nbetter', np.count_nonzero(dvals1 > dvals2) plt.savefig('plots/ksscatter_%s_comp.png' % species) plt.close()
def comp_ks_plots(plots1, plots2, flavour): for plotname in plots1: if plotname != '237': continue plot1 = plots1[plotname] plot2 = plots2[plotname] L, U, N, T, x, plotfile = plot1['attrs'] dx = float(U - L) / N nsw1, naw1 = plot1['nsw'], plot1['naw'] nsw2, naw2 = plot2['nsw'], plot2['naw'] sw1, aw1 = abb_init_kernels(L, U, N, flavour, plotname) sw2, aw2 = abb_init_kernels(L, U, N, flavour, plotname) for j, t in enumerate(T): if t in sw1.years and j > 0: if t != 2000: continue fig, ax = plt.subplots(2, 2) meassw = np.asarray(sw1.meas['SW'][t]) measaw = np.asarray(aw1.meas['AW'][t]) ytop = max(nsw1[j].max(), nsw2[j].max(), naw1[j].max(), naw2[j].max()) * 1.2 # spruce weights = 1e4 / sw1.plot_size * (dx/4) * np.ones(meassw.shape) ax[0,0].hist(meassw, bins=N/4, weights=weights, histtype='stepfilled', color='0.75', edgecolor='0.75') ax[0,0].plot(sw1.x, nsw1[j], color='r', linestyle='-', linewidth=1) ax[0,0].plot(sw2.x, nsw2[j], color='b', linestyle=':', linewidth=1) # ax[0,0].legend(loc='best') ax[0,0].set_ylabel('stems per hectare') ax[0,0].set_title('spruce') ax[0,0].set_ylim(0, ytop) plt.setp(ax[0,0].get_xticklabels(), visible=False) # aspen weights = 1e4 / aw1.plot_size * (dx/4) * np.ones(measaw.shape) ax[0,1].hist(measaw, bins=N/4, weights=weights, histtype='stepfilled', color='0.75', edgecolor='0.75') ax[0,1].plot(aw1.x, naw1[j], color='r', linestyle='-', linewidth=1) ax[0,1].plot(aw2.x, naw2[j], color='b', linestyle=':', linewidth=1) ax[0,1].set_ylim(0, ytop) ax[0,1].set_title('aspen') plt.setp(ax[0,1].get_xticklabels(), visible=False) plt.setp(ax[0,1].get_yticklabels(), visible=False) # spruce ks ccdf, dcdf, d, p = ks1(sw1.meas['SW'][t], nsw1[j], x) z = np.sort(sw1.meas['SW'][t]) ax[1,0].plot(z, dcdf, color='0.75', linestyle='--', linewidth=2) ax[1,0].plot(z, ccdf, '-r') ccdf, dcdf, d, p = ks1(sw1.meas['SW'][t], nsw2[j], x) ax[1,0].plot(z, ccdf, ':b') ax[1,0].set_xlabel('dbh (mm)') ax[1,0].set_ylabel('cdf') ax[1,0].set_xlim(L, U) ax[1,0].set_ylim(0, 1) # aspen ks ccdf, dcdf, d, p = ks1(aw1.meas['AW'][t], naw1[j], x) z = np.sort(aw1.meas['AW'][t]) ax[1,1].plot(z, dcdf, color='0.75', linestyle='--', linewidth=2, label='data') ax[1,1].plot(z, ccdf, '-r', label='w/ competition') ccdf, dcdf, d, p = ks1(aw1.meas['AW'][t], naw2[j], x) ax[1,1].plot(z, ccdf, ':b', label='w/o competition') ax[1,1].set_xlabel('dbh (mm)') ax[1,1].set_ylabel('cdf') ax[1,1].set_xlim(L, U) ax[1,1].set_ylim(0, 1) ax[1,1].legend(loc='lower right', prop={'size': 10}) plt.setp(ax[1,1].get_yticklabels(), visible=False) fig.savefig('plots/projkscomp_%s_%d.png' % (plotname, t)) fig.savefig('plots/projkscomp_%s_%d.pdf' % (plotname, t)) # plt.show() plt.close()
def comp_ks_plots(plots1, plots2, flavour): for plotname in plots1: if plotname != '237': continue plot1 = plots1[plotname] plot2 = plots2[plotname] L, U, N, T, x, plotfile = plot1['attrs'] dx = float(U - L) / N nsw1, naw1 = plot1['nsw'], plot1['naw'] nsw2, naw2 = plot2['nsw'], plot2['naw'] sw1, aw1 = abb_init_kernels(L, U, N, flavour, plotname) sw2, aw2 = abb_init_kernels(L, U, N, flavour, plotname) for j, t in enumerate(T): if t in sw1.years and j > 0: if t != 2000: continue #fig, ax = plt.subplots(ncols=2) plt.figure(1) meassw = np.asarray(sw1.meas['SW'][t]) # measaw = np.asarray(aw1.meas['AW'][t]) # ytop = max(nsw1[j].max(), nsw2[j].max(), naw1[j].max(), naw2[j].max()) * 1.2 # spruce weights = 1e4 / sw1.plot_size * (dx/4) * np.ones(meassw.shape) plt.hist(meassw, bins=N/4, weights=weights, histtype='stepfilled', color='0.5', edgecolor='0.5') plt.plot(sw1.x, nsw1[j], color='r', linestyle='-', linewidth=1) plt.plot(sw2.x, nsw2[j], color='b', linestyle='-', linewidth=1) plt.ylabel('stems per hectare') plt.xlabel('size') plt.xlim(L, 500) plt.savefig('plots/projkscompswonly_%s_%d_sph.png' % (plotname, t)) # spruce ks ccdf, dcdf, d, p = ks1(sw1.meas['SW'][t], nsw1[j], x) z = np.sort(sw1.meas['SW'][t]) plt.figure(2) plt.plot(z, dcdf, color='0.5', linestyle='-', linewidth=2) plt.plot(z, ccdf, '-r') ccdf, dcdf, d, p = ks1(sw1.meas['SW'][t], nsw2[j], x) plt.plot(z, ccdf, '-b') plt.xlabel('size') plt.ylabel('cumulative distribution function') plt.legend(['measurements', 'with comp', 'without comp'], loc='lower right') plt.xlim(L, 500) plt.ylim(0, 1) plt.savefig('plots/projkscompswonly_%s_%d_cdf.png' % (plotname, t)) plt.close()