CBM-CFS3 support in libcbm

LibCBM has an implementation of CBM which is comparable with CBM-CFS3.

libcbm supports 2 primary methods for running the CBM-CFS3 implementation. These are via the CBM standard import tool format (SIT) and through a more basic multi-stand modelling framework.

Standard import tool (CBM-SIT) format

See cbm3-tutorial2 for a step by step example for running libcbm via SIT format in a jupyter notebook.

Code documentation for the SIT implementation CBM Standard Import tool format

Multi-Stand level modelling support

Libcbm can also run a simplified version of CBM, without the rule-based disturbances and transitions built into the SIT. This method may be more suitable for larger scale simulations with direct, many-to-one linkages for disturbance events to stands across timesteps.

See multi-stand-modelling for a step by step example of running libcbm this way.

Internal CBM Classes and functions

A CBM instance can be initialized by using a composed factory method.

libcbm.model.cbm.cbm_factory.create(dll_path: str, dll_config_factory: Callable[[], dict], cbm_parameters_factory: Callable[[], dict], merch_volume_to_biomass_factory: Callable[[], dict], classifiers_factory: Callable[[], dict]) Iterator[CBM]

Create and initialize an instance of the CBM model

Parameters:
  • dll_path (str) – path to the libcbm compiled library

  • dll_config_factory (func) – a function that returns dll configuration.

  • cbm_parameters_factory (func) – a function that returns CBM parameters.

  • merch_volume_to_biomass_factory (func) – function that creates a valid merchantable volume to biomass configuration for CBM (see: libcbm.model.cbm.cbm_config.merch_volume_to_biomass_config())

  • classifiers_factory (func) – function that creates a valid classifier configuration for CBM (see: libcbm.model.cbm.cbm_config.classifier_config())

In the following example a CBM instance is built with a single growth curve, and classifier set. The libcbm.model.cbm.cbm_defaults module is use to construct the other factory methods

Example:

from libcbm.model.cbm import cbm_defaults
from libcbm.model.cbm import cbm_factory
from libcbm.model.cbm import cbm_config

db_path = "cbm_defaults.db"
dll_path = "LibCBM.dll"

classifiers = lambda : cbm_config.classifier_config([
    cbm_config.classifier(
        "c1",
        values=[cbm_config.classifier_value("c1_v1")])
])

merch_volumes = lambda : cbm_config.merch_volume_to_biomass_config(
    db_path=db_path,
    merch_volume_curves=[
        cbm_config.merch_volume_curve(
        classifier_set=["c1_v1"],
        merch_volumes=[{
            "species_id": 1,
            "age_volume_pairs": [[0,0],[50,100],[100,150],[150,200]]
        }]
    )]
)

cbm = cbm_factory.create(
    dll_path=dll_path,
    dll_config_factory=cbm_defaults.get_libcbm_configuration_factory(
        db_path),
    cbm_parameters_factory=cbm_defaults.get_cbm_parameters_factory(
        db_path),
    merch_volume_to_biomass_factory=merch_volumes,
    classifiers_factory=classifiers)
Yields:

libcbm.model.cbm.CBM

an initialized instance of the CBM

model

The CBM class is a set of functions that run the CBM model including spinup, variable initialization and model stepping. It replicates the Carbon dynamics and stand state of the CBM-CFS3 model.

The CBM class consists of 3 methods for simulating Carbon dynamics

An instance of the CBM model can be created with the libcbm.model.cbm.cbm_factory() function.

class libcbm.model.cbm.cbm_model.CBM(compute_functions: LibCBMWrapper, model_functions: CBMWrapper, pool_codes: list[str], flux_indicator_codes: list[str])

The CBM model.

Parameters:
  • compute_functions (LibCBMWrapper) – an instance of LibCBMWrapper.

  • model_functions (CBMWrapper) – an instance of CBMWrapper

  • pool_codes (list) – list of pool code names (non localizable)

  • flux_indicator_codes (list) – list of flux indicator code names (non localizable)

compute_disturbance_production(cbm_vars: CBMVariables, disturbance_type: Series | int | None = None, eligible: Series | None = None, density: bool = True)

Computes a series of disturbance production values based on the current pools in cbm_vars, and disturbance matrices associated with cbm_vars.parameters.disturbance type by default, and the specified disturbance_type scalar, or array otherwise. Does not change values in cbm_vars.

Parameters:
  • cbm_vars (CBMVariables) – object containing current simulation state

  • disturbance_type (Series, int, optional) – The integer code or array specifying the disturbance type(s). If not specified, the value of cbm_vars.parameters.disturbance_type is used.

  • eligible (Series, optional) – Bit values where True specifies the index is eligible for the disturbance, and false the opposite. In the returned result False indices will be set with 0’s. Specifying None is equivant to an full array of True values. Defaults to None.

  • density (bool, optional) – if set to True the return value is expressed in units of tonnes Carbon/hectare, and if False the return value is expressed in units of tonnes Carbon. Defaults to True.

Returns:

dataframe describing the C production associated

with applying the specified disturbance type on the specified pools. All columns are expressed as area density values in units tonnes C/ha.

Fields:

  • DisturbanceSoftProduction: the softwood C production

  • DisturbanceSoftProduction: the hardwood C production

  • DisturbanceDOMProduction: the dead organic matter C

    production

  • Total: the row sums of the above three values

Return type:

DataFrame

init(cbm_vars: CBMVariables) CBMVariables

Set the initial state of CBM variables after spinup and prior to starting CBM simulation stepping

Parameters:

cbm_vars (CBMVariables) – cbm vars

Returns:

cbm_vars

Return type:

CBMVariables

spinup(cbm_vars: CBMVariables, reporting_func: Callable[[int, CBMVariables], None] | None = None) CBMVariables

Run the CBM-CFS3 spinup function on an array of stands, initializing the specified variables.

See libcbm.model.cbm.cbm_variables for initialization routines for the cbm_vars object.

Parameters:
  • cbm_vars (CBMVariables) – spinup CBM variables

  • reporting_func (function) – a function which accepts the spinup iteration spinup variables for reporting results by spinup iteration. The function returns None.

Returns:

cbm_vars

Return type:

CBMVariables

step(cbm_vars: CBMVariables) CBMVariables

Run all default cbm step methods. It is assumed that any records in the specified cbm_vars that require the spinup routine have been passed into the init() and spinup() and methods in this class.

