예제 #1
0
 def test_increment_from_node(self):
     node = Patch(0, [self.compartment])
     self.assertEqual(
         self.event.increment_state_variable_from_node(node, None), 0)
     node.update_subpopulation(self.compartment, 12)
     self.assertEqual(
         self.event.increment_state_variable_from_node(node, None), 12)
예제 #2
0
    def setUp(self):
        self.probability = 0.1
        self.compartment = 'a'
        self.new_compartment = 'z'
        self.edge_type = 'edge1'
        EDGE_ID = 'edgeid'
        self.event = TranslocateAndChange([Patch], self.probability,
                                          self.compartment, self.edge_type,
                                          self.new_compartment)

        self.nodes = [
            Patch(0, [self.compartment]),
            Patch(1, [self.compartment]),
            Patch(2, [self.compartment]),
            Patch(3, [self.compartment])
        ]
        self.edges = [(self.nodes[0], self.nodes[1], {
            EDGE_TYPE: self.edge_type,
            EDGE_ID: 1
        }),
                      (self.nodes[0], self.nodes[2], {
                          EDGE_TYPE: self.edge_type,
                          EDGE_ID: 2
                      }),
                      (self.nodes[2], self.nodes[3], {
                          EDGE_TYPE: 'edge2',
                          EDGE_ID: 3
                      })]

        self.network = MetapopulationNetwork([self.compartment], self.nodes,
                                             self.edges, [self.event])
예제 #3
0
    def test_update_rate(self):
        nodes = [Patch(0, ['a']), Patch(1, ['a'])]

        nodes[0].update_subpopulation('a', 1)
        nodes[1].update_subpopulation('a', 2)

        self.non_abstract_event.attach_nodes(nodes)
        self.non_abstract_event.update_rate(None)
        self.assertEqual(self.non_abstract_event.state_variable, 3)
        self.assertEqual(self.non_abstract_event.rate, 3 * self.prob)
 def test_increment_from_node(self):
     node = Patch(0, [self.mac_heal, self.mac_inf, self.bac])
     self.assertEqual(self.event.increment_state_variable_from_node(node, None), 0)
     node.update_subpopulation(self.bac, 4)
     self.assertEqual(self.event.increment_state_variable_from_node(node, None), 0)
     node.update_subpopulation(self.mac_inf, 5)
     node.update_subpopulation(self.bac, -4)
     self.assertEqual(self.event.increment_state_variable_from_node(node, None), 0)
     node.update_subpopulation(self.bac, 4)
     self.assertEqual(self.event.increment_state_variable_from_node(node, None), 5)
class PatchTestCase(unittest.TestCase):
    def setUp(self):
        self.node_id = 90
        self.compartments = ['a', 'b', 'c']
        self.position = (6, 7)
        self.patch = Patch(self.node_id, self.compartments, self.position)

    def test_initialise(self):
        self.assertItemsEqual(self.patch.subpopulations.keys(),
                              self.compartments)
        for key in self.patch.subpopulations:
            self.assertEqual(self.patch.subpopulations[key], 0)
        self.assertSequenceEqual(self.patch.position, self.position)
        self.assertEqual(self.patch.node_id, self.node_id)

    def test_update_subpopulation(self):
        self.patch.update_subpopulation(self.compartments[0], 1)
        self.assertEqual(self.patch.subpopulations[self.compartments[0]], 1)

        # Fail wrong key
        with self.assertRaises(AssertionError) as context:
            self.patch.update_subpopulation('FAIL', 1)
        self.assertEqual('Invalid compartment FAIL for update',
                         str(context.exception))

    def test_compartment_per_compartment(self):
        self.patch.update_subpopulation(self.compartments[0], 10)
        self.patch.update_subpopulation(self.compartments[1], 3)
        self.assertEqual(
            self.patch.compartment_per_compartment(self.compartments[0],
                                                   self.compartments[1]), 3)
