import six


def canonicalize_unimodalities(unimodalities):
  """Converts string constants representing unimodalities into integers.

  Args:
    unimodalities: unimodalities hyperparameter of `tfl.layers.Lattice` layer.

  Returns:
    A list of unimodalities represented as -1, 0, 1, or the value None if
    unimodalities is None.

  Raises:
    ValueError: If one of unimodalities is not in the set
      {-1, 0, 1, 'peak', 'none', 'valley'}.
  """
  if not unimodalities:
    return None
  canonicalized = []
  for unimodality in unimodalities:
    if unimodality in [-1, 0, 1]:
      canonicalized.append(unimodality)
    elif isinstance(unimodality,
                    six.string_types) and unimodality.lower() == "peak":
      canonicalized.append(-1)
    elif isinstance(unimodality,
                    six.string_types) and unimodality.lower() == "none":
      canonicalized.append(0)
    elif isinstance(unimodality,
                    six.string_types) and unimodality.lower() == "valley":
      canonicalized.append(1)
    else:
      raise ValueError(
          "'unimodalities' elements must be from: [-1, 0, 1, 'peak', 'none', "
          "'valley']. Given: {}".format(unimodalities))
  return canonicalized