stagereg 0.1.0__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.
Files changed (78) hide show
  1. stagereg-0.1.0/LICENSE +28 -0
  2. stagereg-0.1.0/PKG-INFO +115 -0
  3. stagereg-0.1.0/README.md +87 -0
  4. stagereg-0.1.0/pyproject.toml +106 -0
  5. stagereg-0.1.0/pyproject.toml.orig +101 -0
  6. stagereg-0.1.0/src/stagereg/__init__.py +1 -0
  7. stagereg-0.1.0/src/stagereg/core/__init__.py +149 -0
  8. stagereg-0.1.0/src/stagereg/core/checkpoints.py +100 -0
  9. stagereg-0.1.0/src/stagereg/core/distributions.py +60 -0
  10. stagereg-0.1.0/src/stagereg/core/early_stopping.py +252 -0
  11. stagereg-0.1.0/src/stagereg/core/losses.py +130 -0
  12. stagereg-0.1.0/src/stagereg/core/metric_registry.py +545 -0
  13. stagereg-0.1.0/src/stagereg/core/metrics.py +168 -0
  14. stagereg-0.1.0/src/stagereg/core/naming.py +138 -0
  15. stagereg-0.1.0/src/stagereg/core/protocols.py +52 -0
  16. stagereg-0.1.0/src/stagereg/core/stages.py +46 -0
  17. stagereg-0.1.0/src/stagereg/core/tracking.py +143 -0
  18. stagereg-0.1.0/src/stagereg/core/types.py +228 -0
  19. stagereg-0.1.0/src/stagereg/jax/__init__.py +103 -0
  20. stagereg-0.1.0/src/stagereg/jax/bayes_by_backprop.py +367 -0
  21. stagereg-0.1.0/src/stagereg/jax/distributions.py +496 -0
  22. stagereg-0.1.0/src/stagereg/jax/epoch_loop.py +516 -0
  23. stagereg-0.1.0/src/stagereg/jax/evaluation.py +198 -0
  24. stagereg-0.1.0/src/stagereg/jax/losses.py +106 -0
  25. stagereg-0.1.0/src/stagereg/jax/metrics.py +758 -0
  26. stagereg-0.1.0/src/stagereg/jax/posterior.py +249 -0
  27. stagereg-0.1.0/src/stagereg/jax/posterior_stage.py +688 -0
  28. stagereg-0.1.0/src/stagereg/jax/psgld.py +337 -0
  29. stagereg-0.1.0/src/stagereg/jax/rng.py +20 -0
  30. stagereg-0.1.0/src/stagereg/jax/stage_checkpoints.py +215 -0
  31. stagereg-0.1.0/src/stagereg/jax/state.py +283 -0
  32. stagereg-0.1.0/src/stagereg/jax/supervised.py +209 -0
  33. stagereg-0.1.0/src/stagereg/jax/supervised_staged.py +731 -0
  34. stagereg-0.1.0/src/stagereg/jax/validation.py +61 -0
  35. stagereg-0.1.0/src/stagereg/py.typed +0 -0
  36. stagereg-0.1.0/tests/conftest.py +225 -0
  37. stagereg-0.1.0/tests/core/__init__.py +1 -0
  38. stagereg-0.1.0/tests/core/conftest.py +41 -0
  39. stagereg-0.1.0/tests/core/test_checkpoints.py +68 -0
  40. stagereg-0.1.0/tests/core/test_distributions.py +41 -0
  41. stagereg-0.1.0/tests/core/test_early_stopping.py +195 -0
  42. stagereg-0.1.0/tests/core/test_losses.py +121 -0
  43. stagereg-0.1.0/tests/core/test_metric_registry.py +304 -0
  44. stagereg-0.1.0/tests/core/test_metrics.py +160 -0
  45. stagereg-0.1.0/tests/core/test_naming.py +197 -0
  46. stagereg-0.1.0/tests/core/test_protocols.py +73 -0
  47. stagereg-0.1.0/tests/core/test_stages.py +66 -0
  48. stagereg-0.1.0/tests/core/test_tracking.py +164 -0
  49. stagereg-0.1.0/tests/core/test_types.py +76 -0
  50. stagereg-0.1.0/tests/jax/cmapss_trajectories.py +85 -0
  51. stagereg-0.1.0/tests/jax/conftest.py +167 -0
  52. stagereg-0.1.0/tests/jax/posterior_runs.py +190 -0
  53. stagereg-0.1.0/tests/jax/test_bayes_by_backprop.py +207 -0
  54. stagereg-0.1.0/tests/jax/test_bayes_by_backprop_stage.py +89 -0
  55. stagereg-0.1.0/tests/jax/test_cmapss_data.py +339 -0
  56. stagereg-0.1.0/tests/jax/test_cmapss_model.py +81 -0
  57. stagereg-0.1.0/tests/jax/test_cmapss_plots.py +340 -0
  58. stagereg-0.1.0/tests/jax/test_cmapss_preprocessing.py +458 -0
  59. stagereg-0.1.0/tests/jax/test_cmapss_run.py +271 -0
  60. stagereg-0.1.0/tests/jax/test_distributions.py +352 -0
  61. stagereg-0.1.0/tests/jax/test_epoch_loop.py +185 -0
  62. stagereg-0.1.0/tests/jax/test_losses.py +70 -0
  63. stagereg-0.1.0/tests/jax/test_metrics.py +570 -0
  64. stagereg-0.1.0/tests/jax/test_mve.py +76 -0
  65. stagereg-0.1.0/tests/jax/test_posterior.py +65 -0
  66. stagereg-0.1.0/tests/jax/test_posterior_stage.py +793 -0
  67. stagereg-0.1.0/tests/jax/test_psgld.py +317 -0
  68. stagereg-0.1.0/tests/jax/test_state.py +156 -0
  69. stagereg-0.1.0/tests/jax/test_supervised.py +849 -0
  70. stagereg-0.1.0/tests/jax/test_supervised_staged.py +1398 -0
  71. stagereg-0.1.0/tests/jax/test_tracking_run.py +71 -0
  72. stagereg-0.1.0/tests/jax/test_tracking_tracker.py +265 -0
  73. stagereg-0.1.0/tests/jax/test_xsin_examples.py +169 -0
  74. stagereg-0.1.0/tests/test_built_distribution.py +210 -0
  75. stagereg-0.1.0/tests/test_dataclass_attributes.py +90 -0
  76. stagereg-0.1.0/tests/test_docstring_cross_references.py +29 -0
  77. stagereg-0.1.0/tests/test_reference_completeness.py +114 -0
  78. stagereg-0.1.0/tests/test_rename_completeness.py +53 -0
