Skip to content

Commit

Permalink
Switching to sqlLite backend
Browse files Browse the repository at this point in the history
  • Loading branch information
romainsacchi committed Jul 28, 2024
1 parent 0e45ee8 commit 9927e24
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 78 deletions.
2 changes: 1 addition & 1 deletion dev/Untitled1.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -3299,7 +3299,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.13"
"version": "3.11.9"
}
},
"nbformat": 4,
Expand Down
80 changes: 5 additions & 75 deletions premise/brightway25.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@
This module contains functions to write a Brightway 2.5 database.
"""

import itertools
from copy import copy

from bw2data import Database, databases
from bw2data.backends.iotable.backend import IOTableBackend
from bw2io.importers.base_lci import LCIImporter
from wurst.linking import change_db_name, check_internal_linking, link_internal

Expand All @@ -29,82 +25,16 @@ def __init__(self, db_name: str, data: list) -> None:
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):
"""
Write a Brightway 2.5 database from a Wurst database.
"""

def no_exchange_generator(data):
"""
Remove exchanges from data.
"""
for ds in data:
cp = copy(ds)
cp["exchanges"] = []
yield cp

if self.db_name in databases:
print(f"Database {self.db_name} 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 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,
}

db = IOTableBackend(name=self.db_name)
db.write_exchanges(
technosphere_generator(self.data, lookup),
biosphere_generator(self.data, lookup),
list(dependents),
)


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)

if name in databases:
print(f"Database {name} already exists: it will be overwritten.")
del databases[name]

BW25Importer(name, data).write_database()
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
# for which constructive_geometries
# cannot find an IAM equivalent
---
Europe without Austria:
Europe without Switzerland and Austria:
remind: EUR
image: WEU
message: WEU
tiam-ucl: WEU
gcam: EU-15
Europe without Switzerland and Austria:
Europe without Austria:
remind: EUR
image: WEU
message: WEU
Expand Down

0 comments on commit 9927e24

Please sign in to comment.