import six


def canonicalize_convexity(convexity):
  """Converts string constants representing convexity into integers.

  Args:
    convexity: The convexity hyperparameter of `tfl.layers.PWLCalibration`
      layer.

  Returns:
    convexity represented as -1, 0, 1, or None.

  Raises:
    ValueError: If convexity is not in the set
      {-1, 0, 1, 'concave', 'none', 'convex'}.
  """
  if convexity is None:
    return None

  if convexity in [-1, 0, 1]:
    return convexity
  elif isinstance(convexity, six.string_types):
    if convexity.lower() == "concave":
      return -1
    if convexity.lower() == "none":
      return 0
    if convexity.lower() == "convex":
      return 1
  raise ValueError("'convexity' must be from: [-1, 0, 1, 'concave', "
                   "'none', 'convex']. Given: {}".format(convexity))