-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgraph_measurments.py
154 lines (127 loc) · 5.42 KB
/
graph_measurments.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
"""
TITLE : topo_fun
AUTHOR : Hernansaiz Ballesteros, Rosa.
DESCRIPTION : topological functions to deal with CARNIVAL's results.
LICENSE : GPL-v3
"""
def carnival2directGraph(weigthSample, inverse=True, verbose=False):
"""
Creates a dirct graph from CARNIVAL's weight's file.
:param weithSample string or pd.df: interaction file with weights
:param inverse boolean: type of carnival analysis. inverse=True (default)
:param verbose boolean: give printed information about repeated edges.
False (default)
:return: DG (direct graph)
:rtype: networkX graph
"""
import pandas as pd
import networkx as nx
if type(weigthSample) == str:
wm = pd.read_csv(weigthSample, sep='\t') # read file
else:
wm = weigthSample
# rename columns
wm.columns = ['Node1', 'Sign', 'Node2', 'Weight']
if inverse:
wm = wm[wm.Node1 != 'Perturbation'] # remove perturbation node
wm.reset_index(inplace = True, drop=True) # reset indexes
dupEdge = wm[wm[['Node1', 'Node2']].duplicated(keep=False)] # get duplicates
if dupEdge.shape[0] > 0:
idxDE = dupEdge[['Node1', 'Node2']].groupby(dupEdge[['Node1', 'Node2']].columns.tolist()).apply(lambda x: tuple(x.index)).tolist()
if verbose:
print('Number of repeated edges:', dupEdge.shape[0]/2)
# get the highest value for the repeated edge (keeping sign)
index2drop = []
for dupIndex in idxDE:
weight = [wm.iloc[dupIndex[0]]['Weight'], wm.iloc[dupIndex[1]]['Weight']]
i = weight.index(min(weight))
index2drop.append(dupIndex[i])
wm.drop(labels=[dupIndex[i]], axis='index', inplace=True)
# create direct graph
DG = nx.DiGraph()
auxL = []
for row in wm.values:
auxL.append((row[0], row[2], int(row[1])*int(row[3])))
DG.add_weighted_edges_from(auxL)
return(DG)
def get_initiators_effectors(weigthSample, inverse=True):
"""
Get initiators (initial nodes) and effectors (ending nodes)
from CARNIVAL weightedModel_1
:param weithSample string or pd.df: interaction file with weights
:param inverse boolean: type of carnival analysis. inverse=True (default)
:return: iNodes (initial nodes), eNodes (effectors)
:rtype: set
"""
import pandas as pd
if type(weigthSample) == str:
wm = pd.read_csv(weigthSample, sep='\t') # read file
else:
wm = weigthSample
if inverse:
wm = wm[wm.Node1 != 'Perturbation'] # remove perturbation node
wm.reset_index(inplace = True) # reset indexes
# rename columns
wm.columns = ['Node1', 'Sign', 'Node2', 'Weight']
iNodes = set(wm['Node1']) - set(wm['Node2'])
eNodes = set(wm['Node2']) - set(wm['Node1'])
return(iNodes, eNodes)
def get_measurments(DG, extended=False):
"""
Get network and node-based measurments for a network as networkX.DiGraph
:param DG networkx.DiGraph: directed graph from networkX
:param extended boolean: False default. get extra measures (diameter,
avg closeness centrality,
avg eccentricity,
avg eigenvector centrality)
:return: measure with values: number nodes, edges, network's density,
avg betweenness centrality,
avg degree centrality
and avg eigenvector centrality
:rtype: dict
"""
import networkx as nx
import numpy as np
measure = {}
measure['nNodes'] = nx.number_of_nodes(DG)
measure['nEdges'] = nx.number_of_edges(DG)
measure['density'] = nx.density(DG)
bc = nx.betweenness_centrality(DG)
measure['avg betweenness centrality'] = np.mean(list(bc.values()))
dc = nx.degree_centrality(DG)
measure['avg degree centrality'] = np.mean(list(dc.values()))
if extended:
measure['diameter'] = nx.diameter(DG)
cc = nx.closeness_centrality(DG)
measure['avg closeness centrality'] = np.mean(list(cc.values()))
ec = nx.eccentricity(DG)
measure['avg eccentricity'] = np.mean(list(ec.values()))
ev = nx.eigenvector_centrality(DG)
measure['avg eigenvector centrality'] = np.mean(list(ev.values()))
return(measure)
def adjacency2DG(adjaMTX):
"""
Creates a networkX direct graph based on the adjacency matrix
:param adjaMTX pd.df: pandas data frame with the network interactions
rows are the sources and columns the targets
:param DG networkx.DiGraph: directed graph frim networkX
:param extended boolean: False default. get extra measures (diameter,
avg closeness centrality,
avg eccentricity,
avg eigenvector centrality)
:return: DG networkx.DiGraph: directed graph from networkX
:rtype: networkx.DiGraph
"""
import networkx as nx
import pandas as pd
#import numpy as np
sources = adjaMTX.index.tolist()
targets = adjaMTX.columns.tolist()
DG = nx.DiGraph()
for source in sources:
for target in targets:
w = adjaMTX.loc[source, target]
if w != 0:
DG.add_edge(source, target, weight=w)
return(DG)