mirror of
https://gitlab.com/OpenMW/openmw.git
synced 2025-01-29 18:32:36 +00:00
Merge branch 'stats' into 'master'
Small QOL improvement for osg_stats See merge request OpenMW/openmw!1451
This commit is contained in:
commit
3de8645be9
@ -12,11 +12,14 @@ import numpy
|
||||
import statistics
|
||||
import sys
|
||||
import termtables
|
||||
import re
|
||||
|
||||
|
||||
@click.command()
|
||||
@click.option('--print_keys', is_flag=True,
|
||||
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,
|
||||
help='Show a graph for given metric over time.')
|
||||
@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>.')
|
||||
@click.option('--stats', type=str, multiple=True,
|
||||
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,
|
||||
help='Add a graph to timeseries for a sum per frame of all given timeseries metrics.')
|
||||
@click.option('--commulative_timeseries_sum', is_flag=True,
|
||||
@ -54,7 +59,7 @@ import termtables
|
||||
@click.option('--threshold_value', type=float, default=1.05/60,
|
||||
help='Threshold for hist_over.')
|
||||
@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,
|
||||
commulative_timeseries, commulative_timeseries_sum, frame_number_name,
|
||||
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:
|
||||
print(v)
|
||||
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)
|
||||
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)
|
||||
if 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:
|
||||
draw_plots(sources=frames, plots=plot)
|
||||
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:
|
||||
draw_hist_threshold(sources=frames, keys=hist_threshold, begin_frame=begin_frame,
|
||||
threshold_name=threshold_name, threshold_value=threshold_value)
|
||||
@ -140,6 +145,12 @@ def collect_unique_keys(sources):
|
||||
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):
|
||||
fig, ax = matplotlib.pyplot.subplots()
|
||||
x = numpy.array(range(begin_frame, end_frame))
|
||||
@ -242,13 +253,13 @@ def draw_plots(sources, plots):
|
||||
fig.canvas.set_window_title('plots')
|
||||
|
||||
|
||||
def print_stats(sources, keys, stats_sum):
|
||||
def print_stats(sources, keys, stats_sum, precision):
|
||||
stats = list()
|
||||
for name, frames in sources.items():
|
||||
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:
|
||||
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())
|
||||
termtables.print(
|
||||
[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]
|
||||
|
||||
|
||||
def fixed_float(value, precision):
|
||||
return '{v:.{p}f}'.format(v=value, p=precision) if precision else value
|
||||
|
||||
|
||||
def sum_multiple(frames, keys):
|
||||
result = collections.Counter()
|
||||
for key in keys:
|
||||
@ -292,17 +307,17 @@ def sum_multiple(frames, 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(
|
||||
source=source,
|
||||
key=key,
|
||||
number=len(values),
|
||||
min=min(values),
|
||||
max=max(values),
|
||||
mean=statistics.mean(values),
|
||||
median=statistics.median(values),
|
||||
stdev=statistics.stdev(values),
|
||||
q95=numpy.quantile(values, 0.95),
|
||||
min=fixed_float(min(values), precision),
|
||||
max=fixed_float(max(values), precision),
|
||||
mean=fixed_float(statistics.mean(values), precision),
|
||||
median=fixed_float(statistics.median(values), precision),
|
||||
stdev=fixed_float(statistics.stdev(values), precision),
|
||||
q95=fixed_float(numpy.quantile(values, 0.95), precision),
|
||||
)
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user