def figS4(data_dir=mydir, figname = 'FigS4', saveAs = 'eps'): models = ['lognorm', 'mete', 'zipf'] fig = plt.figure() count = 0 gs = gridspec.GridSpec(4, 4) #gs.update(wspace=0.1, hspace=0.1) for i in range(0, 4, 2): for j in range(0, 4, 2): if count < 2: ax = plt.subplot(gs[i:i+2, j:j+2], adjustable='box-forced') count += 1 else: ax = plt.subplot(gs[i:i+2, 1:3], adjustable='box-forced') if i == 0 and j == 0: NSR2 = importData.import_NSR2_data(data_dir + \ 'data/NSR2/Stratified/lognorm_pln_NSR2_stratify.txt') ax.set_title("Lognormal", fontsize = 18) ll = np.asarray(list(((NSR2["ll"])))) ll = ll[np.isneginf(ll) == False] print 'Lognorm: mean = ' + str(np.mean(ll)) + ' std = ' + str(np.std(ll)) elif i == 0 and j == 2: NSR2 = importData.import_NSR2_data(data_dir + \ 'data/NSR2/Stratified/zipf_mle_NSR2_stratify.txt') ax.set_title("Zipf", fontsize = 18) ll = np.asarray(list(((NSR2["ll"])))) ll = ll[np.isneginf(ll) == False] print 'Zipf: mean = ' + str(np.mean(ll)) + ' std = ' + str(np.std(ll)) elif i == 2 and j == 0: NSR2 = importData.import_NSR2_data(data_dir + \ 'data/NSR2/Stratified/mete_NSR2_stratify.txt') ax.set_title("Log-series", fontsize = 18) ll = np.asarray(list(((NSR2["ll"])))) ll = ll[np.isneginf(ll) == False] print 'Log-series: mean = ' + str(np.mean(ll)) + ' std = ' + str(np.std(ll)) else: continue ax.set( adjustable='box-forced') KDE = mo.CV_KDE(ll) #ax.hist(ll, 30, fc='gray', histtype='stepfilled', alpha=0.5, normed=True) ax.plot(KDE[0], KDE[1], linewidth=3, alpha=0.8 , color = 'blue') ax.yaxis.set_major_formatter(mticker.FormatStrFormatter('%.0E')) ax.xaxis.set_major_formatter(mticker.FormatStrFormatter('%.0E')) ax.set_xlim([min(KDE[0]), 0]) plt.xticks(fontsize = 7) plt.yticks(fontsize = 7) ax.set_xlabel('Log-likelihood', fontsize = 16) ax.set_ylabel('Probability density', fontsize = 14) plt.setp(ax.get_xticklabels()[::2], visible=False) plt.setp(ax.get_yticklabels()[::2], visible=False) fig_name = str(mydir + 'figures/' + figname + '_RGB.' + saveAs) fig.subplots_adjust(left=0.1, bottom = 0.1,hspace=0.1) fig.tight_layout()#pad=1.2, w_pad=0.8, h_pad=0.8 #fig.text(0.50, 0.017, 'Log-likelihood', ha='center', va='center', fontsize=15) #fig.text(0.04, 0.5, 'Probability', ha='center', va='center', rotation='vertical', fontsize=20) plt.savefig(fig_name, dpi=600, format = saveAs) plt.close()
def tableS3(data_dir=mydir, lognormType = 'pln', remove =1, seqSim = False): methods = ['geom', 'lognorm', 'mete', 'zipf'] for method in methods: if method == 'zipf': nsr2 = importData.import_NSR2_data(data_dir + \ 'data/NSR2/Stratified_Test/Remove_1s/zipf_mle_NSR2_1_stratify.txt') elif method == 'lognorm': nsr2 = importData.import_NSR2_data(data_dir + \ 'data/NSR2/Stratified_Test/Remove_1s/lognorm_pln_NSR2_1_stratify.txt') elif method == 'mete': nsr2 = importData.import_NSR2_data(data_dir + \ 'data/NSR2/Stratified_Test/Remove_1s/mete_NSR2_1_stratify.txt') elif method == 'geom': nsr2 = importData.import_NSR2_data(data_dir + \ 'data/NSR2/Stratified_Test/Remove_1s/geom_NSR2_1_stratify.txt') print method print "r2 mean = " + str(np.mean(((nsr2["R2"])))) print "r2 std = " + str(np.std(((nsr2["R2"]))))
def table1(data_dir=mydir, lognormType = 'pln', remove =0, seqSim = False): methods = ['geom', 'lognorm', 'mete', 'zipf'] if seqSim != False: print seqSim for method in methods: if method == 'zipf': nsr2 = importData.import_NSR2_data(data_dir + \ 'data/NSR2/Stratified_Test/' + method + '_mle' + '_NSR2_stratify.txt') elif method == 'lognorm': nsr2 = importData.import_NSR2_data(data_dir + \ 'data/NSR2/Stratified_Test/' + method + '_'+ lognormType + '_NSR2_stratify.txt') else: nsr2 = importData.import_NSR2_data(data_dir + \ 'data/NSR2/Stratified_Test/' + method + '_NSR2_stratify.txt') print method print "r2 mean = " + str(np.mean(((nsr2["R2"])))) print "r2 std = " + str(np.std(((nsr2["R2"]))))
def tableS2(data_dir=mydir, lognormType = 'pln', remove =1): seqsims = ['95', '97', '99'] methods = ['geom', 'lognorm', 'mete', 'zipf'] for seqsim in seqsims: print seqsim for method in methods: if method == 'zipf': nsr2 = importData.import_NSR2_data(data_dir + \ 'data/NSR2/Stratified_Test/SequenceSimilarity/'+ method + '_mle_' +seqsim +'_NSR2_stratify.txt') elif method == 'lognorm': nsr2 = importData.import_NSR2_data(data_dir + \ 'data/NSR2/Stratified_Test/SequenceSimilarity/'+ method + '_' + lognormType + '_' +seqsim +'_NSR2_stratify.txt') else: nsr2 = importData.import_NSR2_data(data_dir + \ 'data/NSR2/Stratified_Test/SequenceSimilarity/'+ method + '_' +seqsim +'_NSR2_stratify.txt') print method print "r2 mean = " + str(np.mean(((nsr2["R2"])))) print "r2 std = " + str(np.std(((nsr2["R2"]))))
def tableS5(data_dir=mydir, lognormType = 'pln'): 'Mean and standard deviation of log-likelihood' methods = ['lognorm', 'mete', 'zipf'] for method in methods: if method == 'zipf': nsr2 = importData.import_NSR2_data(data_dir + \ 'data/NSR2/Stratified_Test/Remove_1s/zipf_mle_NSR2_1_stratify.txt') elif method == 'lognorm': nsr2 = importData.import_NSR2_data(data_dir + \ 'data/NSR2/Stratified_Test/Remove_1s/lognorm_pln_NSR2_1_stratify.txt') elif method == 'mete': nsr2 = importData.import_NSR2_data(data_dir + \ 'data/NSR2/Stratified_Test/Remove_1s/mete_NSR2_1_stratify.txt') ll = np.asarray(list(((nsr2["ll"])))) ll = ll[np.isneginf(ll) == False] print method print "Log-likelihood mean = " + str(np.mean(ll)) print "Log-likelihood std = " + str(np.std(ll))
def figSuppp(figname = 'SuppFig3', data_dir=mydir, radius=2): fig = plt.figure() plot_dim = 2 count = 0 IN_Obs_Pred = importData.import_NSR2_data(mydir + \ 'data/NSR2/Stratified/lognorm_pln_NSR2_stratify.txt') N = np.asarray(list(((IN_Obs_Pred["N"])))) S = np.asarray(list(((IN_Obs_Pred["S"])))) NmaxObs = np.asarray(list(((IN_Obs_Pred["NmaxObs"])))) NmaxPred = [] SPred = [] for i in range(len(N)): NmaxPred_i = importPredictS.predictS(N[i], NmaxObs[i], predictNmax=True).getNmax() SPred_i = importPredictS.predictS(N[i], NmaxObs[i], predictNmax=True).getS() NmaxPred.append(NmaxPred_i) SPred.append(SPred_i) NmaxPred = np.asarray(NmaxPred) SPred = np.asarray(SPred) toIteratePred = [NmaxPred, SPred] toIterateObs = [NmaxObs, S] for x in range(2): axis_min = 0 axis_max = 2 * max(toIteratePred[x]) #print plot_dim ax = fig.add_subplot(plot_dim-1, plot_dim, count+1) if x == 0: ax.set_title(r"$\mathbf{N_{max}}$") else: ax.set_title(r"$\mathbf{S}$") macroecotools.plot_color_by_pt_dens(toIteratePred[x], toIterateObs[x], radius, loglog=1, plot_obj=plt.subplot(plot_dim-1,plot_dim,count+1)) plt.plot([axis_min, axis_max],[axis_min, axis_max], 'k-') plt.xlim(axis_min, axis_max) plt.ylim(0, axis_max) r2_all = macroecotools.obs_pred_rsquare(np.log10(toIterateObs[x]), np.log10(toIteratePred[x])) r2text = r"${}^{{2}}_{{m}} = {:.{p}f} $".format('r',r2_all , p=2) plt.text(0.18, 0.93, r2text, fontsize=10, horizontalalignment='center', verticalalignment='center',transform = ax.transAxes) plt.tick_params(axis='both', which='major', labelsize=7) plt.subplots_adjust(wspace=0.5, hspace=0.3) #axins = inset_axes(ax, width="30%", height="30%", loc=4) ax.set(adjustable='box-forced', aspect='equal') #plt.setp(axins, xticks=[], yticks=[]) count += 1 fig.text(0.50, 0.04, r'$Predicted$', ha='center', va='center') fig.text(0.05, 0.5, r'$Observed$', ha='center', va='center', rotation='vertical') fig_name = str(mydir + 'figures/' + figname + '.png') plt.savefig(fig_name, dpi=600)#, bbox_inches = 'tight')#, pad_inches=0) plt.close()
def table2(data_dir=mydir, lognormType = 'pln', remove =0, seqSim = False): methods = ['geom', 'lognorm', 'mete', 'zipf'] if seqSim != False: print seqSim for method in methods: if method == 'zipf': nsr2 = importData.import_NSR2_data(data_dir + \ 'data/NSR2/Stratified_Test/' + method + '_mle' + '_NSR2_stratify.txt') elif method == 'lognorm': nsr2 = importData.import_NSR2_data(data_dir + \ 'data/NSR2/Stratified_Test/' + method + '_'+ lognormType + '_NSR2_stratify.txt') else: nsr2 = importData.import_NSR2_data(data_dir + \ 'data/NSR2/Stratified_Test/' + method + '_NSR2_stratify.txt') print method domSlope = np.mean(((nsr2["NmaxPredSlope"]))) evenSlope = np.mean(((nsr2["evennessPredSlope"]))) skewSlope = np.mean(((nsr2["skewnessPredSlope"]))) print "Dominance slope " + str(domSlope) diffDom = domSlope - 1 p_diffDom = ((np.absolute(diffDom) / ( 0.5 *(domSlope + 1) ))) * 100 diffDom_norm = diffDom / 1 print "Dominance percent difference " + str(p_diffDom) print "Evenness slope " + str( evenSlope ) diffEven = evenSlope - (-0.31) p_diffEven = ((np.absolute(diffEven) / ( 0.5 *(np.absolute(evenSlope + -0.31)) ))) * 100 diffEven_norm = diffEven / -0.31 print "Evenness percent difference " + str(p_diffEven) print "Skewness slope " + str(skewSlope ) diffSkew = skewSlope - 0.13 p_diffSkew = ((np.absolute(diffSkew) / ( 0.5 *(skewSlope + 0.13) ))) * 100 diffSkew_norm = diffSkew / 0.13 print "Skewness percent difference " + str(p_diffSkew)
def tableS4(data_dir=mydir, lognormType = 'pln'): 'Mean and standard deviation of parameters' methods = ['lognorm', 'mete', 'zipf'] for method in methods: print method if method == 'zipf': nsr2 = importData.import_NSR2_data(data_dir + \ 'data/NSR2/Stratified_Test/Remove_1s/zipf_mle_NSR2_1_stratify.txt') print "gamma mean = " + str(np.mean(((nsr2["gamma"])))) print "gamma std = " + str(np.std(((nsr2["gamma"])))) elif method == 'lognorm': nsr2 = importData.import_NSR2_data(data_dir + \ 'data/NSR2/Stratified_Test/Remove_1s/lognorm_pln_NSR2_1_stratify.txt') print "mu mean = " + str(np.mean(((nsr2["mu"])))) print "mu std = " + str(np.std(((nsr2["mu"])))) print "sigma mean = " + str(np.mean(((nsr2["sigma"])))) print "sigma std = " + str(np.std(((nsr2["sigma"])))) elif method == 'mete': nsr2 = importData.import_NSR2_data(data_dir + \ 'data/NSR2/Stratified_Test/Remove_1s/mete_NSR2_1_stratify.txt') p = np.asarray(list(((nsr2["p"])))) beta = np.asarray([math.log(x) * -1 for x in p]) print "beta mean = " + str(np.mean(beta)) print "beta std = " + str(np.std(beta))
def getLogNormSim(testNumber = 100, sample_size = 1000): '''This function randomly samples a number of sites set by testNumber from 'Stratified' dataset using the 75-25 simulation for the lognormal. Because some SADs take a very long time to generate (> 2 minutes), the function runs on a timer with a timeout function that moves to the next randomly chosed SAD (sampled without replacement), stopping when after testNumber of successful runs. ''' IN_NSR2 = importData.import_NSR2_data(mydir + 'data/NSR2/Stratified/lognorm_pln_NSR2_stratify.txt') IN_Obs_Pred = importData.import_obs_pred_data(mydir + 'data/ObsPred/Stratified/lognorm_pln_obs_pred_stratify.txt') OUT = open(mydir + 'data/ObsPred/Stratified/lognorm_75_25_obs_pred_stratify_test.txt', 'w+') siteNSR2 = np.asarray(list(((IN_NSR2["site"])))) N = np.asarray(list(((IN_NSR2["N"])))) S = np.asarray(list(((IN_NSR2["S"])))) siteObsPred = np.asarray(list(((IN_Obs_Pred["site"])))) obs = np.asarray(list(((IN_Obs_Pred["obs"])))) pred = np.asarray(list(((IN_Obs_Pred["pred"])))) uniqueSites = np.unique(siteNSR2) #randomSites = np.random.choice(uniqueSites, size=testNumber, replace=False) obs7525 = [] pred7525 = [] sites7525 = [] pred_pln = [] sites_pln =[] signal.signal(signal.SIGALRM, models.timeout_handler) count = testNumber while count > 0: #randomSite = np.random.choice(uniqueSites, size=1, replace=False) index = random.randint(0,len(uniqueSites)-1) randomSite = uniqueSites[index] uniqueSites = np.delete(uniqueSites,index) for i, site in enumerate(siteNSR2): if site == randomSite: N_i = N[i] S_i = S[i] a = datetime.datetime.now() #siteNSR2_i = siteNSR2[i] SAD = simLogNormFile.simLogNorm(N_i, S_i, sample_size).SimLogNormInt() if len(SAD) != S_i: continue print 'countdown: ' + str(count) print site, S_i, len(SAD) count -= 1 for j in SAD: pred7525.append(j) sites7525.append(site) zipSitesPred7527 = zip(sites7525, pred7525) #print zipSitesPred7527 indexes = np.unique(sites7525, return_index=True)[1] uniqueSites7525 = [sites7525[index] for index in sorted(indexes)] zipOsPredPln = zip(siteObsPred, obs, pred) zipOsPredPlnFilter = [x for x in zipOsPredPln if x[0] in uniqueSites7525] #print zipOsPredPlnFilter #print len(zipOsPredPlnFilter) #zipSitesPred7527Sort = sorted(L, key=itemgetter(0)) countTest = 0 for spot, uniqueSite7525 in enumerate(uniqueSites7525): for r, s in enumerate(siteObsPred): if int(s) == uniqueSite7525: print>> OUT, int(zipSitesPred7527[spot][0]),int(obs[r]), int(pred[r]), int(zipSitesPred7527[countTest][1]) countTest += 1 #obs7525.append(obs[r]) #pred_pln.append(pred[r]) #sites_pln.append(s) #print "pred sites obs752527 pred7525 " #print len(pred_pln), len(sites_pln), len(obs7525), len(pred7525) #for x, site_x in enumerate(sites7525): # print>> OUT, int(site_x), int(sites_pln[x]),int(obs7525[x]), int(pred7525[x]), int(pred_pln[x]) OUT.close()
def figS5(data_dir=mydir, saveAs="eps", figname="figS5"): params = ["beta", "gamma", "mu", "sigma"] fig = plt.figure() count = 0 for i, param in enumerate(params): ax = fig.add_subplot(2, 2, count + 1) if param == "beta": NSR2 = importData.import_NSR2_data(data_dir + "data/NSR2/Stratified/mete_NSR2_stratify.txt") x = np.asarray(list(((NSR2["p"])))) x = np.asarray([math.log(p) * -1 for p in x]) ax.set_title("METE") ax.set_xlabel(r"$\beta$", fontsize=16) ax.set_ylabel("Probability", fontsize=16) print "Beta: mean = " + str(np.mean(x)) + " std = " + str(np.std(x)) x_grid = np.linspace(np.amin(x), np.amax(x), 10000) kde_1 = kde_sklearn(x, x_grid, bandwidth=0.01) kde_1 = [k / sum(kde_1) for k in kde_1] ax.plot(x_grid, kde_1, linewidth=3, alpha=0.5) elif param == "gamma": NSR2 = importData.import_NSR2_data(data_dir + "data/NSR2/Stratified/zipf_mle_NSR2_stratify.txt") x = np.asarray(list(((NSR2["gamma"])))) ax.set_title("Zipf") ax.set_xlabel(r"$\gamma$", fontsize=16) ax.set_ylabel("Probability", fontsize=16) print "Gamma: mean = " + str(np.mean(x)) + " std = " + str(np.std(x)) x_grid = np.linspace(np.amin(x), np.amax(x), 10000) kde_1 = kde_sklearn(x, x_grid, bandwidth=0.05) kde_1 = [k / sum(kde_1) for k in kde_1] ax.plot(x_grid, kde_1, linewidth=3, alpha=0.5) elif param == "mu" or param == "sigma": NSR2 = importData.import_NSR2_data(data_dir + "data/NSR2/Stratified/lognorm_pln_NSR2_stratify.txt") ax.set_title("Lognormal") ax.set_ylabel("Probability", fontsize=16) if param == "mu": x = np.asarray(list(((NSR2["mu"])))) ax.set_xlabel(r"$\mu$", fontsize=16) KDE = mo.CV_KDE(x) ax.plot(KDE[0], KDE[1], linewidth=3, alpha=0.5, color="blue") print "Mu: mean = " + str(np.mean(x)) + " std = " + str(np.std(x)) elif param == "sigma": x = np.asarray(list(((NSR2["sigma"])))) ax.set_xlabel(r"$\sigma$", fontsize=16) KDE = mo.CV_KDE(x) ax.plot(KDE[0], KDE[1], linewidth=3, alpha=0.5, color="blue") print "Sigma: mean = " + str(np.mean(x)) + " std = " + str(np.std(x)) ax.set_xlim([min(x), max(x)]) plt.setp(ax.get_xticklabels()[::2], visible=False) plt.setp(ax.get_yticklabels()[::2], visible=False) plt.xticks(fontsize=7) plt.yticks(fontsize=7) count += 1 fig_name = str(mydir + "figures/" + figname + "_RGB." + saveAs) fig.subplots_adjust(left=0.1, bottom=0.1, hspace=0.1) fig.tight_layout() # pad=1.2, w_pad=0.8, h_pad=0.8 plt.savefig(fig_name, dpi=600, format=saveAs) plt.close()
def statOutput( data_dir=mydir, lognormType = 'pln', remove =0, seqSim = False): methods = ['geom', 'lognorm', 'mete', 'zipf'] if seqSim != False: print seqSim for method in methods: if remove == 0 and seqSim == False: if method == 'zipf': nsr2 = importData.import_NSR2_data(data_dir + \ 'data/NSR2/Stratified_Test/' + method + '_mle' + '_NSR2_stratify.txt') elif method == 'lognorm': nsr2 = importData.import_NSR2_data(data_dir + \ 'data/NSR2/Stratified_Test/' + method + '_'+ lognormType + '_NSR2_stratify.txt') else: nsr2 = importData.import_NSR2_data(data_dir + \ 'data/NSR2/Stratified_Test/' + method + '_NSR2_stratify.txt') elif remove != 0 and seqSim == False: if method == 'zipf': nsr2 = importData.import_NSR2_data(data_dir + \ 'data/NSR2/Stratified_Test/Remove_1s/' + method + '_mle' + '_NSR2_1_stratify.txt') elif method == 'lognorm': nsr2 = importData.import_NSR2_data(data_dir + \ 'data/NSR2/Stratified_Test/Remove_1s/' + method + '_'+ lognormType + '_NSR2_1_stratify.txt') else: nsr2 = importData.import_NSR2_data(data_dir + \ 'data/NSR2/Stratified_Test/Remove_1s/' + method + '_NSR2_1_stratify.txt') else: if method == 'zipf': nsr2 = importData.import_NSR2_data(data_dir + \ 'data/NSR2/Stratified_Test/SequenceSimilarity/'+ method + '_mle_' +seqSim +'_NSR2_stratify.txt') elif method == 'lognorm': nsr2 = importData.import_NSR2_data(data_dir + \ 'data/NSR2/Stratified_Test/SequenceSimilarity/'+ method + '_' + lognormType + '_' +seqSim +'_NSR2_stratify.txt') else: nsr2 = importData.import_NSR2_data(data_dir + \ 'data/NSR2/Stratified_Test/SequenceSimilarity/'+ method + '_' +seqSim +'_NSR2_stratify.txt') print method print "r2 mean = " + str(np.mean(((nsr2["R2"])))) print "r2 std = " + str(np.mean(((nsr2["R2std"])))) if seqSim == False: domSlope = np.mean(((nsr2["NmaxPredSlope"]))) evenSlope = np.mean(((nsr2["evennessPredSlope"]))) skewSlope = np.mean(((nsr2["skewnessPredSlope"]))) print "Dominance slope " + str(domSlope) diffDom = domSlope - 1 p_diffDom = ((np.absolute(diffDom) / ( 0.5 *(domSlope + 1) ))) * 100 diffDom_norm = diffDom / 1 P_errorDom = np.absolute(diffDom_norm) * 100 print "percent difference " + str(p_diffDom) print "percent error " + str(P_errorDom) print "Evenness slope " + str( evenSlope ) diffEven = evenSlope - (-0.31) p_diffEven = ((np.absolute(diffEven) / ( 0.5 *(np.absolute(evenSlope + -0.31)) ))) * 100 diffEven_norm = diffEven / -0.31 P_errorEven = np.absolute(diffEven_norm) * 100 print "percent difference " + str(p_diffEven) print "percent error " + str(P_errorEven) print "Skewness slope " + str(skewSlope ) diffSkew = skewSlope - 0.13 p_diffSkew = ((np.absolute(diffSkew) / ( 0.5 *(skewSlope + 0.13) ))) * 100 diffSkew_norm = diffSkew / 0.13 P_errorSkew = np.absolute(diffSkew_norm) * 100 print "percent difference " + str(p_diffSkew) print "percent error " + str(P_errorSkew)
def figS5(data_dir=mydir, saveAs = 'eps', figname = 'FigS5'): params = ['beta', 'gamma', 'mu', 'sigma'] fig = plt.figure() count = 0 for i, param in enumerate(params): ax = fig.add_subplot(2, 2, count+1) if param == 'beta': NSR2 = importData.import_NSR2_data(data_dir + \ 'data/NSR2/Stratified/mete_NSR2_stratify.txt') x = np.asarray(list(((NSR2["p"])))) x = np.asarray([math.log(p) * -1 for p in x]) ax.set_title("Log-series") ax.set_xlabel(r'$\beta$', fontsize = 16) ax.set_ylabel('Probability density', fontsize = 14) print 'Beta: mean = ' + str(np.mean(x)) + ' std = ' + str(np.std(x)) x_grid = np.linspace(np.amin(x), np.amax(x), 10000) kde_1 = mo.kde_sklearn(x, x_grid, bandwidth=0.01) kde_1 = [k / sum(kde_1) for k in kde_1] ax.plot(x_grid, kde_1, linewidth=3, alpha=0.5) elif param == 'gamma': NSR2 = importData.import_NSR2_data(data_dir + \ 'data/NSR2/Stratified/zipf_mle_NSR2_stratify.txt') x = np.asarray(list(((NSR2["gamma"])))) ax.set_title("Zipf") ax.set_xlabel(r'$\gamma$', fontsize = 16) ax.set_ylabel('Probability density', fontsize = 14) print 'Gamma: mean = ' + str(np.mean(x)) + ' std = ' + str(np.std(x)) x_grid = np.linspace(np.amin(x), np.amax(x), 10000) kde_1 = mo.kde_sklearn(x, x_grid, bandwidth=0.05) kde_1 = [k / sum(kde_1) for k in kde_1] ax.plot(x_grid, kde_1, linewidth=3, alpha=0.5) elif param == 'mu' or param == 'sigma': NSR2 = importData.import_NSR2_data(data_dir + \ 'data/NSR2/Stratified/lognorm_pln_NSR2_stratify.txt') ax.set_title("Lognormal") ax.set_ylabel('Probability density', fontsize = 14) if param == 'mu': x = np.asarray(list(((NSR2["mu"])))) ax.set_xlabel(r'$\mu$', fontsize = 16) KDE = mo.CV_KDE(x) ax.plot(KDE[0], KDE[1], linewidth=3, alpha=0.5, color = 'blue') print 'Mu: mean = ' + str(np.mean(x)) + ' std = ' + str(np.std(x)) elif param == 'sigma': x = np.asarray(list(((NSR2["sigma"])))) ax.set_xlabel(r'$\sigma$', fontsize = 16) KDE = mo.CV_KDE(x) ax.plot(KDE[0], KDE[1], linewidth=3, alpha=0.5, color = 'blue') print 'Sigma: mean = ' + str(np.mean(x)) + ' std = ' + str(np.std(x)) ax.set_xlim([min(x), max(x)]) plt.setp(ax.get_xticklabels()[::2], visible=False) plt.setp(ax.get_yticklabels()[::2], visible=False) plt.xticks(fontsize = 7) plt.yticks(fontsize = 7) count += 1 fig_name = str(mydir + 'figures/' + figname + '_RGB.' + saveAs) fig.subplots_adjust(left=0.1, bottom = 0.1,hspace=0.1) fig.tight_layout()#pad=1.2, w_pad=0.8, h_pad=0.8 plt.savefig(fig_name, dpi=600, format = saveAs) plt.close()
def fig4(figname="Fig4", data_dir=mydir, radius=2, saveAs="eps"): fig = plt.figure() fig.subplots_adjust(bottom=0.15) plot_dim = 1 count = 0 IN_Obs_Pred = importData.import_NSR2_data(mydir + "data/NSR2/Stratified/lognorm_pln_NSR2_stratify.txt") N = np.asarray(list(((IN_Obs_Pred["N"])))) S = np.asarray(list(((IN_Obs_Pred["S"])))) NmaxObs = np.asarray(list(((IN_Obs_Pred["NmaxObs"])))) models = ["geom", "lognorm", "mete", "zipf"] modelSlopes = [0.647520323289, 0.942904468437, 0.769214774397, 0.954497727096] modelInterepts = [0.116508916992, 0.292527611072, 0.19240314275, 0.189954627996] for g, model in enumerate(models): NmaxPred = [] SPred = [] for i in range(len(N)): NmaxPred_i = mo.predictS(N[i], NmaxObs[i], predictNmax=True).getNmax( b=modelInterepts[g], slope=modelSlopes[g] ) SPred_i = mo.predictS(N[i], NmaxObs[i], predictNmax=True).getS() NmaxPred.append(NmaxPred_i) SPred.append(SPred_i) NmaxPred = np.asarray(NmaxPred) SPred = np.asarray(SPred) axis_min = 0 axis_max = 2 * max(NmaxObs) ax = fig.add_subplot(2, 2, count + 1) if model == "zipf": OUT2 = importData.import_NSR2_data( data_dir + "data/NSR2/Stratified_Test/" + model + "_mle_NSR2_stratify.txt" ) elif model == "lognorm": OUT2 = importData.import_NSR2_data( data_dir + "data/NSR2/Stratified_Test/" + model + "_pln_NSR2_stratify.txt" ) else: OUT2 = importData.import_NSR2_data(data_dir + "data/NSR2/Stratified_Test/" + model + "_NSR2_stratify.txt") NmaxObs_BS = np.asarray(list(((OUT2["NmaxObs"])))) NmaxPred_BS = np.asarray(list(((OUT2["NmaxPred"])))) if model == "geom": ax.set_title("Broken-stick") elif model == "lognorm": ax.set_title("Lognormal") elif model == "mete": ax.set_title("Log-series") elif model == "zipf": ax.set_title("Zipf") macroecotools.plot_color_by_pt_dens(NmaxPred, NmaxObs, radius, loglog=1, plot_obj=plt.subplot(2, 2, count + 1)) plt.plot([axis_min, axis_max], [axis_min, axis_max], "k-") plt.xlim(axis_min, axis_max) plt.ylim(0, axis_max) r2_all = macroecotools.obs_pred_rsquare(np.log10(NmaxObs), np.log10(NmaxPred)) r2text = r"${}^{{2}}_{{m}} = {:.{p}f} $".format("r", r2_all, p=2) plt.text( 0.72, 0.12, r2text, fontsize=13, horizontalalignment="center", verticalalignment="center", transform=ax.transAxes, ) plt.tick_params(axis="both", which="major", labelsize=12) plt.subplots_adjust(wspace=0.00001, hspace=0.3) ax.set(adjustable="box-forced", aspect="equal") count += 1 fig.text(0.50, 0.055, "Predicted, " + r"$log_{10}(N_{max})$", ha="center", va="center", fontsize=19) fig.text( 0.09, 0.5, "Observed, " + r"$log_{10}(N_{max})$", ha="center", va="center", rotation="vertical", fontsize=19 ) fig_name = str(mydir + "figures/" + figname + "_RGB." + saveAs) plt.savefig(fig_name, dpi=600, format=saveAs) # , bbox_inches = 'tight')#, pad_inches=0) plt.close()
def fig2(n=352899, figname = 'Fig2', data_dir=mydir, \ stratify = True, radius=2, remove = 0, zipfType = 'mle', RGF = False, \ lognormType = 'pln', saveAs = 'eps'): # TAKEN FROM THE mete_sads.py script used for White et al. (2012) # Used for Figure 3 Locey and White (2013) """Multiple obs-predicted plotter""" fig = plt.figure() count = 0 plot_dim = 2 methods = ['geom', 'lognorm', 'mete', 'zipf'] fig.subplots_adjust(bottom= 0.30) for i, method in enumerate(methods): if method == 'zipf': obs_pred_data = importData.import_obs_pred_data(data_dir + 'data/ObsPred/Stratified/'+ method + '_'+ zipfType+'_obs_pred_stratify.txt') INh2 = importData.import_NSR2_data(data_dir + 'data/NSR2/Stratified/' + method + '_mle' + '_NSR2_stratify.txt') elif method == 'lognorm': obs_pred_data = importData.import_obs_pred_data(data_dir + 'data/ObsPred/Stratified/'+ method + '_'+ lognormType+'_obs_pred_stratify.txt') INh2 = importData.import_NSR2_data(data_dir + 'data/NSR2/Stratified/' + method + '_'+ lognormType + '_NSR2_stratify.txt') else: obs_pred_data = importData.import_obs_pred_data(data_dir + 'data/ObsPred/Stratified/'+ method +'_obs_pred_stratify.txt') INh2 = importData.import_NSR2_data(data_dir + 'data/NSR2/Stratified/' + method + '_NSR2_stratify.txt') obs = np.asarray(list(((obs_pred_data["obs"])))) pred = np.asarray(list(((obs_pred_data["pred"])))) site = np.asarray(list(((obs_pred_data["site"])))) obs2 = [] pred2 = [] site2 = [] obs_all = np.asarray(obs) pred_all = np.asarray(pred) site_all = np.asarray(site) if n == 'all' or len(obs) <= n: obs2 = list(obs) pred2 = list(pred) site2 = list(site) else: if len(obs) > n: inds = np.random.choice(range(len(site)), size=n, replace=False) for ind in inds: obs2.append(obs[ind]) pred2.append(pred[ind]) site2.append(site[ind]) obs = np.asarray(obs2) pred = np.asarray(pred2) site = np.asarray(site2) if method == 'zipf': axis_min = 0 axis_max = 2 * max(pred) else: axis_min = 0 axis_max = 2 * max(obs) ax = fig.add_subplot(plot_dim, plot_dim, count+1) if method == 'zipf': NSR2_BS = importData.import_NSR2_data(data_dir + 'data/NSR2/Stratified_Test/'+ method + '_mle_NSR2_stratify.txt') elif method == 'lognorm': NSR2_BS = importData.import_NSR2_data(data_dir + 'data/NSR2/Stratified_Test/'+ method + '_pln_NSR2_stratify.txt') else: NSR2_BS = importData.import_NSR2_data(data_dir + 'data/NSR2/Stratified_Test/'+ method +'_NSR2_stratify.txt') if method == 'geom': ax.set_title("Broken-stick") elif method == 'lognorm': ax.set_title("Lognormal") elif method == 'mete': ax.set_title("Log-series") elif method == 'zipf': ax.set_title("Zipf") print len(pred), len(obs) macroecotools.plot_color_by_pt_dens(pred, obs, radius, loglog=1, plot_obj=plt.subplot(plot_dim,plot_dim,count+1)) plt.plot([axis_min, axis_max],[axis_min, axis_max], 'k-') if method == 'zipf': plt.xlim(0, axis_max) plt.ylim(0, axis_max) else: plt.xlim(0, axis_max) plt.ylim(0, axis_max) r2s = ((INh2["R2"])) r2s = r2s.astype(float) # insert r2 of all data r2_all = np.mean(((NSR2_BS["R2"]))) print method + ' mean = ' + str(r2_all) print method + ' std dev = ' +str(np.std(r2_all)) r2text = r"${}^{{2}}_{{m}} = {:.{p}f} $".format('r',r2_all , p=2) if method == 'geom': plt.text(0.25, 0.90, r2text, fontsize=14, horizontalalignment='center', verticalalignment='center',transform = ax.transAxes) else: plt.text(0.22, 0.90, r2text, fontsize=14, horizontalalignment='center', verticalalignment='center',transform = ax.transAxes) plt.tick_params(axis='both', which='major', labelsize=10) plt.subplots_adjust(wspace=0.0000000001, hspace=0.5) axins = inset_axes(ax, width="30%", height="30%", loc=4) hist_r2 = np.histogram(r2s, range=(0, 1)) xvals = hist_r2[1] + (hist_r2[1][1] - hist_r2[1][0]) xvals = xvals[0:len(xvals)-1] yvals = hist_r2[0] plt.plot(xvals, yvals, 'k-', linewidth=2) plt.axis([0, 1, 0, 1.1 * max(yvals)]) ax.set(adjustable='box-forced', aspect='equal') plt.setp(axins, xticks=[], yticks=[]) count += 1 plt.tight_layout(pad=1.5, w_pad=0.8, h_pad=0.8) fig.text(0.50, 0.03, 'Predicted abundance', ha='center', va='center', fontsize=16) fig.text(0.08, 0.5, 'Observed abundance', ha='center', va='center', rotation='vertical', fontsize=16) fig_name = str(mydir + 'figures/' + figname + '_RGB.' + saveAs) plt.savefig(fig_name, dpi=600, format = saveAs)#, bbox_inches = 'tight')#, pad_inches=0) plt.close()
def figS1(n=35289, figname = 'FigS1', data_dir=mydir, radius=2, zipfType = 'mle', \ saveAs = 'eps', lognormType = 'pln'): methods = ['geom', 'lognorm', 'mete', 'zipf'] datasets = ['95', '97', '99'] fig = plt.figure() count = 0 rows = len(datasets) columns = len(methods) for i, dataset in enumerate(datasets): for j, method in enumerate(methods): print count if method == 'zipf': obs_pred_data = importData.import_obs_pred_data(data_dir + 'data/ObsPred/Stratified/'+ method + '_'+ zipfType+'_obs_pred_stratify.txt') INh2 = importData.import_NSR2_data(data_dir + 'data/NSR2/Stratified/' + method + '_mle' + '_NSR2_stratify.txt') elif method == 'lognorm': obs_pred_data = importData.import_obs_pred_data(data_dir + 'data/ObsPred/Stratified/'+ method + '_'+ lognormType+'_obs_pred_stratify.txt') INh2 = importData.import_NSR2_data(data_dir + 'data/NSR2/Stratified/' + method + '_'+ lognormType + '_NSR2_stratify.txt') else: obs_pred_data = importData.import_obs_pred_data(data_dir + 'data/ObsPred/Stratified/'+ method +'_obs_pred_stratify.txt') INh2 = importData.import_NSR2_data(data_dir + 'data/NSR2/Stratified/' + method + '_NSR2_stratify.txt') obs = np.asarray(list(((obs_pred_data["obs"])))) pred = np.asarray(list(((obs_pred_data["pred"])))) site = np.asarray(list(((obs_pred_data["site"])))) obs2 = [] pred2 = [] site2 = [] obs_all = np.asarray(obs) pred_all = np.asarray(pred) site_all = np.asarray(site) if n == 'all' or len(obs) <= n: obs2 = list(obs) pred2 = list(pred) site2 = list(site) else: if len(obs) > n: inds = np.random.choice(range(len(site)), size=n, replace=False) for ind in inds: obs2.append(obs[ind]) pred2.append(pred[ind]) site2.append(site[ind]) obs = np.asarray(obs2) pred = np.asarray(pred2) site = np.asarray(site2) print "number of points " + str(len(obs)) if method == 'zipf': axis_min = 0 axis_max = 2 * max(pred) else: axis_min = 0 axis_max = 2 * max(obs) ax = fig.add_subplot(rows, columns, count+1) if i == 0 and j == 0: ax.set_title("Broken-stick") elif i == 0 and j == 1: ax.set_title("Lognormal") elif i == 0 and j == 2: ax.set_title("Log-series") elif i == 0 and j == 3: ax.set_title("Zipf") if j == 0: if dataset == '95': ax.set_ylabel("MG-RAST 95%", rotation=90, size=12) elif dataset == '97': ax.set_ylabel("MG-RAST 97%", rotation=90, size=12) elif dataset == '99': ax.set_ylabel("MG-RAST 99%", rotation=90, size=12) macroecotools.plot_color_by_pt_dens(pred, obs, radius, loglog=1, plot_obj=plt.subplot(rows,columns,count+1)) plt.plot([axis_min, axis_max],[axis_min, axis_max], 'k-') if method == 'zipf': plt.xlim(0, axis_max) plt.ylim(0, axis_max) else: plt.xlim(0, axis_max) plt.ylim(0, axis_max) r2s = ((INh2["R2"])) r2s = r2s.astype(float) mean_r2s = np.mean(r2s) std_r2s = np.std(r2s) print method, dataset print "Mean r2 " + str(mean_r2s) print "Standard dev. " + str(std_r2s) if method == 'zipf': getR2 = importData.import_NSR2_data(data_dir + 'data/NSR2/Stratified_Test/SequenceSimilarity/'+ method + '_mle_' +dataset +'_NSR2_stratify.txt') elif method == 'lognorm': getR2 = importData.import_NSR2_data(data_dir + 'data/NSR2/Stratified_Test/SequenceSimilarity/'+ method + '_' + lognormType + '_' +dataset +'_NSR2_stratify.txt') else: getR2 = importData.import_NSR2_data(data_dir + 'data/NSR2/Stratified_Test/SequenceSimilarity/'+ method + '_' +dataset +'_NSR2_stratify.txt') r2_mean = np.mean(((getR2["R2"]))) if method == 'geom': r2text = r"${}^{{2}}_{{m}} = {:.{p}f} $".format('r',r2_mean , p=3) else: r2text = r"${}^{{2}}_{{m}} = {:.{p}f} $".format('r',r2_mean , p=2) if method == 'geom': plt.text(0.28, 0.90, r2text, fontsize=10, horizontalalignment='center', verticalalignment='center',transform = ax.transAxes) else: plt.text(0.25, 0.90, r2text, fontsize=10, horizontalalignment='center', verticalalignment='center',transform = ax.transAxes) plt.tick_params(axis='both', which='major', labelsize=8) plt.subplots_adjust(wspace=0.0000000001, hspace=0.5) axins = inset_axes(ax, width="30%", height="30%", loc=4) hist_r2 = np.histogram(r2s, range=(0, 1)) xvals = hist_r2[1] + (hist_r2[1][1] - hist_r2[1][0]) xvals = xvals[0:len(xvals)-1] yvals = hist_r2[0] plt.plot(xvals, yvals, 'k-', linewidth=2) plt.axis([0, 1, 0, 1.1 * max(yvals)]) ax.set(adjustable='box-forced', aspect='equal') plt.setp(axins, xticks=[], yticks=[]) count += 1 plt.tight_layout(pad=1.5, w_pad=0.8, h_pad=0.8) fig.subplots_adjust(left=0.1) fig.text(0.50, 0.02, 'Predicted abundance', ha='center', va='center', fontsize=14) fig.text(0.03, 0.5, 'Observed abundance', ha='center', va='center', rotation='vertical', fontsize=14) fig_name = str(mydir + 'figures/' + figname + '_RGB.' + saveAs) plt.savefig(fig_name, dpi=600, format = saveAs)#, bbox_inches = 'tight')#, pad_inches=0) plt.close()
def fig4(figname = 'Fig4', data_dir=mydir, radius=1.5, saveAs = 'png'): fig = plt.figure() fig.subplots_adjust(bottom= 0.15) plot_dim = 1 count = 0 models = ['geom', 'lognorm', 'mete', 'zipf'] #modelSlopes = [0.647520323289, 0.942904468437, 0.769214774397, 0.954497727096] #modelInterepts = [0.116508916992, 0.292527611072, 0.19240314275, 0.189954627996] modelSlopes = [] modelInterepts = [] for g, model in enumerate(models): if model == 'geom': IN_Obs_Pred = importData.import_NSR2_data(mydir + \ 'data/NSR2/Stratified/geom_NSR2_stratify.txt') nsr2 = importData.import_NSR2_data(data_dir + \ 'data/NSR2/Stratified_Test/' + model + '_NSR2_stratify.txt') elif model == 'lognorm': IN_Obs_Pred = importData.import_NSR2_data(mydir + \ 'data/NSR2/Stratified/lognorm_pln_NSR2_stratify.txt') nsr2 = importData.import_NSR2_data(data_dir + \ 'data/NSR2/Stratified_Test/' + model + '_'+ 'pln' + '_NSR2_stratify.txt') elif model == 'mete': IN_Obs_Pred = importData.import_NSR2_data(mydir + \ 'data/NSR2/Stratified/mete_NSR2_stratify.txt') nsr2 = importData.import_NSR2_data(data_dir + \ 'data/NSR2/Stratified_Test/' + model + '_NSR2_stratify.txt') elif model == 'zipf': IN_Obs_Pred = importData.import_NSR2_data(mydir + \ 'data/NSR2/Stratified/zipf_mle_NSR2_stratify.txt') nsr2 = importData.import_NSR2_data(data_dir + \ 'data/NSR2/Stratified_Test/' + model + '_mle' + '_NSR2_stratify.txt') N = np.asarray(list(((IN_Obs_Pred["N"])))) N_All = np.asarray(list(((nsr2["N"])))) domSlope = np.mean(((nsr2["NmaxPredSlope"]))) domIntercept = 10 ** np.mean(((nsr2["NmaxPredIntercept"]))) NmaxObs = np.asarray(list(((IN_Obs_Pred["NmaxObs"])))) NmaxObsAll = np.asarray(list(((nsr2["NmaxObs"])))) NmaxPred = [] NmaxPredAll = [] for i in range(len(N)): NmaxPred_i = mo.predictNmax(N[i]).getNmax(b = domIntercept, slope = domSlope) NmaxPred.append(NmaxPred_i) NmaxPred = np.asarray(NmaxPred) NmaxPred_obs = [k for k in zip(NmaxObs, NmaxPred) if k[0] < 200000 ] NmaxObs = np.asarray([k[0] for k in NmaxPred_obs]) NmaxPred = np.asarray([k[1] for k in NmaxPred_obs]) axis_min = 10 axis_max = 1000000 ax = fig.add_subplot(2, 2, count+1) if model == 'geom': ax.set_title("Broken-stick") elif model == 'lognorm': ax.set_title("Lognormal") elif model == 'mete': ax.set_title("Log-series") elif model == 'zipf': ax.set_title("Zipf") #plot_color_by_pt_dens(NmaxPred, NmaxObs, radius, loglog=1, # plot_obj=plt.subplot(2,2,count+1)) #if model == 'lognorm': # radius = 1.3 macroecotools.plot_color_by_pt_dens(NmaxPred, NmaxObs, radius, loglog=1, plot_obj=plt.subplot(2,2,count+1)) plt.plot([axis_min, axis_max],[axis_min, axis_max], 'k-') plt.xlim(axis_min, axis_max) plt.ylim(axis_min, axis_max) ax.set_xlim(axis_min, axis_max) ax.set_ylim(axis_min, axis_max ) r2_all = macroecotools.obs_pred_rsquare(np.log10(NmaxObs), np.log10(NmaxPred)) r2text = r"${}^{{2}}_{{m}} = {:.{p}f} $".format('r',r2_all , p=2) plt.text(0.72, 0.12, r2text, fontsize=13, horizontalalignment='center', verticalalignment='center',transform = ax.transAxes) plt.tick_params(axis='both', which='major', labelsize=12) plt.subplots_adjust(wspace=0.00001, hspace=0.3) ax.set(adjustable='box-forced', aspect='equal') count += 1 fig.text(0.50, 0.055 , 'Predicted, ' +r'$log_{10}(N_{max})$', ha='center', va='center', fontsize = 19) fig.text(0.09, 0.5, 'Observed, ' +r'$log_{10}(N_{max})$', ha='center', va='center', rotation='vertical',\ fontsize = 19) fig_name = str(mydir + 'figures/' + figname + '_RGB.' + saveAs) plt.savefig(fig_name, dpi=600, format = saveAs)#, bbox_inches = 'tight')#, pad_inches=0) plt.close()
def fig3(figname = 'Fig3', \ zipfType = 'mle', lognormType = 'pln', Stratified = True, data_dir= mydir, \ saveAs = 'eps'): methods = ['geom', 'lognorm', 'mete', 'zipf'] fig = plt.figure() count = 0 params = ['N'] removeSADs = [] for i, param in enumerate(params): for j, method in enumerate(methods): if method == 'zipf': obs_pred_data = importData.import_NSR2_data(data_dir + 'data/NSR2/Stratified/'+ method + '_'+ zipfType + '_NSR2_stratify.txt') elif method == 'lognorm': obs_pred_data = importData.import_NSR2_data(data_dir + 'data/NSR2/Stratified/'+ method + '_'+ lognormType +'_NSR2_stratify.txt') else: obs_pred_data = importData.import_NSR2_data(data_dir + 'data/NSR2/Stratified/'+ method +'_NSR2_stratify.txt') site = np.asarray(list(((obs_pred_data["site"])))) y = np.asarray(list(((obs_pred_data["R2"])))) x = np.log10(np.asarray(list(((obs_pred_data[param]))))) #print "nmax" + str(np.mean(np.asarray(list(((obs_pred_data["NmaxObs"])))))) mean_x = np.mean(x) mean_y = np.mean(y) std_error = sp.stats.sem(y) print method, param print "mean modified r2 = " + str(mean_y) print "modified r2 standard error = " + str(std_error) print "mean " + param + " is " + str(np.mean(np.asarray(list(((obs_pred_data[param])))))) ax = fig.add_subplot(2, 2, count+1) macroecotools.plot_color_by_pt_dens(x, y, 0.1, loglog=0, plot_obj=plt.subplot(2, 2, count+1)) slope, intercept, r_value, p_value, std_err = stats.linregress(x,y) print "slope is " + str(slope) print "r2-value is " + str(r_value **2) print "p-value is " + str(p_value) print "NmaxPred ", method NmaxPred = np.log10(np.asarray(list(((obs_pred_data["NmaxPred"]))))) slope1, intercept1, r_value1, p_value1, std_err1 = stats.linregress(x,NmaxPred) diff1 = slope1 - 1 p_diff1 = (np.absolute(diff1) / ( 0.5 *(slope1 + 1) )) * 100 print "percent difference " + str(p_diff1) print "evennessPred ",method evennessPred = np.log10(np.asarray(list(((obs_pred_data["evennessPred"]))))) slope2, intercept2, r_value2, p_value2, std_err2 = stats.linregress(x,evennessPred) diff2 = slope2 - (-0.31) p_diff2 = (np.absolute(diff2) / ( 0.5 *(slope1 + 1) )) * 100 print "percent difference " + str(p_diff2) print "skewnessPred ",method skewnessPred = np.log10(np.asarray(list(((obs_pred_data["skewnessPred"]))))) slope3, intercept3, r_value3, p_value3, std_err3 = stats.linregress(x,skewnessPred) diff3 = slope3 - 0.13 p_diff3 = (np.absolute(diff3) / ( 0.5 *(slope1 + 1) )) * 100 print "percent difference " + str(p_diff3) plt.xlim(np.amin(x), np.amax(x)) plt.ylim(-1.5,1.5) predict_y = intercept + slope * x pred_error = y - predict_y degrees_of_freedom = len(x) - 2 residual_std_error = np.sqrt(np.sum(pred_error**2) / degrees_of_freedom) plt.plot(x, predict_y, 'k-') plt.axhline(linewidth=2, color='darkgrey',ls='--') plt.tick_params(axis='both', which='major', labelsize=10) if i == 0 and j == 0: ax.set_title("Broken-stick", fontsize = 15) ax.set_ylabel(r'$r^{2}_{m}$', fontsize = 22) if i == 0 and j == 1: ax.set_title("Lognormal", fontsize = 15) if i == 0 and j == 2: ax.set_title("Log-series", fontsize = 15) ax.set_xlabel('Abundance, ' +r'$log_{10}$', fontsize = 17) ax.set_ylabel(r'$r^{2}_{m}$', fontsize = 22) if i == 0 and j == 3: ax.set_title("Zipf", fontsize = 15) ax.set_xlabel('Abundance, ' +r'$log_{10}$', fontsize = 17) ax.tick_params(axis='x', labelsize=12) ax.tick_params(axis='y', labelsize=12) count += 1 plt.tight_layout(pad=0.8, w_pad=0.8, h_pad=0.8) fig_name = str(mydir + 'figures/' + figname + '_RGB.' + saveAs) plt.savefig(fig_name, bbox_inches = "tight", pad_inches = 0.4, dpi = 600, \ format = saveAs) plt.close()
def fig4(figname = 'Fig4', data_dir=mydir, radius=2): fig = plt.figure() plot_dim = 1 count = 0 IN_Obs_Pred = importData.import_NSR2_data(mydir + \ 'data/NSR2/Stratified/lognorm_pln_NSR2_stratify.txt') N = np.asarray(list(((IN_Obs_Pred["N"])))) S = np.asarray(list(((IN_Obs_Pred["S"])))) NmaxObs = np.asarray(list(((IN_Obs_Pred["NmaxObs"])))) # order models = ['geom', 'lognorm', 'mete', 'zipf'] modelSlopes = [0.647520323289, 0.942904468437, 0.769214774397, 0.954497727096] modelInterepts = [0.116508916992, 0.292527611072, 0.19240314275, 0.189954627996] for g, model in enumerate(models): NmaxPred = [] SPred = [] for i in range(len(N)): NmaxPred_i = importPredictS.predictS(N[i], NmaxObs[i], \ predictNmax=True).getNmax(b = modelInterepts[g], slope = modelSlopes[g]) SPred_i = importPredictS.predictS(N[i], NmaxObs[i], predictNmax=True).getS() NmaxPred.append(NmaxPred_i) SPred.append(SPred_i) NmaxPred = np.asarray(NmaxPred) SPred = np.asarray(SPred) axis_min = 0 axis_max = 2 * max(NmaxObs) ax = fig.add_subplot(2, 2, count+1) #ax.set_title(r"$\mathbf{N_{max}}$", y=1.03) if model == 'geom': ax.set_title(r"$\mathbf{Broken-stick}$") elif model == 'lognorm': ax.set_title(r"$\mathbf{Lognormal}$") elif model == 'mete': ax.set_title(r"$\mathbf{Log-series}$") elif model == 'zipf': ax.set_title(r"$\mathbf{Zipf}$") macroecotools.plot_color_by_pt_dens(NmaxPred, NmaxObs, radius, loglog=1, plot_obj=plt.subplot(2,2,count+1)) plt.plot([axis_min, axis_max],[axis_min, axis_max], 'k-') plt.xlim(axis_min, axis_max) plt.ylim(0, axis_max) print max(NmaxPred) r2_all = macroecotools.obs_pred_rsquare(np.log10(NmaxObs), np.log10(NmaxPred)) r2text = r"${}^{{2}}_{{m}} = {:.{p}f} $".format('r',r2_all , p=2) plt.text(0.22, 0.91, r2text, fontsize=13, horizontalalignment='center', verticalalignment='center',transform = ax.transAxes) plt.tick_params(axis='both', which='major', labelsize=7) plt.subplots_adjust(wspace=0.5, hspace=0.3) #axins = inset_axes(ax, width="30%", height="30%", loc=4) ax.set(adjustable='box-forced', aspect='equal') #plt.setp(axins, xticks=[], yticks=[]) count += 1 fig.text(0.50, 0.04, r'$Predicted\, log_{10}(N_{max})$', ha='center', va='center', fontsize = 16) fig.text(0.04, 0.5, r'$Observed\,log_{10}(N_{max})$', ha='center', va='center', rotation='vertical',\ fontsize = 16) fig_name = str(mydir + 'figures/' + figname + '.png') plt.savefig(fig_name, dpi=600)#, bbox_inches = 'tight')#, pad_inches=0) plt.close()
def figS2( n=35289, figname="FigS2", data_dir=mydir, stratify=True, radius=2, remove=1, zipfType="mle", RGF=False, saveAs="eps", lognormType="pln", ): # TAKEN FROM THE mete_sads.py script used for White et al. (2012) # Used for Figure 3 Locey and White (2013) """Multiple obs-predicted plotter""" fig = plt.figure() count = 0 plot_dim = 2 fig.subplots_adjust(bottom=0.30) methods = ["geom", "lognorm", "mete", "zipf"] for i, method in enumerate(methods): if method == "zipf": obs_pred_data = importData.import_obs_pred_data( data_dir + "data/ObsPred/Remove_1s/Stratified/" + method + "_" + zipfType + "_obs_pred_1_stratify.txt" ) INh2 = importData.import_NSR2_data( data_dir + "data/NSR2/Remove_1s/Stratified/" + method + "_mle" + "_NSR2_1_stratify.txt" ) elif method == "lognorm": obs_pred_data = importData.import_obs_pred_data( data_dir + "data/ObsPred/Remove_1s/Stratified/" + method + "_" + lognormType + "_obs_pred_1_stratify.txt" ) INh2 = importData.import_NSR2_data( data_dir + "data/NSR2/Remove_1s/Stratified/" + method + "_" + lognormType + "_NSR2_1_stratify.txt" ) else: obs_pred_data = importData.import_obs_pred_data( data_dir + "data/ObsPred/Remove_1s/Stratified/" + method + "_obs_pred_1_stratify.txt" ) INh2 = importData.import_NSR2_data( data_dir + "data/NSR2/Remove_1s/Stratified/" + method + "_NSR2_1_stratify.txt" ) obs = np.asarray(list(((obs_pred_data["obs"])))) pred = np.asarray(list(((obs_pred_data["pred"])))) site = np.asarray(list(((obs_pred_data["site"])))) obs2 = [] pred2 = [] site2 = [] obs_all = np.asarray(obs) pred_all = np.asarray(pred) site_all = np.asarray(site) if n == "all" or len(obs) <= n: obs2 = list(obs) pred2 = list(pred) site2 = list(site) else: if len(obs) > n: inds = np.random.choice(range(len(site)), size=n, replace=False) for ind in inds: obs2.append(obs[ind]) pred2.append(pred[ind]) site2.append(site[ind]) obs = np.asarray(obs2) pred = np.asarray(pred2) site = np.asarray(site2) if method == "zipf": axis_min = 0 axis_max = 2 * max(pred) else: axis_min = 0 axis_max = 2 * max(obs) ax = fig.add_subplot(plot_dim, plot_dim, count + 1) if method == "zipf": NSR2_BS = importData.import_NSR2_data( data_dir + "data/NSR2/Stratified_Test/Remove_1s/" + method + "_mle_NSR2_1_stratify.txt" ) elif method == "lognorm": NSR2_BS = importData.import_NSR2_data( data_dir + "data/NSR2/Stratified_Test/Remove_1s/" + method + "_pln_NSR2_1_stratify.txt" ) else: NSR2_BS = importData.import_NSR2_data( data_dir + "data/NSR2/Stratified_Test/Remove_1s/" + method + "_NSR2_1_stratify.txt" ) if method == "geom": ax.set_title("Broken-stick") elif method == "lognorm": ax.set_title("Lognormal") elif method == "mete": ax.set_title("Log-series") elif method == "zipf": ax.set_title("Zipf") macroecotools.plot_color_by_pt_dens( pred, obs, radius, loglog=1, plot_obj=plt.subplot(plot_dim, plot_dim, count + 1) ) plt.plot([axis_min, axis_max], [axis_min, axis_max], "k-") if method == "zipf": plt.xlim(0, axis_max) plt.ylim(0, axis_max) else: plt.xlim(0, axis_max) plt.ylim(0, axis_max) r2s = INh2["R2"] r2s = r2s.astype(float) # insert r2 of all data r2_all = np.mean(((NSR2_BS["R2"]))) print method r2text = r"${}^{{2}}_{{m}} = {:.{p}f} $".format("r", r2_all, p=2) if method == "geom": plt.text( 0.25, 0.90, r2text, fontsize=14, horizontalalignment="center", verticalalignment="center", transform=ax.transAxes, ) else: plt.text( 0.22, 0.90, r2text, fontsize=14, horizontalalignment="center", verticalalignment="center", transform=ax.transAxes, ) plt.tick_params(axis="both", which="major", labelsize=10) plt.subplots_adjust(wspace=0.0000000001, hspace=0.5) axins = inset_axes(ax, width="30%", height="30%", loc=4) hist_r2 = np.histogram(r2s, range=(0, 1)) xvals = hist_r2[1] + (hist_r2[1][1] - hist_r2[1][0]) xvals = xvals[0 : len(xvals) - 1] yvals = hist_r2[0] plt.plot(xvals, yvals, "k-", linewidth=2) plt.axis([0, 1, 0, 1.1 * max(yvals)]) ax.set(adjustable="box-forced", aspect="equal") plt.setp(axins, xticks=[], yticks=[]) count += 1 plt.tight_layout(pad=1.5, w_pad=0.8, h_pad=0.8) fig.text(0.50, 0.02, "Predicted abundance", ha="center", va="center", fontsize=16) fig.text(0.08, 0.5, "Observed abundance", ha="center", va="center", rotation="vertical", fontsize=16) fig_name = str(mydir + "figures/" + figname + "_RGB." + saveAs) plt.savefig(fig_name, dpi=600, format=saveAs) # , bbox_inches = 'tight')#, pad_inches=0) plt.close()