forked from prabeshpaudel/geometric-point-processes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbifiltration3.py
87 lines (76 loc) · 3.2 KB
/
bifiltration3.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
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib import collections as mc
import math
### FUNCTIONS ###
def distance(a,b): # function that returns distance between two points
return(abs(math.sqrt((x[a] - x[b]) ** 2 + (y[a] - y[b]) ** 2)))
def rips (degree, distance): # function that returns vertices present in the rips complex given degree and distance
vertices = []
i = 0
while i < num:
if sorted_dist_mat[i,degree] <= distance:
vertices.append(i)
i += 1
return(vertices)
### PARAMETERS ###
num = 5 # number of points
edge_num = int(num * (num - 1) / 2) # number of edges
x = np.random.uniform(-100,100,num) # uniform distribution for x
y = np.random.uniform(-100,100,num) # uniform distribution for y
### INITIATION ###
dist_mat = np.zeros((num,num)) # empty space to store distance matrix
sorted_dist_mat = np.zeros((num,num)) # empty space to store sorted distance matrix
edges = np.zeros(edge_num) # empty space to store all edge length
edges_critical = np.zeros(edge_num) # empty space to store
sorted_edge_mat = np.zeros((edge_num,num)) # empty space to store sorted edge matrix (critical distance at each degree for all edges)
# critical distance: the distance at which the edge/vertex appears in the rips complex, given the degree
edges_dist = {} # edge dictionary to store edge and corresponding vertices
# Distance Matrix, Sorted Edges, and Distance dictionary
i = 0
k = 0
while i < num:
j = i + 1
while j < num:
dist_mat[j,i] = dist_mat[i,j] = edges[k] = distance(i,j) # store distance
edges_dist[edges[k]]= (i,j) # store the corresponding vertices for the edge
j += 1
k += 1
i += 1
edges = np.sort(edges) # sort edges
print("\nDistance Matrix")
print(dist_mat) # print distance matrix
# print(edges) # print sorted edges
# print(edges_dist) # print edge dictionary
# Sorted Distance Matrix
i = 0
while i < num:
sorted_dist_mat[i,] = np.sort(dist_mat[i,]) # sort distance matrix by row
i += 1
print("\nSorted Distance Matrix")
print(sorted_dist_mat) # print sorted distance matrix
# Sorted Edge Matrix
filler = [] # emtpy array
i = 0
while i < edge_num: # loop through all edges
j = 0
while j < num: # loop through number of degrees
points = edges_dist[edges[i]] # two vertices associated with the edge
one = sorted_dist_mat[points[0],j] # critical distance for vertex 1
two = sorted_dist_mat[points[1],j] # critical distance for vertex 2
sorted_edge_mat[i,j] = max(one,two,edges[i]) # critical distance for the edge
j += 1
filler = np.unique(sorted_edge_mat[i,]) # unique critical distances for the edge
edges_critical[i] = len(filler) # length of unique critical distances or the number of critical points
i += 1
print("\nSorted Edge Matrix")
print(sorted_edge_mat) # print sorted edge matrix
### RESULTS ###
#print(np.array(np.unique(edges_critical, return_counts=True)).T)
results = np.unique(edges_critical, return_counts=True) # counts for # of edges given # of critical points
print(results)
# fig = plt.figure() # print figure
# ax = fig.add_axes([0,0,1,1]) # add axis
# ax.bar(results[0],results[1]) # plot results
# plt.show() # show plot