Exemplo n.º 1
0
    def setUp(self):
        self.probability = 0.1
        self.event = Event(self.probability)

        class NAEvent(Event):

            def __init__(self, prob, type):
                self.type = type
                Event.__init__(self, prob)

            def increment_from_node(self, node, network):
                return node.subpopulations[self.type]

            def update_network(self, chosen_node, network):
                chosen_node.update(self.type, 1)

        self.type = 'a'
        self.non_abstract_event = NAEvent(self.probability, self.type)

        self.events = [self.non_abstract_event]
        self.keys = ['a']
        node1 = Patch(1, self.keys)
        node2 = Patch(2, self.keys)
        self.nodes = [node1, node2]
        self.edges = [(node1, node2, {EDGE_TYPE:'edge'})]
        self.network = MetapopulationNetwork(self.keys, self.events, self.nodes, self.edges)
Exemplo n.º 2
0
 def test_increment_from_node(self):
     node = Patch(0, self.keys)
     node.update(self.event.class_from, 1)
     self.assertEqual(self.event.increment_from_node(node, None), 1)
     node.update(self.event.class_from, 13)
     self.assertEqual(self.event.increment_from_node(node, None), 14)
     node2 = Patch(2, self.keys)
     node2.update(self.event.class_to, 13)
     self.assertEqual(self.event.increment_from_node(node2, None), 0)
Exemplo n.º 3
0
 def test_increment_from_node(self):
     node = Patch(0, self.keys)
     # Has neither
     self.assertEqual(self.event.increment_from_node(node, None), 0)
     # Has a, no b
     node.subpopulations[self.keys[0]] = 1
     self.assertEqual(self.event.increment_from_node(node, None), 0)
     # Has b, no a
     node.subpopulations[self.keys[0]] = 0
     node.subpopulations[self.keys[1]] = 1
     self.assertEqual(self.event.increment_from_node(node, None), 0)
     # Has both
     node.subpopulations[self.keys[0]] = 2
     node.subpopulations[self.keys[1]] = 3
     self.assertEqual(self.event.increment_from_node(node, None), 6)
Exemplo n.º 4
0
 def test_increment_from_node(self):
     node = Patch(0, self.keys)
     # Has a, no c
     node.subpopulations[self.keys[0]] = 1
     node.subpopulations[self.keys[2]] = 0
     self.assertEqual(self.event.increment_from_node(node, None), 0)
     # Has c, no a
     node.subpopulations[self.keys[0]] = 0
     node.subpopulations[self.keys[2]] = 1
     self.assertEqual(self.event.increment_from_node(node, None), 0)
     # Has both a and c
     node.subpopulations[self.keys[0]] = 3
     node.subpopulations[self.keys[2]] = 4
     self.assertEqual(self.event.increment_from_node(node, None), 12)
     # Has neither a nor c
     node.subpopulations[self.keys[0]] = 0
     node.subpopulations[self.keys[2]] = 0
     self.assertEqual(self.event.increment_from_node(node, None), 0)
Exemplo n.º 5
0
    def test_calculate_bacteria_moving(self):
        node = Patch(0, [MACROPHAGE_INFECTED, BACTERIA_INTRACELLULAR])
        node.update(MACROPHAGE_INFECTED, 10)
        node.update(BACTERIA_INTRACELLULAR, 54)

        has_intra_moves = self.event_has_intra.amounts_to_move(node)
        self.assertItemsEqual(has_intra_moves.keys(), [MACROPHAGE_INFECTED, BACTERIA_INTRACELLULAR])
        self.assertEqual(has_intra_moves[MACROPHAGE_INFECTED], 1)
        self.assertEqual(has_intra_moves[BACTERIA_INTRACELLULAR], int(round(54/10)))

        no_intra_moves = self.event_no_intra.amounts_to_move(node)
        self.assertItemsEqual(no_intra_moves.keys(), [MACROPHAGE_REGULAR])
        self.assertEqual(no_intra_moves[MACROPHAGE_REGULAR], 1)
