yggdrasil.serialize package

Submodules

yggdrasil.serialize.AsciiMapSerialize module

class yggdrasil.serialize.AsciiMapSerialize.AsciiMapSerialize(*args, **kwargs)[source]

Bases: SerializeBase

Class for serializing/deserializing name/value mapping.

Parameters:

delimiter (str, optional) – Delimiter that should be used to separate name/value pairs in the map. Defaults to t.

classmethod concatenate(objects, **kwargs)[source]

Concatenate objects to get object that would be recieved if the concatenated serialization were deserialized.

Parameters:
  • objects (list) – Objects to be concatenated.

  • **kwargs – Additional keyword arguments are ignored.

Returns:

Set of objects that results from concatenating those provided.

Return type:

list

concats_as_str = False
default_datatype = {'type': 'object'}
func_deserialize(msg)[source]

Deserialize a message.

Parameters:

msg (bytes) – Message to be deserialized.

Returns:

Deserialized Python dictionary.

Return type:

dict

func_serialize(args)[source]

Serialize a message.

Parameters:

args (dict) – Python dictionary to be serialized.

Returns:

Serialized message.

Return type:

bytes, str

classmethod get_testing_options(**kwargs)[source]

Method to return a dictionary of testing options for this class.

Returns:

Dictionary of variables to use for testing.

Return type:

dict

yggdrasil.serialize.AsciiTableSerialize module

class yggdrasil.serialize.AsciiTableSerialize.AsciiTableSerialize(*args, **kwargs)[source]

Bases: DefaultSerialize

Class for serialize table output into bytes messages comprising a formatted ASCII table.

Parameters:
  • format_str (str, optional) – If provided, this string will be used to format messages from a list of arguments and parse messages to get a list of arguments in C printf/scanf style. Defaults to None and messages are assumed to already be bytes.

  • field_names (list, optional) – The names of fields in the format string. If not provided, names are set based on the order of the fields in the format string.

  • field_units (list, optional) – The units of fields in the format string. If not provided, all fields are assumed to be dimensionless.

  • as_array (bool, optional) – If True, each of the arguments being serialized/deserialized will be arrays that are converted to/from bytes in column major (‘F’) order. Otherwise, each argument should be a scalar. Defaults to False.

  • delimiter (str, optional) – Character(s) that should be used to separate columns. Defaults to ‘t’.

  • use_astropy (bool, optional) – If True, the astropy package will be used to serialize/deserialize table. Defaults to False.

  • **kwargs – Additional keyword args are processed as part of the type definition.

format_str

Format string used to format/parse bytes messages from/to a list of arguments in C printf/scanf style.

Type:

str

field_names

The names of fields in the format string.

Type:

list

field_units

The units of fields in the format string.

Type:

list

as_array

True or False depending if serialized/deserialized python objects will be arrays or scalars.

Type:

bool

delimiter

Character(s) that should be used to separate columns.

Type:

str

use_astropy

If True, the astropy package will be used to serialize/deserialize table.

Type:

bool

default_datatype = {'type': 'array'}
default_read_meth = 'readline'
deserialize_file_header(fd)[source]

Deserialize the header information from the file and update the serializer.

Parameters:

fd (file) – File containing header.

file_extensions = ['.txt']
func_deserialize(msg)[source]

Deserialize a message.

Parameters:

msg – Message to be deserialized.

Returns:

Deserialized message.

Return type:

obj

func_serialize(args)[source]

Serialize a message.

Parameters:

args – List of arguments to be formatted or numpy array to be serialized.

Returns:

Serialized message.

Return type:

bytes, str

classmethod get_testing_options(**kwargs)[source]

Method to return a dictionary of testing options for this class.

Parameters:

**kwargs – Keyword arguments are passed to the parent class’s method.

Returns:

Dictionary of variables to use for testing.

Return type:

dict

has_header = True
normalize(args)[source]

Normalize a message to conform to the expected datatype.

Parameters:

args (object) – Message arguments.

Returns:

Normalized message.

Return type:

object

property read_meth

Method that should be used to read data for deserialization.

Type:

str

serialize_file_header()[source]

Return the serialized header information that should be prepended to files serialized using this class.

Returns:

Header string that should be written to the file.

Return type:

bytes

update_field_names()[source]

list: Names for each field in the data type.

update_field_units()[source]

list: Units for each field in the data type.

update_format_str()[source]

Update the format string based on the type definition.

update_serializer(*args, **kwargs)[source]

Update serializer with provided information.

Parameters:
  • skip_type (bool, optional) – If True, everything is updated except the data type. Defaults to False.

  • **kwargs – Additional keyword arguments are processed as part of they type definition and are parsed for old-style keywords.

Raises:

RuntimeError – If there are keywords that are not valid datatype keywords (currect or old-style).

yggdrasil.serialize.CABOSerialize module

class yggdrasil.serialize.CABOSerialize.CABOSerialize(*args, **kwargs)[source]

Bases: AsciiMapSerialize

Class for serializing/deserializing CABO parameter files.

classmethod concatenate(objects, **kwargs)[source]

Concatenate objects to get object that would be recieved if the concatenated serialization were deserialized.

Parameters:
  • objects (list) – Objects to be concatenated.

  • **kwargs – Additional keyword arguments are ignored.

Returns:

Set of objects that results from concatenating those provided.

Return type:

list

concats_as_str = True
default_datatype = {'type': 'object'}
file_extensions = ['.cab']
func_deserialize(msg)[source]

Deserialize a message.

Parameters:

msg (bytes) – Message to be deserialized.

Returns:

Deserialized Python dictionary.

Return type:

dict

func_serialize(args)[source]

Serialize a message.

