tranpy-ai 0.1.3__tar.gz → 0.2.0.dev0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- tranpy_ai-0.2.0.dev0/.gitignore +54 -0
- tranpy_ai-0.2.0.dev0/PKG-INFO +114 -0
- tranpy_ai-0.2.0.dev0/README.md +62 -0
- tranpy_ai-0.2.0.dev0/docs/README.md +11 -0
- tranpy_ai-0.2.0.dev0/pyproject.toml +124 -0
- tranpy_ai-0.2.0.dev0/src/tranpy/__init__.py +41 -0
- tranpy_ai-0.2.0.dev0/src/tranpy/cli.py +135 -0
- tranpy_ai-0.2.0.dev0/src/tranpy/datasets.py +397 -0
- tranpy_ai-0.2.0.dev0/src/tranpy/early_prediction.py +391 -0
- tranpy_ai-0.2.0.dev0/src/tranpy/features.py +426 -0
- tranpy_ai-0.2.0.dev0/src/tranpy/plotting.py +122 -0
- tranpy_ai-0.2.0.dev0/src/tranpy/pretrained.py +99 -0
- tranpy_ai-0.2.0.dev0/src/tranpy/py.typed +0 -0
- tranpy_ai-0.2.0.dev0/src/tranpy/resources/ieee_9bus_snapshots.npz +0 -0
- tranpy_ai-0.2.0.dev0/src/tranpy/resources/new_england_snapshots.npz +0 -0
- tranpy_ai-0.2.0.dev0/src/tranpy/resources/registry.json +429 -0
- tranpy_ai-0.2.0.dev0/src/tranpy/simulation/__init__.py +25 -0
- tranpy_ai-0.2.0.dev0/src/tranpy/simulation/engine.py +270 -0
- tranpy_ai-0.2.0.dev0/src/tranpy/simulation/mock.py +344 -0
- tranpy_ai-0.2.0.dev0/src/tranpy/simulation/powerfactory.py +206 -0
- tranpy_ai-0.2.0.dev0/src/tranpy/systems.py +59 -0
- tranpy_ai-0.2.0.dev0/src/tranpy/vocabulary.py +111 -0
- tranpy_ai-0.2.0.dev0/src/tranpy_core/__init__.py +48 -0
- tranpy_ai-0.2.0.dev0/src/tranpy_core/_optional.py +30 -0
- tranpy_ai-0.2.0.dev0/src/tranpy_core/config.py +178 -0
- tranpy_ai-0.2.0.dev0/src/tranpy_core/data/__init__.py +48 -0
- tranpy_ai-0.2.0.dev0/src/tranpy_core/data/dataset.py +417 -0
- tranpy_ai-0.2.0.dev0/src/tranpy_core/data/download.py +85 -0
- tranpy_ai-0.2.0.dev0/src/tranpy_core/data/shards.py +374 -0
- tranpy_ai-0.2.0.dev0/src/tranpy_core/data/split.py +124 -0
- tranpy_ai-0.2.0.dev0/src/tranpy_core/data/storage.py +238 -0
- tranpy_ai-0.2.0.dev0/src/tranpy_core/errors.py +50 -0
- tranpy_ai-0.2.0.dev0/src/tranpy_core/evaluation.py +179 -0
- tranpy_ai-0.2.0.dev0/src/tranpy_core/experiment.py +179 -0
- tranpy_ai-0.2.0.dev0/src/tranpy_core/explain/__init__.py +16 -0
- tranpy_ai-0.2.0.dev0/src/tranpy_core/explain/base.py +109 -0
- tranpy_ai-0.2.0.dev0/src/tranpy_core/explain/breakdown.py +69 -0
- tranpy_ai-0.2.0.dev0/src/tranpy_core/explain/global_surrogate.py +94 -0
- tranpy_ai-0.2.0.dev0/src/tranpy_core/explain/local_surrogate.py +73 -0
- tranpy_ai-0.2.0.dev0/src/tranpy_core/explain/shapley.py +55 -0
- tranpy_ai-0.2.0.dev0/src/tranpy_core/features/__init__.py +17 -0
- tranpy_ai-0.2.0.dev0/src/tranpy_core/features/segments.py +125 -0
- tranpy_ai-0.2.0.dev0/src/tranpy_core/features/window.py +145 -0
- tranpy_ai-0.2.0.dev0/src/tranpy_core/models/__init__.py +19 -0
- tranpy_ai-0.2.0.dev0/src/tranpy_core/models/neural.py +354 -0
- tranpy_ai-0.2.0.dev0/src/tranpy_core/models/persistence.py +55 -0
- tranpy_ai-0.2.0.dev0/src/tranpy_core/models/registry.py +244 -0
- tranpy_ai-0.2.0.dev0/src/tranpy_core/plotting.py +216 -0
- tranpy_ai-0.2.0.dev0/src/tranpy_core/py.typed +0 -0
- tranpy_ai-0.2.0.dev0/src/tranpy_core/random.py +36 -0
- tranpy_ai-0.2.0.dev0/tests/__init__.py +0 -0
- tranpy_ai-0.2.0.dev0/tests/conftest.py +75 -0
- tranpy_ai-0.2.0.dev0/tests/core/data/test_dataset.py +161 -0
- tranpy_ai-0.2.0.dev0/tests/core/data/test_shards.py +180 -0
- tranpy_ai-0.2.0.dev0/tests/core/data/test_split.py +78 -0
- tranpy_ai-0.2.0.dev0/tests/core/data/test_storage_download.py +143 -0
- tranpy_ai-0.2.0.dev0/tests/core/explain/test_explain.py +121 -0
- tranpy_ai-0.2.0.dev0/tests/core/features/test_segments_window.py +127 -0
- tranpy_ai-0.2.0.dev0/tests/core/models/test_neural_persistence.py +126 -0
- tranpy_ai-0.2.0.dev0/tests/core/models/test_registry.py +125 -0
- tranpy_ai-0.2.0.dev0/tests/core/test_config.py +132 -0
- tranpy_ai-0.2.0.dev0/tests/core/test_evaluation_leakage.py +171 -0
- tranpy_ai-0.2.0.dev0/tests/core/test_experiment_plotting.py +104 -0
- tranpy_ai-0.2.0.dev0/tests/core/test_random_optional.py +46 -0
- tranpy_ai-0.2.0.dev0/tests/regression/test_snapshot.py +63 -0
- tranpy_ai-0.2.0.dev0/tests/snapshots/metrics.json +728 -0
- tranpy_ai-0.2.0.dev0/tests/test_import_boundaries.py +110 -0
- tranpy_ai-0.2.0.dev0/tests/tranpy/simulation/test_simulation.py +311 -0
- tranpy_ai-0.2.0.dev0/tests/tranpy/test_datasets.py +189 -0
- tranpy_ai-0.2.0.dev0/tests/tranpy/test_early_prediction_plotting.py +141 -0
- tranpy_ai-0.2.0.dev0/tests/tranpy/test_event_shards.py +156 -0
- tranpy_ai-0.2.0.dev0/tests/tranpy/test_features.py +157 -0
- tranpy_ai-0.2.0.dev0/tests/tranpy/test_pretrained_cli.py +95 -0
- tranpy_ai-0.2.0.dev0/tests/update_snapshot.py +72 -0
- tranpy_ai-0.1.3/MANIFEST.in +0 -18
- tranpy_ai-0.1.3/PKG-INFO +0 -115
- tranpy_ai-0.1.3/README.md +0 -51
- tranpy_ai-0.1.3/pyproject.toml +0 -109
- tranpy_ai-0.1.3/setup.cfg +0 -4
- tranpy_ai-0.1.3/src/tranpy/__init__.py +0 -36
- tranpy_ai-0.1.3/src/tranpy/data/__init__.py +0 -14
- tranpy_ai-0.1.3/src/tranpy/data/cache.py +0 -183
- tranpy_ai-0.1.3/src/tranpy/data/datasets/registry.json +0 -52
- tranpy_ai-0.1.3/src/tranpy/data/download.py +0 -150
- tranpy_ai-0.1.3/src/tranpy/data/models/registry.json +0 -844
- tranpy_ai-0.1.3/src/tranpy/datasets/__init__.py +0 -23
- tranpy_ai-0.1.3/src/tranpy/datasets/base.py +0 -65
- tranpy_ai-0.1.3/src/tranpy/datasets/loaders.py +0 -268
- tranpy_ai-0.1.3/src/tranpy/explainers/__init__.py +0 -34
- tranpy_ai-0.1.3/src/tranpy/explainers/base.py +0 -65
- tranpy_ai-0.1.3/src/tranpy/explainers/dalex.py +0 -247
- tranpy_ai-0.1.3/src/tranpy/explainers/lime.py +0 -269
- tranpy_ai-0.1.3/src/tranpy/explainers/shap.py +0 -316
- tranpy_ai-0.1.3/src/tranpy/models/__init__.py +0 -92
- tranpy_ai-0.1.3/src/tranpy/models/base.py +0 -160
- tranpy_ai-0.1.3/src/tranpy/models/classical.py +0 -960
- tranpy_ai-0.1.3/src/tranpy/models/ensemble.py +0 -195
- tranpy_ai-0.1.3/src/tranpy/models/neural.py +0 -293
- tranpy_ai-0.1.3/src/tranpy/models/pretrained.py +0 -255
- tranpy_ai-0.1.3/src/tranpy/simulation/__init__.py +0 -119
- tranpy_ai-0.1.3/src/tranpy/simulation/config.py +0 -340
- tranpy_ai-0.1.3/src/tranpy/simulation/dataset_generator.py +0 -320
- tranpy_ai-0.1.3/src/tranpy/simulation/events.py +0 -371
- tranpy_ai-0.1.3/src/tranpy/simulation/mock_engine.py +0 -364
- tranpy_ai-0.1.3/src/tranpy/simulation/model.py +0 -358
- tranpy_ai-0.1.3/src/tranpy/simulation/powerfactory_config.py +0 -236
- tranpy_ai-0.1.3/src/tranpy/simulation/results.py +0 -287
- tranpy_ai-0.1.3/src/tranpy/simulation/simulator.py +0 -501
- tranpy_ai-0.1.3/src/tranpy/utils/__init__.py +0 -11
- tranpy_ai-0.1.3/src/tranpy/utils/config.py +0 -70
- tranpy_ai-0.1.3/src/tranpy/utils/io.py +0 -64
- tranpy_ai-0.1.3/src/tranpy/utils/logging.py +0 -144
- tranpy_ai-0.1.3/src/tranpy_ai.egg-info/PKG-INFO +0 -115
- tranpy_ai-0.1.3/src/tranpy_ai.egg-info/SOURCES.txt +0 -48
- tranpy_ai-0.1.3/src/tranpy_ai.egg-info/dependency_links.txt +0 -1
- tranpy_ai-0.1.3/src/tranpy_ai.egg-info/requires.txt +0 -46
- tranpy_ai-0.1.3/src/tranpy_ai.egg-info/top_level.txt +0 -1
- tranpy_ai-0.1.3/tests/test_all_classifiers.py +0 -175
- tranpy_ai-0.1.3/tests/test_basic_functionality.py +0 -178
- tranpy_ai-0.1.3/tests/test_datasets.py +0 -217
- tranpy_ai-0.1.3/tests/test_explainers.py +0 -298
- tranpy_ai-0.1.3/tests/test_import.py +0 -3
- tranpy_ai-0.1.3/tests/test_models.py +0 -301
- tranpy_ai-0.1.3/tests/test_pretrained_models.py +0 -261
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
# Python
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.py[cod]
|
|
4
|
+
*.egg-info/
|
|
5
|
+
.eggs/
|
|
6
|
+
|
|
7
|
+
# Build and distribution
|
|
8
|
+
build/
|
|
9
|
+
dist/
|
|
10
|
+
docs/_build/
|
|
11
|
+
|
|
12
|
+
# Environments and tool caches
|
|
13
|
+
.venv/
|
|
14
|
+
venv/
|
|
15
|
+
.python-version
|
|
16
|
+
.pytest_cache/
|
|
17
|
+
.mypy_cache/
|
|
18
|
+
.ruff_cache/
|
|
19
|
+
.hypothesis/
|
|
20
|
+
.coverage
|
|
21
|
+
.coverage.*
|
|
22
|
+
coverage.xml
|
|
23
|
+
htmlcov/
|
|
24
|
+
.ipynb_checkpoints/
|
|
25
|
+
|
|
26
|
+
# Generated outputs (notebooks and CLI write here)
|
|
27
|
+
outputs/
|
|
28
|
+
release/
|
|
29
|
+
examples/outputs/
|
|
30
|
+
|
|
31
|
+
# Local data that must not be committed: legacy pickles, raw exports, downloads
|
|
32
|
+
*.pickle
|
|
33
|
+
*.pkl
|
|
34
|
+
src/tranpy/data/
|
|
35
|
+
|
|
36
|
+
# Secrets and private material
|
|
37
|
+
.env
|
|
38
|
+
.env.*
|
|
39
|
+
*.env
|
|
40
|
+
papers/
|
|
41
|
+
|
|
42
|
+
# Editors and OS
|
|
43
|
+
.idea/
|
|
44
|
+
.vscode/
|
|
45
|
+
.DS_Store
|
|
46
|
+
Thumbs.db
|
|
47
|
+
|
|
48
|
+
# Local assistant and publishing notes (not part of the package)
|
|
49
|
+
AGENTS.md
|
|
50
|
+
CLAUDE.md
|
|
51
|
+
.claude/
|
|
52
|
+
.agents/
|
|
53
|
+
skills/
|
|
54
|
+
PUBLISHING.md
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: tranpy-ai
|
|
3
|
+
Version: 0.2.0.dev0
|
|
4
|
+
Summary: Machine learning for power-system transient stability: datasets, models, evaluation and explanation
|
|
5
|
+
Project-URL: Homepage, https://github.com/mahmouddraz/tranpy
|
|
6
|
+
Project-URL: Repository, https://github.com/mahmouddraz/tranpy
|
|
7
|
+
Project-URL: Issues, https://github.com/mahmouddraz/tranpy/issues
|
|
8
|
+
Author-email: Mahmoud Draz <m.draz@tu-berlin.de>
|
|
9
|
+
License-Expression: MIT
|
|
10
|
+
Keywords: explainability,machine-learning,power-system,transient-stability
|
|
11
|
+
Classifier: Development Status :: 3 - Alpha
|
|
12
|
+
Classifier: Intended Audience :: Science/Research
|
|
13
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
14
|
+
Classifier: Programming Language :: Python :: 3
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
18
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
19
|
+
Classifier: Typing :: Typed
|
|
20
|
+
Requires-Python: >=3.11
|
|
21
|
+
Requires-Dist: numpy>=1.26
|
|
22
|
+
Requires-Dist: pandas>=2.1
|
|
23
|
+
Requires-Dist: platformdirs>=4.0
|
|
24
|
+
Requires-Dist: pyyaml>=6.0
|
|
25
|
+
Requires-Dist: scikit-learn<1.11,>=1.5
|
|
26
|
+
Requires-Dist: scipy>=1.11
|
|
27
|
+
Provides-Extra: all
|
|
28
|
+
Requires-Dist: dalex>=1.7; extra == 'all'
|
|
29
|
+
Requires-Dist: lightgbm>=4.0; extra == 'all'
|
|
30
|
+
Requires-Dist: lime>=0.2.0.1; extra == 'all'
|
|
31
|
+
Requires-Dist: matplotlib>=3.8; extra == 'all'
|
|
32
|
+
Requires-Dist: setuptools<85; extra == 'all'
|
|
33
|
+
Requires-Dist: shap>=0.45; extra == 'all'
|
|
34
|
+
Requires-Dist: skops>=0.10; extra == 'all'
|
|
35
|
+
Requires-Dist: tensorflow>=2.16; extra == 'all'
|
|
36
|
+
Requires-Dist: xgboost>=2.0; extra == 'all'
|
|
37
|
+
Provides-Extra: boosting
|
|
38
|
+
Requires-Dist: lightgbm>=4.0; extra == 'boosting'
|
|
39
|
+
Requires-Dist: xgboost>=2.0; extra == 'boosting'
|
|
40
|
+
Provides-Extra: explain
|
|
41
|
+
Requires-Dist: dalex>=1.7; extra == 'explain'
|
|
42
|
+
Requires-Dist: lime>=0.2.0.1; extra == 'explain'
|
|
43
|
+
Requires-Dist: setuptools<85; extra == 'explain'
|
|
44
|
+
Requires-Dist: shap>=0.45; extra == 'explain'
|
|
45
|
+
Provides-Extra: neural
|
|
46
|
+
Requires-Dist: tensorflow>=2.16; extra == 'neural'
|
|
47
|
+
Provides-Extra: persistence
|
|
48
|
+
Requires-Dist: skops>=0.10; extra == 'persistence'
|
|
49
|
+
Provides-Extra: plot
|
|
50
|
+
Requires-Dist: matplotlib>=3.8; extra == 'plot'
|
|
51
|
+
Description-Content-Type: text/markdown
|
|
52
|
+
|
|
53
|
+
# TranPy
|
|
54
|
+
|
|
55
|
+
<!-- intro-start -->
|
|
56
|
+
Machine learning for power-system transient stability.
|
|
57
|
+
<!-- intro-end -->
|
|
58
|
+
|
|
59
|
+
## Installation
|
|
60
|
+
|
|
61
|
+
```bash
|
|
62
|
+
pip install tranpy-ai
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
Optional extras: `tranpy-ai[explain]`, `tranpy-ai[neural]`, `tranpy-ai[boosting]`, `tranpy-ai[plot]`, or `tranpy-ai[all]`.
|
|
66
|
+
|
|
67
|
+
Requires Python 3.11 or newer.
|
|
68
|
+
|
|
69
|
+
## Get started
|
|
70
|
+
|
|
71
|
+
```python
|
|
72
|
+
import tranpy
|
|
73
|
+
from tranpy_core import evaluate, make_pipeline, train_test_split
|
|
74
|
+
|
|
75
|
+
data = tranpy.load_new_england()
|
|
76
|
+
split = train_test_split(data.labels, test_size=0.2, seed=42)
|
|
77
|
+
|
|
78
|
+
model = make_pipeline("random_forest", seed=0)
|
|
79
|
+
model.fit(data.features[split.train], data.labels[split.train])
|
|
80
|
+
|
|
81
|
+
scores = evaluate(model, data.features[split.test], data.labels[split.test])
|
|
82
|
+
print(scores.accuracy, scores.roc_auc)
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
## Example: early prediction
|
|
86
|
+
|
|
87
|
+
How soon after a fault can stability be predicted?
|
|
88
|
+
|
|
89
|
+
```python
|
|
90
|
+
import tranpy
|
|
91
|
+
from tranpy.early_prediction import run_early_prediction
|
|
92
|
+
from tranpy_core.models import make_pipeline
|
|
93
|
+
|
|
94
|
+
events = tranpy.open_event_shards("ieee_9bus")
|
|
95
|
+
|
|
96
|
+
results = run_early_prediction(
|
|
97
|
+
events,
|
|
98
|
+
{"random forest": make_pipeline("random_forest", seed=0)},
|
|
99
|
+
reference="fault_start",
|
|
100
|
+
windows_s=(0.05, 0.1, 0.2),
|
|
101
|
+
)
|
|
102
|
+
print(results[["window_s", "model", "accuracy_mean"]])
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
## Learn more
|
|
106
|
+
|
|
107
|
+
- [Examples](examples/)
|
|
108
|
+
- [Architecture](docs/architecture.md)
|
|
109
|
+
- [Data](docs/data.md)
|
|
110
|
+
- [Validation](docs/validation.md)
|
|
111
|
+
|
|
112
|
+
## License
|
|
113
|
+
|
|
114
|
+
MIT
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
# TranPy
|
|
2
|
+
|
|
3
|
+
<!-- intro-start -->
|
|
4
|
+
Machine learning for power-system transient stability.
|
|
5
|
+
<!-- intro-end -->
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
pip install tranpy-ai
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
Optional extras: `tranpy-ai[explain]`, `tranpy-ai[neural]`, `tranpy-ai[boosting]`, `tranpy-ai[plot]`, or `tranpy-ai[all]`.
|
|
14
|
+
|
|
15
|
+
Requires Python 3.11 or newer.
|
|
16
|
+
|
|
17
|
+
## Get started
|
|
18
|
+
|
|
19
|
+
```python
|
|
20
|
+
import tranpy
|
|
21
|
+
from tranpy_core import evaluate, make_pipeline, train_test_split
|
|
22
|
+
|
|
23
|
+
data = tranpy.load_new_england()
|
|
24
|
+
split = train_test_split(data.labels, test_size=0.2, seed=42)
|
|
25
|
+
|
|
26
|
+
model = make_pipeline("random_forest", seed=0)
|
|
27
|
+
model.fit(data.features[split.train], data.labels[split.train])
|
|
28
|
+
|
|
29
|
+
scores = evaluate(model, data.features[split.test], data.labels[split.test])
|
|
30
|
+
print(scores.accuracy, scores.roc_auc)
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## Example: early prediction
|
|
34
|
+
|
|
35
|
+
How soon after a fault can stability be predicted?
|
|
36
|
+
|
|
37
|
+
```python
|
|
38
|
+
import tranpy
|
|
39
|
+
from tranpy.early_prediction import run_early_prediction
|
|
40
|
+
from tranpy_core.models import make_pipeline
|
|
41
|
+
|
|
42
|
+
events = tranpy.open_event_shards("ieee_9bus")
|
|
43
|
+
|
|
44
|
+
results = run_early_prediction(
|
|
45
|
+
events,
|
|
46
|
+
{"random forest": make_pipeline("random_forest", seed=0)},
|
|
47
|
+
reference="fault_start",
|
|
48
|
+
windows_s=(0.05, 0.1, 0.2),
|
|
49
|
+
)
|
|
50
|
+
print(results[["window_s", "model", "accuracy_mean"]])
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
## Learn more
|
|
54
|
+
|
|
55
|
+
- [Examples](examples/)
|
|
56
|
+
- [Architecture](docs/architecture.md)
|
|
57
|
+
- [Data](docs/data.md)
|
|
58
|
+
- [Validation](docs/validation.md)
|
|
59
|
+
|
|
60
|
+
## License
|
|
61
|
+
|
|
62
|
+
MIT
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
# Documentation sources
|
|
2
|
+
|
|
3
|
+
Build locally:
|
|
4
|
+
|
|
5
|
+
```bash
|
|
6
|
+
uv sync --group docs
|
|
7
|
+
uv run sphinx-build -W -b html docs docs/_build/html
|
|
8
|
+
```
|
|
9
|
+
|
|
10
|
+
`index.md` is the entry page; `architecture.md` and `validation.md` are the main guides;
|
|
11
|
+
`api.md` renders the API reference from the docstrings.
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling>=1.24"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "tranpy-ai"
|
|
7
|
+
version = "0.2.0.dev0"
|
|
8
|
+
description = "Machine learning for power-system transient stability: datasets, models, evaluation and explanation"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = "MIT"
|
|
11
|
+
authors = [{ name = "Mahmoud Draz", email = "m.draz@tu-berlin.de" }]
|
|
12
|
+
requires-python = ">=3.11"
|
|
13
|
+
dependencies = [
|
|
14
|
+
"numpy>=1.26",
|
|
15
|
+
"pandas>=2.1",
|
|
16
|
+
"scipy>=1.11",
|
|
17
|
+
"scikit-learn>=1.5,<1.11", # SVC(probability=True) is removed in 1.11
|
|
18
|
+
"pyyaml>=6.0",
|
|
19
|
+
"platformdirs>=4.0",
|
|
20
|
+
]
|
|
21
|
+
classifiers = [
|
|
22
|
+
"Development Status :: 3 - Alpha",
|
|
23
|
+
"Intended Audience :: Science/Research",
|
|
24
|
+
"License :: OSI Approved :: MIT License",
|
|
25
|
+
"Programming Language :: Python :: 3",
|
|
26
|
+
"Programming Language :: Python :: 3.11",
|
|
27
|
+
"Programming Language :: Python :: 3.12",
|
|
28
|
+
"Programming Language :: Python :: 3.13",
|
|
29
|
+
"Topic :: Scientific/Engineering :: Artificial Intelligence",
|
|
30
|
+
"Typing :: Typed",
|
|
31
|
+
]
|
|
32
|
+
keywords = ["power-system", "transient-stability", "machine-learning", "explainability"]
|
|
33
|
+
|
|
34
|
+
[project.urls]
|
|
35
|
+
Homepage = "https://github.com/mahmouddraz/tranpy"
|
|
36
|
+
Repository = "https://github.com/mahmouddraz/tranpy"
|
|
37
|
+
Issues = "https://github.com/mahmouddraz/tranpy/issues"
|
|
38
|
+
|
|
39
|
+
[project.optional-dependencies]
|
|
40
|
+
explain = ["shap>=0.45", "lime>=0.2.0.1", "dalex>=1.7", "setuptools<85"]
|
|
41
|
+
neural = ["tensorflow>=2.16"]
|
|
42
|
+
boosting = ["xgboost>=2.0", "lightgbm>=4.0"]
|
|
43
|
+
plot = ["matplotlib>=3.8"]
|
|
44
|
+
persistence = ["skops>=0.10"]
|
|
45
|
+
all = ["tranpy-ai[explain,neural,boosting,plot,persistence]"]
|
|
46
|
+
|
|
47
|
+
[project.scripts]
|
|
48
|
+
tranpy = "tranpy.cli:main"
|
|
49
|
+
|
|
50
|
+
[dependency-groups]
|
|
51
|
+
dev = [
|
|
52
|
+
"pytest>=8.0",
|
|
53
|
+
"pytest-cov>=5.0",
|
|
54
|
+
"hypothesis>=6.100",
|
|
55
|
+
"ruff>=0.6",
|
|
56
|
+
"mypy>=1.11",
|
|
57
|
+
"pandas-stubs>=2.1",
|
|
58
|
+
"types-PyYAML>=6.0",
|
|
59
|
+
]
|
|
60
|
+
notebooks = ["jupyter>=1.0", "nbconvert>=7.16", "ipykernel>=6.29"]
|
|
61
|
+
docs = ["sphinx>=7.0", "myst-parser>=3.0", "sphinx-rtd-theme>=2.0"]
|
|
62
|
+
|
|
63
|
+
[tool.hatch.build.targets.wheel]
|
|
64
|
+
packages = ["src/tranpy_core", "src/tranpy"]
|
|
65
|
+
exclude = ["src/tranpy/data/datasets/events"]
|
|
66
|
+
|
|
67
|
+
[tool.hatch.build.targets.sdist]
|
|
68
|
+
include = ["src", "tests", "README.md", "LICENSE", "pyproject.toml"]
|
|
69
|
+
exclude = ["src/tranpy/data/datasets/events"]
|
|
70
|
+
|
|
71
|
+
[tool.ruff]
|
|
72
|
+
line-length = 100
|
|
73
|
+
target-version = "py311"
|
|
74
|
+
src = ["src", "tests"]
|
|
75
|
+
extend-exclude = ["papers", "examples/*.ipynb"]
|
|
76
|
+
|
|
77
|
+
[tool.ruff.lint]
|
|
78
|
+
select = ["E", "F", "W", "I", "B", "UP", "SIM", "NPY", "PT", "RUF", "C4", "PIE", "T20"]
|
|
79
|
+
ignore = ["RUF001", "RUF002", "RUF003"]
|
|
80
|
+
|
|
81
|
+
[tool.ruff.lint.per-file-ignores]
|
|
82
|
+
"tests/**" = ["T20", "PT018"]
|
|
83
|
+
"scripts/**" = ["T20"]
|
|
84
|
+
"src/tranpy/cli.py" = ["T20"]
|
|
85
|
+
|
|
86
|
+
[tool.mypy]
|
|
87
|
+
python_version = "3.11"
|
|
88
|
+
strict = true
|
|
89
|
+
files = ["src"]
|
|
90
|
+
warn_unreachable = true
|
|
91
|
+
|
|
92
|
+
[[tool.mypy.overrides]]
|
|
93
|
+
module = ["shap.*", "lime.*", "dalex.*", "xgboost.*", "lightgbm.*", "tensorflow.*", "keras.*", "sklearn.*", "scipy.*", "skops.*", "powerfactory", "matplotlib.*"]
|
|
94
|
+
ignore_missing_imports = true
|
|
95
|
+
|
|
96
|
+
[tool.pytest.ini_options]
|
|
97
|
+
testpaths = ["tests"]
|
|
98
|
+
addopts = ["--strict-markers", "--strict-config", "-ra", "--import-mode=importlib"]
|
|
99
|
+
filterwarnings = [
|
|
100
|
+
"error",
|
|
101
|
+
# expected with the documented default hyperparameters on small test data
|
|
102
|
+
"ignore::sklearn.exceptions.ConvergenceWarning",
|
|
103
|
+
# SVC(probability=True) is kept for continuity of results until scikit-learn 1.11
|
|
104
|
+
"ignore:The `probability` parameter was deprecated:FutureWarning",
|
|
105
|
+
]
|
|
106
|
+
markers = [
|
|
107
|
+
"slow: long-running test (deselect with -m 'not slow')",
|
|
108
|
+
"requires_explain: needs the [explain] extra",
|
|
109
|
+
"requires_neural: needs the [neural] extra",
|
|
110
|
+
"requires_boosting: needs the [boosting] extra",
|
|
111
|
+
"requires_plot: needs the [plot] extra",
|
|
112
|
+
"requires_persistence: needs the [persistence] extra",
|
|
113
|
+
]
|
|
114
|
+
|
|
115
|
+
[tool.coverage.run]
|
|
116
|
+
source = ["tranpy_core", "tranpy"]
|
|
117
|
+
branch = true
|
|
118
|
+
omit = []
|
|
119
|
+
|
|
120
|
+
[tool.coverage.report]
|
|
121
|
+
fail_under = 90
|
|
122
|
+
show_missing = true
|
|
123
|
+
skip_covered = true
|
|
124
|
+
exclude_also = ["if TYPE_CHECKING:", "raise NotImplementedError", "@overload", "\\.\\.\\."]
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"""Machine learning for power-system transient stability.
|
|
2
|
+
|
|
3
|
+
The power-system layer on top of :mod:`tranpy_core`: test systems, datasets of simulated
|
|
4
|
+
fault events, trajectory features, early-prediction experiments and simulation engines.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from importlib.metadata import PackageNotFoundError, version
|
|
8
|
+
|
|
9
|
+
from tranpy.datasets import (
|
|
10
|
+
list_datasets,
|
|
11
|
+
load_events,
|
|
12
|
+
load_ieee_9bus,
|
|
13
|
+
load_new_england,
|
|
14
|
+
load_snapshots,
|
|
15
|
+
open_event_shards,
|
|
16
|
+
)
|
|
17
|
+
from tranpy.features import TrajectoryFeatures
|
|
18
|
+
from tranpy.systems import IEEE_9BUS, NEW_ENGLAND, PowerSystem, get_system
|
|
19
|
+
from tranpy.vocabulary import LABEL_NAMES, Stability
|
|
20
|
+
|
|
21
|
+
try:
|
|
22
|
+
__version__ = version("tranpy-ai")
|
|
23
|
+
except PackageNotFoundError: # pragma: no cover - source checkout without install
|
|
24
|
+
__version__ = "0+unknown"
|
|
25
|
+
|
|
26
|
+
__all__ = [
|
|
27
|
+
"IEEE_9BUS",
|
|
28
|
+
"LABEL_NAMES",
|
|
29
|
+
"NEW_ENGLAND",
|
|
30
|
+
"PowerSystem",
|
|
31
|
+
"Stability",
|
|
32
|
+
"TrajectoryFeatures",
|
|
33
|
+
"__version__",
|
|
34
|
+
"get_system",
|
|
35
|
+
"list_datasets",
|
|
36
|
+
"load_events",
|
|
37
|
+
"load_ieee_9bus",
|
|
38
|
+
"load_new_england",
|
|
39
|
+
"load_snapshots",
|
|
40
|
+
"open_event_shards",
|
|
41
|
+
]
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
"""Command-line interface: ``tranpy <command> ...`` (see ``tranpy --help``)."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import argparse
|
|
6
|
+
import json
|
|
7
|
+
import logging
|
|
8
|
+
import sys
|
|
9
|
+
from collections.abc import Sequence
|
|
10
|
+
from dataclasses import dataclass
|
|
11
|
+
from pathlib import Path
|
|
12
|
+
|
|
13
|
+
from tranpy.datasets import list_datasets, load_snapshots, read_event_csvs, write_event_shards
|
|
14
|
+
from tranpy.pretrained import train_pretrained
|
|
15
|
+
from tranpy.simulation import MockEngine, ScenarioSampler, generate_events
|
|
16
|
+
from tranpy_core.config import load_yaml
|
|
17
|
+
from tranpy_core.data import save_signals, sha256_file
|
|
18
|
+
from tranpy_core.errors import TranpyError
|
|
19
|
+
from tranpy_core.experiment import ExperimentSpec, run_experiment
|
|
20
|
+
|
|
21
|
+
__all__ = ["ExperimentConfig", "main"]
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
@dataclass(frozen=True)
|
|
25
|
+
class ExperimentConfig:
|
|
26
|
+
"""YAML file for ``tranpy experiment``: a snapshot dataset and an experiment spec."""
|
|
27
|
+
|
|
28
|
+
dataset: str
|
|
29
|
+
experiment: ExperimentSpec
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def _experiment(args: argparse.Namespace) -> None:
|
|
33
|
+
config = load_yaml(args.config, ExperimentConfig)
|
|
34
|
+
result = run_experiment(config.experiment, load_snapshots(config.dataset))
|
|
35
|
+
for name, scores in result.test_scores.items():
|
|
36
|
+
print(
|
|
37
|
+
f"{name:24s} accuracy={scores.accuracy:.4f} f1={scores.f1:.4f} "
|
|
38
|
+
f"roc_auc={scores.roc_auc:.4f}"
|
|
39
|
+
)
|
|
40
|
+
if args.output:
|
|
41
|
+
print(f"written {result.save_json(args.output)}")
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def _datasets(_: argparse.Namespace) -> None:
|
|
45
|
+
print(list_datasets().to_string(index=False))
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def _convert_events(args: argparse.Namespace) -> None:
|
|
49
|
+
path = save_signals(read_event_csvs(args.csv_dir, args.system), args.output)
|
|
50
|
+
print(f"written {path} sha256={sha256_file(path)}")
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def _build_shards(args: argparse.Namespace) -> None:
|
|
54
|
+
manifest = write_event_shards(
|
|
55
|
+
args.csv_dir, args.system, args.output, shard_size=args.shard_size
|
|
56
|
+
)
|
|
57
|
+
print(f"{args.output}: {len(manifest.shards)} shards, {manifest.n_samples} events")
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def _simulate(args: argparse.Namespace) -> None:
|
|
61
|
+
engine = MockEngine(args.system, simulation_time_s=args.duration)
|
|
62
|
+
sampler = ScenarioSampler(
|
|
63
|
+
n_lines=engine.n_lines,
|
|
64
|
+
n_loads=engine.n_loads,
|
|
65
|
+
simulation_time_s=args.duration,
|
|
66
|
+
clearing_cycles=args.clearing_cycles,
|
|
67
|
+
nominal_frequency_hz=engine.system.nominal_frequency_hz,
|
|
68
|
+
)
|
|
69
|
+
dataset = generate_events(engine, sampler.sample(args.events, seed=args.seed))
|
|
70
|
+
path = save_signals(dataset, args.output)
|
|
71
|
+
print(f"written {path}: {dataset}")
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def _train_pretrained(args: argparse.Namespace) -> None:
|
|
75
|
+
entries = train_pretrained(args.output, models=args.models or None)
|
|
76
|
+
print(json.dumps({k: v["test_accuracy"] for k, v in entries.items()}, indent=2))
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
def build_parser() -> argparse.ArgumentParser:
|
|
80
|
+
parser = argparse.ArgumentParser(prog="tranpy", description=__doc__)
|
|
81
|
+
parser.add_argument("-v", "--verbose", action="store_true", help="log progress")
|
|
82
|
+
sub = parser.add_subparsers(dest="command", required=True)
|
|
83
|
+
|
|
84
|
+
p = sub.add_parser("datasets", help="list available datasets")
|
|
85
|
+
p.set_defaults(func=_datasets)
|
|
86
|
+
|
|
87
|
+
p = sub.add_parser("experiment", help="run an experiment described in a YAML file")
|
|
88
|
+
p.add_argument("config", type=Path)
|
|
89
|
+
p.add_argument("-o", "--output", type=Path, help="write the result as JSON")
|
|
90
|
+
p.set_defaults(func=_experiment)
|
|
91
|
+
|
|
92
|
+
p = sub.add_parser("convert-events", help="convert PowerFactory result CSVs to .npz")
|
|
93
|
+
p.add_argument("csv_dir", type=Path)
|
|
94
|
+
p.add_argument("system")
|
|
95
|
+
p.add_argument("output", type=Path)
|
|
96
|
+
p.set_defaults(func=_convert_events)
|
|
97
|
+
|
|
98
|
+
p = sub.add_parser("build-shards", help="convert PowerFactory CSVs to a sharded dataset")
|
|
99
|
+
p.add_argument("csv_dir", type=Path)
|
|
100
|
+
p.add_argument("system")
|
|
101
|
+
p.add_argument("output", type=Path, help="output folder (manifest.json + shards)")
|
|
102
|
+
p.add_argument("--shard-size", type=int, default=250)
|
|
103
|
+
p.set_defaults(func=_build_shards)
|
|
104
|
+
|
|
105
|
+
p = sub.add_parser("simulate", help="generate an event dataset with the mock engine")
|
|
106
|
+
p.add_argument("system")
|
|
107
|
+
p.add_argument("output", type=Path)
|
|
108
|
+
p.add_argument("--events", type=int, default=100)
|
|
109
|
+
p.add_argument("--seed", type=int, default=0)
|
|
110
|
+
p.add_argument("--clearing-cycles", type=float, default=12.0)
|
|
111
|
+
p.add_argument("--duration", type=float, default=10.0, help="simulated time in s")
|
|
112
|
+
p.set_defaults(func=_simulate)
|
|
113
|
+
|
|
114
|
+
p = sub.add_parser("train-pretrained", help="train and save the pretrained pipelines")
|
|
115
|
+
p.add_argument("output", type=Path)
|
|
116
|
+
p.add_argument("--models", nargs="*", help="registry names (default: all installed)")
|
|
117
|
+
p.set_defaults(func=_train_pretrained)
|
|
118
|
+
return parser
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
def main(argv: Sequence[str] | None = None) -> int:
|
|
122
|
+
args = build_parser().parse_args(argv)
|
|
123
|
+
logging.basicConfig(
|
|
124
|
+
level=logging.INFO if args.verbose else logging.WARNING, format="%(message)s"
|
|
125
|
+
)
|
|
126
|
+
try:
|
|
127
|
+
args.func(args)
|
|
128
|
+
except TranpyError as exc:
|
|
129
|
+
print(f"error: {exc}", file=sys.stderr)
|
|
130
|
+
return 1
|
|
131
|
+
return 0
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
if __name__ == "__main__": # pragma: no cover
|
|
135
|
+
raise SystemExit(main())
|