Example #1
0
def test_lowest_common_ancestor_(lca,
                                 height,
                                 leaf_a,
                                 leaf_b,
                                 test_instance,
                                 root=None):
  """Check the correctness of the lowest common ancestor and its height."""
  # First, check that it is a common ancestor of the longest paths.
  paths_a = imagenet_spec.get_upward_paths_from(leaf_a)
  longest_path_a = paths_a[np.argmax([len(p) for p in paths_a])]
  test_instance.assertIn(lca, longest_path_a)
  paths_b = imagenet_spec.get_upward_paths_from(leaf_b)
  longest_path_b = paths_b[np.argmax([len(p) for p in paths_b])]
  test_instance.assertIn(lca, longest_path_b)

  # Check that the LCA is not higher than the root.
  if root is not None:
    test_instance.assertFalse(imagenet_spec.is_descendent(root, lca))

  # Assert that there is no lower common ancestor than the given lca.
  for height_a, node in enumerate(longest_path_a):
    if node in longest_path_b:
      height_b = longest_path_b.index(node)
      node_height = max(height_a, height_b)
      if node == lca:
        test_instance.assertEqual(node_height, height)
      else:
        # It then must have greater height than the lca's height.
        test_instance.assertGreaterEqual(node_height, height)
Example #2
0
def validate_graph(graph_nodes, subset_synsets, test_instance):
  """Checks that the DAG structure is as expected."""
  # 1) Test that the leaves are all and only the ILSVRC 2012 synsets
  leaves = imagenet_spec.get_leaves(graph_nodes)
  test_instance.assertEqual(len(leaves), len(subset_synsets))
  test_instance.assertEqual(set(leaves), set(subset_synsets))

  # 2) Validate the connectivity
  # If a node is listed as a child of another, the latter must also be listed as
  # a parent of the former, and similarly if a node is listed as a parent of
  # another, the latter must also be listed as a child of the former.
  for n in graph_nodes:
    for c in n.children:
      test_instance.assertIn(n, c.parents)
    for p in n.parents:
      test_instance.assertIn(n, p.children)

  # 3) Check that no node has only 1 child, as it's not possible to create an
  # episode from such a node.
  for n in graph_nodes:
    test_instance.assertNotEqual(len(n.children), 1)

  # 4) Check that the graph is detached from the remaining non-graph synsets.
  # We want to guarantee that by following parent or child pointers of graph
  # nodes we will stay within the graph.
  for n in graph_nodes:
    for c in n.children:
      test_instance.assertIn(c, graph_nodes)
    for p in n.parents:
      test_instance.assertIn(p, graph_nodes)

  # 5) Check that every node in graph nodes is either an ILSVRC 2012 synset or
  # the ancestor of an ILSVRC 2012 synset
  for n in graph_nodes:
    if n in subset_synsets:
      continue
    has_2012_descendent = False
    for s in subset_synsets:
      has_2012_descendent = imagenet_spec.is_descendent(s, n)
      if has_2012_descendent:
        break
    test_instance.assertTrue(has_2012_descendent)