예제 #1
0
    def test_set_recovery(self):
        edges = []
        for i in range(1,100):
            for j in range(1,100):
                if i == j:
                    continue
                else:
                    edges.append((i,j))
                    
        g = Graph(edges)        

        recovered = []
        def recovery_function(n):
            recovered.append(n)
        
        g.set_recovery(recovery_function)

        g.infect_seeds([g.get_node_by_name(i) for i in range(1,100)])

        assert not recovered

        before = g.infected()
        g.recover()
        after = g.infected()
        
        assert not not recovered
        
        assert len(after) < len(before)        
        
        
예제 #2
0
    def test_add_edge(self):
        g = Graph([(1, 2), (1, 3), (1, 4), (1, 5)])

        one = g.get_node_by_name(1)
        two = g.get_node_by_name(2)
        three = g.get_node_by_name(3)

        assert one.degree() == 4
        assert two.degree() == 1
        assert three.degree() == 1

        g.add_edge(one, two)

        assert one.degree() == 5
        assert two.degree() == 2
        assert three.degree() == 1

        g.add_edge(two, one)

        assert one.degree() == 6
        assert two.degree() == 3
        assert three.degree() == 1

        g.add_edge(two, three)

        assert one.degree() == 6
        assert two.degree() == 4
        assert three.degree() == 2
예제 #3
0
    def test_export(self):
        g = Graph([(1,2),(2,3),(3,4)]) 

        exported = g.export()
        
        g_copy = Graph(edges=exported)
        
        for node in g:
            assert node.name() in [n.name() for n in g_copy.nodes()]
예제 #4
0
 def test_add_edge(self):
     g = Graph([(1,2),(1,3),(1,4),(1,5)])
     
     one = g.get_node_by_name(1)
     two = g.get_node_by_name(2)
     three = g.get_node_by_name(3)
     
     assert one.degree() == 4
     assert two.degree() == 1
     assert three.degree() == 1
     
     g.add_edge(one, two)
     
     assert one.degree() == 5
     assert two.degree() == 2
     assert three.degree() == 1
     
     g.add_edge(two, one)
     
     assert one.degree() == 6
     assert two.degree() == 3
     assert three.degree() == 1
     
     g.add_edge(two, three)
     
     assert one.degree() == 6
     assert two.degree() == 4
     assert three.degree() == 2
예제 #5
0
 def test_degree(self):
     graph = Graph()
     nodes = []
     for i in range(1000):
         n = Node()
         graph.add_node(n)
         nodes.append(n)
     target = nodes[0]
     for n in nodes[1:]:
         random.choice([target.add_child, target.add_parent])(n)
     assert target.degree() == 999
예제 #6
0
 def test_init_positional_edge(self):
     g = Graph([(1,2),(1,3),(1,4),(1,5)])
     
     one = g.get_node_by_name(1)
     two = g.get_node_by_name(2)
     three = g.get_node_by_name(3)
     four = g.get_node_by_name(4)
     five = g.get_node_by_name(5)
     
     assert one.degree() == 4, "Expected 4, got %s" % one.degree()
     for n in [two, three, four, five]:
         assert n.degree() == 1
예제 #7
0
    def test_init_positional_edge(self):
        g = Graph([(1, 2), (1, 3), (1, 4), (1, 5)])

        one = g.get_node_by_name(1)
        two = g.get_node_by_name(2)
        three = g.get_node_by_name(3)
        four = g.get_node_by_name(4)
        five = g.get_node_by_name(5)

        assert one.degree() == 4, "Expected 4, got %s" % one.degree()
        for n in [two, three, four, five]:
            assert n.degree() == 1
예제 #8
0
 def test_node_naming(self):
     self.graph = Graph()
     self.nodes = []
     for i in range(1000):
         n = Node()
         self.graph.add_node(n)
         self.nodes.append(n)
     assert len(self.nodes) == len(set(self.nodes))
     assert len(self.nodes) == 1000
     assert len(set([n.name() for n in self.nodes])) == 1000
     
     node = self.nodes[0]
     assert type('') == type(node.name())
예제 #9
0
    def test_get_node_by_name(self):
        g = Graph([(1, 2), (1, 3), (1, 4), (1, 5)])

        one = g.get_node_by_name(1)
        two = g.get_node_by_name(2)
        three = g.get_node_by_name(3)
        four = g.get_node_by_name(4)
        five = g.get_node_by_name(5)

        assert one.name() == 1
        assert two.name() == 2
        assert three.name() == 3
        assert four.name() == 4
        assert five.name() == 5
