Skip to content

Commit

Permalink
Adapt code to work with numpy 2.0.0 (#771)
Browse files Browse the repository at this point in the history
Signed-off-by: Etienne LESOT <[email protected]>
Co-authored-by: Geoffroy Jamgotchian <[email protected]>
  • Loading branch information
EtienneLt and geofjamg authored Jun 24, 2024
1 parent 778bd3f commit 242a91b
Show file tree
Hide file tree
Showing 18 changed files with 141 additions and 135 deletions.
2 changes: 1 addition & 1 deletion cpp/lib/pybind11
Submodule pybind11 updated 212 files
6 changes: 3 additions & 3 deletions docs/reference/network.rst
Original file line number Diff line number Diff line change
Expand Up @@ -247,9 +247,9 @@ I/O
get_import_parameters
get_export_formats
get_export_parameters
save
save_to_string
save_to_binary_buffer
Network.save
Network.save_to_string
Network.save_to_binary_buffer


Advanced network modifications
Expand Down
4 changes: 2 additions & 2 deletions docs/user_guide/flowdecomposition.rst
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ As we cannot set a PST on an interconnection, we set an equivalent null load cal
>>> network.get_phase_tap_changers()
tap low_tap high_tap step_count regulating regulation_mode regulation_value target_deadband regulating_bus_id
id
BLOAD 11 BLOAD 12 2 0 -16 16 33 False FIXED_TAP NaN NaN
BLOAD 11 BLOAD 12 2 0 -16 16 33 False FIXED_TAP NaN NaN

Neutral tap position
^^^^^^^^^^^^^^^^^^^^
Expand Down Expand Up @@ -170,7 +170,7 @@ Here are the results with non-neutral tap position.
>>> network.get_phase_tap_changers()
tap low_tap high_tap step_count regulating regulation_mode regulation_value target_deadband regulating_bus_id
id
BLOAD 11 BLOAD 12 2 1 -16 16 33 False FIXED_TAP NaN NaN
BLOAD 11 BLOAD 12 2 1 -16 16 33 False FIXED_TAP NaN NaN
>>> flow_decomposition = pp.flowdecomposition.create_decomposition().add_monitored_elements(['FGEN 11 BLOAD 11 1', 'FGEN 11 BLOAD 12 1'])
>>> flow_decomposition_dataframe = flow_decomposition.run(network)
>>> flow_decomposition_dataframe
Expand Down
1 change: 1 addition & 0 deletions docs/user_guide/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ relying as much as possible on practical examples.

network
network_visualization
per_unit
loadflow
security
sensitivity
Expand Down
4 changes: 2 additions & 2 deletions docs/user_guide/network.rst
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ of a generator to 700 MW:

>>> network.set_working_variant('Variant')
>>> network.update_generators(id='GEN', target_p=700)
>>> network.get_generators()['target_p']['GEN']
>>> network.get_generators()['target_p']['GEN'].item()
700.0

If you switch back to the initial variant, you will see that
Expand All @@ -272,7 +272,7 @@ its state has not changed, our generator still produces 607 MW:
.. doctest::

>>> network.set_working_variant('InitialState')
>>> network.get_generators()['target_p']['GEN']
>>> network.get_generators()['target_p']['GEN'].item()
607.0

Once you're done working with your variant, you can remove it:
Expand Down
4 changes: 2 additions & 2 deletions docs/user_guide/per_unit.rst
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Per Unit data
---------------
-------------

.. testsetup:: *

Expand Down Expand Up @@ -36,7 +36,7 @@ The nominal apparent power is by default 100 MVA. It can be set like this :
LINE_S3S4 0.000016 0.020469 0.0 0.0 0.0 0.0 0.960014 0.008700 0.960054 -0.960000 0.010166 0.960054 S3VL1 S4VL1 S3VL1_0 S4VL1_0 True True

Per Unit formula
---------------
----------------

#. Resistance R

Expand Down
2 changes: 1 addition & 1 deletion docs/user_guide/security.rst
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ The following operator strategy define the application of the switch action 'Swi
>>> sa_result = sa.run_ac(n)
>>> df = sa_result.branch_results
>>> #Get the detailed results post operator strategy
>>> df.loc['Breaker contingency', 'OperatorStrategy1', 'LINE_S3S4']['p1']
>>> df.loc['Breaker contingency', 'OperatorStrategy1', 'LINE_S3S4']['p1'].item()
240.00360040333226

Results for the post remedial action state are available in the branch results indexed with the operator strategy unique id.
12 changes: 9 additions & 3 deletions docs/user_guide/voltage_initializer.rst
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Run the voltage initializer
===========================================
===========================

.. currentmodule:: pypowsybl.voltage_initializer

Expand All @@ -23,7 +23,7 @@ Here is an example of a simple config.yaml file.
homeDir: PATH_TO_AMPL

Quick start
-------------
-----------

Here is a simple starting example:

Expand All @@ -48,11 +48,12 @@ Here is a simple starting example:
Available settings in the VoltageInitializerParameters class
-------------
------------------------------------------------------------

- Specify which buses will have reactive slacks attached in the ACOPF solving.

.. code-block:: python
import pypowsybl as pp
import pypowsybl.voltage_initializer as v_init
params = v_init.VoltageInitializerParameters()
Expand All @@ -61,6 +62,7 @@ Available settings in the VoltageInitializerParameters class
- Specify what is the log level of the AMPL solving.

.. code-block:: python
import pypowsybl as pp
import pypowsybl.voltage_initializer as v_init
params = v_init.VoltageInitializerParameters()
Expand All @@ -70,6 +72,7 @@ Available settings in the VoltageInitializerParameters class
- Change plausible voltage level limits in ACOPF solving.

.. code-block:: python
import pypowsybl as pp
import pypowsybl.voltage_initializer as v_init
params = v_init.VoltageInitializerParameters()
Expand All @@ -79,6 +82,7 @@ Available settings in the VoltageInitializerParameters class
- Tune the threshold defining null values in AMPL.

.. code-block:: python
import pypowsybl as pp
import pypowsybl.voltage_initializer as v_init
params = v_init.VoltageInitializerParameters()
Expand All @@ -88,6 +92,7 @@ Available settings in the VoltageInitializerParameters class
- Modify the parameters used for the correction of generator limits.

.. code-block:: python
import pypowsybl as pp
import pypowsybl.voltage_initializer as v_init
params = v_init.VoltageInitializerParameters()
Expand All @@ -100,6 +105,7 @@ Available settings in the VoltageInitializerParameters class
- Tune the thresholds used to ignore buses or voltage level limits with nominal voltage lower than them.

.. code-block:: python
import pypowsybl as pp
import pypowsybl.voltage_initializer as v_init
params = v_init.VoltageInitializerParameters()
Expand Down
14 changes: 7 additions & 7 deletions pypowsybl/network/impl/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
Any
)

