Ejemplo n.º 1
0
def community_colors(db, pno, threshold, show_vis = False, savefn=None):
    # Get the patents in the lineage and the adjacency dictionary. 
    lineage = crawl_lineage(
        db, pno, n_generations = 5, 
        enforce_func = lambda x: len(x.get('citedby', [])) >= threshold,
        flatten=True
    )
    adj = subnet_adj_dict(lineage)

    # detect communities
    detector = detect.CommunityDetector(adj)
    communities = detector.run()
    n_communities = len(communities)
    community_lookup = util.get_community_lookup(communities)

    # assign each patent a color, and provide a lookup dictionary
    colors = visualize.discrete_color_scheme(n_communities+1)
    node_color_lookup = {node: colors[community_lookup[node]] for node in adj.keys()}
    
    # make the visualization.
    G = visualize.get_graph(adj)
    G.graph['ancestor'] = pno
    ancestor_idx = G.nodes().index(G.graph['ancestor'])
    node_colors = [colors[community_lookup[node]] for node in G.nodes()]
    node_colors[G.nodes().index(G.graph['ancestor'])] = colors[n_communities]
    default_node_size = 60
    node_sizes = [default_node_size for node in G.nodes()]
    node_sizes[ancestor_idx] = 6*default_node_size
    f = plt.figure()
    f.set_size_inches(18.5, 10.5)
    if savefn is not None or show_vis:
        nx.draw_networkx(
            G, 
            nx.spring_layout(G, iterations=20000), 
            node_color=node_colors, 
            node_size = node_sizes,
            with_labels = False,
            fontsize=1,
#            font_weight = 'bold',
            linewidths=.5,
            width=.5
        )
    if savefn is not None:
        plt.title('Communities in Patent {}'.format(pno))
        plt.savefig(savefn, dpi=100)
    if show_vis:
        plt.show()
    return node_color_lookup
Ejemplo n.º 2
0
# load data.
patent_adj = data.get_patent_adj()

# Build a community detector object and run the algorithm
detector = detect.CommunityDetector(patent_adj)
communities = detector.run()
n_communities = len(communities)

#Build a lookup table for node to color.
community_lookup = {}
for i,c in enumerate(communities):
    for pno in c:
        community_lookup[pno] = i

colors = visualize.discrete_color_scheme(n_communities+1)


# Get a graph from the adjacency list.
G = visualize.get_graph(patent_adj)
G.graph['ancestor'] = 4723129
node_colors = [colors[community_lookup[node]] for node in G.nodes()]

# Set the ancestor to its own color.
node_colors[G.nodes().index(G.graph['ancestor'])] = colors[n_communities]
nx.draw(G, nx.spring_layout(G, iterations=10000), cmap=plt.get_cmap('jet'), node_color=node_colors, node_size=100)
#nx.draw_spring(G, cmap=plt.get_cmap('jet'), node_color=node_colors, node_size=100)