def calculate_WL_subtree_kernel(self, igraph_list):
     K_wl = gk.CalculateWLKernel(igraph_list, par=50)
     print(K_wl.shape)
     print(K_wl)
     plt.imshow(K_wl, cmap='gist_ncar', interpolation='none')
     plt.show()
     return K_wl
def compute_wl_kernel(mols, params):
    """
    Compute edge hist kernel
    Arguments:
            mols {list[Molecule]} -- [description]
    """
    par = int(params["int_par"])
    mol_graphs_list = [mol2graph_igraph(m) for m in mols]
    return gk.CalculateWLKernel(mol_graphs_list, par=par)
def compute_kernel_distance_matrices(slice_subgraphs, kernel_params):
    # Relabel based on requested graph kernels
    kernel_label_pair_to_relabeled_graphs = get_relabeled_graphs(
        slice_subgraphs, kernel_params)

    # Actually compute the kernel distance matrices
    kernel_to_distance_matrix = {}
    for kp in kernel_params:
        kernel = kp["name"]
        params = kp["params"]
        # Compute Weisfeiler-Lehman subtree pattern kernel
        if kernel == "wlst":
            n_iters = params["n_iters"]
            label = params["label"]
            kernel_label_pair = (kernel, label)
            relabeled_graphs = kernel_label_pair_to_relabeled_graphs[
                kernel_label_pair]
            kernel_mat = gk.CalculateWLKernel(relabeled_graphs, n_iters)
            distance_mat = convert_to_distance_matrix(kernel_mat)
            kernel_params_key = (kernel, label, n_iters)
            kernel_to_distance_matrix[kernel_params_key] = distance_mat
        # Compute edge-histogram kernel
        elif kernel == "eh":
            label = params["label"]
            kernel_label_pair = (kernel, label)
            relabeled_graphs = kernel_label_pair_to_relabeled_graphs[
                kernel_label_pair]
            kernel_mat = gk.CalculateEdgeHistKernel(relabeled_graphs)
            distance_mat = convert_to_distance_matrix(kernel_mat)
            kernel_key = (kernel, label)
            kernel_to_distance_matrix[kernel_key] = distance_mat
        # Compute vertex-histogram kernel
        elif kernel == "vh":
            label = params["label"]
            key = (kernel, label)
            relabeled_graphs = kernel_label_pair_to_relabeled_graphs[key]
            kernel_mat = gk.CalculateVertexHistKernel(relabeled_graphs)
            distance_mat = convert_to_distance_matrix(kernel_mat)
            kernel_to_distance_matrix[key] = distance_mat
        else:
            raise NotImplementedError(
                "Kernel: {} not supported".format(kernel))
    return kernel_to_distance_matrix
Beispiel #4
0
def compute_all_kernels(graphs):
    return (
        gk.CalculateEdgeHistKernel(graphs),
        gk.CalculateVertexHistKernel(graphs),
        gk.CalculateVertexEdgeHistKernel(graphs),
        gk.CalculateVertexVertexEdgeHistKernel(graphs),
        gk.CalculateEdgeHistGaussKernel(graphs),
        gk.CalculateVertexHistGaussKernel(graphs),
        gk.CalculateVertexEdgeHistGaussKernel(graphs),
        gk.CalculateGeometricRandomWalkKernel(graphs),
        # gk.CalculateExponentialRandomWalkKernel(graphs),
        gk.CalculateKStepRandomWalkKernel(graphs, [1.0]),
        gk.CalculateWLKernel(graphs),
        gk.CalculateConnectedGraphletKernel(graphs, 3),
        gk.CalculateConnectedGraphletKernel(graphs, 4),
        gk.CalculateConnectedGraphletKernel(graphs, 5),
        gk.CalculateGraphletKernel(graphs, 3),
        gk.CalculateGraphletKernel(graphs, 4),
        gk.CalculateShortestPathKernel(graphs),
    )
 def calculate_kernel_matrices(self, igraph_list):
     kernel_matrices = []
     kernel_matrices.append(gk.CalculateEdgeHistKernel(igraph_list))
     kernel_matrices.append(gk.CalculateVertexHistKernel(igraph_list))
     kernel_matrices.append(
         gk.CalculateVertexVertexEdgeHistKernel(igraph_list))
     kernel_matrices.append(
         gk.CalculateVertexVertexEdgeHistKernel(igraph_list))
     kernel_matrices.append(gk.CalculateVertexHistGaussKernel(igraph_list))
     kernel_matrices.append(gk.CalculateEdgeHistGaussKernel(igraph_list))
     kernel_matrices.append(
         gk.CalculateVertexEdgeHistGaussKernel(igraph_list))
     #kernel_matrices.append(gk.CalculateGeometricRandomWalkKernel(igraph_list))
     #kernel_matrices.append(gk.CalculateExponentialRandomWalkKernel(igraph_list))
     #kernel_matrices.append(gk.CalculateKStepRandomWalkKernel(igraph_list))
     #kernel_matrices.append(gk.CalculateShortestPathKernel(igraph_list))
     kernel_matrices.append(gk.CalculateWLKernel(igraph_list))
     #kernel_matrices.append(gk.CalculateGraphletKernel(igraph_list))
     #kernel_matrices.append(gk.CalculateConnectedGraphletKernel(igraph_list))
     return kernel_matrices
