Skip to content

Commit

Permalink
Corrections to allow the dynamic importing of the ecoinvent biosphere…
Browse files Browse the repository at this point in the history
… flows (#1014)

* Corrections to allow the dynamic importing of the ecoinvent biosphere flow functions for updating the biosphere exchanges

* Reduces the number of imports from bw2io.data and avoids doing so inside a thread
  • Loading branch information
Zoophobus authored Aug 21, 2023
1 parent 30ed32c commit 5a8674b
Showing 1 changed file with 4 additions and 16 deletions.
20 changes: 4 additions & 16 deletions activity_browser/ui/widgets/biosphere_update.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
# -*- coding: utf-8 -*-
import brightway2 as bw
from bw2data.errors import ValidityError
from bw2io.data import (
add_ecoinvent_33_biosphere_flows, add_ecoinvent_34_biosphere_flows,
add_ecoinvent_35_biosphere_flows, add_ecoinvent_36_biosphere_flows, add_ecoinvent_37_biosphere_flows,
add_ecoinvent_38_biosphere_flows,
)
from PySide2 import QtCore, QtWidgets
from PySide2.QtCore import Signal, Slot

import bw2io.data as data
from ...signals import signals


Expand Down Expand Up @@ -40,16 +35,8 @@ def update_progress(self, current: int):


class UpdateBiosphereThread(QtCore.QThread):
PATCHES = (
add_ecoinvent_33_biosphere_flows,
add_ecoinvent_34_biosphere_flows,
add_ecoinvent_35_biosphere_flows,
add_ecoinvent_36_biosphere_flows,
add_ecoinvent_37_biosphere_flows,
add_ecoinvent_38_biosphere_flows,
)
PATCHES = [patch for patch in dir(data) if patch.startswith('add_ecoinvent') and patch.endswith('biosphere_flows')]
progress = Signal(int)

def __init__(self, parent=None):
super().__init__(parent)
self.total_patches = len(self.PATCHES)
Expand All @@ -58,7 +45,8 @@ def run(self):
try:
for i, patch in enumerate(self.PATCHES):
self.progress.emit(i)
patch()
update_bio = getattr(data, patch)
update_bio()
except ValidityError as e:
print("Could not patch biosphere: {}".format(str(e)))
self.exit(1)

0 comments on commit 5a8674b

Please sign in to comment.