예제 #10
0
    def test_get_node_by_name(self):
        g = Graph([(1,2),(1,3),(1,4),(1,5)])

        one = g.get_node_by_name(1)
        two = g.get_node_by_name(2)
        three = g.get_node_by_name(3)
        four = g.get_node_by_name(4)
        five = g.get_node_by_name(5)

        assert one.name() == 1
        assert two.name() == 2
        assert three.name() == 3
        assert four.name() == 4
        assert five.name() == 5
예제 #11
0
    def test_closeness(self):
        g = Graph([(1,2),(2,3),(3,4)]) 
        
        one = g[1]
        four = g[4]            
        
        closeness, path = g.closeness(one, four)
        assert closeness == 3.0

        g = Graph([(1,2),(2,3),(3,4)],
                  length=lambda e: e.weight_/2.0)
        
        one = g[1]
        four = g[4]
        
        closeness, path = g.closeness(one, four)
        
        assert closeness == 1.5
예제 #12
0
    def test_init_advanced_defs(self):
        g = Graph(graph=[{
            'from_': 1,
            'to_': 2,
            'type_': 'first',
            'weight_': 1.0
        }, {
            'from_': 1,
            'to_': 3,
            'type_': 'second',
            'weight_': 2.0
        }, {
            'from_': 1,
            'to_': 4,
            'type_': 'third',
            'weight_': 3.0
        }, {
            'from_': 1,
            'to_': 5,
            'type_': 'fourth',
            'weight_': 4.0
        }],
                  directed=True,
                  transmission_probability=lambda x, y: 1.0)

        one = g.get_node_by_name(1)
        two = g.get_node_by_name(2)
        three = g.get_node_by_name(3)
        four = g.get_node_by_name(4)
        five = g.get_node_by_name(5)

        assert one.degree() == 4, "Expected 4, got %s" % one.degree()
        for n in [two, three, four, five]:
            assert n.degree() == 1

        assert one.edges()[2].type_ == 'first'
        assert one.edges()[3].type_ == 'second'
        assert one.edges()[4].type_ == 'third'
        assert one.edges()[5].type_ == 'fourth'

        assert one.edges()[2].weight_ == 1.0
        assert one.edges()[3].weight_ == 2.0
        assert one.edges()[4].weight_ == 3.0
        assert one.edges()[5].weight_ == 4.0
예제 #13
0
    def test_init_advanced_defs(self):
        g = Graph(graph=[{'from_': 1,
                          'to_': 2,
                          'type_': 'first',
                          'weight_': 1.0
                         },{'from_': 1,
                          'to_': 3,
                          'type_': 'second',
                          'weight_': 2.0
                         },{'from_': 1,
                          'to_': 4,
                          'type_': 'third',
                          'weight_': 3.0
                         },{'from_': 1,
                          'to_': 5,
                          'type_': 'fourth',
                          'weight_': 4.0
                         }], 
                  directed=True,
                  transmission_probability=lambda x,y: 1.0)
        
        one = g.get_node_by_name(1)
        two = g.get_node_by_name(2)
        three = g.get_node_by_name(3)
        four = g.get_node_by_name(4)
        five = g.get_node_by_name(5)
        
        assert one.degree() == 4, "Expected 4, got %s" % one.degree()
        for n in [two, three, four, five]:
            assert n.degree() == 1
            
        assert one.edges()[2].type_ == 'first'
        assert one.edges()[3].type_ == 'second'
        assert one.edges()[4].type_ == 'third'
        assert one.edges()[5].type_ == 'fourth'

        assert one.edges()[2].weight_ == 1.0
        assert one.edges()[3].weight_ == 2.0
        assert one.edges()[4].weight_ == 3.0
        assert one.edges()[5].weight_ == 4.0
예제 #14
0
    def test_set_infection(self):
        g = Graph([(1, 2), (1, 3), (1, 4), (1, 5)])
        infected = []

        def callback(n):
            infected.append(n)

        g.set_infection(callback)

        one = g.get_node_by_name(1)
        two = g.get_node_by_name(2)
        three = g.get_node_by_name(3)

        g.infect_seeds([one, two, three])

        assert one in infected
        assert two in infected
        assert three in infected

        assert one in g.infected()
        assert two in g.infected()
        assert three in g.infected()
