Skip to content

Commit 6afad01

Browse files
committed
Update the examples
Signed-off-by: GitHub Actions Bot <[email protected]>
1 parent 240cc44 commit 6afad01

10 files changed

+161
-95
lines changed

examples/Asymmetric Calculation Example.ipynb

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,16 @@
4848
"import numpy as np\n",
4949
"import pandas as pd\n",
5050
"\n",
51-
"from power_grid_model import LoadGenType, ComponentType, DatasetType\n",
52-
"from power_grid_model import PowerGridModel, CalculationMethod, CalculationType, MeasuredTerminalType\n",
53-
"from power_grid_model import initialize_array"
51+
"from power_grid_model import (\n",
52+
" CalculationMethod,\n",
53+
" CalculationType,\n",
54+
" ComponentType,\n",
55+
" DatasetType,\n",
56+
" LoadGenType,\n",
57+
" MeasuredTerminalType,\n",
58+
" PowerGridModel,\n",
59+
" initialize_array,\n",
60+
")"
5461
]
5562
},
5663
{
@@ -317,7 +324,7 @@
317324
"source": [
318325
"#### Accessing batch data\n",
319326
"\n",
320-
"It may be a bit unintuitive to read the `output_data` or `update_data` of a component directly as they are a dictionary of 4 dimension data, ie. $ids \\times batches \\times attributes \\times phases$. Remember that the `output_data` or `update_data` are a dictionary of numpy structured arrays. Hence the component should be indexed first. The index that follows can be [indexed with numpy structured arrays](https://numpy.org/doc/stable/user/basics.rec.html#indexing-structured-arrays).\n",
327+
"It may be a bit unintuitive to read the `output_data` or `update_data` of a component directly as they are a dictionary of 4 dimension data, i.e., $ids \\times batches \\times attributes \\times phases$. Remember that the `output_data` or `update_data` are a dictionary of numpy structured arrays. Hence the component should be indexed first. The index that follows can be [indexed with numpy structured arrays](https://numpy.org/doc/stable/user/basics.rec.html#indexing-structured-arrays).\n",
321328
"\n",
322329
"To read the result of a single batch, e.g. 1st batch,"
323330
]

examples/Asymmetric Line.ipynb

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,16 @@
4040
"import numpy as np\n",
4141
"import pandas as pd\n",
4242
"\n",
43-
"from power_grid_model import LoadGenType, DatasetType, ComponentType\n",
44-
"from power_grid_model import PowerGridModel, CalculationMethod, CalculationType, MeasuredTerminalType\n",
45-
"from power_grid_model import initialize_array"
43+
"from power_grid_model import (\n",
44+
" CalculationMethod,\n",
45+
" CalculationType,\n",
46+
" ComponentType,\n",
47+
" DatasetType,\n",
48+
" LoadGenType,\n",
49+
" MeasuredTerminalType,\n",
50+
" PowerGridModel,\n",
51+
" initialize_array,\n",
52+
")"
4653
]
4754
},
4855
{
@@ -329,7 +336,7 @@
329336
"name": "python",
330337
"nbconvert_exporter": "python",
331338
"pygments_lexer": "ipython3",
332-
"version": "3.12.0"
339+
"version": "3.13.3"
333340
}
334341
},
335342
"nbformat": 4,

