-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #123 from cmutel/bw25compat
Support for both Brightway 2 and 2.5
- Loading branch information
Showing
9 changed files
with
234 additions
and
136 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
from bw2data import databases | ||
from bw2io.importers.base_lci import LCIImporter | ||
from wurst.linking import change_db_name, check_internal_linking, link_internal | ||
|
||
from .utils import reset_all_codes | ||
|
||
|
||
class BW2Importer(LCIImporter): | ||
def __init__(self, db_name, data): | ||
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): | ||
if self.db_name in databases: | ||
print(f"Database {self.db_name} already exists: it will be overwritten.") | ||
super().write_database() | ||
|
||
|
||
def write_brightway_database(data, name, reset_codes=False): | ||
# Restore parameters to Brightway2 format | ||
# which allows for uncertainty and comments | ||
change_db_name(data, name) | ||
if reset_codes: | ||
reset_all_codes(data) | ||
link_internal(data) | ||
check_internal_linking(data) | ||
BW2Importer(name, data).write_database() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import itertools | ||
from copy import copy | ||
|
||
from bw2data import Database, databases | ||
from bw2io.importers.base_lci import LCIImporter | ||
from wurst.linking import change_db_name, check_internal_linking, link_internal | ||
|
||
from .utils import reset_all_codes | ||
|
||
|
||
class BW25Importer(LCIImporter): | ||
def __init__(self, db_name, data): | ||
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} 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, | ||
} | ||
|
||
Database(self.db_name).write_exchanges( | ||
technosphere_generator(self.data, lookup), | ||
biosphere_generator(self.data, lookup), | ||
list(dependents), | ||
) | ||
|
||
|
||
def write_brightway_database(data, name, reset_codes=False): | ||
# Restore parameters to Brightway2 format | ||
# which allows for uncertainty and comments | ||
change_db_name(data, name) | ||
if reset_codes: | ||
reset_all_codes(data) | ||
link_internal(data) | ||
check_internal_linking(data) | ||
BW25Importer(name, data).write_database() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
[build-system] | ||
requires = ["setuptools>=61.0"] | ||
build-backend = "setuptools.build_meta" | ||
|
||
[project] | ||
name = "premise" | ||
authors = [ | ||
{ name="Romain Sacchi", email="[email protected]" }, | ||
{ name="Alois Dirnaichner", email="[email protected]" }, | ||
{ name=" Chris Mutel", email="[email protected]" } | ||
] | ||
maintainers = [ | ||
{ name="Romain Sacchi", email="[email protected]" } | ||
] | ||
description = "Coupling IAM output to ecoinvent LCA database ecoinvent for prospective LCA" | ||
readme = "README.md" | ||
dynamic = ["dependencies", "version"] | ||
classifiers = [ | ||
"Intended Audience :: Science/Research", | ||
"License :: OSI Approved :: BSD License", | ||
"Natural Language :: English", | ||
"Programming Language :: Python :: 3", | ||
"Operating System :: OS Independent", | ||
"Topic :: Scientific/Engineering", | ||
] | ||
requires-python = ">=3.9,<3.12" | ||
|
||
[project.urls] | ||
source = "https://github.com/polca/premise" | ||
homepage = "https://github.com/polca/premise" | ||
tracker = "https://github.com/polca/premise/issues" | ||
|
||
[project.optional-dependencies] | ||
testing = [ | ||
"setuptools", | ||
"pytest", | ||
"pytest-cov", | ||
"coveralls" | ||
] | ||
|
||
docs = [ | ||
"sphinx-rtd-theme" | ||
] | ||
bw25 = [ | ||
"bw2analyzer >=0.11.4", | ||
"bw2calc >=2.0.dev13", | ||
"bw2data >=4.0.dev31", | ||
"bw2io >=0.9.dev23", | ||
"bw_processing >=0.8.2", | ||
"matrix_utils >=0.2.5" | ||
] | ||
|
||
[tool.setuptools] | ||
license-files = ["LICENSE"] | ||
include-package-data = true | ||
packages = ["premise"] | ||
|
||
[tool.setuptools.dynamic] | ||
dependencies = {file = ["requirements.txt"]} | ||
version = {attr = "premise.__version__"} | ||
|
||
[tool.pytest.ini_options] | ||
markers = [ | ||
# marks tests that require ecoinvent (to be disabled on Travis) | ||
"ecoinvent", | ||
"serial" | ||
] | ||
norecursedirs = [ | ||
"dist", | ||
"build", | ||
".tox" | ||
] | ||
testpaths = ["tests/*.py"] | ||
|
||
[tool.flake8] | ||
# Some sane defaults for the code style checker flake8 | ||
max_line_length = 88 | ||
extend_ignore = ["E203", "W503"] | ||
# ^ Black-compatible | ||
# E203 and W503 have edge cases handled by black | ||
exclude = [ | ||
".tox", | ||
"build", | ||
"dist", | ||
".eggs", | ||
"docs/conf.py", | ||
] |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.