def plot_network2(network): """Function that plots the network of topics. It mainly relies on webweb: https://github.com/dblarremore/webweb Args: network (dictionary): computed network. Returns: """ G = nx.Graph() for node in network["nodes"]: G.add_node(node["label"]) G.nodes[node["label"]]['isPaper'] = False G.nodes["paper"]['isPaper'] = True for edge in network["edges"]: G.add_edge(edge["source"], edge["target"], kind=edge["kind"], style='dashed') # create the web web = Web(nx_G=G) web.display.showNodeNames = True web.display.colorBy = 'isPaper' web.show()
def webweb_plot(g, coms): # create dictionary of node:community pairs for quick look up print('Plotting with WebWeb') coms_dict = {} for i in range(len(coms)): for c in coms[i]: # this is to control for string reformatting R did... if c[0:3] == 'HP.': c = c.replace('.', ':') coms_dict[c] = i # create metadata labels for plotting labels = {} for i in g.nodes: name = g.nodes[i]['Name'] if name in coms_dict.keys(): labels[i] = { 'isGene': g.nodes[i]['Type'] == 'HPO', 'name': coms_dict[name], 'Community': coms_dict[name] } w = Web(adjacency=nx.to_numpy_array(g), display={'nodes': labels}) # set some default visualization settings in webweb w.display.charge = 50 w.display.sizeBy = 'degree' # genes will be colored green w.display.colorBy = 'Community' w.display.charge = 10 w.display.linkLength = 5 w.display.gravity = 0.5 w.show()
def net_vis(ld_file, loci_file): web = Web(title='webweb') loci_path = os.path.abspath(loci_file) loci = pandas.read_csv(loci_path) def get_ef(l): q = 'locus ==' + str(l) r = loci.query(q) return r.iloc[0]['ef'] def get_chr(l): q = 'locus ==' + str(l) r = loci.query(q) return r.iloc[0]['chromo'] path = os.path.abspath(ld_file) df = pandas.read_csv(path) gens = df['generation'].unique() thres = 0.005 max_locus = df['locus2'].max() metadata = { 'ef': { 'values': [get_ef(x) for x in range(max_locus)] }, 'chromo': { 'values': [get_chr(x) for x in range(max_locus)] }, } for gen in gens: q = 'generation == ' + str(gen) this_gen = df.query(q) max = this_gen['D'].max() edge_list = [] for row in this_gen.itertuples(): L1 = row[2] L2 = row[3] D = row[4] Gen = row[1] if D > thres: edge = [L1, L2, 0.1] edge_list.append(edge) else: edge = [L1, L2, 0] edge_list.append(edge) web.networks.webweb.add_layer(adjacency=edge_list, metadata=metadata) #web.display.scaleLinkWidth = True web.show()
def plot_single_community_webweb(g, coms, com): print('Plotting with WebWeb') # create dictionary of node:community pairs for quick look up coms_dict = {} for i in range(len(coms)): for c in coms[i]: coms_dict[c] = i # get the indexes of the nodes in the community of interest com_indexes = [] for i in g.nodes: name = g.nodes[i]['Name'] if name in coms_dict.keys(): if coms_dict[name] == com: com_indexes.append(i) # get a subset of the grapha that is just the community of interest subgraph = g.subgraph(com_indexes) # create a name mapping to relabel subgraph nodes starting at one name_mapping = {} node_count = 0 for i in subgraph.nodes: print(i) name_mapping[i] = node_count node_count += 1 # relabel subgraph nodes starting at one subgraph = nx.relabel_nodes(subgraph, name_mapping) # create labels for the nodes for use in webweb labels = {} for i in subgraph.nodes: name = subgraph.nodes[i]['Name'] if name in coms_dict.keys(): if coms_dict[name] == com: labels[i] = { 'isGene': subgraph.nodes[i]['Type'] == 'HPO', 'name': name, 'Community': coms_dict[name] } # plot it using webweb w = Web(adjacency=nx.to_numpy_array(subgraph), display={'nodes': labels}) # set some default visualization settings in webweb w.display.charge = 50 w.display.sizeBy = 'degree' w.display.colorBy = 'isGene' w.display.charge = 20 w.display.linkLength = 50 w.display.gravity = 0.1 w.show()
def display_with_webweb(graph, section_sequence): print("Displaying graph using webweb...") web = Web(title='book') web.display.colorBy = 'degree' web.display.sizeBy = 'degree' web.display.l = 60 web.display.c = 120 web.display.scaleLinkWidth = True for G in graph.graph_by_sections(section_sequence, aggregate=True, decay_weights=True, stability=40): web.networks.book.add_layer(nx_G=G) web.show()
def visual_graph(all_bands): edge_list = [] b = [] i = 0 for band_id in all_bands: i +=1 # if i > 100: # break if all_bands[band_id]["is_dup"]: continue for to in all_bands[band_id]["in_conns"]: if all_bands[to]["is_dup"]: to = all_bands[to]["link_to"] edge_list.append([band_id, to]) edge_list.append([to, band_id]) if set(all_bands[band_id]["genres"]) == {"rock"}: group = 'R' elif set(all_bands[band_id]["genres"]) == {"country"}: group = 'C' elif set(all_bands[band_id]["genres"]) == {"metal"}: group = "M" elif set(all_bands[band_id]["genres"]) == {"rock", "country"}: group = "R+C" elif set(all_bands[band_id]["genres"]) == {"rock", "metal"}: group = "R+M" elif set(all_bands[band_id]["genres"]) == {"country", "metal"}: group = "C+M" elif set(all_bands[band_id]["genres"]) == {"rock", "country", "metal"}: group = "R+C+M" else: raise Exception("Band with unspecified group") b.append([band_id, group]) web = Web(title='sbm_demo', # assign its edgelist adjacency=edge_list, # give it the community metadata nodes={i: {'Genre': g} for i, g in b}, # display={"metadata": {'Genre': {'values': ["R", "C", "M", "R+C", "R+M", "C+M", "R+C+M"]}}} ) web.display.colorBy = 'Genre' web.show()
def visualizePassingNetworks(visTitle, visInfoList): # Visualization Parameters width = 800 height = 480 padding = 50 #field dimensions in yards fieldLength = 120 fieldWidth = 75 # instantiate webweb web = Web(title=visTitle) for item in visInfoList: passing, libPlayers = PassingUtilities.createPassingListAndLib(item[0]) if len(passing) != 0: A = PassingUtilities.createAdjacencyMatrix(passing, libPlayers) displayInfo = createMetaData(libPlayers, width, height, padding, fieldLength, fieldWidth) web.networks.__dict__[item[1]] = webweb.webweb.Network( adjacency=A, metadata=displayInfo) web.display.charge = 500 web.display.linkLength = 200 web.display.linkStrength = 0.5 web.display.gravity = .1 web.display.radius = 10 web.display.scaleLinkOpacity = True web.display.colorBy = 'team' web.display.scaleLinkWidth = True web.display.sizeBy = 'strength' web.display.showNodeNames = True web.display.showLegend = True web.display.colorPalette = 'Dark2' web.display.freezeNodeMovement = True web.display.width = width web.display.height = height web.display.attachWebwebToElementWithId = 'soccer-vis' web.show()
from webweb import Web # Instantiate webweb object web = Web( [[0, 1], [1, 2], [2, 3], [3, 4], [4, 5]], display={ 'metadata': { 'emotion': { 'values': ['happy', 'sad', 'angry', 'sad', 'happy', 'sad'] } } }) # use the 'emotion' attribute to color nodes web.display.colorBy = 'emotion' # show the visualization web.show()
def make_network(data): nodes = defaultdict(dict) edges = [] all_edges = [] interests = data["areas"]["topics"] projects = data["projects"]["projects"] project_areas = set() for project in projects: name = project["name"] nodes[name] = {"name": name, "kind": "Project"} for area in project["areas"]: all_edges.append([name, area]) project_areas.add(area) for interest in interests: area = interest["area"] # if area not in project_areas: # continue nodes[area] = {"name": area, "kind": interest["kind"]} if not interest["related"]: continue for related in interest["related"]: all_edges.append([area, related]) # Get only unique edges for edge in all_edges: if edge in edges or [edge[1], edge[0]] in edges: continue edges.append(edge) for node in nodes: kind = nodes[node]['kind'] size = 1 # if kind == 'Theory': # size = 1.5 # elif kind == 'Application': # size = 1.25 nodes[node]['size'] = size nodes[node]['color'] = KIND_TO_COLOR_MAP[kind] web = Web(adjacency=edges, nodes=dict(nodes)) web.display.sizeBy = 'size' web.display.colorBy = 'color' web.display.hideMenu = True web.display.showLegend = False web.display.gravity = 1e-5 web.display.charge = 500 web.display.width = 400 web.display.height = 400 web.display.scaleLinkOpacity = True web.display.scaleLinkWidth = True web.display.scales = { 'nodeSize': { 'min': 0.65, 'max': 1.1, } } WEBWEB_JSON_PATH.write_text(web.json) web.show()