예제 #6
0
    def test_destroy_internals_functions(self):
        node = Patch(0, ['a', 'b', 'c'])
        node.subpopulations['a'] = 5
        node.subpopulations['b'] = 10
        node.subpopulations['c'] = 18

        destroy_internals(['b', 'c'], 'a', node)

        self.assertEqual(node.subpopulations['a'], 5)
        self.assertEqual(node.subpopulations['b'], 8)
        self.assertEqual(node.subpopulations['c'], 15)
예제 #7
0
    def test_update_network(self):
        np.random.seed(101)

        nodes = [Patch(0, ['a']), Patch(1, ['a'])]

        nodes[0].update_subpopulation('a', 1)
        nodes[1].update_subpopulation('a', 2)

        self.non_abstract_event.attach_nodes(nodes)
        self.non_abstract_event.update_rate(None)
        self.non_abstract_event.update_network(None)

        self.assertEqual(nodes[0].subpopulations['a'], 1)
        self.assertEqual(nodes[1].subpopulations['a'], 3)
예제 #8
0
    def setUp(self):
        self.probability = 0.1
        self.compartment = 'a'
        self.internal_compartment = 'b'
        self.edge_type = 'edge1'
        EDGE_ID = 'edgeid'
        self.event_no_internals = Translocate([Patch], self.probability,
                                              self.compartment, self.edge_type)
        self.event_with_internals = Translocate(
            [Patch],
            self.probability,
            self.compartment,
            self.edge_type,
            internal_compartments=[self.internal_compartment])
        self.event_not_affected_by_degree = Translocate(
            [Patch],
            self.probability,
            self.compartment,
            self.edge_type,
            probability_increases_with_edges=False)

        self.nodes = [
            Patch(0, [self.compartment]),
            Patch(1, [self.compartment]),
            Patch(2, [self.compartment]),
            Patch(3, [self.compartment])
        ]
        self.edges = [(self.nodes[0], self.nodes[1], {
            EDGE_TYPE: self.edge_type,
            EDGE_ID: 1
        }),
                      (self.nodes[0], self.nodes[2], {
                          EDGE_TYPE: self.edge_type,
                          EDGE_ID: 2
                      }),
                      (self.nodes[2], self.nodes[3], {
                          EDGE_TYPE: 'edge2',
                          EDGE_ID: 3
                      })]
        events = [self.event_no_internals]

        self.network = MetapopulationNetwork([self.compartment], self.nodes,
                                             self.edges, events)
    def test_update_node(self):
        node = Patch(0, [self.comp_from, self.comp_to])
        node.update_subpopulation(self.comp_from, 6)
        self.event.update_node(node, None)
        self.assertEqual(node.subpopulations[self.comp_from], 5)
        self.assertEqual(node.subpopulations[self.comp_to], 1)

        node = Patch(0, [
            self.comp_from, self.comp_to, self.internal_comps[0],
            self.internal_comps[1]
        ])
        node.update_subpopulation(self.comp_from, 6)
        node.update_subpopulation(self.internal_comps[0], 12)
        node.update_subpopulation(self.internal_comps[1], 33)
        self.event_with_internals.update_node(node, None)
        self.assertEqual(node.subpopulations[self.comp_from], 5)
        self.assertEqual(node.subpopulations[self.comp_to], 1)
        self.assertEqual(node.subpopulations[self.internal_comps[0]], 10)
        self.assertEqual(node.subpopulations[self.internal_comps[1]], 28)
    def test_update_node(self):
        node = Patch(0, [
            self.compartment, self.influencing_comps[0],
            self.influencing_comps[1], self.new_comp
        ])
        node.subpopulations[self.compartment] = 5
        node.subpopulations[self.influencing_comps[0]] = 3
        node.subpopulations[self.influencing_comps[1]] = 8

        self.event.update_node(node, None)
        self.assertEqual(node.subpopulations[self.compartment], 4)
        self.assertEqual(node.subpopulations[self.influencing_comps[0]], 3)
        self.assertEqual(node.subpopulations[self.influencing_comps[1]], 8)
        self.assertEqual(node.subpopulations[self.new_comp], 0)

        node = Patch(0, [
            self.compartment, self.influencing_comps[0],
            self.influencing_comps[1], self.new_comp
        ])
        node.subpopulations[self.compartment] = 5
        node.subpopulations[self.influencing_comps[0]] = 3
        node.subpopulations[self.influencing_comps[1]] = 8

        self.event_destroy_influencer.update_node(node, None)
        self.assertEqual(node.subpopulations[self.compartment], 4)
        self.assertEqual(node.subpopulations[self.influencing_comps[0]], 2)
        self.assertEqual(node.subpopulations[self.influencing_comps[1]], 8)
        self.assertEqual(node.subpopulations[self.new_comp], 0)

        node = Patch(0, [
            self.compartment, self.influencing_comps[0],
            self.influencing_comps[1], self.new_comp
        ])
        node.subpopulations[self.compartment] = 5
        node.subpopulations[self.influencing_comps[0]] = 3
        node.subpopulations[self.influencing_comps[1]] = 8

        self.event_change_influencer.update_node(node, None)
        self.assertEqual(node.subpopulations[self.compartment], 4)
        self.assertEqual(node.subpopulations[self.influencing_comps[0]], 3)
        self.assertEqual(node.subpopulations[self.influencing_comps[1]], 7)
        self.assertEqual(node.subpopulations[self.new_comp], 1)
 def test_increment_from_node(self):
     node = Patch(0, [self.comp_from, self.comp_to] + self.externals)
     self.assertEqual(
         self.event.increment_state_variable_from_node(node, None), 0)
     node.update_subpopulation(self.comp_from, 10)
     self.assertEqual(
         self.event.increment_state_variable_from_node(node, None), 0)
     node.update_subpopulation(self.externals[0], 1)
     self.assertEqual(
         self.event.increment_state_variable_from_node(node, None), 10 * 1)
     node.update_subpopulation(self.externals[1], 2)
     self.assertEqual(
         self.event.increment_state_variable_from_node(node, None),
         10 * (1 + 2))
 def test_increment_from_node(self):
     node = Patch(0, [self.bac_original, self.bac_new, self.mac_original, self.mac_new])
     self.assertEqual(self.event_no_change.increment_state_variable_from_node(node, None), 0)
     node.update_subpopulation(self.bac_original, 13)
     self.assertEqual(self.event_no_change.increment_state_variable_from_node(node, None), 0)
     node.update_subpopulation(self.mac_original, 5)
     self.assertEqual(self.event_no_change.increment_state_variable_from_node(node, None), 13 * 5)
 def test_increment_from_node(self):
     node = Patch(0, [self.comp_from, self.comp_to] + self.inf_comps)
     self.assertEqual(
         self.event.increment_state_variable_from_node(node, None), 0)
     node.update_subpopulation(self.comp_from, 10)
     self.assertEqual(
         self.event.increment_state_variable_from_node(node, None), 0)
     node.update_subpopulation(self.inf_comps[0], 3)
     self.assertEqual(
         self.event.increment_state_variable_from_node(node, None),
         10.0 * 3.0 / 13.0)
 def test_increment_from_node(self):
     node = Patch(0, [
         self.compartment, self.influencing_comps[0],
         self.influencing_comps[1], self.new_comp
     ])
     self.assertEqual(
         self.event.increment_state_variable_from_node(node, None), 0)
     node.subpopulations[self.compartment] = 12
     self.assertEqual(
         self.event.increment_state_variable_from_node(node, None), 0)
     node.subpopulations[self.compartment] = 0
     node.subpopulations[self.influencing_comps[0]] = 12
     self.assertEqual(
         self.event.increment_state_variable_from_node(node, None), 0)
     node.subpopulations[self.compartment] = 3
     node.subpopulations[self.influencing_comps[0]] = 4
     self.assertEqual(
         self.event.increment_state_variable_from_node(node, None), 12)
     node.subpopulations[self.influencing_comps[1]] = 2
     self.assertEqual(
         self.event.increment_state_variable_from_node(node, None), 18)
 def __init__(self, compartments, events):
     node = Patch(0, compartments)
     MetapopulationNetwork.__init__(self, compartments, [node], [], events)