Parameters:

cbm_vars (CBMVariables) – cbm_vars object

Returns:

cbm_vars

Return type:

CBMVariables

step_annual_process(cbm_vars: CBMVariables) CBMVariables

Compute CBM annual process dynamics: growth, turnover and decay. This updates the cbm_vars.pools value and cbm_vars.flux values with computed annual process dynamics

Parameters:

cbm_vars (CBMVariables) – cbm_vars object

Returns:

cbm_vars

Return type:

CBMVariables

step_disturbance(cbm_vars: CBMVariables) CBMVariables

Compute disturbance dynamics and compute disturbance flux on the current value of cbm_vars.pools. The values stored in array cbm_vars.parameters.disturbance_type determines the disturbance type applied to each stand row.

If conformance to CBM-CFS3 methodolgy is required, this function can safely be called multiple times per timestep, but note CBM-CFS does not support multiple disturbances to a single stand per timestep. This situation could arise if care is not taken to exclude stand indexes disturbed in prior calls to this function, within a given timestep, from all subsequent calls within that timestep.

Parameters:

cbm_vars (CBMVariables) – cbm_vars object

Returns:

cbm_vars

Return type:

CBMVariables

step_end(cbm_vars: CBMVariables) CBMVariables

Apply end of timestep state changes. Updates values in cbm_vars.state.

Parameters:

cbm_vars (CBMVariables) – cbm_vars object

Returns:

cbm_vars

Return type:

CBMVariables

step_start(cbm_vars: CBMVariables) CBMVariables

Advance stand state and initialize variables prior to disturbance and annual process steps. Should be called one time at the start of each CBM time step.

Parameters:

cbm_vars (CBMVariables) – cbm vars

Returns:

cbm_vars

Return type:

CBMVariables

Simulator

libcbm.model.cbm.cbm_simulator.simulate(cbm: CBM, n_steps: int, classifiers: DataFrame, inventory: DataFrame, reporting_func: Callable[[int, CBMVariables], None], pre_dynamics_func: Callable[[int, CBMVariables], CBMVariables] | None = None, spinup_params: DataFrame | None = None, spinup_reporting_func: Callable[[int, CBMVariables], None] | None = None, backend_type: BackendType | None = None)

Runs the specified number of timesteps of the CBM model. Model output is processed by the provided reporting_func. The provided pre_dynamics_func is called prior to each CBM dynamics step.

Parameters:
  • cbm (libcbm.model.cbm.cbm_model.CBM) – Instance of the CBM model

  • n_steps (int) – The number of CBM timesteps to run

  • classifiers (DataFrame) – CBM classifiers for each of the rows in the inventory

  • inventory (DataFrame) – CBM inventory which defines the initial state of the simulation

  • reporting_func (function) – a function which accepts the simulation timestep and all CBM variables for reporting results by timestep.

  • pre_dynamics_func (function, optional) – A function which accepts the simulation timestep and all CBM variables, and which is called prior to computing C dynamics. The function returns all CBM variables which will then be passed into the current CBM timestep.

  • spinup_params (object) – Collection of spinup specific parameters. If unspecified, CBM default values are used. See libcbm.model.cbm.cbm_variables.initialize_spinup_parameters() for object format

  • spinup_reporting_func (function, optional) – a function which accepts the spinup iteration, and all spinup variables. Specifying this function will result in a performance penalty as the per-iteration spinup results are computed and tracked. If unspecified spinup results are not tracked. Defaults to None.

  • backend_type (BackendType) – specifies the backend storage method for dataframes. If unspecified, the inventory data frame’s backend type is used.

CBM Input and Variables

CBM Variables class (cbm_vars)

class libcbm.model.cbm.cbm_variables.CBMVariables(pools: DataFrame, flux: DataFrame, classifiers: DataFrame, state: DataFrame, inventory: DataFrame, parameters: DataFrame)

CBM Variables are the entire set of state, variable, and stand specific parameters for CBM spinup and simulation. Data is stored in DataFrame objects. Several libcbm methods both accept and return CBMVariables.

property classifiers: DataFrame

Get or set CBM classifier storage

When setting, if the specified data frame’s backend_type differs from the existing dataframe’s type the assigned value will be converted to the existing type.

Returns:

classifier dataframe, each row represents a stand, and columns are each inventory classifier.

Return type:

DataFrame

property flux: DataFrame

Get or set CBM flux indicator storage

When setting, if the specified data frame’s backend_type differs from the existing dataframe’s type the assigned value will be converted to the existing type.

Returns:

flux dataframe, each row represents a stand, and columns are each CBM flux indicator

Return type:

DataFrame

property inventory: DataFrame

Get or set CBM inventory storage

When setting, if the specified data frame’s backend_type differs from the existing dataframe’s type the assigned value will be converted to the existing type.

Returns:

state dataframe, each row represents a stand.

Return type:

DataFrame

property parameters: DataFrame

Get or set CBM inventory storage

When setting, if the specified data frame’s backend_type differs from the existing dataframe’s type the assigned value will be converted to the existing type.

Returns:

state dataframe, each row represents a stand.

Contains stand-specific parameters for CBM spinup or stepping

Return type:

DataFrame

property pools: DataFrame

Get or set CBM Pools

When setting, if the specified data frame’s backend_type differs from the existing dataframe’s type the assigned value will be converted to the existing type.

Returns:

pools dataframe, each row represents a stand, and columns are each CBM pool

Return type:

DataFrame

property state: DataFrame

Get or set CBM state storage

When setting, if the specified data frame’s backend_type differs from the existing dataframe’s type the assigned value will be converted to the existing type.

Returns:

state dataframe, each row represents a stand, and columns are the CBM state variables.

Return type:

DataFrame

CBM Spinup variables Initialization

libcbm.model.cbm.cbm_variables.initialize_spinup_variables(cbm_vars: CBMVariables, backend_type: BackendType, spinup_params: DataFrame | None = None, include_flux: bool = False) CBMVariables

Initialize spinup variables

Parameters:
  • cbm_vars (CBMVariables) – instance of CBM vars. See initialize_simulation_variables()

  • backend_type (BackendType) – dataframe storage backend

  • spinup_params (DataFrame, optional) – Collection of spinup specific parameters. If unspecified, CBM default values are used. Defaults to None.

  • include_flux (bool, optional) – If set to true the spinup process will track flux during computations. Defaults to False.

