class TestSnapshotBehaviour(TestCase): def setUp(self): self.model = Model() self.model.objects += [Tree(1, 1, dbh=30.2, height=20, species='Quercus mongolica', stage='adult')] self.model.objects += [Tree(0, 1, dbh=18.0, height=20, species='Quercus mongolica', stage='sapling')] self.model.objects += [Tree(1, 0, ddh=3.0, height=20, species='Quercus mongolica', stage='seedling')] self.model.insert_action(ConstantRadialGrowthAction()) self.snapshooter = Snapshots() # Use memory backend by default def test_record_ten_timesteps(self): olddbh = self.model.objects[0].dbh self.snapshooter.attach_model(self.model) self.snapshooter.record(until=10) mstate = self.snapshooter.get_snapshot(0) self.assertEqual(mstate.objects[0].dbh, olddbh) mstate1 = self.snapshooter.get_snapshot(1) self.assertGreater(mstate1.objects[0].dbh, olddbh) def test_init_laststates_properties(self): olddbh = self.model.objects[0].dbh self.snapshooter.attach_model(self.model) self.snapshooter.record(until=10) self.assertEqual(self.snapshooter.initstate.objects[0].dbh, olddbh) self.assertGreater(self.snapshooter.laststate.objects[0].dbh, olddbh) def test_serialize_model(self): '''Simple check for serializing deserializing model instance''' ss = BytesIO() pickle.dump(self.model, ss) ss.seek(0) mm = pickle.loads(ss.read()) self.assertIsInstance(mm, Model) self.assertEqual(mm.objects[0].dbh, self.model.objects[0].dbh)
class TestModelCreation(TestCase): def setUp(self): self.model = Model() self.model_with_pars = Model() # ------------- set ups of objects ------------- actions = [ AbstractAction(model=self.model_with_pars), AbstractAction(model=self.model_with_pars) ] events = [AbstractEvent(model=self.model_with_pars)] resources = [ AbstractResource(model=self.model_with_pars), AbstractResource(model=self.model_with_pars), AbstractResource(model=self.model_with_pars), ] self.model_with_pars.actions = actions self.model_with_pars.events = events self.model_with_pars.resources = resources def test_settingup_resources(self): testdata = {'value': 1, 'name': 'noname'} for item in self.model_with_pars.resources: item.data = testdata for item in self.model_with_pars.resources: self.assertEqual(item.data, testdata) def test_models_created(self): self.assertIsInstance(self.model, Model) self.assertIsInstance(self.model_with_pars, Model) def test_actions_inside(self): for item in self.model_with_pars.actions: self.assertIsInstance(item, AbstractAction) def test_events_inside(self): for item in self.model_with_pars.events: self.assertIsInstance(item, AbstractEvent) def test_using_insert_event(self): self.model_with_pars.events = [] event = AbstractEvent() self.model_with_pars.insert_event(event) self.assertIsInstance(self.model_with_pars.events[0].model, Model) def test_using_insert_action(self): self.model_with_pars.actions = [] action = AbstractAction() self.model_with_pars.insert_action(action) self.assertIsInstance(self.model_with_pars.actions[0].model, Model) def test_object_container_select_by_type(self): tree = Tree(1, 1, species='Mongolian Oak', dbh=30, height=26, ddh=7) shrub = Shrub(3, 3, species='Nothing', height=26, diam=7) self.model.objects.append(tree) self.model.objects.append(shrub) self.assertEqual(self.model.objects.select_by_type('tree')[0], tree) self.assertEqual(self.model.objects.select_by_type('shrub')[0], shrub)
class TestCommonRunnerStructure(TestCase): def setUp(self): self.model = Model() self.model.objects += [Tree(1, 1, dbh=30.2, height=20, species='Quercus mongolica', stage='adult')] self.model.objects += [Tree(0, 1, dbh=18.0, height=20, species='Quercus mongolica', stage='sapling')] self.model.objects += [Tree(1, 0, ddh=3.0, height=20, species='Quercus mongolica', stage='seedling')] def test_4_stages_structure(self): self.assertIsInstance(self.model._actions_pre, list) self.assertIsInstance(self.model._actions_post, list) self.assertIsInstance(self.model.actions, list) self.assertIsInstance(self.model.events, list) def test_run_exists(self): self.assertTrue(hasattr(self.model, 'run')) def test_run_callable(self): self.assertTrue(inspect.ismethod(getattr(self.model, 'run'))) def test_run_has_until_kwarg(self): # Could be used `__code__`, but I prefer inspect... self.assertIn('until', inspect.getargspec(self.model.run).args) def test_run_has_until_eq_1(self): self.assertEqual((1,), inspect.getargspec(self.model.run).defaults) def test_actions_to_stream(self): self.model._to_stream([[0], 1, 2, [3, [4, 5]]]) self.assertEqual(list(self.model._action_stream), list(range(6))) def test_exec_ten_empty_steps(self): self.model.run(until=10) self.assertEqual(self.model.step, 10) def test_history_was_recordered(self): olddbh = self.model.objects[0].get_dbh_history() self.assertIsNone(olddbh) self.model.run(until=10) histdbh = self.model.objects[0].get_dbh_history() # History is remembered for 5 steps only self.assertEqual(list(histdbh), [30.2]*5) def test_simple_growth(self): self.model.insert_action(ConstantRadialGrowthAction()) self.model.run(until=10) histdbh = self.model.objects[0].get_dbh_history() self.assertNotEqual(histdbh[0], histdbh[-1]) self.assertGreater(histdbh[-1], histdbh[-2]) def test_sapling_to_adult_autotransform(self): self.model.insert_action(ConstantRadialGrowthAction()) self.assertEqual(self.model.objects[-2].stage, 'sapling') self.model.run(until=10) self.assertEqual(self.model.objects[-2].stage, 'adult') def test_seedling_to_sapling_autotransform(self): self.model.insert_action(ConstantRadialGrowthAction()) self.assertEqual(self.model.objects[-1].stage, 'seedling') self.model.run(until=10) self.assertEqual(self.model.objects[-1].stage, 'sapling') def test_run_generator_works(self): 'Rungenerator is needed for making snapshots in time' self.model.insert_action(ConstantRadialGrowthAction()) zz = self.model._run_generator(until=10) self.assertIsInstance(next(zz), Model) self.assertEqual(next(zz).step, 2) self.assertEqual(next(zz).step, 3) self.assertEqual(next(zz).step, 4)
from pyforest.actions.spreads import UniformDisperseTreeAction from pyforest.core import Model from pyforest.events.random import CreateRandomSimpleTrees from pyforest.visual.D2 import Scene2D m = Model(bbox=[(0, 10), (0, 40)], data={'orientation': {'xaxis': 45}}) rnd_trees = CreateRandomSimpleTrees(treenum=100, species=list( map(lambda x: str(x), range(10))), bbox=[(0, 10), (0, 40)], heights=[0, 15], dbhs=[10, 100]) myseeding = UniformDisperseTreeAction(intensity=50, distribution='uniform', species='oak tree') m.insert_action(myseeding) m.insert_event(rnd_trees) m.events[0].activate() m.actions[0].activate() m.step += 1 m.actions[0].activate() d2 = Scene2D() d2.plot_model(m) d2.show()
class TestWorkingWithActions(TestCase): ''' Actions & events controls: prepending, appending, insertion''' def setUp(self): self.model = Model() self.model.insert_action(KillWhenTooLowGrowthSpeedAction()) class C(AbstractEvent): pass class D(AbstractEvent): pass self.A = C() self.B = D() self.CA = C self.CB = D def test_append_action(self): self.model.insert_action(HarvestSeedsRandomlyAction()) self.assertIsInstance(self.model.actions[-1], HarvestSeedsRandomlyAction) self.assertIsInstance(self.model.actions[0], KillWhenTooLowGrowthSpeedAction) self.assertNotIsInstance(self.model.actions[0], HarvestSeedsRandomlyAction) def test_prepend_action(self): self.model.prepend_action(HarvestSeedsRandomlyAction()) self.assertIsInstance(self.model.actions[0], HarvestSeedsRandomlyAction) self.assertIsInstance(self.model.actions[-1], KillWhenTooLowGrowthSpeedAction) def test_append_event(self): self.model.insert_event(self.A) self.model.insert_event(self.B) self.assertIsInstance(self.model.events[-1], self.CB) self.assertIsInstance(self.model.events[0], self.CA) def test_prepend_event(self): self.model.prepend_event(self.A) self.model.prepend_event(self.B) self.assertIsInstance(self.model.events[0], self.CB) self.assertIsInstance(self.model.events[-1], self.CA) def test_insert_in_position_actions(self): self.model.insert_action(AbstractAction()) self.model.insert_action(AbstractAction()) self.model.insert_action(HarvestSeedsRandomlyAction(), 1) self.assertIsInstance(self.model.actions[0], KillWhenTooLowGrowthSpeedAction) self.assertIsInstance(self.model.actions[1], AbstractAction) self.assertIsInstance(self.model.actions[2], HarvestSeedsRandomlyAction) self.assertIsInstance(self.model.actions[3], AbstractAction) def test_insert_in_poisition_events(self): self.model.insert_event(AbstractEvent()) self.model.insert_event(self.A) self.model.insert_event(self.B, 0) self.assertIsInstance(self.model.events[0], AbstractEvent) self.assertIsInstance(self.model.events[1], self.CB) self.assertIsInstance(self.model.events[2], self.CA)