|
21 | 21 | }, |
22 | 22 | { |
23 | 23 | "cell_type": "code", |
24 | | - "execution_count": 15, |
| 24 | + "execution_count": 1, |
25 | 25 | "metadata": {}, |
26 | 26 | "outputs": [], |
27 | 27 | "source": [ |
|
44 | 44 | "import numpy as np" |
45 | 45 | ] |
46 | 46 | }, |
47 | | - { |
48 | | - "cell_type": "code", |
49 | | - "execution_count": 16, |
50 | | - "metadata": {}, |
51 | | - "outputs": [], |
52 | | - "source": [ |
53 | | - "# A constant showing error message\n", |
54 | | - "ZERO_COPY_ERROR_MSG = \"Zero-copy conversion requested, but the data types do not match.\"" |
55 | | - ] |
56 | | - }, |
57 | 47 | { |
58 | 48 | "cell_type": "markdown", |
59 | 49 | "metadata": {}, |
|
93 | 83 | }, |
94 | 84 | { |
95 | 85 | "cell_type": "code", |
96 | | - "execution_count": 17, |
| 86 | + "execution_count": 2, |
97 | 87 | "metadata": {}, |
98 | 88 | "outputs": [ |
99 | 89 | { |
|
142 | 132 | }, |
143 | 133 | { |
144 | 134 | "cell_type": "code", |
145 | | - "execution_count": 18, |
| 135 | + "execution_count": 3, |
146 | 136 | "metadata": {}, |
147 | 137 | "outputs": [ |
148 | 138 | { |
149 | 139 | "name": "stdout", |
150 | 140 | "output_type": "stream", |
151 | 141 | "text": [ |
152 | | - "-------node combined asym scehma-------\n", |
| 142 | + "-------node asym scehma-------\n", |
153 | 143 | "id: int32\n", |
154 | 144 | "u_rated: double\n", |
155 | | - "-------asym load combined asym scehma-------\n", |
| 145 | + "-------asym load scehma-------\n", |
156 | 146 | "id: int32\n", |
157 | 147 | "node: int32\n", |
158 | 148 | "status: int8\n", |
|
179 | 169 | " return pa.schema(schemas)\n", |
180 | 170 | "\n", |
181 | 171 | "\n", |
182 | | - "print(\"-------node combined asym scehma-------\")\n", |
| 172 | + "print(\"-------node asym scehma-------\")\n", |
183 | 173 | "print(pgm_schema(DatasetType.input, ComponentType.node))\n", |
184 | | - "print(\"-------asym load combined asym scehma-------\")\n", |
| 174 | + "print(\"-------asym load scehma-------\")\n", |
185 | 175 | "print(pgm_schema(DatasetType.input, ComponentType.asym_load))" |
186 | 176 | ] |
187 | 177 | }, |
|
198 | 188 | }, |
199 | 189 | { |
200 | 190 | "cell_type": "code", |
201 | | - "execution_count": 19, |
| 191 | + "execution_count": 4, |
202 | 192 | "metadata": {}, |
203 | 193 | "outputs": [ |
204 | 194 | { |
|
212 | 202 | "u_rated: [10500,10500,10500]" |
213 | 203 | ] |
214 | 204 | }, |
215 | | - "execution_count": 19, |
| 205 | + "execution_count": 4, |
216 | 206 | "metadata": {}, |
217 | 207 | "output_type": "execute_result" |
218 | 208 | } |
|
272 | 262 | "Similar approach be adopted by the user to convert to row based data.\n", |
273 | 263 | "\n", |
274 | 264 | "```{note}\n", |
275 | | - "The option of `zero_copy_only` in the function below is added in this demo to verify no copies are made. Its usage is not mandatory to do zero copy conversion.\n", |
| 265 | + "The option of `zero_copy_only` in the function below and assert for correct dtype is added in this demo to verify no copies are made. \n", |
| 266 | + "Its usage is not mandatory to do zero copy conversion.\n", |
276 | 267 | "```" |
277 | 268 | ] |
278 | 269 | }, |
279 | 270 | { |
280 | 271 | "cell_type": "code", |
281 | | - "execution_count": 20, |
| 272 | + "execution_count": 5, |
282 | 273 | "metadata": {}, |
283 | 274 | "outputs": [ |
284 | 275 | { |
|
287 | 278 | "{'id': array([1, 2, 3]), 'u_rated': array([10500., 10500., 10500.])}" |
288 | 279 | ] |
289 | 280 | }, |
290 | | - "execution_count": 20, |
| 281 | + "execution_count": 5, |
291 | 282 | "metadata": {}, |
292 | 283 | "output_type": "execute_result" |
293 | 284 | } |
294 | 285 | ], |
295 | 286 | "source": [ |
296 | | - "def arrow_to_numpy(\n", |
297 | | - " data: pa.RecordBatch, dataset_type: DatasetType, component_type: ComponentType, zero_copy_only: bool = False\n", |
298 | | - ") -> np.ndarray:\n", |
| 287 | + "def arrow_to_numpy(data: pa.RecordBatch, dataset_type: DatasetType, component_type: ComponentType) -> np.ndarray:\n", |
299 | 288 | " \"\"\"Convert Arrow data to NumPy data.\"\"\"\n", |
300 | 289 | " result = {}\n", |
301 | 290 | " result_dtype = power_grid_meta_data[dataset_type][component_type].dtype\n", |
302 | 291 | " for name, column in zip(data.column_names, data.columns):\n", |
303 | | - " column_data = column.to_numpy(zero_copy_only=zero_copy_only)\n", |
304 | | - " if zero_copy_only and column_data.dtype != result_dtype[name]:\n", |
305 | | - " raise ValueError(ZERO_COPY_ERROR_MSG)\n", |
| 292 | + " column_data = column.to_numpy(zero_copy_only=True)\n", |
| 293 | + " assert column_data.dtype == result_dtype[name]\n", |
306 | 294 | " result[name] = column_data.astype(dtype=result_dtype[name], copy=False)\n", |
307 | 295 | " return result\n", |
308 | 296 | "\n", |
309 | 297 | "\n", |
310 | | - "node_input = arrow_to_numpy(nodes, DatasetType.input, ComponentType.node, zero_copy_only=True)\n", |
| 298 | + "node_input = arrow_to_numpy(nodes, DatasetType.input, ComponentType.node)\n", |
311 | 299 | "line_input = arrow_to_numpy(lines, DatasetType.input, ComponentType.line)\n", |
312 | 300 | "source_input = arrow_to_numpy(sources, DatasetType.input, ComponentType.source)\n", |
313 | 301 | "sym_load_input = arrow_to_numpy(sym_loads, DatasetType.input, ComponentType.sym_load)\n", |
|
324 | 312 | }, |
325 | 313 | { |
326 | 314 | "cell_type": "code", |
327 | | - "execution_count": 21, |
| 315 | + "execution_count": 6, |
328 | 316 | "metadata": {}, |
329 | 317 | "outputs": [], |
330 | 318 | "source": [ |
|
338 | 326 | }, |
339 | 327 | { |
340 | 328 | "cell_type": "code", |
341 | | - "execution_count": 22, |
| 329 | + "execution_count": 7, |
342 | 330 | "metadata": {}, |
343 | 331 | "outputs": [], |
344 | 332 | "source": [ |
|
361 | 349 | }, |
362 | 350 | { |
363 | 351 | "cell_type": "code", |
364 | | - "execution_count": 23, |
| 352 | + "execution_count": null, |
365 | 353 | "metadata": {}, |
366 | 354 | "outputs": [ |
367 | 355 | { |
|
473 | 461 | }, |
474 | 462 | { |
475 | 463 | "cell_type": "code", |
476 | | - "execution_count": 24, |
| 464 | + "execution_count": 9, |
477 | 465 | "metadata": {}, |
478 | 466 | "outputs": [ |
479 | 467 | { |
|
497 | 485 | "q: [-3299418.661306348,-0.5000000701801947,-1.4999998507078594]" |
498 | 486 | ] |
499 | 487 | }, |
500 | | - "execution_count": 24, |
| 488 | + "execution_count": 9, |
501 | 489 | "metadata": {}, |
502 | 490 | "output_type": "execute_result" |
503 | 491 | } |
|
536 | 524 | }, |
537 | 525 | { |
538 | 526 | "cell_type": "code", |
539 | | - "execution_count": 25, |
| 527 | + "execution_count": 10, |
540 | 528 | "metadata": {}, |
541 | 529 | "outputs": [ |
542 | 530 | { |
|
560 | 548 | "q_specified: [[0.5,1500,0.1],[1.5,2.5,1500]]" |
561 | 549 | ] |
562 | 550 | }, |
563 | | - "execution_count": 25, |
| 551 | + "execution_count": 10, |
564 | 552 | "metadata": {}, |
565 | 553 | "output_type": "execute_result" |
566 | 554 | } |
|
584 | 572 | }, |
585 | 573 | { |
586 | 574 | "cell_type": "code", |
587 | | - "execution_count": 26, |
| 575 | + "execution_count": 11, |
588 | 576 | "metadata": {}, |
589 | 577 | "outputs": [ |
590 | 578 | { |
|
600 | 588 | " [1.5e+00, 2.5e+00, 1.5e+03]])}" |
601 | 589 | ] |
602 | 590 | }, |
603 | | - "execution_count": 26, |
| 591 | + "execution_count": 11, |
604 | 592 | "metadata": {}, |
605 | 593 | "output_type": "execute_result" |
606 | 594 | } |
607 | 595 | ], |
608 | 596 | "source": [ |
609 | | - "def arrow_to_numpy_asym(\n", |
610 | | - " data: pa.RecordBatch, dataset_type: DatasetType, component_type: ComponentType, zero_copy_only: bool = False\n", |
611 | | - ") -> np.ndarray:\n", |
| 597 | + "def arrow_to_numpy_asym(data: pa.RecordBatch, dataset_type: DatasetType, component_type: ComponentType) -> np.ndarray:\n", |
612 | 598 | " \"\"\"Convert asymmetric Arrow data to NumPy data.\n", |
613 | 599 | "\n", |
614 | 600 | " This function is similar to the arrow_to_numpy function, but also supports asymmetric data.\"\"\"\n", |
|
621 | 607 | " dtype = result_dtype[name]\n", |
622 | 608 | "\n", |
623 | 609 | " if len(dtype.shape) == 0:\n", |
624 | | - " column_data = data.column(name).to_numpy(zero_copy_only=zero_copy_only)\n", |
| 610 | + " column_data = data.column(name).to_numpy(zero_copy_only=True)\n", |
625 | 611 | " else:\n", |
626 | | - " column_data = data.column(name).flatten().to_numpy(zero_copy_only=zero_copy_only).reshape(-1, 3)\n", |
627 | | - "\n", |
628 | | - " if zero_copy_only and column_data.dtype.base != dtype.base:\n", |
629 | | - " raise ValueError(ZERO_COPY_ERROR_MSG)\n", |
| 612 | + " column_data = data.column(name).flatten().to_numpy(zero_copy_only=True).reshape(-1, 3)\n", |
| 613 | + " assert column_data.dtype.base == dtype.base\n", |
630 | 614 | " result[name] = column_data.astype(dtype=dtype.base, copy=False)\n", |
631 | 615 | " return result\n", |
632 | 616 | "\n", |
633 | 617 | "\n", |
634 | | - "asym_load_input = arrow_to_numpy_asym(asym_loads, DatasetType.input, ComponentType.asym_load, zero_copy_only=True)\n", |
| 618 | + "asym_load_input = arrow_to_numpy_asym(asym_loads, DatasetType.input, ComponentType.asym_load)\n", |
635 | 619 | "\n", |
636 | 620 | "asym_load_input" |
637 | 621 | ] |
|
645 | 629 | }, |
646 | 630 | { |
647 | 631 | "cell_type": "code", |
648 | | - "execution_count": 27, |
| 632 | + "execution_count": 12, |
649 | 633 | "metadata": {}, |
650 | 634 | "outputs": [ |
651 | 635 | { |
|
704 | 688 | "2 -0.004338 -2.098733 2.090057" |
705 | 689 | ] |
706 | 690 | }, |
707 | | - "execution_count": 27, |
708 | 691 | "metadata": {}, |
709 | | - "output_type": "execute_result" |
| 692 | + "output_type": "display_data" |
710 | 693 | } |
711 | 694 | ], |
712 | 695 | "source": [ |
|
728 | 711 | ")\n", |
729 | 712 | "\n", |
730 | 713 | "# use pandas to display the results, but beware the data types\n", |
731 | | - "pd.DataFrame(asym_result[ComponentType.node][\"u_angle\"])" |
| 714 | + "display(pd.DataFrame(asym_result[ComponentType.node][\"u_angle\"]))" |
732 | 715 | ] |
733 | 716 | }, |
734 | 717 | { |
|
740 | 723 | }, |
741 | 724 | { |
742 | 725 | "cell_type": "code", |
743 | | - "execution_count": 28, |
| 726 | + "execution_count": 13, |
744 | 727 | "metadata": {}, |
745 | 728 | "outputs": [ |
746 | 729 | { |
|
769 | 752 | "q: [[-1099806.4185888197,-1098301.0302391076,-1098302.79423175],[-0.499999998516201,-1499.9999999095232,-0.10000001915949493],[-1.5000000216889147,-2.50000006806065,-1500.0000000385737]]" |
770 | 753 | ] |
771 | 754 | }, |
772 | | - "execution_count": 28, |
| 755 | + "execution_count": 13, |
773 | 756 | "metadata": {}, |
774 | 757 | "output_type": "execute_result" |
775 | 758 | } |
|
0 commit comments