def __call__(self, obj, fmt=None, doc=None): """ Render the supplied HoloViews component using the appropriate backend. The output is not a file format but a suitable, in-memory byte stream together with any suitable metadata. """ plot, fmt = self._validate(obj, fmt) info = {'file-ext': fmt, 'mime_type': MIME_TYPES[fmt]} if self.mode == 'server': return self.server_doc(plot, doc), info elif isinstance(plot, tuple(self.widgets.values())): return plot(), info elif fmt == 'png': if bokeh_version < '0.12.6': raise RuntimeError( 'Bokeh png export only supported by versions >=0.12.6.') elif bokeh_version > '0.12.9': from bokeh.io.export import get_screenshot_as_png else: from bokeh.io import _get_screenshot_as_png as get_screenshot_as_png if bokeh_version > '0.12.6': img = get_screenshot_as_png(plot.state, None) else: img = get_screenshot_as_png(plot.state) imgByteArr = BytesIO() img.save(imgByteArr, format='PNG') return imgByteArr.getvalue(), info elif fmt == 'html': html = self._figure_data(plot, doc=doc) html = "<div style='display: table; margin: 0 auto;'>%s</div>" % html return self._apply_post_render_hooks(html, obj, fmt), info elif fmt == 'json': return self.diff(plot), info
def get_screenshot(self, space, add_info=True): if space in self._plot: if add_info and space in self._toggle and space in self._div: return get_screenshot_as_png(layout([self._toggle[space]],[self._div[space]],[self._plot[space]])) else: return get_screenshot_as_png(self._plot[space]) else: return None
def test_get_screenshot_as_png_large_plot(webdriver): layout = Plot(x_range=Range1d(), y_range=Range1d(), plot_height=800, plot_width=800, toolbar_location=None, outline_line_color=None, background_fill_color=None, border_fill_color=None) bie.get_screenshot_as_png(layout, driver=webdriver) # LC: Although the window size doesn't match the plot dimensions (unclear # why), the window resize allows for the whole plot to be captured assert webdriver.get_window_size() == {'width': 1366, 'height': 768}
def query(): # pylint: disable=too-many-locals """Query script entry point.""" hl.init(default_reference='GRCh38') mt = hl.read_matrix_table(FILTERED_VARIANTS) nrows = mt.count_rows() print(f'mt.count_rows() = {nrows}') # Plot the allele frequency fig = figure( title='Variant AF', x_axis_label='Allele Frequency', y_axis_label='Frequency (%)', ) variant_af = mt.variant_qc.AF[1].collect() af_count, edges = np.histogram(variant_af, bins=100, weights=np.ones(len(variant_af)) / len(variant_af)) variant_af_count = pd.DataFrame({ 'variant_af_count': af_count, 'left': edges[:-1], 'right': edges[1:] }) fig.quad( bottom=0, top=variant_af_count['variant_af_count'], left=variant_af_count['left'], right=variant_af_count['right'], fill_color='blue', line_color='black', ) # Add in the cumulative distribution cumulative_af = np.cumsum(af_count) fig.line( x=variant_af_count['right'], y=cumulative_af, color='gray', line_width=1, legend='Cum dist', ) fig.legend.location = 'top_left' fig_filename = output_path('variant_selection_histogram.png', 'web') with hl.hadoop_open(fig_filename, 'wb') as f: get_screenshot_as_png(fig).save(f, format='PNG') html = file_html(fig, CDN, 'my plot') fig_filename_html = output_path('variant_selection_histogram.html', 'web') with hl.hadoop_open(fig_filename_html, 'w') as f: f.write(html)
def test_get_screenshot_as_png_with_glyph(webdriver, dimensions: Tuple[int, int]) -> None: width, height = dimensions border = 5 layout = Plot(x_range=Range1d(-1, 1), y_range=Range1d(-1, 1), plot_height=width, plot_width=height, toolbar_location=None, min_border=border, hidpi=False, outline_line_color=None, background_fill_color="#00ff00", border_fill_color="#00ff00") glyph = Rect(x="x", y="y", width=2, height=2, fill_color="#ff0000", line_color="#ff0000") source = ColumnDataSource(data=dict(x=[0], y=[0])) layout.add_glyph(source, glyph) png = bie.get_screenshot_as_png(layout, driver=webdriver) assert png.size == (width, height) data = png.tobytes() assert len(data) == 4*width*height # count red pixels in center area count = 0 for x in range(width*height): pixel = data[x*4:x*4+4] if pixel == b"\xff\x00\x00\xff": count += 1 w, h, b = width, height, border expected_count = w*h - 2*b*(w + h) + 4*b**2 assert count == expected_count
def test_get_screenshot_as_png_with_glyph(): layout = Plot(x_range=Range1d(0, 1), y_range=Range1d(0, 1), plot_height=20, plot_width=20, toolbar_location=None, outline_line_color=None, background_fill_color=None, min_border=2, border_fill_color="blue", border_fill_alpha=1) glyph = Rect(x="x", y="y", width=2, height=2, fill_color="red", line_color="red") source = ColumnDataSource(data=dict(x=[0.5], y=[0.5])) layout.add_glyph(source, glyph) png = bie.get_screenshot_as_png(layout) assert png.size == (20, 20) # count 256 red pixels in center area (400 - 20*4 - 16*4) data = png.tobytes() count = 0 for x in range(400): if data[x * 4:x * 4 + 4] == b"\xff\x00\x00\xff": count += 1 assert count == 256 assert len(data) == 1600
def get_image(chart, lib): ''' Gets chart as image ''' if lib == 'BOKEH': img = get_screenshot_as_png(chart) roi_img = img.crop() img_byte_array = io.BytesIO() roi_img.save(img_byte_array, format='PNG') return img_byte_array.getvalue() if lib == 'FOLIUM': ff_options = Options() ff_options.set_headless(headless=True) cap = DesiredCapabilities().FIREFOX cap["marionette"] = True driver = webdriver.Firefox(firefox_options=ff_options, capabilities=cap) driver.get( "data:text/html;charset=utf-8,{html_content}".format( html_content=chart._repr_html_() ) ) return driver.get_screenshot_as_png() return None
def test_get_screenshot_as_png(webdriver: WebDriver, dimensions: Tuple[int, int]) -> None: width, height = dimensions border = 5 layout = Plot(x_range=Range1d(), y_range=Range1d(), height=width, width=height, min_border=border, hidpi=False, toolbar_location=None, outline_line_color=None, background_fill_color="#00ff00", border_fill_color="#00ff00") with silenced(MISSING_RENDERERS): png = bie.get_screenshot_as_png(layout, driver=webdriver) # a WxHpx image of white pixels assert png.size == (width, height) data = png.tobytes() assert len(data) == 4 * width * height assert data == b"\x00\xff\x00\xff" * width * height
def image_base64_bokeh(im): with BytesIO() as buffer: im = get_screenshot_as_png(im) im = im.resize((width, height)) im.convert('RGBA').save(buffer, 'png') return base64.b64encode(buffer.getvalue()).decode()
def test_get_screenshot_as_png_with_unicode_unminified(webdriver) -> None: p = figure(title="유니 코드 지원을위한 작은 테스트") with silenced(MISSING_RENDERERS): png = bie.get_screenshot_as_png(p, driver=webdriver, resources=Resources(mode="inline", minified=False)) assert len(png.tobytes()) > 0
def test_get_screenshot_as_png_large_plot(): layout = Plot(x_range=Range1d(), y_range=Range1d(), plot_height=800, plot_width=800, toolbar_location=None, outline_line_color=None, background_fill_color=None, border_fill_color=None) driver = webdriver.PhantomJS(service_log_path=os.path.devnull) assert driver.get_window_size() == {'width': 400, 'height': 300} bie.get_screenshot_as_png(layout, driver=driver) # LC: Although the window size doesn't match the plot dimensions (unclear # why), the window resize allows for the whole plot to be captured assert driver.get_window_size() == {'width': 1366, 'height': 768} # Have to manually clean up the driver session driver.quit()
def test_get_screenshot_as_png_with_unicode_minified(webdriver): p = figure(title="유니 코드 지원을위한 작은 테스트") png = bie.get_screenshot_as_png(p, driver=webdriver, resources=Resources(mode="inline", minified=True)) assert len(png.tobytes()) > 0
def test_get_screenshot_as_png_with_unicode_unminified(webdriver): layout = Div(text="유니 코드 지원을위한 작은 테스트") png = bie.get_screenshot_as_png(layout, driver=webdriver, resources=Resources(mode="inline", minified=False)) assert len(png.tobytes()) > 0
def _figure_data(self, plot, fmt='html', doc=None, as_script=False, **kwargs): """ Given a plot instance, an output format and an optional bokeh document, return the corresponding data. If as_script is True, the content will be split in an HTML and a JS component. """ model = plot.state if doc is None: doc = plot.document else: plot.document = doc for m in model.references(): m._document = None doc.theme = self.theme doc.add_root(model) # Bokeh raises warnings about duplicate tools and empty subplots # but at the holoviews level these are not issues logger = logging.getLogger(bokeh.core.validation.check.__file__) logger.disabled = True if fmt == 'png': from bokeh.io.export import get_screenshot_as_png img = get_screenshot_as_png(plot.state, None) imgByteArr = BytesIO() img.save(imgByteArr, format='PNG') data = imgByteArr.getvalue() if as_script: b64 = base64.b64encode(data).decode("utf-8") (mime_type, tag) = MIME_TYPES[fmt], HTML_TAGS[fmt] src = HTML_TAGS['base64'].format(mime_type=mime_type, b64=b64) div = tag.format(src=src, mime_type=mime_type, css='') js = '' else: try: with silence_warnings(EMPTY_LAYOUT, MISSING_RENDERERS): js, div, _ = notebook_content(model) html = NOTEBOOK_DIV.format(plot_script=js, plot_div=div) data = encode_utf8(html) doc.hold() except: logger.disabled = False raise logger.disabled = False plot.document = doc if as_script: return div, js return data
def _renew_images(self, driver=None): """Renew thumbnail and deep zoom images. Parameters ---------- driver selenium webdriver instance, if not given a firefox instance is created using `utils.get_firefox_webdriver()` Returns ------- None """ plot = self.get_plot(thumbnail=True) # # Create a plot and save a thumbnail image in in-memory file # generate_driver = not driver if generate_driver: driver = webdriver_control.create() driver.implicitly_wait(1200) try: image = get_screenshot_as_png(plot, driver=driver, timeout=20) thumbnail_height = 400 thumbnail_width = int(image.size[0] * thumbnail_height / image.size[1]) image.thumbnail((thumbnail_width, thumbnail_height)) image_file = io.BytesIO() image.save(image_file, 'PNG') # # Remove old thumbnail # self.thumbnail.delete() # # Save the contents of in-memory file in Django image field # self.thumbnail.save( f'{self.id}/thumbnail.png', ContentFile(image_file.getvalue()), ) except RuntimeError as exc: _log.error(f"Cannot generate thumbnail for topography {self.id}. Reason: {exc}") self.thumbnail.delete() _log.warning(f"Thumbnail generation failed for topography {self.id}. Deleted old thumbnail which could be outdated.") if generate_driver: driver.close() # important to free memory if self.size_y is not None: # This is a topography (map), we need to create a Deep Zoom Image make_dzi(self.topography(), user_directory_path(self, f'{self.id}/dzi'))
def save_image_figure(f, jpg_path): f.toolbar.logo = None f.toolbar_location = None f.title = None f.sizing_mode = 'fixed' im = get_screenshot_as_png(f) w, h = im.size im = im.convert('RGB').crop((1, 1, w, h)).resize((w, h)) im.save(jpg_path) return im
def assign_adaptive_ticker_post_render_hook(self, rendered_data, rendered_obj): plot_factors = rendered_obj.state.x_range.factors desired_num_ticks = self.get_factor_type_dependent_ticks(plot_factors) self.assign_adaptive_ticker(rendered_obj.state, desired_num_ticks) img = get_screenshot_as_png(rendered_obj.state, driver=state.webdriver) imgByteArr = BytesIO() img.save(imgByteArr, format='PNG') data = imgByteArr.getvalue() return data
def test_get_screenshot_as_png_with_unicode_unminified(): layout = Div(text="유니 코드 지원을위한 작은 테스트") driver = create_webdriver() try: png = bie.get_screenshot_as_png(layout, driver=driver, resources=Resources(mode="inline", minified=False)) finally: # Have to manually clean up the driver session terminate_webdriver(driver) assert len(png.tobytes()) > 0
def test_get_screenshot_as_png(): layout = Plot(x_range=Range1d(), y_range=Range1d(), plot_height=20, plot_width=20, toolbar_location=None, outline_line_color=None, background_fill_color=None, border_fill_color=None) png = bie.get_screenshot_as_png(layout) assert png.size == (20, 20) # a 20x20px image of transparent pixels assert png.tobytes() == ("\x00"*1600).encode()
def prepare_plots3(target_vals,epoch, METRICSPATH): cfm_file = f"{METRICSPATH}/confusion-{epoch}.png" bok_file = f"{METRICSPATH}/ranking_{epoch}.html" classes, rankings, preds, pred_rank = [],[],[],[] for t_ in target_vals: cl_,ra_,pr_,em_ = t_ classes.append(cl_) rankings.append(ra_) preds.append(pr_) pred_rank.append(em_) classes = np.squeeze(np.concatenate(classes)) rankings = np.squeeze(np.concatenate(rankings)) predictions = np.concatenate([softmax(p,axis=0) for p in preds]) pred_rank = np.concatenate(pred_rank) activations = np.argmax(predictions,axis=1) conf_mat = confusion_matrix(classes,activations) fig = plt.figure(figsize=[10,8]) plot_confusion_matrix(conf_mat, classes=class_names, normalize=False, title=f'Confusion matrix epoch {epoch}') plt.savefig(cfm_file,format="png") pil_image = fig2pil(fig) neptune.send_image('conf_mat', pil_image) df = pd.DataFrame(data={ 'tar': rankings, 'pred': pred_rank, 'class': classes}) palette = magma(num_of_classes + 1) p = figure(plot_width=600, plot_height=800, title=f"Ranking by exercise, epoch {epoch}") p.xgrid.grid_line_color = None p.xaxis.axis_label = 'Target ranking' p.yaxis.axis_label = 'Predicted ranking' for cl in range(num_of_classes): if cl == 6: continue df2 = df.loc[df['class']==cl] p.circle(x=jitter('tar',0.3), y='pred', size=8, alpha=0.1, color=palette[cl], legend=class_names[cl], source=df2 ) p.line(x='tar', y='pred', line_width=2, alpha=0.5, color=palette[cl],legend=class_names[cl], source=df2.groupby(by="tar").mean()) p.legend.location = "top_left" p.legend.click_policy="hide" output_file(bok_file, title="Ranking by exercise") save(p) pil_image2 = get_screenshot_as_png(p) neptune.send_image('rank_distances', pil_image2)
def query(rerun): """Query script entry point.""" hl.init(default_reference='GRCh38') sample_qc_path = output_path('sample_qc.mt') if rerun or not hl.hadoop_exists(sample_qc_path): mt = hl.read_matrix_table(GNOMAD_HGDP_1KG_MT) mt = mt.head(100, n_cols=100) mt_qc = hl.sample_qc(mt) mt_qc.write(sample_qc_path) mt_qc = hl.read_matrix_table(sample_qc_path) plot_filename = output_path('call_rate_plot.png', 'web') if rerun or not hl.hadoop_exists(plot_filename): call_rate_plot = hl.plot.histogram(mt_qc.sample_qc.call_rate, range=(0, 1), legend='Call rate') with hl.hadoop_open(plot_filename, 'wb') as f: get_screenshot_as_png(call_rate_plot).save(f, format='PNG')
def _figure_data(self, plot, fmt='html', doc=None, as_script=False, **kwargs): """ Given a plot instance, an output format and an optional bokeh document, return the corresponding data. If as_script is True, the content will be split in an HTML and a JS component. """ model = plot.state if doc is None: doc = plot.document else: plot.document = doc for m in model.references(): m._document = None doc.theme = self.theme doc.add_root(model) # Bokeh raises warnings about duplicate tools and empty subplots # but at the holoviews level these are not issues logger = logging.getLogger(bokeh.core.validation.check.__file__) logger.disabled = True if fmt == 'png': from bokeh.io.export import get_screenshot_as_png img = get_screenshot_as_png(plot.state, None) imgByteArr = BytesIO() img.save(imgByteArr, format='PNG') data = imgByteArr.getvalue() if as_script: b64 = base64.b64encode(data).decode("utf-8") (mime_type, tag) = MIME_TYPES[fmt], HTML_TAGS[fmt] src = HTML_TAGS['base64'].format(mime_type=mime_type, b64=b64) div = tag.format(src=src, mime_type=mime_type, css='') js = '' else: try: js, div, _ = notebook_content(model) html = NOTEBOOK_DIV.format(plot_script=js, plot_div=div) data = encode_utf8(html) doc.hold() except: logger.disabled = False raise logger.disabled = False plot.document = doc if as_script: return div, js return data
def query(output): # pylint: disable=too-many-locals """Query script entry point.""" hl.init(default_reference='GRCh38') loadings_ht = hl.read_table(LOADINGS) number_of_pcs = hl.len(loadings_ht.loadings).take(1)[0] for i in range(0, (number_of_pcs)): pc = i + 1 p = manhattan_loadings( pvals=hl.abs(loadings_ht.loadings[i]), locus=loadings_ht.locus, title='Loadings of PC ' + str(pc), collect_all=True, ) plot_filename = f'{output}/loadings_manhattan_plot_pc' + str( pc) + '.png' with hl.hadoop_open(plot_filename, 'wb') as f: get_screenshot_as_png(p).save(f, format='PNG') plot_filename_html = 'loadings_pc' + str(pc) + '.html' output_file(plot_filename_html) save(p) subprocess.run(['gsutil', 'cp', plot_filename_html, output], check=False)
def test_get_screenshot_as_png_with_driver(): layout = Plot(x_range=Range1d(), y_range=Range1d(), plot_height=20, plot_width=20, toolbar_location=None, outline_line_color=None, background_fill_color=None, border_fill_color=None) driver = create_webdriver() try: png = bie.get_screenshot_as_png(layout, driver=driver) finally: terminate_webdriver(driver) assert png.size == (20, 20) # a 20x20px image of transparent pixels assert png.tobytes() == ("\x00"*1600).encode()
def generate_daily_chart_image(chart_figure, image_format: str = '.png') -> BytesIO: """ Write the given chart to the descired image format into a BytesIO() object """ if image_format not in SUPPORTED_IMAGE_FORMATS: raise ImageFormatError( f'"{image_format}" not in SUPPORTED_IMAGE_FORMATS: {SUPPORTED_IMAGE_FORMATS}' ) buffer = BytesIO() if image_format == '.png': image = get_screenshot_as_png(chart_figure) image.save(buffer, format='png') buffer.seek(0) return buffer
def test_get_screenshot_as_png_with_driver(): layout = Plot(x_range=Range1d(), y_range=Range1d(), plot_height=20, plot_width=20, toolbar_location=None, outline_line_color=None, background_fill_color=None, border_fill_color=None) driver = webdriver.PhantomJS(service_log_path=os.path.devnull) png = bie.get_screenshot_as_png(layout, driver=driver) # Have to manually clean up the driver session driver.quit() assert png.size == (20, 20) # a 20x20px image of transparent pixels assert png.tobytes() == ("\x00"*1600).encode()
def test_get_screenshot_as_png_with_driver(): layout = Plot(x_range=Range1d(), y_range=Range1d(), plot_height=20, plot_width=20, toolbar_location=None, outline_line_color=None, background_fill_color=None, border_fill_color=None) driver = create_webdriver() try: png = bie.get_screenshot_as_png(layout, driver=driver) finally: terminate_webdriver(driver) assert png.size == (20, 20) # a 20x20px image of transparent pixels assert png.tobytes() == ("\x00" * 1600).encode()
def _figure_data(self, plot, fmt, doc=None, as_script=False, **kwargs): """ Given a plot instance, an output format and an optional bokeh document, return the corresponding data. If as_script is True, the content will be split in an HTML and a JS component. """ model = plot.state if doc is None: doc = plot.document else: plot.document = doc for m in model.references(): m._document = None doc.theme = self.theme doc.add_root(model) # Bokeh raises warnings about duplicate tools and empty subplots # but at the holoviews level these are not issues logger = logging.getLogger(bokeh.core.validation.check.__file__) logger.disabled = True if fmt == 'png': from bokeh.io.export import get_screenshot_as_png img = get_screenshot_as_png(plot.state, None) imgByteArr = BytesIO() img.save(imgByteArr, format='PNG') data = imgByteArr.getvalue() if as_script: b64 = base64.b64encode(data).decode("utf-8") (mime_type, tag) = MIME_TYPES[fmt], HTML_TAGS[fmt] src = HTML_TAGS['base64'].format(mime_type=mime_type, b64=b64) div = tag.format(src=src, mime_type=mime_type, css='') else: div = render_mimebundle(plot.state, doc, plot.comm)[0]['text/html'] plot.document = doc if as_script: return div else: return data
def box_plot(x,y,cl_ar,bok_file,epoch): class_names = ['squat', 'deadlift', 'pushups', 'pullups', 'wallpushups', 'lunges', 'other', 'cleanandjerk'] num_of_classes = len(class_names) df = pd.DataFrame(data={ 'tar': x, 'pred': y, 'class': cl_ar}) palette = magma(num_of_classes + 1) p = figure(plot_width=600, plot_height=800, title=f"Ranking by exercise, epoch {epoch}") p.xgrid.grid_line_color = None p.xaxis.axis_label = 'Target ranking' p.yaxis.axis_label = 'Predicted ranking' for cl in range(num_of_classes): if cl == 6: continue df2 = df.loc[df['class']==cl] p.circle(x=jitter('tar', 0.5), y='pred', size=8, alpha=0.1, color=palette[cl], legend=class_names[cl], source=df2 ) p.line(x='tar', y='pred', line_width=2, alpha=0.5, color=palette[cl], source=df2.groupby(by="tar").mean()) p.legend.location = "top_left" p.legend.click_policy="hide" output_file(bok_file, title="Ranking by exercise") save(p) pil_image2 = get_screenshot_as_png(p) neptune.send_image('rank_distances', pil_image2)
def handle_output(self, p, plot_type, prefix="bokeh_"): if plot_type == 'native': return p elif plot_type == 'jupyter': show(p) return None elif plot_type == 'png': logging.debug("exporting figure to png") fd, temp_path = tempfile.mkstemp(dir=self.get_destination(), suffix=".png", prefix=prefix) os.close(fd) logging.info("tmppath@%s" % temp_path) export_png(p, filename=temp_path) display(Image(temp_path)) return temp_path elif plot_type == 'screenshot': logging.debug("exporting figure to screenshot") image = get_screenshot_as_png(p) return image else: logging.error("unknown plottype") sys.exit(0) return "unknown plottype"
def query(): # pylint: disable=too-many-locals """Query script entry point.""" hl.init(default_reference='GRCh38') scores = hl.read_table(SCORES) scores = scores.annotate(cohort_sample_codes=hl.if_else( scores.s.contains('snp_chip'), 'snp_chip', 'tob_wgs')) labels = scores.cohort_sample_codes hover_fields = dict([('s', scores.s)]) # get percent variance explained eigenvalues = hl.import_table(EIGENVALUES) eigenvalues = eigenvalues.to_pandas() eigenvalues.columns = ['eigenvalue'] eigenvalues = pd.to_numeric(eigenvalues.eigenvalue) variance = eigenvalues.divide(float(eigenvalues.sum())) * 100 variance = variance.round(2) # Get number of PCs number_of_pcs = len(eigenvalues) for i in range(0, (number_of_pcs - 1)): pc1 = i pc2 = i + 1 print(f'PC{pc1 + 1} vs PC{pc2 + 1}') p = hl.plot.scatter( scores.scores[pc1], scores.scores[pc2], label=labels, title='TOB-WGS + TOB SNP Chip', xlabel='PC' + str(pc1 + 1) + ' (' + str(variance[pc1]) + '%)', ylabel='PC' + str(pc2 + 1) + ' (' + str(variance[pc2]) + '%)', hover_fields=hover_fields, ) plot_filename = output_path('pc' + str(pc2) + '.png', 'web') with hl.hadoop_open(plot_filename, 'wb') as f: get_screenshot_as_png(p).save(f, format='PNG') html = file_html(p, CDN, 'my plot') plot_filename_html = output_path(f'pc{pc2}.html', 'web') with hl.hadoop_open(plot_filename_html, 'w') as f: f.write(html) # Get partner sample information sample_names = scores.s.collect() def sample_type(sample_name): if sample_name.endswith('snp_chip'): partner_name = re.sub('_snp_chip', '', sample_name) tech = 'snp' else: partner_name = sample_name + '_snp_chip' tech = 'wgs' if partner_name in sample_names: prefix = 'dual_' else: prefix = '' return prefix + tech # save as html labels = list(map(sample_type, sample_names)) html = pd.DataFrame({ 'sample_name': sample_names, 'sample_tech': labels }).to_html() plot_filename_html = output_path(f'sample_technology.html', 'web') with hl.hadoop_open(plot_filename_html, 'w') as f: f.write(html) # plot cohort_sample_codes = list(set(labels)) tooltips = [('labels', '@label'), ('samples', '@samples')] for i in range(0, (number_of_pcs - 1)): pc1 = i pc2 = i + 1 plot = figure( title='Reprocessed Sample Projection', x_axis_label='PC' + str(pc1 + 1) + ' (' + str(variance[pc1]) + '%)', y_axis_label='PC' + str(pc2 + 1) + ' (' + str(variance[pc1]) + '%)', tooltips=tooltips, ) source = ColumnDataSource( dict( x=scores.scores[pc1].collect(), y=scores.scores[pc2].collect(), label=labels, samples=sample_names, )) plot.circle( 'x', 'y', alpha=0.5, source=source, size=8, color=factor_cmap('label', Dark2[len(cohort_sample_codes)], cohort_sample_codes), legend_group='label', ) plot.add_layout(plot.legend[0], 'left') plot_filename = output_path('technology_type_pc' + str(pc2) + '.png', 'web') with hl.hadoop_open(plot_filename, 'wb') as f: get_screenshot_as_png(plot).save(f, format='PNG') html = file_html(plot, CDN, 'my plot') plot_filename_html = output_path(f'technology_type_pc{pc2}.html', 'web') with hl.hadoop_open(plot_filename_html, 'w') as f: f.write(html)
def _figure_data(self, plot, fmt, doc=None, as_script=False, **kwargs): """ Given a plot instance, an output format and an optional bokeh document, return the corresponding data. If as_script is True, the content will be split in an HTML and a JS component. """ model = plot.state if doc is None: doc = plot.document else: plot.document = doc for m in model.references(): m._document = None doc.theme = self.theme doc.add_root(model) # Bokeh raises warnings about duplicate tools and empty subplots # but at the holoviews level these are not issues logger = logging.getLogger(bokeh.core.validation.check.__file__) logger.disabled = True if fmt == 'gif': from bokeh.io.export import get_screenshot_as_png, create_webdriver webdriver = create_webdriver() nframes = len(plot) frames = [] for i in range(nframes): plot.update(i) img = get_screenshot_as_png(plot.state, webdriver) frames.append(img) webdriver.close() bio = BytesIO() duration = (1. / self.fps) * 1000 frames[0].save(bio, format='GIF', append_images=frames[1:], save_all=True, duration=duration, loop=0) bio.seek(0) data = bio.read() elif fmt == 'png': from bokeh.io.export import get_screenshot_as_png img = get_screenshot_as_png(plot.state, None) imgByteArr = BytesIO() img.save(imgByteArr, format='PNG') data = imgByteArr.getvalue() else: div = render_mimebundle(plot.state, doc, plot.comm)[0]['text/html'] if as_script and fmt in ['png', 'gif']: b64 = base64.b64encode(data).decode("utf-8") (mime_type, tag) = MIME_TYPES[fmt], HTML_TAGS[fmt] src = HTML_TAGS['base64'].format(mime_type=mime_type, b64=b64) div = tag.format(src=src, mime_type=mime_type, css='') plot.document = doc if as_script: return div else: return data