Skip to content

Commit

Permalink
Align wrt pathways.
Browse files Browse the repository at this point in the history
  • Loading branch information
romainsacchi committed Aug 6, 2024
1 parent f5418f8 commit 21fdef2
Show file tree
Hide file tree
Showing 19 changed files with 210 additions and 52 deletions.
2 changes: 1 addition & 1 deletion dev/Untitled1.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -4175,7 +4175,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.8"
"version": "3.11.9"
}
},
"nbformat": 4,
Expand Down
41 changes: 41 additions & 0 deletions premise/clean_datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,42 @@ def remove_categories(database: List[dict]) -> List[dict]:
return database


def strip_string_from_spaces(database: List[dict]) -> List[dict]:
"""
Strip strings from spaces in the dataset of the wurst inventory database.
Modifies in place (does not return anything).
:param database: wurst inventory database
:type database: list
"""
for dataset in database:
dataset["name"] = dataset["name"].strip()
# also check for unicode characters like \xa0
dataset["name"] = dataset["name"].replace("\xa0", "")

dataset["reference product"] = dataset["reference product"].strip()
dataset["location"] = dataset["location"].strip()
for exc in dataset["exchanges"]:
exc["name"] = exc["name"].strip()
# also check for unicode characters like \xa0
exc["name"] = exc["name"].replace("\xa0", "")
if exc.get("product"):
exc["product"] = exc["product"].strip()
# also check for unicode characters like \xa0
exc["product"] = exc["product"].replace("\xa0", "")
if exc.get("reference product"):
exc["reference product"] = exc["reference product"].strip()
# also check for unicode characters like \xa0
exc["reference product"] = exc["reference product"].replace("\xa0", "")
if exc.get("location"):
exc["location"] = exc["location"].strip()
if exc.get("unit"):
exc["unit"] = exc["unit"].strip()

return database


class DatabaseCleaner:
"""
Class that cleans the datasets contained in the inventory database for further processing.
Expand All @@ -163,6 +199,8 @@ def __init__(
)
self.database = wurst.extract_brightway2_databases(source_db)
self.database = remove_categories(self.database)
# strip strings form spaces
self.database = strip_string_from_spaces(self.database)

if source_type == "ecospold":
# The ecospold data needs to be formatted
Expand All @@ -171,6 +209,9 @@ def __init__(
)
ecoinvent.apply_strategies()
self.database = ecoinvent.data
# strip strings form spaces
self.database = strip_string_from_spaces(self.database)

# Location field is added to exchanges
self.add_location_field_to_exchanges()
# Product field is added to exchanges
Expand Down
Binary file modified premise/data/additional_inventories/lci-battery-capacity.xlsx
Binary file not shown.
Binary file modified premise/data/additional_inventories/lci-biofuels.xlsx
Binary file not shown.
Binary file modified premise/data/additional_inventories/lci-buses.xlsx
Binary file not shown.
Binary file modified premise/data/additional_inventories/lci-carbon-capture.xlsx
Binary file not shown.
Binary file modified premise/data/additional_inventories/lci-hydrogen-distribution.xlsx
Binary file not shown.
Binary file not shown.
Binary file modified premise/data/additional_inventories/lci-pass_cars.xlsx
Binary file not shown.
Binary file modified premise/data/additional_inventories/lci-trucks.xlsx
Binary file not shown.
Binary file modified premise/data/additional_inventories/lci-two_wheelers.xlsx
Binary file not shown.
2 changes: 1 addition & 1 deletion premise/electricity.py
Original file line number Diff line number Diff line change
Expand Up @@ -1550,7 +1550,7 @@ def create_region_specific_power_plants(self):
ws.either(
*[ws.contains("name", name) for name in list_datasets_to_duplicate]
),
# ws.exclude(ws.contains("name", "market")),
ws.exclude(ws.contains("name", "market")),
ws.exclude(ws.contains("name", ", oxy, ")),
ws.exclude(ws.contains("name", ", pre, ")),
):
Expand Down
11 changes: 5 additions & 6 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, keep_uncertainty_data=False, biosphere_name=None
):
def prepare_db_for_export(scenario, name, original_database, biosphere_name=None):
"""
Prepare a database for export.
"""
Expand All @@ -979,7 +977,6 @@ def prepare_db_for_export(
original_database=original_database,
database=scenario["database"],
db_name=name,
keep_uncertainty_data=keep_uncertainty_data,
biosphere_name=biosphere_name,
)
validator.run_all_checks()
Expand All @@ -991,15 +988,13 @@ def _prepare_database(
scenario,
db_name,
original_database,
keep_uncertainty_data,
biosphere_name,
):

scenario["database"] = prepare_db_for_export(
scenario,
name=db_name,
original_database=original_database,
keep_uncertainty_data=keep_uncertainty_data,
biosphere_name=biosphere_name,
)

Expand Down Expand Up @@ -1169,6 +1164,10 @@ def create_B_matrix_coordinates(self):
"Cannot find the biosphere flow",
exc["name"],
exc["categories"],
"in ",
ds["name"],
ds["reference product"],
ds["location"],
)
row = ()
list_rows.append(row)
Expand Down
2 changes: 1 addition & 1 deletion premise/iam_variables_mapping/electricity_variables.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -902,7 +902,7 @@ Solar PV Residential:
Storage, Battery:
ecoinvent_aliases:
fltr:
- market for battery capacity, stationary (CONT scenario)
- electricity supply, from stationary battery (CONT scenario)
iam_aliases:
image: Secondary Energy|Electricity|Storage
message: Secondary Energy|Electricity|Storage
Expand Down
24 changes: 12 additions & 12 deletions premise/inventory_imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,22 +262,18 @@ def check_uncertainty_data(data, filename):
exc["uncertainty type"] = 0

if exc["uncertainty type"] not in {0, 1}:
if not all(
f in exc
missing_parameters = [
f
for f in MANDATORY_UNCERTAINTY_FIELDS[exc["uncertainty type"]]
):
if exc.get(f) is None
]
if missing_parameters:
rows.append(
[
dataset["name"][:30],
exc["name"][:30],
exc["uncertainty type"],
[
f
for f in MANDATORY_UNCERTAINTY_FIELDS[
exc["uncertainty type"]
]
if f not in exc
],
missing_parameters,
]
)

Expand All @@ -294,7 +290,11 @@ def check_uncertainty_data(data, filename):
]
)

if not exc["minimum"] <= exc["loc"] <= exc["maximum"]:
if (
not exc.get("minimum", 0)
<= exc.get("loc", 0)
<= exc.get("maximum", 0)
):
rows.append(
[
dataset["name"][:30],
Expand Down Expand Up @@ -876,7 +876,7 @@ def prepare_inventory(self) -> None:
# Remove uncertainty data
if not self.keep_uncertainty_data:
print("Remove uncertainty data.")
self.database = remove_uncertainty(self.database)
self.import_db.data = remove_uncertainty(self.import_db.data)
else:
check_uncertainty_data(self.import_db.data, filename=Path(self.path).stem)

Expand Down
25 changes: 12 additions & 13 deletions premise/new_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,8 @@ def __init__(
use_cached_database: bool = True,
external_scenarios: list = None,
quiet=False,
keep_uncertainty_data=False,
keep_imports_uncertainty=False,
keep_source_db_uncertainty=False,
gains_scenario="CLE",
use_absolute_efficiency=False,
biosphere_name: str = "biosphere3",
Expand All @@ -523,7 +524,8 @@ def __init__(
self.system_model = check_system_model(system_model)
self.system_model_args = system_args
self.use_absolute_efficiency = use_absolute_efficiency
self.keep_uncertainty_data = keep_uncertainty_data
self.keep_imports_uncertainty = keep_imports_uncertainty
self.keep_source_db_uncertainty = keep_source_db_uncertainty
self.biosphere_name = check_presence_biosphere_database(biosphere_name)

# if version is anything other than 3.8 or 3.9
Expand Down Expand Up @@ -628,7 +630,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_uncertainty_data is True else "wo_uncertainty"
"w_uncertainty"
if self.keep_source_db_uncertainty is True
else "wo_uncertainty"
)

file_name = (
Expand Down Expand Up @@ -661,7 +665,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_uncertainty_data is True else "wo_uncertainty"
"w_uncertainty"
if self.keep_imports_uncertainty is True
else "wo_uncertainty"
)

file_name = (
Expand Down Expand Up @@ -694,7 +700,7 @@ def __clean_database(self) -> List[dict]:
"""
return DatabaseCleaner(
self.source, self.source_type, self.source_file_path, self.version
).prepare_datasets(self.keep_uncertainty_data)
).prepare_datasets(self.keep_source_db_uncertainty)

