Skip to content

Commit ea4ed26

Browse files
Fixing flex result handling
1 parent 7efbb19 commit ea4ed26

File tree

5 files changed

+63
-24
lines changed

5 files changed

+63
-24
lines changed

pypsdm/models/enums.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ def get_plot_name(self):
3939

4040
def get_result_type(self) -> type[TimeSeries]:
4141
# locally to avoid circular imports
42+
from pypsdm import FlexOption
4243
from pypsdm.models.result.grid.connector import ConnectorCurrent
4344
from pypsdm.models.result.grid.switch import SwitchResult
4445
from pypsdm.models.result.grid.transformer import Transformer2WResult
@@ -47,7 +48,6 @@ def get_result_type(self) -> type[TimeSeries]:
4748
ComplexPowerWithSoc,
4849
ComplexVoltage,
4950
)
50-
from pypsdm import FlexOption
5151

5252
if isinstance(self, SystemParticipantsEnum):
5353
if self.has_soc():

pypsdm/models/result/container/participants.py

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,14 @@ def __add__(self, other: "SystemParticipantsResultContainer"):
8383
def p(self) -> DataFrame:
8484
p_series = {
8585
participants.entity_type().value: participants.p_sum() # type: ignore
86-
for participants in self.to_list(include_flex=False)
86+
for participants in self.participants_to_list()
8787
}
8888
return pd.DataFrame(p_series).sort_index().ffill().fillna(0)
8989

9090
def q(self) -> DataFrame:
9191
q_series = {
9292
participants.entity_type().value: participants.q_sum() # type: ignore
93-
for participants in self.to_list(include_flex=False)
93+
for participants in self.participants_to_list()
9494
}
9595
return pd.DataFrame(q_series).sort_index().ffill().fillna(0)
9696