Beispiel #6
0
def evaluate_wlst_kernel(graphs,
                         graph_labels,
                         wl_iter_range,
                         label_requests,
                         n_folds=10,
                         seed=None):
    """
    """

    # Print progress
    print("Evaluating Weisfeiler-Lehman Subtree-Pattern Kernel")
    print()

    # Just a few sanity checks
    assert (len(graphs) == len(graph_labels))

    # Mapping from vertex labeling to results
    vertex_labeling_to_results = {}

    # Sweep over vertex label options
    for lr in label_requests:

        # Unpack
        requested_vertex_label = lr["vertex"]

        # Print progress
        print("Vertex Label: {}".format(requested_vertex_label))
        print()

        # Convert the base graphs into representations with
        # the requested vertex and edge labels, if any.
        relabeled_graphs = [
            relabel_for_wlst_kernel(g, label=requested_vertex_label)
            for g in graphs
        ]

        # Store results for this vertex labeling
        n_iters_to_results = {}

        # Sweep over WL-iteration range
        for n_wl_iters in wl_iter_range:

            # Print progress
            print("# WL-Iterations: {}".format(n_wl_iters))
            print()

            # Compute kernel matrix
            k_mat = gk.CalculateWLKernel(relabeled_graphs, n_wl_iters)

            # Define lists to track non-determinism fraction prediction results
            # over multiple folds
            true_nd_vals = []
            pred_nd_vals = []

            # Define training and testing sets
            graph_indices = list(range(len(graph_labels)))
            kf = KFold(n_splits=n_folds, random_state=seed, shuffle=True)
            for split_idx, (train_indices, test_indices) in enumerate(
                    kf.split(graph_indices)):

                # Print progress
                print("Running split {}/{}".format(split_idx + 1, n_folds))

                # Get training and testing graphs
                g_train = [relabeled_graphs[i] for i in train_indices]
                g_test = [relabeled_graphs[i] for i in test_indices]

                # Get the non-determinism fraction values for the training and
                # testing graphs
                y_train = [graph_labels[i] for i in train_indices]
                y_test = [graph_labels[i] for i in test_indices]

                # Retrieve embeddings of training and test graphs
                k_train = np.zeros((len(train_indices), len(train_indices)))
                k_test = np.zeros((len(test_indices), len(train_indices)))
                for i in range(len(train_indices)):
                    for j in range(len(train_indices)):
                        k_train[i][j] = k_mat[train_indices[i]][
                            train_indices[j]]
                for i in range(len(test_indices)):
                    for j in range(len(train_indices)):
                        k_test[i][j] = k_mat[test_indices[i]][train_indices[j]]

                # Train SVM regressor using precomputed kernel matrix
                model = svm.SVR("precomputed").fit(k_train, y_train)
                model.fit(k_train, y_train)

                # Evaluate model against the embeddedings of the test graphs
                y_pred = model.predict(k_test)

                # Print progress
                print("Done with split {}/{}".format(split_idx + 1, n_folds))
                print()

                # Aggregate results for this fold
                true_nd_vals += list(y_test)
                pred_nd_vals += list(y_pred)

            # Aggregate results for this # iterations
            n_iters_to_results[n_wl_iters] = {
                "true": true_nd_vals,
                "pred": pred_nd_vals
            }

        # Aggregate results for this vertex labeling
        vertex_labeling_to_results[requested_vertex_label] = n_iters_to_results

    return vertex_labeling_to_results
    catagrateryGraphList = []
    for i in range(len(DataSet)):  # 10类数据
        pointsGraphList = []
        pointsGraphList = GetGraphList(DataSet[i])  #获得单类的图列表
        catagrateryGraphList.append(pointsGraphList)
    print catagrateryGraphList

    sum_Graphlist = []
    for i in range(len(catagrateryGraphList)):
        for j in range(len(catagrateryGraphList[i])):
            sum_Graphlist.append(catagrateryGraphList[i][j])

    mutag_list = np.array(sum_Graphlist)
    ### ALL KERNELS COMPUTE
    K1 = gk.CalculateEdgeHistKernel(mutag_list)
    K2 = gk.CalculateVertexHistKernel(mutag_list)
    K3 = gk.CalculateVertexEdgeHistKernel(mutag_list)
    K4 = gk.CalculateVertexVertexEdgeHistKernel(mutag_list)
    K5 = gk.CalculateEdgeHistGaussKernel(mutag_list)
    K6 = gk.CalculateVertexHistGaussKernel(mutag_list)
    K7 = gk.CalculateVertexEdgeHistGaussKernel(mutag_list)
    # K8 = gk.CalculateGeometricRandomWalkKernel(mutag_list)
    # K9 = gk.CalculateExponentialRandomWalkKernel(mutag_list)
    K10 = gk.CalculateKStepRandomWalkKernel(mutag_list)
    K11 = gk.CalculateWLKernel(mutag_list)
    K12 = gk.CalculateConnectedGraphletKernel(mutag_list, 4)
    K13 = gk.CalculateGraphletKernel(mutag_list, 4)
    # K14 = gk.CalculateShortestPathKernel(mutag_list)
    #获得10类,50种图,计算相识度
    pass
