|
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 |
27 | 2 | import numpy as np |
28 | 3 | from typing import Callable |
29 | 4 | from copy import deepcopy |
30 | 5 | 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 |
33 | 9 | from collections import defaultdict |
34 | 10 | from cdlib import NodeClustering, FuzzyNodeClustering |
35 | 11 | from cdlib.algorithms.internal.belief_prop import detect_belief_communities |
|
57 | 33 | from cdlib.algorithms.internal.principled import principled |
58 | 34 | from cdlib.algorithms.internal.MCODE import m_code |
59 | 35 | from cdlib.algorithms.internal.RSC import rsc_evaluate_graph |
| 36 | +import warnings |
60 | 37 |
|
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 |
72 | 38 | import markov_clustering as mc |
73 | 39 | from chinese_whispers import chinese_whispers as cw |
74 | 40 | from chinese_whispers import aggregate_clusters |
75 | 41 | from thresholdclustering.thresholdclustering import best_partition as th_best_partition |
76 | | - |
77 | 42 | import networkx as nx |
78 | 43 |
|
79 | 44 | from cdlib.utils import ( |
|
83 | 48 | nx_node_integer_mapping, |
84 | 49 | ) |
85 | 50 |
|
| 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 | + |
86 | 104 | __all__ = [ |
87 | 105 | "louvain", |
88 | 106 | "leiden", |
@@ -509,7 +527,7 @@ def louvain( |
509 | 527 |
|
510 | 528 | g = convert_graph_formats(g_original, nx.Graph) |
511 | 529 |
|
512 | | - coms = louvain_modularity.best_partition( |
| 530 | + coms = community_louvain.best_partition( |
513 | 531 | g, weight=weight, resolution=resolution, randomize=randomize |
514 | 532 | ) |
515 | 533 |
|
@@ -568,12 +586,15 @@ def leiden( |
568 | 586 |
|
569 | 587 | .. note:: Reference implementation: https://github.com/vtraag/leidenalg |
570 | 588 | """ |
571 | | - |
| 589 | + global leidenalg |
572 | 590 | 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 | + ) |
577 | 598 |
|
578 | 599 | g = convert_graph_formats(g_original, ig.Graph) |
579 | 600 |
|
@@ -1049,15 +1070,21 @@ def infomap(g_original: object, flags: str = "") -> NodeClustering: |
1049 | 1070 |
|
1050 | 1071 | .. note:: Infomap Python API documentation: https://mapequation.github.io/infomap/python/ |
1051 | 1072 | """ |
1052 | | - |
| 1073 | + global imp, pipes |
1053 | 1074 | 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 | + ) |
1057 | 1081 | 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 | + ) |
1061 | 1088 |
|
1062 | 1089 | g = convert_graph_formats(g_original, nx.Graph, directed=g_original.is_directed()) |
1063 | 1090 |
|
@@ -1445,12 +1472,15 @@ def sbm_dl( |
1445 | 1472 |
|
1446 | 1473 | .. note:: Implementation from graph-tool library, please report to https://graph-tool.skewed.de for details |
1447 | 1474 | """ |
| 1475 | + |
1448 | 1476 | if gt is None: |
1449 | 1477 | raise Exception( |
1450 | 1478 | "===================================================== \n" |
1451 | 1479 | "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.)" |
1454 | 1484 | ) |
1455 | 1485 | gt_g = convert_graph_formats(g_original, nx.Graph) |
1456 | 1486 | gt_g, label_map = __from_nx_to_graph_tool(gt_g) |
@@ -1498,13 +1528,21 @@ def sbm_dl_nested( |
1498 | 1528 |
|
1499 | 1529 | .. note:: Implementation from graph-tool library, please report to https://graph-tool.skewed.de for details |
1500 | 1530 | """ |
| 1531 | + global gt |
| 1532 | + |
1501 | 1533 | 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 | + |
1508 | 1546 | gt_g = convert_graph_formats(g_original, nx.Graph) |
1509 | 1547 | gt_g, label_map = __from_nx_to_graph_tool(gt_g) |
1510 | 1548 | state = gt.minimize_nested_blockmodel_dl(gt_g) |
@@ -1716,9 +1754,18 @@ def edmot( |
1716 | 1754 |
|
1717 | 1755 | .. note:: Reference implementation: https://karateclub.readthedocs.io/ |
1718 | 1756 | """ |
| 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 | + ) |
1719 | 1766 |
|
1720 | 1767 | 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) |
1722 | 1769 |
|
1723 | 1770 | model.fit(g) |
1724 | 1771 | members = model.get_memberships() |
@@ -2360,8 +2407,17 @@ def gemsec( |
2360 | 2407 |
|
2361 | 2408 | .. note:: Reference implementation: https://karateclub.readthedocs.io/ |
2362 | 2409 | """ |
| 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 | + |
2363 | 2419 | g = convert_graph_formats(g_original, nx.Graph) |
2364 | | - model = GEMSEC( |
| 2420 | + model = karateclub.GEMSEC( |
2365 | 2421 | walk_number=walk_number, |
2366 | 2422 | walk_length=walk_length, |
2367 | 2423 | dimensions=dimensions, |
@@ -2438,8 +2494,18 @@ def scd( |
2438 | 2494 |
|
2439 | 2495 | .. note:: Reference implementation: https://karateclub.readthedocs.io/ |
2440 | 2496 | """ |
| 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 | + |
2441 | 2507 | 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) |
2443 | 2509 | model.fit(g) |
2444 | 2510 | members = model.get_memberships() |
2445 | 2511 |
|
|
0 commit comments