def test_cycles(self):
     K_undir = nx.all_pairs_node_connectivity(self.cycle)
     for source in K_undir:
         for target, k in K_undir[source].items():
             assert_true(k == 2)
     K_dir = nx.all_pairs_node_connectivity(self.directed_cycle)
     for source in K_dir:
         for target, k in K_dir[source].items():
             assert_true(k == 1)
 def test_paths(self):
     K_undir = nx.all_pairs_node_connectivity(self.path)
     for source in K_undir:
         for target, k in K_undir[source].items():
             assert_true(k == 1)
     K_dir = nx.all_pairs_node_connectivity(self.directed_path)
     for source in K_dir:
         for target, k in K_dir[source].items():
             if source < target:
                 assert_true(k == 1)
             else:
                 assert_true(k == 0)
def create_dynamic_density_architecture(n_qubits,
                                        density_prob=0.1,
                                        backend=None,
                                        **kwargs):
    # Generate a random graph
    graph = nx.gnp_random_graph(n_qubits, density_prob)
    # Make sure it is connected
    connectivity = nx.all_pairs_node_connectivity(graph)
    disconnected = [(v1, v2) for v1, d in connectivity.items()
                    for v2, score in d.items() if score == 0]
    while disconnected != []:
        to_add = np.random.choice(len(disconnected))
        graph.add_edge(*disconnected[to_add])
        connectivity = nx.all_pairs_node_connectivity(graph)
        disconnected = [(v1, v2) for v1, d in connectivity.items()
                        for v2, score in d.items() if score == 0]
    # Number the edges
    numbered = {}
    numbered = {v: i for i, v in enumerate(nx.dfs_postorder_nodes(graph))}
    # Make the coupling graph and adjust the numbering
    m = nx.to_numpy_array(graph)
    perm = [numbered[i] for i in range(n_qubits)]
    perm = [perm.index(i) for i in range(n_qubits)]
    m = m[perm][:, perm]
    # Find the reduce order
    spanning_tree = nx.dfs_tree(graph)
    #print(*[[numbered[n] for n in edge] for edge in spanning_tree.edges()])
    g = Graph(backend=backend)
    vertices = g.add_vertices(n_qubits)
    g.add_edges([(vertices[v1], vertices[v2])
                 for v1, v2 in [[numbered[n] for n in edge]
                                for edge in spanning_tree.edges()]])
    arch = Architecture(name="temp",
                        coupling_graph=g,
                        backend=backend,
                        **kwargs)
    subgraph = [i for i in range(n_qubits)]
    reduce_order = []
    while subgraph != []:
        non_cutting = arch.non_cutting_vertices(subgraph)
        node = max(non_cutting)
        reduce_order.append(node)
        subgraph.remove(node)
    #arch.reduce_order = reduce_order
    #reduce_order = []
    name = dynamic_size_architecture_name(DENSITY + str(density_prob),
                                          n_qubits)
    arch = Architecture(name,
                        coupling_matrix=m,
                        reduce_order=reduce_order,
                        **kwargs)
    return arch
예제 #4
0
        def set_connectivity(data):
            df = data.copy()
            df['DateOfDeparture'] = pd.to_datetime(df['DateOfDeparture'])
            df['month'] = df['DateOfDeparture'].dt.week.astype(str)
            df['year'] = df['DateOfDeparture'].dt.year.astype(str)
            df['arr_dep'] = df[['Departure',
                                'Arrival']].apply(lambda x: '-'.join(x),
                                                  axis=1)

            G = nx.Graph()
            connectivity = {}

            list_dep_arr = zip(df['Departure'], df['Arrival'])
            G.add_edges_from(list_dep_arr)
            #G.number_of_nodes()
            #G.number_of_edges()
            connectivity = nx.all_pairs_node_connectivity(G)
            for j, jtem in enumerate(connectivity):
                if j == 0:
                    a = [jtem] * len(connectivity[jtem])
                    d = pd.DataFrame(connectivity[jtem].items())
                    d = pd.concat([pd.DataFrame(a), d], axis=1)
                    c = d.copy()
                a = [jtem] * len(connectivity[jtem])
                d = pd.DataFrame(connectivity[jtem].items())
                d = pd.concat([pd.DataFrame(a), d], axis=1)
                c = c.append(d)
            c['arr_dep'] = c[c.columns[[0]]].apply(lambda x: '-'.join(x),
                                                   axis=1)
            connectivity_dep_arr = dict(zip(c['arr_dep'], c[1]))

            df['connectivity'] = df['arr_dep'].map(connectivity_dep_arr)
            return df
예제 #5
0
 def min_connectivity(self, graph):
     apnc = nx.all_pairs_node_connectivity(graph)
     # start with graph diameter; minimum won't be larger than this
     mc = nx.diameter(graph)
     for targets in apnc.itervalues():
         mc = min(min(targets.itervalues()), mc)
     return mc
예제 #6
0
 def min_connectivity(self, graph):
     apnc = nx.all_pairs_node_connectivity(graph)
     # start with graph diameter; minimum won't be larger than this
     mc = nx.diameter(graph)
     for targets in apnc.itervalues():
         mc = min(min(targets.itervalues()), mc)
     return mc
        def set_connectivity(data):    
            df = data.copy()
            df['DateOfDeparture'] = pd.to_datetime(df['DateOfDeparture'])
            df['month'] = df['DateOfDeparture'].dt.week.astype(str)
            df['year'] = df['DateOfDeparture'].dt.year.astype(str)
            df['arr_dep'] = df[['Departure','Arrival']].apply(lambda x: '-'.join(x),axis=1)

            G = nx.Graph()
            connectivity = {}

            list_dep_arr = zip(df['Departure'], df['Arrival'])
            G.add_edges_from(list_dep_arr)
            #G.number_of_nodes()
            #G.number_of_edges()
            connectivity = nx.all_pairs_node_connectivity(G)
            for j, jtem in enumerate(connectivity):
                if j==0:
                    a=[jtem]*len(connectivity[jtem])
                    d = pd.DataFrame(connectivity[jtem].items())
                    d = pd.concat([pd.DataFrame(a),d], axis=1)
                    c = d.copy()
                a=[jtem]*len(connectivity[jtem])
                d = pd.DataFrame(connectivity[jtem].items())
                d = pd.concat([pd.DataFrame(a),d], axis=1)
                c = c.append(d)
            c['arr_dep'] = c[c.columns[[0]]].apply(lambda x: '-'.join(x),axis=1)
            connectivity_dep_arr = dict(zip(c['arr_dep'],c[1]))

            df['connectivity'] = df['arr_dep'].map(connectivity_dep_arr)
            return df
