Skip to content

Commit

Permalink
add from_igraph and to_pygsp methods
Browse files Browse the repository at this point in the history
  • Loading branch information
scottgigante committed Nov 17, 2018
1 parent 411b7ba commit d63007b
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 2 deletions.
2 changes: 1 addition & 1 deletion graphtools/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
from .api import Graph
from .api import Graph, from_igraph
from .version import __version__
8 changes: 8 additions & 0 deletions graphtools/api.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import numpy as np
import warnings
import tasklogger
from scipy import sparse

from . import base
from . import graphs
Expand Down Expand Up @@ -222,3 +223,10 @@ def Graph(data,
for key, value in params.items()
if key != "data"])))
return Graph(**params)


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)
5 changes: 5 additions & 0 deletions graphtools/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
elementwise_maximum,
set_diagonal)


class Base(object):
"""Class that deals with key-word arguments but is otherwise
just an object.
Expand Down Expand Up @@ -534,6 +535,10 @@ def build_kernel(self):
"""
raise NotImplementedError

def to_pygsp(self):
from . import api
return api.Graph(self.K, precomputed="affinity", use_pygsp=True)


class PyGSPGraph(with_metaclass(abc.ABCMeta, pygsp.graphs.Graph, Base)):
"""Interface between BaseGraph and PyGSP.
Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
'nose2',
'pandas',
'coverage',
'coveralls'
'coveralls',
'python-igraph'
]

if sys.version_info[0] == 3:
Expand Down
22 changes: 22 additions & 0 deletions test/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,30 @@
build_graph,
raises,
)
import igraph
import numpy as np
import graphtools


def test_from_igraph():
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)
G2 = graphtools.Graph(K, precomputed='adjacency')
assert np.all(G.K == G2.K)


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)

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

0 comments on commit d63007b

Please sign in to comment.