Example #1
0
    def test_has_edge_undirected(self):
        """Tests the has_edge method for an undirected graph.
        """
        undirected = Graph(graph = { 'A' : ['B'], 'B' : ['A'] })

        self.assertTrue(undirected.has_edge('A', 'B'), 'There is an edge between A and B.')
        self.assertTrue(undirected.has_edge('B', 'A'), 'There is an edge between B and A.')
Example #2
0
def task_3(sequence_length, phase_filtering):
    polyharmonic_sequence = generate_polyharmonic_sequence(sequence_length)
    direct_transformer = DirectFourierTransformer(polyharmonic_sequence)
    amplitude_spectrum = direct_transformer.get_amplitude_spectrum()
    phase_spectrum = [
        x if amplitude_spectrum[i] > phase_filtering else 0
        for i, x in enumerate(direct_transformer.get_phase_spectrum())
    ]
    restored_sequence = InverseFourierTransformer(
        amplitude_spectrum, phase_spectrum).restore_polyharmonic(True)
    restored_sequence_without_phases \
        = InverseFourierTransformer(amplitude_spectrum, phase_spectrum).restore_polyharmonic(False)

    drawer = GraphDrawer()
    drawer.add_plot(
        Graph(range(sequence_length), polyharmonic_sequence,
              "Original sequence"))
    drawer.add_stem(
        Graph(range(sequence_length), amplitude_spectrum,
              "Amplitude spectrum"))
    drawer.add_stem(
        Graph(range(sequence_length), phase_spectrum, "Phase spectrum"))
    drawer.add_plot(
        Graph(range(sequence_length), restored_sequence, "Restored sequence"))
    drawer.add_plot(
        Graph(range(sequence_length), restored_sequence_without_phases,
              "Restored sequence w/o phase spectrum"))
    drawer.draw()
    drawer.show()
Example #3
0
def run(data):
    servers = Graph(data, Transformer, " - ", ": ", oriented=True)
    res = {}
    out = ""

    for node in servers.nodes:
        if not out:
            out = node
        best = float("inf")
        for neighbour in servers.nodes[node].neighbours():
            if neighbour in res.keys():
                continue

            path, cost = servers.dijkstra(node, neighbour)
            if cost < best:
                best = cost
                res[node] = path

    total = 0
    to_unpack = len(res)

    while to_unpack:
        key = out[-1]
        direction = res[key].pop()
        total += servers.vertex_cost(key, direction)
        out = out + " - " + direction
        to_unpack -= 1

    print(out + ": " + str(total))
Example #4
0
    def test_has_edge_directed(self):
        """Tests the has_edge method for a directed graph.
        """
        directed = Graph(graph = { 'C' : ['D'], 'D' : [] }, is_directed = True)

        self.assertTrue(directed.has_edge('C', 'D'), 'There is an edge from C to D.')
        self.assertFalse(directed.has_edge('D', 'C'), 'There is no edge from D to C.')
Example #5
0
def assessment_page():
    """ This is the assessment category page. This method is used to generate
    the graphs and talbes that display on the page.
    """

    graph = Graph()

    # assessment_count = graph.assessment_count()
    # assessment_avgtone = graph.assessment_avgtone()

    maine_html_table = graph.table_generator("main_nummen_assessment",
                                             "assessment")
    student_html_table = graph.table_generator("student_nummen_assessment",
                                               "assessment")
    US_html_table = graph.table_generator("US_nummen_assessment", "assessment")
    FL_html_table = graph.table_generator("FL_nummen_assessment", "assessment")

    return render_template(
        'assessment.html',
        maine_html_table=maine_html_table,
        student_html_table=student_html_table,
        US_html_table=US_html_table,
        FL_html_table=FL_html_table,
        # assessment_avgtone=assessment_avgtone,
        # assessment_count=assessment_count
    )
Example #6
0
def build_graph(code):
    graph = Graph()
    blocks = [b["name"] for b in code["blocks"]]
    edges = [(b["name"], e) for b in code["blocks"] for e in b["next_block"]]
    graph.add_nodes(*blocks)
    graph.add_edges(*edges)
    return graph
