|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": 1, |
| 6 | + "metadata": {}, |
| 7 | + "outputs": [ |
| 8 | + { |
| 9 | + "ename": "ModuleNotFoundError", |
| 10 | + "evalue": "No module named 'page_ranker'", |
| 11 | + "output_type": "error", |
| 12 | + "traceback": [ |
| 13 | + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", |
| 14 | + "\u001b[0;31mModuleNotFoundError\u001b[0m Traceback (most recent call last)", |
| 15 | + "\u001b[0;32m<ipython-input-1-99a27dde11b2>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0;31m#import json\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0;31m#import os\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 6\u001b[0;31m \u001b[0;32mimport\u001b[0m \u001b[0mpage_ranker\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0mpr\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 7\u001b[0m \u001b[0;31m#import import_graph as ig\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 8\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0minspect_subgraph\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0misg\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", |
| 16 | + "\u001b[0;31mModuleNotFoundError\u001b[0m: No module named 'page_ranker'" |
| 17 | + ] |
| 18 | + } |
| 19 | + ], |
| 20 | + "source": [ |
| 21 | + "import numpy as np\n", |
| 22 | + "import pandas as pd\n", |
| 23 | + "import networkx as nx\n", |
| 24 | + "#import json\n", |
| 25 | + "#import os\n", |
| 26 | + "import page_ranker as pr\n", |
| 27 | + "#import import_graph as ig\n", |
| 28 | + "import inspect_subgraph as isg\n", |
| 29 | + "\n", |
| 30 | + "import matplotlib.pyplot as plt\n", |
| 31 | + "%matplotlib inline" |
| 32 | + ] |
| 33 | + }, |
| 34 | + { |
| 35 | + "cell_type": "code", |
| 36 | + "execution_count": null, |
| 37 | + "metadata": {}, |
| 38 | + "outputs": [], |
| 39 | + "source": [ |
| 40 | + "cwd = os.getcwd()\n", |
| 41 | + "\n", |
| 42 | + "dir_as_list = cwd.split('/')\n", |
| 43 | + "base_path=''\n", |
| 44 | + "go = True\n", |
| 45 | + "i=0\n", |
| 46 | + "while go:\n", |
| 47 | + " base_path= base_path+dir_as_list[i]+'/'\n", |
| 48 | + " if dir_as_list[i]=='research':\n", |
| 49 | + " go = False\n", |
| 50 | + " i=i+1\n", |
| 51 | + " \n", |
| 52 | + "rel_path = \"sample-graphs/sourcecred_sourcecred.json\"\n", |
| 53 | + "\n", |
| 54 | + "abs_file_path = os.path.abspath(os.path.join(base_path, rel_path))" |
| 55 | + ] |
| 56 | + }, |
| 57 | + { |
| 58 | + "cell_type": "code", |
| 59 | + "execution_count": null, |
| 60 | + "metadata": {}, |
| 61 | + "outputs": [], |
| 62 | + "source": [ |
| 63 | + "N =10" |
| 64 | + ] |
| 65 | + }, |
| 66 | + { |
| 67 | + "cell_type": "code", |
| 68 | + "execution_count": null, |
| 69 | + "metadata": {}, |
| 70 | + "outputs": [], |
| 71 | + "source": [ |
| 72 | + "line = nx.path_graph(N, create_using=nx.MultiDiGraph)\n", |
| 73 | + "edges = line.edges\n", |
| 74 | + "\n", |
| 75 | + "G = nx.MultiDiGraph()\n", |
| 76 | + "for e in edges:\n", |
| 77 | + " G.add_edge(e[0],e[1])\n", |
| 78 | + " G.add_edge(e[1],e[0])\n", |
| 79 | + " \n", |
| 80 | + "nx.set_node_attributes(G,'vanilla', 'type')\n", |
| 81 | + "nx.set_edge_attributes(G,'vanilla', 'type')" |
| 82 | + ] |
| 83 | + }, |
| 84 | + { |
| 85 | + "cell_type": "code", |
| 86 | + "execution_count": null, |
| 87 | + "metadata": {}, |
| 88 | + "outputs": [], |
| 89 | + "source": [ |
| 90 | + "G.nodes[0]" |
| 91 | + ] |
| 92 | + }, |
| 93 | + { |
| 94 | + "cell_type": "code", |
| 95 | + "execution_count": null, |
| 96 | + "metadata": {}, |
| 97 | + "outputs": [], |
| 98 | + "source": [ |
| 99 | + "G.edges[(0,1,0)]" |
| 100 | + ] |
| 101 | + }, |
| 102 | + { |
| 103 | + "cell_type": "code", |
| 104 | + "execution_count": null, |
| 105 | + "metadata": {}, |
| 106 | + "outputs": [], |
| 107 | + "source": [ |
| 108 | + "isg.inspectSubGraph(G, G.nodes, expand=False, verbose=False, label=True,pos = \"kk\")" |
| 109 | + ] |
| 110 | + }, |
| 111 | + { |
| 112 | + "cell_type": "code", |
| 113 | + "execution_count": null, |
| 114 | + "metadata": {}, |
| 115 | + "outputs": [], |
| 116 | + "source": [ |
| 117 | + "G.edges[(0,1,0)]" |
| 118 | + ] |
| 119 | + }, |
| 120 | + { |
| 121 | + "cell_type": "code", |
| 122 | + "execution_count": null, |
| 123 | + "metadata": {}, |
| 124 | + "outputs": [], |
| 125 | + "source": [ |
| 126 | + "alpha = .1\n", |
| 127 | + "iterations = 25\n", |
| 128 | + "\n", |
| 129 | + "seed = {n:1/len(G.nodes) for n in G.nodes}\n", |
| 130 | + "#seed = {n:0 for n in G.nodes}\n", |
| 131 | + "#seed[0] =1\n", |
| 132 | + "\n", |
| 133 | + "node_wt_by_type = {'vanilla': 1}\n", |
| 134 | + "edge_wt_by_type = {'vanilla': (1,1/2)}\n", |
| 135 | + "\n", |
| 136 | + "self_loop_wt= .1" |
| 137 | + ] |
| 138 | + }, |
| 139 | + { |
| 140 | + "cell_type": "code", |
| 141 | + "execution_count": null, |
| 142 | + "metadata": {}, |
| 143 | + "outputs": [], |
| 144 | + "source": [ |
| 145 | + "r, df, g = pr.pageRanker(G,\n", |
| 146 | + " alpha,\n", |
| 147 | + " iterations,\n", |
| 148 | + " seed=seed,\n", |
| 149 | + " initial_value = seed,\n", |
| 150 | + " lazy=True,\n", |
| 151 | + " lazy_decay=True,\n", |
| 152 | + " self_loop_wt=self_loop_wt, \n", |
| 153 | + " node_wt_by_type =node_wt_by_type,\n", |
| 154 | + " edge_wt_by_type=edge_wt_by_type)" |
| 155 | + ] |
| 156 | + }, |
| 157 | + { |
| 158 | + "cell_type": "code", |
| 159 | + "execution_count": null, |
| 160 | + "metadata": {}, |
| 161 | + "outputs": [], |
| 162 | + "source": [ |
| 163 | + "df.tail(1).T.plot(kind='bar',legend=False, title='PageRank Distribution')" |
| 164 | + ] |
| 165 | + }, |
| 166 | + { |
| 167 | + "cell_type": "code", |
| 168 | + "execution_count": null, |
| 169 | + "metadata": {}, |
| 170 | + "outputs": [], |
| 171 | + "source": [ |
| 172 | + "df.plot(figsize=(20,10))" |
| 173 | + ] |
| 174 | + }, |
| 175 | + { |
| 176 | + "cell_type": "code", |
| 177 | + "execution_count": null, |
| 178 | + "metadata": {}, |
| 179 | + "outputs": [], |
| 180 | + "source": [ |
| 181 | + "df.sum(axis=1).plot()" |
| 182 | + ] |
| 183 | + }, |
| 184 | + { |
| 185 | + "cell_type": "code", |
| 186 | + "execution_count": null, |
| 187 | + "metadata": {}, |
| 188 | + "outputs": [], |
| 189 | + "source": [ |
| 190 | + "ns = [10000*G.nodes[n]['score'] for n in G.nodes]\n", |
| 191 | + "nl = {n:n for n in G.nodes}\n", |
| 192 | + "\n", |
| 193 | + "nx.draw_circular(G, node_size=ns, labels=nl, font_size=8, alpha=.5)" |
| 194 | + ] |
| 195 | + }, |
| 196 | + { |
| 197 | + "cell_type": "code", |
| 198 | + "execution_count": null, |
| 199 | + "metadata": {}, |
| 200 | + "outputs": [], |
| 201 | + "source": [ |
| 202 | + "nx.get_node_attributes(G,'self_wt')" |
| 203 | + ] |
| 204 | + }, |
| 205 | + { |
| 206 | + "cell_type": "code", |
| 207 | + "execution_count": null, |
| 208 | + "metadata": {}, |
| 209 | + "outputs": [], |
| 210 | + "source": [ |
| 211 | + "nx.get_edge_attributes(G,'out_weight')" |
| 212 | + ] |
| 213 | + }, |
| 214 | + { |
| 215 | + "cell_type": "code", |
| 216 | + "execution_count": null, |
| 217 | + "metadata": {}, |
| 218 | + "outputs": [], |
| 219 | + "source": [ |
| 220 | + "nx.get_edge_attributes(G,'in_weight')" |
| 221 | + ] |
| 222 | + }, |
| 223 | + { |
| 224 | + "cell_type": "code", |
| 225 | + "execution_count": null, |
| 226 | + "metadata": {}, |
| 227 | + "outputs": [], |
| 228 | + "source": [ |
| 229 | + "nx.get_node_attributes(G,'total_wt')" |
| 230 | + ] |
| 231 | + }, |
| 232 | + { |
| 233 | + "cell_type": "code", |
| 234 | + "execution_count": null, |
| 235 | + "metadata": {}, |
| 236 | + "outputs": [], |
| 237 | + "source": [ |
| 238 | + "all_nbr = {n:list(nx.all_neighbors(G,n)) for n in G.nodes}" |
| 239 | + ] |
| 240 | + }, |
| 241 | + { |
| 242 | + "cell_type": "code", |
| 243 | + "execution_count": null, |
| 244 | + "metadata": {}, |
| 245 | + "outputs": [], |
| 246 | + "source": [ |
| 247 | + "all_nbr" |
| 248 | + ] |
| 249 | + }, |
| 250 | + { |
| 251 | + "cell_type": "code", |
| 252 | + "execution_count": null, |
| 253 | + "metadata": {}, |
| 254 | + "outputs": [], |
| 255 | + "source": [ |
| 256 | + "nbr = {n:list(G.neighbors(n)) for n in G.nodes}" |
| 257 | + ] |
| 258 | + }, |
| 259 | + { |
| 260 | + "cell_type": "code", |
| 261 | + "execution_count": null, |
| 262 | + "metadata": {}, |
| 263 | + "outputs": [], |
| 264 | + "source": [ |
| 265 | + "nbr" |
| 266 | + ] |
| 267 | + }, |
| 268 | + { |
| 269 | + "cell_type": "code", |
| 270 | + "execution_count": null, |
| 271 | + "metadata": {}, |
| 272 | + "outputs": [], |
| 273 | + "source": [ |
| 274 | + "G.edges" |
| 275 | + ] |
| 276 | + }, |
| 277 | + { |
| 278 | + "cell_type": "code", |
| 279 | + "execution_count": null, |
| 280 | + "metadata": {}, |
| 281 | + "outputs": [], |
| 282 | + "source": [] |
| 283 | + }, |
| 284 | + { |
| 285 | + "cell_type": "code", |
| 286 | + "execution_count": null, |
| 287 | + "metadata": {}, |
| 288 | + "outputs": [], |
| 289 | + "source": [] |
| 290 | + } |
| 291 | + ], |
| 292 | + "metadata": { |
| 293 | + "kernelspec": { |
| 294 | + "display_name": "Python 3", |
| 295 | + "language": "python", |
| 296 | + "name": "python3" |
| 297 | + }, |
| 298 | + "language_info": { |
| 299 | + "codemirror_mode": { |
| 300 | + "name": "ipython", |
| 301 | + "version": 3 |
| 302 | + }, |
| 303 | + "file_extension": ".py", |
| 304 | + "mimetype": "text/x-python", |
| 305 | + "name": "python", |
| 306 | + "nbconvert_exporter": "python", |
| 307 | + "pygments_lexer": "ipython3", |
| 308 | + "version": "3.6.8" |
| 309 | + } |
| 310 | + }, |
| 311 | + "nbformat": 4, |
| 312 | + "nbformat_minor": 2 |
| 313 | +} |
0 commit comments