コード例 #1
0
    def create_networks(self):

        print "creating networks"

        # WORDS
        print "-"*10+" Words"
        words_graph= self.create_network(self.words, self.words_to_words, self.limit_words, 1)

        if words_graph.order() != 0: 
            self.words_allowed=[self.words[int(w)] for w in words_graph.nodes()]
            print "%d words_allowed"%len(self.words_allowed)

            self.words_communities = community.best_partition(words_graph.to_undirected()) 
            print "Number of words partitions : ", len(set(self.words_communities.values()))

        # CITATIONS
        print
        print "-"*10+" Citations"
        citations_graph= self.create_network(self.cited, self.citations, self.limit_citations, 0)

        if citations_graph.order() != 0:
            self.cited_allowed=[self.cited[int(w)] for w in citations_graph.nodes()]
            print "%d cited_allowed"%len(self.cited_allowed)

            # Communities
            self.citations_communities = community.best_partition(citations_graph.to_undirected()) 
            print "Number of citations partitions : ", len(set(self.citations_communities.values()))
コード例 #2
0
def assign_community(graph):
    g=nx.Graph(graph)
    partition=community.best_partition(g)
    print "Partition found: ",len(set(partition.values()))
    for n in g.nodes_iter():
        g.node[n]["partition"]=partition[n]
    return g
コード例 #3
0
def _partitions(graph):
    """
    Internal use only.
    Finds partitions with louvain method and returns dict
    """
    # Get Partitions with Louvain method
    part = community.best_partition(graph)

    # Structure them for info purpose
    parts = {}
    for item in part:
        n = part[item]  # Partition number
        if n not in parts:  # Check if list for partition exists
            parts[n] = []

        # add node with degree in graph (for sorting) as tuples to list
        parts[n].append(item)  # ((item, graph.degree()[item]))

    # Use degree to find name for category
    names = {key: max(
        [(item, graph.degree(weight='weight')[item]) for item in parts[key]],
        key=itemgetter(1))
        [0] for key in parts}

    # New return dict. ToDo: make it like thisright away
    res = {key: {'name': names[key], 'categories': parts[key]}
           for key in parts}

    return res
コード例 #4
0
    def detect_communities(self, graph, users, resolution, fraction):

        partitions = community.best_partition(graph.to_undirected(), resolution=resolution)

        counter = Counter(partitions.values())
        number_of_nodes = sum(counter.values())

        self._logger.info("Counter %s", counter)

        communities = [i for i in counter.items() if i[1] > fraction * number_of_nodes]

        self._logger.info("Number of nodes: %d", number_of_nodes)
        self._logger.info("Number of communities to map: %d", len(communities))
        self._logger.info("Communities: %s", communities)

        partitions_to_com = dict.fromkeys(set(partitions.values()), CommunityUser.UNCLASSIFIED)

        output = {}

        for com, _ in communities:
            com_nodes = [users[n].get_classification() for n in partitions.keys() if partitions[n] == com]
            com_classes = Counter(com_nodes)

            self._logger.info("%d: %s", com, com_classes)
            partitions_to_com[com] = com_classes.most_common(1)[0][0]
            output[com] = (partitions_to_com[com], com_classes)

        for node in graph.nodes():
            c = partitions[node]
            graph.node[node]["community"] = c
            graph.node[node]["classification"] = partitions_to_com[c]

        # json.dump(output, open("per_classification.txt", "w"))

        return graph
コード例 #5
0
ファイル: Interjectors.py プロジェクト: cpsnowden/GDOTwitter
def detect_communities(graph, users, resolution=1.0, fraction=0.05):
    partitions = community.best_partition(graph.to_undirected(), resolution=resolution)

    counter = Counter(partitions.values())
    number_of_nodes = sum(counter.values())
    communities = [i for i in counter.items() if i[1] > fraction * number_of_nodes]

    partitions_to_com = dict.fromkeys(set(partitions.values()), CommunityUser.UNCLASSIFIED)

    output = {}

    for com, _ in communities:
        com_nodes = [users[n].get_classification() for n in partitions.keys() if partitions[n] == com]
        com_classes = Counter(com_nodes)
        partitions_to_com[com] = com_classes.most_common(1)[0][0]
        output[com] = (partitions_to_com[com], com_classes)

    for node in graph.nodes():
        c = partitions[node]
        graph.node[node]["community"] = c
        graph.node[node]["classification"] = partitions_to_com[c]
        if partitions_to_com[c] != "Unclassified" and users[node].get_classification() != "Unclassified":
            if partitions_to_com[c] != users[node].get_classification():
                print node, partitions_to_com[c], users[node].get_classification()
                wrongs.append({"user":node,"louvian": partitions_to_com[c], "class": users[node].get_classification()})
    return graph
コード例 #6
0
def community_structure(G, candidates):
    partition = community.best_partition(G)
    to_return = {}
    candidates_found = 0
    for candidate in candidates:
        to_return[candidate] = {}
    candidate_to_pacs_in_community = {}
    candidate_to_community_size = {}
    nodes_so_far = 0
    candidates_per_community = []
    pacs_per_community = [] 
    community_size = []
    for com in set(partition.values()) :
        list_nodes = [nodes for nodes in partition.keys() if partition[nodes] == com]
        num_candidates = 0
        num_pacs = 0
        for node in list_nodes:
        	if node[0:2] == 'C0': num_pacs += 1
        	if node in candidates: num_candidates += 1
        for node in list_nodes:
            if node in candidates:
                to_return[node]['community_size'] = len(list_nodes)
                to_return[node]['pacs_in_community'] = num_pacs
                candidates_found += 1
                print 'found ', node
        if (candidates_found >= len(candidates)): break
    for candidate in candidates:
        if 'community_size' not in to_return[candidate]:
            to_return[candidate]['community_size']  = 0
            print 'didnt find ',candidate
    for candidate in candidates:
        if 'pacs_in_community' not in to_return[candidate]:
            to_return[candidate]['pacs_in_community']  = 0
    return to_return
コード例 #7
0
def louvain_method(G):
    partition = community.best_partition(G)
    print "Graph nodes:", len(G.nodes()), "egdes:", len(G.edges())
    print "Partitions:", len(set(partition.values())),\
          "Modularity:", community.modularity(partition, G.to_undirected())
    print "\n\n"
    return partition
コード例 #8
0
ファイル: analyze.py プロジェクト: azercephi/kraftwerk
def printStats(filename):
	'''
	Converts json adjacency list into networkx to calculate and print the
	graphs's 
	  - average clustering coefficient
	  - overall clustering coefficient
	  - maximum diameter
	  - average diameter
	  - number of paritions using community.best_parition
	  - modularity of community.best_partition
	'''
	g = makeGraphFromJSON(filename)
	
	print "Average Clustering Coefficient: %f" % nx.average_clustering(g)
	print "Overall Clustering Coefficient: %f" % nx.transitivity(g)
	
	connected_subgraphs = list(nx.connected_component_subgraphs(g))
	largest = max(nx.connected_component_subgraphs(g), key=len)
	print "# Connected Components: %d" % len(connected_subgraphs)
	print "    Maximal Diameter: %d" % nx.diameter(largest)
	print "    Average Diameter: %f" % nx.average_shortest_path_length(largest)

	# Find partition that maximizes modularity using Louvain's algorithm
	part = community.best_partition(g)	
	print "# Paritions: %d" % (max(part.values()) + 1)
	print "Louvain Modularity: %f" % community.modularity(part, g)
コード例 #9
0
def getRandomPageRanks(filename):
	Ga=nx.read_graphml(sys.argv[1])

	# create a copy of the graph and extract giant component
	# get component size distribution
	cc=nx.connected_components(Ga)
	cc_dict={}
	for x in range(0,len(cc)):
		try:
			cc_dict[len(cc[x])].append(x)
		except KeyError:
			cc_dict[len(cc[x])]=[]
			cc_dict[len(cc[x])].append(x)

	isolates=nx.isolates(Ga)

	rg=nx.fast_gnp_random_graph(Ga.number_of_nodes(),2.0*Ga.number_of_edges()/(Ga.number_of_nodes()*(Ga.number_of_nodes()-1)))
	c_rg=nx.average_clustering(rg)
	rg_cc=nx.connected_component_subgraphs(rg)[0]
	rg_asp=nx.algorithms.shortest_paths.generic.average_shortest_path_length(rg_cc)

	p_rg=community.best_partition(rg_cc)
	m_rg=community.modularity(p_rg,rg_cc)

	pageranks = nx.pagerank_numpy(rg)
	return pageranks
