Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Basic graph generation using hypothesis, compare outputs with NX #18

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:
- name: Install dependencies
run: |
conda install -c conda-forge python-graphblas scipy pandas \
pytest-cov pytest-randomly black flake8-comprehensions flake8-bugbear
pytest-cov pytest-randomly black flake8-comprehensions flake8-bugbear hypothesis
# matplotlib lxml pygraphviz pydot sympy # Extra networkx deps we don't need yet
pip install git+https://github.com/networkx/networkx.git@main --no-deps
pip install -e . --no-deps
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
import pytest
from hypothesis import given, settings

from graphblas_algorithms import pagerank
from graphblas_algorithms.utils import custom_graph_generators


@settings(deadline=None, max_examples=500)
@given(custom_graph_generators())
def test_pagerank_gen(graph):
assert pagerank(graph) == pytest.approx(nx.pagerank(graph))


from networkx.algorithms.link_analysis.tests.test_pagerank import * # isort:skip
19 changes: 19 additions & 0 deletions graphblas_algorithms/algorithms/tests/test_reciprocity.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
from graphblas_algorithms import overall_reciprocity, reciprocity
import pytest
from hypothesis import given, settings
from graphblas_algorithms.utils import custom_graph_generators


@settings(deadline=None, max_examples=500)
@given(custom_graph_generators())
def test_overall_reciprocity_gen(graph):
# TODO: Fix the graph gen to not produce empty graphs and control directions
if len(graph) > 0 and graph.is_directed():
assert overall_reciprocity(graph) == pytest.approx(nx.overall_reciprocity(graph))


@settings(deadline=None, max_examples=500)
@given(custom_graph_generators())
def test_reciprocity_gen(graph):
if len(graph) > 0 and graph.is_directed():
assert reciprocity(graph) == pytest.approx(nx.reciprocity(graph))


from networkx.algorithms.tests.test_reciprocity import * # isort:skip
1 change: 1 addition & 0 deletions graphblas_algorithms/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
from ._misc import *
from .decorators import *
from .graph_gen import *
30 changes: 30 additions & 0 deletions graphblas_algorithms/utils/graph_gen.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import networkx as nx
from hypothesis.strategies import booleans, composite, integers, lists, tuples


@composite
def custom_graph_generators(
draw,
directed=booleans(),
self_loops=booleans(),
sym_digraph=booleans(),
edges=tuples(integers(0, 100), integers(0, 100), integers(-100, 100)),
edge_data=booleans(),
):
G = nx.DiGraph() if draw(directed) else nx.Graph()
self_loop = draw(self_loops)
edges = draw(lists(edges, max_size=1000))
edge_data = draw(edge_data)
for u, v, d in edges:
if not self_loop and u == v:
continue
if edge_data:
G.add_edge(u, v, data=d)
else:
G.add_edge(u, v)
if G.is_directed():
sym_digraph = draw(sym_digraph)
if sym_digraph:
G = G.to_undirected()
G = G.to_directed()
return G