Note
Go to the end to download the full example code
Use a pydantic grammar¶
from __future__ import annotations
from gemseo.core.grammars.errors import InvalidDataError
from gemseo.core.grammars.pydantic_grammar import PydanticGrammar
from numpy import array
from numpy import ndarray
from numpy._typing import NDArray
from pydantic import BaseModel
from pydantic import Field
Create the pydantic model¶
The pydantic model is a class that describes the names and types of data to be
validated.
Descriptions are defined with docstrings, default values can be defined naturally.
Mind that default values with a mutable object must be defined with the
default_factory
of a Field
.
class Model(BaseModel):
"""The description of the model."""
a_int: int
"""The description of a_int."""
an_ndarray: ndarray
"""The description of an_ndarray."""
an_ndarray_of_int: NDArray[int]
"""The description of an_ndarray_of_int."""
an_ndarray_with_default: ndarray = Field(default_factory=lambda: array([0]))
"""The description of an_ndarray_with_default."""
a_str_with_default: str = "default"
"""The description of a_str."""
Create the grammar¶
grammar = PydanticGrammar("grammar", model=Model)
Show the contents of the grammar.
grammar
Validate data against the grammar¶
Validating missing data will raise an error shows the missing required elements, here the first 3 elements are missing.
try:
grammar.validate({})
except InvalidDataError as error:
print(error)
Grammar grammar: validation failed.
Missing required names: a_int, an_ndarray, an_ndarray_of_int.
Validating data with bad type will raise an error shows the bad elements, here the first elements shall be an int and the third one shall be a ndarray of int.
try:
grammar.validate(
{"a_int": 0.0, "an_ndarray": array([1]), "an_ndarray_of_int": array([1.0])}
)
except InvalidDataError as error:
print(error)
Grammar grammar: validation failed.
2 validation errors for Model
a_int
value is not a valid integer (type=type_error.integer)
an_ndarray_of_int
value could not be parsed to a NumPy ndarray with dtype <class 'int'>. (type=type_error.ndarray; dtype_info= with dtype <class 'int'>)
Validating compliant data.
grammar.validate(
{"a_int": 0, "an_ndarray": array([1]), "an_ndarray_of_int": array([1])}
)
Grammar defaults¶
As compared to the other types of grammars, the grammar defaults are be defined in the pydantic model and does not require to be manually defined from the grammar.
grammar.defaults
{'an_ndarray_with_default': array([0]), 'a_str_with_default': 'default'}
Model inheritance¶
Since pydantic models are classes, one can easily build grammar via inheritance of the pydantic model. Here we change the type of one element, and we add a new one.
class Model2(Model):
"""A model that inherits from a parent model."""
an_ndarray: NDArray[float] = Field(default_factory=lambda: array([1.0]))
"""The new description of an_ndarray."""
a_bool: bool = True
"""The description of a_bool."""
grammar = PydanticGrammar("grammar", model=Model2)
Show the contents of the grammar.
grammar
Total running time of the script: (0 minutes 0.009 seconds)