Beispiel #8
0
    years.sort()
    year_tup = list(zip(years[0:], years[1:]))

    for yr1, yr2 in year_tup:
        yr1_graphs, yr1_labels = get_graphs(
            yr1, os.path.join(root_folder, str(yr1), 'communities_pajek'),
            master_map)
        yr2_graphs, yr2_labels = get_graphs(
            yr2, os.path.join(root_folder, str(yr2), 'communities_pajek'),
            master_map)

        graph_list = []
        graph_list.extend(yr1_graphs)
        graph_list.extend(yr2_graphs)

        K_WL = gk.CalculateWLKernel(graph_list, par=3)
        K_WL_subset = K_WL[:len(yr1_graphs), len(yr1_graphs):]
        joblib.dump(K_WL_subset,
                    '{0}/WL-{1}-{2}.pkl'.format(outpath, yr1, yr2),
                    compress=1)
        create_kernel_csv(K_WL_subset, yr1_labels, yr2_labels,
                          '{0}/WL-{1}-{2}.csv'.format(outpath, yr1, yr2))

        # multiplying penalising coeff to block small communities getting merged with very large communities.
        row_len, col_len = K_WL_subset.shape
        yr1_graph_sizes = [g.vcount() for g in yr1_graphs]
        yr2_graph_sizes = [g.vcount() for g in yr2_graphs]

        #penalize_coeff = np.array([x/float(y) for x,y in list(itertools.product(yr1_graph_sizes,yr2_graph_sizes))]).reshape(row_len, col_len)
        # 1/abs(x-y)+1
        penalize_coeff = np.array([