power_grid_model

class power_grid_model.PowerGridModel(*_args, **_kwargs)

Bases: object

Main class for Power Grid Model

property batch_error: PowerGridBatchError | None

Get the batch error object, if present

Returns:

Batch error object, or None

property all_component_count: Dict[str, int]

Get count of number of elements per component type. If the count for a component type is zero, it will not be in the returned dictionary.

Returns:

A dictionary with

  • key: Component type name

  • value: Integer count of elements of this type

copy() PowerGridModel

Copy the current model

Returns:

A copy of PowerGridModel

__init__(input_data: Dict[str, ndarray], system_frequency: float = 50.0)

Initialize the model from an input data set.

Parameters:
  • input_data

    Input data dictionary

    • key: Component type name

    • value: 1D numpy structured array for this component input

  • system_frequency – Frequency of the power system, default 50 Hz

update(*, update_data: Dict[str, ndarray])

Update the model with changes.

Parameters:

update_data

Update data dictionary

  • key: Component type name

  • value: 1D numpy structured array for this component update

Returns:

None

get_indexer(component_type: str, ids: ndarray)

Get array of indexers given array of ids for component type

Parameters:
  • component_type – Type of component

  • ids – Array of ids

Returns:

Array of indexers, same shape as input array ids

calculate_power_flow(*, symmetric: bool = True, error_tolerance: float = 1e-08, max_iterations: int = 20, calculation_method: CalculationMethod | str = CalculationMethod.newton_raphson, update_data: Dict[str, ndarray | Dict[str, ndarray]] | None = None, threading: int = -1, output_component_types: Set[str] | List[str] | None = None, continue_on_batch_error: bool = False, decode_error: bool = True) Dict[str, ndarray]

Calculate power flow once with the current model attributes. Or calculate in batch with the given update dataset in batch.

Parameters:
  • symmetric (bool, optional) –

    Whether to perform a three-phase symmetric calculation.

    • True: Three-phase symmetric calculation, even for asymmetric loads/generations (Default).

    • False: Three-phase asymmetric calculation.

  • error_tolerance (float, optional) – Error tolerance for voltage in p.u., applicable only when the calculation method is iterative.

  • max_iterations (int, optional) – Maximum number of iterations, applicable only when the calculation method is iterative.

  • calculation_method (an enumeration or string) –

    The calculation method to use.

    • newton_raphson: Use Newton-Raphson iterative method (default).

    • linear: Use linear method.

  • update_data (dict, optional) –

    None: Calculate power flow once with the current model attributes. Or a dictionary for batch calculation with batch update.

    • key: Component type name to be updated in batch.

    • value:

      • For homogeneous update batch (a 2D numpy structured array):

        • Dimension 0: Each batch.

        • Dimension 1: Each updated element per batch for this component type.

      • For inhomogeneous update batch (a dictionary containing two keys):

  • threading (int, optional) –

    Applicable only for batch calculation.

    • < 0: Sequential

    • = 0: Parallel, use number of hardware threads

    • > 0: Specify number of parallel threads

  • output_component_types ({set, list}, optional) – List or set of component types you want to be present in the output dict. By default, all component types will be in the output.

  • continue_on_batch_error (bool, optional) – Continue the program (instead of throwing error) if some scenarios fail.

  • decode_error (bool, optional) – Decode error messages to their derived types if possible.

Returns:

Dictionary of results of all components.

  • key: Component type name to be updated in batch.

  • value:

    • For single calculation: 1D numpy structured array for the results of this component type.

    • For batch calculation: 2D numpy structured array for the results of this component type.

      • Dimension 0: Each batch.

      • Dimension 1: The result of each element for this component type.

Raises:

Exception – In case an error in the core occurs, an exception will be thrown.

calculate_state_estimation(*, symmetric: bool = True, error_tolerance: float = 1e-08, max_iterations: int = 20, calculation_method: CalculationMethod | str = CalculationMethod.iterative_linear, update_data: Dict[str, ndarray | Dict[str, ndarray]] | None = None, threading: int = -1, output_component_types: Set[str] | List[str] | None = None, continue_on_batch_error: bool = False, decode_error: bool = True) Dict[str, ndarray]

