Skip to content

Commit

Permalink
update chap 4
Browse files Browse the repository at this point in the history
  • Loading branch information
Ziaeemehr committed Aug 20, 2024
1 parent 157bd64 commit 65bd9d1
Show file tree
Hide file tree
Showing 3 changed files with 269 additions and 83 deletions.
288 changes: 207 additions & 81 deletions docs/examples/chap_04.ipynb

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion netsci/datasets/sample_graphs.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
"function": "TOTDO add a function to load the file and make the graph"
},
"WWW":{
"filename": "WWW.edgelist.txt",
"filename": "www.edgelist.txt",
"url": "https://networksciencebook.com/translations/en/resources/networks.zip",
"description": "Nodes represent web pages form the University of Notre Dame under the domain nd.edu, directed links represent hyperlinks between them. Data collected in 1999. Ref: Albert, R., Jeong, H., & Barabási, A. L. (1999). Internet: Diameter of the world-wide web. Nature, 401(6749), 130-131.",
"directed": true,
Expand Down
62 changes: 61 additions & 1 deletion netsci/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import gzip
import json
import numpy as np
import igraph as ig
import networkx as nx
from numpy import power
from os.path import join
Expand Down Expand Up @@ -114,7 +115,7 @@ def load_sample_graph(name, verbose=False):
Parameters
--------------
name: str
The name of the graph. Get names from `netsci.utils.show_sample_graphs()`.
The name of the graph. Get names from `netsci.utils.list_sample_graphs()`.
verbose: bool, optional
If True, print information about the loaded graph. Default is True.
Expand Down Expand Up @@ -159,6 +160,65 @@ def list_sample_graphs():
return data


def _load_graphi(file_path, url, directed, verbose=False):
'''
load a graph from igraph
'''

# path = get_sample_dataset_path()
# path_zip = join(path, "networks.zip")
# if not os.path.isfile(file_path):
# if not os.path.isfile(path_zip):
# os.system(f"wget -P {path} {url}")

# if not os.path.isfile(file_path):
# if os.path.isfile(path_zip):
# os.system(f"unzip {path_zip} -d {path}")

edges = []
with open(file_path, "r") as file:
for line in file:
if line.startswith("#"):
continue # Skip comments
A, B = map(int, line.split())
edges.append((A, B))
G = ig.Graph(edges=edges, directed=directed)
return G

def load_sample_graphi(name, verbose=False):
'''
load a graph from igraph
Parameters
-------------
name: str
The name of the graph. Get names from `netsci.utils.list_sample_graphs()`.
verbose: bool, optional
If True, print information about the loaded graph. Default is True.
Returns
---------
value: igraph.Graph
Loaded graph.
'''

path = get_sample_dataset_path()
with open(os.path.join(path, "sample_graphs.json"), "r") as f:
data = json.load(f)
if name in list(data.keys()):
filename = data[name]["filename"]
file_path = os.path.join(path, f"{filename}")
directed = data[name]["directed"]
G = _load_graphi(
file_path, url=data[name]["url"], directed=directed, verbose=verbose
)
if verbose:
print(f"Successfully loaded {name}")
print("================================")
print(data[name]["description"])
return G


def generate_power_law_dist(N: int, a: float, xmin: float):
"""
generate power law random numbers p(k) ~ x^(-a) for a>1
Expand Down

0 comments on commit 65bd9d1

Please sign in to comment.