Skip to content

Commit

Permalink
Fix some issues related to mapping.
Browse files Browse the repository at this point in the history
  • Loading branch information
romainsacchi committed Oct 3, 2023
1 parent 31aa2c4 commit 05c2bc5
Show file tree
Hide file tree
Showing 22 changed files with 625 additions and 684 deletions.
2 changes: 1 addition & 1 deletion conda/meta.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ requirements:
run:
- numpy
- pandas
- bw2io==0.8.7
- bw2io >=0.8.7, != 0.8.8
- bw2data
- wurst
- xarray
Expand Down
512 changes: 112 additions & 400 deletions dev/Untitled.ipynb

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion docs/extract.rst
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,9 @@ indicate the database name in `source_db` and its version in `source_version`:
],
source_db="ecoinvent 3.7 cutoff", # <-- this is NEW.
source_version="3.7.1", # <-- this is NEW
key='xxxxxxxxxxxxxxxxxxxxxxxxx'
key='xxxxxxxxxxxxxxxxxxxxxxxxx',
use_multiprocessing=True, # True by default, set to False if multiprocessing is causing troubles
keep_uncertainty_data=False # False by default, set to True if you want to keep ecoinvent's uncertainty data
)
Note that a cache of the database will be created the first time and
Expand Down
4 changes: 1 addition & 3 deletions docs/introduction.rst
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,7 @@ Provided a decryption key (ask the maintainers_), the following IAM scenarios ar

CarbonBrief_ wrote a good article explaining the meaning of the SSP/RCP system.

Additionally, we provided a summary of the main characteristics of each scenario in a spreadsheet_.

.. _spreadsheet: https://github.com/polca/premise/raw/master/docs/scenario_report_2023-05-02.xlsx
Additionally, we provided a summary of the main characteristics of each scenario `here <https://premisedash-6f5a0259c487.herokuapp.com/>`_.


.. _CarbonBrief: https://www.carbonbrief.org/explainer-how-shared-socioeconomic-pathways-explore-future-climate-change
Expand Down
9 changes: 6 additions & 3 deletions docs/load.rst
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,12 @@ This is done as follows:
ndb.write_db_to_datapackage()
This creates a zip file that contains the all the data necessary for
other users to replicate teh databases, provided they have access
other users to replicate the databases, provided they have access
to the source database locally.

See the library ``unfold`` for more information on data packages
for sharing LCA databases.
See the library <``unfold`` https://github.com/polca/unfold/tree/main>_ for more information on data packages
for sharing LCA databases. ``unfold`` can read these data packages and create
brightway2 databases (or superstructure databases) from them.
``unfold`` can also fold premise databases registered in your brightway2 project
into data packages, to be shared with and recreated by others.

2 changes: 1 addition & 1 deletion examples/examples.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -1030,7 +1030,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.12"
"version": "3.10.12"
}
},
"nbformat": 4,
Expand Down
2 changes: 1 addition & 1 deletion premise/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
__all__ = ("NewDatabase", "clear_cache", "get_regions_definition")
__version__ = (1, 7, 0)
__version__ = (1, 7, 1)

from pathlib import Path

Expand Down
26 changes: 21 additions & 5 deletions premise/activity_maps.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
mapping between ``premise`` and ``ecoinvent`` terminology.
"""

import sys
from collections import defaultdict
from pathlib import Path
from typing import List, Union
Expand All @@ -23,11 +24,12 @@
)


def get_mapping(filepath: Path, var: str) -> dict:
def get_mapping(filepath: Path, var: str, model: str = None) -> dict:
"""
Loa a YAML file and return a dictionary given a variable.
:param filepath: YAML file path
:param var: variable to return the dictionary for.
:param model: if provided, only return the dictionary for this model.
:return: a dictionary
"""

Expand All @@ -37,7 +39,11 @@ def get_mapping(filepath: Path, var: str) -> dict:
mapping = {}
for key, val in techs.items():
if var in val:
mapping[key] = val[var]
if model is None:
mapping[key] = val[var]
else:
if model in val.get("iam_aliases", {}):
mapping[key] = val[var]

return mapping

Expand Down Expand Up @@ -114,12 +120,13 @@ class InventorySet:
These functions return the result of applying :func:`act_fltr` to the filter dictionaries.
"""

def __init__(self, database: List[dict], version: str = None) -> None:
def __init__(self, database: List[dict], version: str = None, model: str = None) -> None:
self.database = database
self.version = version
self.model = model

self.powerplant_filters = get_mapping(
filepath=POWERPLANT_TECHS, var="ecoinvent_aliases"
filepath=POWERPLANT_TECHS, var="ecoinvent_aliases", model=self.model
)

