Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ Convert OSeMOSYS results to IAMC format
It is currently necessary to install the OpenEntrance dependency as an editable installation.
See [issue](https://github.com/openENTRANCE/openentrance/issues/202)

pip install git+https://github.com/osemosys/osemosys2iamc@main#egg=osemosys2iamc
pip install -e git+https://github.com/openENTRANCE/openentrance.git@main#egg=openentrance
pip install git+https://github.com/osemosys/osemosys2iamc@main#egg=osemosys2iamc

## Run the package

Expand Down
23 changes: 16 additions & 7 deletions src/osemosys2iamc/resultify.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@

"""
import functools
from multiprocessing.sharedctypes import Value
from sqlite3 import DatabaseError
import pandas as pd
import pyam
from openentrance import iso_mapping
Expand All @@ -32,6 +30,7 @@ def read_file(filename) -> pd.DataFrame:

return df


def filter_regex(df: pd.DataFrame, patterns: List[str], column: str) -> pd.DataFrame:
"""Generic filtering of rows based on columns that match a list of patterns

Expand All @@ -41,6 +40,7 @@ def filter_regex(df: pd.DataFrame, patterns: List[str], column: str) -> pd.DataF
masks = [df[column].str.match(p) for p in patterns]
return pd.concat([df[mask] for mask in masks])


def filter_fuels(df: pd.DataFrame, fuels: List[str]) -> pd.DataFrame:
"""Returns rows which match list of regex patterns in ``technologies``

Expand All @@ -53,6 +53,7 @@ def filter_fuels(df: pd.DataFrame, fuels: List[str]) -> pd.DataFrame:
"""
return filter_regex(df, fuels, 'FUEL')


def filter_technologies(df: pd.DataFrame, technologies: List[str]) -> pd.DataFrame:
"""Returns rows which match list of regex patterns in ``technologies``

Expand All @@ -65,16 +66,19 @@ def filter_technologies(df: pd.DataFrame, technologies: List[str]) -> pd.DataFra
"""
return filter_regex(df, technologies, 'TECHNOLOGY')


def filter_technology_fuel(df: pd.DataFrame, technologies: List, fuels: List) -> pd.DataFrame:
"""Return rows which match ``technologies`` and ``fuels``
"""
df = filter_technologies(df, technologies)
df = filter_fuels(df, fuels)

df = df.groupby(by=['REGION','YEAR'], as_index=False)["VALUE"].sum()
df = df.groupby(by=['REGION', 'YEAR'], as_index=False)["VALUE"].sum()
return df[df.VALUE != 0]

def filter_emission_tech(df: pd.DataFrame, emission: List[str], technologies: Optional[List[str]]=None) -> pd.DataFrame:

def filter_emission_tech(df: pd.DataFrame, emission: List[str],
technologies: Optional[List[str]] = None) -> pd.DataFrame:
"""Return annual emissions or captured emissions by one or several technologies.

Parameters
Expand All @@ -90,14 +94,19 @@ def filter_emission_tech(df: pd.DataFrame, emission: List[str], technologies: Op
pandas.DataFrame
"""

df['REGION'] = df['TECHNOLOGY'].str[:2]
# Try to extract region from technology
try:
df['REGION'] = df['TECHNOLOGY'].str[:2]
except KeyError:
# No Technology column
pass
df = filter_regex(df, emission, 'EMISSION')

if technologies:
# Create a list of masks, one for each row that matches the pattern listed in ``tech``
# Create a list of masks, one for each row that matches the pattern listed in ``tech``
df = filter_technologies(df, technologies)

df = df.groupby(by=['REGION','YEAR'], as_index=False)["VALUE"].sum()
df = df.groupby(by=['REGION', 'YEAR'], as_index=False)["VALUE"].sum()
return df[df.VALUE != 0]

def filter_capacity(df: pd.DataFrame, technologies: List[str]) -> pd.DataFrame:
Expand Down
6 changes: 6 additions & 0 deletions tests/fixtures/carbonprice.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
VALUE,constraint,REGION,EMISSION,YEAR
-0.011043939576413982,Constr E8_AnnualEmissionsLimit,REGION1,CO2,2036
-0.013899709405226217,Constr E8_AnnualEmissionsLimit,REGION1,CO2,2037
-0.014572746545426708,Constr E8_AnnualEmissionsLimit,REGION1,CO2,2038
-0.011609757344217923,Constr E8_AnnualEmissionsLimit,REGION1,CO2,2039
0.0,Constr E8_AnnualEmissionsLimit,REGION1,UKWO,2060
31 changes: 31 additions & 0 deletions tests/test_resultify.py
Original file line number Diff line number Diff line change
Expand Up @@ -553,3 +553,34 @@ def test_price_bm(self):
print(expected)

pd.testing.assert_frame_equal(actual, expected)

class TestCarbonPrice:

def test_carbon_price(self):
"""
VALUE,constraint,REGION,EMISSION,YEAR
-0.011043939576413982,Constr E8_AnnualEmissionsLimit,REGION1,CO2,2036
-0.013899709405226217,Constr E8_AnnualEmissionsLimit,REGION1,CO2,2037
-0.014572746545426708,Constr E8_AnnualEmissionsLimit,REGION1,CO2,2038
-0.011609757344217923,Constr E8_AnnualEmissionsLimit,REGION1,CO2,2039
0.0,Constr E8_AnnualEmissionsLimit,REGION1,UKWO,2060
"""

filepath = os.path.join("tests","fixtures","carbonprice.csv")
input_data = pd.read_csv(filepath)
emission = ['CO2']
actual = filter_emission_tech(input_data, emission)

data = [
['REGION1',2036,-0.011043939576413982],
['REGION1',2037,-0.013899709405226217],
['REGION1',2038,-0.014572746545426708],
['REGION1',2039,-0.011609757344217923],
]

expected = pd.DataFrame(data=data, columns=["REGION", "YEAR", "VALUE"])

print(actual)
print(expected)

pd.testing.assert_frame_equal(actual, expected)