Skip to content
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

Get network elements properties as data frame #909

Merged
merged 5 commits into from
Dec 17, 2024
Merged
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
3 changes: 2 additions & 1 deletion cpp/pypowsybl-cpp/bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,8 @@ PYBIND11_MODULE(_pypowsybl, m) {
.value("AREA", element_type::AREA)
.value("AREA_VOLTAGE_LEVELS", element_type::AREA_VOLTAGE_LEVELS)
.value("AREA_BOUNDARIES", element_type::AREA_BOUNDARIES)
.value("INTERNAL_CONNECTION", element_type::INTERNAL_CONNECTION);
.value("INTERNAL_CONNECTION", element_type::INTERNAL_CONNECTION)
.value("PROPERTIES", element_type::PROPERTIES);

py::enum_<filter_attributes_type>(m, "FilterAttributesType")
.value("ALL_ATTRIBUTES", filter_attributes_type::ALL_ATTRIBUTES)
Expand Down
3 changes: 2 additions & 1 deletion cpp/pypowsybl-java/powsybl-api.h
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,8 @@ typedef enum {
AREA,
AREA_VOLTAGE_LEVELS,
AREA_BOUNDARIES,
INTERNAL_CONNECTION
INTERNAL_CONNECTION,
PROPERTIES
} element_type;

typedef enum {
Expand Down
1 change: 1 addition & 0 deletions docs/reference/network.rst
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ Network elements can be modified using dataframes:
Network.update_switches
Network.update_voltage_levels
Network.update_vsc_converter_stations
Network.get_elements_properties
Network.add_elements_properties
Network.remove_elements_properties
Network.add_aliases
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,6 @@ public enum DataframeElementType {
AREA,
AREA_VOLTAGE_LEVELS,
AREA_BOUNDARIES,
INTERNAL_CONNECTION
INTERNAL_CONNECTION,
PROPERTIES
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ private static Map<DataframeElementType, NetworkDataframeMapper> createMappers()
mappers.put(DataframeElementType.INJECTION, injections());
mappers.put(DataframeElementType.BRANCH, branches());
mappers.put(DataframeElementType.TERMINAL, terminals());
mappers.put(DataframeElementType.PROPERTIES, properties());
return Collections.unmodifiableMap(mappers);
}

Expand Down Expand Up @@ -1333,6 +1334,20 @@ private static Stream<Pair<Identifiable<?>, String>> getAliasesData(Network netw
.map(alias -> Pair.of(identifiable, alias)));
}

private static NetworkDataframeMapper properties() {
return NetworkDataframeMapperBuilder.ofStream(NetworkDataframes::getPropertiesData)
.stringsIndex("id", pair -> pair.getLeft().getId())
.strings("key", Pair::getRight)
.strings("value", pair -> pair.getLeft().getProperty(pair.getRight()))
.build();
}

private static Stream<Pair<Identifiable<?>, String>> getPropertiesData(Network network) {
return network.getIdentifiables().stream()
.flatMap(identifiable -> identifiable.getPropertyNames().stream()
.map(prop -> Pair.of(identifiable, prop)));
}

private static NetworkDataframeMapper areaVoltageLevels() {
return NetworkDataframeMapperBuilder.ofStream(NetworkDataframes::areaVoltageLevelsData)
.stringsIndex("id", pair -> pair.getLeft().getId())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,8 @@ public enum ElementType {
AREA,
AREA_VOLTAGE_LEVELS,
AREA_BOUNDARIES,
INTERNAL_CONNECTION;
INTERNAL_CONNECTION,
PROPERTIES;

@CEnumValue
public native int getCValue();
Expand Down
2 changes: 2 additions & 0 deletions java/src/main/java/com/powsybl/python/commons/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ public static PyPowsyblApiHeader.ElementType convert(DataframeElementType type)
case AREA_VOLTAGE_LEVELS -> PyPowsyblApiHeader.ElementType.AREA_VOLTAGE_LEVELS;
case AREA_BOUNDARIES -> PyPowsyblApiHeader.ElementType.AREA_BOUNDARIES;
case INTERNAL_CONNECTION -> PyPowsyblApiHeader.ElementType.INTERNAL_CONNECTION;
case PROPERTIES -> PyPowsyblApiHeader.ElementType.PROPERTIES;
};
}

Expand Down Expand Up @@ -271,6 +272,7 @@ public static DataframeElementType convert(PyPowsyblApiHeader.ElementType type)
case AREA_VOLTAGE_LEVELS -> DataframeElementType.AREA_VOLTAGE_LEVELS;
case AREA_BOUNDARIES -> DataframeElementType.AREA_BOUNDARIES;
case INTERNAL_CONNECTION -> DataframeElementType.INTERNAL_CONNECTION;
case PROPERTIES -> DataframeElementType.PROPERTIES;
};
}

Expand Down
1 change: 1 addition & 0 deletions pypowsybl/_pypowsybl.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@ class ElementType:
AREA_VOLTAGE_LEVELS: ClassVar[ElementType] = ...
AREA_BOUNDARIES: ClassVar[ElementType] = ...
INTERNAL_CONNECTION: ClassVar[ElementType] = ...
PROPERTIES: ClassVar[ElementType] = ...
def __init__(self, arg0: int) -> None: ...
def __eq__(self, arg0: object) -> bool: ...
def __getstate__(self) -> int: ...
Expand Down
20 changes: 20 additions & 0 deletions pypowsybl/network/impl/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -5358,6 +5358,26 @@ def get_extension(self, extension_name: str) -> DataFrame:
warnings.warn("get_extension is deprecated, use get_extensions instead", DeprecationWarning)
return self.get_extensions(extension_name)

def get_elements_properties(self, all_attributes: bool = False, attributes: List[str] = None,
**kwargs: ArrayLike) -> DataFrame:
"""
Get a dataframe of properties of all network elements.

Args:

Returns:
A dataframe of properties

Notes:
The resulting dataframe, depending on the parameters, will include the following columns:

- **key**: property key
- **value**: property value

This dataframe is indexed on the network element ID.
"""
return self.get_elements(ElementType.PROPERTIES, all_attributes, attributes, **kwargs)

def add_elements_properties(self, df: DataFrame = None, **kwargs: ArrayLike) -> None:
"""
Add properties to network elements, provided as a :class:`~pandas.DataFrame` or as named arguments.
Expand Down
12 changes: 12 additions & 0 deletions tests/test_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -2150,6 +2150,18 @@ def test_properties():
assert "Network element \'notHere\' does not exist." in str(exc)


def test_get_properties():
network = pp.network.create_eurostag_tutorial_example1_network()
assert network.get_elements_properties().empty
network.add_elements_properties(id=network.id, key1='value1', key2='value2')
network.add_elements_properties(id='GEN', key3='value3')
expected = pd.DataFrame.from_records(index='id',
data=[{'id': 'sim1', 'key': 'key1', 'value': 'value1'},
{'id': 'sim1', 'key': 'key2', 'value': 'value2'},
{'id': 'GEN', 'key': 'key3', 'value': 'value3'}])
pd.testing.assert_frame_equal(network.get_elements_properties(), expected, check_dtype=False)


def test_pathlib_load_save(tmpdir):
bat_path = TEST_DIR.joinpath('battery.xiidm')
n_path = pp.network.load(bat_path)
Expand Down
Loading