Serialize

Collection of serializers that transform Python data into a BSON-compatible form.

class vivarium.core.serialize.DictDeserializer[source]

Bases: vivarium.core.registry.Serializer

Iterates through dictionaries and applies deserializers.

can_deserialize(data: Any)bool[source]
deserialize(data: dict)dict[source]
class vivarium.core.serialize.FunctionSerializer[source]

Bases: vivarium.core.registry.Serializer

Serializer for function objects.

serialize(data: collections.abc.Callable)str[source]
class vivarium.core.serialize.NumpyFallbackSerializer[source]

Bases: vivarium.core.registry.Serializer

Orjson does not handle Numpy arrays with strings

serialize(data: Any)list[source]
class vivarium.core.serialize.ProcessSerializer[source]

Bases: vivarium.core.registry.Serializer

Serializer for processes if emit_process is enabled.

serialize(data: vivarium.core.process.Process)str[source]
class vivarium.core.serialize.QuantitySerializer[source]

Bases: vivarium.core.registry.Serializer

Serializes data with units into strings of the form !units[...], where ... is the result of calling str(data). Deserializes strings of this form back into data with units.

serialize(data: Any) → Union[List[str], str][source]
class vivarium.core.serialize.SequenceDeserializer[source]

Bases: vivarium.core.registry.Serializer

Iterates through lists and applies deserializers.

can_deserialize(data: Any)bool[source]
deserialize(data: Any)List[Any][source]
class vivarium.core.serialize.SetSerializer[source]

Bases: vivarium.core.registry.Serializer

Serializer for set objects.

serialize(data: set)List[source]
class vivarium.core.serialize.UnitsSerializer[source]

Bases: vivarium.core.registry.Serializer

Serializes data with units into strings of the form !units[...], where ... is the result of calling str(data). Deserializes strings of this form back into data with units.

can_deserialize(data: Any)bool[source]
deserialize(data: str, unit: Optional[pint.unit.Unit] = None)pint.quantity.Quantity[source]

Deserialize data with units from a human-readable string.

Parameters
  • data – The data to deserialize. Providing a list here is deprecated. You should use 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.

serialize(data: Any) → Union[List[str], str][source]
vivarium.core.serialize.deserialize_value(value: Any) → Any[source]

Find and apply the correct serializer for a value by calling each registered serializer’s vivarium.core.registry.Serializer.can_deserialize() method. Returns the value as is if no compatible serializer is found.

Parameters

value (Any) – Data to be deserialized

Raises

ValueError – Only one serializer should apply for any given value

Returns

Deserialized data

Return type

Any

vivarium.core.serialize.find_numpy_and_non_strings(d: dict, curr_path: tuple = (), saved_paths: Optional[List[tuple]] = None)List[tuple][source]

Return list of paths which terminate in a non-string or Numpy string dictionary key. Orjson does not handle these types of keys by default.

vivarium.core.serialize.make_fallback_serializer_function()collections.abc.Callable[source]

Creates a fallback function that is called by orjson on data of types that are not natively supported. Define and register instances of vivarium.core.registry.Serializer() with serialization routines for the types in question.

vivarium.core.serialize.serialize_value(value: Any, default: Optional[collections.abc.Callable] = None) → Any[source]

Apply orjson-based serialization routine on value.

Parameters
  • value (Any) – Data to be serialized. All keys must be strings. Notably, Numpy strings (np.str_) are not acceptable keys.

  • default (Callable) – A function that is called on any data of a type that is not natively supported by orjson. Returns an object that can be handled by default up to 254 times before an exception is raised.

Returns

Serialized data

Return type

Any