def degree_vibrance_highlighter(G, use_hue=False): """Sets node colors and edges to be brighter the higher degree they have. Singletons are black.""" highlighter = hl.Highlighter(G) highlighter.layout = nx.spring_layout(G, dim=3, scale=1000) degrees = [degree for node, degree in G.degree] max_degree = 1 / max(degrees) hue = lambda degree : (degree * max_degree) * 0.7 # Make it less than 1 so we don't get red values for max and min node_colors = [hl.Material( colorsys.hsv_to_rgb(hue(degree) if use_hue else 0, 1, 1), colorsys.hsv_to_rgb(hue(degree) if use_hue else 0, 1, 1), colorsys.hsv_to_rgb(0, 0.0, 1), 1) for degree in degrees] edge_colors = [(*node_colors[node].ambient, 0.5) for edge in G.edges for node in edge] node_colors[139] = hl.Material( (0.5, 1, 1), (0.5, 1, 1), (0.5, 1, 1), 1) highlighter.set_node_colors(node_colors) highlighter.set_edge_colors(edge_colors) highlighter.set_light_color(hl.Material((15, 15, 15), (0.0, 0.0, 0.0), (0.0, 0.0, 0.0), 0)) return highlighter
def partition_highlighter(G, partition_filename, c = 2): z = get_partition(G, partition_filename, c) highlighter = hl.Highlighter(G) max_degree = 1 / max(z) hue = lambda group : (group * max_degree) * 0.6 # Make it less than 1 so we don't get red values for max and min node_colors = [hl.Material( colorsys.hsv_to_rgb(hue(group), 1, 1), colorsys.hsv_to_rgb(hue(group), 1, 1), (0.0, 0.0, 0.0), 1) for group in z] edge_colors = [(*colorsys.hsv_to_rgb(hue(z[node]), 1, 1), 0.5) for edge in G.edges for node in edge] highlighter.set_node_colors(node_colors) highlighter.set_edge_colors(edge_colors) highlighter.set_light_color(hl.Material((5, 5, 5), (0.0, 0.0, 0.0), (0.0, 0.0, 0.0), 0)) # Makes the nodes neon! return highlighter
def triangle_highlighter(G, name, feed_back_loop=False): highlighter = hl.Highlighter(G) highlighter.name = name degrees = [degree for node, degree in G.degree] normal_material = hl.Material(colorsys.hsv_to_rgb(0,0.5,0.5), colorsys.hsv_to_rgb(0,0.5,0.5), (0.5, 0.5, 0.5), 32) triangle_material = hl.Material(colorsys.hsv_to_rgb(0.5,0.5,1), colorsys.hsv_to_rgb(0.5,0.5,1), (0.5, 0.5, 0.5), 32) normal_edge_color = (*normal_material.ambient, 0.5) triangle_edge_color = (*colorsys.hsv_to_rgb(0.5,0.5,2), 0.5) # Find all nodes that are part of a triangle def find_triangles(): triangle_nodes = set() triangle_edges = set() for node in G: for neighbor in G.neighbors(node): for second_neighbor in G.neighbors(neighbor): if neighbor != second_neighbor and G.has_edge(second_neighbor, node): triangle_nodes.update((node, neighbor, second_neighbor)) triangle_edges.update(((node, neighbor), (neighbor, second_neighbor), (second_neighbor, node), (neighbor, node), (second_neighbor, neighbor), (node, second_neighbor))) return triangle_nodes, triangle_edges def find_feed_back_loop(): triangle_nodes = set() triangle_edges = set() for node in G: for neighbor in G.neighbors(node): for second_neighbor in G.neighbors(neighbor): if neighbor != second_neighbor and node in G.neighbors(second_neighbor): triangle_nodes.update((node, neighbor, second_neighbor)) triangle_edges.update(((node, second_neighbor), (node, neighbor), (neighbor, second_neighbor))) return triangle_nodes, triangle_edges triangle_nodes, triangle_edges = find_feed_back_loop() if feed_back_loop else find_triangles() node_colors = [triangle_material if node in triangle_nodes else normal_material for node in G.nodes] # Edges that are a part of a triangle are highlighted with a special color edge_colors = [triangle_edge_color if edge in triangle_edges else normal_edge_color for edge in G.edges for node in edge] highlighter.set_node_colors(node_colors) highlighter.set_edge_colors(edge_colors) return highlighter
def text_highlighter(G): highlighter = hl.Highlighter(G) highlighter.set_node_radius(0.01) if nx.is_directed(G): reciprocities = list(nx.algorithms.reciprocity(G, nodes=G.nodes).values()) def display_node_attributes(node): """Accepts a node, calculates various attributes, and formats it into a string""" text = G[node]["name"] if hasattr(G[node], "name") else "Node: " + str(node) if nx.is_directed(G): text += ", In Degree: " + str(G.in_degree[node]) text += ", Out Degree: " + str(G.out_degree[node]) else: text += "Degree: " + str(G.degree[node]) eccentricity = max(nx.single_source_shortest_path_length(G, node)) text += ", Ecentricity: " + str(eccentricity) if nx.is_directed(G): text += ", Reciprocity: {:.2f}".format(reciprocities[node]) return text highlighter.set_node_text(display_node_attributes) degrees = [degree for node, degree in G.in_degree] median_degree = np.median(degrees) def color(degree): return (0.5, 0.5, 1) if degree < (median_degree * 9) else (15, 10, 10) node_colors = [hl.Material( color(degree), color(degree), color(degree), 32) for degree in degrees] edge_colors = [(0.8, 0.8, 0.8, 0.05) for edge in G.edges for node in edge] highlighter.set_node_colors(node_colors) highlighter.set_edge_colors(edge_colors) return highlighter
def neighbors_highlighter(G): highlighter = hl.Highlighter(G) def on_node_selection(node): return "Node: {}".format(node) highlighter.set_node_text(on_node_selection) def color(node): return (0.5, 0.5, 0.5) node_colors = [hl.Material( color(node), color(node), color(node), 32) for node in G.nodes] edge_colors = [(0.8, 0.8, 0.8, 0.05) for edge in G.edges for node in edge] highlighter.set_node_colors(node_colors) highlighter.set_edge_colors(edge_colors) return highlighter