Beispiel #1
0
def sequential_degree(G,INTERDEPENDENCY): #formally cascading
    '''Code to perform the sequential analysis where the node to be removed is
    selected via that with the highest degree.
    Input: a network, INTERDEPENDENCY variable
    Return: a network, the node removed '''
    #get the node and with the maximum value from the network
    var = tools.max_val_random(nx.degree(G))
    if type(var) == int:
        return 3000
    else: degree, node = var

    #if the returned node is -99999, then there is an error
    if node == -99999:
        #check to see if the number of edges in the network is greater than 0
        if G.number_of_edges() > 0:
            raise error_classes.GeneralError('There are %s edge on the network. An unknown error has occred.' %(G.number_of_edges()))
            return 3001

        else:
            raise error_classes.CalculationError('There are no edges in the network, thus no values could be computed.')
            return 3002
    else:
        #remove the edges connected to the node - networkx does this automatically
        #remove the node from the network
        try:
            G.remove_node(node) #remove the node
        except:
            return 3003

    #return the editied network and the node removed
    var = G, node
    return var
Beispiel #2
0
def sequential_flow(G, NO_ISOLATES, INTERDEPENDENCY):
    '''Sequential anlysis method where the node to be removed is selcted
    via having the highest flow value.
    Input: a network, NO_ISOLATES varaible, INTERDEPENDENCY variable
    Return: a network, the node removed'''

    flow_dict = {}
    for nd in G.nodes():
        flow_dict[nd]=G.node[nd]['flow']

    var = tools.max_val_random(flow_dict)
    if type(var) == int:
        return 3030
    else: betweenness_value, node = var

    #if the node has the value of an error
    if node == -99999:
        raise error_classes.GeneralError('Error. An error occured when calcualting the node to remove.')
        return 3031
    else:
        #remove all edges which feature the node and then the node
        try:
            G.remove_node(node)
        except:
            return 3032
    #return the eddited network and the node removed
    var = G, node
    return var
Beispiel #3
0
def get_max_flow_values(G, use_node_capacities=True, use_node_demands=False):
    '''
    This runs a ford-fulkerson maximum flow algrothim over the provided network
    and assigns the resulting flows to each edge. The flows for each node are 
    also calcualted and assigned.
    '''
    print G.edges()
    #print G.edges()

    if use_node_capacities == True:
        G = convert_topo(G)

    print G.edges()
    exit()

    #check for supply and demand nodes. Needs to be at least one of each.
    #if more than one need to create a super source/sink(demand) nodes.
    G, supply_nodes, demand_nodes, added_nodes, added_edges = check_for_demand_supply_nodes(
        G)

    #get supply node and demand node
    supply_nd, demand_nd = get_check_supply_demand_nodes(
        G, supply_nodes, demand_nodes, added_nodes)

    if use_node_capacities == True and use_node_demands == False:
        #this returns the maximum flow and the flow on each edge by node
        #first check a path is possible - ford-fulkerson would just return 0 otherwise
        path = nx.has_path(G, supply_nd, demand_nd)
        if path == False:
            raise error_classes.GeneralError(
                'No path exists between the supply node (node %s) and the demand node (node %s).'
                % (supply_nd, demand_nd))

        max_flow, edge_flows = nx.ford_fulkerson(G, supply_nd, demand_nd,
                                                 'flow_capacity')

    elif use_node_capacities == True and use_node_demands == True:
        #returns the flow on each edge given capacities and demands
        #the sum of the demand needs to equal the sum of the supply (indicated by a negative demand value)
        edge_flows = nx.min_cost_flow(G, 'demand', 'flow_capacity', 'weight')
    elif use_node_capacities == False and use_node_demands == True:
        #returns the flow on each edge given demands (capacities should be set at 9999999 (a very high number))
        edge_flows = nx.min_cost_flow(G, 'demand', 'flow_capacity', 'weight')

    #assign flows to nodes and edges
    G = assign_edge_node_flows(G, edge_flows)

    #before running any analysis, need to remove the added nodes and edges - the super source/demand if used
    G = remove_added_nodes_edges(G, added_edges, added_nodes)

    node_flow_max, edge_flow_max = get_max_flows(G)

    return G, {
        'max_flow': max_flow,
        'max_node_flow': node_flow_max,
        'max_edge_flow': edge_flow_max
    }
