/
CMGDB_util.py
284 lines (230 loc) · 10 KB
/
CMGDB_util.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
# CMGDB_util.py # 2021-10-26
# MIT LICENSE 2020 Ewerton R. Vieira
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import math
import csv
import time
import os
import CMGDB
from datetime import datetime
import time
import itertools
class CMGDB_util:
def run_CMGDB(self, subdiv_min, subdiv_max, lower_bounds, upper_bounds, phase_periodic, F, base_name, subdiv_init=6, subdiv_limit=10000, cmap=matplotlib.cm.get_cmap('viridis', 256)):
# Define the parameters for CMGDB
model = CMGDB.Model(subdiv_min, subdiv_max, subdiv_init, subdiv_limit,
lower_bounds, upper_bounds, phase_periodic, F)
startTime = datetime.now()
morse_graph, map_graph = CMGDB.ComputeMorseGraph(model)
print(datetime.now() - startTime)
# Save Morse graph
dir_path = os.path.abspath(os.getcwd()) + "/output/"
MG = dir_path + base_name
morse_fname = dir_path + base_name + ".csv"
CMGDB.PlotMorseGraph(morse_graph, cmap).format = 'png'
CMGDB.PlotMorseGraph(morse_graph, cmap).render(MG)
# Save file
morse_nodes = range(morse_graph.num_vertices())
morse_sets = [box + [node]
for node in morse_nodes for box in morse_graph.morse_set_boxes(node)]
np.savetxt(morse_fname, np.array(morse_sets), delimiter=',')
return morse_graph, map_graph
# def run_CMGDB(phase_subdiv, lower_bounds, upper_bounds, phase_periodic, F, base_name):
# # Define the parameters for CMGDB
#
# model = CMGDB.Model(phase_subdiv, lower_bounds, upper_bounds, phase_periodic, F)
#
# startTime = datetime.now()
#
# morse_graph, map_graph = CMGDB.ComputeMorseGraph(model)
#
# print(datetime.now() - startTime)
#
# # Save Morse graph
# dir_path = os.path.abspath(os.getcwd()) + "/output/"
# MG = dir_path + base_name
# morse_fname = dir_path + base_name + ".csv"
#
# CMGDB.PlotMorseGraph(morse_graph).format = 'png'
# CMGDB.PlotMorseGraph(morse_graph).render(MG)
#
# # Save file
# morse_nodes = range(morse_graph.num_vertices())
# morse_sets = [box + [node] for node in morse_nodes for box in morse_graph.morse_set_boxes(node)]
# np.savetxt(morse_fname, np.array(morse_sets), delimiter=',')
#
# return morse_graph, map_graph
def F_K(self, f, rect, K):
"""Input: function f, rectangle rect, and the Lipschit constant in vector form K
Output: Image of rectangle by f, also taking in account the expansion given by K"""
half = len(rect) // 2
im_center = f([rect[i] + (rect[half + i] - rect[i])/2 for i in range(half)])
list1 = []
list2 = []
for i in range(half): # image of the center of the rect +or- lenght * K
list1.append(im_center[i] - (rect[half + i] - rect[i]) * K[i] / 2)
list2.append(im_center[i] + (rect[half + i] - rect[i]) * K[i] / 2)
return list1 + list2
def Morse_sets_vol(self, name_file):
"""Compute the volume of a Morse set"""
with open(name_file, 'r') as f:
lines = csv.reader(f, delimiter=',')
d_vol = dict()
for row in lines:
size = len(row) - 1
half = int(size / 2)
volume_cube = 1
for i in range(half):
volume_cube *= float(row[half + i]) - float(row[i])
if row[size] in d_vol.keys():
d_vol[row[size]] = d_vol[row[size]] + volume_cube
else:
d_vol[row[size]] = volume_cube
return d_vol
def sample_points(self, lower_bounds, upper_bounds, num_pts):
# Sample num_pts in dimension dim, where each
# component of the sampled points are in the
# ranges given by lower_bounds and upper_bounds
dim = len(lower_bounds)
X = np.random.uniform(lower_bounds, upper_bounds, size=(num_pts, dim))
return X
def BoxMapK(self, f, rect, K):
dim = int(len(rect) / 2)
X = CMGDB.CornerPoints(rect)
# Evaluate f at point in X
Y = [f(x) for x in X]
# Get lower and upper bounds of Y
Y_l_bounds = [min([y[d] for y in Y]) - K[d]*(rect[d + dim] - rect[d]) for d in range(dim)]
Y_u_bounds = [max([y[d] for y in Y]) + K[d]*(rect[d + dim] - rect[d]) for d in range(dim)]
return Y_l_bounds + Y_u_bounds
def Box_GP_K(self, learned_f, rect, K, n=-3):
"""learned_f with predicted mean and standard deviation
K Lipschit constant"""
dim = int(len(rect) / 2)
X = CMGDB.CornerPoints(rect)
# print(X)
# Evaluate f at point in X
Y, S = learned_f(X[0])
X = X[1::]
for x_ in X:
y_, s_ = learned_f(x_)
Y = np.concatenate((Y, y_))
S = np.concatenate((S, s_))
# print(f"{Y}\n {S} \n {S_}")
Y_max = Y + S*(2**n)
Y_min = Y - S*(2**n)
# print(f"{Y_min}\n {Y_max}")
# print(f"{np.amin(Y_min[:,0])}\n {np.amax(Y_max[:,1])}")
# Get lower and upper bounds of Y
Y_l_bounds = [np.amin(Y_min[:, d]) - K*(rect[d + dim] - rect[d]) for d in range(dim)]
Y_u_bounds = [np.amax(Y_max[:, d]) + K*(rect[d + dim] - rect[d]) for d in range(dim)]
return Y_l_bounds + Y_u_bounds
def F_GP_K(self, learned_f, rect, K, n=-3):
"""learned_f with predicted mean and standard deviation
K Lipschit constant"""
dim = int(len(rect) / 2)
X = CMGDB.CenterPoint(rect)
# print(X)
# Evaluate f at point in X
Y, S = learned_f(X)
# print(f"{Y}\n {S} \n {S_}")
Y_max = Y + S*(2**n)
Y_min = Y - S*(2**n)
# print(f"{Y_min}\n {Y_max}")
# print(f"{np.amin(Y_min[:,0])}\n {np.amax(Y_max[:,1])}")
# Get lower and upper bounds of Y
Y_l_bounds = [np.amin(Y_min[:, d]) - K*(rect[d + dim] - rect[d]) for d in range(dim)]
Y_u_bounds = [np.amax(Y_max[:, d]) + K*(rect[d + dim] - rect[d]) for d in range(dim)]
return Y_l_bounds + Y_u_bounds
def Box_J(self, f, J, rect):
"""f: function, J: Jacobian matrix, rect: rectangle
Given a rect return the smallest rectangle that contains the image of the
J \cdot rect"""
dim = int(len(rect) / 2)
x = rect[0:dim]
y = f(x)
Jac, _ = J(np.array(x).reshape(-1, dim))
# next, add the sum of the columns
Jac = np.concatenate((Jac, Jac.sum(axis=0).reshape(1, dim)), axis=0)
Y_l_bounds = []
Y_u_bounds = []
for d in range(dim):
Y_l_bounds.append(
np.amin(Jac[:, d]) * (rect[d + dim] - rect[d]) + y[d]
)
Y_u_bounds.append(
np.amax(Jac[:, d]) * (rect[d + dim] - rect[d]) + y[d]
)
return Y_l_bounds + Y_u_bounds
def F_J(self, learned_f, J, rect, lower_bounds, n=-3, weak_multivalued_map=1):
"""f: function, J: Jacobian matrix, rect: rectangle
Given a rect return the smallest rectangle that contains the image of the
J \cdot rect
weak_multivalued_map: the number to compute the subset S of all cells"""
dim = int(len(rect) / 2)
X = rect[0:dim]
Y, S = learned_f(X)
Y_max = Y + S*(2**n)
Y_min = Y - S*(2**n)
size_of_box = [rect[d+dim] - rect[d] for d in range(dim)]
if weak_multivalued_map:
coordinate = [int(
np.rint((rect[d] - lower_bounds[d]) / size_of_box[d])
) % (1+weak_multivalued_map) for d in range(dim)
]
else:
coordinate = True
# print(coordinate)
Y_l_bounds = []
Y_u_bounds = []
# print(f"X {X} \n Y {Y} \n Y_min {Y_min}\n Y_max {Y_max} \n size_of_box {size_of_box}")
if not all(coordinate): # Compute J if all coordinate = 0 module (weak_multivalued_map+1)
print(coordinate)
Jac, _ = J(np.array(X).reshape(-1, dim))
# print(f"J {Jac}")
for d in range(dim):
J_d_norm = np.linalg.norm(Jac[d, :])
Y_l_bounds.append(Y_min[:, d] - J_d_norm * size_of_box[d])
Y_u_bounds.append(Y_max[:, d] + J_d_norm * size_of_box[d])
else:
for d in range(dim):
Y_l_bounds.append(Y_min[:, d])
Y_u_bounds.append(Y_max[:, d])
return Y_l_bounds + Y_u_bounds
def Box_ptwise(self, learned_f, rect, n=-3):
"""learned_f with predicted mean applied to the corner points
and standard deviation applied to the center point"""
dim = int(len(rect) / 2)
X = CMGDB.CenterPoint(rect) + CMGDB.CornerPoints(rect)
# Evaluate f at point in X
Y, S = learned_f(X[0])
Y = Y - S*(2**n)
Y = np.concatenate((Y, Y + 2 * S * (2 ** n)))
X = X[1::]
for x_ in X:
y_, _ = learned_f(x_)
Y = np.concatenate((Y, y_))
# Get lower and upper bounds of Y
Y_l_bounds = [np.amin(Y[:, d]) for d in range(dim)]
Y_u_bounds = [np.amax(Y[:, d]) for d in range(dim)]
return Y_l_bounds + Y_u_bounds
def CornerPoints_noisy(self, rect, noise):
dim = int(len(rect) / 2)
# Get list of intervals
list_intvals = [[rect[d] - noise[d], rect[d + dim] + noise[d]] for d in range(dim)]
# Get points in the cartesian product of intervals
X = [list(u) for u in itertools.product(*list_intvals)]
return X
def Box_noisy_K(self, f, rect, K, noise):
"""Box map with noise"""
dim = int(len(rect) / 2)
X = self.CornerPoints_noisy(rect, noise)
# Evaluate f at point in X
Y = [f(x) for x in X]
# Get lower and upper bounds of Y
Y_l_bounds = [min([y[d] for y in Y]) - K[d]*(rect[d + dim] - rect[d]) for d in range(dim)]
Y_u_bounds = [max([y[d] for y in Y]) + K[d]*(rect[d + dim] - rect[d]) for d in range(dim)]
f_rect = Y_l_bounds + Y_u_bounds
return f_rect