Skip to content

Commit 70728aa

Browse files
committed
Move away from using the parameter table for parsing parameter scenarios
1 parent cacb843 commit 70728aa

3 files changed

Lines changed: 82 additions & 88 deletions

File tree

activity_browser/bwutils/superstructure/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@
66
from .file_imports import ABCSVImporter, ABFeatherImporter, ABFileImporter
77
from .manager import SuperstructureManager
88
from .mlca import SuperstructureContributions, SuperstructureMLCA
9-
from .utils import SUPERSTRUCTURE, _time_it_, edit_superstructure_for_string
9+
from .utils import SUPERSTRUCTURE, _time_it_, edit_superstructure_for_string, parameters_to_sdf

activity_browser/bwutils/superstructure/utils.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,23 @@ def wrapper(*args):
8383
return result
8484

8585
return wrapper
86+
87+
88+
def parameters_to_sdf(parameter_scenarios: pd.DataFrame) -> pd.DataFrame:
89+
from activity_browser.bwutils.utils import Parameters
90+
from activity_browser.bwutils import manager, superstructure
91+
92+
parameter_scenarios.set_index(["Group", "Name"], inplace=True)
93+
94+
defaults = [p[:3] for p in Parameters.from_bw_parameters()]
95+
df = pd.DataFrame(defaults, columns=["Name", "Group", "default"]).set_index(["Group", "Name"])
96+
97+
for name in [n for n in parameter_scenarios.columns]:
98+
df[name] = df["default"]
99+
100+
df.update(parameter_scenarios)
101+
102+
pm = manager.ParameterManager()
103+
exchanges = pm.exchanges_from_scenarios(df.columns, [df[scenario] for scenario in df.columns])
104+
return superstructure.superstructure_from_scenario_exchanges(exchanges)
105+

activity_browser/layouts/pages/calculation_setup/scenario_section.py

Lines changed: 61 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66

77
import pandas as pd
88
import bw2data as bd
9+
from activity_browser.bwutils import superstructure as ss
910

1011
from activity_browser import signals
1112
from activity_browser.ui import icons, widgets
12-
from activity_browser.bwutils import superstructure as ss
1313
from activity_browser.bwutils import errors
1414

1515
log = getLogger(__name__)
@@ -295,98 +295,72 @@ def connect_signals(self):
295295

296296
def load_action(self) -> None:
297297
dialog = ExcelReadDialog(self)
298-
if dialog.exec_() == ExcelReadDialog.DialogCode.Accepted:
299-
300-
try:
301-
path = dialog.path
302-
idx = dialog.import_sheet.currentIndex()
303-
file_type_suffix = dialog.path.suffix
304-
separator = dialog.field_separator.currentData()
305-
log.debug("separator == '{}'".format(separator))
306-
QtWidgets.QApplication.setOverrideCursor(Qt.WaitCursor)
307-
log.info("Loading Scenario file. This may take a while for large files")
308-
# Try and read as a superstructure file
309-
# Choose a different routine for reading the file dependent on file type
310-
if file_type_suffix == ".feather":
311-
df = ss.ABFeatherImporter.read_file(path)
312-
elif file_type_suffix.startswith(".xls"):
313-
df = ss.import_from_excel(path, idx)
314-
else:
315-
df = ss.ABCSVImporter.read_file(path, separator=separator)
316-
# Read in the file as a scenario flow table if the file is arranged as one
317-
if len(df.columns.intersection(ss.SUPERSTRUCTURE)) >= 12:
318-
if df is None:
319-
QtWidgets.QApplication.restoreOverrideCursor()
320-
return
321-
self.sync_superstructure(df)
322-
# Read the file as a parameter scenario file if it is correspondingly arranged
323-
elif len(df.columns.intersection({"Name", "Group"})) == 2:
324-
# Try and read as parameter scenario file.
325-
log.info(
326-
"Superstructure: Attempting to read as parameter scenario file."
327-
)
298+
if dialog.exec_() != ExcelReadDialog.DialogCode.Accepted:
299+
return
328300

