Skip to content

Commit

Permalink
Improve choosing and handling of alternative locations for location c…
Browse files Browse the repository at this point in the history
…hoosing
  • Loading branch information
marc-vdm committed Nov 7, 2023
1 parent bd73ac5 commit deb1a81
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
23 changes: 20 additions & 3 deletions activity_browser/controllers/activity.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import uuid

import brightway2 as bw
import pandas as pd
from bw2data.backends.peewee.proxies import Activity, ExchangeProxyBase
from PySide2.QtCore import QObject, Slot, Qt
from PySide2 import QtWidgets
Expand Down Expand Up @@ -163,7 +164,13 @@ def duplicate_activity_new_loc(self, old_key: tuple) -> None:
"""
def find_candidate(dbs, exch, old_location, new_location, use_alternatives, alternatives) -> Optional[object]:
"""Find a candidate to replace the exchange with."""
db = dbs[exch.input[0]]
current_db = exch.input[0]
if current_db == db_name:
db = dbs[current_db]
else: # if the exchange is not from the current database, also check the current
# (user may have added their own alternative dependents already)
db = pd.concat([dbs[current_db], dbs[db_name]])

if db.loc[db['key'] == exch.input]['location'].iloc[0] != old_location:
return # this exchange has a location we're not trying to re-link

Expand Down Expand Up @@ -228,11 +235,21 @@ def find_candidate(dbs, exch, old_location, new_location, use_alternatives, alte

# read the data from the dialog
for old, new in dialog.relink.items():
alternatives = []
new_location = new
use_alternatives = dialog.use_alternatives_checkbox.isChecked()
if dialog.use_rer.isChecked(): # RER
alternatives.append(dialog.use_rer.text())
if dialog.use_ews.isChecked(): # Europe without Switzerland
alternatives.append(dialog.use_ews.text())
if dialog.use_row.isChecked(): # RoW
alternatives.append(dialog.use_row.text())
# the order we add alternatives is important, they are checked in this order!
if len(alternatives) > 0:
use_alternatives = True
else:
use_alternatives = False

succesful_links = {} # dict of dicts, key of new exch : {new values} <-- see 'values' below
alternatives = ['RoW', 'GLO'] # alternatives to try to match to
# in the future, 'alternatives' could be improved by making use of some location hierarchy. From that we could
# get things like if the new location is NL but there is no NL, but RER exists, we use that. However, for that
# we need some hierarchical structure to the location data, which may be available from ecoinvent, but we need
Expand Down
15 changes: 11 additions & 4 deletions activity_browser/ui/widgets/dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -1162,9 +1162,13 @@ def __init__(self, parent=None):
self.grid = QtWidgets.QGridLayout()
self.grid_box.setLayout(self.grid)

self.use_alternatives_checkbox = QtWidgets.QCheckBox('Use generic alternatives (RoW, GLO) as fallback')
self.use_alternatives_checkbox.setToolTip('If the location is not found, try to match to generic locations '
'RoW or GLO (in that order).')
self.use_alternatives_label = QtWidgets.QLabel('Use generic alternatives as fallback:')
self.use_alternatives_label.setToolTip('If the chosen location is not found, try matching the selected '
'locations below too')
self.use_row = QtWidgets.QCheckBox('RoW')
self.use_row.setChecked(True)
self.use_rer = QtWidgets.QCheckBox('RER')
self.use_ews = QtWidgets.QCheckBox('Europe without Switzerland')

self.buttons = QtWidgets.QDialogButtonBox(
QtWidgets.QDialogButtonBox.Ok | QtWidgets.QDialogButtonBox.Cancel,
Expand All @@ -1175,7 +1179,10 @@ def __init__(self, parent=None):
layout = QtWidgets.QVBoxLayout()
layout.addWidget(self.loc_label)
layout.addWidget(self.grid_box)
layout.addWidget(self.use_alternatives_checkbox)
layout.addWidget(self.use_alternatives_label)
layout.addWidget(self.use_row)
layout.addWidget(self.use_rer)
layout.addWidget(self.use_ews)
layout.addWidget(self.buttons)
self.setLayout(layout)

Expand Down

0 comments on commit deb1a81

Please sign in to comment.