Example #7
0
def curri_page():
    """ This is the curriculum category page. This method is used to generate the
    graphs and tables that display on this page.
    """
    # The Graph class is instantiated and assigned to the graph variable.
    graph = Graph()

    # CA_distplot = graph.curri_distplot("ca_avgtone_curri_topstates", "California")
    # TX_distplot = graph.curri_distplot("tx_avgtone_curri_topstates", "Texas")
    # MA_distplot = graph.curri_distplot("ma_avgtone_curri_topstates", "Massachusetts")
    # NY_distplot = graph.curri_distplot("ny_avgtone_curri_topstates", "New York")

    CA_html_table = graph.table_generator("ca_curri_topstates", "curriculum")
    TX_html_table = graph.table_generator("tx_curri_topstates", "curriculum")
    MA_html_table = graph.table_generator("ma_curri_topstates", "curriculum")
    NY_html_table = graph.table_generator("ny_curri_topstates", "curriculum")

    return render_template(
        'curriculum.html',
        # CA_distplot=CA_distplot,
        #                         TX_distplot=TX_distplot,
        #                         MA_distplot=MA_distplot,
        #                         NY_distplot=NY_distplot,
        CA_html_table=CA_html_table,
        TX_html_table=TX_html_table,
        MA_html_table=MA_html_table,
        NY_html_table=NY_html_table)
Example #8
0
    def test_has_edge_no_connexion(self):
        """Tests the has_edge method for a graph without connexion.
        """
        graph = Graph(graph = {'Z' : []})

        self.assertFalse(graph.has_edge('Z', 'A'), 'There is no edge between Z to A.')
        self.assertFalse(graph.has_edge('Z', None), 'There is no edge between Z and a None vertex.')
Example #9
0
def topsort(G: Graph) -> List[str]:
    """
    Topologically sort the graph
    """
    i = 0
    visited = set()
    ft = {v: None for v in G.get_vertices()}  # finishing time

    def DFS(u):
        nonlocal i, visited, ft

        visited.add(u)

        for v in G[u]:
            if v not in visited:
                DFS(v)

        ft[u] = i
        i += 1

    for u in G.get_vertices():
        if u not in visited:
            DFS(u)

    return sorted(ft, key=ft.get, reverse=True)
Example #10
0
def charter_schools_page():
    """ This is the charter school category page. This method is used to generate
    the graphs and tables that display on the page.
    """
    graph = Graph()

    # This line can be used to generate the avgtone/nummention line plot again if needed.
    # I left it commented out because there are a lot of data points and it takes a long time
    # to load.
    # lineplot_url = graph.charter_lineplot()
    # top10_us_url = graph.buzzwords_graph("keyword_count")

    # I used the highest number of mentions from 2014-2015 to create this table
    top_20_20142015 = graph.table_generator("charter_schools_20142015",
                                            "charter-school")

    # I used the lowest number of mentions from 2014-2015 to create this table
    top_20_20152016 = graph.table_generator("charter_schools_20152016",
                                            "charter-school")

    # I used the lowest number of mentions from 2014-2015 to create this table
    top_20_2017pres = graph.table_generator("charter_schools_2017pres",
                                            "charter-school")

    return render_template(
        'charter-schools.html',
        # top10_us_url=top10_us_url,
        # # lineplot_url=lineplot_url,
        top_20_20142015=top_20_20142015,
        top_20_20152016=top_20_20152016,
        top_20_2017pres=top_20_2017pres)
Example #11
0
    def test_add_edge_not_valid_vertex(self):
        """Tests the add_edge method in a graph with a non-existing vertex.
        """
        graph = Graph(graph = { 'A' : [] })

        graph.add_edge('A', 'B')
        self.assertFalse(graph.has_edge('A', 'B'), 'No edge between A and non-existing B.')
Example #12
0
 def setUp(self):
     n6 = Node(6)
     n5 = Node(5, [n6])
     n4 = Node(4, [n6])
     n3 = Node(3, [n4, n5])
     n2 = Node(2, [n4])
     n1 = Node(1, [n2, n3])
     self.g = Graph([n1, n2, n3, n4, n5, n6])
Example #13
0
def test_get_nodes():
    graph = Graph()
    graph.add_node('coffee')
    graph.add_node('muffin')
    Vertex('loner')
    expected = 2
    actual = len(graph.get_nodes())
    assert actual == expected
Example #14
0
    def test_remove_unexisting_edge(self):
        """Tests the remove_edge method in a graph without edge between two vertex
        """
        graph = Graph(graph = { 'A' : [], 'B' : [] })

        self.assertFalse(graph.has_edge('A', 'B'), 'There is no edge between A and B.')
        graph.remove_edge('A', 'B')
        self.assertFalse(graph.has_edge('A', 'B'), 'Nothing changed.')
