Skip to content

Commit

Permalink
CI: Add nbqa for linting notebooks (#63)
Browse files Browse the repository at this point in the history
Adds nbqa to CI to run common linters (black, pyugrade, etc.) via pre-commit.
  • Loading branch information
MridulS authored Jun 14, 2022
1 parent a4334ea commit 73cf5d8
Show file tree
Hide file tree
Showing 10 changed files with 174 additions and 104 deletions.
14 changes: 11 additions & 3 deletions .github/workflows/notebooks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ jobs:
strategy:
max-parallel: 12
matrix:
os: [Ubuntu-20.04, macOS-latest]
python-version: [3.8, 3.9, "3.10"]
os: [Ubuntu-22.04, macOS-latest]
python-version: ["3.9", "3.10"]

steps:
- uses: actions/checkout@v2
Expand All @@ -39,8 +39,16 @@ jobs:
run: |
pip install pip==21.1.1
pip install -r requirements.txt
- name: Lint with precommit
run: |
pip install pre-commit
find content/ -name "*.md" -exec jupytext --to notebook {} \;
# pre-commit wants files to be staged
git add content/
pre-commit run --all-files --show-diff-on-failure --color always
- name: Test with nbval
run: |
pip install pytest
find content/ -name "*.md" -exec jupytext --to notebook {} \;
pytest --nbval-lax content/
7 changes: 7 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
repos:
- repo: https://github.com/nbQA-dev/nbQA
rev: 1.3.1
hooks:
- id: nbqa-black
- id: nbqa-pyupgrade
args: [--py38-plus]
3 changes: 2 additions & 1 deletion content/algorithms/assortativity/correlation.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ jupytext:
extension: .md
format_name: myst
format_version: 0.13
jupytext_version: 1.11.2
jupytext_version: 1.13.8
kernelspec:
display_name: Python 3
language: python
Expand Down Expand Up @@ -140,6 +140,7 @@ import pickle
import copy
import random
import warnings
warnings.filterwarnings("ignore")
```

Expand Down
8 changes: 5 additions & 3 deletions content/algorithms/dag/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ jupytext:
extension: .md
format_name: myst
format_version: 0.13
jupytext_version: 1.11.1
jupytext_version: 1.13.8
kernelspec:
display_name: Python 3
language: python
Expand Down Expand Up @@ -44,7 +44,8 @@ triangle_graph = nx.from_edgelist([(1, 2), (2, 3), (3, 1)], create_using=nx.DiGr
```

```{code-cell} ipython3
nx.draw_planar(triangle_graph,
nx.draw_planar(
triangle_graph,
with_labels=True,
node_size=1000,
node_color="#ffff8f",
Expand Down Expand Up @@ -75,7 +76,8 @@ clothing_graph = nx.read_graphml(f"data/clothing_graph.graphml")
```{code-cell} ipython3
plt.figure(figsize=(12, 12), dpi=150)
nx.draw_planar(clothing_graph,
nx.draw_planar(
clothing_graph,
arrowsize=12,
with_labels=True,
node_size=8000,
Expand Down
48 changes: 27 additions & 21 deletions content/algorithms/flow/dinitz_alg.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ jupytext:
extension: .md
format_name: myst
format_version: 0.13
jupytext_version: 1.13.4
jupytext_version: 1.13.8
kernelspec:
display_name: Python 3 (ipykernel)
language: python
Expand Down Expand Up @@ -162,10 +162,12 @@ def check_valid_flow(G, flow, source_node, target_node):
def visualize_flow(flow_graph):
"""Visualize flow returned by the `check_valid_flow` funcion."""
fig, ax = plt.subplots(figsize=(15, 9))
# Draw the full graph for reference
nx.draw(G, pos, ax=ax, node_color=node_colors, edge_color="lightgrey", with_labels=True)
nx.draw(
G, pos, ax=ax, node_color=node_colors, edge_color="lightgrey", with_labels=True
)
# Draw the example flow on top
flow_nc = [
"skyblue" if n in {"s", "t"} else flow_graph.nodes[n].get("color", "lightgrey")
Expand Down Expand Up @@ -248,9 +250,7 @@ def residual_graph(G, flow):
for (u, v), f in flow.items():
capacity = G[u][v]["capacity"]
if f > G[u][v]["capacity"]:
raise ValueError(
f"Flow {f} exceeds the capacity of edge {u!r}->{v!r}."
)
raise ValueError(f"Flow {f} exceeds the capacity of edge {u!r}->{v!r}.")
H[u][v]["capacity"] -= f
if H.has_edge(v, u):
H[v][u]["capacity"] += f
Expand Down Expand Up @@ -283,18 +283,20 @@ def draw_residual_graph(R, ax=None):
# Draw edges
nx.draw_networkx_edges(R, pos, edgelist=orig_edges)
nx.draw_networkx_edges(
R, pos, edgelist=rev_edges, edge_color="goldenrod", connectionstyle="arc3,rad=0.2"
R,
pos,
edgelist=rev_edges,
edge_color="goldenrod",
connectionstyle="arc3,rad=0.2",
)
nx.draw_networkx_edges(
R, pos, edgelist=zero_edges, style="--", edge_color="lightgrey"
)
nx.draw_networkx_edges(R, pos, edgelist=zero_edges, style="--", edge_color="lightgrey")
# Label edges by capacity
rv = set(rev_edges)
fwd_caps = {
(u, v): c for u, v, c in R.edges(data="capacity") if (u, v) not in rv
}
rev_caps = {
(u, v): c for u, v, c in R.edges(data="capacity") if (u, v) in rv
}
fwd_caps = {(u, v): c for u, v, c in R.edges(data="capacity") if (u, v) not in rv}
rev_caps = {(u, v): c for u, v, c in R.edges(data="capacity") if (u, v) in rv}
nx.draw_networkx_edge_labels(R, pos, edge_labels=fwd_caps, label_pos=0.667)
nx.draw_networkx_edge_labels(
R, pos, edge_labels=rev_caps, label_pos=0.667, font_color="goldenrod"
Expand Down Expand Up @@ -333,9 +335,15 @@ network $L$ which connect nodes of 2 different levels
```{code-cell} ipython3
# Mapping between node level and color for visualization
level_colors = {
1:'aqua', 2:'lightgreen', 3:'yellow', 4:'orange', 5:'lightpink', 6:'violet'
1: "aqua",
2: "lightgreen",
3: "yellow",
4: "orange",
5: "lightpink",
6: "violet",
}
def level_bfs(R, flow, source_node, target_node):
"""BFS to construct the level network from residual network for given flow."""
parents, level = {}, {}
Expand Down Expand Up @@ -453,7 +461,7 @@ aug_path = residual_graph(R.subgraph(path), aug_flow)
# Node ordering in the subgraph can be different than `path`
nodes = list(aug_path.nodes)
node_colors = [level_colors[level[n]] for n in nodes]
node_colors[nodes.index('s')] = node_colors[nodes.index('t')] = "skyblue"
node_colors[nodes.index("s")] = node_colors[nodes.index("t")] = "skyblue"
draw_residual_graph(aug_path, ax=plt.gca())
```
Expand Down Expand Up @@ -509,9 +517,7 @@ for cutoff, ax in zip(cutoff_list, axes.ravel()):
R = nx.flow.dinitz(G, s="s", t="t", capacity="capacity", cutoff=cutoff)
# coloring and labeling edges depending on if they have non-zero flow value or not
edge_colors = [
"lightgray" if R[u][v]["flow"] == 0 else "black" for u, v in G.edges
]
edge_colors = ["lightgray" if R[u][v]["flow"] == 0 else "black" for u, v in G.edges]
edge_labels = {
(u, v): f"{R[u][v]['flow']}/{G[u][v]['capacity']}"
for u, v in G.edges
Expand Down
2 changes: 2 additions & 0 deletions content/algorithms/index.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@


# Algorithms

A closer look at some of the algorithms and network analysis techniques
Expand Down
Loading

0 comments on commit 73cf5d8

Please sign in to comment.