Skip to content

Commit

Permalink
Added function for calculated weighted average of timesliced dual
Browse files Browse the repository at this point in the history
  • Loading branch information
willu47 committed Dec 18, 2024
1 parent 7014194 commit 13ffe6c
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/osemosys2iamc/resultify.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,21 @@ def read_file(path: str, osemosys_param: str, region_name_option: str) -> pd.Dat
return df


def calculate_timeslice_dual(
input_data: pd.DataFrame, fuels: list[str], yearsplit: pd.DataFrame
):
"""Calculate the weighted average dual value"""
filtered_data = filter_fuels(input_data, fuels).drop(columns="FUEL")

index = ["REGION", "TIMESLICE", "YEAR"]

weighted = filtered_data.set_index(index) * yearsplit.set_index(
["TIMESLICE", "YEAR"]
)

return weighted.groupby(by=["REGION", "YEAR"]).sum().reset_index()


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 Down
25 changes: 25 additions & 0 deletions tests/fixtures/dual/dual_values_EBa11.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
REGION,FUEL,TIMESLICE,YEAR,VALUE
REGION1,ATE2,S01B1,2015,1
REGION1,ATE2,S01B1,2016,1
REGION1,ATE2,S01B1,2017,1
REGION1,ATE2,S01B1,2018,1
REGION1,BEE2,S01B1,2015,1
REGION1,BEE2,S01B1,2016,1
REGION1,BEE2,S01B1,2017,1
REGION1,BEE2,S01B1,2018,1
REGION1,ATE2,S01B2,2015,1
REGION1,ATE2,S01B2,2016,1
REGION1,ATE2,S01B2,2017,1
REGION1,ATE2,S01B2,2018,1
REGION1,BEE2,S01B2,2015,1
REGION1,BEE2,S01B2,2016,1
REGION1,BEE2,S01B2,2017,1
REGION1,BEE2,S01B2,2018,1
REGION1,ATE2,S01B3,2015,1
REGION1,ATE2,S01B3,2016,1
REGION1,ATE2,S01B3,2017,1
REGION1,ATE2,S01B3,2018,1
REGION1,BEE2,S01B3,2015,1
REGION1,BEE2,S01B3,2016,1
REGION1,BEE2,S01B3,2017,1
REGION1,BEE2,S01B3,2018,1
13 changes: 13 additions & 0 deletions tests/fixtures/dual/yearsplit.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
TIMESLICE,YEAR,VALUE
S01B1,2015,0.1
S01B2,2015,0.4
S01B3,2015,0.5
S01B1,2016,0.1
S01B2,2016,0.4
S01B3,2016,0.5
S01B1,2017,0.1
S01B2,2017,0.4
S01B3,2017,0.5
S01B1,2018,0.1
S01B2,2018,0.4
S01B3,2018,0.5
28 changes: 28 additions & 0 deletions tests/test_resultify.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
calculate_trade,
read_file,
iso_to_country,
calculate_timeslice_dual,
)


Expand Down Expand Up @@ -537,6 +538,33 @@ def test_filter_inst_capacity_wi_offshore(self):
pd.testing.assert_frame_equal(actual, expected)


class TestDualPrice:
def test_dual_variable_cost(self):
folderpath = os.path.join("tests", "fixtures", "dual")
input_data = read_file(folderpath, "dual_values_EBa11", "iso2_start")
fuels = ["^.{2}(E2)"]
yearsplit = read_file(folderpath, "yearsplit", "iso2_start")
actual = calculate_timeslice_dual(input_data, fuels, yearsplit)

data = [
["Austria", 2015, 1.0],
["Austria", 2016, 1.0],
["Austria", 2017, 1.0],
["Austria", 2018, 1.0],
["Belgium", 2015, 1.0],
["Belgium", 2016, 1.0],
["Belgium", 2017, 1.0],
["Belgium", 2018, 1.0],
]

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

print(actual)
print(expected)

pd.testing.assert_frame_equal(actual, expected)


class TestPrice:
def test_price_bm(self):
folderpath = os.path.join("tests", "fixtures")
Expand Down

0 comments on commit 13ffe6c

Please sign in to comment.