Exemple #1
0
def _GetAllowedDefaultValue(
    value_type: pa.DataType,
    default_value_proto: schema_pb2.TensorRepresentation.DefaultValue
) -> Union[int, float, bytes]:
  """Returns the default value set in DefaultValue proto or raises."""
  kind = default_value_proto.WhichOneof("kind")
  if kind in ("int_value", "uint_value") and pa.types.is_integer(value_type):
    value = getattr(default_value_proto, kind)
    iinfo = np.iinfo(value_type.to_pandas_dtype())
    if value <= iinfo.max and value >= iinfo.min:
      return value
    else:
      raise ValueError("Integer default value out of range: {} is set for a "
                       "{} column".format(value, value_type))
  elif kind == "float_value" and pa.types.is_floating(value_type):
    return default_value_proto.float_value
  elif kind == "bytes_value" and _IsBinaryLike(value_type):
    return default_value_proto.bytes_value

  raise ValueError(
      "Incompatible default value: {} is set for a {} column".format(
          kind, value_type))
Exemple #2
0
def _GetDefaultValuesList(
    unbatched_shape: List[int], feature_type: schema_pb2.FeatureType,
    default_value_proto: schema_pb2.TensorRepresentation.DefaultValue
) -> List[Union[int, float, bytes]]:
    """Returns a List filled with the default value given in the proto.

  Args:
    unbatched_shape: The shape of the tensor to fill.
    feature_type: The expected type of the default_value.
    default_value_proto: The DefaultValue proto that holds the default_value.

  Raises:
    ValueError: if the default_value is incompatible with feature_type.
  """
    kind = default_value_proto.WhichOneof("kind")
    default_value = getattr(default_value_proto, kind)
    expected_feature_type = _DEFAULT_VALUE_KIND_TO_FEATURE_TYPE.get(kind, None)
    if feature_type != expected_feature_type:
        raise ValueError(
            "FeatureType: {} is incompatible with default_value: {}".format(
                schema_pb2.FeatureType.Name(feature_type), default_value))
    size = int(np.prod(unbatched_shape, initial=1))

    return [default_value] * size