Returns:

initialized spinup variables

Return type:

CBMVariables

libcbm.model.cbm.cbm_variables.initialize_spinup_parameters(n_stands: int, back_end: BackendType = BackendType.numpy, return_interval: Series | None = None, min_rotations: Series | None = None, max_rotations: Series | None = None, mean_annual_temp: Series | None = None) DataFrame

Create spinup parameters as a collection of variable vectors

The variables here are all of length N stands and are row-aligned with all other vectors and dataframes using this convention.

Each keyword argument is optional, and if unspecified, libcbm will use a default for the corresponding parameter drawn from cbm_defaults. These parameters are available here to override those default values on a per-stand basis.

If a scalar value is provided to any of the optional parameters, that value will be promoted to a Series in the resulting vector.

Parameters:
  • n_stands (int) – The length of each of the resulting variables vectors returned by this function.

  • return_interval (Series, optional) – The number of years between historical disturbances in the spinup function. Defaults to None.

  • min_rotations (Series, optional) – The minimum number of historical rotations to perform. Defaults to None.

  • max_rotations (Series, optional) – The maximum number of historical rotations to perform. Defaults to None.

  • mean_annual_temp (Series, optional) – The mean annual temperature used in the spinup procedure. Defaults to None.

Returns:

table of spinup paramaeters

Return type:

DataFrame

CBM Step variables Initialization

libcbm.model.cbm.cbm_variables.initialize_simulation_variables(classifiers: DataFrame, inventory: DataFrame, pool_codes: list[str], flux_indicator_codes: list[str], backend_type: BackendType) CBMVariables

Packages and initializes the cbm variables (cbm_vars) as an object with named properties

Parameters:
  • classifiers (DataFrame) – DataFrame of integer classifier value ids. Rows are stands and cols are classifiers

  • inventory (DataFrame) – The inventory to simulate. Each row represents a stand.

  • pool_codes (list) – the list of string pool names which act as column labels for the resulting cbm_vars.pools DataFrame.

  • flux_indicator_codes (list) – the list of string flux indicator names which act as column labels for the resulting cbm_vars.flux DataFrame.

  • backend_type (BackendType) – specifies which backend storage method to use

Returns:

Returns the cbm_vars object for simulating CBM.

Return type:

object

Output processing

class libcbm.model.cbm.cbm_output.CBMOutput(density: bool = False, classifier_map: dict[int, str] | None = None, disturbance_type_map: dict[int, str] | None = None, backend_type: BackendType = BackendType.numpy)

Initialize CBMOutput

Parameters:
  • density (bool, optional) – if set to true pool and flux indicators will be computed as area densities (tonnes C/ha). By default, pool and flux outputs are computed as mass (tonnes C) based on the area of each stand. Defaults to False.

  • classifier_map (dict[int, str], optional) – a classifier map for subsituting the internal classifier id values with classifier value names. If specified, the names associated with each id in the map are the values in the the classifiers result DataFrame. If set to None the id values will be returned.

  • disturbance_type_map (dict[int, str], optional) – a disturbance type map for subsituting the internally defined disturbance type id with names or other ids in the parameters and state tables. If set to none no substitution will occur.

  • backend_type (BackendType, optional) – the storage backend for the output, one of the values of libcbm.storage.backends.BackendType. Defaults to BackendType.numpy meaning simulation results will be stored in memory.

append_simulation_result(timestep: int, cbm_vars: CBMVariables)

Append simulation resuls

Parameters:
  • timestep (int) – the timestep corresponding to the results

  • cbm_vars (CBMVariables) – The cbm vars for the timestep

property area: DataFrame

get all accumulated area results

property backend_type: BackendType

get this instance’s backend type

property classifier_map: dict[int, str]

get this instance’s clasifier map

property classifiers: DataFrame

get all accumulated clasifier results

property disturbance_type_map: dict[int, str]

get this instance’s disturbance type map

property flux: DataFrame

get all accumulated flux results

property parameters: DataFrame

get all accumulated parameter results

property pools: DataFrame

get all accumulated pool results

property state: DataFrame

get all accumulated state results

Configuration Details

Classifiers

libcbm.model.cbm.cbm_config.classifier(name: str, values: dict) dict

Composes a classifier structure used for configuring libcbm.

Parameters:
  • name (str) – the name of the classifier

  • values (dict) – The classifier values that compose this classifier. Use classifier values of the format returned by: classifier_value()

Returns:

A dictionary with the following keys:

  • ”classifier”: The classifier

  • ”classifier_values”: The list of classifier value dictionaries

    associated with the classifier

Example return value:

{
    "classifier": {"name": "c1"},
    "classifier_values": [
        {
            "value": "c1_v1",
            "description": "c1_v1"
        },
        ...
    ]
}

Return type:

dict

libcbm.model.cbm.cbm_config.classifier_value(value: str, description: str = '') dict[str, str]

Composes a classifier value structure used for configuring libcbm.

Parameters:
  • value (str) – the classifier value string identifier

  • description (str, optional) – An optional classifier value description. Defaults to “”.

Returns:

a dictionary formatted for libcbm.

Example return value:

{
    "value": "c1_v1",
    "description": "c1_v1"
}

Return type:

dict

libcbm.model.cbm.cbm_config.classifier_config(classifiers: list) dict

Compose the classifier value scheme used for configuring libcbm. This method will generate classifier ids and classifier value ids based on the order of classifiers and classifier values.

Parameters:

classifiers (list) –

A list of dictionaries describing classifiers. For example:

[
    {
        "classifier": {
            "name" "c1"
        },
        "classifier_values":[
            {"value": "c1_v1", "description": "c1_v1"},
            {"value": "c1_v2", "description": "c1_v2"},
            ...
            {"value": "c1_vn", "description": "c1_vn"}
        ]
    },
    {
        "classifier": {
            "name" "c2"
        },
        "classifier_values":[
            {"value": "c2_v1", "description": "c2_v1"},
            {"value": "c2_v2", "description": "c2_v2"},
            ...
            {"value": "c2_vk", "description": "c1_vk"}
        ]
    },
    ...,
    {
        "classifier": {
            "name" "cj"
        },
        "classifier_values":[
            {"value": "cj_v1", "description": "cj_v1"},
            {"value": "cj_v2", "description": "cj_v2"},
            ...
            {"value": "cj_vi", "description": "cj_vi"}
        ]
    }
]

