Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/pathways' into pathways
Browse files Browse the repository at this point in the history
  • Loading branch information
romainsacchi authored and romainsacchi committed Aug 5, 2024
2 parents 4a99443 + 0bd1bde commit 02be4c1
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 20 deletions.
4 changes: 1 addition & 3 deletions premise/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -960,9 +960,7 @@ def check_geographical_linking(scenario, original_database):
return scenario


def prepare_db_for_export(
scenario, name, original_database, biosphere_name=None
):
def prepare_db_for_export(scenario, name, original_database, biosphere_name=None):
"""
Prepare a database for export.
"""
Expand Down
8 changes: 6 additions & 2 deletions premise/new_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -632,7 +632,9 @@ def __find_cached_db(self, db_name: str) -> List[dict]:
db_name = f"ecospold_{self.system_model}_{self.version}"

uncertainty_data = (
"w_uncertainty" if self.keep_source_db_uncertainty is True else "wo_uncertainty"
"w_uncertainty"
if self.keep_source_db_uncertainty is True
else "wo_uncertainty"
)

file_name = (
Expand Down Expand Up @@ -665,7 +667,9 @@ def __find_cached_inventories(self, db_name: str) -> Union[None, List[dict]]:
db_name = f"ecospold_{self.system_model}_{self.version}"

uncertainty_data = (
"w_uncertainty" if self.keep_imports_uncertainty is True else "wo_uncertainty"
"w_uncertainty"
if self.keep_imports_uncertainty is True
else "wo_uncertainty"
)

file_name = (
Expand Down
78 changes: 65 additions & 13 deletions premise/transformation.py
Original file line number Diff line number Diff line change
Expand Up @@ -1062,12 +1062,30 @@ def relink_datasets(self, excludes_datasets=None, alt_names=None):
for exc in excs_to_relink:
if exc["type"] == "technosphere":
if exc.get("uncertainty type", 0) != 0:
old_uncertainty[(exc["name"], exc.get("product"), exc["unit"])] = {
old_uncertainty[
(exc["name"], exc.get("product"), exc["unit"])
] = {
"uncertainty type": exc.get("uncertainty type", 0),
"loc": exc.get("loc", 0) / exc["amount"] if exc.get("loc") else None,
"scale": exc.get("scale", 0) / exc["amount"] if exc.get("scale") else None,
"minimum": exc.get("minimum", 0) / exc["amount"] if exc.get("minimum") else None,
"maximum": exc.get("maximum", 0) / exc["amount"] if exc.get("maximum") else None,
"loc": (
exc.get("loc", 0) / exc["amount"]
if exc.get("loc")
else None
),
"scale": (
exc.get("scale", 0) / exc["amount"]
if exc.get("scale")
else None
),
"minimum": (
exc.get("minimum", 0) / exc["amount"]
if exc.get("minimum")
else None
),
"maximum": (
exc.get("maximum", 0) / exc["amount"]
if exc.get("maximum")
else None
),
}

# make a dictionary with the names and amounts
Expand Down Expand Up @@ -1103,7 +1121,9 @@ def relink_datasets(self, excludes_datasets=None, alt_names=None):
for exc in new_exchanges:
key = (exc["name"], exc["product"], exc["unit"])
if key in old_uncertainty:
exc["uncertainty type"] = old_uncertainty[key]["uncertainty type"]
exc["uncertainty type"] = old_uncertainty[key][
"uncertainty type"
]
for k, v in old_uncertainty[key].items():
if k != "uncertainty type":
if v is not None:
Expand Down Expand Up @@ -1972,10 +1992,26 @@ def relink_technosphere_exchanges(
if exc.get("uncertainty type", 0) != 0:
old_uncertainty[(exc["name"], exc.get("product"), exc["unit"])] = {
"uncertainty type": exc.get("uncertainty type", 0),
"loc": exc.get("loc", 0) / exc["amount"] if exc.get("loc") else None,
"scale": exc.get("scale", 0) / exc["amount"] if exc.get("scale") else None,
"minimum": exc.get("minimum", 0) / exc["amount"] if exc.get("minimum") else None,
"maximum": exc.get("maximum", 0) / exc["amount"] if exc.get("maximum") else None,
"loc": (
exc.get("loc", 0) / exc["amount"]
if exc.get("loc")
else None
),
"scale": (
exc.get("scale", 0) / exc["amount"]
if exc.get("scale")
else None
),
"minimum": (
exc.get("minimum", 0) / exc["amount"]
if exc.get("minimum")
else None
),
"maximum": (
exc.get("maximum", 0) / exc["amount"]
if exc.get("maximum")
else None
),
}

new_exchanges = self.find_candidates(
Expand All @@ -1998,11 +2034,27 @@ def relink_technosphere_exchanges(
"type": "technosphere",
"amount": sum(exc["amount"] for exc in exchanges),
}
for (name, prod, location, unit, ), exchanges in groupby(
for (
name,
prod,
location,
unit,
), exchanges in groupby(
sorted(
new_exchanges, key=itemgetter("name", "product", "location", "unit",)
new_exchanges,
key=itemgetter(
"name",
"product",
"location",
"unit",
),
),
key=itemgetter(
"name",
"product",
"location",
"unit",
),
key=itemgetter("name", "product", "location", "unit",),
)
]

Expand Down
4 changes: 3 additions & 1 deletion premise/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,9 @@ def hide_messages():
"""

print("Keep uncertainty data?")
print("NewDatabase(..., keep_source_db_uncertainty=True), keep_imports_uncertainty=True)")
print(
"NewDatabase(..., keep_source_db_uncertainty=True), keep_imports_uncertainty=True)"
)
print("")
print("Hide these messages?")
print("NewDatabase(..., quiet=True)")
Expand Down
4 changes: 3 additions & 1 deletion premise/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,9 @@ def check_uncertainty(self):
int(exc["uncertainty type"])
]
):
message = f"Exchange {exc['name']} has incomplete uncertainty data."
message = (
f"Exchange {exc['name']} has incomplete uncertainty data."
)
self.log_issue(ds, "incomplete uncertainty data", message)

def check_datasets_integrity(self):
Expand Down

0 comments on commit 02be4c1

Please sign in to comment.