brightway

What is the error with the update_social_flows_in_older_consequential function when importing ecoinvent 3.6 conseq to brightway?


When trying to import ecoinvent in brightway,

ei36 = bw2.SingleOutputEcospold2Importer(ei36dir, "ecoinvent 3.6 conseq")
ei36.apply_strategies()
ei36.statistics()
ei36.write_database()

I get the following error message:

*Applying strategy: update_social_flows_in_older_consequential

Traceback (most recent call last):

File "C:\Users\...\test.py", line 24, in <module>
ei36.apply_strategies()

File "C:\Users\...\Lib\site-packages\bw2io\importers\base.py", line 69, in apply_strategies
self.apply_strategy(func, verbose)

File "C:\Users\...\Lib\site-packages\bw2io\importers\base.py", line 48, in apply_strategy
self.data = strategy(self.data)

File "C:\Users\...\Lib\site-packages\bw2io\strategies\ecospold2.py", line 1350, in update_social_flows_in_older_consequential

exc['input'] = cache[exc['name']]

KeyError: 'venting of argon, crude, liquid'*

I had a look at the update_social_flows_in_older_consequential function (below), but I can't figure out the problem.

def update_social_flows_in_older_consequential(db, biosphere_db):

    FLOWS = {
        'residual wood, dry',
        'venting of argon, crude, liquid',
        'venting of nitrogen, liquid',
    }

    cache = {}

    def get_cache(cache, biosphere_db):
        for flow in biosphere_db:
            if flow['name'] in FLOWS:
                cache[flow['name']] = flow.key

    for ds in db:
        for exc in ds['exchanges']:
            if not exc.get('input') and exc['name'] in FLOWS:
                if not cache:
                    get_cache(cache, biosphere_db)
                exc['input'] = cache[exc['name']]
    return db

Is there anybody who encountered a similar issue?


Solution

  • Try this instead:

    import bw2data as bd
    import bw2io as bi
    bd.projects.set_current("CHANGE-ME")
    bi.import_ecoinvent_release('3.6', 'consequential')
    

    Note that this requires a recent version of bw2io (either BW2 or BW2.5).

    Note that you don't need to run bw2setup(). The above command will create the correct biosphere database for you. See the mailing list announcement for more info.

    Why do you get an error? The "social" flows in ecoinvent only exist in the consequential version, and as such were created as that system model was executed. That meant that they didn't have a fixed UUID (this has now changed). So in each release there was a new value (i.e. 73dbaeb8-6b92-490d-859c-88e8148c71c4 in 3.6, 845e9acd-5f87-4ebd-8dbd-0a54cfbbd63b in 3.7), and as bw2io matches against the UUID it needed to be updated. bw2setup() is hard-coded to a specific release version, and if that didn't agree with the expected migration value you would have errors.