@@ -109,31 +109,31 @@ def get_participants(
109109
return self.to_dict().get(sp_type)
110110

111111
def find_participant_result(self, uuid: str):
112-
for participants_res in self.to_list(include_flex=False):
112+
for participants_res in self.to_list():
113113
if uuid in participants_res:
114114
return participants_res.get(uuid)
115115
return ValueError(f"No participant result with uuid: {uuid}")
116116

117117
def energies(self) -> dict[SystemParticipantsEnum, float]:
118118
return {
119119
sp_type: res.energy()
120-
for sp_type, res in self.to_dict(include_empty=False).items()
120+
for sp_type, res in self.participants_to_dict(include_empty=False).items()
121121
if sp_type != SystemParticipantsEnum.FLEX_OPTIONS
122122
}
123123

124124
def load_and_generation_energies(self) -> dict[EntitiesEnum, Tuple[float, float]]:
125125
return {
126126
sp_type: res.load_and_generation()
127-
for sp_type, res in self.to_dict(include_empty=False).items()
127+
for sp_type, res in self.participants_to_dict(include_empty=False).items()
128128
}
129129

130130
def sum(self) -> ComplexPower:
131131
participant_res = []
132-
for participant in self.to_list(include_em=False, include_flex=False):
132+
for participant in self.participants_to_list(include_em=False):
133133
participant_res.append(participant.sum())
134134
return ComplexPower.sum(participant_res)
135135

136-
def to_dict(
136+
def participants_to_dict(
137137
self, include_empty: bool = False
138138
) -> dict[SystemParticipantsEnum, ComplexPowerDict]:
139139
dct = {
@@ -146,20 +146,38 @@ def to_dict(
146146
SystemParticipantsEnum.EV_CHARGING_STATION: self.evcs,
147147
SystemParticipantsEnum.ELECTRIC_VEHICLE: self.evs,
148148
SystemParticipantsEnum.HEAT_PUMP: self.hps,
149-
SystemParticipantsEnum.FLEX_OPTIONS: self.flex,
150149
}
151150
if not include_empty:
152151
dct = {k: v for k, v in dct.items() if len(v) > 0}
153152
return dct
154153

155-
def to_list(
156-
self, include_em: bool = True, include_flex=True, include_empty=True
154+
def participants_to_list(
155+
self, include_em: bool = True, include_empty=True
157156
) -> list[ComplexPowerDict]: # type: ignore
157+
dct = self.participants_to_dict(include_empty)
158+
if not include_em:
159+
del dct[SystemParticipantsEnum.ENERGY_MANAGEMENT]
160+
return list(dct.values())
161+
162+
def to_dict(
163+
self, include_empty: bool = False
164+
) -> dict[SystemParticipantsEnum, TimeSeriesDict]:
165+
dct: dict[SystemParticipantsEnum, TimeSeriesDict] = self.participants_to_dict(
166+
include_empty=True
167+
)
168+
169+
dct[SystemParticipantsEnum.FLEX_OPTIONS] = self.flex
170+
171+
if not include_empty:
172+
dct = {k: v for k, v in dct.items() if len(v) > 0}
173+
return dct
174+
175+
def to_list(
176+
self, include_em: bool = True, include_empty=True
177+
) -> list[TimeSeriesDict]: # type: ignore
158178
dct = self.to_dict(include_empty)
159179
if not include_em:
160180
del dct[SystemParticipantsEnum.ENERGY_MANAGEMENT]
161-
if not include_flex:
162-
del dct[SystemParticipantsEnum.FLEX_OPTIONS]
163181
return list(dct.values())
164182

165183
def filter_by_date_time(self, time: Union[datetime, list[datetime]]):
@@ -199,7 +217,7 @@ def concat(
199217
)
200218

201219
def to_csv(self, path: str, delimiter: str = ",", mkdirs=False):
202-
for participant in self.to_list(include_empty=False, include_flex=True):
220+
for participant in self.to_list(include_empty=False):
203221
participant.to_csv(path, delimiter, mkdirs=mkdirs)
204222

205223
@classmethod

pypsdm/plots/results/connector.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ def ax_plot_transformer_s(
101101
resolution: Resolution | None = None,
102102
**kwargs,
103103
):
104-
if len(res.i_a_mag) == 0:
104+
if len(res.data) == 0:
105105
raise ValueError("Transformer current time series is empty. No data to plot")
106106

107107
rated_power = res.apparent_power(trafo_key, gwr, side).apply(lambda x: np.real(x))

pypsdm/plots/results/power.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ def plot_aggregated_load_and_generation(
298298
def plot_all_participants(
299299
participants: Union[SystemParticipantsResultContainer, list[ComplexPower]],
300300
title: str,
301-
hourly_mean: bool,
301+
hourly_mean: bool = False,
302302
stack=False,
303303
with_residual=False,
304304
resolution: Resolution | None = None,
@@ -347,9 +347,7 @@ def _get_complex_power_from_union(
347347
if isinstance(participants, list)
348348
else [
349349
participant.sum()
350-
for participant in participants.to_list(
351-
include_em=False, include_flex=False
352-
)
350+
for participant in participants.participants_to_list(include_em=False)
353351
if participant
354352
]
355353
)

tests/models/result/container/test_participants.py

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import pandas as pd
22

3+
from pypsdm import FlexOption
34
from pypsdm.models.enums import SystemParticipantsEnum
45
from pypsdm.models.result.container.participants import (
56
SystemParticipantsResultContainer,
@@ -27,11 +28,33 @@ def get_power_data(with_soc=False):
2728
return ComplexPower(data)
2829

2930

30-
def get_entities_dict(with_soc=False):
31+
def get_flex_data():
32+
data = pd.DataFrame(
33+
{
34+
TIME_COLUMN_NAME: [
35+
"2021-01-01",
36+
"2021-01-02",
37+
"2021-01-03",
38+
"2021-01-04",
39+
],
40+
"p_min": [0.0, -1.0, 1.0, 2.0],
41+
"p_max": [0.0, 0.0, 2.0, 3.0],
42+
"q_ref": [0.0, 1.0, 2.0, 3.0],
43+
},
44+
)
45+
return FlexOption(data)
46+
47+
48+
def get_entities_dict(key):
49+
if key == SystemParticipantsEnum.FLEX_OPTIONS:
50+
data = get_flex_data()
51+
else:
52+
data = get_power_data(with_soc=key.has_soc())
53+
3154
dct = {
32-
EntityKey("a"): get_power_data(with_soc),
33-
EntityKey("b"): get_power_data(with_soc),
34-
EntityKey("c"): get_power_data(with_soc),
55+
EntityKey("a"): data.copy(),
56+
EntityKey("b"): data.copy(),
57+
EntityKey("c"): data.copy(),
3558
}
3659
return dct
3760

@@ -40,7 +63,7 @@ def get_container() -> SystemParticipantsResultContainer:
4063
dcts = {}
4164
for key in SystemParticipantsResultContainer.entity_keys():
4265
dict_type = key.get_result_dict_type()
43-
dcts[key] = dict_type(get_entities_dict(key.has_soc()))
66+
dcts[key] = dict_type(get_entities_dict(key))
4467
return SystemParticipantsResultContainer(dcts)
4568

4669

0 commit comments

Comments
 (0)