Calculate state estimation once with the current model attributes. Or calculate in batch with the given update dataset in batch.

Parameters:
  • symmetric (bool, optional) –

    Whether to perform a three-phase symmetric calculation.

    • True: Three-phase symmetric calculation, even for asymmetric loads/generations (Default).

    • False: Three-phase asymmetric calculation.

  • error_tolerance (float, optional) – error tolerance for voltage in p.u., only applicable when the calculation method is iterative.

  • max_iterations (int, optional) – Maximum number of iterations, applicable only when the calculation method is iterative.

  • calculation_method (an enumeration) – Use iterative linear method.

  • update_data (dict, optional) –

    None: Calculate state estimation once with the current model attributes. Or a dictionary for batch calculation with batch update.

    • key: Component type name to be updated in batch.

    • value:

      • For homogeneous update batch (a 2D numpy structured array):

        • Dimension 0: Each batch.

        • Dimension 1: Each updated element per batch for this component type.

      • For inhomogeneous update batch (a dictionary containing two keys):

  • threading (int, optional) –

    Applicable only for batch calculation.

    • < 0: Sequential

    • = 0: Parallel, use number of hardware threads

    • > 0: Specify number of parallel threads

  • output_component_types ({set, list}, optional) – List or set of component types you want to be present in the output dict. By default, all component types will be in the output.

  • continue_on_batch_error (bool, optional) – Continue the program (instead of throwing error) if some scenarios fail.

  • decode_error (bool, optional) – Decode error messages to their derived types if possible.

Returns:

Dictionary of results of all components.

  • key: Component type name to be updated in batch.

  • value:

    • For single calculation: 1D numpy structured array for the results of this component type.

    • For batch calculation: 2D numpy structured array for the results of this component type.

      • Dimension 0: Each batch.

      • Dimension 1: The result of each element for this component type.

Raises:

Exception – In case an error in the core occurs, an exception will be thrown.

calculate_short_circuit(*, calculation_method: CalculationMethod | str = CalculationMethod.iec60909, update_data: Dict[str, ndarray | Dict[str, ndarray]] | None = None, threading: int = -1, output_component_types: Set[str] | List[str] | None = None, continue_on_batch_error: bool = False, decode_error: bool = True, short_circuit_voltage_scaling: ShortCircuitVoltageScaling | str = ShortCircuitVoltageScaling.maximum) Dict[str, ndarray]

Calculate a short circuit once with the current model attributes. Or calculate in batch with the given update dataset in batch

Parameters:
  • calculation_method (an enumeration) – Use the iec60909 standard.

  • update_data

    None: calculate a short circuit once with the current model attributes. Or a dictionary for batch calculation with batch update

    • key: Component type name to be updated in batch

    • value:

      • For homogeneous update batch (a 2D numpy structured array):

        • Dimension 0: each batch

        • Dimension 1: each updated element per batch for this component type

      • For inhomogeneous update batch (a dictionary containing two keys):

  • threading (int, optional) –

    Applicable only for batch calculation.

    • < 0: Sequential

    • = 0: Parallel, use number of hardware threads

    • > 0: Specify number of parallel threads

  • output_component_types ({set, list}, optional) – List or set of component types you want to be present in the output dict. By default, all component types will be in the output.

  • continue_on_batch_error (bool, optional) – Continue the program (instead of throwing error) if some scenarios fail.

  • decode_error (bool, optional) – Decode error messages to their derived types if possible.

  • short_circuit_voltage_scaling ({ShortCircuitVoltageSaling, str}, optional) – Whether to use the maximum or minimum voltage scaling. By default, the maximum voltage scaling is used to calculate the short circuit.

Returns:

Dictionary of results of all components.

  • key: Component type name to be updated in batch.

  • value:

    • For single calculation: 1D numpy structured array for the results of this component type.

    • For batch calculation: 2D numpy structured array for the results of this component type.

      • Dimension 0: Each batch.

      • Dimension 1: The result of each element for this component type.

Raises:

Exception – In case an error in the core occurs, an exception will be thrown.

power_grid_model.initialize_array(data_type: str, component_type: str, shape: tuple | int, empty: bool = False) ndarray

Initializes an array for use in Power Grid Model calculations

