Esempio n. 1
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
    def depth_first_search(network: Network, node_id: int = None) -> bool:
        """
        Returns bool indicating graph connectivity (path between all nodes).
        This is an iterative DFS.

        :param network: object representing a graph (network)
        :param int node_id: identifies node where search will start (None by default)

        :return: bool indicating graph connectivity
        :rtype: bool
        """
        nodes_encountered: Set[Optional[int]] = set()
        if not node_id:
            node_id = network.nodes()[0]
        stack = [node_id]
        while stack:
            node = stack.pop()
            if node not in nodes_encountered:
                nodes_encountered.add(node)
                stack.extend(network.network_dict[node].get_adjacents())

        if len(nodes_encountered) != len(network.nodes()):
            return False
        else:
            return True
    def mark_network_inactive(network: Network) -> None:
        """
        Marks all nodes in a given network inactive
        (this is the starting point for the subnetwork)

        :param Network network: graph that is the foundation for subnetworks
        """
        for node in network.nodes():
            network.mark_node_inactive(node, feedback=False)
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')
Esempio n. 5
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
    def is_connected(network: Network,
                     nodes_encountered: Set[Optional[int]] = None,
                     source: int = None) -> bool:
        """
        Returns bool indicating graph connectivity (path between all nodes).
        This is a recursive DFS.

        :param network: object representing a graph (network)
        :param Set[int] nodes_encountered: set of node_id's encountered (None by default)
        :param int source: node_id of start of search (None by default)

        :return: bool indicating graph connectivity
        :rtype: bool
        """
        if not nodes_encountered:
            nodes_encountered = set()
        nodes = network.nodes()
        if not source:
            # chose a vertex from network as start point
            source = nodes[0]
        nodes_encountered.add(source)
        if len(nodes_encountered) != len(nodes):
            for node in network.network_dict[source].get_adjacents():
                if node not in nodes_encountered:
                    if ConnectivityChecker.is_connected(
                            network, nodes_encountered, node):
                        return True
        else:
            return True
        return False
    def generate_organs(graph: Network, n: int) -> List[Organ]:
        """
        Harvests a random number of organs from n patients. Not all organs are harvested
        to represent organs that are not suitable for donation (health condition, etc

        Generates n patients to add to wait list with random combinations of
        organ needed, blood type, priority, and location

        :param Network graph: network where organs can be generated
        :param int n: number of of bodies to harvest organs from
        """

        # list of currently active nodes
        nodes = graph.nodes()
        organs: List[Organ] = list()

        # number of patients to harvest from
        for _ in range(n):
            # number of possible organs to harvest
            location_id = random.choice(nodes)
            blood_type = BloodType(BloodTypeLetter.random_blood_type(),
                                   BloodTypePolarity.random_blood_polarity())

            for organ_type in OrganType:
                # determines if organ is suitable for harvest
                if random.randrange(4) != 0:
                    organs.append(
                        Organ(organ_type=organ_type,
                              blood_type=blood_type,
                              location=location_id))
        return organs
Esempio n. 8
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
    def graph_builder(n: int,
                      max_weight: int = None,
                      seed: int = None) -> Network:
        """
        Returns randomly generated network with n nodes.

        :param int n: number of nodes generated graph will contain
        :param int max_weight: optional param that sets a maximum weight for edges
        :param int seed: optional parameter to control pseudorandom generator

        :return: randomly generated network with N nodes
        :rtype: Network
        """

        network_dict = {}
        if not max_weight:
            max_weight = 50

        for x in range(1, n + 1):
            adjacency_dict = GraphBuilder.generate_random_adjacency_dict(
                x, n, max_weight)
            node = Node(x, 'Node #' + str(x), adjacency_dict)
            network_dict[x] = node
        network = Network(network_dict)
        return network
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)
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)
Esempio n. 12
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
    def breadth_first_search(network: Network, node_id: int = None) -> bool:
        """
        Returns bool indicating graph connectivity (path between all nodes).
        This is an iterative BFS.

        :param network: object representing a graph (network)
        :param int node_id: identifies node where search will start (None by default)

        :return: bool indicating graph connectivity
        :rtype: bool
        """
        # mark all the nodes as not visited (value is None)
        visited = dict.fromkeys(network.nodes())
        nodes_encountered = set()
        queue = []

        if not node_id:
            node_id = network.nodes()[0]

        # mark the source node as visited and enqueue it
        queue.append(node_id)
        visited[node_id] = True
        nodes_encountered.add(node_id)

        while queue:
            # dequeue a node from queue and add to nodes_encountered
            node_id = queue.pop(0)
            nodes_encountered.add(node_id)

            # all adjacents of current node are checked, if the node hasn't been
            # enqueued previously, node is enqueued and added to nodes_encountered
            for node in network.network_dict[node_id].get_adjacents():
                if not visited[node]:
                    queue.append(node)
                    visited[node] = True
                    nodes_encountered.add(node)

        # if size of nodes_encountered equals size of nodes(), return True
        if len(nodes_encountered) == len(network.nodes()):
            return True
        else:
            return False
