def test_populate_the_island(): """ Tests that the function puts an animal population in the wanted cells. """ island_map = """\ OOOOOOO OSSSSSO OSJJJSO OSJMJSO OSDDJSO OSJJJSO OSSSSSO OOOOOOO""" island = bi.Island(island_map) popgen = pg.Population(n_herbivores=3, coord_herb=[(5, 2), (2, 5), (4, 3)]) pop = popgen.get_animals() island.populate_the_island(pop) assert len(island.numpy_map[5][2].animal_population[0]) == 3 assert isinstance(island.numpy_map[5][2], bl.Jungle) assert len(island.numpy_map[2][5].animal_population[0]) == 3 assert isinstance(island.numpy_map[2][5], bl.Savannah) assert len(island.numpy_map[4][3].animal_population[0]) == 3 assert isinstance(island.numpy_map[4][3], bl.Desert)
def test_total_population(): """ Tests if the function can find the correct herbivore, carnivore and total animal population of the island. """ island_map = """\ OOOOOOO OSSSSSO OSJJJSO OSJMJSO OSDDJSO OSJJJSO OSSSSSO OOOOOOO""" island = bi.Island(island_map) popgen = pg.Population(n_herbivores=3, coord_herb=[(5, 2), (2, 5), (4, 3)], n_carnivores=2, coord_carn=[(5, 3), (1, 5)]) pop = popgen.get_animals() island.populate_the_island(pop) species_population = island.total_species_population total_population = island.total_island_population assert species_population == (9, 4) assert total_population == 13
def test_population_in_each_cell(): """ Tests if the function counts the population in each cell correctly. """ island_map = """\ OOOOOOO OSSSSSO OSJJJSO OSJMJSO OSDDJSO OSJJJSO OSSSSSO OOOOOOO""" island = bi.Island(island_map) popgen = pg.Population(n_herbivores=3, coord_herb=[(5, 2), (2, 5), (4, 3)], n_carnivores=2, coord_carn=[(5, 3), (1, 5)]) pop = popgen.get_animals() island.populate_the_island(pop) cell_populations = island.population_in_each_cell assert cell_populations[19][2] == 3 assert cell_populations[37][2] == 3 assert cell_populations[31][2] == 3 assert cell_populations[12][3] == 2 assert cell_populations[38][3] == 2 assert cell_populations[36].all() == 0
def standard_map_peninsula(): """Creates an populated test island""" geogr = """\ OOOOOOOOOOOOOOOOOOOOO OOOOOOOOSMMMMJJJJJJJO OSSSSSJJJJMMJJJJJJJOO OSSSSSSSSSMMJJJJJJOOO OSSSSSJJJJJJJJJJJJOOO OSSSSSJJJDDJJJSJJJOOO OSSJJJJJDDDJJJSSSSOOO OOSSSSJJJDDJJJSOOOOOO OSSSJJJJJDDJJJJJJJOOO OSSSSJJJJDDJJJJOOOOOO OOSSSSJJJJJJJJOOOOOOO OOOSSSSJJJJJJJOOOOOOO OOOOOOOOOOOOOOOOOOOOO""" island = isle.Island(geogr) occupants = [{ 'loc': (1, 19), 'pop': [{ 'species': 'Herbivore', 'age': 9, 'weight': 10 }, { 'species': 'Carnivore', 'age': 9, 'weight': 10 }] }] island.populate_island(occupants) return island
def test_find_cell_position(): """ Tests if the function find correct cell positions. """ island = bi.Island() positions = [] for cell in island.numpy_map[0][0:5]: positions.append(island.find_cell_position(cell)) true_positions = [(0, 0), (0, 1), (0, 2), (0, 3), (0, 4)] assert positions == true_positions
def test_find_surrounding_cells(): """ Tests if the function finds the wanted neighbouring cells for a specific cell. """ island_map = """\ OOOO ODOO OSJO OMOO OOOO""" island = bi.Island(island_map) neighbour_cells = island.find_surrounding_cells([2, 1]) assert isinstance(neighbour_cells[0], bl.Mountain) assert isinstance(neighbour_cells[1], bl.Desert) assert isinstance(neighbour_cells[2], bl.Jungle) assert isinstance(neighbour_cells[3], bl.Ocean)
def test_landscape_position_in_map(): """ Tests that a numpy array is created from the method. """ island_map = """\ OOOOOOO OSSSSSO OSJJJSO OSJMJSO OSJDJSO OSJJJSO OSSSSSO OOOOOOO""" island = bi.Island(island_map) assert isinstance(island.numpy_map, np.ndarray) assert isinstance(island.numpy_map[0][0], bl.Ocean) assert isinstance(island.numpy_map[3][3], bl.Mountain) assert isinstance(island.numpy_map[4][3], bl.Desert) assert isinstance(island.numpy_map[3][2], bl.Jungle) assert isinstance(island.numpy_map[4][5], bl.Savannah)
def test_island_instance(): """ Tests whether an Island instance can be created. """ island = bi.Island() assert isinstance(island, bi.Island)
def test_animals_on_island(): island_map = "OOOO\nOJSO\nOOOO" ini_pop = [] island = bi.Island(island_map) island.populate_the_island(ini_pop) assert island.total_island_population == 0
def __init__( self, island_map, ini_pop, seed, ymax_animals=None, cmax_animals=None, img_base=None, img_fmt="png", ): """ :param island_map: Multi-line string specifying island geography :param ini_pop: List of dictionaries specifying initial population :param seed: Integer used as random number seed :param ymax_animals: Number specifying y-axis limit for graph showing animal numbers :param cmax_animals: Dict specifying color-code limits for animal densities :param img_base: String with beginning of file name for figures, including path :param img_fmt: String with file type for figures, e.g. 'png' If ymax_animals is None, the y-axis limit should be adjusted automatically. If cmax_animals is None, sensible, fixed default values should be used. cmax_animals is a dict mapping species names to numbers, e.g., {'Herbivore': 50, 'Carnivore': 20} If img_base is None, no figures are written to file. Filenames are formed as '{}_{:05d}.{}'.format(img_base, img_no, img_fmt) where img_no are consecutive image numbers starting from 0. img_base should contain a path and beginning of a file name. """ random.seed(seed) self.last_year_simulated = 0 self.island_map = island_map self.ini_pop = ini_pop self.island = bi.Island(island_map=island_map) self.island.populate_the_island(ini_pop) self.herbivore_list = [self.island.total_species_population[0]] self.carnivore_list = [self.island.total_species_population[1]] self.ymax_animals = ymax_animals self.cmax_animals = cmax_animals self.img_base = img_base self.img_fmt = img_fmt self._img_ctr = 0 # the following will be initialized by setup_graphics self._fig = None self._map_ax = None self._map_axis = None self._pop_ax = None self._pop_axis = None self._herb_heat_ax = None self._herb_heat_axis = None self._carn_heat_ax = None self._carn_heat_axis = None