-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcoreness.py
More file actions
48 lines (43 loc) · 1.45 KB
/
coreness.py
File metadata and controls
48 lines (43 loc) · 1.45 KB
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
"""
Alex Levenson
alex@isnotinvain.com | www.isnotinvain.com
(c) Reya Group | http://www.reyagroup.com
Friday July 23rd 2010
"""
from scipy import optimize
from scipy.stats.stats import pearsonr
import numpy
import networkx as nx
def coreCorrelation(A,C):
"""
returns the pearson correlation between A and
the ideal coreness matrix created from C
A: ajacency matrix (valued or unvalued)
C: 1D matrix representing the coreness of each node
"""
cMat = numpy.matrix(C)
Cij = numpy.multiply(cMat,cMat.transpose())
return pearsonr(A.flat,Cij.flat)
def _coreFitness(C,*args):
"""
converts coreCorrelation(A,C) to something useable
with scipy.optimize (which aims to MINIMIZE a function)
Need to express highest positive correlation as function
to be minimized
"""
return coreCorrelation(args[0],C)[0] * -1.0
def getCoreness(graph,returnCorrelation=False):
"""
Calculates each node's 'coreness'
returns: a dictionary mapping node->coreness, and the final correlation to the ideal core/periphery model
if returnCorrelation is True
"""
A = nx.convert.to_numpy_matrix(graph)
initialC = numpy.random.rand(len(A)) # can we do better? Is it important? Maybe use constraint or centrality?
best = optimize.fmin_l_bfgs_b(_coreFitness, initialC,args=(A,None),approx_grad=True,bounds=[(0.0,1.0) for i in xrange(len(A))])
part = {}
for node in graph:
part[node] = best[0][graph.nodes().index(node)]
if returnCorrelation:
return part,best[1] * -1.0
return part