Esempio n. 14
0
    def dijkstra(graph: Network, source: int) -> dijkstra_structure:
        """
        This function finds the shortest path to all connected nodes
        from a given source node. The return are two dicts:
            1) the weight from source to the key
            2) the node preceding the key

        This function is intended to be used by the initializer when
        creating and Dijkstra instance. The output will be stored as
        attributes of the object. These structures can be referenced later
        to determine the shortest path (and cost) to a given destination

        :param graph: network which will be used for path finding
        :param source: source node (where all paths will start)

        :return: weight: dict represented as {node_id: <weight>, ...}
        :return: previous: dict represented as {node_id: <previous_node_id>, ...}
        """
        unvisited = graph.nodes()
        weight = dict.fromkeys(graph.nodes(), float('inf'))
        previous: Dict[int, Union[int,
                                  None]] = dict.fromkeys(graph.nodes(), None)
        weight[source] = 0

        # while there are nodes which haven't been visited
        while unvisited:
            # picks the unvisited node with the shortest weight to visit next
            current_node = Dijkstra.minimum_unvisited_distance(
                unvisited, weight)
            unvisited.remove(current_node)

            # looks at all of the current node's adjacents
            for adjacent in graph.network_dict[current_node].get_adjacents():
                alt_weight = weight[current_node] + \
                             graph.network_dict[current_node].adjacency_dict[adjacent]['weight']
                # if this adjacent has a lower weight than currently recorded weight, replace it
                if alt_weight < weight[adjacent]:
                    weight[adjacent] = alt_weight
                    previous[adjacent] = current_node

        return weight, previous
def import_nodes(worksheet):
    """
    Imports node from each row and adds it to the network. These nodes
    only contain the following fields: node_id, hospital_name, region,
    city, and state. (adjacency_dict is handled in generate_distance_vector()
    and status is assumed to be True)

    :param Worksheet worksheet: worksheet to read data from
    :return: Network
    """
    network = Network()
    for x in range(2, worksheet.max_row + 1):
        network.add_node(
                node=Node(node_id=int(worksheet[f'{column_indices["unique id"]}{x}'].value),
                          hospital_name=worksheet[f'{column_indices["hospital name"]}{x}'].value,
                          region=int(worksheet[f'{column_indices["region"]}{x}'].value),
                          city=worksheet[f'{column_indices["city"]}{x}'].value,
                          state=worksheet[f'{column_indices["state"]}{x}'].value),
                feedback=False)

    generate_distance_vector(network=network)
    return network
    def convert_to_networkx(network: Network,
                            is_regional_weight: bool = None) -> nx.Graph:
        """
        Converts a Network object to a NetworkX graph

        :param Network network:
        :param bool is_regional_weight: indicates whether weight metric should
                be regional weight or distance
        :return: NetworkX Graph
        """
        nx_graph = nx.Graph()
        nodes = network.nodes()

        for node in nodes:
            nx_graph.add_node(node)
            GraphConverter.add_edges(network, node, nx_graph, is_regional_weight)

        return nx_graph
