예제 #1
0
    def createEdgesGraph(self, college):
        """
        print(graph.edges())
        print(graph.nodes())
        (cut, parts) = metis.part_graph(graph, 40)
        print("hola")
        #(cut2, parts2) = metis.partition(graph, 2)
        metisGraph = metis.networkx_to_metis(graph)
        print(metisGraph)
        graph['U27476']['U27661']['weight']=1
        print(graph['U27476']['U27661'])
        graph.graph['edge_weight_attr'] = 'weight'
        metisGraph = metis.networkx_to_metis(graph)
        (cutW, partsW) = metis.part_graph(metisGraph, 10)
        print("paso esto")
        print(graph)
        print(metisGraph)
        """
        graph = nx.read_gexf("mediumLinkedin.gexf")
        weightedGraph = nx.read_gexf("mediumLinkedin.gexf")

        # self.draw_graph(graph, parts)
        weightedGraph = self.setEdgesWeights(weightedGraph, college)

        weightedGraph.graph['edge_weight_attr'] = 'weight'
        metisWeightedGraph = metis.networkx_to_metis(weightedGraph)
        print("Llego hasta aca")

        # self.draw_graph(metisWeightedGraph,parts)

        (cutW, partsW) = metis.part_graph(metisWeightedGraph, 5)
        print(partsW)
        self.draw_graph(graph, partsW)
예제 #2
0
def nxgraph_to_metis(
    graph,
    node_weight_attr=(constants.NODE_SWITCH_CAPACITY_WEIGHT_KEY,
                      constants.NODE_CPU_WEIGHT_KEY)):
    # graph.graph['edge_weight_attr'] = constants.EDGE_WEIGHT_KEY
    graph.graph['node_weight_attr'] = node_weight_attr
    return metis.networkx_to_metis(graph)
예제 #3
0
    def predictAttributesByCluster(self, graph, emptyNodes, attrs, attr):

        # starts new method
        weightedGraph = graph
        weightedGraph = self.setEdgesWeights(weightedGraph, attr, emptyNodes)
        weightedGraph.graph['edge_weight_attr'] = 'weight'
        metisWeightedGraph = metis.networkx_to_metis(weightedGraph)

        #metisWeightedGraph = metis.networkx_to_metis(graph)

        (cut, parts) = metis.part_graph(graph, 12)
        # new method ends

        # (cut, parts) = metis.part_graph(graph, 10, recursive = True)
        # print(cut)
        self.setNodesClustersDictionary(graph, parts)

        predicted_values = {}
        for node in emptyNodes:
            # print('hola')

            # print('lista:', attrs[0])
            # if node not in attrs[0]:
            # print('no esta')

            nbrs_attr_values = []
            clusterID = self.nodesclusters[node]
            for nbr in self.getSubgraphByClusterID(graph, clusterID):
                if nbr in attr:
                    for val in attr[nbr]:
                        nbrs_attr_values.append(val)
                        # print('nbrs: ', nbrs_attr_values)
            predicted_values[node] = []
            if nbrs_attr_values:  # non empty list
                # count the number of occurrence each value and returns a dict
                cpt = Counter(nbrs_attr_values)
                # print('cpt:', cpt)
                # print('Items: ', cpt.items())
                # take the most represented attribute value among neighbors
                a, nb_occurrence = max(cpt.items(), key=lambda t: t[1])
                predicted_values[node].append(a)
        return predicted_values
예제 #4
0
def recursive_partition(g, taxonomy_out, query_topic, k=4):
    """
    Based on a query topic and a vertex and edge-weighted graph, partition the graph into a query-based topical taxonomy
    :param g: source graph
    :param taxonomy_out: output graph (can be empty)
    :param query_topic: the head vertex to generate taxonomy from
    :param k: partition size for graph bisection
    :return: taxonomy graph (taxonomy_out)
    """
    if query_topic not in g.nodes():
        return taxonomy_out, query_topic

    from lib.subgraph import get_subgraph # todo: this is here to prevent an annoying circular reference

    taxonomy_out.add_node(query_topic, weight=g.node[query_topic]["weight"])
    g_sub = get_subgraph(g, query_topic)
    if len(g_sub) > 1:
        #Graph().add_nodes_from() g_sub.add_node(query_topic, weight=g.node[query_topic]["weight"])
        return g_sub, query_topic
        x = metis.networkx_to_metis(g_sub)
        (edgecuts, parts) = metis.part_graph(x, k)

        for part in range(k):
            max_degree = 0
            max_node = None
            for node in [[node for node in g_sub.nodes()][i] for i, j in enumerate(parts) if j == part]:
                degree = g_sub.degree(node)
                if degree > max_degree:
                    max_node = node
                    max_degree = degree
            if max_node is not None:
                recursive_partition(
                    g_sub.subgraph([[node for node in g_sub.nodes()][i] for i, j in enumerate(parts) if j == part]),
                    taxonomy_out, max_node)
                taxonomy_out.add_node(max_node, weight=g_sub.node[max_node]["weight"])
                taxonomy_out.add_edge(query_topic, max_node)

    return taxonomy_out, query_topic
예제 #5
0
import networkx as nx
import metis

G = metis.example_networkx()

mg =  metis.networkx_to_metis(G)

(edgecuts, parts) = metis.part_graph(G, 3)
print("edgecuts", edgecuts)
print("parts", parts)

colors = ['red','blue','green']
for i, p in enumerate(parts):
    G.node[i]['color'] = colors[p]
nx.draw(G)

nx.write_dot(G, 'example.dot') # Requires pydot or pygraphviz
예제 #6
0
 def to_metis(self):
     """ Convert tree's graph into metis structure """
     return metis.networkx_to_metis(self.add_weights())
예제 #7
0
def offline_b_lin_method(nx_graph, attempt_split_parts=2, prob=PROB, approx_rank=False):

    """
    :param nx_graph: networkx graph
    :param attempt_split_parts:
    to split the graph into several parts
    note: might get fewer cuts desired
    :param prob: probability to restart to origin pos
    :param approx_rank: the similarity to decompose the W2 matrix
    :return: W_telta, Q1_I, U, A, V
    :note: if the W2 is a singular matrix then A add some value to it
    """

    # phase 1: graph partition
    import metis
    G = metis.networkx_to_metis(nx_graph)
    (objval, parts) = metis.part_graph(G, attempt_split_parts)
    # we should normalize parts since we CAN get the FEWER parts

    # record all node index in one partition {partitionId:[nodeIds]}
    groups = {}
    for r, gn in enumerate(parts):
        if gn not in groups.keys():
            groups[gn] = []
        groups[gn].append(r)
    for key in groups.keys():
        groups[key] = sorted(groups[key])
    # print (objval, parts)
    # we build the W_telta as a normalized matrix
    node_size = len(nx_graph.nodes())

    # first we construct the index
    row_idx = []
    for part in groups.keys():
        for idx in groups[part]:
            row_idx.append(idx)

    # represent and normalize the matrix
    # we could use other methods to normalize to see the effect
    W_telta = numpy_helper.normalize_matrix(nx_graph)

    # phase 2 and 3
    # w1 contains all within partition link
    W1_group = {}
    W1 = numpy.matrix([[0.0] * node_size] * node_size)
    cur_k_row = 0
    for gn in groups.keys():
        matrix_len = len(groups[gn])
        W1_group[gn] = numpy.matrix([[0.0]*matrix_len]*matrix_len)
        for i in range(0, matrix_len):
            for j in range(0, matrix_len):
                W1_group[gn][i, j] = W_telta[cur_k_row+i, cur_k_row+j]
                W1[cur_k_row+i, cur_k_row+j] = W_telta[cur_k_row+i, cur_k_row+j]
        cur_k_row += matrix_len
    # w2 contains all without partition link
    W2 = W_telta - W1

    # phase 4
    # pre-compute Q
    Q1_I_group = {}
    for key in W1_group.keys():
        Q1_I_group[key] = \
            (numpy.identity(W1_group[key].shape[0]) - prob*W1_group[key]).I

    # phase 5
    # do low rand approx
    # currently we use the default
    # *we may further use other approx to test*
    # #pymf#
    U, S, V = low_rank_approx_svd(W2, approx_rank)
    # TODO if u v s is not full rank matrix we need to compute NB_LIN
    try:
        S_I = S.I
    except Exception:
        # make it reverse
        S += 1e-15 *numpy.identity(S.shape[0])

    # phase 6 construct Q1_I
    Q1_I = numpy.matrix([[0.0]*node_size]*node_size)
    diag_index = 0
    for gn in groups.keys():
        if gn in Q1_I_group.keys():
            sz = Q1_I_group[gn].shape[0]
            for i in range(0, sz):
                for j in range(0, sz):
                    Q1_I[i+diag_index, j+diag_index] = Q1_I_group[gn][i, j]
            diag_index += sz
    A = (S.I - prob*V*Q1_I*U).I
    return W_telta, Q1_I, U, A, V
예제 #8
0
파일: oracles.py 프로젝트: xybu/cs590-map
 def networkx_to_metis(self):
     self.metis_graph = metis.networkx_to_metis(self.graph)
예제 #9
0
g = nx.read_adjlist("email-Eu-core.txt", nodetype=int)


def createGraph(filename):
    G = nx.Graph()
    for line in open(filename):
        strlist = line.split()
        n1 = int(strlist[0])
        n2 = int(strlist[1])
        G.add_edges_from([(n1, n2)])
    return G


g = createGraph("email-Eu-core.txt")
metis.networkx_to_metis(g)


def read_data(filename):
    adj_list = []
    with open(filename, 'r') as f:
        lines = f.readlines()
        for line in lines:
            temp = line.split(' ')
            # max_v = max(max_v, int(temp[0]), int(temp[1]))
            adj_list.append((int(temp[0]), int(temp[1])))
    return adj_list


adjlist = read_data('email-Eu-core.txt')
예제 #10
0
#!/usr/bin/python3

import sys

import metis

import oracles
import parse_input

chaco_graph_file = sys.argv[1]
vhost_cpu_file = sys.argv[2]
oracle = oracles.ChacoOracle(chaco_graph_file)
oracle.update_vhost_cpu_req(parse_input.read_flat_ints(vhost_cpu_file))

metis.networkx_to_metis(oracle.graph)