Skip to content

More changes needed for NetworkX plugin #25

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

Merged
merged 5 commits into from
Nov 1, 2022
Merged
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 @@ -33,7 +33,7 @@ jobs:
conda install -c conda-forge python-graphblas scipy pandas \
pytest-cov pytest-randomly black flake8-comprehensions flake8-bugbear
# matplotlib lxml pygraphviz pydot sympy # Extra networkx deps we don't need yet
pip install git+https://github.com/mriduls/networkx.git@nx-sparse --no-deps
pip install git+https://github.com/jim22k/networkx.git@nx-sparse --no-deps
pip install -e . --no-deps
- name: Style checks
run: |
Expand Down
4 changes: 3 additions & 1 deletion graphblas_algorithms/classes/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import numpy as np
from graphblas import Matrix, Vector, binary
from graphblas.core.matrix import TransposedMatrix
from graphblas.core.utils import ensure_type

################
# Classmethods #
Expand All @@ -19,7 +20,8 @@ def from_networkx(cls, G, weight=None, dtype=None):


def from_graphblas(cls, A, *, key_to_id=None):
# Does not copy!
# Does not copy if A is a Matrix!
A = ensure_type(A, Matrix)
if A.nrows != A.ncols:
raise ValueError(f"Adjacency matrix must be square; got {A.nrows} x {A.ncols}")
rv = cls()
Expand Down
8 changes: 6 additions & 2 deletions graphblas_algorithms/classes/digraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -414,8 +414,10 @@ def to_directed_graph(G, weight=None, dtype=None):
# We should do some sanity checks here to ensure we're returning a valid directed graph
if isinstance(G, DiGraph):
return G
if isinstance(G, Matrix):
try:
return DiGraph.from_graphblas(G)
except TypeError:
pass

try:
import networkx as nx
Expand All @@ -431,9 +433,11 @@ def to_directed_graph(G, weight=None, dtype=None):
def to_graph(G, weight=None, dtype=None):
if isinstance(G, (DiGraph, ga.Graph)):
return G
if isinstance(G, Matrix):
try:
# Should we check if it can be undirected?
return DiGraph.from_graphblas(G)
except TypeError:
pass

try:
import networkx as nx
Expand Down
4 changes: 3 additions & 1 deletion graphblas_algorithms/classes/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,10 @@ def to_undirected_graph(G, weight=None, dtype=None):
# We should do some sanity checks here to ensure we're returning a valid undirected graph
if isinstance(G, Graph):
return G
if isinstance(G, Matrix):
try:
return Graph.from_graphblas(G)
except TypeError:
pass

try:
import networkx as nx
Expand Down
10 changes: 9 additions & 1 deletion graphblas_algorithms/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class Dispatcher:
is_triad = nxapi.triads.is_triad

@staticmethod
def convert(graph, weight=None):
def convert_from_nx(graph, weight=None, *, name=None):
import networkx as nx

from .classes import DiGraph, Graph, MultiDiGraph, MultiGraph
Expand All @@ -83,6 +83,14 @@ def convert(graph, weight=None):
return Graph.from_networkx(graph, weight=weight)
raise TypeError(f"Unsupported type of graph: {type(graph)}")

@staticmethod
def convert_to_nx(obj, *, name=None):
from .classes import Graph

if isinstance(obj, Graph):
obj = obj.to_networkx()
Comment on lines +87 to +91
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we check isinstance(obj, Matrix) and convert it to a scipy sparse array if scipy is installed?

We can also merge this PR and update things later.

return obj

@staticmethod
def on_start_tests(items):
skip = [
Expand Down
4 changes: 1 addition & 3 deletions graphblas_algorithms/nxapi/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,4 @@
def k_truss(G, k):
G = to_undirected_graph(G, dtype=bool)
result = algorithms.k_truss(G, k)
# TODO: don't convert to networkx graph
# We want to be able to pass networkx tests, so we need to improve our graph objects
return result.to_networkx()
return result