ILinePlotGen.py revision bd75d5c3abd977e41ce8c9f7e4740a552a239932
18741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh# $Copyright: 28741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh# ---------------------------------------------------------------- 38741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh# This confidential and proprietary software may be used only as 48741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh# authorised by a licensing agreement from ARM Limited 58741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh# (C) COPYRIGHT 2015 ARM Limited 68741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh# ALL RIGHTS RESERVED 78741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh# The entire notice above must be reproduced on all authorised 88741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh# copies and copies may only be made to the extent permitted 98741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh# by a licensing agreement from ARM Limited. 108741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh# ---------------------------------------------------------------- 118741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh# File: ILinePlotGen.py 128741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh# ---------------------------------------------------------------- 138741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh# $ 148741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh# 158741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh"""This module is reponsible for creating a layout 168741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singhof plots as a 2D axes and handling corener cases 178741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singhand deleting empty plots 188741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh""" 198741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh 208741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singhfrom cr2.plotter import AttrConf 218741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singhimport uuid 228741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singhimport json 238741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singhimport os 248741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singhfrom IPython.display import display, HTML 258741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh 268741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singhif not AttrConf.PLOTTER_IPYTHON: 278741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh raise ImportError("No Ipython Environment found") 288741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh 298741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh 308741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singhclass ILinePlotGen(object): 318741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh 328741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh """Cols is the number of columns to draw 338741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh rows are calculated as 1D - 2D transformation 348741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh the same transformation is used to index the 358741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh axes array 368741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh """ 378741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh 388741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh def _add_lib(self): 398741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh """Add Library String""" 408741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh lib_str = '<script src="{0}"></script>'.format( 418741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh AttrConf.DYGRAPH_LIB_URL) 428741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh self._html.append(lib_str) 438741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh 448741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh def _add_graph_cell(self, fig_name): 458741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh """Add a HTML table cell to hold the plot""" 468741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh 478741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh width = int(self._attr["width"] / self._cols) 488741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh 49c4abeeed1f450cbe153228de5a46dc0c1b488a8eKapileshwar Singh cell = '<td style="border-style: hidden;"><div class="ilineplot" id="{0}" style="width: \ 508741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh{1}px; height: {2}px;"></div></td>'.format(fig_name, 518741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh width, 528741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh self._attr["height"]) 538741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh 548741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh self._html.append(cell) 558741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh 568741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh def _add_legend_cell(self, fig_name): 578741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh """Add HTML table cell for the legend""" 588741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh 598741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh width = int(self._attr["width"] / self._cols) 608741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh legend_div_name = fig_name + "_legend" 618741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh cell = '<td style="border-style: hidden;"><div style="text-align:right; \ 628741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singhwidth: {0}px; height: auto;"; id="{1}"></div></td>'.format(width, 638741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh legend_div_name) 648741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh 658741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh self._html.append(cell) 668741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh 678741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh def _begin_row(self): 688741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh """Add the opening tag for HTML row""" 698741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh 708741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh self._html.append("<tr>") 718741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh 728741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh def _end_row(self): 738741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh """Add the closing tag for the HTML row""" 748741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh 758741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh self._html.append("</tr>") 768741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh 778741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh def _generate_fig_name(self): 788741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh """Generate a unique figure name""" 798741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh 808741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh fig_name = "fig_" + uuid.uuid4().hex 818741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh self._fig_map[self._fig_index] = fig_name 828741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh self._fig_index += 1 838741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh return fig_name 848741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh 858741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh def _init_html(self): 868741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh """Initialize HTML code for the plots""" 878741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh 888741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh width = self._attr["width"] 898741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh self._add_lib() 908741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh table = '<table style="width: {0}px; border-style: hidden;">'.format( 918741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh width) 928741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh self._html.append(table) 938741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh 948741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh for _ in range(self._rows): 958741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh self._begin_row() 968741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh 978741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh legend_figs = [] 988741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh for _ in range(self._cols): 998741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh fig_name = self._generate_fig_name() 1008741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh legend_figs.append(fig_name) 1018741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh self._add_graph_cell(fig_name) 1028741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh 1038741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh self._end_row() 1048741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh self._begin_row() 1058741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh 1068741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh for l_fig in legend_figs: 1078741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh self._add_legend_cell(l_fig) 1088741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh 1098741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh self._end_row() 1108741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh display(HTML(self.html())) 1118741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh 112bd75d5c3abd977e41ce8c9f7e4740a552a239932Kapileshwar Singh def __init__(self, cols, num_plots, **kwargs): 1138741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh """ 1148741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh Args: 1158741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh cols (int): Number of plots in a single line 1168741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh num_plots (int): Total Number of Plots 1178741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh """ 1188741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh 1198741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh self._cols = cols 120bd75d5c3abd977e41ce8c9f7e4740a552a239932Kapileshwar Singh self._attr = kwargs 1218741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh self._html = [] 1228741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh self._js = [] 1238741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh self.num_plots = num_plots 1248741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh self._fig_map = {} 1258741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh self._fig_index = 0 1268741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh 1278741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh self._single_plot = False 1288741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh if self.num_plots == 0: 1298741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh raise RuntimeError("No plots for the given constraints") 1308741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh 1318741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh if self.num_plots < self._cols: 1328741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh self._cols = self.num_plots 1338741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh self._rows = (self.num_plots / self._cols) 1348741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh 1358741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh if self.num_plots % self._cols != 0: 1368741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh self._rows += 1 1378741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh 1388741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh self._attr["width"] = AttrConf.HTML_WIDTH 1398741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh self._attr["height"] = AttrConf.HTML_HEIGHT 1408741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh self._init_html() 1418741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh self._init_js() 1428741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh 1438741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh def add_plot(self, plot_num, data_frame, title=""): 1448741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh """Add a plot to for a corresponding index""" 1458741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh 1468741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh fig_name = self._fig_map[plot_num] 1478741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh fig_params = {} 1488741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh fig_params["data"] = json.loads(data_frame.to_json()) 1498741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh fig_params["name"] = fig_name 1508741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh fig_params["rangesel"] = False 1518741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh fig_params["logscale"] = False 1528741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh fig_params["title"] = title 1538741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh fig_params["step_plot"] = self._attr["step_plot"] 154bd75d5c3abd977e41ce8c9f7e4740a552a239932Kapileshwar Singh fig_params["fill_graph"] = self._attr["fill"] 1558741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh 156c4abeeed1f450cbe153228de5a46dc0c1b488a8eKapileshwar Singh json_file = os.path.join(AttrConf.PLOTTER_STATIC_DATA_DIR, fig_name + ".json") 157c4abeeed1f450cbe153228de5a46dc0c1b488a8eKapileshwar Singh fh = open(json_file, "w") 158c4abeeed1f450cbe153228de5a46dc0c1b488a8eKapileshwar Singh json.dump(fig_params, fh) 159c4abeeed1f450cbe153228de5a46dc0c1b488a8eKapileshwar Singh fh.close() 1608741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh 1618741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh def finish(self): 1628741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh """Called when the Plotting is finished""" 1638741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh 1648741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh figs = [] 1658741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh 1668741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh for fig_idx in self._fig_map.keys(): 1678741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh figs.append(self._fig_map[fig_idx]) 1688741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh 1698741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh display(HTML(self.js())) 1708741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh 1718741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh def _init_js(self): 1728741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh """Initialize Javascript for the plots""" 1738741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh 1748741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh base_dir = os.path.dirname(os.path.realpath(__file__)) 1758741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh js_file = os.path.join(base_dir, "js/ILinePlot.js") 1768741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh 1778741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh js_fh = open(js_file, 'r') 1788741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh self._js = js_fh.readlines() 1798741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh js_fh.close() 1808741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh 1818741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh def js(self): 1828741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh """Return the raw js text""" 1838741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh 1848741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh return "\n".join(self._js) 1858741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh 1868741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh def html(self): 1878741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh """Return the raw HTML text""" 1888741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh 1898741ef170bc7fcc772837fd2db3df2f8d74b6c10Kapileshwar Singh return "\n".join(self._html) 190