Beispiel #4
0
def sequential_by_list(G,failures_to_occur,node_to_fail_list,method):
    '''
    Runs the sequential by list method. Takes parameters and method for node selection and uses/generates a list of nodes to remove. Removes the node with the greatest value.
    '''
    #if fewer nodes in the network than wanted for list reduce request
    if G.number_of_nodes() < failures_to_occur:
        failures_to_occur = G.number_of_nodes()

    #if list has no nodes in, build list
    if len(node_to_fail_list) == 0 or node_to_fail_list == {}:
        node_to_fail_list = create_dict_of_removals(G,failures_to_occur,node_to_fail_list,method)
    node = -99999

    if len(node_to_fail_list) != 0:
        use_next = True
        while use_next == True:
            # if the list becomes zero while trying to remove a node
            if len(node_to_fail_list) == 0:
                node_to_fail_list = create_dict_of_removals(G,failures_to_occur,node_to_fail_list,method)
            #get node with the max value and delete from the list
            max_val = max(node_to_fail_list.values())
            for key in node_to_fail_list.keys():
                if node_to_fail_list[key] == max_val:
                    node = key
                    break
            if node == -99999:
                print('Did not find node to remove')
                exit()
            del node_to_fail_list[key]
            # check node still in network - it may have been removed eg. isolated
            try:
                G.node[node]
                use_next = False
            except:
                use_next = True

    #if the node has the value of an error
    if node == -99999:

        raise error_classes.GeneralError('Error. An error occured when calculating the node to remove.')
        return 3011
    else:
        #remove all edges and the node
        try:
            G.remove_node(node)
        except:
            return 3012

    #return the eddited network and the node remvoed
    var = G, node, node_to_fail_list
    return var
Beispiel #5
0
def max_val_random(value_list):
    '''Find the maximum value in a list, and if tied between two or more
    values, randomly select one of the ties values. In the special case of
    betweenness centrality being used, all entires may be zero as they as
    isolated or in pairs, thus a node is also selected at random here as well.
    Input: a list
    Return: maximum value and the entry (chosen) with that value '''
    #set the necasary variables and create an empty list
    ma = -99999  #will store the maximum value
    node = -99999  #will store the node (number) with the maximum value
    tie_list = []  #use to store any nodes which shre the maximum value
    node_iterator = 0  #counts how many node have been selected
    list_iterator = 0

    #loop through the whole list
    while node_iterator < len(value_list):
        try:
            if value_list[list_iterator] > ma:
                #if the value is higher than the max value already
                #reset the tie list
                tie_list = []
                #add the value to the tie_list in case another one with the same value is found
                tie_list.append(list_iterator)
                ma = value_list[list_iterator]
                node = list_iterator
            elif value_list[list_iterator] == ma:
                #if the value is the same as the current max value, add to the tie list
                tie_list.append(list_iterator)
            else:
                pass
            node_iterator += 1
        except:
            #pass as no node with this value
            pass
        list_iterator += 1

    #if there is more than one node in the tie list, pick one at random
    if len(tie_list) > 0:
        node = random.choice(tie_list)
        var = ma, node
        return var
    else:
        raise error_classes.GeneralError(
            'Error. No node was found with a value greater than -99999.')
        return 6040
Beispiel #6
0
def sequential_betweenness(G,INTERDEPENDENCY):
    '''Sequential analysis where the node to be removed on each iteration is
    that with the highest betweenness centrality value.
    Input: a network, INTERDEPENDENCY variable
    Return: a network, the node removed'''
    #find the node with the max betweenness value in the network
    var = tools.max_val_random(nx.betweenness_centrality(G))
    if type(var) == int:
        return 3010
    else: betweenness_value, node = var

    #if the node has the value of an error
    if node == -99999:
        raise error_classes.GeneralError('Error. An error occured when calcualting the node to remove.')
        return 3011
    else:
        #remove all edges which feature the node and then the node
        try:
            G.remove_node(node)
        except:
            return 3012
    #return the eddited network and the node remvoed
    var = G, node
    return var
Beispiel #7
0
def set_failure_dict(analysis_type, failure_type, selection_type):
    '''
    Takes three inputs (strings):
        analysis_type: stand_alone, dependency, interdependency
        failure_type: single, sequential, cascading
        selection_type: random, degree, betweenness
    '''
    failure = {}
    if analysis_type == 'stand_alone':
        failure['stand_alone'] = True
        failure['dependency'] = False
        failure['interdependency'] = False
    elif analysis_type == 'dependency':
        failure['stand_alone'] = False
        failure['dependency'] = True
        failure['interdependency'] = False
    elif analysis_type == 'interdependency':
        failure['stand_alone'] = False
        failure['dependency'] = False
        failure['interdependency'] = True
    else:
        raise error_classes.GeneralError(
            'Error. Could not assign the analysis_type %s to one of the available methods.'
            % analysis_type)
        return 6100

    if failure_type == 'single':
        failure['single'] = True
        failure['sequential'] = False
        failure['cascading'] = False
    elif failure_type == 'sequential':
        failure['single'] = False
        failure['sequential'] = True
        failure['cascading'] = False
    elif failure_type == 'cascading':
        failure['single'] = False
        failure['sequential'] = False
        failure['cascading'] = True
    else:
        raise error_classes.GeneralError(
            'Error. Could not assign the failure_type %s to one of the available methods.'
            % failure_type)
        return 6101

    if selection_type == 'random':
        failure['random'] = True
        failure['degree'] = False
        failure['betweenness'] = False
        failure['from_list'] = False
        failure['flow'] = False
    elif selection_type == 'degree':
        failure['random'] = False
        failure['degree'] = True
        failure['betweenness'] = False
        failure['from_list'] = False
        failure['flow'] = False
    elif selection_type == 'betweenness':
        failure['random'] = False
        failure['degree'] = False
        failure['betweenness'] = True
        failure['from_list'] = False
        failure['flow'] = False
    elif selection_type == 'from_list':
        failure['random'] = False
        failure['degree'] = False
        failure['betweenness'] = False
        failure['from_list'] = True
        failure['flow'] = False
    elif selection_type == 'flow':
        failure['random'] = False
        failure['degree'] = False
        failure['betweenness'] = False
        failure['from_list'] = False
        failure['flow'] = True
    else:
        raise error_classes.GeneralError(
            'Error. Could not assign the selection_type %s to one of the available methods.'
            % selection_type)
        return 6103

    return failure
Beispiel #8
0
def analyse_existing_networks(NETWORK_NAME, conn, db, parameters, noioa,
                              use_db, use_csv, logfilepath, nx_location):
    '''
    '''
    import ogr, sys, tools
    sys.path.append()
    import nx_pgnet
    import networkx as nx
    import interdependency_analysis as ia
    #unpack varaibles
    metrics, failure, handling_variables, fileName, a_to_b_edges, write_step_to_db, write_results_table, db_parameters, store_n_e_atts, length = parameters
    var = failure_type(failure)
    if type(var) == int:
        return var
    else:
        failuretype = var
    #if performing analysis on one network only
    if failure['stand_alone'] == True:
        count = 0
        #loop through the listed networks
        for nets in NETWORK_NAME:
            if db == 'theoretic_networks_tree' or db == 'theoretic_networks_hc' or db == 'theoretic_networks_hr' or db == 'theoretic_networks_ahr' or db == 'theoretic_networks_ba' or db == 'theoretic_networks_ws' or db == 'theoretic_networks_gnm' or db == 'theoretic_networks_er':
                nets = str(nets) + '_' + str(count)
                count += 1
            iterations = 0
            #while noia(the number of simulation to perform) is greater then the number performed
            while iterations < noioa:
                #if the network is to be got from the database
                if use_db == True:
                    #connect to the database and get the network
                    conn = ogr.Open(conn)
                    G = nx_pgnet.read(conn).pgnet(nets)
                #the network must come from a csv
                else:
                    #get the text file
                    # maybe replace file_path with fileName1
                    filepath = str(fileName) + '%s/%s.txt' % (db, nets)
                    var = tools.get_nodes_edges_csv(filepath)
                    if type(var) == int:
                        return var
                    else:
                        nodelist, edgelist = var
                    #build the network from the lists returned from the function
                    G = nx.Graph()
                    G.add_nodes_from(nodelist)
                    G.add_edges_from(edgelist)
                #set the name of the results text file
                fileName = str(fileName) + '%s/%s%s.txt' % (db, nets,
                                                            failuretype)
                #package the parameters together
                parameters = metrics, failure, handling_variables, fileName, a_to_b_edges, write_step_to_db, write_results_table, db_parameters, store_n_e_atts, length = parameters
                #need a value for network B (G2)
                G2 = None
                #perform the analusis
                ia.main(G, G2, parameters, logfilepath)
                iterations += 1

    #if dependency or intersedpendcy
    elif failure['stand_alone'] == False:
        if use_db == True:
            conn = ogr.Open(conn)
            G = nx_pgnet.read(conn).pgnet(nets)
            raise error_classes.GeneralError(
                'Error. This function does not work as yet.')
        elif use_db == False:
            #get both networks from csv
            filepath = str(fileName) + '%s/%s.txt' % (db, NETWORK_NAME[0])
            var = tools.get_nodes_edges_csv(filepath)
            if type(var) == int:
                return var
            else:
                nodelist, edgelist = var
            G1 = nx.Graph()
            G1.add_nodes_from(nodelist)
            G1.add_edges_from(edgelist)
            filepath = str(fileName) + '%s/%s.txt' % (db, NETWORK_NAME[1])
            var = tools.get_nodes_edges_csv(filepath)
            if type(var) == int:
                return var
            else:
                nodelist, edgelist = var
            G2 = nx.Graph()
            G2.add_nodes_from(nodelist)
            G2.add_edges_from(edgelist)
        ia.main(G1, G2, parameters, logfilepath)
    else:
        raise error_classes.GeneralError(
            'Error. The STAND_ALONE variable must have a boolean value')