-
Notifications
You must be signed in to change notification settings - Fork 2
/
serialize.py
429 lines (340 loc) · 14.3 KB
/
serialize.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
from collections.abc import Sequence
import math
from typing import Any, Callable, List, Union
import warnings
import numpy as np
from bson import ObjectId
from pint import Unit
from pint.quantity import Quantity
from vivarium.core.registry import serializer_registry, Serializer
from vivarium.core.composer import Composer, Composite
from vivarium.core.process import Process
from vivarium.library.dict_utils import remove_multi_update
from vivarium.library.units import units
# Serialization and deserialization functions that handle arbitrary data
# types.
def serialize_value(value: Any) -> Any:
# Try to lookup by exclusive type
serializer = serializer_registry.access(str(type(value)))
# If lookup fails, search through registered serializers
if not serializer:
compatible_serializers = []
for serializer_name in serializer_registry.list():
serializer = serializer_registry.access(serializer_name)
if serializer.can_serialize(value):
compatible_serializers.append(serializer)
if not compatible_serializers:
raise ValueError(
f'No serializer found for {value} of type {type(value)}')
if len(compatible_serializers) > 1:
raise ValueError(
f'Multiple serializers ({compatible_serializers}) found '
f'for {value} of type {type(value)}')
serializer = compatible_serializers[0]
return serializer.serialize(value)
def deserialize_value(value: Any) -> Any:
compatible_serializers = []
for serializer_name in serializer_registry.list():
serializer = serializer_registry.access(serializer_name)
if serializer.can_deserialize(value):
compatible_serializers.append(serializer)
if not compatible_serializers:
raise ValueError(
f'No deserializer found for {value}')
if len(compatible_serializers) > 1:
raise ValueError(
f'Multiple deserializers ({compatible_serializers}) found '
f'for {value}')
serializer = compatible_serializers[0]
return serializer.deserialize(value)
# We can ignore the abstract-method warning because Serializer only
# requires that we override serialize() or serialize_to_string(), not
# both. Similar reasoning applies to deserialize() and
# deserialize_from_string().
class IdentitySerializer(Serializer): # pylint: disable=abstract-method
'''Serializer for base types that get serialized as themselves.'''
def __init__(self) -> None:
super().__init__(exclusive_types=(int, float, bool, str))
def can_serialize(self, data: Any) -> bool:
if (
isinstance(data, (int, float, bool, str))
and not NumpyScalarSerializer.can_serialize(data)):
return True
if data is None:
return True
return False
def serialize(self, data: Any) -> Any:
return data
def can_deserialize(self, data: Any) -> bool:
if isinstance(data, (int, float, bool)):
return True
if isinstance(data, str):
return not self.REGEX_FOR_SERIALIZED_ANY_TYPE.fullmatch(data)
if data is None:
return True
return False
def deserialize(self, data: Any) -> Any:
return data
class NumpySerializer(Serializer):
"""Serializer for Numpy arrays.
Numpy array serialization is lossy--we serialize to Python lists, so
deserialization will produce a Python list instead of a Numpy array.
"""
def __init__(self) -> None:
super().__init__(exclusive_types=(np.ndarray,))
def can_serialize(self, data: Any) -> bool:
return isinstance(data, np.ndarray)
def serialize(self, data: Any) -> Any:
"""Returns ``data.tolist()``."""
lst = data.tolist()
return serialize_value(lst)
def can_deserialize(self, data: Any) -> bool:
return False
def deserialize(self, data: Any) -> None:
raise NotImplementedError(
'There is no deserializer for numpy arrays.')
class SequenceSerializer(Serializer): # pylint: disable=abstract-method
def __init__(self) -> None:
super().__init__(exclusive_types=(list, tuple))
def can_serialize(self, data: Any) -> bool:
return isinstance(data, Sequence) and not isinstance(data, str)
def serialize(self, data: Any) -> List[Any]:
'''Serialize sequence to list of serialized elements.
Note that sequence serialization is lossy. For example, a tuple
will be serialized as a list, which will then be deserialized to
a list.
'''
return [serialize_value(value) for value in data]
def can_deserialize(self, data: Any) -> bool:
return isinstance(data, list)
def deserialize(self, data: Any) -> List[Any]:
return [deserialize_value(value) for value in data]
class DictSerializer(Serializer): # pylint: disable=abstract-method
def __init__(self) -> None:
super().__init__(exclusive_types=(dict,))
def can_serialize(self, data: Any) -> bool:
return isinstance(data, dict)
def serialize(self, data: dict) -> dict:
'''Serialize to dict of serialized elements.
Note that dict serialization of keys is lossy. For example, a
float key will be serialized as a string, which will then be
deserialized to a string.
'''
serialized = {}
for key, value in data.items():
if not isinstance(key, str):
key = str(key)
serialized[key] = serialize_value(value)
return serialized
def can_deserialize(self, data: Any) -> bool:
return isinstance(data, dict)
def deserialize(self, data: dict) -> dict:
return {
key: deserialize_value(value)
for key, value in data.items()
}
class NumpyScalarSerializer(Serializer):
"""Serializer for Numpy scalars.
Note that this serialization is lossy. Upon deserialization, the
serialized values will remain Python builtin types, not Numpy
scalars.
"""
def __init__(self) -> None:
super().__init__(exclusive_types=(
np.bool_,
np.int8, np.int16, np.int32, np.int64,
np.uint8, np.uint16, np.uint32, np.uint64,
np.float16, np.float32, np.float64, np.float128,
np.complex64, np.complex128, np.complex256,
))
@staticmethod
def can_serialize(data: Any) -> bool:
return isinstance(data, (np.integer, np.floating, np.bool_))
def serialize(
self,
data: Union[np.integer, np.floating, np.bool_],
) -> Union[int, float, bool]:
if isinstance(data, np.integer):
return int(data)
if isinstance(data, np.floating):
return float(data)
return bool(data)
def can_deserialize(self, data: Any) -> bool:
return False
def deserialize(self, data: Any) -> None:
raise NotImplementedError(
'Deserializing serialized Numpy scalars is not supported.')
class UnitsSerializer(Serializer):
"""Serializer for data with units."""
def __init__(self) -> None:
super().__init__(
name='units',
exclusive_types=(type(units.fg), type(1 * units.fg)))
def can_serialize(self, data: Any) -> bool:
return isinstance(data, (Quantity, Unit))
def serialize_to_string(self, data: Union[Quantity, Unit]) -> str:
return str(data)
# Here the differing argument is `unit`, which is optional, so we
# can ignore the pylint warning.
def serialize( # pylint: disable=arguments-differ
self,
data: Union[Quantity, Unit],
unit: Unit = None) -> Union[str, List[str]]:
"""Serialize data with units into a human-readable string.
Args:
data: The data to serialize. Should be a Pint object or a
list of such objects. Note that providing a list is
deprecated. You should use :py:func:`serialize_value`
instead, which uses a separate list serializer.
unit: The units to convert ``data`` to before serializing.
Optional. If omitted, no conversion occurs. This option
is deprecated and should not be used.
Returns:
The string representation of ``data`` if ``data`` is a
single Pint object. Otherwise, a list of string
representations.
"""
if unit is not None:
warnings.warn(
'The `unit` argument to `UnitsSerializer.serialize` is '
'deprecated.',
DeprecationWarning,
)
if isinstance(data, list):
warnings.warn(
'Passing a list to `UnitsSerializer.serialize` is '
'deprecated. Please use `serialize_value()` instead.',
DeprecationWarning,
)
if unit is not None:
# We can't convert `Unit` objects.
assert isinstance(data, Quantity)
data = [d.to(unit) for d in data]
return serialize_value(data)
if unit is not None:
# We can't convert `Unit` objects.
assert isinstance(data, Quantity)
data.to(unit)
# The superclass serialize() method uses
# `serialize_to_string()`.
if isinstance(data, Quantity) and isinstance(
data.magnitude, (list, tuple, np.ndarray)):
# Pint doesn't correctly deserialize Quantities whose
# magnitudes are lists/tuples, so instead we make them lists
# of Quantities and serialize accordingly.
return serialize_value(
[quantity * data.units for quantity in data.magnitude])
return super().serialize(data)
def deserialize_from_string(self, data: str) -> Quantity:
if data.startswith('nan'):
unit = data[len('nan'):].strip()
return math.nan * units(unit)
return units(data)
# Here the differing argument is `unit`, which is optional, so we
# can ignore the pylint warning.
def deserialize( # pylint: disable=arguments-differ
self, data: str, unit: Unit = None) -> Quantity:
"""Deserialize data with units from a human-readable string.
Args:
data: The data to deserialize. Providing a list here is
deprecated. You should use :py:func:`deserialize_value`
instead, which uses a separate list deserializer.
unit: The units to convert ``data`` to after deserializing.
If omitted, no conversion occurs. This option is
deprecated.
Returns:
A single deserialized object or, if ``data`` is a list, a
list of deserialized objects.
"""
if unit is not None:
warnings.warn(
'The `unit` argument to `UnitsSerializer.deserialize` is '
'deprecated.',
DeprecationWarning,
)
if isinstance(data, list):
warnings.warn(
'Passing a list to `UnitsSerializer.deserialize` is '
'deprecated. Please use `deserialize_value()` instead.',
DeprecationWarning,
)
unit_data = [units(d) for d in data]
if unit is not None:
unit_data = [d.to(unit) for d in data]
else:
# The superclass deserialize() uses
# deserialize_from_string().
unit_data = super().deserialize(data)
if unit is not None:
unit_data.to(unit)
return unit_data
class ProcessSerializer(Serializer):
"""Serializer for processes.
Currently only supports serialization (for emitting simulation
configs).
"""
def can_serialize(self, data: Any) -> bool:
return isinstance(data, Process)
def serialize_to_string(self, data: Process) -> str:
"""Create a dictionary of process name and parameters."""
return str(dict(data.parameters, _name=data.name))
def can_deserialize(self, data: Any) -> bool:
return False
def deserialize(self, data: Any) -> None:
raise NotImplementedError(
'Processes cannot be deserialized.')
class ComposerSerializer(Serializer):
"""Serializer for composers.
Currently only supports serialization (for emitting simulation
configs).
"""
def can_serialize(self, data: Any) -> bool:
return isinstance(data, Composer)
def serialize_to_string(self, data: Composer) -> str:
"""Create a dictionary of composer name and parameters."""
return str(dict(data.config, _name=str(type(data))))
def can_deserialize(self, data: Any) -> bool:
return False
def deserialize(self, data: Any) -> None:
raise NotImplementedError(
'Composers cannot be deserialized.')
class FunctionSerializer(Serializer):
"""Serializer for functions.
Currently only supports serialization (for emitting simulation
configs).
"""
def can_serialize(self, data: Any) -> bool:
return callable(data)
def serialize_to_string(self, data: Callable) -> str:
return str(data)
def can_deserialize(self, data: Any) -> bool:
return False
def deserialize(self, data: Any) -> None:
raise NotImplementedError(
'Functions cannot be deserialized.')
class ObjectIdSerializer(Serializer):
"""Serializer for BSON ObjectIds.
Currently only supports serialization.
"""
def can_serialize(self, data: Any) -> bool:
return isinstance(data, ObjectId)
def serialize_to_string(self, data: ObjectId) -> str:
return str(data)
def can_deserialize(self, data: Any) -> bool:
return False
def deserialize(self, data: Any) -> None:
raise NotImplementedError(
'ObjectIds cannot be deserialized.')
def composite_specification(
composite: Composite,
initial_state: bool = False,
) -> dict:
"""Return the serialized specification for a :term:`composite` instance"""
composite_dict = serialize_value(composite)
composite_dict.pop('_schema')
if initial_state:
composite_initial_state = remove_multi_update(composite.initial_state())
composite_dict = dict(
composite_dict,
initial_state=composite_initial_state)
return composite_dict