Parameters:
  • data_type – input, update, sym_output, or asym_output

  • component_type – one component type, e.g. node

  • shape – shape of initialization integer, it is a 1-dimensional array tuple, it is an N-dimensional (tuple.shape) array

  • empty – if True, leave the memory block un-initialized

Returns:

np structured array with all entries as null value

enum

Common Enumerations

Note: these enumeration match the C++ arithmetic core, so don’t change the values unless you change them in C++ as well

class power_grid_model.enum.LoadGenType(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)

Bases: IntEnum

Load and Generator Types

const_power = 0
const_impedance = 1
const_current = 2
class power_grid_model.enum.WindingType(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)

Bases: IntEnum

Transformer Winding Types

wye = 0
wye_n = 1
delta = 2
zigzag = 3
zigzag_n = 4
class power_grid_model.enum.BranchSide(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)

Bases: IntEnum

Branch Sides

from_side = 0
to_side = 1
class power_grid_model.enum.Branch3Side(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)

Bases: IntEnum

Branch3 Sides

side_1 = 0
side_2 = 1
side_3 = 2
class power_grid_model.enum.CalculationType(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)

Bases: IntEnum

Calculation Types

power_flow = 0
state_estimation = 1
short_circuit = 2
class power_grid_model.enum.CalculationMethod(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)

Bases: IntEnum

Calculation Methods

linear = 0
newton_raphson = 1
iterative_linear = 2
iterative_current = 3
linear_current = 4
iec60909 = 5
class power_grid_model.enum.MeasuredTerminalType(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)

Bases: IntEnum

The type of asset measured by a (power) sensor

branch_from = 0

Measuring the from-terminal between a branch (except link) and a node

branch_to = 1

Measuring the to-terminal between a branch (except link) and a node

source = 2

Measuring the terminal between a source and a node

shunt = 3

Measuring the terminal between a shunt and a node

load = 4

Measuring the terminal between a load and a node

generator = 5

Measuring the terminal between a generator and a node

branch3_1 = 6

Measuring the terminal-1 between a branch3 and a node

branch3_2 = 7

Measuring the terminal-2 between a branch3 and a node

branch3_3 = 8

Measuring the terminal-3 between a branch3 and a node

node = 9

Measuring the total power injection into a node

class power_grid_model.enum.FaultType(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)

Bases: IntEnum

The type of fault represented by a fault component

three_phase = 0
single_phase_to_ground = 1
two_phase = 2
two_phase_to_ground = 3
nan = -128

Unspecified fault type. Needs to be overloaded at the latest in the update_data

class power_grid_model.enum.FaultPhase(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)

Bases: IntEnum

The faulty phase(s) affected by the provided fault type

abc = 0

All phases are faulty in a three-phase fault

a = 1

The first phase is faulty in a single-phase-to-ground fault

b = 2

The second phase is faulty in a single-phase-to-ground fault

c = 3

The third phase is faulty in a single-phase-to-ground fault

ab = 4

The first and second phase are faulty in a two-phase or two-phase-to-ground fault

ac = 5

The first and third phase are faulty in a two-phase or two-phase-to-ground fault

bc = 6

The second and third phase are faulty in a two-phase or two-phase-to-ground fault

default_value = -1

Use the default fault phase. Depends on the fault_type.

nan = -128

Unspecified fault phase. Needs to be overloaded at the latest in the update_data

class power_grid_model.enum.ShortCircuitVoltageScaling(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)

Bases: IntEnum

Voltage scaling for short circuit calculations

minimum = 0
maximum = 1

data types

power_grid_model.data_types.Dataset

A general data set can be a SingleDataset or a BatchDataset.

power_grid_model.data_types.SingleDataset

A single dataset is a dictionary where the keys are the component types and the values are SingleArray

power_grid_model.data_types.BatchDataset

A batch dataset is a dictionary where the keys are the component types and the values are BatchArray

power_grid_model.data_types.DataArray

A data array can be a SingleArray or a BatchArray.

power_grid_model.data_types.SingleArray

A single array is a one-dimensional structured containing a list of components of the same type.

  • Examples:

    • structure: <1d-array>

    • concrete: array([(0, 10500.0), (0, 10500.0)], dtype=power_grid_meta_data[“input”][“node”].dtype)