Parameters:

args (dict) – Python dictionary to be serialized.

Returns:

Serialized message.

Return type:

bytes, str

classmethod get_testing_options(**kwargs)[source]

Method to return a dictionary of testing options for this class.

Returns:

Dictionary of variables to use for testing.

Return type:

dict

classmethod parse_units(x)[source]

Parse units.

Parameters:

x (str) – Unit string.

Returns:

Propertly formatted units.

Return type:

str

yggdrasil.serialize.DefaultSerialize module

class yggdrasil.serialize.DefaultSerialize.DefaultSerialize(*args, **kwargs)[source]

Bases: SerializeBase

Default class for serializing/deserializing a python object into/from a bytes message.

Parameters:

**kwargs – Additional keyword args are passed to the parent class.

classmethod concatenate(objects, as_array=False, **kwargs)[source]

Concatenate objects to get object that would be recieved if the concatenated serialization were deserialized.

Parameters:
  • objects (list) – Objects to be concatenated.

  • as_array (bool, optional) – If True, the objects in the list are complete columns in a table and as_format is set to True. Defaults to False.

  • **kwargs – Additional keyword arguments are ignored.

Returns:

Set of objects that results from concatenating those provided.

Return type:

list

classmethod dict2object(obj, as_array=False, field_names=None, **kwargs)[source]

Conver a dictionary to a message object.

Parameters:
  • obj (dict) – Dictionary to convert to serializable object.

  • as_array (bool, optional) – If True, the objects in the list are complete columns in a table and as_format is set to True. Defaults to False.

  • field_names (list, optional) – The field names associated with a table-like data type. Defaults to None. This keyword must be provided if as_array is True.

  • **kwargs – Additional keyword arguments are ignored.

Returns:

Serializable object.

Return type:

object

file_extensions = ['.ygg']
func_deserialize(msg)[source]

Deserialize a message.

Parameters:

msg – Message to be deserialized.

Returns:

Deserialized message.

Return type:

obj

func_serialize(args)[source]

Serialize a message.

Parameters:

args – List of arguments to be formatted or numpy array to be serialized.

Returns:

Serialized message.

Return type:

bytes, str

classmethod get_testing_options(**kwargs)[source]

Method to return a dictionary of testing options for this class.

classmethod object2array(obj, as_array=False, field_names=None, **kwargs)[source]

Convert a message object into an array.

Parameters:
  • obj (object) – Object that would be serialized by this class and should be returned in an array form.

  • as_array (bool, optional) – If True, the objects in the list are complete columns in a table and as_format is set to True. Defaults to False.

  • field_names (list, optional) – The field names associated with a table-like data type. Defaults to None. This keyword must be provided if as_array is True.

  • **kwargs – Additional keyword arguments are ignored.

Returns:

Array version of the provided object.

Return type:

np.array

classmethod object2dict(obj, as_array=False, field_names=None, **kwargs)[source]

Convert a message object into a dictionary.

Parameters:
  • obj (object) – Object that would be serialized by this class and should be returned in a dictionary form.

  • as_array (bool, optional) – If True, the objects in the list are complete columns in a table and as_format is set to True. Defaults to False.

  • field_names (list, optional) – The field names associated with a table-like data type. Defaults to None. This keyword must be provided if as_array is True.

  • **kwargs – Additional keyword arguments are ignored.

Returns:

Dictionary version of the provided object.

Return type:

dict

yggdrasil.serialize.DirectSerialize module

class yggdrasil.serialize.DirectSerialize.DirectSerialize(*args, **kwargs)[source]

Bases: SerializeBase

Class for directly serializing bytes.

classmethod concatenate(objects, **kwargs)[source]

Concatenate objects to get object that would be recieved if the concatenated serialization were deserialized.

Parameters:
  • objects (list) – Objects to be concatenated.

  • **kwargs – Additional keyword arguments are ignored.

Returns:

Set of objects that results from concatenating those provided.

Return type:

list

default_datatype = {'type': 'bytes'}
func_deserialize(msg)[source]

Deserialize a message.

Parameters:

msg – Message to be deserialized.

Returns:

Deserialized message.

Return type:

obj

func_serialize(args)[source]

Serialize a message.

Parameters:

args – List of arguments to be formatted or numpy array to be serialized.

Returns:

Serialized message.

Return type:

bytes, str

classmethod get_testing_options(**kwargs)[source]

Method to return a dictionary of testing options for this class.

Returns:

Dictionary of variables to use for testing. Key/value pairs:
kwargs (dict): Keyword arguments for comms tested with the

provided content.

empty (object): Object produced from deserializing an empty

message.

objects (list): List of objects to be serialized/deserialized. extra_kwargs (dict): Extra keyword arguments not used to

construct type definition.

typedef (dict): Type definition resulting from the supplied

kwargs.

dtype (np.dtype): Numpy data types that is consistent with the

determined type definition.

Return type:

dict

yggdrasil.serialize.FunctionalSerialize module

class yggdrasil.serialize.FunctionalSerialize.FunctionalSerialize(*args, **kwargs)[source]

Bases: SerializeBase

Class for serializing/deserializing a Python object into/from a bytes message using defined functions.

Parameters:
  • encoded_datatype (schema, optional) – JSON schema describing the type that serialized objects should conform to. Defaults to the class attribute default_encoded_datatype. If either func_serialize or func_deserialize are not provided, this needs to be specified in order to serialize non-bytes objects.

  • func_serialize (func, optional) – Callable object that takes Python objects as input and returns a representation that conforms to encoded_datatype. Defaults to None and the default serialization for encoded_datatype will be used.

  • func_deserialize (func, optional) – Callable object that takes objects of a type that conforms to encoded_datatype and returns a deserialized Python object. Defaults to None and the default deserialization for encoded_datatype will be used.

  • **kwargs – Additional keyword args are passed to the parent class’s constructor.