from numpy import Inf
from numpy import inf
from numpy.typing import ArrayLike
from pandas import DataFrame
import pandas as pd
Expand Down Expand Up @@ -3986,23 +3986,23 @@ def create_3_windings_transformers(self, df: DataFrame = None, **kwargs: ArrayLi
- **bus1_id**: the bus where the new transformer will be connected on side 1,
if the voltage level has a bus-breaker topology kind.
- **connectable_bus1_id**: the bus to which the transformer can be connected on side 1, if the voltage level
has a bus-breaker topology kind. The transformer is created disconnected from this bus.
has a bus-breaker topology kind. The transformer is created disconnected from this bus.
- **node1**: the node where the new transformer will be connected on side 1,
if the voltage level has a node-breaker topology kind.
- **voltage_level2_id**: the voltage level where the new transformer will be connected on side 2.
The voltage level must already exist.
- **bus2_id**: the bus where the new transformer will be connected on side 2,
if the voltage level has a bus-breaker topology kind.
- **connectable_bus2_id**: the bus to which the transformer can be connected on side 2, if the voltage level
has a bus-breaker topology kind. The transformer is created disconnected from this bus.
has a bus-breaker topology kind. The transformer is created disconnected from this bus.
- **node2**: the node where the new transformer will be connected on side 2,
if the voltage level has a node-breaker topology kind.
- **voltage_level3_id**: the voltage level where the new transformer will be connected on side 3.
The voltage level must already exist.
- **bus3_id**: the bus where the new transformer will be connected on side 3,
if the voltage level has a bus-breaker topology kind.
- **connectable_bus3_id**: the bus to which the transformer can be connected on side 3, if the voltage level
has a bus-breaker topology kind. The transformer is created disconnected from this bus.
has a bus-breaker topology kind. The transformer is created disconnected from this bus.
- **node3**: the node where the new transformer will be connected on side 3,
if the voltage level has a node-breaker topology kind.
- **name**: an optional human-readable name
Expand Down Expand Up @@ -4247,7 +4247,7 @@ def create_ratio_tap_changers(self, rtc_df: DataFrame, steps_df: DataFrame) -> N
- **target_deadband**: the target voltage regulation deadband, in kV
- **regulating**: true if the tap changer should regulate voltage
- **regulated_side**: the side where voltage is regulated (ONE or TWO if two-winding transformer, ONE, TWO
or THREE if three-winding transformer)
or THREE if three-winding transformer)
- **side**: Side of the tap changer (only for three-winding transformers)
Valid attributes for the steps dataframe are:
Expand Down Expand Up @@ -4304,7 +4304,7 @@ def create_phase_tap_changers(self, ptc_df: DataFrame, steps_df: DataFrame) -> N
- **target_deadband**: the regulation deadband
- **regulating**: true if the tap changer should regulate
- **regulated_side**: the side where the current or active power is regulated (ONE or TWO if two-winding
transformer, ONE, TWO or THREE if three-winding transformer)
transformer, ONE, TWO or THREE if three-winding transformer)
- **side**: Side of the tap changer (only for three-winding transformers)
Valid attributes for the steps dataframe are:
Expand Down Expand Up @@ -4404,7 +4404,7 @@ def create_operational_limits(self, df: DataFrame = None, **kwargs: ArrayLike) -
kwargs: Attributes as keyword arguments.
"""
if df is not None:
df['acceptable_duration'] = df['acceptable_duration'].map(lambda x: -1 if x == Inf else int(x))
df['acceptable_duration'] = df['acceptable_duration'].map(lambda x: -1 if x == inf else int(x))
return self._create_elements(ElementType.OPERATIONAL_LIMITS, [df], **kwargs)

def create_minmax_reactive_limits(self, df: DataFrame = None, **kwargs: ArrayLike) -> None:
Expand Down
9 changes: 5 additions & 4 deletions pypowsybl/utils/impl/dataframes.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ def _to_array(value: _Any) -> np.ndarray:
"""
Converts a scalar or array to an array
"""
as_array = np.array(value, ndmin=1, copy=False)
as_array = np.asarray(value)
if as_array.ndim == 0:
as_array = np.expand_dims(as_array, axis=0)
if as_array.ndim != 1:
raise ValueError(f'Network elements update: expecting only scalar or 1 dimension array '
f'as keyword argument, got {as_array.ndim} dimensions')
Expand Down Expand Up @@ -86,9 +88,9 @@ def _create_c_dataframe(df: DataFrame, series_metadata: List[_pp.SeriesMetadata]
if index_name is None:
index_name = series_metadata[idx].name
if is_multi_index:
columns_values.append(df.index.get_level_values(index_name))
columns_values.append(list(df.index.get_level_values(index_name)))
else:
columns_values.append(df.index.values)
columns_values.append(list(df.index.values))
columns_names.append(index_name)
columns_types.append(metadata_by_name[index_name].type)
is_index.append(True)
Expand Down Expand Up @@ -153,7 +155,6 @@ def _adapt_properties_kwargs(**kwargs: _ArrayLike) -> DataFrame:
"""
Converts named arguments to a dataframe.
"""

columns = {}
expected_size = None
for key, value in kwargs.items():
Expand Down
1 change: 0 additions & 1 deletion pypowsybl/utils/impl/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ def create_data_frame_from_series_array(series_array: _pypowsybl.SeriesArray) ->
index_names.append(series.name)
else:
series_dict[series.name] = series.data
index = None
if not index_names:
raise ValueError('No index in returned dataframe')
if len(index_names) == 1:
Expand Down
8 changes: 4 additions & 4 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# actual dependencies
pandas==2.1.1; python_version == "3.12"
pandas==2.0.3; python_version < "3.12"
pandas==2.2.2; python_version >= "3.9"
pandas==2.0.3; python_version <= "3.8"
prettytable==2.0.0
networkx
matplotlib
Expand All @@ -14,6 +14,6 @@ wheel==0.38.1
coverage==7.3.2
pytest>=6.2.5
mypy==0.982
pandas-stubs==1.2.0.47
pandas-stubs==2.2.2.240603; python_version >= "3.9"
pandas-stubs==2.0.3.230814; python_version <= "3.8"
pylint==3.2.0
numpy==1.26.0; python_version >= "3.9"
7 changes: 3 additions & 4 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,9 @@ include_package_data = True
packages = find:
python_requires = >=3.8
install_requires =
prettytable
numpy>=1.20.0
pandas>=1.4.4; sys_platform == "darwin" and platform_machine == "arm64"
pandas>=1.3.5; sys_platform != "darwin" or platform_machine != "arm64"
prettytable>=2.0.0
pandas>=2.2.2; python_version >= "3.9"
pandas>=2.0.3; python_version <= "3.8"
networkx

[options.package_data]
Expand Down
Loading

0 comments on commit 242a91b

Please sign in to comment.