Example #15
0
    def test_add_vertex(self):
        """Tests the add_vertex method with an empty graph.
        """
        graph = Graph()

        self.assertFalse(graph.contains_vertex('A'), 'The graph does not contain A yet.')
        graph.add_vertex('A')
        self.assertTrue(graph.contains_vertex('A'), 'The graph now contains A.')
Example #16
0
    def test_contains_vertex(self):
        """Tests the contains_vertex method with a single vertex.
        """
        data = { 'A' : [] }
        graph = Graph(graph=data)

        self.assertTrue(graph.contains_vertex('A'), 'The graph contains A.')
        self.assertFalse(graph.contains_vertex('B'), 'The graph does not contain B.')
Example #17
0
    def test_remove_edge_undirected(self):
        """Tests the remove_edge method in an undirected graph.
        """
        graph = Graph(graph = { 'A' : ['B'], 'B' : ['A'] })

        self.assertTrue(graph.has_edge('A', 'B'), 'There is an edge between A and B.')
        graph.remove_edge('A', 'B')
        self.assertFalse(graph.has_edge('A', 'B'), 'No more edge between A and B.')
Example #18
0
    def __init__(self):
        Graph.__init__(self)

        # stop to cluster matching
        self.stop_to_cluster = {}

        # cluster's name to node id matching
        self.cluster_id = {}
Example #19
0
    def test_remove_unexisting_edge_with_unexisting_vertex(self):
        """Tests the remove_edge method in a graph between an existing vertex and a non-existing one.
        """
        graph = Graph(graph = { 'A' : [] })

        self.assertFalse(graph.has_edge('A', 'C'), 'There cannot be edge between A and C.')
        graph.remove_edge('A', 'C')
        self.assertFalse(graph.has_edge('A', 'C'), 'Nothing changed.')
Example #20
0
 def test_degree(self):
     """Tests the degree methods in a graph.
     """
     graph = Graph(graph = { 'A' : ['B', 'C'], 'B' : ['A'], 'C' : [] })
     
     self.assertEqual(graph.interior_degree('A'), 1, 'There is 1 edge coming to A.')
     self.assertEqual(graph.exterior_degree('A'), 2, 'There are 2 edges extending from A.')
     self.assertEqual(graph.degree('A'), 3, 'There are 3 edges from and to A.')
Example #21
0
def test_add_edge_interloper_start():
    graph = Graph()

    start = Vertex('start')

    end = graph.add_node('end')

    with pytest.raises(KeyError):
        graph.add_edge(start, end)
Example #22
0
def run(data):
    people = Graph(data, Person, " - ")
    groups = people.get_groups_a2()

    # hax groups
    groups = hax_one_member_grp(people.nodes, groups)

    for group in sorted(groups):
        print(", ".join(g for g in group))
Example #23
0
 def test_circular_layout(self):
   G = Graph.from_edge_pairs([], num_vertices=4)
   expected = np.array([[1,0],[0,1],[-1,0],[0,-1]])
   assert_array_almost_equal(G.layout_circle(), expected)
   # edge cases
   for nv in (0, 1):
     G = Graph.from_edge_pairs([], num_vertices=nv)
     X = G.layout_circle()
     self.assertEqual(X.shape, (nv, 2))
Example #24
0
def knightsGraph(board_size=8):
    g = Graph()
    for row in range(board_size):
        for col in range(board_size):
            currPos = getNormalizedPos(row, col, board_size)
            newPos = getPossiblePositions(row, col, board_size)
            for pos in newPos:
                g.addEdge(currPos, pos)
    return g
Example #25
0
def test__graph_get_community_labels_sparse_graph():
    login, passwd = get_login_password()

    from vkwrapper import Vk
    from graphs import Graph

    v = Vk(cache=FileCache(), login=login, password=passwd)
    g = Graph(v, "148907612")

    g.get_community_labels()
Example #26
0
 def test_it_can_topo_sort_a_graph(self):
     n5 = Node(105)
     n3 = Node(103, [n5])
     n4 = Node(104)
     n2 = Node(102, [n3, n4, n5])
     n1 = Node(101, [n2, n4])
     g = Graph([n1, n2, n3, n4, n5])
     topoSorted = g.topo_sort()
     r = g.min_path(topoSorted)
     self.assertEqual(r, [1, 2, 3, 4, 5])
def read_edge_list_graph(infile):
    """Parse an edge-list formatted graph from `infile`."""
    num_vertices = int(infile.readline())
    graph = Graph(range(num_vertices))

    for line in infile:
        source, target = line.split(' ')
        graph.add_edge(int(source), int(target))

    return graph
