-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathzgraph.py
More file actions
147 lines (90 loc) · 2.74 KB
/
zgraph.py
File metadata and controls
147 lines (90 loc) · 2.74 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
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
import networkx as nx
import numpy as np
import pandas as pd
def normalise(x):
x = x[:]#deepcopy error
x -= min(x)
x /= max(x)
return x
def jgraph(posjac):
'''
networkx graph object from posjac at timestep
'''
posjac = 1 - normalise(np.log10(posjac).replace([np.inf,-np.inf],np.nan).dropna())
split = [i.split('->') for i in posjac.index]
#graph
G = nx.DiGraph()
for e in range(len(split)):
G.add_edge(split[e][0],split[e][1],weight=posjac[e])
G.remove_edges_from(G.selfloop_edges())
return G
def getnx(self, ts ,save=False):
'''
Create a networkx graph from a DSMACC new class
Usage:
getnx(a,a.ts[-1], 'propane')
'''
self.create_posjac()
G = nx.DiGraph()
posjac = self.posjac.loc[ts,:]
split = [i.split('->') for i in posjac.index]
for e in range(len(split)):
G.add_edge(split[e][0],split[e][1],weight=posjac[e])
G.remove_edges_from(G.selfloop_edges())
if save:
nx.write_weighted_edgelist(G, save+'.wedgelist')
#G=nx.read_weighted_edgelist('propane.wedgelist',create_using=nx.DiGraph)
return G
def pagerank(a):
return geobj2df(metric(tograph(group_hour(a.jacsp))))
def tograph(jac):
'''
Use hourly avg
'''
rt = []
for t in jac.iterrows():
jacsp=t[1]
#inverse negative links
index = np.array(jacsp.index)
lt = list(jacsp<0)
index[lt] = map(lambda x: '->'.join(reversed(x.split('->'))),index[lt])
jacsp.index = index
jacsp = jacsp.abs()
#normalize jacsp
jacsp = jacsp*1.01 - jacsp.min().min()
jacsp /= jacsp.max().max()
split = [i.split('->') for i in jacsp.index]
#graph
G = nx.DiGraph()
for e in range(len(split)):
G.add_edge(split[e][0],split[e][1],weight=jacsp[e])
G.remove_edges_from(G.selfloop_edges())
rt.append({'graph':G,'time':t[0]})
return rt
def metric(GS,met = 'nx.pagerank'):
'''
GS - out array from to_graph
'''
metfn = eval(met)
for gt in range(len(GS)):
res = metfn(GS[gt]['graph'])
res = [[key, res[key]] for key, value in sorted(res.iteritems(), key=lambda k,v: (v,k))]
GS[gt][met] = res
return GS
def geobj2df(GS,what = 'nx.pagerank'):
res = []
index = []
for s in GS:
index.append(s['time'])
s = pd.DataFrame(s[what])
s.index = s[0]
s=s[1]
res.append(s)
df = pd.concat(res,axis = 1).T
df.index = index
df = (df*1.1).subtract(df.min(axis=0
))
df=df.divide(df.max(axis=1),axis=0)
import zcreate_centrality as p
#p.createhtml(df)
return df