Skip to content

Commit

Permalink
document and test api functions
Browse files Browse the repository at this point in the history
  • Loading branch information
scottgigante committed Nov 17, 2018
1 parent 6252eb1 commit fcc3b43
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 6 deletions.
29 changes: 25 additions & 4 deletions graphtools/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,28 @@ def Graph(data,


def from_igraph(G, **kwargs):
if 'precomputed' in kwargs and kwargs['precomputed'] != 'adjacency':
raise ValueError("Cannot build graph from igraph with precomputed={}. "
"Use 'adjacency' instead.".format(kwargs['precomputed']))
return Graph(sparse.coo_matrix(G.get_adjacency().data), precomputed='adjacency', **kwargs)
"""Convert an igraph.Graph to a graphtools.Graph
Creates a graphtools.graphs.TraditionalGraph with a
precomputed adjacency matrix
Parameters
----------
G : igraph.Graph
Graph to be converted
kwargs
keyword arguments for graphtools.Graph
Returns
-------
G : graphtools.graphs.TraditionalGraph
"""
if 'precomputed' in kwargs:
if kwargs['precomputed'] != 'adjacency':
warnings.warn(
"Cannot build graph from igraph with precomputed={}. "
"Use 'adjacency' instead.".format(kwargs['precomputed']),
UserWarning)
del kwargs['precomputed']
return Graph(sparse.coo_matrix(G.get_adjacency().data),
precomputed='adjacency', **kwargs)
36 changes: 34 additions & 2 deletions graphtools/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -535,9 +535,41 @@ def build_kernel(self):
"""
raise NotImplementedError

def to_pygsp(self):
def to_pygsp(self, **kwargs):
"""Convert to a PyGSP graph
For use only when the user means to create the graph using
the flag `use_pygsp=True`, and doesn't wish to recompute the kernel.
Creates a graphtools.graphs.TraditionalGraph with a precomputed
affinity matrix which also inherits from pygsp.graphs.Graph.
Parameters
----------
kwargs
keyword arguments for graphtools.Graph
Returns
-------
G : graphtools.base.PyGSPGraph, graphtools.graphs.TraditionalGraph
"""
from . import api
return api.Graph(self.K, precomputed="affinity", use_pygsp=True)
if 'precomputed' in kwargs:
if kwargs['precomputed'] != 'affinity':
warnings.warn(
"Cannot build PyGSPGraph with precomputed={}. "
"Using 'affinity' instead.".format(kwargs['precomputed']),
UserWarning)
del kwargs['precomputed']
if 'use_pygsp' in kwargs:
if kwargs['use_pygsp'] is not True:
warnings.warn(
"Cannot build PyGSPGraph with use_pygsp={}. "
"Use True instead.".format(kwargs['use_pygsp']),
UserWarning)
del kwargs['use_pygsp']
return api.Graph(self.K,
precomputed="affinity", use_pygsp=True,
**kwargs)


class PyGSPGraph(with_metaclass(abc.ABCMeta, pygsp.graphs.Graph, Base)):
Expand Down
25 changes: 25 additions & 0 deletions test/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
data,
build_graph,
raises,
warns,
)
import warnings

Expand All @@ -24,12 +25,36 @@ def test_from_igraph():
assert np.all(G.K == G2.K)


@warns(UserWarning)
def test_from_igraph_invalid_precomputed():
n = 100
m = 500
K = np.zeros((n, n))
for _ in range(m):
e = np.random.choice(n, 2, replace=False)
K[e[0], e[1]] = K[e[1], e[0]] = 1
g = igraph.Graph.Adjacency(K.tolist())
G = graphtools.from_igraph(g, precomputed='affinity')


def test_to_pygsp():
G = build_graph(data)
G2 = G.to_pygsp()
assert isinstance(G2, graphtools.graphs.PyGSPGraph)
assert np.all(G2.K == G.K)


@warns(UserWarning)
def test_to_pygsp_invalid_precomputed():
G = build_graph(data)
G2 = G.to_pygsp(precomputed='adjacency')


@warns(UserWarning)
def test_to_pygsp_invalid_use_pygsp():
G = build_graph(data)
G2 = G.to_pygsp(use_pygsp=False)

#####################################################
# Check parameters
#####################################################
Expand Down

0 comments on commit fcc3b43

Please sign in to comment.