Parameters¶
Parameters extend Sympy symbols. They can be used in any python expression and result as Sympy Expressions to be used as amounts in Exchanges.
Create parameters¶
Float parameters¶
A float parameter represents a decimal value. The user should provide its name, unit and statistical distribution.
- lca_algebraic.newFloatParam(name, default, min: float | None = None, max: float | None = None, unit: str | None = None, description: str | None = None, label: str | None = None, group: str | None = None, distrib: DistributionType = 'linear', formula=None, save=True, **kwargs)[source]¶
Creates a float (decimal) parameter.
Parameters¶
- name:
Name of the parameter
- default:
Default value
- min:
Minimum value
- max:
Maximum value
- unit:
Unit of the parameter
- description:
Long description (optional)
- label:
Extended name (optional)
- group:
Name of the group (optional). Used to organize parameters.
- distrib:
Type of the distribution (optional) Linear (uniform) by default
- formula:
Sympy expression. Optional. If provided the default value of this parameter (if not provided at runtime) will be computed from other parameter values.
- kwargs:
Extra parameters required for advanced distribution types.
Examples¶
The following code defines a float parameter p1 of unit kg, with a triangle distribution.
>>> p1 = newFloatParam("p1", min=1.0, max=3.0, default=2.0, distrib=DistributionType.TRIANGLE, unit="kg")
Returns¶
The newly created parameter
Distribution types¶
The following types of float distributions are supported.
- class lca_algebraic.DistributionType[source]¶
Type of statistic distribution of a float parameter. Some type of distribution requires extra parameters, in italic, to be provided in the constructor of **ParamDef**()
- BETA = 'beta'¶
Beta distribution with extra params a and b, using default value as ‘loc’ (0 of beta distribution) and std as ‘scale’ (1 of beta distribution) See [scipy doc](https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.beta.html#scipy.stats.beta)
- FIXED = 'fixed'¶
Fixed value, not considered as a variable input for monte carlo simulation.
- LINEAR = 'linear'¶
Uniform distribution between min and max
- LOGNORMAL = 'lognormal'¶
Lognormal distribution, centered on default value (mean), with deviation of std, not truncated
- NORMAL = 'normal'¶
Normal distribution, centered on default value (mean), with deviation of std and truncated between min and max
- TRIANGLE = 'triangle'¶
Triangle distribution between min and max (set to zero probability), with highest probability at default value
Boolean parameters¶
Boolean parameters can take only two values 0 and 1.
- lca_algebraic.newBoolParam(name, default, description: str | None = None, label: str | None = None, group: str | None = None, formula=None, save=True, **kwargs)[source]¶
Creates a boolean parameter.
Parameters¶
- name:
Name of the parameter
- default:
Default value
- description:
Long description (optional)
- label:
Extended name (optional)
- group:
Name of the group (optional). Used to organize parameters.
- formula:
Sympy expression. Optional. If provided the default value of this parameter (if not provided at runtime) will be computed from other parameter values.
Examples¶
>>> p1 = newBoolParam("p1", default=0, group="param group")
Returns¶
The newly created parameter
Enum parameters¶
Enum parameters represent a set of mutually exclusive choices.
- lca_algebraic.newEnumParam(name: str, default: str, values: List[str] | Dict[str, float], description: str | None = None, label: str | None = None, group: str | None = None, save=True, **kwargs)[source]¶
Creates an enum parameter : a set of mutually exclusive boolean choices. Enum parameters themselves are not Sympy symbols. Each of the choice is represented internally as a boolean sympy symbol, that can be accessed via param.symbol(“choice_name”)
Parameters¶
- name:
Name of the parameter
- default:
Default
- values:
Possible choices. The values can be provided as a list of strings, in which case every choice is equiprobable. They can also be provided as a python dictionnary of “choice” => value. The proability to be picked is then the pro-rata of the value.
- description:
Long description (optional)
- label:
Extended name (optional)
- group:
Name of the group (optional). Used to organize parameters.
Examples¶
p1 is an enum param with equiprobable choices “choice_a”, “choice_b” and “choice_c”
>>> p1 = newEnumParam("p1", default="choice_a", values=["choice_a", "choice_b", "choice_c"])
p2 is an anum param with “choice_a” and “choice_b” of probability 25% and “choice_c” of probability 50%.
>>> p2 = newEnumParam("p2", default="choice_a", values={"choice_a":1, "choice_b":1, "choice_c":2})
Utils¶
List of parameters¶
Save / load params¶
- lca_algebraic.resetParams(db_name=None)[source]¶
Clear parameters in live memory (registry) and on disk. Clear either all params (project and all db params) or db params from a single database (if db_name provided).
This is a good practice in your code to start fresh, cleaning your foreground database and parameters and redefine all programmatically at the start. This ensures the state of the projet / database is always in sync your code and your session / in memory.
- lca_algebraic.freezeParams(db_name, **params: Dict[str, float])[source]¶
Freezes amounts in all exchanges for a given set of parameter values. The formulas are computed and the ‘amount’ attributes are set with the result.
This enables parametric datasets to be used by standard, non-parametric tools of Brightway2 (like Activities browser).
Parameters¶
- db_name :
Name of the database for freeze (your foreground db usually)
- params:
All other parameters of this function are threated as the values of lca_algebraic parameters
Examples¶
>>> freezeParams("USER_DB", p1=0.1, p2=3.0)
- lca_algebraic.loadParams(global_variable=True, dbname=None)[source]¶
Load parameters from Brightway database, as per : https://stats-arrays.readthedocs.io/en/latest/
The recommended way is to delete parameters at the start of your session and to define them programmically each time.
However, it can be useful if your parameters come from an import or where defined in Activity Browser.
Parameters¶
- global_variable:
If true, loaded parameters are made available as global variable.
- dbname:
If provided, load only database parameters for the given db
Returns¶
A dictionary of parameters
- lca_algebraic.persistParams()[source]¶
Persist parameters into Brightway project, as per : https://stats-arrays.readthedocs.io/en/latest/.
This is important only in case you want t o: - Use parameters outside lca_algebraic (Activity Browser for instance) - Use loadParams() to init your parameters instead of deleting them and creating them programmaically each time.
This function is automatically run when creating new parameters. However, it should be called manually after updating manually some properties of a parameter.
Misc¶
- lca_algebraic.switchValue(param: EnumParam, **values: Dict[str, float | Expr])[source]¶
Helper method defining an expression that returns a different value / formula for each possible choice of an anum param.
Parameters¶
- param: EnumParam
The enum param
- values: Dict[str, ValueOrExpression]
Each param should correspond to a valid choice of the num parameter.
Examples¶
Given the enum parameter p1 :
>>> p1 = newEnumParam("p1", values=["choice1", "choice2", "choice3"])
The following code defines an expression worth 0.1 for choice1, 0.2 for choice2 and 4 x p2 for choice3
>>> amount = switchValue(p1, choice1=0.1, choice2=0.2, choice3=4*p2)