def test_animal_death(): island_map = "OOO\nOJO\nOOO" ini_herbs = [{ "loc": (1, 1), "pop": [{ "species": "Herbivore", "age": 5, "weight": rd.randint(1, 5) } for _ in range(150)], }] ini_carns = [{ "loc": (1, 1), "pop": [{ "species": "Carnivore", "age": 5, "weight": rd.randint(1, 5) } for _ in range(40)], }] t = BioSim(island_map, ini_herbs, None) t.add_population(ini_carns) loc = (1, 1) cell_object = t.island.cells[loc] cell_object.die() assert len(cell_object.population["Carnivore"]) < 40 assert len(cell_object.population["Herbivore"]) < 150
def test_population_to_cell(): """ Tests that you can add and store animals in a cell of the map. """ sim = BioSim(island_map="OOO\nOJO\nOOO", ini_pop=[{ "loc": (1, 1), "pop": [{ "species": "Herbivore", "age": 1, "weight": 15.0 }] }], seed=0) assert len(sim.map.array_map[1, 1].present_herbivores) == 1 sim.add_population([ { "loc": (1, 1), "pop": [ { "species": "Herbivore", "age": 1, "weight": 10.0 }, { "species": "Carnivore", "age": 1, "weight": 10.0 }, ], }, ]) assert len(sim.map.array_map[1, 1].present_herbivores) + len( sim.map.array_map[1, 1].present_carnivores) == 3
def test_carnivore_feed(): """Many testes for the method 'carnivore_feed()': 1. Test carnivore weight increases after feeding herbivore """ island_map = "OOO\nOJO\nOOO" ini_herbs = [{ "loc": (1, 1), "pop": [{ "species": "Herbivore", "age": 5, "weight": 40 } for _ in range(150)], }] ini_carns = [{ "loc": (1, 1), "pop": [{ "species": "Carnivore", "age": 5, "weight": 40 } for _ in range(40)], }] t = BioSim(island_map, ini_herbs, None) t.add_population(ini_carns) loc = (1, 1) cell_object = t.island.cells[loc] carn_start_weight = np.sum(carn.weight for carn in cell_object.population['Carnivore']) cell_object.carnivore_feed() carn_new_weight = np.sum(carn.weight for carn in cell_object.population['Carnivore']) assert carn_start_weight < carn_new_weight
def biosim_with_animals(self): """ Create Biosim instance with population for further testing """ biosim = BioSim(island_map='WWWWW\nWLDHW\nWWWWW', plot_graph=False) biosim.add_population([{ "loc": (2, 2), "pop": [{ "species": "Carnivore", "age": 5, "weight": 20 } for _ in range(20)] }, { "loc": (2, 2), "pop": [{ "species": "Herbivore", "age": 5, "weight": None } for _ in range(50)] }, { "loc": (2, 3), "pop": [{ "species": "Herbivore", "age": 5, "weight": 0 } for _ in range(20)] }]) return biosim
def test_consistensy_simulation_one_cell_two_species(): ini_herb = [{ "species": "Herbivore", "age": 5, "weight": 20 } for _ in range(100)] ini_carn = [{ "species": "Carnivore", "age": 5, "weight": 20 } for _ in range(20)] ini_pop = [{"loc": (2, 2), "pop": ini_herb}] n_herb = 0 n_carn = 0 for _ in range(1, 51): sim = BioSim(island_map="WWW\nWLW\nWWW", ini_pop=ini_pop, tmean=True) sim.simulate(num_years=50, vis_years=1) sim.add_population([{"loc": (2, 2), "pop": ini_carn}]) sim.simulate(num_years=150, vis_years=1) n_herb += sim.mean["Herbivore"] n_carn += sim.mean["Carnivore"] n_herb /= 50 n_carn /= 50 assert n_herb == pt.approx(85, abs=17) assert n_carn == pt.approx(41, abs=10)
def test_set_param_on_single_animal(): island_setup = BioSim(island_map="WWW\nWLW\nWWW", seed=1234, ini_pop=[]) island_setup.set_animal_parameters("Herbivore", {"beta": 20, "gamma": 100}) island_setup.add_population([{ "loc": (2, 2), "pop": [{ 'species': 'Herbivore', 'age': 5, 'weight': 100 } for _ in range(1)] }]) animal = island_setup.island[(2, 2)].default["Herbivore"][0] assert animal.var["beta"] == 20 and animal.var["gamma"] == 100
def test_add_population(self): """ Tests the population is added to cell on island. Returns ------- """ s1 = BioSim(island_map=ISLE_MAP, ini_pop=INI_HERB, seed=123) assert len(s1.island.cells[1, 1].herbivores) == 20 s1.add_population(INI_HERB) assert len(s1.island.cells[1, 1].herbivores) == 40 assert len(s1.island.cells[1, 1].carnivores) == 0 s1.add_population(INI_CARN) assert len(s1.island.cells[1, 1].carnivores) == 10
def test_add_population(self): sim = BioSim() assert sim.island.map[(1, 2)].num_animals == 0 sim.add_population([{ 'loc': (1, 2), 'pop': [{ "species": "Herbivore", "age": 5, "weight": 40 }, { "species": "Carnivore", "age": 10, "weight": 14.5 }] }]) assert sim.island.map[(1, 2)].num_animals == 2
def test_set_param_more_animals(): island_setup = BioSim(island_map="WWW\nWLW\nWWW", seed=1234, ini_pop=[]) island_setup.set_animal_parameters("Herbivore", {"beta": 20, "gamma": 100}) island_setup.set_animal_parameters("Carnivore", {"eta": 20, "a_half": 100}) island_setup.add_population([{ "loc": (2, 2), "pop": [{ 'species': 'Herbivore', 'age': 5, 'weight': 100 }, { 'species': 'Carnivore', 'age': 5, 'weight': 100 }] }]) herb = island_setup.island[(2, 2)].default["Herbivore"][0] carn = island_setup.island[(2, 2)].default["Carnivore"][0] assert herb.var["beta"] == 20 and herb.var["gamma"] == 100 assert carn.var["eta"] == 20 and carn.var["a_half"] == 100
ini_herbs = [ {'loc': (5, 5), 'pop': [{'species': 'Herbivore', 'age': 5, 'weight': 50} for _ in range(50)]}] ini_carn = [ {'loc': (5, 5), 'pop': [{'species': 'Carnivore', 'age': 5, 'weight': 50} for _ in range(50)]}] kart = """\ WWWWWWWWWWWWWWWWWWWWW WWWWWWWWHWWWWLLLLLLLW WHHHHHLLLLWWLLLLLLLWW WHHHHHHHHHWWLLLLLLWWW WHHHHHLLLLLLLLLLLLWWW WHHHHHLLLDDLLLHLLLWWW WHHLLLLLDDDLLLHHHHWWW WWHHHHLLLDDLLLHWWWWWW WHHHLLLLLDDLLLLLLLWWW WHHHHLLLLDDLLLLWWWWWW WWHHHHLLLLLLLLWWWWWWW WWWHHHHLLLLLLLWWWWWWW WWWWWWWWWWWWWWWWWWWWW""" sim = BioSim(seed=123, ini_pop=ini_herbs, island_map=kart) sim.simulate(num_years=5) sim.add_population(ini_carn) sim.set_animal_parameters('Carnivore', {'infected': True}) sim.simulate(num_years=100)
}] sim = BioSim( island_map=geogr, ini_pop=ini_herbs, seed=123456) #cmax_animals={"Herbivore":100, "Carnivore": 50}) sim.set_animal_parameters('Herbivore', { 'mu': 1, 'omega': 0, 'gamma': 0, 'a_half': 1000 }) sim.set_animal_parameters('Carnivore', { 'mu': 1, 'omega': 0, 'gamma': 0, 'F': 0, 'a_half': 1000 }) sim.set_landscape_parameters("J", {"f_max": 700}) sim._img_pause_time = 1 sim.simulate(num_years=100, vis_years=1, img_years=2000) sim.add_population(population=ini_carns) sim.simulate(num_years=100, vis_years=1, img_years=2000) plt.savefig("check_sim.pdf") input("Press ENTER")
kart = """\ WWWWWWWWWW WDDDDDDDDW WDDDDDDDDW WDDDDDDDDW WDDDDDDDDW WDDDDDDDDW WDDDDDDDDW WDDDDDDDDW WDDDDDDDDW WWWWWWWWWW""" b = BioSim(seed=123, ini_pop=ini_herbs, island_map=kart) b.set_animal_parameters('Herbivore', { 'mu': 1, 'omega': 0, 'gamma': 0, 'a_half': 10000 }) b.set_animal_parameters('Carnivore', { 'mu': 1, 'omega': 0, 'gamma': 0, 'F': 0, 'a_half': 10000 }) b.add_population(ini_carn) b.simulate(num_years=10)
img_base=None, ymax_animals=200, cmax_animals=c_max) print( k.add_population([ { "loc": (2, 2), "pop": [{ "species": "Herbivore", "age": 9, "weight": 45.0 }, { "species": "Herbivore", "age": 5, "weight": 17.0 }, { "species": "Herbivore", "age": 9, "weight": 45.0 }, { "species": "Herbivore", "age": 5, "weight": 17.0 }], }, ])) k.simulate(50) print(k.num_animals, 'live animals at year', k.year) print('Added carnivores to simulation') k.add_population([ {
class TestSimulation(object): geo = """OOOOOOO OJJJJJO OJSMSJO OSDJDSO OJSMSJO OJJJJJO OOOOOOO""" def __init__(self): self.original_herb = Herbivore.params self.original_carn = Carnivore.params self.original_Jungle = Jungle.params self.original_Savannah = Savannah.params self.sim = None def setup(self): ini_carns = [{ 'loc': (5, 3), 'pop': [{ 'species': 'Carnivore', 'age': 5, 'weight': 20 } for _ in xrange(20)] }] ini_herbs = [{ 'loc': (3, 3), 'pop': [{ 'species': 'Herbivore', 'age': 5, 'weight': 20 } for _ in xrange(80)] }] ini_pop = ini_carns + ini_herbs self.sim = BioSim(self.geo, ini_pop, 123456) def teardown(self): Herbivore.params = self.original_herb Carnivore.params = self.original_carn Jungle.params = self.original_Jungle Savannah.params = self.original_Savannah def test_add_population(self): """ Test that add_population function adds population in right cell with given weight and age. """ num_carnivores = 5 num_herbivores = 10 herbs = [{ 'loc': (2, 2), 'pop': [{ 'species': 'Herbivore', 'age': 8, 'weight': 40 } for _ in xrange(num_herbivores)] }] carns = [{ 'loc': (2, 2), 'pop': [{ 'species': 'Carnivore', 'age': 7, 'weight': 30 } for _ in xrange(num_carnivores)] }] self.sim.add_population(herbs + carns) nt.assert_equal(num_carnivores, len(self.sim.island.island[1][1].carnivores)) nt.assert_equal(num_herbivores, len(self.sim.island.island[1][1].herbivores)) for carn in self.sim.island.island[1][1].carnivores: nt.assert_equal(carn.age, 7) nt.assert_equal(carn.weight, 30) for herb in self.sim.island.island[1][1].herbivores: nt.assert_equal(herb.age, 8) nt.assert_equal(herb.weight, 40) herbs_ = [{ 'loc': (2, 2), 'pop': [{ 'species': 'Herbivore', 'age': 8, 'weight': 40 }] }] self.sim.add_population(herbs_) def test_heatmap(self): """ Tests that heatmap makes array with correct number of herbivores. :return: """ Carnivore.set_parameters({"gamma": 0, "omega": 0, "mu": 0}) Herbivore.set_parameters({"gamma": 0, "omega": 0, "mu": 0}) initial_herbivores = 80 initial_carnivores = 20 herbivores, carnivores = self.sim.heatmap(self.sim.island.one_year()) nt.assert_equal(initial_carnivores, np.sum(carnivores)) nt.assert_equal(initial_herbivores, np.sum(herbivores)) nt.assert_equal(initial_herbivores, herbivores[2][2]) nt.assert_equal(initial_carnivores, carnivores[4][2]) @staticmethod def test_standard_arguments(): """ Tests that simulation works without any given arguments. """ initial_herbivores = 150 initial_carnivores = 40 sim_ = BioSim() Carnivore.set_parameters({"gamma": 0, "omega": 0, "mu": 0}) Herbivore.set_parameters({"gamma": 0, "omega": 0, "mu": 1}) for row in sim_.island.island: for cell in row: for herb in cell.herbivores: herb.fitness = 1 sim_.island.migration() herbivores, carnivores = sim_.heatmap(sim_.island.one_year()) nt.assert_equal(initial_carnivores, np.sum(carnivores)) nt.assert_equal(initial_herbivores, np.sum(herbivores)) nt.assert_equal(initial_carnivores, carnivores[2][2]) def test_number_animals(self): """ Tests that the animal function works """ total = 100 herbs = 80 carns = 20 animals = self.sim.animal() nt.assert_equal(herbs, animals["Herbivores"]) nt.assert_equal(carns, animals["Carnivores"]) nt.assert_equal(total, animals["Herbivores"] + animals["Carnivores"])