Example #1
0
    def to_pyvis(self, max_vertices: int = 5000) -> Network:
        """Convert this graph into a PyVis Network object.

        max_vertices specifies the maximum number of vertices that can appear in the graph.

        NOTE: Running this method withough iPython may result in KeyErrors
        """
        graph_pyvis = Network()
        for v in self._vertices.values():
            graph_pyvis.add_node(v.item)

            for u in v.neighbours:
                if graph_pyvis.num_nodes() < max_vertices:
                    graph_pyvis.add_node(u.item)

                if u.item in graph_pyvis.get_nodes():
                    graph_pyvis.add_edge(v.item, u.item)

            if graph_pyvis.num_nodes() >= max_vertices:
                break

        return graph_pyvis
Example #2
0
db_info = [info for info in db_info if info['name'] in connected_nodes]

documents = [document_from_database_info(info) for info in db_info]
k = 10
labels = tfidf_kmeans(documents, k)
# labels = get_wordnet_labels(documents)
vis_network = Network(height="100%", width="70%")

colors = get_different_colors(max(labels)+1)
# labels from k means
color_labels = [colors[l] for l in labels]
# services use this tables
# color_labels = ["#FF0000" if len(get_services_from_table(x['name'])) > 0 else "#DDDDDD" for x in db_info]

vis_network.add_nodes([x['name'] for x in db_info], color=color_labels)

for table in db_info:
    for key in table['foreign_keys']:
        foreign_key_graph.add_edge(table['name'], key)
        if key in vis_network.get_nodes():
            vis_network.add_edge(table['name'], key)

# git_network = Network(height="750px", width="100%")
# git_network.from_nx(foreign_key_graph)

# git_network.show_buttons(filter_=['physics'])
# git_network.show("network_outputs/foreign_key.html")

vis_network.show_buttons(filter_=['physics'])
vis_network.show("network_outputs/foreign_key.html")
def map_kcs(kc_list,
            kc_matrix,
            node_color="#8B008B",
            edge_color="#03DAC6",
            node_shape="ellipse",
            alg="barnes",
            edge_smooth=None,
            buttons=False,
            treshold=0.5):
    """
    Use this on  a test that has less than 26 KCs, or else there will be problems with
    relations between the edges due to name conflict

    :param threshold: float,
    :param kc_list: np.array, list of all kcs
    :param edge_smooth: string, How the user wants the edges to be, default is continuous
    :param buttons: bool, buttons to edit graph with
    :param node_shape: string, shape of node
    :param edge_color: string, shape of edge
    :param node_color: string, hexvalue of color for node
    :param kc_matrix: 2D np array, mapping of kcs
    :param alg: string, algorithm for graph
    :return: renders a graph in which you can see the relations between nodes and their edges

    """
    g = Network(height="1500px",
                width="75%",
                bgcolor="#222222",
                font_color="white",
                directed=True)

    # if buttons:
    g.width = "75%"
    # nodes, layout, interaction, selection, renderer, physics
    g.show_buttons(filter_=["edges", "physics"])

    # Create the nodes
    for kc_node in kc_list:
        g.add_node(n_id=kc_node,
                   label=kc_node,
                   color=node_color,
                   shape=node_shape)

    # Creates edges between nodes if they're connected (if the value is true in the database)
    n = len(kc_matrix)
    for r in range(n):
        for c in range(n):
            if r != c:
                # Directly logically connnected edge
                if 0.9 <= kc_matrix[r, c] <= 1.0:
                    g.add_edge(kc_list[r], kc_list[c],
                               color="#42cc14")  # green
                # Necessary edge
                elif 0.7 <= kc_matrix[r, c] < 0.9:
                    g.add_edge(kc_list[r], kc_list[c],
                               color="#ff8c00")  # orange
                # Important edge
                elif 0.5 <= kc_matrix[r, c] < 0.7:
                    g.add_edge(kc_list[r], kc_list[c],
                               color="#ffd900")  # orange
                elif kc_matrix[r, c] == -1.0:
                    g.add_edge(kc_list[r], kc_list[c], color="#56f0eb")

    map_algs(g, alg)

    if edge_smooth is not None:
        map_smoothenes(graph=g, smooth=edge_smooth)

    node_list = g.get_nodes()
    for node in g.nodes:
        neighbors = g.neighbors(node_list[node_list.index(node['id'])])
        if neighbors is None:
            title = "Endpoint KC"
        else:
            title = "What to learn next:<br>"
            for neighbor in neighbors:
                title += f"{neighbor}<br>"
        node["title"] = title

    # g.save_graph("../../kc.html")
    g.show("kc_map.html")