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

Add assortativity to torch_geometric.utils #5587

Merged
merged 13 commits into from
Oct 5, 2022
Merged

Add assortativity to torch_geometric.utils #5587

merged 13 commits into from
Oct 5, 2022

Conversation

EdisonLeeeee
Copy link
Contributor

@EdisonLeeeee EdisonLeeeee commented Oct 1, 2022

This PR implements the computation of degree assortativity coefficient, as described in Mixing patterns in networks. Assortativity is a preference for a network's nodes to attach to others that are similar in some way. A larger assortativity indicates nodes tend to be connected with other nodes with similar degree values, and vice versa. Link to Slack Discussion

The implementation is based on networkx.degree_assortativity_coefficient but much faster than using networkx.

Test

  • Generate a graph (without duplicated edges)
import torch
from torch_geometric.utils import coalesce

torch.manual_seed(12345)
num_nodes = 10000
num_edges = 1000000
edge_index = torch.randint(0, num_nodes, size=(2, num_edges))
edge_index = coalesce(edge_index)
  • networkx.degree_assortativity_coefficient
import networkx as nx
nxg = nx.from_edgelist(edge_index.t().tolist(), create_using=nx.DiGraph)
t = time.perf_counter()
result = nx.degree_assortativity_coefficient(nxg)
print(f"result={result}, time={time.perf_counter()-t}")

>>> result=-0.0007873635931715391, time=0.48579016700000466
  • torch_geometric.utils.assortativity (On CPU)
from torch_geometric.utils import assortativity
t = time.perf_counter()
result = assortativity(edge_index)
print(f"result={result}, time={time.perf_counter()-t}")

>>> result=-0.0007878749747760594, time=0.07563883299985719
  • torch_geometric.utils.assortativity (On GPU)
>>> result=-0.0007857615128159523, time=0.0036659350153058767

The slight difference in results is caused by two different frameworks (Numpy and PyTorch).

torch_geometric/utils/assortativity.py Outdated Show resolved Hide resolved
torch_geometric/utils/assortativity.py Outdated Show resolved Hide resolved
torch_geometric/utils/assortativity.py Outdated Show resolved Hide resolved
@rusty1s rusty1s added the utils label Oct 1, 2022
test/utils/test_assortativity.py Outdated Show resolved Hide resolved
torch_geometric/utils/assortativity.py Outdated Show resolved Hide resolved
torch_geometric/utils/assortativity.py Show resolved Hide resolved
torch_geometric/utils/assortativity.py Show resolved Hide resolved
EdisonLeeeee and others added 5 commits October 2, 2022 12:13
Co-authored-by: Matthias Fey <matthias.fey@tu-dortmund.de>
Co-authored-by: Matthias Fey <matthias.fey@tu-dortmund.de>
Co-authored-by: Padarn Wilson <padarn.wilson@grabtaxi.com>
@EdisonLeeeee EdisonLeeeee requested a review from rusty1s October 5, 2022 02:11
Copy link
Member

@rusty1s rusty1s left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me. Please also add it to the CHANGELOG.md.

@codecov
Copy link

codecov bot commented Oct 5, 2022

Codecov Report

Merging #5587 (9e56273) into master (6ca2332) will increase coverage by 0.02%.
The diff coverage is 96.77%.

@@            Coverage Diff             @@
##           master    #5587      +/-   ##
==========================================
+ Coverage   83.85%   83.88%   +0.02%     
==========================================
  Files         348      349       +1     
  Lines       19169    19200      +31     
==========================================
+ Hits        16074    16105      +31     
  Misses       3095     3095              
Impacted Files Coverage Δ
torch_geometric/utils/assortativity.py 96.77% <96.77%> (ø)
torch_geometric/utils/coalesce.py 100.00% <0.00%> (+3.12%) ⬆️

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@rusty1s rusty1s merged commit 7029bad into master Oct 5, 2022
@rusty1s rusty1s deleted the assortativity branch October 5, 2022 06:26
JakubPietrakIntel pushed a commit to JakubPietrakIntel/pytorch_geometric that referenced this pull request Nov 25, 2022
* add assortativity

* test

* doc-string

* doc-string

* Update torch_geometric/utils/assortativity.py

Co-authored-by: Matthias Fey <matthias.fey@tu-dortmund.de>

* Update torch_geometric/utils/assortativity.py

Co-authored-by: Matthias Fey <matthias.fey@tu-dortmund.de>

* Update torch_geometric/utils/assortativity.py

Co-authored-by: Padarn Wilson <padarn.wilson@grabtaxi.com>

* update test

* doc-string

* fix test

* changelog

Co-authored-by: Matthias Fey <matthias.fey@tu-dortmund.de>
Co-authored-by: Padarn Wilson <padarn.wilson@grabtaxi.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants