Pytorch-Geometric

aneeshaasc: <p>I’m new to Pytorch-geometric, and geometric deep learning. I’m trying to visualize the datasets available in pytorch-geometric, but couldn’t find anything to do so.</p>
<p>How do I visualize these graph datasets. Is there provision to do so in pytorch-geometric? How else can we visualize this ?</p>

aneeshaasc: <pre><code class="lang-auto">from torch_geometric.datasets import KarateClub
dataset = KarateClub()

for i in dataset[0]:
  print(i)
# this torch.geometric.datasets object comprises of edge(edge information for each node), x(nodes) and y(labels for each node)

edge,x,y = dataset[0]
numpyx = x[1].numpy()
numpyy = y[1].numpy()
numpyedge = edge[1].numpy()

import networkx as nx

g = nx.Graph(numpyx)

name,edgeinfo = edge

src = edgeinfo[0].numpy()
dst = edgeinfo[1].numpy()
edgelist = zip(src,dst)

for i,j in edgelist:
  g.add_edge(i,j) 

nx.draw_networkx(g)

</code></pre>

Nikronic: <aside class="quote no-group" data-username="aneeshaasc" data-post="2" data-topic="44994">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/a/d9b06d/48.png" class="avatar"> aneeshaasc:</div>
<blockquote>
<p>nx.Graph(numpyx)</p>
</blockquote>
</aside>
<p>Hi,<br>
What is <code>nx</code> here? Is it <code>networkx</code> library?</p>

aneeshaasc: <p>Yes. My apologies. I’ve now modified the code to include the import line.</p>

Nikronic: <p>No problem, Good luck.</p>

aneeshaasc: <p>Actually… there’s an even better way. PyG has something in-built to convert the graph datasets to a networkx graph.</p>
<pre><code class="lang-auto">import networkx as nx
import torch
import numpy as np
import pandas as pd
from torch_geometric.datasets import Planetoid
from torch_geometric.utils.convert import to_networkx

dataset1 = Planetoid(root = '/content/cora',name='Cora')

cora = dataset1 [0]

coragraph = to_networkx(cora)

node_labels = cora.y[list(coragraph.nodes)].numpy()

import matplotlib.pyplot as plt
plt.figure(1,figsize=(14,12)) 
nx.draw(coragraph, cmap=plt.get_cmap('Set1'),node_color = node_labels,node_size=75,linewidths=6)
plt.show()

</code></pre>

Juan_Espinosa_Reinos: <p>Hello all! I am also new in PyG… I am trying to import the dataset using Colab but following your instructions outputs an error: module ‘community’ has no attribute ‘best_partition’. Do you know why this could be happening? Thanks for the help!!</p>
<p>from torch_geometric.datasets import KarateClub</p>
<p>dataset = KarateClub()<br>
AttributeError: module ‘community’ has no attribute ‘best_partition’</p>

Juan_Espinosa_Reinos: <p>For future issues, python-louvain already installs networkx package. Therefore, if the previous error appears uninstall both libraries and install back only python-louvain.</p>

Brando_Miranda: <p>this is impler:</p>
<aside class="onebox stackexchange" data-onebox-src="https://stackoverflow.com/questions/61274847/how-to-visualize-a-torch-geometric-graph-in-python">
  <header class="source">

      <a href="https://stackoverflow.com/questions/61274847/how-to-visualize-a-torch-geometric-graph-in-python" target="_blank" rel="noopener nofollow ugc">stackoverflow.com</a>
  </header>

  <article class="onebox-body">
      <a href="https://stackoverflow.com/users/12809299/white" target="_blank" rel="noopener nofollow ugc">
    <img alt="White" src="https://www.gravatar.com/avatar/db815680cf3575306e0ce640c8506274?s=128&amp;d=identicon&amp;r=PG&amp;f=1" class="thumbnail onebox-avatar" width="128" height="128">
  </a>

