| 1 |
""" |
| 2 |
Usage |
| 3 |
|
| 4 |
bokeh_timeline.py sample |
| 5 |
|
| 6 |
Renders media player's internal state graphically using bokeh |
| 7 |
|
| 8 |
Arguments |
| 9 |
sample: sample to report |
| 10 |
|
| 11 |
The output will be written to session's output dir under |
| 12 |
reports/sample.timeline.html |
| 13 |
|
| 14 |
Notice the plot can be zoomed live with the mouse wheel, but you must click |
| 15 |
the button that looks as a distorted 'OP'; it also does pan with mouse drag. |
| 16 |
|
| 17 |
Example |
| 18 |
|
| 19 |
bokeh_timeline.py small.mp4 |
| 20 |
|
| 21 |
will write the output to report/small.mp4.timeline.html |
| 22 |
""" |
| 23 |
|
| 24 |
|
| 25 |
import os |
| 26 |
import sys |
| 27 |
|
| 28 |
from bokeh.layouts import column |
| 29 |
from bokeh.models import LinearAxis, Range1d |
| 30 |
from bokeh.plotting import figure, output_file, show |
| 31 |
|
| 32 |
import fs |
| 33 |
import mpexceptions |
| 34 |
import timeline |
| 35 |
|
| 36 |
|
| 37 |
def main(sample): |
| 38 |
try: |
| 39 |
pathserv = fs.get_path_info_for_active_session() |
| 40 |
except mpexceptions.ExceptionUndefinedSamplesDir: |
| 41 |
print("The env var 'pyglet_mp_samples_dir' is not defined.") |
| 42 |
return 1 |
| 43 |
except mpexceptions.ExceptionNoSessionIsActive: |
| 44 |
print("*** Error, no session active.") |
| 45 |
return 1 |
| 46 |
|
| 47 |
bokeh_render_timeline(pathserv, sample) |
| 48 |
|
| 49 |
|
| 50 |
def bokeh_render_timeline(pathserv, sample): |
| 51 |
infile = pathserv.report_filename(sample, "timeline.pkl", False) |
| 52 |
if not os.path.isfile(infile): |
| 53 |
timeline.save_timeline(pathserv, sample, ".pkl") |
| 54 |
|
| 55 |
timeline_postprocessed = fs.pickle_load(infile) |
| 56 |
info = unpack_timeline(timeline_postprocessed) |
| 57 |
outfile = pathserv.report_filename(sample, "timeline.html", False) |
| 58 |
make_plot(info, outfile) |
| 59 |
|
| 60 |
|
| 61 |
def unpack_timeline(timeline_postprocessed): |
| 62 |
timeline, current_time_nones, audio_time_nones = timeline_postprocessed |
| 63 |
(wall_times, pyglet_times, audio_times, current_times, |
| 64 |
frame_nums, rescheds) = zip(*timeline) |
| 65 |
|
| 66 |
if current_time_nones: |
| 67 |
x_vnones, y_vnones = zip(*current_time_nones) |
| 68 |
else: |
| 69 |
x_vnones, y_vnones = [], [] |
| 70 |
|
| 71 |
if audio_time_nones: |
| 72 |
x_anones, y_anones = zip(*audio_time_nones) |
| 73 |
else: |
| 74 |
x_anones, y_anones = [], [] |
| 75 |
|
| 76 |
return (wall_times, pyglet_times, audio_times, |
| 77 |
current_times, frame_nums, rescheds, |
| 78 |
x_vnones, y_vnones, |
| 79 |
x_anones, y_anones) |
| 80 |
|
| 81 |
|
| 82 |
def make_plot(info, outfile): |
| 83 |
|
| 84 |
(wall_times, pyglet_times, audio_times, |
| 85 |
current_times, frame_nums, rescheds, |
| 86 |
x_vnones, y_vnones, |
| 87 |
x_anones, y_anones) = info |
| 88 |
|
| 89 |
|
| 90 |
output_file(outfile) |
| 91 |
|
| 92 |
|
| 93 |
p = figure( |
| 94 |
tools="pan,wheel_zoom,reset,save", |
| 95 |
y_axis_type="linear", y_range=[0.000, wall_times[-1]], title="timeline", |
| 96 |
x_axis_label='wall_time', y_axis_label='time', |
| 97 |
plot_width=600, plot_height=600 |
| 98 |
) |
| 99 |
|
| 100 |
|
| 101 |
p.line(wall_times, wall_times, legend="wall_time") |
| 102 |
|
| 103 |
p.line(wall_times, current_times, legend="current_times", line_color="red") |
| 104 |
p.line(wall_times, audio_times, legend="audio_times", line_color="orange", line_dash="4 4") |
| 105 |
|
| 106 |
p.circle(x_vnones, y_vnones, legend="current time nones", fill_color="green", size=8) |
| 107 |
p.circle(x_anones, y_anones, legend="audio time nones", fill_color="red", size=6) |
| 108 |
|
| 109 |
|
| 110 |
p.extra_y_ranges = {"frame_num": Range1d(start=0, end=frame_nums[-1])} |
| 111 |
p.line(wall_times, frame_nums, legend="frame_num", |
| 112 |
line_color="black", y_range_name="frame_num") |
| 113 |
p.add_layout(LinearAxis(y_range_name="frame_num", axis_label="frame num"), 'left') |
| 114 |
|
| 115 |
p.legend.location = "bottom_right" |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
q = figure( |
| 121 |
tools="pan,wheel_zoom,reset,save", |
| 122 |
y_axis_type="linear", y_range=[-0.3, 0.3], title="rescheduling time", |
| 123 |
x_axis_label='wall_time', y_axis_label='rescheduling time', |
| 124 |
plot_width=600, plot_height=150 |
| 125 |
) |
| 126 |
q.line(wall_times, rescheds) |
| 127 |
|
| 128 |
show(column(p, q)) |
| 129 |
|
| 130 |
|
| 131 |
def usage(): |
| 132 |
print(__doc__) |
| 133 |
sys.exit(1) |
| 134 |
|
| 135 |
|
| 136 |
def sysargs_to_mainargs(): |
| 137 |
"""builds main args from sys.argv""" |
| 138 |
if len(sys.argv) < 2: |
| 139 |
usage() |
| 140 |
sample = sys.argv[1] |
| 141 |
return sample |
| 142 |
|
| 143 |
|
| 144 |
if __name__ == '__main__': |
| 145 |
sample = sysargs_to_mainargs() |
| 146 |
main(sample) |