func_deserialize = None
func_serialize = None
property serializer_info

Serializer info.

Type:

dict

yggdrasil.serialize.JSONSerialize module

class yggdrasil.serialize.JSONSerialize.JSONSerialize(*args, **kwargs)[source]

Bases: SerializeBase

Class for serializing a python object into a bytes message using JSON.

Parameters:
  • indent (str, int, optional) – String or number of spaces that should be used to indent each level within the seiralized structure. Defaults to ‘t’.

  • sort_keys (bool, optional) – If True, the serialization of dictionaries will be in key sorted order. Defaults to True.

classmethod concatenate(objects, **kwargs)[source]

Concatenate objects to get object that would be recieved if the concatenated serialization were deserialized.

Parameters:
  • objects (list) – Objects to be concatenated.

  • **kwargs – Additional keyword arguments are ignored.

Returns:

Set of objects that results from concatenating those provided.

Return type:

list

concats_as_str = False
default_datatype = {'type': 'object'}
file_extensions = ['.json']
func_deserialize(msg)[source]

Deserialize a message.

Parameters:

msg (str, bytes) – Message to be deserialized.

Returns:

Deserialized Python object.

Return type:

obj

func_serialize(args)[source]

Serialize a message.

Parameters:

args (obj) – Python object to be serialized.

Returns:

Serialized message.

Return type:

bytes, str

classmethod get_testing_options(**kwargs)[source]

Method to return a dictionary of testing options for this class.

Returns:

Dictionary of variables to use for testing.

Return type:

dict

yggdrasil.serialize.JSONSerialize.decode_json(msg, **kwargs)[source]

Decode a Python object from a JSON serialization.

Parameters:
  • msg (str) – JSON serialization to decode.

  • **kwargs – Additional keyword arguments are passed to rapidjson.loads.

Returns:

Deserialized Python object.

Return type:

object

yggdrasil.serialize.JSONSerialize.encode_json(obj, fd=None, indent=None, sort_keys=True, **kwargs)[source]

Encode a Python object in JSON format.

Parameters:
  • obj (object) – Python object to encode.

  • fd (file, optional) – File descriptor for file that encoded object should be written to. Defaults to None and string is returned.

  • indent (int, str, optional) – Indentation for new lines in encoded string. Defaults to None.

  • sort_keys (bool, optional) – If True, the keys will be output in sorted order. Defaults to True.

  • **kwargs – Additional keyword arguments are passed to rapidjson.dumps.

Returns:

Encoded object.

Return type:

str, bytes

yggdrasil.serialize.JSONSerialize.indent_char2int(indent)[source]

Convert a character indent into a number of spaces that should be used. Tabs are set to be equivalent to 4 spaces.

Parameters:

indent (str) – String indent.

Returns:

Number of whitespaces that is equivalent to the provided string.

Return type:

int

yggdrasil.serialize.MatSerialize module

class yggdrasil.serialize.MatSerialize.MatSerialize(*args, **kwargs)[source]

Bases: SerializeBase

Class for serializing a python object into a bytes message using the Matlab .mat format.

classmethod concatenate(objects, **kwargs)[source]

Concatenate objects to get object that would be recieved if the concatenated serialization were deserialized.

Parameters:
  • objects (list) – Objects to be concatenated.

  • **kwargs – Additional keyword arguments are ignored.

Returns:

Set of objects that results from concatenating those provided.

Return type:

list

concats_as_str = False
default_datatype = {'type': 'object'}
file_extensions = ['.mat']
func_deserialize(msg)[source]

Deserialize a message.

Parameters:

msg (str, bytes) – Message to be deserialized.

Returns:

Deserialized Python object.

Return type:

obj

func_serialize(args)[source]

Serialize a message.

Parameters:

args (obj) – Python object to be serialized.

Returns:

Serialized message.

Return type:

bytes, str

Raises:

TypeError – If args is not a dictionary.

classmethod get_testing_options(**kwargs)[source]

Method to return a dictionary of testing options for this class.

Returns:

Dictionary of variables to use for testing.

Return type:

dict

yggdrasil.serialize.ObjSerialize module

class yggdrasil.serialize.ObjSerialize.ObjDict[source]

Bases: GeometryBase, ObjWavefront

Enhanced dictionary class for storing Obj information.

apply_scalar_map(*args, **kwargs)[source]

Set the color of faces in a 3D object based on a scalar map. This creates a copy unless no_copy is True.

Parameters:
  • scalar_arr (arr) – Scalar values that should be mapped to colors for each face.

  • color_map (str, optional) – The name of the color map that should be used. Defaults to ‘plasma’.

  • vmin (float, optional) – Value that should map to the minimum of the colormap. Defaults to min(scalar_arr).

  • vmax (float, optional) – Value that should map to the maximum of the colormap. Defaults to max(scalar_arr).

  • scaling (str, optional) – Scaling that should be used to map the scalar array onto the colormap. Defaults to ‘linear’.

  • scale_by_area (bool, optional) – If True, the elements of the scalar array will be multiplied by the area of the corresponding face. If True, vmin and vmax should be in terms of the scaled array. Defaults to False.

  • no_copy (bool, optional) – If True, the returned object will not be a copy. Defaults to False.

Returns:

Obj with updated vertex colors.

Return type:

dict

classmethod from_shape(shape, d, conversion=1.0)[source]

Create a ply dictionary from a PlantGL shape and descritizer.

