blob: 5ae8cc6c07caa3e1d9ffeed918b64acad07f7406 [file] [log] [blame]
Kapileshwar Singh584f88d2015-09-03 13:21:19 +01001{
2 "metadata": {
3 "kernelspec": {
4 "display_name": "Python 2",
5 "language": "python",
6 "name": "python2"
7 },
8 "language_info": {
9 "codemirror_mode": {
10 "name": "ipython",
11 "version": 2
12 },
13 "file_extension": ".py",
14 "mimetype": "text/x-python",
15 "name": "python",
16 "nbconvert_exporter": "python",
17 "pygments_lexer": "ipython2",
18 "version": "2.7.6"
19 },
Michele Di Giorgio6458e4c2016-02-16 12:41:59 +000020 "name": ""
Kapileshwar Singh584f88d2015-09-03 13:21:19 +010021 },
22 "nbformat": 3,
23 "nbformat_minor": 0,
24 "worksheets": [
25 {
26 "cells": [
27 {
28 "cell_type": "heading",
29 "level": 1,
30 "metadata": {},
31 "source": [
32 "TRAPpy: Interactive Plotting"
33 ]
34 },
35 {
Javi Merino0645c1e2016-05-09 18:23:45 +010036 "cell_type": "markdown",
Kapileshwar Singh584f88d2015-09-03 13:21:19 +010037 "metadata": {},
38 "source": [
Javi Merino0645c1e2016-05-09 18:23:45 +010039 "<span style=\"font-size: 150%\">Re Run the cells to generate the graphs</span>"
Kapileshwar Singh584f88d2015-09-03 13:21:19 +010040 ]
41 },
42 {
43 "cell_type": "code",
44 "collapsed": false,
45 "input": [
Kapileshwar Singh584f88d2015-09-03 13:21:19 +010046 "import sys,os\n",
47 "sys.path.append(\"..\")\n",
Javi Merino0a31cd22016-02-29 18:36:39 +000048 "import numpy.random\n",
Javi Merinoaf8ff922016-01-15 19:25:49 +000049 "import pandas as pd\n",
Kapileshwar Singh584f88d2015-09-03 13:21:19 +010050 "import shutil\n",
Javi Merinoaf8ff922016-01-15 19:25:49 +000051 "import tempfile\n",
Kapileshwar Singh584f88d2015-09-03 13:21:19 +010052 "import trappy\n",
53 "trace_thermal = \"./trace.txt\"\n",
54 "trace_sched = \"../tests/raw_trace.dat\"\n",
55 "TEMP_BASE = \"/tmp\""
56 ],
57 "language": "python",
58 "metadata": {},
Javi Merino0a31cd22016-02-29 18:36:39 +000059 "outputs": [],
Michele Di Giorgio2ffab142016-03-03 18:49:17 +000060 "prompt_number": null
Kapileshwar Singh584f88d2015-09-03 13:21:19 +010061 },
62 {
63 "cell_type": "code",
64 "collapsed": true,
65 "input": [
66 "def setup_thermal():\n",
67 " tDir = tempfile.mkdtemp(dir=\"/tmp\", prefix=\"trappy_doc\", suffix = \".tempDir\")\n",
68 " shutil.copyfile(trace_thermal, os.path.join(tDir, \"trace.txt\"))\n",
69 " return tDir\n",
70 "\n",
71 "def setup_sched():\n",
72 " tDir = tempfile.mkdtemp(dir=\"/tmp\", prefix=\"trappy_doc\", suffix = \".tempDir\")\n",
73 " shutil.copyfile(trace_sched, os.path.join(tDir, \"trace.dat\"))\n",
74 " return tDir"
75 ],
76 "language": "python",
77 "metadata": {},
78 "outputs": [],
Michele Di Giorgio2ffab142016-03-03 18:49:17 +000079 "prompt_number": null
Kapileshwar Singh584f88d2015-09-03 13:21:19 +010080 },
81 {
82 "cell_type": "code",
83 "collapsed": true,
84 "input": [
85 "temp_thermal_location = setup_thermal()\n",
Javi Merinoa6c6adf2016-05-09 18:14:43 +010086 "trace1 = trappy.FTrace(temp_thermal_location)"
Kapileshwar Singh584f88d2015-09-03 13:21:19 +010087 ],
88 "language": "python",
89 "metadata": {},
90 "outputs": [],
Michele Di Giorgio2ffab142016-03-03 18:49:17 +000091 "prompt_number": null
Kapileshwar Singh584f88d2015-09-03 13:21:19 +010092 },
93 {
94 "cell_type": "heading",
95 "level": 1,
96 "metadata": {},
97 "source": [
Michele Di Giorgio2ffab142016-03-03 18:49:17 +000098 "Interactive Line Plotting of Data Frames"
Kapileshwar Singh584f88d2015-09-03 13:21:19 +010099 ]
100 },
101 {
102 "cell_type": "markdown",
103 "metadata": {},
104 "source": [
Michele Di Giorgio6458e4c2016-02-16 12:41:59 +0000105 "Interactive Line Plots Supports the same API as the LinePlot but provide an interactive plot that can be zoomed by clicking and dragging on the desired area. Double clicking resets the zoom."
Kapileshwar Singh584f88d2015-09-03 13:21:19 +0100106 ]
107 },
108 {
Javi Merinoaf8ff922016-01-15 19:25:49 +0000109 "cell_type": "markdown",
110 "metadata": {},
111 "source": [
112 "We can create an interactive plot easily from a dataframe by passing the data frame and the columns we want to plot as parameters:"
113 ]
114 },
115 {
116 "cell_type": "code",
117 "collapsed": false,
118 "input": [
119 "columns = [\"tick\", \"tock\"]\n",
Javi Merino0a31cd22016-02-29 18:36:39 +0000120 "df = pd.DataFrame(numpy.random.randn(1000, 2), columns=columns).cumsum()\n",
Javi Merinoaf8ff922016-01-15 19:25:49 +0000121 "\n",
122 "trappy.ILinePlot(df, column=columns).view()"
123 ],
124 "language": "python",
125 "metadata": {},
126 "outputs": [],
127 "prompt_number": null
128 },
129 {
Michele Di Giorgio2ffab142016-03-03 18:49:17 +0000130 "cell_type": "heading",
Javi Merino0645c1e2016-05-09 18:23:45 +0100131 "level": 2,
Michele Di Giorgio2ffab142016-03-03 18:49:17 +0000132 "metadata": {},
133 "source": [
134 "Plotting independent series"
135 ]
136 },
137 {
138 "cell_type": "markdown",
139 "metadata": {},
140 "source": [
141 "It is also possible to plot traces with different index values (i.e. x-axis values)"
142 ]
143 },
144 {
145 "cell_type": "code",
146 "collapsed": false,
147 "input": [
148 "columns = [\"tick\", \"tock\", \"bang\"]\n",
149 "df_len = 1000\n",
150 "df1 = pd.DataFrame(numpy.random.randn(df_len, 3), columns=columns, index=range(df_len)).cumsum()\n",
151 "df2 = pd.DataFrame(numpy.random.randn(df_len, 3), columns=columns, index=(numpy.arange(0.5, df_len, 1))).cumsum()"
152 ],
153 "language": "python",
154 "metadata": {},
155 "outputs": [],
156 "prompt_number": null
157 },
158 {
159 "cell_type": "code",
160 "collapsed": false,
161 "input": [
162 "trappy.ILinePlot([df1, df2], column=\"tick\").view()"
163 ],
164 "language": "python",
165 "metadata": {},
166 "outputs": [],
167 "prompt_number": null
168 },
169 {
170 "cell_type": "markdown",
171 "metadata": {},
172 "source": [
173 "This does not affect filtering or pivoting in any way"
174 ]
175 },
176 {
177 "cell_type": "code",
178 "collapsed": false,
179 "input": [
180 "df1[\"bang\"] = df1[\"bang\"].apply(lambda x: numpy.random.randint(0, 4))\n",
181 "df2[\"bang\"] = df2[\"bang\"].apply(lambda x: numpy.random.randint(0, 4))"
182 ],
183 "language": "python",
184 "metadata": {},
185 "outputs": [],
186 "prompt_number": null
187 },
188 {
189 "cell_type": "code",
190 "collapsed": false,
191 "input": [
192 "trappy.ILinePlot([df1, df2], column=\"tick\", filters = {'bang' : [2]}, title=\"tick column values for which bang is 2\").view()"
193 ],
194 "language": "python",
195 "metadata": {
196 "scrolled": true
197 },
198 "outputs": [],
199 "prompt_number": null
200 },
201 {
202 "cell_type": "code",
203 "collapsed": true,
204 "input": [
205 "trappy.ILinePlot([df1, df2], column=\"tick\", pivot=\"bang\", title=\"tick column pivoted on bang column\").view()"
206 ],
207 "language": "python",
208 "metadata": {},
209 "outputs": [],
210 "prompt_number": null
211 },
212 {
213 "cell_type": "heading",
214 "level": 1,
215 "metadata": {},
216 "source": [
217 "Interactive Line Plotting of Traces"
218 ]
219 },
220 {
Javi Merinoaf8ff922016-01-15 19:25:49 +0000221 "cell_type": "markdown",
222 "metadata": {},
223 "source": [
224 "We can also create them from trace objects"
225 ]
226 },
227 {
Kapileshwar Singh584f88d2015-09-03 13:21:19 +0100228 "cell_type": "code",
229 "collapsed": false,
230 "input": [
Javi Merino1381e3a2016-05-09 18:28:29 +0100231 "map_label = {\n",
232 " \"00000000,00000006\" : \"A57\",\n",
233 " \"00000000,00000039\" : \"A53\",\n",
234 "}\n",
Kapileshwar Singh67874682015-12-17 11:14:22 +0000235 "\n",
Javi Merino1381e3a2016-05-09 18:28:29 +0100236 "l = trappy.ILinePlot(\n",
237 " trace1, # TRAPpy FTrace Object\n",
238 " trappy.cpu_power.CpuInPower, # TRAPpy Event (maps to a unique word in the Trace)\n",
239 " column=[ # Column(s)\n",
240 " \"dynamic_power\",\n",
241 " \"load1\"],\n",
242 "\n",
243 " filters={ # Filter the data \n",
244 " \"cdev_state\": [\n",
245 " 1,\n",
246 " 0]},\n",
247 " pivot=\"cpus\", # One plot for each pivot will be created\n",
248 " map_label=map_label, # Optionally, provide an alternative label for pivots\n",
249 " per_line=1) # Number of graphs per line\n",
250 "l.view()"
Kapileshwar Singh584f88d2015-09-03 13:21:19 +0100251 ],
252 "language": "python",
253 "metadata": {},
Kapileshwar Singh67874682015-12-17 11:14:22 +0000254 "outputs": [],
255 "prompt_number": null
Kapileshwar Singh584f88d2015-09-03 13:21:19 +0100256 },
257 {
258 "cell_type": "markdown",
259 "metadata": {},
260 "source": [
261 "You can also change the drawstyle to \"steps-post\" for step plots. These are suited if the data is discrete \n",
262 "and linear interploation is not required between two data points"
263 ]
264 },
265 {
266 "cell_type": "code",
267 "collapsed": false,
268 "input": [
Javi Merino1381e3a2016-05-09 18:28:29 +0100269 "l = trappy.ILinePlot(\n",
270 " trace1, # TRAPpy FTrace Object\n",
271 " trappy.cpu_power.CpuInPower, # TRAPpy Event (maps to a unique word in the Trace)\n",
272 " column=[ # Column(s)\n",
273 " \"dynamic_power\",\n",
274 " \"load1\"],\n",
275 "\n",
276 " filters={ # Filter the data \n",
277 " \"cdev_state\": [\n",
278 " 1,\n",
279 " 0]},\n",
280 " pivot=\"cpus\", # One plot for each pivot will be created\n",
281 "\n",
282 " per_line=1, # Number of graphs per line\n",
283 " drawstyle=\"steps-post\") \n",
284 "l.view()"
Kapileshwar Singh584f88d2015-09-03 13:21:19 +0100285 ],
286 "language": "python",
287 "metadata": {},
Javi Merinoce5efc82016-07-06 19:11:15 +0100288 "outputs": []
289 },
290 {
291 "cell_type": "heading",
292 "level": 2,
293 "metadata": {},
294 "source": [
295 "Plots for a given time range"
296 ]
297 },
298 {
299 "cell_type": "markdown",
300 "metadata": {},
301 "source": [
302 "Performance can suffer if ILinePlot tries to make a huge plot. One way of fixing it is by limiting the period of time plotted using the `xlim` parameter:"
303 ]
304 },
305 {
306 "cell_type": "code",
307 "collapsed": false,
308 "input": [
309 "trappy.ILinePlot(\n",
310 " trace1,\n",
Javi Merino2ad4f2d2016-07-08 14:54:17 +0100311 " signals=[\"thermal:temp\"],\n",
Javi Merinoce5efc82016-07-06 19:11:15 +0100312 " xlim=(1, 4), # Only between seconds 1 and 4\n",
313 ").view()"
314 ],
315 "language": "python",
316 "metadata": {
317 "run_control": {
318 "marked": true
319 }
320 },
321 "outputs": []
Kapileshwar Singh584f88d2015-09-03 13:21:19 +0100322 },
323 {
324 "cell_type": "heading",
Javi Merinod9d82782016-03-15 17:52:42 +0000325 "level": 2,
326 "metadata": {},
327 "source": [
328 "Synchronized zoom in multiple plots"
329 ]
330 },
331 {
332 "cell_type": "markdown",
333 "metadata": {},
334 "source": [
Javi Merinob071d512016-06-17 20:18:49 +0100335 "ILinePlots can zoom all at the same time. You can do so using the `group` and `sync_zoom` parameter. All ILinePlots using the same group name zoom at the same time. Note the use of signals with colors."
Javi Merinod9d82782016-03-15 17:52:42 +0000336 ]
337 },
338 {
339 "cell_type": "code",
340 "collapsed": false,
341 "input": [
342 "trappy.ILinePlot(\n",
343 " trace1,\n",
Javi Merinob071d512016-06-17 20:18:49 +0100344 " signals=[\"cpu_in_power:dynamic_power:18,140,171\", \"cpu_in_power:load1:0xcf,0x36,0x4a\"],\n",
Javi Merinod9d82782016-03-15 17:52:42 +0000345 " pivot=\"cpus\",\n",
346 " group=\"synchronized\",\n",
347 " sync_zoom=True\n",
348 ").view()"
349 ],
350 "language": "python",
351 "metadata": {},
Brendan Jackman74efec02016-11-04 14:25:39 +0000352 "outputs": [],
353 "prompt_number": null
354 },
355 {
356 "cell_type": "heading",
357 "level": 2,
358 "metadata": {},
359 "source": [
360 "Styling ILinePlots"
361 ]
362 },
363 {
364 "cell_type": "markdown",
365 "metadata": {},
366 "source": [
367 "Use `fill=True` to colour-fill the space under the line. `fill_alpha` optionally sets the opacity."
368 ]
369 },
370 {
371 "cell_type": "code",
372 "collapsed": false,
373 "input": [
374 "trappy.ILinePlot(\n",
375 " trace1,\n",
376 " signals=[\"thermal:temp\"],\n",
377 " fill=True,\n",
378 " fill_alpha=0.5\n",
379 ").view()"
380 ],
381 "language": "python",
382 "metadata": {},
383 "outputs": [],
384 "prompt_number": null
Javi Merinod9d82782016-03-15 17:52:42 +0000385 },
386 {
387 "cell_type": "heading",
Kapileshwar Singh584f88d2015-09-03 13:21:19 +0100388 "level": 1,
389 "metadata": {},
390 "source": [
391 "EventPlot"
392 ]
393 },
394 {
395 "cell_type": "markdown",
396 "metadata": {},
397 "source": [
398 "TRAPpy's Interactive Plotter features an Interactive Event TimeLine Plot. It accepts an input data of the type\n",
399 "<pre>\n",
400 "<code>\n",
401 " { \"A\" : [\n",
402 " [event_start, event_end, lane],\n",
403 " .\n",
404 " .\n",
405 " [event_start, event_end, lane],\n",
406 " ],\n",
407 " .\n",
408 " .\n",
409 " .\n",
410 "\n",
411 " \"B\" : [\n",
412 " [event_start, event_end, lane],\n",
413 " .\n",
414 " .\n",
415 " [event_start, event_end, lane],\n",
416 " .\n",
417 " .\n",
418 " .\n",
419 " }\n",
420 " \n",
421 "</code>\n",
422 "</pre>\n",
423 "\n",
424 "Hovering on the rectangles gives the name of the process element and scrolling on the Plot Area and the window in the summary controls the zoom. One can also click and drag for panning a zoomed graph.\n",
425 "\n",
426 "For Example:"
427 ]
428 },
429 {
430 "cell_type": "code",
431 "collapsed": false,
432 "input": [
433 "A = [\n",
434 " \n",
435 " [0, 3, 0],\n",
436 " [4, 5, 2],\n",
437 "]\n",
438 "\n",
439 "B = [\n",
440 " [0, 2, 1],\n",
441 " [2, 3, 3],\n",
442 " [3, 4, 0],\n",
443 "]\n",
444 "\n",
445 "C = [\n",
446 " [0, 2, 3],\n",
447 " [2, 3, 2],\n",
448 " [3, 4, 1],\n",
449 "]\n",
450 "\n",
451 "EVENTS = {}\n",
452 "EVENTS[\"A\"] = A\n",
453 "EVENTS[\"B\"] = B\n",
454 "EVENTS[\"C\"] = C\n",
455 "\n",
456 "trappy.EventPlot(EVENTS,\n",
457 " keys=EVENTS.keys, # Name of the Process Element\n",
458 " lane_prefix=\"LANE: \", # Name of Each TimeLine\n",
459 " num_lanes=4, # Number of Timelines\n",
460 " domain=[0,5] # Time Domain\n",
461 " ).view()"
462 ],
463 "language": "python",
464 "metadata": {},
Kapileshwar Singh67874682015-12-17 11:14:22 +0000465 "outputs": [],
466 "prompt_number": null
Kapileshwar Singh584f88d2015-09-03 13:21:19 +0100467 },
468 {
469 "cell_type": "markdown",
470 "metadata": {},
471 "source": [
Kapileshwar Singh26906d82015-10-09 11:29:39 +0100472 "Lane names can also be specified as strings (or hashable objects that have an str representation) as follows"
Kapileshwar Singh584f88d2015-09-03 13:21:19 +0100473 ]
474 },
475 {
476 "cell_type": "code",
477 "collapsed": false,
478 "input": [
Kapileshwar Singh26906d82015-10-09 11:29:39 +0100479 "A = [\n",
480 " \n",
481 " [0, 3, \"zero\"],\n",
482 " [4, 5, \"two\"],\n",
483 "]\n",
484 "\n",
485 "B = [\n",
486 " [0, 2, 1],\n",
487 " [2, 3, \"three\"],\n",
488 " [3, 4, \"zero\"],\n",
489 "]\n",
490 "\n",
491 "C = [\n",
492 " [0, 2, \"three\"],\n",
493 " [2, 3, \"two\"],\n",
494 " [3, 4, 1],\n",
495 "]\n",
496 "\n",
497 "EVENTS = {}\n",
498 "EVENTS[\"A\"] = A\n",
499 "EVENTS[\"B\"] = B\n",
500 "EVENTS[\"C\"] = C\n",
501 "\n",
502 "trappy.EventPlot(EVENTS,\n",
503 " keys=EVENTS.keys, # Name of the Process Element\n",
504 " lanes=[\"zero\", 1, \"two\", \"three\"],\n",
505 " domain=[0,5] # Time Domain\n",
506 " ).view()"
Kapileshwar Singh584f88d2015-09-03 13:21:19 +0100507 ],
508 "language": "python",
509 "metadata": {},
Kapileshwar Singh67874682015-12-17 11:14:22 +0000510 "outputs": [],
511 "prompt_number": null
Kapileshwar Singh26906d82015-10-09 11:29:39 +0100512 },
513 {
Michele Di Giorgio303610e2016-07-20 19:28:39 +0100514 "cell_type": "markdown",
515 "metadata": {},
516 "source": [
517 "It is also possible to define a colour map to associate a specific colour to each event. A colour string can be:\n",
518 "\n",
519 " - a colour name, `green`, `red`, `blue`, etc.\n",
520 " \n",
521 " - the HEX representation of the colour, `#0000FF` for blue, `#FF0000` for red\n",
522 " "
523 ]
524 },
525 {
526 "cell_type": "code",
527 "collapsed": false,
528 "input": [
529 "# Using colour names\n",
530 "trappy.EventPlot(EVENTS,\n",
531 " keys=EVENTS.keys, # Name of the Process Element\n",
532 " lanes=[\"zero\", 1, \"two\", \"three\"],\n",
533 " domain=[0,5], # Time Domain\n",
534 " color_map={\"A\" : \"blue\", \"B\" : \"red\", \"C\" : \"green\"}\n",
535 " ).view()"
536 ],
537 "language": "python",
538 "metadata": {},
539 "outputs": [],
540 "prompt_number": null
541 },
542 {
543 "cell_type": "code",
544 "collapsed": false,
545 "input": [
546 "# Using HEX representation of colours\n",
547 "trappy.EventPlot(EVENTS,\n",
548 " keys=EVENTS.keys, # Name of the Process Element\n",
549 " lanes=[\"zero\", 1, \"two\", \"three\"],\n",
550 " domain=[0,5], # Time Domain\n",
551 " color_map={\"A\" : \"\t#ffa07a\", \"B\" : \"#f08080\", \"C\" : \"#add8e6\"}\n",
552 " ).view()"
553 ],
554 "language": "python",
555 "metadata": {},
556 "outputs": [],
557 "prompt_number": null
558 },
559 {
Kapileshwar Singh26906d82015-10-09 11:29:39 +0100560 "cell_type": "heading",
561 "level": 1,
562 "metadata": {},
563 "source": [
564 "TracePlot"
565 ]
566 },
567 {
568 "cell_type": "markdown",
569 "metadata": {},
570 "source": [
571 "A specification of the EventPlot creates a kernelshark like plot if the sched_switch event is enabled in the traces"
572 ]
573 },
574 {
575 "cell_type": "code",
576 "collapsed": false,
577 "input": [
578 "f = setup_sched()\n",
579 "trappy.plotter.plot_trace(f)"
580 ],
581 "language": "python",
582 "metadata": {},
583 "outputs": [],
584 "prompt_number": null
Javi Merino98885522016-05-09 18:55:26 +0100585 },
586 {
587 "cell_type": "heading",
588 "level": 1,
589 "metadata": {},
590 "source": [
591 "Exporting notebooks with interactive plots to HTML"
592 ]
593 },
594 {
595 "cell_type": "markdown",
596 "metadata": {},
597 "source": [
598 "Notebooks with ILinePlot or EventPlot can't be exported to HTML using File->Download as->HTML. They need to be converted from the command line:\n",
599 "```\n",
600 "jupyter nbconvert --to=trappy.nbexport.HTML notebook.ipynb\n",
601 "```\n",
602 "You need nbconvert >= 4.2 and trappy has to be in your `PYTHONPATH`."
603 ]
Kapileshwar Singh584f88d2015-09-03 13:21:19 +0100604 }
605 ],
606 "metadata": {}
607 }
608 ]
Michele Di Giorgio2ffab142016-03-03 18:49:17 +0000609}