where: - j is the number of classifiers, - n,k,i are the number of classifier values for each corresponding classifier

The dictionaries composing this argument can be created by the classifier_value(), and classifier() functions

Returns:

A dictionary with the following keys

  • ”classifiers”: the list of classifiers

  • ”classifier_values”: the list of classifier values

This is the format required by the libcbm CBM model component.

For example: (following the same pattern given in the input):

{
"classifiers": [
    {"id": 1, "name": "c1"},
    {"id": 2, "name": "c2"},
    ...,
    {"id": j, "name": "cj"}
],
"classifier_values": [
    {"id": 1, "classifier_id": 1, "value": "c1_v1", ...},
    {"id": 2, "classifier_id": 1, "value": "c1_v2", ...},
    ...,
    {"id": n, "classifier_id": 1, "value": "c1_vn", ...},
    {"id": n+1, "classifier_id": 2, "value": "c2_v1", ...},
    {"id": n+2, "classifier_id": 2, "value": "c2_v2", ...},
    ...,
    {"id": n+k, "classifier_id": 2, "value": "c2_vk", ...},
    {"id": n+k+1, "classifier_id": j, "value": "cj_v1", ...},
    {"id": n+k+2, "classifier_id": j, "value": "cj_v2", ...},
    ...,
    {"id": n+k+i, "classifier_id": j, "value": "cj_vi", ...}
]
}

Return type:

dict

Merchantable Volume Curves

libcbm.model.cbm.cbm_config.merch_volume_curve(classifier_set: list[str], merch_volumes: list[dict]) dict

Formats merchantable volume growth curve data for libcbm CBM model consumption.

Parameters:
  • classifier_set (list) –

    a list of classifier value names ordered by classifiers. For example:

    ["c1_v1", "c2_v1", ... "ck_vx"]
    

  • merch_volumes (list) –

    a list of dictionaries with keys:

    • species_id,

    • age_volume_pairs

    for example:

    [
        {
            "species_id": 1,
            "age_volume_pairs": [(a1,v1),(a2,v2),...,(an,vn)]
        },
        {
            "species_id": 6,
            "age_volume_pairs": [(a1,v1),(a2,v2),...,(an,vn)]
        },
    ]
    

Returns:

a dictionary with keys classifier_set and components.

For example:

{
"classifier_set": {
    "type": "name",
    "values": ["c1_v1", "c2_v1", ... "ck_vx"]
},
"components": [
    {
        "species_id": 1,
        "age_volume_pairs": [(a1,v1),(a2,v2),...,(an,vn)]
    },
    {
        "species_id": 6,
        "age_volume_pairs": [(a1,v1),(a2,v2),...,(an,vn)]
    },
]
}

Return type:

dict

libcbm.model.cbm.cbm_config.merch_volume_to_biomass_config(db_path: str, merch_volume_curves: list) dict

Formats merchantable volume growth curve data for libcbm CBM model consumption.

Parameters:
  • db_path (str) – path to a cbm_defaults database

  • merch_volume_curves (list) – a list of dictionaries in the same format as the return value of merch_volume_curve()

Returns:

A dictionary containing configuration for merchantable volume

growth in the CBM model

For example:

{
    "db_path": "cbm_defaults.db"
    "merch_volume_curves": [
        {
            "classifier_set": {
                "type": "name",
                "values": ["c1_v1", "c2_v1", ... "ck_vx"]
            },
            "components": [
                {
                    "species_id": 1,
                    "age_volume_pairs": [(a1,v1),
                                         (a2,v2),
                                         ...,
                                         (an,vn)]
                },
                {
                    "species_id": 6,
                    "age_volume_pairs": [(a1,v1),
                                         (a2,v2),
                                         ...,
                                         (an,vn)]
                },
            ]
        },
        {
            "classifier_set": {
                "type": "name",
                "values": ["c1_v5", "c2_v1", ... "ck_vx"]
            },
            "components": [
                {
                    "species_id": 2,
                    "age_volume_pairs": [(a1,v1),
                                         (a2,v2),
                                         ...,
                                         (an,vn)]
                },
                {
                    "species_id": 65,
                    "age_volume_pairs": [(a1,v1),
                                         (a2,v2),
                                         ...,
                                         (an,vn)]
                },
            ]
        }
    ]
}

Return type:

dict

CBM default parameters

The parameters in this section are the simulation-constant model parameters. These are used to initialize the CBM class.

libcbm.model.cbm.cbm_defaults.get_cbm_parameters_factory(db_path: str) Callable[[], dict[str, DataFrame]]

Get a function that formats CBM parameters for libcbm.wrapper.cbm.cbm_wrapper.CBMWrapper drawn from the specified database.

Parameters:

db_path (str) – path to a cbm_defaults database

Returns:

a function that creates CBM parameters

Compatible with: libcbm.model.cbm.cbm_factory.create()

Return type:

func

libcbm.model.cbm.cbm_defaults.get_libcbm_configuration_factory(db_path: str) Callable[[], dict[str, list[dict]]]

Get a parameterless function that creates configuration for libcbm.wrapper.libcbm_wrapper.LibCBMWrapper

Parameters:

db_path (str) – path to a cbm_defaults database

Returns:

a function that creates CBM configuration input for libcbm

Compatible with: libcbm.model.cbm.cbm_factory.create()

Return type:

func

libcbm.model.cbm.cbm_defaults.load_cbm_flux_indicators(sqlite_path: str) list[dict]

Loads cbm flux indicator information from a cbm_defaults database into the format expected by the libcbm compiled library.

Used to capture flows between specified source pools and specified sink pools for a given process to return as model output.

Parameters:

sqlite_path (str) – path to a cbm_defaults database

Returns:

list of dictionaries describing CBM flux indicators.

For example:

[
    {
        "name": "flux_1"
        "id": 1,
        "index": 0,
        "process_id": 1,
        "source_pools": [1, 2, 3, 4],
        "sink_pools": [5, 6, 7, 8],
    },
]

Return type:

list

libcbm.model.cbm.cbm_defaults.load_cbm_parameters(sqlite_path: str) dict[str, DataFrame]

