Skip to content

Commit

Permalink
🐛 fix bayanpy test
Browse files Browse the repository at this point in the history
  • Loading branch information
GiulioRossetti committed Jun 6, 2023
1 parent 14d991c commit 8c47d8a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 18 deletions.
16 changes: 14 additions & 2 deletions cdlib/algorithms/crisp_partition.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from cdlib.algorithms.internal import DER

from community import community_louvain
import bayanpy

from collections import defaultdict
from cdlib import NodeClustering, FuzzyNodeClustering
Expand Down Expand Up @@ -86,6 +85,12 @@
missing_packages.add("graph_tool")
gt = None

try:
import bayanpy as by
except ModuleNotFoundError:
missing_packages.add("bayanpy")
by = None

# try:
# import karateclub
# except ModuleNotFoundError:
Expand Down Expand Up @@ -2950,9 +2955,16 @@ def bayan(
Aref, Samin, Hriday Chheda, and Mahdi Mostajabdaveh. "The Bayan Algorithm: Detecting Communities in Networks Through Exact and Approximate Optimization of Modularity." arXiv preprint arXiv:2209.04562 (2022).
"""

if by is None:
raise Exception(
"===================================================== \n"
"The bayan algorithm seems not to be installed (or incorrectly installed). \n"
"Please resolve with: pip install bayanpy"
)

g = convert_graph_formats(g_original, nx.Graph)

_, _, community, _, _ = bayanpy.bayan(
_, _, community, _, _ = by.bayan(
g, threshold=threshold, time_allowed=time_allowed, resolution=resolution
)

Expand Down
35 changes: 19 additions & 16 deletions cdlib/test/test_community_discovery_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@
grc = None


try:
import bayanpy as by
except ModuleNotFoundError:
by = None


def get_string_graph():
g = nx.karate_club_graph()
node_map = {}
Expand Down Expand Up @@ -1037,21 +1043,18 @@ def test_rsc(self):
self.assertEqual(type(coms.communities[0][0]), int)

def test_bayan(self):
try:
import gurobipy as gp
except ModuleNotFoundError:
return

G = nx.Graph()
G.add_edge(0, 1)
G.add_edge(0, 2)
G.add_edge(0, 3)
G.add_edge(1, 2)
G.add_edge(1, 3)
G.add_edge(2, 3)
if by is not None:

coms = algorithms.bayan(G)
self.assertEqual(type(coms.communities), list)
if len(coms.communities) > 0:
self.assertEqual(type(coms.communities[0]), list)
self.assertEqual(type(coms.communities[0][0]), int)
try:
import gurobipy as gp
except ModuleNotFoundError:
return

G = nx.florentine_families_graph()

coms = algorithms.bayan(G)
self.assertEqual(type(coms.communities), list)
if len(coms.communities) > 0:
self.assertEqual(type(coms.communities[0]), list)
self.assertEqual(type(coms.communities[0][0]), str)

0 comments on commit 8c47d8a

Please sign in to comment.