Exemplo n.º 6
0
 def test_update_network(self):
     node = Patch(0, self.keys)
     node.update(self.event.class_from, 1)
     self.event.update_network(node, None)
     self.assertEqual(node.subpopulations[self.class_from], 0)
     self.assertEqual(node.subpopulations[self.class_to], 1)
    def test_update_network(self):
        # Release load and has intra
        node = Patch(
            0, [BACTERIA_INTRACELLULAR, self.class_die, self.class_released])
        node.update(self.class_die, 10)
        node.update(BACTERIA_INTRACELLULAR, 100)
        self.event_release.update_network(node, None)
        expected_bac_released = 100 / 10
        self.assertEqual(node.subpopulations[self.class_released],
                         expected_bac_released)
        self.assertEqual(node.subpopulations[self.class_die], 10 - 1)

        # Not release load and has intra
        node = Patch(
            0, [BACTERIA_INTRACELLULAR, self.class_die, self.class_released])
        node.update(self.class_die, 10)
        node.update(BACTERIA_INTRACELLULAR, 100)
        self.event_destroy.update_network(node, None)
        expected_bac_released = 0
        self.assertEqual(node.subpopulations[self.class_released],
                         expected_bac_released)
        self.assertEqual(node.subpopulations[self.class_die], 10 - 1)

        # Release load and doesn't have intra
        no_intra_event_release = MacrophageDeath(MACROPHAGE_REGULAR, 0.1, True,
                                                 self.class_released)
        node = Patch(
            0,
            [BACTERIA_INTRACELLULAR, MACROPHAGE_REGULAR, self.class_released])
        node.update(MACROPHAGE_REGULAR, 10)
        node.update(BACTERIA_INTRACELLULAR, 100)
        no_intra_event_release.update_network(node, None)
        expected_bac_released = 0
        self.assertEqual(node.subpopulations[self.class_released],
                         expected_bac_released)
        self.assertEqual(node.subpopulations[MACROPHAGE_REGULAR], 10 - 1)

        # Not release load and doesn't have intra
        no_intra_event_destroy = MacrophageDeath(MACROPHAGE_REGULAR, 0.1,
                                                 False)
        node = Patch(
            0,
            [BACTERIA_INTRACELLULAR, MACROPHAGE_REGULAR, self.class_released])
        node.update(MACROPHAGE_REGULAR, 10)
        node.update(BACTERIA_INTRACELLULAR, 100)
        no_intra_event_destroy.update_network(node, None)
        expected_bac_released = 0
        self.assertEqual(node.subpopulations[self.class_released],
                         expected_bac_released)
        self.assertEqual(node.subpopulations[MACROPHAGE_REGULAR], 10 - 1)
Exemplo n.º 8
0
    def test_update_network(self):
        node = Patch(0, [BACTERIA_INTRACELLULAR, MACROPHAGE_REGULAR, MACROPHAGE_INFECTED])
        node.update(BACTERIA_INTRACELLULAR, 100)
        node.update(MACROPHAGE_INFECTED, 10)
        self.event.update_network(node, None)
        self.assertEqual(node.subpopulations[BACTERIA_INTRACELLULAR], 99)
        self.assertEqual(node.subpopulations[MACROPHAGE_INFECTED], 10)

        node = Patch(0, [BACTERIA_INTRACELLULAR, MACROPHAGE_REGULAR, MACROPHAGE_INFECTED])
        node.update(BACTERIA_INTRACELLULAR, 10)
        node.update(MACROPHAGE_INFECTED, 10)
        self.event.update_network(node, None)
        self.assertEqual(node.subpopulations[BACTERIA_INTRACELLULAR], 9)
        self.assertEqual(node.subpopulations[MACROPHAGE_REGULAR], 1)
        self.assertEqual(node.subpopulations[MACROPHAGE_INFECTED], 9)
