Skip to content

Commit

Permalink
Added some new functions
Browse files Browse the repository at this point in the history
  • Loading branch information
lotzma committed Feb 12, 2025
1 parent 42c7e68 commit 51b2c59
Show file tree
Hide file tree
Showing 10 changed files with 940 additions and 1,448 deletions.
207 changes: 207 additions & 0 deletions examples/clustering.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%load_ext autoreload\n",
"%autoreload 2"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [],
"source": [
"import community as community_louvain\n",
"import collections\n",
"import numpy as np\n",
"import pandas as pd\n",
"import networkx as nx\n",
"import matplotlib.cm as cm\n",
"import matplotlib.pyplot as plt\n",
"import seaborn as sns\n",
"import os\n",
"import torch\n",
"from torch import Tensor\n",
"from torch_geometric.utils import to_networkx\n",
"from torch_geometric.datasets import Planetoid\n",
"device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\n",
"data_dir = \"./data\"\n",
"os.makedirs(data_dir, exist_ok=True)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"#l2g related imports\n",
"from l2gv2.clustering import distributed_clustering\n",
"from l2gv2.network.tgraph import TGraph\n",
"from l2gv2.network.npgraph import NPGraph\n",
"from l2gv2.patch.patches import create_patch_data\n",
"from l2gv2.patch.clustering import hierarchical_clustering\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# <font color=\"grey\"> Hierarchical construction of Patch Graphs</font>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### <a id='chapter1'> <font color=\"grey\">1. The local2global approach </font></a>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The initial step in creating the patch graph consists of clustering the graph. The clusters "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The Cora dataset is a well-known dataset in the field of graph research. This consists of 2708 scientific publications classified into one of seven classes. The citation network consists of 5429 links. Each publication in the dataset is described by a 0/1-valued word vector indicating the absence/presence of the corresponding word from the dictionary. The dictionary consists of 1433 unique words."
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Downloading https://github.com/kimiyoung/planetoid/raw/master/data/ind.cora.x\n",
"Downloading https://github.com/kimiyoung/planetoid/raw/master/data/ind.cora.tx\n",
"Downloading https://github.com/kimiyoung/planetoid/raw/master/data/ind.cora.allx\n",
"Downloading https://github.com/kimiyoung/planetoid/raw/master/data/ind.cora.y\n",
"Downloading https://github.com/kimiyoung/planetoid/raw/master/data/ind.cora.ty\n",
"Downloading https://github.com/kimiyoung/planetoid/raw/master/data/ind.cora.ally\n",
"Downloading https://github.com/kimiyoung/planetoid/raw/master/data/ind.cora.graph\n",
"Downloading https://github.com/kimiyoung/planetoid/raw/master/data/ind.cora.test.index\n",
"Processing...\n",
"Done!\n"
]
}
],
"source": [
"dataset = Planetoid(root=data_dir, name='Cora')\n",
"data = dataset[0]"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Number of nodes: 2708\n",
"Number of edges: 10556\n",
"Has isolated nodes: False\n",
"Has self-loops: False\n",
"Is undirected: True\n",
"Average node degree: 3.90\n"
]
}
],
"source": [
"print(f'Number of nodes: {data.num_nodes}')\n",
"print(f'Number of edges: {data.num_edges}')\n",
"print(f'Has isolated nodes: {data.has_isolated_nodes()}')\n",
"print(f'Has self-loops: {data.has_self_loops()}')\n",
"print(f'Is undirected: {data.is_undirected()}')\n",
"print(f'Average node degree: {data.num_edges / data.num_nodes:.2f}')"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"ename": "NameError",
"evalue": "name 'pd' is not defined",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn[16], line 3\u001b[0m\n\u001b[1;32m 1\u001b[0m G \u001b[38;5;241m=\u001b[39m to_networkx(data, to_undirected\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mTrue\u001b[39;00m)\n\u001b[1;32m 2\u001b[0m degrees \u001b[38;5;241m=\u001b[39m [val \u001b[38;5;28;01mfor\u001b[39;00m (node, val) \u001b[38;5;129;01min\u001b[39;00m G\u001b[38;5;241m.\u001b[39mdegree()]\n\u001b[0;32m----> 3\u001b[0m display(pd\u001b[38;5;241m.\u001b[39mDataFrame(pd\u001b[38;5;241m.\u001b[39mSeries(degrees)\u001b[38;5;241m.\u001b[39mdescribe())\u001b[38;5;241m.\u001b[39mtranspose()\u001b[38;5;241m.\u001b[39mround(\u001b[38;5;241m2\u001b[39m))\n\u001b[1;32m 4\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;28mlen\u001b[39m(degrees))\n\u001b[1;32m 5\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;28msum\u001b[39m(degrees))\n",
"\u001b[0;31mNameError\u001b[0m: name 'pd' is not defined"
]
}
],
"source": [
"G = to_networkx(data, to_undirected=True)\n",
"degrees = [val for (node, val) in G.degree()]\n",
"display(pd.DataFrame(pd.Series(degrees).describe()).transpose().round(2))\n",
"print(len(degrees))\n",
"print(sum(degrees))\n",
"plt.figure(figsize=(10, 6))\n",
"plt.hist(degrees, bins=50)\n",
"plt.xlabel(\"node degree\")\n",
"plt.show()"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(2, 10556)\n"
]
}
],
"source": [
"edge_index = data.edge_index.numpy()\n",
"print(edge_index.shape)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "base",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
33 changes: 28 additions & 5 deletions examples/nas.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 6,
"id": "c0f84710-fa07-4ae7-a37f-f933992a5183",
"metadata": {},
"outputs": [],
Expand Down Expand Up @@ -46,19 +46,42 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 9,
"id": "4958605c",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"[autoreload of l2gv2.embedding failed: Traceback (most recent call last):\n",
" File \"/opt/anaconda3/lib/python3.12/site-packages/IPython/extensions/autoreload.py\", line 276, in check\n",
" superreload(m, reload, self.old_objects)\n",
" File \"/opt/anaconda3/lib/python3.12/site-packages/IPython/extensions/autoreload.py\", line 475, in superreload\n",
" module = reload(module)\n",
" ^^^^^^^^^^^^^^\n",
" File \"/opt/anaconda3/lib/python3.12/importlib/__init__.py\", line 131, in reload\n",
" _bootstrap._exec(spec, module)\n",
" File \"<frozen importlib._bootstrap>\", line 866, in _exec\n",
" File \"<frozen importlib._bootstrap_external>\", line 995, in exec_module\n",
" File \"<frozen importlib._bootstrap>\", line 488, in _call_with_frames_removed\n",
" File \"/Users/u1774790/Projects/Graphs/code/L2Gv2/l2gv2/embedding/__init__.py\", line 2, in <module>\n",
" from .train import lr_grid_search, train\n",
" File \"/Users/u1774790/Projects/Graphs/code/L2Gv2/l2gv2/embedding/train.py\", line 27, in <module>\n",
" from ..utils import EarlyStopping\n",
"ImportError: cannot import name 'EarlyStopping' from 'l2gv2.utils' (/Users/u1774790/Projects/Graphs/code/L2Gv2/l2gv2/utils/__init__.py)\n",
"]\n"
]
},
{
"ename": "ImportError",
"evalue": "cannot import name 'VGAE' from 'l2gv2.embedding' (/Users/u1774790/Projects/Graphs/code/L2Gv2/l2gv2/embedding/__init__.py)",
"evalue": "cannot import name 'Patch' from 'l2gv2.patch' (/Users/u1774790/Projects/Graphs/code/L2Gv2/l2gv2/patch/__init__.py)",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mImportError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn[4], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01ml2gv2\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01membedding\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m VGAE, GAE, VGAE_loss, GAE_loss\n\u001b[1;32m 2\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01ml2gv2\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mutils\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m DataLoader\n\u001b[1;32m 3\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01ml2gv2\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mpatch\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m Patch, AlignmentProblem\n",
"\u001b[0;31mImportError\u001b[0m: cannot import name 'VGAE' from 'l2gv2.embedding' (/Users/u1774790/Projects/Graphs/code/L2Gv2/l2gv2/embedding/__init__.py)"
"Cell \u001b[0;32mIn[9], line 3\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01ml2gv2\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01membedding\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m VGAE, GAE, VGAE_loss, GAE_loss\n\u001b[1;32m 2\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01ml2gv2\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mutils\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m DataLoader\n\u001b[0;32m----> 3\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01ml2gv2\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mpatch\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m Patch, AlignmentProblem\n",
"\u001b[0;31mImportError\u001b[0m: cannot import name 'Patch' from 'l2gv2.patch' (/Users/u1774790/Projects/Graphs/code/L2Gv2/l2gv2/patch/__init__.py)"
]
}
],
Expand Down
25 changes: 0 additions & 25 deletions l2gv2/network/__init__.py

This file was deleted.

Loading

0 comments on commit 51b2c59

Please sign in to comment.