def estimate_diversity(taxon_table_t): """ Function to estimate common diversity matrics from a taxon table with samples as rows and observation/otus/pathogens as column the output of this function is a dataframe of the diversity matrices per sample """ # Import the modules for diversity calculations and manipulating dataframes # Get the sample names which are the indices of the transposed taxon table table_samples = taxon_table_t.index.tolist() # Get the number of observed otus / pathogens per sample observed_pathogens = [] #initialize an empty list # Calculate the observed species per sample for sample in table_samples: observed_pathogens.append( observed_otus(table=taxon_table_t, sample_id=sample)) # Estimate diversity shannon = ep.diversity(x=taxon_table_t, method='shannon') simpson = ep.diversity(x=taxon_table_t, method='simpson') species_richness = ep.diversity(x=taxon_table_t, method='spRich') eveness = ep.diversity(x=taxon_table_t, method='even') # Convert the estimates to Pandas series shannon = pd.Series(data=shannon, index=table_samples) simpson = pd.Series(data=simpson, index=table_samples) observed_pathogens = pd.Series(data=observed_pathogens, index=table_samples) species_richness = pd.Series(data=species_richness, index=table_samples) eveness = pd.Series(data=eveness, index=table_samples) diversity_dict = { 'observed_species': observed_pathogens, 'species_richness': species_richness, 'eveness': eveness, 'shannon': shannon, 'simpson': simpson } diversity_table = pd.DataFrame(data=diversity_dict) return (diversity_table)
def diversity(otu_table): methods = ['shannon' , 'gini-simpson', 'simpson' , 'dominance', 'spRich', 'even'] index = list(otu_table.index) methods_res = {} for m in methods: div = ecopy.diversity(otu_table, method=m, breakNA=True) # methods_res.append(list(div)) methods_res.update({m:list(div)}) div_pd = pd.DataFrame(methods_res, columns=methods) div_pd.index = otu_table.index return div_pd
def test_diversity(self): sp = np.array([0, 1, 2, 3, 0]).reshape(1, 5) div = np.round(diversity(sp)) self.assertEqual(div, 1)
def test_diversity(self): sp = np.array([0, 1, 2, 3, 0]).reshape(1,5) div = np.round(diversity(sp)) self.assertEqual(div, 1)