def test_simulate_function(self, input_map, ini_pop): s = BioSim(input_map, ini_pop, seed=123) prev_number_of_animals = s.num_animals_per_species s.simulate() assert s.num_animals_per_species['Herbivore'] !=\ prev_number_of_animals['Herbivore'] assert s.num_animals_per_species['Carnivore'] !=\ prev_number_of_animals['Carnivore']
def test_num_animals_and_add_population(self, input_map, ini_pop, add_carns): """""" s = BioSim(input_map, ini_pop, seed=1) assert s.num_animals_per_species['Herbivore'] > 0 assert s.num_animals_per_species['Carnivore'] > 0 prev_carns = s.num_animals_per_species['Carnivore'] s.add_population(add_carns) assert s.num_animals_per_species['Carnivore'] > prev_carns
def test_set_animal_parameters(self, input_map, ini_pop): """set_animal_parameters should change parameters successfully""" s = BioSim(island_map=input_map, ini_pop=ini_pop, seed=1) s.set_animal_parameters("Herbivore", {"zeta": 3.2, "xi": 1.8}) s.set_animal_parameters("Carnivore", {"zeta": 5.0, "xi": 2.0}) assert Herbivore.animal_params["zeta"] == 3.2 assert Herbivore.animal_params["xi"] == 1.8 assert Carnivore.animal_params["zeta"] == 5.0 assert Carnivore.animal_params["xi"] == 2.0
def test_figure_saved(figfile_root): """Test that figures are saved during simulation""" sim = BioSim( island_map="OOOO\nOJSO\nOOOO", ini_pop=[], seed=1, img_base=figfile_root, img_fmt="png", ) sim.simulate(2, vis_years=1, img_years=1) assert os.path.isfile(figfile_root + "_00000.png") assert os.path.isfile(figfile_root + "_00001.png")
def test_invalid_boundary(bad_boundary): """Non-ocean boundary must raise error""" with pytest.raises(ValueError): BioSim( island_map="{}OO\nOJO\nOOO".format(bad_boundary), ini_pop=[], seed=1, )
def test_set_plot_limits(): """Test that y-axis and color limits for plots can be set.""" BioSim( island_map="O", ini_pop=[], seed=1, ymax_animals=20, cmax_animals={ "Herbivore": 10, "Carnivore": 20 }, )
def test_animals_reproduce(self, input_map, ini_pop): """Animals should reproduce properly""" s = BioSim(input_map, ini_pop, seed=1) herb_count_prev = s.object_matrix[1][1].n_herbs carn_count_prev = s.object_matrix[1][1].n_carns c = Cycle(s.object_matrix) c.food_grows() c.animals_reproduce() herb_count_curr = s.object_matrix[1][1].n_herbs carn_count_curr = s.object_matrix[1][1].n_carns assert carn_count_curr > carn_count_prev, "Animals have not reproduced" assert herb_count_curr > herb_count_prev, "Animals have not reproduced"
def create_sim(self): """Creates a Simulation object""" map = """\ OOOOOOOOO OSSJJSSOO OOOOOOOOO """ ini_herbs = [ { "loc": (2, 2), "pop": [ {"species": "Herbivore", "age": 5, "weight": 20} for _ in range(200) ], } ] return BioSim(map, ini_herbs, seed=1)
def test_initial_population(): """Test that population can be placed on construction""" BioSim( island_map="OOOO\nOJSO\nOOOO", ini_pop=[ { "loc": (1, 1), "pop": [ { "species": "Herbivore", "age": 1, "weight": 10.0 }, { "species": "Carnivore", "age": 1, "weight": 10.0 }, ], }, { "loc": (1, 2), "pop": [ { "species": "Herbivore", "age": 1, "weight": 10.0 }, { "species": "Carnivore", "age": 1, "weight": 10.0 }, ], }, ], seed=1, )
def test_set_param_animals(species, extra): """Parameters can be set on animal classes""" params = { "w_birth": 8.0, "sigma_birth": 1.5, "beta": 0.9, "eta": 0.05, "a_half": 40.0, "phi_age": 0.2, "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, } params.update(extra) BioSim(island_map="O", ini_pop=[], seed=1).set_animal_parameters(species, params)
def create_s(self, input_map, ini_pop): """Makes BioSim object""" return BioSim(input_map, ini_pop, seed=1)
def create_s(self, input_map, ini_pop): """Creates a Simulation object""" s = BioSim(input_map, ini_pop, seed=1) return s
def test_inconsistent_length(): """Inconsistent line length must raise error""" with pytest.raises(ValueError): BioSim(island_map="OOO\nOJJO\nOOO", ini_pop=[], seed=1)
def test_invalid_landscape(): """Invalid landscape type must raise error""" with pytest.raises(ValueError): BioSim(island_map="OOO\nORO\nOOO", ini_pop=[], seed=1)
from src.biosim.simulation import BioSim from src.biosim.animals import Carnivore, Herbivore, BaseAnimal default_population = [ { "loc": (3, 3), "pop": [{ "species": "Herbivore", "age": 5, "weight": 20 } for _ in range(2000)], }, ] sim = BioSim(island_map='OOOOOOO\n' 'ODDDDDO\n' 'ODDDDDO\n' 'ODDDDDO\n' 'ODDDDDO\n' 'ODDDDDO\n' 'OOOOOOO', ini_pop=default_population, img_base='migrate_checker', ymax_animals=3000) sim.set_animal_parameters('Herbivore', { 'mu': 1e10, 'omega': 1e-10, 'eta': 1e-10 }) print(Herbivore.mu, Herbivore.omega, Herbivore.eta, Herbivore.w_birth, Herbivore.sigma_birth) sim.make_movie()
OSSSSSJJJJJJJJJJJJOOO OSSSSSJJJDDJJJSJJJOOO OSSJJJJJDDDJJJSSSSOOO OOSSSSJJJDDJJJSOOOOOO OSSSJJJJJDDJJJJJJJOOO OSSSSJJJJDDJJJJOOOOOO OOSSSSJJJJJJJJOOOOOOO OOOSSSSJJJJJJJOOOOOOO OOOOOOOOOOOOOOOOOOOOO""" import textwrap geostring = textwrap.dedent(geogr) Landscape = Bio(geostring, poplist,seed =0, ymax_animals=None, cmax_animals=None, img_base=None, img_fmt="png") landscape, geostring_list = Landscape.create_landscape() print(landscape) herb, carn = Landscape.populate_island() print(herb) print(carn) age = [] weight = [] for animal in range(len(herb)): num_herb = len(herb) num_carn = len(carn) age.append(herb[animal]['age']) weight.append(herb[animal]['weight'])
def test_minimal_island(): """Island of single jungle cell""" BioSim(island_map="OOO\nOJO\nOOO", ini_pop=[], seed=1)
def test_empty_island(): """Empty island can be created""" BioSim(island_map="OO\nOO", ini_pop=[], seed=1)
def plain_sim(): """Return a simple island for used in various tests below""" return BioSim(island_map="OOOO\nOJSO\nOOOO", ini_pop=[], seed=1)
def test_set_param_landscape(lscape, params): """Parameters can be set on landscape classes""" BioSim(island_map="O", ini_pop=[], seed=1).set_landscape_parameters(lscape, params)
"pop": [{ "species": "Herbivore", "age": 5, "weight": 20 } for _ in range(150)], }] ini_carns = [{ "loc": (10, 10), "pop": [{ "species": "Carnivore", "age": 5, "weight": 20 } for _ in range(40)], }] sim = BioSim(island_map=geogr, ini_pop=ini_herbs, seed=123456) sim.set_animal_parameters("Herbivore", {"zeta": 3.2, "xi": 1.8}) sim.set_animal_parameters( "Carnivore", { "a_half": 70, "phi_age": 0.5, "omega": 0.3, "F": 65, "DeltaPhiMax": 9.0, }, ) sim.set_landscape_parameters("J", {"f_max": 700}) sim.simulate(num_years=100, vis_years=1, img_years=2000)
def test_all_types(): """All types of landscape can be created""" BioSim(island_map="OOOO\nOJSO\nOMDO\nOOOO", ini_pop=[], seed=1)
} for _ in range(100)], }] ini_carns = [{ "loc": (10, 10), "pop": [{ "species": "Carnivore", "age": 5, "weight": 20 } for _ in range(100)], }] # Should specify the total_years sim = BioSim(island_map=geogr, ini_pop=ini_herbs, seed=123456, total_years=2000, img_base='Raw_Images', cmax_animals={ 'Herbivore': 100, 'Carnivore': 100 }) sim.set_animal_parameters("Herbivore", {"zeta": 3.2, "xi": 1.8}) sim.set_animal_parameters( "Carnivore", { "a_half": 70, "phi_age": 0.5, "omega": 0.3, "F": 65, "DeltaPhiMax": 9.0, },
{"species": "Herbivore", "age": 5, "weight": 20} for _ in range(150) ], } ] ini_carns = [ { "loc": (10, 10), "pop": [ {"species": "Carnivore", "age": 5, "weight": 20} for _ in range(40) ], } ] sim = BioSim(ini_pop=ini_herbs, seed=123456) """ sim.set_animal_parameters("Herbivore", {"zeta": 3.2, "xi": 1.8}) sim.set_animal_parameters( "Carnivore", { "a_half": 70, "phi_age": 0.5, "omega": 0.3, "F": 65, "DeltaPhiMax": 9.0, }, ) sim.set_landscape_parameters("J", {"f_max": 700}) """