Parameters:
  • scene (openalea.plantgl.scene) – Scene that should be descritized.

  • d (openalea.plantgl.descritizer) – Descritizer.

  • conversion (float, optional) – Conversion factor that should be applied to the vertex positions. Defaults to 1.0.

to_geom_args(conversion=1.0, name=None)[source]

Get arguments for creating a PlantGL geometry.

Parameters:
  • conversion (float, optional) – Conversion factor that should be applied to the vertices. Defaults to 1.0.

  • name (str, optional) – Name that should be given to the created PlantGL symbol. Defaults to None and is ignored.

Returns:

Class, arguments and keyword arguments for PlantGL geometry.

Return type:

tuple

class yggdrasil.serialize.ObjSerialize.ObjSerialize(*args, **kwargs)[source]

Bases: PlySerialize

Class for serializing/deserializing .obj file formats. Reader adapted from https://www.pygame.org/wiki/OBJFileLoader.

default_datatype = {'type': 'obj'}
file_extensions = ['.obj']
func_deserialize(msg)[source]

Deserialize a message.

Parameters:

msg (bytes) – Message to be deserialized.

Returns:

Deserialized message.

Return type:

obj

func_serialize(args)[source]

Serialize a message.

Parameters:

args – List of arguments to be formatted or numpy array to be serialized.

Returns:

Serialized message.

Return type:

bytes

classmethod get_testing_options(**kwargs)[source]

Method to return a dictionary of testing options for this class.

Returns:

Dictionary of variables to use for testing.

Return type:

dict

normalize(args)[source]

Normalize a message to conform to the expected datatype.

Parameters:

args (object) – Message arguments.

Returns:

Normalized message.

Return type:

object

yggdrasil.serialize.PandasSerialize module

class yggdrasil.serialize.PandasSerialize.PandasSerialize(*args, **kwargs)[source]

Bases: AsciiTableSerialize

Class for serializing/deserializing Pandas data frames.

Parameters:
  • no_header (bool, optional) – If True, headers will not be read or serialized from/to tables. Defaults to False.

  • str_as_bytes (bool, optional) – If True, strings in columns are read as bytes. Defaults to False.

classmethod apply_field_names(frame, field_names=None)[source]

Apply field names as columns to a frame, first checking for a mapping. If there is a direct mapping, the columns are reordered to match the order of the field names. If there is not an overlap in the field names and columns, a one-to-one mapping is assumed, but a warning is issued. If there is a partial overlap, an error is raised.

Parameters:
  • frame (pandas.DataFrame) – Frame to apply field names to as columns.

  • field_names (list, optional) – New field names that should be applied. If not provided, the original frame will be returned unaltered.

Returns:

Frame with updated field names.

Return type:

pandas.DataFrame

Raises:

RuntimeError – If there is a partial overlap between the field names and columns.

as_array = True
cformat2nptype(*args, **kwargs)[source]

Method to convert c format string to numpy data type.

Parameters:
  • *args – Arguments are passed to serialize.cformat2nptype.

  • **kwargs – Keyword arguments are passed to serialize.cformat2nptype.

Returns:

Corresponding numpy data type.

Return type:

np.dtype

classmethod concatenate(objects, **kwargs)[source]

Concatenate objects to get object that would be recieved if the concatenated serialization were deserialized.

Parameters:
  • objects (list) – Objects to be concatenated.

  • **kwargs – Additional keyword arguments are ignored.

Returns:

Set of objects that results from concatenating those provided.

Return type:

list

concats_as_str = False
consolidate_array(out)[source]

Consolidate message into a structure numpy array if possible.

Parameters:

out (list, tuple, np.ndarray) – Object to consolidate into a structured numpy array.

Returns:

Structured numpy array containing consolidated message.

Return type:

np.ndarray

Raises:

ValueError – If the array cannot be consolidated.

default_read_meth = 'read'
deserialize_file_header(fd)[source]

Deserialize the header information from the file and update the serializer.

Parameters:

fd (file) – File containing header.

classmethod dict2object(obj, field_names=None, **kwargs)[source]

Conver a dictionary to a message object.

Parameters:
  • obj (dict) – Dictionary to convert to serializable object.

  • **kwargs – Additional keyword arguments are ignored.

Returns:

Serializable object.

Return type:

object

disable_file_header()[source]

Set serializer attributes to disable file headers from being included in the serializations.

property empty_msg

Object indicating empty message.

Type:

obj

enable_file_header()[source]

Set serializer attributes to enable file headers to be included in the serializations.

func_deserialize(msg)[source]

Deserialize a message.

Parameters:

msg (str, bytes) – Message to be deserialized.

Returns:

Deserialized Python object.

Return type:

obj

func_serialize(args)[source]

Serialize a message.

Parameters:

args (obj) – Python object to be serialized.

Returns:

Serialized message.

Return type:

bytes, str

get_field_names(*args, **kwargs)[source]

Get the field names for an array of fields.

Parameters:
  • *args – Arguments are passed to the parent class’s method.

  • **kwargs – Keyword arguments are passed to the parent class’s method.

Returns:

Names for each field in the data type.

Return type:

list

classmethod get_testing_options(not_as_frames=False, no_names=False, no_header=False, **kwargs)[source]

Method to return a dictionary of testing options for this class.

Parameters:
  • not_as_frames (bool, optional) – If True, the returned example includes data that is not in a pandas data frame. Defaults to False.

  • no_names (bool, optional) – If True, an example is returned where the names are not provided to the deserializer. Defaults to False.

  • no_header (bool, optional) – If True, an example is returned

  • False. (where a header is not included. Defaults to) –

Returns:

Dictionary of variables to use for testing.

Return type:

dict

initialize_from_message(msg, **kwargs)[source]