예제 #15
0
 def test_set_infection(self):
     g = Graph([(1,2),(1,3),(1,4),(1,5)])
     infected = []
     
     def callback(n):
         infected.append(n)
         
     g.set_infection(callback)
     
     one = g.get_node_by_name(1)
     two = g.get_node_by_name(2)
     three = g.get_node_by_name(3)
     
     g.infect_seeds([one, two, three])
     
     assert one in infected
     assert two in infected
     assert three in infected
     
     assert one in g.infected()
     assert two in g.infected()
     assert three in g.infected()
예제 #16
0
 def test_set_transmission_function_in_constructor(self):
     
     target = [False]
     def trans(a,b):
         target[0] = True
         return 1
         
     g = Graph([(1,2)], 
               transmission_probability=trans)
     
     g.infect_seeds([g.get_node_by_name(1)])
     
     g.propagate()
     
     assert target[0]
예제 #17
0
class NodeTest(TestApi):

    def test_node_naming(self):
        self.graph = Graph()
        self.nodes = []
        for i in range(1000):
            n = Node()
            self.graph.add_node(n)
            self.nodes.append(n)
        assert len(self.nodes) == len(set(self.nodes))
        assert len(self.nodes) == 1000
        assert len(set([n.name() for n in self.nodes])) == 1000
        
        node = self.nodes[0]
        assert type('') == type(node.name())
        
        
    def test_add_parent_and_child(self):
        parent = Node()
        child = Node()
        
        parent.add_child(child)
        child.add_parent(parent)
        
        assert parent.is_parent_of(child)
        assert child.is_child_of(parent)
        
        assert parent.name() in child.edges()
        assert child.name() in parent.edges()
        
        assert parent.degree() == 2L
        assert child.degree() == 2L
        
        parent = Node()
        child = Node()
        parent.add_child(child)
        
        assert parent.degree() == 1L
        assert child.degree() == 1L
        
        assert child.is_child_of(parent)
        assert parent.is_parent_of(child)
        
    def test_degree(self):
        graph = Graph()
        nodes = []
        for i in range(1000):
            n = Node()
            graph.add_node(n)
            nodes.append(n)
        target = nodes[0]
        for n in nodes[1:]:
            random.choice([target.add_child, target.add_parent])(n)
        assert target.degree() == 999
    
    def test_edges(self):
        target = Node()
        to_add = [Node() for i in range(1000)]
        for n in to_add:
            random.choice([target.add_child, target.add_parent])(n)
        assert len(target.edges().keys()) == target.degree()

    def test_cleanup(self):
        target = Node()
        to_clean = [Node(), Node(), Node()]
        not_to_clean = [Node(), Node()]
        for n in to_clean + not_to_clean:
            random.choice([target.add_parent, target.add_child])(n)
            
        assert len(target.edges().keys()) == 5
        assert target.degree() == 5

        while to_clean:
            n = to_clean.pop()
            del n
                
        assert len(target.edges().keys()) == 2
        assert target.degree() == 2, "Actual degree is %s" % target.degree()
    
    def test_weakref_cleanup(self):
        class A:
            pass
        
        a = A()
        refs = []
        def cleanup(wr):
            refs.remove(wr)
        
        refs.append(weakref.ref(a, cleanup))
        refs.append(weakref.ref(a, cleanup))
        refs.append(weakref.ref(a, cleanup))

        for ref in refs:
            assert ref() is a        
        
        del a
        
        assert len(refs) == 0

    def test_circular_weakref_cleanup(self):
        class A:
            pass
        class B:
            def __init__(self, a, b):
                self.a = a
                self.b = b
        a = A()
        b = A()
        
        refs = []
        
        def cleanup(wr):
            refs.remove(wr)
        
        a.b = weakref.ref(b, cleanup)
        b.a = weakref.ref(a, cleanup)
    
        br = B(weakref.ref(a), weakref.ref(b))
        
        refs.append(a.b)
        refs.append(b.a)
        
        assert len(refs) == 2
        
        del a
        
        assert len(refs) == 1
    
    def test_circular_node_cleanup(self):
        parent = Node()
        child = Node()
        
        par_mul = parent.add_child(child)
        assert par_mul == 1L, "Expected multiplicity 1, got %s" % par_mul
        child_mul = child.add_parent(parent) 
        assert child_mul == 2L, "Expected multiplicity 2, got %s" % child_mul
        
        assert parent.degree() == 2L, "Expected 2, got %s" % parent.degree()
        assert child.degree() == 2L, "Expected 2, got %s" % child.degree()
        
        del child
        
        assert len(parent.edges().items()) == 0, "Parent has %s edges." % len(parent.edges().items())
        assert parent.degree() == 0L, "Parent degree is: %s" % parent.degree()
        
        parent = Node()
        child = Node()
        
        assert parent.add_child(child) == 1L
        assert child.add_parent(parent) == 2L
        
        assert parent.degree() == 2L
        assert child.degree() == 2L
        
        del parent
        
        assert child.degree() == 0L, "Child degree is: %s" % child.degree()
        
    def test_transmission_probability(self):
        parent = Node()
        child = Node()
        child2 = Node()
        child3 = Node()
        
        parent.add_child(child)
        child.add_parent(parent)
        
        assert child.is_child_of(parent)
        assert parent.is_parent_of(child)
        
        assert child.transmission_probability(parent) == 1.0, "Expected 1.0, got %s" % child.transmission_probability(parent)
        assert parent.transmission_probability(child) == 1.0, "Expected 1.0, got %s" % parent.transmission_probability(child)
        
        parent.add_child(child2)
        parent.add_child(child3)
        
        assert child.degree() == 2L
        assert child2.degree() == 1L
        assert child3.degree() == 1L
        assert parent.degree() == 4L
        
        assert parent.transmission_probability(child) == 1.0/2.0, "Expected 1/2, got %s" % parent.transmission_probability(child)
        assert parent.transmission_probability(child2) == 1.0/4.0, "Expected 1/4, got %s" % parent.transmission_probability(child2)
        assert parent.transmission_probability(child3) == 1.0/4.0, "Expected 1/4, got %s" % parent.transmission_probability(child3)
        
    def test_propagate(self):
        parent = Node()
        child = Node()
        
        child.infected = False
        
        parent.add_child(child)
        
        def infection(n):
            n.infected = True
            
        parent.propagate_infection(infection)
        
        assert child.infected
        
        parent = Node()
        children = [Node() for i in range(1000)]
         
        for c in children:
            c.infected = False
            parent.add_child(c)
            
        # Test that around 1 in 1000 is infected:
        parent.propagate_infection(infection)
        
        infected = float(len(filter(lambda n: n.infected, children)))
        assert infected / 1000.0 <= 10.0 / 1000.0
