def test_graph(self): graph = zen.DiGraph() G = zen.generating.local_attachment(20,4,2,graph=graph) self.assertEquals(graph,G) self.assertEquals(len(G),20)
def test_empty_graph(self): graph = zen.DiGraph() graph.add_edge(1,2) try: zen.generating.local_attachment(10,5,2,graph=graph) self.fail('a non-empty graph should not be accepted') except zen.ZenException as e: pass
def test_1(self): G = zen.DiGraph() G.add_edge(1, 2) G.add_edge(3, 4) n = zen.control.num_min_controls(G) self.assertEqual(n, 2)
def test_all_sources(self): G = zen.DiGraph() G.add_edge(1, 2) G.add_edge(2, 3) G.add_edge(4, 5) cp = zen.control.profile(G, normalized=False) self.assertEquals(cp, (2, 0, 0)) cp = zen.control.profile(G) self.assertEquals(cp, (1, 0, 0))
def test_an_external_dilation(self): G = zen.DiGraph() G.add_edge(1, 2) G.add_edge(2, 3) G.add_edge(2, 4) cp = zen.control.profile(G, normalized=False) self.assertEquals(cp, (1, 1, 0)) cp = zen.control.profile(G) self.assertEquals(cp, (0.5, 0.5, 0))
def create_residual_digraph(G, capacity): residual = z.DiGraph() for node in G.nodes(): residual.add_node(nobj=node) if capacity == UNIT_CAPACITY: for u, v in G.edges(): residual.add_edge(u, v, weight=1) else: for u, v, w in G.edges(weight=True): residual.add_edge(u, v, weight=w) return residual
def test_no_controls(self): G = zen.DiGraph() G.add_edge(1, 2) G.add_edge(2, 3) G.add_edge(3, 4) G.add_edge(4, 1) cp = zen.control.profile(G, normalized=False) self.assertEquals(cp, (0, 0, 0)) cp = zen.control.profile(G) self.assertEquals(cp, (0, 0, 0))
def min_cut_(G, sidx, tidx, capacity=UNIT_CAPACITY): """ Compute the min-cut/max-flow of graph ``G`` with source node ``s`` and sink node ``t``. **Args**: * ``G`` (:py:class:`zen.DiGraph`): the graph to compute the flow on. * ``sidx``: the node index of the source node. * ``tidx``: the node index of the target node. * ``capacity [=UNIT_CAPACITY]``: the capacity that each edge has. This value can be either ``UNIT_CAPACITY`` or ``WEIGHT_CAPACITY``. If set to ``UNIT_CAPACITY``, then each edge has the same capacity (of 1). If ``WEIGHT_CAPACITY``, then the weight of each edge is used as its capacity. **Returns**: The weight of the min-cut (also indicating the max-flow). """ if type(G) is not z.DiGraph: raise ZenException, 'min_cut_ only supports DiGraph; found %s.' % type( G) if G.node_object(sidx) == None: raise ZenException, 'the source node is not in the graph;' else: s = G.node_object(sidx) if G.node_object(tidx) == None: raise ZenException, 'the sink node is not in the graph;' else: t = G.node_object(tidx) if capacity != UNIT_CAPACITY and capacity != WEIGHT_CAPACITY: raise ZenException, 'capacity must either be \'unit\' or \'weight\'' #copies the inputted graph, the copy will be used as the residual capacities graph residual_capacities = G.copy() residual_capacities = create_residual_digraph(residual_capacities, capacity) #creates a dummy graph that stores the source node and all of its outgoing edges (to be used for flow calculation) dg = z.DiGraph() dg.add_node(s) for u, v, w in residual_capacities.out_edges(s, weight=True): dg.add_node(v) dg.add_edge(u, v, weight=w) try: #execute the Ford-Fulkerson algorithm to compute the residual capacities graph residual_capacities = ford_fulkerson(residual_capacities, s, t, capacity) except: return float('inf') #the flow through each edge is equal to the original capacity - the residual capacity flows = result_flow(dg, residual_capacities) return sum(flows[u][v] for u, v in dg.out_edges(s))
def test_an_internal_dilation(self): G = zen.DiGraph() G.add_edge(1, 2) G.add_edge(2, 3) G.add_edge(3, 4) G.add_edge(2, 5) G.add_edge(5, 4) cp = zen.control.profile(G, normalized=False) self.assertEqual(cp, (1, 0, 1)) cp = zen.control.profile(G) self.assertEqual(cp, (0.5, 0, 0.5))
def sampleGraph(G, samples): H = zen.DiGraph() for i in samples: for j in samples: if G.has_edge_(i, j): # if G has edge between i and j idx = H.add_edge(G.node_object(i), G.node_object(j)) # add edge to H H.set_weight_(idx, H.weight_( H.edge_idx(G.node_object(i), G.node_object(j)))) # copy weight return H
def posts2mention_network(posts_fname, extract_user_id, extract_mentions, working_dir=None): """ This method builds a valid `mention_network.elist` file from the `posts.json.gz` file specified. Unless indicated otherwise, the directory containing the posts file will be used as the working and output directory for the construction process. `extract_user_id` is a function that accepts a post and returns a string user_id. `extract_mentions` is a function that accepts a post and returns a list of string user_ids mentioned in the post. """ G = zen.DiGraph() # figure out the working dir if not working_dir: working_dir = os.path.dirname(posts_fname) # bin the user data logging.info('building the network') fh = gzip.open(posts_fname, 'r') for line in fh: post = jsonlib.loads(line) uid = extract_user_id(post) mentions = extract_mentions(post) for m in mentions: if G.has_edge(uid, m): G.set_weight(uid, m, G.weight(uid, m) + 1) else: G.add_edge(uid, m, weight=1) # save the graph logging.info('writing network') # TODO: Add compression to this... zen.io.edgelist.write(G, os.path.join(working_dir, 'mention_network.elist'), use_weights=True) # done return
def test_write_directed(self): G = zen.DiGraph() 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=True) self.assertTrue(G2.is_directed()) self.assertEqual(len(G2), len(G)) self.assertEqual(G2.size(), G.size())
def test_write_compact_directed(self): G = zen.DiGraph() 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 test_min_cut(self): #sample graph G = zen.DiGraph() G.add_node('a') G.add_node('b') G.add_node('c') G.add_node('d') G.add_node('e') G.add_node('f') G.add_node('g') G.add_node('h') G.add_edge('a','b',weight=10) G.add_edge('a','c',weight=5) G.add_edge('a','d',weight=15) G.add_edge('b','e',weight=9) G.add_edge('b','f',weight=15) G.add_edge('b','c',weight=4) G.add_edge('c','f',weight=8) G.add_edge('c','d',weight=4) G.add_edge('d','g',weight=30) G.add_edge('g','c',weight=6) G.add_edge('e','f',weight=15) G.add_edge('e','h',weight=10) G.add_edge('f','g',weight=15) G.add_edge('f','h',weight=10) G.add_edge('g','h',weight=10) self.assertEquals(28, zen.min_cut(G,'a','h','weight')) self.assertEquals(3, zen.min_cut(G,'a','h','unit')) G.set_weight('d','g', float('inf')) self.assertEquals(28, zen.min_cut_(G,0,7,'weight')) self.assertEquals(3, zen.min_cut_(G,0,7,'unit')) G.set_weight('a','c', float('inf')) G.set_weight('c','f', float('inf')) G.set_weight('f','h', float('inf')) self.assertEquals(float('inf'), zen.min_cut(G,'a','h','weight')) self.assertEquals(3, zen.min_cut(G,'a','h','unit'))
return def highlight_edges(self, edges): self.highlight_edges_(map(lambda x: self._graph.edge_idx(*x), edges)) def highlight_nodes(self, nodes): self.highlight_nodes_(map(lambda x: self._graph.node_idx(x), nodes)) if __name__ == '__main__': import zen import time logging.basicConfig(level=logging.DEBUG) G = zen.DiGraph() ur = UbigraphRenderer('http://localhost:20738/RPC2') ur.default_node_shape = 'sphere' ur.default_node_color = '#1100dd' ur.graph = G e1 = G.add_edge(1, 2) time.sleep(1) e2 = G.add_edge(2, 3) time.sleep(1) e3 = G.add_edge(3, 4) time.sleep(1) e4 = G.add_edge(1, 4) ur.highlight_edges([(1, 2), (2, 3)]) ur.highlight_nodes([1])
def makeNetwork(dataset, bot_list, gml_dst, error_log): start = time.time() # Get initial time error_flag = 0 G = zen.DiGraph() # Initialize directed graph bots = np.loadtxt(bot_list, dtype=str) # create array of known bot accounts df = pd.read_json(dataset, lines=True) # create dataframe from the comment data set df.set_index('comment_id', inplace=True) # index by comment id for easy searching stats = df.describe() # calculate average upper quartile comment length reddit_scale = np.mean( [stats['comment_len']['max'], stats['comment_len']['75%']]) for commentID in df.index.values: # for each comment username = df['author'][commentID] # get the username parentID = df['parent_id'][ commentID] # get the id of the parent comment try: parent = df['author'][ parentID] # get the usename of the parent commentor if username != '[deleted]' and parent != '[deleted]': #ignore deleted comments (unknown account) # Calculate the information score info_score = df['comment_len'][commentID] info_score += df['gen_link_num'][commentID] * 1100 info_score += df['reddit_link_num'][commentID] * reddit_scale info_score += df['pic_link_num'][commentID] * 500 G.add_edge(username, parent, weight=info_score) # add edge # if username is a known bot, mark the node as such # add /u/ prefix to allow for botname matching if '/u/' + username in bots: G.set_node_data(username, 'bot') # If the edge already exists, add the new information score to the existing edge weight except zen.exceptions.ZenException: w = G.weight(username, parent) + info_score G.set_weight(username, parent, w) # If the parent ID is not in the dataset, don't add an edge except KeyError: pass # If an unanticipated error happens, log it except: err = traceback.format_exc() with open(error_log, 'ab') as fObj: fObj.write(err) fObj.write('\n') # Write GML file zen.io.gml.write(G, gml_dst) duration = time.time() - start duration_hour = duration / 3600 duration_min = (duration_hour - int(duration_hour)) * 60 duration_sec = (duration_min - int(duration_min)) * 60 N = "{:,}".format(G.num_nodes) E = "{:,}".format(G.num_edges) with open('Output.txt', 'wb') as fObj: fObj.write('%s nodes, %s edges.\n' % (N, E)) fObj.write('Processing time: %d hours %d minutes %.2f seconds' % (duration_hour, duration_min, duration_sec)) if error_flag: with open('Output.txt', 'ab') as fObj: fObj.write('\n\nERRORS ENCOUNTERED')
def test_min_cut_set_(self): #sample graph G = zen.DiGraph() G.add_node('a') #node 0 G.add_node('b') G.add_node('c') G.add_node('d') G.add_node('e') #node 4 G.add_node('f') G.add_node('g') G.add_node('h') #node 7 G.add_edge('a','b',weight=10) #edge 0 G.add_edge('a','c',weight=5) G.add_edge('a','d',weight=15) G.add_edge('b','e',weight=9) G.add_edge('b','f',weight=15) G.add_edge('b','c',weight=4) #edge 5 G.add_edge('c','f',weight=8) G.add_edge('c','d',weight=4) G.add_edge('d','g',weight=30) G.add_edge('g','c',weight=6) G.add_edge('e','f',weight=15) #edge 10 G.add_edge('e','h',weight=10) G.add_edge('f','g',weight=15) G.add_edge('f','h',weight=10) G.add_edge('g','h',weight=10) #edge 14 cut_set = zen.min_cut_set_(G,0,7,'weight') self.assertEquals(3, len(cut_set)) self.assertTrue(0 in cut_set) self.assertTrue(6 in cut_set) self.assertTrue(14 in cut_set) cut_set = zen.min_cut_set_(G,0,7,'unit') self.assertEquals(3, len(cut_set)) self.assertTrue(0 in cut_set) self.assertTrue(1 in cut_set) self.assertTrue(2 in cut_set) G.set_weight('d','g', float('inf')) cut_set = zen.min_cut_set_(G,0,7,'weight') self.assertEquals(3, len(cut_set)) self.assertTrue(0 in cut_set) self.assertTrue(6 in cut_set) self.assertTrue(14 in cut_set) cut_set = zen.min_cut_set_(G,0,7,'unit') self.assertEquals(3, len(cut_set)) self.assertTrue(0 in cut_set) self.assertTrue(1 in cut_set) self.assertTrue(2 in cut_set) G.set_weight('a','c', float('inf')) G.set_weight('c','f', float('inf')) G.set_weight('f','h', float('inf')) cut_set = zen.min_cut_set_(G,0,7,'weight') self.assertEquals(3, len(cut_set)) self.assertTrue(0 in cut_set) self.assertTrue(1 in cut_set) self.assertTrue(2 in cut_set) cut_set = zen.min_cut_set_(G,0,7,'unit') self.assertEquals(3, len(cut_set)) self.assertTrue(0 in cut_set) self.assertTrue(1 in cut_set) self.assertTrue(2 in cut_set)
def test_min_cut_set(self): #sample graph G = zen.DiGraph() G.add_node('a') G.add_node('b') G.add_node('c') G.add_node('d') G.add_node('e') G.add_node('f') G.add_node('g') G.add_node('h') G.add_edge('a','b',weight=10) G.add_edge('a','c',weight=5) G.add_edge('a','d',weight=15) G.add_edge('b','e',weight=9) G.add_edge('b','f',weight=15) G.add_edge('b','c',weight=4) G.add_edge('c','f',weight=8) G.add_edge('c','d',weight=4) G.add_edge('d','g',weight=30) G.add_edge('g','c',weight=6) G.add_edge('e','f',weight=15) G.add_edge('e','h',weight=10) G.add_edge('f','g',weight=15) G.add_edge('f','h',weight=10) G.add_edge('g','h',weight=10) cut_set = zen.min_cut_set(G,'a','h','weight') self.assertEquals(3, len(cut_set)) self.assertTrue(('a','b') in cut_set) self.assertTrue(('c','f') in cut_set) self.assertTrue(('g','h') in cut_set) cut_set = zen.min_cut_set(G,'a','h','unit') self.assertEquals(3, len(cut_set)) self.assertTrue(('a','b') in cut_set) self.assertTrue(('a','c') in cut_set) self.assertTrue(('a','d') in cut_set) G.set_weight('d','g', float('inf')) cut_set = zen.min_cut_set(G,'a','h','weight') self.assertEquals(3, len(cut_set)) self.assertTrue(('a','b') in cut_set) self.assertTrue(('c','f') in cut_set) self.assertTrue(('g','h') in cut_set) cut_set = zen.min_cut_set(G,'a','h','unit') self.assertEquals(3, len(cut_set)) self.assertTrue(('a','b') in cut_set) self.assertTrue(('a','c') in cut_set) self.assertTrue(('a','d') in cut_set) G.set_weight('a','c', float('inf')) G.set_weight('c','f', float('inf')) G.set_weight('f','h', float('inf')) cut_set = zen.min_cut_set(G,'a','h','weight') self.assertEquals(3, len(cut_set)) self.assertTrue(('a','b') in cut_set) self.assertTrue(('a','c') in cut_set) self.assertTrue(('a','d') in cut_set) cut_set = zen.min_cut_set(G,'a','h','unit') self.assertEquals(3, len(cut_set)) self.assertTrue(('a','b') in cut_set) self.assertTrue(('a','c') in cut_set) self.assertTrue(('a','d') in cut_set)