コード例 #10
0
def t_delta_partition(t_delta_matrix,sm,verbose=False):
    import community;
    g=nx.to_networkx_graph(t_delta_matrix+t_delta_matrix.T - np.diag(t_delta_matrix.diagonal()) ,create_using=nx.Graph()); 
    if verbose==True:
        plt.figure, plt.pcolor(np.array(nx.to_numpy_matrix(g))), plt.colorbar();
        plt.show()
    return community.best_partition(g);
コード例 #11
0
ファイル: CreateGraph.py プロジェクト: cpsnowden/GDOTwitter
def testGraph(fileName, r=1):
    G = nx.read_graphml(fileName)
    partitions = community.best_partition(G, resolution=r)

    inv_map = {}
    for k, v in partitions.iteritems():
        inv_map[v] = inv_map.get(v, [])
        inv_map[v].append(k)

    intra_community_distance = {}
    for c in inv_map:
        intra_community_distance[c] = get_inter_nodes_distance(G, inv_map[c])
        inter_community_distance = {}

    for c1, c2 in combinations(inv_map.keys(), 2):
        inter_community_distance[(c1, c2)] = get_intra_community_distance(G,
                                                                          inv_map[c1],
                                                                          inv_map[c2])

    inter_node_ratios = {}
    for c1, c2 in inter_community_distance:
        inter_node_ratios[(c1, c2)] = np.power(inter_community_distance[(c1, c2)], 2) / (
            intra_community_distance[c1] * intra_community_distance[c2]
        )

    pprint.pprint(intra_community_distance)
    pprint.pprint(inter_community_distance)

    # pprint.pprint(inter_node_ratios)

    return np.average(inter_node_ratios.values()), np.std(inter_node_ratios.values())
コード例 #12
0
ファイル: graph_utils.py プロジェクト: tvzeller/SumProj
def add_metrics(g):
	"""
	Adds centrality metrics and community number attributes to each node in the given graph.
	Returns the graph with new node attributes.
	"""
	# Each function returns a dict keyed by node id with the computed metric as value
	deg_cent = nx.degree_centrality(g)
	close_cent = nx.closeness_centrality(g)
	between_cent = nx.betweenness_centrality(g)
	com = community.best_partition(g)
	# Only interested in communities with more than one member - get a list
	# of multimember communities, sorted by community number
	sorted_coms = get_sorted_multimember_coms(com)

	# Loop through nodes in the graph and give them new attributes
	for vertex in self.graph.node.keys():
		g.node[vertex]["deg_cent"] = deg_cent[vertex]
		g.node[vertex]["close_cent"] = close_cent[vertex]
		g.node[vertex]["between_cent"] = between_cent[vertex]

		# Only nodes in a multimember community get a community number
		if com[vertex] in sorted_coms:
			# So community numbers start at 1, change community numbers to their position in the sorted_coms
			# list, plus 1
			# e.g. first multimember community number may be 3, this makes it 0 (position in list) + 1
			new_com_num = sorted_coms.index(com[vertex]) + 1
			g.node[vertex]["com"] = new_com_num
		# If node not in a multimember community, gets False as com number attribute
		else:
			g.node[vertex]["com"] = False

	return g
コード例 #13
0
def InitClusterAnalysis(graph):
    global comMemClean
    global comMemNames
    global comsizeClean
    global partition
    print "starting best partition algorithm (will take a while)...."
    partition = community.best_partition(graph)
    modularity = community.modularity(partition, graph)
    LogPrint("the modularity is %f"%modularity)
    if partition !=None:
        for node in partition.iteritems():
            if comSize.has_key(node[1]):
                comSize[node[1]]= comSize[node[1]]+1
                comMem[node[1]].append(node[0])
            else:
                comSize[node[1]]=1
                comMem[node[1]]=[]
    for cSize in comSize.iteritems():
        if cSize[1] >1:
            print "cSize[1]=",cSize[1]
            comsizeClean[cSize[0]] =cSize[1]
            if len(comMem[cSize[0]])==1:
                print "way this value is only one member...",comMem[cSize[0]]
            comMemClean[cSize[0]] = comMem[cSize[0]]
    
    for memberIDs in comMemClean.iteritems():
        comMemNames[memberIDs[0]]=[]
        for member in memberIDs[1]:
            comMemNames[memberIDs[0]].append(utils.GetNodeName(member,graph))  
