Skip to content

Commit

Permalink
Partial fixes for US EEIO
Browse files Browse the repository at this point in the history
  • Loading branch information
cmutel committed Dec 18, 2023
1 parent 482c964 commit 56598df
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 34 deletions.
8 changes: 4 additions & 4 deletions bw2io/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"SingleOutputEcospold2Importer",
"unlinked_data",
"UnlinkedData",
"useeio11",
"useeio20",
]

from .version import version as __version__
Expand Down Expand Up @@ -164,9 +164,9 @@ def bw2setup():
create_core_migrations()


def useeio11(name="USEEIO-1.1", collapse_products=False, prune=False):
def useeio20(name="USEEIO-2.0", collapse_products=False, prune=False):
""""""
URL = "https://www.lcacommons.gov/lca-collaboration/ws/public/download/json/repository_US_Environmental_Protection_Agency@USEEIO"
URL = "https://www.lcacommons.gov/lca-collaboration/ws/public/download/json/repository_US_Environmental_Protection_Agency@USEEIO_v2"

if name in databases:
print(f"{name} already present")
Expand All @@ -182,7 +182,7 @@ def useeio11(name="USEEIO-1.1", collapse_products=False, prune=False):

with tempfile.TemporaryDirectory() as td:
dp = Path(td)
print("Downloading US EEIO 1.1")
print("Downloading US EEIO 2.0")
filepath = Path(download_with_progressbar(URL, dirpath=td))

print("Unzipping file")
Expand Down
48 changes: 24 additions & 24 deletions bw2io/importers/json_ld.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,23 +117,23 @@ def write_separate_biosphere_database(self):
self.write_database(data=self.biosphere_database, db_name=db_name)

def flows_as_biosphere_database(self, data, database_name, suffix=" biosphere"):
def boolcheck(lst):
return tuple([elem for elem in lst if elem is not None])

category_mapping = {
obj["@id"]: boolcheck(
obj.get("category", {}).get("categoryPath", [])
+ [obj.get("category", {}).get("name")]
+ [obj["name"]]
)
for obj in data["categories"].values()
}
# def boolcheck(lst):
# return tuple([elem for elem in lst if elem is not None])

# category_mapping = {
# obj["@id"]: boolcheck(
# obj.get("category", {}).get("categoryPath", [])
# + [obj.get("category", {}).get("name")]
# + [obj["name"]]
# )
# for obj in data["categories"].values()
# }

return [
{
"code": obj["@id"],
"name": obj["name"],
"categories": category_mapping[obj["category"]["@id"]],
"categories": obj.get("category", "Unknown").split("/"),
"CAS number": obj.get("cas"),
"database": database_name + suffix,
"exchanges": [],
Expand All @@ -145,23 +145,23 @@ def boolcheck(lst):
]

def flows_as_products(self, data):
def boolcheck(lst):
return tuple([elem for elem in lst if elem is not None])

category_mapping = {
obj["@id"]: boolcheck(
obj.get("category", {}).get("categoryPath", [])
+ [obj.get("category", {}).get("name")]
+ [obj["name"]]
)
for obj in data["categories"].values()
}
# def boolcheck(lst):
# return tuple([elem for elem in lst if elem is not None])

# category_mapping = {
# obj["@id"]: boolcheck(
# obj.get("category", {}).get("categoryPath", [])
# + [obj.get("category", {}).get("name")]
# + [obj["name"]]
# )
# for obj in data["categories"].values()
# }

return [
{
"code": obj["@id"],
"name": obj["name"],
"categories": category_mapping[obj["category"]["@id"]],
"categories": obj.get("category", "Unknown").split("/"),
"location": obj["location"]["name"] if "location" in obj else None,
"exchanges": [],
"unit": "",
Expand Down
4 changes: 2 additions & 2 deletions bw2io/strategies/json_ld.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ def json_ld_add_activity_unit(db):
production_exchanges = [
exc
for exc in ds["exchanges"]
if exc["flow"]["flowType"] == "PRODUCT_FLOW" and not exc["input"]
if exc["flow"]["flowType"] == "PRODUCT_FLOW" and not exc.get("isInput")
]
assert len(production_exchanges) == 1, "Failed allocation"
ds["unit"] = production_exchanges[0]["unit"]
Expand Down Expand Up @@ -514,7 +514,7 @@ def json_ld_label_exchange_type(db):
if exc.get("input"):
raise ValueError("Avoided products are outputs, not inputs")
exc["type"] = "substitution"
elif exc["input"]:
elif exc.get("isInput"):
if not exc.get("flow", {}).get("flowType") == "PRODUCT_FLOW":
raise ValueError("Inputs must be products")
exc["type"] = "technosphere"
Expand Down
8 changes: 4 additions & 4 deletions tests/useeio.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@
import pytest
from bw2data.tests import bw2test

from bw2io import useeio11
from bw2io import useeio20


@pytest.mark.slow
@bw2test
def test_useeio_import():
useeio11("foo")
useeio20("foo")
assert "foo" in bd.databases
assert len(bd.Database("foo")) > 2000


@pytest.mark.slow
@bw2test
def test_useeio_import_collapse_products():
useeio11("foo", collapse_products=True)
useeio20("foo", collapse_products=True)
assert "foo" in bd.databases
assert len(bd.Database("foo")) > 2000
assert not sum(1 for ds in bd.Database("foo") if ds["type"] == "product")
Expand All @@ -34,7 +34,7 @@ def test_useeio_import_collapse_products():
@pytest.mark.slow
@bw2test
def test_useeio_import_prune():
useeio11("foo", collapse_products=True, prune=True)
useeio20("foo", collapse_products=True, prune=True)
assert "foo" in bd.databases
assert len(bd.Database("foo")) > 2000
assert not sum(1 for ds in bd.Database("foo") if ds["type"] == "product")
Expand Down

0 comments on commit 56598df

Please sign in to comment.