Skip to content

Commit

Permalink
Merge 17ea1a0 into 47d53b1
Browse files Browse the repository at this point in the history
  • Loading branch information
dsalvaz committed Sep 13, 2019
2 parents 47d53b1 + 17ea1a0 commit 56ef8a4
Show file tree
Hide file tree
Showing 13 changed files with 729 additions and 17 deletions.
1 change: 1 addition & 0 deletions cdlib/__init__.py
@@ -1,3 +1,4 @@
from cdlib.classes.node_clustering import NodeClustering
from cdlib.classes.edge_clustering import EdgeClustering
from cdlib.classes.fuzzy_node_clustering import FuzzyNodeClustering
from cdlib.classes.attr_node_clustering import AttrNodeClustering
1 change: 1 addition & 0 deletions cdlib/algorithms/__init__.py
@@ -1,3 +1,4 @@
from .edge_clustering import *
from .crisp_partition import *
from .overlapping_partition import *
from .attribute_clustering import *
85 changes: 85 additions & 0 deletions cdlib/algorithms/attribute_clustering.py
@@ -0,0 +1,85 @@
try:
import igraph as ig
except ModuleNotFoundError:
ig = None

import Eva

from collections import defaultdict
from cdlib import AttrNodeClustering

import networkx as nx

from cdlib.utils import convert_graph_formats

from cdlib.algorithms.internal.ILouvain import ML2

__all__ = ['eva', 'ilouvain']

def eva(g, labels, weight='weight', resolution=1., randomize=False, alpha=0.5):

"""
The Eva algorithm extends the Louvain approach in order to deal with the attributes of the nodes (aka Louvain Extended to Vertex Attributes).
It optimizes - combining them linearly - two quality functions, a structural and a clustering one, namely the modularity and the purity.
A parameter alpha tunes the importance of the two functions: an high value of alpha favors the clustering criterion instead of the structural one.
:param g: a networkx/igraph object
:param weight: str, optional the key in graph to use as weight. Default to 'weight'
:param resolution: double, optional Will change the size of the communities, default to 1.
:param randomize: boolean, optional Will randomize the node evaluation order and the community evaluation order to get different partitions at each call, default False
:param alpha: a value assumed in [0,1] tuning the importance of modularity and purity criteria
:return: AttrNodeClustering object
:Example:
>>> from cdlib.algorithms import eva
>>> import networkx as nx
>>> import random
>>> l1 = ['A', 'B', 'C', 'D']
>>> l2 = ["E", "F", "G"]
>>> g = nx.barabasi_albert_graph(100, 5)
>>> labels=dict()
>>> for node in g.nodes():
>>> labels[node]={"l1":random.choice(l1), "l2":random.choice(l2)}
>>> communities = eva(g_attr, labels, alpha=0.8)
:References:
1. #####
.. note:: Reference implementation: https://github.com/GiulioRossetti/Eva/tree/master/Eva
"""

g = convert_graph_formats(g, nx.Graph)
nx.set_node_attributes(g, labels)

coms, coms_labels = Eva.eva_best_partition(g, weight=weight, resolution=resolution, randomize=randomize, alpha=alpha)

# Reshaping the results
coms_to_node = defaultdict(list)
for n, c in coms.items():
coms_to_node[c].append(n)

coms_eva = [list(c) for c in coms_to_node.values()]
return AttrNodeClustering(coms_eva, g, "Eva", coms_labels, method_parameters={"weight": weight, "resolution": resolution,
"randomize": randomize, "alpha":alpha})


def ilouvain(g, labels, id):
g = convert_graph_formats(g, nx.Graph)
nx.set_node_attributes(g, labels)
id = dict()
for n in g.nodes():
id[n] = n

algo = ML2(g,labels, id)
coms = algo.findPartition()

# Reshaping the results
coms_to_node = defaultdict(list)
for n, c in coms.items():
coms_to_node[c].append(n)

coms_ilouv = [list(c) for c in coms_to_node.values()]

return AttrNodeClustering(coms_ilouv, g, "ILouvain")
1 change: 0 additions & 1 deletion cdlib/algorithms/crisp_partition.py
Expand Up @@ -965,4 +965,3 @@ def sbm_dl_nested(g, B_min=None,B_max=None, deg_corr=True, **kwargs):
coms = affiliations2nodesets(affiliations)
coms = [list(v) for k,v in coms.items()]
return NodeClustering(coms, g, "SBM_nested", method_parameters={"B_min": B_min, "B_max": B_max, "deg_corr": deg_corr})

0 comments on commit 56ef8a4

Please sign in to comment.