Exemplo n.º 1
0
def test_integration():

    classifiers = {
        "c1": ["c1_v1"],
        "c2": ["c2_v1"],
    }
    merch_volumes = [{
        "classifier_set": ["c1_v1", "?"],
        "merch_volumes": [{
            "species":
            "Spruce",
            "age_volume_pairs": [
                [0, 0],
                [50, 100],
                [100, 150],
                [150, 200],
            ]
        }]
    }]

    cbm_factory = StandCBMFactory(classifiers, merch_volumes)

    n_steps = 10
    return_interval = 50
    n_rotations = 5
    age = 15
    inventory = pd.DataFrame(columns=[
        "c1", "c2", "admin_boundary", "eco_boundary", "age", "area", "delay",
        "land_class", "afforestation_pre_type", "historic_disturbance_type",
        "last_pass_disturbance_type"
    ],
                             data=[[
                                 "c1_v1", "c2_v1", "British Columbia",
                                 "Pacific Maritime", age, 1.0, 0,
                                 "UNFCCC_FL_R_FL", None, "Wildfire", "Wildfire"
                             ]])
    csets, inv = cbm_factory.prepare_inventory(inventory)
    n_stands = len(inv.index)
    with cbm_factory.initialize_cbm() as cbm:

        spinup_results, spinup_reporting_func = \
            cbm_simulator.create_in_memory_reporting_func(density=True)
        cbm_results, cbm_reporting_func = \
            cbm_simulator.create_in_memory_reporting_func(
                classifier_map=cbm_factory.get_classifier_map(),
                disturbance_type_map=cbm_factory.get_disturbance_type_map())
        cbm_simulator.simulate(
            cbm,
            n_steps=n_steps,
            classifiers=csets,
            inventory=inv,
            pre_dynamics_func=lambda t, cbm_vars: cbm_vars,
            reporting_func=cbm_reporting_func,
            spinup_params=cbm_variables.initialize_spinup_parameters(
                n_stands, return_interval, n_rotations, n_rotations, -1),
            spinup_reporting_func=spinup_reporting_func)
        assert len(cbm_results.pools.index) == (n_steps + 1) * n_stands
        assert (len(
            spinup_results.pools.index) == (n_rotations * return_interval) +
                age - 1)
Exemplo n.º 2
0
    def test_integration_with_tutorial2_eligbilities(self):
        """tests full CBM integration with rule based disturbances and
        disturbance event eligibility expressions
        """
        config_path = os.path.join(
            resources.get_test_resources_dir(),
            "cbm3_tutorial2_eligibilities", "sit_config.json")
        sit = sit_cbm_factory.load_sit(config_path)
        classifiers, inventory = \
            sit_cbm_factory.initialize_inventory(sit)
        with sit_cbm_factory.initialize_cbm(sit) as cbm:
            results, reporting_func = \
                cbm_simulator.create_in_memory_reporting_func()
            rule_based_processor = \
                sit_cbm_factory.create_sit_rule_based_processor(sit, cbm)

            cbm_simulator.simulate(
                cbm,
                n_steps=1,
                classifiers=classifiers,
                inventory=inventory,
                pre_dynamics_func=rule_based_processor.pre_dynamics_func,
                reporting_func=reporting_func)
            self.assertTrue(
                results.pools[results.pools.timestep == 0].shape[0] ==
                inventory.shape[0])
            self.assertTrue(
                len(rule_based_processor.sit_event_stats_by_timestep) > 0)
Exemplo n.º 3
0
    def test_integration(self):

        sit = SimpleNamespace()
        sit.config = {
            "mapping_config": {
                "nonforest":
                None,
                "species": {
                    "species_classifier":
                    "classifier1",
                    "species_mapping": [{
                        "user_species": "a",
                        "default_species": "Spruce"
                    }]
                },
                "spatial_units": {
                    "mapping_mode": "SingleDefaultSpatialUnit",
                    "default_spuid": 42
                },
                "disturbance_types": [{
                    "user_dist_type": "fire",
                    "default_dist_type": "Wildfire"
                }]
            }
        }

        sit.sit_data = sit_reader.parse(
            sit_classifiers=pd.DataFrame(data=[(1, "_CLASSIFIER",
                                                "classifier1"), (1, "a",
                                                                 "a")]),
            sit_disturbance_types=pd.DataFrame(data=[("1", "fire")]),
            sit_age_classes=sit_age_class_parser.generate_sit_age_classes(
                5, 100),
            sit_inventory=pd.DataFrame(data=[("a", False, 100, 1, 0, 0, "1",
                                              "1")]),
            sit_yield=pd.DataFrame([["a", "a"] +
                                    [x * 15 for x in range(0, 20 + 1)]]),
            sit_events=None,
            sit_transitions=None)
        sit = sit_cbm_factory.initialize_sit_objects(sit)
        classifiers, inventory = sit_cbm_factory.initialize_inventory(sit)
        with sit_cbm_factory.initialize_cbm(sit) as cbm:
            results, reporting_func = \
                cbm_simulator.create_in_memory_reporting_func()
            cbm_simulator.simulate(
                cbm,
                n_steps=1,
                classifiers=classifiers,
                inventory=inventory,
                pre_dynamics_func=lambda time_step, cbm_vars: cbm_vars,
                reporting_func=reporting_func)
            # there should be 2 rows, timestep 0 and timestep 1
            self.assertTrue(results.pools.shape[0] == 2)
Exemplo n.º 4
0
 def run(self):
     """
     Call `libcbm_py` to run the actual CBM simulation after creating some
     objects.
     The interaction with `libcbm_py` is decomposed in several calls to pass
     a `.json` config, a default database (also called aidb) and csv files.
     """
     # Message #
     self.runner.log.info("Setting up the libcbm_py objects.")
     # The 'AIDB' path as it was called previously #
     db_path = self.runner.country.aidb.paths.db
     if not db_path:
         msg = "The database file at '%s' was not found."
         raise FileNotFoundError(msg % self.runner.country.aidb.paths.db)
     # Create a SIT object #
     json_path = str(self.runner.paths.json)
     self.sit = sit_cbm_factory.load_sit(json_path, str(db_path))
     # Do some initialization #
     init_inv = sit_cbm_factory.initialize_inventory
     self.clfrs, self.inv = init_inv(self.sit)
     # This will contain results #
     create_func = cbm_simulator.create_in_memory_reporting_func
     self.results, self.reporting_func = create_func()
     # Create a CBM object #
     with sit_cbm_factory.initialize_cbm(self.sit) as self.cbm:
         # Create a function to apply rule based events #
         create_proc = sit_cbm_factory.create_sit_rule_based_processor
         self.rule_based_proc = create_proc(self.sit, self.cbm)
         # Message #
         self.runner.log.info("Calling the cbm_simulator.")
         # Run #
         cbm_simulator.simulate(self.cbm,
                                n_steps=self.runner.num_timesteps,
                                classifiers=self.clfrs,
                                inventory=self.inv,
                                pre_dynamics_func=self.dynamics_func,
                                reporting_func=self.reporting_func)
     # If we got here then we did not encounter any simulation error #
     self.error = False
     # Return for convenience #
     return self.results