<commit_before>import numpy
import openravepy
import logging
from .exceptions import UnsupportedTypeSerializationException

TYPE_KEY = '__type__'

serialization_logger = logging.getLogger('prpy.serialization')
deserialization_logger = logging.getLogger('prpy.deserialization')

# Serialization.
def _serialize_internal(obj):
    from numpy import ndarray
    from openravepy import Environment, KinBody, Robot, Trajectory
    from prpy.tsr import TSR, TSRChain

    NoneType = type(None)

    if isinstance(obj, (int, float, basestring, NoneType)):
        return obj
    elif isinstance(obj, (list, tuple)):
        return [ serialize(x) for x in obj ]
    elif isinstance(obj, dict):
        return { serialize(k): serialize(v) for k, v in obj.iteritems() }
    elif isinstance(obj, ndarray):
        return { 'data': serialize(obj.tolist()) }
    elif isinstance(obj, Environment):
        return { 'data': serialize_environment(obj) }
    elif isinstance(obj, (KinBody, Robot)):
        return { 'name': obj.GetName() }
    elif isinstance(obj, (KinBody.Link, KinBody.Joint, Robot.Manipulator)):
        return {
            'name': obj.GetName(),
            'parent_name': obj.GetParent().GetName()
        }
    elif isinstance(obj, Trajectory):
        return { 'data': obj.serialize(0) }
    elif isinstance(obj, (TSR, TSRChain)):
        return { 'data': obj.serialize_dict() }
    else:
        raise UnsupportedTypeSerializationException(obj)

def serialize(obj):
    data = _serialize_internal(obj)
    if isinstance(data, dict):
        data[TYPE_KEY] = type(obj).__name__

    return data

def serialize_environment(env):
    return {
        'bodies': [ serialize_kinbody(body) for body in env.GetBodies() ],
    }

def serialize_environment_file(env, path, writer=None):
    if writer is None:
        import json
        writer = json.dump

    data = serialize_environment(env)

    if path is not None:
        with open(path, 'wb') as output_file:
            writer(data, output_file)
            serialization_logger.debug('Wrote environment to "%s".', path)

    return data

def serialize_kinbody(body):
    all_joints = []
    all_joints.extend(body.GetJoints())
    all_joints.extend(body.GetPassiveJoints())

    data = {
        'is_robot': body.IsRobot(),<commit_msg>Fixed serialization with custom subclasses.<commit_after>import numpy
import openravepy
import logging
from .exceptions import UnsupportedTypeSerializationException

TYPE_KEY = '__type__'

serialization_logger = logging.getLogger('prpy.serialization')
deserialization_logger = logging.getLogger('prpy.deserialization')

# Serialization.
def serialize(obj):
    from numpy import ndarray
    from openravepy import Environment, KinBody, Robot, Trajectory
    from prpy.tsr import TSR, TSRChain

    NoneType = type(None)

    if isinstance(obj, (int, float, basestring, NoneType)):
        return obj
    elif isinstance(obj, (list, tuple)):
        return [ serialize(x) for x in obj ]
    elif isinstance(obj, dict):
        obj = { serialize(k): serialize(v) for k, v in obj.iteritems() }
        obj[TYPE_KEY] = dict.__name__
        return obj
    elif isinstance(obj, ndarray):
        return {
            TYPE_KEY: ndarray.__name__,
            'data': serialize(obj.tolist())
        }
    elif isinstance(obj, Environment):
        return {
            TYPE_KEY: Environment.__name__,
            'data': serialize_environment(obj)
        }
    elif isinstance(obj, KinBody):
        return {
            TYPE_KEY: KinBody.__name__,
            'name': obj.GetName()
        }
    elif isinstance(obj, Robot):
        return {
            TYPE_KEY: Robot.__name__,
            'name': obj.GetName()
        }
    elif isinstance(obj, KinBody.Link):
        return {
            TYPE_KEY: KinBody.Link.__name__,
            'name': obj.GetName(),
            'parent_name': obj.GetParent().GetName()
        }
    elif isinstance(obj, KinBody.Joint):
        return {
            TYPE_KEY: KinBody.Joint.__name__,
            'name': obj.GetName(),
            'parent_name': obj.GetParent().GetName()
        }
    elif isinstance(obj, Robot.Manipulator):
        return {
            TYPE_KEY: KinBody.Manipulator.__name__,
            'name': obj.GetName(),
            'parent_name': obj.GetParent().GetName()
        }
    elif isinstance(obj, Trajectory):
        return {
            TYPE_KEY: Trajectory.__name__,
            'data': obj.serialize(0)
        }
    elif isinstance(obj, TSR):
        return {
            TYPE_KEY: TSR.__name__,
            'data': obj.serialize_dict()
        }
    elif isinstance(obj, TSRChain):
        return {
            TYPE_KEY: TSRChain.__name__,
            'data': obj.serialize_dict()
        }
    else:
        raise UnsupportedTypeSerializationException(obj)

def serialize_environment(env):
    return {
        'bodies': [ serialize_kinbody(body) for body in env.GetBodies() ],
    }

def serialize_environment_file(env, path, writer=None):
    if writer is None:
        import json
        writer = json.dump

    data = serialize_environment(env)

    if path is not None:
        with open(path, 'wb') as output_file:
            writer(data, output_file)
            serialization_logger.debug('Wrote environment to "%s".', path)

    return data

def serialize_kinbody(body):
    all_joints = []
    all_joints.extend(body.GetJoints())
    all_joints.extend(body.GetPassiveJoints())

    data = {
        'is_robot': body.IsRobot(),
{'fraction_non_alphanumeric': 0.10884971833545339, 'fraction_numerical': 0.00036343812465927677, 'mean_word_length': 2.6354029062087188, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7189132', 'n_tokens_mistral': 1648, 'n_tokens_neox': 1625, 'n_words': 384}