Skip to content
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

Failure when using activities with biosphere exchanges #39

Closed
simaosr opened this issue Dec 11, 2023 · 8 comments
Closed

Failure when using activities with biosphere exchanges #39

simaosr opened this issue Dec 11, 2023 · 8 comments

Comments

@simaosr
Copy link

simaosr commented Dec 11, 2023

When working with an LCA model on which there are exchanges directly with the biosphere, lca_algebraic fails due to the "formula" field, that means something else.

Changing, in, _getAmountOrFormula, the line

if "formula" in ex :
for
if "formula" in ex and ex["type"] != "biosphere":

solves it for me.

Is there another way of handling activities like this, or this is one right solution?

Thank you.
Simão Rodrigues

@raphaeljolivet
Copy link
Contributor

Hi,

Normally, when stumbling upon biosphere flux, the library creates a dummy activity in the technosphere with a single biosphere flux, in order to be able to ask LCIA impact for it to brightway.

This is done in the method _createTechProxyForBio that checks the db name is "biosphere3" or that the its type is either "emission" or "natural resource".

Also, please ensure that only a single database is marked as foreground, and that neither ecoinvent or the biosphere are marked as foreground db. See setForeground and setBackground

@simaosr
Copy link
Author

simaosr commented Dec 12, 2023

Hi,

Thank you for answering.

I am working with an LCA that has some activities that are adapted from ecoinvent, and in the biosphere flows have the "formula" field filled with its chemical formula. They are in the only foreground DB in this project.

I have the code failing in actToExpression inside _modelToExpr, so before _createTechProxyForBio is called. The expression that is being built is based on the chemical formulas, which are seen as parameters.

I see that in bw2.5, for elementary flows, the "formula" field is correctly assigned to the mathematical relationship, and there is another field "chemical formula", but not in bw2.

Best regards,
Simão

@raphaeljolivet
Copy link
Contributor

This kind of error is usually catched and ignored :
It simply writes a warning message that you could ignore as well.

See :

def _getAmountOrFormula(ex: ExchangeDataset) -> Union[Basic, float]:
    """ Return either a fixed float value or an expression for the amount of this exchange"""
    if 'formula' in ex:
        try:
            return _parse_formula(ex['formula'])
        except Exception as e:
            error("Error while parsing formula '%s' : backing to amount" % ex['formula'])

@simaosr
Copy link
Author

simaosr commented Dec 12, 2023

Yes, I get these warnings, and then it fails in:

            res += formula * act_expr * avoidedBurden

with
TypeError: unsupported operand type(s) for *: 'SingletonRegistry' and 'Symbol'

at this point, formula is "S".

_parse_formula is able to parse a few of the chemical formulas as mathematical formulas and is creating the expression using those.

@raphaeljolivet
Copy link
Contributor

Ok, I never encountered this case. Biosphere formulate were catched by this mecanism before.

Then I guess you fix is a good one !
Does it work after that ?

@simaosr
Copy link
Author

simaosr commented Dec 12, 2023

Yes, with that fix everything works fine for me.

@raphaeljolivet
Copy link
Contributor

Then thanks for this feedback.
I will integrate it in a next release that I plan to do at the beginning of 2024.

Have a good day.

@raphaeljolivet
Copy link
Contributor

For the record, I won't include this fix in the main branch, since it make impossible to parametrize biosphere flux with formulas.
It seems Brightway fixed in with the package bw2io 0.9.DEV7, by using chemical-formula instead of formula.

raphaeljolivet added a commit that referenced this issue Feb 20, 2024
It was breaking tests and make it impossible to parametrize biosphere
flux.
You should rather us ethe fix of bw2io==0.9.DEV7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants