-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
940 additions
and
1,448 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.