예제 #16
0
 def test_update_node(self):
     node = Patch(0, [self.compartment])
     self.event.update_node(node, None)
     self.assertEqual(node.subpopulations[self.compartment], 1)
 def setUp(self):
     self.node_id = 90
     self.compartments = ['a', 'b', 'c']
     self.position = (6, 7)
     self.patch = Patch(self.node_id, self.compartments, self.position)
    def test_update_node(self):
        node = Patch(0, [
            self.compartment, self.internal_comp_destroy,
            self.internal_comp_from, self.internal_comp_to
        ])
        node.update_subpopulation(self.compartment, 5)
        node.update_subpopulation(self.internal_comp_destroy, 10)
        node.update_subpopulation(self.internal_comp_from, 18)
        node.update_subpopulation(self.internal_comp_to, 0)
        self.event.update_node(node, None)
        self.assertEqual(node.subpopulations[self.compartment], 4)
        self.assertEqual(node.subpopulations[self.internal_comp_destroy], 10)
        self.assertEqual(node.subpopulations[self.internal_comp_from], 18)
        self.assertEqual(node.subpopulations[self.internal_comp_to], 0)

        node = Patch(0, [
            self.compartment, self.internal_comp_destroy,
            self.internal_comp_from, self.internal_comp_to
        ])
        node.update_subpopulation(self.compartment, 5)
        node.update_subpopulation(self.internal_comp_destroy, 10)
        node.update_subpopulation(self.internal_comp_from, 18)
        node.update_subpopulation(self.internal_comp_to, 0)
        self.event_with_internals_destroyed.update_node(node, None)
        self.assertEqual(node.subpopulations[self.compartment], 4)
        self.assertEqual(node.subpopulations[self.internal_comp_destroy], 8)
        self.assertEqual(node.subpopulations[self.internal_comp_from], 18)
        self.assertEqual(node.subpopulations[self.internal_comp_to], 0)

        node = Patch(0, [
            self.compartment, self.internal_comp_destroy,
            self.internal_comp_from, self.internal_comp_to
        ])
        node.update_subpopulation(self.compartment, 5)
        node.update_subpopulation(self.internal_comp_destroy, 10)
        node.update_subpopulation(self.internal_comp_from, 18)
        node.update_subpopulation(self.internal_comp_to, 0)
        self.event_with_internals_changed.update_node(node, None)
        self.assertEqual(node.subpopulations[self.compartment], 4)
        self.assertEqual(node.subpopulations[self.internal_comp_destroy], 10)
        self.assertEqual(node.subpopulations[self.internal_comp_from], 15)
        self.assertEqual(node.subpopulations[self.internal_comp_to], 3)
    def test_update_node(self):
        node = Patch(0, [self.comp_from, self.comp_to, self.new_external] +
                     self.externals)
        node.update_subpopulation(self.comp_from, 10)
        node.update_subpopulation(self.externals[0], 5)
        node.update_subpopulation(self.externals[1], 3)
        self.event.update_node(node, None)
        self.assertEqual(node.subpopulations[self.comp_from], 9)
        self.assertEqual(node.subpopulations[self.comp_to], 1)
        self.assertEqual(node.subpopulations[self.externals[0]], 5)
        self.assertEqual(node.subpopulations[self.externals[1]], 3)
        self.assertEqual(node.subpopulations[self.new_external], 0)

        node = Patch(0, [self.comp_from, self.comp_to, self.new_external] +
                     self.externals)
        node.update_subpopulation(self.comp_from, 10)
        node.update_subpopulation(self.externals[0], 5)
        node.update_subpopulation(self.externals[1], 3)
        self.event_change_internals.update_node(node, None)
        self.assertEqual(node.subpopulations[self.comp_from], 9)
        self.assertEqual(node.subpopulations[self.comp_to], 1)
        self.assertEqual(node.subpopulations[self.externals[0]], 4)
        self.assertEqual(node.subpopulations[self.externals[1]], 3)
        self.assertEqual(node.subpopulations[self.new_external], 1)
    def test_update_node(self):
        node = Patch(0, [self.mac_heal, self.mac_inf, self.bac])
        node.update_subpopulation(self.bac, 20)
        node.update_subpopulation(self.mac_inf, 4)
        self.event.update_node(node, None)
        self.assertEqual(node.subpopulations[self.mac_inf], 4)
        self.assertEqual(node.subpopulations[self.bac], 19)
        self.assertEqual(node.subpopulations[self.mac_heal], 0)

        node = Patch(0, [self.mac_heal, self.mac_inf, self.bac])
        node.update_subpopulation(self.bac, 4)
        node.update_subpopulation(self.mac_inf, 4)
        self.event.update_node(node, None)
        self.assertEqual(node.subpopulations[self.mac_inf], 3)
        self.assertEqual(node.subpopulations[self.bac], 3)
        self.assertEqual(node.subpopulations[self.mac_heal], 1)
예제 #21
0
 def test_attach_nodes(self):
     nodes = [Patch(0, ['a']), Patch(1, ['b'])]
     self.event.attach_nodes(nodes)
     self.assertItemsEqual(self.event.nodes_impacted, nodes)
    def test_update_node(self):
        node = Patch(0, [self.bac_original, self.bac_new, self.mac_original, self.mac_new])
        node.update_subpopulation(self.bac_original, 2)
        node.update_subpopulation(self.mac_original, 2)
        self.event_no_change.update_node(node, None)
        self.assertEqual(node.subpopulations[self.bac_original], 1)
        self.assertEqual(node.subpopulations[self.mac_original], 2)
        self.assertEqual(node.subpopulations[self.bac_new], 0)
        self.assertEqual(node.subpopulations[self.mac_new], 0)

        node = Patch(0, [self.bac_original, self.bac_new, self.mac_original, self.mac_new])
        node.update_subpopulation(self.bac_original, 2)
        node.update_subpopulation(self.mac_original, 2)
        self.event_bac_change.update_node(node, None)
        self.assertEqual(node.subpopulations[self.bac_original], 1)
        self.assertEqual(node.subpopulations[self.mac_original], 2)
        self.assertEqual(node.subpopulations[self.bac_new], 1)
        self.assertEqual(node.subpopulations[self.mac_new], 0)

        node = Patch(0, [self.bac_original, self.bac_new, self.mac_original, self.mac_new])
        node.update_subpopulation(self.bac_original, 2)
        node.update_subpopulation(self.mac_original, 2)
        self.event_mac_change.update_node(node, None)
        self.assertEqual(node.subpopulations[self.bac_original], 1)
        self.assertEqual(node.subpopulations[self.mac_original], 1)
        self.assertEqual(node.subpopulations[self.bac_new], 0)
        self.assertEqual(node.subpopulations[self.mac_new], 1)

        node = Patch(0, [self.bac_original, self.bac_new, self.mac_original, self.mac_new])
        node.update_subpopulation(self.bac_original, 2)
        node.update_subpopulation(self.mac_original, 2)
        self.event_both_change.update_node(node, None)
        self.assertEqual(node.subpopulations[self.bac_original], 1)
        self.assertEqual(node.subpopulations[self.mac_original], 1)
        self.assertEqual(node.subpopulations[self.bac_new], 1)
        self.assertEqual(node.subpopulations[self.mac_new], 1)