import six


def canonicalize_monotonicity(monotonicity):
  """Converts string constants representing monotonicity into integers.

  Args:
    monotonicity: monotonicity hyperparameter of `PWLCalibration` layer.

  Raises:
    ValueError if monotonicity is invalid.

  Returns:
    monotonicity represented as -1, 0 or 1.
  """
  if monotonicity is None:
    return None

  if monotonicity in [-1, 0, 1]:
    return monotonicity
  elif isinstance(monotonicity, six.string_types):
    if monotonicity.lower() == "decreasing":
      return -1
    if monotonicity.lower() == "none":
      return 0
    if monotonicity.lower() == "increasing":
      return 1
  raise ValueError("'monotonicities' must be from: [-1, 0, 1, 'decreasing', "
                   "'none', 'increasing']. Given: %s" % monotonicity)