power_grid_model.data_types.BatchArray

A batch array is a either a DenseBatchArray or a SparseBatchArray.

power_grid_model.data_types.DenseBatchArray

A dense batch array is a two-dimensional structured numpy array containing a list of components of the same type for each scenario.

power_grid_model.data_types.SparseBatchArray

A sparse batch array is a dictionary containing the keys indptr and data.

  • data: a SingleArray. The exact dtype depends on the type of component.

  • indptr: a one-dimensional numpy int64 array containing n+1 elements where n is the amount of scenarios.

    • The elements are the indices in the data that point to the first element of that scenario.

    • The last element is one after the data index of the last element of the last scenario.

    • Usually, the last element will therefore be the size of the data.

  • Examples:

    • structure: {“indptr”: <1d-array>, “data”: SingleArray}

    • concrete example: {“indptr”: [0, 2, 2, 3], “data”: [(0, 1, 1), (1, 1, 1), (0, 0, 0)]}

      • the scenario 0 sets the statuses of components ids 0 and 1 to 1 (and keeps defaults for other components)

      • scenario 1 keeps the default values for all components

      • scenario 2 sets the statuses of component with id 0 to 0 (and keeps defaults for other components)

error types

Error classes

exception power_grid_model.errors.PowerGridError

Generic power grid error

exception power_grid_model.errors.PowerGridBatchError

Error occurs in batch calculation

failed_scenarios: ndarray
succeeded_scenarios: ndarray
error_messages: List[str]
errors: List[PowerGridError]
exception power_grid_model.errors.MissingCaseForEnumError

An enum value is not covered in a for loop.

This usually happens when an invalid combination of (enum) settings is provided.

exception power_grid_model.errors.ConflictVoltage

There is a confliciting voltage

exception power_grid_model.errors.InvalidBranch

A branch is invalid

exception power_grid_model.errors.InvalidBranch3

A branch3 is invalid

exception power_grid_model.errors.InvalidTransformerClock

Invalid transformer clock found

exception power_grid_model.errors.SparseMatrixError

Attempting to invert a non-invertible matrix

exception power_grid_model.errors.NotObservableError

Attempting to solve a non-observable system

exception power_grid_model.errors.IterationDiverge

Unable to iteratively converge to an optimum within the set number of iterations and precision

exception power_grid_model.errors.InvalidID

An ID is invalid

exception power_grid_model.errors.ConflictID

Conflicting IDs found

exception power_grid_model.errors.IDNotFound

A reference to a non-existent ID was provided

exception power_grid_model.errors.InvalidMeasuredObject

A provided measured object is invalid

exception power_grid_model.errors.InvalidRegulatedObject

A provided regulated object is invalid

exception power_grid_model.errors.IDWrongType

A referenced ID points to a component that cannot be referenced here

exception power_grid_model.errors.InvalidCalculationMethod

Invalid calculation method provided

exception power_grid_model.errors.AutomaticTapCalculationError

Automatic tap changer with tap at LV side is unsupported for automatic tap changing calculation.

exception power_grid_model.errors.InvalidShortCircuitPhaseOrType

Invalid (combination of) short circuit types and phase(s) provided

exception power_grid_model.errors.PowerGridSerializationError

Error occurs during (de-)serialization

exception power_grid_model.errors.PowerGridDatasetError

Error occurs during dataset handling

exception power_grid_model.errors.PowerGridUnreachableHitError

Supposedly unreachable code was hit

This usually means a failed assumption and may be caused by a bug in the PGM library.

validation

power_grid_model.validation.validate_input_data(input_data: Dict[str, ndarray], calculation_type: CalculationType | None = None, symmetric: bool = True) List[ValidationError] | None

Validates the entire input dataset:

  1. Is the data structure correct? (checking data types and numpy array shapes)

  2. Are all required values provided? (checking NaNs)

  3. Are all ID’s unique? (checking object identifiers across all components)

  4. Are the supplied values valid? (checking limits and other logic as described in “Graph Data Model”)

Parameters:
  • input_data – A power-grid-model input dataset

  • calculation_type – Supply a calculation method, to allow missing values for unused fields

  • symmetric – A boolean to state whether input data will be used for a symmetric or asymmetric calculation

