def gen_animal_data(self): seed(1) h1 = Herbivore() h2 = Herbivore() seed(1) c1 = Carnivore() c2 = Carnivore() return c1, c2, h1, h2
def animal_objects(self): np.random.seed(123) herb = Herbivore() herb.set_parameters(Herbivore.parameters) np.random.seed(123) carn = Carnivore() carn.set_parameters(Carnivore.parameters) return herb, carn
def test_check__phi_borders(): """Test if method 'check__phi_borders()' verifies the _phy conditions '0 <= _phi <= 1' and returns an ValueError if not satisfied""" with pytest.raises(ValueError): Herbivore(1, 50).check__phi_borders(-0.9999) Carnivore(1, 50).check__phi_borders(-0.9999) Herbivore(1, 50).check__phi_borders(1.0001) Carnivore(1, 50).check__phi_borders(1.0001)
def animal_objects(self): np.random.seed(123) herb1 = Herbivore() herb2 = Herbivore() np.random.seed(123) carn1 = Carnivore() carn2 = Carnivore() print(herb1.weight, herb2.weight) return carn1, carn2, herb1, herb2
def test_kill_probability(self, animal_objects): """ The given weights shows that the herb is more fit than the carn. so, the probability should be false. """ np.random.seed(123) carn = Carnivore(weight=0) herb = Herbivore(weight=100) assert not carn.probability_of_kill(herb)
def test_kill_probability(self, gen_animal_data): """ The given weights shows that the herb is more fit than the carn. so, the probability should be false. ---------- gen_animal_data: Carnivore and Herbivore objects """ np.random.seed(1) carn = Carnivore(weight=10) herb = Herbivore(weight=100) assert not carn.kill_prob(herb)
def test_calculate_fitness_and_formula(): """Test if the method 'calculate_fitness()' correctly communicates to the method 'fit_formula()' and returns the correct fitness of the animal (pop_object)'""" herbivore = Herbivore(10, 20) carnivore = Carnivore(15, 30) assert pytest.approx( herbivore.calculate_fitness(herbivore.age, herbivore.weight, herbivore.parameters), 0.7292) assert pytest.approx( carnivore.calculate_fitness(carnivore.age, carnivore.weight, carnivore.parameters), 0.999969)
def test_carnivore_kill(mocker): """ Test that carnivore kills herbivore if 1. carnivore fitness is greater than herbivore fitness 2. the difference between carnivore fitness and herbivore fitness divided by DeltaPhiMax parameter is greater than random value. """ mocker.patch('numpy.random.random', return_value=0.05) herbivore = Herbivore() carnivore = Carnivore() herbivore.fitness = 0.3 carnivore.fitness = 0.9 assert carnivore.will_kill(herbivore.fitness)
def test_animal_non_neagtive_weight(): """ test if animal age is non-negative """ h = Herbivore() c = Carnivore() assert h.weight >= 0 assert c.weight >= 0
def gen_animal_data(self): """ Fixture to generate animals data to be used as input for landscape data Returns ------- (carn_1, carn_2, herb_1, herb_2) : tuple """ np.random.seed(1) herb_1 = Herbivore() herb_2 = Herbivore() np.random.seed(1) carn_1 = Carnivore() carn_2 = Carnivore() print(herb_1.weight, herb_2.weight) return carn_1, carn_2, herb_1, herb_2
def gen_animal_data(self): """ Two animals of the two different species (Herbivore, Carnivore) with predefined set of parameters Returns ------- carn: Carnivore object herb: Herbivore object """ c_params = { 'eta': 0.30, 'w_birth': 4.0, 'sigma_birth': 1.7, 'w_half': 10, 'phi_age': 0.4, 'phi_weight': 0.6, 'a_half': 60, 'beta': 0.2, 'DeltaPhiMax': 11.0, 'mu': 0.25, 'gamma': 0.6, 'zeta': 1, 'omega': 0.6 } h_params = { 'eta': 0.30, 'w_birth': 2.0, 'sigma_birth': 1.5, 'w_half': 10, 'phi_age': 0.3, 'phi_weight': 0.5, 'a_half': 40, 'beta': 0.3, 'mu': 0.3, 'gamma': 0.7, 'zeta': 1, 'omega': 0.3 } np.random.seed(1) carn = Carnivore() carn.set_given_parameters(c_params) np.random.seed(1) herb = Herbivore() herb.set_given_parameters(h_params) return carn, herb
def test_fitness_range(): """ test fitness value is between 0 and 1 """ herb = Herbivore(np.random.randint(0, 100), np.random.randint(0, 100)) carn = Carnivore(np.random.randint(0, 100), np.random.randint(0, 100)) assert 1 >= herb.fitness >= 0 assert 1 >= carn.fitness >= 0
def test_give_birth_lose_weight(self, gen_animal_data): carn, herb = gen_animal_data carn_baby = Carnivore(weight=2) herb_baby = Herbivore(weight=1) carn.lose_weight_give_birth(carn_baby) herb.lose_weight_give_birth(herb_baby) assert carn.weight == pytest.approx(4.5613871182275103) assert herb.weight == pytest.approx(3.2365180454948623)
def generate_animal_data(self): """ Generates population of animals and an array of their initial weight to be used in test_birth_weight_dist Returns ------- birth_weight_list: list list of animals' initial weight """ birth_weight_list = [] for _ in range(20): carn = Carnivore() carn.set_default_weight() birth_weight_list.append(carn.weight) for _ in range(20): herb = Herbivore() herb.set_default_weight() birth_weight_list.append(herb.weight) return birth_weight_list
def animal_objects(self): self.herb_small = Herbivore(5, 20) self.herb_large = Herbivore(5, 50) self.herb_young = Herbivore(10, 20) self.herb_old = Herbivore(50, 20) self.herb_no_weight = Herbivore(20, 0) self.herb = Herbivore() self.carn_small = Carnivore(5, 20) self.carn_large = Carnivore(5, 50) self.carn_young = Carnivore(10, 20) self.carn_old = Carnivore(50, 20) self.carn = Carnivore()
def test_die(self, mocker): """ Tests the die_animal method and death_prob in two conditions: 1. animal with fitness zero (herb_1). 2. a fixed return value for random number with the use of mock (carn_1). Explanation of code: First, generating animals population for one of the landscapes types (i.e. jungle). Generating herb_1 with weight 0 to get the fitness zero and gain death_prob = True. Also, generates carn_1 with some default value just to have fixed values for its fitness. Then, use mock for numpy random number and set the return_value to it in the way taht it always fulfill death_prob = True for carn_1 Finally, run the die_animal method for all of the animals of jungle instance and assert that the 2 animals are died and only the other 2 are in the animal list. """ herb_1 = Herbivore(6, 0) herb_2 = Herbivore() carn_1 = Carnivore(1, 5) carn_2 = Carnivore() params = {'f_max': 10.0} j = Jungle(params) j.add_animal(carn_1) j.add_animal(carn_2) j.add_animal(herb_1) j.add_animal(herb_2) herb_1.calculate_fitness() assert herb_1.death_prob mocker.patch('numpy.random.random', return_value=0.0) j.die_animals() h_count = len(j.in_cell_fauna['Herbivore']) c_count = len(j.in_cell_fauna['Carnivore']) fauna_count = h_count + c_count assert fauna_count == 2
def test__animal_birth_probability(): """ 1. Test no birth if single herbivore or carnivore 2. Test no birth if weight of herbivore or carnivore is less than output of following condition: xi(w_birth + sigma_birth) """ animal = Herbivore(20, 50) assert not animal.birth(1) animal = Carnivore(10, 50) animal.weight = 5 assert not animal.birth(200)
class TestFauna: """ Tests for various methods in the fauna class \n """ @pytest.fixture(autouse=True) def animal_objects(self): self.herb_small = Herbivore(5, 20) self.herb_large = Herbivore(5, 50) self.herb_young = Herbivore(10, 20) self.herb_old = Herbivore(50, 20) self.herb_no_weight = Herbivore(20, 0) self.herb = Herbivore() self.carn_small = Carnivore(5, 20) self.carn_large = Carnivore(5, 50) self.carn_young = Carnivore(10, 20) self.carn_old = Carnivore(50, 20) self.carn = Carnivore() def test_animal_weight(self): """ Tests if the weight of an animal is larger than zero when its set to 5 \n """ assert self.herb_small.weight > 0 assert self.carn_small.weight > 0 def test_herb_age(self): """ Tests if the age of an animal is larger than zero when its set to 20 \n """ assert self.herb_small.age > 0 assert self.carn_small.age > 0 def test_fitness_between_zero_one(self): """ Tests if the fitness of an animal is between zero and one as specified by the fitness \n function \n """ assert 0 <= self.herb_small.animal_fitness <= 1 assert 0 <= self.carn_small.animal_fitness <= 1 def test_fitness_increases_with_weight(self): """ Tests if the fitness of an animal increases as their weight increases \n """ assert self.herb_small.animal_fitness < self.herb_large.animal_fitness assert self.carn_small.animal_fitness < self.carn_large.animal_fitness def test_fitness_decreases_with_age(self): """ Tests if the animal fitness decreases with age. Assumption is that a young animal \n will have better fitness than an older animal assuming equal weight. \n """ assert self.herb_young.animal_fitness > self.herb_old.animal_fitness assert self.carn_young.animal_fitness > self.carn_old.animal_fitness def test_weight_increases_after_eating(self): """ Tests if the weight of an animal that has eaten is larger than the weight \n of an animal that didnt eat \n """ assert self.herb_small.animal_weight_with_food( 0) < self.herb_small.animal_weight_with_food(10) assert self.carn_small.animal_weight_with_food( 0) < self.carn_small.animal_weight_with_food(10) def test_probability_of_birth_if_only_one_animal(self): """ Testing probability of birth returns False when only \n one animal is present \n """ assert self.herb_small.proba_animal_birth(1) is False assert self.carn_young.proba_animal_birth(1) is False def test_probability_of_birth_for_more_than_two_animal(self): """ Checks the probability of birth when more than 2 animals exists \n """ assert self.herb_small.proba_animal_birth(20) is False assert self.carn_young.proba_animal_birth(10) is False def test_age_increases_by_one_per_year(self): """ Takes an animal with age 5 in this case. Then loops for 2 years and the animal \n should then be 7 years old \n """ for _ in range(2): self.herb_small.animal_weight_with_age() self.carn_small.animal_weight_with_age() assert self.herb_small.age == 7 assert self.carn_small.age == 7 def test_weight_decreases_at_end_of_the_year(self): """ Tests if the weight of an animal is decreased after one year \n """ weight_before = self.herb_small.weight for _ in range(1): self.herb_small.animal_weight_with_age() assert self.herb_small.weight < weight_before def test_animal_dies(self): """ test that animal dies when it's weight/fitness is 0 \n """ self.herb.weight = 0 assert self.herb.death_probability is True def test_animal_migration_chances(self, mocker): """ test that the bool of migration is False if \n weight/ fitness is zero \n """ mocker.patch('numpy.random.uniform', return_value=0) self.carn_young.weight = 0 assert self.carn_young.animal_moves_bool is False def test_animal_migration_chances_for_fit_animal(self, mocker): """ test the probability of migration is True if \n fitness is high \n """ mocker.patch('numpy.random.uniform', return_value=0) assert self.herb_young.animal_moves_bool def test_carnivore_kills(self, mocker): """ Test that carnivore kills herbivore if carnivore fitness is greater than \n herbivore fitness. \n """ mocker.patch('numpy.random.uniform', return_value=0) assert self.carn_young.probability_of_killing(self.herb_old) def test_valueerror_for_negative_age(self): """ Tests if valueerror is being raised when a negative age is set as input """ with pytest.raises(ValueError) as err: Herbivore(-5, 20) assert err.type is ValueError def test_valueerror_for_negative_weight(self): """ Tests if value error is being raised when a negative weight is set as input """ with pytest.raises(ValueError) as err: Herbivore(5, -20) assert err.type is ValueError def test_death_z_test(self, mocker): """ Source: Hans Ekkehard Plesser, Biolab project Probabilistic test of death function. Test the number of deaths is normally distributed for large number of animals. And the death probability is significant with a p-value of 0.01. : param p: The hypothesized probabilty """ mocker.patch("biosim.fauna.Fauna.death_probability", return_value=0.1) hypo_proba = 0.1 num_animals = 100 n_died = sum(self.herb_small.death_probability() for _ in range(num_animals)) mean = num_animals * hypo_proba var = num_animals * hypo_proba * (1 - hypo_proba) z = (n_died - mean) / math.sqrt(var) phi = 2 * stats.norm.cdf(-abs(z)) assert phi > ALPHA def test_bionmial_death(self, mocker): """ Source: Hans Ekkehard Plesser, Biolab project Test if the death function returns statistical significant results under the bionomial test, with a given death probability p. : param p: The hypothesized probabilty """ mocker.patch("biosim.fauna.Fauna.death_probability", return_value=0.5) hypo_proba = 0.5 num_animals = 100 num_dead = sum(self.carn_large.death_probability() for _ in range(num_animals)) assert binom_test(num_dead, num_animals, hypo_proba) > ALPHA
def animal_objects(self): self.herb1 = Herbivore(10, 50) self.herb2 = Herbivore(20, 35) self.carn1 = Carnivore(5, 60) self.carn2 = Carnivore(20, 40) return self.herb1, self.herb2, self.carn1, self.carn2
def test_check_known_parameters(): """Test method 'check_unknown_parameters()' if it identifies the given parameter and does not return ValueError""" Herbivore.check_unknown_parameters(params={'zeta': 100}) Carnivore.check_unknown_parameters(params={'mu': 100})
def test_check_unknown_parameters(): """Test method 'check_unknown_parameters()' if it does not identifies the given parameter and returns ValueError""" with pytest.raises(ValueError): Herbivore.check_unknown_parameters(params={'zetaa': 100}) Carnivore.check_unknown_parameters(params={'muu': 100})