-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchange_detection.py
297 lines (227 loc) · 10.2 KB
/
change_detection.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
"""Change detection algorithms for time series.
Disclaimer
This material was prepared as an account of work sponsored by an agency of the United States Government. Neither the United States Government nor the United States Department of Energy, nor Battelle, nor any of their employees, nor any jurisdiction or organization that has cooperated in the development of these materials, makes any warranty, express or implied, or assumes any legal liability or responsibility for the accuracy, completeness, or usefulness or any information, apparatus, product, software, or process disclosed, or represents that its use would not infringe privately owned rights.
Reference herein to any specific commercial product, process, or service by trade name, trademark, manufacturer, or otherwise does not necessarily constitute or imply its endorsement, recommendation, or favoring by the United States Government or any agency thereof, or Battelle Memorial Institute. The views and opinions of authors expressed herein do not necessarily state or reflect those of the United States Government or any agency thereof.
PACIFIC NORTHWEST NATIONAL LABORATORY
operated by
BATTELLE
for the
UNITED STATES DEPARTMENT OF ENERGY
under Contract DE-AC05-76RL01830
"""
from pathlib import Path
import h5py
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable
from matplotlib.ticker import MaxNLocator
from PIL import Image as im
import ruptures as rpt
from sklearn.decomposition import PCA
from kernel_matrix import KernelMatrix
import matplotlib.style as mplstyle
mplstyle.use('fast')
def format_fn(tick_val, tick_pos):
if int(tick_val) in xs:
return labels[int(tick_val)]
else:
return ''
def pelt_changepoints(data: np.ndarray, cost: callable, pen: float, **pelt_kwargs):
"""Get the pelt changepoints for data, given cost."""
algo = rpt.Pelt(custom_cost=cost, **pelt_kwargs).fit(data[:])
cp = algo.predict(pen=pen)[:-1] # don't include final point
return cp
def evaluate_rank_cost(data: np.ndarray, pen=300, **pelt_kwargs):
"""Evaluate the data using the cosine cost function."""
c = rpt.costs.CostRank()
return pelt_changepoints(data=data, cost=c, pen=pen, **pelt_kwargs)
def evaluate_cosine_cost(data: np.ndarray, pen=35, **pelt_kwargs):
"""Evaluate the data using the cosine cost function."""
c = rpt.costs.CostCosine()
return pelt_changepoints(data=data, cost=c, pen=pen, **pelt_kwargs)
def evaluate_continuous_linear(data: np.ndarray, pen=500, **pelt_kwargs):
"""Evaluate the data using the cosine cost function."""
c = rpt.costs.CostCLinear()
return pelt_changepoints(data=data, cost=c, pen=pen, **pelt_kwargs)
def plot_changepoints(data: np.ndarray, cp: list[float], num_channels: int):
"""Plot the changepoints agains the first "num_channels" components."""
fig, axs = plt.subplots(
data.shape[1], 1, figsize=(10, 2 * data.shape[1]), tight_layout=True
)
for i in range(data.shape[1]):
axs[i].vlines(cp, ymin=min(data[:, i]), ymax=max(data[:, i]), color="r")
axs[i].plot(data[:, i])
return (fig, axs)
def display_changepoints(cp, original_data, pca_data, pca, center, reshape_param):
fig, axs = plt.subplots(len(cp) + 1, 2, figsize=(8, 15), tight_layout=True)
axs[0, 0].set_title("PCA reproduced")
axs[0, 1].set_title("Original Data")
for i, c in enumerate([1] + cp):
image = pca.inverse_transform(pca_data[c]).reshape(reshape_param) + center / 255
axs[i, 0].imshow(-image, interpolation="nearest", cmap="binary")
axs[i, 0].set_yticklabels([])
axs[i, 0].set_xticklabels([])
axs[i, 0].set_yticks([])
axs[i, 0].set_xticks([])
axs[i, 0].set_ylabel(rf"$t={c}$")
image = original_data[:, :, c - 1]
axs[i, 1].imshow(1 - image / 255, interpolation="nearest", cmap="binary")
axs[i, 1].set_yticklabels([])
axs[i, 1].set_xticklabels([])
axs[i, 1].set_yticks([])
axs[i, 1].set_xticks([])
return (fig, axs)
def display_changepoints_no_pca(cp, original_data):
fig, axs = plt.subplots(len(cp) + 1, 1, figsize=(8, 15), tight_layout=True)
axs[0].set_title("Original Data")
for i, c in enumerate([1] + cp):
image = original_data[:, :, c - 1]
axs[i].imshow(-image, interpolation="nearest", cmap="binary")
axs[i].set_yticklabels([])
axs[i].set_xticklabels([])
axs[i].set_yticks([])
axs[i].set_xticks([])
axs[i].set_ylabel(rf"$t={c}$")
return (fig, axs)
def read_h5_data(h5_path: Path, h5_file_key: str = None):
"""Read in data from an h5 file.
Use time slice to slice the time axis and bbox to slice the spacial dimensions.
bbox should follow this format:
bbox = (x0, y0, x1, y1)
(x0, y0)
+-----+
| |
+-----+
(x1, y1)
"""
f = h5py.File(h5_path, "r")
h5_file_key = list(f.keys())[0] if h5_file_key is None else h5_file_key
data = f[h5_file_key][:, :, 1, :]
return data
class ChangepointDetection:
"""Class for the changepoint detection."""
def __init__(
self,
cost_threshold: float = 0.06,
window_size=np.inf,
min_time_between_changepoints:int=10,
):
"""Initialize self."""
self.cost_threshold = cost_threshold
self.window_size = window_size
self.min_time_between_changepoints = min_time_between_changepoints
# set plot defaults
self.dpi = 72
self.height = 7
self.width = 9
self._reset()
def _reset(self):
self.changepoints = [0]
self.detected_times = [0]
def set_plot_size(self, plot_size, dpi):
self.dpi = dpi
self.height = plot_size[0] / self.dpi
self.width = plot_size[1] / self.dpi
self.layout = {}
self.first_plot = True
@staticmethod
def segmented_cost(matrix: np.ndarray, tau:int, start:int=0, end:int=None):
"""Return the cost of segmenting the square matrix at tau."""
sub_matrix_1 = matrix[start:tau, start:tau]
val1 = np.diagonal(sub_matrix_1).sum()
val1 -= sub_matrix_1.sum() / (tau-start)
sub_matrix_2 = matrix[tau:end, tau:end]
val2 = np.diagonal(sub_matrix_2).sum()
val2 -= sub_matrix_2.sum() / (end - tau)
sub_matrix_3 = matrix[start:end, start:end]
val3 = np.diagonal(sub_matrix_3).sum()
val3 -= sub_matrix_3.sum() / (end - start)
return (val3 - val1 - val2) / (end - start)
def maximize_segmented_cost(self, matrix, current_time, window_start):
"""Maximize the segmented cost for the square matrix on an interval."""
xs = np.arange(window_start + 1, current_time, 3).astype(int)
vals = [self.segmented_cost(matrix, x, start=window_start, end=current_time) for x in xs]
if len(vals) > 0:
max_idx = np.argmax(vals)
max_time = xs[max_idx]
max_val = vals[max_idx]
return (max_time, max_val)
else:
return []
def get_changepoint(self, matrix: np.ndarray, step: int):
"""Get the changepoints on the given interval."""
if matrix.shape[0] != matrix.shape[1]:
raise ValueError(
"Cannot do changepoint detection on a rectangular matrix, "
f"({matrix.shape[0]}, {matrix.shape[1]})."
)
current_time = step
window_start = self.changepoints[-1]
vals = self.maximize_segmented_cost(
matrix=matrix, current_time=current_time, window_start=window_start
)
actual_changepoint = False
if len(vals) > 0:
proposed_changepoint = vals[0]
changepoint_amplitude = vals[1]
if current_time - self.changepoints[-1] > self.min_time_between_changepoints:
if changepoint_amplitude > self.cost_threshold:
self.changepoints.append(proposed_changepoint)
self.detected_times.append(current_time)
actual_changepoint = True
else:
actual_changepoint = False
else:
actual_changepoint = False
else:
proposed_changepoint = np.nan
changepoint_amplitude = np.nan
current_time = current_time
actual_changepoint = False
return (
proposed_changepoint,
changepoint_amplitude,
current_time,
actual_changepoint,
)
def get_image(
self,
matrix: np.ndarray,
step: int,
savepath: str,
proposed_changepoint=None,
max_display_window: int = np.inf,
):
"""Get the image associated to the display window."""
fig,ax = plt.subplots(1,1,figsize=(self.width, self.height), dpi=self.dpi)
divider = make_axes_locatable(ax)
cax = divider.append_axes("right", size="5%", pad=0.10)
fig.colorbar(
matplotlib.cm.ScalarMappable(norm=matplotlib.colors.Normalize(-1, 1)),
cax=cax, ticks=[-1, 0, 1],
orientation="vertical",
)
cax.set_ylabel('Similarity', fontsize=24, labelpad=-8)
cax.tick_params(axis='y', which='major', labelsize=22)
window_start = max([0, step - max_display_window])
ax.pcolorfast(matrix, vmin=-1, vmax=1)
ax.invert_yaxis()
for cp in self.changepoints:
if cp - window_start > 0:
ax.axvline(cp - window_start, c="r", alpha=0.6)
ax.tick_params(axis='both', which='major', labelsize=22)
ax.set_xlabel('RHEED Frame', fontsize=24)
ax.set_ylabel('RHEED Frame', fontsize=24)
if self.first_plot:
plt.tight_layout(pad=3)
self.layout = {par : getattr(fig.subplotpars, par) for par in ["left", "right", "bottom", "top", "wspace", "hspace"]}
self.first_plot = False
else:
fig.subplots_adjust(**self.layout)
plt.savefig(savepath, format='png', dpi=self.dpi, transparent=False, facecolor='white', pad_inches=1)
plt.close()
@property
def current_changepoint(self):
"""Return the most recent changepoint"""
return self.changepoints[-1]