예제 #8
0
 def test_all_pairs_connectivity_nbunch(self):
     G = nx.complete_graph(5)
     nbunch = [0, 2, 3]
     A = dict.fromkeys(nbunch, dict())
     for u, v in itertools.combinations(nbunch, 2):
         A[u][v] = nx.node_connectivity(G, u, v)
     C = nx.all_pairs_node_connectivity(G, nbunch=nbunch)
     assert_equal(sorted((k, sorted(v)) for k, v in A.items()),
                  sorted((k, sorted(v)) for k, v in C.items()))
예제 #9
0
 def test_all_pairs_connectivity_nbunch_iter(self):
     G = nx.complete_graph(5)
     nbunch = [0, 2, 3]
     A = dict.fromkeys(nbunch, dict())
     for u, v in itertools.combinations(nbunch, 2):
         A[u][v] = nx.node_connectivity(G, u, v)
     C = nx.all_pairs_node_connectivity(G, nbunch=iter(nbunch))
     assert_equal(sorted((k, sorted(v)) for k, v in A.items()),
                  sorted((k, sorted(v)) for k, v in C.items()))
예제 #10
0
 def test_all_pairs_connectivity_nbunch(self):
     G = nx.complete_graph(5)
     nbunch = [0, 2, 3]
     A = {n: {} for n in nbunch}
     for u, v in itertools.combinations(nbunch, 2):
         A[u][v] = A[v][u] = nx.node_connectivity(G, u, v)
     C = nx.all_pairs_node_connectivity(G, nbunch=nbunch)
     assert_equal(sorted((k, sorted(v)) for k, v in A.items()),
                  sorted((k, sorted(v)) for k, v in C.items()))
예제 #11
0
 def test_all_pairs_connectivity_nbunch_iter(self):
     G = nx.complete_graph(5)
     nbunch = [0, 2, 3]
     A = {n: {} for n in nbunch}
     for u, v in itertools.combinations(nbunch, 2):
         A[u][v] = A[v][u] = nx.node_connectivity(G, u, v)
     C = nx.all_pairs_node_connectivity(G, nbunch=iter(nbunch))
     assert_equal(sorted((k, sorted(v)) for k, v in A.items()),
                  sorted((k, sorted(v)) for k, v in C.items()))
예제 #12
0
def add_shortest_path(rand, graph, min_length=1):
    """Samples a shortest path from A to B and adds attributes to indicate it.

  Args:
    rand: A random seed for the graph generator. Default= None.
    graph: A `nx.Graph`.
    min_length: (optional) An `int` minimum number of edges in the shortest
      path. Default= 1.

  Returns:
    The `nx.DiGraph` with the shortest path added.

  Raises:
    ValueError: All shortest paths are below the minimum length
  """
    node_connected = nx.all_pairs_node_connectivity(graph)
    #   path = nx.all_simple_paths(graph, 1, 4)
    paths = []
    path_nodes = []

    # print
    #   print("node_connected_list", list(node_connected))
    # print(type(node_connected))

    i = random.choice(list(node_connected))
    source = i
    #   print(i)
    node_connected_pair = {}
    node_reachable = []
    for x, yy in node_connected.items():
        for y, l in yy.items():
            if x == i and l > 0:
                node_connected_pair[x, y] = l
                path = nx.all_simple_paths(graph, x, y)
                node_reachable.append(y)
                for p in list(path):
                    paths.extend(list(pairwise(p)))

    node_pairs = list(node_connected_pair)
    paths = set(paths)
    path_nodes = set(path_nodes)

    digraph = graph

    digraph.add_node(source, source=True)
    digraph.add_nodes_from(set_diff(digraph.nodes(), [source]), source=False)
    digraph.add_nodes_from(node_reachable, reachable=True)
    digraph.add_nodes_from(set_diff(digraph.nodes(), node_reachable),
                           reachable=False)
    digraph.add_nodes_from(set_diff(digraph.nodes(), path_nodes),
                           solution=False)
    digraph.add_nodes_from(path_nodes, solution=True)
    digraph.add_edges_from(set_diff(digraph.edges(), paths), solution=False)
    digraph.add_edges_from(paths, solution=True)

    return digraph
예제 #13
0
 def test_all_pairs_connectivity(self):
     G = nx.Graph()
     nodes = [0, 1, 2, 3]
     G.add_path(nodes)
     A = dict.fromkeys(G, dict())
     for u, v in itertools.combinations(nodes, 2):
         A[u][v] = nx.node_connectivity(G, u, v)
     C = nx.all_pairs_node_connectivity(G)
     assert_equal(sorted((k, sorted(v)) for k, v in A.items()),
                  sorted((k, sorted(v)) for k, v in C.items()))
예제 #14
0
 def test_all_pairs_connectivity(self):
     G = nx.Graph()
     nodes = [0, 1, 2, 3]
     nx.add_path(G, nodes)
     A = {n: {} for n in G}
     for u, v in itertools.combinations(nodes, 2):
         A[u][v] = A[v][u] = nx.node_connectivity(G, u, v)
     C = nx.all_pairs_node_connectivity(G)
     assert (sorted((k, sorted(v)) for k, v in A.items()) == sorted(
         (k, sorted(v)) for k, v in C.items()))
예제 #15
0
 def test_all_pairs_connectivity_directed(self):
     G = nx.DiGraph()
     nodes = [0, 1, 2, 3]
     G.add_path(nodes)
     A = dict.fromkeys(G, dict())
     for u, v in itertools.permutations(nodes, 2):
         A[u][v] = nx.node_connectivity(G, u, v)
     C = nx.all_pairs_node_connectivity(G)
     assert_equal(sorted((k, sorted(v)) for k, v in A.items()),
                  sorted((k, sorted(v)) for k, v in C.items()))
예제 #16
0
 def test_all_pairs_connectivity(self):
     G = nx.Graph()
     nodes = [0, 1, 2, 3]
     nx.add_path(G, nodes)
     A = {n: {} for n in G}
     for u, v in itertools.combinations(nodes,2):
         A[u][v] = A[v][u] = nx.node_connectivity(G, u, v)
     C = nx.all_pairs_node_connectivity(G)
     assert_equal(sorted((k, sorted(v)) for k, v in A.items()),
                  sorted((k, sorted(v)) for k, v in C.items()))
예제 #17
0
 def test_all_pairs_connectivity_directed(self):
     G = nx.DiGraph()
     nodes = [0, 1, 2, 3]
     nx.add_path(G, nodes)
     A = {n: {} for n in G}
     for u, v in itertools.permutations(nodes, 2):
         A[u][v] = nx.node_connectivity(G, u, v)
     C = nx.all_pairs_node_connectivity(G)
     assert_equal(sorted((k, sorted(v)) for k, v in A.items()),
                  sorted((k, sorted(v)) for k, v in C.items()))
예제 #18
0
        def node_conn(graph):
            # calculating node connectivity
            node_connectivity = nx.all_pairs_node_connectivity(graph)
            N = graph.number_of_nodes()

            node_conn = np.zeros([N, N])
            for key1, value1 in node_connectivity.items():
                for key2, value2 in value1.items():
                    node_conn[key1, key2] = value2
            return list(np.triu(node_conn).flatten())
예제 #19
0
파일: paraOrder.py 프로젝트: Lancert9/WUA
def cal_para_order(para_list):
    """
    To record the parameter variable's all fixed order.
    :param para_list: the url's kinds of url's.
    :return: list -> all fixed parameter order.
    """
    result_order = list()

    order_collection = set()
    for order_seg in para_list:
        length = len(order_seg)
        for i in range(length - 1):
            order_collection.add((order_seg[i], order_seg[i + 1]))
    print 'G2:', sorted(list(order_collection))
    g = nx.DiGraph()
    g.add_edges_from(order_collection)
    connectivity_dict = dict([])
    for source_node, connect_dict in nx.all_pairs_node_connectivity(g).items():
        connectivity_dict[source_node] = []
        for target_node, hop in connect_dict.items():
            if hop > 0:
                connectivity_dict[source_node].append(target_node)
    print 'Connectivity_Original:', nx.all_pairs_node_connectivity(g)
    print 'Connectivity_dict:', connectivity_dict
    node_list = g.nodes()
    node_length = len(node_list)
    for i in range(node_length):
        for j in range(i + 1, node_length):
            node_1 = node_list[i]
            node_2 = node_list[j]
            connect_1_2 = node_2 in connectivity_dict[node_1]
            connect_2_1 = node_1 in connectivity_dict[node_2]
            if connect_1_2 and not connect_2_1:
                result_order.append((node_1, node_2))
            elif connect_2_1 and not connect_1_2:
                result_order.append((node_2, node_1))
            else:
                continue

    return result_order
예제 #20
0
    def approximation(self):
        result = {}
        result['all_pairs_node_connectivity'] = nx.all_pairs_node_connectivity(
            self.graph)
        result['node_connectivity'] = nx.node_connectivity(self.graph)

        if self.directed == 'undirected':
            result['k_components'] = nx.k_components(self.graph)
            result['average_clustering'] = nx.average_clustering(self.graph)

        fname_approx = self.DIR + '/appoximation.json'
        with open(fname_approx, "w") as f:
            json.dump(result, f, cls=SetEncoder, indent=2)
        print(fname_approx)
예제 #21
0
 def compute_node_connectivity(self):
     connectivity_dict = nx.all_pairs_node_connectivity(
         self.virtual_solution_graph,
         self.formulation.graph_container.end_nodes)
     min_connectivity = 1e20
     total_connectivity = 0
     for key, val in connectivity_dict.items():
         vals = list(val.values())
         total_connectivity += sum(vals)
         if min(vals) < min_connectivity:
             min_connectivity = min(vals)
     # Divide by 2 times the unique (s,t) pairs, since connectivity is computed in both directions
     avg_connectivity = total_connectivity / (
         2 * self.formulation.graph_container.num_unique_pairs)
     return min_connectivity, avg_connectivity
예제 #22
0
def GetAllPairsNodeConnectivity(G, csvfile):
    with open(csvfile, 'w') as file:
        data = collections.OrderedDict()
        nodes = sorted(nx.nodes(G))
        apnc = nx.all_pairs_node_connectivity(G)
        # Write Header
        for n in nodes:
            file.write(',' + str(n))
        file.write('\n')
        # Create and Write Body
        for n in nodes:
            s = str(n)
            for k in nodes:
                s += ','
                if n != k:
                    s += str(apnc[n][k])
            file.write(s + '\n')
예제 #23
0
def main():
    pairs_filename = sys.argv[1]
    selections_filename = sys.argv[2]
    pairs = read_pairs(pairs_filename)
    selections = read_selections(selections_filename)
    dg = nx.DiGraph()
    dg.add_weighted_edges_from(pairs)
    connectivity = nx.all_pairs_node_connectivity(dg)
    results = OrderedSet()
    for k, v in connectivity.items():
        if k in selections:
            results.add(MAPPING[k])
            for k2, v2 in v.items():
                if v2:
                    results.add(MAPPING[k2])
    for r in results:
        print(r)
예제 #24
0
def reachable_from(graph, node):
    """
    Given an nx graph and a node within this graph,
    return all of the nodes in the same connected component as the input node.
    """
    import networkx as nx
    if isinstance(graph, nx.DiGraph):
        conn = nx.all_pairs_node_connectivity(graph)
        return [n1 for (n1, dist) in conn[node].iteritems() if dist > 0]

    elif isinstance(graph, nx.Graph):
        for comp in nx.connected_components(graph):
            if node in comp:
                return comp

    # Shouldn't get here
    # since the node should appear in some connected component of the graph
    raise Exception("Node {} not in graph".format(node))
예제 #25
0
def Play_Add_backup(G, N, B, U, C_Val, Path_E, D):
    E = nx.all_pairs_node_connectivity(G)
    for i in N:
        max_u = np.sort(U[i, :])
        max_u = max_u[::-1]
        # print max_u
        m = 0
        while (B[i] > 0 and m < NUMBER_OF_NODES and chk_conn(E, i)):
            j = U[i, :].tolist().index(max_u[m])
            # print j
            cost = C_Val[i][j][0] + C_Val[i][j][2]
            if U[i, j] > 0 and cost < B[i] and [i, j] not in N_warn:
                G.add_edge(i, j)
                B[i] = B[i] - cost
            m = m + 1

    LOG.info('Add Played')
    return G, B
예제 #26
0
def roda_funcoes_grafo(nx_grafo):
    # Connectivity (em "Approximations and Heuristics")
    print('Connectivity')
    print(nx.all_pairs_node_connectivity(nx_grafo))
    # Centrality degree (em "Centrality")
    print('Centrality degree')
    print(nx.degree_centrality(nx_grafo))
    # Closeness (em "Centrality")
    print('Closeness')
    print(nx.closeness_centrality(nx_grafo, distance='weight'))
    # (Shortest Path) Betweenness (em "Centrality")
    print('Shortest path betweenness')
    print(
        nx.betweenness_centrality(nx_grafo, normalized=False, weight='weight'))
    # Communicability Betweenness
    print('Communicability Betweenness')
    print(
        nx.communicability_betweenness_centrality(nx_grafo.to_undirected(),
                                                  normalized=False))
예제 #27
0
    def __cal_variable_order_rule(variable_order_set):
        """
        To record the parameter variable's all fixed order.

        :param variable_order_set: set((v1, v2, v3), (v3, v4), ...)
        :return: set((v1, v2), (v1, v3), ...)
        """
        result_order = set()

        order_collection = set()
        for order_seg in variable_order_set:
            for i in range(len(order_seg) - 1):
                order_collection.add((order_seg[i], order_seg[i + 1]))

        g = nx.DiGraph()
        g.add_edges_from(order_collection)
        connectivity_dict = dict([])
        for source_node, connect_dict in nx.all_pairs_node_connectivity(
                g).items():
            connectivity_dict[source_node] = set()
            for target_node, hop in connect_dict.items():
                if hop > 0:
                    connectivity_dict[source_node].add(target_node)

        node_list = g.nodes()
        node_count = len(node_list)
        for i in range(node_count):
            for j in range(i + 1, node_count):
                node_1 = node_list[i]
                node_2 = node_list[j]
                # if node_1 connect to node_2
                connect_1_2 = node_2 in connectivity_dict[node_1]
                # if node_2 connect to node_1
                connect_2_1 = node_1 in connectivity_dict[node_2]
                if connect_1_2 and not connect_2_1:
                    result_order.add((node_1, node_2))
                elif connect_2_1 and not connect_1_2:
                    result_order.add((node_2, node_1))
                else:
                    continue

        return result_order
예제 #28
0
파일: HostFeature.py 프로젝트: Lancert9/WUA
    def __cal_variable_order_rule(variable_order_set):
        """
        To record the parameter variable's all fixed order.

        :param variable_order_set: set((v1, v2, v3), (v3, v4), ...)
        :return: set((v1, v2), (v1, v3), ...)
        """
        result_order = set()

        order_collection = set()
        for order_seg in variable_order_set:
            for i in range(len(order_seg) - 1):
                order_collection.add((order_seg[i], order_seg[i + 1]))

        g = nx.DiGraph()
        g.add_edges_from(order_collection)
        connectivity_dict = dict([])
        for source_node, connect_dict in nx.all_pairs_node_connectivity(g).items():
            connectivity_dict[source_node] = set()
            for target_node, hop in connect_dict.items():
                if hop > 0:
                    connectivity_dict[source_node].add(target_node)

        node_list = g.nodes()
        node_count = len(node_list)
        for i in range(node_count):
            for j in range(i + 1, node_count):
                node_1 = node_list[i]
                node_2 = node_list[j]
                # if node_1 connect to node_2
                connect_1_2 = node_2 in connectivity_dict[node_1]
                # if node_2 connect to node_1
                connect_2_1 = node_1 in connectivity_dict[node_2]
                if connect_1_2 and not connect_2_1:
                    result_order.add((node_1, node_2))
                elif connect_2_1 and not connect_1_2:
                    result_order.add((node_2, node_1))
                else:
                    continue

        return result_order
예제 #29
0
 def test_all_pairs_connectivity_icosahedral(self):
     G = nx.icosahedral_graph()
     C = nx.all_pairs_node_connectivity(G)
     assert_true(all(5 == C[u][v] for u, v in itertools.combinations(G, 2)))
예제 #30
0
 def test_all_pairs_connectivity_icosahedral(self):
     G = nx.icosahedral_graph()
     C = nx.all_pairs_node_connectivity(G)
     assert_true(all(5 == C[u][v] for u, v in itertools.combinations(G, 2)))
예제 #31
0
    def state_features(self):
        f = []

        legal_actions = self.get_legal_actions()

        if params.use_in_out_matrix:
            out_degree = self.G.out_degree(self.I)
            for (i, j) in out_degree:
                f.extend(
                    RP_utils.polynomialize(j / params.m,
                                           params.num_polynomial))

            in_degree = self.G.in_degree(self.I)
            for (i, j) in in_degree:
                f.extend(
                    RP_utils.polynomialize(j / params.m,
                                           params.num_polynomial))

        if params.use_in_out_relative_matrix:
            out_degree = self.G.out_degree(self.I)
            for (i, j) in out_degree:
                f.extend(
                    RP_utils.polynomialize(
                        RP_utils.safe_div(j, self.E_0.out_degree(i)),
                        params.num_polynomial))

            in_degree = self.G.in_degree(self.I)
            for (i, j) in in_degree:
                f.extend(
                    RP_utils.polynomialize(
                        RP_utils.safe_div(j, self.E_0.in_degree(i)),
                        params.num_polynomial))

        if params.use_total_degree_matrix:
            for i in self.I:
                i_total = self.G.out_degree(i) + self.G.in_degree(i)
                i_e0_total = self.E_0.out_degree(i) + self.E_0.in_degree(i)
                f.extend(
                    RP_utils.polynomialize(
                        RP_utils.safe_div(i_total, i_e0_total),
                        params.num_polynomial))

        if params.use_in_out_binary_matrix:
            out_degree = self.G.out_degree(self.I)
            for (i, j) in out_degree:
                f.append(2 * int(j > 0) - 1)

            in_degree = self.G.in_degree(self.I)
            for (i, j) in in_degree:
                f.append(2 * int(j > 0) - 1)

        if params.use_voting_rules_matrix:
            for i in self.plurality_scores:
                f.extend(RP_utils.polynomialize(i, params.num_polynomial))
            for i in self.borda_scores:
                f.extend(RP_utils.polynomialize(i, params.num_polynomial))
            for i in self.copeland_scores:
                f.extend(RP_utils.polynomialize(i, params.num_polynomial))
            for i in self.maximin_scores:
                f.extend(RP_utils.polynomialize(i, params.num_polynomial))

        if params.use_edge_weight:
            f.extend(
                RP_utils.polynomialize(
                    self.E_0_really[legal_actions[0][0]][legal_actions[0][1]]
                    ['weight'] / self.max_edge_weight, params.num_polynomial))

        if params.use_vectorized_wmg:
            f.extend(self.vectorized_wmg)

        if params.use_posmat:
            f.extend(self.posmat)

        if params.use_tier_adjacency_matrix:
            T_matrix = np.zeros((int(params.m), int(params.m)))
            for (c1, c2) in legal_actions:
                T_matrix[c1, c2] = 1
            T_vec = list(T_matrix.flatten())
            f.extend(T_vec)

        if params.use_connectivity_matrix:
            for i in self.I:
                for j in self.I:
                    if i != j:
                        f.extend(
                            RP_utils.polynomialize(
                                local_edge_connectivity(self.G, i, j) /
                                (params.m - 2), params.num_polynomial)
                        )  # normalized by m-2 since max edges needed to disconnect i and j is all edges but i -> i and i -> j
            all_pairs_node_connectivity = nx.all_pairs_node_connectivity(
                self.G)
            for i in self.I:
                for j in self.I:
                    if i != j:
                        f.extend(
                            RP_utils.polynomialize(
                                all_pairs_node_connectivity[i][j] /
                                (params.m - 2), params.num_polynomial)
                        )  # normalized by m-2 since max nodes needed to disconnect i and j is all nodes but i and j

        # adjacency matrix of current state
        if params.use_adjacency_matrix:
            adjacency = nx.adjacency_matrix(self.G, nodelist=self.I).todense()
            adjacency = np.multiply(adjacency, self.adjacency_0)
            adjacency_normalized = np.divide(adjacency, params.n)
            f.extend(adjacency_normalized.flatten().tolist()[0])

        # K representation
        if params.use_K_representation:
            K_list = []
            for i in self.I:
                if i in self.K:
                    K_list.append(1)
                else:
                    K_list.append(0)
            f.extend(K_list)

        # node2vec every time
        # G_with_weights = nx.DiGraph()
        # G_with_weights.add_nodes_from(self.I)
        # for (cand1, cand2) in self.G.edges():
        #     G_with_weights.add_edge(cand1, cand2, weight=self.E_0_really[cand1][cand2]['weight'])
        # node2vec_G = node2vec.Graph(G_with_weights, True, self.node2vec_args.p, self.node2vec_args.q)
        # node2vec_G.preprocess_transition_probs()
        # walks = node2vec_G.simulate_walks(self.node2vec_args.num_walks, self.node2vec_args.walk_length)
        # self.node2vec_model = node2vecmain.learn_embeddings(walks, self.node2vec_args)

        # node2vec features
        # node2vec_u = self.node2vec_model.wv[str(u)]
        # node2vec_v = self.node2vec_model.wv[str(v)]
        # node2vec_uv = np.append(node2vec_u, node2vec_v)

        # node2vec_f = np.append(node2vec_uv, np.array(f))

        if params.debug_mode >= 3:
            print("features", f)

        return Variable(torch.from_numpy(np.array(f)).float())
    def state_features(self, G, K, T, profile):
        f = []

        E_0 = self.profile_to_E0[profile]
        adjacency_0 = self.profile_to_adjacency0[profile]

        if params.use_in_out_matrix:
            out_degree = G.out_degree(self.I)
            for (i, j) in out_degree:
                f.extend(
                    RP_utils.polynomialize(
                        RP_utils.safe_div(j, E_0.out_degree(i)),
                        params.num_polynomial))

            in_degree = G.in_degree(self.I)
            for (i, j) in in_degree:
                f.extend(
                    RP_utils.polynomialize(
                        RP_utils.safe_div(j, E_0.in_degree(i)),
                        params.num_polynomial))

        if params.use_total_degree_matrix:
            for i in self.I:
                i_total = G.out_degree(i) + G.in_degree(i)
                i_e0_total = E_0.out_degree(i) + E_0.in_degree(i)
                f.extend(
                    RP_utils.polynomialize(
                        RP_utils.safe_div(i_total, i_e0_total),
                        params.num_polynomial))

        if params.use_in_out_binary_matrix:
            out_degree = G.out_degree(self.I)
            for (i, j) in out_degree:
                f.append(2 * int(j > 0) - 1)

            in_degree = G.in_degree(self.I)
            for (i, j) in in_degree:
                f.append(2 * int(j > 0) - 1)

        if params.use_voting_rules_matrix:
            for i in self.profile_to_plurality[profile]:
                f.extend(RP_utils.polynomialize(i, params.num_polynomial))
            for i in self.profile_to_borda[profile]:
                f.extend(RP_utils.polynomialize(i, params.num_polynomial))
            for i in self.profile_to_copeland[profile]:
                f.extend(RP_utils.polynomialize(i, params.num_polynomial))
            for i in self.profile_to_maximin[profile]:
                f.extend(RP_utils.polynomialize(i, params.num_polynomial))

        if params.use_edge_weight:
            f.extend(
                RP_utils.polynomialize(
                    E_0[T[0][0]][T[0][1]]['weight'] /
                    self.profile_to_max_edge_weight[profile],
                    params.num_polynomial))

        if params.use_vectorized_wmg:
            f.extend(self.profile_to_vectorized_wmg[profile])

        if params.use_posmat:
            f.extend(self.profile_to_posmat[profile])

        if params.use_tier_adjacency_matrix:
            T_matrix = np.zeros((int(params.m), int(params.m)))
            for (c1, c2) in T:
                T_matrix[c1, c2] = 1
            T_vec = list(T_matrix.flatten())
            f.extend(T_vec)

        if params.use_connectivity_matrix:
            for i in self.I:
                for j in self.I:
                    if i != j:
                        f.extend(
                            RP_utils.polynomialize(
                                local_edge_connectivity(G, i, j) /
                                (params.m - 2), params.num_polynomial)
                        )  # normalized by m-2 since max edges needed to disconnect i and j is all edges but i -> i and i -> j
            all_pairs_node_connectivity = nx.all_pairs_node_connectivity(G)
            for i in self.I:
                for j in self.I:
                    if i != j:
                        f.extend(
                            RP_utils.polynomialize(
                                all_pairs_node_connectivity[i][j] /
                                (params.m - 2), params.num_polynomial)
                        )  # normalized by m-2 since max nodes needed to disconnect i and j is all nodes but i and j

        # adjacency matrix
        if params.use_adjacency_matrix:
            adjacency = nx.adjacency_matrix(G, nodelist=self.I).todense()
            adjacency = np.multiply(adjacency, adjacency_0)
            adjacency_normalized = np.divide(adjacency, params.n)
            f.extend(adjacency_normalized.flatten().tolist()[0])

        # K representation
        if params.use_K_representation:
            K_list = []
            for i in self.I:
                if i in K:
                    K_list.append(1)
                else:
                    K_list.append(0)
            f.extend(K_list)

        return Variable(torch.from_numpy(np.array(f)).float())
예제 #33
0
def main():
    # Directed Bison Network
    bison_file = 'moreno_bison/out.moreno_bison_bison'
    bison_graph = nx.DiGraph()

    create_network(bison_graph, bison_file, True)

    # Undirected Kangaroo Network
    kangaroo_file = 'moreno_kangaroo/out.moreno_kangaroo_kangaroo'
    kangaroo_graph = nx.Graph()

    create_network(kangaroo_graph, kangaroo_file, False)

    # Part A: Connected Component Analysis
    # Connected Component Analysis of Bison Directed Graph
    print("PART A:\n")
    print("Bison Directed Graph Connected Component Analysis",
          "\nWeakly connected: ", nx.is_weakly_connected(bison_graph),
          "\nNumber of Weakly CCs: ",
          nx.number_weakly_connected_components(bison_graph),
          "\nSize of largest CC: ",
          len(max(nx.weakly_connected_components(bison_graph),
                  key=len)), "\nSize of smallest CC: ",
          len(min(nx.weakly_connected_components(bison_graph), key=len)))

    # Connected Component Analysis of Kangaroo Undirected Graph
    print("\nKangaroo Undirected Graph Connected Component Analysis",
          "\nConnected: ", nx.is_connected(kangaroo_graph),
          "\nNumber of CCs: ", nx.number_connected_components(kangaroo_graph),
          "\nSize of largest CC: ",
          len(max(nx.connected_components(kangaroo_graph),
                  key=len)), "\nSize of smallest CC: ",
          len(min(nx.connected_components(kangaroo_graph), key=len)))

    # Part B Computing Degrees and finding the Probability distribution
    # Creation of an arrayList to store the degree for each node of Bison Network
    bison_degrees = []
    for node in range(1, 26):
        bison_degrees.append(bison_graph.degree(node))

    # Computing Mean and Standard Deviation for Directed
    x_label = stats(bison_degrees)

    # Creating a Histogram to plot the data of the degrees Bison Network
    plt.figure(3)
    plt.title('Part B: Histogram Directed Bison')
    plt.xlabel(x_label)
    plt.hist(bison_degrees, bins='auto')

    # Creation of an arrayList to store the degree for each node of Kangaroo Network
    kangaroo_degrees = []
    for node in range(1, 17):
        kangaroo_degrees.append(kangaroo_graph.degree(node))

    # Computing Mean and Standard Deviation for Undirected
    x_label = stats(kangaroo_degrees)

    # Creating a Histogram to plot the data of the degrees for Kangaroo Network
    plt.figure(4)
    plt.title('Part B: Histogram Undirected Kangaroo')
    plt.xlabel(x_label)
    plt.hist(kangaroo_degrees, bins='auto')
    # lt.show()

    # Part C Find the Path between 2 abritrary vertices in the largest CC
    # Creating two arbritrary nodes making sure they aren't the same number
    node1 = random.randrange(1, 27, 1)
    node2 = random.randrange(1, 27, 1)
    while node1 == node2:
        node1 = random.randrange(1, 27, 1)

    # I put a cutoff on the list of simple paths for now so I can atleast run something
    # cut off is the act of only focusing on the paths <= 5
    # This section creates a list of all simple paths and then creates a list with the lengths of these paths
    bison_paths = list(nx.all_simple_paths(bison_graph, node1, node2,
                                           cutoff=5))
    bison_p_lengths = []

    for node in range(0, len(bison_paths) - 1):
        bison_p_lengths.append(len(bison_paths[node]))

    x_label = stats(bison_p_lengths)

    # Creating a histogram for the degrees of the graph
    plt.figure(5)
    plt.title('Part C: Histogram Directed Bison Paths')
    plt.xlabel(x_label)
    plt.hist(bison_p_lengths, bins='auto')
    # plt.show()

    # Creating two arbitrary nodes making sure they aren't the same number
    node1 = random.randrange(1, 17, 1)
    node2 = random.randrange(1, 17, 1)
    while node1 == node2:
        node1 = random.randrange(1, 17, 1)

    # This section creates a list of all simple paths and then creates a list with the lengths of these paths
    kangaroo_paths = list(
        nx.all_simple_paths(kangaroo_graph, node1, node2, cutoff=5))
    kangaroo_p_lengths = []

    for node in range(0, len(kangaroo_paths) - 1):
        kangaroo_p_lengths.append(len(kangaroo_paths[node]))

    x_label = stats(kangaroo_p_lengths)

    # Creating a histogram for the degrees of the graph
    plt.figure(6)
    plt.title('Part C: Histogram Undirected Kangaroo Paths')
    plt.xlabel(x_label)
    plt.hist(kangaroo_p_lengths, bins='auto')

    # plt.show()

    # Part D Find the Simple Circuits between 2 abritrary vertices in the largest CC
    # UNABLE TO RUN BISON CIRCUITS ON LAPTOP THERE ARE TO MANY AND I CANNOT CREATE A CUTOFF

    # Creates a list of simple cycles and then creates another list of the lengths of the cycles
    # bison_circuits = list(nx.simple_cycles(bison_graph))
    # bison_c_lengths = []
    # for node in range(0,len(bison_circuits)-1):
    #    bison_c_lengths.append(len(bison_circuits[node]))
    #
    # x_label = stats(bison_c_lengths)
    #
    # plt.figure(7)
    # plt.title('PART D: Histogram Directed Bison Circuits')
    # plt.xlabel(x_label)
    # plt.hist(bison_c_lengths, bins = 'auto')

    # You can't use the simple cycle function for undirected graphs so I used the basis function.
    # Creates a list of simple cycles and then creates another list of the lengths of the cycles
    kangaroo_circuits = nx.cycle_basis(kangaroo_graph)
    kangaroo_c_lengths = []
    for node in range(0, len(kangaroo_circuits) - 1):
        kangaroo_c_lengths.append(len(kangaroo_circuits[node]))

    x_label = stats(kangaroo_c_lengths)
    plt.figure(7)
    plt.title('PART D: Histogram Undirected Kangaroo Circuits')
    plt.xlabel(x_label)
    plt.hist(kangaroo_c_lengths, bins='auto')
    # plt.show()

    # Part E Check if Eulerian, Find a Eulerian Path
    print("\nPART E:")
    print("\nDirected Bison Graph")
    print("Euelerian: ", nx.is_eulerian(bison_graph))
    print("Has a Eulerian Path: ", nx.has_eulerian_path(bison_graph))

    print("\nUndirected Kangaroo Graph")
    print("Euelerian: ", nx.is_eulerian(kangaroo_graph))
    print("Has a Eulerian Path: ", nx.has_eulerian_path(kangaroo_graph))

    # Part F: Convert to Matrix.
    # I don't know if this covers everything?
    bison_matrix = nx.to_numpy_matrix(bison_graph)
    plt.matshow(bison_matrix)
    # plt.show()

    kangaroo_matrix = nx.to_numpy_matrix(kangaroo_graph)
    plt.matshow(kangaroo_matrix)
    # plt.show()

    # Part G: Copy Largest CC comparing it to a copy and a slightly different CC
    print("\nPart G:\n")
    # copying the largest connected component from the Bison Directed graph
    bison_n1 = nx.Graph()
    largest_cc_bison = list(
        max(nx.weakly_connected_components(bison_graph), key=len))
    for i in largest_cc_bison:
        bison_n1.add_edge(i, i + 1)
    bison_n2 = bison_n1.copy()

    # Checking Equivalence between copied graphs
    print("Is bison_n1 Equivalent to bison_n2?")
    compare(bison_n1, bison_n2)

    # Checking Equivalence between copied graphs but one has an extra 10 edges
    print("\nIs bison_n1 Equivalent to N3?")
    bison_n3 = bison_n2.copy()
    add_10_edges(bison_n3, len(bison_n3))
    compare(bison_n1, bison_n3)

    # Repeat for Kangaroo Undirected Network
    kangaroo_n1 = nx.Graph()
    largest_cc_kangaroo = list(
        max(nx.connected_components(kangaroo_graph), key=len))
    for i in largest_cc_kangaroo:
        kangaroo_n1.add_edge(i, i + 1)
    kangaroo_n2 = kangaroo_n1.copy()

    print("\nIs kangaroo_n1 Equivalent to kangaroo_n2?")
    compare(kangaroo_n1, kangaroo_n2)

    print("\nIs kangaroo_n1 Equivalent to N3?")
    kangaroo_n3 = kangaroo_n2.copy()
    add_10_edges(kangaroo_n3, len(kangaroo_n3))
    compare(kangaroo_n1, kangaroo_n3)

    # Part H: Generate Minimum Spanning Tree
    print("\nPart H:\n")
    # Cannot generate SPanning tree for Directed networks
    # Generating a minimum spanning tree for Undirected network
    kangaroo_min_tree = nx.minimum_spanning_tree(kangaroo_graph)
    print(
        "~A Minimum Spanning Tree was created for the Undirected Kangaroo Graph~"
    )
    tree_or_forest(kangaroo_min_tree)

    # Finding two random nodes that are connected
    x = 0
    y = 0
    while (not (kangaroo_min_tree.has_edge(x, y))):
        x = random.randrange(1, 17, 1)
        y = random.randrange(1, 17, 1)
        while x == y:
            x = random.randrange(1, 17, 1)

    # Removing the found edge
    print("\nAn edge from the spanning tree was removed")
    kangaroo_min_tree.remove_edge(x, y)
    tree_or_forest(kangaroo_min_tree)

    # Part I: Dijkstra's Algorithm

    bison_pairs = list(nx.all_pairs_node_connectivity(bison_graph))
    connected_nodes = []
    for i in bison_pairs:
        for j in bison_pairs:
            if bison_graph.has_edge(i, j + 1):
                connected_nodes.append([i, j + 1])

    dijkstra_paths = []
    length = len(connected_nodes)
    for i in range(0, length - 1):
        for j in range(0, 1):
            dijkstra_paths.append(
                int(
                    nx.dijkstra_path_length(bison_graph, connected_nodes[i][j],
                                            connected_nodes[i][j + 1])))

    x_label = stats(dijkstra_paths)

    plt.figure()
    plt.xlabel(x_label)
    plt.title('Directed Bison Dijkstra Path Lengths')
    plt.hist(dijkstra_paths)
    # plt.show()

    #Created a new temporary graph with edges from the connected nodes and weights from the distance list
    temp_bison = nx.DiGraph()
    for i in range(0, length - 1):
        j = 0
        temp_bison.add_edge(connected_nodes[i][j],
                            connected_nodes[i][j + 1],
                            weight=dijkstra_paths[i])

    # I dont really know if this creates a matrix for the weigths this is just what i did in a previous part
    bison_distance_matrix = nx.to_numpy_matrix(temp_bison)
    plt.matshow(bison_distance_matrix)
    plt.show()

    # Repeat for Kangaroo Undirected

    KangarooPairs = list(nx.all_pairs_node_connectivity(KangarooGraph))
    ConnectedNodesK = []
    for i in KangarooPairs:
        for j in KangarooPairs:
            if KangarooGraph.has_edge(i, j + 1):
                ConnectedNodesK.append([i, j + 1])

    dijkstra_PathsK = []
    length = len(ConnectedNodesK)
    for i in range(0, length):
        dijkstra_PathsK.append(
            int(
                nx.dijkstra_path_length(KangarooGraph, ConnectedNodesK[i][0],
                                        ConnectedNodesK[i][1])))
    xLabel = Stats(dijkstra_PathsK)

    plt.figure()
    plt.xlabel(xLabel)
    plt.title('Undirected Kangaroo Dijkstra Path Lengths')
    plt.hist(dijkstra_PathsK)
    plt.show()

    temp_kangaroo = nx.Graph()
    for i in range(0, length - 1):
        j = 0
        temp_kangaroo.add_edge(ConnectedNodesK[i][j],
                               ConnectedNodesK[i][j + 1],
                               weight=dijkstra_PathsK[i])

    kangaroo_distance_matrix = nx.to_numpy_matrix(temp_kangaroo)
    plt.matshow(kangaroo_distance_matrix)
    plt.show()
