Skip to content

Commit

Permalink
🎨 black
Browse files Browse the repository at this point in the history
  • Loading branch information
GiulioRossetti committed May 29, 2024
1 parent 9cbda66 commit f8fd1a9
Show file tree
Hide file tree
Showing 3 changed files with 211 additions and 80 deletions.
58 changes: 44 additions & 14 deletions cdlib/algorithms/edge_clustering.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,23 @@
from cdlib import EdgeClustering
from collections import defaultdict
import networkx as nx
from cdlib.utils import convert_graph_formats, nx_node_integer_mapping, remap_edge_communities
from cdlib.algorithms.internal.HLC import HLC, HLC_read_edge_list_unweighted, HLC_read_edge_list_weighted, HLC_full

__all__ = ["hierarchical_link_community", "hierarchical_link_community_w", "hierarchical_link_community_full"]
from cdlib.utils import (
convert_graph_formats,
nx_node_integer_mapping,
remap_edge_communities,
)
from cdlib.algorithms.internal.HLC import (
HLC,
HLC_read_edge_list_unweighted,
HLC_read_edge_list_weighted,
HLC_full,
)

__all__ = [
"hierarchical_link_community",
"hierarchical_link_community_w",
"hierarchical_link_community_full",
]


def hierarchical_link_community(g_original: object) -> EdgeClustering:
Expand Down Expand Up @@ -96,10 +109,18 @@ def hierarchical_link_community_w(g_original: object) -> EdgeClustering:
return EdgeClustering(coms, g_original, "HLC_w", method_parameters={})


def hierarchical_link_community_full(
g_original: object,
weight="weight",
simthr=None,
hcmethod="single",
min_edges=None,
verbose=False,
) -> EdgeClustering:
"""
HLC (hierarchical link clustering) is a method to classify links into topologically related groups.
The algorithm uses a similarity between links to build a dendrogram where each leaf is a link from the original network and branches represent link communities.
At each level of the link dendrogram is calculated the partition density function, based on link density inside communities, to pick the best level to cut.
At each level of the link dendrogram is calculated the partition density function, based on link density inside communities, to pick the best level to cut.
This implementation follows exactly the algorithm described in Ahn et al and uses numpy/scipy to improve the clustering computation (It is faster and consumes less memory.
Expand All @@ -112,11 +133,11 @@ def hierarchical_link_community_w(g_original: object) -> EdgeClustering:
========== ======== ========
:param g_original: a networkx/igraph object
:weight: None for unweighted networks, jaccard approximation is used. When defined with a string, edge attribute name (usually 'weight') to be used as weight and Tanimoto approximation is used.
:simthr: None by default. If set to float, all values less than threshold are set to 0 in similarity matrix (it could reduce memory usage).
:hcmethod: Linkage method used in hierarchical clustering, 'single' by default. See scipy.cluster.hierarchy.linkage to get full method list.
:min_edges: None by default. If set to float, minimum number of edges that a community must contain to be kept in the clustering
:verbose: If True, write intermediary steps to disk.
:param weight: None for unweighted networks, jaccard approximation is used. When defined with a string, edge attribute name (usually 'weight') to be used as weight and Tanimoto approximation is used.
:param simthr: None by default. If set to float, all values less than threshold are set to 0 in similarity matrix (it could reduce memory usage).
:param hcmethod: Linkage method used in hierarchical clustering, 'single' by default. See scipy.cluster.hierarchy.linkage to get full method list.
:param min_edges: None by default. If set to float, minimum number of edges that a community must contain to be kept in the clustering
:param verbose: If True, write intermediary steps to disk.
:return: EdgeClustering object
Expand All @@ -131,11 +152,20 @@ def hierarchical_link_community_w(g_original: object) -> EdgeClustering:
Ahn, Yong-Yeol, James P. Bagrow, and Sune Lehmann. `Link communities reveal multiscale complexity in networks. <https://www.nature.com/articles/nature09182/>`_ nature 466.7307 (2010): 761.
"""
def hierarchical_link_community_full(g_original: object, weight='weight', simthr=None, hcmethod='single', min_edges= None, verbose=False) -> EdgeClustering:

g = convert_graph_formats(g_original, nx.Graph)
g_number, dictio = nx_node_integer_mapping(g)

coms = HLC_full(g_number, weight=weight, simthr=simthr, hcmethod=hcmethod, min_edges=min_edges, verbose=verbose, dictio= dictio).clusters
coms = HLC_full(
g_number,
weight=weight,
simthr=simthr,
hcmethod=hcmethod,
min_edges=min_edges,
verbose=verbose,
dictio=dictio,
).clusters
clustering = EdgeClustering(coms, g_number, "HLC_f", method_parameters={})
if dictio != None: clustering.communities = remap_edge_communities(clustering.communities, dictio)
return clustering
if dictio != None:
clustering.communities = remap_edge_communities(clustering.communities, dictio)
return clustering
Loading

0 comments on commit f8fd1a9

Please sign in to comment.