def savesvg(plot, filepath, *args, **kwargs): # TODO: Implement hooks here. # br = hv.renderer('matplotlib') # svg = br.get_plot(plot) # svg = svg.state # svg.output_backend='svg' hv.save(plot, filepath)
def get_html_header() -> str: """Return the javascript includes needed to render holoviews plots """ hv.extension("bokeh") hmap = hv.HoloMap() # need at least two plots in the holoviews for it to work hmap["a"] = hv.Curve([(0, 1), (0, 1)]) hmap["b"] = hv.Curve([(0, 1), (0, 1)]) fp = io.BytesIO() hv.save(hmap, fp, fmt="html") html = fp.getvalue().decode("UTF-8") # need to add root tag to parse with lxml doc = pq(html) header = "" for script in doc("script"): try: script.attrib["src"] except KeyError: pass else: # need to prevent self-closing script tags <src ... /> does not work in # Firefox (and maybe other browsers) header += (ET.tostring( script, short_empty_elements=False).decode("UTF-8").strip()) return header
def execute(csvfilepath): #df = pd.read_csv('Dataset/desktop_tracks.csv') df = pd.read_csv(csvfilepath) print(max(df['Mean'])) tracks = [ df.loc[df['Mean'] == x, ['Slice', 'X', 'Y', 'Mean']] for x in range(1, int(max(df['Mean']))) ] print("Number of Tracks:", len(tracks)) tracklists = [ list(zip(tracks[x]['X'], tracks[x]['Y'], tracks[x]['Slice'])) for x in range(len(tracks)) ] tracklists[0:3] trackplot = hv.Path(tracklists, vdims='time') trackplot.opts(cmap='Inferno', color='time', line_width=2) outfilename = "trackplot.html" hv.save(trackplot, outfilename, fmt='html') #file_name = "hello_world.txt" #copyfile(input, file_name) #f = open(file_name, "w") #f.write("Hello, World!") #f.close() return {'Output': outfilename}
def _save(self, plot: str) -> None: save_path = Path( get_filepath_str(self._get_save_path(), self._protocol)) save_path.parent.mkdir(parents=True, exist_ok=True) hv.save(plot, save_path, **self._save_args) self._invalidate_cache()
def plot_curve( func: Callable[[np.ndarray], np.ndarray] = lambda x: 1 + (x - 2) ** 2, x_min: int = 0, x_max: int = 5, renderer: hv.renderer = hv.renderer("bokeh"), ): xs = np.linspace(x_min, x_max, 100) ys = [func(x) for x in xs] curve = hv.Curve((xs, ys), "x", hv.Dimension("f(x)"), label="Graph of f") curve.opts(opts.Curve(height=600, width=900, line_width=2.50, tools=["hover"])) # Create plot and save it as html file. We create the plot depending on the input # parameter renderer either via bokeh or via plotly. if renderer == "bokeh": renderer = hv.renderer("bokeh") # Using renderer save renderer.save(curve, "bokeh") elif renderer == "plotly": renderer = hv.renderer("plotly").get_plot(curve).state plotly.offline.plot(renderer, filename="plotly.html") elif renderer == "online": hv.save(curve, 'browse_me.html', )
def visualize(self, vis_type: str = 'holoviews', filename: str = 'graph'): lg.info('starting visualization') if not self.is_calculated: raise ValueError("Network not calculated.") color_map = {'S': 'yellow', 'R': 'green', 'I': 'red'} label_map = {'S': 'Susceptible', 'I': 'Infected', 'R': 'Recovered'} if vis_type == 'html': layout = self._holoviews_get_networks(color_by="status", color_map=color_map) + \ self._holoviews_get_metrics(color_map=color_map, label_map=label_map) hv.save(layout, filename + ".html", backend='bokeh') self._add_metric_list(filename, self._static_metrics) webbrowser.open(filename + ".html") elif vis_type == 'mp4': self._save_as_animation(filename + '.mp4', 'status', color_map=color_map, label_map=label_map) elif vis_type == 'gif': self._save_as_animation(filename + '.gif', 'status', color_map=color_map, label_map=label_map) elif vis_type == 'png': self._save_as_png(filename + '.png', color_by='status', color_map=color_map, label_map=label_map) else: raise ValueError("Unrecognised vis_type") lg.info('visualization done')
def GenPositionFiles(file_base, position_data): dir = os.path.dirname(file_base) if dir != '' and not os.path.isdir(dir): os.makedirs(dir) logger.info('Generating surface triangle mesh for: %s', file_base) surface_mesh = sonar.merged_position.GenerateSurfaceMeshFromPositionData( position_data, include_corners=True) # Create a depth map image merged_position_image = sonar.merged_position.GenerateDepthMapImageFromPositionData( position_data) file_name = file_base + '.quantized_map.png' if merged_position_image is not None: logger.info('Saving depth map image: %s', file_name) holoviews.save(merged_position_image, file_name) # Create a depth map STL #file_name = file_base + '.quantized_map.stl' #logger.info('Saving depth map STL: %s', file_name) #sonar.merged_position.CreateStlFromSurfaceMesh(file_name, surface_mesh) else: logger.warning('Unable to produce %s as there is no image', file_name) file_name = file_base + '.quantized_map.obj' if surface_mesh is not None: logger.info('Saving depth map 3D Wavefront OBJ: %s', file_name) sonar.merged_position.CreateObjFromSurfaceMesh(file_name, surface_mesh) else: logger.warning('Unable to produce %s as there is no surface mesh', file_name)
def visualize(self, deltas, phis, to_save, outname=None): hv.extension('matplotlib') tsteps= list(phis.keys()) # hmap*contours hmap = hv.HoloMap({t: hv.Image((self.xs, self.ys, phis[t])) for t in tsteps}) contour_hmap =hv.operation.contours(hmap, levels=5) phi_hmap = hmap * contour_hmap.opts(framewise=True, fig_inches=10) hmap_point = hv.HoloMap({t: hv.Points([(t,deltas[t])]) for t in tsteps}).opts(color='r', s=15) # delta curve try: deltas.pop(0.) except KeyError: print("deltas don't contain time=0 value") pass curve_hmap = (hv.Curve(deltas) *hmap_point).redim.label(x='time', y='delta') overlay_hmap = phi_hmap + curve_hmap # save hmap overlay as gif animation if to_save: outname = outname or utils.get_temp_fname(prefix='ls_propagate', suffix='.gif') hv.save((overlay_hmap.opts(framewise=True, fig_inches=10)), outname, fps=1) print("Saved simulation as gif: ", outname) return overlay_hmap
def main(data_sequence_folder: str, limit: int, start: int, out: str): root = Path(data_sequence_folder) img_folder_path = root.joinpath("imgs/") npz_filepath = root.joinpath("rendered.npz") data = np.load(npz_filepath) datasource = {key: data[key] for key in data.files} images_filepaths = list(img_folder_path.glob("*.png")) images_filepaths.sort() limit = min(start + limit, len(images_filepaths) - 1) with_slides = {} for idx, filepath in enumerate(tqdm(images_filepaths[start:limit])): dataframe = dict([(key, str(value[start + idx])) for key, value in datasource.items()]) image = hv.RGB.load_image(str(filepath)) image.opts(title=f"Frame #{idx+1}") table = hv.Div(pformat(dataframe).replace("\n", "</br>")) fig = image + table with_slides[idx + 1] = fig data.close() hmap = hv.HoloMap(with_slides, "frame") hmap = hmap.collate() path_info = hv.Div(f"Sequence from {str(img_folder_path.parent)}") layout = hv.Layout([path_info] + [hmap]) hv.save(layout, "holomap.html")
def plot_hv(input1, stoichiometric_dict, include_el, opfilename, Min=0, Max=1e9): """ Plotting module input1: list of input elements include_el: list of element for which sankey chart is drawn Min: min mole number species to be included in chart Max: max mole number species to be included in the chart opfilename: opfilename with extension (allowed formats: ['html', 'json', 'auto', 'png', 'widgets', 'scrubber', 'auto', None]) returns: saves sankey chart """ f = open('released_sp.txt', 'r') data = f.readlines() sp_dict = {} for line in data: cols = line.split() sp_dict[cols[0]] = float(cols[1]) e = [] sp = [] val = [] hv.extension('bokeh') for i in include_el: for j in sp_dict: s1 = set() for j1 in stoichiometric_dict[j]: s1.add(j1[0]) if i.upper() in s1: if i == j: if sp_dict[j] > Min and sp_dict[j] < Max: e.append(i) sp.append(j + '*') val.append(sp_dict[j]) else: if sp_dict[j] > Min and sp_dict[j] < Max: e.append(i) sp.append(j) val.append(sp_dict[j]) data = {'elements': e, 'species': sp, 'values': val} print(data) df = pd.DataFrame(data) print(df) sankey = hv.Sankey(df) # , show_values=False sankey.opts(label_position='right', edge_color='species', node_color='elements', labelled=['elements', 'species'], xlabel='part', show_values=False) hv.save(sankey, opfilename, backend='bokeh')
def test_static_detector_plots(): tpc = straxen.holoviews_utils.plot_tpc_circle(straxen.cryostat_outer_radius) diffuer_balls = straxen.holoviews_utils.plot_diffuser_balls_nv() nveto_reflector = straxen.holoviews_utils.plot_nveto_reflector() with TemporaryDirectory() as d: # Have to store plot to make sure it is rendered hv.save(tpc * diffuer_balls * nveto_reflector, os.path.join(d, 'hitlet_matrix.html'))
def save_plot(self): """Save the plot of the wrapped :class:`Swarm` to the target path.""" filename = self._get_file_name() filepath = os.path.join(self.output_path, filename) holoviews.save(self.current_plot, filename=filepath, fmt=self._fmt, **self._save_kwargs, backend=holoviews.Store.current_backend)
def _save(self, data: HoloViews) -> None: bytes_buffer = io.BytesIO() hv.save(data, bytes_buffer, **self._save_args) save_path = get_filepath_str(self._get_save_path(), self._protocol) with self._fs.open(save_path, **self._fs_open_args_save) as fs_file: fs_file.write(bytes_buffer.getvalue()) self._invalidate_cache()
def save(plot, filename, filetype='html'): """ Saves supplied plot with the supplied filename. Default format is html. Can also save as png. """ if filetype == 'svg': raise RuntimeError( "svg format not supported with Bokeh backend in Holoviews.") hv.save(plot, filename, fmt=filetype)
def test_plot_nveto_pattern(): hit = np.zeros(1, dtype=strax.hit_dtype) hit['channel'] = 2000 hit['area'] = 1 nvd = nVETOEventDisplay(pmt_map=_dummy_map) pmt_plot = nvd.plot_nveto(hitlets=hit) with TemporaryDirectory() as d: # Have to store plot to make sure it is rendered hv.save(pmt_plot, os.path.join(d, 'hitlet_matrix.html'))
def test_save_data(self, versioned_hv_writer, dummy_hv_object, tmp_path): """Test saving Holoviews object with enabled versioning.""" versioned_hv_writer.save(dummy_hv_object) test_filepath = tmp_path / "test_image.png" actual_filepath = Path(versioned_hv_writer._get_load_path().as_posix()) hv.save(dummy_hv_object, test_filepath) assert actual_filepath.read_bytes() == test_filepath.read_bytes()
def test_save_data(self, tmp_path, dummy_hv_object, hv_writer): """Test saving Holoviews object.""" hv_writer.save(dummy_hv_object) actual_filepath = Path(hv_writer._filepath.as_posix()) test_filepath = tmp_path / "locally_saved.png" hv.save(dummy_hv_object, test_filepath) assert actual_filepath.read_bytes() == test_filepath.read_bytes() assert hv_writer._fs_open_args_save == {"mode": "wb"} assert hv_writer._save_args == {"fmt": "png"}
def draw_graph(G, label, cmap): print(f'Shading {label}') shaded = (datashade(G, normalization='linear', width=1000, height=1000) * G.nodes).opts( opts.Nodes(color=label, width=1000, cmap=cmap, legend_position='right')) hv.save(shaded, f'graphs/png/{label}.png') hv.save(shaded, f'graphs/html/{label}.html')
def test_static_path_in_holoviews_save(tmpdir): import holoviews as hv hv.Store.set_current_backend('bokeh') plot = hv.Curve(np.random.seed(42)) res = Resources(mode='server', root_url='/') out_file = Path(tmpdir) / 'plot.html' hv.save(plot, out_file, resources=res) content = out_file.read_text() assert 'src="/static/js/bokeh' in content and 'src="static/js/bokeh' not in content assert 'href="/static/extensions/panel/css/' in content and 'href="static/extensions/panel/css/' not in content
def bokeh_test(): if self.webView.isHidden(): self.webView.show() else: curve = hv.Curve(range(10)) img = hv.Image(np.random.rand(10, 10)) buf = BytesIO() hv.save(curve + img, buf, backend='bokeh', fmt='html') self.html = buf.read().decode() self.webView.setHtml(self.html) self.webView.adjustSize()
def plot_sankey(sankey_dataframe, filename, save=True): """The plotting code below is credited to the Holoviews user Gallery: http://holoviews.org/gallery/demos/bokeh/energy_sankey.html#bokeh-gallery-energy-sankey By default this function will save a PNG of the plot used. If not desired, set the "save" kwarg to False or change the hv.save() extension to the desired format.""" sankey = hv.Sankey(sankey_dataframe, label='Citrulinated Proteins') sankey.opts(label_position='right', edge_color='target', node_color='index', cmap='tab20c') if save: # hv.save(sankey, filename+'.png') hv.save(sankey, filename+'.svg')
def GenMapImage(sonar_data): try: holoviews_map = sonar.map.GenMap(sonar_data) # @todo There is a bug somehow where the map tiles dont load in time and renders a white square time.sleep(10) map_file_name = sonar_data.attrs['gen_name'] + '.map.png' logger.info('Saving map image: %s', map_file_name) holoviews.save(holoviews_map, map_file_name) except: logger.exception('Failed to produce map image for: %s', sonar_data.attrs['file_name']) if DEBUG_EXCEPTIONS: raise
def getChordDiagram(messages_df): # name_id = messages_df[['sender_id', 'name']].astype(str) # name_id = name_id.tail(200) # name_id = name_id.drop_duplicates().set_index('sender_id').to_dict() names_dict = {'17289343': 'Ben Pagel', '18257442': 'Logan Camilletti', '23885372': 'Jake Linford', '41651233': 'Shad Karlson', '41651237': 'Jackson Esplin', '43797901': 'John Hammond', '52396192': 'Jon Michael Ossola', '9803929': 'Zach Preator'} df = messages_df[['sender_id', 'favorited_by', 'fav_num']] df = df[df['fav_num'] > 0] df = df[['sender_id', 'favorited_by']] df = df.explode('favorited_by') df = df.groupby(df.columns.tolist()).size().reset_index().rename(columns={0: 'likes'}) df['sender_id'] = df['sender_id'].astype(str) df['favorited_by'] = df['favorited_by'].astype(str) df = df.replace(names_dict) names = list(set(df["favorited_by"].unique().tolist() + df["sender_id"].unique().tolist())) names_dataset = hv.Dataset(pd.DataFrame(names, columns=["Name"])) chord = hv.Chord((df, names_dataset)) # Bokeh # hv.extension("bokeh") # plot = chord.opts(labels='Name', # node_color='Name', # edge_color='favorited_by', # label_index='sender_id', # cmap='Category10', # edge_cmap='Category10', # width=1000, # height=1000, # bgcolor="black", # label_text_color="white") # output_file('templates/likes.html') # save(hv.render(chord)) # hv.save(plot, 'static/likes.svg', fmt='auto') # Matplotlib hv.extension('matplotlib') plot = chord.opts(labels='Name', node_color='Name', edge_color='favorited_by', label_index='sender_id', cmap='Category10', edge_cmap='Category10', bgcolor="black") # output_file('templates/likes.html') # save(plot) hv.save(plot, 'static/likes.svg', fmt='auto') hv.save(plot, 'templates/likes.html', fmt='auto') # export_svg(plot, filename='static/likes.svg') return plot
def save_holoviews(fpath, obj, save_components=False, save_doc=False, save_html=True, save_pickle=False, save_item=False): from holoviews.core.io import Pickler import holoviews as hv obj.opts(title="") if fpath.endswith('.html'): fpath = ".".join(fpath.split(".")[:-1]) if save_pickle: print("saving {}.hvz".format(fpath)) with open(fpath + '.hvz', 'wb') as f: Pickler.save(obj, f) if save_item: print("saving {}-item.json".format(fpath)) from bokeh.embed import json_item p = hv.render(obj, backend='bokeh') item_json = json_item(p) with open(fpath + '-item.json', 'w') as f: json.dump(item_json, f, indent=2) if save_doc: print("saving {}-doc.json".format(fpath)) from bokeh.document import Document p = hv.render(obj, backend='bokeh') doc = Document() doc.add_root(p) doc_json = doc.to_json() with open(fpath + '-doc.json', 'w') as f: json.dump(doc_json, f, indent=2) if save_components: print("saving {}.{{script|div}}".format(fpath)) from bokeh.embed import components p = hv.render(obj, backend='bokeh') script, div = components(p) with open(fpath + '.script', 'w') as f: f.write(script) with open(fpath + '.div', 'w') as f: f.write(div) if save_html: print("saving {}.html".format(fpath)) hv.save(obj, fpath + ".html")
def test_hitlet_matrix(): hit = np.zeros(1, dtype=strax.hit_dtype) hit['time'] = 10 hit['length'] = 2 hit['dt'] = 1 hit['channel'] = 2000 hit['area'] = 1 nvd = nVETOEventDisplay(pmt_map=_dummy_map) hit_m = nvd.plot_hitlet_matrix(hitlets=hit) with TemporaryDirectory() as d: # Have to store plot to make sure it is rendered hv.save(hit_m, os.path.join(d, 'hitlet_matrix.html'))
def visualize(input_frame, output_path, width=2000, height=2000, logz=True, backend="bokeh"): """ Generates high resolution visualizations of supplied Pandas DataFrame using HoloViews. """ try: dask_df = daskdf.from_pandas( input_frame, npartitions=multiprocessing.cpu_count()).persist() logger.info("Converted to Dask Frame..") num_genes, num_samples = input_frame.shape da = dask_df.to_dask_array(True).persist() logger.info("Converted to Dask Array..") viridis_bad_black = copy.copy(mpl.cm.get_cmap("viridis")) viridis_bad_black.set_bad( (1, 0, 1)) # It's actually pink, but it shows up as white in bokeh # Visualize if backend == "matplotlib": img = hv.Image((np.arange(num_samples), np.arange(num_genes), da)) rasterized_img = rasterize(img) rasterized_img.opts(cmap=viridis_bad_black, dpi=400, logz=True) hv.save(rasterized_img, output_path, backend=backend) else: img = hv.Image((np.arange(num_samples), np.arange(num_genes), da)) rasterized_img = rasterize(img, width=width, height=height) rasterized_img.opts(width=width, height=height, cmap=viridis_bad_black, logz=True) hv.save(rasterized_img, output_path, backend="bokeh") logger.info("Output visualization!", output_path=output_path) return output_path except Exception: logger.exception( "Unable to visualize dataframe!", output_path=output_path, width=width, height=height, logz=logz, backend=backend, ) return None
def GenDebugAll(sonar_data, chan_id): try: plots = sonar.all.GenAll(sonar_data, chan_id) chan_name = sonar.lowrance_log_parser.ChannelToStr(chan_id) for k, plot in plots.items(): file_name = sonar_data.attrs['gen_name'] + '.' + str( chan_name) + '.' + k + '.png' logger.info('Saving image: %s', file_name) holoviews.save(plot, file_name) except: logger.exception('Failed to produce depth images for: %s', sonar_data.attrs['file_name']) if DEBUG_EXCEPTIONS: raise
def plot_network(all_transactions): hv.extension('bokeh') hv.output(size=500) links = pd.DataFrame(generate_links(all_transactions)) print(links) hv.Chord(links) nodes = hv.Dataset(pd.DataFrame(generate_nodes(all_transactions)), 'index') nodes.data.head() chord = hv.Chord((links, nodes)).select(value=(1, None)) chord.opts( opts.Chord(cmap='Category20', edge_cmap='Category20', edge_color=dim('source').str(), labels='name', node_color=dim('index').str())) hv.save(chord, 'image.html') print("Network analysis complete, saved as image.html")
def plot_chord(df): # Making and saving and showing Chord cities = list(set(df.cod_name_x.unique()).union(set(df.cod_name_y.unique()))) cities_dataset = hv.Dataset(pd.DataFrame(cities, columns=["city"])) chord = hv.Chord((df, cities_dataset)) chord.opts(hv.opts.Chord(height=400, width=400, title="Flow of families among municipalities", node_cmap="Category20", edge_cmap='Category20', edge_color='cod_name_x', labels='city', node_color='city', bgcolor="black", edge_alpha=0.8, edge_line_width=2, node_size=25, label_text_color="white")) # To save to figure # hv.extension("matplotlib") # hv.output(fig='svg', size=250) hv.save(chord, '../other/PS2_validation/chord.html')
def make_chords(dict_df, type_IO='_PhysUse'): hv.extension('bokeh') hv.output(size=250) for key_dict, dataf in dict_df.items(): links = dict_df[key_dict] nodes = hv.Dataset(pd.DataFrame(node_d), 'index') chord = hv.Chord((links, nodes)).select(value=(5, None)) chord.opts( opts.Chord(cmap='Category20', edge_cmap='Category20', edge_color=dim('source').str(), labels='name', node_color=dim('index').str())) hv.save(chord, 'chord_' + str(key_dict) + str(type_IO) + '.html')
def save_figure(self) -> None: """Saves the holoview""" holoviews.save(self.plot, self.folder_path.joinpath(self.file_name)) return