Returns:

None if the data is valid, or a list containing all validation errors.

Raises:

Error – KeyError | TypeError | ValueError: if the data structure is invalid.

power_grid_model.validation.validate_batch_data(input_data: Dict[str, ndarray], update_data: Dict[str, ndarray | Dict[str, ndarray]], calculation_type: CalculationType | None = None, symmetric: bool = True) Dict[int, List[ValidationError]] | None

The input dataset is validated:

  1. Is the data structure correct? (checking data types and numpy array shapes)

  2. Are all input data ID’s unique? (checking object identifiers across all components)

For each batch the update data is validated:
  1. Is the update data structure correct? (checking data types and numpy array shapes)

  2. Are all update ID’s valid? (checking object identifiers across update and input data)

Then (for each batch independently) the input dataset is updated with the batch’s update data and validated:
  1. Are all required values provided? (checking NaNs)

  2. Are the supplied values valid? (checking limits and other logic as described in “Graph Data Model”)

Parameters:
  • input_data – A power-grid-model input dataset

  • update_data – A power-grid-model update dataset (one or more batches)

  • calculation_type – Supply a calculation method, to allow missing values for unused fields

  • symmetric – A boolean to state whether input data will be used for a symmetric or asymmetric calculation

Returns:

None if the data is valid, or a dictionary containing all validation errors, where the key is the batch number (0-indexed).

Raises:

Error – KeyError | TypeError | ValueError: if the data structure is invalid.

power_grid_model.validation.assert_valid_input_data(input_data: Dict[str, ndarray], calculation_type: CalculationType | None = None, symmetric: bool = True)

Validates the entire input dataset:

  1. Is the data structure correct? (checking data types and numpy array shapes)

  2. Are all required values provided? (checking NaNs)

  3. Are all ID’s unique? (checking object identifiers across all components)

  4. Are the supplied values valid? (checking limits and other logic as described in “Graph Data Model”)

Parameters:
  • input_data – A power-grid-model input dataset

  • calculation_type – Supply a calculation method, to allow missing values for unused fields

  • symmetric – A boolean to state whether input data will be used for a symmetric or asymmetric calculation

Raises:
  • KeyError | TypeError | ValueError – if the data structure is invalid.

  • ValidationException – if the contents are invalid.

power_grid_model.validation.assert_valid_batch_data(input_data: Dict[str, ndarray], update_data: Dict[str, ndarray | Dict[str, ndarray]], calculation_type: CalculationType | None = None, symmetric: bool = True)

The input dataset is validated:

  1. Is the data structure correct? (checking data types and numpy array shapes)

  2. Are all input data ID’s unique? (checking object identifiers across all components)

For each batch the update data is validated:
  1. Is the update data structure correct? (checking data types and numpy array shapes)

  2. Are all update ID’s valid? (checking object identifiers across update and input data)

Then (for each batch independently) the input dataset is updated with the batch’s update data and validated:
  1. Are all required values provided? (checking NaNs)

  2. Are the supplied values valid? (checking limits and other logic as described in “Graph Data Model”)

Parameters:
  • input_data – a power-grid-model input dataset

  • update_data – a power-grid-model update dataset (one or more batches)

  • calculation_type – Supply a calculation method, to allow missing values for unused fields

  • symmetric – A boolean to state whether input data will be used for a symmetric or asymmetric calculation

Raises:
  • KeyError | TypeError | ValueError – if the data structure is invalid.

  • ValidationException – if the contents are invalid.

power_grid_model.validation.errors_to_string(errors: List[ValidationError] | Dict[int, List[ValidationError]] | None, name: str = 'the data', details: bool = False, id_lookup: List[str] | Dict[int, str] | None = None) str

Convert a set of errors (list or dict) to a human readable string representation.

Parameters:
  • errors – The error objects. List for input_data only, dict for batch data.

  • name – Human understandable name of the dataset, e.g. input_data, or update_data.

  • details – Display object ids and error specific information.

  • id_lookup – A list or dict (int->str) containing textual object ids

Returns:

A human readable string representation of a set of errors.

errors

class power_grid_model.validation.errors.ValidationError

The Validation Error is an abstract base class which should be extended by all validation errors. It supplies three public member variables: component, field and ids; storing information about the origin of the validation error. Error classes can extend the public members. For example:

NotBetweenError(ValidationError):

component = ‘vehicle’ field = ‘direction’ id = [3, 14, 15, 92, 65, 35] ref_value = (-3.1416, 3.1416)

For convenience, a human readable representation of the error is supplied using the str() function. I.e. print(str(error)) will print a human readable error message like:

Field direction is not between -3.1416 and 3.1416 for 6 vehicles

component: str | List[str] | None = None

The component, or components, to which the error applies.

field: str | List[str] | List[Tuple[str, str]] | None = None

The field, or fields, to which the error applies. A field can also be a tuple (component, field) when multiple components are being addressed.

ids: List[int] | List[Tuple[str, int]] | None = None

The object identifiers to which the error applies. A field object identifier can also be a tuple (component, id) when multiple components are being addressed.

property component_str: str

A string representation of the component to which this error applies

property field_str: str

A string representation of the field to which this error applies

get_context(id_lookup: List[str] | Dict[int, str] | None = None) Dict[str, Any]

Returns a dictionary that supplies (human readable) information about this error. Each member variable is included in the dictionary. If a function {field_name}_str() exists, the value is overwritten by that function.

Parameters:

id_lookup – A list or dict (int->str) containing textual object ids

utils

power_grid_model.utils.get_dataset_type(data: Mapping[str, ndarray | Mapping[str, ndarray]]) str

Deduce the dataset type from the provided dataset.

Parameters:

data – the dataset

Raises:
  • ValueError – if the dataset type cannot be deduced because multiple dataset types match the format (probably because the data contained no supported components, e.g. was empty)

  • PowerGridError – if no dataset type matches the format of the data (probably because the data contained conflicting data formats)

Returns:

The dataset type.

power_grid_model.utils.get_dataset_scenario(dataset: Dict[str, ndarray | Dict[str, ndarray]], scenario: int) Dict[str, ndarray]

Obtain the single dataset at a given scenario, independently of the internal batch data structure.

Parameters:
  • dataset – the batch dataset

  • scenario – the scenario index

Raises:

IndexError – if the scenario is out of range for any of the components.

Returns:

The dataset for a specific scenario

power_grid_model.utils.json_deserialize(data: str | bytes) Dict[str, ndarray | Dict[str, ndarray]]

Load serialized JSON data to a new dataset.

Parameters:

data – the data to deserialize.

Raises:
  • ValueError – if the data is inconsistent with the rest of the dataset or a component is unknown.

  • PowerGridError – if there was an internal error.

Returns:

A tuple containing the deserialized dataset in Power grid model input format and the type of the dataset.

power_grid_model.utils.json_serialize(data: Mapping[str, ndarray] | Mapping[str, ndarray | Mapping[str, ndarray]], dataset_type: str | None = None, use_compact_list: bool = False, indent: int = 2) str

Dump data to a JSON str.

If the dataset_type is not specified or None, it will be deduced from the dataset if possible. Deduction is not possible in case data is empty.

Parameters:
  • data – the dataset

  • dataset_type – the type of the dataset. Defaults to None. Required str-type if data is empty.

  • use_compact_list – whether or not to use compact lists (sparse data). Defaults to False.

  • indent – use specified indentation to make data more readable Use 0 or negative value for no indentation. Defaults to 2

Raises:

PowerGridError – if there was an internal error.

Returns:

A serialized string containing the dataset.

power_grid_model.utils.msgpack_deserialize(data: bytes) Dict[str, ndarray | Dict[str, ndarray]]

Load serialized msgpack data to a new dataset.

Parameters:

data – the data to deserialize.

Raises:
  • ValueError – if the data is inconsistent with the rest of the dataset or a component is unknown.

  • PowerGridError – if there was an internal error.

Returns:

A tuple containing the deserialized dataset in Power grid model input format and the type of the dataset.

power_grid_model.utils.msgpack_serialize(data: Mapping[str, ndarray] | Mapping[str, ndarray | Mapping[str, ndarray]], dataset_type: str | None = None, use_compact_list: bool = False) bytes

Dump the data to raw msgpack bytes.

If the dataset_type is not specified or None, it will be deduced from the dataset if possible. Deduction is not possible in case data is empty.

Parameters:
  • data – the dataset

  • dataset_type – the type of the dataset. Defaults to None. Required str-type if data is empty.

  • use_compact_list – whether or not to use compact lists (sparse data). Defaults to False.

Raises:
  • KeyError – if the dataset_type was not provided and could not be deduced from the dataset (i.e. it was empty).

  • PowerGridError – if there was an internal error.

Returns:

A serialized string containing the dataset.

power_grid_model.utils.json_deserialize_from_file(file_path: Path) Dict[str, ndarray] | Dict[str, ndarray | Dict[str, ndarray]]

Load and deserialize a JSON file to a new dataset.

Parameters:

file_path – the path to the file to load and deserialize.

Raises:
  • ValueError – if the data is inconsistent with the rest of the dataset or a component is unknown.

  • PowerGridError – if there was an internal error.

Returns:

The deserialized dataset in Power grid model input format.

power_grid_model.utils.json_serialize_to_file(file_path: Path, data: Dict[str, ndarray] | Dict[str, ndarray | Dict[str, ndarray]], dataset_type: str | None = None, use_compact_list: bool = False, indent: int | None = 2)

Export JSON data in most recent format.

Parameters:
  • file_path – the path to the file to load and deserialize.

  • data – a single or batch dataset for power-grid-model.

  • use_compact_list – write components on a single line.

  • indent – indent of the file. Defaults to 2.

Returns:

Save to file.

power_grid_model.utils.msgpack_deserialize_from_file(file_path: Path) Dict[str, ndarray] | Dict[str, ndarray | Dict[str, ndarray]]

Load and deserialize a msgpack file to a new dataset.

Parameters:

file_path – the path to the file to load and deserialize.

Raises:
  • ValueError – if the data is inconsistent with the rest of the dataset or a component is unknown.

  • PowerGridError – if there was an internal error.

Returns:

The deserialized dataset in Power grid model input format.

power_grid_model.utils.msgpack_serialize_to_file(file_path: Path, data: Dict[str, ndarray] | Dict[str, ndarray | Dict[str, ndarray]], dataset_type: str | None = None, use_compact_list: bool = False)

Export msgpack data in most recent format.

Parameters:
  • file_path – the path to the file to load and deserialize.

  • data – a single or batch dataset for power-grid-model.

  • use_compact_list – write components on a single line.

  • indent – indent of the file, default 2.

Returns:

Save to file.

power_grid_model.utils.import_json_data(json_file: Path, data_type: str, *args, **kwargs) Dict[str, ndarray] | Dict[str, ndarray | Dict[str, ndarray]]

[deprecated] Import json data.

This function is deprecated. Please use json_deserialize_from_file instead.

Parameters:
  • json_file – path to the json file.

  • data_type – type of data: input, update, sym_output, or asym_output.

  • [deprecated] – All extra positional and keyword arguments are ignored.

Returns:

A single or batch dataset for power-grid-model.

power_grid_model.utils.export_json_data(json_file: Path, data: Dict[str, ndarray] | Dict[str, ndarray | Dict[str, ndarray]], indent: int | None = 2, compact: bool = False, use_deprecated_format: bool = True)

[deprecated] Export json data in a deprecated serialization format.

WARNING: This function is deprecated. Please use json_serialize_to_file instead.

For backwards compatibility, this function outputs the deprecated serialization format by default. This feature may be removed in the future.

Parameters:
  • json_file – path to json file.

  • data – a single or batch dataset for power-grid-model.

  • indent – indent of the file, default 2.

  • compact – write components on a single line.

  • use_deprecated_format – use the old style format. Defaults to True for backwards compatibility.

Returns:

Save to file.

power_grid_model._utils.get_and_verify_batch_sizes(batch_data: Dict[str, ndarray | Dict[str, ndarray]]) int

Determine the number of batches for each component and verify that each component has the same number of batches

Parameters:

batch_data – a batch dataset for power-grid-model

Returns:

The number of batches

power_grid_model._utils.get_batch_size(batch_data: ndarray | Dict[str, ndarray]) int

Determine the number of batches and verify the data structure while we’re at it.

Parameters:

batch_data – a batch array for power-grid-model

Returns:

The number of batches