Skip to content

Commit 13e8faa

Browse files
committed
Merge branch 'main' into dev/target_cleanup
2 parents 568ed52 + 68c5fae commit 13e8faa

61 files changed

Lines changed: 423 additions & 585 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
6060
- Objective transformations (both tensor and dataframe based) now always use the Torch
6161
computation route, avoiding the need for duplicated transformation logic
6262
- Specifying bounds for `Interval` is now optional
63+
- `unstructure_base` and `get_base_structure_hook` (de-)serialization utilities
64+
have been replaced with `unstructure_with_type` and `make_base_structure_hook`
6365

6466
### Fixed
6567
- It is no longer possible to use identical names between parameters and targets
@@ -68,6 +70,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
6870
point representation inaccuracy
6971
- Exotic serialization issues with constraints and conditions arising from missing
7072
converters for floats
73+
- `MetaRecommender`'s no longer expose their private attributes via the constructor
7174

7275
### Removed
7376
- Telemetry

baybe/acquisition/base.py

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,6 @@
1515
)
1616
from baybe.objectives.base import Objective
1717
from baybe.searchspace.core import SearchSpace
18-
from baybe.serialization.core import (
19-
converter,
20-
get_base_structure_hook,
21-
unstructure_base,
22-
)
2318
from baybe.serialization.mixin import SerialMixin
2419
from baybe.surrogates.base import SurrogateProtocol
2520
from baybe.utils.basic import classproperty
@@ -186,11 +181,5 @@ def _get_botorch_acqf_class(
186181
)
187182

188183

189-
# Register (un-)structure hooks
190-
converter.register_structure_hook(
191-
AcquisitionFunction, get_base_structure_hook(AcquisitionFunction)
192-
)
193-
converter.register_unstructure_hook(AcquisitionFunction, unstructure_base)
194-
195184
# Collect leftover original slotted classes processed by `attrs.define`
196185
gc.collect()

