-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathttk_saliency.py
1087 lines (870 loc) · 39.7 KB
/
ttk_saliency.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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Author: Melih Can Yesilli
Date: 5/1/2022
Contact: [email protected]
Description: This library includes functions that can be used to compute topological saliency for a given surface.
In addition, users can perform clustering and topological simplification based on topological saliency.
Required softwares and packages:
- TTK (https://topology-tool-kit.github.io/installation.html)
- VTK Python API
- pygeodesic (https://pypi.org/project/pygeodesic/)
- multiprocessing
- potpourri3d (https://github.com/nmwsharp/potpourri3d)
"""
from vtk import (
vtkDataObject,
vtkTableWriter,
vtkThreshold,
vtkXMLPolyDataWriter,
vtkXMLPolyDataReader,
vtkUnstructuredGridReader,
vtkXMLUnstructuredGridReader,
vtkXMLUnstructuredGridWriter,
vtkXMLStructuredGridWriter,
vtkDataSetReader,
vtkUnstructuredGridWriter,
vtkXMLStructuredGridReader,
vtkDelaunay2D,
)
import vtk
from vtk.util.numpy_support import vtk_to_numpy,numpy_to_vtk
from topologytoolkit import (
ttkMorseSmaleComplex,
ttkPersistenceCurve,
ttkPersistenceDiagram,
ttkTopologicalSimplification,
ttkArrayPreconditioning,
)
from itertools import combinations
from pygeodesic.examples.vtk_helpers import getPointsAndCellsFromPolydata
import multiprocessing
from multiprocessing import Pool
import time
import pygeodesic.geodesic as geodesic
import numpy as np
import math
import scipy.spatial.distance as dist
from sklearn.cluster import AgglomerativeClustering
from scipy.cluster.hierarchy import dendrogram
import potpourri3d as pp3d
from scipy.spatial.distance import squareform
def triangulate(inputFilePath):
"""
This function triangulates given vtu file using vtkDelaunay2D function of vtk.
Parameters
----------
inputFilePath : str
Path to the vtu file
Returns
-------
reader : vtk object
triangulated surface/image
polydata : vtk object
Output of the vtk object of triangulated surface/image
"""
# loading the input data
reader = vtkXMLUnstructuredGridReader()
reader.SetFileName(inputFilePath)
reader.Update()
# triagulating it
triangulation = vtkDelaunay2D()
triangulation.SetInputConnection(reader.GetOutputPort())
triangulation.Update()
reader = triangulation
polydata = reader.GetOutput()
return reader,polydata
def MS_Complex(reader,saving):
"""
Parameters
----------
reader : vtk object
triangulated surface/image
saving : boolean
Set to True if user wants to save the MS complex output
Returns
-------
out : dict
Output of the MS Complex function in TTK. This includes the ascending and descending manifolds.
"""
# compute MS Complex
morseSmaleComplex = ttkMorseSmaleComplex()
morseSmaleComplex.SetInputConnection(reader.GetOutputPort())
morseSmaleComplex.SetInputArrayToProcess(0, 0, 0, 0, "data")
morseSmaleComplex.SetDebugLevel(3)
morseSmaleComplex.Update()
n_output = morseSmaleComplex.GetOutputDataObject(3).GetPointData().GetNumberOfArrays()
out = {}
for i in range(n_output):
out[morseSmaleComplex.GetOutputDataObject(3).GetPointData().GetArrayName(i)]=vtk_to_numpy(morseSmaleComplex.GetOutputDataObject(3).GetPointData().GetArray(i))
if saving:
np.save("MS_Complex_Output",out)
return out
def geo_dist_exact(k,points, faces,comb):
"""
Computes the exact geodesic distance using paralell computing.
Parameters
----------
k : int
index for the list of combinations of selected points in triangulated surfaces
points : 2D array
the coordinate points of the points on the surface
faces : 2D array
the vertex number of the points that generates faces after triangulation
comb : list
the list of combinations between the points
Returns
-------
d : array
the distances between the selected points
"""
# select indexes of two critical points
source_Index = comb[k][0]
target_Index = comb[k][1]
geoalg = geodesic.PyGeodesicAlgorithmExact(points, faces)
# compute the distance
d,path = geoalg.geodesicDistance(source_Index, target_Index)
return d
def geo_dist_approx(solver,source_Index):
"""
Computes the approximated geodesic distance
Parameters
----------
solver : potpourri3d object
solver for distance computation, check the documentation of potpourri3d for more details
source_Index : float
the vertex number in triangulated surface
Returns
-------
dist : array
the distances between selected vertex number and all surface points
"""
dist = solver.compute_distance(source_Index)
return dist
def geo_dist_par(polydata,compute_comb,dist_type,*args):
"""
Computes the pairwise distance matrix between the given critical points.
The exact distance or approaximated distance is computed based on user choice.
Parameters
----------
polydata : vtkPolyData
vtkPolyData object obtained after generatin triangulation
compute_comb : boolean
Set it to True if you want algorithm to generate combinations whose
pairwise distance will be computed. If False, user needs to provide
the list of indicies whose pairwise distances needed
dist_type : string
Geodesic distance type. If 'exact' is selected, exact distance will be
computed. For faster computation, approximated distance is recommended using
'approximate' flag.
*args :
vertexID : np.array
The list of indicies of the points whose pairwise distance needed.
It is only required if user set compute_comb parameter to True.
comb : list
The list of combinations provided by user when compute_comb is set
to False
Returns
-------
DM_cp: np.array
2D array that only includes the pairwise distances between the points whose
indices given in vertexID or whose combinations are provided by the user
distances: np.array
2D array that only includes the pairwise distances between selected critical points
of the surface and the all surface points of the surface. All other distances is shown
as zero. Its dimenison equals to the dimension of the surface.
"""
points, faces = getPointsAndCellsFromPolydata(polydata) # obtain points and faces of the surface
# user choses if exact or approximate geodesic distance will be computed
# exact distance uses pygeodesic package, while approximate distance uses
# potpourri3d package. The latter is much faster than the former.
if dist_type =='exact':
if compute_comb:
vertexID = args[0]
comb = combinations(vertexID, 2)
comb = list(comb)
else:
comb = args[0]
# put all inputs of the distance computation function together
inputs = []
for i in range(len(comb)):
inputs.append((i,points, faces, comb))
# time the paralell computation of distances
start = time.time()
n_proc = multiprocessing.cpu_count()
with Pool(processes=n_proc//2) as p:
DM_cp = p.starmap(geo_dist_exact, inputs)
finish = time.time()
print('Elapsed time (paralell execution-exact): {}'.format(finish-start))
return DM_cp
elif dist_type=='approximate':
if compute_comb:
vertexID = args[0]
comb = combinations(vertexID, 2)
comb = list(comb)
else:
comb = args[0]
# generate the solver
solver = pp3d.MeshHeatMethodDistanceSolver(points,faces)
# time the computation of distances between surface points and the selected critical points
start = time.time()
distances = np.zeros((len(points),len(points)))
for i in range(len(vertexID)):
distances[vertexID[i],:] = geo_dist_approx(solver,vertexID[i])
distances[:,vertexID[i]] = distances[vertexID[i],:]
finish = time.time()
print('Elapsed time (serial execution-approximation): {}'.format(finish-start))
# the distances between selected critical points
DM_cp = np.zeros((len(comb)))
for i in range(len(comb)):
DM_cp[i] = distances[comb[i][0], comb[i][1]]
#convert it to square matrix
DM_cp = squareform(DM_cp)
return DM_cp,distances
def perDiagTTK(reader,diagtype):
"""
Computes selected type of persistence diagram and provides user with details.
Parameters
----------
reader : vtk object
triangulated surface/image
diagtype : str
the type of persistence diagram need to be computed (superlevel, sublevel or infinite)
Returns
-------
pd : list of tuples
all persistence diagrams including superlevel, sublevel and infinite
output : dictionary
the dictionary that includes the all necessary output such as coordinates of critical points,
desired persistence diagrams, vertex number of all or desired critical points in triangulated
surface
Pairs : vtk object
vtkThreshold object that includes information related to all persistence diagram types. This information
contains coordinates of the critical points, persistence values and vertex number of critical points.
criticalPairs : vtk object
vtkThreshold object that includes information related to selected persistence diagram type. This information
contains coordinates of the critical points, persistence values and vertex number of critical points.
"""
diagram = ttkPersistenceDiagram()
diagram.SetInputConnection(reader.GetOutputPort())
diagram.SetInputArrayToProcess(0, 0, 0, 0, "data")
diagram.SetDebugLevel(3)
diagram.Update()
# obtain critical points from PD
criticalPairs = vtkThreshold()
criticalPairs.SetInputConnection(diagram.GetOutputPort())
criticalPairs.SetInputArrayToProcess(
0, 0, 0, vtkDataObject.FIELD_ASSOCIATION_CELLS, "PairIdentifier")
criticalPairs.ThresholdBetween(-0.1, 999999)
criticalPairs.Update()
if diagtype == "infinite":
# obtain Global Min - Global Max pairs
Pairs = vtkThreshold()
Pairs.SetInputConnection(criticalPairs.GetOutputPort())
Pairs.SetInputArrayToProcess(
0, 0, 0, vtkDataObject.FIELD_ASSOCIATION_CELLS, "PairType")
Pairs.ThresholdBetween(-1, -1)
Pairs.Update()
elif diagtype == "sublevel":
# obtain sub-level set persistent
Pairs = vtkThreshold()
Pairs.SetInputConnection(criticalPairs.GetOutputPort())
Pairs.SetInputArrayToProcess(
0, 0, 0, vtkDataObject.FIELD_ASSOCIATION_CELLS, "PairType")
Pairs.ThresholdBetween(0, 0)
Pairs.Update()
elif diagtype=="superlevel":
Pairs = vtkThreshold()
Pairs.SetInputConnection(criticalPairs.GetOutputPort())
Pairs.SetInputArrayToProcess(
0, 0, 0, vtkDataObject.FIELD_ASSOCIATION_CELLS, "PairType")
Pairs.ThresholdBetween(1, 1)
Pairs.Update()
# vertexID, coordinates and persistence of all critical points including superlevels, sublevels and infinite
vertexID_all = criticalPairs.GetOutput().GetPointData().GetArray("ttkVertexScalarField")
coord_all = criticalPairs.GetOutput().GetPointData().GetArray("Coordinates")
persistence_all = criticalPairs.GetOutput().GetCellData().GetArray("Persistence")
# vertexID, coordinates and persistence of critical points belong to diagram chosen above
vertexID_des = Pairs.GetOutput().GetPointData().GetArray("ttkVertexScalarField")
coord_des = Pairs.GetOutput().GetPointData().GetArray("Coordinates")
persistence_des = Pairs.GetOutput().GetCellData().GetArray("Persistence")
pd = []
pd_pairs_coord = []
# obtain persistence diagram and coordinates of all points
for i in range(criticalPairs.GetOutput().GetNumberOfCells()):
tCell = criticalPairs.GetOutput().GetCell(i)
pair_type = vtk_to_numpy(criticalPairs.GetOutput().GetCellData().GetArray(1))[i]
birthId = tCell.GetPointId(0)
bp = coord_all.GetTuple(birthId)[2]
deathId = tCell.GetPointId(1)
dp = coord_all.GetTuple(deathId)[2]
pd.append((bp,dp,pair_type))
pd_pairs_coord.append((coord_all.GetTuple(birthId),coord_all.GetTuple(deathId)))
# obtain persistence diagram and coordinates of desired points only
pd_des = []
pd_pairs_coord_des = []
# obtain persistence diagram and coordinates of desired (sublevel or superlevel) points
for i in range(Pairs.GetOutput().GetNumberOfCells()):
tCell = Pairs.GetOutput().GetCell(i)
pair_type = vtk_to_numpy(Pairs.GetOutput().GetCellData().GetArray(1))[i]
birthId = tCell.GetPointId(0)
bp = coord_des.GetTuple(birthId)[2]
deathId = tCell.GetPointId(1)
dp = coord_des.GetTuple(deathId)[2]
pd_des.append((bp,dp,pair_type))
pd_pairs_coord_des.append((coord_des.GetTuple(birthId),coord_des.GetTuple(deathId)))
# convert coordinates of all critical point pairs from list to an array
pair_coord = np.asarray(pd_pairs_coord)
# convert the coordinates of the pair of critical points of desired persistence diagram into an array
pd_pairs_coord_des = np.asarray(pd_pairs_coord_des)
# convert the coordinates of desired points or all points into numpy array
coord_all = vtk_to_numpy(coord_all)
coord_des = vtk_to_numpy(coord_des)
# find the index of matching birth and death critical points in vertexID_des
# For example, if the first matching indices are 0 and 1, it will show that
# first and second vertex_ID des are matching and they are a pair in persistence
# diagram
pair_indices = []
for i in range(len(pd_pairs_coord_des)):
p1 = pd_pairs_coord_des[i,0,:].reshape((1,3)).astype("float32")
p2 = pd_pairs_coord_des[i,1,:].reshape((1,3))[0]
ind1 = np.where(np.all(coord_des==p1,axis=1))[0][0]
ind2 = np.where(np.all(coord_des==p2,axis=1))[0][0]
pair_indices.append((ind1,ind2))
pair_index = np.asarray(pair_indices)
# Depending on chosen persistence diagram type provide users with the index of the
# minima or maxima
vertexID_des = vtk_to_numpy(vertexID_des)
if diagtype=='sublevel':
pair_index_des = vertexID_des[pair_index[:,0]] # vertexID of birth times
elif diagtype == 'superlevel':
pair_index_des = vertexID_des[pair_index[:,1]] # vertexID of death times
# parse the outputs into a dictionary
output = {}
output["vertexID_all"] = vtk_to_numpy(vertexID_all)
output["coord_all"] = coord_all
output["persistence_all"] = vtk_to_numpy(persistence_all)
output["pairs_coord_all"] = pd_pairs_coord
output['pair_ind_des'] = pair_index
output["cp_ind_des"]=pair_index_des
output["vertexID_des"] = vertexID_des
output["coord_des"] = coord_des
output["persistence_des"] = vtk_to_numpy(persistence_des)
return pd,output,Pairs,criticalPairs
def saliency_weight(vertexID,r,D_M):
"""
Computes the gaussian weights for each critical point at given neighborhood
size.
Parameters
----------
vertexID : np.array
The vertex number of the critical points for the
r : np.array
The array that includes the neighborhood sizes used to compute the saliency
D_M : 2D np.array
The matrix that includes the distances between the critical points
Returns
-------
W : 2D np.array
The gaussian weights used to compute the saliency for correponding r
"""
comb = combinations(np.arange(0,len(vertexID)), 2)
comb = list(comb)
weights = []
# upper diagonal weights
for i in range(len(comb)):
ind1 = comb[i][0]
ind2 = comb[i][1]
weights.append(math.exp(-D_M[ind1,ind2]**2/r**2))
# convert the list of weights into array
W = dist.squareform(weights)
# add diagonal weigths
for i in range(len(W)):
W[i,i]=1
return W
def Topological_Saliency(perdiag,weight):
"""
Computes topological saliency for given weights
Parameters
----------
perdiag : 2D np.array
The selected persistence diagram ('sublevel', 'superlevel' or 'infinite')
weight : 2D np.array
The gaussian weights used to compute the saliency for correponding r
Returns
-------
T : np.array
Saliency values computed at a certain neighborhood size for the critical points
"""
T = []
for i in range(len(perdiag)):
denominator = 0
for j in range(len(perdiag)):
denominator = denominator + weight[i,j]*(perdiag[j,1]-perdiag[j,0])
T.append(weight[i,i]*(perdiag[j,1]-perdiag[j,0])/denominator)
return T
def Topological_Saliency_r(pd,r_range,distances,vertexID,D_M,diagtype):
"""
Provides user with salency of critical points computed at given neighborhood range
Parameters
----------
pd : list of tuples
Persistence diagrams including all types
r_range : np.array
The array that includes all r values which will be used to compute saliency
distances : np.array
Geodesic distance that includes only the distances between critical points
vertexID : np.array
The vertex number of critical points for selected peristence diagram.
D_M : np.array (2D)
Pairwise distance matrix between the critical points of selected persistence diagram
diagtype : str
The type of selected persistence diagram ('superlevel', 'sublevel', or 'infinite')
Returns
-------
output : dict
The dictionary that includes the saliency values of selected critical points, and the index of selected persistence diagrams
The saliency matrix will only include the values for selected critical points, for all other points, the saliency value is set to 'inf'
"""
# conver the list of tuples into an array and generate the saliency matrix
perDiag= np.asarray(pd)
saliency = np.ones((len(perDiag),len(r_range)))*float("inf")
# set the saliencies of persistence diagram points that excludes the points
# of selected persistence diagram
if diagtype == 'sublevel':
pd_index = np.where(perDiag[:,2]==0)[0]
chosen_pd = perDiag[perDiag[:,2]==0]
elif diagtype == 'superlevel':
pd_index = np.where(perDiag[:,2]==1)[0]
chosen_pd = perDiag[perDiag[:,2]==1]
elif diagtype == 'infinite':
pd_index = np.where(perDiag[:,2]==-1)[0]
chosen_pd = perDiag[perDiag[:,2]==-1]
i=0
for r in r_range:
weight = saliency_weight(vertexID,r,D_M)
saliency[pd_index,i] = np.asarray(Topological_Saliency(chosen_pd,weight))
i +=1
# output object to return
output = {}
output["chosen_pd_ind"]= pd_index
output["saliency"]= saliency
return output
def save_surf_info(reader, saliency, r, diagtype, inc):
"""
This function saves the surface information during simplification process. For each iteration,
saliency and triangulated surface are saved.
Parameters
----------
reader : vtk object
triangulated surface/image
saliency : 2D np.array
Saliency values of all critical points for varying critical points
r : np.array
the range of values for neighborhood size
diagtype : str
The type of persistence diagrams selected for the analysis. It could be 'superlevel', 'sublevel' or 'infinite'
inc : int
The iteration number for simplification process
Returns
-------
None.
"""
# persistence diagram
pd,output,Pairs,criticalPairs = perDiagTTK(reader,diagtype)
pair_index = output['cp_ind_des']
# MS-Complex
MS_Cplx = MS_Complex(reader,False)
AM = MS_Cplx['AscendingManifold']
DM = MS_Cplx['DescendingManifold']
# surface data
surf = vtk_to_numpy(reader.GetOutputDataObject(0).GetPointData().GetArray(0))
surf = surf.reshape(-1,int(np.sqrt(len(surf))))
# saving
output = {}
output['surface'] = surf
output['saliency'] = saliency
output['r'] = r
output['cp_ind_des'] = pair_index
output['Des_Man']= DM
output['Asc_Man']= AM
save_name = save_name = 'Simplified_Surface_Iteration_{}'.format(inc)
np.save(save_name,output)
save_name_polydata = 'Simplified_Surface_Iter_PolyData_{}'.format(inc)
writer = vtkXMLPolyDataWriter()
writer.SetFileName(save_name_polydata)
writer.SetInputData(reader.GetOutputDataObject(0))
writer.Write()
def Saliency_Simplification(reader,saliency,criticalPairs,chosen_pd_ind,r,r_threshold_ind,vertexID,n_features,n_feature_remain,diagtype):
"""
This function applies the topological simplification based on topological saliency.
Parameters
----------
reader : vtk object
triangulated surface/image
saliency : 2D np.array
The saliency values computed at given neighborhood range for all critical points
criticalPairs : vtk object
The vtk object that only includes the selected critical points (local minimas or maximas)
chosen_pd_ind : np.array
the indicies of the selected persistence diagram points in the array that includes all types of persistence diagrams
r : np.array
The range for the neighbohood size
r_threshold_ind : int
The index of neighborhood size which will be used to threshold saliency values in simplification process
vertexID : np.array
The vertex number of the all critical points of the triangulated surface
n_features : int
The number of features (critical points). This number depends on the selected type of persistence diagram
n_feature_remain : int
The maximum number of features (critical points) that the user wants to have at the end of simplification process
diagtype : str
The type of persistence diagram user wants to work on ('sublevel','superlevel','infinite')
Returns
-------
SalientPairs : vtkThreshold object
The object that includes the information about the most salient critical points after the simplification process
topologicalSimplification : ttk Object
The TTK object that includes information about the simplified surface
"""
topologicalSimplification = reader
inc= 0
# the loop that simplifies the surface until the desired number of features remain
while n_features>n_feature_remain:
# save persistence diagram, saliency, critical point information for the original surface
if inc==0:
save_surf_info(reader, saliency, r, diagtype, inc)
# define the thereshold for saliency for chosen r value
# it will ignore the the infinite saliency values
init_threshold_s = max(saliency[chosen_pd_ind,r_threshold_ind])*0.15
max_saliecny = max(saliency[:,r_threshold_ind])
# create a vtk array for saliency matrix and fill it with saliency values
# of selected r parameter
array = vtk.vtkDoubleArray()
array.SetName("Saliency")
array.SetNumberOfComponents(1)
array.SetNumberOfTuples(len(saliency))
for x in zip(range(len(saliency)), np.reshape(saliency[:,r_threshold_ind],(len(saliency),1))):
array.SetTuple(*x)
# add saliency matrix into vtk object of critical pairs
criticalPairs.GetOutput().GetCellData().AddArray(array)
criticalPairs.Update()
# find the most salient points for the given range
SalientPairs = vtkThreshold()
SalientPairs.SetInputConnection(criticalPairs.GetOutputPort())
SalientPairs.SetInputArrayToProcess(
0, 0, 0, vtkDataObject.FIELD_ASSOCIATION_CELLS, "Saliency")
SalientPairs.ThresholdBetween(init_threshold_s,max_saliecny)
SalientPairs.Update()
# remove the non-salient points from the surface
t_Simplification = ttkTopologicalSimplification()
t_Simplification.SetInputConnection(0, topologicalSimplification.GetOutputPort())
t_Simplification.SetInputArrayToProcess(0, 0, 0, 0, "data")
t_Simplification.SetInputConnection(1, SalientPairs.GetOutputPort())
t_Simplification.SetDebugLevel(3)
t_Simplification.Update()
# update the topological simplification object since this is an iterative process
topologicalSimplification = t_Simplification
# triangulate the new simplified surface
triangulation = vtkDelaunay2D()
triangulation.SetInputConnection(topologicalSimplification.GetOutputPort())
triangulation.Update()
reader = triangulation
polydata = reader.GetOutput()
# compute the persistence diagram of the simplified surface
pd,output,Pairs,criticalPairs = perDiagTTK(reader,diagtype)
pair_index = output['cp_ind_des']
perDiag= np.asarray(pd)
# compute the number of feature remained after each simplification
prev_n_feat = n_features
if diagtype == 'sublevel':
n_features = len(perDiag[perDiag[:,2]==0])
elif diagtype == 'superlevel':
n_features = len(perDiag[perDiag[:,2]==1])
elif diagtype == 'infinite':
n_features = len(perDiag[perDiag[:,2]==-1])
# compute the distances between critical points of the simplified surface
# exact distance
# distances = geo_dist_par(polydata,True,'exact',pair_index)
# D_M = dist.squareform(distances)
# approximate distance
D_M,distances = geo_dist_par(polydata,True,'approximate',pair_index)
# compute the saliency
output_sal = Topological_Saliency_r(pd,r,distances,pair_index,D_M,diagtype)
saliency = output_sal["saliency"]
chosen_pd_ind = output_sal["chosen_pd_ind"]
print(n_features)
inc= inc+1
# save the surface information
save_surf_info(reader, saliency, r, diagtype, inc)
# # extract the simplified surface information
simp = topologicalSimplification
simp = vtk_to_numpy(simp.GetOutputDataObject(0).GetPointData().GetArray(0))
simp = simp.reshape(-1,int(np.sqrt(len(simp))))
# # save simplified surface and corresponding saliency information
save_name = 'Simplified_Surface_Iteration_{}'.format(inc+1)
output = {}
output['surface'] = simp
output['saliency'] = saliency
np.save(save_name,output)
# if the number of feature does not change with respect to previous
# iteration break the loop
if prev_n_feat == n_features:
break
return SalientPairs,topologicalSimplification
def area_between_features(feat_1, feat_2, saliency,r):
"""
Parameters
----------
feat_1 : int
index for critical point 1
feat_2 : int
index for critical point 2
saliency : 2D np.array
Saliency matrix
r : np.array
The array that includes the range for neighborhood size
Returns
-------
float
area between given two saliency curves
"""
# extract the saliency array for feature 1 and feature 2
sal_1 = saliency[feat_1,:]
sal_2 = saliency[feat_2,:]
# compute the areas under these arrays
area_1 = np.trapz(sal_1,r)
area_2 = np.trapz(sal_2,r)
return abs(area_1-area_2)
def feature_similarity(saliency,r):
"""
Parameters
----------
saliency : 2D np.array
The saliency values of the critical points
r : np.array
The range for the neighborhood size
Returns
-------
np.array
The array that includes the areas between saliency curves
"""
# distance = []
# for i in range(len(saliency)):
# for j in range(i,len(saliency)):
# distance.append(area_between_features(i, j, saliency,r))
comb = combinations(np.arange(0,len(saliency)), 2)
comb = list(comb)
distance = []
for i in range(len(comb)):
ind1 = comb[i][0]
ind2 = comb[i][1]
distance.append(area_between_features(ind1, ind2, saliency,r))
return np.asarray(distance)
def plot_dendrogram(model, **kwargs):
"""
Parameters
----------
model : cluster model
Selected clustering function
**kwargs :
Additional plot parameters. Please check the documentation for the dendogram function of SciPy.
Returns
-------
Figure that inlcudes the dendogram plot of the clustering
"""
# this function directly copied and pasted from
# https://scikit-learn.org/stable/auto_examples/cluster/plot_agglomerative_dendrogram.html#sphx-glr-auto-examples-cluster-plot-agglomerative-dendrogram-py
# Create linkage matrix and then plot the dendrogram
# create the counts of samples under each node
counts = np.zeros(model.children_.shape[0])
n_samples = len(model.labels_)
for i, merge in enumerate(model.children_):
current_count = 0
for child_idx in merge:
if child_idx < n_samples:
current_count += 1 # leaf node
else:
current_count += counts[child_idx - n_samples]
counts[i] = current_count
linkage_matrix = np.column_stack([model.children_, model.distances_,
counts]).astype(float)
# Plot the corresponding dendrogram
dendrogram(linkage_matrix, **kwargs)
def saliency_based_clustering(Dist_M, dendrogram_plot,cluster_n):
"""
Parameters
----------
Dist_M : 2D np.array
The distance matrix that contains the pairwise distances between the critical points of the simplified surface.
dendrogram_plot : boolean
Set to True if user wants to obtain dendogram plot for the clustering of the critical points
cluster_n : int
Number of clusters user wants to have
Returns
-------
cluster_model : cluster model
Selected clustering function
clustering : np.array
Cluster numbers for each critical point
"""
if dendrogram_plot:
cluster_model = AgglomerativeClustering(n_clusters=cluster_n, affinity='precomputed',
linkage='single',compute_distances=True)
clustering = cluster_model.fit_predict(Dist_M)
else:
cluster_model = AgglomerativeClustering(n_clusters=cluster_n, affinity='precomputed',
linkage='single')
clustering = cluster_model.fit_predict(Dist_M)
return cluster_model,clustering
def Surface_Patch_Clustering(cluster_n,diagtype, Dist_M, dist_type, manifold, pair_index, polydata):
"""
Parameters
----------
cluster_n : list
The list that includes the number of clusters. User try multiple cluster number to compare the outputs
diagtype : str
the type of persistence diagrams that saliency is computed for. ('sublevel', 'superlevel', 'infinite')
Dist_M : 2D np.array
The distance matrix that contains the pairwise distances between the critical points of the simplified surface
dist_type : str
'approximate' or 'exact' geodesic distance. 'approximate' is recommended for faster computation.
manifold : np.array
The array that includes the clusters obtained using descending or ascending manifolds from Morse-Smale Complex
pair_index : np.array
The vertex numbers of the critical points of trinagulated surface
polydata : vtkPolyData object
The VTK object that includes the simplified surface information
Returns
-------
clustered_surf : TYPE
DESCRIPTION.
distances: 2D np.array
The distance matrix that includes the pairwise distances between the critical points only
"""
labels_clust = np.zeros((len(cluster_n),1),dtype=object)
clustered_surf = np.zeros((len(cluster_n),1),dtype=object)
for j in range(len(cluster_n)):
cluster_model,labels = saliency_based_clustering(Dist_M, False,cluster_n[j])
# rearrange the label matrix for each element of the pairs
new_labels = np.zeros((len(labels),2))
new_labels[:,0] = pair_index
new_labels[:,1] = labels
new_labels = new_labels.astype(int)
labels_clust[j,0] = new_labels
# generate final cluster matrices
clustered_surf[j,0] = np.full((len(manifold),1),-1)
if dist_type=='exact':
for i in np.unique(manifold):
print(i)
# find the index of cluster i
ind = np.where(manifold==i)[0]
# find how many critical points are in this cluster
common_cp, comm1, comm2 = np.intersect1d(ind, new_labels[:,0],return_indices=True)
common_cp = common_cp.astype(int)
# proceed with computation reduction if there is intersection
if len(common_cp)!=0:
# remove these critical points from the index matrix since we dont need to compute the pairwise distance between them
ind = np.delete(ind, comm1)
comb=[]
# generate the combinations of indices
for j in range(len(common_cp)):
for k in range(len(ind)):
comb.append((common_cp[j],ind[k]))
# compute the distance
distances = geo_dist_par(polydata,False,'exact',comb)
distances = np.asarray(distances)
distances = np.reshape(distances,(-1,len(ind)))
# find the nearest neighbor for each surface point in the cluster and assign them a label
for j in range(len(ind)):
closest_cp_loc = np.where(new_labels[:,0] == common_cp[np.where(distances[:,j]==distances[:,j].min())[0][0]])[0][0]
# assing labels for different number of clusters chosen in the beginning
for cl_num in range(len(cluster_n)):
clustered_surf[cl_num,0][ind[j]] = labels_clust[cl_num,0][closest_cp_loc,1]
# proceed with brute force approach if there is no intersection
else:
comb=[]
# generate the combinations of indices
for j in range(len(new_labels[:,0])):
for k in range(len(ind)):
comb.append((new_labels[j,0],ind[k]))
# compute the distance
distances = geo_dist_par(polydata,False,'exact',comb)
distances = np.asarray(distances)
# reshape the distances
distances = distances.reshape(-1,len(ind))
# find the nearest neighbor for each surface point in the cluster and assign them a label
for j in range(len(ind)):
closest_cp_loc = np.where(distances[:,j]==distances[:,j].min())[0][0]
# assing labels for different number of clusters chosen in the beginning
for cl_num in range(len(cluster_n)):
clustered_surf[cl_num,0][ind[j]] = labels_clust[cl_num,0][closest_cp_loc,1]
print("i:{} ---> no intersection".format(i))
for cl_num in range(len(cluster_n)):
# add the labels of critical points into the matrix
clustered_surf[cl_num,0][new_labels[:,0].astype(int)] = labels_clust[cl_num,0][:,1].reshape((len(new_labels[:,0]),1))
# make the labels square matrix
clustered_surf[cl_num,0] = clustered_surf[cl_num,0].reshape(-1,int(np.sqrt(len(manifold))))