Skip to content
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
41 changes: 41 additions & 0 deletions cdlib/test/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,21 @@ def get_string_graph():
return g


# networkx nodes just need to be Hashable:
# this class is used in a test below that verifies
# that such graphs can be converted into igraph
class Node:
def __init__(self, id, type):
self.id = id
self.type = type

def __hash__(self):
return hash(self.id)

def __eq__(self, other):
return self.id == other.id


class UtilsTests(unittest.TestCase):
def test_convert(self):
g = nx.karate_club_graph()
Expand All @@ -36,6 +51,32 @@ def test_convert(self):
g3 = utils.convert_graph_formats(ign, nx.Graph, directed=True)
self.assertIsInstance(g3, nx.DiGraph)

def test_convert_bipartite(self):
john = Node('John Travolta', 'actor')
nick = Node('Nick Cage', 'actor')
face_off = Node('Face Off', 'movie')

g = nx.Graph()
g.add_node(john)
g.add_node(nick)
g.add_edge(john, face_off, label='ACTED_IN')
g.add_edge(nick, face_off, label='ACTED_IN')

if ig is not None:
ign = utils.convert_graph_formats(g, ig.Graph)
self.assertEqual(g.number_of_nodes(), ign.vcount())
self.assertEqual(g.number_of_edges(), ign.ecount())

g2 = utils.convert_graph_formats(ign, nx.Graph)
self.assertEqual(g.number_of_nodes(), g2.number_of_nodes())
self.assertEqual(g.number_of_edges(), g2.number_of_edges())

g3 = utils.convert_graph_formats(g, nx.Graph)
self.assertEqual(isinstance(g, nx.Graph), isinstance(g3, nx.Graph))

g3 = utils.convert_graph_formats(ign, nx.Graph, directed=True)
self.assertIsInstance(g3, nx.DiGraph)

def test_nx_integer_mapping(self):
g = nx.karate_club_graph()
g, node_map = utils.nx_node_integer_mapping(g)
Expand Down
3 changes: 2 additions & 1 deletion cdlib/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,9 @@ def __from_nx_to_igraph(g: object, directed: bool = None) -> object:
gi.add_edges([("\\" + str(u), "\\" + str(v)) for (u, v) in g.edges()])

if bipartite.is_bipartite(g) and not skip_bipartite:
convert = {str(x):x for x in g.nodes()}
gi.vs["type"] = [
a_r[name] if type(name) == int else a_r[int(name.replace("\\", ""))]
a_r[name] if type(name) == int else a_r[convert[name.replace("\\", "")]]
for name in gi.vs["name"]
]

Expand Down