Skip to content

Commit 2a78b46

Browse files
committed
[ENH] Add enum validation and serialization improvements
Added validation logic for `kernel_function` fields to ensure proper deserialization from string inputs to enums. Enhanced default JSON serialization by including additional encoders and adjusted relevant field exclusions.
1 parent fff2fcb commit 2a78b46

File tree

2 files changed

+34
-13
lines changed

2 files changed

+34
-13
lines changed

gempy_engine/core/data/options/interpolation_options.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,14 @@ class CacheMode(enum.Enum):
2323
arbitrary_types_allowed=False,
2424
use_enum_values=False,
2525
json_encoders={
26-
CacheMode: lambda e: e.value
26+
CacheMode: lambda e: e.value,
27+
AvailableKernelFunctions: lambda e: e.name
2728
}
2829
)
2930

3031
# @off
31-
kernel_options: KernelOptions = Field(init=True) # * This is the compression of the fields above and the way to go in the future
32-
evaluation_options: EvaluationOptions = Field(init=True)
32+
kernel_options: KernelOptions = Field(init=True, exclude=False) # * This is the compression of the fields above and the way to go in the future
33+
evaluation_options: EvaluationOptions = Field(init=True, exclude= False)
3334

3435
debug: bool
3536
cache_mode: CacheMode

gempy_engine/core/data/options/kernel_options.py

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33
from dataclasses import dataclass, asdict
44
from typing import Optional
55

6+
from pydantic import field_validator
7+
68
from gempy_engine.core.data.kernel_classes.kernel_functions import AvailableKernelFunctions
79
from gempy_engine.core.data.kernel_classes.solvers import Solvers
810

911

1012
@dataclass(frozen=False)
1113
class KernelOptions:
12-
range: int | float # TODO: have constructor from RegularGrid
14+
range: int | float # TODO: have constructor from RegularGrid
1315
c_o: float # TODO: This should be a property
1416
uni_degree: int = 1
1517
i_res: float = 4.
@@ -23,6 +25,24 @@ class KernelOptions:
2325
optimizing_condition_number: bool = False
2426
condition_number: Optional[float] = None
2527

28+
@field_validator('kernel_function', mode='before')
29+
@classmethod
30+
def _deserialize_kernel_function_from_name(cls, value):
31+
"""
32+
Ensures that a string input (e.g., "cubic" from JSON)
33+
is correctly converted to an AvailableKernelFunctions enum member.
34+
"""
35+
if isinstance(value, str):
36+
try:
37+
return AvailableKernelFunctions[value] # Lookup enum member by name
38+
except KeyError:
39+
# This provides a more specific error if the name doesn't exist
40+
valid_names = [member.name for member in AvailableKernelFunctions]
41+
raise ValueError(f"Invalid kernel function name '{value}'. Must be one of: {valid_names}")
42+
# If it's already an AvailableKernelFunctions member (e.g., during direct model instantiation),
43+
# or if it's another type that Pydantic's later validation will catch as an error.
44+
return value
45+
2646
@property
2747
def n_uni_eq(self):
2848
if self.uni_degree == 1:
@@ -66,16 +86,16 @@ def update_options(self, **kwargs):
6686
def __hash__(self):
6787
# Using a tuple to hash all the values together
6888
return hash((
69-
self.range,
70-
self.c_o,
71-
self.uni_degree,
72-
self.i_res,
73-
self.gi_res,
74-
self.number_dimensions,
75-
self.kernel_function,
76-
self.compute_condition_number,
89+
self.range,
90+
self.c_o,
91+
self.uni_degree,
92+
self.i_res,
93+
self.gi_res,
94+
self.number_dimensions,
95+
self.kernel_function,
96+
self.compute_condition_number,
7797
))
78-
98+
7999
def __repr__(self):
80100
return f"KernelOptions({', '.join(f'{k}={v}' for k, v in asdict(self).items())})"
81101

0 commit comments

Comments
 (0)