aboutsummaryrefslogtreecommitdiff
path: root/doc/InteractivePlotter.ipynb
blob: 01cc103faed33b6b233c9ece02780e6229f03236 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
{
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  },
  "name": ""
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "heading",
     "level": 1,
     "metadata": {},
     "source": [
      "TRAPpy: Interactive Plotting"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<span style=\"font-size: 150%\">Re Run the cells to generate the graphs</span>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import sys,os\n",
      "sys.path.append(\"..\")\n",
      "import numpy.random\n",
      "import pandas as pd\n",
      "import shutil\n",
      "import tempfile\n",
      "import trappy\n",
      "trace_thermal = \"./trace.txt\"\n",
      "trace_sched = \"../tests/raw_trace.dat\"\n",
      "TEMP_BASE = \"/tmp\""
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": null
    },
    {
     "cell_type": "code",
     "collapsed": true,
     "input": [
      "def setup_thermal():\n",
      "    tDir = tempfile.mkdtemp(dir=\"/tmp\", prefix=\"trappy_doc\", suffix = \".tempDir\")\n",
      "    shutil.copyfile(trace_thermal, os.path.join(tDir, \"trace.txt\"))\n",
      "    return tDir\n",
      "\n",
      "def setup_sched():\n",
      "    tDir = tempfile.mkdtemp(dir=\"/tmp\", prefix=\"trappy_doc\", suffix = \".tempDir\")\n",
      "    shutil.copyfile(trace_sched, os.path.join(tDir, \"trace.dat\"))\n",
      "    return tDir"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": null
    },
    {
     "cell_type": "code",
     "collapsed": true,
     "input": [
      "temp_thermal_location = setup_thermal()\n",
      "trace1 = trappy.FTrace(temp_thermal_location)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": null
    },
    {
     "cell_type": "heading",
     "level": 1,
     "metadata": {},
     "source": [
      "Interactive Line Plotting of Data Frames"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "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."
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "We can create an interactive plot easily from a dataframe by passing the data frame and the columns we want to plot as parameters:"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "columns = [\"tick\", \"tock\"]\n",
      "df = pd.DataFrame(numpy.random.randn(1000, 2), columns=columns).cumsum()\n",
      "\n",
      "trappy.ILinePlot(df, column=columns).view()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": null
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Plotting independent series"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "It is also possible to plot traces with different index values (i.e. x-axis values)"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "columns = [\"tick\", \"tock\", \"bang\"]\n",
      "df_len = 1000\n",
      "df1 = pd.DataFrame(numpy.random.randn(df_len, 3), columns=columns, index=range(df_len)).cumsum()\n",
      "df2 = pd.DataFrame(numpy.random.randn(df_len, 3), columns=columns, index=(numpy.arange(0.5, df_len, 1))).cumsum()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": null
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "trappy.ILinePlot([df1, df2], column=\"tick\").view()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": null
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "This does not affect filtering or pivoting in any way"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "df1[\"bang\"] = df1[\"bang\"].apply(lambda x: numpy.random.randint(0, 4))\n",
      "df2[\"bang\"] = df2[\"bang\"].apply(lambda x: numpy.random.randint(0, 4))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": null
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "trappy.ILinePlot([df1, df2], column=\"tick\", filters = {'bang' : [2]}, title=\"tick column values for which bang is 2\").view()"
     ],
     "language": "python",
     "metadata": {
      "scrolled": true
     },
     "outputs": [],
     "prompt_number": null
    },
    {
     "cell_type": "code",
     "collapsed": true,
     "input": [
      "trappy.ILinePlot([df1, df2], column=\"tick\", pivot=\"bang\", title=\"tick column pivoted on bang column\").view()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": null
    },
    {
     "cell_type": "heading",
     "level": 1,
     "metadata": {},
     "source": [
      "Interactive Line Plotting of Traces"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "We can also create them from trace objects"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "map_label = {\n",
      "        \"00000000,00000006\" : \"A57\",\n",
      "        \"00000000,00000039\" : \"A53\",\n",
      "}\n",
      "\n",
      "l = trappy.ILinePlot(\n",
      "        trace1,                                          # TRAPpy FTrace Object\n",
      "        trappy.cpu_power.CpuInPower,                     # TRAPpy Event (maps to a unique word in the Trace)\n",
      "        column=[                                         # Column(s)\n",
      "            \"dynamic_power\",\n",
      "            \"load1\"],\n",
      "\n",
      "        filters={                                        # Filter the data  \n",
      "            \"cdev_state\": [\n",
      "                1,\n",
      "                0]},\n",
      "        pivot=\"cpus\",                                    # One plot for each pivot will be created\n",
      "        map_label=map_label,                             # Optionally, provide an alternative label for pivots\n",
      "        per_line=1)                                      # Number of graphs per line\n",
      "l.view()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": null
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "You can also change the drawstyle to \"steps-post\" for step plots. These are suited if the data is discrete \n",
      "and linear interploation is not required between two data points"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "l = trappy.ILinePlot(\n",
      "    trace1,                                          # TRAPpy FTrace Object\n",
      "    trappy.cpu_power.CpuInPower,                     # TRAPpy Event (maps to a unique word in the Trace)\n",
      "    column=[                                         # Column(s)\n",
      "        \"dynamic_power\",\n",
      "        \"load1\"],\n",
      "\n",
      "    filters={                                        # Filter the data  \n",
      "        \"cdev_state\": [\n",
      "            1,\n",
      "            0]},\n",
      "    pivot=\"cpus\",                                    # One plot for each pivot will be created\n",
      "\n",
      "    per_line=1,                                      # Number of graphs per line\n",
      "    drawstyle=\"steps-post\")                                     \n",
      "l.view()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Plots for a given time range"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "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:"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "trappy.ILinePlot(\n",
      "    trace1,\n",
      "    signals=[\"thermal:temp\"],\n",
      "    xlim=(1, 4),           # Only between seconds 1 and 4\n",
      ").view()"
     ],
     "language": "python",
     "metadata": {
      "run_control": {
       "marked": true
      }
     },
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Synchronized zoom in multiple plots"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "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."
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "trappy.ILinePlot(\n",
      "    trace1,\n",
      "    signals=[\"cpu_in_power:dynamic_power:18,140,171\", \"cpu_in_power:load1:0xcf,0x36,0x4a\"],\n",
      "    pivot=\"cpus\",\n",
      "    group=\"synchronized\",\n",
      "    sync_zoom=True\n",
      ").view()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 1,
     "metadata": {},
     "source": [
      "EventPlot"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "TRAPpy's  Interactive Plotter features an Interactive Event TimeLine Plot. It accepts an input data of the type\n",
      "<pre>\n",
      "<code>\n",
      "                   { \"A\" : [\n",
      "                                    [event_start, event_end, lane],\n",
      "                                    .\n",
      "                                    .\n",
      "                                    [event_start, event_end, lane],\n",
      "                                 ],\n",
      "                     .\n",
      "                     .\n",
      "                     .\n",
      "\n",
      "                     \"B\" : [\n",
      "                                    [event_start, event_end, lane],\n",
      "                                    .\n",
      "                                    .\n",
      "                                    [event_start, event_end, lane],\n",
      "                    .\n",
      "                    .\n",
      "                    .\n",
      "                   }\n",
      "                                \n",
      "</code>\n",
      "</pre>\n",
      "\n",
      "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",
      "\n",
      "For Example:"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "A = [\n",
      "    \n",
      "        [0, 3, 0],\n",
      "        [4, 5, 2],\n",
      "]\n",
      "\n",
      "B = [\n",
      "        [0, 2, 1],\n",
      "        [2, 3, 3],\n",
      "        [3, 4, 0],\n",
      "]\n",
      "\n",
      "C =  [\n",
      "        [0, 2, 3],\n",
      "        [2, 3, 2],\n",
      "        [3, 4, 1],\n",
      "]\n",
      "\n",
      "EVENTS = {}\n",
      "EVENTS[\"A\"] = A\n",
      "EVENTS[\"B\"] = B\n",
      "EVENTS[\"C\"] = C\n",
      "\n",
      "trappy.EventPlot(EVENTS,\n",
      "                 keys=EVENTS.keys,                     # Name of the Process Element\n",
      "                 lane_prefix=\"LANE: \",                 # Name of Each TimeLine\n",
      "                 num_lanes=4,                          # Number of Timelines\n",
      "                 domain=[0,5]                          # Time Domain\n",
      "                ).view()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": null
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Lane names can also be specified as strings (or hashable objects that have an str representation) as follows"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "A = [\n",
      "    \n",
      "        [0, 3, \"zero\"],\n",
      "        [4, 5, \"two\"],\n",
      "]\n",
      "\n",
      "B = [\n",
      "        [0, 2, 1],\n",
      "        [2, 3, \"three\"],\n",
      "        [3, 4, \"zero\"],\n",
      "]\n",
      "\n",
      "C =  [\n",
      "        [0, 2, \"three\"],\n",
      "        [2, 3, \"two\"],\n",
      "        [3, 4, 1],\n",
      "]\n",
      "\n",
      "EVENTS = {}\n",
      "EVENTS[\"A\"] = A\n",
      "EVENTS[\"B\"] = B\n",
      "EVENTS[\"C\"] = C\n",
      "\n",
      "trappy.EventPlot(EVENTS,\n",
      "                 keys=EVENTS.keys,                     # Name of the Process Element\n",
      "                 lanes=[\"zero\", 1, \"two\", \"three\"],\n",
      "                 domain=[0,5]                          # Time Domain\n",
      "                ).view()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": null
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "It is also possible to define a colour map to associate a specific colour to each event. A colour string can be:\n",
      "\n",
      " - a colour name, `green`, `red`, `blue`, etc.\n",
      " \n",
      " - the HEX representation of the colour, `#0000FF` for blue, `#FF0000` for red\n",
      " "
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# Using colour names\n",
      "trappy.EventPlot(EVENTS,\n",
      "                 keys=EVENTS.keys,                     # Name of the Process Element\n",
      "                 lanes=[\"zero\", 1, \"two\", \"three\"],\n",
      "                 domain=[0,5],                         # Time Domain\n",
      "                 color_map={\"A\" : \"blue\", \"B\" : \"red\", \"C\" : \"green\"}\n",
      "                ).view()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": null
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# Using HEX representation of colours\n",
      "trappy.EventPlot(EVENTS,\n",
      "                 keys=EVENTS.keys,                     # Name of the Process Element\n",
      "                 lanes=[\"zero\", 1, \"two\", \"three\"],\n",
      "                 domain=[0,5],                         # Time Domain\n",
      "                 color_map={\"A\" : \"\t#ffa07a\", \"B\" : \"#f08080\", \"C\" : \"#add8e6\"}\n",
      "                ).view()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": null
    },
    {
     "cell_type": "heading",
     "level": 1,
     "metadata": {},
     "source": [
      "TracePlot"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "A specification of the EventPlot creates a kernelshark like plot if the sched_switch event is enabled in the traces"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "f = setup_sched()\n",
      "trappy.plotter.plot_trace(f)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": null
    },
    {
     "cell_type": "heading",
     "level": 1,
     "metadata": {},
     "source": [
      "Exporting notebooks with interactive plots to HTML"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "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",
      "```\n",
      "jupyter nbconvert --to=trappy.nbexport.HTML notebook.ipynb\n",
      "```\n",
      "You need nbconvert >= 4.2 and trappy has to be in your `PYTHONPATH`."
     ]
    }
   ],
   "metadata": {}
  }
 ]
}