コード例 #1
0
def test_allocate_organs():
    ab_pos = BloodType(BloodTypeLetter.AB, BloodTypePolarity.POS)
    o_neg = BloodType(BloodTypeLetter.O, BloodTypePolarity.NEG)
    organ_list = OrganList()

    organ1 = Organ(OrganType.Pancreas, ab_pos, 1, organ_list)
    organ2 = Organ(OrganType.Heart, o_neg, 1, organ_list)

    wait_list = WaitList()
    patient1 = Patient('name', 'illness 1', OrganType.Pancreas,
                       ab_pos, 100, 1, wait_list)
    patient2 = Patient('name', 'illness 2', OrganType.Pancreas,
                       o_neg, 200, 3, wait_list)
    patient3 = Patient('name', 'illness 3', OrganType.Heart,
                       ab_pos, 100, 2, wait_list)
    patient4 = Patient('name', 'illness 4', OrganType.Heart,
                       ab_pos, 50, 2, wait_list)

    node1 = Node(1, adjacency_dict={2: {'weight': 10, 'status': True},
                                    3: {'weight': 25, 'status': True}})
    node2 = Node(2, adjacency_dict={3: {'weight': 35, 'status': True}})
    node3 = Node(3)
    test_net = Network({1: node1, 2: node2, 3: node3})

    OrganAllocator.allocate_organs(organ_list, wait_list, test_net)
    assert len(organ_list.organ_list) is 0
    assert len(wait_list.wait_list) is 2
    assert patient2 in wait_list.wait_list
    assert patient4 in wait_list.wait_list
コード例 #2
0
def test_remove_edge():
    node1 = Node(1, adjacency_dict={2: {'weight': 3, 'status': True}})
    node2 = Node(2, adjacency_dict={1: {'weight': 3, 'status': True}})
    node3 = Node(3)

    test_net = Network({1: node1, 2: node2})

    # test nodes are connected
    assert node2.node_id in node1.adjacency_dict
    assert node1.node_id in node2.adjacency_dict

    # remove edge
    test_net.remove_edge(node1.node_id, node2.node_id)
    assert node2.node_id not in node1.adjacency_dict
    assert node1.node_id not in node2.adjacency_dict
    assert not node1.adjacency_dict
    assert not node2.adjacency_dict
    assert len(test_net.network_dict) is 2

    # attempt to remove edge with no shared edge
    test_net.remove_edge(node1.node_id, node3.node_id)
    assert not node1.adjacency_dict
    assert not node2.adjacency_dict
    assert len(test_net.network_dict) is 2

    # attempt to remove edge from a node that doesn't exist
    test_net.remove_edge(node1.node_id, 4)
    assert not node1.adjacency_dict
    assert not node2.adjacency_dict
    assert len(test_net.network_dict) is 2
コード例 #3
0
def test_shortest_path():
    test_node1 = Node(node_id=1, adjacency_dict={2: {'weight': 10, 'status': True}})
    test_node2 = Node(node_id=2, adjacency_dict={3: {'weight': 5, 'status': True}})
    test_node3 = Node(node_id=3, adjacency_dict={4: {'weight': 3, 'status': True}})
    test_node4 = Node(node_id=4, adjacency_dict={1: {'weight': 6, 'status': True}})
    test_node5 = Node(node_id=5, adjacency_dict={1: {'weight': 1, 'status': True},
                                                 3: {'weight': 2, 'status': True}})
    test_net = Network({test_node1.node_id: test_node1,
                        test_node2.node_id: test_node2,
                        test_node3.node_id: test_node3,
                        test_node4.node_id: test_node4,
                        test_node5.node_id: test_node5})

    dijkstra = Dijkstra(graph=test_net, source=test_node1.node_id)
    path, weight = dijkstra.shortest_path(destination=test_node3.node_id)
    assert path == [1, 5, 3]
    assert weight is 3

    test_net.remove_node(test_node5.node_id)
    dijkstra = Dijkstra(graph=test_net, source=test_node1.node_id)
    path, weight = dijkstra.shortest_path(destination=test_node3.node_id)
    assert path == [1, 4, 3]
    assert weight is 9

    test_net.remove_node(test_node4.node_id)
    dijkstra = Dijkstra(graph=test_net, source=test_node1.node_id)
    path, weight = dijkstra.shortest_path(destination=test_node3.node_id)
    assert path == [1, 2, 3]
    assert weight is 15

    test_net.remove_node(test_node2.node_id)
    dijkstra = Dijkstra(graph=test_net, source=test_node1.node_id)
    path, weight = dijkstra.shortest_path(destination=test_node3.node_id)
    assert path is None
    assert weight == float('inf')
コード例 #4
0
def test_nodes():
    node1 = Node(1, adjacency_dict={2: {'weight': 3, 'status': True}})
    node2 = Node(2, adjacency_dict={1: {'weight': 3, 'status': True}})
    test_net = Network({1: node1, 2: node2})

    # test graph with 2 active nodes
    assert len(test_net.nodes()) is 2
    assert len(test_net.nodes()) is len(test_net.network_dict)
    assert node1.node_id in test_net.nodes()
    assert node2.node_id in test_net.nodes()

    # test edge deactivation
    test_net.mark_edge_inactive(node1.node_id, node2.node_id)
    assert len(test_net.nodes()) is 2
    assert len(test_net.nodes()) is len(test_net.network_dict)
    assert node1.node_id in test_net.nodes()
    assert node2.node_id in test_net.nodes()

    # deactivate 1 node and test
    test_net.mark_node_inactive(node1.node_id)
    assert len(test_net.nodes()) is 1
    assert len(test_net.nodes()) is not len(test_net.network_dict)
    assert len(test_net.network_dict) is 2
    assert node1.node_id not in test_net.nodes()
    assert node2.node_id in test_net.nodes()

    # deactivate last node
    test_net.mark_node_inactive(node2.node_id)
    assert not test_net.nodes()
    assert len(test_net.nodes()) is not len(test_net.network_dict)
    assert len(test_net.network_dict) is 2
    assert node1.node_id not in test_net.nodes()
    assert node2.node_id not in test_net.nodes()
コード例 #5
0
def test_mark_node_inactive():
    node1 = Node(1, adjacency_dict={2: {'weight': 3, 'status': True}})
    node2 = Node(2, adjacency_dict={1: {'weight': 3, 'status': True}})
    test_net = Network({1: node1, 2: node2})

    assert len(test_net.network_dict) is len(test_net.nodes())
    assert node2.node_id in test_net.network_dict[node1.node_id].adjacency_dict
    assert node1.node_id in test_net.network_dict[node2.node_id].adjacency_dict

    # test marking existing active node as inactive
    test_net.mark_node_inactive(1)
    assert len(test_net.network_dict) is 2
    assert len(test_net.network_dict) is not len(test_net.nodes())
    assert not node1.status
    assert node2.status
    assert not node1.adjacency_dict[node2.node_id]['status']
    assert not node2.adjacency_dict[node1.node_id]['status']
    assert node2.node_id in test_net.network_dict[node1.node_id].adjacency_dict
    assert node1.node_id in test_net.network_dict[node2.node_id].adjacency_dict

    # test already inactive node
    test_net.mark_node_inactive(1)
    assert len(test_net.network_dict) is 2
    assert len(test_net.network_dict) is not len(test_net.nodes())
    assert not node1.status
    assert node2.status
    assert not node1.adjacency_dict[node2.node_id]['status']
    assert not node2.adjacency_dict[node1.node_id]['status']
    assert node2.node_id in test_net.network_dict[node1.node_id].adjacency_dict
    assert node1.node_id in test_net.network_dict[node2.node_id].adjacency_dict

    # test node that doesn't exist
    test_net.mark_node_inactive(3)
    assert len(test_net.network_dict) is 2
    assert len(test_net.network_dict) is not len(test_net.nodes())
