-
Notifications
You must be signed in to change notification settings - Fork 61
/
alpha_integration.py
240 lines (186 loc) 路 7.95 KB
/
alpha_integration.py
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#
# Copyright (c) 2021, Neptune Labs Sp. z o.o.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import abc
from collections import namedtuple
from neptune.exceptions import NeptuneException
from neptune.internal.channels.channels import ChannelType, ChannelValueType
from neptune.new.attributes import constants as alpha_consts
from neptune.new.internal import operation as alpha_operation
from neptune.new.internal.backends.api_model import AttributeType as AlphaAttributeType
# Alpha equivalent of old api's `KeyValueProperty` used in `Experiment.properties`
from neptune.new.internal.operation import ImageValue
AlphaKeyValueProperty = namedtuple("AlphaKeyValueProperty", ["key", "value"])
class AlphaAttributeWrapper(abc.ABC):
"""It's simple wrapper for `AttributeDTO`."""
_allowed_atribute_types = list()
def __init__(self, attribute):
"""Expects `AttributeDTO`"""
assert self._allowed_atribute_types is not None
if not self.is_valid_attribute(attribute):
raise NeptuneException(f"Invalid channel attribute type: {attribute.type}")
self._attribute = attribute
@classmethod
def is_valid_attribute(cls, attribute):
"""Checks if attribute can be wrapped by particular descendant of this class"""
return attribute.type in cls._allowed_atribute_types
@property
def _properties(self):
"""Returns proper attribute property according to type"""
return getattr(self._attribute, f"{self._attribute.type}Properties")
class AlphaPropertyDTO(AlphaAttributeWrapper):
"""It's simple wrapper for `AttributeDTO` objects which uses alpha variables attributes to fake properties.
Alpha leaderboard doesn't have `KeyValueProperty` since it doesn't support properties at all,
so we do need fake `KeyValueProperty` class for backward compatibility with old client's code."""
_allowed_atribute_types = [
AlphaAttributeType.STRING.value,
]
@classmethod
def is_valid_attribute(cls, attribute):
"""Checks if attribute can be used as property"""
has_valid_type = super().is_valid_attribute(attribute)
is_in_properties_space = attribute.name.startswith(alpha_consts.PROPERTIES_ATTRIBUTE_SPACE)
return has_valid_type and is_in_properties_space
@property
def key(self):
return self._properties.attributeName.split("/", 1)[-1]
@property
def value(self):
return self._properties.value
class AlphaParameterDTO(AlphaAttributeWrapper):
"""It's simple wrapper for `AttributeDTO` objects which uses alpha variables attributes to fake properties.
Alpha leaderboard doesn't have `KeyValueProperty` since it doesn't support properties at all,
so we do need fake `KeyValueProperty` class for backward compatibility with old client's code."""
_allowed_atribute_types = [
AlphaAttributeType.FLOAT.value,
AlphaAttributeType.STRING.value,
AlphaAttributeType.DATETIME.value,
]
@classmethod
def is_valid_attribute(cls, attribute):
"""Checks if attribute can be used as property"""
has_valid_type = super().is_valid_attribute(attribute)
is_in_parameters_space = attribute.name.startswith(alpha_consts.PARAMETERS_ATTRIBUTE_SPACE)
return has_valid_type and is_in_parameters_space
@property
def name(self):
return self._properties.attributeName.split("/", 1)[-1]
@property
def value(self):
return self._properties.value
@property
def parameterType(self):
return (
"double"
if self._properties.attributeType == AlphaAttributeType.FLOAT.value
else "string"
)
class AlphaChannelDTO(AlphaAttributeWrapper):
"""It's simple wrapper for `AttributeDTO` objects which uses alpha series attributes to fake channels.
Alpha leaderboard doesn't have `ChannelDTO` since it doesn't support channels at all,
so we do need fake `ChannelDTO` class for backward compatibility with old client's code."""
_allowed_atribute_types = [
AlphaAttributeType.FLOAT_SERIES.value,
AlphaAttributeType.STRING_SERIES.value,
AlphaAttributeType.IMAGE_SERIES.value,
]
@property
def id(self):
return self._properties.attributeName
@property
def name(self):
return self._properties.attributeName.split("/", 1)[-1]
@property
def channelType(self):
attr_type = self._properties.attributeType
if attr_type == AlphaAttributeType.FLOAT_SERIES.value:
return ChannelType.NUMERIC.value
elif attr_type == AlphaAttributeType.STRING_SERIES.value:
return ChannelType.TEXT.value
elif attr_type == AlphaAttributeType.IMAGE_SERIES.value:
return ChannelType.IMAGE.value
@property
def x(self):
return self._properties.lastStep
@property
def y(self):
if self.channelType == ChannelType.IMAGE.value:
# We do not store last value for image series
return None
return self._properties.last
class AlphaChannelWithValueDTO:
"""Alpha leaderboard doesn't have `ChannelWithValueDTO` since it doesn't support channels at all,
so we do need fake `ChannelWithValueDTO` class for backward compatibility with old client's code"""
def __init__(self, channelId: str, channelName: str, channelType: str, x, y):
self._ch_id = channelId
self._ch_name = channelName
self._ch_type = channelType
self._x = x
self._y = y
@property
def channelId(self):
return self._ch_id
@property
def channelName(self):
return self._ch_name
@property
def channelType(self):
return self._ch_type
@property
def x(self):
return self._x
@x.setter
def x(self, x):
self._x = x
@property
def y(self):
return self._y
@y.setter
def y(self, y):
self._y = y
def _map_using_dict(el, el_name, source_dict) -> alpha_operation.Operation:
try:
return source_dict[el]
except KeyError as e:
raise NeptuneException(f"We're not supporting {el} {el_name}.") from e
def channel_type_to_operation(channel_type: ChannelType) -> alpha_operation.Operation:
_channel_type_to_operation = {
ChannelType.TEXT: alpha_operation.LogStrings,
ChannelType.NUMERIC: alpha_operation.LogFloats,
ChannelType.IMAGE: alpha_operation.LogImages,
}
return _map_using_dict(channel_type, "channel type", _channel_type_to_operation)
def channel_type_to_clear_operation(
channel_type: ChannelType,
) -> alpha_operation.Operation:
_channel_type_to_operation = {
ChannelType.TEXT: alpha_operation.ClearStringLog,
ChannelType.NUMERIC: alpha_operation.ClearFloatLog,
ChannelType.IMAGE: alpha_operation.ClearImageLog,
}
return _map_using_dict(channel_type, "channel type", _channel_type_to_operation)
def channel_value_type_to_operation(
channel_value_type: ChannelValueType,
) -> alpha_operation.Operation:
_channel_value_type_to_operation = {
ChannelValueType.TEXT_VALUE: alpha_operation.LogStrings,
ChannelValueType.NUMERIC_VALUE: alpha_operation.LogFloats,
ChannelValueType.IMAGE_VALUE: alpha_operation.LogImages,
}
return _map_using_dict(
channel_value_type, "channel value type", _channel_value_type_to_operation
)
def deprecated_img_to_alpha_image(img: dict) -> ImageValue:
return ImageValue(data=img["data"], name=img["name"], description=img["description"])