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 get_pln(S, mu, sigma, lower_trunc=True): """Obtain the predicted RAD from a Poisson lognormal distribution""" abundance = list(empty([S])) rank = range(1, int(S) + 1) cdf_obs = [(rank[i] - 0.5) / S for i in range(0, int(S))] j = 0 cdf_cum = 0 i = 1 while j < S: cdf_cum += pln.pmf(i, mu, sigma, lower_trunc) while cdf_cum >= cdf_obs[j]: abundance[j] = i j += 1 if j == S: abundance.reverse() return abundance i += 1
def get_rad_pln(self, mu, sigma, lower_trunc = True): """Obtain the predicted RAD from a Poisson lognormal distribution""" abundance = list(np.empty([self.S])) rank = range(1, int(self.S) + 1) cdf_obs = [(rank[i]-0.5) / self.S for i in range(0, int(self.S))] j = 0 cdf_cum = 0 i = 1 while j < self.S: cdf_cum += pln.pmf(i, mu, sigma, lower_trunc) while cdf_cum >= cdf_obs[j]: abundance[j] = i j += 1 if j == S: abundance.reverse() return abundance i += 1
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()