examples/Generic Branch Example.ipynb

Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,6 @@
129129
}
130130
],
131131
"source": [
132-
"import numpy as np\n",
133132
"import warnings\n",
134133
"\n",
135134
"with warnings.catch_warnings():\n",
@@ -138,9 +137,15 @@
138137
"\n",
139138
"import pandas as pd\n",
140139
"\n",
141-
"from power_grid_model import LoadGenType, ComponentType, DatasetType\n",
142-
"from power_grid_model import PowerGridModel, CalculationMethod, CalculationType\n",
143-
"from power_grid_model import initialize_array\n",
140+
"from power_grid_model import (\n",
141+
" CalculationMethod,\n",
142+
" CalculationType,\n",
143+
" ComponentType,\n",
144+
" DatasetType,\n",
145+
" LoadGenType,\n",
146+
" PowerGridModel,\n",
147+
" initialize_array,\n",
148+
")\n",
144149
"from power_grid_model.validation import assert_valid_input_data\n",
145150
"\n",
146151
"# network\n",
@@ -242,9 +247,17 @@
242247
" symmetric=True, error_tolerance=1e-8, max_iterations=20, calculation_method=CalculationMethod.newton_raphson\n",
243248
")\n",
244249
"\n",
245-
"e3 = lambda x: x / 1e3\n",
246-
"e6 = lambda x: x / 1e6\n",
247-
"percent = lambda x: x * 100\n",
250+
"\n",
251+
"def e3(x):\n",
252+
" return x / 1e3\n",
253+
"\n",
254+
"\n",
255+
"def e6(x):\n",
256+
" return x / 1e6\n",
257+
"\n",
258+
"\n",
259+
"def percent(x):\n",
260+
" return x * 100\n",
248261
"\n",
249262
"\n",
250263
"def print_node_input(input_data):\n",
@@ -519,18 +532,22 @@
519532
],
520533
"source": [
521534
"# some basic imports\n",
522-
"import numpy as np\n",
523535
"import warnings\n",
524536
"\n",
525537
"with warnings.catch_warnings():\n",
526538
" warnings.simplefilter(\"ignore\", category=DeprecationWarning)\n",
527539
" # Suppress warning about pyarrow as future required dependency\n",
528540
"\n",
529-
"import pandas as pd\n",
530541
"\n",
531-
"from power_grid_model import LoadGenType, ComponentType, DatasetType\n",
532-
"from power_grid_model import PowerGridModel, CalculationMethod, CalculationType\n",
533-
"from power_grid_model import initialize_array\n",
542+
"from power_grid_model import (\n",
543+
" CalculationMethod,\n",
544+
" CalculationType,\n",
545+
" ComponentType,\n",
546+
" DatasetType,\n",
547+
" LoadGenType,\n",
548+
" PowerGridModel,\n",
549+
" initialize_array,\n",
550+
")\n",
534551
"from power_grid_model.validation import assert_valid_input_data\n",
535552
"\n",
536553
"# network\n",
@@ -621,9 +638,17 @@
621638
"pd.set_option(\"display.max_columns\", None)\n",
622639
"pd.set_option(\"display.max_colwidth\", None)\n",
623640
"\n",
624-
"e3 = lambda x: x / 1e3\n",
625-
"e6 = lambda x: x / 1e6\n",
626-
"percent = lambda x: x * 100\n",
641+
"\n",
642+
"def e3(x):\n",
643+
" return x / 1e3\n",
644+
"\n",
645+
"\n",
646+
"def e6(x):\n",
647+
" return x / 1e6\n",
648+
"\n",
649+
"\n",
650+
"def percent(x):\n",
651+
" return x * 100\n",
627652
"\n",
628653
"\n",
629654
"def print_node_input(input_data):\n",
@@ -1039,7 +1064,7 @@
10391064
"name": "python",
10401065
"nbconvert_exporter": "python",
10411066
"pygments_lexer": "ipython3",
1042-
"version": "3.13.0"
1067+
"version": "3.13.3"
10431068
}
10441069
},
10451070
"nbformat": 4,

examples/Make Test Dataset.ipynb

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -163,9 +163,7 @@
163163
"import numpy as np\n",
164164
"import pandas as pd\n",
165165
"\n",
166-
"from power_grid_model import LoadGenType, ComponentType, DatasetType\n",
167-
"from power_grid_model import PowerGridModel\n",
168-
"from power_grid_model import initialize_array\n",
166+
"from power_grid_model import ComponentType, DatasetType, LoadGenType, PowerGridModel, initialize_array\n",
169167
"\n",
170168
"# network\n",
171169
"\n",
@@ -229,10 +227,11 @@
229227
"metadata": {},
230228
"outputs": [],
231229
"source": [
232-
"from power_grid_model.utils import json_serialize_to_file\n",
233230
"import tempfile\n",
234231
"from pathlib import Path\n",
235232
"\n",
233+
"from power_grid_model.utils import json_serialize_to_file\n",
234+
"\n",
236235
"temp_path = Path(tempfile.gettempdir())\n",
237236
"json_serialize_to_file(temp_path / \"input.json\", input_data)"
238237
]

