Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions activity_browser/actions/activity/activity_relink.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
from activity_browser.ui.widgets import (ActivityLinkingDialog,
ActivityLinkingResultsDialog)


class ActivityRelink(ABAction):
"""
ABAction to relink the exchanges of an activity to exchanges from another database.

This action only uses the first key from activity_keys
This action only uses the first key from activity_keys.
Only shows databases relevant to activity.
"""

icon = qicons.edit
Expand All @@ -31,8 +31,10 @@ def run(activity_keys: List[tuple]):
db = bd.Database(key[0])
activity = bd.get_activity(key)

# find the dependents for the database and construct the alternatives in tuple format
depends = db.find_dependents()
# find the dependents for the activity using bw2data method and construct the alternatives in tuple format
data = {}
data[activity.key] = db.load()[activity.key]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am slightly worried about this. db.load() is a very intensive function, and although I realize db.find_dependents() also uses this method, it will be very VERY slow for a database the size of ecoinvent.

The data we need is already there in activity.exchanges(). It should be a lot quicker to just iterate over them and make a set of their input databases.

depends = db.find_dependents(data = data, ignore= [db.name])
options = [(depend, list(bd.databases)) for depend in depends]

# present the alternatives to the user in a linking dialog
Expand All @@ -49,16 +51,15 @@ def run(activity_keys: List[tuple]):

# use the relink_activity_exchanges strategy to relink the exchanges of the activity
relinking_results = {}
failed = 0
for old, new in dialog.relink.items():
other = bd.Database(new)
failed, succeeded, examples = relink_activity_exchanges(
activity, old, other
)
relinking_results[f"{old} --> {other.name}"] = (failed, succeeded)

# restore normal cursor
QtWidgets.QApplication.restoreOverrideCursor()

# if any relinks failed present them to the user
if failed > 0:
relinking_dialog = ActivityLinkingResultsDialog.present_relinking_results(
Expand Down
4 changes: 2 additions & 2 deletions activity_browser/ui/widgets/dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ def construct_results_dialog(
for act, key in unlinked_exchanges.items():
button = QtWidgets.QPushButton(act.as_dict()["name"])
button.clicked.connect(
lambda: signals.unsafe_open_activity_tab.emit(act.key)
lambda: signals.safe_open_activity_tab.emit(act.key)
)
obj.exchangesUnlinked.addWidget(button)
obj.updateGeometry()
Expand Down Expand Up @@ -574,7 +574,7 @@ def construct_results_dialog(
for act, key in unlinked_exchanges.items():
button = QtWidgets.QPushButton(act.as_dict()["name"])
button.clicked.connect(
lambda: signals.unsafe_open_activity_tab.emit(act.key)
lambda: signals.safe_open_activity_tab.emit(act.key)
)
obj.exchangesUnlinked.addWidget(button)
obj.updateGeometry()
Expand Down