appabuild.database.user_database_elements#

The purpose of this module is to make user database data readily writable in bw2. Activities and exchanges are organized in a graph-like fashion (Activities being the nodes, Activity producing the reference flow being the root node, Exchanges being the edges) to ease LCA scope-dependent features such as parameter propagation or Activity duplication.

Classes#

Activity

Contains common attributes between Activity and Exchange.

DatabaseElement

Contains common attributes between Activity and Exchange.

Exchange

Exchange are connection between an input and an output dataset.

UserDatabaseContext

Gathers all context information necessary for generating activities.

Module Contents#

class appabuild.database.user_database_elements.Activity#

Bases: DatabaseElement

Contains common attributes between Activity and Exchange.

classmethod from_serialized_activity(serialized_activity: appabuild.database.serialized_data.SerializedActivity, context: UserDatabaseContext)#

Generates an Activity from a SerializedActivity object, after completing following steps: - If Activity code is already in context, generates a duplicate by adding a suffix to code. - If Activity name is already in context and activity will become an ImpactModel node, also make the name unique by adding a suffix to name. - Create Activity and add in context - Populate exchange attribute by creating Exchange objects :param serialized_activity: information required to generate Activity :param context: gathers all context information necessary for generating activities. Typically generated by UserDatabase object. :return: generated Activity

propagate_include_in_tree(context: UserDatabaseContext)#

If an activity has include_in_tree set to False, its children should also have include_in_tree set to False. Propagate Activity’s exchanges’ include_in_tree to activity’s exchanges’ input activities. :param context: gathers all context information necessary for generating activities. Typically generated by UserDatabase object. :return:

propagate_parameters(context: UserDatabaseContext)#

Propagate Activity’s exchanges’ parameter_matching to activity’s exchanges’ input activities. :param context: gathers all context information necessary for generating activities. Typically generated by UserDatabase object. :return:

replace_parameters(parameters_matching: Dict[str, str | Dict[str, numbers.Number]])#

Replace Activity’s exchanges’ parameters_matching with parameters_matching keys by parameters matching values. Values can be constant, formulas or parameter name. :param parameters_matching: keys should be Exchange parameter to update, and values should be replacing value. Will be passed to all activity’s exchanges. A float will set the parameter to a fixed value, str will update parameter’s name or affect it to a formula, and dict is used to fix value of a categorical parameter. :return:

to_bw_format() Tuple[Tuple[str, str], dict]#

Converts an Activity to a format importable in a Brightway database :return: dictionary with all fields compatible with Brightway representation of Activity.

code: str | None = None#

Must be unique in the user database (equivalent to the notion of uuid).

data_quality: dict | None = None#

Deprecated.

database: str#

Name of the user database.

exchanges: List[Exchange]#

Emissions or consumptions generated when a unit of the activity is used.

include_in_tree: bool | None = False#

If True, activity will become a node in built ImpactModel.

location: str#

Location of the activity. Default value is GLO for global.

parameters: List[str] | None = []#

Optional list of parameters necessary to execute this dataset.

properties: Dict[str, str | float | bool] | None#

Properties will remain on impact model, and can be used by apparun to breakdown the results according to life cycle phase, for exemple. Properties can be key/value (ex: {“phase”: “production”} or flags (ex: {production_phase: True}).

unit: str#

Unit of the amount.

class appabuild.database.user_database_elements.DatabaseElement#

Bases: pydantic.BaseModel

Contains common attributes between Activity and Exchange.

class Config#
arbitrary_types_allowed = True#
amount: numbers.Number | None = None#

Amount of output flow generated for given amount of exchanges.

comment: str | None = None#

Free text for any context information about the activity/exchange.

context: UserDatabaseContext#

Gathers all context information necessary for generating activities. Typically generated by UserDatabase object.

name: str#

Name of the activity/exchange.

type: str#

According to Brightway, can be production, technosphere, or biosphere.

class appabuild.database.user_database_elements.Exchange#

Bases: DatabaseElement

Exchange are connection between an input and an output dataset.

classmethod from_serialized_exchange(serialized_exchange: appabuild.database.serialized_data.SerializedExchange, context: UserDatabaseContext, calling_activity: Activity) List[Exchange]#

Generates an Exchange from a SerializedExchange object, after completing following steps: - Resolve input if necessary, i.e. determine its uuid - Resolve switch, i.e. transform a SerializedExchange with Switch to a list of SerializedExchange without Switch - Generate a new Activity for each Exchange which input database is equal to context database :param serialized_exchange: information required to generate Exchange :param context: gathers all context information necessary for generating activities. Typically generated by UserDatabase object. :param calling_activity: Activity containing the Exchange (should be Exchange’s output) :return: a list of Exchange, as resolving a Switch can lead to several Exchanges

propagate_include_in_tree(context: UserDatabaseContext)#

If an activity has include_in_tree set to False, its children should also have include_in_tree set to False. Check include_in_tree value of output, change include_in_tree value of input if needed. :param context: gathers all context information necessary for generating activities. Typically generated by UserDatabase object. :return:

propagate_parameters(context: UserDatabaseContext)#

Propagate Exchange’s parameter_matching to input activity’s exchanges. :param context: gathers all context information necessary for generating activities. Typically generated by UserDatabase object. :return:

replace_parameters(parameters_matching: Dict[str, str | Dict[str, numbers.Number] | numbers.Number])#

Replace Exchange’s parameters matching with parameters_matching keys by parameters matching values. Values can be constant, formulas or parameter name. :param parameters_matching: keys should be Exchange parameter to update, and values should be replacing value. A float will set the parameter to a fixed value, str will update parameter’s name or affect it to a formula, and dict is used to fix value of a categorical parameter. :return:

to_bw_format() Dict#

Converts an Exchange to a format importable in a Brightway database :return: dictionary with all fields compatible with Brightway representation of Exchange.

update_parameters_matching(new_parameters_matching: Dict[str, str | Dict[str, numbers.Number] | numbers.Number])#

Update exchange’s parameters_matching values according to a new parameters_matching dict. :param new_parameters_matching: each new_parameters_matching item will be appended to Exchange’s parameters_matching attribute. Pre-existing keys will be overwritten. :return:

formula: str | None = None#

Scipy-compliant formula to dynamically determine quantity function of parameters.

input: Activity | appabuild.database.serialized_data.ActivityIdentifier#

Input/downstream activity. If from background database, will be of type ActivityIdentifier.

output: Activity#

Output/upstream activity.

parameters_matching: Dict[str, str | Dict[str, numbers.Number] | numbers.Number] | None#

Name or values of input’s parameters can be dynamically changed. Key is the name of the input parameter’s name to change, and value the replacing variable. A float will set the parameter to a fixed value, str will update parameter’s name or affect it to a formula, and dict is used to fix value of a categorical parameter.

class appabuild.database.user_database_elements.UserDatabaseContext#

Bases: pydantic.BaseModel

Gathers all context information necessary for generating activities.

activities: List[Activity]#

Activities already produced, necessary to update an activity’s uuid and name in case its being used several times. Activities used several times should be duplicated with different uuid as it could have a different parameterization between different instances, and with different names as it could become independent ImpactModel nodes.

database: appabuild.database.bw_databases.BwDatabase#

UserDatabase that is being created and populated.

serialized_activities: List[appabuild.database.serialized_data.SerializedActivity]#

All serialized activities necessary to build activity tree with reference flow activity as a root.