Example #28
0
def test_graph_get_community_labels():
    login, passwd = get_login_password()

    from vkwrapper import Vk
    from graphs import Graph

    v = Vk(cache=FileCache(), login=login, password=passwd)
    g = Graph(v, "238696131")

    g.get_community_labels()
Example #29
0
    def test_add_edge_undirected(self):
        """Tests the add_edge method for an undirected graph.
        """
        graph = Graph(graph = { 'A' : [], 'B' : [] })

        self.assertFalse(graph.has_edge('A', 'B'), 'There is no edge between A and B.')

        graph.add_edge('A', 'B')

        self.assertTrue(graph.has_edge('A', 'B'), 'There is now an edge between A and B.')
Example #30
0
def allMoves(bdSize):
    ktGraph = Graph()
    for row in range(bdSize):
        for col in range(bdSize):
            nodeId = posToNodeId(row, col, bdSize)
            newPositions = genAllMoves(row, col, bdSize)
            for e in newPositions:
                nid = posToNodeId(e[0], e[1], bdSize)
                ktGraph.addEdge(nodeId, nid)
    return ktGraph
Example #31
0
def roadsAndLibraries(n, c_lib, c_road, cities):
    # Complete this function
    if not n:
        return 0
    if c_lib <= c_road:
        return n * c_lib
    graph = Graph()
#    for _n in range(n):
#        graph.addVertex(Vertex(str(_n+1)))
    for edge in cities:
        try:
            graph.addVertex(Vertex(str(edge[0])))
        except KeyError:
            pass
        try:
            graph.addVertex(Vertex(str(edge[1])))
        except KeyError:
            pass
        graph.createEdge(str(edge[0]), str(edge[1]))
    print graph.degree
    subgraphs = graph.listSubGraphs()
    print subgraphs
    libraries = len(subgraphs) + (n - graph.degree)
    roads = 0
    for g in subgraphs:
        roads += len(g) - 1
    return (libraries * c_lib) + (roads * c_road)
Example #32
0
 def setUp(self):
     pairs = np.array([[0, 1], [0, 2], [1, 2], [3, 4]])
     adj = [[0, 1, 2, 0, 0], [0, 0, 3, 0, 0], [0, 0, 0, 0, 0],
            [0, 0, 0, 0, 4], [0, 0, 0, 0, 0]]
     self.graphs = [
         Graph.from_edge_pairs(pairs),
         Graph.from_edge_pairs(pairs, symmetric=True),
         Graph.from_adj_matrix(adj),
         Graph.from_adj_matrix(csr_matrix(adj)),
     ]
     self.coords = np.random.random((5, 3))
Example #33
0
def knight_graph(board_size):
    # Build the graph
    graph = Graph()
    for row in range(board_size):
        for col in range(board_size):
            node_id = pos_to_node_id(row, col, board_size)
            new_positions = generate_legal_moves(row, col, board_size)
            for e in new_positions:
                nid = pos_to_node_id(e[0], e[1], board_size)
                graph.add_edge(node_id, nid)
    return graph
Example #34
0
 def test_kernelize(self):
     graphs = [
         Graph.from_edge_pairs(PAIRS),
         Graph.from_adj_matrix(ADJ),
         Graph.from_adj_matrix(coo_matrix(ADJ)),
         Graph.from_adj_matrix(csr_matrix(ADJ)),
     ]
     for G in graphs:
         for kernel in ('none', 'binary'):
             K = G.kernelize(kernel)
             assert_array_equal(K.matrix('dense'), ADJ)
         self.assertRaises(ValueError, G.kernelize, 'foobar')
Example #35
0
 def test_greedy_coloring(self):
     pairs = np.array([[0, 1], [0, 2], [1, 0], [1, 2], [2, 0], [2, 1],
                       [3, 4], [4, 3]])
     adj = [[0, 1, 1, 0, 0], [1, 0, 1, 0, 0], [1, 1, 0, 0, 0],
            [0, 0, 0, 0, 1], [0, 0, 0, 1, 0]]
     test_cases = [
         Graph.from_edge_pairs(pairs),
         Graph.from_adj_matrix(adj),
         Graph.from_adj_matrix(coo_matrix(adj)),
     ]
     for G in test_cases:
         assert_array_equal([1, 2, 3, 1, 2], G.color_greedy())