<h4>
  <a href="https://stackoverflow.com/questions/61274847/how-to-visualize-a-torch-geometric-graph-in-python" target="_blank" rel="noopener nofollow ugc">How to visualize a torch_geometric graph in Python?</a>
</h4>

<div class="tags">
  <strong>graph, pytorch, draw</strong>
</div>

<div class="date">
  asked by
  
  <a href="https://stackoverflow.com/users/12809299/white" target="_blank" rel="noopener nofollow ugc">
    White
  </a>
  on <a href="https://stackoverflow.com/questions/61274847/how-to-visualize-a-torch-geometric-graph-in-python" target="_blank" rel="noopener nofollow ugc">03:09PM - 17 Apr 20 UTC</a>
</div>

  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>

<pre><code class="lang-auto">import networkx as nx

edge_index = torch.tensor([[0, 1, 1, 2],
                           [1, 0, 2, 1]], dtype=torch.long)
x = torch.tensor([[-1], [0], [1]], dtype=torch.float)

data = torch_geometric.data.Data(x=x, edge_index=edge_index)
g = torch_geometric.utils.to_networkx(data, to_undirected=True)
nx.draw(g)
</code></pre>
<p>you need matplot lib installed too do <code>conda install -y matplotlib</code>.</p>

zyxcab123: <p>I tried with enzyme datset in the same way but i didnt get and i tried in this way and it is giving error: please help:</p>
<pre><code class="lang-auto">
def convert_to_networkx(graph, n_sample=None):
   
    g = to_networkx(graph, node_attrs=["x"])
    y = graph.y.numpy()
    if n_sample is not None:
        if n_sample &gt;= len(g.nodes):
            # If n_sample is larger or equal to the number of nodes, use all nodes
            sampled_nodes = list(g.nodes)
        else:
            # Sample n_sample nodes from the graph
            sampled_nodes = random.sample(list(g.nodes), n_sample)

        g = g.subgraph(sampled_nodes)
        y = y[sampled_nodes]

    return g, y


def plot_graph(g, y):

    plt.figure(figsize=(9, 7))
    nx.draw_spring(g, node_size=30, arrows=False, node_color=y)
    plt.show() 
</code></pre>
<pre><code class="lang-auto">from torch_geometric.datasets import TUDataset
import random

# Load the Enzymes dataset
dataset = TUDataset(root='/path/to/your/dataset', name='ENZYMES')

# Check the total number of graphs in the dataset
num_graphs_in_dataset = len(dataset)

# Define the number of graphs you want to visualize (e.g., 5 random graphs)
num_graphs_to_visualize = 5

# Ensure you don't sample more graphs than are available in the dataset
if num_graphs_to_visualize &gt; num_graphs_in_dataset:
    num_graphs_to_visualize = num_graphs_in_dataset

# Create a list of unique indices to select random graphs
random_graph_indices = random.sample(range(num_graphs_in_dataset), num_graphs_to_visualize)

# Iterate through the selected graphs
for i in random_graph_indices:
    g, y = convert_to_networkx(dataset[i], n_sample=1000)  # You can adjust n_sample as needed
    plot_graph(g, y)

</code></pre>
<p>I am getting error:</p>
<pre><code class="lang-auto">Untitled-2.ipynb Cell 4 line 2
     22 for i in random_graph_indices:
     23     print("i=",i )
---&gt; 24     g, y = convert_to_networkx(dataset[i], n_sample=1000)  # You can adjust n_sample as needed
     25     plot_graph(g, y)

Untitled-2.ipynb Cell 4 line 4
     37         sampled_nodes = random.sample(list(g.nodes), n_sample)
     39     g = g.subgraph(sampled_nodes)
---&gt; 40     y = y[sampled_nodes]
     42 return g, y

IndexError: index 1 is out of bounds for axis 0 with size 1
</code></pre>
<p>Please help me !! thanks in advance</p>