def setup(self): # build an ER network self.ER_G = zn.Graph() for i in range(self.NUM_NODES): self.ER_G.add_node(i) # add edges for i in range(self.NUM_NODES): for j in range(i + 1, self.NUM_NODES): if random.random() < self.P: self.ER_G.add_edge_(i, j)
def makeBattleGraph(selectYear,selectByFatality,startYear=1997,endYear=2015): #print startYear, endYear fail_count = 0 ## Create the graph of interest acled_graph = zen.Graph() #This function adds an edge if not present, adds weight if edge is present: def add_weight(G,nodeA,nodeB,myValue): if G.has_edge(nodeA,nodeB): myWeight = G.weight(nodeA,nodeB)+myValue G.set_weight(nodeA,nodeB,myWeight) else: G.add_edge(nodeA,nodeB,weight=myValue) with open(sourceFile) as mycsv: reader = csv.DictReader(mycsv) for row in reader: #By default include row for processing to_process = True #Following statements change to_process depending on conditions #print row['YEAR'], len(row['YEAR']), if selectYear != "Y": if int(row['YEAR'])> endYear or int(row['YEAR'])<startYear: to_process = False #Do not include if actor1/2 are "NA" (only two-parties please) if row['ACTOR1']== 'NA' or row['ACTOR2'] == 'NA' or row['ACTOR2'] =="": to_process = False #Do not include self-edges if row['ACTOR1']==row['ACTOR2']: to_process = False #Filter away non-battle events if to_process and 'Battle' in row['EVENT_TYPE']: #Add the edge if selectByFatality==1: add_weight(acled_graph,row['ACTOR1'],row['ACTOR2'],1) else: deathCount = int(row['FATALITIES']) if deathCount > 0: add_weight(acled_graph,row['ACTOR1'],row['ACTOR2'],deathCount) else: fail_count += 1 #print "Number of nodes in generated graph:",acled_graph.num_nodes #print fail_count return acled_graph
def setup(self): # build an ER network self.ER_G = zn.Graph() for i in range(self.NUM_NODES): self.ER_G.add_node(i) # add edges for i in range(self.NUM_NODES): for j in range(i+1,self.NUM_NODES): if random.random() < self.P: self.ER_G.add_edge_(i,j) # generate some endpoints self.sources = random.sample(range(self.NUM_NODES),self.NUM_SOURCES)
def bibliography(R): R_bb = zen.Graph() n = R.num_nodes for l in range(n): R_bb.add_node(R.node_object(l)) for i in range(n): nbrs_i = set(R.out_neighbors_(i)) for j in range(i + 1, n): nbrs_j = set(R.out_neighbors_(j)) common = nbrs_i.intersection(nbrs_j) if len(common) > 0: R_bb.add_edge(R.node_object(i), R.node_object(j), weight=len(common)) return R_bb
def setup(self): # build a network self.ER_G = zn.Graph() for i in range(self.NUM_NODES): self.ER_G.add_node(i) # add edges for i in range(self.NUM_NODES): for j in range(i+1,self.NUM_NODES): r = random.random() w = random.random() if r < self.P: w = math.ceil(w*10.0) self.ER_G.add_edge_(i,j, weight=w) self.weights.append(w)
def load_json_net(fname): data = json.load(open(fname, 'r')) nodes_list = data['nodes'] char_id2name = { i: x['name'].replace(' ', '_') for i, x in enumerate(nodes_list) } G = zen.Graph() for e in data['links']: G.add_edge(char_id2name[e['source']], char_id2name[e['target']], weight=e['value']) return G
def test_write_undirected(self): G = zen.Graph() G.add_nodes(5) G.add_edge_(0, 1) G.add_edge_(1, 2) G.add_edge_(3, 4) fd, fname = tempfile.mkstemp() os.close(fd) memlist.write(G, fname) G2 = memlist.read(fname, directed=False) self.assertEqual(len(G2), len(G)) self.assertEqual(G2.size(), G.size())
def build_graph(self, fname, directed, weighted): command = ("wc -l %s" % fname) process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=None, shell=True) output = process.communicate() graph_edge_capacity = int(output[0].split()[0]) + 1 G = zen.edgelist.read(fname, weighted=weighted, ignore_duplicate_edges=True, merge_graph=zen.Graph( directed=directed, edge_capacity=graph_edge_capacity, edge_list_capacity=1)) return G
def cocitation(G): G1 = zen.Graph() g = G.nodes() H = 0 H1 = 0 e = 0 j = 0 y = 0 for i in range(0, len(g)): G1.add_node(g[i]) for j in range(0, len(g)): u = G.neighbors(g[j]) r = len(u) if r == 0: r = r + 1 for y in range(0, len(g)): w = [] v = G.neighbors(g[y]) p = len(v) if p == 0: p = p + 1 for s in range(0, r): if len(u) == 0: continue H = G.weight(u[s], g[j]) for e in range(0, p): if len(v) == 0: continue H1 = G.weight(v[e], g[y]) if len(u) == 0: continue if len(v) == 0: continue if (v[e] == u[s]): w.append(H * H1) count = numpy.sum(w) if G1.has_edge(g[j], g[y]) == True: G1.set_weight(g[j], g[y], count) continue if g[j] == g[y]: continue G1.add_edge(g[j], g[y], weight=count) #X=G1.matrix() #numpy.fill_diagonal(X,0) #print X.sum().sum() return G1
def makeGraph(selectYear,selectByFatality,startYear=1997,endYear=2015): #print startYear, endYear fail_count = 0 ## Create the graph of interest acled_graph = zen.Graph() with open(sourceFile) as mycsv: reader = csv.DictReader(mycsv) for row in reader: #By default include row for processing to_process = True #Following statements change to_process depending on conditions #print row['YEAR'], len(row['YEAR']), if selectYear != "Y": if int(row['YEAR'])> endYear or int(row['YEAR'])<startYear: to_process = False #Do not include if actor1/2 are "NA" (only two-parties please) if row['ACTOR1']== 'NA' or row['ACTOR2'] == 'NA' or row['ACTOR2'] =="": to_process = False #Do not include self-edges if row['ACTOR1']==row['ACTOR2']: to_process = False if to_process: #Add the edge if selectByFatality==1: add_weight(acled_graph,row['ACTOR1'],row['ACTOR2'],1) else: deathCount = int(row['FATALITIES']) if deathCount > 0: add_weight(acled_graph,row['ACTOR1'],row['ACTOR2'],deathCount) else: fail_count += 1 #print "Number of nodes in generated graph:",acled_graph.num_nodes #print fail_count return acled_graph
def test_write_compact_undirected(self): G = zen.Graph() G.add_nodes(5) e1 = G.add_edge_(0, 1) G.add_edge_(1, 2) # this should succeed fd, fname = tempfile.mkstemp() os.close(fd) memlist.write(G, fname) # this should fail because the graph isn't compact G.rm_edge_(e1) try: memlist.write(G, fname) self.fail('Writing an uncompact graph should raise an exception') except zen.ZenException: pass
def cocitation(G): CG = zen.Graph() d = [] for i in G.nodes(): if len(G.in_neighbors(i)) != 0: d.append(i) for i in G.nodes(): CG.add_node(i) for i in d: for j in d: if i != j: a = G.in_neighbors(i) b = G.in_neighbors(j) c = set(a).intersection(b) if len(c) != 0: if CG.has_edge(i, j) == False: CG.add_edge(i, j, weight=len(c)) return CG
def bm_zenopt(self): G = zn.Graph() # add nodes G.add_nodes(self.NUM_NODES) # add edges num_nodes = self.NUM_NODES i = 0 ni = 0 nj = 1 while i < self.NUM_EDGES: G.add_edge_(ni, nj) i += 1 nj += 1 if nj >= num_nodes: ni += 1 nj = ni + 1
def bm_zen(self): G = zn.Graph() # add nodes for i in range(self.NUM_NODES): G.add_node(i) # add edges num_nodes = self.NUM_NODES i = 0 ni = 0 nj = 1 while i < self.NUM_EDGES: G.add_edge(ni, nj) i += 1 nj += 1 if nj >= num_nodes: ni += 1 nj = ni + 1
def bm_zenopt(self): G = zn.Graph() # add nodes G.add_nodes(self.NUM_NODES) # add edges num_nodes = self.NUM_NODES i = 0 ni = 0 nj = 1 while i < self.NUM_EDGES: G.add_edge_(ni, nj) i += 1 nj += 1 if nj >= num_nodes: ni += 1 nj = ni + 1 # def bm_zenopt(self): # G = zn.Graph() # # # add nodes # for i in range(self.NUM_NODES): # G.add_node() # # # add edges # num_nodes = self.NUM_NODES # i = 0 # ni = 0 # nj = 1 # while i < self.NUM_EDGES: # G.add_edge_(ni,nj) # i += 1 # # nj += 1 # if nj >= num_nodes: # ni += 1 # nj = ni + 1
def configuration_model(degree_sequence, G1=None): import numpy.random as numpyrandom if G1 is None: G1 = zen.Graph() n = len(degree_sequence) for i in range(n): G1.add_node(i) # this is twice the number of edges, needs to be even assert mod( sum(degree_sequence), 2 ) == 0, 'The number of edges needs to be even; the sum of degrees is not even.' num_edges = sum(degree_sequence) / 2 # the number of edges should be even assert mod(num_edges, 2) == 0, 'The number of edges needs to be even.' stubs = [ nidx for nidx, degree in enumerate(degree_sequence) for d in range(degree) ] stub_pairs = numpyrandom.permutation(num_edges * 2) self_edges = 0 multi_edges = 0 for i in range(num_edges): uidx = stubs[stub_pairs[2 * i]] vidx = stubs[stub_pairs[2 * i + 1]] if uidx == vidx: self_edges += 1 if G1.has_edge_(uidx, vidx): eidx = G1.edge_idx_(uidx, vidx) G1.set_weight_(eidx, G1.weight_(eidx) + 1) multi_edges += 1 else: G1.add_edge_(uidx, vidx) print 'self edges: %i, multi-edges: %i' % (self_edges, multi_edges) return G1
def modular_graph(Size1, Size2, edges1, edges2, common, katz_alpha=0.001): g1 = zen.generating.barabasi_albert(Size1, edges1) avgDeg1 = (2.0 * g1.num_edges) / g1.num_nodes #lcc1 = np.mean(zen.algorithms.clustering.lcc_(g1)) g2 = zen.generating.barabasi_albert(Size2, edges2) avgDeg2 = (2.0 * g2.num_edges) / g2.num_nodes #lcc2 = np.mean(zen.algorithms.clustering.lcc_(g2)) Size = Size1 + Size2 G = zen.Graph() for i in range(Size): G.add_node(i) for edge in g1.edges_iter(): u = edge[0] v = edge[1] G.add_edge(u, v) for edge in g2.edges_iter(): u = edge[0] + Size1 v = edge[1] + Size1 G.add_edge(u, v) # Select random pairs of nodes to connect the subgraphs join_nodes = np.empty((common, 2), dtype=np.int64) nodes1 = np.random.randint(0, Size1, size=common) nodes2 = np.random.randint(Size1, Size, size=common) join_nodes[:, 0] = nodes1 join_nodes[:, 1] = nodes2 for edge in join_nodes: if not G.has_edge(edge[0], edge[1]): G.add_edge(edge[0], edge[1]) return G
# h=open('community.txt','r') # for line in h: # k=line.replace(',','') # K=k.split() # for i in K: # print G.node_data(int(i)) #============================================================================================= print '\nFriendship paradox\n' check_friendship_paradox(G) ############################################################################### G3 = G.copy() print 'CONFIG MODEL**********************************************' H = degree_sequence(G3) G3 = zen.Graph() G3 = configuration_model(H, G3) o, p, C, P, D, J = Stats(G3) print "Config MODEL Number of Nodes %s" % o print "Config MODEL Number of edges %s" % p print "Config MODEL Average Degree %s" % C d = zen.diameter(G3) print 'Network has a diameter of %i.' % d # for i in X: # for j in Y: # numpy.delete(D,i) ############################################################################### ##################################################################### print 'Percolation*************************************************' G2 = G.copy() X = G2.edges_()
with open('raw_twitter.json','rb') as fObj: raw_twitter = fObj.readlines() i = 0 for tweet in raw_twitter[1:]: i += 1 tweet_text = (json.loads(tweet)['text']+' ').lower() hashtags = re.findall(r'\#(.*?)\ ',tweet_text) hashtags = filter(lambda x: x != '',hashtags) if len(hashtags) > 0: with open('hashtag_sets.txt','ab') as fObj: fObj.write(((' '.join(hashtags))+'\n').encode('utf-8')) print '"hashtag_sets.txt" created.' #%% Construct graph from setlist G = zen.Graph() with open('hashtag_sets.txt','rb') as fObj: hashtag_set = fObj.readlines() for ht_set in hashtag_set: hts = ht_set.strip().split(' ') for ht1 in hts: for ht2 in hts: if ht1 != ht2: try: G.add_edge(ht1,ht2,weight=1) except zen.exceptions.ZenException: w = G.weight(ht1,ht2) + 1 G.set_weight(ht1,ht2,w) #zen.io.edgelist.write(G,'hashtags.edgelist',use_weights=True) #,use_node_indices=True)