Skip to content

Commit 40f3d20

Browse files
Merge pull request #203 from GiulioRossetti/light_version
Light version
2 parents 2f99722 + f5d8376 commit 40f3d20

File tree

11 files changed

+265
-122
lines changed

11 files changed

+265
-122
lines changed

cdlib/algorithms/bipartite_clustering.py

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
11
from cdlib import BiNodeClustering
2-
32
import networkx as nx
3+
from cdlib.utils import convert_graph_formats
4+
from collections import defaultdict
5+
from cdlib.algorithms.internal.pycondor import condor_object, initial_community, brim
6+
7+
missing_packages = set()
48

59
try:
610
import infomap as imp
711
except ModuleNotFoundError:
12+
missing_packages.add("infomap")
813
imp = None
914

1015
try:
1116
from wurlitzer import pipes
1217
except ModuleNotFoundError:
18+
missing_packages.add("wurlitzer")
1319
pipes = None
1420

1521
try:
@@ -20,11 +26,14 @@
2026
try:
2127
import leidenalg
2228
except ModuleNotFoundError:
29+
missing_packages.add("leidenalg")
2330
leidenalg = None
2431

25-
from cdlib.utils import convert_graph_formats
26-
from collections import defaultdict
27-
from cdlib.algorithms.internal.pycondor import condor_object, initial_community, brim
32+
if len(missing_packages) > 0:
33+
print(
34+
"Note: to be able to use all bipartite methods, you need to install some additional packages: ",
35+
missing_packages,
36+
)
2837

2938
__all__ = ["bimlpa", "CPM_Bipartite", "infomap_bipartite", "condor"]
3039

