<jupyter_start><jupyter_text>"Writing Networkx Functions"
> "Experimenting with networkx and unit tests in notebooks."

- toc:false
- branch: master
- badges: true
- comments: true
- author: JC
- categories: [networkx, graphs, unit testing]<jupyter_code>import sys
!{sys.executable} -m pip install networkx
import networkx as nx
import unittest as ut
def n_distance_neighbors(G, start_node, distance):
  """
  This function takes in a graph, starting node, and a distance. It will traverse
  the graph from the starting node and find all the neighbors within the distance
  where distance between two vertices in a graph is the number of edges in a 
  shortest path.

  :param G: A networkx graph.
  :param start_node: The starting point. Distance from this node. Must be in G.
  :param distance: The distance to find neighbors within.
  :return: A list of vertices within specified distance.
  :rtype: List
  """
  neighbors = []
  nodes = nx.single_source_shortest_path_length(G,start_node,distance)
  for x in nodes:
    if nodes[x] <= distance:
      neighbors.append(x)
  return neighbors

class TestNeighbors(ut.TestCase):
  
  def test_distance_two_simple_graph(self):
    G = nx.Graph()
    G.add_edges_from([(1, 2), (1, 3),(1,4),(2,5),(5,6),(6,7)])
    result = n_distance_neighbors(G, 1, 2)
    answer = [1,2,3,4,5]
    self.assertEqual(result, answer)

  def test_distance_three_simple_graph(self):
    G = nx.Graph()
    G.add_edges_from([(1, 2), (1, 3),(1,4),(2,5),(5,6),(6,7)])
    result = n_distance_neighbors(G, 1, 3)
    answer = [1,2,3,4,5,6]
    self.assertEqual(result, answer)

  def test_distance_three_simple_graph(self):
    G = nx.Graph()
    G.add_edges_from([(1, 2), (1, 3),(1,4),(2,5),(5,6),(6,7)])
    result = n_distance_neighbors(G, 1, 4)
    answer = [1,2,3,4,5,6,7]
    self.assertEqual(result, answer)

  def test_distance_three_simple_graph(self):
    G = nx.Graph()
    G.add_edges_from([(1, 2), (1, 3),(1,4),(2,5),(5,6),(6,7)])
    result = n_distance_neighbors(G, 1, 4)
    answer = [1,2,3,4,5,6,7]
    self.assertEqual(result, answer)

  def test_distance_two_complex_graph(self):
    G = nx.Graph()
    G.add_edges_from([(1, 2), (1, 3),(1,9), (2,3), (2,4), (2,5), (2,8), (3,6), 
                      (4,5), (4,12), (4,13),(6,7), (7,8), (9,10), (10,11), (11,12),
                      (13,14), (14,15), (14,17),(15,16) ])
    result = n_distance_neighbors(G, 1, 2)
    result.sort()
    answer = [1,2,3,4,5,6,8,9, 10]
    self.assertEqual(result, answer)

  def test_distance_three_complex_graph(self):
    G = nx.Graph()
    G.add_edges_from([(1, 2), (1, 3),(1,9), (2,3), (2,4), (2,5), (2,8), (3,6), 
                      (4,5), (4,12), (4,13),(6,7), (7,8), (9,10), (10,11), (11,12),
                      (13,14), (14,15), (14,17),(15,16) ])
    result = n_distance_neighbors(G, 1, 3)
    result.sort()
    answer = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
    self.assertEqual(result, answer)

  def test_distance_four_complex_graph(self):
    G = nx.Graph()
    G.add_edges_from([(1, 2), (1, 3),(1,9), (2,3), (2,4), (2,5), (2,8), (3,6), 
                      (4,5), (4,12), (4,13),(6,7), (7,8), (9,10), (10,11), (11,12),
                      (13,14), (14,15), (14,17),(15,16) ])
    result = n_distance_neighbors(G, 1, 4)
    result.sort()
    answer = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,14]
    self.assertEqual(result, answer)

  def test_distance_five_complex_graph(self):
    G = nx.Graph()
    G.add_edges_from([(1, 2), (1, 3),(1,9), (2,3), (2,4), (2,5), (2,8), (3,6), 
                      (4,5), (4,12), (4,13),(6,7), (7,8), (9,10), (10,11), (11,12),
                      (13,14), (14,15), (14,17),(15,16) ])
    result = n_distance_neighbors(G, 1, 5)
    result.sort()
    answer = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 17]
    self.assertEqual(result, answer)

  def test_distance_six_complex_graph(self):
    G = nx.Graph()
    G.add_edges_from([(1, 2), (1, 3),(1,9), (2,3), (2,4), (2,5), (2,8), (3,6), 
                      (4,5), (4,12), (4,13),(6,7), (7,8), (9,10), (10,11), (11,12),
                      (13,14), (14,15), (14,17),(15,16) ])
    result = n_distance_neighbors(G, 1, 6)
    result.sort()
    answer = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17]
    self.assertEqual(result, answer)

ut.main(argv=[''], exit=False)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.20223964165733482, 'fraction_numerical': 0.10302351623740201, 'mean_word_length': 2.9910634495084896, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '84455', 'license': 'Apache-2.0', 'n_tokens_mistral': 2097, 'n_tokens_neox': 1817, 'n_words': 421, 'path': '_notebooks/2021-02-17-Networkx.ipynb', 'repo_name': 'jdclifton2/blog', 'size': '4465'}