opf_dataset_utils


Nameopf_dataset_utils JSON
Version 0.0.2 PyPI version JSON
download
home_pageNone
SummaryPhysical calculations, plotting, and other utils for the PyG OPFDataset.
upload_time2024-10-19 12:20:38
maintainerNone
docs_urlNone
authorViktor Todosijevic
requires_python<3.12,>=3.10
licenseNone
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # opf-dataset-utils

In this package we provide utils to support working with the
[OPFDataset](https://pytorch-geometric.readthedocs.io/en/latest/generated/torch_geometric.datasets.OPFDataset.html#torch_geometric.datasets.OPFDataset).

We implement:

* Efficient OPF related calculations to be used as metrics or in a physics informed setting:
    * Power Flow errors
    * Branch powers
    * Costs
    * Inequalities
* Data visualization
* Enums for indexing the OPFData JSON format
* And more...

## Installation

### Requirements

Requires [_PyTorch_](https://pytorch.org/get-started/locally/) (any [version](https://pytorch.org/get-started/previous-versions/)
supported by PyTorch Geometric) and [_PyTorch Geometric_](https://pytorch-geometric.readthedocs.io/en/latest/install/installation.html) (>=2.6.0)
including it's optional dependencies (`pyg_lib`, `torch_scatter`, `torch_sparse`, `torch_cluster`, and `torch_spline_conv`).

May require installing _graphviz_ for visualization purposes.

### From PyPI

Install latest release from PyPI:
```
pip install opf_dataset_utils
```

### Directly from GitHub

Install the latest version directly from GitHub:
```
pip install git+https://github.com/viktor-ktorvi/opf-dataset-utils.git
```

## Usage

### Plotting

See [scripts/draw.py](scripts/draw.py) for a full example.

```python
from opf_dataset_utils.plotting.draw import draw_graph

draw_graph(dataset[0], ax=ax, node_size=300)
```

<p align="center">
<img src="img/draw_example.png" alt="Example graph" width="600"/>
</p>

### OPF calculations

#### Power flow errors

See [scripts/power_flow_errors.py](scripts/power_flow_errors.py) for a full example.

```python
from opf_dataset_utils.physics.errors.power_flow import calculate_power_flow_errors

with torch.no_grad():
    untrained_predictions = untrained_model(batch.x_dict, batch.edge_index_dict)

mean_abs_errors_solution = calculate_power_flow_errors(batch, batch.y_dict).abs().mean()
mean_abs_errors_untrained = calculate_power_flow_errors(batch, untrained_predictions).abs().mean()
```
Example results:
```
Mean power flow errors:
	Solution: 1.28563e-06 [p.u.]
	Untrained model prediction: 413350.84375 [p.u.]
```

#### Costs

See [scripts/costs.py](scripts/costs.py) for a full example.

```python
from opf_dataset_utils.costs import (
    calculate_costs_per_generator,
    calculate_costs_per_grid,
)

costs_per_grid = calculate_costs_per_grid(data, data.y_dict)
costs_per_generator = calculate_costs_per_generator(data, data.y_dict)
```

Example results:

```
Costs per grid [$/h]:
tensor([7564.9541, 8622.5996, 7247.7939, 7504.3018, 8446.8887, 7478.8228,
        8023.4907], device='cuda:0')
Costs per generator [$/h]:
tensor([4003.2192, 3561.7349,    0.0000,    0.0000,    0.0000,    0.0000,
        4035.1089, 4587.4902,    0.0000,    0.0000,    0.0000,    0.0000,
        3992.6309, 3255.1628,    0.0000,    0.0000,    0.0000,    0.0000,
        4005.5452, 3498.7563,    0.0000,    0.0000,    0.0000,    0.0000,
        4045.0081, 4401.8809,    0.0000,    0.0000,    0.0000,    0.0000,
        3994.2031, 3484.6196,    0.0000,    0.0000,    0.0000,    0.0000,
        4021.5444, 4001.9463,    0.0000,    0.0000,    0.0000,    0.0000],
       device='cuda:0')
```

#### Inequality violations

See [scripts/inequality_errors.py](scripts/inequality_errors.py) for a full example.

```python
from opf_dataset_utils.enumerations import EdgeTypes
from opf_dataset_utils.physics.errors.inequality.voltage import calculate_upper_voltage_angle_difference_errors
from opf_dataset_utils.physics.errors.inequality.generator_power import calculate_lower_active_power_errors

upper_voltage_angle_violations_transformer = calculate_upper_voltage_angle_difference_errors(data, data.y_dict, EdgeTypes.TRANSFORMER)
lower_active_power_generation_violations = calculate_lower_active_power_errors(data, data.y_dict)
# etc.
```

Example results:

```
Worst case violations:

Solution:

Upper Vm:                         1.1920928955078125e-07 [p.u.]                   
Lower Vm:                                            0.0 [p.u.]                   
Upper Va diff. (transformers):                       0.0 [rad]                    
Upper Va diff. (AC lines):                           0.0 [rad]                    
Lower Va diff. (transformers):                       0.0 [rad]                    
Lower Va diff. (AC lines):                           0.0 [rad]                    
Upper Pg:                                            0.0 [p.u.]                   
Lower Pg:                          9.985742011053844e-09 [p.u.]                   
Upper Qg:                                            0.0 [p.u.]                   
Lower Qg:                                            0.0 [p.u.]                   
Upper S_ij (transformers):                           0.0 [p.u.]                   
Upper S_ij (AC lines):                               0.0 [p.u.]                   
Upper S_ji (transformers):                           0.0 [p.u.]                   
Upper S_ji (AC lines):                               0.0 [p.u.]                   

Untrained model:

Upper Vm:                             112.72354125976562 [p.u.]                   
Lower Vm:                             306.87078857421875 [p.u.]                   
Upper Va diff. (transformers):                       0.0 [rad]                    
Upper Va diff. (AC lines):            1149.0396728515625 [rad]                    
Lower Va diff. (transformers):         124.8240737915039 [rad]                    
Lower Va diff. (AC lines):             686.0045776367188 [rad]                    
Upper Pg:                                            0.0 [p.u.]                   
Lower Pg:                              33.55585479736328 [p.u.]                   
Upper Qg:                             0.1578141152858734 [p.u.]                   
Lower Qg:                              157.2376708984375 [p.u.]                   
Upper S_ij (transformers):                  417092.96875 [p.u.]                   
Upper S_ij (AC lines):                        2345705.25 [p.u.]                   
Upper S_ji (transformers):              10831.6123046875 [p.u.]                   
Upper S_ji (AC lines):                        3082554.75 [p.u.]                   

Process finished with exit code 0

```

#### Branch power flows

See [scripts/branch_powers.py](scripts/branch_powers.py) for a full example.

```python
from opf_dataset_utils.enumerations import EdgeTypes
from opf_dataset_utils.physics.power import calculate_branch_powers

ac_line_powers_from, ac_line_powers_to = calculate_branch_powers(batch, batch.y_dict, EdgeTypes.AC_LINE)
transformer_powers_from, transformer_powers_to = calculate_branch_powers(batch, batch.y_dict, EdgeTypes.TRANSFORMER)
```

Example results:

```
AC line power flows [p.u.]:
tensor([ 1.9860e+00-0.0678j,  8.5920e-01+0.0821j,  7.7557e-01-0.0108j,
         5.6563e-01-0.0239j,  4.0300e-01+0.0017j, -2.6185e-01+0.1008j,
        -6.7861e-01+0.1603j,  8.0781e-02+0.0468j,  8.7156e-02+0.0244j,
         1.9349e-01+0.0730j,  2.4476e-07-0.1052j,  2.9223e-01+0.0275j,
         5.5007e-02+0.0391j,  9.9253e-02+0.0241j, -4.9108e-02-0.0280j,
         1.5681e-02+0.0090j,  6.4659e-02+0.0209j], device='cuda:0')


Transformer line power flows [p.u.]:
tensor([0.2922-0.0601j, 0.1681+0.0037j, 0.4683+0.1187j], device='cuda:0')
```

#### Etc.

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "opf_dataset_utils",
    "maintainer": null,
    "docs_url": null,
    "requires_python": "<3.12,>=3.10",
    "maintainer_email": null,
    "keywords": null,
    "author": "Viktor Todosijevic",
    "author_email": "todosijevicviktor998@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/55/99/5197b58f608dd702e5fb84b73b81ceb8b84708b73dd0f5fe9ddb081e33bf/opf_dataset_utils-0.0.2.tar.gz",
    "platform": null,
    "description": "# opf-dataset-utils\n\nIn this package we provide utils to support working with the\n[OPFDataset](https://pytorch-geometric.readthedocs.io/en/latest/generated/torch_geometric.datasets.OPFDataset.html#torch_geometric.datasets.OPFDataset).\n\nWe implement:\n\n* Efficient OPF related calculations to be used as metrics or in a physics informed setting:\n    * Power Flow errors\n    * Branch powers\n    * Costs\n    * Inequalities\n* Data visualization\n* Enums for indexing the OPFData JSON format\n* And more...\n\n## Installation\n\n### Requirements\n\nRequires [_PyTorch_](https://pytorch.org/get-started/locally/) (any [version](https://pytorch.org/get-started/previous-versions/)\nsupported by PyTorch Geometric) and [_PyTorch Geometric_](https://pytorch-geometric.readthedocs.io/en/latest/install/installation.html) (>=2.6.0)\nincluding it's optional dependencies (`pyg_lib`, `torch_scatter`, `torch_sparse`, `torch_cluster`, and `torch_spline_conv`).\n\nMay require installing _graphviz_ for visualization purposes.\n\n### From PyPI\n\nInstall latest release from PyPI:\n```\npip install opf_dataset_utils\n```\n\n### Directly from GitHub\n\nInstall the latest version directly from GitHub:\n```\npip install git+https://github.com/viktor-ktorvi/opf-dataset-utils.git\n```\n\n## Usage\n\n### Plotting\n\nSee [scripts/draw.py](scripts/draw.py) for a full example.\n\n```python\nfrom opf_dataset_utils.plotting.draw import draw_graph\n\ndraw_graph(dataset[0], ax=ax, node_size=300)\n```\n\n<p align=\"center\">\n<img src=\"img/draw_example.png\" alt=\"Example graph\" width=\"600\"/>\n</p>\n\n### OPF calculations\n\n#### Power flow errors\n\nSee [scripts/power_flow_errors.py](scripts/power_flow_errors.py) for a full example.\n\n```python\nfrom opf_dataset_utils.physics.errors.power_flow import calculate_power_flow_errors\n\nwith torch.no_grad():\n    untrained_predictions = untrained_model(batch.x_dict, batch.edge_index_dict)\n\nmean_abs_errors_solution = calculate_power_flow_errors(batch, batch.y_dict).abs().mean()\nmean_abs_errors_untrained = calculate_power_flow_errors(batch, untrained_predictions).abs().mean()\n```\nExample results:\n```\nMean power flow errors:\n\tSolution: 1.28563e-06 [p.u.]\n\tUntrained model prediction: 413350.84375 [p.u.]\n```\n\n#### Costs\n\nSee [scripts/costs.py](scripts/costs.py) for a full example.\n\n```python\nfrom opf_dataset_utils.costs import (\n    calculate_costs_per_generator,\n    calculate_costs_per_grid,\n)\n\ncosts_per_grid = calculate_costs_per_grid(data, data.y_dict)\ncosts_per_generator = calculate_costs_per_generator(data, data.y_dict)\n```\n\nExample results:\n\n```\nCosts per grid [$/h]:\ntensor([7564.9541, 8622.5996, 7247.7939, 7504.3018, 8446.8887, 7478.8228,\n        8023.4907], device='cuda:0')\nCosts per generator [$/h]:\ntensor([4003.2192, 3561.7349,    0.0000,    0.0000,    0.0000,    0.0000,\n        4035.1089, 4587.4902,    0.0000,    0.0000,    0.0000,    0.0000,\n        3992.6309, 3255.1628,    0.0000,    0.0000,    0.0000,    0.0000,\n        4005.5452, 3498.7563,    0.0000,    0.0000,    0.0000,    0.0000,\n        4045.0081, 4401.8809,    0.0000,    0.0000,    0.0000,    0.0000,\n        3994.2031, 3484.6196,    0.0000,    0.0000,    0.0000,    0.0000,\n        4021.5444, 4001.9463,    0.0000,    0.0000,    0.0000,    0.0000],\n       device='cuda:0')\n```\n\n#### Inequality violations\n\nSee [scripts/inequality_errors.py](scripts/inequality_errors.py) for a full example.\n\n```python\nfrom opf_dataset_utils.enumerations import EdgeTypes\nfrom opf_dataset_utils.physics.errors.inequality.voltage import calculate_upper_voltage_angle_difference_errors\nfrom opf_dataset_utils.physics.errors.inequality.generator_power import calculate_lower_active_power_errors\n\nupper_voltage_angle_violations_transformer = calculate_upper_voltage_angle_difference_errors(data, data.y_dict, EdgeTypes.TRANSFORMER)\nlower_active_power_generation_violations = calculate_lower_active_power_errors(data, data.y_dict)\n# etc.\n```\n\nExample results:\n\n```\nWorst case violations:\n\nSolution:\n\nUpper Vm:                         1.1920928955078125e-07 [p.u.]                   \nLower Vm:                                            0.0 [p.u.]                   \nUpper Va diff. (transformers):                       0.0 [rad]                    \nUpper Va diff. (AC lines):                           0.0 [rad]                    \nLower Va diff. (transformers):                       0.0 [rad]                    \nLower Va diff. (AC lines):                           0.0 [rad]                    \nUpper Pg:                                            0.0 [p.u.]                   \nLower Pg:                          9.985742011053844e-09 [p.u.]                   \nUpper Qg:                                            0.0 [p.u.]                   \nLower Qg:                                            0.0 [p.u.]                   \nUpper S_ij (transformers):                           0.0 [p.u.]                   \nUpper S_ij (AC lines):                               0.0 [p.u.]                   \nUpper S_ji (transformers):                           0.0 [p.u.]                   \nUpper S_ji (AC lines):                               0.0 [p.u.]                   \n\nUntrained model:\n\nUpper Vm:                             112.72354125976562 [p.u.]                   \nLower Vm:                             306.87078857421875 [p.u.]                   \nUpper Va diff. (transformers):                       0.0 [rad]                    \nUpper Va diff. (AC lines):            1149.0396728515625 [rad]                    \nLower Va diff. (transformers):         124.8240737915039 [rad]                    \nLower Va diff. (AC lines):             686.0045776367188 [rad]                    \nUpper Pg:                                            0.0 [p.u.]                   \nLower Pg:                              33.55585479736328 [p.u.]                   \nUpper Qg:                             0.1578141152858734 [p.u.]                   \nLower Qg:                              157.2376708984375 [p.u.]                   \nUpper S_ij (transformers):                  417092.96875 [p.u.]                   \nUpper S_ij (AC lines):                        2345705.25 [p.u.]                   \nUpper S_ji (transformers):              10831.6123046875 [p.u.]                   \nUpper S_ji (AC lines):                        3082554.75 [p.u.]                   \n\nProcess finished with exit code 0\n\n```\n\n#### Branch power flows\n\nSee [scripts/branch_powers.py](scripts/branch_powers.py) for a full example.\n\n```python\nfrom opf_dataset_utils.enumerations import EdgeTypes\nfrom opf_dataset_utils.physics.power import calculate_branch_powers\n\nac_line_powers_from, ac_line_powers_to = calculate_branch_powers(batch, batch.y_dict, EdgeTypes.AC_LINE)\ntransformer_powers_from, transformer_powers_to = calculate_branch_powers(batch, batch.y_dict, EdgeTypes.TRANSFORMER)\n```\n\nExample results:\n\n```\nAC line power flows [p.u.]:\ntensor([ 1.9860e+00-0.0678j,  8.5920e-01+0.0821j,  7.7557e-01-0.0108j,\n         5.6563e-01-0.0239j,  4.0300e-01+0.0017j, -2.6185e-01+0.1008j,\n        -6.7861e-01+0.1603j,  8.0781e-02+0.0468j,  8.7156e-02+0.0244j,\n         1.9349e-01+0.0730j,  2.4476e-07-0.1052j,  2.9223e-01+0.0275j,\n         5.5007e-02+0.0391j,  9.9253e-02+0.0241j, -4.9108e-02-0.0280j,\n         1.5681e-02+0.0090j,  6.4659e-02+0.0209j], device='cuda:0')\n\n\nTransformer line power flows [p.u.]:\ntensor([0.2922-0.0601j, 0.1681+0.0037j, 0.4683+0.1187j], device='cuda:0')\n```\n\n#### Etc.\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "Physical calculations, plotting, and other utils for the PyG OPFDataset.",
    "version": "0.0.2",
    "project_urls": null,
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "c007bb1272009bb3d9fb01150deb28f55f4e5eb069e57091fdb8b8a9b7a7015f",
                "md5": "48d7a168fc4e67248a91f2ccefcbdcc9",
                "sha256": "c64c36d35e7da78e6f9244d6dbc6c361f86c8534f9ff49a5a481ce96ad71916a"
            },
            "downloads": -1,
            "filename": "opf_dataset_utils-0.0.2-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "48d7a168fc4e67248a91f2ccefcbdcc9",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": "<3.12,>=3.10",
            "size": 18991,
            "upload_time": "2024-10-19T12:20:36",
            "upload_time_iso_8601": "2024-10-19T12:20:36.586769Z",
            "url": "https://files.pythonhosted.org/packages/c0/07/bb1272009bb3d9fb01150deb28f55f4e5eb069e57091fdb8b8a9b7a7015f/opf_dataset_utils-0.0.2-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "55995197b58f608dd702e5fb84b73b81ceb8b84708b73dd0f5fe9ddb081e33bf",
                "md5": "ca937c3e883b2bd8e1df937f46c2f053",
                "sha256": "beb38818bacc6938d758c5c4492c695020185abcc8dfad36c87c6a703332a2d0"
            },
            "downloads": -1,
            "filename": "opf_dataset_utils-0.0.2.tar.gz",
            "has_sig": false,
            "md5_digest": "ca937c3e883b2bd8e1df937f46c2f053",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "<3.12,>=3.10",
            "size": 30781,
            "upload_time": "2024-10-19T12:20:38",
            "upload_time_iso_8601": "2024-10-19T12:20:38.346194Z",
            "url": "https://files.pythonhosted.org/packages/55/99/5197b58f608dd702e5fb84b73b81ceb8b84708b73dd0f5fe9ddb081e33bf/opf_dataset_utils-0.0.2.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-10-19 12:20:38",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "lcname": "opf_dataset_utils"
}
        
Elapsed time: 0.33730s