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)
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 setUp(self): self.mem = MemoryBackend() self.m1 = Model() self.m2 = Model() self.m3 = Model() self.m1.objects = [1, 2, 3] self.m2.objects = [1, 2, 4] self.mem.extend([(1, self.m1), (2, self.m2), (3, self.m3)])
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)
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 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
class TestSimpleSolarIrradiance(TestCase): def setUp(self): self.model = Model() self.model.data = {'orientation': {'xaxis': 3.0 * np.pi / 2.0}} self.model.insert_resource(SimpleSolarIrradianceMixin()) self.res = self.model.resources[0] def test_get_direction_default(self): direction = self.res._get_direction() np.testing.assert_almost_equal(self.res.sundirection, direction) def test_get_direction_sun_from_north(self): res = SimpleSolarIrradianceMixin( sundirection=np.array([0, -1.0 / np.sqrt(2), -1 / np.sqrt(2)])) self.model.insert_resource(res) direction = self.model.resources[-1]._get_direction() np.testing.assert_almost_equal(direction, np.array(res.sundirection)) def test_get_direction_sun_from_east(self): res = SimpleSolarIrradianceMixin( sundirection=np.array([-1.0, 0.0, 0.0])) self.model.insert_resource(res) direction = self.model.resources[-1]._get_direction() np.testing.assert_almost_equal(direction, np.array(res.sundirection)) def test_get_direction_modified_orientation(self): self.model.data = {'orientation': {'xaxis': np.pi}} direction = self.res._get_direction() np.testing.assert_almost_equal( direction, np.array([-1.0 / np.sqrt(2.0), 0, -1.0 / np.sqrt(2.0)]))
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()
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')]
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)
def setUp(self): self.model = Model()
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)
from pyforest.core import Model from pyforest.events.random import CreateRandomSimpleTrees from pyforest.objects.data import pool pool.autodiscover('./objects/data') # ------------ Test Model initialization ----------------------- model = Model(bbox=[(0, 10), (0, 20)]) seedlings = CreateRandomSimpleTrees(treenum=1000, species=list(map(str, range(10))), bbox=[(0, 10), (0, 10)], heights=[0, 15], dbhs=[10, 100], stage='seedling' ) saplings = CreateRandomSimpleTrees(treenum=1000, species=list(map(str, range(10))), bbox=[(0, 10), (0, 10)], heights=[0, 15], dbhs=[10, 100], stage='sapling' ) adult = CreateRandomSimpleTrees(treenum=1000, species=list(map(str, range(10))), bbox=[(0, 10), (0, 10)], heights=[0, 15], dbhs=[10, 100], stage='adult' )
def setUp(self): self.model = Model() self.model.data = {'orientation': {'xaxis': 3.0 * np.pi / 2.0}} self.model.insert_resource(SimpleSolarIrradianceMixin()) self.res = self.model.resources[0]
KillOldSeedlings, KillOldSaplings) from pyforest.actions.spreads import RadialDecayTreeSeedingAction from pyforest.core import MemoryBackend, Model, Snapshots from pyforest.core import Model from pyforest.objects.trees import Tree # ------------ Initialization ----------------------------- # --------------------------------------------------------- # ------------ Create simple model instance --------------- model = Model() # --------------------------------------------------------- # ------------- Filling the model space with trees -------- model.objects += [Tree(1, 1, dbh=30.2, height=20, species='Quercus mongolica', stage='adult')] model.objects += [Tree(0, 1, dbh=18.0, height=20, species='Quercus mongolica', stage='sapling')] model.objects += [Tree(1, 0, ddh=3.0, height=20, species='Quercus mongolica', stage='seedling')]
from pyforest.core import Model from pyforest.events.random import CreateRandomSimpleTrees from pyforest.visual.D3 import pv m = Model(bbox=[(0, 20), (0, 40)]) rnd_trees = CreateRandomSimpleTrees(treenum=50, species=map(lambda x: str(x), range(10)), bbox=[(0, 20), (0, 40)], heights=[3, 10], dbhs=[10, 30], stage='sapling') m.insert_event(rnd_trees) m.events[0].activate() d2 = pv.ParaviewSimple3DScene(model=m) d2.plot_model()
''' Plots a few trees with different diameters ''' from pyforest.objects.trees import Tree from pyforest.core import Model from pyforest.visual.D2 import Scene2D m = Model(bbox=[(0, 10), (0, 40)]) t1 = Tree(1, 1, dbh=35, stage='adult') t2 = Tree(12, 2, dbh=15, stage='sapling') t3 = Tree(3, 13, ddh=1, dbh=0, stage='seedling') m.objects += [t1, t2, t3] d2 = Scene2D() d2.plot_model(m) d2.show()