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