Skip to content
Draft
Show file tree
Hide file tree
Changes from 3 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
36 changes: 35 additions & 1 deletion src/power_grid_model_io/functions/_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import numpy as np
import structlog
from power_grid_model import WindingType
from power_grid_model import MeasuredTerminalType

T = TypeVar("T")

Expand All @@ -24,7 +25,30 @@
"ZN": WindingType.zigzag_n,
}


MEASURED_TERMINAL_TYPE_MAP = {
"cable_from": MeasuredTerminalType.branch_from,
"cable_to": MeasuredTerminalType.branch_to,
"line_from": MeasuredTerminalType.branch_from,
"line_to": MeasuredTerminalType.branch_to,
"reactance_coil_from": MeasuredTerminalType.branch_from,
"reactance_coil_to": MeasuredTerminalType.branch_to,
"special_transformer_from": MeasuredTerminalType.branch_from,
"special_transformer_to": MeasuredTerminalType.branch_to,
"transformer_from": MeasuredTerminalType.branch_from,
"transformer_to": MeasuredTerminalType.branch_to,
"transformer_load": MeasuredTerminalType.branch_to,
"earthing_transformer": MeasuredTerminalType.branch_from,
"transformer3_1": MeasuredTerminalType.branch3_1,
"transformer3_2": MeasuredTerminalType.branch3_2,
"transformer3_3": MeasuredTerminalType.branch3_3,
"source": MeasuredTerminalType.source,
"shunt_capacitor": MeasuredTerminalType.shunt,
"shunt_reactor": MeasuredTerminalType.shunt,
"pv": MeasuredTerminalType.generator,
"wind_turbine": MeasuredTerminalType.generator,
"load": MeasuredTerminalType.load,
}

def has_value(value: Any) -> bool:
"""
Return True if the value is not None, NaN or empty string.
Expand Down Expand Up @@ -104,3 +128,13 @@ def both_zeros_to_nan(value: float, other_value: float) -> float:
_LOG.warning("0 replaced to nan")
return float("nan")
return value

def find_terminal_type(**kwargs) -> MeasuredTerminalType:
"""
Return the measured terminal type, based on the string representation
"""
for key, id in kwargs.items():
if id is not None:
return MEASURED_TERMINAL_TYPE_MAP[key]
_LOG.warning("No measured terminal type is found!")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what happens when key is not in MEASURED_TERMINAL_TYPE_MAP?

return float("nan")
2 changes: 1 addition & 1 deletion src/power_grid_model_io/utils/auto_id.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,4 @@ def __getitem__(self, idx: int) -> Any:
Returns:
The original item
"""
return self._items[idx]
return getattr(self._items[idx], None)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the purpose of this change? pytest fails this one and passes when reverted to the original. (It should also be supplied with a str for the name field)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i indeed think that this is not what you intended to do. as it is now, besides the missing str type, it will basically try to seek the attribute None in self._items[idx], e.g. self._items[idx].None

My guess is that you want to make it such that you return None if idx is not in self_items? in that case, you will want to use

Suggested change
return getattr(self._items[idx], None)
return self._items.get(idx)

which is shorthand for

        return self._items.get(idx, None)

(the None is the default argument)

Loading