def test_population_can_gather_vs_survive(self):
        assert hasattr(Pop(), "gatherAndSurvive")
        assert callable(getattr(Pop(), "gatherAndSurvive"))

        self.pop = Pop("test/test/parameters.txt")
        self.pop.create()
        try:
            self.pop.gatherAndSurvive()
        except ValueError as e:
            assert False, "missing info: {0}".format(e)
    def test_population_keeps_track_of_ecological_time(self):
        self.pop = Pop(par="test/test/parameters.txt")
        self.pop.create()

        assert hasattr(
            self.pop,
            "ecoTime"), "population must keep track of its ecological time!"
    def test_ecological_time_correctly_assessed(self):
        self.pop = Pop(par="test/test/parameters.txt")
        self.pop.create()
        self.pop.lifeCycle()
        ngen = 5

        assert self.pop.ecoTime == self.pop.routineSteps, "one life cycle should be {0} units of ecological time, not {1}".format(
            self.pop.routineSteps, self.pop.ecoTime)

        self.pop = Pop(par="test/test/parameters.txt")
        self.pop.create()
        for i in range(ngen):
            self.pop.lifeCycle()

        assert self.pop.ecoTime == self.pop.routineSteps * ngen, "{2} life cycles should be {0} units of ecological time, not {1}".format(
            self.pop.routineSteps * 10, self.pop.ecoTime, ngen)
    def test_population_keeps_track_of_resources_over_life_cycle(self):
        self.pop = Pop(par="test/test/parameters.txt")
        self.pop.create()

        assert hasattr(
            self.pop, "ecologyShortHistory"
        ), "population must keep track of its ecological history!"
    def test_population_keeps_track_of_exploration_over_life_cycle(self):
        self.pop = Pop(par="test/test/parameters.txt")
        self.pop.create()

        assert hasattr(
            self.pop, "explorationShortHistory"
        ), "population must keep track of its exploration history!"
    def test_mutants_are_drawn_from_binomial(self, pseudorandom):
        pseudorandom(0)
        self.nIndividuals = 1000
        self.fakepop = Pop("test/test/parameters.txt")
        self.fakepop.create(n=self.nIndividuals)
        self.mutationRate = 0.2

        self.mutantCount = 0
        for ind in self.fakepop.individuals:
            ind.mutate(mutRate=self.mutationRate, mutStep=0.05)
            if ind.mutant:
                self.mutantCount += 1

        stat1, pval1 = scistats.ttest_1samp(
            [1] * self.mutantCount + [0] *
            (self.nIndividuals - self.mutantCount), self.mutationRate)
        assert pval1 > 0.05, "T-test mean failed. Observed: {0}, Expected: {1}".format(
            self.mutantCount / self.nIndividuals, self.mutationRate)
        self.test = scistats.binom_test(self.mutantCount,
                                        self.nIndividuals,
                                        self.mutationRate,
                                        alternative="two-sided")
        assert self.test > 0.05, "Success rate = {0} when mutation rate = {1}".format(
            self.mutantCount / self.nIndividuals, self.mutationRate)

        gc.collect()
    def test_resources_info_augmented_at_each_routine(self):
        self.pop = Pop(par="test/test/parameters.txt")
        self.pop.create()
        for i in range(self.pop.routineSteps):
            self.pop.routine()

        assert self.pop.ecologyShortHistory.shape == ((self.pop.gridSize**2) *
                                                      self.pop.routineSteps, 4)
    def test_population_creates_grid(self):
        self.pop = Pop("test/test/parameters.txt")
        self.pop.create(n=20)

        assert hasattr(self.pop, "grid")
        assert type(self.pop.grid) is Grid
        assert self.pop.grid.resources.shape == (self.pop.gridSize,
                                                 self.pop.gridSize)
    def test_resources_crash_when_too_large(self):
        self.pop = Pop(par="test/test/parameters.txt")
        self.pop.initRes = 20
        self.pop.growth = 100
        self.pop.create()
        self.pop.lifeCycle()

        for cell in np.nditer(self.pop.grid.resources):
            assert cell <= self.pop.initRes, "resources too large, should have crashed"
    def test_exploration_info_augmented_at_each_routine(self):
        self.pop = Pop(par="test/test/parameters.txt")
        self.pop.predation = 0
        self.pop.create()
        for i in range(self.pop.routineSteps):
            self.pop.routine()

        assert self.pop.explorationShortHistory.shape == (
            self.pop.nIndiv * self.pop.routineSteps, 4)