examples/Power Flow Example.ipynb

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,17 @@
4747
"import numpy as np\n",
4848
"import pandas as pd\n",
4949
"\n",
50-
"from power_grid_model import LoadGenType, ComponentType, DatasetType, ComponentAttributeFilterOptions\n",
51-
"from power_grid_model import PowerGridModel, CalculationMethod, CalculationType\n",
52-
"from power_grid_model import initialize_array, attribute_dtype"
50+
"from power_grid_model import (\n",
51+
" CalculationMethod,\n",
52+
" CalculationType,\n",
53+
" ComponentAttributeFilterOptions,\n",
54+
" ComponentType,\n",
55+
" DatasetType,\n",
56+
" LoadGenType,\n",
57+
" PowerGridModel,\n",
58+
" attribute_dtype,\n",
59+
" initialize_array,\n",
60+
")"
5361
]
5462
},
5563
{
@@ -464,9 +472,12 @@
464472
" max_iterations=20,\n",
465473
" calculation_method=CalculationMethod.newton_raphson,\n",
466474
" output_component_types={\n",
467-
" ComponentType.node: None, # node output as row-based\n",
468-
" ComponentType.line: [\"id\", \"p_from\"], # line output columns id and p_from\n",
469-
" ComponentType.sym_load: ComponentAttributeFilterOptions.everything, # all sym_load attributes as columns\n",
475+
" # node output as row-based\n",
476+
" ComponentType.node: None,\n",
477+
" # line output columns id and p_from\n",
478+
" ComponentType.line: [\"id\", \"p_from\"],\n",
479+
" # all sym_load attributes as columns\n",
480+
" ComponentType.sym_load: ComponentAttributeFilterOptions.everything,\n",
470481
" },\n",
471482
")\n",
472483
"\n",
@@ -515,7 +526,8 @@
515526
" error_tolerance=1e-8,\n",
516527
" max_iterations=20,\n",
517528
" calculation_method=CalculationMethod.newton_raphson,\n",
518-
" output_component_types=ComponentAttributeFilterOptions.everything, # all attributes for all component types as columns\n",
529+
" # all attributes for all component types as columns\n",
530+
" output_component_types=ComponentAttributeFilterOptions.everything,\n",
519531
")\n",
520532
"\n",
521533
"print(\"List of component types in result dataset\")\n",
@@ -934,7 +946,7 @@
934946
"source": [
935947
"##### Accessing batch data\n",
936948
"\n",
937-
"It may be a bit unintuitive to read the `output_data` or `update_data` of a component directly as they are a dictionary of 3 dimension data, ie. $ids \\times batches \\times attributes$. Remember that the `output_data` or `update_data` are a dictionary of numpy structured arrays. Hence the component should be indexed first. The index that follows can be [indexed with numpy structured arrays](https://numpy.org/doc/stable/user/basics.rec.html#indexing-structured-arrays)\n",
949+
"It may be a bit unintuitive to read the `output_data` or `update_data` of a component directly as they are a dictionary of 3 dimension data, i.e., $ids \\times batches \\times attributes$. Remember that the `output_data` or `update_data` are a dictionary of numpy structured arrays. Hence the component should be indexed first. The index that follows can be [indexed with numpy structured arrays](https://numpy.org/doc/stable/user/basics.rec.html#indexing-structured-arrays)\n",
938950
"\n",
939951
"To read the result of a single batch, lets say 1st batch,"
940952
]
@@ -1216,7 +1228,7 @@
12161228
}
12171229
],
12181230
"source": [
1219-
"from power_grid_model.errors import PowerGridError, ConflictVoltage\n",
1231+
"from power_grid_model.errors import ConflictVoltage, PowerGridError\n",
12201232
"\n",
12211233
"# node\n",
12221234
"node_error = initialize_array(DatasetType.input, ComponentType.node, 2)\n",
@@ -1372,7 +1384,7 @@
13721384
"\n",
13731385
"Failed scenarios: [3 7]\n",
13741386
"Succeeded scenarios: [0 1 2 4 5 6 8 9]\n",
1375-
"Error messages: ['The id cannot be found: 1000\\n', 'Iteration failed to converge after 20 iterations! Max deviation: 5.81174e+85, error tolerance: 1e-08.\\n']\n"
1387+
"Error messages: ['The id cannot be found: 1000\\n', 'Sparse matrix error, possibly singular matrix!\\nIf you get this error from state estimation, it might mean the system is not fully observable, i.e. not enough measurements.\\nIt might also mean that you are running into a corner case where PGM cannot resolve yet.See https://github.com/PowerGridModel/power-grid-model/issues/864.']\n"
13761388
]
13771389
}
13781390
],
@@ -1403,16 +1415,16 @@
14031415
"output_type": "stream",
14041416
"text": [
14051417
"Node data with invalid results\n",
1406-
"[[9.99401170e-001 9.92685785e-001 9.94521366e-001]\n",
1407-
" [9.99347687e-001 9.86226389e-001 9.89352855e-001]\n",
1408-
" [9.99288384e-001 9.79654011e-001 9.84095542e-001]\n",
1409-
" [3.94357132e+180 2.87518198e+161 2.04418455e+214]\n",
1410-
" [9.99151380e-001 9.66149483e-001 9.73298790e-001]\n",
1411-
" [9.99073166e-001 9.59205860e-001 9.67750710e-001]\n",
1412-
" [9.98988099e-001 9.52126208e-001 9.62096474e-001]\n",
1413-
" [0.00000000e+000 0.00000000e+000 0.00000000e+000]\n",
1414-
" [9.98796126e-001 9.37530046e-001 9.50447962e-001]\n",
1415-
" [9.98688504e-001 9.29997471e-001 9.44441670e-001]]\n",
1418+
"[[ 9.99401170e-001 9.92685785e-001 9.94521366e-001]\n",
1419+
" [ 9.99347687e-001 9.86226389e-001 9.89352855e-001]\n",
1420+
" [ 9.99288384e-001 9.79654011e-001 9.84095542e-001]\n",
1421+
" [-2.66881060e+116 2.33997016e-302 6.70346672e-198]\n",
1422+
" [ 9.99151380e-001 9.66149483e-001 9.73298790e-001]\n",
1423+
" [ 9.99073166e-001 9.59205860e-001 9.67750710e-001]\n",
1424+
" [ 9.98988099e-001 9.52126208e-001 9.62096474e-001]\n",
1425+
" [-2.44756775e+092 5.35663612e-256 1.91838796e-203]\n",
1426+
" [ 9.98796126e-001 9.37530046e-001 9.50447962e-001]\n",
1427+
" [ 9.98688504e-001 9.29997471e-001 9.44441670e-001]]\n",
14161428
"Node data with only valid results\n",
14171429
"[[0.99940117 0.99268579 0.99452137]\n",
14181430
" [0.99934769 0.98622639 0.98935286]\n",
@@ -1455,7 +1467,7 @@
14551467
],
14561468
"metadata": {
14571469
"kernelspec": {
1458-
"display_name": ".venv",
1470+
"display_name": "venv",
14591471
"language": "python",
14601472
"name": "python3"
14611473
},
@@ -1469,7 +1481,7 @@
14691481
"name": "python",
14701482
"nbconvert_exporter": "python",
14711483
"pygments_lexer": "ipython3",
1472-
"version": "3.13.0"
1484+
"version": "3.13.3"
14731485
}
14741486
},
14751487
"nbformat": 4,

