-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsetup_rdp.py
279 lines (239 loc) · 8.69 KB
/
setup_rdp.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
"""
setup_rdp.py v0.1
F. Falcioni, P. L. A. Popelier
Script with the function to run the Ramer-Douglas-Peucker algorithm to potential energy surfaces.
Check for updates at github.com/FabioFalcioni
Please, report bugs and issues to fabio.falcioni@manchester.ac.uk
coded by F.Falcioni
"""
from REG import reg
import numpy as np
import matplotlib.pyplot as plt
from optparse import OptionParser
def get_energies_from_file(file, file_type):
"""
Function to parse .txt files containing energies at each step of a PES
"""
energies = []
if file_type == "txt":
with open(file, "r") as f:
lines = f.readlines()[4:]
for line in lines:
for value in line.split()[1:]:
energies.append(float(value))
elif file_type == "grep":
with open(file, "r") as f:
for line in f.readlines():
energies.append(float(line.split(" ")[-1]))
return energies
def find_point_between_two_points(start, end, value):
"""
Function to find the y value of a point between two other points of a function given its x value (i.e. interpolation)
"""
slope = slope_intercept(start[0], start[1], end[0], end[1])
intercept = start[1] - slope * start[0]
y = slope * value + intercept
return y
def slope_intercept(x1, y1, x2, y2):
"""
Function to find the slope and intercept of a line between two points
"""
a = (y2 - y1) / (x2 - x1)
return a
def deviation(point, start, end):
"""
Function to calculate the perpendicular distance of a point from the line defined by other two points.
"""
if np.all(np.equal(start, end)):
return np.linalg.norm(point - start)
return np.divide(
np.abs(np.linalg.norm(np.cross(end - start, start - point))),
np.linalg.norm(end - start),
)
def rdp_rec(M, epsilon, pdist=deviation):
"""
Function to run the RDP algorithm recursively
"""
max_dist = 0.0
index = -1
for i in range(1, M.shape[0]):
d = pdist(M[i], M[0], M[-1])
if d > max_dist:
index = i
max_dist = d
if max_dist > epsilon:
r1 = rdp_rec(M[: index + 1], epsilon, pdist)
r2 = rdp_rec(M[index:], epsilon, pdist)
return np.vstack((r1[:-1], r2))
else:
return np.vstack((M[0], M[-1]))
def rdp(M, epsilon=0, pdist=deviation):
if "numpy" in str(type(M)):
return rdp_rec(M, epsilon, pdist)
return rdp_rec(np.array(M), epsilon, pdist).tolist()
def find_maximum_deviation(M, pdist=deviation):
"""
Function to calculate the maximum perpendicular distance from the line defined between the extreme points of a segment
"""
max_dist = 0.0
index = -1
for i in range(1, M.shape[0]):
d = pdist(M[i], M[0], M[-1])
if d > max_dist:
index = i
max_dist = d
return max_dist
def rmse(true_value, predicted_value):
"""
Function to calculate the Root-Mean-Squared-Error between true and predicted values
"""
if len(true_value) != len(predicted_value): ## Checks if X and Y have the same size
raise ValueError("Arrays must have the same size")
error = [true_value[i] - predicted_value[i] for i in range(len(true_value))]
temp = [a**2 for a in error]
RMSE = (sum(temp) / len(temp)) ** 0.5
return RMSE
def minimum_points_segment_RDP(energy, cc, epsilon):
"""
Function to obtain a segment (or function) with the minimum amount of points given a specific value of epsilon (i.e. deviation)
"""
vector = []
x = []
y = []
for i in range(0, len(energy)):
coordinate = (cc[i], energy[i])
vector.append(coordinate)
new_points = rdp(vector, epsilon=epsilon)
for value in new_points:
new_x, new_y = value
x.append(new_x)
y.append(new_y)
new_interpolated_points = []
for j in range(1, len(new_points)):
for i in range(0, len(cc)):
if cc[i] > new_points[j - 1][0] and cc[i] < new_points[j][0]:
interpolate_point = find_point_between_two_points(
new_points[j - 1], new_points[j], cc[i]
)
new_interpolated_points.append(interpolate_point)
y_reference = y + new_interpolated_points
if energy[0] > energy[-1]:
y_reference.sort(reverse=True)
else:
y_reference.sort(reverse=False)
RMSE = rmse(energy, y_reference)
return y, x, RMSE
def cross_validation_RDP(energy, cc, rmse_confidence=1, step_size=0.01):
"""
Function to run the RDP algorithm X times (depending on step_size) at different values of epsilon and obtain
obtain a function with lowest number of points at that (or below) RMSE of confidence.
"""
new_vector = []
[new_vector.append((cc[i], energy[i])) for i in range(0, len(energy))]
max_epsilon = find_maximum_deviation(np.array(new_vector), pdist=deviation)
epsilon = np.arange(0, max_epsilon, step_size).tolist()
y_values_minimum = []
x_values_minimum = []
rmse_min = []
for eps in epsilon:
y_values, x_values, rmse = minimum_points_segment_RDP(energy, cc, eps)
if rmse <= rmse_confidence:
y_values_minimum.append(y_values)
x_values_minimum.append(x_values)
rmse_min.append(rmse)
return (
min(x_values_minimum, key=len),
min(y_values_minimum, key=len),
rmse_min[x_values_minimum.index(min(x_values_minimum, key=len))],
)
def main():
# Parsing USER INPUT
global segments, cc_new
usage = "usage: %prog [options] arg"
parser = OptionParser(usage)
parser.add_option(
"-f",
"--file",
action="store",
type="string",
dest="file",
help="input file with PES energies",
)
parser.add_option(
"-r",
"--rmse",
action="store",
type="float",
dest="rmse_confidence",
help="Root Mean Squared error of confidence",
)
parser.add_option(
"-c",
"--critical_points",
action="store",
type="string",
dest="critical_points",
help="'AUTO' for automatic search / List of integer values (e.g. cp1,cp2,..cpn) for user "
"defined points / Nothing for single segment PES",
)
(option, args) = parser.parse_args()
print(
"RDP setup: searching for a new polyline with RMSE of confidence {} kJ/mol ...".format(
option.rmse_confidence
)
)
wfn_energies = get_energies_from_file(option.file, file_type="txt")
wfn_energies = np.array(wfn_energies) * 2625.5 # Converting in kJ/mol from a.u.
cc = np.array([i for i in range(1, len(wfn_energies) + 1)])
# Search for critical points in the function
if option.critical_points == "AUTO":
tp = reg.find_critical(wfn_energies, cc, use_inflex=False, min_points=5)
segments = reg.split_segm(
wfn_energies - sum(wfn_energies) / len(wfn_energies), tp
)
cc_new = reg.split_segm(cc, tp)
elif isinstance(option.critical_points, str):
tp = [(int(value)) for value in option.critical_points.split(",")]
print(tp)
segments = reg.split_segm(
wfn_energies - sum(wfn_energies) / len(wfn_energies), tp
)
cc_new = reg.split_segm(cc, tp)
elif not option.critical_points:
segments = [wfn_energies - sum(wfn_energies) / len(wfn_energies)]
cc_new = [cc]
# Creating a figure with the new polyline given the USER input rmse of confidence
plt.rcParams.update({"font.size": 12})
fig = plt.figure(figsize=(9, 5))
props = dict(boxstyle="round", facecolor="wheat", alpha=0.5)
for i in range(0, len(segments)):
x, y, RMSE = cross_validation_RDP(
segments[i], cc_new[i], rmse_confidence=option.rmse_confidence
)
print("RMSE Segment {} = {}".format(i + 1, RMSE))
print("Points of the new polyline for Segment {} = {}".format(i + 1, x))
plt.plot(x, y, marker="o", markersize=10)
plt.plot(x[0], y[0], marker="o", markersize=10, c="red")
plt.plot(x[-1], y[-1], marker="o", markersize=10, c="red")
textstr = "RMSE = {}".format(round(RMSE, 2))
plt.text(
sum(x) / len(x),
sum(y) / len(y),
textstr,
fontsize=12,
verticalalignment="top",
bbox=props,
)
plt.plot(
cc,
wfn_energies - sum(wfn_energies) / len(wfn_energies),
c="#4d4d4d",
marker="o",
markersize=2,
)
plt.xlabel(r"Control Coordinate (N step)")
plt.ylabel(r"Relative Energy (kJ $\mathrm{mol^{-1}}$)")
plt.show()
fig.savefig("RDP_out.png", dpi=300, bbox_inches="tight")
if __name__ == "__main__":
main()