Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
181 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
from .edge_clustering import * | ||
from .crisp_partition import * | ||
from .overlapping_partition import * | ||
from .attribute_clustering import * |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
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 | ||
|
||
__all__ = ['eva'] | ||
|
||
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, coms_labels, "Eva", method_parameters={"weight": weight, "resolution": resolution, | ||
"randomize": randomize, "alpha":alpha}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
from .node_clustering import NodeClustering | ||
from .edge_clustering import EdgeClustering | ||
from .fuzzy_node_clustering import FuzzyNodeClustering | ||
from .attr_node_clustering import AttrNodeClustering |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
from cdlib.classes.node_clustering import NodeClustering | ||
from cdlib import evaluation | ||
|
||
|
||
class AttrNodeClustering(NodeClustering): | ||
|
||
"""Attribute Node Communities representation. | ||
:param communities: list of communities | ||
:param graph: a networkx/igraph object | ||
:param coms_labels: dictionary specifying for each community the frequency of the attribute values | ||
:param method_name: community discovery algorithm name | ||
:param method_parameters: configuration for the community discovery algorithm used | ||
:param overlap: boolean, whether the partition is overlapping or not | ||
""" | ||
|
||
def __init__(self, communities, graph, coms_labels, method_name, method_parameters=None, overlap=False): | ||
super().__init__(communities, graph, method_name, method_parameters, overlap) | ||
self.coms_labels = coms_labels | ||
|
||
def purity(self): | ||
|
||
"""Purity is the product of the frequencies of the most frequent labels carried by the nodes within the communities | ||
:return: FitnessResult object | ||
""" | ||
return evaluation.purity(self.coms_labels) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import unittest | ||
from cdlib import algorithms | ||
import networkx as nx | ||
import random | ||
|
||
class AttrCommunityDiscoveryTests(unittest.TestCase): | ||
|
||
def test_eva(self): | ||
|
||
l1 = ['one', 'two', 'three', 'four'] | ||
l2 = ["A", "B", "C"] | ||
g = nx.barabasi_albert_graph(100, 5) | ||
labels=dict() | ||
|
||
for node in g.nodes(): | ||
labels[node]={"l1":random.choice(l1), "l2":random.choice(l2)} | ||
|
||
coms = algorithms.eva(g,labels,alpha=0.5) | ||
|
||
self.assertEqual(type(coms.communities), list) | ||
if len(coms.communities) > 0: | ||
self.assertEqual(type(coms.communities[0]), list) | ||
self.assertEqual(type(coms.communities[0][0]), int) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,3 +16,4 @@ pulp==1.6.* | |
pquality==0.0.7 | ||
seaborn==0.9.* | ||
pandas==0.25.* | ||
eva_lcd |