Skip to content

Commit 13ffe6c

Browse files
committed
Added function for calculated weighted average of timesliced dual
1 parent 7014194 commit 13ffe6c

File tree

4 files changed

+81
-0
lines changed

4 files changed

+81
-0
lines changed

src/osemosys2iamc/resultify.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,21 @@ def read_file(path: str, osemosys_param: str, region_name_option: str) -> pd.Dat
163163
return df
164164

165165

166+
def calculate_timeslice_dual(
167+
input_data: pd.DataFrame, fuels: list[str], yearsplit: pd.DataFrame
168+
):
169+
"""Calculate the weighted average dual value"""
170+
filtered_data = filter_fuels(input_data, fuels).drop(columns="FUEL")
171+
172+
index = ["REGION", "TIMESLICE", "YEAR"]
173+
174+
weighted = filtered_data.set_index(index) * yearsplit.set_index(
175+
["TIMESLICE", "YEAR"]
176+
)
177+
178+
return weighted.groupby(by=["REGION", "YEAR"]).sum().reset_index()
179+
180+
166181
def filter_regex(df: pd.DataFrame, patterns: List[str], column: str) -> pd.DataFrame:
167182
"""Generic filtering of rows based on columns that match a list of patterns
168183
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
REGION,FUEL,TIMESLICE,YEAR,VALUE
2+
REGION1,ATE2,S01B1,2015,1
3+
REGION1,ATE2,S01B1,2016,1
4+
REGION1,ATE2,S01B1,2017,1
5+
REGION1,ATE2,S01B1,2018,1
6+
REGION1,BEE2,S01B1,2015,1
7+
REGION1,BEE2,S01B1,2016,1
8+
REGION1,BEE2,S01B1,2017,1
9+
REGION1,BEE2,S01B1,2018,1
10+
REGION1,ATE2,S01B2,2015,1
11+
REGION1,ATE2,S01B2,2016,1
12+
REGION1,ATE2,S01B2,2017,1
13+
REGION1,ATE2,S01B2,2018,1
14+
REGION1,BEE2,S01B2,2015,1
15+
REGION1,BEE2,S01B2,2016,1
16+
REGION1,BEE2,S01B2,2017,1
17+
REGION1,BEE2,S01B2,2018,1
18+
REGION1,ATE2,S01B3,2015,1
19+
REGION1,ATE2,S01B3,2016,1
20+
REGION1,ATE2,S01B3,2017,1
21+
REGION1,ATE2,S01B3,2018,1
22+
REGION1,BEE2,S01B3,2015,1
23+
REGION1,BEE2,S01B3,2016,1
24+
REGION1,BEE2,S01B3,2017,1
25+
REGION1,BEE2,S01B3,2018,1

tests/fixtures/dual/yearsplit.csv

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
TIMESLICE,YEAR,VALUE
2+
S01B1,2015,0.1
3+
S01B2,2015,0.4
4+
S01B3,2015,0.5
5+
S01B1,2016,0.1
6+
S01B2,2016,0.4
7+
S01B3,2016,0.5
8+
S01B1,2017,0.1
9+
S01B2,2017,0.4
10+
S01B3,2017,0.5
11+
S01B1,2018,0.1
12+
S01B2,2018,0.4
13+
S01B3,2018,0.5

tests/test_resultify.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
calculate_trade,
1111
read_file,
1212
iso_to_country,
13+
calculate_timeslice_dual,
1314
)
1415

1516

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

539540

541+
class TestDualPrice:
542+
def test_dual_variable_cost(self):
543+
folderpath = os.path.join("tests", "fixtures", "dual")
544+
input_data = read_file(folderpath, "dual_values_EBa11", "iso2_start")
545+
fuels = ["^.{2}(E2)"]
546+
yearsplit = read_file(folderpath, "yearsplit", "iso2_start")
547+
actual = calculate_timeslice_dual(input_data, fuels, yearsplit)
548+
549+
data = [
550+
["Austria", 2015, 1.0],
551+
["Austria", 2016, 1.0],
552+
["Austria", 2017, 1.0],
553+
["Austria", 2018, 1.0],
554+
["Belgium", 2015, 1.0],
555+
["Belgium", 2016, 1.0],
556+
["Belgium", 2017, 1.0],
557+
["Belgium", 2018, 1.0],
558+
]
559+
560+
expected = pd.DataFrame(data=data, columns=["REGION", "YEAR", "VALUE"])
561+
562+
print(actual)
563+
print(expected)
564+
565+
pd.testing.assert_frame_equal(actual, expected)
566+
567+
540568
class TestPrice:
541569
def test_price_bm(self):
542570
folderpath = os.path.join("tests", "fixtures")

0 commit comments

Comments
 (0)