Skip to content
Merged
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
18 changes: 14 additions & 4 deletions cdlib/algorithms/crisp_partition.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ def girvan_newman(g_original: object, level: int) -> NodeClustering:
========== ======== ========

:param g_original: a networkx/igraph object
:param level: the level where to cut the dendrogram
:param level: the level where to cut the dendrogram (-1 for highest modularity)
:return: NodeClustering object

:Example:
Expand All @@ -194,8 +194,18 @@ def girvan_newman(g_original: object, level: int) -> NodeClustering:

gn_hierarchy = nx.algorithms.community.girvan_newman(g)
coms = []
for _ in range(level):
coms = next(gn_hierarchy)
if level==-1:
max_modularity = -float('inf')

for current_coms in gn_hierarchy:
mod = nx.algorithms.community.modularity(g, current_coms)
if mod > max_modularity:
max_modularity = mod
coms = current_coms
else:
for _ in range(level):
coms = next(gn_hierarchy)


communities = []

Expand Down Expand Up @@ -1607,7 +1617,7 @@ def sbm_dl_nested(
>>> from cdlib import algorithms
>>> import networkx as nx
>>> G = nx.karate_club_graph()
>>> coms = algorithms.sbm_dl(G)
>>> coms = algorithms.sbm_dl_nested(G)


:References:
Expand Down
6 changes: 3 additions & 3 deletions cdlib/algorithms/internal/LPAM.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def LPAM(graph, k=2, threshold=0.5, distance="amp", seed=0):
Alexander Ponomarenko, Leonidas Pitsoulis, Marat Shamshetdinov
"""

def getCommuteDistace(G):
def getCommuteDistance(G):
"""
Returns commute distance matrix
"""
Expand Down Expand Up @@ -112,14 +112,14 @@ def getAmp(G):
if distance == "amp":
D = getAmp(line_graph)
if distance == "cm":
D = getCommuteDistace
D = getCommuteDistance(line_graph)
if isinstance(distance, np.ndarray):
D = distance
distance_name = "custom"
if D is None:
raise TypeError('Parameter distance should be "amp"/"cm", or numpy.ndarray')
_n = len(line_graph.nodes())
np.random.seed(0)
np.random.seed(seed)
initial_medoids = np.random.choice(_n, k, replace=False)
kmedoids_instance = kmedoids(D, initial_medoids, data_type="distance_matrix")
# run cluster analysis and obtain results
Expand Down
21 changes: 16 additions & 5 deletions cdlib/algorithms/internal/lfm.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,10 @@ def __init__(self, G, alpha, weight="weight"):

def execute(self):
communities = []
accepted_communities = set()

node_not_include = list(self.g.nodes())[:]

while len(node_not_include) != 0:
c = Community(self.g, self.alpha, self.weight)
# randomly select a seed node
Expand Down Expand Up @@ -128,8 +131,16 @@ def execute(self):

to_be_examined = c.get_neighbors()

for node in c.nodes:
if node in node_not_include:
node_not_include.remove(node)
communities.append(list(c.nodes))
return list(communities)
community_as_frozenset = frozenset(c.nodes)

if community_as_frozenset in accepted_communities:
node_not_include.remove(seed)
else:
accepted_communities.add(community_as_frozenset)
communities.append(list(c.nodes))

for node in c.nodes:
if node in node_not_include:
node_not_include.remove(node)

return list(communities)
2 changes: 1 addition & 1 deletion cdlib/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ def affiliations2nodesets(affiliations: dict) -> dict:
return asNodeSets

for n, coms in affiliations.items():
if isinstance(coms, str) or isinstance(coms, int) or isinstance(coms, np.int32):
if isinstance(coms, (str, int, np.integer)):
coms = [coms]
for c in coms:
asNodeSets.setdefault(c, set())
Expand Down
Loading