def __import_inventories(self) -> List[dict]:
"""
Expand Down Expand Up @@ -806,7 +812,7 @@ def __import_inventories(self) -> List[dict]:
version_out=self.version,
path=filepath[0],
system_model=self.system_model,
keep_uncertainty_data=self.keep_uncertainty_data,
keep_uncertainty_data=self.keep_imports_uncertainty,
)
datasets = inventory.merge_inventory()
data.extend(datasets)
Expand Down Expand Up @@ -999,7 +1005,6 @@ def write_superstructure_db_to_brightway(
scenario=scenario,
db_name=name,
original_database=self.database,
keep_uncertainty_data=self.keep_uncertainty_data,
biosphere_name=self.biosphere_name,
)

Expand All @@ -1023,7 +1028,6 @@ def write_superstructure_db_to_brightway(
scenario=tmp_scenario,
name="database",
original_database=self.database,
keep_uncertainty_data=self.keep_uncertainty_data,
biosphere_name=self.biosphere_name,
)

Expand Down Expand Up @@ -1091,7 +1095,6 @@ def write_db_to_brightway(self, name: [str, List[str]] = None):
scenario=scenario,
db_name=name[s],
original_database=self.database,
keep_uncertainty_data=self.keep_uncertainty_data,
biosphere_name=self.biosphere_name,
)
write_brightway_database(
Expand Down Expand Up @@ -1166,7 +1169,6 @@ def scenario_name(scenario):
scenario=scenario,
db_name="database",
original_database=self.database,
keep_uncertainty_data=self.keep_uncertainty_data,
biosphere_name=self.biosphere_name,
)
Export(scenario, filepath[s], self.version).export_db_to_matrices()
Expand Down Expand Up @@ -1207,7 +1209,6 @@ def write_db_to_simapro(self, filepath: str = None):
scenario=scenario,
db_name="database",
original_database=self.database,
keep_uncertainty_data=self.keep_uncertainty_data,
biosphere_name=self.biosphere_name,
)
export = Export(scenario, filepath, self.version)
Expand Down Expand Up @@ -1254,7 +1255,6 @@ def write_db_to_olca(self, filepath: str = None):
scenario=scenario,
db_name="database",
original_database=self.database,
keep_uncertainty_data=self.keep_uncertainty_data,
biosphere_name=self.biosphere_name,
)
Export(scenario, filepath, self.version).export_db_to_simapro(
Expand Down Expand Up @@ -1295,7 +1295,6 @@ def write_datapackage(
scenario=scenario,
db_name=name,
original_database=self.database,
keep_uncertainty_data=self.keep_uncertainty_data,
biosphere_name=self.biosphere_name,
)

Expand Down
Loading

0 comments on commit 21fdef2

Please sign in to comment.