def make_hist_empir_model(datasets, analysis_ext, data_dir, fig_ext): plt.figure() for i, dataset in enumerate (datasets): datafile = datafile = data_dir + dataset + analysis_ext raw_data = import_abundance(datafile) usites = np.sort(list(set(raw_data["site"]))) subplot = i + 1 ax = plt.subplot(4,3, subplot) for site in usites: subsites = raw_data["site"][raw_data["site"] == site] abunds = raw_data["ab"][raw_data["site"] == site] N = sum(abunds) # N = total abundance for a site S = len(subsites) # S = species richness at a site if S > 15: #Graphing code """Make a histogram comparing the two models to the empirical data""" xs = range(1, max(abunds) * 2) pln_paras = get_par_multi_dists(abunds, 'pln') + (1,) #add truncation at 1 negbin_paras = get_par_multi_dists(abunds, 'negbin') pln_pmf = pln.pmf(xs, *pln_paras) negbin_pmf = nbinom_lower_trunc.pmf(xs, *negbin_paras) hist_empir, hist_bins = preston_sad(abunds) hist_empir = hist_empir / sum(hist_empir) hist_pln, _ = hist_pmf(xs, pln_pmf, hist_bins) hist_negbin, _ = hist_pmf(xs, negbin_pmf, hist_bins) hist_bins_log = np.log2(hist_bins) xticks = hist_bins_log[:-1] + 0.5 xvalues = [int(np.exp2(val)) for val in hist_bins_log[:-1]] plt.bar(hist_bins_log[:-1], hist_empir, color='gray', width=1) plt.plot(xticks, hist_pln, linewidth=2, color = 'm') plt.plot(xticks, hist_negbin, linewidth=2, color = 'c') plt.xticks(xticks, xvalues, rotation='vertical', fontsize = 'x-small') plt.yticks(fontsize = 'x-small') plt.title(dataset, fontsize = 'small') plt.tight_layout() break ax = plt.subplot(4,3, 12) plt.axis('off') pln_line = plt.scatter([],[], s=100, marker = 's', facecolors='m', edgecolors='black') negbin_line = plt.scatter([],[], s=100, marker = 's', facecolors='c', edgecolors='black') labels = ["Poisson lognormal", "Negative binomial"] plt.legend([pln_line, negbin_line], labels, frameon=False, fontsize=12, scatterpoints = 1) output_file = data_dir + fig_ext plt.savefig(output_file, dpi=250) plt.show() plt.close()
def plot_avg_deviation_from_logseries(sites, obs_ab, p=None, sites_for_p=None, error_bars=0, color='b'): """Plot a figure showing deviations from the log-series as a function of ab Takes the obs-pred data for individual sites, groups them into Preston bins, stores the difference between observed and predicted data within each bin for each site, and then plots the average deviation against the center of the bin. Deviations are calculated as the percentage deviation within each bin, so if there is a difference of one species in a bin with 10 predicted species the deviation = 0.1. """ usites = np.unique(sites) max_N = max(obs_ab) max_integer_logN = int(np.ceil(np.log2(max_N)) + 1) log_bin_edges = np.array(range(0, max_integer_logN)) bin_edges = np.exp2(log_bin_edges) deviations = np.zeros((len(usites), len(bin_edges)-1)) for i, site in enumerate(usites): site_abundances = obs_ab[sites == site] S = len(site_abundances) N = sum(site_abundances) obs_sad = macroecotools.preston_sad(site_abundances, b=bin_edges) if p==None: pred_sad = mete.get_mete_sad(S, N, bin_edges=bin_edges) else: beta = -log(p[sites_for_p==site]) pred_sad = mete.get_mete_sad(S, N, beta=beta, bin_edges=bin_edges) deviation_from_predicted = (obs_sad[0] - pred_sad) / S * 100 deviations[i,:] = deviation_from_predicted bin_numbers = range(1, max_integer_logN) mean_deviations = stats.nanmean(deviations) if error_bars == 1: std_deviations = stats.nanstd(deviations) plt.errorbar(bin_numbers, mean_deviations, yerr=std_deviations, fmt='b-') else: plt.plot(bin_numbers, mean_deviations, color=color, linewidth=3)
def make_hist_empir_model(datasets, analysis_ext, data_dir, fig_ext): plt.figure() for i, dataset in enumerate(datasets): datafile = datafile = data_dir + dataset + analysis_ext raw_data = import_abundance(datafile) usites = np.sort(list(set(raw_data["site"]))) subplot = i + 1 ax = plt.subplot(4, 3, subplot) for site in usites: subsites = raw_data["site"][raw_data["site"] == site] abunds = raw_data["ab"][raw_data["site"] == site] N = sum(abunds) # N = total abundance for a site S = len(subsites) # S = species richness at a site if S > 15: #Graphing code """Make a histogram comparing the two models to the empirical data""" xs = range(1, max(abunds) * 2) pln_paras = get_par_multi_dists(abunds, 'pln') + ( 1, ) #add truncation at 1 negbin_paras = get_par_multi_dists(abunds, 'negbin') pln_pmf = pln.pmf(xs, *pln_paras) negbin_pmf = nbinom_lower_trunc.pmf(xs, *negbin_paras) hist_empir, hist_bins = preston_sad(abunds) hist_empir = hist_empir / sum(hist_empir) hist_pln, _ = hist_pmf(xs, pln_pmf, hist_bins) hist_negbin, _ = hist_pmf(xs, negbin_pmf, hist_bins) hist_bins_log = np.log2(hist_bins) xticks = hist_bins_log[:-1] + 0.5 xvalues = [int(np.exp2(val)) for val in hist_bins_log[:-1]] plt.bar(hist_bins_log[:-1], hist_empir, color='gray', width=1) plt.plot(xticks, hist_pln, linewidth=2, color='m') plt.plot(xticks, hist_negbin, linewidth=2, color='c') plt.xticks(xticks, xvalues, rotation='vertical', fontsize='x-small') plt.yticks(fontsize='x-small') plt.title(dataset, fontsize='small') plt.tight_layout() break ax = plt.subplot(4, 3, 12) plt.axis('off') pln_line = plt.scatter([], [], s=100, marker='s', facecolors='m', edgecolors='black') negbin_line = plt.scatter([], [], s=100, marker='s', facecolors='c', edgecolors='black') labels = ["Poisson lognormal", "Negative binomial"] plt.legend([pln_line, negbin_line], labels, frameon=False, fontsize=12, scatterpoints=1) output_file = data_dir + fig_ext plt.savefig(output_file, dpi=250) plt.show() plt.close()