Loads cbm default parameters into configuration dictionary format. Used for initializing CBM functionality in LibCBM via the InitializeCBM function.

Parameters:

sqlite_path (str) – Path to a CBM parameters database as formatted like: https://github.com/cat-cfs/cbm_defaults

Raises:

AssertionError – if the name of any 2 queries is the same, an error is raised.

Returns:

a dictionary of name/pandas.DataFrame pairs for use with LibCBM

configuration.

Return type:

dict

libcbm.model.cbm.cbm_defaults.load_cbm_pools(sqlite_path: str) list[dict]

Loads cbm pool information from a cbm_defaults database into the format expected by the libcbm compiled library.

Parameters:

sqlite_path (str) – path to a cbm_defaults database

Returns:

list of dictionaries describing CBM pools

For example:

[
    {"name": "pool1", "id": 1, "index": 0},
    {"name": "pool2", "id": 2, "index": 1},
    ...,
    {"name": "poolN", "id": N, "index": N-1},
]

Return type:

list

CBM default parameters reference

class libcbm.model.cbm.cbm_defaults_reference.CBMDefaultsReference(sqlite_path: str, locale_code: str = 'en-CA')

Creates a reference to the localized name and id relationships stored in a cbm_defaults database.

Parameters:
  • sqlite_path (str) – path to a cbm_defaults sqlite database.

  • locale_code (str, optional) – locale code as defined in the locale table of the cbm_defaults database. Defaults to “en-CA”.

static as_data_frame(row_list: list[Row]) DataFrame

helper method to convert functions that return lists of sqlite3.Row objects into dataframes

Parameters:

row_list (list[sqlite3.Row]) – list of rows

Returns:

pandas dataframe representation of the specified

list.

Return type:

pd.DataFrame

get_afforestation_pre_type_id(afforestation_pre_type_name: str) int

Get the afforestation pre-type id associated with the specified afforestation pre-type name

Parameters:

afforestation_pre_type_name (str) – an afforestation pre-type name

Returns:

the associated afforestation_pre_type_id

Return type:

int

get_afforestation_pre_types() list[Row]

Get name and id information for the afforestation pre-types defined in the underlying cbm_defaults database. Returns a list of dictionaries with the following keys:

  • afforestation_pre_type_id

  • afforestation_pre_type_name

Returns:

list of rows with afforestation pre type data

Return type:

list

get_biomass_pools() list[str]

Returns the subset of the pool codes that correspond to biomass pools in cbm_defaults

Returns:

list of string codes for cbm biomass pools

Return type:

list

get_dead_organic_matter_pools() list[str]

Returns the subset of the pool codes that correspond to dead organic matter (DOM) pools in cbm_defaults

Returns:

list of string codes for cbm DOM pools

Return type:

list

get_disturbance_type_id(disturbance_type_name: str) int

Get the disturbance type id associated with the specified disturbance type name

Parameters:

disturbance_type_name (str) – a disturbance type name

Returns:

disturbance type id

Return type:

int

get_disturbance_types() list[Row]

Get all name and id information about every CBM disturbance type as a list of rows with keys:

  • disturbance_type_id

  • disturbance_type_name

Returns:

list – list of rows with disturbance type information

get_flux_indicators() list[str]
Get the ordered list of human readable flux indicator codes defined

in cbm_defaults

Returns:

list of string names of flux indicators

Return type:

list

get_land_class_disturbance_ref() list[Row]

Get name and id information for the cbm disturbance types that cause a change to UNFCCC land class, along with the post-disturbance land type. Non-land class altering disturbance types are not included in the result. Returns a list of dictionaries with the following keys:

  • disturbance_type_id

  • disturbance_type_name

  • land_type_id

  • land_type_name

Returns:

a list of rows with disturbance type/landtype data

Return type:

list

get_land_class_id(land_class_code: str) int

Get the land class id associated with the specified CBM land class code (where a code might be for example: UNFCCC_FL_R_FL)

Parameters:

land_class_code (str) – a CBM formatted UNFCCC land class code

Returns:

the land class id associated with the code

Return type:

int

get_land_classes() list[Row]

Get all name and id information about every CBM land class.

Result is returned as a list of rows with keys:

  • land_class_id

  • code - land class code

  • description - land class description

  • is_forest - whether or not this land class is forested

  • is_simulated - whether or not CBM dynamics are tracked in this

    land class state

  • transitional_period - number of years in the transtional period

  • transition_id - land_class_id (from within this table) of the

    land class to which this land class transitions

  • land_type_id_1 - the pre-land-class-transition land type id

  • land_type_id_2 - the post-land-class-transition land type id

for defintion of land_type ids and their relationship to disturbance types see: get_land_class_disturbance_ref()

Returns:

list of rows with land class information information

Return type:

list

get_pools() list[str]

Get the ordered list of human readable pool codes defined in cbm_defaults

Returns:

list of string codes for cbm pools

Return type:

list

get_spatial_unit(spatial_unit_id: int) Tuple[str, str]

Get a tuple of admin boundary name, eco boundary name for the specified spatial unit id

Parameters:

spatial_unit_id (int) – the spatial unit id

Returns:

a pair of strings admin_boundary_name, eco_boundary_name

Return type:

tuple

get_spatial_unit_id(admin_boundary_name: str, eco_boundary_name: str) int

Get the spatial unit id associated with the specified admin-boundary-name, eco-boundary-name combination

Parameters:
  • admin_boundary_name (str) – an admin boundary name

  • eco_boundary_name (str) – an eco boundary name

Returns:

the spatial unit id

Return type:

int

get_spatial_units() list[Row]

Get name and id information for the spatial units defined in the underlying cbm_defaults database. Returns a list of rows with the following keys:

  • spatial_unit_id

  • admin_boundary_id

  • admin_boundary_name

  • eco_boundary_id

  • eco_boundary_name

Returns:

rows containing spatial unit data

Return type:

list

get_species() list[Row]

Get all name and id information about every CBM species.

Result is returned as a list of rows with keys:

  • species_id

  • species_name

  • genus_id

  • genus_name

  • forest_type_id

  • forest_type_name

Returns:

list of rows with species information

Return type:

list

get_species_id(species_name: str) int

Get the species id associated with the specified species name.

Parameters:

species_name (str) – a species name

Returns:

the id associated with the specified name

Return type:

int

