-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathClass_persistence.py
More file actions
276 lines (201 loc) · 9.1 KB
/
Copy pathClass_persistence.py
File metadata and controls
276 lines (201 loc) · 9.1 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
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
import numpy as np
from sklearn.metrics import pairwise_distances
import itertools as it
import math
import time
from sys import getsizeof
class filtration:
def __init__(self, locations, simplex1_check=None, simplex2_check=None, simplex3_check=None):
self.simplex_to_order = dict()
self.order_to_simplex = dict()
self.locations = locations
self.simplex1_check = simplex1_check
self.simplex2_check = simplex2_check
self.simplex3_check = simplex3_check
self.homology_gps = []
self.order = -1
self.parameters = []
self.boundaries = []
self.col_operations = []
self.is_last_boundary = []
self.homology_gps = [[], [], []]
def add_simplex(self, simplex, parameter, boundaries):
self.boundaries.append(boundaries)
self.simplex_to_order.update({simplex:self.order})
self.order_to_simplex.update({self.order:simplex})
self.parameters.append(parameter)
self.is_last_boundary.append([])
self.col_operations.append([self.order])
if boundaries:
last_boundary_order = max(boundaries)
self.is_last_boundary[last_boundary_order].append(self.order)
def generate_simplices(self):
data = pairwise_distances(self.locations)
n_rows, n_cols = data.shape
edges = []
parameter = 0
# Add 0-simplices
for v in range(n_rows):
self.order = self.order + 1
boundary = []
self.add_simplex(frozenset({v,}), parameter, boundary)
# Make a list of 1-simplices
for pair in it.combinations(list(range(n_rows)), 2):
# Add here any restrictions on the edges
if math.isinf(data[pair[0], pair[1]]):
continue
if self.simplex1_check:
if not self.simplex1_check(pair[0], pair[1]):
continue
edges.append([(pair[0], pair[1]),data[pair[0], pair[1]]])
# Now sort the list onthe basis of the distance
edges.sort(key=lambda x: x[1])
# The 'boundary' for 0-simplices will store its neighbors
n_edges = len(edges)
count = 0
# Iterate through the edges sorted by length
for edge, parameter in edges:
count = count + 1
#print(count, n_edges, end='\r')
simplex = frozenset(edge)
v0_set = frozenset({edge[0],})
v1_set = frozenset({edge[1],})
v0_order = self.simplex_to_order[v0_set]
v1_order = self.simplex_to_order[v1_set]
# Add 1-simplices
self.order = self.order + 1
boundaries = [v0_order, v1_order]
self.add_simplex(simplex, parameter, boundaries)
self.boundaries[v0_order].append(v1_order)
self.boundaries[v1_order].append(v0_order)
## Find common neighbors
#common_neighbors = []
#for v in self.boundaries[v0_order]:
# if v in self.boundaries[v1_order]:
# common_neighbors.append(v)
## Add 2-simplices
#for v in common_neighbors:
# if self.simplex2_check:
# if not self.simplex2_check(edge[0], edge[1], v):
# continue
# #print(order, simplex, end='\r')
# self.order = self.order + 1
# simplex = frozenset({edge[0], edge[1], v})
# o1 = self.simplex_to_order[frozenset({edge[0], edge[1]})]
# o2 = self.simplex_to_order[frozenset({edge[1], v})]
# o3 = self.simplex_to_order[frozenset({edge[0], v})]
# boundaries = [o1, o2, o3]
# self.add_simplex(simplex, parameter, boundaries)
#for v2 in self.boundaries[v]:
# if v2 in common_neighbors:
# s1 = frozenset({v, v2, edge[0]})
# if self.simplex2_check:
# if not self.simplex2_check(v, v2, edge[0]):
# continue
# s2 = frozenset({v2, edge[0], edge[1]})
# if self.simplex2_check:
# if not self.simplex2_check(v2, edge[0], edge[1]):
# continue
# s4 = frozenset({v, v2, edge[1]})
# if self.simplex2_check:
# if not self.simplex2_check(v, v2, edge[1]):
# continue
# o1 = self.simplex_to_order[frozenset({v, v2, edge[0]})]
# o2 = self.simplex_to_order[frozenset({v2, edge[0], edge[1]})]
# o4 = self.simplex_to_order[frozenset({v, v2, edge[1]})]
#for pair in it.combinations(common_neighbors, 2):
# v = pair[0]
# v2 = pair[1]
# simplex = frozenset({v, v2, edge[0], edge[1]})
# if simplex not in self.simplex_to_order:
# print(order, simplex, end='\r')
# self.order = self.order + 1
# o1 = self.simplex_to_order[frozenset({v, v2, edge[0]})]
# o2 = self.simplex_to_order[frozenset({v2, edge[0], edge[1]})]
# o3 = self.simplex_to_order[frozenset({v, edge[0], edge[1]})]
# o4 = self.simplex_to_order[frozenset({v, v2, edge[1]})]
# boundaries = [o1, o2, o3, o4]
# self.add_simplex(simplex, parameter, boundaries)
## Add 3-simplices
#for v in common_neighbors:
# for v2 in self.boundaries[v]:
# if v2 in common_neighbors:
# simplex = frozenset({v, v2, edge[0], edge[1]})
# if simplex not in self.simplex_to_order:
# print(order, simplex, end='\r')
# order = order + 1
# o1 = self.simplex_to_order[frozenset({v, v2, edge[0]})]
# o2 = self.simplex_to_order[frozenset({v2, edge[0], edge[1]})]
# o3 = self.simplex_to_order[frozenset({v, edge[0], edge[1]})]
# o4 = self.simplex_to_order[frozenset({v, v2, edge[1]})]
# boundaries = [o1, o2, o3, o4]
# self.add_simplex(simplex, order, parameter, boundaries)
def reduce_filtration(self, simplex_order):
last_boundary_order = max(self.boundaries[simplex_order])
first_last_boundary_order = min(self.is_last_boundary[last_boundary_order])
while first_last_boundary_order < simplex_order:
self.boundaries[simplex_order] = list(\
set(self.boundaries[simplex_order])\
.symmetric_difference(\
set(self.boundaries[first_last_boundary_order])))
# Record the column operation
self.col_operations[simplex_order] = self.col_operations[simplex_order] +\
self.col_operations[first_last_boundary_order]
# This boundary is not the last boundary of the simplex anymore
self.is_last_boundary[last_boundary_order].remove(simplex_order)
if self.boundaries[simplex_order]:
# Find the new max
last_boundary_order = max(self.boundaries[simplex_order])
# Update the is_last_boundary of this new max
self.is_last_boundary[last_boundary_order].append(simplex_order)
# Find the new first_last_boundary_order
first_last_boundary_order = min(self.is_last_boundary[last_boundary_order])
else:
break
def evaluate_persistence_homology(self):
for i in range(self.order+1):
simplex = self.order_to_simplex[i]
if len(simplex) < 2:
continue
self.reduce_filtration(i)
# The aim right now is to get only H0
for i in range(self.order+1):
simplex = self.order_to_simplex[i]
if len(simplex) > 1:
break
if not self.is_last_boundary[i]:
birth_order = i
birth = self.parameters[i]
death = math.inf
death_order = math.inf
self.homology_gps[0].append([(birth, death), (birth_order, death_order), simplex])
elif len(self.is_last_boundary[i]) == 1:
birth_order = i
birth = self.parameters[i]
death_order = self.is_last_boundary[i][0]
death = self.parameters[death_order]
self.homology_gps[0].append([(birth, death), (birth_order, death_order), simplex])
else:
print('Unexpected')
#for i in is
#for i in range(self.order+1):
# simplex = self.order_to_simplex[i]
# if len(simplex) < 2:
# continue
# if not self.boundaries[i]:
# if not self.is_last_boundary[i]:
# birth = self.parameters[i]
# birth_order = i
# death = math.inf
# dim = len(simplex)
# self.homology_gps[dim-1].append([(birth, death), (birth_order, death_order), self.col_operations[i]])
# print(simplex)
# else:
# death = self.parameters[i]
# death_order = i
# birth_order = max(self.boundaries[i])
# birth = self.parameters[birth_order]
# birth_simplex = self.order_to_simplex[birth_order]
# dim = len(birth_simplex)
# if death != birth:
# self.homology_gps[dim-1].append([(birth, death), (birth_order, death_order), self.col_operations[i]])