329-
if not df["Group"].dtype == object:
330-
df["Group"] = df["Group"].astype(str)
331-
332-
include_default = True
333-
if "default" not in df.columns:
334-
query = QtWidgets.QMessageBox.question(
335-
self,
336-
"Default column not found",
337-
"Attempt to load and include the 'default' scenario column?",
338-
QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.No,
339-
QtWidgets.QMessageBox.No,
340-
)
341-
if query == QtWidgets.QMessageBox.No:
342-
include_default = False
343-
signals.parameter_scenario_sync.emit(
344-
self.index, df, include_default
345-
)
346-
else:
347-
# this is a wrong file type
348-
msg = (
349-
"The Activity-Browser is attempting to import a scenario file.<p>During the attempted import"
350-
" another file type was detected. Please check the file type of the attempted import, if it is"
351-
" a scenario file make sure it contains a valid format.</p>"
352-
"<p>A flow exchange scenario file requires the following headers:<br>"
353-
+ ss.edit_superstructure_for_string(sep=", ", fhighlight='"')
354-
+ "</p>"
355-
"<p>A parameter scenario file requires the following:<br>"
356-
+ ss.edit_superstructure_for_string(
357-
["name", "group"], sep=", ", fhighlight='"'
358-
)
359-
+ "</p>"
360-
)
361-
critical = ss.ABPopup.abCritical(
362-
"Wrong file type", msg, QtWidgets.QPushButton("Cancel")
363-
)
301+
try:
302+
path = dialog.path
303+
idx = dialog.import_sheet.currentIndex()
304+
file_type_suffix = dialog.path.suffix
305+
separator = dialog.field_separator.currentData()
306+
log.debug("separator == '{}'".format(separator))
307+
QtWidgets.QApplication.setOverrideCursor(Qt.WaitCursor)
308+
log.info("Loading Scenario file. This may take a while for large files")
309+
# Try and read as a superstructure file
310+
# Choose a different routine for reading the file dependent on file type
311+
if file_type_suffix == ".feather":
312+
df = ss.ABFeatherImporter.read_file(path)
313+
elif file_type_suffix.startswith(".xls"):
314+
df = ss.import_from_excel(path, idx)
315+
else:
316+
df = ss.ABCSVImporter.read_file(path, separator=separator)
317+
# Read in the file as a scenario flow table if the file is arranged as one
318+
if len(df.columns.intersection(ss.SUPERSTRUCTURE)) >= 12:
319+
if df is None:
364320
QtWidgets.QApplication.restoreOverrideCursor()
365-
critical.exec_()
366321
return
367-
except errors.CriticalScenarioExtensionError as e:
368-
# Triggered when combining different scenario files by extension leads to no scenario columns
369-
QtWidgets.QApplication.restoreOverrideCursor()
370-
return
371-
except errors.ScenarioDatabaseNotFoundError as e:
372-
QtWidgets.QApplication.restoreOverrideCursor()
373-
return
374-
except errors.ScenarioExchangeNotFoundError as e:
375-
QtWidgets.QApplication.restoreOverrideCursor()
376-
return
377-
except errors.ImportCanceledError as e:
378-
QtWidgets.QApplication.restoreOverrideCursor()
379-
return
380-
except errors.ScenarioExchangeDataNotFoundError as e:
381-
QtWidgets.QApplication.restoreOverrideCursor()
382-
return
383-
except errors.UnalignableScenarioColumnsWarning as e:
322+
self.sync_superstructure(df)
323+
# Read the file as a parameter scenario file if it is correspondingly arranged
324+
elif len(df.columns.intersection({"Name", "Group"})) == 2:
325+
# Try and read as parameter scenario file.
326+
log.info("Superstructure: Attempting to read as parameter scenario file.")
327+
328+
if not df["Group"].dtype == object:
329+
df["Group"] = df["Group"].astype(str)
330+
331+
df = ss.parameters_to_sdf(df)
332+
self.sync_superstructure(df)
333+
334+
else:
335+
# this is a wrong file type
336+
msg = (
337+
"The Activity-Browser is attempting to import a scenario file.<p>During the attempted import"
338+
" another file type was detected. Please check the file type of the attempted import, if it is"
339+
" a scenario file make sure it contains a valid format.</p>"
340+
"<p>A flow exchange scenario file requires the following headers:<br>"
341+
+ ss.edit_superstructure_for_string(sep=", ", fhighlight='"')
342+
+ "</p>"
343+
"<p>A parameter scenario file requires the following:<br>"
344+
+ ss.edit_superstructure_for_string(
345+
["name", "group"], sep=", ", fhighlight='"'
346+
)
347+
+ "</p>"
348+
)
349+
critical = ss.ABPopup.abCritical(
350+
"Wrong file type", msg, QtWidgets.QPushButton("Cancel")
351+
)
384352
QtWidgets.QApplication.restoreOverrideCursor()
353+
critical.exec_()
385354
return
386-
self.scenario_name.setText(path.name)
387-
self.scenario_name.setToolTip(path.name)
388-
self._parent.save_button(True)
355+
except:
389356
QtWidgets.QApplication.restoreOverrideCursor()
357+
raise
358+
359+
self.scenario_name.setText(path.name)
360+
self.scenario_name.setToolTip(path.name)
361+
self._parent.save_button(True)
362+
363+
QtWidgets.QApplication.restoreOverrideCursor()
390364

391365
def sync_superstructure(self, df: pd.DataFrame) -> None:
392366
"""synchronizes the contents of either a single, or multiple scenario files to create a single scenario

0 commit comments

Comments
 (0)