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

Adds Cycle Motif Support #6256

Merged
merged 5 commits into from
Dec 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).

## [2.3.0] - 2023-MM-DD
### Added
- Added `CycleMotif` motif generator to generate `n`-node cycle shaped motifs ([#6256](https://github.com/pyg-team/pytorch_geometric/pull/6256))
- Added the `InfectionDataset` to evaluate explanations ([#6222](https://github.com/pyg-team/pytorch_geometric/pull/6222))
- Added `characterization_score` and `fidelity_curve_auc` explainer metrics ([#6188](https://github.com/pyg-team/pytorch_geometric/pull/6188))
- Added `get_message_passing_embeddings` ([#6201](https://github.com/pyg-team/pytorch_geometric/pull/6201))
Expand Down
15 changes: 15 additions & 0 deletions test/datasets/motif_generator/test_cycle_motif.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from torch_geometric.datasets.motif_generator import CycleMotif


def test_cycle_motif():
motif_generator = CycleMotif(5)
assert str(motif_generator) == 'CycleMotif(5)'

motif = motif_generator()
assert len(motif) == 2
assert motif.num_nodes == 5
assert motif.num_edges == 10
assert motif.edge_index.tolist() == [
[0, 0, 1, 1, 2, 2, 3, 3, 4, 4],
[1, 4, 0, 2, 1, 3, 2, 4, 0, 3],
]
2 changes: 2 additions & 0 deletions torch_geometric/datasets/motif_generator/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from .base import MotifGenerator
from .custom import CustomMotif
from .house import HouseMotif
from .cycle import CycleMotif

__all__ = classes = [
'MotifGenerator',
'CustomMotif',
'HouseMotif',
'CycleMotif',
]
31 changes: 31 additions & 0 deletions torch_geometric/datasets/motif_generator/cycle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import torch

from torch_geometric.data import Data
from torch_geometric.datasets.motif_generator import CustomMotif


class CycleMotif(CustomMotif):
r"""Generates the cycle motif from the `"GNNExplainer:
Generating Explanations for Graph Neural Networks"
<https://arxiv.org/pdf/1903.03894.pdf>`_ paper.

Args:
num_nodes (int): The number of nodes in the cycle.
"""
def __init__(self, num_nodes: int):
self.num_nodes = num_nodes

row = torch.arange(num_nodes).view(-1, 1).repeat(1, 2).view(-1)
col1 = torch.arange(-1, num_nodes - 1) % num_nodes
col2 = torch.arange(1, num_nodes + 1) % num_nodes
col = torch.stack([col1, col2], dim=1).sort(dim=-1)[0].view(-1)

structure = Data(
num_nodes=num_nodes,
edge_index=torch.stack([row, col], dim=0),
)

super().__init__(structure)

def __repr__(self) -> str:
return f'{self.__class__.__name__}({self.num_nodes})'