Exemplo n.º 9
0
    def test_update_network(self):

        node = Patch(0, [BACTERIA_FAST, MACROPHAGE, MACROPHAGE_INFECTED])
        node.update(BACTERIA_FAST, 10)
        node.update(MACROPHAGE, 10)
        self.event_destroy_no_new_mac.update_network(node, None)
        self.assertEqual(node.subpopulations[BACTERIA_FAST], 10-1)
        self.assertEqual(node.subpopulations[MACROPHAGE], 10)
        self.assertEqual(node.subpopulations[MACROPHAGE_INFECTED], 0)

        node = Patch(0, [BACTERIA_FAST, MACROPHAGE, MACROPHAGE_INFECTED])
        node.update(BACTERIA_FAST, 10)
        node.update(MACROPHAGE, 10)
        self.event_destroy_new_mac.update_network(node, None)
        self.assertEqual(node.subpopulations[BACTERIA_FAST], 10 - 1)
        self.assertEqual(node.subpopulations[MACROPHAGE], 10-1)
        self.assertEqual(node.subpopulations[MACROPHAGE_INFECTED], 1)

        node = Patch(0, [BACTERIA_FAST, BACTERIA_INTRACELLULAR, MACROPHAGE, MACROPHAGE_INFECTED])
        node.update(BACTERIA_FAST, 10)
        node.update(MACROPHAGE, 10)
        self.event_retain_and_change.update_network(node, None)
        self.assertEqual(node.subpopulations[BACTERIA_FAST], 10 - 1)
        self.assertEqual(node.subpopulations[MACROPHAGE], 10 - 1)
        self.assertEqual(node.subpopulations[MACROPHAGE_INFECTED], 1)
        self.assertEqual(node.subpopulations[BACTERIA_INTRACELLULAR], 1)

        node = Patch(0, [BACTERIA_FAST, BACTERIA_INTRACELLULAR, MACROPHAGE, MACROPHAGE_INFECTED])
        node.update(BACTERIA_FAST, 10)
        node.update(MACROPHAGE_INFECTED, 10)
        self.event_retain_dont_change.update_network(node, None)
        self.assertEqual(node.subpopulations[BACTERIA_FAST], 10 - 1)
        self.assertEqual(node.subpopulations[MACROPHAGE], 0)
        self.assertEqual(node.subpopulations[MACROPHAGE_INFECTED], 10)
        self.assertEqual(node.subpopulations[BACTERIA_INTRACELLULAR], 1)
Exemplo n.º 10
0
 def test_update_network(self):
     node = Patch(0, self.keys)
     node.subpopulations[self.keys[0]] = 3
     self.event.update_network(node, None)
     self.assertEqual(node.subpopulations[self.keys[0]], 2)
Exemplo n.º 11
0
 def test_increment_from_node(self):
     node = Patch(0, self.keys)
     self.assertEqual(self.event.increment_from_node(node, None), 0)
     node.subpopulations[self.keys[0]] = 2
     self.assertEqual(self.event.increment_from_node(node, None), 2)
Exemplo n.º 12
0
 def setUp(self):
     self._id = 1
     self.keys = ['a', 'b', 'c']
     self.position = (9, 8)
     self.patch = Patch(self._id, self.keys, self.position)
Exemplo n.º 13
0
class PatchTestCase(unittest.TestCase):
    def setUp(self):
        self._id = 1
        self.keys = ['a', 'b', 'c']
        self.position = (9, 8)
        self.patch = Patch(self._id, self.keys, self.position)

    def test_initialise(self):
        self.assertEqual(self.patch.id, self._id)
        self.assertItemsEqual(self.patch.subpopulations.keys(), self.keys)
        for key in self.keys:
            self.assertEqual(self.patch.subpopulations[key], 0)
        self.assertSequenceEqual(self.patch.position, self.position)
        self.assertTrue(isinstance(self.patch.degrees, dict))

    def test_update_correct(self):
        self.patch.update(self.keys[0], 1)
        self.assertEqual(self.patch.subpopulations[self.keys[0]], 1)

    def test_update_fail_wrong_key(self):
        wrong_key = 'fail'
        self.assertTrue(wrong_key not in self.keys)
        with self.assertRaises(AssertionError) as context:
            self.patch.update(wrong_key, 1)
        self.assertTrue("update_node: Invalid class " +
                        wrong_key in context.exception)

    def test_update_fail_invalid_value(self):
        with self.assertRaises(AssertionError) as context:
            self.patch.update(self.keys[0], -1)
        self.assertTrue(
            "update_node: Count cannot drop below zero" in context.exception)

    def test_type_per_type(self):
        self.patch.update(self.keys[0], 10)
        self.patch.update(self.keys[1], 5)
        self.patch.update(self.keys[2], 3)
        self.assertEqual(self.patch.type_per_type(self.keys[0], self.keys[1]),
                         2)
        self.assertEqual(self.patch.type_per_type(self.keys[0], self.keys[2]),
                         3)
        self.assertEqual(self.patch.type_per_type(self.keys[1], self.keys[2]),
                         1)