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