示例#11
0
    def test_fertility_returns_positive_float(self):
        self.fakepop = Pop("test/test/parameters.txt")
        self.fakepop.create()

        for ind in range(len(self.fakepop.individuals)):
            indiv = self.fakepop.individuals[ind]
            setattr(indiv, "storage", 1 + 9 * ind)
            indiv.reproduce(fecundity=2)
            assert type(indiv.fertility) is float
            assert indiv.fertility >= 0
    def test_update_replaces_old_gen_with_new_gen(self):
        self.pop = Pop("test/test/parameters.txt")
        self.pop.create()
        self.pop.routine()
        self.pop.reproduce()
        oldgen = self.pop.individuals
        self.pop.update()

        assert len(self.pop.individuals) == self.pop.nIndiv
        assert self.pop.individuals != oldgen
    def test_mutation_does_not_affect_phenotype_type(self):
        self.fakepop = Pop("test/test/parameters.txt")
        self.fakepop.create(n=1)
        self.indiv = self.fakepop.individuals[0]
        self.phen = self.indiv.vigilance

        self.indiv.mutate(mutRate=1, mutStep=0.05)
        assert type(self.indiv.vigilance) is float

        gc.collect()
    def test_population_routine_changes_resources_grid(self):
        self.pop = Pop("test/test/parameters.txt")
        self.pop.create()

        resG = self.pop.grid.resources

        self.pop.routine()

        compareGrids = self.pop.grid.resources != resG

        assert compareGrids.all()
    def test_update_returns_population_vigilance_info(self):
        self.pop = Pop(par="test/test/parameters.txt")
        self.pop.create()
        self.pop.routine()
        self.pop.reproduce()
        self.pop.update()

        assert hasattr(self.pop, "vigilance")
        assert self.pop.vigilance is not None
        assert type(self.pop.vigilance) is float
        assert 0 <= self.pop.vigilance <= 1
    def test_population_routine_changes_share_grid(self):
        self.pop = Pop("test/test/parameters.txt")
        self.pop.create()

        shareG = self.pop.grid.share

        self.pop.routine()

        compareGrids = self.pop.grid.share == shareG

        assert compareGrids.all() == False
 def test_lam_error_when_resources_too_big(self):
     self.pop = Pop(par="test/test/parameters.txt")
     self.pop.initRes = 20000000000000000000
     self.pop.create()
     try:
         self.pop.lifeCycle()
     except ValueError as e:
         assert str(
             e
         ) == 'lam value too large', "This program should fail at poisson random draw, not '{0}'".format(
             e)
示例#18
0
 def test_population_has_plotting_option(self):
     self.pop = Pop("test/test/parameters.txt")
     self.pop.create()
     try:
         self.pop.launch(dev='on')
     except TypeError as e:
         assert False, "allow for 'on' device to show plots"
     os.remove("output/vigilance_out.txt")
     os.remove("output/vigilance_out.gif")
     os.remove('output/resources_out.txt')
     os.remove('output/exploration_out.txt')
     os.remove('output/grid_out.gif')
示例#19
0
    def test_grid_gif_is_created(self):
        self.pop = Pop("test/test/parameters.txt")
        self.pop.create()
        self.pop.launch(dev='on')

        self.filesListRootOut = os.listdir("./output")
        assert "grid_out.gif" in self.filesListRootOut, "no output grid gif created"
        os.remove("output/vigilance_out.txt")
        os.remove("output/vigilance_out.gif")
        os.remove('output/resources_out.txt')
        os.remove('output/exploration_out.txt')
        os.remove('output/grid_out.gif')
    def test_mutants_are_defined(self):
        self.pop = Pop("test/test/parameters.txt")
        self.pop.create(n=1)
        self.indiv = self.pop.individuals[0]

        self.indiv.mutate(mutRate=0.5, mutStep=0.5)
        assert hasattr(
            self.indiv, "mutant"
        ), "We don't know if our individual is a mutant because it doesn't have this attribute"
        assert type(self.indiv.mutant) is bool

        gc.collect()
