Ejemplo n.º 1
0
    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)
def test_death_z_test(omega_dict):
    """
    This test based and heavily inspired by Plesser H.E:
    https://github.com/heplesser/nmbu_inf200_june2020/blob/master/examples/biolab_project/tests/test_bacteria.py

    Probabilistic test of death function. For this test we will use a 95 percent level of
    confidence, which will give us a aplha value set to 0.05. The test is only used on herbivores,
    since the death function is the same for both species. We do tests with different omega values,
    to represent the difference between the species. We use the herbivore class for both tests
    to simplify the set up, but the result will yield that for herbivore, then for carnivore.

    We will assume and use a low fitness value for the animals, so the formula of death probability
    can be interpreted as the death probability. That means that we will be using omega values set
    above as the death probability.

    We will take number of dead herbivores returned by our death function and use that number to
    calculated the Z-score

    Our hypothesis will be as following:

    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.
    """
    alpha = 0.05  # Setting tha alpha value that we will be using
    herb = Herbivore(
        age=200, weight=5)  # Since we dont have a way of setting the fitness,
    # high age and low weight will ensure a low fitness.
    herb.set_params(
        omega_dict
    )  # As said above, we assume the death probability to be omega
    p = Herbivore.params["omega"]
    N = 1000  # This will be the total amount of the population we use in the Z-test
    n = sum(herb.death() for _ in range(
        N))  # This is the number of death animals in the population
    mean = N * p  # Finds the mean of the population
    var = N * p * (
        1 - p
    )  # Finds the variance of the population, that will be used for std
    Z = (n - mean) / np.sqrt(var)  # Calculated the Z-score
    phi = 2 * stats.norm.cdf(
        -abs(Z))  # Calculated the percentile from the Z-score
    assert phi > alpha  # If the test pass, we can say that our null hypothesis is correct
def test_set_default_params():
    """Set back to default params."""
    Herbivore.set_params({
        "w_birth": 8.0,
        "sigma_birth": 1.5,
        "beta": 0.9,
        "eta": 0.05,
        "a_half": 40.0,
        "phi_age": 0.6,
        "w_half": 10.0,
        "phi_weight": 0.1,
        "mu": 0.25,
        "gamma": 0.2,
        "zeta": 3.5,
        "xi": 1.2,
        "omega": 0.4,
        "F": 10.0,
    })
def test_set_animal_parameters():
    """Sets default parameters to animals for the tests"""
    Herbivore.set_params(
        {
            "w_birth": 8.0,
            "sigma_birth": 1.5,
            "beta": 0.9,
            "eta": 0.05,
            "a_half": 40.0,
            "phi_age": 0.6,
            "w_half": 10.0,
            "phi_weight": 0.1,
            "mu": 0.25,
            "gamma": 0.2,
            "zeta": 3.5,
            "xi": 1.2,
            "omega": 0.4,
            "F": 10.0,
        }
    )
    Carnivore.set_params(
        {
            "w_birth": 6.0,
            "sigma_birth": 1.0,
            "beta": 0.75,
            "eta": 0.125,
            "a_half": 40.0,
            "phi_age": 0.3,
            "w_half": 4.0,
            "phi_weight": 0.4,
            "mu": 0.4,
            "gamma": 0.8,
            "zeta": 3.5,
            "xi": 1.1,
            "omega": 0.8,
            "F": 50.0,
            "DeltaPhiMax": 10.0,
        }
    )
def test_set_params_value_error():
    """Test if adding a value that isn't valid displays ValueError."""
    new_params_1 = {"eta": 5}
    new_params_2 = {"DeltaPhiMax": 0}
    new_params_3 = {"F": -5}

    with pytest.raises(ValueError):
        Herbivore.set_params(new_params_1)

    with pytest.raises(ValueError):
        Herbivore.set_params(new_params_2)

    with pytest.raises(ValueError):
        Herbivore.set_params(new_params_3)
def test_set_params():
    """Tests if it is possible to change the parameters of Animals class."""
    params = Herbivore.params
    new_params = {
        "w_birth": 12.0,
        "sigma_birth": 1.2,
        "beta": 0.8,
        "eta": 0.01,
        "a_half": 35.0,
        "phi_age": 0.4,
        "w_half": 14.0,
        "phi_weight": 0.2,
        "mu": 0.50,
        "gamma": 0.3,
        "zeta": 3.75,
        "xi": 1.552,
        "omega": 0.41,
        "F": 12.0,
    }
    new_params = Herbivore.set_params(new_params)
    assert new_params != params