예제 #18
0
    def test_set_recovery(self):
        edges = []
        for i in range(1, 100):
            for j in range(1, 100):
                if i == j:
                    continue
                else:
                    edges.append((i, j))

        g = Graph(edges)

        recovered = []

        def recovery_function(n):
            recovered.append(n)

        g.set_recovery(recovery_function)

        g.infect_seeds([g.get_node_by_name(i) for i in range(1, 100)])

        assert not recovered

        before = g.infected()
        g.recover()
        after = g.infected()

        assert not not recovered

        assert len(after) < len(before)
예제 #19
0
 def test_get_set_transmission_probability_function(self):
     g = Graph([(1,2),(2,3),(3,4)]) 
     
     assert g.get_transmission_probability() == tp
     for node in g.nodes():
         assert node.get_transmission_probability() == tp
     
     def a(n, m):
         return 1
     
     def b(n, m):
         return 0
     
     
     g.set_transmission_probability(a)
     
     assert g.get_transmission_probability() == a
     for node in g.nodes():
         assert node.get_transmission_probability() == a
         
     g.set_transmission_probability(b)
     
     assert g.get_transmission_probability() == b
     for node in g.nodes():
         assert node.get_transmission_probability() == b
예제 #20
0
 def test_get_set_recovery_probability_function(self):
     
     g = Graph([(1,2),(2,3),(3,4)]) 
     
     assert g.get_recovery_probability() == rp
     for node in g.nodes():
         assert node.get_recovery_probability() == rp
     
     def a(n):
         return 1
     
     def b(n):
         return 0
     
     g.set_recovery_probability(a)
     
     assert g.get_recovery_probability() == a
     for node in g.nodes():
         assert node.get_recovery_probability() == a
         
     g.set_recovery_probability(b)
     
     assert g.get_recovery_probability() == b
     for node in g.nodes():
         assert node.get_recovery_probability() == b