|
6 | 6 |
|
7 | 7 | import pandas as pd |
8 | 8 | import bw2data as bd |
| 9 | +from activity_browser.bwutils import superstructure as ss |
9 | 10 |
|
10 | 11 | from activity_browser import signals |
11 | 12 | from activity_browser.ui import icons, widgets |
12 | | -from activity_browser.bwutils import superstructure as ss |
13 | 13 | from activity_browser.bwutils import errors |
14 | 14 |
|
15 | 15 | log = getLogger(__name__) |
@@ -295,98 +295,72 @@ def connect_signals(self): |
295 | 295 |
|
296 | 296 | def load_action(self) -> None: |
297 | 297 | 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 |
328 | 300 |
|
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: |
364 | 320 | QtWidgets.QApplication.restoreOverrideCursor() |
365 | | - critical.exec_() |
366 | 321 | 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 | + ) |
384 | 352 | QtWidgets.QApplication.restoreOverrideCursor() |
| 353 | + critical.exec_() |
385 | 354 | return |
386 | | - self.scenario_name.setText(path.name) |
387 | | - self.scenario_name.setToolTip(path.name) |
388 | | - self._parent.save_button(True) |
| 355 | + except: |
389 | 356 | 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() |
390 | 364 |
|
391 | 365 | def sync_superstructure(self, df: pd.DataFrame) -> None: |
392 | 366 | """synchronizes the contents of either a single, or multiple scenario files to create a single scenario |
|
0 commit comments