1
|
|
|
"""Base class for experiment.""" |
2
|
|
|
|
3
|
|
|
from skbase.base import BaseObject |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
class BaseExperiment(BaseObject): |
7
|
|
|
"""Base class for experiment.""" |
8
|
|
|
|
9
|
|
|
_tags = { |
10
|
|
|
"object_type": "experiment", |
11
|
|
|
"property:randomness": "random", # randomized or deterministic |
12
|
|
|
# if deterministic, two calls of score will result in the same value |
13
|
|
|
# random = two calls may result in different values; same as "stochastic" |
14
|
|
|
} |
15
|
|
|
|
16
|
|
|
def __init__(self): |
17
|
|
|
super().__init__() |
18
|
|
|
|
19
|
|
|
def __call__(self, **kwargs): |
20
|
|
|
"""Score parameters, with kwargs call.""" |
21
|
|
|
score, _ = self.score(kwargs) |
22
|
|
|
return score |
23
|
|
|
|
24
|
|
|
@property |
25
|
|
|
def __name__(self): |
26
|
|
|
return type(self).__name__ |
27
|
|
|
|
28
|
|
|
def paramnames(self): |
29
|
|
|
"""Return the parameter names of the search. |
30
|
|
|
|
31
|
|
|
Returns |
32
|
|
|
------- |
33
|
|
|
list of str |
34
|
|
|
The parameter names of the search parameters. |
35
|
|
|
""" |
36
|
|
|
return self._paramnames() |
37
|
|
|
|
38
|
|
|
def _paramnames(self): |
39
|
|
|
"""Return the parameter names of the search. |
40
|
|
|
|
41
|
|
|
Returns |
42
|
|
|
------- |
43
|
|
|
list of str |
44
|
|
|
The parameter names of the search parameters. |
45
|
|
|
""" |
46
|
|
|
raise NotImplementedError |
47
|
|
|
|
48
|
|
|
def score(self, params): |
49
|
|
|
"""Score the parameters. |
50
|
|
|
|
51
|
|
|
Parameters |
52
|
|
|
---------- |
53
|
|
|
params : dict with string keys |
54
|
|
|
Parameters to score. |
55
|
|
|
|
56
|
|
|
Returns |
57
|
|
|
------- |
58
|
|
|
float |
59
|
|
|
The score of the parameters. |
60
|
|
|
dict |
61
|
|
|
Additional metadata about the search. |
62
|
|
|
""" |
63
|
|
|
paramnames = self.paramnames() |
64
|
|
|
if not set(params.keys()) <= set(paramnames): |
65
|
|
|
raise ValueError("Parameters do not match.") |
66
|
|
|
return self._score(params) |
67
|
|
|
|
68
|
|
|
def _score(self, params): |
69
|
|
|
"""Score the parameters. |
70
|
|
|
|
71
|
|
|
Parameters |
72
|
|
|
---------- |
73
|
|
|
params : dict with string keys |
74
|
|
|
Parameters to score. |
75
|
|
|
|
76
|
|
|
Returns |
77
|
|
|
------- |
78
|
|
|
float |
79
|
|
|
The score of the parameters. |
80
|
|
|
dict |
81
|
|
|
Additional metadata about the search. |
82
|
|
|
""" |
83
|
|
|
raise NotImplementedError |
84
|
|
|
|