-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevolution_plot.py
More file actions
341 lines (313 loc) · 15.2 KB
/
Copy pathevolution_plot.py
File metadata and controls
341 lines (313 loc) · 15.2 KB
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
import numpy as np
from lmfit import Model, Parameters
import logging
import matplotlib.patches as mpatches
import matplotlib.pyplot as plt
from matplotlib.ticker import FuncFormatter
import matplotlib.patches as patches
import matplotlib.colors as mcolors
import time
from datetime import datetime
from constants import *
from plotly_plots import normalize_spectrum
from preprocessing import get_info_total, load_dataset, show_image, download_csv_if_needed
from similarity_plot import get_ticks_for_helsinki_tz, format_time_to_helsinki, get_extended_datetimes
from gauss_plot import plot_rectangles, fit_model
def plot_evolution(ds, start, end, output_path, name_overide=None):
start_time = time.time()
logging.info(f"Plotting peak evolution for requested range\nSTART: {start}\nEND: {end}")
assert start < end
images = []
all_sensors = np.unique(ds.sensor)
for sensor_id in all_sensors:
filtered_ds = ds.where(
(ds.sensor == sensor_id) &
(ds['datetime'] > start) &
(ds['datetime'] < end),
drop=True,
other=0
)
if len(filtered_ds['datetime'].values) == 0:
continue
measurement_datetimes = np.array([dt.astimezone(HELSINKI_TZ) for dt in filtered_ds['datetime'].values])
extended_datetimes = np.array([dt.astimezone(HELSINKI_TZ) for dt in get_extended_datetimes(ds, sensor_id, start, end)])
assert extended_datetimes[0] <= start # pre-extension datetime should be before start or at start
assert extended_datetimes[-1] >= end # post-extension datetime should be after end or at end
unix_timestamps = np.array([t.timestamp() for t in extended_datetimes]) # Unix/Posix epochs (counted from UTC)
voronoi_edges = (unix_timestamps[:-1] + unix_timestamps[1:]) / 2
first_edge = pd.to_datetime(voronoi_edges[0], unit='s', utc=True).tz_convert('Europe/Helsinki')
last_edge = pd.to_datetime(voronoi_edges[-1], unit='s', utc=True).tz_convert('Europe/Helsinki')
logging.info(f"Voronoi edges from: {first_edge}\nVoronoi edges to: {last_edge}")
spectra = np.vstack(filtered_ds['spectrum'].values)
freq_factor = filtered_ds['frequency_scaling_factor'].values[0]
freq_start = filtered_ds['frequency_start_index'].values[0]
spectra_len = spectra.shape[1]
frequencies = np.array([(bin+freq_start)*freq_factor for bin in range(spectra_len)])
fig, axes = plt.subplots(
1, 2,
figsize=(14, 8),
gridspec_kw={'width_ratios': [8, 1]},
sharey=True
)
ax0 = axes[0]
ax1 = axes[1]
ax0.grid(True, alpha=0.3)
gauss_count = sum(1 for cfg in FITTING_MODEL if cfg['type'] == 'peak')
rmse_values = []
for j, spectrum in enumerate(spectra):
normalized_spectrum = normalize_spectrum(spectrum, frequencies, NORMALIZATION_LIMIT)
(_, result, _, rmse) = fit_model(frequencies, normalized_spectrum)
rmse_values.append(rmse)
measurement_datetime = measurement_datetimes[j]
measurment_timestamp = measurement_datetime.timestamp()
start_edge = voronoi_edges[j]
end_edge = voronoi_edges[j+1]
freq_grid = np.linspace(0, 700, 175) # Increase num of points to have better resolution
model_intensity = result.eval(x=freq_grid) # Evaluate model at frequency grid
cmap = plt.cm.turbo
cmap_colors = cmap(np.linspace(0, 1, cmap.N))
cmap_colors[:, -1] = 0.8 # Set alpha channel to 0.8
cmap = mcolors.ListedColormap(cmap_colors)
norm = mcolors.Normalize(vmin=0, vmax=np.max(model_intensity))
# Draw sections of horizontal colored stripe
for i in range(len(freq_grid)-1):
model_intensity_at_freq = model_intensity[i]
color = cmap(norm(model_intensity_at_freq))
ax0.fill_betweenx(
[start_edge, end_edge], # y-values (time range)
freq_grid[i], # x-values (frequency)
freq_grid[i+1],
color=color,
alpha=0.8,
linewidth=0
)
# Draw letters
for i, cfg in enumerate(FITTING_MODEL):
if cfg['type'] == 'peak':
prefix = f'g{i}_'
if f'{prefix}center' in result.params:
center = result.params[f'{prefix}center'].value
letter = chr(64 + i)
ax0.text(center, measurment_timestamp, letter, fontsize=7,
ha='center', va='center', weight='bold', color='black'
)
# Draw ranges
plot_rectangles(ax0, gauss_count, fill=False)
ax1.scatter(rmse_values, [d.timestamp() for d in measurement_datetimes], color='grey', edgecolors='black')
ax1.plot(rmse_values, [d.timestamp() for d in measurement_datetimes], 'k-')
datetimes_for_ticks = get_ticks_for_helsinki_tz(start, end)
timestamps_for_ticks = [d.timestamp() for d in datetimes_for_ticks]
ax0.set_yticks(timestamps_for_ticks, labels=datetimes_for_ticks)
ax0.yaxis.set_major_formatter(FuncFormatter(format_time_to_helsinki))
ax0.set_xlabel('Frequency, Hz', fontsize=14)
ax0.set_ylabel('DateTime', fontsize=14)
ax0.set_xlim(0, 700)
ax0.set_ylim(start.timestamp(), end.timestamp())
ax1.set_xlabel('RMSE', fontsize=14)
# Add colorbar
sm = plt.cm.ScalarMappable(cmap=cmap, norm=norm)
sm.set_array([])
cbar = plt.colorbar(sm, ax=ax0, label='Intensity, %')
cbar.ax.tick_params(labelsize=10)
handles = []
for i, cfg in enumerate(FITTING_MODEL):
if cfg['type'] == 'peak':
letter = chr(64 + i)
center = cfg['center_range']
patch = mpatches.Patch(color='None', label=f'{letter}: center of peak in {center} Hz range')
handles.append(patch)
datapoints_info = "\nEvolution of total model intensity (color)\nand gauss peak center positions (letters)\nfor fitted individual acoustic spectra\n{}Sensor: {}".format(get_info_total(filtered_ds), sensor_id, FITTING_WINDOW_MIN, FITTING_WINDOW_MAX)
patch = mpatches.Patch(color='None', label=datapoints_info)
handles.append(patch)
window_text = f"\nWindow used for fitting: {FITTING_WINDOW_MIN} to {FITTING_WINDOW_MAX} Hz"
patch = mpatches.Patch(color='None', label=window_text)
handles.append(patch)
fig.legend(bbox_to_anchor=(0.98, 0.98), handles=handles, fontsize=9)
patch = mpatches.Patch(color='None', label=f"Gauss peak N: Center, FWHM, Amplitude")
handles.append(patch)
plt.tight_layout()
plt.subplots_adjust(right=0.74)
if name_overide:
img_pathname = os.path.join(output_path, name_overide)
else:
first_datetime = min(measurement_datetimes)
last_datetime = max(measurement_datetimes)
img_pathname = create_artifact_pathname(
"evolution", output_path, sensor_id, first_datetime, last_datetime, "png"
)
images.append(img_pathname)
os.makedirs(output_path, exist_ok=True)
plt.savefig(img_pathname, dpi=300, bbox_inches='tight')
plt.close()
logging.info(f"PNG file {img_pathname} was created!")
total_time = time.time() - start_time
logging.info(f"Total plot creation time: {total_time:.2f} seconds")
return images
# def plot_evolution_old(ds, start, end, output_path, name_overide=None):
# start_time = time.time()
# logging.info(f"Plotting peak evolution for requested range\nSTART: {start}\nEND: {end}")
# assert start < end
# images = []
# all_sensors = np.unique(ds.sensor)
#
# for sensor_id in all_sensors:
# filtered_ds = ds.where(
# (ds.sensor == sensor_id) &
# (ds['datetime'] > start) &
# (ds['datetime'] < end),
# drop=True,
# other=0
# )
# if len(filtered_ds['datetime'].values) == 0:
# continue
#
# measurement_datetimes = np.array([dt.astimezone(HELSINKI_TZ) for dt in filtered_ds['datetime'].values])
# extended_datetimes = get_extended_datetimes(ds, sensor_id, start, end)
# extended_datetimes = np.array([dt.astimezone(HELSINKI_TZ) for dt in extended_datetimes])
# assert extended_datetimes[0] <= start # pre-extension datetime should be before start or at start
# assert extended_datetimes[-1] >= end # post-extension datetime should be after end or at end
#
# unix_epochs = np.array([t.timestamp() for t in extended_datetimes]) # Unix/Posix epochs (counted from UTC)
# voronoi_edges = (unix_epochs[:-1] + unix_epochs[1:]) / 2
# leftmost_edge = pd.to_datetime(voronoi_edges[0], unit='s', utc=True).tz_convert('Europe/Helsinki')
# rightmost_edge = pd.to_datetime(voronoi_edges[-1], unit='s', utc=True).tz_convert('Europe/Helsinki')
# logging.info(f"Voronoi edges from: {leftmost_edge}\nVoronoi edges to: {rightmost_edge}")
#
# spectra = np.vstack(filtered_ds['spectrum'].values)
# freq_factor = filtered_ds['frequency_scaling_factor'].values[0]
# freq_start = filtered_ds['frequency_start_index'].values[0]
# spectra_len = spectra.shape[1]
# frequencies = np.array([(bin+freq_start)*freq_factor for bin in range(spectra_len)])
#
# fig, axes = plt.subplots(
# 1, 2,
# figsize=(14, 8),
# gridspec_kw={'width_ratios': [6, 1]},
# sharey=True
# )
# ax0 = axes[0]
# ax1 = axes[1]
# ax0.grid(True, alpha=0.3)
# gauss_count = sum(1 for cfg in FITTING_MODEL if cfg['type'] == 'peak')
#
# rmse_values = []
#
# for j, spectrum in enumerate(spectra):
# normalized_spectrum = normalize_spectrum(spectrum, frequencies, NORMALIZATION_LIMIT)
# (_, result, _, rmse) = fit_model(frequencies, normalized_spectrum)
# rmse_values.append(rmse)
#
# p = result.params
# measurement_datetime = measurement_datetimes[j]
# left_edge = voronoi_edges[j]
# right_edge = voronoi_edges[j+1]
# duration = right_edge - left_edge
#
# peak_data = []
# max_fit_intensity = 0
#
# for i, cfg in enumerate(FITTING_MODEL):
# if cfg['type'] == 'peak':
# prefix = f'g{i}_'
# center = p[f'{prefix}center'].value
# fwhm = p[f'{prefix}fwhm'].value
# amplitude = p[f'{prefix}amplitude'].value
# intensity = amplitude * fwhm
# max_fit_intensity = max(max_fit_intensity, intensity)
# peak_data.append((i, center, fwhm, amplitude, intensity))
#
# for i, center, fwhm, amplitude, intensity in peak_data:
# color = COLORMAP_FOR_GAUSSIANS(i / gauss_count)
# rect = patches.Rectangle(
# (center - fwhm/2, left_edge),
# fwhm,
# duration,
# linewidth=1,
# edgecolor=color,
# facecolor=color,
# alpha = intensity / max_fit_intensity
# )
# ax0.add_patch(rect)
# ax0.scatter(center, measurement_datetime.timestamp(), color=color, edgecolors='black')
#
# plot_rectangles(ax0, gauss_count, fill=False)
#
# ax1.scatter(rmse_values, [d.timestamp() for d in measurement_datetimes], color='grey', edgecolors='black')
# ax1.plot(rmse_values, [d.timestamp() for d in measurement_datetimes], 'k-')
#
# datetimes_for_ticks = get_ticks_for_helsinki_tz(start, end)
# timestamps_for_ticks = [d.timestamp() for d in datetimes_for_ticks]
# ax0.set_yticks(timestamps_for_ticks, labels=datetimes_for_ticks)
# ax0.yaxis.set_major_formatter(FuncFormatter(format_time_to_helsinki))
#
# ax0.set_xlabel('Frequency, Hz', fontsize=14)
# ax0.set_ylabel('DateTime', fontsize=14)
# ax0.set_xlim(0, 700)
# ax0.set_ylim(start.timestamp(), end.timestamp())
# ax1.set_xlabel('RMSE', fontsize=14)
#
# handles = []
# for i, cfg in enumerate(FITTING_MODEL):
# if cfg['type'] == 'peak':
# color = COLORMAP_FOR_GAUSSIANS(i / gauss_count)
# center = cfg['center_range']
# handles.append(mpatches.Patch(color=color, label=f'Gauss peak {i}: {center} Hz'))
# patch = mpatches.Patch(color='None', label=f"Gauss peak N: range for center position")
# handles.append(patch)
# datapoints_info = "\nEvolution of FWHM (colored areas), \ncenter positions (dots) and\nintensity within each fit (color saturation)\nof gauss peaks for fitted acoustic spectra\n{}Sensor: {}".format(get_info_total(filtered_ds), sensor_id, FITTING_WINDOW_MIN, FITTING_WINDOW_MAX)
# patch = mpatches.Patch(color='None', label=datapoints_info)
# handles.append(patch)
# window_text = f"\nWindow used for fitting: {FITTING_WINDOW_MIN} to {FITTING_WINDOW_MAX} Hz"
# patch = mpatches.Patch(color='None', label=window_text)
# handles.append(patch)
# fig.legend(bbox_to_anchor=(0.98, 0.98), handles=handles, fontsize=9)
#
# plt.tight_layout()
# plt.subplots_adjust(right=0.74)
#
# if name_overide:
# img_pathname = os.path.join(output_path, name_overide)
# else:
# first_datetime = min(measurement_datetimes)
# last_datetime = max(measurement_datetimes)
# img_pathname = create_artifact_pathname(
# "evolution", output_path, sensor_id, first_datetime, last_datetime, "png"
# )
#
# images.append(img_pathname)
# os.makedirs(output_path, exist_ok=True)
# plt.savefig(img_pathname, dpi=300, bbox_inches='tight')
# plt.close()
# logging.info(f"PNG file {img_pathname} was created!")
#
# total_time = time.time() - start_time
# logging.info(f"Total plot creation time: {total_time:.2f} seconds")
# return images
def plot_peak_evolution_example():
sensors = [21]
start = datetime(2024, 8, 11, 0, tzinfo = HELSINKI_TZ)
end = datetime(2024, 8, 14, 0, 0, tzinfo = HELSINKI_TZ)
csv_files = download_csv_if_needed(
sensors,
start.astimezone(UTC_TZ),
end.astimezone(UTC_TZ),
DATA_DIR
)
filtered_ds = load_dataset(csv_files, True, start, end)
evolution_plots = plot_evolution(
filtered_ds,
start,
end,
OUTPUT_DIR,
)
return evolution_plots
if __name__ == "__main__":
logging.basicConfig(
level=logging.INFO,
format="%(message)s",
handlers=[
logging.StreamHandler() # Log to console
]
)
evolution_example = plot_peak_evolution_example()
show_image(evolution_example[0])