forked from facebook/Ax
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmetric_registry.py
More file actions
81 lines (72 loc) · 2.52 KB
/
Copy pathmetric_registry.py
File metadata and controls
81 lines (72 loc) · 2.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/usr/bin/env python3
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
# pyre-strict
from collections.abc import Callable
from typing import Any
from ax.core.derived_metric import ExpressionDerivedMetric
from ax.core.map_metric import MapMetric
from ax.core.metric import Metric
from ax.metrics.branin import BraninMetric
from ax.metrics.branin_map import BraninTimestampMapMetric
from ax.metrics.chemistry import ChemistryMetric
from ax.metrics.factorial import FactorialMetric
from ax.metrics.hartmann6 import Hartmann6Metric
from ax.metrics.noisy_function import NoisyFunctionMetric
from ax.metrics.sklearn import SklearnMetric
from ax.storage.json_store.encoders import metric_to_dict
from ax.storage.json_store.registry import (
CORE_DECODER_REGISTRY,
CORE_ENCODER_REGISTRY,
TDecoderRegistry,
)
from ax.storage.utils import stable_hash
"""
Mapping of Metric classes to ints.
All metrics will be stored in the same table in the database. When
saving, we look up the metric subclass in METRIC_REGISTRY, and store
the corresponding type field in the database.
"""
CORE_METRIC_REGISTRY: dict[type[Metric], int] = {
Metric: 0,
FactorialMetric: 1,
BraninMetric: 2,
NoisyFunctionMetric: 3,
Hartmann6Metric: 4,
SklearnMetric: 5,
ChemistryMetric: 7,
MapMetric: 8,
BraninTimestampMapMetric: 9,
ExpressionDerivedMetric: 10,
}
def register_metrics(
metric_clss: dict[type[Metric], int | None],
encoder_registry: dict[
type[Any], Callable[[Any], dict[str, Any]]
] = CORE_ENCODER_REGISTRY,
decoder_registry: TDecoderRegistry = CORE_DECODER_REGISTRY,
) -> tuple[
dict[type[Metric], int],
dict[type[Any], Callable[[Any], dict[str, Any]]],
TDecoderRegistry,
]:
"""Add custom metric classes to the SQA and JSON registries.
For the SQA registry, if no int is specified, use a hash of the class name.
"""
new_metric_registry = {
metric_cls: (
val if val is not None else abs(stable_hash(metric_cls.__name__)) % (10**5)
)
for metric_cls, val in metric_clss.items()
}
new_encoder_registry = {
**{metric_cls: metric_to_dict for metric_cls in metric_clss},
**encoder_registry,
}
new_decoder_registry = {
**{metric_cls.__name__: metric_cls for metric_cls in metric_clss},
**decoder_registry,
}
return new_metric_registry, new_encoder_registry, new_decoder_registry