Skip to content

Commit

Permalink
improve pylint
Browse files Browse the repository at this point in the history
Signed-off-by: Etienne LESOT <etienne.lesot@rte-france.com>
  • Loading branch information
EtienneLt committed Sep 1, 2023
1 parent 5215e7a commit 35fa45e
Show file tree
Hide file tree
Showing 30 changed files with 190 additions and 195 deletions.
2 changes: 1 addition & 1 deletion pypowsybl/flowdecomposition/impl/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
# SPDX-License-Identifier: MPL-2.0
#
#
2 changes: 1 addition & 1 deletion pypowsybl/flowdecomposition/impl/flowdecomposition.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,6 @@ def run(self, network: _Network, flow_decomposition_parameters: Parameters = Non
======================= =========== ============== ======== ======== ================= ================= =============== =========== ======== ============= ================= =================
"""
fd_p = flow_decomposition_parameters._to_c_parameters() if flow_decomposition_parameters is not None else _pypowsybl.FlowDecompositionParameters()
lf_p = load_flow_parameters._to_c_parameters() if load_flow_parameters is not None else _pypowsybl.LoadFlowParameters()
lf_p = load_flow_parameters.to_c_parameters() if load_flow_parameters is not None else _pypowsybl.LoadFlowParameters()
res = _pypowsybl.run_flow_decomposition(self._handle, network._handle, fd_p, lf_p)
return create_data_frame_from_series_array(res)
2 changes: 1 addition & 1 deletion pypowsybl/flowdecomposition/impl/parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,4 @@ def __repr__(self) -> str:
f", rescale_enabled={self.rescale_enabled!r}" \
f", dc_fallback_enabled_after_ac_divergence={self.dc_fallback_enabled_after_ac_divergence}" \
f", sensitivity_variable_batch_size={self.sensitivity_variable_batch_size}" \
f")"
f")"
2 changes: 1 addition & 1 deletion pypowsybl/flowdecomposition/impl/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ def create_decomposition() -> FlowDecomposition:
Returns:
A flow decomposition object, which allows to run a flow decomposition on a network.
"""
return FlowDecomposition(_pypowsybl.create_flow_decomposition())
return FlowDecomposition(_pypowsybl.create_flow_decomposition())
8 changes: 4 additions & 4 deletions pypowsybl/loadflow/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@
get_default_provider,
get_provider_names,
set_default_provider,
get_provider_parameters
get_provider_parameters,
run_validation
)

