Skip to content

Adding CongestionResult. #200

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
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
5 changes: 3 additions & 2 deletions pypsdm/io/utils.py
Original file line number Diff line number Diff line change
@@ -89,14 +89,15 @@ def to_date_time(zoned_date_time: str) -> datetime:


def csv_to_grpd_df(
file_name: str, simulation_data_path: str, delimiter: str | None = None
file_name: str, simulation_data_path: str, by: str, delimiter: str | None = None
) -> DataFrameGroupBy:
"""
Reads in a PSDM csv results file cleans it up and groups it by input_archive model.

Args:
file_name: name of the file to read
simulation_data_path: base directory of the result data
by: the column to group by
delimiter: the csv delimiter

Returns:
@@ -106,7 +107,7 @@ def csv_to_grpd_df(

if "uuid" in data.columns:
data = data.drop(columns=["uuid"])
return data.groupby(by="input_model")
return data.groupby(by=by)


def check_filter(filter_start: Optional[datetime], filter_end: Optional[datetime]):
16 changes: 13 additions & 3 deletions pypsdm/models/enums.py
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@
from typing import TYPE_CHECKING, Type, TypeVar

if TYPE_CHECKING:
from pypsdm.models.result.participant.dict import EntitiesResultDictMixin
from pypsdm.models.result.participant.dict import ResultDictMixin
from pypsdm.models.ts.base import TimeSeries


@@ -28,7 +28,10 @@ def get_csv_input_file_name(self):
return self.value + "_input.csv"

def get_csv_result_file_name(self):
return self.value + "_res.csv"
if self.value == "subgrid":
return "congestion_res.csv"
else:
return self.value + "_res.csv"

def get_type_file_name(self):
assert self.has_type() is True
@@ -42,6 +45,7 @@ def get_result_type(self) -> type[TimeSeries]:
from pypsdm.models.result.grid.connector import ConnectorCurrent
from pypsdm.models.result.grid.switch import SwitchResult
from pypsdm.models.result.grid.transformer import Transformer2WResult
from pypsdm.models.result.grid.congestions import CongestionResult
from pypsdm.models.ts.types import (
ComplexPower,
ComplexPowerWithSoc,
@@ -63,18 +67,21 @@ def get_result_type(self) -> type[TimeSeries]:
return ConnectorCurrent
case RawGridElementsEnum.SWITCH:
return SwitchResult
case RawGridElementsEnum.SUBGRID:
return CongestionResult
case _:
raise NotImplementedError(
f"Result type {self} not implemented yet!"
)
else:
raise ValueError(f"Entity type {self} not supported!")

def get_result_dict_type(self) -> Type["EntitiesResultDictMixin"]:
def get_result_dict_type(self) -> Type["ResultDictMixin"]:
from pypsdm.models.result.grid.line import LinesResult
from pypsdm.models.result.grid.node import NodesResult
from pypsdm.models.result.grid.switch import SwitchesResult
from pypsdm.models.result.grid.transformer import Transformers2WResult
from pypsdm.models.result.grid.congestions import CongestionsResult
from pypsdm.models.result.participant.dict import (
EmsResult,
EvcsResult,
@@ -97,6 +104,8 @@ def get_result_dict_type(self) -> Type["EntitiesResultDictMixin"]:
return Transformers2WResult
case RawGridElementsEnum.SWITCH:
return SwitchesResult
case RawGridElementsEnum.SUBGRID:
return CongestionsResult
case SystemParticipantsEnum.ELECTRIC_VEHICLE:
return EvsResult
case SystemParticipantsEnum.EV_CHARGING_STATION:
@@ -158,6 +167,7 @@ class RawGridElementsEnum(EntitiesEnum):
TRANSFROMER_3_W = "transformer_3_w"
SWITCH = "switch"
MEASUREMENT_UNIT = "measurement_unit"
SUBGRID = "subgrid"


class ThermalGridElementsEnum(EntitiesEnum):
4 changes: 4 additions & 0 deletions pypsdm/models/gwr.py
Original file line number Diff line number Diff line change
@@ -106,6 +106,10 @@ def transformers_2_w_res(self):
def switches_res(self):
return self.raw_grid_res.switches

@property
def congestions_res(self):
return self.results.raw_grid.congestions

@property
def participants_res(self):
return self.results.participants
8 changes: 4 additions & 4 deletions pypsdm/models/input/container/mixins.py
Original file line number Diff line number Diff line change
@@ -17,7 +17,7 @@

if TYPE_CHECKING:
from pypsdm.models.input.container.grid import GridContainer
from pypsdm.models.result.participant.dict import EntitiesResultDictMixin
from pypsdm.models.result.participant.dict import ResultDictMixin


class ContainerMixin(ABC):
@@ -118,8 +118,8 @@ def entities_from_csv(
delimiter: str | None = None,
filter_start: datetime | None = None,
filter_end: datetime | None = None,
) -> dict[EntitiesEnum, EntitiesResultDictMixin]:
from pypsdm.models.result.participant.dict import EntitiesResultDictMixin
) -> dict[EntitiesEnum, ResultDictMixin]:
from pypsdm.models.result.participant.dict import ResultDictMixin

res_files = [
f for f in os.listdir(simulation_data_path) if f.endswith("_res.csv")
@@ -135,7 +135,7 @@ def entities_from_csv(
with concurrent.futures.ProcessPoolExecutor() as executor:
# warning: Breakpoints in the underlying method might not work when started from ipynb
pa_from_csv_for_participant = partial(
EntitiesResultDictMixin.from_csv_for_entity,
ResultDictMixin.from_csv_for_entity,
simulation_data_path,
simulation_end,
grid_container,
2 changes: 2 additions & 0 deletions pypsdm/models/input/container/raw_grid.py
Original file line number Diff line number Diff line change
@@ -77,6 +77,8 @@ def get_with_enum(self, enum: RawGridElementsEnum):
return self.transformers_2_w
case RawGridElementsEnum.SWITCH:
return self.switches
case RawGridElementsEnum.SUBGRID:
return None
case _:
raise ValueError(f"Unknown enum {enum}")

4 changes: 4 additions & 0 deletions pypsdm/models/result/container/grid.py
Original file line number Diff line number Diff line change
@@ -40,6 +40,10 @@ def transformers_2w(self):
def switches(self):
return self.raw_grid.switches

@property
def congestions(self):
return self.raw_grid.congestions

@property
def ems(self):
return self.participants.ems
4 changes: 4 additions & 0 deletions pypsdm/models/result/container/raw_grid.py
Original file line number Diff line number Diff line change
@@ -9,6 +9,7 @@
from pypsdm.models.result.grid.node import NodesResult
from pypsdm.models.result.grid.switch import SwitchesResult
from pypsdm.models.result.grid.transformer import Transformers2WResult
from pypsdm.models.result.grid.congestions import CongestionsResult
from pypsdm.models.ts.base import EntityKey


@@ -18,6 +19,7 @@ class RawGridResultContainer(ResultContainerMixin):
lines: LinesResult
transformers_2w: Transformers2WResult
switches: SwitchesResult
congestions: CongestionsResult

def __init__(self, dct):
def get_or_empty(key: RawGridElementsEnum, dict_type):
@@ -36,6 +38,7 @@ def get_or_empty(key: RawGridElementsEnum, dict_type):
RawGridElementsEnum.TRANSFORMER_2_W, Transformers2WResult
)
self.switches = get_or_empty(RawGridElementsEnum.SWITCH, SwitchesResult)
self.congestions = get_or_empty(RawGridElementsEnum.SUBGRID, CongestionsResult)

def __len__(self):
return sum(len(v) for v in self.to_dict().values())
@@ -54,6 +57,7 @@ def to_dict(self, include_empty: bool = False) -> dict:
RawGridElementsEnum.LINE: self.lines,
RawGridElementsEnum.TRANSFORMER_2_W: self.transformers_2w,
RawGridElementsEnum.SWITCH: self.switches,
RawGridElementsEnum.SUBGRID: self.congestions,
}
if not include_empty:
res = {k: v for k, v in res.items() if v}
2 changes: 2 additions & 0 deletions pypsdm/models/result/grid/__init__.py
Original file line number Diff line number Diff line change
@@ -3,6 +3,7 @@
from .node import NodesResult
from .switch import SwitchesResult, SwitchResult
from .transformer import Transformer2WResult, Transformers2WResult
from .congestions import CongestionResult, CongestionsResult

__all__ = [
"ConnectorCurrent",
@@ -13,4 +14,5 @@
"LinesResult",
"SwitchResult",
"SwitchesResult",
"CongestionsResult",
]
90 changes: 90 additions & 0 deletions pypsdm/models/result/grid/congestions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
from dataclasses import dataclass
from datetime import datetime

from pandas import DataFrame

from pypsdm.models.enums import RawGridElementsEnum
from pypsdm.models.result.participant.dict import SubgridResultDictMixin
from pypsdm.models.ts.base import (
SubGridKey,
TimeSeries,
TimeSeriesDict,
TimeSeriesDictMixin,
)


@dataclass
class CongestionResult(TimeSeries):
def __init__(self, data: DataFrame, end: datetime | None = None):
super().__init__(data, end)

def __eq__(self, other: object) -> bool:
return super().__eq__(other)

def __add__(self, _):
return NotImplemented

@property
def vMin(self) -> float:
return self.data["vMin"].drop_duplicates()[0]

@property
def vMax(self) -> float:
return self.data["vMax"].drop_duplicates()[0]

@property
def subnet(self) -> int:
return self.data["subgrid"].drop_duplicates()[0]

@property
def voltage(self):
return self.data["voltage"]

@property
def line(self):
return self.data["line"]

@property
def transformer(self):
return self.data["transformer"]

@staticmethod
def attributes() -> list[str]:
return ["vMin", "vMax", "subgrid", "voltage", "line", "transformer"]


class CongestionsResult(
TimeSeriesDict[SubGridKey, CongestionResult],
TimeSeriesDictMixin,
SubgridResultDictMixin,
):
def __eq__(self, other: object) -> bool:
return super().__eq__(other)

@property
def vMin(self) -> DataFrame:
return self.attr_df("vMin")

@property
def vMax(self) -> DataFrame:
return self.attr_df("vMax")

@property
def subnet(self) -> DataFrame:
return self.attr_df("subgrid")

@property
def voltage(self) -> DataFrame:
return self.attr_df("voltage")

@property
def line(self) -> DataFrame:
return self.attr_df("line")

@property
def transformer(self) -> DataFrame:
return self.attr_df("transformer")

@classmethod
def entity_type(cls) -> RawGridElementsEnum:
return RawGridElementsEnum.SUBGRID
Loading