def test_eat_if_eaten(mocker): """Test that carnivores wont eat more meat than their parameter ['F'] says.""" mocker.patch("numpy.random.random", return_value=0) carn = Carnivore(5, 20) herbs = [Herbivore(6, 20), Herbivore(6, 20), Herbivore(6, 20)] carn.eat(herbs) assert carn.weight == 57.5
def test_death_weight(): """Testing that the animal dies if the weight is 0.""" herb = Herbivore(3, 0) carn = Carnivore(5, 0) assert herb.death() is True assert carn.death() is True
def test_procreation(self, mocker): herb = Herbivore(4, 30) herb_born = herb.procreation(1) assert herb_born is None carn = Carnivore(4, 30) carn_born = carn.procreation(1) assert carn_born is None herb = Herbivore(4, 30) weight = herb.weight lose_weight = ( herb.params_dict["zeta"] * (herb.params_dict["w_birth"] + herb.params_dict["sigma_birth"])) assert weight < lose_weight for _ in range(10): procreation = herb.procreation(10) assert procreation is None herb = Herbivore(5, 40) phi = herb.phi mocker.patch('random.random', return_value=0.0001) herb.procreation(2) mocker.patch('random.gauss', return_value=5) herb.weight = 40 - (herb.params_dict["xi"] * herb.get_initial_weight_offspring()) phi_procreated = herb.phi assert herb.weight == 34 # NOT WORKING CHECK IT OUT!!!! assert phi > phi_procreated
def test_migration(self): """ Test for migration Returns ------- """ i1 = Island(ISLE_MAP2) Herbivore.set_parameters({'mu': 1.0}) Carnivore.set_parameters({'mu': 1.0}) coordinate = (2, 3) i1.add_animal_island(coordinate, INI_HERB[0]['pop']) i1.add_animal_island(coordinate, INI_CARN[0]['pop']) for herbivore in i1.cells[2, 3].herbivores: herbivore.fitness = 1 for carnivore in i1.cells[2, 3].carnivores: carnivore.fitness = 1 i1.migration() # can only move left or down len_left_herb = len(i1.cells[2, 2].herbivores) len_down_herb = len(i1.cells[3, 3].herbivores) len_left_carn = len(i1.cells[2, 2].carnivores) len_down_carn = len(i1.cells[3, 3].carnivores) assert (len_down_herb + len_left_herb, len_left_carn + len_down_carn) == (20, 20)
def test_migration(self, mocker): cell = SingleCell() neighbor_cells = [((10, 10), Water), ((10, 10), Water), ((10, 10), Water), ((10, 10), Water)] herb_migrate, carn_migrate = cell.migrate(neighbor_cells) assert isinstance(herb_migrate, list) assert isinstance(carn_migrate, list) carn = Carnivore(age=1, weight=50) carn.has_migrated = False cell.present_carnivores.append(carn) cell.animals_allocate([{ 'species': 'Herbivore', 'age': 5, 'weight': 30 } for _ in range(20)]) for _ in range(10): herb_migrate = cell.migrate(neighbor_cells) assert herb_migrate herb_migrate, carn_migrate = cell.migrate(neighbor_cells) assert len(herb_migrate + carn_migrate) > 0 assert carn.has_migrated is True herb = Herbivore(age=1, weight=60) cell.animals_allocate(herb) herb.has_migrated = False assert herb.has_migrated is True mocker.patch('random.choice', return_value=((5, 5), Lowland))
def test_no_birth_weight_to_low(): """Test that the birth function returns None if the weight of the animal is to low.""" herb = Herbivore(5, 15) carn = Carnivore(5, 15) nr_animals = 2 assert herb.birth(nr_animals) is None assert carn.birth(nr_animals) is None
def test_get_initial_weight(self, mocker): mocker.patch('random.gauss', return_value=5) herb = Herbivore(3, None) assert herb.get_initial_weight_offspring() == 5 carn = Carnivore(2, None) assert carn.get_initial_weight_offspring() == 5
def test_fitness_herb_more_than_carn(): """Test that the carnivore won't eat if the herbivore fitness is higher that the carnivores.""" carn = Carnivore(2, 5) carn_weight = carn.weight herb = [Herbivore(6, 25), Herbivore(6, 25), Herbivore(6, 25)] assert carn.eat(herb) assert carn_weight == carn.weight
def test_eat_if_eaten_enough_deltaphimax_low(mocker): """Test that the carnivores wont eat more that their parameter ['F'] when the difference in fitness is bigger than DeltaPhiMax""" mocker.patch("numpy.random.random", return_value=0) carn = Carnivore(5, 20) herbs = [Herbivore(6, 20), Herbivore(6, 20), Herbivore(6, 20)] carn.eat(herbs) assert carn.weight == 57.5
def teardown_carnivore_tests(self): """ This class is for use in several tests, where we need to change the DeltaPhiMax parameter. After a test is run, we reset all parameters to default values. """ yield None Carnivore.reset_params()
def test_eaten_enough(): """Test that a carnivore won't eat if its parameter ['F'] is 0.""" carn = Carnivore(5, 20) carn_weight = carn.weight herbs = [Herbivore(6, 20), Herbivore(6, 20), Herbivore(6, 20)] carn.set_params({"F": 0}) assert carn.eat(herbs) assert carn.weight == carn_weight
def test_false_when_prob_of_killing_is_zero(self, example_properties): """ Asserts that kill method returns False when prob_kill is zero. """ herbivore = Herbivore(example_properties) carnivore = Carnivore(example_properties) herbivore.find_fitness() carnivore.fitness = 0.5 assert carnivore.kill(herbivore) is False
def test_no_birth_probability(mocker): """Test that the animal should not give birth if the random value is higher than the birth probability.""" mocker.patch("numpy.random.random", return_value=1) herb = Herbivore(5, 35) carn = Carnivore(5, 30) nr_animals = 10 assert herb.birth(nr_animals) is None assert carn.birth(nr_animals) is None
def test_no_birth_baby_to_heavy(mocker): """Test that the animal doesn't give birth if the baby is heavier than the mother.""" mocker.patch("numpy.random.normal", return_value=50) mocker.patch("numpy.random.random", return_value=0) herb = Herbivore(5, 35) carn = Carnivore(5, 30) nr_animals = 10 assert herb.birth(nr_animals) is None assert carn.birth(nr_animals) is None
def test_ageing(): """Test that the both herbivore and carnivore ages by 1 year each time called.""" herb = Herbivore(10, 20) carn = Carnivore(10, 20) herb.aging() carn.aging() assert herb.age == 11 assert carn.age == 11
def set_default_animal(self): """ Method for resetting parameters for herbivores and carnivores. Returns ------- """ Herbivore.set_parameters(DEFAULT_HERBIVORE_PARAMETERS) Carnivore.set_parameters(DEFAULT_CARNIVORE_PARAMETERS)
def test_correct_rel_abund_fodder_carn(self, example_population_carn, example_properties): """ Checks that find_rel_abund_of_fodder returns correct value for carnivores. """ jungle = Jungle(example_population_carn) carnivore = Carnivore(example_properties) rel_abund_fodder = carnivore.find_rel_abund_of_fodder(jungle) assert rel_abund_fodder == 0.15
def test_carn_doesnt_eat_when_no_herbs_available(self, example_properties): """ Asserts that carnivore hasn't gained weight after eat method when there are no herbivores to be eaten. """ carnivore = Carnivore(example_properties) old_weight = carnivore.weight pop_herb = [] carnivore.attempt_eating_all_herbivores_in_cell(pop_herb) assert carnivore.weight == old_weight
def test_feed(self, herbivore_list): carnivore = Carnivore(5, 100) sorted_list = sorted(herbivore_list, key=lambda var: var.fitness) length_a = len(sorted_list) weight_a = carnivore.weight new_list = carnivore.feed(sorted_list) length_b = len(new_list) weight_b = carnivore.weight assert length_b < length_a assert weight_a < weight_b assert weight_b - weight_a <= carnivore.F * carnivore.beta # Should add tests that changes DeltaPhiMax and checks # that part of the code carnivore.set_parameters(DeltaPhiMax=0.5) carnivore = Carnivore(5, 100) assert carnivore.fitness > 0.5 sorted_list = sorted(herbivore_list, key=lambda var: var.fitness) for herbivore in sorted_list: herbivore.weight = 200 herbivore._compute_fitness = False herbivore._fitness = 0.0 times_to_eat = len(sorted_list) herbivores_eaten = 0 while len(sorted_list) > 0: carnivore.feed(sorted_list) herbivores_eaten += 1 assert herbivores_eaten == times_to_eat reset_parameters()
def test_natural_weight_loss(): """Does animals lose weight each year as expected?""" h = Herbivore() weight_h = h.weight h.change_weight() assert weight_h - (h._ani_params['eta'] * weight_h) == h.weight c = Carnivore() weight_c = c.weight c.change_weight() assert weight_c - (c._ani_params['eta'] * weight_c) == c.weight
def test_weight_gain_eating_carnivore(mocker): """Test that carnivores eat the right amount of meat and that dead herbivores get removed from from the list of alive herbivores.""" mocker.patch("numpy.random.random", return_value=0) carn = Carnivore(5, 20) herb = [Herbivore(6, 20)] herb = carn.eat(herb) assert carn.weight != 20 assert carn.weight == 35 assert herb == []
def test_age(): """Test that it is possible to set and get the age of animals.""" herb = Herbivore(10, 20) carn = Carnivore(10, 20) assert herb.age == 10 assert carn.age == 10 herb.age = 12 carn.age = 12 assert herb.age == 12 assert carn.age == 12
def test_kill_or_not(self): carnivore = Carnivore() carnivore._compute_fitness = False herbivore = Herbivore() herbivore._compute_fitness = False carnivore._fitness = 1 herbivore._fitness = 0.5 kills = 0 for _ in range(1000): if carnivore.kill_or_not(herbivore): kills += 1 assert 30 < kills < 70
def test_weight(): """Tests that it is possible to set and get weight of an animal.""" herb = Herbivore(10, 20) carn = Carnivore(10, 20) assert herb.weight == 20 assert carn.weight == 20 herb.weight = 30 carn.weight = 30 assert herb.weight == 30 assert carn.weight == 30
def test_weight_loss(): """Test that the animals loses weight each year.""" herb = Herbivore(9, 30) herb.weight_loss() carn = Carnivore(9, 40) carn.weight_loss() assert herb.weight != 30 assert herb.weight == 28.5 assert carn.weight != 40 assert carn.weight == 35
def test_value_error_for_age_and_weight(): """Test that age and weight can't be negative values, and that these raises ValueError.""" with pytest.raises(ValueError): Herbivore(age=-9) with pytest.raises(ValueError): Herbivore(weight=-4) with pytest.raises(ValueError): Carnivore(age=-40) with pytest.raises(ValueError): Carnivore(weight=-8)
def test_fittest_carn_eats_first(self, mocker): """ Tests to see if the fittest of three carnivores is the one that gets to eat the Herbivore """ mocker.patch('random.random', return_value=0) self.island.cell_carn_pop = [Carnivore(1, 20), Carnivore(1, 20), Carnivore(5, 30)] self.island.cell_herb_pop = [Herbivore()] self.island.location.food = 0 self.island.execute_feeding() assert self.island.cell_carn_pop[2].weight > 30 \ and self.island.cell_carn_pop[0].weight == 20
def test_death_cycle(plain_sim): """ Tests that the death cycle works as intended. """ Herbivore.new_parameters({'omega': 0.99}) Carnivore.new_parameters({'omega': 0.99}) plain_sim.add_population([{ "loc": (1, 1), "pop": [ { "species": "Herbivore", "age": 100, "weight": 0.1 }, { "species": "Herbivore", "age": 7, "weight": 100.0 }, { "species": "Herbivore", "age": 100, "weight": 0.1 }, ] }, { "loc": (1, 2), "pop": [ { "species": "Carnivore", "age": 7, "weight": 100.0 }, { "species": "Carnivore", "age": 100, "weight": 1.0 }, { "species": "Carnivore", "age": 7, "weight": 100.0 }, ] }]) plain_sim.death_cycle() assert len(plain_sim.map.array_map[1, 2].present_carnivores) == 2 assert len(plain_sim.map.array_map[1, 1].present_herbivores) == 1 Herbivore.new_parameters({'omega': 0.40}) Carnivore.new_parameters({'omega': 0.90})
def test_fitness_weight_loss(): """Test that the fitness changes with weight loss.""" herb = Herbivore(5, 10) fitness_before_herbivore = herb.fitness carn = Carnivore(5, 30) fitness_before_carnivore = carn.fitness herb.weight_loss() carn.weight_loss() fitness_after_herbivore = herb.fitness fitness_after_carnivore = carn.fitness assert fitness_before_herbivore != fitness_after_herbivore assert fitness_before_carnivore != fitness_after_carnivore
def set_animal_parameters(species, params): """Set parameters for animal species. Parameters ---------- species : str String, name of animal species params : dict Dict with valid parameter specification for species """ if species == "Herbivore": Herbivore.set_params(params) elif species == "Carnivore": Carnivore.set_params(params)