def coerce_type(cls, obj, typedef=None, key_order=None, dont_wrap_single=False, **kwargs): r"""Coerce objects of specific types to match the data type. Args: obj (object): Object to be coerced. typedef (dict, optional): Type defintion that object should be coerced to. Defaults to None. key_order (list, optional): Order that keys from a dictionary should be used to compose an array. Defaults to None. dont_wrap_single (bool, optional): If True, single element data types will not attempt to wrap input object in additional list. Defaults to False. **kwargs: Additional keyword arguments are metadata entries that may aid in coercing the type. Returns: object: Coerced object. Raises: RuntimeError: If obj is a dictionary, but key_order is not provided. """ from yggdrasil.serialize import pandas2list, numpy2list, dict2list if isinstance(obj, pd.DataFrame): obj = pandas2list(obj) elif isinstance(obj, np.ndarray) and (len(obj.dtype) == 0): obj = [obj] elif isinstance(obj, np.ndarray) and (len(obj.dtype) > 0): obj = numpy2list(obj) elif isinstance(obj, dict): if (key_order is not None) or (len(obj) == 1): obj = dict2list(obj, order=key_order) elif (isinstance(typedef, dict) and isinstance(typedef.get('items', None), list) and all([('title' in x) for x in typedef['items']])): key_order = [x['title'] for x in typedef['items']] obj = dict2list(obj, order=key_order) else: obj = [obj] elif ((isinstance(typedef, dict) and (not dont_wrap_single) and (len(typedef.get('items', [])) == 1))): typedef_validated = kwargs.get('typedef_validated', False) try_obj = cls.coerce_type([obj], typedef=typedef, key_order=key_order, dont_wrap_single=True, **kwargs) if cls.check_decoded(try_obj, typedef, typedef_validated=typedef_validated): obj = try_obj return super(JSONArrayMetaschemaType, cls).coerce_type(obj, typedef=typedef, **kwargs)
def test_dict2list(): r"""Test conversion of a dictionary to a list and back.""" with pytest.raises(TypeError): serialize.dict2list(None) with pytest.raises(TypeError): serialize.list2dict(None) x = {'c': 0, 'b': 1, 'a': 2} y = [2, 1, 0] assert (serialize.dict2list(x) == y)
def test_coerce(): r"""Test serialization of coerced types.""" typedef = { 'type': 'object', 'properties': { 'a': { 'type': '1darray', 'subtype': 'float', 'title': 'a', 'precision': 64 } } } x = JSONObjectMetaschemaType(**typedef) key_order = ['a'] msg_recv = {'a': np.zeros(3, 'float64')} msg_send_list = [ serialize.dict2numpy(msg_recv, order=key_order), serialize.dict2pandas(msg_recv, order=key_order), serialize.dict2list(msg_recv, order=key_order) ] def do_send_recv(msg_send): msg_seri = x.serialize(msg_send, tyepdef=typedef, key_order=key_order) assert_equal(x.deserialize(msg_seri)[0], msg_recv) for y in msg_send_list: do_send_recv(y)
def test_coerce(nested_approx): r"""Test serialization of coerced types.""" from yggdrasil.metaschema.datatypes.JSONObjectMetaschemaType import ( JSONObjectMetaschemaType) from yggdrasil import serialize typedef = { 'type': 'object', 'properties': { 'a': { 'type': '1darray', 'subtype': 'float', 'title': 'a', 'precision': 64 } } } x = JSONObjectMetaschemaType(**typedef) key_order = ['a'] msg_recv = {'a': np.zeros(3, 'float64')} msg_send_list = [{ 'a': np.zeros(3, 'float32') }, serialize.dict2numpy(msg_recv, order=key_order), serialize.dict2pandas(msg_recv, order=key_order), serialize.dict2list(msg_recv, order=key_order)] def do_send_recv(msg_send): msg_seri = x.serialize(msg_send, tyepdef=typedef, key_order=key_order) assert (x.deserialize(msg_seri)[0] == nested_approx(msg_recv)) for y in msg_send_list: do_send_recv(y)
def coerce_type(cls, obj, typedef=None, key_order=None, **kwargs): r"""Coerce objects of specific types to match the data type. Args: obj (object): Object to be coerced. typedef (dict, optional): Type defintion that object should be coerced to. Defaults to None. key_order (list, optional): Order that keys from a dictionary should be used to compose an array. Defaults to None. **kwargs: Additional keyword arguments are metadata entries that may aid in coercing the type. Returns: object: Coerced object. Raises: RuntimeError: If obj is a dictionary, but key_order is not provided. """ from yggdrasil.serialize import pandas2list, numpy2list, dict2list if isinstance(obj, pd.DataFrame): obj = pandas2list(obj) elif isinstance(obj, np.ndarray) and (len(obj.dtype) > 0): obj = numpy2list(obj) elif isinstance(obj, dict): if (key_order is not None) or (len(obj) == 1): obj = dict2list(obj, order=key_order) else: obj = [obj] elif isinstance(typedef, dict) and (len(typedef.get('items', [])) == 1): typedef_validated = kwargs.get('typedef_validated', False) if cls.check_decoded([obj], typedef, typedef_validated=typedef_validated): obj = [obj] return obj
def evaluate_transform(self, x, no_copy=False): r"""Call transform on the provided message. Args: x (object): Message object to transform. no_copy (bool, optional): If True, the transformation occurs in place. Otherwise a copy is created and transformed. Defaults to False. Returns: object: The transformed message. """ out = x np_dtype = type2numpy(self.transformed_datatype) if isinstance(x, pandas.DataFrame): out = pandas2numpy(x).astype(np_dtype, copy=True) elif isinstance(x, np.ndarray): out = x.astype(np_dtype, copy=True) elif np_dtype and isinstance(x, (list, tuple, dict, np.ndarray)): if len(x) == 0: out = np.zeros(0, np_dtype) else: if isinstance(x, dict): x = dict2list(x, order=np_dtype.names) out = consolidate_array(x, dtype=np_dtype) else: # warning? raise TypeError(("Cannot consolidate object of type %s " "into a structured numpy array.") % type(x)) if not no_copy: out = copy.deepcopy(out) return out
def test_dict2list(): r"""Test conversion of a dictionary to a list and back.""" assert_raises(TypeError, serialize.dict2list, None) assert_raises(TypeError, serialize.list2dict, None) x = {'c': 0, 'b': 1, 'a': 2} y = [2, 1, 0] assert_equal(serialize.dict2list(x), y)