Skip to content

Commit

Permalink
Small notebook fix (#148)
Browse files Browse the repository at this point in the history
* Improved the plotting tutorial notebook
* Formatted all notebooks with black and updated the developer requirements to match.
  • Loading branch information
nwlandry authored Aug 8, 2022
1 parent 43ffb28 commit 4069c0d
Show file tree
Hide file tree
Showing 11 changed files with 262 additions and 158 deletions.
4 changes: 3 additions & 1 deletion benchmarks/hypernetx.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@
"start = time.time()\n",
"H2 = hnx.Hypergraph(df, static=True)\n",
"print(f\"HyperNetX construction time: {time.time() - start} sec\")\n",
"print(f\"The hypergraph has {H2.number_of_nodes()} nodes and {H2.number_of_edges()} edges.\")"
"print(\n",
" f\"The hypergraph has {H2.number_of_nodes()} nodes and {H2.number_of_edges()} edges.\"\n",
")"
]
},
{
Expand Down
4 changes: 3 additions & 1 deletion benchmarks/networkx.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@
"metadata": {},
"outputs": [],
"source": [
"print(f\"The NetworkX graph has {G1.number_of_nodes()} nodes and {G1.number_of_edges()} edges\")\n",
"print(\n",
" f\"The NetworkX graph has {G1.number_of_nodes()} nodes and {G1.number_of_edges()} edges\"\n",
")\n",
"print(f\"The XGI graph has {G2.num_nodes} nodes and {G2.num_edges} edges\")"
]
},
Expand Down
2 changes: 1 addition & 1 deletion requirements/developer.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
black==22.3.0
black[jupyter]==22.3.0
pre-commit>=2.12
isort==5.10.1
pylint>=2.10
23 changes: 18 additions & 5 deletions tutorials/Tutorial 1 - Basic Hypergraph Functionality.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,16 @@
"max_edge_size = 25\n",
"\n",
"# hyperedge list\n",
"hyperedge_list = [random.sample(range(n), random.choice(range(min_edge_size,max_edge_size+1))) for i in range(m)]\n",
"hyperedge_list = [\n",
" random.sample(range(n), random.choice(range(min_edge_size, max_edge_size + 1)))\n",
" for i in range(m)\n",
"]\n",
"\n",
"# hyperedge dict\n",
"hyperedge_dict = {i : random.sample(range(n), random.choice(range(min_edge_size,max_edge_size+1))) for i in range(m)}\n",
"hyperedge_dict = {\n",
" i: random.sample(range(n), random.choice(range(min_edge_size, max_edge_size + 1)))\n",
" for i in range(m)\n",
"}\n",
"\n",
"# pandas dataframe\n",
"fname = \"../data/disGene.txt\"\n",
Expand Down Expand Up @@ -243,7 +249,10 @@
"max_edge_size = 10\n",
"\n",
"# hyperedge list\n",
"hyperedge_list = [random.sample(range(n), random.choice(range(min_edge_size,max_edge_size+1))) for i in range(m)]\n",
"hyperedge_list = [\n",
" random.sample(range(n), random.choice(range(min_edge_size, max_edge_size + 1)))\n",
" for i in range(m)\n",
"]\n",
"H = xgi.Hypergraph(hyperedge_list)"
]
},
Expand Down Expand Up @@ -274,7 +283,9 @@
"print([len(component) for component in xgi.connected_components(H)])\n",
"\n",
"node = np.random.choice(H.nodes)\n",
"print(f\"The size of the component containing node {node} is {len(xgi.node_connected_component(H, node))}\")"
"print(\n",
" f\"The size of the component containing node {node} is {len(xgi.node_connected_component(H, node))}\"\n",
")"
]
},
{
Expand All @@ -297,7 +308,9 @@
"# A subhypergraph induced on edges\n",
"edge_subhypergraph = xgi.subhypergraph(H, edges=list(range(100)))\n",
"# A subhypergraph induced on both nodes and edges\n",
"arbitrary_subhypergraph = xgi.subhypergraph(H, nodes=list(range(100)), edges=list(range(100)))"
"arbitrary_subhypergraph = xgi.subhypergraph(\n",
" H, nodes=list(range(100)), edges=list(range(100))\n",
")"
]
},
{
Expand Down
11 changes: 8 additions & 3 deletions tutorials/Tutorial 2 - Read and Write.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@
"max_edge_size = 10\n",
"\n",
"# hyperedge list\n",
"hyperedge_list = [random.sample(range(n), random.choice(range(min_edge_size,max_edge_size+1))) for i in range(m)]\n",
"hyperedge_list = [\n",
" random.sample(range(n), random.choice(range(min_edge_size, max_edge_size + 1)))\n",
" for i in range(m)\n",
"]\n",
"H = xgi.Hypergraph(hyperedge_list)"
]
},
Expand All @@ -64,7 +67,7 @@
"outputs": [],
"source": [
"# Write the example hypergraph to a JSON file\n",
"xgi.write_json(H,\"hypergraph_json.json\")\n",
"xgi.write_json(H, \"hypergraph_json.json\")\n",
"# Load the file just written and store it in a new hypergraph\n",
"H_json = xgi.read_json(\"hypergraph_json.json\")"
]
Expand Down Expand Up @@ -108,7 +111,9 @@
"# Write the hypergraph as a bipartite edge list\n",
"xgi.write_bipartite_edgelist(H, \"bipartite_edge_list.csv\", delimiter=\",\")\n",
"# Read the file just written as a new hypergraph\n",
"H_bel = xgi.read_bipartite_edgelist(\"bipartite_edge_list.csv\", delimiter=\",\", nodetype=int)"
"H_bel = xgi.read_bipartite_edgelist(\n",
" \"bipartite_edge_list.csv\", delimiter=\",\", nodetype=int\n",
")"
]
}
],
Expand Down
37 changes: 25 additions & 12 deletions tutorials/Tutorial 3 - Basic simplicial complex usage.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"outputs": [],
"source": [
"SC = xgi.SimplicialComplex()\n",
"H = xgi.Hypergraph()"
"H = xgi.Hypergraph()"
]
},
{
Expand Down Expand Up @@ -115,8 +115,8 @@
},
"outputs": [],
"source": [
"SC.add_simplex([0,1,2])\n",
"H.add_edge([0,1,2])"
"SC.add_simplex([0, 1, 2])\n",
"H.add_edge([0, 1, 2])"
]
},
{
Expand Down Expand Up @@ -245,9 +245,9 @@
}
],
"source": [
"for dim in [0,1,2]:\n",
" print('# edges in H at order', dim, H.edges.filterby(\"order\", dim))\n",
" print('# edges in SC at order', dim, SC.edges.filterby(\"order\", dim), '\\n')"
"for dim in [0, 1, 2]:\n",
" print(\"# edges in H at order\", dim, H.edges.filterby(\"order\", dim))\n",
" print(\"# edges in SC at order\", dim, SC.edges.filterby(\"order\", dim), \"\\n\")"
]
},
{
Expand Down Expand Up @@ -281,9 +281,14 @@
}
],
"source": [
"for dim in [0,1,2]:\n",
"for dim in [0, 1, 2]:\n",
" edge_sel = list(SC.edges.filterby(\"order\", dim))\n",
" print('Composition of simplices at order', dim, [SC.edges.members(x) for x in edge_sel], '\\n')"
" print(\n",
" \"Composition of simplices at order\",\n",
" dim,\n",
" [SC.edges.members(x) for x in edge_sel],\n",
" \"\\n\",\n",
" )"
]
},
{
Expand Down Expand Up @@ -317,9 +322,14 @@
}
],
"source": [
"for dim in [0,1,2]:\n",
"for dim in [0, 1, 2]:\n",
" edge_sel = list(H.edges.filterby(\"order\", dim))\n",
" print('Composition of edges at order', dim, [H.edges.members(x) for x in edge_sel], '\\n')\n"
" print(\n",
" \"Composition of edges at order\",\n",
" dim,\n",
" [H.edges.members(x) for x in edge_sel],\n",
" \"\\n\",\n",
" )"
]
},
{
Expand Down Expand Up @@ -348,7 +358,7 @@
},
"outputs": [],
"source": [
"facets = [[0,1,2], [0,1,3,4]]\n",
"facets = [[0, 1, 2], [0, 1, 3, 4]]\n",
"SC1 = xgi.SimplicialComplex(facets)"
]
},
Expand Down Expand Up @@ -418,7 +428,10 @@
"min_edge_size = 2\n",
"max_edge_size = 4\n",
"\n",
"hyperedge_list = [random.sample(range(n), random.choice(range(min_edge_size,max_edge_size+1))) for i in range(m)]\n",
"hyperedge_list = [\n",
" random.sample(range(n), random.choice(range(min_edge_size, max_edge_size + 1)))\n",
" for i in range(m)\n",
"]\n",
"SC_big = xgi.SimplicialComplex(hyperedge_list)\n",
"str(SC_big)"
]
Expand Down
17 changes: 9 additions & 8 deletions tutorials/Tutorial 4 - Generative_Models.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@
"outputs": [],
"source": [
"n = 1000\n",
"k1 = {i : random.randint(10, 30) for i in range(n)}\n",
"k2 = {i : sorted(k1.values())[i] for i in range(n)}\n",
"k1 = {i: random.randint(10, 30) for i in range(n)}\n",
"k2 = {i: sorted(k1.values())[i] for i in range(n)}\n",
"H = xgi.chung_lu_hypergraph(k1, k2)"
]
},
Expand Down Expand Up @@ -145,10 +145,10 @@
],
"source": [
"n = 1000\n",
"k1 = {i : random.randint(1, 100) for i in range(n)}\n",
"k2 = {i : sorted(k1.values())[i] for i in range(n)}\n",
"g1 = {i : random.choice([0, 1]) for i in range(n)}\n",
"g2 = {i : random.choice([0, 1]) for i in range(n)}\n",
"k1 = {i: random.randint(1, 100) for i in range(n)}\n",
"k2 = {i: sorted(k1.values())[i] for i in range(n)}\n",
"g1 = {i: random.choice([0, 1]) for i in range(n)}\n",
"g2 = {i: random.choice([0, 1]) for i in range(n)}\n",
"omega = np.array([[100, 10], [10, 100]])\n",
"H = xgi.dcsbm_hypergraph(k1, k2, g1, g2, omega)"
]
Expand Down Expand Up @@ -182,7 +182,7 @@
},
"outputs": [],
"source": [
"n = 20;\n",
"n = 20\n",
"ps = [0.1, 0.2, 0.1]\n",
"SC = xgi.random_simplicial_complex(n, ps)"
]
Expand Down Expand Up @@ -270,7 +270,8 @@
},
"outputs": [],
"source": [
"import networkx as nx \n",
"import networkx as nx\n",
"\n",
"G = nx.karate_club_graph()\n",
"\n",
"SC_KC = xgi.flag_complex(G, max_order=3)"
Expand Down
66 changes: 44 additions & 22 deletions tutorials/Tutorial 5 - Plotting.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@
"outputs": [],
"source": [
"H = xgi.Hypergraph()\n",
"H.add_edges_from([[1,2,3],[3,4,5],[3,6],[6,7,8,9],[1,4,10,11,12],[1,4]])"
"H.add_edges_from(\n",
" [[1, 2, 3], [3, 4, 5], [3, 6], [6, 7, 8, 9], [1, 4, 10, 11, 12], [1, 4]]\n",
")"
]
},
{
Expand Down Expand Up @@ -100,18 +102,18 @@
"metadata": {},
"outputs": [],
"source": [
"plt.figure(figsize=(10,4))\n",
"plt.figure(figsize=(10, 4))\n",
"\n",
"#Sequential colormap\n",
"# Sequential colormap\n",
"cmap = plt.cm.Paired\n",
"\n",
"ax = plt.subplot(1,2,1)\n",
"ax = plt.subplot(1, 2, 1)\n",
"xgi.draw(H, pos, ax=ax, edge_face_colormap=cmap)\n",
"\n",
"#Qualitative colormap\n",
"# Qualitative colormap\n",
"cmap = plt.cm.Purples\n",
"\n",
"ax = plt.subplot(1,2,2)\n",
"ax = plt.subplot(1, 2, 2)\n",
"xgi.draw(H, pos, ax=ax, edge_face_colormap=cmap)"
]
},
Expand All @@ -131,15 +133,24 @@
"outputs": [],
"source": [
"cmap = plt.cm.Reds\n",
"edge_lc = 'gray'\n",
"edge_lc = \"gray\"\n",
"edge_lw = 4\n",
"node_fc = 'black'\n",
"node_ec = 'white'\n",
"node_fc = \"black\"\n",
"node_ec = \"white\"\n",
"node_lw = 2\n",
"node_size = 20\n",
"\n",
"xgi.draw(H, pos, edge_lc=edge_lc, edge_lw=edge_lw,\n",
" node_fc=node_fc, node_ec=node_ec, node_lw=node_lw, node_size=node_size, edge_face_colormap=cmap)"
"xgi.draw(\n",
" H,\n",
" pos,\n",
" edge_lc=edge_lc,\n",
" edge_lw=edge_lw,\n",
" node_fc=node_fc,\n",
" node_ec=node_ec,\n",
" node_lw=node_lw,\n",
" node_size=node_size,\n",
" edge_face_colormap=cmap,\n",
")"
]
},
{
Expand Down Expand Up @@ -172,7 +183,7 @@
"outputs": [],
"source": [
"SC = xgi.SimplicialComplex()\n",
"SC.add_simplices_from([[3,4,5],[3,6],[6,7,8,9],[1,4,10,11,12],[1,4]])"
"SC.add_simplices_from([[3, 4, 5], [3, 6], [6, 7, 8, 9], [1, 4, 10, 11, 12], [1, 4]])"
]
},
{
Expand Down Expand Up @@ -212,7 +223,10 @@
"outputs": [],
"source": [
"n = 100\n",
"H = xgi.random_hypergraph(100, [0.03, 0.0002, 0.00001])\n",
"is_connected = False\n",
"while not is_connected:\n",
" H = xgi.random_hypergraph(n, [0.03, 0.0002, 0.00001])\n",
" is_connected = xgi.is_connected(H)\n",
"pos = xgi.barycenter_spring_layout(H)"
]
},
Expand All @@ -231,17 +245,17 @@
"metadata": {},
"outputs": [],
"source": [
"plt.figure(figsize=(10,10))\n",
"plt.figure(figsize=(10, 10))\n",
"ax = plt.subplot(111)\n",
"xgi.draw(H, pos, node_size = 10, ax=ax)"
"xgi.draw(H, pos, node_size=10, ax=ax)"
]
},
{
"cell_type": "markdown",
"id": "39e2d438",
"metadata": {},
"source": [
"We can even size/color the nodes by their degree!"
"We can even size/color the nodes and edges by NodeStats or EdgeStats (e.g., degree, centrality, size, etc.)!"
]
},
{
Expand All @@ -251,9 +265,17 @@
"metadata": {},
"outputs": [],
"source": [
"plt.figure(figsize=(10,10))\n",
"plt.figure(figsize=(10, 10))\n",
"ax = plt.subplot(111)\n",
"xgi.draw(H, pos, node_size = H.nodes.degree, node_lw = H.nodes.degree, node_fc=H.nodes.cec_centrality, edge_fc=H.edges.node_edge_centrality, ax=ax)"
"xgi.draw(\n",
" H,\n",
" pos,\n",
" node_size=H.nodes.degree,\n",
" node_lw=H.nodes.degree,\n",
" node_fc=H.nodes.cec_centrality,\n",
" edge_fc=H.edges.node_edge_centrality,\n",
" ax=ax,\n",
")"
]
},
{
Expand All @@ -274,13 +296,13 @@
"source": [
"centers, heights = xgi.degree_histogram(H)\n",
"\n",
"plt.figure(figsize=(12,4))\n",
"plt.figure(figsize=(12, 4))\n",
"ax = plt.subplot(111)\n",
"\n",
"ax.bar(centers, heights)\n",
"ax.set_ylabel('Count')\n",
"ax.set_xlabel('Degree')\n",
"ax.set_xticks(np.arange(1, max(centers)+1, step=1));"
"ax.set_ylabel(\"Count\")\n",
"ax.set_xlabel(\"Degree\")\n",
"ax.set_xticks(np.arange(1, max(centers) + 1, step=1));"
]
}
],
Expand Down
Loading

0 comments on commit 4069c0d

Please sign in to comment.