nematocyte 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.
- nematocyte-0.1.0/LICENSE +21 -0
- nematocyte-0.1.0/PKG-INFO +101 -0
- nematocyte-0.1.0/README.md +81 -0
- nematocyte-0.1.0/pyproject.toml +143 -0
- nematocyte-0.1.0/pyproject.toml.orig +136 -0
- nematocyte-0.1.0/src/nematocyte/__init__.py +44 -0
- nematocyte-0.1.0/src/nematocyte/_api.py +142 -0
- nematocyte-0.1.0/src/nematocyte/_build.py +304 -0
- nematocyte-0.1.0/src/nematocyte/_cli.py +177 -0
- nematocyte-0.1.0/src/nematocyte/_compose.py +1060 -0
- nematocyte-0.1.0/src/nematocyte/_errors.py +15 -0
- nematocyte-0.1.0/src/nematocyte/_files.py +184 -0
- nematocyte-0.1.0/src/nematocyte/_help.py +263 -0
- nematocyte-0.1.0/src/nematocyte/_interpolation.py +204 -0
- nematocyte-0.1.0/src/nematocyte/_json_form.py +297 -0
- nematocyte-0.1.0/src/nematocyte/_multirun.py +627 -0
- nematocyte-0.1.0/src/nematocyte/_overrides.py +676 -0
- nematocyte-0.1.0/src/nematocyte/_run.py +255 -0
- nematocyte-0.1.0/src/nematocyte/_schema.py +744 -0
- nematocyte-0.1.0/src/nematocyte/_settings.py +221 -0
- nematocyte-0.1.0/src/nematocyte/_tree.py +452 -0
- nematocyte-0.1.0/src/nematocyte/_types.py +62 -0
- nematocyte-0.1.0/src/nematocyte/_validation.py +266 -0
- nematocyte-0.1.0/src/nematocyte/py.typed +0 -0
nematocyte-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Thomas M Kehrenberg
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: nematocyte
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Configuration for Python programs, as a tree of dataclasses filled from YAML or JSON5 files and the command line
|
|
5
|
+
Author: Thomas M Kehrenberg
|
|
6
|
+
Author-email: Thomas M Kehrenberg <tmke8@posteo.net>
|
|
7
|
+
License-Expression: MIT
|
|
8
|
+
License-File: LICENSE
|
|
9
|
+
Requires-Dist: pydantic>=2.12
|
|
10
|
+
Requires-Dist: ruamel-yaml>=0.18
|
|
11
|
+
Requires-Dist: typing-extensions>=4.16
|
|
12
|
+
Requires-Dist: json5>=0.17 ; extra == 'json5'
|
|
13
|
+
Requires-Dist: optuna>=4 ; extra == 'optuna'
|
|
14
|
+
Requires-Dist: submitit>=1.5 ; extra == 'submitit'
|
|
15
|
+
Requires-Python: >=3.14
|
|
16
|
+
Provides-Extra: json5
|
|
17
|
+
Provides-Extra: optuna
|
|
18
|
+
Provides-Extra: submitit
|
|
19
|
+
Description-Content-Type: text/markdown
|
|
20
|
+
|
|
21
|
+
# nematocyte
|
|
22
|
+
|
|
23
|
+
A configuration library for Python programs: a replacement for Hydra with
|
|
24
|
+
fewer features and more focus on usability. The config is a tree of stdlib
|
|
25
|
+
dataclasses (or `TypedDict`s), filled from YAML (or JSON5) files and
|
|
26
|
+
command-line overrides.
|
|
27
|
+
|
|
28
|
+
```python
|
|
29
|
+
from dataclasses import dataclass, field
|
|
30
|
+
from typing import Annotated
|
|
31
|
+
|
|
32
|
+
from pydantic import Discriminator, Tag
|
|
33
|
+
|
|
34
|
+
import nematocyte
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
@dataclass(kw_only=True)
|
|
38
|
+
class Fcn:
|
|
39
|
+
"""Fully-connected network."""
|
|
40
|
+
|
|
41
|
+
num_hidden: int = field(default=1, doc="Number of hidden layers.")
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
@dataclass(kw_only=True)
|
|
45
|
+
class Cnn:
|
|
46
|
+
"""Convolutional network."""
|
|
47
|
+
|
|
48
|
+
kernel_size: int = field(default=5, doc="Kernel size of the convolutions.")
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
@dataclass(kw_only=True)
|
|
52
|
+
class Config:
|
|
53
|
+
"""Train a model."""
|
|
54
|
+
|
|
55
|
+
model: Annotated[
|
|
56
|
+
Annotated[Fcn, Tag("fcn")] | Annotated[Cnn, Tag("cnn")],
|
|
57
|
+
Discriminator(nematocyte.pick),
|
|
58
|
+
] = field(default_factory=Fcn, doc="The model architecture.")
|
|
59
|
+
seed: int = field(default=42, doc="Random seed.")
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
@nematocyte.main(Config, config_path="configs")
|
|
63
|
+
def main(config: Config) -> None:
|
|
64
|
+
print(config)
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
if __name__ == "__main__":
|
|
68
|
+
main()
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
```bash
|
|
72
|
+
python main.py model=cnn model.kernel_size=3 seed=1
|
|
73
|
+
python main.py --help
|
|
74
|
+
python main.py --multirun seed=1,2,3
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
- [`example/`](example/) is a complete project with an annotated README.
|
|
78
|
+
- [`design/`](design/) holds the design documents, which are the
|
|
79
|
+
specification.
|
|
80
|
+
- [`DIFFERENCES.md`](DIFFERENCES.md) lists what works differently from
|
|
81
|
+
Hydra, for someone who knows it.
|
|
82
|
+
|
|
83
|
+
## Installing
|
|
84
|
+
|
|
85
|
+
nematocyte needs Python 3.14 or later.
|
|
86
|
+
|
|
87
|
+
```bash
|
|
88
|
+
uv add nematocyte
|
|
89
|
+
uv add 'nematocyte[submitit]' # for the SLURM launcher
|
|
90
|
+
uv add 'nematocyte[optuna]' # for the Optuna sweeper
|
|
91
|
+
uv add 'nematocyte[json5]' # for config files in JSON5
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
## Developing
|
|
95
|
+
|
|
96
|
+
```bash
|
|
97
|
+
uv run ruff check
|
|
98
|
+
uv run ruff format --check
|
|
99
|
+
uv run ty check
|
|
100
|
+
uv run pytest
|
|
101
|
+
```
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
# nematocyte
|
|
2
|
+
|
|
3
|
+
A configuration library for Python programs: a replacement for Hydra with
|
|
4
|
+
fewer features and more focus on usability. The config is a tree of stdlib
|
|
5
|
+
dataclasses (or `TypedDict`s), filled from YAML (or JSON5) files and
|
|
6
|
+
command-line overrides.
|
|
7
|
+
|
|
8
|
+
```python
|
|
9
|
+
from dataclasses import dataclass, field
|
|
10
|
+
from typing import Annotated
|
|
11
|
+
|
|
12
|
+
from pydantic import Discriminator, Tag
|
|
13
|
+
|
|
14
|
+
import nematocyte
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
@dataclass(kw_only=True)
|
|
18
|
+
class Fcn:
|
|
19
|
+
"""Fully-connected network."""
|
|
20
|
+
|
|
21
|
+
num_hidden: int = field(default=1, doc="Number of hidden layers.")
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
@dataclass(kw_only=True)
|
|
25
|
+
class Cnn:
|
|
26
|
+
"""Convolutional network."""
|
|
27
|
+
|
|
28
|
+
kernel_size: int = field(default=5, doc="Kernel size of the convolutions.")
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
@dataclass(kw_only=True)
|
|
32
|
+
class Config:
|
|
33
|
+
"""Train a model."""
|
|
34
|
+
|
|
35
|
+
model: Annotated[
|
|
36
|
+
Annotated[Fcn, Tag("fcn")] | Annotated[Cnn, Tag("cnn")],
|
|
37
|
+
Discriminator(nematocyte.pick),
|
|
38
|
+
] = field(default_factory=Fcn, doc="The model architecture.")
|
|
39
|
+
seed: int = field(default=42, doc="Random seed.")
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
@nematocyte.main(Config, config_path="configs")
|
|
43
|
+
def main(config: Config) -> None:
|
|
44
|
+
print(config)
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
if __name__ == "__main__":
|
|
48
|
+
main()
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
```bash
|
|
52
|
+
python main.py model=cnn model.kernel_size=3 seed=1
|
|
53
|
+
python main.py --help
|
|
54
|
+
python main.py --multirun seed=1,2,3
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
- [`example/`](example/) is a complete project with an annotated README.
|
|
58
|
+
- [`design/`](design/) holds the design documents, which are the
|
|
59
|
+
specification.
|
|
60
|
+
- [`DIFFERENCES.md`](DIFFERENCES.md) lists what works differently from
|
|
61
|
+
Hydra, for someone who knows it.
|
|
62
|
+
|
|
63
|
+
## Installing
|
|
64
|
+
|
|
65
|
+
nematocyte needs Python 3.14 or later.
|
|
66
|
+
|
|
67
|
+
```bash
|
|
68
|
+
uv add nematocyte
|
|
69
|
+
uv add 'nematocyte[submitit]' # for the SLURM launcher
|
|
70
|
+
uv add 'nematocyte[optuna]' # for the Optuna sweeper
|
|
71
|
+
uv add 'nematocyte[json5]' # for config files in JSON5
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
## Developing
|
|
75
|
+
|
|
76
|
+
```bash
|
|
77
|
+
uv run ruff check
|
|
78
|
+
uv run ruff format --check
|
|
79
|
+
uv run ty check
|
|
80
|
+
uv run pytest
|
|
81
|
+
```
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "nematocyte"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "Configuration for Python programs, as a tree of dataclasses filled from YAML or JSON5 files and the command line"
|
|
5
|
+
license = "MIT"
|
|
6
|
+
license-files = ["LICENSE"]
|
|
7
|
+
readme = "README.md"
|
|
8
|
+
requires-python = ">=3.14"
|
|
9
|
+
dependencies = [
|
|
10
|
+
"pydantic>=2.12",
|
|
11
|
+
"ruamel-yaml>=0.18",
|
|
12
|
+
"typing-extensions>=4.16",
|
|
13
|
+
]
|
|
14
|
+
|
|
15
|
+
[[project.authors]]
|
|
16
|
+
name = "Thomas M Kehrenberg"
|
|
17
|
+
email = "tmke8@posteo.net"
|
|
18
|
+
|
|
19
|
+
[project.optional-dependencies]
|
|
20
|
+
submitit = ["submitit>=1.5"]
|
|
21
|
+
optuna = ["optuna>=4"]
|
|
22
|
+
json5 = ["json5>=0.17"]
|
|
23
|
+
|
|
24
|
+
[build-system]
|
|
25
|
+
requires = ["uv_build>=0.13.7,<0.14.0"]
|
|
26
|
+
build-backend = "uv_build"
|
|
27
|
+
|
|
28
|
+
[dependency-groups]
|
|
29
|
+
test = [
|
|
30
|
+
"pytest>=8",
|
|
31
|
+
"submitit>=1.5",
|
|
32
|
+
"optuna>=4",
|
|
33
|
+
"json5>=0.17",
|
|
34
|
+
]
|
|
35
|
+
lint = [
|
|
36
|
+
"ruff>=0.14",
|
|
37
|
+
"ty",
|
|
38
|
+
]
|
|
39
|
+
reference = [
|
|
40
|
+
"hydra-core==1.4.0rc3",
|
|
41
|
+
"hydra-qol>=0.2.3",
|
|
42
|
+
"tyro>=1.0.17",
|
|
43
|
+
]
|
|
44
|
+
typecheck = [
|
|
45
|
+
"mypy>=2.4.1",
|
|
46
|
+
"pyright>=1.1.414",
|
|
47
|
+
]
|
|
48
|
+
|
|
49
|
+
[[dependency-groups.dev]]
|
|
50
|
+
include-group = "test"
|
|
51
|
+
|
|
52
|
+
[[dependency-groups.dev]]
|
|
53
|
+
include-group = "lint"
|
|
54
|
+
|
|
55
|
+
[[dependency-groups.dev]]
|
|
56
|
+
include-group = "typecheck"
|
|
57
|
+
|
|
58
|
+
[[dependency-groups.dev]]
|
|
59
|
+
include-group = "reference"
|
|
60
|
+
|
|
61
|
+
[tool.pytest.ini_options]
|
|
62
|
+
testpaths = ["tests"]
|
|
63
|
+
addopts = "-ra"
|
|
64
|
+
filterwarnings = ["error::nematocyte.DroppedValueWarning"]
|
|
65
|
+
|
|
66
|
+
[tool.pyright]
|
|
67
|
+
pythonVersion = "3.14"
|
|
68
|
+
venvPath = "."
|
|
69
|
+
venv = ".venv"
|
|
70
|
+
|
|
71
|
+
[tool.ty.src]
|
|
72
|
+
include = [
|
|
73
|
+
"src",
|
|
74
|
+
"tests",
|
|
75
|
+
]
|
|
76
|
+
|
|
77
|
+
[tool.ty.rules]
|
|
78
|
+
disjoint-cast = "warn"
|
|
79
|
+
possibly-missing-attribute = "warn"
|
|
80
|
+
possibly-missing-import = "warn"
|
|
81
|
+
possibly-unresolved-reference = "warn"
|
|
82
|
+
truthiness-test-of-none-union = "warn"
|
|
83
|
+
unsound-assignment = "warn"
|
|
84
|
+
unsound-return = "warn"
|
|
85
|
+
|
|
86
|
+
[tool.ruff]
|
|
87
|
+
line-length = 88
|
|
88
|
+
target-version = "py314"
|
|
89
|
+
extend-exclude = ["example"]
|
|
90
|
+
|
|
91
|
+
[tool.ruff.format]
|
|
92
|
+
quote-style = "preserve"
|
|
93
|
+
|
|
94
|
+
[tool.ruff.lint]
|
|
95
|
+
select = [
|
|
96
|
+
"ARG",
|
|
97
|
+
"B",
|
|
98
|
+
"C4",
|
|
99
|
+
"E",
|
|
100
|
+
"F",
|
|
101
|
+
"FBT",
|
|
102
|
+
"I",
|
|
103
|
+
"INP",
|
|
104
|
+
"N",
|
|
105
|
+
"PLC",
|
|
106
|
+
"PLE",
|
|
107
|
+
"PLR",
|
|
108
|
+
"PLW",
|
|
109
|
+
"PTH",
|
|
110
|
+
"SIM",
|
|
111
|
+
"UP",
|
|
112
|
+
"W",
|
|
113
|
+
]
|
|
114
|
+
ignore = [
|
|
115
|
+
"E501",
|
|
116
|
+
"N812",
|
|
117
|
+
"PLR0402",
|
|
118
|
+
"PLR2004",
|
|
119
|
+
"PLR0913",
|
|
120
|
+
"PLW2901",
|
|
121
|
+
"PLC0105",
|
|
122
|
+
"W191",
|
|
123
|
+
]
|
|
124
|
+
|
|
125
|
+
[tool.ruff.lint.per-file-ignores]
|
|
126
|
+
"__init__.py" = [
|
|
127
|
+
"F403",
|
|
128
|
+
"PLC0414",
|
|
129
|
+
]
|
|
130
|
+
"tests/**" = ["INP"]
|
|
131
|
+
|
|
132
|
+
[tool.ruff.lint.isort]
|
|
133
|
+
extra-standard-library = ["typing_extensions"]
|
|
134
|
+
no-lines-before = [
|
|
135
|
+
"future",
|
|
136
|
+
"standard-library",
|
|
137
|
+
]
|
|
138
|
+
force-sort-within-sections = true
|
|
139
|
+
split-on-trailing-comma = false
|
|
140
|
+
|
|
141
|
+
[tool.ruff.lint.pylint]
|
|
142
|
+
max-statements = 60
|
|
143
|
+
max-branches = 13
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "nematocyte"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "Configuration for Python programs, as a tree of dataclasses filled from YAML or JSON5 files and the command line"
|
|
5
|
+
license = "MIT"
|
|
6
|
+
license-files = ["LICENSE"]
|
|
7
|
+
readme = "README.md"
|
|
8
|
+
authors = [
|
|
9
|
+
{ name = "Thomas M Kehrenberg", email = "tmke8@posteo.net" }
|
|
10
|
+
]
|
|
11
|
+
requires-python = ">=3.14"
|
|
12
|
+
dependencies = [
|
|
13
|
+
"pydantic>=2.12",
|
|
14
|
+
"ruamel-yaml>=0.18",
|
|
15
|
+
"typing-extensions>=4.16",
|
|
16
|
+
]
|
|
17
|
+
|
|
18
|
+
[project.optional-dependencies]
|
|
19
|
+
submitit = [
|
|
20
|
+
"submitit>=1.5",
|
|
21
|
+
]
|
|
22
|
+
optuna = [
|
|
23
|
+
"optuna>=4",
|
|
24
|
+
]
|
|
25
|
+
json5 = [
|
|
26
|
+
"json5>=0.17",
|
|
27
|
+
]
|
|
28
|
+
|
|
29
|
+
[build-system]
|
|
30
|
+
requires = ["uv_build>=0.13.7,<0.14.0"]
|
|
31
|
+
build-backend = "uv_build"
|
|
32
|
+
|
|
33
|
+
[dependency-groups]
|
|
34
|
+
dev = [
|
|
35
|
+
{ include-group = "test" },
|
|
36
|
+
{ include-group = "lint" },
|
|
37
|
+
{ include-group = "typecheck" },
|
|
38
|
+
{ include-group = "reference" },
|
|
39
|
+
]
|
|
40
|
+
# The tests of the launchers, the sweepers and the JSON5 files need the
|
|
41
|
+
# optional extras.
|
|
42
|
+
test = ["pytest>=8", "submitit>=1.5", "optuna>=4", "json5>=0.17"]
|
|
43
|
+
lint = ["ruff>=0.14", "ty"]
|
|
44
|
+
# Not used by nematocyte; installed to compare behaviour during development.
|
|
45
|
+
reference = [
|
|
46
|
+
"hydra-core==1.4.0rc3",
|
|
47
|
+
"hydra-qol>=0.2.3",
|
|
48
|
+
"tyro>=1.0.17",
|
|
49
|
+
]
|
|
50
|
+
typecheck = [
|
|
51
|
+
"mypy>=2.4.1",
|
|
52
|
+
"pyright>=1.1.414",
|
|
53
|
+
]
|
|
54
|
+
|
|
55
|
+
[tool.pytest.ini_options]
|
|
56
|
+
testpaths = ["tests"]
|
|
57
|
+
addopts = "-ra"
|
|
58
|
+
# A test that drops a value with a selection on purpose says so with
|
|
59
|
+
# `pytest.warns`; anywhere else, the warning is a mistake in the test.
|
|
60
|
+
filterwarnings = ["error::nematocyte.DroppedValueWarning"]
|
|
61
|
+
|
|
62
|
+
# pyright does not read the version from `requires-python`. Without this, a
|
|
63
|
+
# pyright outside the project uses whatever Python is first on the PATH.
|
|
64
|
+
[tool.pyright]
|
|
65
|
+
pythonVersion = "3.14"
|
|
66
|
+
venvPath = "."
|
|
67
|
+
venv = ".venv"
|
|
68
|
+
|
|
69
|
+
[tool.ty.src]
|
|
70
|
+
include = ["src", "tests"]
|
|
71
|
+
|
|
72
|
+
[tool.ty.rules]
|
|
73
|
+
disjoint-cast = "warn"
|
|
74
|
+
possibly-missing-attribute = "warn"
|
|
75
|
+
possibly-missing-import = "warn"
|
|
76
|
+
possibly-unresolved-reference = "warn"
|
|
77
|
+
truthiness-test-of-none-union = "warn"
|
|
78
|
+
unsound-assignment = "warn"
|
|
79
|
+
unsound-return = "warn"
|
|
80
|
+
|
|
81
|
+
[tool.ruff]
|
|
82
|
+
line-length = 88
|
|
83
|
+
target-version = "py314"
|
|
84
|
+
# The example is a project of its own, with its own configuration.
|
|
85
|
+
extend-exclude = ["example"]
|
|
86
|
+
|
|
87
|
+
[tool.ruff.format]
|
|
88
|
+
quote-style = "preserve"
|
|
89
|
+
|
|
90
|
+
[tool.ruff.lint]
|
|
91
|
+
select = [
|
|
92
|
+
"ARG", # flake8-unused-args
|
|
93
|
+
"B", # bugbear
|
|
94
|
+
"C4", # flake8-comprehensions
|
|
95
|
+
"E", # pycodestyle
|
|
96
|
+
"F", # pyflakes
|
|
97
|
+
"FBT", # boolean trap
|
|
98
|
+
"I", # isort
|
|
99
|
+
"INP", # implicit namespace package
|
|
100
|
+
"N", # pep8-naming
|
|
101
|
+
"PLC", # pylint convention
|
|
102
|
+
"PLE", # pylint error
|
|
103
|
+
"PLR", # pylint refactor
|
|
104
|
+
"PLW", # pylint warning
|
|
105
|
+
"PTH", # flake8-use-pathlib
|
|
106
|
+
"SIM", # flake8-simplify
|
|
107
|
+
"UP", # pyupgrade
|
|
108
|
+
"W",
|
|
109
|
+
]
|
|
110
|
+
ignore = [
|
|
111
|
+
"E501", # line too long (conflicts with formatter)
|
|
112
|
+
"N812", # case of import alias
|
|
113
|
+
"PLR0402", # useless import alias
|
|
114
|
+
"PLR2004", # magic number comparison
|
|
115
|
+
"PLR0913", # too many arguments
|
|
116
|
+
"PLW2901", # overwriting loop variables
|
|
117
|
+
"PLC0105", # mark variance in typevar name
|
|
118
|
+
"W191", # tab indentation (conflicts with formatter)
|
|
119
|
+
]
|
|
120
|
+
|
|
121
|
+
[tool.ruff.lint.per-file-ignores]
|
|
122
|
+
"__init__.py" = [
|
|
123
|
+
"F403",
|
|
124
|
+
"PLC0414", # reexporting
|
|
125
|
+
]
|
|
126
|
+
"tests/**" = ["INP"]
|
|
127
|
+
|
|
128
|
+
[tool.ruff.lint.isort]
|
|
129
|
+
extra-standard-library = ["typing_extensions"]
|
|
130
|
+
no-lines-before = ["future", "standard-library"]
|
|
131
|
+
force-sort-within-sections = true
|
|
132
|
+
split-on-trailing-comma = false
|
|
133
|
+
|
|
134
|
+
[tool.ruff.lint.pylint]
|
|
135
|
+
max-statements = 60 # default: 50
|
|
136
|
+
max-branches = 13 # default: 12
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"""nematocyte: configuration for Python programs, as a tree of dataclasses.
|
|
2
|
+
|
|
3
|
+
A node of the tree can also be a `TypedDict`.
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
from ._api import EntryPoint as EntryPoint
|
|
7
|
+
from ._api import compose as compose
|
|
8
|
+
from ._api import from_run_config as from_run_config
|
|
9
|
+
from ._api import main as main
|
|
10
|
+
from ._errors import ConfigError as ConfigError
|
|
11
|
+
from ._errors import DroppedValueWarning as DroppedValueWarning
|
|
12
|
+
from ._errors import SchemaError as SchemaError
|
|
13
|
+
from ._json_form import to_dict as to_dict
|
|
14
|
+
from ._run import configure_logging as configure_logging
|
|
15
|
+
from ._run import job_num as job_num
|
|
16
|
+
from ._run import log_level as log_level
|
|
17
|
+
from ._run import output_dir as output_dir
|
|
18
|
+
from ._run import run_config as run_config
|
|
19
|
+
from ._schema import pick as pick
|
|
20
|
+
from ._types import JSONValue as JSONValue
|
|
21
|
+
from ._validation import ValidationResult as ValidationResult
|
|
22
|
+
from ._validation import Validator as Validator
|
|
23
|
+
from ._validation import validate_configs as validate_configs
|
|
24
|
+
|
|
25
|
+
__all__ = [
|
|
26
|
+
"ConfigError",
|
|
27
|
+
"DroppedValueWarning",
|
|
28
|
+
"EntryPoint",
|
|
29
|
+
"JSONValue",
|
|
30
|
+
"SchemaError",
|
|
31
|
+
"ValidationResult",
|
|
32
|
+
"Validator",
|
|
33
|
+
"compose",
|
|
34
|
+
"configure_logging",
|
|
35
|
+
"from_run_config",
|
|
36
|
+
"job_num",
|
|
37
|
+
"log_level",
|
|
38
|
+
"main",
|
|
39
|
+
"output_dir",
|
|
40
|
+
"pick",
|
|
41
|
+
"run_config",
|
|
42
|
+
"to_dict",
|
|
43
|
+
"validate_configs",
|
|
44
|
+
]
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
"""The public functions: the entry point, `compose` and `from_run_config`."""
|
|
2
|
+
|
|
3
|
+
from collections.abc import Callable, Mapping, Sequence
|
|
4
|
+
from datetime import datetime
|
|
5
|
+
import functools
|
|
6
|
+
import os
|
|
7
|
+
from pathlib import Path
|
|
8
|
+
import sys
|
|
9
|
+
from typing import Any, Protocol, cast
|
|
10
|
+
from typing_extensions import TypeForm
|
|
11
|
+
|
|
12
|
+
from ._build import build
|
|
13
|
+
from ._cli import run_cli
|
|
14
|
+
from ._compose import Composer
|
|
15
|
+
from ._interpolation import resolve
|
|
16
|
+
from ._overrides import split_arguments
|
|
17
|
+
from ._run import ENTRY_ATTRIBUTE, Entry
|
|
18
|
+
from ._schema import RESERVED_KEY, config_class, validate_schema
|
|
19
|
+
from ._settings import Settings
|
|
20
|
+
from ._tree import node_from_json_tree
|
|
21
|
+
from ._types import JSONValue
|
|
22
|
+
|
|
23
|
+
__all__ = ["EntryPoint", "compose", "from_run_config", "main"]
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
class EntryPoint(Protocol):
|
|
27
|
+
"""The decorated function: it reads the command line and runs the program."""
|
|
28
|
+
|
|
29
|
+
def __call__(self, args: Sequence[str] | None = None) -> None:
|
|
30
|
+
"""Run the program.
|
|
31
|
+
|
|
32
|
+
`args` replaces the arguments of the command line, which is useful
|
|
33
|
+
in tests.
|
|
34
|
+
"""
|
|
35
|
+
...
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def _defining_dir(function: Callable[..., Any]) -> Path:
|
|
39
|
+
"""The directory of the file that defines `function`."""
|
|
40
|
+
module = sys.modules.get(function.__module__)
|
|
41
|
+
filename = getattr(module, "__file__", None)
|
|
42
|
+
if filename is None:
|
|
43
|
+
code = getattr(function, "__code__", None)
|
|
44
|
+
filename = code.co_filename if code is not None else None
|
|
45
|
+
if filename is None or not Path(filename).parent.is_dir():
|
|
46
|
+
return Path.cwd()
|
|
47
|
+
return Path(filename).resolve().parent
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def main[C](
|
|
51
|
+
config_cls: TypeForm[C],
|
|
52
|
+
*,
|
|
53
|
+
config_path: str | os.PathLike[str],
|
|
54
|
+
config_name: str | None = None,
|
|
55
|
+
) -> Callable[[Callable[[C], object]], EntryPoint]:
|
|
56
|
+
"""Turn a function that takes the config into the entry point of a program.
|
|
57
|
+
|
|
58
|
+
Args:
|
|
59
|
+
config_cls: The root config class, a dataclass or a TypedDict.
|
|
60
|
+
Every config is checked against it, and the function receives an
|
|
61
|
+
instance of it.
|
|
62
|
+
config_path: The directory with the config files, relative to the
|
|
63
|
+
file that defines the function. It has to exist, even if it is empty.
|
|
64
|
+
config_name: The primary config to use if `--config-name` is not
|
|
65
|
+
given. Without it, the config comes from the root config class alone.
|
|
66
|
+
|
|
67
|
+
The return value of the function is the objective for a sweeper, and is
|
|
68
|
+
ignored otherwise.
|
|
69
|
+
"""
|
|
70
|
+
|
|
71
|
+
cls = config_class(config_cls)
|
|
72
|
+
|
|
73
|
+
def decorator(function: Callable[[C], object]) -> EntryPoint:
|
|
74
|
+
directory = _defining_dir(function) / Path(config_path)
|
|
75
|
+
entry = Entry(function, cls, directory, config_name)
|
|
76
|
+
|
|
77
|
+
@functools.wraps(function)
|
|
78
|
+
def wrapper(args: Sequence[str] | None = None) -> None:
|
|
79
|
+
run_cli(entry, wrapper, args)
|
|
80
|
+
|
|
81
|
+
vars(wrapper)[ENTRY_ATTRIBUTE] = entry
|
|
82
|
+
return wrapper
|
|
83
|
+
|
|
84
|
+
return decorator
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
def compose[C](
|
|
88
|
+
config_cls: TypeForm[C],
|
|
89
|
+
config_dir: str | os.PathLike[str] | None = None,
|
|
90
|
+
*,
|
|
91
|
+
config_name: str | None = None,
|
|
92
|
+
overrides: Sequence[str] = (),
|
|
93
|
+
) -> C:
|
|
94
|
+
"""Compose a config without the command line, for tests and notebooks.
|
|
95
|
+
|
|
96
|
+
Args:
|
|
97
|
+
config_cls: The root config class, a dataclass or a TypedDict.
|
|
98
|
+
config_dir: The directory with the config files. A relative path is
|
|
99
|
+
relative to the working directory.
|
|
100
|
+
config_name: The primary config, as `--config-name` takes it.
|
|
101
|
+
overrides: The arguments as they are written on the command line:
|
|
102
|
+
`key=value`, and `+NAME` for a primary config to apply on top.
|
|
103
|
+
|
|
104
|
+
Returns:
|
|
105
|
+
An instance of `config_cls`; a dict for a TypedDict.
|
|
106
|
+
"""
|
|
107
|
+
directory = Path(config_dir) if config_dir is not None else None
|
|
108
|
+
composer = Composer(config_class(config_cls), directory, Settings)
|
|
109
|
+
added, parsed = split_arguments(overrides)
|
|
110
|
+
root = composer.compose(config_name, parsed, added)
|
|
111
|
+
resolve(root, datetime.now())
|
|
112
|
+
return cast("C", build(root, skip=RESERVED_KEY))
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
def from_run_config[C](config_cls: TypeForm[C], data: Mapping[str, JSONValue]) -> C:
|
|
116
|
+
"""Build the config object from plain dicts, such as a saved `config.json`.
|
|
117
|
+
|
|
118
|
+
This is how a run's `.nematocyte/config.json` becomes the object that the
|
|
119
|
+
function of the run received:
|
|
120
|
+
|
|
121
|
+
config = nematocyte.from_run_config(Config, json.loads(path.read_text()))
|
|
122
|
+
|
|
123
|
+
It reads what `config.json` holds, the resolved tree of the run. It also
|
|
124
|
+
takes the output of `to_dict`, which leaves out the values of `InitVar`
|
|
125
|
+
fields; those then take the default in the class.
|
|
126
|
+
|
|
127
|
+
The mapping of a group names its option under the key ``_option_``. A
|
|
128
|
+
key that is left out takes the default in the class. Every value is checked as a value from a
|
|
129
|
+
config file is, but no interpolation is resolved and no config file is
|
|
130
|
+
read.
|
|
131
|
+
|
|
132
|
+
Args:
|
|
133
|
+
config_cls: The root config class, a dataclass or a TypedDict.
|
|
134
|
+
data: The config, as plain dicts, lists and scalars.
|
|
135
|
+
|
|
136
|
+
Returns:
|
|
137
|
+
An instance of `config_cls`; a dict for a TypedDict.
|
|
138
|
+
"""
|
|
139
|
+
cls = config_class(config_cls)
|
|
140
|
+
validate_schema(cls)
|
|
141
|
+
root = node_from_json_tree(cls, data, source="the dict given to from_run_config()")
|
|
142
|
+
return cast("C", build(root))
|