コード例 #14
0
ファイル: Cluster.py プロジェクト: ahanden/Bioinference
def louvain(graph):
    """Computes clusters using the Louvain algorithm

    Parameters
    ----------
    graph : A NetworkX Graph to cluster. This object will also be modified.
            Nodes will gain a new attribute called 'cluster' indicating which
            cluster it belongs to.

    Returns
    -------
    A dictionary of clusters to node lists
    """

    try:
        clust = community.best_partition(graph)    # attempt louvain method
    except:
        clust = {}                # if clustering fails, assign all nodes to the same cluster
        for x in graph:
            clust[x] = 0

    clustDict = {}
    for x in clust:
        graph.node[x]['cluster'] = clust[x] # tag nodes by clusterID
        if clust[x] in clustDict: # rework dictionary
            clustDict[clust[x]].append(x)
        else:
            clustDict[clust[x]] = [x]

    return clustDict
コード例 #15
0
ファイル: graphAnalys.py プロジェクト: WingYn/DtuJobBot
def groupGraph(G, userNodeId):
    """docstring for groupGraph"""
    G.node[userNodeId]['group'] = 0
    Gc = nx.Graph(G)
    Gc.remove_node(userNodeId)
    
    if len(Gc.edges()) < 1:
        partition = {}
        for n in Gc.nodes():
            partition[n] = 1
    else:
        partition = community.best_partition(Gc)
    
    for nodes in partition.keys():
        G.node[nodes]['group'] = partition[nodes] + 1
    
    #For Connected Sub Graphs
    #Gcc=nx.connected_component_subgraphs(Gc)
    Gcc = []
    for com in set(partition.values()) :
        list_nodes = [nodes for nodes in partition.keys()
                                    if partition[nodes] == com]
        Gcc.append(G.subgraph(list_nodes))
        
    
    for SG in Gcc:
        if len(SG.nodes()) > 3:
            bm, cm, dm = CentralityNoself(SG)
            G.node[bm]['central'] =  1
            #G.node[em]['central'] =  2
            G.node[cm]['central'] =  3
            G.node[dm]['central'] =  4
            
    return G, len(set(partition.values()))
コード例 #16
0
ファイル: convert.py プロジェクト: huihuifan/jq1
def graphmltojson(graphfile, outfile):
	"""
	Converts GraphML file to json while adding communities/modularity groups
	using python-louvain. JSON output is usable with D3 force layout.
	Usage:
	>>> python convert.py -i mygraph.graphml -o outfile.json
	"""
	
	G = nx.read_graphml(graphfile)
	G = nx.Graph(G)
	#G = nx.DiGraph.to_undirected(G)

	#karate = Nexus.get(G)
	#cl = karate.community_fastgreedy()
	#k = 57
	#cl.as_clustering(k).membership

	#finds best community using louvain
	partition = community.best_partition(G)
 
	#adds partition/community number as attribute named 'modularitygroup'
	for n,d in G.nodes_iter(data=True):
		d['group'] = partition[n]
 
	node_link = json_graph.node_link_data(G)
	json = json_graph.dumps(node_link)
	
	# Write to file
	fo = open(outfile, "w")
	fo.write(json);
	fo.close()
コード例 #17
0
def create_3comms_bipartite(n,m,p,No_isolates=True):
    
    import community as comm

    from networkx.algorithms import bipartite as bip
    u=0
    while  True:
        G=nx.bipartite_random_graph(n,m,p)
        list_of_isolates=nx.isolates(G)
        if No_isolates:
            G.remove_nodes_from(nx.isolates(G))
        partition=comm.best_partition(G)
        sel=max(partition.values())
        if sel==2 and nx.is_connected(G):
            break
        u+=1
        print u,sel
    ndlss=bip.sets(G)
    ndls=[list(i) for i in ndlss]
    slayer1=ndls[0]
    slayer2=ndls[1]
    layer1=[i for i,v in partition.items() if v==0]
    layer2=[i for i,v in partition.items() if v==1]
    layer3=[i for i,v in partition.items() if v==2]
    edgeList=[]
    for e in G.edges():
        if (e[0] in slayer1 and e[1] in slayer2) or (e[0] in slayer2 and e[1] in slayer1):
            edgeList.append(e)
    return G,layer1,layer2,layer3,slayer1,slayer2,edgeList,partition