Initialize the serializer based on recieved message.

Parameters:
  • msg (object) – Message that serializer should be initialized from.

  • **kwargs – Additional keyword arguments are treated as metadata that may contain additional information for initializing the serializer.

normalize(args)[source]

Normalize a message to conform to the expected datatype.

Parameters:

args (object) – Message arguments.

Returns:

Normalized message.

Return type:

object

classmethod normalize_bytes2unicode(args)[source]

Convert columns that are bytes to unicode.

Parameters:

args (pd.DataFrame) – Pandas dataframe to convert columns for.

Returns:

Version of args without bytes in columns.

Return type:

pd.DataFrame

classmethod normalize_unicode2bytes(args)[source]

Convert columns that are unicode to bytes.

Parameters:

args (pd.DataFrame) – Pandas dataframe to convert columns for.

Returns:

Version of args without unicode in columns.

Return type:

pd.DataFrame

classmethod object2array(obj, **kwargs)[source]

Convert a message object into an array.

Parameters:
  • obj (object) – Object that would be serialized by this class and should be returned in an array form.

  • **kwargs – Additional keyword arguments are ignored.

Returns:

Array version of the provided object.

Return type:

np.array

classmethod object2dict(obj, **kwargs)[source]

Convert a message object into a dictionary.

Parameters:
  • obj (object) – Object that would be serialized by this class and should be returned in a dictionary form.

  • **kwargs – Additional keyword arguments are ignored.

Returns:

Dictionary version of the provided object.

Return type:

dict

property send_converter
serialize_file_header()[source]

Return the serialized header information that should be prepended to files serialized using this class.

Returns:

Header string that should be written to the file.

Return type:

bytes

yggdrasil.serialize.PickleSerialize module

class yggdrasil.serialize.PickleSerialize.PickleSerialize(*args, **kwargs)[source]

Bases: DefaultSerialize

Class for serializing a python object into a bytes message by pickling.

classmethod concatenate(objects, **kwargs)[source]

Concatenate objects to get object that would be recieved if the concatenated serialization were deserialized.

Parameters:
  • objects (list) – Objects to be concatenated.

  • **kwargs – Additional keyword arguments are ignored.

Returns:

Set of objects that results from concatenating those provided.

Return type:

list

file_extensions = ['.pkl']
func_deserialize(msg)[source]

Deserialize a message.

Parameters:

msg (str, bytes) – Message to be deserialized.

Returns:

Deserialized Python object.

Return type:

obj

func_serialize(args)[source]

Serialize a message.

Parameters:

args (obj) – Python object to be serialized.

Returns:

Serialized message.

Return type:

bytes, str

classmethod get_first_frame(msg)[source]

Extract one frame from the provided message that may contain one or more frames.

Parameters:

msg (bytes) – Message containing one or more frames.

Returns:

Portion of message containing the first frame. If no frames

are found, an empty string will be returned.

Return type:

bytes

classmethod get_testing_options(**kwargs)[source]

Method to return a dictionary of testing options for this class.

Returns:

Dictionary of variables to use for testing.

Return type:

dict

is_framed = True

yggdrasil.serialize.PlySerialize module

class yggdrasil.serialize.PlySerialize.GeometryBase[source]

Bases: object

Base class for extening rapidjson geometry classes.

apply_scalar_map(scalar_arr, color_map=None, vmin=None, vmax=None, scaling='linear', scale_by_area=False, no_copy=False, _as_obj=False)[source]

Set the color of faces in a 3D object based on a scalar map. This creates a copy unless no_copy is True.

Parameters:
  • scalar_arr (arr) – Scalar values that should be mapped to colors for each face.

  • color_map (str, optional) – The name of the color map that should be used. Defaults to ‘plasma’.

  • vmin (float, optional) – Value that should map to the minimum of the colormap. Defaults to min(scalar_arr).

  • vmax (float, optional) – Value that should map to the maximum of the colormap. Defaults to max(scalar_arr).

  • scaling (str, optional) – Scaling that should be used to map the scalar array onto the colormap. Defaults to ‘linear’.

  • scale_by_area (bool, optional) – If True, the elements of the scalar array will be multiplied by the area of the corresponding face. If True, vmin and vmax should be in terms of the scaled array. Defaults to False.

  • no_copy (bool, optional) – If True, the returned object will not be a copy. Defaults to False.

Returns:

Geometry with updated vertex colors.

Return type:

dict

classmethod from_scene(scene, d=None, conversion=1.0)[source]

Create a geometry dictionary from a PlantGL scene and descritizer.

Parameters:
  • scene (openalea.plantgl.scene) – Scene that should be descritized.

  • d (openalea.plantgl.descritizer, optional) – Descritizer. Defaults to openalea.plantgl.all.Tesselator.

  • conversion (float, optional) – Conversion factor that should be applied to the vertex positions. Defaults to 1.0.

classmethod from_shape(shape, d, conversion=1.0, _as_obj=False)[source]

Create a geometry dictionary from a PlantGL shape and descritizer.

Parameters:
  • scene (openalea.plantgl.scene) – Scene that should be descritized.

  • d (openalea.plantgl.descritizer) – Descritizer.

  • conversion (float, optional) – Conversion factor that should be applied to the vertex positions. Defaults to 1.0.

to_geom_args(conversion=1.0, name=None, _as_obj=False)[source]

Get arguments for creating a PlantGL geometry.

Parameters:
  • conversion (float, optional) – Conversion factor that should be applied to the vertices. Defaults to 1.0.

  • name (str, optional) – Name that should be given to the created PlantGL symbol. Defaults to None and is ignored.

Returns:

Class, arguments and keyword arguments for PlantGL geometry.

Return type:

tuple

to_scene(conversion=1.0, name=None)[source]

Create a PlantGL scene from a geometry dictionary.

Parameters:
  • conversion (float, optional) – Conversion factor that should be applied to the vertices. Defaults to 1.0.

  • name (str, optional) – Name that should be given to the created PlantGL symbol. Defaults to None and is ignored.

Returns:

class yggdrasil.serialize.PlySerialize.PlyDict[source]

Bases: GeometryBase, Ply

Enhanced dictionary class for storing Ply information.

class yggdrasil.serialize.PlySerialize.PlySerialize(*args, **kwargs)[source]

Bases: SerializeBase

Class for serializing/deserializing .ply file formats.

Parameters:
  • write_header (bool, optional) – If True, headers will be added to serialized output. Defaults to True.

  • newline (str, optional) – String that should be used for new lines. Defaults to ‘n’.

  • prune_duplicates (bool, optional) – If True, serialized meshes in array format will be pruned of duplicates when being normalized into a Ply object. If False, duplicates will not be pruned. Defaults to True.

write_header

If True, headers will be added to serialized output.

Type:

bool

newline

String that should be used for new lines.

Type:

str

default_rgb

Default color in RGB that should be used for missing colors.

Type:

list

classmethod concatenate(objects, **kwargs)[source]

Concatenate objects to get object that would be recieved if the concatenated serialization were deserialized.

Parameters:
  • objects (list) – Objects to be concatenated.

  • **kwargs – Additional keyword arguments are ignored.

Returns:

Set of objects that results from concatenating those provided.

Return type:

list

concats_as_str = False
default_datatype = {'type': 'ply'}
file_extensions = ['.ply']
func_deserialize(msg)[source]

Deserialize a message.

Parameters:

msg (bytes) – Message to be deserialized.

Returns:

Deserialized message.

Return type:

obj

func_serialize(args)[source]

Serialize a message.

Parameters:

args – List of arguments to be formatted or numpy array to be serialized.

Returns:

Serialized message.

Return type:

bytes

classmethod get_testing_options(**kwargs)[source]

Method to return a dictionary of testing options for this class.

Returns:

Dictionary of variables to use for testing.

Return type:

dict

property initialized

True if the serializer has been initialized.

Type:

bool

classmethod is_mesh(args)[source]

Check if an object is a 3D mesh with x, y, z for vertices in each face in rows.

Parameters:

args (object) – Object to check.

Returns:

True if object is a mesh, false otherwise.

Return type:

bool

normalize(args)[source]

Normalize a message to conform to the expected datatype.

Parameters:

args (object) – Message arguments.

Returns:

Normalized message.

Return type:

object

yggdrasil.serialize.SerializeBase module

class yggdrasil.serialize.SerializeBase.SerializeBase(*args, **kwargs)[source]

Bases: YggClass

Base class for serializing/deserializing a Python object into/from a bytes message.

Parameters:
  • newline (str, optional) – One or more characters indicating a newline. Defaults to ‘n’.

  • comment (str, optional) – One or more characters indicating a comment. Defaults to ‘# ‘.

  • datatype (schema, optional) – JSON schema defining the type of object that the serializer will be used to serialize/deserialize. Defaults to default_datatype.

  • **kwargs – Additional keyword args are processed as part of the type definition.

initialized

True if the serializer has been initialized either by input arguments specifying the type or by infering the type from a processed message.

Type:

bool

Class Attributes:
has_header (bool): True if the serialization has a header when written

to a file.

default_read_meth (str): Default method that data should be read from

a file for deserialization.

is_framed (bool): True if the serialization has a frame allowing

multiple serialized objects to be recovered from a single message.

concats_as_str (bool): True if serialized objects can be concatenated

directly as strings.

encoded_datatype (schema): JSON schema defining the type of object

produced by the class’s func_serialize method. For most classes this will be {‘type’: ‘scalar’, ‘subtype’: ‘string’}, indicating that the method will produce bytes suitable for serialization.

static before_registration(cls)[source]

Operations that should be performed to modify class attributes prior to registration.

cformat2nptype(*args, **kwargs)[source]

Method to convert c format string to numpy data type.

Parameters:
  • *args – Arguments are passed to serialize.cformat2nptype.

  • **kwargs – Keyword arguments are passed to serialize.cformat2nptype.

Returns:

Corresponding numpy data type.

Return type:

np.dtype

classmethod concatenate(objects, **kwargs)[source]

Concatenate objects to get object that would be recieved if the concatenated serialization were deserialized.

Parameters:
  • objects (list) – Objects to be concatenated.

  • **kwargs – Additional keyword arguments are ignored.

Returns:

Set of objects that results from concatenating those provided.

Return type:

list

concats_as_str = True
consolidate_array(out)[source]

Consolidate message into a structure numpy array if possible.

Parameters:

out (list, tuple, np.ndarray) – Object to consolidate into a structured numpy array.

Returns:

Structured numpy array containing consolidated message.

Return type:

np.ndarray

Raises:

ValueError – If the array cannot be consolidated.

decode(msg, no_data=False, metadata=None)[source]

Decode message parts into header and body.

Parameters:
  • msg (str, bytes) – Message to be decoded.

  • no_data (bool, optional) – If True, only the metadata is returned. Defaults to False.

  • metadata (dict, optional) – Metadata that should be used to deserialize the message instead of the current header content. Defaults to None and is not used.

Returns:

Deserialized message and header information.

Return type:

tuple(obj, dict)

Raises:
  • ValueError – If msg contains a header, but metadata is also provided as an argument.

  • TypeError – If msg is not bytes.