examples/Serialization Example.ipynb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@
2323
"outputs": [],
2424
"source": [
2525
"import json\n",
26-
"from pandas import DataFrame\n",
2726
"import pprint\n",
2827
"\n",
29-
"from power_grid_model import PowerGridModel, ComponentType, ComponentAttributeFilterOptions\n",
28+
"from pandas import DataFrame\n",
29+
"\n",
30+
"from power_grid_model import ComponentAttributeFilterOptions, ComponentType, PowerGridModel\n",
3031
"from power_grid_model.utils import json_deserialize, json_serialize"
3132
]
3233
},

examples/Short Circuit Example.ipynb

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,18 +48,19 @@
4848
"source": [
4949
"# some basic imports\n",
5050
"import numpy as np\n",
51-
"import pandas as pd\n",
5251
"\n",
53-
"from power_grid_model import LoadGenType, ComponentType, DatasetType\n",
5452
"from power_grid_model import (\n",
55-
" PowerGridModel,\n",
5653
" CalculationMethod,\n",
5754
" CalculationType,\n",
58-
" FaultType,\n",
55+
" ComponentType,\n",
56+
" DatasetType,\n",
5957
" FaultPhase,\n",
58+
" FaultType,\n",
59+
" LoadGenType,\n",
60+
" PowerGridModel,\n",
6061
" ShortCircuitVoltageScaling,\n",
61-
")\n",
62-
"from power_grid_model import initialize_array"
62+
" initialize_array,\n",
63+
")"
6364
]
6465
},
6566
{
@@ -337,7 +338,7 @@
337338
"name": "python",
338339
"nbconvert_exporter": "python",
339340
"pygments_lexer": "ipython3",
340-
"version": "3.13.0"
341+
"version": "3.13.3"
341342
}
342343
},
343344
"nbformat": 4,

0 commit comments

Comments
 (0)