Skip to content

Commit

Permalink
Added diversity measures at the metapop level
Browse files Browse the repository at this point in the history
  • Loading branch information
mtomasini committed Jul 11, 2024
1 parent c7bcbb3 commit b77b084
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 27 deletions.
23 changes: 8 additions & 15 deletions exploration.ipynb

Large diffs are not rendered by default.

64 changes: 61 additions & 3 deletions metapypulation/metapopulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from typing import List

from .individual import Individual
from .subpopulation import Subpopulation
from .subpopulation import Subpopulation, SetOfIndividuals

class Metapopulation():
def __init__(self, number_of_subpopulations: int,
Expand Down Expand Up @@ -83,6 +83,13 @@ def migrate(self):
for subpopulation in self.subpopulations:
subpopulation.incorporate_migrants_in_population()


def get_metapopulation_size(self) -> int:
population_size = 0
for subpopulation in self.subpopulations:
population_size += subpopulation.get_population_size()

return population_size

def make_interact(self):
"""Function that starts interactions within each subpopulation.
Expand All @@ -91,20 +98,71 @@ def make_interact(self):
subpopulation.create_interaction()


def metapopulation_shannon_diversity(self) -> List[float]:
def shannon_diversity_per_subpopulation(self) -> List[float]:
subpopulation_shannons = []
for subpopulation in self.subpopulations:
subpopulation_shannons.append(subpopulation.shannon_diversity())

return subpopulation_shannons


def metapopulation_count_traits_sets(self) -> List[int]:
def traits_sets_per_subpopulation(self) -> List[int]:
subpopulation_counts = []
for subpopulation in self.subpopulations:
subpopulation_counts.append(subpopulation.count_traits_sets())

return subpopulation_counts


def metapopulation_shannon_diversity(self) -> float:
"""
This function counts the shannon diversity over the whole metapopulation. The function first
merges all subpopulations into one pot, then measures shannon diversity.
"""
number_of_features = self.number_of_features
traits = np.zeros((self.get_metapopulation_size(), number_of_features))
i = 0
for subpopulation in self.subpopulations:
for individual in subpopulation.population:
traits[i] = (individual.features)
i += 1

shannons = []
# looping over all features, for each feature extract the frequency of each trait in the population
for k in range(0, number_of_features):
frequencies = []
feature_traits = traits[:, k]
unique, counts = np.unique(feature_traits, return_counts=True)
for trait_count in counts:
trait_frequency = trait_count / (self.get_metapopulation_size())
frequencies.append(trait_frequency)

frequencies = np.array(frequencies)
shannon_for_trait = -np.sum(frequencies*np.log(frequencies))

shannons.append(shannon_for_trait)

shannon_index = np.mean(shannons)

return shannon_index


def metapopulation_test_sets(self) -> int:
"""
This function counts the total of unique sets of traits in the whole metapopulation. The function first
merges all subpopulations into one pot, then counts different sets.
"""
number_of_features = self.number_of_features
traits = np.zeros((self.get_metapopulation_size(), number_of_features))
i = 0
for subpopulation in self.subpopulations:
for individual in subpopulation.population:
traits[i] = (individual.features)
i += 1

uniques = np.unique(traits, axis = 0)

return len(uniques)


class SubpopulationIterator(object):
Expand Down
4 changes: 2 additions & 2 deletions metapypulation/subpopulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def create_interaction(self) -> None:

def count_traits_sets(self) -> int:
"""
This function counts the total of unique sets of traits in the population. For example,
This function counts the total of unique sets of traits in the subpopulation. For example,
[0, 1, 2, 3, 4] is a set different from [1, 1, 2, 3, 4], which is different from [5, 4, 3, 2, 1], etc.
"""
number_of_features = self.population.individuals[0].number_of_features
Expand All @@ -100,7 +100,7 @@ def count_traits_sets(self) -> int:

def shannon_diversity(self) -> float:
"""
Shannon_diversity is measure of the richness / diversity in a patch. The formula is
Shannon_diversity is measure of the richness / diversity in a subpopulation. The formula is
H' = -sum(p_i * ln(p_i))
where p_i is the frequency of each species i in the whole sample. The "species" are each trait separately,
Expand Down
17 changes: 10 additions & 7 deletions script.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,16 @@

shannon = []
set_counts = []
metapop_counts = []
metapop_shannon = []

start_time = time.time()
for t in range(300001):
for t in range(200001):
if t%100 == 0:
set_counts.append(metapop.metapopulation_count_traits_sets())
shannon.append(metapop.metapopulation_shannon_diversity())
set_counts.append(metapop.traits_sets_per_subpopulation())
shannon.append(metapop.shannon_diversity_per_subpopulation())
metapop_counts.append(metapop.metapopulation_test_sets())
metapop_shannon.append(metapop.metapopulation_shannon_diversity())
if t%50000 == 0:
print(f"Gen {t}!")
for subpopulation in metapop.subpopulations:
Expand All @@ -42,8 +46,7 @@

print(f"{t} generations ran in {total_time}.")

counts_df = pd.DataFrame(set_counts)
counts_df.to_csv('./Outputs/island.csv', index=False)
# counts_df = pd.DataFrame(set_counts)
# print(counts_df)
# counts_df.plot()
# plt.show()
plt.plot(metapop_shannon)
plt.show()

0 comments on commit b77b084

Please sign in to comment.