brightway-lca / brightway2-calc

The calculation engine for the Brightway2 life cycle assessment framework.
BSD 3-Clause "New" or "Revised" License
14 stars 16 forks source link

LCIA result as a function #47

Closed simb-sdu closed 2 years ago

simb-sdu commented 2 years ago

Help - I don't know where to ask this question.

I think BW2 could benefit from being able to calculate results outputting a function instead of a number.

eg. GWP(x) = a x + b

In my mind this should be doable, and I would like to contribute to BW2 by developing this feature. However I don't really know where to start. Any pointers would be appreciated.

From what I can see, Activity Browser does not allow parameters to have unknown values

marc-vdm commented 2 years ago

I'm not well versed in parameters enough to answer this in detail, but you may want to have a look at lca_algebraic, that may perhaps give you some pointers.

simb-sdu commented 2 years ago

Hi Marc. Thanks for sharing, this seems super cool!! I will experiment to see if it can do the job, and consider implementation into Activity Browser

marc-vdm commented 2 years ago

Looking forward to your findings!

cmutel commented 2 years ago

If I understand correctly, this already exists in LCA.redo_lcia(demand_dict).

cmutel commented 2 years ago

Closing this, as the LCA object can easily be turned into a callable or wrapped in a function, and redo_lci and redo_lcia exist and are well tested.

simb-sdu commented 2 years ago

Closing this, as the LCA object can easily be turned into a callable or wrapped in a function, and redo_lci and redo_lcia exist and are well tested.

Sorry for my lacking reply. I looked into the documentation of redo_lci and redo_lcia and I could not figure how these could be used to turn the LCIA results into a function. Can somebody give me some pointers?

cmutel commented 2 years ago

If you only want the score, you can just call .redo_lcia() and in the next line get the score :)

You can also make it directly callable, e.g. with functools:

from functools import partial

def redoer(lca_instance, func_unit):
    lca_instance.redo_lcia(func_unit)
    return lca_instance.score

lca_func = partial(redoer, lca_instance=my_lca_instance)
simb-sdu commented 2 years ago

Thank you Chris. However if I understand correctly, this would output a numerical score as a function of the func_unit?

My question might have poorly phrased, but what I am looking for is the output to be a symbolic/parametric representation of the score. lca_algebraic does this, however unfortunately I cannot make it work

cmutel commented 2 years ago
import bw2data as bd
import bw2calc as bc
from functools import partial

bd.projects.set_current("to_dataframe USEEIO")

some_products = [node for node in bd.Database("US EEIO 1.1") if node['type'] == 'product'][:10]

my_lca_instance = bc.LCA({some_products[0]: 1}, method=('Impact Potential', 'HRSP'))
my_lca_instance.lci()
my_lca_instance.lcia()

def redoer(lca_instance, func_unit):
    lca_instance.redo_lcia(func_unit)
    return lca_instance.score

lca_func = partial(redoer, lca_instance=my_lca_instance)

# In Brightway 2.5; in Brightway 2 you can do `func_unit={product: 1})`
for product in some_products:
    print(lca_func(func_unit={product.id: 1}))

Prints:

0.00047385311475121265
0.0001826689388104925
0.0033491725057269886
0.0004923539346780334
0.00024265422885614288
0.0
0.001607357828775192
0.0004528422027446008
0.00036792279828114845
0.0001962813230568915