Skip to content

Commit

Permalink
Increase coverage to 100 (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
arnaudon authored Oct 13, 2022
1 parent 6f18c2b commit a54eedc
Show file tree
Hide file tree
Showing 18 changed files with 2,018 additions and 183 deletions.
1 change: 1 addition & 0 deletions .github/workflows/run-tox.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ jobs:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
sudo apt-get install -y poppler-utils imagemagick
python -m pip install --upgrade pip setuptools
pip install tox-gh-actions pybind11
- name: Run tox
Expand Down
9 changes: 0 additions & 9 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,12 +1,3 @@
*.pkl
*.gpickle
*.svg
*.png
*.pdf
*.csv
*.html
examples/communities

# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
Expand Down
3 changes: 1 addition & 2 deletions examples/run_simple_example.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@ pygenstability run \
--n-louvain 100 \
--n-workers 40 \
edges.csv
# sbm_graph.pkl

pygenstability plot_scan --help
pygenstability plot_scan results.pkl

pygenstability plot_communities --help
pygenstability plot_communities sbm_graph.gpickle results.pkl
pygenstability plot_communities edges.csv results.pkl
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"pytest",
"pytest-cov",
"pytest-html",
"diff-pdf-visually",
]

setup(
Expand Down
99 changes: 51 additions & 48 deletions src/pygenstability/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from pathlib import Path

import click
import networkx as nx
import numpy as np
import pandas as pd
from scipy import sparse as sp
Expand All @@ -13,6 +14,26 @@
from pygenstability.plotting import plot_scan as _plot_scan


def _load_graph(graph_file):
try:
# load pickle file
if Path(graph_file).suffix == ".pkl":
with open(graph_file, "rb") as pickle_file: # pragma: no cover
graph = pickle.load(pickle_file)
else:
# load text file with edge list
edges = pd.read_csv(graph_file)
n_nodes = len(np.unique(edges[edges.columns[:2]].to_numpy().flatten()))
# pylint: disable=unsubscriptable-object,no-member
graph = sp.csr_matrix(
(edges[edges.columns[2]], tuple(edges[edges.columns[:2]].to_numpy().T)),
shape=(n_nodes, n_nodes),
)
except Exception as exc: # pragma: no cover
raise Exception("Could not load the graph file.") from exc
return graph


@click.group()
def cli():
"""App initialisation."""
Expand All @@ -27,23 +48,23 @@ def cli():
help="Name of the quality constructor.",
)
@click.option(
"--min-time",
"--min-scale",
default=-2.0,
show_default=True,
help="Minimum Markov time.",
help="Minimum scale.",
)
@click.option(
"--max-time",
"--max-scale",
default=0.5,
show_default=True,
help="Maximum Markov time.",
help="Maximum scale.",
)
@click.option("--n-time", default=20, show_default=True, help="Number of time steps.")
@click.option("--n-scale", default=20, show_default=True, help="Number of scale steps.")
@click.option(
"--log-time",
"--log-scale",
default=True,
show_default=True,
help="Use linear or log scales for times.",
help="Use linear or log scales.",
)
@click.option(
"--n-louvain",
Expand All @@ -52,16 +73,16 @@ def cli():
help="Number of Louvain evaluations.",
)
@click.option(
"--VI/--no-VI",
"--NVI/--no-NVI",
default=True,
show_default=True,
help="Compute the variation of information between Louvain runs.",
help="Compute the normalized variation of information between Louvain runs.",
)
@click.option(
"--n-louvain-VI",
"--n-louvain-NVI",
default=20,
show_default=True,
help="Number of randomly chosen Louvain run to estimate the VI.",
help="Number of randomly chosen Louvain run to estimate the NVI.",
)
@click.option(
"--postprocessing/--no-postprocessing",
Expand All @@ -79,7 +100,7 @@ def cli():
"--spectral-gap/--no-spectral-gap",
default=True,
show_default=True,
help="Normalize time by spectral gap.",
help="Normalize scale by spectral gap.",
)
@click.option(
"--result-file",
Expand All @@ -97,13 +118,13 @@ def cli():
def run(
graph_file,
constructor,
min_time,
max_time,
n_time,
log_time,
min_scale,
max_scale,
n_scale,
log_scale,
n_louvain,
vi,
n_louvain_vi,
nvi,
n_louvain_nvi,
postprocessing,
ttprime,
spectral_gap,
Expand All @@ -120,33 +141,17 @@ def run(
See https://barahona-research-group.github.io/PyGenStability/ for more information.
"""
try:
# load pickle file
if Path(graph_file).suffix == ".pkl":
with open(graph_file, "rb") as pickle_file:
graph = pickle.load(pickle_file)
else:
# load text file with edge list
edges = pd.read_csv(graph_file)
n_nodes = len(np.unique(edges[edges.columns[:2]].to_numpy().flatten()))
# pylint: disable=unsubscriptable-object,no-member
graph = sp.csr_matrix(
(edges[edges.columns[2]], tuple(edges[edges.columns[:2]].to_numpy().T)),
shape=(n_nodes, n_nodes),
)
except Exception as exc:
raise Exception("Could not load the graph file.") from exc

graph = _load_graph(graph_file)
_run(
graph,
constructor=constructor,
min_time=min_time,
max_time=max_time,
n_time=n_time,
log_time=log_time,
min_scale=min_scale,
max_scale=max_scale,
n_scale=n_scale,
log_scale=log_scale,
n_louvain=n_louvain,
with_VI=vi,
n_louvain_VI=n_louvain_vi,
with_NVI=nvi,
n_louvain_NVI=n_louvain_nvi,
with_postprocessing=postprocessing,
with_ttprime=ttprime,
with_spectral_gap=spectral_gap,
Expand All @@ -166,11 +171,9 @@ def plot_scan(results_file):
@cli.command("plot_communities")
@click.argument("graph_file", type=click.Path(exists=True))
@click.argument("results_file", type=click.Path(exists=True))
def plot_communities(results_file, graph_file):
"""Plot communities on networkx graph.
Argument graph_file has to be a .gpickle compatible with network.
"""
with open(graph_file, "rb") as pickle_file:
graph = pickle.load(pickle_file)
def plot_communities(graph_file, results_file):
"""Plot communities on networkx graph."""
graph = _load_graph(graph_file)
if not isinstance(graph, nx.Graph):
graph = nx.from_scipy_sparse_array(graph)
_plot_communities(graph, load_results(results_file))
8 changes: 4 additions & 4 deletions src/pygenstability/contrib/sankey.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Sankey diagram plots."""
import numpy as np
import plotly.graph_objects as go
from plotly.offline import plot
import numpy as np # pragma: no cover
import plotly.graph_objects as go # pragma: no cover
from plotly.offline import plot # pragma: no cover


def plot_sankey(
Expand All @@ -10,7 +10,7 @@ def plot_sankey(
live=False,
filename="communities_sankey.html",
time_index=None,
):
): # pragma: no cover
"""Plot Sankey diagram of communities accros time (plotly only).
Args:
Expand Down
2 changes: 1 addition & 1 deletion src/pygenstability/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def save_results(all_results, filename="results.pkl"):
pickle.dump(all_results, results_file)


def load_results(filename="results.pkl"):
def load_results(filename="results.pkl"): # pragma: no cover
"""Load results from a pickle."""
with open(filename, "rb") as results_file:
return pickle.load(results_file)
Loading

0 comments on commit a54eedc

Please sign in to comment.