Skip to content

Commit 0fb97ef

Browse files
committed
Support moving average for timeseries in osg_stats.py
1 parent acb36cb commit 0fb97ef

File tree

1 file changed

+26
-14
lines changed

1 file changed

+26
-14
lines changed

scripts/osg_stats.py

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,14 @@
7272
help='Threshold for hist_over.')
7373
@click.option('--show_common_path_prefix', is_flag=True,
7474
help='Show common path prefix when applied to multiple files.')
75+
@click.option('--moving_average_window', type=click.IntRange(min=1),
76+
help='Transform timeseries values to moving average with window of given number of points.')
7577
@click.argument('path', type=click.Path(), nargs=-1)
7678
def main(print_keys, regexp_match, timeseries, hist, hist_ratio, stdev_hist, plot, stats, precision,
7779
timeseries_sum, stats_sum, begin_frame, end_frame, path,
7880
cumulative_timeseries, cumulative_timeseries_sum, frame_number_name,
7981
hist_threshold, threshold_name, threshold_value, show_common_path_prefix, stats_sort_by,
80-
timeseries_delta, timeseries_delta_sum, stats_table_format):
82+
timeseries_delta, timeseries_delta_sum, stats_table_format, moving_average_window):
8183
sources = {v: list(read_data(v)) for v in path} if path else {'stdin': list(read_data(None))}
8284
if not show_common_path_prefix and len(sources) > 1:
8385
longest_common_prefix = os.path.commonprefix(list(sources.keys()))
@@ -94,15 +96,19 @@ def matching_keys(patterns):
9496
if regexp_match:
9597
return [key for pattern in patterns for key in keys if re.search(pattern, key)]
9698
return patterns
99+
convolve = None
100+
if moving_average_window is not None:
101+
convolve = numpy.ones(moving_average_window) / float(moving_average_window)
97102
if timeseries:
98-
draw_timeseries(sources=frames, keys=matching_keys(timeseries), add_sum=timeseries_sum,
103+
draw_timeseries(sources=frames, keys=matching_keys(timeseries), add_sum=timeseries_sum, convolve=convolve,
99104
begin_frame=begin_frame, end_frame=end_frame)
100105
if cumulative_timeseries:
101-
draw_cumulative_timeseries(sources=frames, keys=matching_keys(cumulative_timeseries), add_sum=cumulative_timeseries_sum,
102-
begin_frame=begin_frame, end_frame=end_frame)
106+
draw_cumulative_timeseries(sources=frames, keys=matching_keys(cumulative_timeseries),
107+
add_sum=cumulative_timeseries_sum, convolve=convolve, begin_frame=begin_frame,
108+
end_frame=end_frame)
103109
if timeseries_delta:
104110
draw_timeseries_delta(sources=frames, keys=matching_keys(timeseries_delta), add_sum=timeseries_delta_sum,
105-
begin_frame=begin_frame, end_frame=end_frame)
111+
convolve=convolve, begin_frame=begin_frame, end_frame=end_frame)
106112
if hist:
107113
draw_hists(sources=frames, keys=matching_keys(hist))
108114
if hist_ratio:
@@ -173,45 +179,45 @@ def collect_unique_keys(sources):
173179
return sorted(result)
174180

175181

176-
def draw_timeseries(sources, keys, add_sum, begin_frame, end_frame):
182+
def draw_timeseries(sources, keys, add_sum, convolve, begin_frame, end_frame):
177183
fig, ax = matplotlib.pyplot.subplots()
178184
x = numpy.array(range(begin_frame, end_frame))
179185
for name, frames in sources.items():
180186
for key in keys:
181-
y = frames[key]
187+
y = maybe_convolve(frames[key], convolve)
182188
ax.plot(x[:len(y)], y, label=f'{key}:{name}')
183189
if add_sum:
184-
y = sum_arrays_with_none([frames[k] for k in keys])
190+
y = sum_arrays_with_none([maybe_convolve(frames[k], convolve) for k in keys])
185191
ax.plot(x[:len(y)], y, label=f'sum:{name}', linestyle='--')
186192
ax.grid(True)
187193
ax.legend()
188194
fig.canvas.manager.set_window_title('timeseries')
189195

190196

191-
def draw_cumulative_timeseries(sources, keys, add_sum, begin_frame, end_frame):
197+
def draw_cumulative_timeseries(sources, keys, add_sum, convolve, begin_frame, end_frame):
192198
fig, ax = matplotlib.pyplot.subplots()
193199
x = numpy.array(range(begin_frame, end_frame))
194200
for name, frames in sources.items():
195201
for key in keys:
196-
y = cumsum_with_none(frames[key])
202+
y = cumsum_with_none(maybe_convolve(frames[key], convolve))
197203
ax.plot(x[:len(y)], y, label=f'{key}:{name}')
198204
if add_sum:
199-
y = sum_arrays_with_none([cumsum_with_none(frames[k]) for k in keys])
205+
y = sum_arrays_with_none([cumsum_with_none(maybe_convolve(frames[k], convolve)) for k in keys])
200206
ax.plot(x[:len(y)], y, label=f'sum:{name}', linestyle='--')
201207
ax.grid(True)
202208
ax.legend()
203209
fig.canvas.manager.set_window_title('cumulative_timeseries')
204210

205211

206-
def draw_timeseries_delta(sources, keys, add_sum, begin_frame, end_frame):
212+
def draw_timeseries_delta(sources, keys, add_sum, convolve, begin_frame, end_frame):
207213
fig, ax = matplotlib.pyplot.subplots()
208214
x = numpy.array(range(begin_frame + 1, end_frame))
209215
for name, frames in sources.items():
210216
for key in keys:
211-
y = diff_with_none(frames[key])
217+
y = diff_with_none(maybe_convolve(frames[key], convolve))
212218
ax.plot(x[:len(y)], y, label=f'{key}:{name}')
213219
if add_sum:
214-
y = sum_arrays_with_none([diff_with_none(frames[k]) for k in keys])
220+
y = sum_arrays_with_none([diff_with_none(maybe_convolve(frames[k], convolve)) for k in keys])
215221
ax.plot(x[:len(y)], y, label=f'sum:{name}', linestyle='--')
216222
ax.grid(True)
217223
ax.legend()
@@ -422,5 +428,11 @@ def sum_arrays_with_none(arrays):
422428
return numpy.array(result)
423429

424430

431+
def maybe_convolve(values, convolve):
432+
if convolve is None:
433+
return values
434+
return numpy.convolve([v or 0 for v in values], convolve, 'valid')
435+
436+
425437
if __name__ == '__main__':
426438
main()

0 commit comments

Comments
 (0)