Example #36
0
    def test_remove_connected_vertex(self):
        """Tests the remove_vertex method for a connected vertex.
        """
        data = { 'A' : ['B'], 'B' : ['A'] }
        graph = Graph(graph=data)

        self.assertTrue('B' in graph.connected_vertex('A'), 'There is an edge between A and B.')
        self.assertTrue('A' in graph.connected_vertex('B'), 'There is an edge between A and B.')

        graph.remove_vertex('A')

        self.assertFalse('A' in graph.connected_vertex('B'), 'No more edge between A and B.')
Example #37
0
 def test_bicolor_spectral(self):
     pairs = np.array([[0, 1], [0, 2], [1, 0], [1, 2], [2, 0], [2, 1],
                       [2, 3], [3, 2]])
     adj = [[0, 1, 1, 0], [1, 0, 1, 0], [1, 1, 0, 1], [0, 0, 1, 0]]
     test_cases = [
         Graph.from_edge_pairs(pairs),
         Graph.from_adj_matrix(adj),
         Graph.from_adj_matrix(coo_matrix(adj)),
     ]
     expected = np.array([1, 1, 0, 1], dtype=bool)
     for G in test_cases:
         assert_array_equal(expected, G.bicolor_spectral())
Example #38
0
def knightGraph(bdSize):
    ktGraph = Graph()
    for row in range(bdSize):
        for col in range(bdSize):
            #import pdb; pdb.set_trace()
            nodeId = posToNodeId(row, col, bdSize)
            newPositions = genLegalMoves(row, col, bdSize)
            for e in newPositions:
                nid = posToNodeId(e[0], e[1], bdSize)
                ktGraph.addEdge(nodeId, nid)
    #            import pdb; pdb.set_trace()
    return ktGraph
Example #39
0
 def test_kernelize(self):
   graphs = [
       Graph.from_edge_pairs(PAIRS),
       Graph.from_adj_matrix(ADJ),
       Graph.from_adj_matrix(coo_matrix(ADJ)),
       Graph.from_adj_matrix(csr_matrix(ADJ)),
   ]
   for G in graphs:
     for kernel in ('none', 'binary'):
       K = G.kernelize(kernel)
       assert_array_equal(K.matrix('dense'), ADJ)
     self.assertRaises(ValueError, G.kernelize, 'foobar')
Example #40
0
def dijkstra(G: Graph, s: str) -> Dict[str, List[str]]:
    L = {s}
    R = set([v for v in G.get_vertices() if v != s])
    SD = {v: None for v in G.get_vertices()}  # Shortest Distances
    parent = {v: v for v in G.get_vertices()}
    paths = {v: [] for v in G.get_vertices()}
    paths[s] = [s]
    dSoFar = []
    dSoFarDict = {}
    i = 0

    # Initialization
    dSoFarDict[s] = [0, i, s]
    heapq.heappush(dSoFar, [0, i, s])
    i += 1
    for v in G.get_vertices():
        if v != s:
            dSoFarDict[v] = [inf, i, v]
            heapq.heappush(dSoFar, [inf, i, v])
            i += 1

    for _ in range(len(SD) - 1):
        # Extract min
        while dSoFar[0][2] is None:
            heapq.heappop(dSoFar)

        min_d = heapq.heappop(dSoFar)
        vprime = min_d[2]

        R.discard(vprime)
        L.add(vprime)
        SD[vprime] = min_d[0]

        # Decrement Key
        for z in G[vprime]:
            if z in R:
                newDistToZ = SD[vprime] + G.get_weight(vprime, z)
                if newDistToZ < dSoFarDict[z][0]:
                    # decrease key
                    dSoFarDict[z][2] = None
                    entry = [newDistToZ, i, z]
                    i += 1
                    dSoFarDict[z] = entry
                    heapq.heappush(dSoFar, entry)

                    # set parent
                    parent[z] = vprime

                    # record path
                    paths[z] = paths[vprime] + [z]

    return paths
