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 2 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
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 @@ -69,7 +69,7 @@ class Dispatcher:
is_triad = nxapi.triads.is_triad

@staticmethod
def convert(graph, weight=None):
def convert_from_nx(graph, weight=None, *, name):
from .classes import DiGraph, Graph, MultiDiGraph, MultiGraph

if isinstance(graph, nx.MultiDiGraph):
Expand All @@ -82,6 +82,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):
from .classes import Graph

if isinstance(obj, Graph):
obj = obj.to_networkx()
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