Skip to content

Commit

Permalink
Allow export of parameter scenarios to superstructure
Browse files Browse the repository at this point in the history
  • Loading branch information
dgdekoning committed May 5, 2020
1 parent e47410f commit 37ce49e
Showing 1 changed file with 55 additions and 20 deletions.
75 changes: 55 additions & 20 deletions activity_browser/app/ui/tabs/parameters.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
import uuid
from pathlib import Path

import brightway2 as bw
from bw2data.filesystem import safe_filename
Expand All @@ -19,7 +19,7 @@
ActivityParameterTable, DataBaseParameterTable, ExchangesTable,
ProjectParameterTable, ScenarioTable
)
from ..widgets import ForceInputDialog
from ..widgets import ChoiceSelectionDialog, ForceInputDialog
from .base import BaseRightTab


Expand Down Expand Up @@ -317,19 +317,19 @@ def build_tables(self) -> None:

@Slot(name="loadSenarioTable")
def select_read_file(self):
path, _ = QFileDialog().getOpenFileName(
path, _ = QFileDialog.getOpenFileName(
self, caption="Select prepared scenario file",
dir=project_settings.data_dir, filter=self.tbl.EXCEL_FILTER
filter=self.tbl.EXCEL_FILTER
)
if path:
df = ps_utils.load_scenarios_from_file(path)
self.tbl.sync(df=df)

@Slot(name="saveScenarioTable")
def save_scenarios(self):
filename, _ = QFileDialog().getSaveFileName(
filename, _ = QFileDialog.getSaveFileName(
self, caption="Save current scenarios to Excel",
dir=project_settings.data_dir, filter=self.tbl.EXCEL_FILTER
filter=self.tbl.EXCEL_FILTER
)
if filename:
try:
Expand All @@ -351,22 +351,57 @@ def calculate_scenarios(self):
QMessageBox.Ok, QMessageBox.Ok
)
return
dialog = ForceInputDialog.get_text(
self, "Add label", "Add a label to the calculated scenarios"
flow_scenarios = "Save as flow scenarios (excel)"
presamples = "Save as presamples package (presamples)"
choice_dlg = ChoiceSelectionDialog.get_choice(self, flow_scenarios, presamples)
if choice_dlg.exec_() != ChoiceSelectionDialog.Accepted:
return
if choice_dlg.choice == flow_scenarios:
self.build_flow_scenarios()
elif choice_dlg.choice == presamples:
dialog = ForceInputDialog.get_text(
self, "Add label", "Add a label to the calculated scenarios"
)
if dialog.exec_() == ForceInputDialog.Accepted:
result = dialog.output
if result in ps_utils.find_all_package_names():
overwrite = QMessageBox.question(
self, "Label already in use", "Overwrite the old calculations?",
QMessageBox.Yes | QMessageBox.No, QMessageBox.No
)
if overwrite == QMessageBox.Yes:
older = ps_utils.get_package_path(result)
ps_utils.remove_package(older)
self.build_presamples_packages(safe_filename(result, False))
else:
self.build_presamples_packages(safe_filename(result, False))

def build_flow_scenarios(self) -> None:
"""Calculate exchange changes for each parameter scenario and construct
a flow scenarios template file.
"""
from ...bwutils.superstructure import superstructure_from_arrays

ppm = ps_utils.PresamplesParameterManager()
names, data = zip(*self.tbl.iterate_scenarios())
samples, indices = ppm.arrays_from_scenarios(zip(names, data))
df = superstructure_from_arrays(samples, indices, names)
filename, _ = QFileDialog.getSaveFileName(
self, caption="Save calculated flow scenarios to Excel",
filter=self.tbl.EXCEL_FILTER
)
if dialog.exec_() == ForceInputDialog.Accepted:
result = dialog.output
if result in ps_utils.find_all_package_names():
overwrite = QMessageBox.question(
self, "Label already in use", "Overwrite the old calculations?",
QMessageBox.Yes | QMessageBox.No, QMessageBox.No
if filename:
try:
path = Path(filename)
path = path if path.suffix in {".xlsx", ".xls"} else path.with_suffix(".xlsx")
df.to_excel(excel_writer=path, index=False)
except FileCreateError as e:
QMessageBox.warning(
self, "File save error",
"Cannot save the file, please see if it is opened elsewhere or "
"if you are allowed to save files in that location:\n\n{}".format(e),
QMessageBox.Ok, QMessageBox.Ok
)
if overwrite == QMessageBox.Yes:
older = ps_utils.get_package_path(result)
ps_utils.remove_package(older)
self.build_presamples_packages(safe_filename(result, False))
else:
self.build_presamples_packages(safe_filename(result, False))

def build_presamples_packages(self, name: str):
""" Calculate and store presamples arrays from parameter scenarios.
Expand Down

0 comments on commit 37ce49e

Please sign in to comment.