Merge branch 'stats' into 'master'

Small QOL improvement for osg_stats

See merge request OpenMW/openmw!1451
This commit is contained in:
psi29a 2021-12-01 15:30:20 +00:00
commit 3de8645be9

View File

@ -12,11 +12,14 @@ import numpy
import statistics import statistics
import sys import sys
import termtables import termtables
import re
@click.command() @click.command()
@click.option('--print_keys', is_flag=True, @click.option('--print_keys', is_flag=True,
help='Print a list of all present keys in the input file.') help='Print a list of all present keys in the input file.')
@click.option('--regexp_match', is_flag=True,
help='Use all metric that match given key. Can be used with stats and timeseries.')
@click.option('--timeseries', type=str, multiple=True, @click.option('--timeseries', type=str, multiple=True,
help='Show a graph for given metric over time.') help='Show a graph for given metric over time.')
@click.option('--commulative_timeseries', type=str, multiple=True, @click.option('--commulative_timeseries', type=str, multiple=True,
@ -35,6 +38,8 @@ import termtables
'between Physics Actors and physics_time_taken. Format: --plot <x> <y> <function>.') 'between Physics Actors and physics_time_taken. Format: --plot <x> <y> <function>.')
@click.option('--stats', type=str, multiple=True, @click.option('--stats', type=str, multiple=True,
help='Print table with stats for a given metric containing min, max, mean, median etc.') help='Print table with stats for a given metric containing min, max, mean, median etc.')
@click.option('--precision', type=int,
help='Format floating point numbers with given precision')
@click.option('--timeseries_sum', is_flag=True, @click.option('--timeseries_sum', is_flag=True,
help='Add a graph to timeseries for a sum per frame of all given timeseries metrics.') help='Add a graph to timeseries for a sum per frame of all given timeseries metrics.')
@click.option('--commulative_timeseries_sum', is_flag=True, @click.option('--commulative_timeseries_sum', is_flag=True,
@ -54,7 +59,7 @@ import termtables
@click.option('--threshold_value', type=float, default=1.05/60, @click.option('--threshold_value', type=float, default=1.05/60,
help='Threshold for hist_over.') help='Threshold for hist_over.')
@click.argument('path', type=click.Path(), nargs=-1) @click.argument('path', type=click.Path(), nargs=-1)
def main(print_keys, timeseries, hist, hist_ratio, stdev_hist, plot, stats, def main(print_keys, regexp_match, timeseries, hist, hist_ratio, stdev_hist, plot, stats, precision,
timeseries_sum, stats_sum, begin_frame, end_frame, path, timeseries_sum, stats_sum, begin_frame, end_frame, path,
commulative_timeseries, commulative_timeseries_sum, frame_number_name, commulative_timeseries, commulative_timeseries_sum, frame_number_name,
hist_threshold, threshold_name, threshold_value): hist_threshold, threshold_name, threshold_value):
@ -68,10 +73,10 @@ def main(print_keys, timeseries, hist, hist_ratio, stdev_hist, plot, stats,
for v in keys: for v in keys:
print(v) print(v)
if timeseries: if timeseries:
draw_timeseries(sources=frames, keys=timeseries, add_sum=timeseries_sum, draw_timeseries(sources=frames, keys=matching_keys(keys, timeseries, regexp_match), add_sum=timeseries_sum,
begin_frame=begin_frame, end_frame=end_frame) begin_frame=begin_frame, end_frame=end_frame)
if commulative_timeseries: if commulative_timeseries:
draw_commulative_timeseries(sources=frames, keys=commulative_timeseries, add_sum=commulative_timeseries_sum, draw_commulative_timeseries(sources=frames, keys=matching_keys(keys, commulative_timeseries, regexp_match), add_sum=commulative_timeseries_sum,
begin_frame=begin_frame, end_frame=end_frame) begin_frame=begin_frame, end_frame=end_frame)
if hist: if hist:
draw_hists(sources=frames, keys=hist) draw_hists(sources=frames, keys=hist)
@ -82,7 +87,7 @@ def main(print_keys, timeseries, hist, hist_ratio, stdev_hist, plot, stats,
if plot: if plot:
draw_plots(sources=frames, plots=plot) draw_plots(sources=frames, plots=plot)
if stats: if stats:
print_stats(sources=frames, keys=stats, stats_sum=stats_sum) print_stats(sources=frames, keys=matching_keys(keys, stats, regexp_match), stats_sum=stats_sum, precision=precision)
if hist_threshold: if hist_threshold:
draw_hist_threshold(sources=frames, keys=hist_threshold, begin_frame=begin_frame, draw_hist_threshold(sources=frames, keys=hist_threshold, begin_frame=begin_frame,
threshold_name=threshold_name, threshold_value=threshold_value) threshold_name=threshold_name, threshold_value=threshold_value)
@ -140,6 +145,12 @@ def collect_unique_keys(sources):
return sorted(result) return sorted(result)
def matching_keys(keys, patterns, regexp_match):
if regexp_match:
return { key for pattern in patterns for key in keys if re.search(pattern, key) }
return keys
def draw_timeseries(sources, keys, add_sum, begin_frame, end_frame): def draw_timeseries(sources, keys, add_sum, begin_frame, end_frame):
fig, ax = matplotlib.pyplot.subplots() fig, ax = matplotlib.pyplot.subplots()
x = numpy.array(range(begin_frame, end_frame)) x = numpy.array(range(begin_frame, end_frame))
@ -242,13 +253,13 @@ def draw_plots(sources, plots):
fig.canvas.set_window_title('plots') fig.canvas.set_window_title('plots')
def print_stats(sources, keys, stats_sum): def print_stats(sources, keys, stats_sum, precision):
stats = list() stats = list()
for name, frames in sources.items(): for name, frames in sources.items():
for key in keys: for key in keys:
stats.append(make_stats(source=name, key=key, values=filter_not_none(frames[key]))) stats.append(make_stats(source=name, key=key, values=filter_not_none(frames[key]), precision=precision))
if stats_sum: if stats_sum:
stats.append(make_stats(source=name, key='sum', values=sum_multiple(frames, keys))) stats.append(make_stats(source=name, key='sum', values=sum_multiple(frames, keys), precision=precision))
metrics = list(stats[0].keys()) metrics = list(stats[0].keys())
termtables.print( termtables.print(
[list(v.values()) for v in stats], [list(v.values()) for v in stats],
@ -282,6 +293,10 @@ def filter_not_none(values):
return [v for v in values if v is not None] return [v for v in values if v is not None]
def fixed_float(value, precision):
return '{v:.{p}f}'.format(v=value, p=precision) if precision else value
def sum_multiple(frames, keys): def sum_multiple(frames, keys):
result = collections.Counter() result = collections.Counter()
for key in keys: for key in keys:
@ -292,17 +307,17 @@ def sum_multiple(frames, keys):
return numpy.array([result[k] for k in sorted(result.keys())]) return numpy.array([result[k] for k in sorted(result.keys())])
def make_stats(source, key, values): def make_stats(source, key, values, precision):
return collections.OrderedDict( return collections.OrderedDict(
source=source, source=source,
key=key, key=key,
number=len(values), number=len(values),
min=min(values), min=fixed_float(min(values), precision),
max=max(values), max=fixed_float(max(values), precision),
mean=statistics.mean(values), mean=fixed_float(statistics.mean(values), precision),
median=statistics.median(values), median=fixed_float(statistics.median(values), precision),
stdev=statistics.stdev(values), stdev=fixed_float(statistics.stdev(values), precision),
q95=numpy.quantile(values, 0.95), q95=fixed_float(numpy.quantile(values, 0.95), precision),
) )