Skip to content

Commit dacd29e

Browse files
authored
Merge pull request #5502 from chillenzer/transition-pypic-to-pydantic-poc-schema
[Transition PyPIConGPU to pydantic] Proof-of-concept II: Remove schemata
2 parents ea3ffaa + 84c9d35 commit dacd29e

File tree

13 files changed

+42
-244
lines changed

13 files changed

+42
-244
lines changed

lib/python/picongpu/pypicongpu/grid.py

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import enum
99
from typing import Annotated
1010

11-
from pydantic import BaseModel, Field, PlainSerializer, model_validator
11+
from pydantic import AfterValidator, BaseModel, Field, PlainSerializer, model_validator
1212
from typing_extensions import Self
1313

1414
from .rendering import RenderedObject
@@ -59,6 +59,21 @@ def serialise_grid_dist(value):
5959
)
6060

6161

62+
def all_gt(iterable, m):
63+
if all(correct := [x > m for x in iterable]):
64+
return iterable
65+
else:
66+
message = f"{iterable=} contains values <= {m=} while all should be greater than m. Valid are the following: {correct=}."
67+
raise ValueError(message)
68+
69+
70+
def grid_dist_validate(grid_dist):
71+
if grid_dist is None:
72+
return None
73+
if all_gt(sum(grid_dist, []), 0):
74+
return grid_dist
75+
76+
6277
class Grid3D(BaseModel, RenderedObject):
6378
"""
6479
PIConGPU 3 dimensional (cartesian) grid
@@ -68,10 +83,10 @@ class Grid3D(BaseModel, RenderedObject):
6883
The bounding box is implicitly given as TODO.
6984
"""
7085

71-
cell_size: Vec3_float = Field(alias="cell_size_si")
86+
cell_size: Annotated[Vec3_float, AfterValidator(lambda x: all_gt(x, 0))] = Field(alias="cell_size_si")
7287
"""Width of individual cell in each direction"""
7388

74-
cell_cnt: Vec3_int
89+
cell_cnt: Annotated[Vec3_int, AfterValidator(lambda x: all_gt(x, 0))]
7590
"""total number of cells in each direction"""
7691

7792
boundary_condition: Annotated[
@@ -80,10 +95,14 @@ class Grid3D(BaseModel, RenderedObject):
8095
]
8196
"""behavior towards particles crossing each boundary"""
8297

83-
gpu_cnt: Vec3_int = Field((1, 1, 1), alias="n_gpus")
98+
gpu_cnt: Annotated[Vec3_int, AfterValidator(lambda x: all_gt(x, 0))] = Field((1, 1, 1), alias="n_gpus")
8499
"""number of GPUs in x y and z direction as 3-integer tuple"""
85100

86-
grid_dist: Annotated[tuple[list[int], list[int], list[int]] | None, PlainSerializer(serialise_grid_dist)] = None
101+
grid_dist: Annotated[
102+
tuple[list[int], list[int], list[int]] | None,
103+
PlainSerializer(serialise_grid_dist),
104+
AfterValidator(grid_dist_validate),
105+
] = None
87106
"""distribution of grid cells to GPUs for each axis"""
88107

89108
super_cell_size: Vec3_int
@@ -92,8 +111,6 @@ class Grid3D(BaseModel, RenderedObject):
92111
@model_validator(mode="after")
93112
def check(self) -> Self:
94113
"""serialized representation provided for RenderedObject"""
95-
assert all(x > 0 for x in self.cell_cnt), "cell_cnt must be greater than 0"
96-
assert all(x > 0 for x in self.gpu_cnt), "all n_gpus entries must be greater than 0"
97114
if self.grid_dist is not None:
98115
assert sum(self.grid_dist[0]) == self.cell_cnt[0], "sum of grid_dists in x must be equal to number_of_cells"
99116
assert sum(self.grid_dist[1]) == self.cell_cnt[1], "sum of grid_dists in y must be equal to number_of_cells"

