예제 #1
0
def test_AddShortestPath_min_length_too_large():
  """Test that error raised if min_length too large."""
  g = nx.Graph()
  g.add_edge('A', 'B')
  with pytest.raises(ValueError) as e_ctx:
    graph_util.AddShortestPath(np.random.RandomState(seed=1), g, min_length=5)
  assert str(e_ctx.value) == "All shortest paths are below the minimum length"
예제 #2
0
def test_AddShortestPath_num_edges():
  """Test that result graph has same number of edges."""
  g = nx.Graph()
  g.add_edge('A', 'B')
  g.add_edge('B', 'C')
  g.add_edge('C', 'D')
  digraph = graph_util.AddShortestPath(np.random.RandomState(seed=1), g)
  assert digraph.number_of_edges() == 6  # 3 undirected edges * 2
예제 #3
0
def test_AddShortestPath_result_is_digraph():
  """Test that result is a directed graph."""
  g = nx.Graph()
  g.add_edge('A', 'B')
  g.add_edge('B', 'C')
  g.add_edge('C', 'D')
  digraph = graph_util.AddShortestPath(np.random.RandomState(seed=1), g)
  assert isinstance(digraph, nx.DiGraph)
예제 #4
0
def test_AddShortestPath_num_nodes():
  """Test that result graph has same number of nodes."""
  g = nx.Graph()
  g.add_edge("A", "B")
  g.add_edge("B", "C")
  g.add_edge("C", "D")
  digraph = graph_util.AddShortestPath(np.random.RandomState(seed=1), g)
  assert digraph.number_of_nodes() == 4
예제 #5
0
def test_AddShortestPath_result_is_digraph():
  """Test that result is a directed graph."""
  g = nx.Graph()
  g.add_edge("A", "B")
  g.add_edge("B", "C")
  g.add_edge("C", "D")
  digraph = graph_util.AddShortestPath(np.random.RandomState(seed=1), g)
  assert isinstance(digraph, nx.DiGraph)
예제 #6
0
def test_GraphToInputTarget_graph_features_shape():
  """Test number of features graphs."""
  digraph = graph_util.AddShortestPath(
      np.random.RandomState(seed=1),
      graph_util.GenerateGraph(np.random.RandomState(seed=1), [10, 15]))
  igraph, tgraph = graph_util.GraphToInputTarget(digraph)

  assert igraph.graph['features'].shape == (1,)
  assert tgraph.graph['features'].shape == (1,)
예제 #7
0
def test_GraphToInputTarget_number_of_edges():
  """Test number of edges in feature graphs."""
  digraph = graph_util.AddShortestPath(
      np.random.RandomState(seed=1),
      graph_util.GenerateGraph(np.random.RandomState(seed=1), [10, 15]))
  igraph, tgraph = graph_util.GraphToInputTarget(digraph)

  assert igraph.number_of_edges() == digraph.number_of_edges()
  assert tgraph.number_of_edges() == digraph.number_of_edges()
예제 #8
0
def test_GraphToInputTarget_edge_features_shape():
  """Test number of features in edges."""
  digraph = graph_util.AddShortestPath(
      np.random.RandomState(seed=1),
      graph_util.GenerateGraph(np.random.RandomState(seed=1), [10, 15]))
  igraph, tgraph = graph_util.GraphToInputTarget(digraph)

  for from_idx, to_idx in igraph.edges:
    assert igraph.edges[from_idx, to_idx]['features'].shape == (1,)
    assert tgraph.edges[from_idx, to_idx]['features'].shape == (2,)
예제 #9
0
def test_GraphToInputTarget_node_features_shape():
  """Test number of features in nodes."""
  digraph = graph_util.AddShortestPath(
    np.random.RandomState(seed=1),
    graph_util.GenerateGraph(np.random.RandomState(seed=1), [10, 15]),
  )
  igraph, tgraph = graph_util.GraphToInputTarget(digraph)

  for node_idx in igraph.nodes:
    assert igraph.node[node_idx]["features"].shape == (5,)
    assert tgraph.node[node_idx]["features"].shape == (2,)
예제 #10
0
def test_AddShortestPath_simple_graph_path():
  """Test result path for a simple graph."""
  # Graph:
  #     A -- B -- C -- D -- E -- F
  #                 \_ G
  g = nx.Graph()
  g.add_edge("A", "B")
  g.add_edge("B", "C")
  g.add_edge("C", "D")
  g.add_edge("C", "G")
  g.add_edge("D", "E")
  g.add_edge("E", "F")
  # Use min_length 3 so that the path is A -> B -> C -> D
  digraph = graph_util.AddShortestPath(
    np.random.RandomState(seed=1), g, min_length=5
  )
  assert digraph.node["A"]["start"]
  assert digraph.node["A"]["solution"]
  assert not digraph.node["A"]["end"]

  assert not digraph.node["B"]["start"]
  assert digraph.node["B"]["solution"]
  assert not digraph.node["B"]["end"]

  assert not digraph.node["C"]["start"]
  assert digraph.node["C"]["solution"]
  assert not digraph.node["C"]["end"]

  assert not digraph.node["D"]["start"]
  assert digraph.node["D"]["solution"]
  assert not digraph.node["D"]["end"]

  assert not digraph.node["E"]["start"]
  assert digraph.node["E"]["solution"]
  assert not digraph.node["E"]["end"]

  assert not digraph.node["F"]["start"]
  assert digraph.node["F"]["solution"]
  assert digraph.node["F"]["end"]

  assert not digraph.node["G"]["start"]
  assert not digraph.node["G"]["solution"]
  assert not digraph.node["G"]["end"]
예제 #11
0
def test_AddShortestPath_simple_graph_path():
  """Test result path for a simple graph."""
  # Graph:
  #     A -- B -- C -- D -- E -- F
  #                 \_ G
  g = nx.Graph()
  g.add_edge('A', 'B')
  g.add_edge('B', 'C')
  g.add_edge('C', 'D')
  g.add_edge('C', 'G')
  g.add_edge('D', 'E')
  g.add_edge('E', 'F')
  # Use min_length 3 so that the path is A -> B -> C -> D
  digraph = graph_util.AddShortestPath(np.random.RandomState(seed=1), g,
                                       min_length=5)
  assert digraph.node['A']['start']
  assert digraph.node['A']['solution']
  assert not digraph.node['A']['end']

  assert not digraph.node['B']['start']
  assert digraph.node['B']['solution']
  assert not digraph.node['B']['end']

  assert not digraph.node['C']['start']
  assert digraph.node['C']['solution']
  assert not digraph.node['C']['end']

  assert not digraph.node['D']['start']
  assert digraph.node['D']['solution']
  assert not digraph.node['D']['end']

  assert not digraph.node['E']['start']
  assert digraph.node['E']['solution']
  assert not digraph.node['E']['end']

  assert not digraph.node['F']['start']
  assert digraph.node['F']['solution']
  assert digraph.node['F']['end']

  assert not digraph.node['G']['start']
  assert not digraph.node['G']['solution']
  assert not digraph.node['G']['end']
예제 #12
0
def test_AddShortestPath_empty_graph():
  """Test that error raised if graph is empty."""
  g = nx.Graph()
  with pytest.raises(ValueError) as e_ctx:
    graph_util.AddShortestPath(np.random.RandomState(seed=1), g)
  assert str(e_ctx.value) == "All shortest paths are below the minimum length"