load_data(sqlite_path: str, query: str, query_params: tuple | None = None) list[Row]

loads the specified query into a list of dictionary formatted query

Parameters:
  • sqlite_path (str) – path to a SQLite database

  • query (str) – sqlite query

  • query_params (Tuple, optional) – tuple of query parameters to pass to the sqlite3 execute method. Defaults to None.

Returns:

a list of sqlite3.Row objects containing the query results

Return type:

list

CBM rule based disturbances and transition rules

class libcbm.model.cbm.rule_based.classifier_filter.ClassifierFilter(classifiers_config: dict, classifier_aggregates: list[dict])

ClassifierFilter creates a filter for deeming stands eligible or ineligible for disturbance or transition.

create_classifiers_filter(classifier_set: list[str], classifier_values: DataFrame) RuleFilter

Creates a filter based on the specified classifier set to select a subset of the values in classifier_values

Parameters:
  • classifier_set (list) –

    a list of strings, these may be any of:

    • a defined classifier value

    • a classifier aggregate

    • or a wildcard “?”

  • classifier_values (DataFrame) – dataframe of classifier value ids by stand (row), by classifier (columns). Column labels are the classifier names.

Raises:
  • ValueError – mismatch in the number of classifiers

  • ValueError – a classifier value in the specified classifier set is not defined

Returns:

rule filter object

Return type:

RuleFilter

class libcbm.model.cbm.rule_based.event_processor.ProcessEventResult(cbm_vars: CBMVariables, filter_result: Series, rule_target_result: RuleTargetResult)

Storage class for the result of the process_event() function.

Parameters:
  • cbm_vars (object) – an object containing dataframes that store cbm simulation state and variables

  • filter_result (Series) – boolean array indicating for each stand index in cbm_vars the index was eligible for the event when true, and ineligible when false

  • rule_target_result (RuleTargetResult) – instance of libcbm.model.cbm.rule_based.rule_target.RuleTargetResult indicating targeted stands for this event

libcbm.model.cbm.rule_based.event_processor.apply_rule_based_event(target: DataFrame, disturbance_type_id: int, cbm_vars: CBMVariables, disturbance_event_id: int | None = None) CBMVariables

Apply the specified target to the CBM simulation variables, splitting them if necessary.

Parameters:
  • target (DataFrame) – object describing the index of records to disturb and area split proportions.

  • disturbance_type_id (int) – the id for the disturbance event being applied.

  • cbm_vars (CBMVariables) – an object containing dataframes that store cbm simulation state and variables

  • disturbance_event_id (int, optional) – an identifier for the disturbance event being processed.

Returns:

updated and expanded cbm_vars

Return type:

CBMVariables

libcbm.model.cbm.rule_based.event_processor.process_event(event_filters: list[RuleFilter], undisturbed: Series, target_func: Callable[[CBMVariables, Series], RuleTargetResult], disturbance_type_id: int, cbm_vars: CBMVariables, disturbance_event_id: int | None = None) ProcessEventResult

Computes a CBM rule based event by filtering and targeting a subset of the specified inventory. In the case of merchantable or area targets splits may occur to meet a disturbance target exactly.

Parameters:
  • event_filters (list) – a list of filter objects containing information to deem stands eligible or ineligible for events

  • undisturbed (Series) – a boolean value series indicating each specified index is eligible (True) or ineligible (False) for disturbance.

  • target_func (func) – a function for creating a disturbance target.

  • disturbance_type_id (int) – the id for the disturbance event being processed.

  • cbm_vars (CBMVariables) – an object containing dataframes that store cbm simulation state and variables

  • disturbance_event_id (int, optional) – an identifier for the disturbance event being processed.

Returns:

instance of class containing results for the

disturbance event

Return type:

ProcessEventResult

libcbm.model.cbm.rule_based.rule_filter.create_filter(expression: str, data: DataFrame)

Creates a filter object for filtering a pandas dataframe using an expression.

Parameters:
  • expression (str) – a boolean expression in terms of the values in column_variable_map

  • data – the data to for which to create a filter

Returns:

rule_filter object

Return type:

RuleFilter

libcbm.model.cbm.rule_based.rule_filter.evaluate_filters(*filter_objs: RuleFilter) Series | None

Evaluates the specified sequence of filter objects.

  • If all filter expressions in the specified filter_objs are null then a True (unfiltered) series is returned

  • If all filter expressions in the specified filter_objs are null and no data is provided None is returned

  • otherwise the series logical and of all specified filters are returned.

Parameters:

filter_objs (list) – list of RuleFilter objects:

Returns:

filter result (boolean array)

Return type:

Series

class libcbm.model.cbm.rule_based.rule_target.RuleTargetResult(target: DataFrame, statistics: dict)

Standard return value for the functions in this module.

Parameters:
  • target (DataFrame) –

    dataframe containing:

    • target_var: the disturbed amount for each disturbed record

      (in target units)

    • sort_var: the variable used to sort values for disturbance

    • disturbed_index: the index of each disturbed record

    • area_proportions: the proportions for each record to disturb

  • statistics (dict) – dictionary describing the statistics involved in the disturbance target

libcbm.model.cbm.rule_based.rule_target.proportion_area_target(area_target_value: float, inventory: DataFrame, eligible: Series) RuleTargetResult

create a disturbance target which disturbs that proportion of all eligible records that such that the sum of all eligible record areas multiplied by the proportion equals the area target exactly.

Parameters:
  • area_target_value (float) – the target area to disturb

  • inventory (DataFrame) – the inventory being targeted for disturbance.

  • eligible (Series) – boolean array indicating whether or not each index is eligible for this disturbance target

Returns:

object with information targeting a proportion of

all records in the eligible subset of the specified inventory such that the sum of area of all disturbed records matches the specified area target.

Return type:

RuleTargetResult

libcbm.model.cbm.rule_based.rule_target.proportion_merch_target(carbon_target: float, disturbance_production: DataFrame, inventory: DataFrame, efficiency: float, eligible: Series) RuleTargetResult

create a sequence of areas/proportions for disturbance a propotion of all eligible stands such that the proportion * disturbance_production for each stand equals the carbon target exactly.

