2

I would like to import a formatted Excel file into brightway2 that contains custom biosphere exchanges.

For example, let's say I create the following biosphere activity:

import brightway2 as bw
ef = bw.Database("biosphere3").new_activity(code="foo")
ef['name'] = "bar"
ef['unit'] = "baz"
ef['categories'] = ('undefined',)
ef['type'] = 'new type'
ef.save()

Then, I have an Excel file where with a biosphere exchange that specify the name ('foo'), the database ('biosphere3') a type ('biosphere'), categories ('undefined') and a unit ('baz').

If I try importing the Excel file, my biosphere exchange remains unlinked:

imp = ExcelImporter(my_file)
imp.apply_strategies()
imp.match_database(fields=('name', 'unit', 'location'))
imp.statistics()

Gives: Type biosphere: 1 unique unlinked exchanges

However, if I do this:

import functools
from bw2io.strategies.generic import link_iterable_by_fields
imp.apply_strategy(functools.partial(
                              link_iterable_by_fields,
                              other=(obj for obj in Database("biosphere3")),
                              kind="biosphere",
                              fields=["name","categories","unit"]
                              ))

Then all is good.

Why will the standard strategies not work?

I thought it may have something to do with a difference between the imposed code foo and the code generated by set_code_by_activity_hash, but even when I have a code column (which should prevent a new code from being associated with the exchange in the Excel), the standard strategies don't get me 100% there.

Is there something wrong with the way I'm creating the biosphere activity or the way I'm defining the Excel file fields?

MPa
  • 1,086
  • 1
  • 10
  • 25

0 Answers0