lib/python/picongpu/pypicongpu/rendering/renderedobject.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,12 @@ def _get_schema_from_class(class_type: type) -> typing.Any:
165165
try:
166166
schema = RenderedObject._registry.contents(uri)
167167
except referencing.exceptions.NoSuchResource:
168-
raise referencing.exceptions.NoSuchResource("schema not found for FQN {}: URI {}".format(fqn, uri))
168+
try:
169+
schema = class_type.model_json_schema(mode="serialization", by_alias=False) | {"$id": uri}
170+
except Exception as second_error:
171+
raise referencing.exceptions.NoSuchResource(
172+
"schema not found for FQN {}: URI {}".format(fqn, uri)
173+
) from second_error
169174

170175
# validate schema
171176
validator = jsonschema.Draft202012Validator(schema=schema)

share/picongpu/pypicongpu/schema/grid.Grid3D.json

Lines changed: 0 additions & 173 deletions
This file was deleted.

share/picongpu/pypicongpu/schema/output/auto.Auto.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@
88
"png_axis"
99
],
1010
"properties": {
11-
"period": {
12-
"$ref": "https://registry.hzdr.de/crp/picongpu/schema/picongpu.pypicongpu.output.timestepspec.TimeStepSpec"
13-
},
11+
"period": {},
1412
"png_axis": {
1513
"description": "axis pairs (i.e. planes) for which to generate png output",
1614
"type": "array",

share/picongpu/pypicongpu/schema/output/binning.Binning.json

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,7 @@
3131
},
3232
"description": "Species to bin"
3333
},
34-
"period": {
35-
"type": [
36-
"object"
37-
],
38-
"$ref": "https://registry.hzdr.de/crp/picongpu/schema/picongpu.pypicongpu.output.timestepspec.TimeStepSpec",
39-
"description": "Create binning output on specified steps"
40-
},
34+
"period": {},
4135
"openPMD": {
4236
"type": [
4337
"string",

share/picongpu/pypicongpu/schema/output/checkpoint.Checkpoint.json

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,7 @@
1616
}
1717
],
1818
"properties": {
19-
"period": {
20-
"type": [
21-
"object",
22-
"null"
23-
],
24-
"$ref": "https://registry.hzdr.de/crp/picongpu/schema/picongpu.pypicongpu.output.timestepspec.TimeStepSpec",
25-
"description": "Create checkpoints in specified steps"
26-
},
19+
"period": {},
2720
"timePeriod": {
2821
"type": [
2922
"integer",

share/picongpu/pypicongpu/schema/output/energy_histogram.EnergyHistogram.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@
1414
"species": {
1515
"$ref": "https://registry.hzdr.de/crp/picongpu/schema/picongpu.pypicongpu.species.species.Species"
1616
},
17-
"period": {
18-
"$ref": "https://registry.hzdr.de/crp/picongpu/schema/picongpu.pypicongpu.output.timestepspec.TimeStepSpec"
19-
},
17+
"period": {},
2018
"bin_count": {
2119
"type": "integer",
2220
"minimum": 1,

share/picongpu/pypicongpu/schema/output/macro_particle_count.MacroParticleCount.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
"species": {
1212
"$ref": "https://registry.hzdr.de/crp/picongpu/schema/picongpu.pypicongpu.species.species.Species"
1313
},
14-
"period": {
15-
"$ref": "https://registry.hzdr.de/crp/picongpu/schema/picongpu.pypicongpu.output.timestepspec.TimeStepSpec"
16-
}
14+
"period": {}
1715
}
1816
}

share/picongpu/pypicongpu/schema/output/phase_space.PhaseSpace.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@
1515
"species": {
1616
"$ref": "https://registry.hzdr.de/crp/picongpu/schema/picongpu.pypicongpu.species.species.Species"
1717
},
18-
"period": {
19-
"$ref": "https://registry.hzdr.de/crp/picongpu/schema/picongpu.pypicongpu.output.timestepspec.TimeStepSpec"
20-
},
18+
"period": {},
2119
"spatial_coordinate": {
2220
"type": "string",
2321
"enum": [

share/picongpu/pypicongpu/schema/output/png.Png.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,7 @@
2929
"preChannel3"
3030
],
3131
"properties": {
32-
"period": {
33-
"$ref": "https://registry.hzdr.de/crp/picongpu/schema/picongpu.pypicongpu.output.timestepspec.TimeStepSpec"
34-
},
32+
"period": {},
3533
"axis": {
3634
"type": "string",
3735
"description": "2D plane for PNG output (e.g., 'yx')."

0 commit comments

Comments
 (0)