-
Notifications
You must be signed in to change notification settings - Fork 24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Pint Parameters for Brightway2 (legacy) #125
Draft
BenPortner
wants to merge
21
commits into
bw2legacy
Choose a base branch
from
legacy-pint-parameters
base: bw2legacy
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
BenPortner
force-pushed
the
legacy-pint-parameters
branch
2 times, most recently
from
February 13, 2023 15:06
fea510e
to
513d895
Compare
…database. Also introducing `no_pint_units` parameter into helper function `get_new_symbols`. Both are needed make sure known project and activity parameters are not interpreted as units
…rs / parametersets
…` and `ActivityParameter` including pint units
…ntain any parameters
BenPortner
force-pushed
the
legacy-pint-parameters
branch
from
February 16, 2023 19:06
513d895
to
903be83
Compare
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
This PR enables the usage of units in parameter formulas. It uses the newly introduced
bw2parameter.PintInterpreter
andbw2parameter.PintParameterSet
classes for this. Units are permanently stored in thedata
field of the SQLite databases forProjectParameter
,DatabaseParameter
andActivityParameter
so they become available again after reloading.Example
Additional Considerations
Optionality vs. usability
Unit parsing and interpretation relies on the pint package, which is currently not part of the mandatory Brightway2 dependencies. I tried to keep it that way. However, this approach requires extra caution from the user. By default, unit parsing is deactivated. It must be switched on actively by setting
config.use_pint_parameters = True
. If the switch is off and one tries to parse a formula including units, an error will be raised:bw2parameters.errors.MissingName: One or more symbols could not be interpreted. Please check the formula. If it contains units, please set 'bw2parameters.config.use_pint = True': 1 kg + 0.2 kg
.Once a parameter with a unit in its formula is stored in the database, any future manipulation of this parameter or a dependent parameter will require
config.use_pint_parameters = True
or above error will appear (note: non-unit containing and non-dependent parameters can be processed as usual). To prevent such errors, units may be defined optionally in the data field. This way, parameters can be evaluated withuse_pint_parameters
on or off. However, in this case the user must take care that parameters are defined compatibly, otherwise unexpected results may occur:Note that the latter behavior corresponds to the current default: It is left to the user to correctly convert units between formulas. New is the possibility to do so automatically by setting
config.use_pint_parameters = True
.Computation time
Invoking pint for the first time carries a time penalty. Any invocations thereafter are as fast as using the default (asteval) interpreter. I attached a benchmark script to this PR. Short summary:
Requirements
This PR builds on top of two other PRs, which need to be approved and merged first:
Todo (after review):
Attachments
bw2data-pint-benchmark.py.txt