Beispiel #1
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])
Beispiel #2
0
    def setUp(self):
        self.compartments = ['a', 'b', 'c']

        self.nodes = []
        for a in range(5):
            self.nodes.append(Patch_type1(a, self.compartments))
        for a in range(5, 10):
            self.nodes.append(Patch_type2(a, self.compartments))

        self.edge_types = ['edge1', 'edge2']
        edges = [(0, 1), (1, 2), (2, 3), (3, 4), (0, 3), (0, 4)]
        self.edges = []
        for n1, n2 in edges:
            self.edges.append((self.nodes[n1], self.nodes[n2], {
                EDGE_TYPE: self.edge_types[0]
            }))
        self.edges.append((self.nodes[0], self.nodes[9], {
            EDGE_TYPE: self.edge_types[1]
        }))

        class NAEvent(Event):
            def __init__(self, node_types, prob, value):
                self.value = value
                Event.__init__(self, node_types, prob)

            def increment_state_variable_from_node(self, node, network):
                return node.subpopulations[self.value]

            def update_node(self, node, network):
                node.update_subpopulation(self.value, 1)

        self.event_node_type1 = NAEvent([Patch_type1], 0.1, 'a')
        self.event_node_type2 = NAEvent([Patch_type2], 0.2, 'a')
        self.event_node_type_both = NAEvent([Patch_type1, Patch_type2], 0.3,
                                            'a')

        self.events = [
            self.event_node_type1, self.event_node_type2,
            self.event_node_type_both
        ]
        self.network = MetapopulationNetwork(self.compartments, self.nodes,
                                             self.edges, self.events)
Beispiel #3
0
    def test_add_node(self):
        network = MetapopulationNetwork(['a'], [self.nodes[0]], [],
                                        [self.event_node_type1])
        node = Patch(0, ['a'])
        network.add_node(node)
        self.assertTrue(network.has_node(node))

        # Fail not a patch
        with self.assertRaises(AssertionError) as context:
            network.add_node(9)
        self.assertEqual('Node 9 is not a Patch object',
                         str(context.exception))
Beispiel #4
0
    def test_increment_from_nodes(self):
        nodes = []
        for a in range(10):
            nodes.append(BronchopulmonarySegment(a, [self.comp], 0, 0, (0, 0)))

        edges = [(nodes[0], nodes[1], {
            EDGE_TYPE: LYMPHATIC_VESSEL,
            DIRECTION: nodes[0],
            'edgeid': 0,
            FLOW_RATE: 0.9
        })]
        edges.append((nodes[0], nodes[2], {
            EDGE_TYPE: LYMPHATIC_VESSEL,
            DIRECTION: nodes[0],
            'edgeid': 1,
            FLOW_RATE: 0.7
        }))
        edges.append((nodes[2], nodes[3], {
            EDGE_TYPE: LYMPHATIC_VESSEL,
            DIRECTION: nodes[3],
            'edgeid': 2,
            FLOW_RATE: 0.5
        }))
        edges.append((nodes[0], nodes[3], {
            EDGE_TYPE: HAEMATOGENOUS,
            DIRECTION: nodes[3],
            'edgeid': 3
        }))
        events = [self.event, self.event_no_direction]

        network = MetapopulationNetwork([self.comp], nodes, edges, events)

        nodes[0].update_subpopulation(self.comp, 2)
        nodes[1].update_subpopulation(self.comp, 3)
        nodes[2].update_subpopulation(self.comp, 5)
        nodes[3].update_subpopulation(self.comp, 7)

        self.assertEqual(
            self.event.increment_state_variable_from_node(nodes[0], network),
            2 * 0)
        self.assertEqual(
            self.event.increment_state_variable_from_node(nodes[1], network),
            3 * 0.9)
        self.assertEqual(
            self.event.increment_state_variable_from_node(nodes[2], network),
            5 * (0.7 + 0.5))
        self.assertEqual(
            self.event.increment_state_variable_from_node(nodes[3], network),
            7 * 0)
Beispiel #5
0
    def test_choose_neighbour(self):
        nodes = []
        for a in range(10):
            nodes.append(BronchopulmonarySegment(a, [self.comp], 0, 0, (0, 0)))

        edges = [(nodes[0], nodes[1], {EDGE_TYPE: BRONCHUS, WEIGHT: 100})]
        for b in range(2, 10):
            edges.append((nodes[0], nodes[b], {
                EDGE_TYPE: BRONCHUS,
                WEIGHT: 1
            }))

        events = [self.event, self.event_weighted]

        network = MetapopulationNetwork([self.comp], nodes, edges, events)

        np.random.seed(101)
        edges = self.event_weighted.viable_edges(nodes[0], network)
        self.assertEqual(self.event_weighted.choose_neighbour(edges), nodes[1])
Beispiel #6
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)
Beispiel #7
0
    def test_viable_edges(self):
        nodes = []
        for a in range(10):
            nodes.append(BronchopulmonarySegment(a, [self.comp], 0, 0, (0, 0)))

        edges = [(nodes[0], nodes[1], {
            EDGE_TYPE: LYMPHATIC_VESSEL,
            DIRECTION: nodes[0],
            'edgeid': 0
        })]
        edges.append((nodes[0], nodes[2], {
            EDGE_TYPE: LYMPHATIC_VESSEL,
            DIRECTION: nodes[0],
            'edgeid': 1
        }))
        edges.append((nodes[2], nodes[3], {
            EDGE_TYPE: LYMPHATIC_VESSEL,
            DIRECTION: nodes[3],
            'edgeid': 2
        }))
        edges.append((nodes[0], nodes[3], {
            EDGE_TYPE: HAEMATOGENOUS,
            DIRECTION: nodes[3],
            'edgeid': 3
        }))
        events = [self.event, self.event_no_direction]

        network = MetapopulationNetwork([self.comp], nodes, edges, events)

        self.assertEqual(len(self.event.viable_edges(nodes[0], network)), 0)
        self.assertEqual(len(self.event.viable_edges(nodes[1], network)), 1)
        self.assertItemsEqual([
            data['edgeid']
            for (_, data) in self.event.viable_edges(nodes[1], network)
        ], [0])
        self.assertEqual(len(self.event.viable_edges(nodes[2], network)), 2)
        self.assertItemsEqual([
            data['edgeid']
            for (_, data) in self.event.viable_edges(nodes[2], network)
        ], [1, 2])
        self.assertEqual(len(self.event.viable_edges(nodes[3], network)), 0)

        self.assertEqual(
            len(self.event_no_direction.viable_edges(nodes[0], network)), 2)
        self.assertItemsEqual([
            data['edgeid']
            for (_, data
                 ) in self.event_no_direction.viable_edges(nodes[0], network)
        ], [0, 1])
        self.assertEqual(
            len(self.event_no_direction.viable_edges(nodes[1], network)), 1)
        self.assertItemsEqual([
            data['edgeid']
            for (_, data
                 ) in self.event_no_direction.viable_edges(nodes[1], network)
        ], [0])
        self.assertEqual(
            len(self.event_no_direction.viable_edges(nodes[2], network)), 2)
        self.assertItemsEqual([
            data['edgeid']
            for (_, data
                 ) in self.event_no_direction.viable_edges(nodes[2], network)
        ], [1, 2])
        self.assertEqual(
            len(self.event_no_direction.viable_edges(nodes[3], network)), 1)
        self.assertItemsEqual([
            data['edgeid']
            for (_, data
                 ) in self.event_no_direction.viable_edges(nodes[3], network)
        ], [2])
Beispiel #8
0
    def test_add_edge(self):
        node = Patch_type1(0, self.compartments)
        node2 = Patch_type1(1, self.compartments)

        network = MetapopulationNetwork(['a'], [node, node2], [],
                                        [self.event_node_type1])

        self.assertEqual(len(node.neighbours), 0)
        self.assertEqual(len(node2.neighbours), 0)

        edge_data = {EDGE_TYPE: 'edge'}
        network.add_edge(node, node2, edge_data)
        self.assertTrue(network.has_edge(node, node2))
        self.assertItemsEqual(network.edge[node][node2].keys(),
                              edge_data.keys())
        self.assertEqual(network.edge[node][node2][EDGE_TYPE], 'edge')
        self.assertEqual(len(node.neighbours), 1)
        self.assertEqual(node.neighbours[0][0], node2)
        self.assertEqual(node.neighbours[0][1], edge_data)
        self.assertEqual(len(node2.neighbours), 1)
        self.assertEqual(node2.neighbours[0][0], node)
        self.assertEqual(node2.neighbours[0][1], edge_data)

        # Fail node not present
        node3 = Patch(2, ['a'])
        # Fail not a patch
        with self.assertRaises(AssertionError) as context:
            network.add_edge(node, node3, edge_data)
        self.assertTrue('not present in network' in str(context.exception))
        with self.assertRaises(AssertionError) as context:
            network.add_edge(node3, node2, edge_data)
        self.assertTrue('not present in network' in str(context.exception))
        with self.assertRaises(AssertionError) as context:
            network.add_edge(node, node2, {})
        self.assertTrue(
            'Edge type not specified for edge' in str(context.exception))
Beispiel #9
0
class MetapopulationNetworkTestCase(unittest.TestCase):
    def setUp(self):
        self.compartments = ['a', 'b', 'c']

        self.nodes = []
        for a in range(5):
            self.nodes.append(Patch_type1(a, self.compartments))
        for a in range(5, 10):
            self.nodes.append(Patch_type2(a, self.compartments))

        self.edge_types = ['edge1', 'edge2']
        edges = [(0, 1), (1, 2), (2, 3), (3, 4), (0, 3), (0, 4)]
        self.edges = []
        for n1, n2 in edges:
            self.edges.append((self.nodes[n1], self.nodes[n2], {
                EDGE_TYPE: self.edge_types[0]
            }))
        self.edges.append((self.nodes[0], self.nodes[9], {
            EDGE_TYPE: self.edge_types[1]
        }))

        class NAEvent(Event):
            def __init__(self, node_types, prob, value):
                self.value = value
                Event.__init__(self, node_types, prob)

            def increment_state_variable_from_node(self, node, network):
                return node.subpopulations[self.value]

            def update_node(self, node, network):
                node.update_subpopulation(self.value, 1)

        self.event_node_type1 = NAEvent([Patch_type1], 0.1, 'a')
        self.event_node_type2 = NAEvent([Patch_type2], 0.2, 'a')
        self.event_node_type_both = NAEvent([Patch_type1, Patch_type2], 0.3,
                                            'a')

        self.events = [
            self.event_node_type1, self.event_node_type2,
            self.event_node_type_both
        ]
        self.network = MetapopulationNetwork(self.compartments, self.nodes,
                                             self.edges, self.events)

    def test_add_node(self):
        network = MetapopulationNetwork(['a'], [self.nodes[0]], [],
                                        [self.event_node_type1])
        node = Patch(0, ['a'])
        network.add_node(node)
        self.assertTrue(network.has_node(node))

        # Fail not a patch
        with self.assertRaises(AssertionError) as context:
            network.add_node(9)
        self.assertEqual('Node 9 is not a Patch object',
                         str(context.exception))

    def test_add_edge(self):
        node = Patch_type1(0, self.compartments)
        node2 = Patch_type1(1, self.compartments)

        network = MetapopulationNetwork(['a'], [node, node2], [],
                                        [self.event_node_type1])

        self.assertEqual(len(node.neighbours), 0)
        self.assertEqual(len(node2.neighbours), 0)

        edge_data = {EDGE_TYPE: 'edge'}
        network.add_edge(node, node2, edge_data)
        self.assertTrue(network.has_edge(node, node2))
        self.assertItemsEqual(network.edge[node][node2].keys(),
                              edge_data.keys())
        self.assertEqual(network.edge[node][node2][EDGE_TYPE], 'edge')
        self.assertEqual(len(node.neighbours), 1)
        self.assertEqual(node.neighbours[0][0], node2)
        self.assertEqual(node.neighbours[0][1], edge_data)
        self.assertEqual(len(node2.neighbours), 1)
        self.assertEqual(node2.neighbours[0][0], node)
        self.assertEqual(node2.neighbours[0][1], edge_data)

        # Fail node not present
        node3 = Patch(2, ['a'])
        # Fail not a patch
        with self.assertRaises(AssertionError) as context:
            network.add_edge(node, node3, edge_data)
        self.assertTrue('not present in network' in str(context.exception))
        with self.assertRaises(AssertionError) as context:
            network.add_edge(node3, node2, edge_data)
        self.assertTrue('not present in network' in str(context.exception))
        with self.assertRaises(AssertionError) as context:
            network.add_edge(node, node2, {})
        self.assertTrue(
            'Edge type not specified for edge' in str(context.exception))

    def test_initialise(self):
        self.assertItemsEqual(self.compartments, self.network.compartments)
        self.assertItemsEqual(self.network.nodes(), self.nodes)
        self.assertEqual(len(self.network.edges()), len(self.edges))
        for (u, v, data) in self.edges:
            self.assertTrue(self.network.has_edge(u, v))
        self.assertItemsEqual(self.network.events, self.events)

        self.assertItemsEqual(self.event_node_type1.nodes_impacted,
                              self.nodes[0:5])
        self.assertItemsEqual(self.event_node_type2.nodes_impacted,
                              self.nodes[5:10])
        self.assertItemsEqual(self.event_node_type_both.nodes_impacted,
                              self.nodes)

        self.assertEqual(self.network.time, 0.0)

    def test_seed_network_node_type(self):
        seeding = {self.compartments[0]: 5, self.compartments[1]: 8}
        self.network.seed_network_node_type(Patch_type1, seeding)

        for n in self.network.nodes():
            if isinstance(n, Patch_type1):
                self.assertEqual(n.subpopulations[self.compartments[0]], 5)
                self.assertEqual(n.subpopulations[self.compartments[1]], 8)
                self.assertEqual(n.subpopulations[self.compartments[2]], 0)
            else:
                self.assertEqual(n.subpopulations[self.compartments[0]], 0)
                self.assertEqual(n.subpopulations[self.compartments[1]], 0)
                self.assertEqual(n.subpopulations[self.compartments[2]], 0)

    def test_seed_network_node_id(self):
        seeding = {self.compartments[1]: 3, self.compartments[2]: 7}
        self.network.seed_network_node_id(7, seeding)
        for n in self.network.nodes():
            if n.node_id == 7:
                self.assertEqual(n.subpopulations[self.compartments[0]], 0)
                self.assertEqual(n.subpopulations[self.compartments[1]], 3)
                self.assertEqual(n.subpopulations[self.compartments[2]], 7)
            else:
                self.assertEqual(n.subpopulations[self.compartments[0]], 0)
                self.assertEqual(n.subpopulations[self.compartments[1]], 0)
                self.assertEqual(n.subpopulations[self.compartments[2]], 0)

    def test_run(self):
        np.random.seed(101)
        self.nodes[0].subpopulations['a'] = 10
        self.network.run(time_limit=10, console_output=False)
        self.assertTrue(self.network.time > 10)