Example #41
0
def incremental_neighbor_graph(X,
                               precomputed=False,
                               k=None,
                               epsilon=None,
                               weighting='none'):
    '''See neighbor_graph.'''
    assert ((k is not None)
            or (epsilon is not None)), "Must provide `k` or `epsilon`"
    assert (_issequence(k) ^ _issequence(epsilon)
            ), "Exactly one of `k` or `epsilon` must be a sequence."
    assert weighting in ('binary',
                         'none'), "Invalid weighting param: " + weighting
    is_weighted = weighting == 'none'

    if precomputed:
        D = X
    else:
        D = pairwise_distances(X, metric='euclidean')
    # pre-sort for efficiency
    order = np.argsort(D)[:, 1:]

    if k is None:
        k = D.shape[0]

    # generate the sequence of graphs
    # TODO: convert the core of these loops to Cython for speed
    W = np.zeros_like(D)
    I = np.arange(D.shape[0])
    if _issequence(k):
        # varied k, fixed epsilon
        if epsilon is not None:
            D[D > epsilon] = 0
        old_k = 0
        for new_k in k:
            idx = order[:, old_k:new_k]
            dist = D[I, idx.T]
            W[I, idx.T] = dist if is_weighted else 1
            yield Graph.from_adj_matrix(W)
            old_k = new_k
    else:
        # varied epsilon, fixed k
        idx = order[:, :k]
        dist = D[I, idx.T].T
        old_i = np.zeros(D.shape[0], dtype=int)
        for eps in epsilon:
            for i, row in enumerate(dist):
                oi = old_i[i]
                ni = oi + np.searchsorted(row[oi:], eps)
                rr = row[oi:ni]
                W[i, idx[i, oi:ni]] = rr if is_weighted else 1
                old_i[i] = ni
            yield Graph.from_adj_matrix(W)
Example #42
0
 def setUp(self):
   pairs = np.array([[0,1],[0,2],[1,2],[3,4]])
   adj = [[0,1,2,0,0],
          [0,0,3,0,0],
          [0,0,0,0,0],
          [0,0,0,0,4],
          [0,0,0,0,0]]
   self.graphs = [
       Graph.from_edge_pairs(pairs),
       Graph.from_edge_pairs(pairs, symmetric=True),
       Graph.from_adj_matrix(adj),
       Graph.from_adj_matrix(csr_matrix(adj)),
   ]
   self.coords = np.random.random((5, 3))
Example #43
0
 def test_locality_preserving_projections(self):
   X = np.array([[1,2],[2,1],[3,1.5],[4,0.5],[5,1]])
   G = Graph.from_adj_matrix([[0, 1, 1, 0, 0],
                              [1, 0, 1, 0, 0],
                              [1, 1, 0, 1, 1],
                              [0, 0, 1, 0, 1],
                              [0, 0, 1, 1, 0]])
   proj = G.locality_preserving_projections(X, num_dims=1)
   assert_array_almost_equal(proj, np.array([[-0.95479113],[0.29727749]]))
   # test case with bigger d than n
   X = np.hstack((X, X))[:3]
   G = Graph.from_adj_matrix(G.matrix()[:3,:3])
   proj = G.locality_preserving_projections(X, num_dims=1)
   assert_array_almost_equal(proj, np.array([[0.9854859,0.1697574,0,0]]).T)
Example #44
0
 def test_bicolor_spectral(self):
   pairs = np.array([[0,1],[0,2],[1,0],[1,2],[2,0],[2,1],[2,3],[3,2]])
   adj = [[0,1,1,0],
          [1,0,1,0],
          [1,1,0,1],
          [0,0,1,0]]
   test_cases = [
       Graph.from_edge_pairs(pairs),
       Graph.from_adj_matrix(adj),
       Graph.from_adj_matrix(coo_matrix(adj)),
   ]
   expected = np.array([1,1,0,1], dtype=bool)
   for G in test_cases:
     assert_array_equal(expected, G.bicolor_spectral())
Example #45
0
 def test_greedy_coloring(self):
   pairs = np.array([[0,1],[0,2],[1,0],[1,2],[2,0],[2,1],[3,4],[4,3]])
   adj = [[0,1,1,0,0],
          [1,0,1,0,0],
          [1,1,0,0,0],
          [0,0,0,0,1],
          [0,0,0,1,0]]
   test_cases = [
       Graph.from_edge_pairs(pairs),
       Graph.from_adj_matrix(adj),
       Graph.from_adj_matrix(coo_matrix(adj)),
   ]
   for G in test_cases:
     assert_array_equal([1,2,3,1,2], G.color_greedy())
Example #46
0
File: race.py Project: Tiboris/TGR
def run(data):
    crossroads = Graph(data, Crossroad)
    start = "A"
    distances, predecessors = crossroads.bellman_ford(start)
    successors = invert_dict(predecessors)
    path = start + " - " + successors[start][0]
    while (path.count(" - ") + 1) != len(crossroads.nodes):
        path = path + " - " + successors[path[-1]][0]

    bonuses = 0  # get all bonuses
    for node in crossroads.nodes:
        if crossroads.nodes[node].has_bonus:
            bonuses += path.count(node)

    print(path + ":", (-distances[path[-1]]) + bonuses)