self.powerplant_fuels_filters = get_mapping(
Expand Down Expand Up @@ -253,4 +260,13 @@ def generate_sets_from_filters(self, filtr: dict, database=None) -> dict:
tech: act_fltr(database, fltr.get("fltr"), fltr.get("mask"))
for tech, fltr in filtr.items()
}
return {tech: {act["name"] for act in actlst} for tech, actlst in techs.items()}

mapping = {tech: {act["name"] for act in actlst} for tech, actlst in techs.items()}

# check if all keys have values
# if not, print warning
for key, val in mapping.items():
if not val:
print(f"Warning: No activities found for {key} -- revise mapping.")

return mapping
Binary file modified premise/data/additional_inventories/lci-biofuels.xlsx
Binary file not shown.
8 changes: 7 additions & 1 deletion premise/data/additional_inventories/migration_map.csv
Original file line number Diff line number Diff line change
Expand Up @@ -616,4 +616,10 @@ from;to;name_from;ref_prod_from;location_from;name_to;ref_prod_to;location_to
36;39;transmission network construction, electricity, high voltage;transmission network, long-distance;CH;transmission network construction, electricity, high voltage;transmission network, electricity, high voltage;CH
37;39;transmission network construction, electricity, high voltage;transmission network, long-distance;CH;transmission network construction, electricity, high voltage;transmission network, electricity, high voltage;CH
38;39;transmission network construction, electricity, high voltage;transmission network, long-distance;CH;transmission network construction, electricity, high voltage;transmission network, electricity, high voltage;CH
39;38;market for battery cell, Li-ion, LFP;battery cell, Li-ion, LFP;GLO;market for battery cell, Li-ion;battery cell, Li-ion;GLO
39;38;market for battery cell, Li-ion, LFP;battery cell, Li-ion, LFP;GLO;market for battery cell, Li-ion;battery cell, Li-ion;GLO
39;37;market for battery cell, Li-ion, LFP;battery cell, Li-ion, LFP;GLO;market for battery cell, Li-ion;battery cell, Li-ion;GLO
39;37;heat pump production, heat and power co-generation unit, 160kW electrical;heat pump, heat and power co-generation unit, 160kW electrical;RoW;heat pump production, for heat and power co-generation unit, 160kW electrical;heat pump, for heat and power co-generation unit, 160kW electrical;RoW
39;37;heat pump production, heat and power co-generation unit, 160kW electrical;heat pump, heat and power co-generation unit, 160kW electrical;RER;heat pump production, for heat and power co-generation unit, 160kW electrical;heat pump, for heat and power co-generation unit, 160kW electrical;RER
38;37;heat pump production, heat and power co-generation unit, 160kW electrical;heat pump, heat and power co-generation unit, 160kW electrical;RoW;heat pump production, for heat and power co-generation unit, 160kW electrical;heat pump, for heat and power co-generation unit, 160kW electrical;RoW
38;37;heat pump production, heat and power co-generation unit, 160kW electrical;heat pump, heat and power co-generation unit, 160kW electrical;RER;heat pump production, for heat and power co-generation unit, 160kW electrical;heat pump, for heat and power co-generation unit, 160kW electrical;RER
39;37;market for battery cell, Li-ion, NMC811;battery cell, Li-ion, NMC811;GLO;market for battery cell, Li-ion;battery cell, Li-ion;GLO
17 changes: 13 additions & 4 deletions premise/data_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import csv
import os
from functools import lru_cache
from io import StringIO
from io import BytesIO, StringIO
from itertools import chain
from pathlib import Path
from typing import Dict, List, Union
Expand Down Expand Up @@ -717,6 +717,13 @@ def __get_iam_data(

dataframe = dataframe.rename(columns={"variable": "variables"})

# make a list of headers that are integer

headers = [x for x in dataframe.columns if isinstance(x, int)]

# convert the values in these columns to numeric
dataframe[headers] = dataframe[headers].apply(pd.to_numeric, errors="coerce")

array = (
dataframe.melt(
id_vars=["region", "variables", "unit"],
Expand Down Expand Up @@ -1087,9 +1094,11 @@ def get_external_data(self, datapackages):
data[i] = {}

resource = dp.get_resource("scenario_data")
scenario_data = resource.read()
scenario_headers = resource.headers
df = pd.DataFrame(scenario_data, columns=scenario_headers)
# getting scenario data in binary format
scenario_data = resource.raw_read()
df = pd.read_csv(BytesIO(scenario_data), encoding="latin1")
# set headers from first row
df.columns = resource.headers

resource = dp.get_resource("config")
config_file = yaml.safe_load(resource.raw_read())
Expand Down
Loading

0 comments on commit 05c2bc5

Please sign in to comment.