-
Notifications
You must be signed in to change notification settings - Fork 49
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
Path lifting (Graph to Hypergraph) #52
Open
PierrickLeroy
wants to merge
9
commits into
pyt-team:main
Choose a base branch
from
PierrickLeroy:path_lifting
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
f060bbf
created path lifting files post precommit
PierrickLeroy 11df3be
added tuto notebook
PierrickLeroy df87a70
added tests
PierrickLeroy 5676e4e
fix bug in plot_manual_graph
PierrickLeroy cb007ef
refactoring for ruff check
PierrickLeroy b26701f
added default behavior of path lifting
PierrickLeroy 4754e6c
refactor for ruff check
PierrickLeroy 5410453
improved code readability
PierrickLeroy a45514b
Merge branch 'path_feature_lifting' into path_lifting
PierrickLeroy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
3 changes: 3 additions & 0 deletions
3
configs/transforms/liftings/graph2hypergraph/path_lifting.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
transform_type: 'lifting' | ||
transform_name: "PathLifting" | ||
feature_lifting: ProjectionSum |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
136 changes: 136 additions & 0 deletions
136
modules/transforms/liftings/graph2hypergraph/path_lifting.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
"""A module for the PathLifting class.""" | ||
import networkx as nx | ||
import numpy as np | ||
import torch | ||
import torch_geometric | ||
|
||
from modules.transforms.liftings.graph2hypergraph.base import Graph2HypergraphLifting | ||
|
||
|
||
class PathLifting(Graph2HypergraphLifting): | ||
"""Lifts graphs to hypergraph domain by considering paths between nodes.""" | ||
|
||
def __init__( | ||
self, | ||
source_nodes: list[int], | ||
target_nodes: list[int], | ||
lengths: list[int], | ||
include_smaller_paths=False, | ||
**kwargs, | ||
): | ||
# guard clauses | ||
if len(source_nodes) != len(lengths): | ||
raise ValueError("source_nodes and lengths must have the same length") | ||
if target_nodes is not None and len(target_nodes) != len(source_nodes): | ||
raise ValueError( | ||
"When target_nodes is not None, it must have the same length" | ||
"as source_nodes" | ||
) | ||
if len(source_nodes) == 0: | ||
raise ValueError( | ||
"source_nodes,target_nodes and lengths must have at least one element" | ||
) | ||
|
||
super().__init__(**kwargs) | ||
self.source_nodes = source_nodes | ||
self.target_nodes = target_nodes | ||
self.lengths = lengths | ||
self.include_smaller_paths = include_smaller_paths | ||
|
||
def find_hyperedges(self, data: torch_geometric.data.Data): | ||
"""Finds hyperedges from paths between nodes in a graph.""" | ||
G = torch_geometric.utils.convert.to_networkx(data, to_undirected=True) | ||
s_hyperedges = set() | ||
|
||
if self.target_nodes is None: # all paths stemming from source nodes only | ||
for source, length in zip(self.source_nodes, self.lengths, strict=True): | ||
D, d_id2label, l_leafs = self.build_stemmingTree(G, source, length) | ||
s = self.extract_hyperedgesFromStemmingTree(D, d_id2label, l_leafs) | ||
s_hyperedges = s_hyperedges.union(s) | ||
|
||
else: # paths from source_nodes to target_nodes or from source nodes only | ||
for source, target, length in zip( | ||
self.source_nodes, self.target_nodes, self.lengths, strict=True | ||
): | ||
if target is None: | ||
D, d_id2label, l_leafs = self.build_stemmingTree(G, source, length) | ||
s = self.extract_hyperedgesFromStemmingTree(D, d_id2label, l_leafs) | ||
s_hyperedges = s_hyperedges.union(s) | ||
else: | ||
paths = list( | ||
nx.all_simple_paths( | ||
G, source=source, target=target, cutoff=length | ||
) | ||
) | ||
if not self.include_smaller_paths: | ||
paths = [path for path in paths if len(path) - 1 == length] | ||
s_hyperedges = s_hyperedges.union({frozenset(x) for x in paths}) | ||
return s_hyperedges | ||
|
||
def lift_topology(self, data: torch_geometric.data.Data): | ||
s_hyperedges = self.find_hyperedges(data) | ||
indices = [[], []] | ||
for edge_id, x in enumerate(s_hyperedges): | ||
indices[1].extend([edge_id] * len(x)) | ||
indices[0].extend(list(x)) | ||
incidence = torch.sparse_coo_tensor( | ||
indices, torch.ones(len(indices[0])), (len(data.x), len(s_hyperedges)) | ||
) | ||
return { | ||
"incidence_hyperedges": incidence, | ||
"num_hyperedges": len(s_hyperedges), | ||
"x_0": data.x, | ||
} | ||
|
||
def build_stemmingTree(self, G, source_root, length, verbose=False): | ||
"""Creates a directed tree from a source node with paths of a given length.""" | ||
d_id2label = {} | ||
stack = [] | ||
D = nx.DiGraph() | ||
n_id = 0 | ||
D.add_node(n_id) | ||
d_id2label[n_id] = source_root | ||
stack.append(n_id) | ||
n_id += 1 | ||
l_leafs = [] | ||
while len(stack) > 0: | ||
node = stack.pop() | ||
neighbors = list(G.neighbors(d_id2label[node])) | ||
visited_id = nx.shortest_path(D, source=0, target=node) | ||
visited_labels = [d_id2label[i] for i in visited_id] | ||
for neighbor in neighbors: | ||
if neighbor not in visited_labels: | ||
D.add_node(n_id) | ||
d_id2label[n_id] = neighbor | ||
if len(visited_labels) < length: | ||
stack.append(n_id) | ||
elif len(visited_labels) == length: | ||
l_leafs.append(n_id) | ||
else: | ||
raise ValueError("Visited labels length is greater than length") | ||
D.add_edge(node, n_id) | ||
n_id += 1 | ||
if verbose: | ||
print("\nLoop Variables Summary:") | ||
print("nodes:", node) | ||
print("neighbors:", neighbors) | ||
print("visited_id:", visited_id) | ||
print("visited_labels:", visited_labels) | ||
print("stack:", stack) | ||
print("id2label:", d_id2label) | ||
return D, d_id2label, l_leafs | ||
|
||
def extract_hyperedgesFromStemmingTree(self, D, d_id2label, l_leafs): | ||
"""From the root of the directed tree D, | ||
extract hyperedges from the paths to the leafs.""" | ||
a_paths = np.array( | ||
[list(map(d_id2label.get, nx.shortest_path(D, 0, x))) for x in l_leafs] | ||
) | ||
s_hyperedges = { | ||
(frozenset(x)) for x in a_paths | ||
} # set bc != paths can be same hpedge | ||
if self.include_smaller_paths: | ||
for i in range(a_paths.shape[1] - 1, 1, -1): | ||
a_paths = np.unique(a_paths[:, :i], axis=0) | ||
s_hyperedges = s_hyperedges.union({(frozenset(x)) for x in a_paths}) | ||
return s_hyperedges |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
156 changes: 156 additions & 0 deletions
156
test/transforms/liftings/graph2hypergraph/test_path_lifting.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
"""Test the path lifting module.""" | ||
|
||
import numpy as np | ||
|
||
from modules.data.load.loaders import GraphLoader | ||
from modules.transforms.liftings.graph2hypergraph.path_lifting import PathLifting | ||
from modules.utils.utils import load_dataset_config | ||
|
||
|
||
class TestHypergraphPathLifting: | ||
"""Test the PathLifting class.""" | ||
|
||
def setup_method(self): | ||
"""Initialise the PathLifting class.""" | ||
dataset_config = load_dataset_config("manual_dataset") | ||
loader = GraphLoader(dataset_config) | ||
self.dataset = loader.load() | ||
self.data = self.dataset._data | ||
|
||
def test_true(self): | ||
"""Naive test to check if the test is running.""" | ||
assert True | ||
|
||
# def test_false(self): | ||
# """Naive test to check if the test is running.""" | ||
# assert False | ||
|
||
def test_1(self): | ||
"""Verifies setup_method is working.""" | ||
assert self.dataset is not None | ||
|
||
def test_2(self): | ||
"""test: no target node for one source node returns something""" | ||
source_nodes = [0, 2] | ||
target_nodes = [1, None] | ||
lengths = [2, 2] | ||
include_smaller_paths = True | ||
path_lifting = PathLifting( | ||
source_nodes, | ||
target_nodes, | ||
lengths, | ||
include_smaller_paths=include_smaller_paths, | ||
) | ||
res = path_lifting.find_hyperedges(self.data) | ||
res_expected = [ | ||
[0, 1], | ||
[0, 1, 2], | ||
[0, 4, 1], | ||
[2, 4], | ||
[2, 1], | ||
[2, 0], | ||
[2, 7], | ||
[2, 5], | ||
[2, 3], | ||
[2, 1, 4], | ||
[2, 4, 0], | ||
[2, 1, 0], | ||
[2, 0, 7], | ||
[2, 5, 7], | ||
[2, 3, 6], | ||
[2, 5, 6], | ||
# [], | ||
] | ||
assert {frozenset(x) for x in res_expected} == res | ||
|
||
def test_3(self): | ||
"""test: include_smaller_paths=False""" | ||
source_nodes = [0] | ||
target_nodes = [1] | ||
lengths = [2] | ||
include_smaller_paths = False | ||
res = PathLifting( | ||
source_nodes, | ||
target_nodes, | ||
lengths, | ||
include_smaller_paths=include_smaller_paths, | ||
).find_hyperedges(self.data) | ||
assert frozenset({0, 1}) not in res | ||
|
||
def test_4(self): | ||
"""test: include_smaller_paths=True""" | ||
source_nodes = [0] | ||
target_nodes = [1] | ||
lengths = [2] | ||
include_smaller_paths = True | ||
res = PathLifting( | ||
source_nodes, | ||
target_nodes, | ||
lengths, | ||
include_smaller_paths=include_smaller_paths, | ||
).find_hyperedges(self.data) | ||
assert frozenset({0, 1}) in res | ||
|
||
def test_5(self): | ||
"""test: when include_smaller_paths=False all paths have the length specified""" | ||
source_nodes = [0] | ||
target_nodes = [1] | ||
include_smaller_paths = False | ||
for k in range(1, 5): | ||
lengths = [k] | ||
res = PathLifting( | ||
source_nodes, | ||
target_nodes, | ||
lengths, | ||
include_smaller_paths=include_smaller_paths, | ||
).find_hyperedges(self.data) | ||
assert np.array([len(x) - 1 == k for x in res]).all() | ||
|
||
def test_6(self): | ||
"""test: no target node global returns something""" | ||
source_nodes = [0, 1] | ||
target_nodes = None | ||
lengths = [2, 2] | ||
include_smaller_paths = False | ||
res = PathLifting( | ||
source_nodes, | ||
target_nodes, | ||
lengths, | ||
include_smaller_paths=include_smaller_paths, | ||
).find_hyperedges(self.data) | ||
assert len(res) > 0 | ||
|
||
def test_7(self): | ||
"""test: every hyperedge contains the source and target nodes when specified""" | ||
a = np.random.default_rng().choice( | ||
np.arange(len(self.data.x)), 2, replace=False | ||
) | ||
source_nodes = [a[0]] | ||
target_nodes = [a[1]] | ||
lengths = [np.random.default_rng().integers(1, 5)] | ||
include_smaller_paths = False | ||
res = PathLifting( | ||
source_nodes, | ||
target_nodes, | ||
lengths, | ||
include_smaller_paths=include_smaller_paths, | ||
).find_hyperedges(self.data) | ||
if len(res) > 0: | ||
assert ( | ||
np.array([source_nodes[0] in x for x in res]).all() | ||
and np.array([target_nodes[0] in x for x in res]).all() | ||
) | ||
|
||
def test_8(self): | ||
"""test: no target node for one source node returns something""" | ||
source_nodes = [0, 2] | ||
target_nodes = [1, None] | ||
lengths = [2, 2] | ||
include_smaller_paths = False | ||
res = PathLifting( | ||
source_nodes, | ||
target_nodes, | ||
lengths, | ||
include_smaller_paths=include_smaller_paths, | ||
).find_hyperedges(self.data) | ||
assert len(res) > 0 |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not directly part of the submission but it fixes what I believe to be a bug in a plotting function. This issue was created by another participant of the challenge.