コード例 #18
0
ファイル: cluster.py プロジェクト: Ragnara5799/SIFTA
def saveCluster(outPutDirectory, subGraphs, idToApps, edgeLimit, outputName ):
    counter = 0
    
    for graph in subGraphs:
        if graph.number_of_nodes() > 1:
            output = open(str(outPutDirectory) + "/subGraphNodesFiltered_" + outputName + "_" + str(int(edgeLimit)) + "_" + str(counter) + ".txt", 'w')
            for node in graph.nodes():
                output.write(str(idToApps[node]) + "\n")
            output.close()
            counter += 1
        if graph.number_of_nodes() > 50:
            partition = community.best_partition(graph)
            counter2 = 0
            processing = 1
            while(processing == 1):
                processing = 0
                for p in partition:
                    if partition[p] == counter2:
                        processing = 1
                if processing == 1:
                    output2 = open(str(outPutDirectory) + "/subGraphNodesFilteredBigCluster_"+ outputName + "_" + str(int(edgeLimit)) + "_" + str(counter2) + ".txt", 'w')
                    for p in partition:
                        if partition[p] == counter2:
                            output2.write(str(idToApps[p]) + "\n")
                    output2.close()
                counter2 += 1
コード例 #19
0
ファイル: graph.py プロジェクト: gt-elc-lab/george
    def _with_networkx(documents, threshold=1):
        G = nx.Graph()
        G.add_nodes_from(documents)
        nodes = G.nodes()
        for i, node in enumerate(nodes):
            for other in nodes[i+1:]:
                a = set(node.keywords)
                b = set(other.keywords)
                intersection = a.intersection(b)
                if len(intersection) > threshold:
                    G.add_edge(node, other)
                    G[node][other]['weight'] = len(intersection)

        # remove any isolated vertices before we perform community detection
        orphans = []
        for node in G.nodes():
            if not G.neighbors(node):
                G.remove_node(node)
                orphans.append(node)
        partition_lookup = community.best_partition(G).iteritems()
        G.add_nodes_from(orphans)
        partitions = {node.r_id: value for node, value in partition_lookup}
        as_json = json_graph.node_link_data(G)
        frontend_compatable = {}
        frontend_compatable['nodes'] = [node['id'] for node in as_json['nodes']]
        for node in frontend_compatable['nodes']:
            if G.neighbors(node):
                node.partition = partitions[node.r_id]
        frontend_compatable['nodes'] = [json.loads(node.to_json()) for node in frontend_compatable['nodes']]
        for node in frontend_compatable['nodes']:
            if node['_id'] in partitions:
                node['partition'] = partitions[node['_id']]
        frontend_compatable['edges'] = as_json['links']
        return frontend_compatable
コード例 #20
0
ファイル: TBMoviesGraphs.py プロジェクト: Lordie12/Research
def main():

    genedict = read_raw_csv(finalURL)

    filterGenes = json.load(open(filterURL, 'r'))

    tempdict = {}
    gtoidict = {}
    itogdict = {}

    for movie in genedict.keys():
        for gene in genedict[movie]['Genes']:
            if gene in filterGenes:
                continue
            gene = str(gene).lower()
            tempdict[gene] = 0

    count = 0
    for gene in tempdict.keys():
        gtoidict[gene] = count
        itogdict[count] = gene
        count += 1

    G = fill_Graph(genedict, count, gtoidict, itogdict, filterGenes)
    mat = NX.to_numpy_matrix(G)
    res = C.best_partition(G)

    get_components_of_G(mat, res, gtoidict, itogdict)
コード例 #21
0
ファイル: louvain.py プロジェクト: Huarong/people_clustering
 def cluster(self):
     #first compute the best partition
     partition = community.best_partition(self.G)
     # print partition
     category = [(c,) for i, c in partition.items()]
     # print category
     return category
コード例 #22
0
def analyze_graph(G):    
    #centralities and node metrics
    out_degrees = G.out_degree()
    in_degrees = G.in_degree()
    betweenness = nx.betweenness_centrality(G)
    eigenvector = nx.eigenvector_centrality_numpy(G)
    closeness = nx.closeness_centrality(G)
    pagerank = nx.pagerank(G)
    avg_neighbour_degree = nx.average_neighbor_degree(G)
    redundancy = bipartite.node_redundancy(G)
    load = nx.load_centrality(G)
    hits = nx.hits(G)
    vitality = nx.closeness_vitality(G)
    
    for name in G.nodes():
        G.node[name]['out_degree'] = out_degrees[name]
        G.node[name]['in_degree'] = in_degrees[name]
        G.node[name]['betweenness'] = betweenness[name]
        G.node[name]['eigenvector'] = eigenvector[name]
        G.node[name]['closeness'] = closeness[name]
        G.node[name]['pagerank'] = pagerank[name]
        G.node[name]['avg-neigh-degree'] = avg_neighbour_degree[name]
        G.node[name]['redundancy'] = redundancy[name]
        G.node[name]['load'] = load[name]
        G.node[name]['hits'] = hits[name]
        G.node[name]['vitality'] = vitality[name]
        
    #communities
    partitions = community.best_partition(G)
    for member, c in partitions.items():
        G.node[member]['community'] = c   
    
    return G
コード例 #23
0
ファイル: cluster.py プロジェクト: saltfog/r-code
def detect_communities(graph, verbose=False):
    graph = graph_from_csv(graph)
    partition = community.best_partition(graph)
    if verbose:
        print "%i partitions" % len(set(partition.values()))
    nx.set_node_attributes(graph, 'partition', partition)
    return graph, partition
コード例 #24
0
ファイル: test_community.py プロジェクト: cole-maclean/MAI-CN
    def test_karate(self):
        """"test modularity on Zachary's karate club"""
        graph = nx.karate_club_graph()
        part = co.best_partition(graph)
        self.assertTrue(co.modularity(part, graph) > 0.41)

        for e1, e2 in graph.edges_iter():
            graph[e1][e2]["test_weight"] = 1.

        part_weight = co.best_partition(graph, weight="test_weight")
        self.assertAlmostEqual(co.modularity(part, graph),
                               co.modularity(part_weight, graph, "test_weight"))

        part_res_low = co.best_partition(graph, resolution=0.1)
        self.assertTrue(
            len(set(part.values())) < len(set(part_res_low.values())))
コード例 #25
0
 def data(self, **kw):
     try:
         with closing(open('cache.json', 'r')) as data_file:
             print 'Reading from cache'
             return data_file.read()
     except IOError:
         print 'Fetching data'
         with closing(open('cache.json', 'w')) as data_file:
             foaf_graph = None
             try:
                 with closing(open('graph_cache.json', 'r')) as graph_file:
                     print 'Reading from graph cache'
                     foaf_graph = jg.load(graph_file)
             except IOError:
                 foaf_graph = retrieve_foaf(FBTOKEN)
             clusters = community.best_partition(foaf_graph)
             degree_distribution = get_histograms(foaf_graph)
             cluster_counts = get_cluster_counts(clusters)
             top10 = get_top_degree(foaf_graph, 10)
             foaf_json_graph = json.loads(jg.dumps(foaf_graph))
             ob = foaf_graph.degree()
             infos = {
                 'graph':foaf_json_graph,
                 'clusters':clusters,
                 'cluster_counts':cluster_counts,
                 'degree_distribution':degree_distribution,
                 'degree':foaf_graph.degree(),
                 'top10':top10
             }
             foaf_data = json.dumps(infos)
             data_file.write(foaf_data)
             return foaf_data