baybe/campaign.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -850,7 +850,7 @@ def _drop_version(dict_: dict) -> dict:
850850
Campaign, converter, _cattrs_include_init_false=True
851851
)
852852
structure_hook = cattrs.gen.make_dict_structure_fn(
853-
Campaign, converter, _cattrs_include_init_false=True, _cattrs_forbid_extra_keys=True
853+
Campaign, converter, _cattrs_include_init_false=True
854854
)
855855
converter.register_unstructure_hook(
856856
Campaign, lambda x: _add_version(unstructure_hook(x))

baybe/constraints/base.py

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,15 @@
1010
from attrs import define, field
1111
from attrs.validators import ge, instance_of, min_len
1212

13-
from baybe.constraints.deprecation import structure_constraints
13+
from baybe.constraints.deprecation import (
14+
ContinuousLinearEqualityConstraint,
15+
ContinuousLinearInequalityConstraint,
16+
)
1417
from baybe.serialization import (
1518
SerialMixin,
19+
)
20+
from baybe.serialization.core import (
1621
converter,
17-
unstructure_base,
1822
)
1923

2024
if TYPE_CHECKING:
@@ -211,12 +215,26 @@ class ContinuousNonlinearConstraint(ContinuousConstraint, ABC):
211215
"""Abstract base class for continuous nonlinear constraints."""
212216

213217

214-
# Register (un-)structure hooks
215-
converter.register_unstructure_hook(Constraint, unstructure_base)
218+
# >>>>> Deprecation handling
219+
_hook = converter.get_structure_hook(Constraint)
220+
221+
222+
def _deprecate_legacy_classes(dct: dict[str, Any], _) -> Constraint:
223+
"""Enable constraint configs using legacy class names."""
224+
if dct["type"] == "ContinuousLinearEqualityConstraint":
225+
dct.pop("type")
226+
return ContinuousLinearEqualityConstraint(**dct)
227+
elif dct["type"] == "ContinuousLinearInequalityConstraint":
228+
dct.pop("type")
229+
return ContinuousLinearInequalityConstraint(**dct)
230+
return _hook(dct, _)
231+
232+
233+
converter.register_structure_hook_func(
234+
lambda c: c is Constraint, _deprecate_legacy_classes
235+
)
236+
# <<<<< Deprecation handling
216237

217-
# Currently affected by a deprecation
218-
# converter.register_structure_hook(Constraint, get_base_structure_hook(Constraint))
219-
converter.register_structure_hook(Constraint, structure_constraints)
220238

221239
# Collect leftover original slotted classes processed by `attrs.define`
222240
gc.collect()

baybe/constraints/conditions.py

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
from functools import partial
1010
from typing import TYPE_CHECKING, Any
1111

12-
import cattrs
1312
import numpy as np
1413
import pandas as pd
1514
from attrs import define, field
@@ -22,9 +21,6 @@
2221
from baybe.parameters.validation import validate_unique_values
2322
from baybe.serialization import (
2423
SerialMixin,
25-
converter,
26-
get_base_structure_hook,
27-
unstructure_base,
2824
)
2925
from baybe.utils.basic import to_tuple
3026
from baybe.utils.numerical import DTypeFloatNumpy
@@ -230,18 +226,5 @@ def to_polars(self, expr: pl.Expr, /) -> pl.Expr:
230226
return expr.is_in(self.selection)
231227

232228

233-
# Register (un-)structure hooks
234-
_overrides = {
235-
"_selection": cattrs.override(rename="selection"),
236-
}
237-
# FIXME[typing]: https://github.com/python/mypy/issues/4717
238-
converter.register_structure_hook(
239-
Condition,
240-
get_base_structure_hook(Condition, overrides=_overrides), # type: ignore
241-
)
242-
converter.register_unstructure_hook(
243-
Condition, partial(unstructure_base, overrides=_overrides)
244-
)
245-
246229
# Collect leftover original slotted classes processed by `attrs.define`
247230
gc.collect()

baybe/constraints/deprecation.py

Lines changed: 1 addition & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,7 @@
33
from __future__ import annotations
44

55
import warnings
6-
from collections.abc import Callable
7-
from typing import TYPE_CHECKING, Any
8-
9-
from cattrs.gen import make_dict_structure_fn
10-
11-
from baybe.serialization import converter
12-
from baybe.utils.basic import find_subclass, refers_to
13-
from baybe.utils.boolean import is_abstract
14-
15-
if TYPE_CHECKING:
16-
from baybe.constraints.base import Constraint
6+
from typing import Any
177

188

199
def ContinuousLinearEqualityConstraint(
@@ -62,36 +52,3 @@ def ContinuousLinearInequalityConstraint(
6252
kwargs["rhs"] = rhs
6353

6454
return ContinuousLinearConstraint(**kwargs)
65-
66-
67-
def structure_constraints(val: dict, cls: type) -> Constraint:
68-
"""A structure hook taking care of deprecations.""" # noqa: D401 (imperative mood)
69-
from baybe.constraints.base import Constraint
70-
71-
# If the given class can be instantiated, only ensure there is no conflict with
72-
# a potentially specified type field
73-
if not is_abstract(cls):
74-
if (type_ := val.pop("type", None)) and not refers_to(cls, type_):
75-
raise ValueError(
76-
f"The class '{cls.__name__}' specified for deserialization "
77-
f"does not match with the given type information '{type_}'."
78-
)
79-
concrete_cls = cls
80-
81-
# Otherwise, extract the type information from the given input and find
82-
# the corresponding class in the hierarchy
83-
else:
84-
type_ = val if isinstance(val, str) else val.pop("type")
85-
86-
if type_ == "ContinuousLinearEqualityConstraint":
87-
return ContinuousLinearEqualityConstraint(**val)
88-
elif type_ == "ContinuousLinearInequalityConstraint":
89-
return ContinuousLinearInequalityConstraint(**val)
90-
91-
concrete_cls = find_subclass(Constraint, type_)
92-
93-
# Create the structuring function for the class and call it
94-
fn: Callable = make_dict_structure_fn(
95-
concrete_cls, converter, _cattrs_forbid_extra_keys=True
96-
)
97-
return fn({} if isinstance(val, str) else val, concrete_cls)

baybe/kernels/base.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,6 @@
1010

1111
from baybe.exceptions import UnmatchedAttributeError
1212
from baybe.priors.base import Prior
13-
from baybe.serialization.core import (
14-
converter,
15-
get_base_structure_hook,
16-
unstructure_base,
17-
)
1813
from baybe.serialization.mixin import SerialMixin
1914
from baybe.utils.basic import get_baseclasses, match_attributes
2015

@@ -125,9 +120,5 @@ class CompositeKernel(Kernel, ABC):
125120
"""Abstract base class for all composite kernels."""
126121

127122

128-
# Register (un-)structure hooks
129-
converter.register_structure_hook(Kernel, get_base_structure_hook(Kernel))
130-
converter.register_unstructure_hook(Kernel, unstructure_base)
131-
132123
# Collect leftover original slotted classes processed by `attrs.define`
133124
gc.collect()

baybe/objectives/base.py

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,9 @@
77
from abc import ABC, abstractmethod
88
from typing import TYPE_CHECKING, ClassVar
99

10-
import cattrs
1110
import pandas as pd
1211
from attrs import define, field
1312

14-
from baybe.serialization.core import (
15-
converter,
16-
get_base_structure_hook,
17-
unstructure_base,
18-
)
1913
from baybe.serialization.mixin import SerialMixin
2014
from baybe.targets.base import Target
2115
from baybe.targets.numerical import NumericalTarget
@@ -225,26 +219,5 @@ def to_objective(x: Target | Objective, /) -> Objective:
225219
return x if isinstance(x, Objective) else x.to_objective()
226220

227221

228-
# Register (un-)structure hooks
229-
converter.register_structure_hook(
230-
Objective,
231-
get_base_structure_hook(
232-
Objective,
233-
overrides=dict(
234-
_target=cattrs.override(rename="target"),
235-
_targets=cattrs.override(rename="targets"),
236-
),
237-
),
238-
)
239-
converter.register_unstructure_hook(
240-
Objective,
241-
lambda x: unstructure_base(
242-
x,
243-
overrides=dict(
244-
_target=cattrs.override(rename="target"),
245-
_targets=cattrs.override(rename="targets"),
246-
),
247-
),
248-
)
249222
# Collect leftover original slotted classes processed by `attrs.define`
250223
gc.collect()

baybe/parameters/base.py

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@
44

55
import gc
66
from abc import ABC, abstractmethod
7-
from functools import cached_property, partial
7+
from functools import cached_property
88
from typing import TYPE_CHECKING, Any, ClassVar
99

10-
import cattrs
1110
import pandas as pd
1211
from attrs import define, field
1312
from attrs.converters import optional as optional_c
@@ -17,9 +16,6 @@
1716
from baybe.parameters.enum import ParameterEncoding
1817
from baybe.serialization import (
1918
SerialMixin,
20-
converter,
21-
get_base_structure_hook,
22-
unstructure_base,
2319
)
2420
from baybe.utils.basic import to_tuple
2521
from baybe.utils.metadata import MeasurableMetadata, to_metadata
@@ -255,19 +251,5 @@ def to_subspace(self) -> SubspaceContinuous:
255251
return SubspaceContinuous.from_parameter(self)
256252

257253

258-
# Register (un-)structure hooks
259-
_overrides = {
260-
"_values": cattrs.override(rename="values"),
261-
"_active_values": cattrs.override(rename="active_values"),
262-
}
263-
# FIXME[typing]: https://github.com/python/mypy/issues/4717
264-
converter.register_structure_hook(
265-
Parameter,
266-
get_base_structure_hook(Parameter, overrides=_overrides), # type: ignore
267-
)
268-
converter.register_unstructure_hook(
269-
Parameter, partial(unstructure_base, overrides=_overrides)
270-
)
271-
272254
# Collect leftover original slotted classes processed by `attrs.define`
273255
gc.collect()

baybe/priors/base.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,6 @@
55

66
from attrs import define
77

8-
from baybe.serialization.core import (
9-
converter,
10-
get_base_structure_hook,
11-
unstructure_base,
12-
)
138
from baybe.serialization.mixin import SerialMixin
149
from baybe.utils.basic import match_attributes
1510

@@ -39,10 +34,5 @@ def to_gpytorch(self, *args, **kwargs):
3934
return prior_cls(*args, **kwargs)
4035

4136

42-
# Register (un-)structure hooks
43-
converter.register_structure_hook(Prior, get_base_structure_hook(Prior))
44-
converter.register_unstructure_hook(Prior, unstructure_base)
45-
46-
4737
# Collect leftover original slotted classes processed by `attrs.define`
4838
gc.collect()

0 commit comments

Comments
 (0)