default_datatype = {'subtype': 'string', 'type': 'scalar'}
default_read_meth = 'read'
deserialize(msg, **kwargs)[source]

Deserialize a message.

Parameters:
  • msg (str, bytes) – Message to be deserialized.

  • **kwargs – Additional keyword arguments are passed to the decode method.

Returns:

Deserialized message and header information.

Return type:

tuple(obj, dict)

Raises:

TypeError – If msg is not bytes type (str on Python 2).

deserialize_file_header(fd)[source]

Deserialize the header information from the file and update the serializer.

Parameters:

fd (file) – File containing header.

classmethod dict2object(obj, **kwargs)[source]

Conver a dictionary to a message object.

Parameters:
  • obj (dict) – Dictionary to convert to serializable object.

  • **kwargs – Additional keyword arguments are ignored.

Returns:

Serializable object.

Return type:

object

disable_file_header()[source]

Set serializer attributes to disable file headers from being included in the serializations.

dump(fd, obj, **kwargs)[source]

Serialize to a file.

Parameters:
  • fd (str, file) – Filename or file-like object to load from.

  • **kwargs – Additional keyword arguments are passed to the created FileComm used for reading.

property empty_msg

Object indicating empty message.

Type:

obj

enable_file_header()[source]

Set serializer attributes to enable file headers to be included in the serializations.

encode(data, metadata, no_metadata=False, max_header_size=0)[source]

Encode the message with metadata in a header.

Parameters:
  • data (bytes) – Message data serialized into bytes.

  • metadata (dict) – Metadata that should be included in the message header.

  • no_metadata (bool, optional) – If True, no metadata will be added to the serialized message. Defaults to False.

  • max_header_size (int, optional) – Maximum size that header should occupy in order to be sent in a single message. A value of 0 indicates that any size header is valid. Defaults to 0.

Returns:

Encoded message with header.

Return type:

bytes

file_extensions = ['.txt']
func_deserialize(msg)[source]

Deserialize a message.

Parameters:

msg – Message to be deserialized.

Returns:

Deserialized message.

Return type:

obj

func_serialize(args)[source]

Serialize a message.

Parameters:

args – List of arguments to be formatted or numpy array to be serialized.

Returns:

Serialized message.

Return type:

bytes, str

get_field_names(as_bytes=False)[source]

Get the field names for an array of fields.

Parameters:

as_bytes (bool, optional) – If True, the field names will be returned as bytes. If False the field names will be returned as unicode. Defaults to False.

Returns:

Names for each field in the data type.

Return type:

list

get_field_units(as_bytes=False)[source]

Get the field units for an array of fields.

Parameters:

as_bytes (bool, optional) – If True, the field units will be returned as bytes. If False the field units will be returned as unicode. Defaults to False.

Returns:

Units for each field in the data type.

Return type:

list

get_status_message(nindent=0, extra_lines_before=None, extra_lines_after=None)[source]

Return lines composing a status message.

Parameters:
  • nindent (int, optional) – Number of tabs that should be used to indent each line. Defaults to 0.

  • extra_lines_before (list, optional) – Additional lines that should be added to the beginning of the default print message. Defaults to empty list if not provided.

  • extra_lines_after (list, optional) – Additional lines that should be added to the end of the default print message. Defaults to empty list if not provided.

Returns:

Lines composing the status message and the

prefix string used for the last message.

Return type:

tuple(list, prefix)

classmethod get_testing_options(table_example=False, array_columns=False, include_oldkws=False, table_string_type='bytes', no_names=False, no_units=False, **kwargs)[source]

Method to return a dictionary of testing options for this class.

Parameters:
  • table_example (bool, optional) – If True, the returned options will be for an array of elements representing a table-like structure. Defaults to False.

  • array_columns (bool, optional) – If True, table_example is set to True and the returned options will be for an array data type where each element is an array representing a column Defaults to False.

  • include_oldkws (bool, optional) – If True, old-style keywords will be added to the returned options. This will only have an effect if table_example is True. Defaults to False.

  • table_string_type (str, optional) – Type that should be used for the string column in the table. Defaults to ‘bytes’.

  • no_names (bool, optional) – If True, an example is returned where the names are not provided to the deserializer. Defaults to False.

  • no_units (bool, optional) – If True, units will not be added to the returned array if table_example is True.

Returns:

Dictionary of variables to use for testing. Key/value pairs:

  • kwargs (dict): Keyword arguments for comms tested with the provided content.

  • empty (object): Object produced from deserializing an empty message.

  • objects (list): List of objects to be serialized/deserialized. extra_kwargs (dict): Extra keyword arguments not used to construct type definition.

  • typedef (dict): Type definition resulting from the supplied kwargs.

  • dtype (np.dtype): Numpy data types that is consistent with the determined type definition.

  • contents (bytes): Concatenated serialization that will result from deserializing the serialized objects.

  • contents_recv (list): List of objects that would be deserialized from contents.

Return type:

dict

has_header = False
initialize_from_message(msg, serializer=None, **metadata)[source]

Initialize the serializer based on recieved message.

Parameters:
  • msg (object) – Message that serializer should be initialized from.

  • **kwargs – Additional keyword arguments are treated as metadata that may contain additional information for initializing the serializer.

initialize_from_metadata(metadata)[source]

Initialize a serializer based on received metadata. This method will exit early if the serializer has already been intialized.

Parameters:

metadata (dict) – Header information including type info that should be used to initialize the serializer class.

property initialized

True if the serializer has been initialized.

Type:

bool

property input_kwargs

Get the input keyword arguments used to create this class.

Type:

dict

is_framed = False
load(fd, **kwargs)[source]

Deserialize from a file.

