From 5d2f938f3b88f6672a5d2dee7c8c84da571c3ba4 Mon Sep 17 00:00:00 2001 From: Muhammad Yasirroni Date: Wed, 13 Nov 2024 15:03:32 +0700 Subject: [PATCH] support ignore mpc in comments and unparsable operation --- matpowercaseframes/reader.py | 10 +- notebooks/load_any_keys.ipynb | 468 ++++++++++++++++++++++++++++++++-- 2 files changed, 451 insertions(+), 27 deletions(-) diff --git a/matpowercaseframes/reader.py b/matpowercaseframes/reader.py index fd599ad..265f857 100644 --- a/matpowercaseframes/reader.py +++ b/matpowercaseframes/reader.py @@ -16,8 +16,9 @@ def find_name(string): def find_attributes(string): - pattern = "mpc\\.(?P.*?)\\s*=\\s*" - return re.findall(pattern, string, re.DOTALL) + pattern = "^\\s*mpc\\.(?P[^\\s=]*)" + match = re.findall(pattern, string, re.MULTILINE) + return match def parse_file(attribute, string): @@ -49,7 +50,10 @@ def search_file(attribute, string): # ["gen", "gencost", "bus", "branch", "dcline", "dclinecost"] or any keys pattern = r"mpc\.{}\s*=\s*\[[\n]?(?P.*?)[\n]?\];".format(attribute) - match = re.search(pattern, string, re.DOTALL) + try: + match = re.search(pattern, string, re.DOTALL) + except Exception: + return None if match is None: return None diff --git a/notebooks/load_any_keys.ipynb b/notebooks/load_any_keys.ipynb index 1df760f..ad2a9a3 100644 --- a/notebooks/load_any_keys.ipynb +++ b/notebooks/load_any_keys.ipynb @@ -47,28 +47,208 @@ " \n", " 0\n", " 1\n", - " 5\n", " 1\n", - " 90\n", - " 30\n", + " 1\n", + " 74.150480\n", + " 34.896181\n", " 1\n", " \n", " \n", " 1\n", " 2\n", - " 7\n", + " 8\n", " 1\n", - " 100\n", - " 35\n", + " 28.081420\n", + " 15.758833\n", " 1\n", " \n", " \n", " 2\n", " 3\n", + " 8\n", + " 1\n", + " 2.377862\n", + " 0.172607\n", + " 1\n", + " \n", + " \n", + " 3\n", + " 4\n", + " 10\n", + " 1\n", + " 17.485805\n", + " 11.688645\n", + " 1\n", + " \n", + " \n", + " 4\n", + " 5\n", + " 11\n", + " 1\n", + " 23.246418\n", + " 11.789581\n", + " 1\n", + " \n", + " \n", + " 5\n", + " 6\n", + " 7\n", + " 1\n", + " 17.744576\n", + " 0.808382\n", + " 1\n", + " \n", + " \n", + " 6\n", + " 7\n", + " 3\n", + " 1\n", + " 16.111230\n", + " 9.895656\n", + " 1\n", + " \n", + " \n", + " 7\n", + " 8\n", + " 5\n", + " 1\n", + " 2.892705\n", + " 1.954340\n", + " 1\n", + " \n", + " \n", + " 8\n", + " 9\n", + " 6\n", + " 1\n", + " 26.623944\n", + " 21.383458\n", + " 1\n", + " \n", + " \n", + " 9\n", + " 10\n", " 9\n", " 1\n", - " 125\n", - " 50\n", + " 9.696765\n", + " 6.885234\n", + " 1\n", + " \n", + " \n", + " 10\n", + " 11\n", + " 12\n", + " 1\n", + " 16.159454\n", + " 13.865387\n", + " 1\n", + " \n", + " \n", + " 11\n", + " 12\n", + " 13\n", + " 1\n", + " 54.008581\n", + " 40.773567\n", + " 1\n", + " \n", + " \n", + " 12\n", + " 13\n", + " 13\n", + " 1\n", + " 13.839229\n", + " 10.447872\n", + " 1\n", + " \n", + " \n", + " 13\n", + " 14\n", + " 3\n", + " 1\n", + " 5.831158\n", + " 2.685637\n", + " 1\n", + " \n", + " \n", + " 14\n", + " 15\n", + " 3\n", + " 1\n", + " 8.277018\n", + " 11.404585\n", + " 1\n", + " \n", + " \n", + " 15\n", + " 16\n", + " 27\n", + " 1\n", + " 18.487879\n", + " 12.280164\n", + " 1\n", + " \n", + " \n", + " 16\n", + " 17\n", + " 2\n", + " 1\n", + " 0.010161\n", + " 0.191424\n", + " 1\n", + " \n", + " \n", + " 17\n", + " 18\n", + " 14\n", + " 1\n", + " 23.393146\n", + " 25.758367\n", + " 1\n", + " \n", + " \n", + " 18\n", + " 19\n", + " 16\n", + " 1\n", + " 15.395352\n", + " 9.293864\n", + " 1\n", + " \n", + " \n", + " 19\n", + " 20\n", + " 27\n", + " 1\n", + " 23.810698\n", + " 9.011489\n", + " 1\n", + " \n", + " \n", + " 20\n", + " 21\n", + " 16\n", + " 1\n", + " 3.466689\n", + " 1.079331\n", + " 1\n", + " \n", + " \n", + " 21\n", + " 22\n", + " 14\n", + " 1\n", + " 0.000000\n", + " 0.000000\n", + " 1\n", + " \n", + " \n", + " 22\n", + " 23\n", + " 16\n", + " 1\n", + " 0.000000\n", + " 0.000000\n", " 1\n", " \n", " \n", @@ -76,10 +256,30 @@ "" ], "text/plain": [ - " 0 1 2 3 4 5\n", - "0 1 5 1 90 30 1\n", - "1 2 7 1 100 35 1\n", - "2 3 9 1 125 50 1" + " 0 1 2 3 4 5\n", + "0 1 1 1 74.150480 34.896181 1\n", + "1 2 8 1 28.081420 15.758833 1\n", + "2 3 8 1 2.377862 0.172607 1\n", + "3 4 10 1 17.485805 11.688645 1\n", + "4 5 11 1 23.246418 11.789581 1\n", + "5 6 7 1 17.744576 0.808382 1\n", + "6 7 3 1 16.111230 9.895656 1\n", + "7 8 5 1 2.892705 1.954340 1\n", + "8 9 6 1 26.623944 21.383458 1\n", + "9 10 9 1 9.696765 6.885234 1\n", + "10 11 12 1 16.159454 13.865387 1\n", + "11 12 13 1 54.008581 40.773567 1\n", + "12 13 13 1 13.839229 10.447872 1\n", + "13 14 3 1 5.831158 2.685637 1\n", + "14 15 3 1 8.277018 11.404585 1\n", + "15 16 27 1 18.487879 12.280164 1\n", + "16 17 2 1 0.010161 0.191424 1\n", + "17 18 14 1 23.393146 25.758367 1\n", + "18 19 16 1 15.395352 9.293864 1\n", + "19 20 27 1 23.810698 9.011489 1\n", + "20 21 16 1 3.466689 1.079331 1\n", + "21 22 14 1 0.000000 0.000000 1\n", + "22 23 16 1 0.000000 0.000000 1" ] }, "execution_count": 2, @@ -131,28 +331,208 @@ " \n", " 0\n", " 1\n", - " 5\n", " 1\n", - " 90\n", - " 30\n", + " 1\n", + " 74.150480\n", + " 34.896181\n", " 1\n", " \n", " \n", " 1\n", " 2\n", - " 7\n", + " 8\n", " 1\n", - " 100\n", - " 35\n", + " 28.081420\n", + " 15.758833\n", " 1\n", " \n", " \n", " 2\n", " 3\n", + " 8\n", + " 1\n", + " 2.377862\n", + " 0.172607\n", + " 1\n", + " \n", + " \n", + " 3\n", + " 4\n", + " 10\n", + " 1\n", + " 17.485805\n", + " 11.688645\n", + " 1\n", + " \n", + " \n", + " 4\n", + " 5\n", + " 11\n", + " 1\n", + " 23.246418\n", + " 11.789581\n", + " 1\n", + " \n", + " \n", + " 5\n", + " 6\n", + " 7\n", + " 1\n", + " 17.744576\n", + " 0.808382\n", + " 1\n", + " \n", + " \n", + " 6\n", + " 7\n", + " 3\n", + " 1\n", + " 16.111230\n", + " 9.895656\n", + " 1\n", + " \n", + " \n", + " 7\n", + " 8\n", + " 5\n", + " 1\n", + " 2.892705\n", + " 1.954340\n", + " 1\n", + " \n", + " \n", + " 8\n", " 9\n", + " 6\n", + " 1\n", + " 26.623944\n", + " 21.383458\n", + " 1\n", + " \n", + " \n", + " 9\n", + " 10\n", + " 9\n", + " 1\n", + " 9.696765\n", + " 6.885234\n", + " 1\n", + " \n", + " \n", + " 10\n", + " 11\n", + " 12\n", + " 1\n", + " 16.159454\n", + " 13.865387\n", + " 1\n", + " \n", + " \n", + " 11\n", + " 12\n", + " 13\n", " 1\n", - " 125\n", - " 50\n", + " 54.008581\n", + " 40.773567\n", + " 1\n", + " \n", + " \n", + " 12\n", + " 13\n", + " 13\n", + " 1\n", + " 13.839229\n", + " 10.447872\n", + " 1\n", + " \n", + " \n", + " 13\n", + " 14\n", + " 3\n", + " 1\n", + " 5.831158\n", + " 2.685637\n", + " 1\n", + " \n", + " \n", + " 14\n", + " 15\n", + " 3\n", + " 1\n", + " 8.277018\n", + " 11.404585\n", + " 1\n", + " \n", + " \n", + " 15\n", + " 16\n", + " 27\n", + " 1\n", + " 18.487879\n", + " 12.280164\n", + " 1\n", + " \n", + " \n", + " 16\n", + " 17\n", + " 2\n", + " 1\n", + " 0.010161\n", + " 0.191424\n", + " 1\n", + " \n", + " \n", + " 17\n", + " 18\n", + " 14\n", + " 1\n", + " 23.393146\n", + " 25.758367\n", + " 1\n", + " \n", + " \n", + " 18\n", + " 19\n", + " 16\n", + " 1\n", + " 15.395352\n", + " 9.293864\n", + " 1\n", + " \n", + " \n", + " 19\n", + " 20\n", + " 27\n", + " 1\n", + " 23.810698\n", + " 9.011489\n", + " 1\n", + " \n", + " \n", + " 20\n", + " 21\n", + " 16\n", + " 1\n", + " 3.466689\n", + " 1.079331\n", + " 1\n", + " \n", + " \n", + " 21\n", + " 22\n", + " 14\n", + " 1\n", + " 0.000000\n", + " 0.000000\n", + " 1\n", + " \n", + " \n", + " 22\n", + " 23\n", + " 16\n", + " 1\n", + " 0.000000\n", + " 0.000000\n", " 1\n", " \n", " \n", @@ -160,10 +540,30 @@ "" ], "text/plain": [ - " LD_ID LD_BUS LD_STAUTS LD_PD LD_QD LD_FACT\n", - "0 1 5 1 90 30 1\n", - "1 2 7 1 100 35 1\n", - "2 3 9 1 125 50 1" + " LD_ID LD_BUS LD_STAUTS LD_PD LD_QD LD_FACT\n", + "0 1 1 1 74.150480 34.896181 1\n", + "1 2 8 1 28.081420 15.758833 1\n", + "2 3 8 1 2.377862 0.172607 1\n", + "3 4 10 1 17.485805 11.688645 1\n", + "4 5 11 1 23.246418 11.789581 1\n", + "5 6 7 1 17.744576 0.808382 1\n", + "6 7 3 1 16.111230 9.895656 1\n", + "7 8 5 1 2.892705 1.954340 1\n", + "8 9 6 1 26.623944 21.383458 1\n", + "9 10 9 1 9.696765 6.885234 1\n", + "10 11 12 1 16.159454 13.865387 1\n", + "11 12 13 1 54.008581 40.773567 1\n", + "12 13 13 1 13.839229 10.447872 1\n", + "13 14 3 1 5.831158 2.685637 1\n", + "14 15 3 1 8.277018 11.404585 1\n", + "15 16 27 1 18.487879 12.280164 1\n", + "16 17 2 1 0.010161 0.191424 1\n", + "17 18 14 1 23.393146 25.758367 1\n", + "18 19 16 1 15.395352 9.293864 1\n", + "19 20 27 1 23.810698 9.011489 1\n", + "20 21 16 1 3.466689 1.079331 1\n", + "21 22 14 1 0.000000 0.000000 1\n", + "22 23 16 1 0.000000 0.000000 1" ] }, "execution_count": 3, @@ -180,6 +580,26 @@ "cf.load" ] }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['version', 'baseMVA', 'bus', 'load', 'gen', 'branch', 'gencost']" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "cf.attributes" + ] + }, { "cell_type": "code", "execution_count": null,