예제 #34
0
def reachable(graph):
    # graph = nx.DiGraph()
    # graph.add_edges_from([(1, 2), (3, 1), (4, 2), (5, 2), (2, 3), (1, 5)])
    node_connected = nx.all_pairs_node_connectivity(graph)
    path = nx.all_simple_paths(graph, 1, 4)
    paths = []
    path_nodes = []

    # print
    print("node_connected_list", list(node_connected))
    # print(type(node_connected))

    i = random.choice(list(node_connected))
    source = i
    print(i)
    node_connected_pair = {}
    node_reachable = []
    for x, yy in node_connected.items():
        # print("x, y: ", x,yy)
        # print(type(y))
        for y, l in yy.items():
            if x == i and l > 0:
                node_connected_pair[x, y] = l
                path = nx.all_simple_paths(graph, x, y)
                node_reachable.append(y)
                # print(path)
                # if len(list(path)) > path_max_length:
                #     longest_path = path
                #     path_max_length = len(list(path))
                for p in list(path):
                    # print(p)
                    paths.extend(list(gu.pairwise(p)))
                    path_nodes.extend(p)
            # print(x, y, l)

    node_pairs = list(node_connected_pair)
    paths = set(paths)
    path_nodes = set(path_nodes)
    # print(path_nodes)
    # print(list(node_connected_pair))
    # print(node_reachable)
    # print("paths:", paths)
    # print(set(paths))

    digraph = graph.to_directed()

    digraph.add_node(source, source=True)
    digraph.add_nodes_from(gu.set_diff(digraph.nodes(), [source]), source=False)
    digraph.add_nodes_from(node_reachable, reachable=True)
    digraph.add_nodes_from(gu.set_diff(digraph.nodes(), node_reachable), reachable=False)
    digraph.add_nodes_from(gu.set_diff(digraph.nodes(), path_nodes), solution=False)
    digraph.add_nodes_from(path_nodes, solution=True)
    digraph.add_edges_from(gu.set_diff(digraph.edges(), paths), solution=False)
    digraph.add_edges_from(paths, solution=True)
    nodes = (digraph.nodes[n] for n in digraph)
    print('nodes_list:', list(nodes))
    edges = (digraph.edges[(u, v)] for u, v in digraph.edges())
    print('edges_list:', list(edges))
    # rand = np.random.RandomState(seed=1)
    # i = rand.choice(len(node_pairs))

    # print(node_pairs[i])
    # start, end = node_pairs[i]
    # path = nx.all_simple_paths(
    #     G, source=start, target=end)
    #
    # print(len(path))

    return digraph
