class Test(unittest.TestCase): def setUp(self): self.graph = Graph() self.graph.debug = False self.graph.input_debug_links = 1 self.graph.output_debug_nodes = 1 self.graph.load_edgelist(StringIO(example_txt), num=True) def tearDown(self): pass def testPrinter(self): printer = Printer() result = '\n'.join(printer.visit(self.graph)) self.assertEqual( result, 'NODE: 1\nNODE: 2\nNODE: 3\nNODE: 4\nNODE: 5\nNODE: 6\nNODE: 7\nNODE: 8\nNODE: 9\nNODE: 666\nEDGE: 1 -- 2 ATTRS: \nEDGE: 3 -- 5 ATTRS: \nEDGE: 4 -- 5 ATTRS: \nEDGE: 6 -- 7 ATTRS: \nEDGE: 7 -- 8 ATTRS: \nEDGE: 7 -- 9 ATTRS: \nEDGE: 8 -- 9 ATTRS: \nEDGE: 9 -- 666 ATTRS: ' ) def testDFSBot(self): bot = Printer(None, DFSBot()) result = '\n'.join(bot.visit(self.graph)) self.assertEqual( result, 'NODE: 666\nEDGE: 666 -- 9 ATTRS: \nNODE: 9\nEDGE: 9 -- 8 ATTRS: \nEDGE: 9 -- 666 ATTRS: \nEDGE: 9 -- 7 ATTRS: \nNODE: 7\nEDGE: 7 -- 8 ATTRS: \nEDGE: 7 -- 9 ATTRS: \nEDGE: 7 -- 6 ATTRS: \nNODE: 6\nEDGE: 6 -- 7 ATTRS: \nNODE: 8\nEDGE: 8 -- 9 ATTRS: \nEDGE: 8 -- 7 ATTRS: \nNODE: 8\nEDGE: 8 -- 9 ATTRS: \nEDGE: 8 -- 7 ATTRS: ' ) def testBFSBot(self): bot = Printer(None, BFSBot()) result = '\n'.join(bot.visit(self.graph)) self.assertEqual( result, 'NODE: 666\nEDGE: 666 -- 9 ATTRS: \nNODE: 9\nEDGE: 9 -- 8 ATTRS: \nEDGE: 9 -- 666 ATTRS: \nEDGE: 9 -- 7 ATTRS: \nNODE: 8\nEDGE: 8 -- 9 ATTRS: \nEDGE: 8 -- 7 ATTRS: \nNODE: 7\nEDGE: 7 -- 8 ATTRS: \nEDGE: 7 -- 9 ATTRS: \nEDGE: 7 -- 6 ATTRS: \nNODE: 7\nEDGE: 7 -- 8 ATTRS: \nEDGE: 7 -- 9 ATTRS: \nEDGE: 7 -- 6 ATTRS: \nNODE: 6\nEDGE: 6 -- 7 ATTRS: \nNODE: 6\nEDGE: 6 -- 7 ATTRS: ' )
def setUp(self): self.digraph = BigDiGraph() self.digraph.debug = False self.digraph.input_debug_links = 1 self.digraph.output_debug_nodes = 1 self.digraph.load_edgelist(StringIO(example_txt), num=False) self.graph = Graph()
def setUp(self): self.graph = Graph() self.graph.debug = False self.graph.input_debug_links = 50000 self.graph.max_links_input = 25000 self.graph.output_debug_nodes = 10000 use_big_alphabet = False self.graph.load_compressed_graph(cloudlight.tests.data_enc1, use_big_alphabet)
def setUp(self): self.graph = Graph() self.graph.debug = False self.graph.input_debug_links = 500000 self.graph.output_debug_nodes = 10000 use_big_alphabet = False self.graph.load_compressed_graph(cloudlight.tests.data_enc1, use_big_alphabet) random.seed(666) self.nodes = self.graph.random_nodes(10000)
def testGraphHistogram(self): self.graph = Graph() self.graph.debug = False self.graph.input_debug_links = 200000 self.graph.output_debug_nodes = 10000 self.graph.load_edgelist(StringIO(example_txt2)) degrees = list(self.graph.degrees()) p = Plot() p.clear() p.hist(degrees, 15, True, True, False)
def setUp(self): self.graph = Graph() self.graph.debug = False self.graph.max_links_input = 50000 self.graph.input_debug_links = 1000 #200000 self.graph.output_debug_nodes = 10000 use_big_alphabet = False self.graph.load_compressed_graph(cloudlight.tests.data_enc1, use_big_alphabet, has_num=True) self.graph.create_indices()
def visitTwitterNode(self, node, *args): print 'Visitando nodo twitter: id = %s, user = %s' % (node.num_id, node.screen_name) tw_local_graph = Graph() friends = self.twitter_browser.friends(node) for f in friends: tw_local_graph.add_edge(node, f) return tw_local_graph
def testPlotSave(self): self.graph = Graph() self.graph.debug = False self.graph.input_debug_links = 200000 self.graph.output_debug_nodes = 10000 self.graph.load_edgelist(StringIO(example_txt)) clusts = list(self.graph.eccentricities()) clusts p = Plot() p.clear() p.hist(clusts, 3, True, True, True) p.save('testPlotSave.png')
def visitFacebookNode(self, node, *args): print 'Visitando nodo facebook: id = %s, name = %s' % (node.id, node.name) fb_local_graph = Graph() friends = self.facebook_browser.friends(node) for f in friends: print node, f fb_local_graph.add_edge(node, f) return fb_local_graph
class Test(unittest.TestCase): def setUp(self): self.graph = Graph() self.graph.debug = False self.graph.max_links_input = 100000 self.graph.input_debug_links = 200000 self.graph.output_debug_nodes = 10000 def testLoadCompressed1(self): use_big_alphabet = False self.graph.load_compressed_graph(cloudlight.tests.data_enc1, use_big_alphabet) self.assertEqual(self.graph.number_of_nodes(), 43948) self.assertEqual(self.graph.number_of_edges(), 50000) def testLoadCompressed2(self): use_big_alphabet = True self.graph.load_compressed_graph(cloudlight.tests.data_enc2, use_big_alphabet) self.assertEqual(self.graph.number_of_nodes(), 43948) self.assertEqual(self.graph.number_of_edges(), 50000) def tearDown(self): pass
class Test(unittest.TestCase): def setUp(self): self.graph = Graph() self.graph.debug = False self.graph.max_links_input = 50000 self.graph.input_debug_links = 1000 #200000 self.graph.output_debug_nodes = 10000 use_big_alphabet = False self.graph.load_compressed_graph(cloudlight.tests.data_enc1, use_big_alphabet, has_num=True) self.graph.create_indices() def tearDown(self): pass def testPickleDump(self): output = StringIO() self.graph.index_all_parameters() self.graph.pickle_dump(output) obj_rep = output.getvalue() #self.assertEqual( len(obj_rep), 1650653 ) graph = cPickle.loads(obj_rep) self.assertEqual(graph.number_of_nodes(), 43948) self.assertEqual(graph.number_of_edges(), 50000) output.close()
def testVisitFriends(self): g = Graph() g.add_node(self.node) friends = self.node.friends() self.assertEqual( set(map(str, friends)), set([ '3108351', '26601797', '18149408', '19253848', '36544954', '75970385', '18114931', '55463984', '19732920', '42917391', '25566068', '25925954', '44674512', '19305701', '45712556' ])) self.g = g
def testComplexTwitterBot(self): mynodemap = { str(TwitterNode('AAA').__class__): TwitterNode.visit_friends } bot = Transform(mynodemap, None, Constructor()) graph = Graph() graph.add_node(TwitterNode('therm000')) list(bot.visit(graph)) self.assertTrue( 'therm000' in map(str, bot.decoratedBot.new_graph.nodes())) self.assertTrue( '89945612' in map(str, bot.decoratedBot.new_graph.nodes()))
def setUp(self): self.graph = Graph() self.graph.debug = False self.graph.max_links_input = 100000 self.graph.input_debug_links = 200000 self.graph.output_debug_nodes = 10000 use_big_alphabet = False self.graph.load_compressed_graph(cloudlight.tests.data_enc1, use_big_alphabet) self.graph.create_index_degree() self.graph.create_index_unseen_degree() lookahead = 0 coverage = 'link' self.strategies = PrivacyAttackStrategies(self.graph, lookahead, coverage, False) random.seed(666) self.coverages = [0.1] # [ float(f)/100 for f in range(2, 104, 2) ]
def testComplexFacebookBot(self): FacebookNode.initialize({}, '*****@*****.**', 'asdfasdf0', False) #FacebookNode.initialize({}, '*****@*****.**', 'asdfasdf0', False) mynodemap = { str(FacebookNode(1151613578).__class__): FacebookNode.visit_friends } bot = Transform(mynodemap, None, Constructor()) graph = Graph() graph.add_node(FacebookNode(1151613578)) list(bot.visit(graph)) self.assertTrue( '1151613578' in map(str, bot.decoratedBot.new_graph.nodes())) self.assertTrue( '507271730' in map(str, bot.decoratedBot.new_graph.nodes()))
class PrivacyStressTest3(unittest.TestCase): def setUp(self): self.graph = Graph() self.graph.debug = False self.graph.max_links_input = 100000 self.graph.input_debug_links = 200000 self.graph.output_debug_nodes = 10000 use_big_alphabet = False self.graph.load_compressed_graph(cloudlight.tests.data_enc1, use_big_alphabet) self.graph.create_index_degree() self.graph.create_index_unseen_degree() lookahead = 0 coverage = 'korolova node' self.strategies = PrivacyAttackStrategies(self.graph, lookahead, coverage, self.graph.debug) random.seed(666) self.coverages = [0.1] # [ float(f)/100 for f in range(2, 104, 2) ] def testStrategyRandom(self): self.assertEqual(list(self.strategies.start_random([0.01]))[0], 145)
class Test(unittest.TestCase): def setUp(self): self.graph = Graph() self.graph.debug = False self.graph.input_debug_links = 200000 self.graph.output_debug_nodes = 10000 self.graph.load_edgelist(StringIO(example_txt2)) def tearDown(self): pass def testInternalScalingGrowth(self): self.assertEqual(list(self.graph.internal_scaling_growth('jcl5m')), [1, 2, 22, 44]) def testInternalScalingDimension(self): self.assertEqual(list(self.graph.internal_scaling_dimension('jcl5m')), [-1.0, -1.0, 4.4594316186372973, 3.4445178457870527]) def testConnectivityGrowth(self): self.assertEqual(list(self.graph.connectivity_growth('jcl5m')), [1, 21, 43, 0]) def testConnectivityDimension(self): self.assertEqual(list(self.graph.connectivity_dimension('jcl5m')), [-1.0, -1.0, 5.4262647547020979, -1.0])
class PlotStressTest(unittest.TestCase): def setUp(self): self.graph = Graph() self.graph.debug = False self.graph.input_debug_links = 500000 self.graph.output_debug_nodes = 10000 use_big_alphabet = False self.graph.load_compressed_graph(cloudlight.tests.data_enc1, use_big_alphabet) random.seed(666) self.nodes = self.graph.random_nodes(10000) def tearDown(self): pass def testDegree(self): degrees = list(self.graph.degrees(self.nodes)) p = Plot() p.clear() p.title = 'Degree distribution' p.x_label = 'd : node degree' p.y_label = 'P(d)' p.dist_plot(degrees, 10, True, True, True) #p.show() pass def testClust(self): clusts = list(self.graph.clustering_indices(self.nodes)) p = Plot() p.clear() p.title = 'Clustering index distribution' p.x_label = 'c : node clustering index' p.y_label = 'P(c)' _, _ = p.dist_plot(clusts, 10, True, True, True) #p.show() pass
class PlotStressTest2(unittest.TestCase): def setUp(self): self.graph = Graph() self.graph.debug = False self.graph.input_debug_links = 50000 self.graph.max_links_input = 25000 self.graph.output_debug_nodes = 10000 use_big_alphabet = False self.graph.load_compressed_graph(cloudlight.tests.data_enc1, use_big_alphabet) def tearDown(self): pass def testCompleteAnalysis(self): p = Plot() p.debug = False sample_size = 10 bins = 10 p.init_complete_analysis(self.graph, '/tmp/graph_analysis', sample_size, bins) p.complete_analysis(self.graph)
def __init__(self, graph, lookahead, debug=False): ''' graph: graph where the attack is made. lookahead: visibility of the nodes in the graph. ''' self.debug = debug self.graph = graph self.lookahead = lookahead + 1 if self.debug: print 'INFO: Initializing LinkPrivacyModel...' self.rogues = set([]) # remember extra rogues nodes added self.false_links_graph = Graph() # remember false links added self.visible_graph = Graph() # infiltrated subgraph self.unseen_graph = copy.deepcopy(graph) # uninfiltrated subgraph self.rogue_effort = 0 # cost in new nodes created self.bribe_effort = 0 # cost in existing nodes bribed self.false_link_effort = 0 # cost in new links created self.cache_korolova_coverage = set([]) self.cached_sorted_degrees_dec = None
def testTotalTriangles(self): self.graph = Graph() self.graph.add_edge(1234000, 1234001) self.graph.add_edge(1234001, 1234002) self.graph.add_edge(1234002, 1234000) self.graph.add_edge(1234000, 1234001) self.graph.add_edge(1234001, 1234003) self.graph.add_edge(1234003, 1234000) self.graph.create_index_triangles() self.assertEqual(self.graph.get_parameter_cache('triangles', 1234000), 2) self.assertEqual(self.graph.get_parameter_cache('triangles', 1234001), 2) self.assertEqual(self.graph.get_parameter_cache('triangles', 1234002), 1) self.assertEqual(self.graph.get_parameter_cache('triangles', 1234003), 1) print for node, value in self.graph.get_parameter_cache_iter('triangles'): print node, value self.assertEqual(self.graph.total_triangles(), 2) self.assertEqual(self.graph.total_triangles_weight(1), 2) self.assertEqual(self.graph.total_triangles_weight(2), 0) self.assertEqual(self.graph.triangles_weight(1234000, 1), 2) self.assertEqual(self.graph.triangles_weight(1234000, 2), 0) self.assertEqual(self.graph.triangles_weight(6756443, 1), 0) self.assertEqual(self.graph.triangles_weight(6756443, 2), 0)
def setUp(self): g = Graph() n1 = Node() n2 = Node() n3 = Node() n4 = Node() g.add_edge(n1, n2) g.add_edge(n2, n3) g.add_edge(n3, n1) g.add_edge(n3, n4) self.g = g
class TestDiGraph(unittest.TestCase): def setUp(self): self.digraph = BigDiGraph() self.digraph.debug = False self.digraph.input_debug_links = 1 self.digraph.output_debug_nodes = 1 self.digraph.load_edgelist(StringIO(example_txt), num=False) self.graph = Graph() def tearDown(self): pass def testNodes(self): nodes = list(self.digraph.nodes_iter()) self.assertEqual(nodes, ['1', '2', '3', '4', '5', '6', '666', '7', '8', '9']) def testNumberOfNodes(self): self.assertEqual(self.digraph.number_of_nodes(), 10) def testEdges(self): edges = list(self.digraph.edges_iter()) self.assertEqual(len(edges), 11) def testEdgesBunch(self): edges = list(self.digraph.edges_iter(['1', '4'])) self.assertEqual(len(edges), 2) def testNumberOfEdges(self): self.assertEqual(self.digraph.number_of_edges(), 11) def testSymmetricSubgraph(self): self.digraph.add_only_symmetric_edgelist(self.graph) edges = list(self.graph.edges_iter()) self.assertEqual(len(edges), 3)
class Test(unittest.TestCase): def setUp(self): pass def tearDown(self): pass def testBasicHistogram(self): x = map(lambda x: 2**x, range(0, 5)) p = Plot() p.clear() p.hist(x, 4, False, False, False) #p.show() def testGraphHistogram(self): self.graph = Graph() self.graph.debug = False self.graph.input_debug_links = 200000 self.graph.output_debug_nodes = 10000 self.graph.load_edgelist(StringIO(example_txt2)) degrees = list(self.graph.degrees()) p = Plot() p.clear() p.hist(degrees, 15, True, True, False) #p.show() def testPlotSave(self): self.graph = Graph() self.graph.debug = False self.graph.input_debug_links = 200000 self.graph.output_debug_nodes = 10000 self.graph.load_edgelist(StringIO(example_txt)) clusts = list(self.graph.eccentricities()) clusts p = Plot() p.clear() p.hist(clusts, 3, True, True, True) p.save('testPlotSave.png')
def testTotalTrianglesWeight(self): g = Graph() g.add_edge(1, 2) g.add_edge(2, 3) g.add_edge(3, 1) g.add_edge(5, 6) g.add_edge(6, 7) g.add_edge(7, 5) g.create_index_triangles() g.index_parameter_from_parameter('triangles', 'unseen_triangles') self.assertEqual(g.total_triangles_weight(1), 2) self.assertEqual(g.total_triangles_weight(2), 0) self.assertEqual(g.total_unseen_triangles(), 2) g.update_edge_weight(1, 2, 2) self.assertEqual(g.total_triangles_weight(1), 1) self.assertEqual(g.total_triangles_weight(2), 0) g.update_edge_weight(2, 3, 2) g.update_edge_weight(3, 1, 2) self.assertEqual(g.total_triangles_weight(1), 1) self.assertEqual(g.total_triangles_weight(2), 1)
class GraphTest(unittest.TestCase): def setUp(self): self.graph = Graph() self.graph.debug = False self.graph.input_debug_links = 1 self.graph.output_debug_nodes = 1 self.graph.load_edgelist(StringIO(example_txt), num=False) def tearDown(self): pass # def testNodes(self): # nodes = list(self.graph.nodes_iter()) # self.assertEqual( nodes, ['666', '1', '3', '2', '5', '4', '7', '6', '9', '8']) # # # def testEdges(self): # edges = list(self.graph.edges_iter()) # self.assertEqual( len(edges), 8) # # # def testEdgesBunch(self): # edges = list(self.graph.edges_iter(['1','4'])) # self.assertEqual( len(edges), 2) # # # def testDegrees(self): # self.assertEqual(list(self.graph.degrees()), [1, 1, 1, 1, 2, 1, 3, 1, 3, 2]) # # # def testDegreesPartial(self): # self.assertEqual(list(self.graph.degrees(['2','3','4'])), [1, 1, 1]) # # # def testDegreesIter(self): # self.assertEqual(list(self.graph.degrees_iter()), [1, 1, 1, 1, 2, 1, 3, 1, 3, 2]) # # # def testClustering(self): # self.assertEqual(list(self.graph.clustering_indices()), [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.33333333333333331, 0.0, 0.33333333333333331, 1.0]) # # # def testKnn(self): # self.assertEqual(list(self.graph.average_neighbor_degrees()), [3.0, 1.0, 2.0, 1.0, 1.0, 2.0, 2.0, 3.0, 2.0, 3.0]) # # # def testExcentricity(self): # self.assertEqual(list(self.graph.eccentricities()), [3, 1, 2, 1, 1, 2, 2, 3, 2, 2]) # # # def testAveragePathLengths(self): # self.assertEqual(list(self.graph.average_path_lengths()), [1.6000000000000001, 0.5, 1.0, 0.5, 0.66666666666666663, 1.0, 1.0, 1.6000000000000001, 1.0, 1.2]) # # # def testKcoreness(self): # self.assertEqual(list(self.graph.kcoreness()), [1, 1, 1, 1, 1, 1, 2, 1, 2, 2]) # # # def testKcorenessPartial(self): # self.assertEqual(list(self.graph.kcoreness(['3', '4', '5'])), [1, 1, 1]) # # # def testKcorenessIterPartial(self): # self.assertEqual(list(self.graph.kcoreness_iter(['3', '4', '5'])), [1, 1, 1]) # # # def testRandomEdges(self): # random.seed(666) # self.assertEqual(self.graph.random_edges(2, False), [('666', '9'), ('3', '5')]) # # # def testRandomNodes(self): # random.seed(666) # self.assertEqual(self.graph.random_nodes(3, False), ['4', '2', '666']) # # # def testShow(self): ## self.graph = Graph() ## self.graph.debug = False ## self.graph.input_debug_links = 1 ## self.graph.output_debug_nodes = 1 ## self.graph.load_edgelist(StringIO(top_edges), num=False) ## self.graph.show() # pass # # def testDegreeEncodedGraph(self): # # self.assertEqual(self.graph.compressed_by_degree_graph().nodes(), ['!', '"', '%', '$', "'", '&', ')', '(', '+', '*']) # self.assertEqual(self.graph.compressed_by_degree_graph().degrees(), [3, 3, 2, 2, 1, 1, 1, 1, 1, 1]) # # # def testSaveEncoded1(self): # enc_g = self.graph.compressed_by_degree_graph(False) # io = StringIO() # enc_g.save_edgelist(io) # self.assertEqual( io.getvalue().split('\n')[2:], ['# ', '0 9', '0 2', '0 1', '1 2', '1 4', '3 5', '3 7', '6 8', '']) # # # def testSaveEncoded2(self): # enc_g = self.graph.compressed_by_degree_graph() # io = StringIO() # enc_g.save_edgelist(io) # self.assertEqual( io.getvalue().split('\n')[2:], ['# ', '! +', '! "', '! $', '" $', '" &', '% )', "% '", '( *', '']) # def testTotalTriangles(self): # # self.assertEqual(self.graph.total_triangles(), 1) # self.assertEqual(self.graph.total_triangles_weight(1), 1) # self.assertEqual(self.graph.total_triangles_weight(2), 0) # # self.graph.add_edge(1234000, 1234001) # self.graph.add_edge(1234001, 1234002) # self.graph.add_edge(1234002, 1234000) # # self.graph.add_edge(1234000, 1234001) # self.graph.add_edge(1234001, 1234003) # self.graph.add_edge(1234003, 1234000) # # self.assertEqual(self.graph.total_triangles(), 3) # self.assertEqual(self.graph.total_triangles_weight(1), 3) # self.assertEqual(self.graph.total_triangles_weight(2), 0) # # self.assertEqual(self.graph.triangles_weight(1234000, 1), 2) # self.assertEqual(self.graph.triangles_weight(1234000, 2), 0) # self.assertEqual(self.graph.triangles_weight(6756443, 1), 0) # self.assertEqual(self.graph.triangles_weight(6756443, 2), 0) # # self.graph.update_edge_weight(1234000, 1234001, 2) # self.graph.update_edge_weight(1234001, 1234003, 2) # self.graph.update_edge_weight(1234003, 1234000, 2) # # self.assertEqual(self.graph.total_triangles(), 3) # self.assertEqual(self.graph.total_triangles_weight(1), 1) # self.assertEqual(self.graph.total_triangles_weight(2), 1) # # self.assertEqual(self.graph.triangles_weight(1234000, 1), 0) # self.assertEqual(self.graph.triangles_weight(1234000, 2), 1) # self.assertEqual(self.graph.triangles_weight(6756443, 1), 0) # self.assertEqual(self.graph.triangles_weight(6756443, 2), 0) # # self.graph.update_edge_weight(1234000, 1234001, 2) # self.graph.update_edge_weight(1234001, 1234002, 2) # self.graph.update_edge_weight(1234002, 1234000, 2) # # self.assertEqual(self.graph.total_triangles(), 3) # self.assertEqual(self.graph.total_triangles_weight(1), 1) # self.assertEqual(self.graph.total_triangles_weight(2), 2) # # self.assertEqual(self.graph.triangles_weight(1234000, 1), 0) # self.assertEqual(self.graph.triangles_weight(1234000, 2), 2) # self.assertEqual(self.graph.triangles_weight(6756443, 1), 0) # self.assertEqual(self.graph.triangles_weight(6756443, 2), 0) def testTotalTriangles(self): self.graph = Graph() self.graph.add_edge(1234000, 1234001) self.graph.add_edge(1234001, 1234002) self.graph.add_edge(1234002, 1234000) self.graph.add_edge(1234000, 1234001) self.graph.add_edge(1234001, 1234003) self.graph.add_edge(1234003, 1234000) self.graph.create_index_triangles() self.assertEqual(self.graph.get_parameter_cache('triangles', 1234000), 2) self.assertEqual(self.graph.get_parameter_cache('triangles', 1234001), 2) self.assertEqual(self.graph.get_parameter_cache('triangles', 1234002), 1) self.assertEqual(self.graph.get_parameter_cache('triangles', 1234003), 1) print for node, value in self.graph.get_parameter_cache_iter('triangles'): print node, value self.assertEqual(self.graph.total_triangles(), 2) self.assertEqual(self.graph.total_triangles_weight(1), 2) self.assertEqual(self.graph.total_triangles_weight(2), 0) self.assertEqual(self.graph.triangles_weight(1234000, 1), 2) self.assertEqual(self.graph.triangles_weight(1234000, 2), 0) self.assertEqual(self.graph.triangles_weight(6756443, 1), 0) self.assertEqual(self.graph.triangles_weight(6756443, 2), 0) def testTotalTrianglesWeight(self): g = Graph() g.add_edge(1, 2) g.add_edge(2, 3) g.add_edge(3, 1) g.add_edge(5, 6) g.add_edge(6, 7) g.add_edge(7, 5) g.create_index_triangles() g.index_parameter_from_parameter('triangles', 'unseen_triangles') self.assertEqual(g.total_triangles_weight(1), 2) self.assertEqual(g.total_triangles_weight(2), 0) self.assertEqual(g.total_unseen_triangles(), 2) g.update_edge_weight(1, 2, 2) self.assertEqual(g.total_triangles_weight(1), 1) self.assertEqual(g.total_triangles_weight(2), 0) g.update_edge_weight(2, 3, 2) g.update_edge_weight(3, 1, 2) self.assertEqual(g.total_triangles_weight(1), 1) self.assertEqual(g.total_triangles_weight(2), 1)
class Test(unittest.TestCase): def setUp(self): self.graph = Graph() self.graph.debug = False self.graph.max_links_input = 50000 self.graph.input_debug_links = 1000 #200000 self.graph.output_debug_nodes = 10000 use_big_alphabet = False self.graph.load_compressed_graph(cloudlight.tests.data_enc1, use_big_alphabet, has_num=True) self.graph.create_indices() def tearDown(self): pass def testDegreeParam(self): self.graph.create_index_degree() sum = 0 for _, value in self.graph.get_parameter_cache_iter('degree'): sum += value self.assertEqual(sum, 100000.0) def testClusteringParam(self): self.graph.create_index_clustering() sum = 0 for _, value in self.graph.get_parameter_cache_iter('clustering'): sum += value self.assertAlmostEqual(sum, 1290.6523256023384) def testKnnParam(self): self.graph.create_index_knn() sum = 0 for _, value in self.graph.get_parameter_cache_iter('knn'): sum += value self.assertAlmostEqual(sum, 14815770.85218275) def testDegreeInverse(self): self.graph.create_index_degree() deg_one_nodes = list( self.graph.get_parameter_cache_inverse('degree', 1)) self.assertEqual(len(deg_one_nodes), 38702) def testKnnInverse(self): self.graph.create_index_knn() deg_one_nodes = list(self.graph.get_parameter_cache_inverse( 'knn', 139)) self.assertEqual(len(deg_one_nodes), 381) def testClusteringInverse(self): self.graph.create_index_clustering() deg_one_nodes = list( self.graph.get_parameter_cache_inverse('clustering', 2.0 / 3)) self.assertEqual(len(deg_one_nodes), 97) def testGetClustering(self): self.graph.create_index_clustering() deg_one_nodes = list( self.graph.get_parameter_cache_inverse('clustering', 2.0 / 3)) node = list(deg_one_nodes)[0] self.assertAlmostEqual( self.graph.get_parameter_cache('clustering', node), 2.0 / 3)
def setUp(self): self.graph = Graph() self.graph.debug = False self.graph.max_links_input = 100000 self.graph.input_debug_links = 200000 self.graph.output_debug_nodes = 10000
class LinkPrivacyModel(object): ''' A class to simulate link privacy attacks on network with limited node visibility. ''' def __init__(self, graph, lookahead, debug=False): ''' graph: graph where the attack is made. lookahead: visibility of the nodes in the graph. ''' self.debug = debug self.graph = graph self.lookahead = lookahead + 1 if self.debug: print 'INFO: Initializing LinkPrivacyModel...' self.rogues = set([]) # remember extra rogues nodes added self.false_links_graph = Graph() # remember false links added self.visible_graph = Graph() # infiltrated subgraph self.unseen_graph = copy.deepcopy(graph) # uninfiltrated subgraph self.rogue_effort = 0 # cost in new nodes created self.bribe_effort = 0 # cost in existing nodes bribed self.false_link_effort = 0 # cost in new links created self.cache_korolova_coverage = set([]) self.cached_sorted_degrees_dec = None def add_rogue_node(self, r_node): if self.debug: print 'INFO: add_rogue_node --> %s ...' % str(r_node) if r_node in self.graph.nodes(): raise LinkPrivacyModelException( 'new rogue node "%s" already in friend graph!' % str(r_node)) if r_node in self.rogues: raise LinkPrivacyModelException( 'new node "%s" already in rogue node set!' % str(r_node)) self.rogues.add(r_node) self.graph.add_node(r_node) self.visible_graph.add_node(r_node) self.rogue_effort += 1 def add_bribed_node(self, r_node): if self.debug: print 'INFO: add_bribed_node --> %s ...' % str(r_node) if not r_node in self.graph.nodes(): raise LinkPrivacyModelException( 'new bribed node "%s" NOT in friend graph!' % str(r_node)) self.visible_graph.add_node(r_node) edges = self.graph.lookahead_edges([r_node], self.lookahead) self.visible_graph.add_edges_from(edges) self.unseen_graph.remove_edges_from(edges) nodes_in_new_visible_edges = list( set([n for n, _ in edges] + [n2 for _, n2 in edges])) for node in nodes_in_new_visible_edges: # if self.unseen_graph.has_node(node) and self.unseen_graph.degree(node) == 0: # self.unseen_graph.remove_node(node) if self.visible_graph.degree(node) == self.graph.degree(node): self.cache_korolova_coverage.add(node) self.bribe_effort += 1 # self.cache_korolova_coverage.add( r_node ) # for neigh in self.graph.neighbors(r_node): # if self.visible_graph.degree(neigh) == self.graph.degree(neigh): # self.cache_korolova_coverage.add( neigh ) def add_false_link(self, src, dst): if not src in self.graph.nodes(): raise LinkPrivacyModelException( 'new false link source node "%s" NOT in rogues or friend graph!' % str(src)) if not dst in self.graph.nodes(): raise LinkPrivacyModelException( 'new false link destination node "%s" NOT in rogues or friend graph!' % str(dst)) self.false_links_graph.add_edge(src, dst) self.graph.add_edge(src, dst) if self.visible_graph.has_node(src): self.visible_graph.add_edges_from( self.graph.lookahead_edges([src], self.lookahead)) if self.visible_graph.has_node(dst): self.visible_graph.add_edges_from( self.graph.lookahead_edges([dst], self.lookahead)) self.false_link_effort += 1 def link_coverage(self): numerator = float(self.visible_graph.number_of_edges() - self.false_links_graph.number_of_edges()) denominator = float(self.graph.number_of_edges() - self.false_links_graph.number_of_edges()) return numerator / denominator def node_coverage(self): numerator = float(self.visible_graph.number_of_nodes() - len(self.rogues)) denominator = float(self.graph.number_of_nodes() - len(self.rogues)) return numerator / denominator def korolova_node_coverage(self): #numerator = 0 #for node in self.visible_graph.nodes_iter(): # if self.graph.degree(node) == self.visible_graph.degree(node): # numerator += 1 numerator = len(self.cache_korolova_coverage) denominator = float(self.graph.number_of_nodes() - len(self.rogues)) return numerator / denominator def total_effort(self): return self.rogue_effort + self.bribe_effort + self.false_link_effort def max_unseen_degree_node(self): unseen_nodes_degrees = zip(self.unseen_graph.nodes(), self.unseen_graph.degrees()) # in reverse order unseen_nodes_degrees.sort(lambda x, y: -cmp(x[1], y[1])) return unseen_nodes_degrees[0][0] def max_unseen_degree_crawler_node(self): if self.debug: print 'INFO: max_unseen_degree_crawler_node ...' unseen_nodes_degrees = zip(self.unseen_graph.nodes(), self.unseen_graph.degrees()) # in reverse order unseen_nodes_degrees.sort(lambda x, y: -cmp(x[1], y[1])) visible_node_with_max_unseen_degree = None for node, _ in unseen_nodes_degrees: if node in self.visible_graph.nodes(): visible_node_with_max_unseen_degree = node break return visible_node_with_max_unseen_degree def sorted_degrees_dec(self): if self.debug: print 'INFO: sorted_degrees_dec ...' if not self.cached_sorted_degrees_dec: degrees = zip(self.graph.nodes(), self.graph.degree()) degrees.sort(lambda x, y: cmp(x[1], y[1])) degrees.reverse() self.cached_sorted_degrees_dec = [x for x, _ in degrees] return self.cached_sorted_degrees_dec def sorted_degrees_dec_iter(self): return iter(self.sorted_degrees_dec()) def random_node_order(self): if self.debug: print 'INFO: random_node_order ...' return self.graph.random_nodes(self.graph.number_of_nodes()) def random_node_order_iter(self): return iter(self.random_node_order()) def random_node(self): return self.graph.random_nodes(1)[0]
''' Created on Apr 8, 2010 @author: jose ''' from cloudlight.classes.graph import Graph from cloudlight.algorithms.plot import Plot g = Graph() g.max_links_input = 50000 g.load_edgelist(open('orkut-links-fst.txt.toundirected.3mill'), num=True) g.save_compressed_graph('./cloudlight/tests/data_enc1.py', False) g.save_compressed_graph('./cloudlight/tests/data_enc2.py', True)