Example #47
0
File: car.py Project: Tiboris/TGR
def run(data):
    start = "Vy"
    car = Graph(data, Transformer, " - ", ": ")
    endpoints = car.nodes.keys()

    res = {}
    for endpoint in endpoints:
        path, cost = car.dijkstra(start, endpoint)
        res[endpoint] = 0 - cost

    inverted = invert_dict(res)
    weights = sorted(inverted)
    for weight in reversed(weights):
        for endpoint in sorted(inverted[weight]):
            print("{}: {}".format(endpoint, weight))
Example #48
0
def incremental_neighbor_graph(X, precomputed=False, k=None, epsilon=None,
                               weighting='none'):
  '''See neighbor_graph.'''
  assert ((k is not None) or (epsilon is not None)
          ), "Must provide `k` or `epsilon`"
  assert (_issequence(k) ^ _issequence(epsilon)
          ), "Exactly one of `k` or `epsilon` must be a sequence."
  assert weighting in ('binary','none'), "Invalid weighting param: " + weighting
  is_weighted = weighting == 'none'

  if precomputed:
    D = X
  else:
    D = pairwise_distances(X, metric='euclidean')
  # pre-sort for efficiency
  order = np.argsort(D)[:,1:]

  if k is None:
    k = D.shape[0]

  # generate the sequence of graphs
  # TODO: convert the core of these loops to Cython for speed
  W = np.zeros_like(D)
  I = np.arange(D.shape[0])
  if _issequence(k):
    # varied k, fixed epsilon
    if epsilon is not None:
      D[D > epsilon] = 0
    old_k = 0
    for new_k in k:
      idx = order[:, old_k:new_k]
      dist = D[I, idx.T]
      W[I, idx.T] = dist if is_weighted else 1
      yield Graph.from_adj_matrix(W)
      old_k = new_k
  else:
    # varied epsilon, fixed k
    idx = order[:,:k]
    dist = D[I, idx.T].T
    old_i = np.zeros(D.shape[0], dtype=int)
    for eps in epsilon:
      for i, row in enumerate(dist):
        oi = old_i[i]
        ni = oi + np.searchsorted(row[oi:], eps)
        rr = row[oi:ni]
        W[i, idx[i,oi:ni]] = rr if is_weighted else 1
        old_i[i] = ni
      yield Graph.from_adj_matrix(W)
Example #49
0
def _slow_neighbor_graph(dist, k, epsilon, binary):
  num_pts = dist.shape[0]

  if k is not None:
    k = min(k+1, num_pts)
    nn, not_nn = _min_k_indices(dist, k, inv_ind=True)
    I = np.arange(num_pts)

  if epsilon is not None:
    mask = dist <= epsilon
    if k is not None:
      mask[I, not_nn.T] = False
    if binary:
      np.fill_diagonal(mask, False)
      W = mask.astype(float)
    else:
      W = np.where(mask, dist, 0)
  else:
    inv_mask = np.eye(num_pts, dtype=bool)
    inv_mask[I, not_nn.T] = True
    if binary:
      W = 1.0 - inv_mask
    else:
      W = np.where(inv_mask, 0, dist)

  # W = scipy.sparse.csr_matrix(W)
  return Graph.from_adj_matrix(W)
Example #50
0
 def setUp(self):
   ii = np.array([0, 0, 1, 2, 2, 3, 3, 3, 4, 5])
   jj = np.array([1, 2, 3, 4, 5, 6, 7, 8, 7, 7])
   adj = np.zeros((9,9), dtype=int)
   adj[ii,jj] = 1
   adj[jj,ii] = 1
   self.G = Graph.from_adj_matrix(adj)
Example #51
0
    def get_node(self, key):
        """
        Override parent's get_node function. (Supported to search by 
        cluster name , key and instance)
        
        @param data The key
        
        @type data db.Key
        @type data int
        @type data basestring The key of cluster
        """
        if isinstance(key, int):
            id = key
        else:
            if isinstance(key, basestring):
                # The name of the cluster
                if isinstance(key, unicode):
                    cluster = str(key)
                else:
                    cluster = key
            elif isinstance(key, db.Key):
                cluster = key().id_or_name()
            else:
                cluster = key.key().id_or_name()

            id = self.cluster_id[cluster]

        return Graph.get_node(self, id)