Parameters:
  • carbon_target (float) – a disturbance target in CBM mass units (tonnes C)

  • disturbance_production (DataFrame) – a table of Carbon density (tonnes C/ha) generated by a disturbance events on the specified inventory. Used in accumulating value towards the carbon_target parameter.

  • inventory (DataFrame) – the inventory being targeted for disturbance.

  • efficiency (float) – A proportion value <= 1 multiplier disturbance production for each stand

  • eligible (Series) – boolean array indicating whether or not each index is eligible for this disturbance target

Returns:

object with information targeting a proportion of

all records in the eligible subset of the specified inventory such that the total carbon produced matches the specified carbon target.

Return type:

RuleTargetResult

libcbm.model.cbm.rule_based.rule_target.proportion_sort_proportion_target(proportion_target: float, inventory: DataFrame, eligible: Series) RuleTargetResult

Create a rule target specifying to the given proportion of all of the eligible stands.

Parameters:
  • proportion_target (float) – a proportion of each eligible inventory record’s area to disturb

  • inventory (DataFrame) – the inventory being targeted for disturbance.

  • eligible (Series) – boolean array indicating whether or not each index is eligible for this disturbance target

Returns:

object with information targeting the specified

proportion of all records in the eligible subset of the specified inventory.

Return type:

RuleTargetResult

libcbm.model.cbm.rule_based.rule_target.sorted_area_target(area_target_value: float, sort_value: Series, inventory: DataFrame, eligible: Series) RuleTargetResult

create a sorted sequence of areas/proportions for meeting an area target exactly.

Parameters:
  • area_target_value (float) – the target area to disturb

  • sort_value (Series) – a sequence of values whose decending sort defines the order to accumulate areas. Length must equal the number of rows in the specified inventory

  • inventory (DataFrame) – the inventory being targeted for disturbance.

  • eligible (Series) – boolean array indicating whether or not each index is eligible for this disturbance target

Returns:

object with information targeting a sorted subset

of all records in the eligible subset of the specified inventory such that the total area disturbed matches the specified area target value.

Return type:

RuleTargetResult

libcbm.model.cbm.rule_based.rule_target.sorted_disturbance_target(target_var: Series, sort_var: Series, target: float, eligible: Series) RuleTargetResult

Given a target variable, a sort variable, and a cumulative target, produce a table of index, area proportions that will satisfy exactly a rule based disturbance target.

Parameters:
  • target_var (Series) – a series of values fed into an accumulator to satisfy the cumulative target.

  • sort_var (Series) – a variable whose descending sort order defines the order in which target_var values are fed into the accumulator.

  • target (float) – the cumulative target.

  • eligible (Series) – boolean array indicating whether or not each index is eligible for this disturbance target

Raises:
  • ValueError – specified target was less than 0

  • ValueError – less than zero values are detected in target_var

Returns:

object with information targeting a proportion of

or the entirety of a subset of rows in the eligible subset of specified target_var.

Return type:

RuleTargetResult

libcbm.model.cbm.rule_based.rule_target.sorted_merch_target(carbon_target: float, disturbance_production: DataFrame, inventory: DataFrame, sort_value: Series, efficiency: float, eligible: Series) RuleTargetResult

create a sorted sequence of areas/proportions for meeting a merch C target exactly.

Parameters:
  • carbon_target (float) – a disturbance target in CBM mass units (tonnes C)

  • disturbance_production (DataFrame) – a table of Carbon density (tonnes C/ha) generated by a disturbance events on the specified inventory. Used in accumulating value towards the carbon_target parameter.

  • inventory (DataFrame) – the inventory being targeted for disturbance.

  • sort_value (Series) – a sequence of values whose decending sort defines the order to accumulate carbon mass. Length must equal the number of rows in the specified inventory

  • efficiency (float) – reduce the disturbance production and split all records

  • eligible (Series) – boolean array indicating whether or not each index is eligible for this disturbance target

Returns:

object with information targeting a subset of

the eligible subset of the specified inventory such that the carbon_target is met.

Return type:

RuleTargetResult

libcbm.model.cbm.rule_based.rule_target.spatially_indexed_target(identifier: int, inventory: DataFrame) RuleTargetResult

return a target for a single inventory record identified by the specified identifier

Parameters:
  • identifier (int) – the integer identifier matching a single row in inventory.spatial_reference

  • inventory (DataFrame) – inventory records

Raises:
  • ValueError – the specified identifier was not present in the specified inventory spatial_reference column

  • ValueError – the specified identifier appears 2 or more times in the specified inventory spatial_reference column

Returns:

object with information on spatially indexed

stand to disturb

Return type:

RuleTargetResult

class libcbm.model.cbm.rule_based.transition_rule_processor.TransitionRuleProcessor(classifiers_config: dict[str, list], wildcard: str, transition_classifier_postfix: str)
apply_transition_rule(tr_group: DataFrame, rule_filters: list[RuleFilter], proportions: list[float], transition_mask: Series, cbm_vars: CBMVariables) Tuple[Series, CBMVariables]

Apply the specified transition rule group to the simulation variables, updating classifier values, and returning the transition rule variables reset age, and regeneration delay. For each member of the transition rule group a split of the simulation variables will occur with area being reduced according to the “percent” column in the member transition rules.

Parameters:
  • tr_group (DataFrame) – the grouped transition rules, where each row is a member.

  • rule_filters (list) – list of trnasition rule filters that includes or excludes areas for this transition.

  • proportions (list) – list of proportions adding up to 100. This is row-index-aligned with the specified tr_group with the exception that an extra element at the end representing non-transitioned proportion can be present.

  • transition_mask (Series) – a boolean mask indicating when true that the correspoding index has already been transitioned. This is used to detect transition rule criteria collisions.

  • cbm_vars (CBMVariables) – CBM simulation variables and state

Raises:

ValueError – a transition rule criteria resulted in the selection of stands targeted by at least one other transition rule

Returns:

  • transition_mask: the specified transition_mask parameter is

    returned altered with the indices transitioned by this function call.

  • cbm_vars: updated and potentially expanded cbm variables and

    state

Return type:

tuple

SIT Specific rule-based disturbance and transition rules functionality

class libcbm.model.cbm.rule_based.sit.sit_event_processor.SITEventProcessor(cbm: CBM, classifier_filter_builder: ClassifierFilter, random_generator: Callable[[int], Series], disturbance_type_map: dict[str, int])

SITEventProcessor processes standard import tool format events.

