matpowercaseframes


Namematpowercaseframes JSON
Version 1.1.1 PyPI version JSON
download
home_pageNone
SummaryParse MATPOWER case into pandas DataFrame.
upload_time2025-01-21 00:50:46
maintainerNone
docs_urlNone
authorNone
requires_python>=3.7.1
licenseMIT License
keywords matpower parser
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # MATPOWER Case Frames

[![PyPI version](https://badge.fury.io/py/matpowercaseframes.svg)](https://pypi.org/project/matpowercaseframes/)

Parse MATPOWER case into pandas DataFrame.

Unlike the [tutorial](https://github.com/yasirroni/matpower-pip#extra-require-oct2py-or-matlabengine) on [`matpower-pip`](https://github.com/yasirroni/matpower-pip), this package supports parsing MATPOWER case using `re` instead of `Oct2Py` and Octave. After that, you can further parse the data into any format supported by your solver.

## Installation

```plaintext
pip install matpowercaseframes
```

## Usage

### Read MATPOWER case by parsing file as string

The main utility of `matpowercaseframes` is to help read `matpower` data in user-friendly format as follows,

```python
from matpowercaseframes import CaseFrames

case_path = 'case9.m'
cf = CaseFrames(case_path)

print(cf.gencost)
```

If you have `matpower` installed via `pip install matpower` (did not requires `matpower[octave]`), you can easily navigate `matpower` case using:

```python
import os
from matpower import path_matpower # require `pip install matpower`
from matpowercaseframes import CaseFrames

case_name = 'case9.m'
case_path = os.path.join(path_matpower, 'data', case_name)
cf = CaseFrames(case_path)

print(cf.gencost)
```

### Read MATPOWER case by running `loadcase`

In some cases, a case file may contain `matlab` code at the end of the file that needs to be executed. An example of such case is `case69.m`. To properly load this type of file, use the method recommended by `matpower`, which is using `loadcase` instead of parsing. To do this, use the `load_case_engine` parameter (requires `matlab` or `octave`), as demonstrated here:

```python
from matpower import start_instance
from matpowercaseframes import CaseFrames

m = start_instance()

case_name = f"case69.m"
cf_lc = CaseFrames(case_name, load_case_engine=m)
cf_lc.branch  # see that the branch is already in p.u., converted by `loadcase`
```

### Convert `oct2py.io.Struct` to `CaseFrames`

If you use `matpower[octave]`, `CaseFrames` also support `oct2py.io.Struct` as input using:

```python
from matpower import start_instance
from matpowercaseframes import CaseFrames

m = start_instance()

# support mpc before runpf
mpc = m.loadcase('case9', verbose=False)
cf = CaseFrames(mpc)
print(cf.gencost)

# support mpc after runpf
mpc = m.runpf(mpc, verbose=False)
cf = CaseFrames(mpc)
print(cf.gencost)

m.exit()
```

### Convert `CaseFrames` to `mpc`

Furthermore, `matpowercaseframes` also support generating data that is acceptable by `matpower` via `matpower-pip` package (requires `matlab` or `octave`),

```python
from matpowercaseframes import CaseFrames

case_path = 'case9.m'
cf = CaseFrames(case_path)
mpc = cf.to_mpc()  # identical with cf.to_dict()

m = start_instance()
m.runpf(mpc)
```

### Add custom data

Sometimes, we want to expand `matpower` data containing custom field. For example, given an `mpc.load` as a `dict`, we can attach it to `CaseFrames` using,

```python
from matpower import start_instance
from matpowercaseframes import CaseFrames

m = start_instance()

LOAD_COL = ["LD_ID", "LD_BUS", "LD_STATUS", "LD_PD", "LD_QD"]

mpc = m.loadcase('case9', verbose=False)
cf = CaseFrames(mpc)
cf.setattr_as_df('load', mpc.load, columns_template=LOAD_COL)
```

If data already in `DataFrame`, we can use `setattr` directly as follows,

```python
from matpower import start_instance
from matpowercaseframes import CaseFrames

m = start_instance()

mpc = m.loadcase('case9', verbose=False)
cf = CaseFrames(mpc)
cf.setattr('load', df_load)
```

### Export as `xlsx`

To save all `DataFrame` to a single `xlsx` file, use:

```python
from matpowercaseframes import CaseFrames

case_path = 'case9.m'
cf = CaseFrames(case_path)

cf.to_excel('PATH/TO/DIR/case9.xlsx')
```

## Acknowledgment

1. This repository was supported by the [Faculty of Engineering, Universitas Gadjah Mada](https://ft.ugm.ac.id/en/) under the supervision of [Mr. Sarjiya](https://www.researchgate.net/profile/Sarjiya_Sarjiya). If you use this package for your research, we would be very glad if you cited any relevant publication under Mr. Sarjiya's name as thanks (but you are not responsible for citing). You can find his publications in the [Semantic Scholar](https://www.semanticscholar.org/author/Sarjiya/2267414) or [IEEE](https://ieeexplore.ieee.org/author/37548066400).

1. This repository is working flawlessly with [matpower-pip](https://github.com/yasirroni/matpower-pip). If you use matpower-pip, make sure to cite using the below citation:

    > M. Yasirroni, Sarjiya, and L. M. Putranto, "matpower-pip: A Python Package for Easy Access to MATPOWER Power System Simulation Package," [Online]. Available: <https://github.com/yasirroni/matpower-pip>.
    >
    > M. Yasirroni, Sarjiya, and L. M. Putranto, "matpower-pip". Zenodo, Jun. 13, 2024. doi: 10.5281/zenodo.11626845.

    ```bib
    @misc{matpower-pip,
      author       = {Yasirroni, M. and Sarjiya and Putranto, L. M.},
      title        = {matpower-pip: A Python Package for Easy Access to MATPOWER Power System Simulation Package},
      year         = {2023},
      howpublished = {\url{https://github.com/yasirroni/matpower-pip}},
    }

    @software{yasirroni_2024_11626845,
      author       = {Yasirroni, Muhammad and
                        Sarjiya, Sarjiya and
                        Putranto, Lesnanto Multa},
      title        = {matpower-pip},
      month        = jun,
      year         = 2024,
      publisher    = {Zenodo},
      version      = {8.0.0.2.1.8},
      doi          = {10.5281/zenodo.11626845},
      url          = {\url{https://doi.org/10.5281/zenodo.11626845}},
    }
    ```

1. This package is a fork and simplification from [psst](https://github.com/ames-market/psst) MATPOWER parser, thus we greatly thank psst developers and contributors.

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "matpowercaseframes",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.7.1",
    "maintainer_email": "Muhammad Yasirroni <muhammadyasirroni@gmail.com>",
    "keywords": "matpower, parser",
    "author": null,
    "author_email": "Muhammad Yasirroni <muhammadyasirroni@gmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/d0/c0/97854a91246dc17e02da19826874fbc71e3ded38c7b580cd1a3025281a52/matpowercaseframes-1.1.1.tar.gz",
    "platform": null,
    "description": "# MATPOWER Case Frames\n\n[![PyPI version](https://badge.fury.io/py/matpowercaseframes.svg)](https://pypi.org/project/matpowercaseframes/)\n\nParse MATPOWER case into pandas DataFrame.\n\nUnlike the [tutorial](https://github.com/yasirroni/matpower-pip#extra-require-oct2py-or-matlabengine) on [`matpower-pip`](https://github.com/yasirroni/matpower-pip), this package supports parsing MATPOWER case using `re` instead of `Oct2Py` and Octave. After that, you can further parse the data into any format supported by your solver.\n\n## Installation\n\n```plaintext\npip install matpowercaseframes\n```\n\n## Usage\n\n### Read MATPOWER case by parsing file as string\n\nThe main utility of `matpowercaseframes` is to help read `matpower` data in user-friendly format as follows,\n\n```python\nfrom matpowercaseframes import CaseFrames\n\ncase_path = 'case9.m'\ncf = CaseFrames(case_path)\n\nprint(cf.gencost)\n```\n\nIf you have `matpower` installed via `pip install matpower` (did not requires `matpower[octave]`), you can easily navigate `matpower` case using:\n\n```python\nimport os\nfrom matpower import path_matpower # require `pip install matpower`\nfrom matpowercaseframes import CaseFrames\n\ncase_name = 'case9.m'\ncase_path = os.path.join(path_matpower, 'data', case_name)\ncf = CaseFrames(case_path)\n\nprint(cf.gencost)\n```\n\n### Read MATPOWER case by running `loadcase`\n\nIn some cases, a case file may contain `matlab` code at the end of the file that needs to be executed. An example of such case is `case69.m`. To properly load this type of file, use the method recommended by `matpower`, which is using `loadcase` instead of parsing. To do this, use the `load_case_engine` parameter (requires `matlab` or `octave`), as demonstrated here:\n\n```python\nfrom matpower import start_instance\nfrom matpowercaseframes import CaseFrames\n\nm = start_instance()\n\ncase_name = f\"case69.m\"\ncf_lc = CaseFrames(case_name, load_case_engine=m)\ncf_lc.branch  # see that the branch is already in p.u., converted by `loadcase`\n```\n\n### Convert `oct2py.io.Struct` to `CaseFrames`\n\nIf you use `matpower[octave]`, `CaseFrames` also support `oct2py.io.Struct` as input using:\n\n```python\nfrom matpower import start_instance\nfrom matpowercaseframes import CaseFrames\n\nm = start_instance()\n\n# support mpc before runpf\nmpc = m.loadcase('case9', verbose=False)\ncf = CaseFrames(mpc)\nprint(cf.gencost)\n\n# support mpc after runpf\nmpc = m.runpf(mpc, verbose=False)\ncf = CaseFrames(mpc)\nprint(cf.gencost)\n\nm.exit()\n```\n\n### Convert `CaseFrames` to `mpc`\n\nFurthermore, `matpowercaseframes` also support generating data that is acceptable by `matpower` via `matpower-pip` package (requires `matlab` or `octave`),\n\n```python\nfrom matpowercaseframes import CaseFrames\n\ncase_path = 'case9.m'\ncf = CaseFrames(case_path)\nmpc = cf.to_mpc()  # identical with cf.to_dict()\n\nm = start_instance()\nm.runpf(mpc)\n```\n\n### Add custom data\n\nSometimes, we want to expand `matpower` data containing custom field. For example, given an `mpc.load` as a `dict`, we can attach it to `CaseFrames` using,\n\n```python\nfrom matpower import start_instance\nfrom matpowercaseframes import CaseFrames\n\nm = start_instance()\n\nLOAD_COL = [\"LD_ID\", \"LD_BUS\", \"LD_STATUS\", \"LD_PD\", \"LD_QD\"]\n\nmpc = m.loadcase('case9', verbose=False)\ncf = CaseFrames(mpc)\ncf.setattr_as_df('load', mpc.load, columns_template=LOAD_COL)\n```\n\nIf data already in `DataFrame`, we can use `setattr` directly as follows,\n\n```python\nfrom matpower import start_instance\nfrom matpowercaseframes import CaseFrames\n\nm = start_instance()\n\nmpc = m.loadcase('case9', verbose=False)\ncf = CaseFrames(mpc)\ncf.setattr('load', df_load)\n```\n\n### Export as `xlsx`\n\nTo save all `DataFrame` to a single `xlsx` file, use:\n\n```python\nfrom matpowercaseframes import CaseFrames\n\ncase_path = 'case9.m'\ncf = CaseFrames(case_path)\n\ncf.to_excel('PATH/TO/DIR/case9.xlsx')\n```\n\n## Acknowledgment\n\n1. This repository was supported by the [Faculty of Engineering, Universitas Gadjah Mada](https://ft.ugm.ac.id/en/) under the supervision of [Mr. Sarjiya](https://www.researchgate.net/profile/Sarjiya_Sarjiya). If you use this package for your research, we would be very glad if you cited any relevant publication under Mr. Sarjiya's name as thanks (but you are not responsible for citing). You can find his publications in the [Semantic Scholar](https://www.semanticscholar.org/author/Sarjiya/2267414) or [IEEE](https://ieeexplore.ieee.org/author/37548066400).\n\n1. This repository is working flawlessly with [matpower-pip](https://github.com/yasirroni/matpower-pip). If you use matpower-pip, make sure to cite using the below citation:\n\n    > M. Yasirroni, Sarjiya, and L. M. Putranto, \"matpower-pip: A Python Package for Easy Access to MATPOWER Power System Simulation Package,\" [Online]. Available: <https://github.com/yasirroni/matpower-pip>.\n    >\n    > M. Yasirroni, Sarjiya, and L. M. Putranto, \"matpower-pip\". Zenodo, Jun. 13, 2024. doi: 10.5281/zenodo.11626845.\n\n    ```bib\n    @misc{matpower-pip,\n      author       = {Yasirroni, M. and Sarjiya and Putranto, L. M.},\n      title        = {matpower-pip: A Python Package for Easy Access to MATPOWER Power System Simulation Package},\n      year         = {2023},\n      howpublished = {\\url{https://github.com/yasirroni/matpower-pip}},\n    }\n\n    @software{yasirroni_2024_11626845,\n      author       = {Yasirroni, Muhammad and\n                        Sarjiya, Sarjiya and\n                        Putranto, Lesnanto Multa},\n      title        = {matpower-pip},\n      month        = jun,\n      year         = 2024,\n      publisher    = {Zenodo},\n      version      = {8.0.0.2.1.8},\n      doi          = {10.5281/zenodo.11626845},\n      url          = {\\url{https://doi.org/10.5281/zenodo.11626845}},\n    }\n    ```\n\n1. This package is a fork and simplification from [psst](https://github.com/ames-market/psst) MATPOWER parser, thus we greatly thank psst developers and contributors.\n",
    "bugtrack_url": null,
    "license": "MIT License",
    "summary": "Parse MATPOWER case into pandas DataFrame.",
    "version": "1.1.1",
    "project_urls": {
        "Documentation": "https://github.com/UGM-EPSLab/matpowercaseframes#readme",
        "Issues": "https://github.com/UGM-EPSLab/matpowercaseframes/issues",
        "Source": "https://github.com/UGM-EPSLab/matpowercaseframes"
    },
    "split_keywords": [
        "matpower",
        " parser"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "1014ddeb1bd6c9e5a8b68852ae4307c60d0a37e65a68fd5d39b3322fcb5dd9a7",
                "md5": "bdfa0c988152bf0aa8603f746e55f8e2",
                "sha256": "6e662aad5b8d5c5c4dbc0646e7d4ef2e153bbd1c329684096af87df97285a71f"
            },
            "downloads": -1,
            "filename": "matpowercaseframes-1.1.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "bdfa0c988152bf0aa8603f746e55f8e2",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.7.1",
            "size": 13117,
            "upload_time": "2025-01-21T00:50:45",
            "upload_time_iso_8601": "2025-01-21T00:50:45.122236Z",
            "url": "https://files.pythonhosted.org/packages/10/14/ddeb1bd6c9e5a8b68852ae4307c60d0a37e65a68fd5d39b3322fcb5dd9a7/matpowercaseframes-1.1.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "d0c097854a91246dc17e02da19826874fbc71e3ded38c7b580cd1a3025281a52",
                "md5": "df03be5ed6f835fe138567cd7e70b242",
                "sha256": "0ec28bb4e03410c0c394691d0259c14cabb2b09f7b3bdfa2af496d974242302f"
            },
            "downloads": -1,
            "filename": "matpowercaseframes-1.1.1.tar.gz",
            "has_sig": false,
            "md5_digest": "df03be5ed6f835fe138567cd7e70b242",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.7.1",
            "size": 16403,
            "upload_time": "2025-01-21T00:50:46",
            "upload_time_iso_8601": "2025-01-21T00:50:46.887383Z",
            "url": "https://files.pythonhosted.org/packages/d0/c0/97854a91246dc17e02da19826874fbc71e3ded38c7b580cd1a3025281a52/matpowercaseframes-1.1.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-01-21 00:50:46",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "UGM-EPSLab",
    "github_project": "matpowercaseframes#readme",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "matpowercaseframes"
}
        
Elapsed time: 1.01207s