Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ke function to detect edge labels in subgraph_matching kernel not working. #117

Open
georgia-max opened this issue Nov 12, 2024 · 0 comments

Comments

@georgia-max
Copy link

Describe the bug
I am trying to get the similarity scores between the two graphs using the SubgraphMatching Kernel, that take into account both node labels and edge labels : SubgraphMatching(normalize=True, ke=custom_edge_kernel, kv = custom_label_kernel)

This should result in a score that is not equal to 1 since one of the edge_labels is different. However, I am getting a score of 1.

To Reproduce

from grakel import Graph
from grakel.kernels import SubgraphMatching

# Define a custom edge kernel function
def custom_edge_kernel(label1, label2):
    # Example: Assign partial similarity for mismatched labels
    print(label1, label2)
    if label1 == label2:
        return 1  # Perfect match
    else:
        return 0.5  # Partial similarity for mismatched labels
def custom_label_kernel(label1, label2):
    # Example: Assign partial similarity for mismatched labels
    # print(label1, label2)
    if label1 == label2:
        return 1  # Perfect match
    else:
        return 0.5  # Partial similarity for mismatched labels

# Define two graphs with labeled edges and nodes
graph1 = {
    "edge": {(0, 1):1, (1, 2):1},
    # "edge": {0:{1:1}, 1:{2:1}},
    "node_labels": {0: 'A', 1: 'B', 2: 'A'},
    "edge_labels": {(0, 1): 'X', (1, 2): 'Z'}  # Edge labels
}

graph2 = {
    "edge": {(0, 1):1, (1, 2):1},
    # "edge": {0:{1:1}, 1:{2:1}},

    "node_labels": {0: 'A', 1: 'B', 2: 'A'},
    "edge_labels": {(0, 1): 'X', (1, 2): 'Y'}  # Note: Different edge label 'Z'
}

# Create Graph objects
G1 = Graph(
    initialization_object = graph1["edge_labels"], 
    node_labels=graph1["node_labels"], 
    edge_labels=graph1["edge_labels"], 
    graph_format="all"
)
G2 = Graph(
    initialization_object = graph2["edge_labels"], 
    node_labels=graph2["node_labels"], 
    edge_labels=graph2["edge_labels"], 
    graph_format="all"
)


# Initialize Subgraph Matching Kernel with custom edge kernel
sm_kernel = SubgraphMatching(normalize=True, ke=custom_edge_kernel, kv = custom_label_kernel)

# Compute the kernel matrix
K = sm_kernel.fit_transform([G1, G2])

# Output the kernel matrix
print("Kernel Matrix:")
print(K)

my result running the code is:

Kernel Matrix: [[1. 1.] [1. 1.]]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant