PGM JSON Converter

The PGM JSON Converter, converts to and from the native power-grid-model JSON data format, but with the addition of handling extra info; information about the components, stored in the json data, but not used for the calculations. This json format makes it convenient to evaluate the grid data in perspective of input to power-grid-model. More details about the JSON format are mentioned in power_grid_model.utils and an example of it is in Make Test Dataset in power-grid-model repository.

1. Load the JSON data

Define source and destination paths:

source_file = "data/tiny-net/input.json"
destination_file = "data/tiny-net/sym_output.json"

For debugging purposes, let’s check the JSON input:

from pathlib import Path
from IPython.display import display, Markdown

with Path(source_file).open() as json_file:
    display(Markdown(f"<pre style='max-height: 160px; white-space: pre'>{json_file.read()}</div>"))
{
  "node":
    [
      {"id": 1, "u_rated": 10500.0, "name": "First Node"},
      {"id": 2, "u_rated": 10500.0, "name": "Second Node"},
      {"id": 3, "u_rated": 10500.0, "name": "Third Node"}
    ],
  "line":
    [
      {"id": 4, "from_node": 1, "to_node": 2, "from_status": 1, "to_status": 1, "r1": 0.11, "x1": 0.12, "c1": 4.1380285203892784e-05, "tan1": 0.1076923076923077, "i_n": 510.0},
      {"id": 5, "from_node": 2, "to_node": 3, "from_status": 1, "to_status": 1, "r1": 0.15, "x1": 0.16, "c1": 5.411268065124442e-05, "tan1": 0.10588235294117646, "i_n": 520.0}
    ],
  "source":
    [
      {"id": 6, "node": 1, "status": 1, "u_ref": 1.01047619047619, "sk": 200000000.0}
    ],
  "sym_load":
    [
      {"id": 7, "node": 2, "status": 1, "type": 4},
      {"id": 8, "node": 3, "status": 1, "type": 4}
    ],
  "sym_voltage_sensor":
    [
      {"id": 101, "measured_object": 1, "u_sigma": 105.0, "u_measured": 10751.072595758282, "u_angle_measured": -0.013054638926306409},
      {"id": 201, "measured_object": 2, "u_sigma": 105.0, "u_measured": 10752.698591183394, "u_angle_measured": -0.01763734945972652},
      {"id": 301, "measured_object": 3, "u_sigma": 100.0, "u_measured": 10748.320749959701, "u_angle_measured": -0.02018233075947404}
    ],
  "sym_power_sensor":
    [
      {"id": 401, "measured_object": 4, "measured_terminal_type": 0, "power_sigma": 37916.0, "p_measured": 2412359.2976399013, "q_measured": -3024028.886598367},
      {"id": 402, "measured_object": 4, "measured_terminal_type": 1, "power_sigma": 1000000000000000.0, "p_measured": -2000000.0, "q_measured": 1000000.0},
      {"id": 501, "measured_object": 5, "measured_terminal_type": 0, "power_sigma": 20878.0, "p_measured": 1230426.390004009, "q_measured": -1742195.1033582848},
      {"id": 502, "measured_object": 5, "measured_terminal_type": 1, "power_sigma": 10435.0, "p_measured": -1019999.9999999485, "q_measured": -219999.99999999927},
      {"id": 601, "measured_object": 6, "measured_terminal_type": 2, "power_sigma": 38009.0, "p_measured": 2412359.297639887, "q_measured": -3024028.8865982923},
      {"id": 701, "measured_object": 7, "measured_terminal_type": 4, "power_sigma": 10316.0, "p_measured": 1010000.0, "q_measured": 210000.0},
      {"id": 801, "measured_object": 8, "measured_terminal_type": 4, "power_sigma": 10435.0, "p_measured": 1020000.0, "q_measured": 220000.0}
    ]
}

Instantiate the converter, optionally with source and destination file paths. Then use load_input_data() to load the data and convert it to power-grid-model data. Additional information is stored in extra_info.

%%capture cap --no-stderr
from power_grid_model_io.converters import PgmJsonConverter

converter = PgmJsonConverter(source_file=source_file, destination_file=destination_file)
input_data, extra_info = converter.load_input_data()

Let’s investigate the data we have converted, for one of the components: nodes

import pandas as pd

# The node data is stored as a numpy structured array in input_data["node"]
display(input_data["node"])

# We can use pandas to display the data in a convenient tabular format
display(pd.DataFrame(input_data["node"]))

# Notice that the node names were not stored in the numpy array, as we don't need them for the calculations
display({i: extra_info[i] for i in input_data["node"]["id"]})
array([(1, 10500.), (2, 10500.), (3, 10500.)],
      dtype={'names': ['id', 'u_rated'], 'formats': ['<i4', '<f8'], 'offsets': [0, 8], 'itemsize': 16, 'aligned': True})
id u_rated
0 1 10500.0
1 2 10500.0
2 3 10500.0
{1: {'name': 'First Node'},
 2: {'name': 'Second Node'},
 3: {'name': 'Third Node'}}