def test_death_z_test(self, reset_herbivore_params, omega_dict):
        """
        Based on H.E. Plesser: biolab/test_bacteria.py

        Probabilistic test of death function. Testing on herbivores.

        Given that the sample size is large. Under the Z-test the distribution under the null
        hypothesis can be estimated approximately by the normal distribution.
        See https://en.wikipedia.org/wiki/Z-test.

        Assuming low fitness of animals such that omega can be interpreted as an approximation of
        the death probability. Testing with different values of omega
        We compute the number of dead animals returned by our death function from class Animal.
        Then we compare this value to the mean of dead animals derived from a fixed probability.


        Null hypothesis: The number of dead animals returned by the death function is
        statistically significant with a p-value greater than the alpha parameter.
        Alternative hypothesis: The number of dead animals returned is not statistically
        significant and we reject the null hypothesis.
        """
        random.seed(123)
        # High age ensures low fitness
        herb = Herbivore(age=100, weight=10)
        # with low fitness we assume that the death probability is the same as omega
        herb.set_params(omega_dict)
        # death probability set equal to omega
        p = Herbivore.p["omega"]
        # Number of animals
        N = 1000
        # Number of dead animals
        n = sum(herb.death() for _ in range(N))
        # Performing the z-test
        assert phi_z_test(N, p, n) > TestAnimal.alpha
 def test_set_params(self):
     """
     Test that parameters can be set
     """
     my_params = {"w_birth": 10, "sigma_birth": 2.5}
     Herbivore.set_params(my_params)
     assert Herbivore.p["w_birth"] == 10
     assert Herbivore.p["sigma_birth"] == 2.5
    def set_animal_parameters(species, params):
        """Set parameters for animal species.

        :param species: String, name of animal species
        :param params: Dict with valid parameter specification for species
        """
        if species == "Herbivore":
            Herbivore.set_params(params)
        elif species == "Carnivore":
            Carnivore.set_params(params)
        else:
            raise ValueError("species needs to be either Herbivore or Carnivore!")
 def test_set_invalid_params(self, reset_herbivore_params):
     """
     Test errors with illegal keys and values
     """
     with pytest.raises(KeyError):
         assert Herbivore.p["w_death"]
     with pytest.raises(ValueError):
         assert Herbivore.set_params({"sigma_birth": -5})
    def test_mean_birth_weight(self, birth_dict, reset_herbivore_params):
        """ Test that the birth weight of animals are normal distributed using the normaltest
        from scipy.
        https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.normaltest.html

        Null hypothesis: The birth weight of the animal is normally distributed
        Alternative hypothesis: The birth weight are not normally distributed.

        We keep the null hypothesis if the p-value is larger than the significance level alpha
        """
        random.seed(123)
        N = 1000
        Herbivore.set_params(birth_dict)
        herb_birth_weights = [
            Herbivore(age=5, weight=20).birth_weight for _ in range(N)
        ]
        k2, phi = stats.normaltest(herb_birth_weights)
        assert phi > TestAnimal.alpha
    def test_give_birth(self, gamma_dict, reset_herbivore_params):
        """Test that for animals with fitness close to one, and two animals of same type one specie
        in a cell. The give_birth function should be well approximated by the parameter gamma.
        An we test this against our function under the significance level alpha.

        Null hypothesis: The give_birth function returns correct with fixed gamma
        Alternative hypothesis: The give_birth function does not return correct. We reject our
        null hypothesis.
        """

        random.seed(123)
        N = 1000
        Herbivore.set_params(gamma_dict)
        num_herbs = 2
        p = gamma_dict["gamma"]
        list_birth = [
            Herbivore(weight=200, age=5).give_birth(num_herbs)
            for _ in range(N)
        ]
        # number when births return True
        n = sum([item[0] for item in list_birth])
        mean = N * p
        assert phi_z_test(N, p, n) > TestAnimal.alpha
def reset_herbivore_params():
    """
    Based on test_dish.py
    set parameters of herbivores back to defaults
    """
    yield Herbivore.set_params(Herbivore.p)