0

I am having some issues using the bw2analyzer.traverse_tagged_databases function based on the recommendations of this answer. I would like to apply the bw2analyzer.traverse_tagged_databases function on tags. I have ensured that the activity I am assessing (my_act in the following) has a tag and then some of the inputs exchanges have tags (but not all of them), and some inputs exchanges of the inputs exchanges have tags (not all of them). Here is what I do (omitting some definition lines):

import bw2analyzer as bw2analyzer
ipcc2013 = ('IPCC 2013', 'climate change', 'GWP 100a')
fu = {my_act:1}
lca_calc = bw.LCA(fu,ipcc2013)
lca_calc.lci()
lca_calc.lcia()
result, tree = bw2analyzer.traverse_tagged_databases(fu, ipcc2013, label='tag',default_tag='other')

When I do that, the kernel dies (exact message: Kernel died, restarting. I am using Spyder v3.3.1, brightway2 v2.3, bw2analyzer v0.9.4) and then it runs indefinitely. Am I using the function correctly? What I am missing? Does that work for you? Thank you for your help!

  • I am not sure it is possible to answer this question - can you provide more details on OS version, and host computer? Also, can you clarify if the kernel dies or runs indefinitely? – Chris Mutel Dec 07 '18 at 15:47
  • Thank you Chris. I first wanted to make sure that I use the function in a right way. So no problem with the way I use the function? The LCA calculations work (it gives me a score). I have Windows 10 but I agree that if nobody has the same issue than me then it is an internal problem. It does a weird effect with the kernel. There is a message appearing `Kernel died, restarting` but Spyder is still running. If I am the only one to observe it and that the function is properly used, I will see what I can do. Sorry and thank you! – amilovanoff Dec 07 '18 at 16:08
  • @ChrisMutel, when I do externally the function `traverse_tagged_databases`, it is the function `tagged.recurse_tagged_database` that crashes the kernel. In this function, there is a recursive loop on the inputs of the exchanges. I am wondering if this function works for you on the ecoinvent database? How does it deal with activities that are looped together (e.g., act1 is one input of act2 and act2 is one input of act1)? Sorry and thank you in advance! – amilovanoff Dec 11 '18 at 19:17

1 Answers1

0

I found a solution to make the bw2analyzer.traverse_tagged_databases function works when used on a large database such as ecoinvent. I rewrote the tagged.recurse_tagged_database function by implementing a calculation depth (i.e., the maximum depth to apply the recursive function) and alternating the results when the depth is achieved. Here is my code (I deleted some line from the original function as I am only using one database):

def recurse_tagged_database(activity, amount, method_dict, lca, label, default_tag, secondary_tags=[],product_system_depth=5):
    from bw2data import get_activity
    if isinstance(activity, tuple):
        activity = get_activity(activity)
    inputs = list(activity.technosphere())
    if(product_system_depth > 0):
        return {
            'activity': activity,
            'amount': amount,
            'tag': activity.get(label) or default_tag,
            'secondary_tags':[activity.get(t[0]) or t[1] for t in secondary_tags],
            'impact': 0,
            'biosphere': [{
                'amount': exc['amount'] * amount,
                'impact': exc['amount'] * amount * method_dict.get(exc['input'], 0),
                'tag': exc.get(label) or activity.get(label) or default_tag,
                'secondary_tags':[exc.get(t[0]) or activity.get(t[0]) or t[1] for t in secondary_tags]
            } for exc in activity.biosphere()],
            'technosphere': [recurse_tagged_database(exc.input, exc['amount'] * amount,
                                                     method_dict, lca, label,exc.get(label) or activity.get(label) or default_tag, secondary_tags,product_system_depth=product_system_depth-1)
                             for exc in inputs]
        }
    else:
        return {
            'activity': activity,
            'amount': amount,
            'tag': activity.get(label) or default_tag,
            'secondary_tags':[activity.get(t[0]) or t[1] for t in secondary_tags],
            'impact': redo_lca_score(lca,{activity:amount}),
            'biosphere': [],
            'technosphere': []
        }

Comments:

  • I changed the default_tag inputs when applying the recursive function to exc.get(label) or activity.get(label) or default_tag. Indeed, in my case, I want the impacts of the exchanges to be associated by default to the tag of the parent activity if no tag.
  • In my case, a depth of 5 is enough to get what I want. This already takes some time.