Skip to main content

Visual exploration and explanation of Graph Neural Networks in computational notebooks

Project description

GNN Explorer: Visual Exploration and Explanation of Graph Neural Networks

A comprehensive visualization library for Graph Neural Networks (GNNs) that enables interactive exploration and explanation of GNN models directly in computational notebooks. This library provides multiple visualization widgets for understanding graph structures, model architectures, and intermediate feature representations.

Python Package Index

We have published the public version to Python Package Index, use

pip install gnn-exp

to install the package.

Features

🎨 GraphVisualizer - Dual View Graph Visualization

  • Visualize graphs with both node-link and matrix views
  • Support for large graphs with subgraph extraction
  • Hoop-based subgraph sampling for focused visualization
  • Interactive exploration of graph structures

✏️ GraphEditor - Interactive Graph Editing

  • Edit graph structures directly in the notebook
  • Add, remove, and modify nodes and edges
  • Export edited graphs to JSON format
  • Real-time visualization updates

🧠 GNNVisualizer - Model Feature Visualization

  • Visualize intermediate layer features and activations
  • Matrix view of feature transformations across layers
  • Support for multiple GNN tasks:
    • Node Classification: Visualize node-level predictions
    • Edge Prediction: Visualize link prediction tasks
    • Graph Classification: Visualize graph-level tasks
  • Interactive layer expansion to explore inner computations
  • Subgraph-based feature visualization for large graphs

Installation

From Source

Clone the repository and install in development mode:

git clone https://github.com/HarryLuUMN/gnn-exp.git
cd gnn-exp

Python Dependencies

We recommend using uv for development, which automatically manages virtual environments and dependencies:

# Install Python dependencies
uv sync

# Run example notebook
uv run jupyter lab example.ipynb

Alternatively, use a traditional virtual environment:

python -m venv .venv
source .venv/bin/activate  # On Windows: .venv\Scripts\activate
pip install -e ".[dev]"
jupyter lab example.ipynb

JavaScript Dependencies

The widget front-end code bundles JavaScript dependencies. After setting up Python, install JavaScript dependencies:

npm install

For development with hot-reloading, run in a separate terminal:

npm run dev

This will automatically rebuild JavaScript as you make changes to files in js/.

Quick Start

Visualizing Graphs

from gnn_explorer import GraphVisualizer

# Create a visualizer instance
w = GraphVisualizer()
w.add_data(dataFile="test_data/karate_dataset.json")
w  # Display in notebook

Visualizing Large Graphs with Subgraphs

# Single hub node subgraph extraction
w = GraphVisualizer()
w.add_data(dataFile="test_data/twitch.json")
w.subgraph_hoop_visualizer(hubNode=0, hoopNum=3)
w

# Multiple hub nodes
w = GraphVisualizer()
w.add_data(dataFile="test_data/twitch.json")
w.multiple_subgraph_hoop_visualizer(hubNodes=[0, 1], hoopNum=1)
w

Editing Graphs

from gnn_explorer import GraphEditor

editor = GraphEditor()
editor.add_data(dataFile="test_data/karate_dataset.json")
editor  # Display editor in notebook

# Export edited graph
editor.export_data_to_json("test_data/edited_graph.json")

Visualizing GNN Models

import torch
import torch.nn as nn
from torch_geometric.nn import GCNConv
from gnn_explorer import GNNVisualizer

# Define your GNN model
class GCN(torch.nn.Module):
    def __init__(self, dataset):
        super().__init__()
        self.conv1 = GCNConv(dataset.num_features, 4)
        self.act1 = nn.Tanh()
        self.conv2 = GCNConv(4, 4)
        self.act2 = nn.Tanh()
        self.classifier = nn.Linear(4, dataset.num_classes)
        self.softmax = nn.Softmax(dim=1)
    
    def forward(self, x, edge_index):
        h = self.act1(self.conv1(x, edge_index))
        h = self.act2(self.conv2(h, edge_index))
        out = self.softmax(self.classifier(h))
        return out

# Create model and data
model = GCN(dataset)
data = dataset[0]

# Visualize model
visualizer = GNNVisualizer()
visualizer.add_model(
    data=data,
    model=model,
    subgraphSample=False,
    forward_fn=None,
    queries=[[12, 18]],  # Node IDs to highlight
    mode='node'  # 'node', 'edge', or 'graph'
)
visualizer  # Display in notebook

Project Structure

gnn-explorer/
├── src/gnn_explorer/          # Python package
│   ├── graph_visualizer.py    # Graph visualization widget
│   ├── graph_editor.py        # Graph editing widget
│   ├── gnn_visualizer.py      # GNN model visualization widget
│   └── static/                # Compiled JavaScript/CSS assets
├── js/                         # TypeScript/React source code
│   ├── graph_visualizer/      # Graph visualization frontend
│   ├── graph_editor/          # Graph editor frontend
│   └── gnn_visualizer/        # GNN visualizer frontend
├── test_data/                 # Example datasets
├── example.ipynb              # Example notebook
└── README.md                  # This file

Development

Building JavaScript

The JavaScript code is built using esbuild. To build once:

npm run build

To watch for changes during development:

npm run dev

Type Checking

Run TypeScript type checking:

npm run typecheck

Hot Module Reloading

For development, enable hot module reloading in your notebook:

%load_ext autoreload
%autoreload 2
%env ANYWIDGET_HMR=1

Changes made to files in js/ will be automatically reflected in the notebook when using npm run dev.

Requirements

  • Python >= 3.8
  • Node.js >= 16 (for development)
  • JupyterLab or Jupyter Notebook
  • PyTorch Geometric (for GNN model visualization)

Supported GNN Layers

  • GCNConv: Graph Convolutional Network layers
  • Linear: Fully connected layers
  • Activation Functions: ReLU, Tanh, Sigmoid, Softmax, and more

License

MIT License - see LICENSE file for details.

Authors

Repository

Citation

If you use this library in your research, please cite:

@software{gnn_explorer,
  title = {GNN Explorer: Visual Exploration and Explanation of Graph Neural Networks},
  author = {Lu, Yilin and Wang, Qianwen},
  url = {https://pypi.org/project/gnn-exp/0.1.0/},
  journal = {Python Package Index}
  year = {2024}
}

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

gnn_exp-0.1.1.tar.gz (261.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

gnn_exp-0.1.1-py3-none-any.whl (261.6 kB view details)

Uploaded Python 3

File details

Details for the file gnn_exp-0.1.1.tar.gz.

File metadata

  • Download URL: gnn_exp-0.1.1.tar.gz
  • Upload date:
  • Size: 261.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.11

File hashes

Hashes for gnn_exp-0.1.1.tar.gz
Algorithm Hash digest
SHA256 e50169f6ac85e707d14ed044bed84c8b19c2a13ef8ecd3cd4b564b818e14f6da
MD5 de9fa0796010d3ac2e6f7512e084ced8
BLAKE2b-256 9674801842e997abd5db63f0bb2045f34acb9ca503b332dc3ed09b2812276ab7

See more details on using hashes here.

File details

Details for the file gnn_exp-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: gnn_exp-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 261.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.11

File hashes

Hashes for gnn_exp-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 e42eb4ecae424978e4eae6277c4bcae541ae0b732d47349e5184608fad36bf9e
MD5 8496114894c7d2d199bbabe33ff6a4e6
BLAKE2b-256 7f59a1da3890f5bfe5d48456646580d8ba2893ced0c6a03878757c695e26b5c7

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page