Esempio n. 17
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
Esempio n. 18
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)
Esempio n. 19
0
    def generate_patients(graph: Network, n: int) -> List[Patient]:
        """
        Generates n patients to add to wait list with random combinations of
        organ needed, blood type, priority, and location

        :param Network graph: network for patients to be allocated to
        :param int n: number of patients to generate
        """
        # list of currently active nodes
        nodes = graph.nodes()
        patients: List[Patient] = list()

        for x in range(n):
            patients.append(
                Patient(patient_name="generated patient #" + str(x + 1),
                        illness="N/A",
                        organ_needed=OrganType.random_organ_type(),
                        blood_type=BloodType(
                            BloodTypeLetter.random_blood_type(),
                            BloodTypePolarity.random_blood_polarity()),
                        priority=random.randrange(100 + n),
                        location=random.choice(nodes)))
        return patients
    def convert_to_attribute_nx(network: Network,
                                is_regional_weight: bool = None,
                                state_dict=None,
                                region_dict=None) -> nx.Graph:
        """
        Converts a Network object to a NetworkX graph

        :param Network network:
        :param bool is_regional_weight: indicates whether weight metric should
                be regional weight or distance
        :param dict state_dict: quantifies how many hospitals share the state
        :param dict region_dict: quantifies how many hospitals share the region
        :return: NetworkX Graph
        """
        nx_graph = nx.Graph()
        nodes = network.nodes()

        for node_id in nodes:
            node = network.network_dict[node_id]
            label = node.label
            city = node.city
            state = node.state
            region = node.region

            state = 'Washington D.C.' if state == 'US' else state

            nx_graph.add_node(node_id,
                              hospital_name=label,
                              city=city,
                              state=state,
                              region=region,
                              state_count=state_dict[state],
                              region_count=region_dict[region])

            GraphConverter.add_edges(network, node_id, nx_graph, is_regional_weight)

        return nx_graph
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
Esempio n. 22
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
Esempio n. 23
0
            'status': True
        },
        7: {
            'weight': 5,
            'status': True
        }
    })

# builds hospital network
hospital_network = Network(
    {
        hospital_a.node_id: hospital_a,
        hospital_b.node_id: hospital_b,
        hospital_c.node_id: hospital_c,
        hospital_d.node_id: hospital_d,
        hospital_e.node_id: hospital_e,
        hospital_f.node_id: hospital_f,
        hospital_g.node_id: hospital_g,
        hospital_h.node_id: hospital_h,
        hospital_i.node_id: hospital_i,
        hospital_j.node_id: hospital_j
    }, 'Hospital Network')

# creates a handful of patients who need organ (across network)
patient_a = Patient(patient_name='patient a',
                    illness='diabetes',
                    organ_needed=OrganType.Pancreas,
                    blood_type=BloodType(BloodTypeLetter.AB,
                                         BloodTypePolarity.POS),
                    priority=1,
                    location=hospital_j.node_id)
Esempio n. 24
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())
Esempio n. 25
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
Esempio n. 26
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()
Esempio n. 27
0
            'weight': 2,
            'status': True
        },
        7: {
            'weight': 5,
            'status': True
        }
    })

# create a network consisting of the nodes above
network_1 = Network({
    node_1.node_id: node_1,
    node_2.node_id: node_2,
    node_3.node_id: node_3,
    node_4.node_id: node_4,
    node_5.node_id: node_5,
    node_6.node_id: node_6,
    node_7.node_id: node_7,
    node_8.node_id: node_8,
    node_9.node_id: node_9,
    node_10.node_id: node_10
})

# demonstrates __str__() function works
print(network_1)

# demonstrates remove_edge function works
print('\t---REMOVE EDGE---')
network_1.remove_edge(1, 2)
print(network_1)

# demonstrates remove_node function works
Esempio n. 28
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()
Esempio n. 29
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
    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
    """
    network_x = GraphConverter.convert_to_networkx(network)
    nodes = network.nodes()
    nx_nodes = list(network_x.nodes)

    for node in nodes: