-
Notifications
You must be signed in to change notification settings - Fork 0
/
initPartition.py
73 lines (51 loc) · 2.11 KB
/
initPartition.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Jan 15 17:12:51 2021
@author: georgiabaltsou
"""
from collections import defaultdict
import csv
import community as community_louvain
import matplotlib.cm as cm
import matplotlib.pyplot as plt
import networkx as nx
import pandas as pd
import sys
import igraph as ig
import louvain
from collections import OrderedDict
import numpy as np
import leidenalg
from cdlib import algorithms, readwrite, viz
import cdlib
def initPartition(G):
# G = cdlib.utils.convert_graph_formats(g, nx.Graph, directed=True)
#
# coms = algorithms.leiden(G)
# viz.plot_community_graph(G, coms)
# readwrite.write_community_csv(coms, "communities.csv", ",")
# InitialPartition = louvain.find_partition(G, louvain.ModularityVertexPartition, initial_membership=None, seed=0)
# InitialPartition = louvain.find_partition(G, louvain.CPMVertexPartition, initial_membership=None, seed=0, resolution_parameter=0.001)
InitialPartition = leidenalg.find_partition(G, leidenalg.ModularityVertexPartition,seed=0)
# InitialPartition = leidenalg.find_partition(G, leidenalg.CPMVertexPartition,seed=0,resolution_parameter=1) #modularity with resolution parameter
# InitialPartition = leidenalg.find_partition(G, leidenalg.RBERVertexPartition,seed=0,max_comm_size=290,resolution_parameter=0.002)
print(InitialPartition.quality()) #for modularity
# partition.set_membership(lisInitialPartition)
# print(InitialPartition)
# print(InitialPartition[78])
# for i in range(len(InitialPartition)):
# print(InitialPartition[i])
# optimiser = leidenalg.Optimiser()
# diff = optimiser.optimise_partition(InitialPartition)
#
# print(diff)
memb = InitialPartition.membership
changedPart = defaultdict(dict)
for i in range(len(InitialPartition)):
for j in InitialPartition[i]:
changedPart[j] = i
changedPartI = dict(sorted(iter(changedPart.items())))
# changedPartI =OrderedDict(sorted(changedPart.items()))
# print(InitialPartition)
return InitialPartition, memb, changedPartI