Find an exchange of an activity by a unique identifier other than the name in Brightway2

158 Views Asked by At

I want to find all activities in my Ecoinvent database that have activity 'market for electricity, high voltage' as an exchange. The problem is that the 'name' of an activity is not the 'name' of the same activity when it is an exchange as I had to find out.

When I search for the activity:

marketforelectricity = [act for act in eidb if 'market for electricity, high voltage' in act['name'] and act['location']=='DE']

it yields the right activity...

But when I loop over the exchanges of an activity and search for the exchange with the name of marketforelectricity it yields nothing even though I know from the activity browser that it includes this exchange...

test_activity = [act for act in eidb if act['name']=='silicon production, electronics grade' and act['location']=='DE'][0]
for exc in test_activity.exchanges():
    if 'market for electricity, high voltage' in exc['name']:
        print(exc)

I realised that the name of the exchange is different than its name as an activity. When I search for it without the string "market for":

for exc in test_activity.exchanges():
    if 'electricity, high voltage' in exc['name']:
        print(exc)

...it finds the exchange, but it also finds anotherone since there is another exchange with exc['name] == 'electricity, high voltage'. Looking into the activity browser I recognised that exc['name'] is in the "Product" column and not the "Activity" column. Now how do I access the "Activity" column then if ['name'] doesn't work? I then tried to find the exchange by its activity key but exc['code'] or exc['key'] does not exist either. I guess there must be a way to uniquely identify an exchange somehow. Thanks in advance for the help.

1

There are 1 best solutions below

2
On

A technosphere exchange occurs between two activities, it is an edge between two nodes. If you want to find activities that have exchanges between specific activities you need to look into the input or the output of the exchange, not the name of the exchange. In your case you could do something like :

marketforelectricity = [act for act in eidb if 'market for electricity, high voltage' in act['name'] and act['location']=='DE'][0]

for act in eidb:
    for ex in act.exchanges():
        
        if ex.input == marketforelectricity:

            print(act)
            break

or substituting exchanges() by technosphere() to save some time. because you know it is going to be a technosphere exchange.

depending on the version of brightway you are using you could simply use marketforelectricity.consumers() to get all the exchanges involving your chosen activity. It is way faster