Parameters:
  • fd (str, file) – Filename or file-like object to load from.

  • **kwargs – Additional keyword arguments are passed to the created FileComm used for reading.

Returns:

The deserialized data object or a list of

deserialized data objects if there is more than one.

Return type:

object

normalize(args)[source]

Normalize a message to conform to the expected datatype.

Parameters:

args (object) – Message arguments.

Returns:

Normalized message.

Return type:

object

property numpy_dtype

Corresponding structured data type. Will be None unless the type is an array of 1darrays.

Type:

np.dtype

classmethod object2array(obj, **kwargs)[source]

Convert a message object into an array.

Parameters:
  • obj (object) – Object that would be serialized by this class and should be returned in an array form.

  • **kwargs – Additional keyword arguments are ignored.

Returns:

Array version of the provided object or None if one cannot

be created.

Return type:

np.array

classmethod object2dict(obj, **kwargs)[source]

Convert a message object into a dictionary.

Parameters:
  • obj (object) – Object that would be serialized by this class and should be returned in a dictionary form.

  • **kwargs – Additional keyword arguments are ignored.

Returns:

Dictionary version of the provided object.

Return type:

dict

parse_header(msg)[source]

Extract header info from a message.

Parameters:

msg (str) – Message to extract header from.

Returns:

Message properties.

Return type:

dict

property read_meth

Method that should be used to read data for deserialization.

Type:

str

classmethod seri_kws()[source]

Get a list of valid keyword arguments.

serialize(args, metadata=None, add_serializer_info=False, no_metadata=False, max_header_size=0)[source]

Serialize a message.

Parameters:
  • args (obj) – List of arguments to be formatted or a ready made message.

  • metadata (dict, optional) – Keyword arguments that should be added to the header. Defaults to None and no header is added.

  • add_serializer_info (bool, optional) – If True, serializer information will be added to the metadata. Defaults to False.

  • no_metadata (bool, optional) – If True, no metadata will be added to the serialized message. Defaults to False.

  • max_header_size (int, optional) – Maximum size that header should occupy in order to be sent in a single message. A value of 0 indicates that any size header is valid. Defaults to 0.

Returns:

Serialized message.

Return type:

bytes, str

Raises:

TypeError – If returned msg is not bytes type (str on Python 2).

serialize_file_header()[source]

Return the serialized header information that should be prepended to files serialized using this class.

Returns:

Header string that should be written to the file.

Return type:

bytes

property serializer_info

Serializer info.

Type:

dict

property typedef

Alias for datatype.

Type:

dict

update_serializer(skip_type=False, seritype=None, datatype=None, from_message=False, **kwargs)[source]

Update serializer with provided information.

Parameters:
  • skip_type (bool, optional) – If True, everything is updated except the data type. Defaults to False.

  • **kwargs – Additional keyword arguments are processed as part of they type definition and are parsed for old-style keywords.

Raises:

RuntimeError – If there are keywords that are not valid datatype keywords (currect or old-style).

update_typedef_from_oldstyle(typedef)[source]

Update a given typedef using an old, table-style serialization spec. Existing typedef values are not overwritten and warnings are raised if the provided serialization spec is not compatible with the type definition.

Parameters:

typedef (dict) – Type definition to update.

Returns:

Updated typedef.

Return type:

dict

yggdrasil.serialize.YAMLSerialize module

class yggdrasil.serialize.YAMLSerialize.YAMLSerialize(*args, **kwargs)[source]

Bases: JSONSerialize

Class for serializing a python object into a bytes message using YAML.

Parameters:
  • indent (str, int, optional) – String or number of spaces that should be used to indent each level within the seiralized structure. Defaults to ‘t’.

  • encoding (str, optional) – Encoding that should be used to serialize the object. Defaults to ‘utf-8’.

  • default_flow_style (bool, optional) – If True, nested collections will be serialized in the block style. If False, they will always be serialized in the flow style. See PyYAML Documentation.

file_extensions = ['.yaml', '.yml']
func_deserialize(msg)[source]

Deserialize a message.

Parameters:

msg (str, bytes) – Message to be deserialized.

Returns:

Deserialized Python object.

Return type:

obj

func_serialize(args)[source]

Serialize a message.

Parameters:

args (obj) – Python object to be serialized.

Returns:

Serialized message.

Return type:

bytes, str

classmethod get_testing_options(**kwargs)[source]

Method to return a dictionary of testing options for this class.

Returns:

Dictionary of variables to use for testing.

Return type:

dict

yggdrasil.serialize.YAMLSerialize.decode_yaml(msg, sorted_dict_type=None, **kwargs)[source]

Decode a Python object from a YAML serialization.

Parameters:
  • msg (str) – YAML serialization to decode.

  • sorted_dict_type (type, optional) – Class that should be used to contain mapping objects while preserving order. Defaults to None and is ignored.

  • **kwargs – Additional keyword arguments are passed to yaml.load.

Returns:

Deserialized Python object.

Return type:

object

yggdrasil.serialize.YAMLSerialize.encode_yaml(obj, fd=None, indent=None, sorted_dict_type=None, sort_keys=True, **kwargs)[source]

Encode a Python object in YAML format.

Parameters:
  • obj (object) – Python object to encode.

  • fd (file, optional) – File descriptor for file that encoded object should be written to. Defaults to None and string is returned.

  • indent (int, str, optional) – Indentation for new lines in encoded string. Defaults to None.

  • sort_keys (bool, optional) – If True, dictionaries will be sorted alphabetically by key. Defaults to True.

  • **kwargs – Additional keyword arguments are passed to yaml.dump.

Returns:

Encoded object.

Return type:

str, bytes

Module contents