Skip to content

Commit e63ace0

Browse files
authored
Merge pull request #1388
* Show non-found methods and acts in the cs tables so they can be deleted * Added name/key to not found cell
1 parent 0068e31 commit e63ace0

1 file changed

Lines changed: 35 additions & 21 deletions

File tree

activity_browser/ui/tables/models/lca_setup.py

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33

44
import numpy as np
55
import pandas as pd
6+
from PySide2 import QtWidgets
67
from PySide2.QtCore import QModelIndex, Qt, Slot
78

8-
from activity_browser import signals
9+
from activity_browser import signals, application
910
from activity_browser.bwutils import commontasks as bc
1011
from activity_browser.mod import bw2data as bd
1112
from activity_browser.mod.bw2data.backends import ActivityDataset
@@ -124,7 +125,7 @@ def sync(self):
124125
columns=self.HEADERS,
125126
)
126127
# Drop rows where the fu key was invalid in some way.
127-
self._dataframe = df.dropna().reset_index(drop=True)
128+
self._dataframe = df
128129
self.key_col = self._dataframe.columns.get_loc("key")
129130
self.updated.emit()
130131

@@ -145,7 +146,8 @@ def build_row(self, key: tuple, amount: float = 1.0) -> dict:
145146
log.error(
146147
f"Could not load key '{key}' in Calculation Setup '{self.current_cs}'"
147148
)
148-
return {}
149+
150+
return {"key": key, "Amount": amount, "Activity": f"NOT FOUND: {key}", "Database": key[0]}
149151

150152
@Slot(name="deleteRows")
151153
def delete_rows(self, proxies: list) -> None:
@@ -155,9 +157,12 @@ def delete_rows(self, proxies: list) -> None:
155157

156158
# we can disconnect from the deleted activities
157159
for key in [self._dataframe.at[row, "key"] for row in rows]:
158-
activity = bd.get_activity(key)
159-
activity.changed.disconnect(self.sync)
160-
del self._activities[activity.key]
160+
try:
161+
activity = bd.get_activity(key)
162+
activity.changed.disconnect(self.sync)
163+
del self._activities[activity.key]
164+
except ActivityDataset.DoesNotExist:
165+
pass
161166

162167
self._dataframe = self._dataframe.drop(rows).reset_index(drop=True)
163168
self.updated.emit()
@@ -230,7 +235,6 @@ def sync(self) -> None:
230235
method_tuples = [
231236
mthd
232237
for mthd in bd.calculation_setups[self.current_cs].get("ia", [])
233-
if mthd in bd.methods
234238
]
235239

236240
# build rows for all the collected methods and store in our dataframe
@@ -244,23 +248,30 @@ def build_row(self, method_tuple: tuple) -> dict:
244248
"""
245249
Build a single row for the methods table and connect the table to the method we're building the row for.
246250
"""
247-
# gather data using the given method_tuple
248-
method_metadata = bd.methods[method_tuple]
249-
method = bd.Method(method_tuple)
251+
try:
252+
# gather data using the given method_tuple
253+
method_metadata = bd.methods[method_tuple]
254+
method = bd.Method(method_tuple)
255+
256+
# construct a row dictionary
257+
row = {
258+
"Name": ", ".join(method_tuple),
259+
"Unit": method_metadata.get("unit", "Unknown"),
260+
"# CFs": method_metadata.get("num_cfs", 0),
261+
"method": method_tuple,
262+
}
250263

251-
# construct a row dictionary
252-
row = {
253-
"Name": ", ".join(method_tuple),
254-
"Unit": method_metadata.get("unit", "Unknown"),
255-
"# CFs": method_metadata.get("num_cfs", 0),
256-
"method": method_tuple,
257-
}
264+
# if the method changes we need to sync
265+
method.changed.connect(self.sync, Qt.UniqueConnection)
266+
self._methods[method.name] = method
258267

259-
# if the method changes we need to sync
260-
method.changed.connect(self.sync, Qt.UniqueConnection)
261-
self._methods[method.name] = method
268+
return row
269+
except KeyError:
270+
log.error(
271+
f"Could not load key '{method_tuple}' in Calculation Setup '{self.current_cs}'"
272+
)
262273

263-
return row
274+
return {"Name": f"NOT FOUND: {method_tuple}", "Unit": "Unknown", "# CFs": 0, "method": method_tuple}
264275

265276
@Slot(list, name="deleteRows")
266277
def delete_rows(self, proxies: list) -> None:
@@ -270,6 +281,9 @@ def delete_rows(self, proxies: list) -> None:
270281

271282
# we can disconnect from the deleted methods
272283
for method_tuple in [self._dataframe.at[row, "method"] for row in rows]:
284+
if method_tuple not in bd.methods:
285+
continue
286+
273287
method = bd.Method(method_tuple)
274288
method.changed.disconnect(self.sync)
275289
del self._methods[method.name]

0 commit comments

Comments
 (0)