from .impl.validation import (
run_validation,
from .impl.validation_parameters import (
ValidationType,
ValidationResult,
ValidationParameters
)
from .impl.validation_result import ValidationResult
from .impl.parameters import Parameters
from .impl.component_result import ComponentResult, ComponentStatus
2 changes: 1 addition & 1 deletion pypowsybl/loadflow/impl/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
# SPDX-License-Identifier: MPL-2.0
#
#
2 changes: 1 addition & 1 deletion pypowsybl/loadflow/impl/component_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,4 @@ def __repr__(self) -> str:
f", slack_bus_id={self.slack_bus_id!r}" \
f", slack_bus_active_power_mismatch={self.slack_bus_active_power_mismatch!r}" \
f", distributed_active_power={self.distributed_active_power!r}" \
f")"
f")"
83 changes: 54 additions & 29 deletions pypowsybl/loadflow/impl/loadflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,23 @@
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
# SPDX-License-Identifier: MPL-2.0
#
from typing import (
List as _List
)
from pandas import DataFrame as _DataFrame

from typing import List
from pandas import DataFrame
from pypowsybl import _pypowsybl
from pypowsybl._pypowsybl import (
ConnectedComponentMode,
BalanceType,
VoltageInitMode
VoltageInitMode,
LoadFlowValidationParameters,
run_loadflow_validation
)
from pypowsybl.network.impl.network import Network as _Network
from pypowsybl.util import create_data_frame_from_series_array as _create_data_frame_from_series_array
from pypowsybl.report import Reporter as _Reporter
from pypowsybl.network.impl.network import Network
from pypowsybl.util import create_data_frame_from_series_array
from pypowsybl.report import Reporter
from pypowsybl.loadflow.impl.component_result import ComponentResult
from pypowsybl.loadflow.impl.parameters import Parameters
from pypowsybl.loadflow.impl.validation_result import ValidationResult
from pypowsybl.loadflow.impl.validation_parameters import ValidationParameters, ValidationType

# enforcing some class metadata on classes imported from C extension,
# in particular for sphinx documentation to work correctly,
Expand All @@ -30,17 +31,8 @@
ConnectedComponentMode.__module__ = __name__



def _parameters_from_c(c_parameters: _pypowsybl.LoadFlowParameters) -> Parameters:
"""
Converts C struct to python parameters (bypassing python constructor)
"""
res = Parameters.__new__(Parameters)
res._init_from_c(c_parameters)
return res


def run_ac(network: _Network, parameters: Parameters = None, provider: str = '', reporter: _Reporter = None) -> _List[ComponentResult]:
def run_ac(network: Network, parameters: Parameters = None, provider: str = '', reporter: Reporter = None) -> List[
ComponentResult]:
"""
Run an AC loadflow on a network.
Expand All @@ -53,11 +45,13 @@ def run_ac(network: _Network, parameters: Parameters = None, provider: str = '',
Returns:
A list of component results, one for each component of the network.
"""
p = parameters._to_c_parameters() if parameters is not None else _pypowsybl.LoadFlowParameters()
return [ComponentResult(res) for res in _pypowsybl.run_loadflow(network._handle, False, p, provider, None if reporter is None else reporter._reporter_model)] # pylint: disable=protected-access
p = parameters.to_c_parameters() if parameters is not None else _pypowsybl.LoadFlowParameters()
return [ComponentResult(res) for res in _pypowsybl.run_loadflow(network._handle, False, p, provider,
None if reporter is None else reporter._reporter_model)] # pylint: disable=protected-access


def run_dc(network: _Network, parameters: Parameters = None, provider: str = '', reporter: _Reporter = None) -> _List[ComponentResult]:
def run_dc(network: Network, parameters: Parameters = None, provider: str = '', reporter: Reporter = None) -> List[
ComponentResult]:
"""
Run a DC loadflow on a network.
Expand All @@ -70,8 +64,9 @@ def run_dc(network: _Network, parameters: Parameters = None, provider: str = '',
Returns:
A list of component results, one for each component of the network.
"""
p = parameters._to_c_parameters() if parameters is not None else _pypowsybl.LoadFlowParameters()
return [ComponentResult(res) for res in _pypowsybl.run_loadflow(network._handle, True, p, provider, None if reporter is None else reporter._reporter_model)] # pylint: disable=protected-access
p = parameters.to_c_parameters() if parameters is not None else _pypowsybl.LoadFlowParameters()
return [ComponentResult(res) for res in _pypowsybl.run_loadflow(network._handle, True, p, provider,
None if reporter is None else reporter._reporter_model)] # pylint: disable=protected-access


def set_default_provider(provider: str) -> None:
Expand All @@ -94,7 +89,7 @@ def get_default_provider() -> str:
return _pypowsybl.get_default_loadflow_provider()


def get_provider_names() -> _List[str]:
def get_provider_names() -> List[str]:
"""
Get list of supported provider names.
Expand All @@ -104,7 +99,7 @@ def get_provider_names() -> _List[str]:
return _pypowsybl.get_loadflow_provider_names()


def get_provider_parameters_names(provider: str = None) -> _List[str]:
def get_provider_parameters_names(provider: str = None) -> List[str]:
"""
Get list of parameters for the specified loadflow provider.
Expand All @@ -117,7 +112,7 @@ def get_provider_parameters_names(provider: str = None) -> _List[str]:
return _pypowsybl.get_loadflow_provider_parameters_names('' if provider is None else provider)


def get_provider_parameters(provider: str = None) -> _DataFrame:
def get_provider_parameters(provider: str = None) -> DataFrame:
"""
Supported loadflow specific parameters for a given provider.
Expand All @@ -139,4 +134,34 @@ def get_provider_parameters(provider: str = None) -> _DataFrame:
'MOST_MESHED'
"""
series_array = _pypowsybl.create_loadflow_provider_parameters_series_array('' if provider is None else provider)
return _create_data_frame_from_series_array(series_array)
return create_data_frame_from_series_array(series_array)


def run_validation(network: Network, validation_types: List[ValidationType] = None,
validation_parameters: ValidationParameters = None) -> ValidationResult:
"""
Checks that the network data are consistent with AC loadflow equations.
Args:
network: The network to be checked.
validation_types: The types of data to be checked. If None, all types will be checked.
validation_parameters: The parameters to run the validation with.
Returns:
The validation result.
"""
if validation_types is None:
validation_types = ValidationType.ALL
validation_config = validation_parameters._to_c_parameters() if validation_parameters is not None else LoadFlowValidationParameters()
res_by_type = {}
for validation_type in validation_types:
series_array = run_loadflow_validation(network._handle, validation_type, validation_config)
res_by_type[validation_type] = create_data_frame_from_series_array(series_array)

return ValidationResult(buses=res_by_type.get(ValidationType.BUSES, None),
branch_flows=res_by_type.get(ValidationType.FLOWS, None),
generators=res_by_type.get(ValidationType.GENERATORS, None),
svcs=res_by_type.get(ValidationType.SVCS, None),
shunts=res_by_type.get(ValidationType.SHUNTS, None),
twts=res_by_type.get(ValidationType.TWTS, None),
t3wts=res_by_type.get(ValidationType.TWTS3W, None))
4 changes: 2 additions & 2 deletions pypowsybl/loadflow/impl/parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ def _init_from_c(self, c_parameters: _pypowsybl.LoadFlowParameters) -> None:
def _init_with_default_values(self) -> None:
self._init_from_c(_pypowsybl.LoadFlowParameters())

def _to_c_parameters(self) -> _pypowsybl.LoadFlowParameters:
def to_c_parameters(self) -> _pypowsybl.LoadFlowParameters:
c_parameters = _pypowsybl.LoadFlowParameters()
c_parameters.voltage_init_mode = self.voltage_init_mode
c_parameters.transformer_voltage_control_on = self.transformer_voltage_control_on
Expand Down Expand Up @@ -164,4 +164,4 @@ def __repr__(self) -> str:
f", countries_to_balance={self.countries_to_balance}" \
f", connected_component_mode={self.connected_component_mode!r}" \
f", provider_parameters={self.provider_parameters!r}" \
f")"
f")"
16 changes: 16 additions & 0 deletions pypowsybl/loadflow/impl/util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Copyright (c) 2023, RTE (http://www.rte-france.com)
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
# SPDX-License-Identifier: MPL-2.0
#
from pypowsybl.loadflow.impl.parameters import Parameters
from pypowsybl import _pypowsybl

def parameters_from_c(c_parameters: _pypowsybl.LoadFlowParameters) -> Parameters:
"""
Converts C struct to python parameters (bypassing python constructor)
"""
res = Parameters.__new__(Parameters)
res._init_from_c(c_parameters)
return res
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,15 @@
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
# SPDX-License-Identifier: MPL-2.0
#
from typing import (
Optional,
List
)
import pandas as pd
import pypowsybl._pypowsybl as _pp
from pypowsybl._pypowsybl import ValidationType

from pypowsybl.loadflow.impl.parameters import Parameters
from pypowsybl.loadflow.impl.loadflow import _parameters_from_c
from pypowsybl.network.impl.network import Network
from pypowsybl.util import create_data_frame_from_series_array
from pypowsybl.loadflow.impl.util import parameters_from_c

ValidationType.ALL = [ValidationType.BUSES, ValidationType.FLOWS, ValidationType.GENERATORS, ValidationType.SHUNTS,
ValidationType.SVCS, ValidationType.TWTS, ValidationType.TWTS3W]

OptionalDf = Optional[pd.DataFrame]


class ValidationParameters: # pylint: disable=too-few-public-methods
"""
Expand Down Expand Up @@ -102,7 +93,7 @@ def _init_from_c(self, c_parameters: _pp.LoadFlowValidationParameters) -> None:
self.loadflow_name = c_parameters.loadflow_name
self.epsilon_x = c_parameters.epsilon_x
self.apply_reactance_correction = c_parameters.apply_reactance_correction
self.loadflow_parameters = _parameters_from_c(c_parameters.loadflow_parameters)
self.loadflow_parameters = parameters_from_c(c_parameters.loadflow_parameters)
self.ok_missing_values = c_parameters.ok_missing_values
self.no_requirement_if_reactive_bound_inversion = c_parameters.no_requirement_if_reactive_bound_inversion
self.compare_results = c_parameters.compare_results
Expand All @@ -116,7 +107,7 @@ def _to_c_parameters(self) -> _pp.LoadFlowValidationParameters:
c_parameters.loadflow_name = self.loadflow_name
c_parameters.epsilon_x = self.epsilon_x
c_parameters.apply_reactance_correction = self.apply_reactance_correction
c_parameters.loadflow_parameters = self.loadflow_parameters._to_c_parameters()
c_parameters.loadflow_parameters = self.loadflow_parameters.to_c_parameters()
c_parameters.ok_missing_values = self.ok_missing_values
c_parameters.no_requirement_if_reactive_bound_inversion = self.no_requirement_if_reactive_bound_inversion
c_parameters.compare_results = self.compare_results
Expand All @@ -137,113 +128,3 @@ def __repr__(self) -> str:
f", compare_results={self.compare_results!r}" \
f", no_requirement_if_setpoint_outside_power_bounds={self.no_requirement_if_setpoint_outside_power_bounds}" \
f")"


class ValidationResult:
"""
The result of a loadflow validation.
"""

def __init__(self, branch_flows: OptionalDf, buses: OptionalDf, generators: OptionalDf, svcs: OptionalDf,
shunts: OptionalDf, twts: OptionalDf, t3wts: OptionalDf):
self._branch_flows = branch_flows
self._buses = buses
self._generators = generators
self._svcs = svcs
self._shunts = shunts
self._twts = twts
self._t3wts = t3wts
self._valid = self._is_valid_or_unchecked(self.branch_flows) and self._is_valid_or_unchecked(self.buses) \
and self._is_valid_or_unchecked(self.generators) and self._is_valid_or_unchecked(self.svcs) \
and self._is_valid_or_unchecked(self.shunts) and self._is_valid_or_unchecked(self.twts) \
and self._is_valid_or_unchecked(self.t3wts)

@staticmethod
def _is_valid_or_unchecked(df: OptionalDf) -> bool:
return df is None or df['validated'].all()

@property
def branch_flows(self) -> OptionalDf:
"""
Validation results for branch flows.
"""
return self._branch_flows

@property
def buses(self) -> OptionalDf:
"""
Validation results for buses.
"""
return self._buses

@property
def generators(self) -> OptionalDf:
"""
Validation results for generators.
"""
return self._generators

@property
def svcs(self) -> OptionalDf:
"""
Validation results for SVCs.
"""
return self._svcs

@property
def shunts(self) -> OptionalDf:
"""
Validation results for shunts.
"""
return self._shunts

@property
def twts(self) -> OptionalDf:
"""
Validation results for two winding transformers.
"""
return self._twts

@property
def t3wts(self) -> OptionalDf:
"""
Validation results for three winding transformers.
"""
return self._t3wts

@property
def valid(self) -> bool:
"""
True if all checked data is valid.
"""
return self._valid


def run_validation(network: Network, validation_types: List[ValidationType] = None,
validation_parameters: ValidationParameters = None) -> ValidationResult:
"""
Checks that the network data are consistent with AC loadflow equations.
Args:
network: The network to be checked.
validation_types: The types of data to be checked. If None, all types will be checked.
validation_parameters: The parameters to run the validation with.
Returns:
The validation result.
"""
if validation_types is None:
validation_types = ValidationType.ALL
validation_config = validation_parameters._to_c_parameters() if validation_parameters is not None else _pp.LoadFlowValidationParameters()
res_by_type = {}
for validation_type in validation_types:
series_array = _pp.run_loadflow_validation(network._handle, validation_type, validation_config)
res_by_type[validation_type] = create_data_frame_from_series_array(series_array)

return ValidationResult(buses=res_by_type.get(ValidationType.BUSES, None),
branch_flows=res_by_type.get(ValidationType.FLOWS, None),
generators=res_by_type.get(ValidationType.GENERATORS, None),
svcs=res_by_type.get(ValidationType.SVCS, None),
shunts=res_by_type.get(ValidationType.SHUNTS, None),
twts=res_by_type.get(ValidationType.TWTS, None),
t3wts=res_by_type.get(ValidationType.TWTS3W, None))
Loading

0 comments on commit 35fa45e

Please sign in to comment.