コード例 #26
0
ファイル: desGraphics.py プロジェクト: wilseypa/desMetrics
def plot_modularity(G):
	outFile = outDir + 'communities'
	modularity = collections.Counter()
	mod = community.best_partition(G)
	modList = mod.values()

	for i in np.arange(len(modList)):
		modularity[modList[i]] += 1

	mean = np.mean(modularity.values())
	std_dev = np.std(modularity.values())
	start = min(modularity.keys(), key=int)
	end = max(modularity.keys(), key=int)

	fig, ax = pylab.subplots()
	ax.scatter(modularity.keys(),modularity.values(),color=colors[0])
	pylab.axhline(mean,color=colors[2],label="mean")
	pylab.axhline(mean+std_dev,color=colors[1],label="standard deviation")
	pylab.axhline(mean-std_dev,color=colors[1])
	ax.set_ylabel('Number of LPs')
	ax.set_xlabel('Modularity Class')
	ax.ticklabel_format(useOffset=False)
	ya = ax.get_yaxis()
	ya.set_major_locator(pylab.MaxNLocator(integer=True))
	pylab.xticks(np.arange(start, end+1,10)) # change 10 to 1 (or smaller number) if # of communities is small
	pylab.title('Communities in LP Communication Graph')
	pylab.legend(loc='best', shadow=True)
	display_graph(outFile)
コード例 #27
0
def get_topics_noun_phrases(num_news, draw=False, url='http://cnn.com'):

    texts = get_news(url, num_news)

    gb = NounPhraseGraphBuilder(text_processing.clean_punctuation_and_stopwords)
    gb.load_texts(texts)
    G = gb.create_graph()
    print "Graph built"

    partition = community.best_partition(G)
    words_by_part = get_words_by_partition(partition)

    print_topics_from_partitions(G, words_by_part, 10)

    mod = community.modularity(partition,G)
    print("modularity:", mod)

    #print_topics_from_partitions(G, words_by_part, 10)
    if draw:
        values = [partition.get(node) for node in G.nodes()]
        nx.draw_spring(G, cmap = plt.get_cmap('jet'), node_color = values, node_size=30, with_labels=False)
        plt.show()

    topics = get_topics_from_partitions(G, words_by_part, 10)

    return G, topics
コード例 #28
0
def evaluate():

    texts = get_texts()
    gb = words_graph.SimpleGraphBuilder(text_processing.clean_punctuation_and_stopwords, stem_words=False)
    #gb = words_graph.SimpleGraphBuilder(text_processing.only_non_dictionary_words, stem_words=False)
    #gb = words_graph.WindowGraphBuilder(text_processing.clean_punctuation_and_stopwords, stem_words=False)
    #gb = words_graph.NounPhraseGraphBuilder(text_processing.only_non_dictionary_words, stem_words=False)

    gb.load_texts(texts)
    G = gb.create_graph()

    partition = community.best_partition(G)
    #words_by_part = topics.get_words_by_partition(partition)
    words_by_part = graph_cluster.get_overlap_clusters(G, 9, 1)

    computed_topics = topics.get_topics_from_partitions(G, words_by_part)

    #Word splitter
    # computed_topics2 = []
    # for topic in computed_topics:
    #     new_topic = []
    #     for phrase in topic:
    #         new_topic.extend(phrase.split(' '))
    #     computed_topics2.append(new_topic)

    print compute_score(computed_topics, true_topics)
コード例 #29
0
    def detect_communities(self):
        partition = community.best_partition(self.G)
        for n in partition:
            nx.set_node_attributes(self.G, 'community', {n: partition[n]})

        self.l.append("community")
        return self
コード例 #30
0
def prepare_network(df):
    df.set_index('yearID', inplace=True)

    # Create co-occurrence matrix
    cooc = df.dot(df.T) * (1 - np.eye(df.shape[0]))
    cooc.to_csv('cooc.csv')

    slicing = 3
    weights = cooc[cooc >= slicing]
    weights = weights.stack()
    weights = weights / weights.max()
    cd_network = weights.to_dict()
    cd_network = {key: float(value) for key, value in cd_network.items()}

    player_network = nx.Graph()
    player_network.add_edges_from(cd_network)
    nx.set_edge_attributes(player_network, 'weight', cd_network)

    partition = community.best_partition(player_network)
    nx.set_node_attributes(player_network, 'part', partition)

    if not os.path.isdir('results'):
        os.mkdir('results')

    with open('results/player_network.graphml', 'wb') as ofile:
        nx.write_graphml(player_network, ofile)
    return