-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathR_properties.py
346 lines (294 loc) · 21.5 KB
/
R_properties.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
import os
import nibabel as nib
import numpy as np
import networkx as nx
from reeb_grapher import *
import pickle
trkfolder = '/mnt/c/Users/shail/Documents/Connectome/Connectome/Data/trk/eps3'
trkfolderI = '/mnt/c/Users/shail/Documents/Connectome/Connectome/Data/trk/raw_data'
eps = 3
trknamelist = os.listdir(trkfolder)
for trk_i in range(len(trknamelist)):
trkname = trknamelist[trk_i]
trkpathI = os.path.join(trkfolderI, trkname[:len(trknamelist[trk_i])-7])
trk = nib.streamlines.load(trkpathI)
streamlines = trk.streamlines
I = create_image(streamlines, eps)
trkpath = os.path.join(trkfolder, trkname)
with open(trkpath, 'rb') as handle:
dic_T = pickle.load(handle)
#construct Reeb graph R
R = nx.Graph()
check_traj = np.zeros((len(I.trajectories), 2000), dtype=bool) #Flag considered points of the trajectories; we are assuming if each trajetcory has a maximum of 2000 points
tree_node_map = {} #for final merge
node_coordinate_map = {} #for location of nodes
nc = 0 #node count
for s in range(len(I.trajectories)): #start with trajectories in I one by one
# print("Processing trajectory", s)
M = {}
prev_connected_comp = []
T1 = I.trajectories[s]
trajectory_to_node_map_pres = {}
trajectory_to_node_map_prev = {}
if not (check_traj[s][0]): #check first point of the trajectory (if first is already considered then all following points are already considered)
G = nx.Graph() # This graph will be modified dynamically
traj_list = [s] # all trajectories to be taken into acount in this batch
ind_list = [0] #corresponding indices of above
while (len(traj_list)>0): #go on till the trajectories to be considered have not exhausted
del_ind_tra = []
disconnected_events = []
disappear_events = []
eventFlag = False
i = 0
delete_nodes = []
while (i < len(traj_list)):
if not (check_traj[traj_list[i]][ind_list[i]]):
check_traj[traj_list[i]][ind_list[i]] = True
if dic_T[traj_list[i]].get(ind_list[i]):
events = dic_T[traj_list[i]][ind_list[i]]
for e in events:
if not e.considered:
eventFlag = True
if e.event == "appear":
# print("Appear ", e.trajectory1)
G.add_node(traj_list[i])
e.considered = True
if e.trajectory1 not in traj_list:
print("WARNING!!!!")
elif e.event == "connect":
# print("Connected event between", e.trajectory1, e.trajectory2)
G.add_node(e.trajectory2)
G.add_node(e.trajectory1)
G.add_edge( e.trajectory1, e.trajectory2)
if e.trajectory2 not in traj_list:
traj_list.append(e.trajectory2)
ind_list.append(e.t2)
connect_j = []
temp_dic = {}
for j in range(e.t2): # to handle events before non zero index
if dic_T[e.trajectory2].get(j):
eventj = dic_T[e.trajectory2][j] #event at index j
for ej in eventj:
if(ej.event == "connect"):
if (ej.trajectory2 != e.trajectory2):
connect_j.append(ej.trajectory2)
temp_dic[ej.trajectory2] = [j, ej.t2]
else:
connect_j.append(ej.trajectory1)
temp_dic[ej.trajectory1] = [j, ej.t1]
elif(ej.event == "disconnect"):
if ej.trajectory2 in connect_j:
connect_j.remove(ej.trajectory2)
elif ej.trajectory1 in connect_j:
connect_j.remove(ej.trajectory1)
for c in connect_j:
G.add_node(c)
G.add_edge(e.trajectory2, c)
if c not in traj_list:
traj_list.append(c)
# e.t2 - j = x- ej.t2 from assumption
ind_list.append(temp_dic[c][1] - temp_dic[c][0] + e.t2)
# else:
# x = traj_list.index(e.trajectory2)
# y = ind_list[x]
# if (e.t2 < y):
# ind_list[x] = e.t2
if e.trajectory1 not in traj_list:
traj_list.append(e.trajectory1)
ind_list.append(e.t1)
connect_j = []
temp_dic = {}
# print("e.t2", e.t2)
for j in range(e.t1): # to handle events before non zero index
if dic_T[e.trajectory1].get(j):
eventj = dic_T[e.trajectory1][j] #event at index j
for ej in eventj:
if(ej.event == "connect"):
if(ej.trajectory1 != e.trajectory1):
connect_j.append(ej.trajectory1)
temp_dic[ej.trajectory1] = [j, ej.t1]
else:
connect_j.append(ej.trajectory2)
temp_dic[ej.trajectory2] = [j, ej.t2]
elif(ej.event == "disconnect"):
if ej.trajectory1 in connect_j:
connect_j.remove(ej.trajectory1)
elif ej.trajectory2 in connect_j:
connect_j.remove(ej.trajectory2)
for c in connect_j:
G.add_node(c)
G.add_edge(e.trajectory1, c)
if c not in traj_list:
traj_list.append(c)
# e.t2 - j = x- ej.t2 from assumption
ind_list.append(temp_dic[c][1] - temp_dic[c][0] + e.t1)
# else:
# x = traj_list.index(c)
# y = ind_list[x]
# if (temp_dic[c][1] - temp_dic[c][0] + e.t1 < y):
# ind_list[x] = y
# else:
# x = traj_list.index(e.trajectory1)
# y = ind_list[x]
# if (e.t1 < y):
# ind_list[x] = y
e.considered = True
elif (e.event == "disconnect"):
# print("Disconnected event between", e.trajectory1, e.trajectory2)
if (G.has_node (e.trajectory2) and G.has_node (e.trajectory1)):
disconnected_events.append(e)
if (G.has_edge(e.trajectory1, e.trajectory2)):
G.remove_edge(e.trajectory1, e.trajectory2)
e.considered = True
#if trajectory is not present what to delete
# if e.trajectory2 not in traj_list:
# traj_list.append(e.trajectory2)
# ind_list.append(e.t2)
# elif e.trajectory1 not in traj_list:
# traj_list.append(e.trajectory1)
# ind_list.append(e.t1)
else:
check_traj[traj_list[i]][ind_list[i]] = False
elif (e.event == "disappear"):
disappear_events.append(e)
delete_nodes.append(traj_list[i])
del_ind_tra.append(i)
e.considered = True
else:
delete_nodes.append(traj_list[i])
del_ind_tra.append(i)
# print("REACHED HERE FINALLY", traj_list[i], M, ind_list[i])
if (trajectory_to_node_map_prev.get(traj_list[i])):
R.add_edge(M[trajectory_to_node_map_prev[traj_list[i]]], tree_node_map[traj_list[i]] )
# if G.has_node(traj_list[i]):
# G.remove_node(traj_list[i])
# traj_list.remove(traj_list[i])
# ind_list.remove(ind_list[i])
# print("Trajectory list", traj_list)
# print("Index list", ind_list)
i += 1
if (eventFlag):
# print(M)
# print("connected component")
# for c in nx.connected_components(G):
# print(c)
if (len(prev_connected_comp) == 0): # this will be executed only once
prev_connected_comp = [c for c in nx.connected_components(G)]
for prev_cc in prev_connected_comp:
R.add_node(nc)
M[frozenset(prev_cc)] = nc
for ci in prev_cc:
trajectory_to_node_map_prev[ci] = frozenset(prev_cc)
if not tree_node_map.get(ci):
tree_node_map[ci] = nc
#node_coordinate_map[nc] = I.trajectories[traj_list[traj_list.index(list(prev_cc)[0])]].points[ind_list[traj_list.index(list(prev_cc)[0])]]
nc += 1
else:
pres_connected_component = [c for c in nx.connected_components(G)]
for pres_cc in pres_connected_component:
for ci in pres_cc:
trajectory_to_node_map_pres[ci] = frozenset(pres_cc)
for c_individual in pres_cc:
if not tree_node_map.get(c_individual):
tree_node_map[c_individual] = nc
copied_nodes = [] #this will store all split events
if len(disconnected_events) != 0:
for de in disconnected_events:
C1 = None
C2 = None
if trajectory_to_node_map_prev.get(de.trajectory1):
C1 = trajectory_to_node_map_prev[de.trajectory1]
else:
check_traj[de.trajectory1][de.t1] = False
if trajectory_to_node_map_prev.get(de.trajectory2):
C2 = trajectory_to_node_map_prev[de.trajectory2]
else:
check_traj[de.trajectory2][de.t2] = False
if (C1 == None and C2 == None):
continue
elif (C1 != C2):
continue
elif C1 not in copied_nodes and (trajectory_to_node_map_pres[de.trajectory1] != trajectory_to_node_map_prev[de.trajectory1]):
R.add_node(nc)
R.add_edge(M[C1],nc)
copied_nodes.append(C1)
M[frozenset(C1)] = nc
# try:
# node_coordinate_map[nc] = I.trajectories[traj_list[traj_list.index(de.trajectory1)]].points[ind_list[traj_list.index(de.trajectory1)]-1]
# except:
# print("node", nc, C1)
nc += 1
for cc in pres_connected_component:
if cc in prev_connected_comp:#do nothing: still epsilon connected
continue
else:
check = False
check_cc = False
for cn in copied_nodes:
if cc.issubset(cn):
M[frozenset(cc)] = M[frozenset(cn)]
check_cc = True
if not check_cc:
for prev_cc in prev_connected_comp:
#add edge if nodes are neighbor
if prev_cc.intersection(cc):
check = True
R.add_node(nc)
M[frozenset(cc)] = nc
# try:
# node_coordinate_map[nc] = I.trajectories[list(cc)[0]].points[ind_list[traj_list.index(list(cc)[0])]]
# # print("check here please",prev_connected_comp, cc)
# except:
# print("Line 212", nc, cc)
R.add_edge(M[frozenset(prev_cc)], M[frozenset(cc)])
for c_individual in cc:
if not tree_node_map.get(c_individual):
tree_node_map[c_individual] = nc
# print("added edge between: ", prev_cc, cc, "node ids are", M[frozenset(prev_cc)], M[frozenset(cc)])
if not check:
R.add_node(nc)
M[frozenset(cc)] = nc
# try:
# node_coordinate_map[nc] = I.trajectories[traj_list[traj_list.index(list(cc)[0])]].points[ind_list[traj_list.index(list(cc)[0])]]
# except:
# print(nc, cc)
for c_individual in cc:
if not tree_node_map.get(c_individual):
tree_node_map[c_individual] = nc
nc += 1
if (len(disappear_events) != 0):
for da in disappear_events:
C1 = None
if trajectory_to_node_map_prev.get(da.trajectory1):
C1 = trajectory_to_node_map_prev[da.trajectory1]
check = False
if len(C1) == 1:
for x in range(len(I.trajectories[list(C1)[0]].points)):
if dic_T[list(C1)[0]].get(x) and "connect" in [y.event for y in dic_T[list(C1)[0]][x]]:
check = True
continue
if check and C1 in prev_connected_comp:
pc = M[frozenset(C1)]
# node_coordinate_map[pc] = I.trajectories[traj_list[traj_list.index(list(C1)[0])]].points[-1]
else:
R.add_node(nc)
R.add_edge(M[C1],nc)
M[frozenset(C1)] = nc
# node_coordinate_map[nc] = I.trajectories[da.trajectory1].points[-1]
nc += 1
prev_connected_comp = []
for cx in pres_connected_component :
prev_connected_comp.append(cx)
trajectory_to_node_map_prev = trajectory_to_node_map_pres
trajectory_to_node_map_pres = {}
# print("Shailja", R.nodes, R.edges)
for node in delete_nodes:
if G.has_node(node):
G.remove_node(node)
ind_list = [x+1 for x in ind_list]
for j in range(len(traj_list)):
if (ind_list[j] >= len(I.trajectories[traj_list[j]].points)):
del_ind_tra.append(j)
traj_list = [i for j, i in enumerate(traj_list) if j not in del_ind_tra]
ind_list = [i for j, i in enumerate(ind_list) if j not in del_ind_tra]
print(trkname, len(streamlines), len(R.nodes()), len(R.edges()))