Skip to content

Commit 1569e10

Browse files
committed
Add serialization tests for ForbiddenCallableRelation
1 parent 6cb84da commit 1569e10

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

ConfigSpace/forbidden.pyx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,11 @@ cdef class ForbiddenCallableRelation(ForbiddenRelation):
583583
The ForbiddenCallable uses two hyperparameters as input to a
584584
specified callable, which returns True if the relationship
585585
between the two hyperparameters is forbidden.
586+
587+
A ForbiddenCallableRelation may not be serializable.
588+
:func:`ConfigSpace.read_and_write.write` will attempt to pickle and base64 encode
589+
the callable with pickle_callables=True. However, the unpicklability
590+
of the callable cannot be assured.
586591
587592
>>> from ConfigSpace import ConfigurationSpace, ForbiddenCallableRelation
588593
>>>

test/read_and_write/test_json.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import os
22
import unittest
3+
from dataclasses import dataclass
34

4-
from ConfigSpace.forbidden import ForbiddenLessThanRelation
5+
from ConfigSpace.forbidden import ForbiddenLessThanRelation, ForbiddenCallableRelation
56
from ConfigSpace.read_and_write.json import read, write
67
from ConfigSpace.read_and_write.pcs import read as read_pcs
78
from ConfigSpace.read_and_write.pcs_new import read as read_pcs_new
@@ -12,6 +13,14 @@
1213
)
1314

1415

16+
@dataclass
17+
class ProductGreaterThan:
18+
limit: int
19+
20+
def __call__(self, a, b):
21+
return a*b > self.limit
22+
23+
1524
class TestJson(unittest.TestCase):
1625

1726
def test_serialize_forbidden_in_clause(self):
@@ -27,6 +36,10 @@ def test_serialize_forbidden_relation(self):
2736
cs.add_forbidden_clause(ForbiddenLessThanRelation(a, b))
2837
write(cs)
2938

39+
cs.add_forbidden_clause(ForbiddenCallableRelation(a, b, ProductGreaterThan(3)))
40+
self.assertRaises(ValueError, write, cs)
41+
write(cs, pickle_callables=True)
42+
3043
def test_configspace_with_probabilities(self):
3144
cs = ConfigurationSpace()
3245
cs.add_hyperparameter(

0 commit comments

Comments
 (0)