[encoder] Improved motion estimation.
[schroedinger/research-port.git] / misc / plot_stuff
blob855a180106e9f7daedccb78dbc475b9e18661dbf
1 #!/usr/bin/env python
3 import cairo;
4 import os;
5 import locale;
6 import Numeric;
7 import math;
11 class Plot:
12 def __init__(self, width, height):
13 self.width=width;
14 self.height=height;
16 self.margin_top=20;
17 self.margin_bottom=40;
18 self.margin_left=40;
19 self.margin_right=20;
21 self.range_xmin=0.0;
22 self.range_xmax=1.0;
23 self.range_ymin=0.0;
24 self.range_ymax=1.0;
26 self.symbol_size = 2;
27 self.tick_size = 5;
29 self.with_lines = 1;
31 self.surface = cairo.ImageSurface (cairo.FORMAT_ARGB32,
32 self.width, self.height);
33 self.ctx = cairo.Context (self.surface);
35 def set_x_range (self, xmin, xmax):
36 if xmin == xmax:
37 print "Invalid x range";
38 xmin -= 1;
39 xmax += 1;
40 self.range_xmin=float(xmin);
41 self.range_xmax=float(xmax);
43 def set_y_range (self, ymin, ymax):
44 print "y range %g %g" % (ymin, ymax);
45 if ymin == ymax:
46 print "Invalid y range";
47 ymin -= 1;
48 ymax += 1;
49 self.range_ymin=float(ymin);
50 self.range_ymax=float(ymax);
52 def set_x_range_data (self, data):
53 xmin = min(data);
54 xmax = max(data);
55 self.set_x_range(xmin,xmax);
57 def set_y_range_data (self, data):
58 ymin = min(data);
59 ymax = max(data);
60 self.set_y_range(ymin,ymax);
62 def set_sane_ticks (self):
63 logrange = math.log10((self.range_xmax - self.range_xmin)/3);
64 tick = math.pow(10, math.floor(logrange));
65 self.tick_major_x = tick;
67 logrange = math.log10((self.range_ymax - self.range_ymin)/3);
68 tick = math.pow(10, math.floor(logrange));
69 self.tick_major_y = tick;
71 print "tick %g %g" % (self.tick_major_x, self.tick_major_y);
73 def draw_border (self):
74 self.ctx.move_to (self.margin_left, self.margin_top);
75 self.ctx.line_to (self.width - self.margin_right, self.margin_top);
76 self.ctx.line_to (self.width - self.margin_right, self.height - self.margin_bottom);
77 self.ctx.line_to (self.margin_left, self.height - self.margin_bottom);
78 self.ctx.close_path ();
80 self.ctx.set_source_rgb (0, 0, 0);
81 self.ctx.stroke();
83 def marker (self, x, y):
84 self.ctx.move_to (x, y);
85 self.ctx.save ();
86 self.ctx.identity_matrix ();
87 self.ctx.rel_move_to (-self.symbol_size, 0);
88 self.ctx.rel_line_to (self.symbol_size*2, 0);
89 self.ctx.rel_move_to (-self.symbol_size, -self.symbol_size);
90 self.ctx.rel_line_to (0, self.symbol_size*2);
91 self.ctx.restore ();
93 def draw_plot (self, points):
94 self.ctx.save();
96 self.ctx.translate (self.margin_left,
97 self.height - self.margin_bottom);
98 self.ctx.scale ((self.width - self.margin_right - self.margin_left)/
99 (self.range_xmax - self.range_xmin),
100 -(self.height - self.margin_top - self.margin_bottom)/
101 (self.range_ymax - self.range_ymin));
102 self.ctx.translate (-self.range_xmin, -self.range_ymin);
104 if (self.with_lines):
105 self.ctx.move_to (points[0][0], points[0][1]);
106 for point in points[1:]:
107 self.ctx.line_to (point[0], point[1]);
108 else:
109 for point in points:
110 self.marker (point[0], point[1]);
112 self.ctx.restore();
113 self.ctx.stroke();
115 def vtick (self, y):
116 self.ctx.move_to (self.range_xmin, y);
117 self.ctx.save ();
118 self.ctx.identity_matrix ();
119 self.ctx.rel_move_to (0, 0);
120 self.ctx.rel_line_to (-self.tick_size, 0);
121 self.ctx.restore ();
123 def htick (self, x):
124 self.ctx.move_to (x, self.range_ymin);
125 self.ctx.save ();
126 self.ctx.identity_matrix ();
127 self.ctx.rel_move_to (0, 0);
128 self.ctx.rel_line_to (0, self.tick_size);
129 self.ctx.restore ();
131 def vlabel (self, y):
132 self.ctx.move_to (self.range_xmin, y);
133 self.ctx.save ();
134 self.ctx.identity_matrix ();
135 self.ctx.rel_move_to (-self.tick_size * 6, 0);
136 self.ctx.show_text("%g" % y);
137 self.ctx.restore ();
139 def hlabel (self, x):
140 self.ctx.move_to (x, self.range_ymin);
141 self.ctx.save ();
142 self.ctx.identity_matrix ();
143 self.ctx.rel_move_to (0, self.tick_size * 4);
144 self.ctx.show_text("%g" % x);
145 self.ctx.restore ();
147 def draw_ticks (self):
148 self.ctx.save ();
149 self.ctx.translate (self.margin_left, self.height - self.margin_bottom);
150 self.x_scale = (self.width - self.margin_right - self.margin_left)/(self.range_xmax - self.range_xmin);
151 self.y_scale = (self.height - self.margin_top - self.margin_bottom)/(self.range_ymax - self.range_ymin);
152 print "scale %g %g" % (self.x_scale, self.y_scale);
153 self.ctx.scale (self.x_scale, -self.y_scale);
154 self.ctx.translate (-self.range_xmin, -self.range_ymin);
156 x = self.range_ymin;
157 while (x <= self.range_ymax):
158 self.vtick(x);
159 x+=self.tick_major_y;
160 x = self.range_xmin;
161 while (x <= self.range_xmax):
162 self.htick(x);
163 x+=self.tick_major_x;
165 x = self.range_ymin;
166 while (x <= self.range_ymax):
167 self.vlabel(x);
168 x+=self.tick_major_y;
169 x = self.range_xmin;
170 while (x <= self.range_xmax):
171 self.hlabel(x);
172 x+=self.tick_major_x;
174 self.ctx.restore();
175 self.ctx.set_source_rgb (0, 0, 0);
176 self.ctx.stroke();
178 def readdata(filename):
179 file = open(filename);
181 points = [];
182 for line in file.readlines():
183 points.append(map(float,line.split()));
185 file.close();
186 return points;
189 def plot_mse():
190 plot = Plot(800,600);
191 plot.set_x_range_data(tpts[0]);
192 m = max(Numeric.transpose(tpts[14]));
193 #plot.set_x_range(0, m);
194 plot.set_y_range(0, m);
196 plot.set_sane_ticks();
198 plot.ctx.set_source_rgb (1, 1, 1);
199 plot.ctx.set_operator (cairo.OPERATOR_SOURCE);
200 plot.ctx.paint();
202 plot.draw_border ();
203 plot.draw_ticks ();
205 plot.ctx.set_line_width(0.5);
207 plot.ctx.set_source_rgb (1, 0, 0);
208 plot.draw_plot (Numeric.transpose(tpts[0:15:14]));
210 plot.ctx.set_source_rgb (0, 1, 0);
211 plot.draw_plot (Numeric.transpose(tpts[0:16:15]));
213 #plot.with_lines = 0;
214 #plot.draw_plot (Numeric.transpose(tpts[14:16:1]));
216 plot.surface.write_to_png('00000-mse.png');
218 def plot_buffer_level():
219 plot = Plot(800,600);
220 plot.set_x_range_data(tpts[0]);
221 m = max(Numeric.transpose(tpts[11]));
222 plot.set_y_range(0, m);
224 plot.set_sane_ticks();
226 plot.ctx.set_source_rgb (1, 1, 1);
227 plot.ctx.set_operator (cairo.OPERATOR_SOURCE);
228 plot.ctx.paint();
230 plot.draw_border ();
231 plot.draw_ticks ();
233 plot.ctx.set_line_width(0.5);
235 plot.ctx.set_source_rgb (1, 0, 0);
236 plot.draw_plot (Numeric.transpose(tpts[0:12:11]));
238 plot.surface.write_to_png('00000-buffer-level.png');
240 def plot_bits_per_pic():
241 plot = Plot(800,600);
242 plot.set_x_range_data(tpts[0]);
244 data_x = Numeric.transpose(tpts[0]);
245 data_y1 = Numeric.transpose(tpts[8]);
246 data_y2 = Numeric.transpose(tpts[9]);
247 data_y = Numeric.transpose(tpts[8]+tpts[9]);
248 m = max(Numeric.transpose(data_y));
249 plot.set_y_range(0, m);
251 plot.set_sane_ticks();
253 plot.ctx.set_source_rgb (1, 1, 1);
254 plot.ctx.set_operator (cairo.OPERATOR_SOURCE);
255 plot.ctx.paint();
257 plot.draw_border ();
258 plot.draw_ticks ();
260 plot.ctx.set_line_width(0.5);
262 plot.ctx.set_source_rgb (1, 0, 0);
263 plot.with_lines = 0;
264 plot.draw_plot (Numeric.transpose([data_x,data_y]));
266 plot.ctx.set_source_rgb (0, 1, 0);
267 plot.with_lines = 0;
268 plot.draw_plot (Numeric.transpose([data_x,data_y1]));
270 plot.ctx.set_source_rgb (0, 0, 1);
271 plot.with_lines = 0;
272 plot.draw_plot (Numeric.transpose([data_x,data_y2]));
274 plot.ctx.set_source_rgb (0.5, 0.5, 0.5);
275 plot.with_lines = 0;
276 plot.draw_plot (Numeric.transpose([data_x,Numeric.transpose(tpts[3])]));
278 plot.ctx.set_source_rgb (0.5, 0.5, 0.5);
279 plot.with_lines = 0;
280 plot.draw_plot (Numeric.transpose([data_x,Numeric.transpose(tpts[4])]));
282 plot.surface.write_to_png('00000-bits-per-pic.png');
284 def plot_badblock_ratio():
285 plot = Plot(800,600);
286 data_x = Numeric.transpose(tpts[0]);
287 data_y = Numeric.transpose(tpts[17]);
288 mx = max(data_x);
289 my = max(data_y);
290 plot.set_x_range(0, mx);
291 plot.set_y_range(0, my);
293 plot.set_sane_ticks();
295 plot.ctx.set_source_rgb (1, 1, 1);
296 plot.ctx.set_operator (cairo.OPERATOR_SOURCE);
297 plot.ctx.paint();
299 plot.draw_border ();
300 plot.draw_ticks ();
302 plot.ctx.set_line_width(0.5);
304 plot.ctx.set_source_rgb (1, 0, 0);
305 plot.draw_plot (Numeric.transpose([data_x,data_y]));
307 plot.surface.write_to_png('00000-badblock-ratio.png');
309 def plot_est_act_residual():
310 plot = Plot(800,600);
311 mx = max(Numeric.transpose(tpts[7]));
312 my = max(Numeric.transpose(tpts[9]));
313 m = max(mx,my);
314 plot.set_x_range(0, m);
315 plot.set_y_range(0, m);
317 plot.set_sane_ticks();
319 plot.ctx.set_source_rgb (1, 1, 1);
320 plot.ctx.set_operator (cairo.OPERATOR_SOURCE);
321 plot.ctx.paint();
323 plot.draw_border ();
324 plot.draw_ticks ();
326 plot.ctx.set_line_width(0.5);
328 plot.ctx.set_source_rgb (0, 1, 0);
329 plot.with_lines = 1;
330 plot.draw_plot ( [[0,0],[m,m]]);
332 plot.ctx.set_source_rgb (1, 0, 0);
333 plot.with_lines = 0;
334 plot.draw_plot (Numeric.transpose(tpts[7:10:2]));
336 plot.surface.write_to_png('00000-est-act-residual.png');
338 def plot_alloc_est_residual():
339 plot = Plot(800,600);
340 data_x = Numeric.transpose(tpts[4]);
341 data_y = Numeric.transpose(tpts[7]);
342 mx = max(data_x);
343 my = max(data_y);
344 m = max(mx,my);
345 plot.set_x_range(0, m);
346 plot.set_y_range(0, m);
348 plot.set_sane_ticks();
350 plot.ctx.set_source_rgb (1, 1, 1);
351 plot.ctx.set_operator (cairo.OPERATOR_SOURCE);
352 plot.ctx.paint();
354 plot.draw_border ();
355 plot.draw_ticks ();
357 plot.ctx.set_line_width(0.5);
359 plot.ctx.set_source_rgb (0, 1, 0);
360 plot.with_lines = 1;
361 plot.draw_plot ( [[0,0],[m,m]]);
363 plot.ctx.set_source_rgb (1, 0, 0);
364 plot.with_lines = 0;
365 plot.draw_plot (Numeric.transpose([data_x,data_y]));
367 plot.surface.write_to_png('00000-alloc-est-residual.png');
369 def plot_est_act_mc():
370 plot = Plot(800,600);
371 mx = max(Numeric.transpose(tpts[6]));
372 my = max(Numeric.transpose(tpts[8]));
373 m = max(mx,my);
374 plot.set_x_range(0, m);
375 plot.set_y_range(0, m);
377 plot.set_sane_ticks();
379 plot.ctx.set_source_rgb (1, 1, 1);
380 plot.ctx.set_operator (cairo.OPERATOR_SOURCE);
381 plot.ctx.paint();
383 plot.draw_border ();
384 plot.draw_ticks ();
386 plot.ctx.set_line_width(0.5);
388 plot.ctx.set_source_rgb (0, 1, 0);
389 plot.with_lines = 1;
390 plot.draw_plot ( [[0,0],[m,m]]);
392 plot.ctx.set_source_rgb (1, 0, 0);
393 plot.with_lines = 0;
394 plot.draw_plot (Numeric.transpose(tpts[6:9:2]));
396 plot.surface.write_to_png('00000-est-act-mc.png');
398 def plot_est_mc_scs():
399 plot = Plot(800,600);
400 data_x = Numeric.transpose(tpts[6]);
401 data_y = Numeric.transpose(tpts[10]);
402 mx = max(data_x);
403 my = max(data_y);
404 plot.set_x_range(0, mx);
405 plot.set_y_range(0, 0.1);
407 plot.set_sane_ticks();
409 plot.ctx.set_source_rgb (1, 1, 1);
410 plot.ctx.set_operator (cairo.OPERATOR_SOURCE);
411 plot.ctx.paint();
413 plot.draw_border ();
414 plot.draw_ticks ();
416 plot.ctx.set_line_width(0.5);
418 plot.ctx.set_source_rgb (1, 0, 0);
419 plot.with_lines = 0;
420 plot.draw_plot (Numeric.transpose([data_x,data_y]));
422 plot.surface.write_to_png('00000-est-mc-scs.png');
424 def plot_scs_mse():
425 plot = Plot(800,600);
426 #mx = max(Numeric.transpose(tpts[10]));
427 mx = 1;
428 my = max(Numeric.transpose(tpts[14]));
429 plot.set_x_range(0, mx);
430 plot.set_y_range(0, my);
432 plot.set_sane_ticks();
434 plot.ctx.set_source_rgb (1, 1, 1);
435 plot.ctx.set_operator (cairo.OPERATOR_SOURCE);
436 plot.ctx.paint();
438 plot.draw_border ();
439 plot.draw_ticks ();
441 plot.ctx.set_line_width(0.5);
443 plot.ctx.set_source_rgb (1, 0, 0);
444 plot.with_lines = 0;
445 plot.draw_plot (Numeric.transpose(tpts[10:15:4]));
447 plot.surface.write_to_png('00000-scs-mse.png');
449 def plot_scs():
450 plot = Plot(800,600);
451 data_x = Numeric.transpose(tpts[0]);
452 data_y = Numeric.transpose(tpts[14]);
453 mx = max(data_x);
454 my = max(data_y);
455 plot.set_x_range(0, mx);
456 plot.set_y_range(0, my);
458 plot.set_sane_ticks();
460 plot.ctx.set_source_rgb (1, 1, 1);
461 plot.ctx.set_operator (cairo.OPERATOR_SOURCE);
462 plot.ctx.paint();
464 plot.draw_border ();
465 plot.draw_ticks ();
467 plot.ctx.set_line_width(0.5);
469 plot.ctx.set_source_rgb (1, 0, 0);
470 plot.with_lines = 0;
471 plot.draw_plot (Numeric.transpose([data_x,data_y]));
473 plot.surface.write_to_png('00000-scs.png');
475 def plot_badblock_vs_mse():
476 plot = Plot(800,600);
477 data_x = Numeric.transpose(tpts[17]);
478 data_y = Numeric.transpose(tpts[14]);
479 mx = max(data_x);
480 my = max(data_y);
481 plot.set_x_range(0, mx);
482 plot.set_y_range(0, my);
484 plot.set_sane_ticks();
486 plot.ctx.set_source_rgb (1, 1, 1);
487 plot.ctx.set_operator (cairo.OPERATOR_SOURCE);
488 plot.ctx.paint();
490 plot.draw_border ();
491 plot.draw_ticks ();
493 plot.ctx.set_line_width(0.5);
495 plot.ctx.set_source_rgb (1, 0, 0);
496 plot.with_lines = 0;
497 plot.draw_plot (Numeric.transpose([data_x,data_y]));
499 plot.surface.write_to_png('00000-badblock-vs-mse.png');
501 def plot_mc_error_vs_mse():
502 plot = Plot(800,600);
503 data_x = Numeric.transpose(tpts[13]);
504 data_y = Numeric.transpose(tpts[14]);
505 #data_x = Numeric.transpose(tpts[0]);
506 #data_y = Numeric.transpose(tpts[13])/Numeric.transpose(tpts[14]);
507 mx = max(data_x);
508 my = max(data_y);
509 plot.set_x_range_data(data_x);
510 plot.set_y_range(0, my);
512 plot.set_sane_ticks();
514 plot.ctx.set_source_rgb (1, 1, 1);
515 plot.ctx.set_operator (cairo.OPERATOR_SOURCE);
516 plot.ctx.paint();
518 plot.draw_border ();
519 plot.draw_ticks ();
521 plot.ctx.set_line_width(0.5);
523 plot.ctx.set_source_rgb (0, 1, 0);
524 plot.with_lines = 1;
525 plot.draw_plot ( [[0,0],[mx,mx]]);
527 plot.ctx.set_source_rgb (1, 0, 0);
528 plot.with_lines = 0;
529 plot.draw_plot (Numeric.transpose([data_x,data_y]));
531 plot.surface.write_to_png('00000-mc-error-vs-mse.png');
533 def plot_hist_slope():
534 plot = Plot(800,600);
535 data_x = Numeric.transpose(tpts[0]);
536 data_y = Numeric.transpose(tpts[18]);
537 plot.set_x_range_data(data_x);
538 plot.set_y_range_data(data_y);
540 plot.set_sane_ticks();
542 plot.ctx.set_source_rgb (1, 1, 1);
543 plot.ctx.set_operator (cairo.OPERATOR_SOURCE);
544 plot.ctx.paint();
546 plot.draw_border ();
547 plot.draw_ticks ();
549 plot.ctx.set_line_width(0.5);
551 plot.ctx.set_source_rgb (1, 0, 0);
552 plot.draw_plot (Numeric.transpose([data_x,data_y]));
554 plot.surface.write_to_png('00000-hist-slope.png');
556 def plot_hist_slope_vs_mse():
557 plot = Plot(800,600);
558 data_x = Numeric.transpose(tpts[18]);
559 data_y = Numeric.transpose(tpts[14]);
560 plot.set_x_range_data(data_x);
561 plot.set_y_range_data(data_y);
563 plot.set_sane_ticks();
565 plot.ctx.set_source_rgb (1, 1, 1);
566 plot.ctx.set_operator (cairo.OPERATOR_SOURCE);
567 plot.ctx.paint();
569 plot.draw_border ();
570 plot.draw_ticks ();
572 plot.ctx.set_line_width(0.5);
574 plot.with_lines = 0;
575 plot.ctx.set_source_rgb (1, 0, 0);
576 plot.draw_plot (Numeric.transpose([data_x,data_y]));
578 plot.surface.write_to_png('00000-hist-slope-vs-mse.png');
581 pts = readdata("schro_dump.picture");
582 #pts = pts[100:200];
583 tpts = Numeric.transpose(pts);
585 plot_mse();
586 plot_buffer_level();
587 plot_alloc_est_residual();
588 plot_est_act_residual();
589 plot_est_act_mc();
590 plot_bits_per_pic();
591 plot_scs_mse();
592 plot_scs();
593 plot_est_mc_scs();
594 plot_badblock_ratio();
595 plot_badblock_vs_mse();
596 plot_mc_error_vs_mse();
597 plot_hist_slope();
598 plot_hist_slope_vs_mse();