コード例 #6
0
def test_is_connected():
    test_net = Network()
    test_net.add_node(Node(1))

    assert ConnectivityChecker.is_connected(test_net)

    test_net.add_node(Node(2))
    assert not ConnectivityChecker.is_connected(test_net)

    test_net.add_edge(node_id1=1, node_id2=2, weight=5)
    assert ConnectivityChecker.is_connected(test_net)
コード例 #7
0
def test_breadth_first_search():
    test_net = Network()
    test_net.add_node(Node(1))

    assert ConnectivityChecker.breadth_first_search(test_net)

    test_net.add_node(Node(2))
    assert not ConnectivityChecker.breadth_first_search(test_net)

    test_net.add_edge(node_id1=1, node_id2=2, weight=5)
    assert ConnectivityChecker.breadth_first_search(test_net)
コード例 #8
0
def test_remove_node():
    node1 = Node(1)
    node2 = Node(2)
    test_net = Network({1: node1, 2: node2})

    # remove an existing node
    assert len(test_net.nodes()) is 2
    test_net.remove_node(1)
    assert len(test_net.nodes()) is 1
    assert node2.node_id in test_net.network_dict

    # remove a node that doesn't exist
    test_net.remove_node(3)
    assert len(test_net.nodes()) is 1
    assert node2.node_id in test_net.network_dict
コード例 #9
0
def test_find_best_match():
    ab_pos = BloodType(BloodTypeLetter.AB, BloodTypePolarity.POS)
    o_neg = BloodType(BloodTypeLetter.O, BloodTypePolarity.NEG)
    wait_list = WaitList()

    patient1 = Patient('name', 'illness 1', OrganType.Pancreas,
                       ab_pos, 100, 1, wait_list)
    patient2 = Patient('name', 'illness 2', OrganType.Pancreas,
                       ab_pos, 250, 3, wait_list)
    patient3 = Patient('name', 'illness 3', OrganType.Pancreas,
                       o_neg, 400, 2, wait_list)
    patient4 = Patient('name', 'illness 4', OrganType.Heart,
                       ab_pos, 500, 2, wait_list)
    organ = Organ(OrganType.Pancreas, o_neg, 1)

    node1 = Node(1, adjacency_dict={2: {'weight': 10, 'status': True},
                                    3: {'weight': 25, 'status': True}})
    node2 = Node(2, adjacency_dict={3: {'weight': 35, 'status': True}})
    node3 = Node(3)
    test_net = Network({1: node1, 2: node2, 3: node3})
    weights, paths = Dijkstra.dijkstra(test_net, 1)

    patient = OrganAllocator.find_best_match(organ, wait_list, weights)
    assert patient is patient3
    assert len(wait_list.wait_list) is 4

    node1.adjacency_dict[3]['weight'] = 300
    node3.adjacency_dict[1]['weight'] = 300
    test_net.mark_node_inactive(2)
    wait_list.remove_patient(patient3)
    weights, paths = Dijkstra.dijkstra(test_net, 1)
    assert len(weights) is 2

    patient = OrganAllocator.find_best_match(organ, wait_list, weights)
    assert patient is patient1
コード例 #10
0
def test_dijkstra():
    test_node1 = Node(node_id=1, adjacency_dict={2: {'weight': 5, 'status': True}})
    test_node2 = Node(node_id=2, adjacency_dict={3: {'weight': 5, 'status': True}})
    test_node3 = Node(node_id=3)
    test_node4 = Node(node_id=4)
    test_net = Network({test_node1.node_id: test_node1,
                        test_node2.node_id: test_node2,
                        test_node3.node_id: test_node3,
                        test_node4.node_id: test_node4})

    weight, previous = Dijkstra.dijkstra(graph=test_net, source=1)
    assert weight[1] is 0
    assert weight[2] is 5
    assert weight[3] is 10
    assert weight[4] == float('inf')
    assert previous[1] is None
    assert previous[2] is 1
    assert previous[3] is 2
    assert previous[4] is None
コード例 #11
0
def test_all_shortest_paths():
    test_node1 = Node(node_id=1, adjacency_dict={2: {'weight': 10, 'status': True}})
    test_node2 = Node(node_id=2, adjacency_dict={3: {'weight': 5, 'status': True},
                                                 4: {'weight': 10, 'status': True},
                                                 5: {'weight': 15, 'status': True}})
    test_node3 = Node(node_id=3)
    test_node4 = Node(node_id=4)
    test_node5 = Node(node_id=5)
    test_net = Network({test_node1.node_id: test_node1,
                        test_node2.node_id: test_node2,
                        test_node3.node_id: test_node3,
                        test_node4.node_id: test_node4,
                        test_node5.node_id: test_node5})

    dijkstra = Dijkstra(graph=test_net, source=test_node1.node_id)
    shortest_paths = dijkstra.all_shortest_paths()
    assert shortest_paths[1] == ([1], 0)
    assert shortest_paths[2] == ([1, 2], 10)
    assert shortest_paths[3] == ([1, 2, 3], 15)
    assert shortest_paths[4] == ([1, 2, 4], 20)
    assert shortest_paths[5] == ([1, 2, 5], 25)
コード例 #12
0
def test_mark_node_active():
    node1 = Node(1,
                 adjacency_dict={2: {
                     'weight': 3,
                     'status': True
                 }},
                 status=False)
    node2 = Node(2,
                 adjacency_dict={1: {
                     'weight': 3,
                     'status': True
                 }},
                 status=False)
    test_net = Network({1: node1, 2: node2})
    assert not test_net.nodes()

    # test existing inactive node
    test_net.mark_node_active(1)
    assert node1.node_id in test_net.nodes()
    assert len(test_net.nodes()) is 1
    assert not node1.adjacency_dict[node2.node_id]['status']
    assert node1.status
    assert len(test_net.network_dict) is 2

    # test existing active node
    test_net.mark_node_active(1)
    assert node1.node_id in test_net.nodes()
    assert len(test_net.nodes()) is 1
    assert not node1.adjacency_dict[node2.node_id]['status']
    assert node1.status
    assert len(test_net.network_dict) is 2

    # test nonexistent node
    test_net.mark_node_active(3)
    assert node1.node_id in test_net.nodes()
    assert len(test_net.nodes()) is 1
    assert not node1.adjacency_dict[node2.node_id]['status']
    assert node1.status
    assert len(test_net.network_dict) is 2
コード例 #13
0
def test_add_node():
    node1 = Node(1)
    test_net = Network()

    # add node to empty graph
    assert len(test_net.nodes()) is 0
    test_net.add_node(node1)
    assert len(test_net.nodes()) is 1
    assert node1.node_id in test_net.nodes()

    # attempt to add node that already exists
    test_net.add_node(node1)
    assert len(test_net.nodes()) is 1
    assert node1.node_id in test_net.nodes()
コード例 #14
0
def test_add_edge():
    node1 = Node(1)
    node2 = Node(2)
    test_net = Network()

    # add nodes, but not edges
    test_net.add_node(node1)
    test_net.add_node(node2)
    assert len(test_net.network_dict[node1.node_id].get_adjacents()) is 0

    # add edge to existing nodes
    test_net.add_edge(node1.node_id, node2.node_id, 5)
    assert len(test_net.network_dict[node1.node_id].get_adjacents()) is 1
    assert test_net.network_dict[node1.node_id].adjacency_dict[
        node2.node_id]['weight'] is 5

    # attempt to add edge that already exists
    test_net.add_edge(node1.node_id, node2.node_id, 5)
    assert len(test_net.network_dict[node1.node_id].get_adjacents()) is 1
    assert test_net.network_dict[node1.node_id].adjacency_dict[
        node2.node_id]['weight'] is 5

    # add inactive node
    node3 = Node(3, status=False)
    test_net.add_node(node3)

    # attempt to add edge to inactive node
    test_net.add_edge(node1.node_id, node3.node_id, 15)
    assert len(test_net.network_dict[node1.node_id].get_adjacents()) is 1
    assert test_net.network_dict[node1.node_id].adjacency_dict[
        node2.node_id]['weight'] is 5

    # attempts to add edge to node that doesn't exist
    test_net.add_edge(node1.node_id, node_id2=4, weight=25)
    assert len(test_net.network_dict[node1.node_id].get_adjacents()) is 1
    assert test_net.network_dict[node1.node_id].adjacency_dict[
        node2.node_id]['weight'] is 5
コード例 #15
0
from network_simulator.GraphConverter import GraphConverter
from network_simulator.Network import Node, Network

node_a = Node(1, 'A', {
    2: {
        'weight': 1,
        'status': True
    },
    3: {
        'weight': 2,
        'status': True
    }
})
node_b = Node(2, 'B', {3: {'weight': 1, 'status': True}})
node_c = Node(3, 'C', {1: {'weight': 2, 'status': True}})
node_d = Node(4, 'D')

network = Network({
    node_a.node_id: node_a,
    node_b.node_id: node_b,
    node_c.node_id: node_c,
    node_d.node_id: node_d
})


def test_graph_converter():
    """
    Verifies all nodes present in Network are present in NetworkX
    Verifies all edges present in Network are present in NetworkX
    Verifies edge weights match for corresponding edges in both Network and NetworkX
    """
コード例 #16
0
def test_mark_edge_inactive():
    node1 = Node(1, adjacency_dict={2: {'weight': 3, 'status': True}})
    node2 = Node(2, adjacency_dict={1: {'weight': 3, 'status': True}})
    test_net = Network({1: node1, 2: node2})

    # test existing nodes with shared, active edge
    test_net.mark_edge_inactive(node1.node_id, node2.node_id)
    assert not node1.adjacency_dict[node2.node_id]['status']
    assert not node2.adjacency_dict[node1.node_id]['status']
    assert not node1.get_adjacents()
    assert not node2.get_adjacents()
    assert len(test_net.network_dict) is 2
    assert len(test_net.network_dict) is len(test_net.nodes())

    # test existing nodes with shared, inactive edge
    test_net.mark_edge_inactive(node1.node_id, node2.node_id)
    assert not node1.adjacency_dict[node2.node_id]['status']
    assert not node2.adjacency_dict[node1.node_id]['status']
    assert not node1.get_adjacents()
    assert not node2.get_adjacents()
    assert len(test_net.network_dict) is 2
    assert len(test_net.network_dict) is len(test_net.nodes())

    # test existing nodes with shared, inactive edge
    test_net.add_node(Node(3))
    test_net.mark_edge_inactive(node1.node_id, 3)
    assert not node1.adjacency_dict[node2.node_id]['status']
    assert not node2.adjacency_dict[node1.node_id]['status']
    assert not node1.get_adjacents()
    assert not node2.get_adjacents()
    assert len(test_net.network_dict) is 3
    assert len(test_net.network_dict) is len(test_net.nodes())

    # test with nonexistent node
    test_net.add_node(Node(3))
    test_net.mark_edge_inactive(node1.node_id, 4)
    assert not node1.adjacency_dict[node2.node_id]['status']
    assert not node2.adjacency_dict[node1.node_id]['status']
    assert not node1.get_adjacents()
    assert not node2.get_adjacents()
    assert len(test_net.network_dict) is 3
    assert len(test_net.network_dict) is len(test_net.nodes())
コード例 #17
0
from network_simulator.Network import Node, Network
from network_simulator.OrganGenerator import OrganGenerator
from network_simulator.OrganList import OrganList

test_net = Network()
test_net.add_node(Node(1))
n = 3


def test_generate_organs():
    organs = OrganGenerator.generate_organs(graph=test_net, n=n)

    assert len(organs) <= n * 6
    for organ in organs:
        assert organ.current_location in test_net.nodes()
        assert 0 <= organ.organ_type.value <= 5
        assert 0 <= organ.blood_type.blood_type_letter.value <= 3
        assert 0 <= organ.blood_type.blood_type_polarity.value <= 1


def test_generate_organs_to_list():
    organ_list = OrganList()
    OrganGenerator.generate_organs_to_list(graph=test_net, n=n, organ_list=organ_list)

    # assert len(organ_list.organ_list) <= n * 6
    for organ in organ_list.organ_list:
        assert organ.current_location in test_net.nodes()
        assert 0 <= organ.organ_type.value <= 5
        assert 0 <= organ.blood_type.blood_type_letter.value <= 3
        assert 0 <= organ.blood_type.blood_type_polarity.value <= 1
コード例 #18
0
def test_mark_edge_active():
    node1 = Node(1, adjacency_dict={2: {'weight': 3, 'status': False}})
    node2 = Node(2, adjacency_dict={1: {'weight': 3, 'status': False}})
    test_net = Network({1: node1, 2: node2})

    # test existing, active nodes with shared, inactive edge
    test_net.mark_edge_active(node1.node_id, node2.node_id)
    assert node1.adjacency_dict[node2.node_id]['status']
    assert node2.adjacency_dict[node1.node_id]['status']
    assert len(node1.adjacency_dict) is 1
    assert len(node2.adjacency_dict) is 1
    assert len(test_net.network_dict) is 2
    assert len(test_net.network_dict) is len(test_net.nodes())

    # test existing, active nodes with shared, active edge
    test_net.mark_edge_active(node1.node_id, node2.node_id)
    assert node1.adjacency_dict[node2.node_id]['status']
    assert node2.adjacency_dict[node1.node_id]['status']
    assert len(node1.adjacency_dict) is 1
    assert len(node2.adjacency_dict) is 1
    assert len(test_net.network_dict) is 2
    assert len(test_net.network_dict) is len(test_net.nodes())

    # test existing, inactive node with shared, inactive edge
    test_net.mark_node_inactive(node1.node_id)
    test_net.mark_edge_active(node1.node_id, node2.node_id)
    assert not node1.adjacency_dict[node2.node_id]['status']
    assert not node2.adjacency_dict[node1.node_id]['status']
    assert len(node1.adjacency_dict) is 1
    assert len(node2.adjacency_dict) is 1
    assert len(test_net.network_dict) is 2
    assert len(test_net.network_dict) is not len(test_net.nodes())
    assert not node1.status
    assert node2.status

    # test existing nodes without shared edge
    test_net.add_node(Node(3))
    test_net.mark_edge_active(node2.node_id, 3)
    test_net.mark_node_inactive(node1.node_id)
    test_net.mark_edge_active(node1.node_id, node2.node_id)
    assert not node1.adjacency_dict[node2.node_id]['status']
    assert not node2.adjacency_dict[node1.node_id]['status']
    assert len(node1.adjacency_dict) is 1
    assert len(node2.adjacency_dict) is 1
    assert len(test_net.network_dict) is 3
    assert len(test_net.network_dict) is not len(test_net.nodes())
    assert not node1.status
    assert node2.status

    # test node that doesn't exist
    test_net.mark_edge_active(node2.node_id, 4)
    test_net.add_node(Node(3))
    test_net.mark_edge_active(node2.node_id, 3)
    test_net.mark_node_inactive(node1.node_id)
    test_net.mark_edge_active(node1.node_id, node2.node_id)
    assert not node1.adjacency_dict[node2.node_id]['status']
    assert not node2.adjacency_dict[node1.node_id]['status']
    assert len(node1.adjacency_dict) is 1
    assert len(node2.adjacency_dict) is 1
    assert len(test_net.network_dict) is 3
    assert len(test_net.network_dict) is not len(test_net.nodes())
    assert not node1.status
    assert node2.status