-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathcycle_cocyle.py
143 lines (108 loc) · 3.32 KB
/
cycle_cocyle.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
import networkx as nx
import numpy as np
import re
def cycle_cocyle(TG):
MST = nx.minimum_spanning_tree(TG)
scaffold = nx.MultiGraph()
used_keys = []
for e in MST.edges(data=True):
edict = e[2]
lbl = edict['label']
ind = edict['index']
ke = (ind,lbl[0],lbl[1],lbl[2])
scaffold.add_edge(e[0],e[1],key=ke)
used_keys.append(ke)
cycle_basis = []
cycle_basis_append = cycle_basis.append
nxfc = nx.find_cycle
for e0 in TG.edges(data=True):
edict = e0[2]
lbl = edict['label']
ind = edict['index']
ke = (ind,lbl[0],lbl[1],lbl[2])
scaffold.add_edge(e0[0],e0[1],key=ke)
if ke not in used_keys:
cycles = list(nxfc(scaffold))
cy_list = [(i[0], i[1], i[2]) for i in cycles]
if cy_list not in cycle_basis:
cycle_basis_append(cy_list)
scaffold.remove_edge(e0[0],e0[1],key=ke)
node_out_edges = []
node_out_edges_append = node_out_edges.append
node_list = list(TG.nodes())
for n in range(len(TG.nodes()) - 1):
node = node_list[n]
noe = [node]
for e in TG.edges(data=True):
if node == e[0] or node == e[1]:
edict = e[2]
lbl = edict['label']
ke = (edict['index'],lbl[0],lbl[1],lbl[2])
positive_direction = edict['pd']
noe.append(positive_direction + ke)
node_out_edges_append(noe)
return cycle_basis, node_out_edges
def Bstar_alpha(CB, CO, TG, num_edges):
edge_keys = dict(((k[0],k[1],k[2]['index']),[]) for k in TG.edges(data=True))
for e in TG.edges(keys=True, data=True):
edge_keys[(e[0],e[1],e[3]['index'])] = e[2]
Bstar = []
a = []
Bstar_append = Bstar.append
a_append = a.append
q = 0
for cycle in CB:
q += 1
cycle_vec = [0] * num_edges
net_voltage = np.array([0,0,0])
for edge in cycle:
s,e,lv = edge
ind = lv[0]
voltage = np.asarray(lv[1:])
try:
key = edge_keys[(s,e,ind)]
except:
key = edge_keys[(e,s,ind)]
positive_direction = TG[s][e][key]['pd']
if (s,e) == positive_direction:
direction = 1
elif (e,s) == positive_direction:
direction = -1
else:
raise ValueError('Error in B* cycle vector construction, edge direction cannot be defined for:',s,e)
cycle_vec[ind - 1] = direction
net_voltage = net_voltage + (direction * voltage)
Bstar_append(cycle_vec)
a_append(net_voltage)
for vertex in sorted(CO, key = lambda x: int(re.sub('[A-Za-z]','',x[0]))):
cocycle_vec = [0] * num_edges
v = vertex[0]
ooa = [[i[2],(i[0],i[1]),np.array(i[3:])] for i in vertex[1:]]
for out_edge in ooa:
ke = (out_edge[0], out_edge[2][0], out_edge[2][1], out_edge[2][2])
ind = out_edge[0]
s,e = out_edge[1]
positive_direction = TG[s][e][ke]['pd']
if '_a' not in s and '_a' not in e:
if s == v:
o = e
else:
o = s
v_ind = int(re.sub('[A-Za-z]','',v))
o_ind = int(re.sub('[A-Za-z]','',o))
if v_ind < o_ind:
cd = 1
else:
cd = -1
if v == s:
direction = 1
else:
direction = -1
if direction != cd:
raise ValueError('The direction assignment for the co-cycle vector', s,e, 'may is incorrect... \n The direction assignment does not follow the low-index to high-index = positive convention')
cocycle_vec[ind - 1] = direction
Bstar_append(cocycle_vec)
a_append(np.array([0,0,0]))
if len(Bstar) != len(a):
raise ValueError('Error in cycle_cocycle.py, the row ranks of Bstar and alpha do not match.')
return np.asarray(Bstar), np.asarray(a)