def test_coordinates_returned_when_animal_will_move_true( self, mocker, example_properties): """ Asserts that a tuple of coordinates is returned from return_new_coordinates if the animal will move because the random number is less than the probability of moving. """ test_population_true = [{ "species": "Herbivore", "age": 1, "weight": 10.0 }] dict_of_neighbours = { (1, 2): Jungle(test_population_true), (2, 1): Jungle(test_population_true), (2, 3): Jungle(test_population_true), (3, 2): Jungle(test_population_true) } for jungle in dict_of_neighbours.values(): jungle.regrowth() herbivore = Herbivore(example_properties) herbivore.fitness = 1 mocker.patch('numpy.random.random', return_value=0.1) new_coordinates = herbivore.return_new_coordinates(dict_of_neighbours) assert type(new_coordinates) == tuple
def test_birth_weight_loss(): """Does birthing an animal reduce the animals weight to a non-negative number?""" h1 = Herbivore(weight=20) h2 = Herbivore() h1.change_weight(h2.weight, True) assert h1.weight < 20 assert h1.weight > 0
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_find_correct_coordinates(self): """ Implements seeding to assert that find_new_coordinates returns correct location for the animal to move to. """ numpy.random.seed(1) test_population_for_coords = [{ "species": "Herbivore", "age": 1, "weight": 10.0 }] test_properties_for_coords = { "species": "Herbivore", "age": 1, "weight": 10 } neighbours_for_coords = { (1, 2): Jungle(test_population_for_coords), (2, 1): Jungle(test_population_for_coords), (2, 3): Jungle(test_population_for_coords), (3, 2): Jungle(test_population_for_coords) } for jungle in neighbours_for_coords.values(): jungle.regrowth() herbivore = Herbivore(test_properties_for_coords) assert herbivore.find_new_coordinates(neighbours_for_coords) == (2, 1)
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_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_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_has_moved(self, mocker): herb = Herbivore(5, 20) assert herb.has_migrated is False mocker.patch('random.random', return_value=0.001) for _ in range(10): herb.prob_migrate() assert herb.has_migrated
def test_weight_gain_eating_herbivore(): """Test that herbivores gains the right amount of weight after eating.""" herb = Herbivore(5, 10) herb.eats(10) assert herb.weight != 10 assert herb.weight == 19
def test_init(self): herb = Herbivore(5, 3) isinstance(herb.age, int) assert herb.age == 5 assert herb.weight >= 0 with pytest.raises(ValueError): Herbivore(-2, 8).__init__()
def test_growing_older(self): herbivore = Herbivore(3, 12) assert herbivore.age == 3 assert herbivore.weight == 12 herbivore.growing_older() assert herbivore.age == 4 assert herbivore.weight == 11.4
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_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 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_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_feed(self): herb = Herbivore() weight_start = herb.weight food = herb.feed(15) weights_end = herb.weight assert food == 5 assert weight_start < weights_end assert pytest.approx(weights_end - weight_start, 1e-06) == \ herb.beta * herb.F herb = Herbivore() weight_start = herb.weight food = herb.feed(5) weights_end = herb.weight assert food == 0 assert weight_start < weights_end assert pytest.approx(weights_end - weight_start, 1e-06) == \ herb.beta * 5 herb = Herbivore() weight_start = herb.weight food = herb.feed(0) weights_end = herb.weight assert food == 0 assert weight_start == weights_end
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_correct_rel_abund_fodder_herb(self, example_population, example_properties): """ Checks that find_rel_abund_of_fodder returns correct amount of fodder for herb in Jungle cell. """ jungle = Jungle(example_population) jungle.regrowth() herbivore = Herbivore(example_properties) rel_abund_fodder = herbivore.find_rel_abund_of_fodder(jungle) assert rel_abund_fodder == 20.0
def test_true_when_prob__of_killing_is_one(self, teardown_carnivore_tests, example_properties): """ Asserts that kill method returns True when prob_kill is one. """ herbivore = Herbivore(example_properties) carnivore = Carnivore(example_properties) herbivore.find_fitness() carnivore.find_fitness() carnivore.params["DeltaPhiMax"] = 0.1 assert carnivore.kill(herbivore) is True
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_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_fitness_calculation(self): herb = Herbivore(6, 0) assert herb.phi == 0 herb = Herbivore(2, 13) assert not herb.phi == 0 herb = Herbivore(0, 5) assert approx(herb.phi) == 0.377541 herb = Herbivore(6, -3) assert herb.phi == 0
def test_death_z(self): # NOT WORKING # b = Herbivore(age=0, weight=10) p = 0.4 N = 10 n = sum([(b.animal_dying() for _ in range(N))]) mean = N * p var = N * p * (1 - p) Z = (n - mean) / math.sqrt(var) phi = 2 * stats.norm.cdf(-abs(Z)) assert phi > 0.01
def test_feeding_plenty(self): """ Test for herbivore feeding method with plenty of fodder. Returns ------- """ h1 = Herbivore(age=5, weight=20) j1 = Jungle() h1.feeding(j1) assert h1.weight == 29 assert j1.get_fodder() == 790