Parameters:
  • cbm (object) – CBM model

  • classifier_filter_builder (ClassifierFilter) – object used to construct and evaluate classifier filters to include or exclude stands from event and transition eligibility

  • random_generator (function) – a function to generate a random sequence, whose single argument is an integer that specifies the number of random numbers in the returned sequence.

  • disturbance_type_map (dict[str, int]) – map of SIT defined disturbance type ids to internally (strictly numeric) disturbance type ids.

process_events(time_step: int, sit_events: DataFrame, cbm_vars: CBMVariables, sit_eligibilities: DataFrame | None = None) Tuple[CBMVariables, DataFrame]

Process sit_events for the start of the given timestep, computing a new simulation state, and the disturbance types to apply for the timestep.

Because of the nature of CBM rule based events, the size of the returned arrays may grow on the “n_stands” dimension from the original sizes due to area splitting, however the total inventory area will remain constant.

Parameters:
  • time_step (int) – the simulation time step for which to compute the events. Used to filter the specified sit_events DataFrame by its “time_step” column

  • sit_events (pandas.DataFrame) – table of SIT formatted events. Expected format is the same as the return value of: libcbm.input.sit.sit_disturbance_event_parser.parse()

  • cbm_vars (object) – an object containing dataframes that store cbm simulation state and variables

  • sit_eligibilities (pandas.DataFrame) – table of eligibility expressions with foreign key “eligibility_id”

Returns:

expanded and updated cbm_vars

Return type:

object

libcbm.model.cbm.rule_based.sit.sit_stand_filter.create_pool_filter_expression(sit_data: dict) str

Create a filter against simulation pool values based on a single row of SIT disturbance events

Parameters:

sit_data (dict) – a row dictionary from an SIT events

Returns:

filter expression

Return type:

str

libcbm.model.cbm.rule_based.sit.sit_stand_filter.create_state_filter_expression(sit_data: dict, age_only: bool) str

Create a filter against simulation state variables based on a single row of SIT disturbance event, or transition rule data.

Parameters:
  • sit_data (dict) – a row dictionary from an SIT events, or SIT transition rules table

  • age_only (bool, optional) – if true specifies that only age variables will be considered when building the expression. This is useful for SIT_Transition_Rules which consider age as a criteria. If False all state variables are included, this is required for SIT_Events.

Returns:

filter expression

Return type:

str

libcbm.model.cbm.rule_based.sit.sit_stand_filter.get_classifier_set(sit_data_row: dict, classifiers: list[str]) list[str]
Get a classifier set from a row of SIT data

disturbance events, transition rules, yield or inventory

Parameters:
  • sit_data_row (dict) – a row dictionary from SIT data

  • classifiers (list) – list of classifier names

Returns:

a list of strings with classifier values in the specified row

also known as a “classifier set”

Return type:

list

libcbm.model.cbm.rule_based.sit.sit_stand_filter.get_state_variable_age_filter_mappings() tuple[str, str, str]

get mappings between SIT events or transitions age criteria columns, and state variable columns, along with a boolean operator to compare age values.

Returns:

a list of (str, str, str) tuples in format

  • SIT_Event column name

  • state variable column name

  • operator string

Return type:

list

libcbm.model.cbm.rule_based.sit.sit_stand_filter.get_state_variable_filter_mappings() tuple[str, str, str]

get mappings between SIT events criteria columns, and state variable columns, along with a boolean operator to compare values.

Returns:

a list of (str, str, str) tuples in format

  • SIT_Event column name

  • state variable column name

  • operator string

Return type:

list

libcbm.model.cbm.rule_based.sit.sit_transition_rule_processor.create_split_proportions(tr_group_key: dict, tr_group: DataFrame, group_error_max: float) list[float]

Create proportions

Parameters:
  • tr_group_key (dict) – the composite key common to the transition rule group members

  • tr_group (DataFrame) – The table of transition rule group members

  • group_error_max (float) – used as a threshold to test if the group’s total percentage exceeds 100 percent.

Raises:

ValueError – Thrown if the absolute difference of total percentage minus 100 percent is greater than the group_error_max threshold.

Returns:

a list of proportions whose sum is 1.0 for splitting records

for transition

Return type:

list

libcbm.model.cbm.rule_based.sit.sit_transition_rule_processor.create_state_variable_filter(tr_group_key: dict, state_variables: DataFrame) RuleFilter

Create a filter based on transition rule state criteria for setting stands eligible or ineligible for transition.

Parameters:
  • tr_group_key (dict) – dictionary of values common to a transition rule group

  • state_variables (DataFrame) – table of state values for the current simulation for which to create a filter.

Returns:

a filter object

Return type:

RuleFilter

libcbm.model.cbm.rule_based.sit.sit_transition_rule_processor.get_transition_rule_filters(classifier_filter: ClassifierFilter, tr_group_key: dict, cbm_vars: CBMVariables) list[RuleFilter]

Build a filter for the default CBM SIT transtion rule format: transition rules are eligible, or ineligible based on the values of disturbance type, classifier set, and age ranges

libcbm.model.cbm.rule_based.sit.sit_transition_rule_processor.sit_transition_rule_iterator(sit_transitions: DataFrame, classifier_names: list[str]) Iterable[tuple[dict[str, str], DataFrame]]

Groups transition rules by classifiers, and eligibility criteria and yields the sequence of group_key, group.

Parameters:
  • sit_transitions (pandas.DataFrame) – parsed sit_transitions. See libcbm.input.sit.sit_transition_rule_parser

  • classifier_names (list) – the list of classifier names which must correspond to the first len(classifier_names) columns of sit_transitions

Raises:

ValueError – the sum of the percent field for any grouped set of transition rule rows exceeded 100%

Returns:

Item1: the “key values” of the grouped transition rule rows Item2: the rows which compose the transtion rule group, as a dataframe

Return type:

Tuple

class libcbm.model.cbm.rule_based.sit.sit_transition_rule_processor.SITTransitionRuleProcessor(transition_rule_processor: TransitionRuleProcessor, classifier_filter: ClassifierFilter, group_error_max: float)
process_transition_rules(sit_transitions: DataFrame, cbm_vars: CBMVariables, sit_eligibilities: DataFrame | None = None) CBMVariables

Process the specified SIT transition rules versus the current model state.

Parameters:
Returns:

the input CBM model state with the transition rules applied.

Return type:

CBMVariables