Activities

lca_algebraic extends Brightway2 and adds methods to the Activity class.

It also provides some helpers functions to search, create and copy activities.

class ActivityExtended

The new methods implemented in this class are automatically added to vanialla Activity objects coming from brightway.

class lca_algebraic.ActivityExtended(document=None, **kwargs)[source]

Improved API for activity : adding a few useful methods. Those methods are backported to #Activity in order to be directly available on all existing instances

addExchanges(exchanges: Dict[Activity, float | Basic | dict] = {})[source]

Add exchanges to an existing activity, with a compact syntax :

Parameters

exchanges :

Dict of activity => amount or activity => attributes_dict. Amount being either a fixed value or Sympy expression (arithmetic expression of Sympy symbols)

deleteExchanges(name, single=True)[source]

Remove matching exchanges

Parameters

name:

Name of the exchange to delete. Can contain wildcards. See #getExchange for more details.

single:

If true (default) expect to only delete a single exchange

getAmount(*args, sum=False, **kargs)[source]

Get the amount of one or several exchanges, selected by name or input. See #getExchange()

getExchange(name: str = None, input: Activity = None, single=True)[source]

Get exchange by name or input

Parameters

name :

name of the exchange. Name can be suffixed with ‘#LOCATION’ to distinguish several exchanges with same name. It can also be suffised by ‘*’ to match an exchange starting with this name. Location can be a negative match ‘!’ Example “Wood*#!RoW” matches any exchange with name containing Wood, and location not “RoW”

input :

input activity

single :

True if a single match is expected. Otherwize, a list of result is returned

Returns

Single exchange or list of exchanges (if _single is False or “name” contains a ‘*’) raise Exception if not matching exchange found

getOutputAmount()[source]

Return the amount of the production : 1 if none is found

listExchanges()[source]

Iterates on all exchanges (except “production”) and return a list of (exch-name, target-act, amount)

non_production_exchanges()[source]

List of exchange, except production (output) one.

setOutputAmount(amount)[source]

Set the amount for the single output exchange (1 by default)

updateExchanges(updates: Dict[str, any] = {})[source]

Update existing exchanges, by name.

Parameters

updates : Dict of “<exchange name>” => <new value>

<exchange name> can be suffixed with ‘#LOCATION’ to distinguish several exchanges with same name. It can also be suffixed by ‘*’ to match an exchange starting with this name. Location can be a negative match ‘!’ Example : “Wood*#!RoW” matches any exchange with name containing Wood, and location not “RoW”

<New Value> : either single value (float or SympPy expression) for updating only amount, or activity for updating only input, or dict of attributes, for updating both at once, or any other attribute. The amount can reference the symbol ‘old_amount’ that will be replaced with the current amount of the exchange.

updateMeta(**kwargs)[source]

Update any property. Useful to update axes

findActivity()

lca_algebraic.findActivity(name=None, loc=None, code=None, categories=None, category=None, db_name=None, single=True, case_sensitive=False, unit=None, limit=1500) ActivityExtended[source]

Find activity by name & location Uses index for fast fetching

Parameters:
  • name – Name of the activity. Can contain ‘*’ for searching partial chain

  • loc – optional location

  • code – Unique code. If provided alone, returns the activity for this code

  • categories – Optional : exact list of catagories

  • category – Optional : single category that should be part of the list of categories of the selected activities

  • db_name – Name of the database

  • single – If False, returns a list of matching activities. If True (default) fails if more than one activity fits.

  • case_sensitive – If True (default) ignore the case

  • unit – If provided, only match activities with provided unit

Returns:

Either a single activity (if single is True) or a list of activities, possibly empty.

findBioAct()

lca_algebraic.findBioAct(name=None, loc=None, **kwargs)[source]

Alias for findActivity(name, … db_name=BIOSPHERE3_DB_NAME). See doc for #findActivity

findTechAct()

lca_algebraic.findTechAct(name=None, loc=None, single=True, **kwargs)[source]

Search activities in technosphere. This function tries to guess which database is your background database. If you have more than one background technosphere, you should use findActivity() and specify the db_name directly. See also doc for #findActivity

newActivity()

lca_algebraic.newActivity(db_name, name, unit, exchanges: Dict[Activity, float | str] = {}, amount=1, code=None, type='process', **argv)[source]

Creates a new activity

Parameters

name :

Name of the new activity

db_name :

Destination DB : ACV DB by default

unit:

Unit of the process

code:

Unique code in the Db. Optional. If not provided, the name is used

exchanges :

Dict of activity => amount. See the doc for @addExchanges()

argv :

Any extra params passed as properties of the new activity

amount:

Production amount. 1 by default

copyActivity()

lca_algebraic.copyActivity(db_name, activity: ActivityExtended, code, withExchanges=True, **kwargs) ActivityExtended[source]

Copy an activity and its exchanges into another database. You usually want to copy activities from your background to your foreground DB to update them, keeping your background DB clean.

Parameters

db_name:

Name of the target database

activity:

Source activity

code:

Code of the target activity. Also used as its name

Returns

The new activity. note that is is flagged with the custom property inherited_from, providing the full key of the initial activity.

newSwitchAct()

lca_algebraic.newSwitchAct(dbname, name, paramDef: ParamDef, acts_dict: Dict[str, Activity | Tuple[Activity, float]])[source]

Creates a new parametrized, virtual activity, made of a map of other activities, controlled by an enum parameter. This enables to implement a “Switch” with brightway parameters Internally, this will create a linear sum of other activities controlled by <param_name>_<enum_value> : 0 or 1

By default, all activities have associated amount of 1. You can provide other amounts by providing a tuple of (activity, amount).

Parameters

dbname:

name of the target DB

name:

Name of the new activity

paramDef :

parameter definition of type enum

acts_dict :

dict of “enumValue” => activity or “enumValue” => (activity, amount)

Examples

>>> newSwitchAct(MYDB, "switchAct", switchParam, {
>>>    "val1" : act1 # Amount is 1
>>>    "val2" : (act2, 0.4) # Different amount
>>>    "val3" : (act3, b + 6) # Amount with formula
>>> }

printAct()

lca_algebraic.printAct(*activities, **params)[source]

Print activities and their exchanges. If parameter values are provided, formulas will be evaluated accordingly.

Parameters

activities:

One or two activities. If two activities are provided, differences are highlighted.

params:

If provided, the formulas are evaluated accordingly and the result amount is shown instead of formula

Returns

A Dataframe is returned, containing all information, exchange by exchange

interpolate_activities()

lca_algebraic.interpolate_activities(db_name, act_name, param: ParamDef, act_per_value: Dict, add_zero=False)[source]

Creates a virtual activity being a linear interpolation between several activities, based on the value of a parameter.

The result activity is a piecewize linear function of input activities.

This is useful to produce a continuous parametrized activity based on the scale of the system, Given that you have discrete activities corresponding to discrete values of the parameter.

Parameters

db_name:

Name of user DB (string)

act_name:

Name of the new activity

param:

Parameter controlling the interpolation

act_per_value:

Dictionnary of value => activitiy [Dict]

add_zero:

If True add the “Zero” point to the data. Useful for linear interpolation of a single activity / point

Returns

The new activity

Examples

>>> interpolated_inverter = interpolate_activities(
>>>     db_name=USER_DB,
>>>     act_name="interpolated_inverter",
>>>     param=power_param, #power parameter, in kW
>>>     act_per_value={ # Those are activities found in the background database
>>>         .5: inverter_500W,
>>>         2: inverter_2KW,
>>>         50:inverter_50KW,
>>>         100:inverter_100KW},
>>>     add_zero=True):