Example #52
0
def sparse_regularized_graph(X, positive=False, sparsity_param=None, kmax=None):
  '''Sparse Regularized Graph Construction, commonly known as an l1-graph.

  positive : bool, optional
    When True, computes the Sparse Probability Graph (SPG).
  sparsity_param : float, optional
    Controls sparsity cost in the LASSO optimization.
    When None, uses cross-validation to find sparsity parameters.
    This is very slow, but it gets good results.
  kmax : int, optional
    When None, allow all points to be edges. Otherwise, restrict to kNN set.

  l1-graph: "Semi-supervised Learning by Sparse Representation"
    Yan & Wang, SDM 2009
    http://epubs.siam.org/doi/pdf/10.1137/1.9781611972795.68

  SPG: "Nonnegative Sparse Coding for Discriminative Semi-supervised Learning"
    He et al., CVPR 2001
  '''
  clf, X = _l1_graph_setup(X, positive, sparsity_param)
  if kmax is None:
    W = _l1_graph_solve_full(clf, X)
  else:
    W = _l1_graph_solve_k(clf, X, kmax)
  return Graph.from_adj_matrix(W)
Example #53
0
def manifold_spanning_graph(X, embed_dim, num_ccs=1, verbose=False):
  G = neighbor_graph(X, k=1, weighting='binary')
  G.symmetrize(method='max')

  G = grow_trees(X, G, embed_dim, verbose=verbose)

  CC_labels, angle_thresh = join_CCs(X, G, embed_dim, num_ccs=num_ccs,
                                     verbose=verbose)

  adj = G.matrix('dense')
  if num_ccs == 1:
    G = flesh_out(X, adj, embed_dim, CC_labels, angle_thresh=angle_thresh,
                  min_shortcircuit=embed_dim+1, verbose=verbose)
  else:
    n, labels = G.connected_components(return_labels=True)
    for i in range(n):
      mask = labels==i
      if verbose:  # pragma: no cover
        print('CC', i, 'has size', np.count_nonzero(mask))
      idx = np.ix_(mask, mask)
      _, tmp_labels = np.unique(CC_labels[mask], return_inverse=True)
      adj[idx] = flesh_out(X[mask], adj[idx], embed_dim, tmp_labels,
                           angle_thresh=angle_thresh,
                           min_shortcircuit=embed_dim+1,
                           verbose=verbose).matrix()
    G = Graph.from_adj_matrix(adj)
  return G
Example #54
0
def permute_graph(G, order):
  '''Reorder the graph's vertices, returning a copy of the input graph.
  order : integer array-like, some permutation of range(G.num_vertices()).
  '''
  adj = G.matrix(dense=True)
  adj = adj[np.ix_(order, order)]
  return Graph.from_adj_matrix(adj)
Example #55
0
 def _make_blob_graphs(self, k=11):
   pts = np.random.random(size=(20, 2))
   pts[10:] += 2
   labels = np.zeros(20)
   labels[10:] = 1
   G_sparse = neighbor_graph(pts, k=k).symmetrize()
   G_dense = Graph.from_adj_matrix(G_sparse.matrix(dense=True))
   return (G_sparse, G_dense), labels
Example #56
0
 def test_laplacian_eigenmaps(self):
   # Test a simple chain graph
   expected = np.array([0.5, 0.5, 0., -0.5, -0.5])
   W = np.zeros((5,5)) + np.diag(np.ones(4), k=1) + np.diag(np.ones(4), k=-1)
   G = Graph.from_adj_matrix(W)
   Y = G.laplacian_eigenmaps(num_dims=1)
   self.assertEqual(Y.shape, (5, 1))
   assert_signless_array_almost_equal(Y[:,0], expected)
   # Test num_dims=None case
   Y = G.laplacian_eigenmaps()
   self.assertEqual(Y.shape, (5, 4))
   assert_signless_array_almost_equal(Y[:,0], expected)
   # Test sparse case
   G = Graph.from_adj_matrix(csr_matrix(W))
   Y = G.laplacian_eigenmaps(num_dims=1)
   self.assertEqual(Y.shape, (5, 1))
   assert_signless_array_almost_equal(Y[:,0], expected)
Example #57
0
def concat_trajectories(traj_lengths, directed=False):
  P = []
  last_idx = 0
  for tl in traj_lengths:
    P.append(last_idx + _traj_pair_idxs(tl))
    last_idx += tl
  return Graph.from_edge_pairs(np.vstack(P), num_vertices=last_idx,
                               symmetric=(not directed))