def _assert_same_min_diff_structure()

in tensorflow_model_remediation/min_diff/keras/utils/structure_utils.py [0:0]


def _assert_same_min_diff_structure(struct1, struct2):
  # pyformat: disable
  """Asserts that the two MinDiff structures are the same.

  Arguments:
    struct1: First MinDiff structure. Must be a single element (including a
      tuple) or an unnested dict.
    struct2: Second MinDiff structure. Must be a single element (including a
      tuple) or an unnested dict.

  Has similar behavior to `tf.nest.assert_same_structure` with the exception
  that tuples will be considered as single elements and not examined further.
  See `tf.nest.assert_same_structure` documentation for additional details on
  behavior.

  Raises:
    ValueError: If either `struct1` or `struct2` is invalid (see
      `validate_min_diff_structure` for details).
    ValueError: If `struct1` and `struct2` do not have the same structure.
  """
  # pyformat: enable

  # validate structures.
  validate_min_diff_structure(struct1)
  validate_min_diff_structure(struct2)

  def _err_msg(specifics_template, use_dict_keys=False):
    struct1_str = "type={} str={}".format(type(struct1), struct1)
    struct2_str = "type={} str={}".format(type(struct2), struct2)

    err_msg = ("The two structures don't have the same structure:\n\n"
               "First structure: {}\n\n"
               "Second structure: {}\n\n".format(struct1_str, struct2_str))
    err_msg += "More Specifically: "
    if use_dict_keys:
      err_msg += specifics_template.format(
          list(struct1.keys()), list(struct2.keys()))
    else:
      err_msg += specifics_template.format(struct1_str, struct2_str)
    return err_msg

  # Assert same structure if they are a dict.
  if isinstance(struct1, dict):
    if not isinstance(struct2, dict):
      raise ValueError(
          _err_msg("Substructure \"{}\" is a dict, while "
                   "substructure \"{}\" is not."))
    if set(struct1.keys()) != set(struct2.keys()):
      raise ValueError(
          _err_msg(
              "The two dictionaries don't have the same set of keys. First "
              "structure has keys: {}, while second structure has keys: {}.",
              use_dict_keys=True))
    return  # Dicts match.

  # If struct1 is not a nested structure, struct2 should not be either.
  if isinstance(struct2, dict):
    raise ValueError(
        _err_msg("Substructure \"{1}\" is a dict, while "
                 "substructure \"{0}\" is not."))