stagereg-0.1.0/LICENSE ADDED
@@ -0,0 +1,28 @@
1
+ BSD 3-Clause License
2
+
3
+ Copyright (c) 2026, Intelligent System Prognostics for Operations & Maintenance Research Group
4
+
5
+ Redistribution and use in source and binary forms, with or without
6
+ modification, are permitted provided that the following conditions are met:
7
+
8
+ 1. Redistributions of source code must retain the above copyright notice, this
9
+ list of conditions and the following disclaimer.
10
+
11
+ 2. Redistributions in binary form must reproduce the above copyright notice,
12
+ this list of conditions and the following disclaimer in the documentation
13
+ and/or other materials provided with the distribution.
14
+
15
+ 3. Neither the name of the copyright holder nor the names of its
16
+ contributors may be used to endorse or promote products derived from
17
+ this software without specific prior written permission.
18
+
19
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
@@ -0,0 +1,115 @@
1
+ Metadata-Version: 2.4
2
+ Name: stagereg
3
+ Version: 0.1.0
4
+ Summary: Stage-oriented probabilistic regression
5
+ Author: Giorgio Tosti Balducci, Intelligent System Prognostics for Operations & Maintenance Research Group
6
+ Author-email: Giorgio Tosti Balducci <g.b.l.tostibalducci@tudelft.nl>
7
+ License-Expression: BSD-3-Clause
8
+ License-File: LICENSE
9
+ Classifier: Development Status :: 3 - Alpha
10
+ Classifier: Intended Audience :: Science/Research
11
+ Classifier: Programming Language :: Python :: 3
12
+ Classifier: Programming Language :: Python :: 3 :: Only
13
+ Classifier: Programming Language :: Python :: 3.12
14
+ Classifier: Programming Language :: Python :: 3.13
15
+ Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
16
+ Requires-Dist: numpy>=2.0
17
+ Requires-Dist: flax>=0.11 ; extra == 'jax'
18
+ Requires-Dist: jax>=0.6 ; extra == 'jax'
19
+ Requires-Dist: optax>=0.2.4 ; extra == 'jax'
20
+ Requires-Dist: matplotlib>=3.9 ; extra == 'plot'
21
+ Requires-Python: >=3.12
22
+ Project-URL: Repository, https://github.com/GroupiSP/stagereg
23
+ Project-URL: Documentation, https://groupisp.github.io/stagereg/
24
+ Project-URL: Issues, https://github.com/GroupiSP/stagereg/issues
25
+ Provides-Extra: jax
26
+ Provides-Extra: plot
27
+ Description-Content-Type: text/markdown
28
+
29
+ # stagereg
30
+
31
+ [![PyPI version](https://img.shields.io/pypi/v/stagereg)](https://pypi.org/project/stagereg/)
32
+ [![Python versions](https://img.shields.io/pypi/pyversions/stagereg)](https://pypi.org/project/stagereg/)
33
+ [![License: BSD-3-Clause](https://img.shields.io/badge/license-BSD--3--Clause-blue)](https://github.com/GroupiSP/stagereg/blob/main/LICENSE)
34
+
35
+ `stagereg` is a library for stage-oriented probabilistic regression: training
36
+ models that predict a distribution over the target, not just a point estimate.
37
+ Training runs as a sequence of explicit, checkpointed stages, such as fitting a
38
+ mean model and then a separate variance model on its residuals. A core of
39
+ backend-neutral contracts is implemented by an optional JAX backend built on
40
+ Flax NNX and Optax.
41
+
42
+ **Documentation:** <https://groupisp.github.io/stagereg/>
43
+
44
+ ## Install
45
+
46
+ <!-- --8<-- [start:install] -->
47
+ Install with the JAX backend (JAX, Flax and Optax):
48
+
49
+ ```bash
50
+ pip install "stagereg[jax]"
51
+ ```
52
+
53
+ The optional `plot` extra also installs matplotlib, for plotting results:
54
+
55
+ ```bash
56
+ pip install "stagereg[jax,plot]"
57
+ ```
58
+
59
+ With uv, use the equivalent `uv add` form:
60
+
61
+ ```bash
62
+ uv add "stagereg[jax]"
63
+ uv add "stagereg[jax,plot]"
64
+ ```
65
+
66
+ Drop the `[jax]` extra for the backend-neutral contracts alone, which depend
67
+ only on NumPy.
68
+
69
+ The documentation tracks the `main` branch, with no per-release docs before
70
+ 1.0, so it may describe features newer than your installed version.
71
+ <!-- --8<-- [end:install] -->
72
+
73
+ ## Minimal example
74
+
75
+ <!-- --8<-- [start:minimal-example] -->
76
+ Fit a linear model to `y = 3x + 2` for a fixed number of epochs with the JAX
77
+ backend:
78
+
79
+ ```python
80
+ import jax
81
+ import jax.numpy as jnp
82
+ import optax
83
+ from flax import nnx
84
+
85
+ from stagereg.core.types import Batch
86
+ from stagereg.jax import create_optimizer, initialize_training_state, run_supervised
87
+
88
+
89
+ def squared_error(model, inputs, targets, sample_weight, key, training):
90
+ return jnp.mean(jnp.square(model(inputs) - targets))
91
+
92
+
93
+ inputs = jnp.linspace(-1.0, 1.0, 32).reshape(-1, 1)
94
+ targets = 3.0 * inputs + 2.0
95
+
96
+
97
+ def loader(*, split, epoch):
98
+ return [Batch(inputs=inputs, targets=targets)]
99
+
100
+
101
+ model = nnx.Linear(1, 1, rngs=nnx.Rngs(0))
102
+ optimizer = create_optimizer(model, optax.sgd(learning_rate=0.1))
103
+ state = initialize_training_state(model, optimizer, rng_key=jax.random.key(0))
104
+
105
+ result = run_supervised(
106
+ model=model,
107
+ optimizer=optimizer,
108
+ train_loader=loader,
109
+ loss=squared_error,
110
+ state=state,
111
+ epochs=3,
112
+ )
113
+ print(f"final training loss: {result.loss:.4f}")
114
+ ```
115
+ <!-- --8<-- [end:minimal-example] -->
@@ -0,0 +1,87 @@
1
+ # stagereg
2
+
3
+ [![PyPI version](https://img.shields.io/pypi/v/stagereg)](https://pypi.org/project/stagereg/)
4
+ [![Python versions](https://img.shields.io/pypi/pyversions/stagereg)](https://pypi.org/project/stagereg/)
5
+ [![License: BSD-3-Clause](https://img.shields.io/badge/license-BSD--3--Clause-blue)](https://github.com/GroupiSP/stagereg/blob/main/LICENSE)
6
+
7
+ `stagereg` is a library for stage-oriented probabilistic regression: training
8
+ models that predict a distribution over the target, not just a point estimate.
9
+ Training runs as a sequence of explicit, checkpointed stages, such as fitting a
10
+ mean model and then a separate variance model on its residuals. A core of
11
+ backend-neutral contracts is implemented by an optional JAX backend built on
12
+ Flax NNX and Optax.
13
+
14
+ **Documentation:** <https://groupisp.github.io/stagereg/>
15
+
16
+ ## Install
17
+
18
+ <!-- --8<-- [start:install] -->
19
+ Install with the JAX backend (JAX, Flax and Optax):
20
+
21
+ ```bash
22
+ pip install "stagereg[jax]"
23
+ ```
24
+
25
+ The optional `plot` extra also installs matplotlib, for plotting results:
26
+
27
+ ```bash
28
+ pip install "stagereg[jax,plot]"
29
+ ```
30
+
31
+ With uv, use the equivalent `uv add` form:
32
+
33
+ ```bash
34
+ uv add "stagereg[jax]"
35
+ uv add "stagereg[jax,plot]"
36
+ ```
37
+
38
+ Drop the `[jax]` extra for the backend-neutral contracts alone, which depend
39
+ only on NumPy.
40
+
41
+ The documentation tracks the `main` branch, with no per-release docs before
42
+ 1.0, so it may describe features newer than your installed version.
43
+ <!-- --8<-- [end:install] -->
44
+
45
+ ## Minimal example
46
+
47
+ <!-- --8<-- [start:minimal-example] -->
48
+ Fit a linear model to `y = 3x + 2` for a fixed number of epochs with the JAX
49
+ backend:
50
+
51
+ ```python
52
+ import jax
53
+ import jax.numpy as jnp
54
+ import optax
55
+ from flax import nnx
56
+
57
+ from stagereg.core.types import Batch
58
+ from stagereg.jax import create_optimizer, initialize_training_state, run_supervised
59
+
60
+
61
+ def squared_error(model, inputs, targets, sample_weight, key, training):
62
+ return jnp.mean(jnp.square(model(inputs) - targets))
63
+
64
+
65
+ inputs = jnp.linspace(-1.0, 1.0, 32).reshape(-1, 1)
66
+ targets = 3.0 * inputs + 2.0
67
+
68
+
69
+ def loader(*, split, epoch):
70
+ return [Batch(inputs=inputs, targets=targets)]
71
+
72
+
73
+ model = nnx.Linear(1, 1, rngs=nnx.Rngs(0))
74
+ optimizer = create_optimizer(model, optax.sgd(learning_rate=0.1))
75
+ state = initialize_training_state(model, optimizer, rng_key=jax.random.key(0))
76
+
77
+ result = run_supervised(
78
+ model=model,
79
+ optimizer=optimizer,
80
+ train_loader=loader,
81
+ loss=squared_error,
82
+ state=state,
83
+ epochs=3,
84
+ )
85
+ print(f"final training loss: {result.loss:.4f}")
86
+ ```
87
+ <!-- --8<-- [end:minimal-example] -->
@@ -0,0 +1,106 @@
1
+ [project]
2
+ name = "stagereg"
3
+ version = "0.1.0"
4
+ description = "Stage-oriented probabilistic regression"
5
+ readme = "README.md"
6
+ license = "BSD-3-Clause"
7
+ license-files = ["LICENSE"]
8
+ requires-python = ">=3.12"
9
+ classifiers = [
10
+ "Development Status :: 3 - Alpha",
11
+ "Intended Audience :: Science/Research",
12
+ "Programming Language :: Python :: 3",
13
+ "Programming Language :: Python :: 3 :: Only",
14
+ "Programming Language :: Python :: 3.12",
15
+ "Programming Language :: Python :: 3.13",
16
+ "Topic :: Scientific/Engineering :: Artificial Intelligence",
17
+ ]
18
+ dependencies = ["numpy>=2.0"]
19
+
20
+ [[project.authors]]
21
+ name = "Giorgio Tosti Balducci"
22
+ email = "g.b.l.tostibalducci@tudelft.nl"
23
+
24
+ [[project.authors]]
25
+ name = "Intelligent System Prognostics for Operations & Maintenance Research Group"
26
+
27
+ [project.optional-dependencies]
28
+ jax = [
29
+ "flax>=0.11",
30
+ "jax>=0.6",
31
+ "optax>=0.2.4",
32
+ ]
33
+ plot = ["matplotlib>=3.9"]
34
+
35
+ [project.urls]
36
+ Repository = "https://github.com/GroupiSP/stagereg"
37
+ Documentation = "https://groupisp.github.io/stagereg/"
38
+ Issues = "https://github.com/GroupiSP/stagereg/issues"
39
+
40
+ [build-system]
41
+ requires = ["uv_build>=0.12.3,<0.13.0"]
42
+ build-backend = "uv_build"
43
+
44
+ [tool.uv.build-backend]
45
+ source-include = ["tests/**"]
46
+ source-exclude = [
47
+ "**/__pycache__",
48
+ "**/*.pyc",
49
+ ]
50
+
51
+ [tool.docformatter]
52
+ recursive = true
53
+ black = true
54
+ style = "google"
55
+
56
+ [tool.pytest.ini_options]
57
+ testpaths = [
58
+ "tests",
59
+ "docs_site",
60
+ "README.md",
61
+ ]
62
+ addopts = [
63
+ "--markdown-docs",
64
+ "--markdown-docs-syntax=superfences",
65
+ ]
66
+
67
+ [tool.pyright]
68
+ typeCheckingMode = "basic"
69
+ reportUnusedVariable = false
70
+
71
+ [tool.ruff.lint.per-file-ignores]
72
+ "examples/jax/cmapss/*.py" = ["E402"]
73
+ "tests/jax/test_cmapss_*.py" = ["E402"]
74
+ "examples/jax/tracking/*.py" = ["E402"]
75
+
76
+ [dependency-groups]
77
+ dev = [
78
+ "docformatter>=1.7.7",
79
+ "hypothesis>=6.140.4",
80
+ "pre-commit>=4.5.1",
81
+ "pytest>=9.0.2",
82
+ "pytest-markdown-docs>=0.9.2",
83
+ "pyyaml>=6.0",
84
+ "ruff>=0.14.9",
85
+ ]
86
+ docs = [
87
+ "mkdocs-material>=9.7.7",
88
+ "mkdocstrings[python]>=1.0.6",
89
+ ]
90
+ example-cmapss = [
91
+ "flax>=0.11",
92
+ "jax>=0.6",
93
+ "optax>=0.2.4",
94
+ "pandas>=2.2",
95
+ "scikit-learn>=1.9.0",
96
+ "seaborn==0.13.2",
97
+ "matplotlib>=3.11.0",
98
+ ]
99
+ example-tracking = [
100
+ "flax>=0.11",
101
+ "jax>=0.6",
102
+ "optax>=0.2.4",
103
+ "matplotlib>=3.11.0",
104
+ "tensorboard>=2.20",
105
+ "tensorboardX>=2.6.4",
106
+ ]
@@ -0,0 +1,101 @@
1
+ [project]
2
+ name = "stagereg"
3
+ version = "0.1.0"
4
+ description = "Stage-oriented probabilistic regression"
5
+ readme = "README.md"
6
+ authors = [
7
+ { name = "Giorgio Tosti Balducci", email = "g.b.l.tostibalducci@tudelft.nl" },
8
+ { name = "Intelligent System Prognostics for Operations & Maintenance Research Group" },
9
+ ]
10
+ license = "BSD-3-Clause"
11
+ license-files = ["LICENSE"]
12
+ requires-python = ">=3.12"
13
+ classifiers = [
14
+ "Development Status :: 3 - Alpha",
15
+ "Intended Audience :: Science/Research",
16
+ "Programming Language :: Python :: 3",
17
+ "Programming Language :: Python :: 3 :: Only",
18
+ "Programming Language :: Python :: 3.12",
19
+ "Programming Language :: Python :: 3.13",
20
+ "Topic :: Scientific/Engineering :: Artificial Intelligence",
21
+ ]
22
+ dependencies = [
23
+ "numpy>=2.0",
24
+ ]
25
+
26
+ [project.optional-dependencies]
27
+ jax = [
28
+ "flax>=0.11",
29
+ "jax>=0.6",
30
+ "optax>=0.2.4",
31
+ ]
32
+ plot = [
33
+ "matplotlib>=3.9",
34
+ ]
35
+
36
+ [project.urls]
37
+ Repository = "https://github.com/GroupiSP/stagereg"
38
+ Documentation = "https://groupisp.github.io/stagereg/"
39
+ Issues = "https://github.com/GroupiSP/stagereg/issues"
40
+
41
+ [build-system]
42
+ requires = ["uv_build>=0.12.3,<0.13.0"]
43
+ build-backend = "uv_build"
44
+
45
+ [tool.uv.build-backend]
46
+ # The sdist also carries the tests, so downstream packagers can check the build. The examples
47
+ # stay in the repository, so the tests that exercise them need a repository checkout.
48
+ source-include = ["tests/**"]
49
+ source-exclude = ["**/__pycache__", "**/*.pyc"]
50
+
51
+ [dependency-groups]
52
+ dev = [
53
+ "docformatter>=1.7.7",
54
+ "hypothesis>=6.140.4",
55
+ "pre-commit>=4.5.1",
56
+ "pytest>=9.0.2",
57
+ "pytest-markdown-docs>=0.9.2",
58
+ "pyyaml>=6.0",
59
+ "ruff>=0.14.9",
60
+ ]
61
+ docs = [
62
+ "mkdocs-material>=9.7.7",
63
+ "mkdocstrings[python]>=1.0.6",
64
+ ]
65
+
66
+ example-cmapss = [
67
+ "flax>=0.11",
68
+ "jax>=0.6",
69
+ "optax>=0.2.4",
70
+ "pandas>=2.2",
71
+ "scikit-learn>=1.9.0",
72
+ "seaborn==0.13.2",
73
+ "matplotlib>=3.11.0"
74
+ ]
75
+
76
+ example-tracking = [
77
+ "flax>=0.11",
78
+ "jax>=0.6",
79
+ "optax>=0.2.4",
80
+ "matplotlib>=3.11.0",
81
+ "tensorboard>=2.20",
82
+ "tensorboardX>=2.6.4",
83
+ ]
84
+
85
+ [tool.docformatter]
86
+ recursive = true
87
+ black = true
88
+ style = "google"
89
+
90
+ [tool.pytest.ini_options]
91
+ testpaths = ["tests", "docs_site", "README.md"]
92
+ addopts = ["--markdown-docs", "--markdown-docs-syntax=superfences"]
93
+
94
+ [tool.pyright]
95
+ typeCheckingMode = "basic"
96
+ reportUnusedVariable = false
97
+
98
+ [tool.ruff.lint.per-file-ignores]
99
+ "examples/jax/cmapss/*.py" = ["E402"]
100
+ "tests/jax/test_cmapss_*.py" = ["E402"]
101
+ "examples/jax/tracking/*.py" = ["E402"]
@@ -0,0 +1 @@
1
+ """Probabilistic regression with explicit, composable training stages."""
@@ -0,0 +1,149 @@
1
+ """Backend-neutral contracts and utilities for probabilistic regression."""
2
+
3
+ from stagereg.core.checkpoints import (
4
+ Checkpoint,
5
+ CheckpointStore,
6
+ InMemoryCheckpointStore,
7
+ )
8
+ from stagereg.core.distributions import (
9
+ DistributionHead,
10
+ DistributionLoss,
11
+ Likelihood,
12
+ Loss,
13
+ PredictionLoss,
14
+ PredictiveDistribution,
15
+ )
16
+ from stagereg.core.early_stopping import (
17
+ EarlyStopper,
18
+ EarlyStoppingDecision,
19
+ EarlyStoppingState,
20
+ OptimizationMode,
21
+ )
22
+ from stagereg.core.losses import (
23
+ BetaNLLLoss,
24
+ GaussianNLLLoss,
25
+ NegativeLogLikelihoodLoss,
26
+ SquaredErrorLoss,
27
+ add_epsilon,
28
+ )
29
+ from stagereg.core.metric_registry import (
30
+ ContinuousRankedProbabilityScore,
31
+ EpochMetric,
32
+ EpochPredictionData,
33
+ EvaluationGrid,
34
+ IntervalCoverage,
35
+ MetricRequirements,
36
+ NegativeLogLikelihood,
37
+ PointContinuousRankedProbabilityScore,
38
+ PredictionInterval,
39
+ RootMeanSquaredError,
40
+ SampleContinuousRankedProbabilityScore,
41
+ WeightedSpread,
42
+ )
43
+ from stagereg.core.metrics import (
44
+ cdf,
45
+ coverage,
46
+ crps,
47
+ point_crps,
48
+ rmse,
49
+ sample_crps,
50
+ wsu,
51
+ )
52
+ from stagereg.core.naming import (
53
+ MetricTag,
54
+ Split,
55
+ flatten_parameters,
56
+ metric_tag,
57
+ parse_metric_tag,
58
+ )
59
+ from stagereg.core.protocols import (
60
+ Dataset,
61
+ LoaderFactory,
62
+ Optimizer,
63
+ Step,
64
+ ValidationStrategy,
65
+ )
66
+ from stagereg.core.stages import StageState, TrainingStage, validate_transition
67
+ from stagereg.core.tracking import (
68
+ Decision,
69
+ EventSink,
70
+ ExperimentTracker,
71
+ TrackerEventSink,
72
+ TrainingEvent,
73
+ )
74
+ from stagereg.core.types import (
75
+ Array,
76
+ Batch,
77
+ CheckpointRef,
78
+ ParameterRole,
79
+ PyTree,
80
+ StageResult,
81
+ TrainingState,
82
+ ValidationResult,
83
+ )
84
+
85
+ __all__ = [
86
+ "Array",
87
+ "Batch",
88
+ "BetaNLLLoss",
89
+ "Checkpoint",
90
+ "CheckpointRef",
91
+ "CheckpointStore",
92
+ "ContinuousRankedProbabilityScore",
93
+ "Dataset",
94
+ "Decision",
95
+ "DistributionHead",
96
+ "DistributionLoss",
97
+ "EarlyStopper",
98
+ "EarlyStoppingDecision",
99
+ "EarlyStoppingState",
100
+ "EpochMetric",
101
+ "EpochPredictionData",
102
+ "EvaluationGrid",
103
+ "EventSink",
104
+ "ExperimentTracker",
105
+ "GaussianNLLLoss",
106
+ "InMemoryCheckpointStore",
107
+ "IntervalCoverage",
108
+ "Likelihood",
109
+ "LoaderFactory",
110
+ "Loss",
111
+ "MetricRequirements",
112
+ "MetricTag",
113
+ "NegativeLogLikelihood",
114
+ "NegativeLogLikelihoodLoss",
115
+ "OptimizationMode",
116
+ "Optimizer",
117
+ "ParameterRole",
118
+ "PointContinuousRankedProbabilityScore",
119
+ "PredictionInterval",
120
+ "PredictionLoss",
121
+ "PredictiveDistribution",
122
+ "PyTree",
123
+ "RootMeanSquaredError",
124
+ "SampleContinuousRankedProbabilityScore",
125
+ "Split",
126
+ "SquaredErrorLoss",
127
+ "StageResult",
128
+ "StageState",
129
+ "Step",
130
+ "TrackerEventSink",
131
+ "TrainingEvent",
132
+ "TrainingStage",
133
+ "TrainingState",
134
+ "ValidationResult",
135
+ "ValidationStrategy",
136
+ "WeightedSpread",
137
+ "add_epsilon",
138
+ "cdf",
139
+ "coverage",
140
+ "crps",
141
+ "flatten_parameters",
142
+ "metric_tag",
143
+ "parse_metric_tag",
144
+ "point_crps",
145
+ "rmse",
146
+ "sample_crps",
147
+ "validate_transition",
148
+ "wsu",
149
+ ]
@@ -0,0 +1,100 @@
1
+ """Checkpoint value objects and persistence protocol."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from collections.abc import Mapping
6
+ from dataclasses import dataclass, field
7
+ from typing import Any, Protocol
8
+
9
+ from stagereg.core.types import TrainingState
10
+
11
+
12
+ @dataclass(frozen=True)
13
+ class Checkpoint:
14
+ """The complete state necessary to resume staged training.
15
+
16
+ Attributes:
17
+ state: Snapshot of the [`TrainingState`][stagereg.core.TrainingState] at
18
+ the time the checkpoint was saved.
19
+ epoch: Epoch of the active stage at which the checkpoint was saved.
20
+ parameters: Snapshot of the model's parameters, in the backend's own
21
+ format. Defaults to ``None``.
22
+ optimizer_state: Snapshot of the optimizer's state, in the backend's own
23
+ format. Defaults to ``None``.
24
+ rng_state: Random key to resume training with. It mirrors the
25
+ ``rng_state`` of the embedded ``state``. Defaults to ``None``.
26
+ early_stopping_state: State of the early stopper, such as an
27
+ [`EarlyStoppingState`][stagereg.core.EarlyStoppingState], so that
28
+ patience and the best value carry over on resume. Defaults to
29
+ ``None``.
30
+ metadata: Information about the checkpoint, such as the stage that saved
31
+ it. Defaults to an empty mapping.
32
+ """
33
+
34
+ state: TrainingState
35
+ epoch: int
36
+ parameters: Any | None = None
37
+ optimizer_state: Any | None = None
38
+ rng_state: Any | None = None
39
+ early_stopping_state: Any | None = None
40
+ metadata: Mapping[str, Any] = field(default_factory=dict)
41
+
42
+
43
+ class CheckpointStore(Protocol):
44
+ """Persists checkpoints under opaque keys."""
45
+
46
+ def save(self, key: str, checkpoint: Checkpoint) -> None: ...
47
+
48
+ def load(self, key: str) -> Checkpoint: ...
49
+
50
+ def exists(self, key: str) -> bool: ...
51
+
52
+
53
+ class InMemoryCheckpointStore:
54
+ """A [`CheckpointStore`][stagereg.core.CheckpointStore] kept in process memory.
55
+
56
+ Checkpoints live only for the lifetime of the store, which makes it the natural
57
+ choice for examples, tests, and short runs that only need the best checkpoint of the
58
+ current process restored at the end. Saving under an existing key replaces the
59
+ checkpoint held there.
60
+ """
61
+
62
+ def __init__(self) -> None:
63
+ self._checkpoints: dict[str, Checkpoint] = {}
64
+
65
+ def save(self, key: str, checkpoint: Checkpoint) -> None:
66
+ """Store a checkpoint under a key, replacing any checkpoint there.
67
+
68
+ Args:
69
+ key: The opaque key to store the checkpoint under.
70
+ checkpoint: The checkpoint to store.
71
+
72
+ Returns:
73
+ None.
74
+ """
75
+ self._checkpoints[key] = checkpoint
76
+
77
+ def load(self, key: str) -> Checkpoint:
78
+ """Return the checkpoint stored under a key.
79
+
80
+ Args:
81
+ key: The key the checkpoint was saved under.
82
+
83
+ Returns:
84
+ The stored checkpoint.
85
+
86
+ Raises:
87
+ KeyError: If no checkpoint has been saved under ``key``.
88
+ """
89
+ return self._checkpoints[key]
90
+
91
+ def exists(self, key: str) -> bool:
92
+ """Report whether a checkpoint is stored under a key.
93
+
94
+ Args:
95
+ key: The key to look up.
96
+
97
+ Returns:
98
+ ``True`` if a checkpoint is stored under ``key``.
99
+ """
100
+ return key in self._checkpoints