-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtda.py
229 lines (109 loc) · 3.11 KB
/
tda.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
import numpy as np;
import matplotlib.pyplot as plt;
import matplotlib.gridspec as gridspec;
import networkx as nx;
from IPython.display import Video;
import ripser;
import persim;
import teaspoon.MakeData.PointCloud as makePtCloud;
import teaspoon.TDA.Draw as Draw;
from teaspoon.SP.network import ordinal_partition_graph;
from teaspoon.TDA.PHN import PH_network;
from teaspoon.SP.network_tools import make_network;
from teaspoon.parameter_selection.MsPE import MsPE_tau;
import teaspoon.MakeData.DynSysLib.DynSysLib as DSL;
print('hello, world!');
#
# Generate Annulus shaped point cloud for testing
#
r = 1;
R = 2;
P = makePtCloud.Annulus(N=200, r=r, R=R, seed=None);
plt.scatter(P[:,0], P[:,1]);
#
# Function to conveniently plot diagrams
#
def drawTDA (P, diagrams, R=2):
fig, axes = plt.subplots(nrows=1, ncols=3, figsize=(20,5));
# Draw point cloud
plt.sca(axes[0]);
plt.title('Point Cloud');
plt.scatter(P[:,0], P[:,1]);
# Draw diagrams
plt.sca(axes[1]);
plt.title('0-Dim Diagram');
Draw.drawDgm(diagrams[0]);
plt.sca(axes[2]);
plt.title('1-Dim Diagram');
Draw.drawDgm(diagrams[1]);
plt.axis([0,R,0,R]);
#
# Example - Point Cloud, 0-Dimensional and 1-Dimensional Persistence Diagrams for Random Noise
#
# P = makePtCloud.Cube();
# diagrams = ripser.ripser(P)['dgms'];
# drawTDA (P=P, diagrams=diagrams, R=.80);
drawTDA(P=makePtCloud.Cube(), diagrams=ripser.ripser(makePtCloud.Cube())['dgms'], R=.80);
#
# Example - Double Annulus
#
def DoubleAnnulus (r1=1, R1=1, r2=.80, R2=1.30, xshift=1.3):
P = makePtCloud.Annulus(r=r1, R=R1);
Q = makePtCloud.Annulus(r=r2, R=R2);
Q[:,0] = Q[:,0] + xshift;
P = np.concatenate((P,Q));
return P;
P = DoubleAnnulus(r1=1, R1=2, r2=.5, R2=1.30, xshift=3);
plt.scatter(P[:,0], P[:,1]);
#
# Simple example by-hand
#
D1 = np.array([
[0, 1, np.inf, np.inf, 6],
[0, 0, 5, np.inf, np.inf],
[0, 0, 0, 2, 4],
[0, 0, 0, 0, 3],
[0, 0, 0, 0, 0]
]);
print(D1);
print(D1.shape);
D = D1 + D1.T;
# Define diagram with distance matrix instead of point cloud
diagrams = ripser.ripser(D, distance_matrix = True, maxdim=1)['dgms'];
print('0-Dim Diagram'); print(diagrams[0]);
print('1-Dim Diagram'); print(diagrams[1]);
#
# - Examples
#
#
# Function to draw graph
#
def drawGraphEx (G):
pos = nx.spring_layout(G);
nx.draw_networkx_nodes (G, pos, node_size=70); # draw nodes
nx.draw_networkx_edges (G, pos, width=2);
edge_labels = nx.draw_networkx_edge_labels (G, pos, edge_labels=nx.get_edge_attributes(G, 'weight'));
#
# Create Erdos-Renyii Random Graph
#
n=10; p=.30;
G = nx.erdos_renyi_graph (n, p, seed=None, directed=False);
m = len(G.edges);
print('there are {} edges'.format(m));
max_weight = 100;
weights = np.random.randint (max_weight, size=m);
for i,e in enumerate(G.edges()):
G[e[0]][e[1]]['weight'] = weights[i];
#
# Plot the graph
#
nx.draw(G); # plt.show();
# Add weights to adjacency matrix
A = nx.adjacency_matrix(G, weight='weight');
A = A.todense();
A = np.array(A);
A = A.astype('float64');
A[np.where(A==0)] = np.inf;
np.fill_diagonal(A,0);
plt.colorbar(plt.matshow(A,vmax=100));
# plt.show();