예제 #35
0
 def test_complete(self):
     for G in [self.K10, self.K5, self.K20]:
         K = nx.all_pairs_node_connectivity(G)
         for source in K:
             for target, k in K[source].items():
                 assert_true(k == len(G)-1)
예제 #36
0
 def test_all_pairs_connectivity_nbunch(self):
     G = nx.complete_graph(5)
     nbunch = [0, 2, 3]
     C = nx.all_pairs_node_connectivity(G, nbunch=nbunch)
     assert_equal(len(C), len(nbunch))
예제 #37
0
    def __init__(self,
                 img_skel,
                 img_dist,
                 units=1,
                 near_node_tol=5,
                 length_tol=1,
                 min_nodes=3,
                 plot=False,
                 img_enhanced=np.zeros(0),
                 output_dir='',
                 name='',
                 save_files=True):

        self.units = units
        self.img_skel = img_skel
        self.img_dist = img_dist
        self.near_node_tol = near_node_tol
        self.length_tol = length_tol
        self.min_nodes = min_nodes
        self.img_enhanced = img_enhanced

        self.get_ends_and_branches()
        self.create_graph_and_nodes()
        self.fill_edges()

        self.combine_near_nodes_eculid()
        self.combine_near_nodes_length()
        self.remove_zero_length_edges()
        self.remove_self_loops()

        self.remove_small_subgraphs()

        for n in self.G.nodes():
            if len(self.G.edges(n)) > 1:
                self.total_branch_points += 1
            elif len(self.G.edges(n)) == 1:
                self.total_ends += 1
            else:
                print(n, self.G.edges(n))

        self.get_tots_and_hist()

        self.connectivity = []
        all_connectivity = nx.all_pairs_node_connectivity(self.G)
        for n1 in all_connectivity:
            for n2 in all_connectivity[n1]:
                if all_connectivity[n1][n2] > 0:
                    self.connectivity.append(all_connectivity[n1][n2])

        self.get_pos_dict()

        save_path = ''
        if save_files:
            if not os.path.isdir(output_dir + 'networks/'):
                os.mkdir(output_dir + 'networks/')
            if not os.path.isdir(output_dir + 'networks/' + name + '/'):
                os.mkdir(output_dir + 'networks/' + name + '/')
            save_path = output_dir + 'networks/' + name + '/'

            nx.write_gpickle(self.G, save_path + 'Graph.gpickle')

        if plot or save_files:
            self.show_graph(with_pos=self.pos,
                            with_background=img_enhanced,
                            with_skel=morphology.dilation(self.img_skel),
                            save=save_files,
                            save_path=save_path + 'with_background.png',
                            show=plot)
            self.show_graph(save=save_files,
                            save_path=save_path + 'without_background.png',
                            show=plot)
예제 #38
0
#    x = pickle.load(f).astype(numpy.float32)

# with open('tfidf_all_pred3_0509.pkl', 'rb') as f:
#    x = pickle.load(f).astype(numpy.float32)
# with open('tfidf_all_pred2_0506.pkl', 'rb') as f:
#    x = pickle.load(f).astype(numpy.float32)
with open('tfidf_all_pred2_0512.pkl', 'rb') as f:
    x = pickle.load(f).astype(numpy.float32)
df['pred'] = x

avg_pos = df[df['is_duplicate'] == 1]['pred'].mean()
avg_neg = df[df['is_duplicate'] == 0]['pred'].mean()

import networkx as nx
numpy.random.seed(111)
G = nx.Graph()

edges = [tuple(x) for x in df[['question1', 'question2', 'pred']].values]
G.add_weighted_edges_from(edges)

map_dist = nx.all_pairs_node_connectivity(G)

list_dist = []
for q1, q2 in tqdm(df[['question1', 'question2']].values):
    list_dist.append(map_dist[q1][q2])

list_dist = numpy.array(list_dist)

with open('train_dist.pkl', 'wb') as f:
    pickle.dump(list_dist, f, -1)