Ejemplo n.º 1
0
def deserialize(obj, as_class: SerializerType):
    """Convert python dict into given class

    :param obj: dict (or list or any primitive) to deserialize
    :param as_class: type or serializer

    :return: deserialized instance of as_class (or real_type of serializer)

    :raise: DeserializationError
    """
    if as_class is Any:
        return obj
    elif is_generic(as_class):
        if is_mapping(as_class):
            key_type, value_type = get_mapping_types(as_class)
            if key_type not in SERIALIZABLE_DICT_TYPES:
                raise DeserializationError(
                    f'mapping key type must be one of {SERIALIZABLE_DICT_TYPES}, not {key_type}. '
                    f'error deserializing {obj}')
            return {
                key_type(k): deserialize(v, value_type)
                for k, v in obj.items()
            }
        elif is_tuple(as_class):
            var_length, types = get_tuple_internal_types(as_class)
            if var_length:
                return tuple(deserialize(o, types) for o in obj)
            else:
                return tuple(deserialize(o, t) for o, t in zip(obj, types))
        elif is_collection(as_class):
            seq_int_type = get_collection_internal_type(as_class)
            seq_type = get_collection_type(as_class)
            return seq_type([deserialize(o, seq_int_type) for o in obj])
    elif isinstance(as_class, Hashable) and as_class in BUILTIN_TYPES:
        return obj
    else:
        if not is_union(as_class):
            if type_field_position_is(
                    as_class,
                    Position.INSIDE) and (is_hierarchy_root(as_class)
                                          or has_subtype_alias(as_class, obj)):
                as_class = resolve_subtype(as_class, obj)
            return _construct_object(obj, as_class)
        else:
            for possible_type in union_args(as_class):
                try:
                    return deserialize(obj, possible_type)
                except TypeError:
                    pass
            else:
                raise DeserializationError(
                    "Cannot construct type {} from argument list {}".format(
                        as_class, obj))
Ejemplo n.º 2
0
 def deserialize(self, obj: dict) -> lgb.Dataset:
     v = self.inner.deserialize(obj)
     try:
         return lgb.Dataset(v, free_raw_data=False)
     except ValueError:
         raise DeserializationError(
             f'object: {obj} could not be converted to lightgbm dataset')
Ejemplo n.º 3
0
 def deserialize(self, obj):
     try:
         return self.to_type(obj)
     except (ValueError, TypeError):
         raise DeserializationError(
             f'given object: {obj} could not be converted to {self.to_type}'
         )
Ejemplo n.º 4
0
 def deserialize(self, obj: list) -> xgboost.DMatrix:
     try:
         return xgboost.DMatrix(obj)
     except (ValueError, TypeError):
         raise DeserializationError(
             f'given object: {obj} could not be converted to xgboost matrix'
         )
Ejemplo n.º 5
0
 def deserialize(self, obj):
     try:
         ret = np.array(obj, dtype=np_type_from_string(self.dtype))
     except (ValueError, TypeError):
         raise DeserializationError(f'given object: {obj} could not be converted to array '
                                    f'of type: {np_type_from_string(self.dtype)}')
     self._check_shape(ret, DeserializationError)
     return ret
Ejemplo n.º 6
0
 def deserialize(self, obj):
     self._check_type(obj, dict, DeserializationError)
     try:
         ret = pd.DataFrame.from_records(obj['values'])
     except (ValueError, KeyError):
         raise DeserializationError(f'given object: {obj} could not be converted to dataframe')
     self._check_columns(ret, DeserializationError)
     return ret
Ejemplo n.º 7
0
 def deserialize(self, obj):
     try:
         ret = torch.tensor(obj, dtype=getattr(torch, self.dtype))
     except (ValueError, TypeError):
         raise DeserializationError(
             f'given object: {obj} could not be converted to tensor '
             f'of type: {getattr(torch, self.dtype)}')
     self._check_shape(ret, DeserializationError)
     return ret
Ejemplo n.º 8
0
    def deserialize(self, obj):
        self._check_type(obj, dict, DeserializationError)
        try:
            ret = pd.DataFrame.from_records(obj['values'])
        except (ValueError, KeyError):
            raise DeserializationError(f'given object: {obj} could not be converted to dataframe')

        self._validate_columns(ret, DeserializationError)  # including index columns
        ret = self.align_types(ret)  # including index columns
        self._validate_dtypes(ret, DeserializationError)
        return self.align_index(ret)
Ejemplo n.º 9
0
 def deserialize(self, obj):
     self._check_type(obj, dict, DeserializationError)
     try:
         ret = pd.DataFrame.from_records(obj['values'])
     except (ValueError, KeyError):
         raise DeserializationError(f'given object: {obj} could not be converted to dataframe')
     self._validate_columns(ret, DeserializationError)
     ret = ret[self.columns]
     for col, expected, dtype in zip(self.columns, self.actual_dtypes, ret.dtypes):
         if expected != dtype:
             ret[col] = ret[col].astype(expected)
     self._validate_dtypes(ret, DeserializationError)
     return ret
Ejemplo n.º 10
0
def resolve_subtype(cls: type, obj):
    # obj must be parent object if position == OUTSIDE
    type_alias = get_subtype_alias(cls, obj)
    if '.' in type_alias:
        try:
            split = type_alias.split('.')
            module = '.'.join(split[:-1])
            import_module(module)
        except ImportError:
            pass
    subtype = cls._subtypes.get(type_alias, None)
    if subtype is None:
        raise DeserializationError(
            f'Unknown subtype {type_alias} of type {cls.__name__}')
    return subtype