def html_data1dint(data): """HTML representation: Data1DInt If have matplotlib then plot the data, otherwise summarize it. """ from sherpa.plot import DataHistogramPlot, backend dtype = type(data).__name__ plotter = DataHistogramPlot() plotter.prepare(data) summary = '{} Plot'.format(dtype) try: out = backend.as_html_plot(plotter, summary) except AttributeError: out = None if out is not None: return formatting.html_from_sections(data, [out]) # Summary properties # meta = [] if data.name is not None and data.name != '': meta.append(('Identifier', data.name)) meta.append(('Number of bins', len(data.xlo))) # Should this only be displayed if a filter has been applied? # fexpr = data.get_filter_expr() nbins = data.get_dep(filter=True).size meta.append(('Using', '{} with {} bins'.format(fexpr, nbins))) # Rely on the _fields ordering, ending at staterror for f in data._fields[1:]: if f == 'staterror': break meta.append((f.upper(), getattr(data, f))) if data.staterror is not None: meta.append(('Statistical error', data.staterror)) if data.syserror is not None: meta.append(('Systematic error', data.syserror)) ls = [formatting.html_section(meta, summary=dtype + ' Summary', open_block=True)] return formatting.html_from_sections(data, ls)
def _repr_html_(self): """Return a HTML (string) representation of the stack """ header, data = self._summary_for_display() htab = html_table(header, data, rowcount=False, summary=f'datastack with {data.shape[0]} datasets') return html_from_sections(self, htab)
def as_html(data, fields): """Create HTML representation of a plot Parameters ---------- data : Plot instance The plot object to display. fields : sequence of strings The fields of data to use. """ # Would like a nicer way to set the summary label, but without # adding a per-class field for this it is safest just to use # the object name. meta = [] for name in fields: # skip records which we don't know about. This indicates # an error in the calling code, but we don't want it to # stop the generation of the HTML. # try: val = getattr(data, name) except Exception as e: lgr.debug("Skipping field {}: {}".format(name, e)) continue meta.append((name, val)) ls = [formatting.html_section(meta, open_block=True, summary=type(data).__name__)] return formatting.html_from_sections(data, ls)
def html_parameter(par): """Construct the HTML to display the parameter.""" # Note that as this is a specialized table we do not use # formatting.html_table but create everything directly. # def addtd(val): "Use the parameter to convert to HTML" return '<td>{}</td>'.format(par._val_to_html(val)) out = '<table class="model">' out += '<thead><tr>' cols = ['Component', 'Parameter', 'Thawed', 'Value', 'Min', 'Max', 'Units'] for col in cols: out += '<th>{}</th>'.format(col) out += '</tr></thead><tbody><tr>' out += '<th class="model-odd">{}</th>'.format(par.modelname) out += '<td>{}</td>'.format(par.name) linked = par.link is not None if linked: out += "<td>linked</td>" else: out += '<td><input disabled type="checkbox"' if not par.frozen: out += ' checked' out += '></input></td>' out += addtd(par.val) if linked: # 8592 is single left arrow # 8656 is double left arrow # val = formatting.clean_bracket(par.link.fullname) out += '<td colspan="2">⇐ {}</td>'.format(val) else: out += addtd(par.min) out += addtd(par.max) out += '<td>{}</td>'.format(par._units_to_html()) out += '</tr>' out += '</tbody></table>' ls = ['<details open><summary>Parameter</summary>' + out + '</details>'] return formatting.html_from_sections(par, ls)
def html_data2d(data): """HTML representation: Data2D and derived classes """ dtype = type(data).__name__ """ It would be nice to plot the plot, but there are several questions to resolve, such as: - do we plot each point (okay for sparse data) or binned - simple binning, adaptive binning, hexagonal binning? - do we just pick a number, like 100, to bin the data to """ # Summary properties # meta = [] if data.name is not None and data.name != '': meta.append(('Identifier', data.name)) # NOTE: shape is not well defined, is it x by y or # the inverse, so I am not going to display it at the # moment. # if data.shape != None: # meta.append(('Shape', data.shape)) meta.append(('Number of bins', len(data.y))) # Rely on the _fields ordering, ending at shape for f in data._fields[1:]: if f == 'shape': break meta.append((f.upper(), getattr(data, f))) if data.staterror is not None: meta.append(('Statistical error', data.staterror)) if data.syserror is not None: meta.append(('Systematic error', data.syserror)) ls = [formatting.html_section(meta, summary=dtype + ' Summary', open_block=True)] return formatting.html_from_sections(data, ls)
def as_html_contour(data, summary=None): """Create HTML representation of a contour The output is a SVG representation of the data, as a HTML svg element. Parameters ---------- data : Contour instance The contour object to display. It has already had its prepare method called. summary : str or None, optional The summary of the detail. If not set then the data type name is used. Returns ------- plot : str or None The HTML, or None if there was an error (e.g. prepare not called). """ def plotfunc(): fig = plt.figure() data.contour() return fig svg = as_svg(plotfunc) if svg is None: return None if summary is None: summary = type(data).__name__ ls = [formatting.html_svg(svg, summary)] return formatting.html_from_sections(data, ls)
def html_model(mdl): """Construct the HTML to display the model.""" # Note that as this is a specialized table we do not use # formatting.html_table but create everything directly. # complist = [] nrows = [] for comp in modelcomponents_to_list(mdl): this_comp_nrows = 0 for par in comp.pars: if par.hidden: continue else: this_comp_nrows += 1 if this_comp_nrows > 0: complist.append(comp) nrows.append(this_comp_nrows) out = '<table class="model">' out += '<caption>Expression: {}</caption>'.format( formatting.clean_bracket(mdl.name)) out += '<thead><tr>' cols = ['Component', 'Parameter', 'Thawed', 'Value', 'Min', 'Max', 'Units'] for col in cols: out += '<th>{}</th>'.format(col) out += '</tr></thead><tbody>' for mcount, (n, comp) in enumerate(zip(nrows, complist)): for i, par in enumerate(comp.pars): style = '' if ((i > 0) or (mcount == 0)) else ' class="block"' if par.hidden: continue def addtd(val): "Use the parameter to convert to HTML" return '<td>{}</td>'.format(par._val_to_html(val)) out += '<tr{}>'.format(style) if i == 0: cls = "model-" + ("even" if mcount % 2 == 1 else "odd") out += '<th class="{}" scope="rowgroup" '.format(cls) out += 'rowspan={}>{}</th>'.format(n, comp.name) out += '<td>{}</td>'.format(par.name) if par.link is not None: out += "<td>linked</td>" else: out += '<td><input disabled type="checkbox"' if not par.frozen: out += ' checked' out += '></input></td>' out += addtd(par.val) if par.link is not None: # 8592 is single left arrow # 8656 is double left arrow # out += '<td colspan=2>⇐ {}</td>'.format( formatting.clean_bracket(par.link.fullname)) else: out += addtd(par.min) out += addtd(par.max) out += '<td>{}</td>'.format(par._units_to_html()) out += '</tr>' out += '</tbody></table>' ls = ['<details open><summary>Model</summary>' + out + '</details>'] return formatting.html_from_sections(mdl, ls)