示例#21
0
    def test_fertility_increases_with_storage(self):
        self.fakepop = Pop("test/test/parameters.txt")
        self.fakepop.create()

        fertility = 0

        for ind in range(len(self.fakepop.individuals)):
            indiv = self.fakepop.individuals[ind]
            setattr(indiv, "storage", 1 + 9 * ind)
            indiv.reproduce(fecundity=2)
            assert indiv.fertility > fertility
            fertility = indiv.fertility
    def test_deviation_function_returns_float(self):
        self.fakepop = Pop("test/test/parameters.txt")
        self.fakepop.create(n=1)
        self.indiv = self.fakepop.individuals[0]
        self.v = self.indiv.vigilance

        for mutationBool in [True, False]:
            self.indiv.mutant = mutationBool
            self.indiv.deviate(mutStep=0.05)
            assert type(self.indiv.mutationDeviation) is float

        gc.collect()
    def test_mutants_get_deviation_from_phenotype(self):
        self.fakepop = Pop("test/test/parameters.txt")
        self.fakepop.create(n=1)
        self.indiv = self.fakepop.individuals[0]
        self.indiv.mutate(mutRate=1, mutStep=0.05)
        assert hasattr(
            self.indiv, "mutationDeviation"
        ), "Individual is a mutant: it needs to be set a deviation from phenotype"

        assert -1 < self.indiv.mutationDeviation < 1

        gc.collect()
    def test_population_exploration_gives_share_info(self):
        self.pop = Pop("test/test/parameters.txt")
        self.pop.create()
        self.pop.explore()
        m = self.pop.gridSize

        assert hasattr(self.pop, "ncell")
        assert type(self.pop.ncell) is np.ndarray
        assert self.pop.ncell.shape == (m, m)
        assert hasattr(self.pop, "vcell")
        assert type(self.pop.vcell) is np.ndarray
        assert self.pop.vcell.shape == (m, m)
    def test_gathering_gives_individuals_storage(self):
        self.pop = Pop(par="test/test/parameters.txt")
        self.pop.create()
        self.pop.grid.share = np.full([self.pop.gridSize, self.pop.gridSize],
                                      0.8)

        for i in self.pop.individuals:
            i.vigilance = 0

        self.pop.gatherAndSurvive()

        for i in self.pop.individuals:
            assert i.storage > 0
    def test_only_live_individuals_gather_and_survive(self):
        self.pop = Pop("test/test/parameters.txt")
        self.pop.create()
        self.pop.explore()

        for ind in self.pop.individuals:
            ind.alive = False

        self.pop.gatherAndSurvive()

        for ind in self.pop.individuals:
            assert ind.alive == False
            assert ind.storage == 0
    def test_individual_mutation_can_be_unbounded(self):
        self.fakepop = Pop("test/test/parameters.txt")
        self.fakepop.nIndiv = 100
        self.fakepop.initialVigilance = 1
        self.fakepop.create()

        collectPhenotypes = []

        for ind in self.fakepop.individuals:
            ind.mutate(1, 0.5, bounded=False)
            collectPhenotypes.append(ind.vigilance)

        assert any([i > 1 for i in collectPhenotypes
                    ]), "no phenotype went over 1, even when unbounded."
    def test_population_vigilance_is_correct(self):
        self.pop = Pop(par="test/test/parameters.txt")
        self.pop.create()
        self.pop.routine()
        self.pop.reproduce()
        self.pop.update()

        collectVigilances = []

        for ind in self.pop.individuals:
            collectVigilances.append(ind.vigilance)

        assert self.pop.vigilance == pytest.approx(np.mean(collectVigilances),
                                                   0.1)
    def test_mutation_function_takes_and_returns_phenotype(self):
        self.pop = Pop("test/test/parameters.txt")
        self.pop.create(n=1)
        self.indiv = self.pop.individuals[0]

        self.indiv.mutate(mutRate=0.5, mutStep=0.5)
        x = self.indiv.vigilance
        assert type(
            x
        ) is float, "Phenotypic values ({0}) must be of type float, and not {1}".format(
            x, type(x))
        assert 0 <= x <= 1, "Phenotypic values must be in range [0,1]"

        gc.collect()
    def test_mutants_depend_on_seed(self, pseudorandom):
        self.nIndividuals = 1000
        self.fakepop = Pop("test/test/parameters.txt")
        self.fakepop.create(n=self.nIndividuals)

        mutants = []

        for ind in self.fakepop.individuals:
            pseudorandom(0)
            ind.mutate(mutRate=0.3, mutStep=0.1)
            mutants.append(ind.mutant)

        assert all(mutants) or not any(
            mutants), "Mutant does not depend on seed: {0}".format(
                set(mutants))