Skip to content

Commit

Permalink
Fix BW25 database export function
Browse files Browse the repository at this point in the history
  • Loading branch information
romainsacchi authored and romainsacchi committed Nov 27, 2024
1 parent 3f3d81b commit df3e1c6
Showing 1 changed file with 18 additions and 72 deletions.
90 changes: 18 additions & 72 deletions unfold/brightway25_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,88 +5,34 @@
from bw2io.importers.base_lci import LCIImporter


def write_brightway_database(data, name):
# Restore parameters to Brightway2 format
# which allows for uncertainty and comments
BW25UnfoldExporter(name, data).write_database()


class BW25UnfoldExporter(LCIImporter):
"""
Inherits from `LCIImporter` to
allow existing databases to be
written to disk.
Class to write a Brightway 2.5 database from a Wurst database.
"""

def __init__(self, db_name, data):
def __init__(self, db_name: str, data: list) -> None:
"""
:param db_name: Name of the database to write
:type db_name: str
:param data: Wurst database
:type data: list
"""
super().__init__(db_name)
self.db_name = db_name
self.data = data
for act in self.data:
act["database"] = self.db_name

# we override `write_database`
# to allow existing databases
# to be overwritten
def write_database(self):
def no_exchange_generator(data):
for ds in data:
cp = copy(ds)
cp["exchanges"] = []
yield cp

if self.db_name in databases:
print(
f"Database {self.db_name} " f"already exists: it will be overwritten."
)
super().write_database(
list(no_exchange_generator(self.data)), backend="iotable"
)

dependents = {exc["input"][0] for ds in self.data for exc in ds["exchanges"]}
lookup = {
obj.key: obj.id
for obj in itertools.chain(*[Database(label) for label in dependents])
}

def technosphere_generator(data, lookup):
for ds in data:
target = lookup[(ds["database"], ds["code"])]
for exc in ds["exchanges"]:
if exc["type"] in (
"substitution",
"production",
"generic production",
):
yield {
"row": lookup[exc["input"]],
"col": target,
"amount": exc["amount"],
"flip": False,
}
elif exc["type"] == "technosphere":
yield {
"row": lookup[exc["input"]],
"col": target,
"amount": exc["amount"],
"flip": True,
}
def write_brightway_database(data: list, name: str) -> None:
# Restore parameters to Brightway2 format
# which allows for uncertainty and comments
change_db_name(data, name)
link_internal(data)
check_internal_linking(data)

def biosphere_generator(data, lookup):
for ds in data:
target = lookup[(ds["database"], ds["code"])]
for exc in ds["exchanges"]:
if exc["type"] == "biosphere":
yield {
"row": lookup[exc["input"]],
"col": target,
"amount": exc["amount"],
"flip": False,
}
if name in databases:
print(f"Database {name} already exists: it will be overwritten.")
del databases[name]

Database(self.db_name).write_exchanges(
technosphere_generator(self.data, lookup),
biosphere_generator(self.data, lookup),
list(dependents),
)
BW25Importer(name, data).write_database()

0 comments on commit df3e1c6

Please sign in to comment.