Skip to content

Commit

Permalink
Fix detection of metals inputs in datasets in metals.py
Browse files Browse the repository at this point in the history
  • Loading branch information
romainsacchi authored and romainsacchi committed Aug 7, 2024
1 parent 07a372d commit fa9065d
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 16 deletions.
9 changes: 4 additions & 5 deletions premise/clean_datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,10 @@ def remove_uncertainty(database):

for dataset in database:
for exchange in dataset["exchanges"]:
if not exchange.get("preserve uncertainty", False):
exchange["uncertainty type"] = 0
exchange["loc"] = exchange["amount"]
for key in uncertainty_keys:
exchange[key] = nan_value
exchange["uncertainty type"] = 0
exchange["loc"] = exchange["amount"]
for key in uncertainty_keys:
exchange[key] = nan_value

return database

Expand Down
Binary file not shown.
2 changes: 1 addition & 1 deletion premise/data/fuels/hydrogen_efficiency_parameters.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ from electrolysis:
var: hydrogen, from electrolysis
feedstock name: electricity, low voltage
feedstock unit: kilowatt hour
floor value: 48.0
floor value: 48.9
from SMR of biogas, with CCS:
name: hydrogen production, steam methane reforming, from biomethane, with CCS
var: hydrogen, from biogas, with CCS
Expand Down
19 changes: 19 additions & 0 deletions premise/external.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""

import logging
import math
import uuid
from collections import defaultdict
from functools import lru_cache
Expand Down Expand Up @@ -1461,6 +1462,19 @@ def relink_to_new_datasets(
if isinstance(new_loc, str):
new_loc = [(new_loc, 1.0)]

def redefine_loc(e):
if e.get("uncertainty type") in [0, 3, 4]:
return e["amount"] * ratio * share

elif e.get("uncertainty type") == 5:
return e.get("loc", 0) * (e["amount"] * ratio * share / e["amount"])

elif e.get("uncertainty type") == 2:
return math.log(e["amount"] * ratio * share)

else:
return None

if len(new_loc) > 0:
for loc, share in new_loc:
# add new exchange
Expand All @@ -1472,6 +1486,11 @@ def relink_to_new_datasets(
"location": loc,
"name": new_name,
"product": new_ref,
"uncertainty type": exc.get("uncertainty type", 0),
"loc": redefine_loc(exc),
"scale": exc.get("scale", 0) if "scale" in exc else None,
"minimum": exc.get("minimum", 0) * (exc["amount"] * ratio * share / exc["amount"]) if "minimum" in exc else None,
"maximum": exc.get("maximum", 0) * (exc["amount"] * ratio * share / exc["amount"]) if "maximum" in exc else None,
}
)

Expand Down
28 changes: 18 additions & 10 deletions premise/transformation.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import copy
import logging.config
import math
import uuid
from collections import defaultdict
from collections.abc import ValuesView
Expand Down Expand Up @@ -46,6 +47,20 @@
logger = logging.getLogger("module")


def redefine_loc(e):
if e.get("uncertainty type") in [0, 3, 4]:
return e["amount"]

elif e.get("uncertainty type") == 5:
return e["amount"]

elif e.get("uncertainty type") == 2:
return math.log(e["amount"])

else:
return None


def get_suppliers_of_a_region(
database: List[dict],
locations: List[str],
Expand Down Expand Up @@ -1066,16 +1081,8 @@ def relink_datasets(self, excludes_datasets=None, alt_names=None):
(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", None) is not None
else None
),
"scale": (
exc.get("scale", 0) / exc["amount"]
if exc.get("scale", None) is not None
else None
),
"loc": redefine_loc(exc) if exc.get("loc") else None,
"scale": exc.get("scale"),
"minimum": (
exc.get("minimum", 0) / exc["amount"]
if exc.get("minimum", None) is not None
Expand Down Expand Up @@ -1124,6 +1131,7 @@ def relink_datasets(self, excludes_datasets=None, alt_names=None):
exc["uncertainty type"] = old_uncertainty[key][
"uncertainty type"
]
#TODO: fix this
for k, v in old_uncertainty[key].items():
if k != "uncertainty type":
if v is not None:
Expand Down

0 comments on commit fa9065d

Please sign in to comment.