@@ -123,11 +132,15 @@ def CPM_Bipartite(
123132
124133
.. note:: Reference implementation: https://leidenalg.readthedocs.io/en/stable/multiplex.html?highlight=bipartite#bipartite
125134
"""
135+
global leidenalg
126136
if ig is None or leidenalg is None:
127-
raise ModuleNotFoundError(
128-
"Optional dependency not satisfied: install igraph and leidenalg to use the "
129-
"selected feature."
130-
)
137+
try:
138+
import leidenalg
139+
except ModuleNotFoundError:
140+
raise ModuleNotFoundError(
141+
"Optional dependency not satisfied: install igraph and leidenalg to use the "
142+
"selected feature."
143+
)
131144

132145
g = convert_graph_formats(g_original, ig.Graph)
133146

@@ -201,14 +214,21 @@ def infomap_bipartite(g_original: object, flags: str = "") -> BiNodeClustering:
201214
.. note:: Infomap Python API documentation: https://mapequation.github.io/infomap/python/
202215
"""
203216

217+
global imp, pipes
204218
if imp is None:
205-
raise ModuleNotFoundError(
206-
"Optional dependency not satisfied: install infomap to use the selected feature."
207-
)
219+
try:
220+
import infomap as imp
221+
except ModuleNotFoundError:
222+
raise ModuleNotFoundError(
223+
"Optional dependency not satisfied: install infomap to use the selected feature."
224+
)
208225
if pipes is None:
209-
raise ModuleNotFoundError(
210-
"Optional dependency not satisfied: install package wurlitzer to use infomap."
211-
)
226+
try:
227+
from wurlitzer import pipes
228+
except ModuleNotFoundError:
229+
raise ModuleNotFoundError(
230+
"Optional dependency not satisfied: install package wurlitzer to use infomap."
231+
)
212232

213233
g = convert_graph_formats(g_original, nx.Graph)
214234

cdlib/algorithms/crisp_partition.py

Lines changed: 130 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,11 @@
1-
try:
2-
import infomap as imp
3-
except ModuleNotFoundError:
4-
imp = None
5-
6-
try:
7-
from wurlitzer import pipes
8-
except ModuleNotFoundError:
9-
pipes = None
10-
11-
try:
12-
import igraph as ig
13-
except ModuleNotFoundError:
14-
ig = None
15-
16-
try:
17-
import leidenalg
18-
except ModuleNotFoundError:
19-
leidenalg = None
20-
21-
try:
22-
import graph_tool.all as gt
23-
except ModuleNotFoundError:
24-
gt = None
25-
26-
import warnings
1+
import sys
272
import numpy as np
283
from typing import Callable
294
from copy import deepcopy
305
from cdlib.algorithms.internal import DER
31-
import community as louvain_modularity
32-
import warnings
6+
7+
# import community as louvain_modularity
8+
from community import community_louvain
339
from collections import defaultdict
3410
from cdlib import NodeClustering, FuzzyNodeClustering
3511
from cdlib.algorithms.internal.belief_prop import detect_belief_communities
@@ -57,23 +33,12 @@
5733
from cdlib.algorithms.internal.principled import principled
5834
from cdlib.algorithms.internal.MCODE import m_code
5935
from cdlib.algorithms.internal.RSC import rsc_evaluate_graph
36+
import warnings
6037

61-
try:
62-
from GraphRicciCurvature.OllivierRicci import OllivierRicci
63-
except ModuleNotFoundError:
64-
OllivierRicci = None
65-
66-
try:
67-
import pycombo as pycombo_part
68-
except ModuleNotFoundError:
69-
pycombo_part = None
70-
71-
from karateclub import EdMot, GEMSEC, SCD
7238
import markov_clustering as mc
7339
from chinese_whispers import chinese_whispers as cw
7440
from chinese_whispers import aggregate_clusters
7541
from thresholdclustering.thresholdclustering import best_partition as th_best_partition
76-
7742
import networkx as nx
7843

7944
from cdlib.utils import (
@@ -83,6 +48,59 @@
8348
nx_node_integer_mapping,
8449
)
8550

51+
missing_packages = set()
52+
53+
try:
54+
import infomap as imp
55+
except ModuleNotFoundError:
56+
missing_packages.add("infomap")
57+
imp = None
58+
59+
try:
60+
from wurlitzer import pipes
61+
except ModuleNotFoundError:
62+
missing_packages.add("wurlitzer")
63+
pipes = None
64+
65+
try:
66+
import igraph as ig
67+
except ModuleNotFoundError:
68+
ig = None
69+
70+
try:
71+
import leidenalg
72+
except ModuleNotFoundError:
73+
missing_packages.add("leidenalg")
74+
leidenalg = None
75+
76+
try:
77+
import graph_tool.all as gt
78+
except ModuleNotFoundError:
79+
missing_packages.add("graph_tool")
80+
gt = None
81+
82+
try:
83+
import karateclub
84+
except ModuleNotFoundError:
85+
missing_packages.add("karateclub")
86+
87+
88+
if len(missing_packages) > 0:
89+
print(
90+
"Note: to be able to use all crisp methods, you need to install some additional packages: ",
91+
missing_packages,
92+
)
93+
94+
try:
95+
from GraphRicciCurvature.OllivierRicci import OllivierRicci
96+
except ModuleNotFoundError:
97+
OllivierRicci = None
98+
99+
try:
100+
import pycombo as pycombo_part
101+
except ModuleNotFoundError:
102+
pycombo_part = None
103+
86104
__all__ = [
87105
"louvain",
88106
"leiden",
@@ -509,7 +527,7 @@ def louvain(
509527

510528
g = convert_graph_formats(g_original, nx.Graph)
511529

512-
coms = louvain_modularity.best_partition(
530+
coms = community_louvain.best_partition(
513531
g, weight=weight, resolution=resolution, randomize=randomize
514532
)
515533

@@ -568,12 +586,15 @@ def leiden(
568586
569587
.. note:: Reference implementation: https://github.com/vtraag/leidenalg
570588
"""
571-
589+
global leidenalg
572590
if ig is None or leidenalg is None:
573-
raise ModuleNotFoundError(
574-
"Optional dependency not satisfied: install igraph and leidenalg to use the "
575-
"selected feature."
576-
)
591+
try:
592+
import leidenalg
593+
except ModuleNotFoundError:
594+
raise ModuleNotFoundError(
595+
"Optional dependency not satisfied: install igraph and leidenalg to use the "
596+
"selected feature."
597+
)
577598

578599
g = convert_graph_formats(g_original, ig.Graph)
579600

@@ -1049,15 +1070,21 @@ def infomap(g_original: object, flags: str = "") -> NodeClustering:
10491070
10501071
.. note:: Infomap Python API documentation: https://mapequation.github.io/infomap/python/
10511072
"""
1052-
1073+
global imp, pipes
10531074
if imp is None:
1054-
raise ModuleNotFoundError(
1055-
"Optional dependency not satisfied: install infomap to use the selected feature."
1056-
)
1075+
try:
1076+
import infomap as imp
1077+
except ModuleNotFoundError:
1078+
raise ModuleNotFoundError(
1079+
"Optional dependency not satisfied: install infomap to use the selected feature."
1080+
)
10571081
if pipes is None:
1058-
raise ModuleNotFoundError(
1059-
"Optional dependency not satisfied: install package wurlitzer to use infomap."
1060-
)
1082+
try:
1083+
from wurlitzer import pipes
1084+
except ModuleNotFoundError:
1085+
raise ModuleNotFoundError(
1086+
"Optional dependency not satisfied: install package wurlitzer to use infomap."
1087+
)
10611088

10621089
g = convert_graph_formats(g_original, nx.Graph, directed=g_original.is_directed())
10631090

@@ -1445,12 +1472,15 @@ def sbm_dl(
14451472
14461473
.. note:: Implementation from graph-tool library, please report to https://graph-tool.skewed.de for details
14471474
"""
1475+
14481476
if gt is None:
14491477
raise Exception(
14501478
"===================================================== \n"
14511479
"The graph-tool library seems not to be installed (or incorrectly installed). \n"
1452-
"Please check installation procedure there https://git.skewed.de/count0/graph-tool/wikis/installation-instructions#native-installation \n"
1453-
"on linux/mac, you can use package managers to do so(apt-get install python3-graph-tool, brew install graph-tool, etc.)"
1480+
"Please check installation procedure there "
1481+
"https://git.skewed.de/count0/graph-tool/wikis/installation-instructions#native-installation \n"
1482+
"on linux/mac, you can use package managers to do so "
1483+
"(apt-get install python3-graph-tool, brew install graph-tool, etc.)"
14541484
)
14551485
gt_g = convert_graph_formats(g_original, nx.Graph)
14561486
gt_g, label_map = __from_nx_to_graph_tool(gt_g)
@@ -1498,13 +1528,21 @@ def sbm_dl_nested(
14981528
14991529
.. note:: Implementation from graph-tool library, please report to https://graph-tool.skewed.de for details
15001530
"""
1531+
global gt
1532+
15011533
if gt is None:
1502-
raise Exception(
1503-
"===================================================== \n"
1504-
"The graph-tool library seems not to be installed (or incorrectly installed). \n"
1505-
"Please check installation procedure there https://git.skewed.de/count0/graph-tool/wikis/installation-instructions#native-installation \n"
1506-
"on linux/mac, you can use package managers to do so(apt-get install python3-graph-tool, brew install graph-tool, etc.)"
1507-
)
1534+
try:
1535+
import graph_tool.all as gt
1536+
except ModuleNotFoundError:
1537+
raise Exception(
1538+
"===================================================== \n"
1539+
"The graph-tool library seems not to be installed (or incorrectly installed). \n"
1540+
"Please check installation procedure "
1541+
"there https://git.skewed.de/count0/graph-tool/wikis/installation-instructions#native-installation \n"
1542+
"on linux/mac, you can use package managers to do so(apt-get install python3-graph-tool, "
1543+
"brew install graph-tool, etc.)"
1544+
)
1545+
15081546
gt_g = convert_graph_formats(g_original, nx.Graph)
15091547
gt_g, label_map = __from_nx_to_graph_tool(gt_g)
15101548
state = gt.minimize_nested_blockmodel_dl(gt_g)
@@ -1716,9 +1754,18 @@ def edmot(
17161754
17171755
.. note:: Reference implementation: https://karateclub.readthedocs.io/
17181756
"""
1757+
global karateclub
1758+
1759+
if "karateclub" not in sys.modules:
1760+
try:
1761+
import karateclub
1762+
except ModuleNotFoundError:
1763+
raise ModuleNotFoundError(
1764+
"Optional dependency not satisfied: install karateclub to use the selected feature."
1765+
)
17191766

17201767
g = convert_graph_formats(g_original, nx.Graph)
1721-
model = EdMot(component_count=2, cutoff=10)
1768+
model = karateclub.EdMot(component_count=2, cutoff=10)
17221769

17231770
model.fit(g)
17241771
members = model.get_memberships()
@@ -2360,8 +2407,17 @@ def gemsec(
23602407
23612408
.. note:: Reference implementation: https://karateclub.readthedocs.io/
23622409
"""
2410+
global karateclub
2411+
if "karateclub" not in sys.modules:
2412+
try:
2413+
import karateclub
2414+
except ModuleNotFoundError:
2415+
raise ModuleNotFoundError(
2416+
"Optional dependency not satisfied: install karateclub to use the selected feature."
2417+
)
2418+
23632419
g = convert_graph_formats(g_original, nx.Graph)
2364-
model = GEMSEC(
2420+
model = karateclub.GEMSEC(
23652421
walk_number=walk_number,
23662422
walk_length=walk_length,
23672423
dimensions=dimensions,
@@ -2438,8 +2494,18 @@ def scd(
24382494
24392495
.. note:: Reference implementation: https://karateclub.readthedocs.io/
24402496
"""
2497+
global karateclub
2498+
2499+
if "karateclub" not in sys.modules:
2500+
try:
2501+
import karateclub
2502+
except ModuleNotFoundError:
2503+
raise ModuleNotFoundError(
2504+
"Optional dependency not satisfied: install karateclub to use the selected feature."
2505+
)
2506+
24412507
g = convert_graph_formats(g_original, nx.Graph)
2442-
model = SCD(iterations=iterations, eps=eps, seed=seed)
2508+
model = karateclub.SCD(iterations=iterations, eps=eps, seed=seed)
24432509
model.fit(g)
24442510
members = model.get_memberships()
24452511

0 commit comments

Comments
 (0)