Esempio n. 1
0
    def add_edge_by_node_sequence(self, parent, child, directed=None, transmission_probability=None, type_=None, weight_=1.0, recovery_probability=None):
        """
        Creates nodes and an edge between two nodes for a given name.
        
        :param str parent: The name for the parent node
        :param str child: The name for the child node
        :param bool directed: Whether or not the edge should be directed.
        :param function transmission_probability: The transmission probability function associated with the edge.
        :param str type_: The type of edge
        :param str weight_: The weight of the edge
        """
        p = self.get_node_by_name(parent)
        if not p:
            p = Node(name=parent,
                     transmission_probability=transmission_probability or self.__transmission_probability,
                     recovery_probability=recovery_probability or self.__recovery_probability,
                     graph=self)

        c = self.get_node_by_name(child)
        if not c:
            c = Node(name=child,
                     transmission_probability=transmission_probability or self.__transmission_probability,
                     recovery_probability=recovery_probability or self.__recovery_probability,
                     graph=self)
        
        p.add_child(c, type_=type_, weight_=weight_)
        
        self.add_node(p)
        self.add_node(c)
Esempio n. 2
0
    def add_edge_by_node_sequence(self, parent, child, directed=None, transmission_probability=None, type_=None, weight_=1.0, recovery_probability=None):
        """
        Creates nodes and an edge between two nodes for a given name.
        
        :param str parent: The name for the parent node
        :param str child: The name for the child node
        :param bool directed: Whether or not the edge should be directed.
        :param function transmission_probability: The transmission probability function. Should take two arguments of type graphism.node.Node. The first positional argument is the parent (infection host), the second is the child. 
        :param function recovery_probability: The recovery probability function. Should take a single objet of type graphism.node.Node. Returns a float on [0,1] indicating the probability of recovery for the node.
        :param str type_: The type of edge
        :param str weight_: The weight of the edge
        """
        p = self.get_node_by_name(parent)
        if not p:
            p = Node(name=parent,
                     transmission_probability=transmission_probability or self.__transmission_probability,
                     recovery_probability=recovery_probability or self.__recovery_probability,
                     graph=self,
                     length=self.__length)

        c = self.get_node_by_name(child)
        if not c:
            c = Node(name=child,
                     transmission_probability=transmission_probability or self.__transmission_probability,
                     recovery_probability=recovery_probability or self.__recovery_probability,
                     graph=self,
                     length=self.__length)
        
        p.add_child(c, type_=type_, weight_=weight_)
        
        self.add_node(p)
        self.add_node(c)
Esempio n. 3
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())
Esempio n. 4
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
Esempio n. 5
0
    def add_edge_by_node_sequence(self,
                                  parent,
                                  child,
                                  directed=None,
                                  transmission_probability=None,
                                  type_=None,
                                  weight_=1.0,
                                  recovery_probability=None):
        """
        Creates nodes and an edge between two nodes for a given name.
        
        :param str parent: The name for the parent node
        :param str child: The name for the child node
        :param bool directed: Whether or not the edge should be directed.
        :param function transmission_probability: The transmission probability function associated with the edge.
        :param str type_: The type of edge
        :param str weight_: The weight of the edge
        """
        p = self.get_node_by_name(parent)
        if not p:
            p = Node(name=parent,
                     transmission_probability=transmission_probability
                     or self.__transmission_probability,
                     recovery_probability=recovery_probability
                     or self.__recovery_probability,
                     graph=self)

        c = self.get_node_by_name(child)
        if not c:
            c = Node(name=child,
                     transmission_probability=transmission_probability
                     or self.__transmission_probability,
                     recovery_probability=recovery_probability
                     or self.__recovery_probability,
                     graph=self)

        p.add_child(c, type_=type_, weight_=weight_)

        self.add_node(p)
        self.add_node(c)
Esempio n. 6
0
 def test_to_dict(self):
     parent = Node()
     child = Node()
     multiplicity = 2
     type_ = 'test'
     weight_ = 2.0
     directed = True
     length = lambda e: e.weight_
     
     e = Edge(weakref.ref(parent), weakref.ref(child), multiplicity, type_, weight_, directed, length)
     e_d = e.to_dict()
     
     assert e_d['from_'] == parent.name()
     assert e_d['to_'] == child.name()
     assert e_d['multiplicity'] == multiplicity
     assert e_d['type_'] == type_
     assert e_d['weight_'] == weight_
     assert e_d['directed'] == directed
     assert e_d['length'] == length
     
     e = Edge(weakref.ref(parent), weakref.ref(child))
     e_d = e.to_dict()
     
     assert e_d['from_'] == parent.name()
     assert e_d['to_'] == child.name()
     assert e_d['multiplicity'] == 1L
     assert e_d['weight_'] == 1.0
     assert e_d['length'](e) == 1.0
     assert e_d['type_'] == None
     assert e_d['directed'] == False 
     
Esempio n. 7
0
    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()
Esempio n. 8
0
 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()
Esempio n. 9
0
 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)
Esempio n. 10
0
 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
Esempio n. 11
0
 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)
Esempio n. 12
0
 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()