def build_gui(self): "Builds the basic gui for the gallery." self.btn_prev = pnw.Button(name="<", width=int(2 * self.width / 6)) self.btn_next = pnw.Button(name=">", width=int(2 * self.width / 6)) self.current = pnw.TextInput(value="1", width=int(self.width / 6)) self.image_count = pn.Row("/" + str(self.num_entries), width=int(self.width / 6)) self.gui_image_selection_controlls = pn.Row(self.btn_prev, self.current, self.image_count, self.btn_next, align="center", height=50) if self.sort_cols is not None: self.sorter = pnw.Select(name="Sort by", options=self.sort_cols) self.sorter.param.watch(self.update_sorting, "value") self.sort_order = pnw.CheckButtonGroup( name="Options", options=["Desc.", "Drop duplicates"]) self.sort_order.param.watch(self.update_sorting, "value") self.sort_gui = pn.Row(self.sorter, self.sort_order) if self.sort_cols is not None: self.gui_controlls = pn.Column(self.sort_gui, self.gui_image_selection_controlls) else: self.gui_controlls = self.gui_image_selection_controlls self.btn_prev.on_click(self._previous) self.btn_next.on_click(self._next) self.current.param.watch(self._number_input, "value") self._image = pn.Row( self.get_image_by_index(int(self.current.value) - 1), align="center") self.gui = pn.Column(self.gui_controlls, self.image)
class PanelWithEbandsRobot(BaseRobotPanel): #, metaclass=abc.ABCMeta): """ Mixin class for panels with a robot that owns a list of of |ElectronBands| """ # Widgets to plot ebands. ebands_plotter_mode = pnw.Select( name="Plot Mode", value="gridplot", options=["gridplot", "combiplot", "boxplot", "combiboxplot"]) # "animate", ebands_plotter_btn = pnw.Button(name="Plot", button_type='primary') ebands_df_checkbox = pnw.Checkbox(name='With Ebands DataFrame', value=False) # Widgets to plot edos. edos_plotter_mode = pnw.Select(name="Plot Mode", value="gridplot", options=["gridplot", "combiplot"]) edos_plotter_btn = pnw.Button(name="Plot", button_type='primary') def get_ebands_plotter_widgets(self): return pn.Column(self.ebands_plotter_mode, self.ebands_df_checkbox, self.ebands_plotter_btn) @param.depends("ebands_plotter_btn.clicks") def on_ebands_plotter_btn(self): if self.ebands_plotter_btn.clicks == 0: return ebands_plotter = self.robot.get_ebands_plotter() plot_mode = self.ebands_plotter_mode.value plotfunc = getattr(ebands_plotter, plot_mode, None) if plotfunc is None: raise ValueError("Don't know how to handle plot_mode: %s" % plot_mode) fig = plotfunc(**self.fig_kwargs) col = pn.Column(self._mp(fig), sizing_mode='scale_width') if self.ebands_df_checkbox.value: df = ebands_plotter.get_ebands_frame(with_spglib=True) col.append(self._df(df)) return pn.Row(col, sizing_mode='scale_width') def get_edos_plotter_widgets(self): return pn.Column(self.edos_plotter_mode, self.edos_plotter_btn) @param.depends("edos_plotter_btn.clicks") def on_edos_plotter_btn(self): if self.edos_plotter_btn.clicks == 0: return edos_plotter = self.robot.get_edos_plotter() plot_mode = self.edos_plotter_mode.value plotfunc = getattr(edos_plotter, plot_mode, None) if plotfunc is None: raise ValueError("Don't know how to handle plot_mode: %s" % plot_mode) fig = plotfunc(**self.fig_kwargs) return pn.Row(pn.Column(self._mp(fig)), sizing_mode='scale_width')
def __init__(self, name, json, figure_image=None): self.name = name self.json = json self.figure_image = figure_image self.btn_remove = pw.Button(name='X', button_type='primary') self.btn_remove.on_click(self.on_click_remove)
def __init__(self, parent): """Initialize single adsorbent row. :param parent: Adsorbates instance """ super().__init__() self.parent = parent self.btn_add = pw.Button(name='+', button_type='primary') self.btn_add.on_click(self.on_click_add) self.btn_remove = pw.Button(name='-', button_type='primary') self.btn_remove.on_click(self.on_click_remove) #self.inp_refcode = pw.TextInput(name='Refcode') #self.row = pn.Row(self.inp_name, self.btn_add, self.btn_remove) self.row = pn.GridSpec(height=50) self.row[0, 0:8] = self.inp_name self.row[0, 8] = self.btn_add self.row[0, 9] = self.btn_remove
def build_gui(self): self.delete_button = pnw.Button(name="Delete", width=self.width, height=25) self.overview_table = table_from_dataframe(self.create_overview_df(), height=self.height-25) if self.with_del_button: self.delete_button.on_click(self.delete_entry) self.gui = pn.Column(self.delete_button, self.overview_table) else: self.gui = pn.Column(self.overview_table)
def sequence_alignment_viewer(filename=None): """Sequence alignment viewer""" title = pn.pane.Markdown() aln_btn = pnw.Button(name='align',width=100,button_type='primary') file_input = pnw.FileInput(name='load file',width=100,accept='.fa,.fasta,.faa') aligner_sel = pnw.Select(name='aligner',value='muscle',options=['muscle','clustal','maaft'],width=100) highlight_sel = pnw.Select(name='highlight mode',value='default',options=['default',''],width=100) rowheight_sl = pnw.IntSlider(name='row height',value=10,start=5,end=20,width=120) seq_pane = pn.pane.HTML(name='sequences',height=200,css_classes=['scrollingArea']) bokeh_pane = pn.pane.Bokeh() def update_title(filename): title.object = '### Sequence aligner: %s' %filename def update_file(event): nonlocal seqtext seqtext = file_input.value.decode('utf-8') title.object = file_input.filename update_title(file_input.filename) return def align(event): #this function does the alignment nonlocal seqtext if seqtext is not None: sequences = SeqIO.parse(io.StringIO(seqtext),format='fasta') elif filename is not None: sequences = SeqIO.parse(filename, format='fasta') else: return sequences = list(sequences) #print (sequences) aligner = aligner_sel.value if aligner == 'muscle': aln = utils.muscle_alignment(sequences) elif aligner == 'clustal': aln = utils.clustal_alignment(sequences) elif aligner == 'mafft': aln = utils.mafft_alignment(sequences) if aln is None: bokeh_pane.object = plotters.plot_empty('%s not installed?' %aligner,900) else: #the bokeh pane is then updated with the new figure bokeh_pane.object = plotters.plot_sequence_alignment(aln, row_height=rowheight_sl.value) return seqtext = None file_input.param.watch(update_file,'value') rowheight_sl.param.watch(align,'value') aln_btn.param.watch(align, 'clicks') aln_btn.param.trigger('clicks') update_title(filename) side = pn.Column(aln_btn,file_input,aligner_sel,highlight_sel,rowheight_sl,seq_pane,css_classes=['form'],width=200,margin=20) app = pn.Column(title,pn.Row(side, bokeh_pane), sizing_mode='stretch_width',width_policy='max',margin=20) return app
def create_export_gui(self): self.export_path = pnw.TextInput(name="Export path", value="datasets", height=50) self.export_button = pnw.Button(name="Export", align="end", height=50) self.export_button.on_click(self.export_datasets) export_dataset_name = "" if len(self.created_datasets) == 0 else self.created_datasets[self.export_dataset_overview.selection[0]].name export_description_name = "" if len(self.created_datasets) == 0 else self.created_datasets[self.export_dataset_overview.selection[0]].description self.export_name_input = pnw.TextInput(name="Dataset name", value=export_dataset_name, height=50) self.export_name_input.param.watch(self.change_dataset_name, "value") self.export_description_input = pnw.TextAreaInput(name="Description", value=export_description_name, height=50) self.export_description_input.param.watch(self.change_dataset_description, "value") return pn.Column(pn.Row(self.export_name_input, self.export_description_input), pn.Row(self.export_path, self.export_button))
def __init__(self): """Initialize empty submission.""" super().__init__() self.btn_submit = pw.Button(name='Submit', button_type='primary') self.btn_submit.on_click(self.on_click_submit) self.btn_download = pn.widgets.FileDownload(filename='submission.zip', button_type='primary', callback=self.on_click_download) self.btn_download.data = '' # bug in panel https://github.com/holoviz/panel/issues/1598 self._submit_btns = pn.Row(self.btn_download, self.btn_submit) self._column = pn.Column(objects=[a.row for a in self])
def build_gui(self): self.dataset_filter = self.DATASET_FILTER(self.base_dataset, columns=self.DATASET_FILTER_COLUMNS, width=self.width, height=self.height-50) self.dataset_filter_create_dataset_button = pnw.Button(name="Create", height=50) self.dataset_filter_create_dataset_button.on_click(self.create_dataset) self.dataset_filter_with_export = pn.Column(pn.Row(self.dataset_filter.show(), align="center"), pn.Row(self.dataset_filter_create_dataset_button, align="center")) if self.with_dataset_overview: self.created_datasets_overview = self.MULTI_DATASET_OVERVIEW(self.created_datasets, with_del_button=True, height=150, width=self.width) self.selected_dataset_overview = self.DATASET_OVERVIEW(self.base_dataset, height=self.height-250, width=self.width).gui else: self.created_datasets_overview = self.MULTI_DATASET_OVERVIEW(self.created_datasets, with_del_button=True, height=self.height-100, width=self.width) self.selected_dataset_overview = pn.Spacer(sizing_mode="stretch_both") self.export_gui = self.create_export_gui() self.datasets_overview = pn.Column(pn.Row(self.created_datasets_overview.gui, align="center"), pn.Row(self.selected_dataset_overview, align="center"), pn.Row(self.export_gui, align="center")) if self.with_dataset_overview: self.created_datasets_overview.overview_table.param.watch(self.update_dataset_overview, "selection") self.gui = pn.Tabs(("Dataset Filter", self.dataset_filter_with_export), ("Dataset Overview", self.datasets_overview))
class PhononBandsPlotterPanel(AbipyParameterized): phbands_plotter_mode = pnw.Select( name="Plot Mode", value="gridplot", options=["gridplot", "combiplot", "boxplot", "combiboxplot"]) # "animate", phbands_plotter_units = pnw.Select( name="Units", value="eV", options=["eV", "meV", "Ha", "cm-1", "Thz"]) phbands_plotter_btn = pnw.Button(name="Plot", button_type='primary') def __init__(self, plotter, **params): super().__init__(**params) self.plotter = plotter @param.depends("phbands_plotter_btn.clicks") def on_phbands_plot_btn(self): if self.phbands_plotter_btn.clicks == 0: return plot_mode = self.phbands_plotter_mode.value plotfunc = getattr(self.plotter, plot_mode, None) if plotfunc is None: raise ValueError("Don't know how to handle plot_mode: %s" % plot_mode) fig = plotfunc(units=self.phbands_plotter_units.value, **self.fig_kwargs) df = self.plotter.get_phbands_frame(with_spglib=True) return pn.Row(pn.Column(self._mp(fig), self._df(df)), sizing_mode='scale_width') def get_panel(self): """Return tabs with widgets to interact with the |PhononBandsPlotter|.""" tabs = pn.Tabs() ws = pn.Column(self.phbands_plotter_mode, self.phbands_plotter_units, self.phbands_plotter_btn) tabs.append(("PhbandsPlotter", pn.Row(ws, self.on_phbands_plot_btn, sizing_mode='scale_width'))) return tabs
def main(): global series_viewer series_viewer = SeriesViewer( ) # model_path=sm.output()[0], sim_run=sm.output()[1], locality=sm.output()[2]) button = pnw.Button(name='Refresh', button_type='primary') button.on_click(refresh) # Assembling the panel material.sidebar.append(pn.Param(series_viewer, name='Control Panel')) # material.sidebar.append(button) material.sidebar.append(pn.layout.Divider()) material.sidebar.append(series_viewer.view_meta) # material.sidebar.append(pn.Row(save_fname, save_button)) # material.sidebar.append(pn.widgets.StaticText(sm.simulation_run.path)) material.main.append( pn.Column( pn.Row(pn.Card(series_viewer.view_map, title='Final State')), pn.Row(pn.Card(series_viewer.view_network, title='Network')), pn.Row(pn.Card(series_viewer.view_series, title='Time Map')), pn.Row(pn.Card(series_viewer.altair_series, title='Time Series')), )) return material, series_viewer
class GsrRobotPanel(PanelWithEbandsRobot): """ A Panel to interoperate with multiple GSR files. """ gsr_dataframe_btn = pnw.Button(name="Compute", button_type='primary') def __init__(self, robot, **params): super().__init__(**params) self.robot = robot @param.depends("gsr_dataframe_btn.clicks") def on_gsr_dataframe_btn(self): if self.gsr_dataframe_btn.clicks == 0: return df = self.robot.get_dataframe(with_geo=True) return pn.Column(self._df(df), sizing_mode='stretch_width') def get_panel(self): """Return tabs with widgets to interact with the |GsrRobot|.""" tabs = pn.Tabs() app = tabs.append app(("Summary", pn.Row( bkw.PreText(text=self.robot.to_string(verbose=self.verbose), sizing_mode="scale_both")))) app(("e-Bands", pn.Row(self.get_ebands_plotter_widgets(), self.on_ebands_plotter_btn))) # Add e-DOS tab only if all ebands have k-sampling. if all(abifile.ebands.kpoints.is_ibz for abifile in self.robot.abifiles): app(("e-DOS", pn.Row(self.get_edos_plotter_widgets(), self.on_edos_plotter_btn))) app(("GSR-DataFrame", pn.Row(self.gsr_dataframe_btn, self.on_gsr_dataframe_btn))) return tabs
class PanelWithElectronBands(AbipyParameterized): #, metaclass=abc.ABCMeta): # Bands plot with_gaps = pnw.Checkbox(name='Show gaps') #ebands_ylims #ebands_e0 # e0: Option used to define the zero of energy in the band structure plot. Possible values: # - `fermie`: shift all eigenvalues to have zero energy at the Fermi energy (`self.fermie`). # - Number e.g e0=0.5: shift all eigenvalues to have zero energy at 0.5 eV # - None: Don't shift energies, equivalent to e0=0 set_fermie_to_vbm = pnw.Checkbox(name="Set Fermie to VBM") plot_ebands_btn = pnw.Button(name="Plot e-bands", button_type='primary') # DOS plot. edos_method = pnw.Select(name="e-DOS method", options=["gaussian", "tetra"]) edos_step = pnw.Spinner(name='e-DOS step (eV)', value=0.1, step=0.05, start=1e-6, end=None) edos_width = pnw.Spinner(name='e-DOS Gaussian broadening (eV)', value=0.2, step=0.05, start=1e-6, end=None) plot_edos_btn = pnw.Button(name="Plot e-DOS", button_type='primary') # Fermi surface plot. fs_viewer = pnw.Select(name="FS viewer", options=["matplotlib", "xcrysden"]) plot_fermi_surface_btn = pnw.Button(name="Plot Fermi surface", button_type='primary') #@abc.abstractproperty #def ebands(self): # """Returns the |ElectronBands| object.""" def get_plot_ebands_widgets(self): """Widgets to plot ebands.""" return pn.Column(self.with_gaps, self.set_fermie_to_vbm, self.plot_ebands_btn) @param.depends('plot_ebands_btn.clicks') def on_plot_ebands_btn(self): """Button triggering ebands plot.""" if self.plot_ebands_btn.clicks == 0: return if self.set_fermie_to_vbm.value: self.ebands.set_fermie_to_vbm() fig1 = self.ebands.plot(e0="fermie", ylims=None, with_gaps=self.with_gaps.value, max_phfreq=None, fontsize=8, **self.fig_kwargs) fig2 = self.ebands.kpoints.plot(**self.fig_kwargs) row = pn.Row(self._mp(fig1), self._mp(fig2)) #, sizing_mode='scale_width') text = bkw.PreText(text=self.ebands.to_string(verbose=self.verbose)) return pn.Column(row, text, sizing_mode='scale_width') def get_plot_edos_widgets(self): """Widgets to compute e-DOS.""" return pn.Column(self.edos_method, self.edos_step, self.edos_width, self.plot_edos_btn) @param.depends('plot_edos_btn.clicks') def on_plot_edos_btn(self): """Button triggering edos plot.""" if self.plot_edos_btn.clicks == 0: return edos = self.ebands.get_edos(method=self.edos_method.value, step=self.edos_step.value, width=self.edos_width.value) fig = edos.plot(**self.fig_kwargs) return pn.Row(self._mp(fig), sizing_mode='scale_width') def get_plot_fermi_surface_widgets(self): """Widgets to compute e-DOS.""" return pn.Column(self.fs_viewer, self.plot_fermi_surface_btn) @param.depends('plot_fermi_surface_btn.clicks') def on_plot_fermi_surface_btn(self): if self.plot_fermi_surface_btn.clicks == 0: return # Cache eb3d if hasattr(self, "_eb3d"): eb3d = self._eb3d else: # Build ebands in full BZ. eb3d = self._eb3d = self.ebands.get_ebands3d() if self.fs_viewer.value == "matplotlib": # Use matplotlib to plot isosurfaces corresponding to the Fermi level (default) # Warning: requires skimage package, rendering could be slow. fig = eb3d.plot_isosurfaces(e0="fermie", cmap=None, **self.fig_kwargs) return pn.Row(self._mp(fig), sizing_mode='scale_width') else: raise ValueError("Invalid choice: %s" % self.fs_viewer.value)
obj = RoomOccupancy() datawidget = pnw.DataFrame(data) datawidget.height = 0 def bnc(event): if button.clicks % 2 == 0: datawidget.height = 0 else: datawidget.height = 150 button = pnw.Button(name='Show/Hide data', button_type='primary') button.on_click(bnc) buttonobj = pn.Column(button, datawidget) testobj = pn.Row(obj.param, obj.view) testobj = pn.Column(testobj, buttonobj) #testobj = pn.Row(obj.param, obj.view) testobj.servable() #### End of via objects # #variable = pnw.RadioButtonGroup(name='variable', value='Temperature', # options=list(data.columns)) #window = pnw.IntSlider(name='window', value=10, start=1, end=60) # #@pn.depends(variable, window) #def reactive_outliers(variable, window):
class PlotManager(param.Parameterized): db_manager = DataBaseManager() endpoint = widgets.TextInput(name="Server endpoint", value="http://*****:*****@param.depends("datasource.value", "x_val.value", "y_val.value", "z_val.value", "color.value", "size.value", "endpoint.value", "extra.value") def _url(self): url = self._get_url() self.url_widget.value = url return pn.pane.Str("") @param.depends("button.clicks", watch=True) def _update_iframe(self): iframe = ( '<iframe width="800" height="600" scrolling="yes" frameBorder="0"' 'scrolling="no" src="{}"></iframe>').format(self.url_widget.value) self.display.object = iframe def _get_url(self): url = "%s?" % self.endpoint.value ds_str = "datasource=%s&" % self.datasource.value url += "" if self.datasource.value is None else ds_str url += "" if self.x_val.value is None else "x=%s" % self.x_val.value url += "" if self.y_val.value is None else "&y=%s" % self.y_val.value url += "" if self.z_val.value is None else "&z=%s" % self.z_val.value url += "" if self.color.value is None else "&color=%s" % self.color.value url += "" if self.size.value is None else "&size=%s" % self.size.value url += "" if self.extra.value is None else "&%s" % self.extra.value return url @param.depends("db_manager.connect_btn.clicks", watch=True) def populate_datasources(self): if self.db_manager.connected: datasources = [None] + list( sorted(self.db_manager.get_table_names())) self.datasource.options = datasources return pn.pane.Str() @param.depends("datasource.value", watch=True) def populate_widgets(self): if self.db_manager.connected: columns = list( sorted(self.db_manager.get_column_names( self.datasource.value))) self.x_val.options = [None] + columns self.y_val.options = [None] + columns self.z_val.options = [None] + columns self.color.options = [None] + columns self.size.options = [None] + columns return pn.pane.Str() @property def url(self): return self._get_url() def panel(self): controls = pn.Column( pn.Row(self.datasource, self.endpoint), pn.Row(self.x_val, self.y_val, self.z_val, self.db_manager.connect_btn), pn.Row(self.color, self.size, self.extra)) plot_dash = pn.Column( pn.pane.Markdown("# Plot manager"), controls, pn.Row(self.button, pn.pane.Markdown("**Target url**"), self.url_widget), self.display, self.populate_widgets, self.populate_datasources, self._url, self._update_iframe) return plot_dash return pn.Tabs(("Plot manager", plot_dash), ("DB connection", self.db_manager.panel()))
def predictions_dashboard(path): """Dashboard for viewing results from epitopepredict runs.""" #folder_input = pn.widgets.TextInput(name='path', value='../zaire_test', width=400,width_policy='fit') #reload_btn = pn.widgets.Button(name='reload',width=100,button_type='primary') names = web.get_file_lists(path) if names is None: return preds = web.get_predictors(path, name=names[0]) print(preds) seqname = pnw.Select(name='name', value=names[0], options=names) cutoff_slider = pnw.FloatSlider(name='cutoff', value=.95, start=.75, end=.99, step=0.01) cutoff_method = pnw.Select(name='cutoff method', value='default', options=['default', 'rank']) n_select = pnw.FloatSlider(name='n', value=1, start=1, end=8, step=1) plot_select = pnw.Select(name='plot view', value='tracks', options=['tracks', 'sequence']) table_select = pnw.Select(name='table view', value='promiscuous', options=['promiscuous', 'binders']) colorseq_box = pnw.Checkbox(name='color sequences', value=False) header = pn.pane.Markdown('__total sequences: %s__' % len(names), css_classes=['main']) tables = pn.Tabs(width=900) plot = pn.pane.Bokeh(width=800) debug = pn.pane.Markdown('test', style={ 'font-size': '10pt', 'background-color': 'yellow' }) summary_plot = pn.pane.Bokeh() summary_table_tabs = pn.Tabs() recalc_button = pnw.Button(name='recalculate', width=200) def update_banner(): """Update the banner""" fullpath = os.path.abspath(path) banner = pn.Row( pn.pane.Markdown( '<h4>epitopepredict: %s</h4> [help](%s) version %s' % (fullpath, helppage, __version__), css_classes=['divheader'], sizing_mode='stretch_width')) return banner def update_header(target, event): names = web.get_file_lists(event.new) target.object = "_total sequences: %s_" % str(len(names)) return def callback_getpath(event): path = os.path.getcwd() folder.value = path def update_plot(preds, name, cutoff, n, kind): """Plot data view""" if kind == 'tracks': p = plotting.bokeh_plot_tracks(preds, name=name, cutoff=cutoff, n=n, width=1000, title=name) plot.object = p elif kind == 'sequence': p = plotting.bokeh_plot_sequence(preds, name=name, cutoff=cutoff, n=n, width=1000, title=name, color_sequence=colorseq_box.value) plot.object = p return p def update_tables(preds, name, n): """Tabular views of results""" P = preds[0] view = table_select.value tables.clear() for P in preds: if view == 'promiscuous': df = P.promiscuous_binders(n=n, name=name) else: df = P.get_binders(name=name) res = df.to_html(classes="tinytable sortable") div = '<div class="scrollingArea">%s</div>' % res tables.append((P.name, div)) #tables.append((P.name,pn.pane.HTML('<p>hddsadsadsasda</p>',width=700))) return def update(event): """Update all elements""" name = seqname.value n = n_select.value cutoff = cutoff_slider.value kind = plot_select.value debug.object = name preds = web.get_predictors(path, name=name) update_plot(preds, name=name, cutoff=cutoff, n=n, kind=kind) update_tables(preds, name, n) return def update_summary(path): """Summary info for folder""" data = web.get_summary_tables(path) df = pd.concat(data, sort=True).reset_index() #plot = plotting.bokeh_summary_plot(df) #summary_plot.object = plot summary_table_tabs.clear() a = web.aggregate_summary(data) div = web.get_scrollable_table(a) summary_table_tabs.append(('all', div)) names = list(data.keys()) for n in names: df = data[n] res = df.to_html(classes="tinytable sortable") div = '<div class="scrollingArea">%s</div>' % res summary_table_tabs.append((n, div)) return @pn.depends(seqname.param.value, n_select.param.value) def download_link(name, n): if preds is None: return df = preds[0].promiscuous_binders(n=n, name=name) df.to_csv() return pn.Pane(HTML('<a>download</a>'), width=700) info = pn.pane.Markdown(web.get_readme()) banner = update_banner() update_summary(path) #reload_btn.param.watch(load_predictors, 'clicks') #reload_btn.param.trigger() seqname.param.watch(update, 'value') cutoff_slider.param.watch(update, 'value') n_select.param.watch(update, 'value') table_select.param.watch(update, 'value') plot_select.param.watch(update, 'value') seqname.param.trigger('options', 'value') top = pn.Row(header) #,download_link) left = pn.Column(plot, tables, margin=10, sizing_mode='stretch_width') right = pn.Column(seqname, cutoff_slider, cutoff_method, n_select, plot_select, table_select, colorseq_box, css_classes=['widget-box'], width=200) center = pn.Row(left, right) #bottom = pn.Row(table) main = pn.Column(top, center) summarypane = pn.Column(recalc_button, (pn.Row(summary_table_tabs))) tabs = pn.Tabs(('summary', summarypane), ('sequence', main), ('about', info)) #tabs.append() app = pn.Column(banner, tabs, sizing_mode='stretch_width') return app
class StructurePanel(AbipyParameterized): """ Panel with widgets to interact with an AbiPy Structure """ verbose = param.Integer(0, bounds=(0, None), doc="Verbosity Level") # Convert widgets. output_format = pnw.Select( name="format", value="abinit", options="abinit,cif,xsf,poscar,qe,siesta,wannier90,cssr,json".split( ",")) # Spglib widgets spglib_symprec = pnw.Spinner(name="symprec", value=0.01, start=0.0, end=None, step=0.01) spglib_angtol = pnw.Spinner(name="angtol", value=5, start=0.0, end=None, step=1) # K-path widgets kpath_format = pnw.Select(name="format", value="abinit", options=["abinit", "siesta", "wannier90"]) line_density = pnw.Spinner(name="line density", value=10, step=5, start=0, end=None) # Viewer widgets. viewer_btn = pnw.Button(name="View structure", button_type='primary') viewer = pnw.Select( name="Viewer", value="vesta", options=["vesta", "xcrysden", "vtk", "matplotlib", "mayavi"]) # Mp-match mp_match_btn = pnw.Button(name="Connect to Materials Project", button_type='primary') # Mp-search #mp_search_btn = pnw.Button(name="Connect to Materials Project", button_type='primary') #mp_api_key # GS input generator widgets. gs_input_btn = pnw.Button(name="Generate input", button_type='primary') gs_type = pnw.Select(name="GS type", value="scf", options=["scf", "relax"]) kppra = pnw.Spinner(name="kppra", value=1000, step=1000, start=0, end=None) label2mode = { "unpolarized": 'unpolarized', "polarized": 'polarized', "anti-ferromagnetic": "afm", "non-collinear with magnetism": "spinor", "non-collinear, no magnetism": "spinor_nomag", } spin_mode = pnw.Select(name="SpinMode", value="unpolarized", options=list(label2mode.keys())) def __init__(self, structure, **params): super().__init__(**params) self.structure = structure @param.depends("output_format.value") def convert(self): return pn.Row(bkw.PreText(text=self.structure.convert( fmt=self.output_format.value)), sizing_mode='stretch_width') @param.depends("spglib_symprec.value", "spglib_angtol.value") def spglib_summary(self): s = self.structure.spget_summary( symprec=self.spglib_symprec.value, angle_tolerance=self.spglib_angtol.value) return pn.Row(bkw.PreText(text=s, sizing_mode='stretch_width')) @param.depends("kpath_format.value", "line_density.value") def get_kpath(self): s = self.structure.get_kpath_input_string( fmt=self.kpath_format.value, line_density=self.line_density.value) return pn.Row(bkw.PreText(text=s, sizing_mode='stretch_width')) @param.depends("viewer_btn.clicks") def view(self): if self.viewer_btn.clicks == 0: return import panel as pn #view = self.structure.get_jsmol_view() #from ipywidgets_bokeh import IPyWidget #view = IPyWidget(widget=view) #, width=800, height=300) #from IPython.display import display #display(view) #return pn.Row(display(view)) #return pn.panel(view) js_files = { 'ngl': 'https://cdn.jsdelivr.net/gh/arose/[email protected]/dist/ngl.js' } pn.extension(comms='ipywidgets', js_files=js_files) view = self.structure.get_ngl_view() return pn.panel(view) #pn.config.js_files["ngl"]="https://cdn.jsdelivr.net/gh/arose/[email protected]/dist/ngl.js" #pn.extension() html = """<div id="viewport" style="width:100%; height:100%;"></div> <script> stage = new NGL.Stage("viewport"); stage.loadFile("rcsb://1NKT.mmtf", {defaultRepresentation: true}); </script>""" # html = """ # <script> # document.addeventlistener("domcontentloaded", function () { # var stage = new ngl.stage("viewport"); # stage.loadfile("rcsb://1crn", {defaultrepresentation: true}); # }); # </script>""" # html = """ #<script> #document.addeventlistener("domcontentloaded", function () { # // create a `stage` object # var stage = new NGL.Stage("viewport"); # // load a PDB structure and consume the returned `Promise` # stage.loadFile("rcsb://1CRN").then(function (component) { # // add a "cartoon" representation to the structure component # component.addRepresentation("cartoon"); # // provide a "good" view of the structure # component.autoView(); # }); #}); #</script>""" ngl_pane = pn.pane.HTML(html, height=500, width=500) return pn.Row(ngl_pane) view = self.structure.get_ngl_view() #return self.structure.crystaltoolkitview() #import nglview as nv #view = nv.demo(gui=False) #from bokeh.models import ColumnDataSource #from bokeh.io import show, curdoc #from bokeh.models.widgets import Button, TextInput #from bokeh.layouts import layout, widgetbox #from jsmol_bokeh_extension import JSMol #script_source = ColumnDataSource() #info = dict( # height="100%", # width="100%", # serverURL="https://chemapps.stolaf.edu/jmol/jsmol/php/jsmol.php", # use="HTML5", # j2sPath="https://chemapps.stolaf.edu/jmol/jsmol/j2s", # script= # "background black;load https://chemapps.stolaf.edu/jmol/jsmol-2013-09-18/data/caffeine.mol", #) #applet = JSMol( # width=600, # height=600, # script_source=script_source, # info=info, #) #button = Button(label='Execute') #inp_script = TextInput(value='background white;') #def run_script(): # script_source.data['script'] = [inp_script.value] #button.on_click(run_script) #ly = layout([applet, widgetbox(button, inp_script)]) #show(ly) #curdoc().add_root(ly) return pn.Row(applet) return self.structure.visualize(appname=self.viewer.value) @param.depends("gs_input_btn.clicks") def on_gs_input_btn(self): if self.gs_input_btn.clicks == 0: return from abipy.abio.factories import gs_input from abipy.data.hgh_pseudos import HGH_TABLE gs_inp = gs_input(self.structure, HGH_TABLE, kppa=self.kppra.value, ecut=8, spin_mode=self.label2mode[self.spin_mode.value], smearing=None) gs_inp.pop_vars(("charge", "chksymbreak")) gs_inp.set_vars(ecut="?? # depends on pseudos", nband="?? # depends on pseudos") if self.gs_type.value == "relax": gs_inp.set_vars(optcell=2, ionmov=2, ecutsm=0.5, dilatmx=1.05) gs_inp.set_mnemonics(False) return html_with_copy_to_clipboard(gs_inp._repr_html_()) @param.depends("mp_match_btn.clicks") def on_mp_match_btn(self): if self.mp_match_btn.clicks == 0: return from abipy.core.structure import mp_match_structure mp = mp_match_structure(self.structure, api_key=None, endpoint=None, final=True) if not mp.structures: raise RuntimeError("No structure found in MP database") return pn.Column(self._df(mp.lattice_dataframe), sizing_mode='stretch_width') #@param.depends("mp_search_btn.clicks") #def on_mp_search_btn(self): # if self.mp_search_btn.clicks == 0: return # from abipy.core.structure import mp_search # chemsys_formula_id = self.stucture.formula # mp = mp_search(chemsys_formula_id, api_key=None, endpoint=None, final=True) # if not mp.structures: # raise RuntimeError("No structure found in MP database") # return pn.Column(self._df(mp.lattice_dataframe), sizing_mode='stretch_width') def get_panel(self): """Build panel with widgets to interact with the structure either in a notebook or in a bokeh app""" tabs = pn.Tabs() app = tabs.append row = pn.Row( bkw.PreText(text=self.structure.to_string(verbose=self.verbose), sizing_mode="scale_both")) app(("Summary", row)) ws = pn.Column('# Spglib options', self.spglib_symprec, self.spglib_angtol) app(("Spglib", pn.Row(ws, self.spglib_summary))) ws = pn.Column('# K-path options', self.kpath_format, self.line_density) app(("Kpath", pn.Row(ws, self.get_kpath))) app(("Convert", pn.Row(pn.Column("# Convert structure", self.output_format), self.convert))) app(("View", pn.Row( pn.Column("# Visualize structure", self.viewer, self.viewer_btn), self.view))) ws = pn.Column('# Generate GS input', self.gs_type, self.spin_mode, self.kppra, self.gs_input_btn) app(("GS-input", pn.Row(ws, self.on_gs_input_btn))) app(("MP-match", pn.Row(pn.Column(self.mp_match_btn), self.on_mp_match_btn))) return tabs
class DdbFilePanel(AbipyParameterized): """ A panel to analyze a |DdbFile|. Provides widgets to invoke anaddb and visualize the results. """ verbose = param.Integer(0, bounds=(0, None), doc="Verbosity Level") mpi_procs = param.Integer(1, bounds=(1, None), doc="Number of MPI processes used in anaddb") nqsmall = param.Integer( 10, bounds=(1, None), doc="Number of divisions for smallest vector to generate Q-mesh") ndivsm = param.Integer( 2, bounds=(1, None), doc="Number of divisions for smallest segment in q-path") lo_to_splitting = param.ObjectSelector(default="automatic", objects=["automatic", True, False]) chneut = param.ObjectSelector(default=1, objects=[0, 1, 2], doc="Abinit variable") dipdip = param.ObjectSelector(default=1, objects=[0, 1], doc="Abinit variable") asr = param.ObjectSelector(default=2, objects=[0, 1, 2], doc="Abinit variable") units = param.ObjectSelector(default="eV", objects=["eV", "meV", "Ha", "cm-1", "Thz"], doc="Energy units") dos_method = param.ObjectSelector(default="tetra", objects=["tetra", "gaussian"], doc="Integration method for DOS") temp_range = pnw.RangeSlider(name="T-range", start=0.0, end=1000, value=(0.0, 300.0), step=20) gamma_ev = param.Number(1e-4, bounds=(1e-20, None), doc="Phonon linewidth in eV") w_range = pnw.RangeSlider(name="Frequency range (eV)", start=0.0, end=1.0, value=(0.0, 0.1), step=0.001) get_epsinf_btn = pnw.Button(name="Compute", button_type='primary') plot_phbands_btn = pnw.Button(name="Plot Bands and DOS", button_type='primary') plot_eps0w_btn = pnw.Button(name="Plot eps0(omega)", button_type='primary') plot_vsound_btn = pnw.Button(name="Calculate speed of sound", button_type='primary') def __init__(self, ddb, **params): super().__init__(**params) self.ddb = ddb @param.depends('get_epsinf_btn.clicks') def get_epsinf(self): """Compute eps_infinity and Born effective charges from DDB.""" if self.get_epsinf_btn.clicks == 0: return epsinf, becs = self.ddb.anaget_epsinf_and_becs( chneut=self.chneut, mpi_procs=self.mpi_procs, verbose=self.verbose) gen, inp = self.ddb.anaget_dielectric_tensor_generator( asr=self.asr, chneut=self.chneut, dipdip=self.dipdip, mpi_procs=self.mpi_procs, verbose=self.verbose, return_input=True) eps0 = gen.tensor_at_frequency(w=0, gamma_ev=self.gamma_ev) #eps0 = pnw.DataFrame(eps0.get_dataframe()) return pn.Column(epsinf, eps0, becs, pn.pane.HTML(inp._repr_html_())) @param.depends('plot_eps0w_btn.clicks') def plot_eps0w(self): """Compute eps0(omega) from DDB and plot the results.""" if self.plot_eps0w_btn.clicks == 0: return gen, inp = self.ddb.anaget_dielectric_tensor_generator( asr=self.asr, chneut=self.chneut, dipdip=self.dipdip, mpi_procs=self.mpi_procs, verbose=self.verbose, return_input=True) ws = self.w_range.value w_max = ws[1] if w_max == 1.0: w_max = None # Will compute w_max in plot routine from ph freqs. def p(component, reim): return gen.plot(w_min=ws[0], w_max=w_max, gamma_ev=self.gamma_ev, num=500, component=component, reim=reim, units=self.units, **self.fig_kwargs) # Build grid gspec = pn.GridSpec(sizing_mode='scale_width') gspec[0, 0] = p("diag", "re") gspec[0, 1] = p("diag", "im") gspec[1, 0] = p("offdiag", "re") gspec[1, 1] = p("offdiag", "im") gspec[2, :] = gen.get_oscillator_dataframe(reim="all", tol=1e-6) # Add HTML pane with input. gspec[3, 0] = pn.pane.HTML(inp._repr_html_()) return gspec @param.depends('plot_phbands_btn.clicks') def plot_phbands_and_phdos(self, event=None): """Compute phonon bands and ph-DOSes from DDB and plot the results.""" if self.plot_phbands_btn.clicks == 0: return #self.plot_phbands_btn.button_type = "warning" print("Computing phbands") with self.ddb.anaget_phbst_and_phdos_files( nqsmall=self.nqsmall, qppa=None, ndivsm=self.ndivsm, line_density=None, asr=self.asr, chneut=self.chneut, dipdip=self.dipdip, dos_method=self.dos_method, lo_to_splitting=self.lo_to_splitting, verbose=self.verbose, mpi_procs=self.mpi_procs, return_input=True) as g: phbst_file, phdos_file = g phbands, phdos = phbst_file.phbands, phdos_file.phdos print("Computing phbands completed") # Build grid gspec = pn.GridSpec(sizing_mode='scale_width') gspec[0, 0] = phbands.plot_with_phdos(phdos, units=self.units, **self.fig_kwargs) gspec[0, 1] = phdos_file.plot_pjdos_type(units=self.units, exchange_xy=True, **self.fig_kwargs) gspec[1, 0] = phdos_file.msqd_dos.plot(units=self.units, **self.fig_kwargs) temps = self.temp_range.value gspec[1, 1] = phdos.plot_harmonic_thermo(tstart=temps[0], tstop=temps[1], num=50, **self.fig_kwargs) #msqd_dos.plot_tensor(**self.fig_kwargs) #self.plot_phbands_btn.button_type = "primary" # Add HTML pane with input gspec[2, :] = pn.pane.HTML(g.input._repr_html_()) return gspec @param.depends('plot_vsound_btn.clicks') def plot_vsound(self): """ Compute the speed of sound by fitting phonon frequencies along selected directions by linear least-squares fit. """ if self.plot_vsound_btn.clicks == 0: return from abipy.dfpt.vsound import SoundVelocity sv = SoundVelocity.from_ddb(self.ddb.filepath, num_points=20, qpt_norm=0.1, ignore_neg_freqs=True, asr=self.asr, chneut=self.chneut, dipdip=self.dipdip, verbose=self.verbose, mpi_procs=self.mpi_procs) # Insert results in grid. gspec = pn.GridSpec(sizing_mode='scale_width') gspec[0, :1] = sv.get_dataframe() gspec[1, :1] = sv.plot(**self.fig_kwargs) return gspec def get_panel(self): """Return tabs with widgets to interact with the DDB file.""" tabs = pn.Tabs() app = tabs.append row = pn.Row( bkw.PreText(text=self.ddb.to_string(verbose=self.verbose), sizing_mode="scale_both")) app(("Summary", row)) app(("Ph-bands", pn.Row( pn.Column( "# PH-bands options", *[ self.param[k] for k in ("nqsmall", "ndivsm", "asr", "chneut", "dipdip", "lo_to_splitting") ], self.temp_range, self.plot_phbands_btn), self.plot_phbands_and_phdos))) app(("BECs", pn.Row( pn.Column( "# Born effective charges options", *[ self.param[k] for k in ("asr", "chneut", "dipdip", "gamma_ev") ], self.get_epsinf_btn), self.get_epsinf))) app(("eps0", pn.Row( pn.Column( "# epsilon_0", *[ self.param[k] for k in ("asr", "chneut", "dipdip", "gamma_ev") ], self.w_range, self.plot_eps0w_btn), self.plot_eps0w))) app(("Speed of sound", pn.Row( pn.Column( "# Speed of sound options", *[self.param[k] for k in ("asr", "chneut", "dipdip")], self.plot_vsound_btn), self.plot_vsound))) app(("Global", pn.Column( "# Global parameters", *[self.param[k] for k in ("units", "mpi_procs", "verbose")]))) return tabs
class WriteParquet(param.Parameterized): write_btn = widgets.Button(name="Write to DB", width=WIDGET_WIDTH) read_btn = widgets.Button(name="Read DB table", width=WIDGET_WIDTH) parquet_file = widgets.FileInput(name="Parquet file", width=WIDGET_WIDTH) table = widgets.TextInput(name="New table name", width=200) table_names = widgets.Select(name="DB tables", width=200) def __init__(self, engine, *args, **kwargs): super(WriteParquet, self).__init__(*args, **kwargs) self.engine = engine self.df = None self.db_df = None @param.depends("parquet_file.value", watch=True) def load_parquet(self): f = io.BytesIO() f.write(self.parquet_file.value) f.seek(0) self.df = pd.read_parquet(f) @param.depends("read_btn.clicks", watch=True) def get_datasource(self): if self.table.value != "": sql = "SELECT * \nFROM {}\nLIMIT 10000".format(self.table.value) self.df = self.sql(sql) table = self.table.value if self.table.value != "" else "None" return panel.pane.Str("Selected Table {}".format(table)) def sql(self, sql): df = pd.read_sql(sql, self.engine) return df def get_table_names(self): if self.engine is None: return names = pd.read_sql("SELECT tablename FROM pg_catalog.pg_tables;", self.engine) self.table_names.options = list(sorted( names.values.flatten().tolist())) @param.depends("write_btn.clicks", watch=True) def write_parquet(self): if self.write_btn.clicks > 0: try: self.load_parquet() self.df.to_sql(self.table.value, self.engine) return panel.pane.Str("Success") except Exception as e: return panel.pane.Str(str(e)) return panel.pane.Str("") @param.depends("read_btn.clicks", watch=True) def read_table(self): if self.read_btn.clicks > 0: sql = "SELECT * \nFROM {}\nLIMIT 10000".format( self.table_names.value) self.db_df = self.sql(sql) return panel.pane.HTML(self.db_df.head().to_html()) return panel.pane.Str() def panel(self): return panel.Row( panel.Column( panel.Row(self.table_names), panel.Row(panel.pane.Markdown("Parquet file"), self.parquet_file), panel.Row(self.table), panel.Row(self.read_btn, self.write_btn), ), panel.Column(self.read_table, self.write_parquet), )
def _update_widgets_panel(self): self._default_component[self.component_type] = self.component component = None controls = None if self.component is pn.pane.HoloViews: component = pn.pane.HoloViews(_create_hvplot()) if self.component is pn.pane.ECharts: # Issue https://github.com/holoviz/panel/issues/1817 component = pn.pane.ECharts(_create_echarts_plot(), min_height=400, min_width=200, sizing_mode="stretch_both") if self.component is pnw.Ace: py_code = inspect.getsource(_create_hvplot) component = pnw.Ace( value=py_code, sizing_mode="stretch_width", language="python", height=400, theme=self._ace_theme, ) elif self.component is pnw.AutocompleteInput: component = pnw.AutocompleteInput( name="Autocomplete Input", options=["Biology", "Chemistry", "Physics"], placeholder="Write something here", ) elif self.component is pnw.Button: component = pnw.Button(name="Click me", button_type="primary") elif self.component is pnw.CheckBoxGroup: component = pnw.CheckBoxGroup( name="Checkbox Group", value=["Apple", "Pear"], options=["Apple", "Banana", "Pear", "Strawberry"], inline=True, ) elif self.component is pnw.CheckButtonGroup: component = pnw.CheckButtonGroup( name="Check Button Group", value=["Apple", "Pear"], options=["Apple", "Banana", "Pear", "Strawberry"], button_type="success", ) elif self.component is pnw.Checkbox: component = pnw.Checkbox(name="Checkbox") elif self.component is pnw.ColorPicker: component = pnw.ColorPicker(name="Color Picker", value="#DF3874") elif self.component is pnw.CrossSelector: component = pnw.CrossSelector( name="Fruits", value=["Apple", "Pear"], options=["Apple", "Banana", "Pear", "Strawberry"], height=300, ) elif self.component is pnw.DataFrame: component = self.component(name="Hello") component.value = get_dataframe() component.formatters = get_default_formatters(component.value) controls = pn.Spacer() elif self.component is pnw.DatePicker: component = pnw.DatePicker(name="Date Picker") # Issue: https://github.com/holoviz/panel/issues/1810 # component.start = date(2020, 1, 20) # component.end = date(2020, 2, 20) # component.value = date(2020, 2, 18) elif self.component is pnw.DateRangeSlider: component = self.component(name="Hello") component.start = date(2020, 1, 20) component.end = date(2020, 2, 20) component.value = (date(2020, 2, 18), date(2020, 2, 20)) elif self.component is pnw.DateSlider: component = self.component(name="Hello") component.start = date(2020, 1, 20) component.end = date(2020, 2, 20) component.value = date(2020, 2, 18) elif self.component is pnw.DatetimeInput: component = self.component(name="Hello") component.value = datetime(2020, 2, 18, 1, 2, 3) elif self.component is pnw.DatetimeRangeInput: component = self.component( name="Hello", start=datetime(2020, 1, 20), end=datetime(2020, 2, 20), value=(datetime(2020, 2, 18), datetime(2020, 2, 20)), ) elif self.component is pnw.DiscretePlayer: component = pnw.DiscretePlayer( name="Discrete Player", options=[2, 4, 8, 16, 32, 64, 128], value=32, loop_policy="loop", ) elif self.component is pnw.DiscreteSlider: component = pnw.DiscreteSlider(name="Discrete Slider", options=[2, 4, 8, 16, 32, 64, 128], value=32) elif self.component is pnw.FileDownload: component = pnw.FileDownload(file="README.md", filename="README.md") elif self.component is pnw.FileInput: component = pnw.FileInput(accept=".csv,.json") elif self.component is pnw.FileSelector: component = pnw.FileSelector(name="Hello", max_height=400) elif self.component is pnw.FloatInput: component = pnw.FloatInput(name="FloatInput", value=5.0, step=1e-1, start=0, end=1000) elif self.component is pnw.FloatSlider: component = pnw.FloatSlider(name="Float Slider", start=0, end=3.141, step=0.01, value=1.57) elif self.component is pnw.IntInput: component = pnw.IntInput(name="IntInput", value=5, step=2, start=0, end=1000) elif self.component is pnw.IntRangeSlider: component = pnw.IntRangeSlider(name="Integer Range Slider", start=0, end=100, value=(8, 40), step=2) elif self.component is pnw.IntSlider: component = pnw.IntSlider(name="Integer Slider", start=0, end=20, step=2, value=4) elif self.component is pnw.LiteralInput: component = pnw.LiteralInput(name="Literal Input (dict)", value={"key": [1, 2, 3]}, type=dict) elif self.component is pnw.MenuButton: menu_items = [ ("Option A", "a"), ("Option B", "b"), ("Option C", "c"), None, ("Help", "help"), ] component = pnw.MenuButton(name="Dropdown", items=menu_items, button_type="primary") elif self.component is pnw.MultiChoice: component = pnw.MultiChoice( name="MultiSelect", value=["Apple", "Pear"], options=["Apple", "Banana", "Pear", "Strawberry"], ) elif self.component is pnw.MultiSelect: component = pnw.MultiSelect( name="MultiSelect", value=["Apple", "Pear"], options=["Apple", "Banana", "Pear", "Strawberry"], size=8, ) elif self.component is pnw.PasswordInput: component = pnw.PasswordInput(name="Password Input", placeholder="Enter a string here...") elif self.component is pnw.Player: component = pnw.Player(name="Player", start=0, end=100, value=32, loop_policy="loop") elif self.component is pnw.Progress: component = pnw.Progress(name="Progress", value=20, width=200) elif self.component is pnw.RadioBoxGroup: component = pnw.RadioBoxGroup( name="RadioBoxGroup", options=["Biology", "Chemistry", "Physics"], inline=True) elif self.component is pnw.RadioButtonGroup: component = pnw.RadioButtonGroup( name="Radio Button Group", options=["Biology", "Chemistry", "Physics"], button_type="success", ) elif self.component is pnw.RangeSlider: component = pnw.RangeSlider( name="Range Slider", start=0, end=math.pi, value=(math.pi / 4.0, math.pi / 2.0), step=0.01, ) elif self.component is pnw.Select: component = pnw.Select(name="Select", options=["Biology", "Chemistry", "Physics"]) elif self.component is pnw.StaticText: component = pnw.StaticText(name="Static Text", value="A string") elif self.component is pnw.TextAreaInput: component = pnw.input.TextAreaInput( name="Text Area Input", placeholder="Enter a string here...") elif self.component is pnw.TextInput: component = pnw.TextInput(name="Text Input", placeholder="Enter a string here...") elif self.component == pnw.Toggle: component = pnw.Toggle(name="Toggle", button_type="success") elif self.component == pnw.VideoStream: component = pnw.VideoStream(name="Video Stream", sizing_mode="stretch_width", height=300) if not component: component = self.component(name="Hello") if not controls: controls = component.controls() controls.margin = 0 self._component_panel[:] = [ pn.pane.Markdown("## " + component.__class__.name + " " + self.component_type), component, pn.layout.Divider(), pn.pane.Markdown("## Parameters"), controls, ]
class ReactiveDashboard(param.Parameterized): title = pn.pane.Markdown("# Booze Cruise YYC") # Add a widget that picks the environment and bucket number_dest = param.Integer(len(DEFAULT_DEST), label="Select number of destinations", bounds=(0, 15)) waypoints_per_batch = param.Integer( 10, label="Waypoints per batch in Google Maps URL", bounds=(1, 12)) progress_bar = pnw.misc.Progress( active=False, bar_color="light", value=None, width_policy="max", sizing_mode="stretch_width", ) date_custom_map: Dict = {} get_best_route_action = pnw.Button(name="Optimize Route", button_type="primary") get_batch_destinations = pnw.Button(name="Import Destinations", button_type="primary") destinations_pane = param.Parameter(default=destinations_pane_default) destinations_wlist = param.List(default=destinations_wlist_default) destinations_latlongs = param.List(default=[(0, 0), (0, 0)], precedence=-0.5) gmaps_urls = param.List(default=['', ''], precedence=-0.5) destinations_addresses = param.List(default=[(0, 0), (0, 0)], precedence=-0.5) all_dates_forecast = default_altair() default_plot = pn.Pane(default_altair()) start_location = param.String(label='Departure Point') end_location = param.String(label='Destination Point') batch_import_str = pnw.TextAreaInput( name='Batch import', placeholder= 'Add locations here by e.g. copy-pasting from a spreadsheet', width=300, height=450, sizing_mode='scale_both') is_start_equal_end = param.Boolean( default=True, label='My final destination same as Departure Point') start_latlong = param.Tuple(default=(0, 0), precedence=-0.5) end_latlong = param.Tuple(default=(0, 0), precedence=-0.5) df_label = param.DataFrame(precedence=-0.5, default=pd.DataFrame()) df_all_pts = param.DataFrame(precedence=-0.5, default=pd.DataFrame()) # Placeholder for tabs: tabs = pn.Tabs(('Batch Location Import', pn.Row())) tmp_buffer = 'Temporary buffer' @param.depends("number_dest", watch=True) def change_destinations_number(self): new_destinations = create_destination_inputs( n=self.number_dest, prev_destinations=self.destinations_wlist) self.destinations_pane, self.destinations_wlist = ( new_destinations[0], new_destinations[1], ) self.tabs.active = 0 return self.destinations_pane def geocode_dest_list_latlong(self, event, destinations_list): self.progress_bar.bar_color = 'info' self.progress_bar.active = True logger_bc.info(event) destinations_str = [_pull_value_wlist(x) for x in destinations_list] logger_bc.info(f"Geocoding the destinations list: {destinations_str}") destinations_jsons = [ _geocode_destination_here(x) for x in destinations_str ] latlongs = [ _pull_lat_long_here(x, n_entry=0) for x in destinations_jsons ] addresses = [ _pull_address_here(x, n_entry=0) for x in destinations_jsons ] logger_bc.info(latlongs) logger_bc.info(addresses) # latlongs = [(random.randint(i, 20), random.randint(i, 40)) for i in range(len(destinations_list))] self.destinations_latlongs = latlongs self.destinations_addresses = addresses logger_bc.info(self.destinations_latlongs) logger_bc.info(self.destinations_addresses) self.progress_bar.bar_color = 'light' self.progress_bar.active = False @param.depends('destinations_latlongs') def show_latlongs(self): destinations_str = [ _pull_value_wlist(x) for x in self.destinations_wlist ] x = f' Length = {len(self.destinations_wlist)}, vals = {destinations_str}' x += f' Latlongs = {len(self.destinations_latlongs)}, vals = {self.destinations_addresses}' res_md = pn.pane.Markdown(x) return res_md def find_best_route(self, event, latlong_list, start_point: Tuple = (0, 0), end_point: Tuple = (0, 0)): ''' Find optimal route using TomTom routing service :param start_point: :param end_point: :param event: :param latlong_list: :return: ''' self.progress_bar.bar_color = 'info' self.progress_bar.active = True latlongs = [start_point] + latlong_list + [end_point] latlong_concat = concat_latlongs(latlongs) url_locations = f'{base_url_tomtom}/{latlong_concat}/json' params = { 'key': API_KEY_TOMTOM, 'travelMode': 'car', 'computeBestOrder': 'true', 'traffic': 'true', 'instructionsType': 'text', 'computeTravelTimeFor': 'all', } response = requests.get(url_locations, params=params) response_json = response.json() latlongs_original_optimal = rearrange_waypoints(response_json) sorted_addresses = self.get_ordered_addresses( latlongs_original_optimal) sorted_addresses_with_terminals = [ self.start_location ] + sorted_addresses + [self.end_location] _, urls = construct_gmaps_urls(sorted_addresses_with_terminals, waypoints_batch_size=10) self.gmaps_urls = urls # Prepare dataframes to feed Bokeh with self.df_label = create_label_df(start_point, end_point, latlongs_original_optimal, sorted_addresses=sorted_addresses, start_location=self.start_location, end_location=self.end_location) self.df_all_pts = create_legs_df(response_json) self.progress_bar.bar_color = 'light' self.progress_bar.active = False @param.depends('df_all_pts') def plot_bokeh(self): if self.df_all_pts.shape[0] > 0: print('Plotting bokeh') p = create_bokeh_figure(df_all_pts=self.df_all_pts, df_label=self.df_label) else: p = figure() return p def get_ordered_addresses(self, ordered_latlongs): """ Sort geocoded addresses into optimal order """ def closest_node(node, nodes): nodes = np.asarray(nodes) deltas = nodes - node dist_2 = np.einsum('ij,ij->i', deltas, deltas) return np.argmin(dist_2) sort_vector = [ closest_node(x, self.destinations_latlongs) for x in ordered_latlongs ] sorted_addresses = [ self.destinations_addresses[x]['label'] for x in sort_vector ] return sorted_addresses @param.depends('gmaps_urls') def show_urls(self): base_url_string = """ ### The route links for navigation in Google Maps: URL """ urls_links_md = [ f'**[Group {i}]({u})**' for i, u in enumerate(self.gmaps_urls) ] url_string = '/n/n'.join(urls_links_md) base_url_string = base_url_string.replace('URL', url_string) res_md = pn.pane.Markdown(base_url_string) print(res_md) return res_md def optimize_route(self, event): print(f'start_loc: {self.start_location}') start_latlong = _pull_lat_long_here( _geocode_destination_here(self.start_location)) if self.is_start_equal_end: end_latlong = start_latlong self.end_latlong = start_latlong self.end_location = self.start_location else: end_latlong = _pull_lat_long_here( _geocode_destination_here(self.end_location)) self.start_latlong = start_latlong self.end_latlong = end_latlong self.geocode_dest_list_latlong( event, destinations_list=self.destinations_wlist) self.find_best_route(event, latlong_list=self.destinations_latlongs, start_point=start_latlong, end_point=end_latlong) def destinations_from_import_str(self, event): self.progress_bar.bar_color = 'info' self.progress_bar.active = True destinations_new = self.batch_import_str.value.split('\n') self.destinations_pane, self.destinations_wlist = create_destination_inputs( n=len(destinations_new), prev_destinations=None, init_vals=destinations_new) self.number_dest = len(destinations_new) self.progress_bar.bar_color = 'light' self.progress_bar.active = False @param.depends('is_start_equal_end') def start_end_widget(self): if self.is_start_equal_end: self.end_location = self.start_location self.end_latlong = self.start_latlong return pn.Column(self.param.start_location, self.param.is_start_equal_end) else: return pn.Column(self.param.start_location, self.param.is_start_equal_end, self.param.end_location) def panel(self): # Attach a callback to geocoding & optimal route search self.get_best_route_action.on_click(lambda x: self.optimize_route(x)) # Attach a callback to batch import: self.batch_import_str.link(self.batch_import_str, callbacks={'value': clean_space_callback}) self.batch_import_str.value = '' # Attach a callback to Import Destinations button so the destinations pasted propagate into the Destinations list & sidebar self.get_batch_destinations.on_click( lambda x: self.destinations_from_import_str(x)) # Setup the sidebar: widgets_sliders = pn.Column(self.param.number_dest, self.param.waypoints_per_batch) widgets_start_end = self.start_end_widget buttons_ = pn.Column(self.get_best_route_action) progress_bar = pn.Pane(self.progress_bar, sizing_mode="stretch_width", width_policy="max") # Set up tabs tab_bokeh = pn.Column(pn.Column(self.plot_bokeh), self.show_urls, sizing_mode="stretch_width", width_policy="max") tab_import = pn.Row(self.batch_import_str, self.get_batch_destinations) self.tabs = pn.Tabs(('Optimal Route Map', tab_bokeh), ('Batch Location Import', tab_import)) result = pn.Row( pn.Column( self.title, widgets_sliders, progress_bar, widgets_start_end, buttons_, self.change_destinations_number, ), self.tabs, sizing_mode="stretch_width", ) return result
class FlowPanel(AbipyParameterized): """ """ verbose = pn.widgets.IntSlider(start=0, end=10, step=1, value=0) engine = pn.widgets.Select(value="fdp", options=[ 'dot', 'neato', 'twopi', 'circo', 'fdp', 'sfdp', 'patchwork', 'osage' ]) dirtree = pn.widgets.Checkbox(name='Dirtree', value=False) graphviz_btn = pn.widgets.Button(name="Show graph", button_type='primary') status_btn = pn.widgets.Button(name="Show status", button_type='primary') history_btn = pn.widgets.Button(name="Show history", button_type='primary') debug_btn = pn.widgets.Button(name="Debug", button_type='primary') events_btn = pn.widgets.Button(name="Events", button_type='primary') corrections_btn = pn.widgets.Button(name="Corrections", button_type='primary') handlers_btn = pn.widgets.Button(name="Handlers", button_type='primary') vars_text = pn.widgets.TextInput( name='Abivars', placeholder='Enter list of variables separated by comma') vars_btn = pn.widgets.Button(name="Show Variables", button_type='primary') dims_btn = pn.widgets.Button(name="Show Dimensions", button_type='primary') structures_btn = pn.widgets.Button(name="Show Structures", button_type='primary') structures_io_checkbox = pn.widgets.CheckBoxGroup( name='Input/Output Structure', value=['output'], options=['input', 'output'], inline=True) # Widgets to plot ebands. ebands_btn = pn.widgets.Button(name="Show Ebands", button_type='primary') ebands_plotter_mode = pnw.Select( name="Plot Mode", value="gridplot", options=["gridplot", "combiplot", "boxplot", "combiboxplot"]) # "animate", ebands_plotter_btn = pnw.Button(name="Plot", button_type='primary') ebands_df_checkbox = pnw.Checkbox(name='With Ebands DataFrame', value=False) ebands_ksamp_checkbox = pn.widgets.CheckBoxGroup( name='Input/Output Structure', value=["with_path", "with_ibz"], options=['with_path', 'with_ibz'], inline=True) #TODO: Implement widget for selected_nids(flow, options), #radio_group = pn.widgets.RadioButtonGroup( # name='Radio Button Group', options=['Biology', 'Chemistry', 'Physics'], button_type='success') def __init__(self, flow, **params): super().__init__(**params) self.flow = flow @param.depends('status_btn.clicks') def on_status_btn(self): if self.status_btn.clicks == 0: return stream = StringIO() self.flow.show_status(stream=stream, verbose=self.verbose.value) return pn.Row(bkw.PreText(text=stream.getvalue())) @param.depends('history_btn.clicks') def on_history_btn(self): if self.history_btn.clicks == 0: return stream = StringIO() #flow.show_history(status=options.task_status, nids=selected_nids(flow, options), # full_history=options.full_history, metadata=options.metadata) self.flow.show_history(stream=stream) return pn.Row(bkw.PreText(text=stream.getvalue())) @param.depends('graphviz_btn.clicks') def on_graphviz_btn(self): """ """ if self.graphviz_btn.clicks == 0: return node = self.flow if self.dirtree.value: graph = node.get_graphviz_dirtree(engine=self.engine.value) else: graph = node.get_graphviz(engine=self.engine.value) return pn.Column(graph) @param.depends('debug_btn.clicks') def on_debug_btn(self): if self.debug_btn.clicks == 0: return #TODO https://github.com/ralphbean/ansi2html ? stream = StringIO() #flow.debug(status=options.task_status, nids=selected_nids(flow, options)) self.flow.debug(stream=stream) return pn.Row(bkw.PreText(text=stream.getvalue())) @param.depends('events_btn.clicks') def on_events_btn(self): if self.events_btn.clicks == 0: return stream = StringIO() self.flow.show_events(stream=stream) #flow.show_events(status=options.task_status, nids=selected_nids(flow, options)) return pn.Row(bkw.PreText(text=stream.getvalue())) @param.depends('corrections_btn.clicks') def on_corrections_btn(self): if self.corrections_btn.clicks == 0: return stream = StringIO() self.flow.show_corrections(stream=stream) #flow.show_corrections(status=options.task_status, nids=selected_nids(flow, options)) return pn.Row(bkw.PreText(text=stream.getvalue())) @param.depends('handlers_btn.clicks') def on_handlers_btn(self): #if self.handlers_btn.clicks == 0: return stream = StringIO() #if options.doc: # flowtk.autodoc_event_handlers() #else: #show_events(self, status=None, nids=None, stream=sys.stdout): self.flow.show_event_handlers(verbose=self.verbose.value, stream=stream) return pn.Row(bkw.PreText(text=stream.getvalue())) @param.depends('vars_btn.clicks') def on_vars_btn(self): if self.vars_btn.clicks == 0: return if not self.vars_text.value: return varnames = [s.strip() for s in self.vars_text.value.split(",")] df = self.flow.compare_abivars( varnames=varnames, # nids=selected_nids(flow, options), printout=False, with_colors=False) return pn.Row(self._df(df)) @param.depends('dims_btn.clicks') def on_dims_btn(self): if self.dims_btn.clicks == 0: return df = self.flow.get_dims_dataframe( # nids=selected_nids(flow, options), printout=False, with_colors=False) return pn.Row(self._df(df), sizing_mode="scale_width") @param.depends('structures_btn.clicks') def on_structures_btn(self): if self.structures_btn.clicks == 0: return what = "" if "input" in self.structures_io_checkbox.value: what += "i" if "output" in self.structures_io_checkbox.value: what += "o" dfs = self.flow.compare_structures( nids=None, # select_nids(flow, options), what=what, verbose=self.verbose.value, with_spglib=False, printout=False, with_colors=False) return pn.Row(self._df(dfs.lattice), sizing_mode="scale_width") @param.depends('ebands_plotter_btn.clicks') def on_ebands_btn(self): if self.ebands_plotter_btn.clicks == 0: return df, ebands_plotter = self.flow.compare_ebands( nids=None, # select_nids(flow, options), with_path="with_path" in self.ebands_ksamp_checkbox.value, with_ibz="with_ibz" in self.ebands_ksamp_checkbox.value, verbose=self.verbose.value, with_spglib=False) if ebands_plotter is None: return plot_mode = self.ebands_plotter_mode.value plotfunc = getattr(ebands_plotter, plot_mode, None) if plotfunc is None: raise ValueError("Don't know how to handle plot_mode: %s" % plot_mode) fig = plotfunc(**self.fig_kwargs) col = pn.Column(self._mp(fig)) if self.ebands_df_checkbox.value: col.append(self._df(df)) return pn.Row(col) #, sizing_mode='scale_width') def get_panel(self): """Return tabs with widgets to interact with the flow.""" tabs = pn.Tabs() app = tabs.append #row = pn.Row(bkw.PreText(text=self.ddb.to_string(verbose=self.verbose.value), sizing_mode="scale_both")) app(("Status", pn.Row(self.status_btn, self.on_status_btn))) app(("History", pn.Row(self.history_btn, self.on_history_btn))) app(("Events", pn.Row(self.events_btn, self.on_events_btn))) app(("Corrections", pn.Row(self.corrections_btn, self.on_corrections_btn))) app(("Handlers", pn.Row(self.handlers_btn, self.on_handlers_btn))) app(("Structures", pn.Row( pn.Column(self.structures_io_checkbox, self.structures_btn), self.on_structures_btn))) ws = pn.Column(self.ebands_plotter_mode, self.ebands_ksamp_checkbox, self.ebands_df_checkbox, self.ebands_plotter_btn) app(("Ebands", pn.Row(ws, self.on_ebands_btn))) app(("Abivars", pn.Row(pn.Column(self.vars_text, self.vars_btn), self.on_vars_btn))) app(("Dims", pn.Row(pn.Column(self.dims_btn), self.on_dims_btn))) app(("Debug", pn.Row(self.debug_btn, self.on_debug_btn))) app(("Graphviz", pn.Row(pn.Column(self.engine, self.dirtree, self.graphviz_btn), self.on_graphviz_btn))) return tabs
class DataBaseManager(param.Parameterized): file_input = widgets.FileInput(name="Parquet file") dbtype = widgets.TextInput(name="Database type", value="postgresql", width=WIDGET_WIDTH) user = widgets.TextInput(name="Username", value="superset", width=WIDGET_WIDTH) password = widgets.TextInput(name="Password", value="superset", width=WIDGET_WIDTH) host = widgets.TextInput(name="Host", value="localhost", width=WIDGET_WIDTH) endpoint = widgets.TextInput(name="Host", value="superset", width=WIDGET_WIDTH) port = widgets.LiteralInput(name="Port", value=5432, width=WIDGET_WIDTH) data_source = widgets.TextInput(name="Table Name", width=WIDGET_WIDTH) connect_btn = widgets.Button(name="Connect to DB", width=WIDGET_WIDTH) datasource_btn = widgets.Button(name="Query table", width=WIDGET_WIDTH) def __init__(self): super().__init__() self.df = None self.datasource = None self.engine = None self._connected = False @property def connected(self) -> bool: return self._connected @param.depends("dbtype.value", "user.value", "password.value", "host.value", "port.value", "endpoint.value") def _url(self): return panel.pane.Str(self._get_url()) def _get_url(self): url = "{}://{}:{}@{}:{}/{}".format( self.dbtype.value, self.user.value, self.password.value, self.host.value, self.port.value, self.endpoint.value, ) return url def get_table_names(self): if self.engine is None: return names = pd.read_sql("SELECT tablename FROM pg_catalog.pg_tables;", self.engine) return names.values.flatten().tolist() def get_column_names(self, table): sql = "SELECT column_name FROM information_schema.COLUMNS WHERE TABLE_NAME = '%s';" % table return pd.read_sql(sql, self.engine).values.flatten().tolist() @property def url(self): return self._get_url() @param.depends("connect_btn.clicks", watch=True) def connect_to_db(self): msg = panel.Column(panel.pane.Str("Connected to"), self._url) if self.connect_btn.clicks == 0: return panel.Column(panel.pane.Str("Ready to connect to"), self._url) if self._connected: return msg try: engine = create_engine(self.url) self.engine = engine self._connected = True return msg except Exception as e: self._connected = False return panel.Column(panel.pane.Str("Error connecting to"), self._url) def panel(self): db_widgets = panel.Row( panel.Column(self.dbtype, self.host, self.port), panel.Column(self.user, self.password, self.endpoint), panel.Column(self.connect_to_db, self.connect_btn), ) return panel.Column( panel.pane.Markdown("## Connect to superset database"), db_widgets)
his dataset contains 130,000 reviews of different type of wines, published by the <a href="https://www.winemag.com/">Wine Enthusiast Magazine</a>. It includes the wine name, country, variety, price, amongst others descriptors for each reviewed wine.</p> <p>The original data was transformed to simplify the analysis: to avoid confusion points was renamed rating, added new columns for vintage, price range, rating range,grape variety and description lengh other than those the only columns left are country, description, rating(points), price, and taster_name. A summary of the final data set can be seen by clicking the blue button.</p> """, width=1200) top = pn.Row(logo, pn.Column(title, desc), css_classes=['panel-widget-box'], width=1440, margin=(20, 0, 0, 0)) rb = pnw.Button(name='Click For Random Sample', button_type='primary', css_classes=['panel_button']) def rand_df(): df = wine.sample(10).hvplot.table(width=500, height=100) return df ppp = pn.pane.HoloViews() def b(event): ppp.object = rand_df()
def genome_features_viewer(gff_file, ref_file=None, plot_width=900): """Gene feature viewer app""" if gff_file is None: return features = utils.gff_to_features(gff_file) df = utils.features_to_dataframe(features) loc_pane = pnw.TextInput(name='location',value='',width=150) search_pane = pnw.TextInput(name='find_gene',value='',width=220) slider = pnw.IntSlider(name='start',start=0,end=10000,step=500,value=1,width=plot_width) xzoom_slider = pnw.IntSlider(name='zoom',start=1,end=500,value=100,step=5,width=100) chrom_select = pnw.Select(name='chrom',width=220) left_button = pnw.Button(name='<',width=40) right_button = pnw.Button(name='>',width=40) feature_pane = pn.pane.Bokeh(height=100,margin=10) seq_pane = pn.pane.Bokeh(height=50,margin=10) debug_pane = pn.pane.Str('debug',width=200,style={'background':'yellow','margin': '4pt'}) if ref_file is not None: seqlen = utils.get_fasta_length(ref_file) slider.end = seqlen else: slider.end = int(df.end.max()) def search_features(event): """Find a feature""" term = search_pane.value feats = utils.gff_to_features(gff_file) df = utils.features_to_dataframe(feats) df['gene'] = df.gene.fillna('') f = df[df.gene.str.contains(term)].iloc[0] #debug_pane.object = str(f.start) slider.value = int(f.start)-100 update(event) return def pan(event): p = feature_pane.object rng = p.x_range.end-p.x_range.start inc = int(rng/10) print (event.obj.name) if event.obj.name == '<': slider.value = int(slider.value) - inc else: slider.value = int(slider.value) + inc update(event) return def update(event): print (event.obj.name) if event.obj.name in ['start', 'zoom']: xzoom = xzoom_slider.value*200 start = int(slider.value) N = xzoom/2 end = int(start+N) loc_pane.value = str(start)+':'+str(end) elif event.obj.name == 'location': vals = loc_pane.value.split(':') start = int(vals[0]) end = int(vals[1]) slider.value = start #debug_pane.object=type(start) p = feature_pane.object p.x_range.start = start p.x_range.end = end if ref_file: sequence = utils.get_fasta_sequence(ref_file, start, end) seq_pane.object = plotters.plot_sequence(sequence, plot_width, plot_height=50,fontsize='9pt',xaxis=False) return slider.param.watch(update,'value',onlychanged=True) #slider.param.trigger('value') xzoom_slider.param.watch(update,'value') search_pane.param.watch(search_features,'value') loc_pane.param.watch(update,'value',onlychanged=True) left_button.param.watch(pan,'clicks') right_button.param.watch(pan,'clicks') #debug_pane.object = utils.get_fasta_names(ref_file)[0] if ref_file != None: chrom_select.options = utils.get_fasta_names(ref_file) #plot p = feature_pane.object = plotters.plot_features(features, 0, 10000, plot_width=plot_width, tools="", rows=4) #side = pn.Column(file_input,css_classes=['form'],width=200,margin=20) top = pn.Row(loc_pane,xzoom_slider,search_pane,chrom_select,left_button,right_button) main = pn.Column(feature_pane, seq_pane, sizing_mode='stretch_width') app = pn.Column(top,slider,main,debug_pane, sizing_mode='stretch_width',width_policy='max',margin=20) return app
class CommitTimeSeriesPreprocessor(param.Parameterized): EMBEDDINGS_TABLE = "developer_embeddings" COMMITS_TABLE = "commit_series" GROUPED_COMMITS_TABLE = "commits_per_repo" GROUPED_EMBEDDINGS_TABLE = "clusters_per_repo" embeddings_file = widgets.FileInput(name="Embeddings") commits_file = widgets.FileInput(name="Commits") grouped_file = widgets.FileInput(name="Grouped series") load_parquet_btn = widgets.Button(name="Load files", width=WIDGET_WIDTH) to_sql_btn = widgets.Button(name="Write to DB.", width=WIDGET_WIDTH) def __init__(self, engine): super(CommitTimeSeriesPreprocessor, self).__init__() self.embeddings = None self.grouped = None self.commits = None self.engine = engine self._data_loaded = False @staticmethod def extract_commits_per_repo(df): """ Extract a DataFrame with columns ['date_ix', 'commits', 'name', 'repository'] \ from grouped commit series data. """ def get_one_commit_series(x): data = pandas.DataFrame( index=df.loc[0, Columns.Value], data=numpy.array(x[Columns.Value].todense()).flatten(), columns=[Columns.Value], ) data.index.name = Columns.Date data = data.reset_index() data = data[data[Columns.Value] > 0] data[Columns.SeriesId] = x[Columns.SeriesId] data[Columns.Repository] = x[Columns.Repository] return data commit_series_per_repo = df.iloc[1:].apply(get_one_commit_series, axis=1) return pandas.concat(commit_series_per_repo.values) @staticmethod def extract_groups(df: pandas.DataFrame): data = df.loc[1:, [ Columns.SeriesId, Columns.Repository, Seriator.CLUSTER, Seriator.SERIATED_IDX, "x_0", "x_1", ], ] return data @staticmethod def read_parquet(file_widget): f = io.BytesIO() f.write(file_widget.value) f.seek(0) return pd.read_parquet(f) def load_grouped_series(self, file_widget) -> pandas.DataFrame: """Load and deserialize the target parquet file containing encoded commit time series.""" if file_widget.value is None: return None df = self.read_parquet(file_widget) start_date, end_date = df.loc[ 0, [Columns.SeriesId, Columns.Repository]].values.tolist() df.loc[:, Columns.Value] = df.loc[:, Columns.Value].astype(object) times = pandas.date_range(start_date, end_date, freq="D").values matrices = df.loc[1:, Columns.Value].map( deserialize_sparse_matrix).values.tolist() df.loc[:, Columns.Value] = [times] + matrices return df def read_parquet_files(self): grouped = self.load_grouped_series(self.grouped_file) embeddings = (self.read_parquet(self.embeddings_file) if self.embeddings_file.value is not None else None) commits = (self.read_parquet(self.commits_file) if self.commits_file.value is not None else None) return commits, embeddings, grouped @param.depends("load_parquet_btn.clicks", watch=True) def load_data(self): if self.load_parquet_btn.clicks == 0: return panel.pane.HTML("") dataframes = self.read_parquet_files() self.commits, self.embeddings, self.grouped = dataframes self._data_loaded = True return panel.pane.HTML(self.grouped.head().to_html() if self. grouped is not None else "No data loaded") def preprocess_data(self): grouped_commits = self.extract_commits_per_repo(self.grouped) grouped_embeddings = self.extract_groups(self.grouped) commits_with_teams = self.commits.merge(self.embeddings, left_on="name", right_index=True, how="inner") commits_with_teams = commits_with_teams.drop(["x_0", "x_1", "type_x"], axis=1) commits_with_teams = commits_with_teams.rename( columns={"type_y": "type"}) return commits_with_teams, self.embeddings, grouped_commits, grouped_embeddings def to_sql(self, *args, **kwargs): commits, embeddings, grouped_commits, grouped_embeddings = self.preprocess_data( ) grouped_commits.to_sql(self.GROUPED_COMMITS_TABLE, self.engine, *args, **kwargs) grouped_embeddings.to_sql(self.GROUPED_EMBEDDINGS_TABLE, self.engine, *args, **kwargs) commits.to_sql(self.COMMITS_TABLE, self.engine, *args, **kwargs) embeddings.to_sql(self.EMBEDDINGS_TABLE, self.engine, *args, **kwargs) @param.depends("to_sql_btn.clicks", watch=True) def to_parquet(self): if self._data_loaded: mkdwn = "Created the following tables: \n" try: commits, embeddings, grouped_commits, grouped_embeddings = self.preprocess_data( ) grouped_commits.to_parquet(self.GROUPED_COMMITS_TABLE + ".parquet") mkdwn += "- {}\n".format(self.GROUPED_COMMITS_TABLE) grouped_embeddings.to_parquet(self.GROUPED_EMBEDDINGS_TABLE + ".parquet") mkdwn += "- {}\n".format(self.GROUPED_EMBEDDINGS_TABLE) commits.to_parquet(self.COMMITS_TABLE + ".parquet") mkdwn += "- {}\n".format(self.COMMITS_TABLE) embeddings.to_parquet(self.EMBEDDINGS_TABLE + ".parquet") mkdwn += "- {}\n".format(self.EMBEDDINGS_TABLE) except Exception as e: raise e pass return panel.pane.Str(mkdwn) else: return panel.pane.Str("") def panel(self): return panel.Column( panel.pane.Markdown("###Load eee-cts data into superset"), panel.Row(panel.pane.Str("Embeddings "), self.embeddings_file), panel.Row(panel.pane.Str("Grouped Series"), self.grouped_file), panel.Row(panel.pane.Str("Commit Series "), self.commits_file), panel.Row(self.load_data), panel.Row(self.to_parquet), panel.Row(self.load_parquet_btn, self.to_sql_btn), )
def gallery( records, class_map: Optional[ClassMap] = None, display_label: bool = True, display_bbox: bool = True, display_mask: bool = True, display_keypoints: bool = True, ): """Shows a gallery for a list of records.""" # gui btn_prev = pnw.Button(name="<") btn_next = pnw.Button(name=">") current = pnw.TextInput(value="1") overall = pn.Row("/" + str(len(records))) gui = pn.Row(btn_prev, current, overall, btn_next, align="center") # plotting function @pn.depends(current.param.value) def _plot(_): nonlocal current try: index = int(current.value) - 1 except: pass img = draw_record_with_bokeh( record=records[index], class_map=class_map, display_label=display_label, display_bbox=display_bbox, display_mask=display_mask, display_keypoints=display_keypoints, return_figure=True, ) return img # add interactions def _next(_): nonlocal current nonlocal records try: index = int(current.value) if index == len(records): index = 1 else: index += 1 current.value = str(index) except: pass def _previous(_): nonlocal current nonlocal records try: index = int(current.value) if index == 1: index = len(records) else: index -= 1 current.value = str(index) except: pass btn_prev.on_click(_previous) btn_next.on_click(_next) return pn.Column(gui, pn.Row(_plot, align="center"))
def bam_viewer(bam_file, ref_file, gff_file=None, width=1000, height=200, color='gray'): """Bam viewer widget. Args: bam_file: sorted bam file ref_file: reference sequence in fasta format gff_file: optional genomic features file """ slider = pnw.IntSlider(name='location',start=1,end=10000,value=500,step=500,width=300) main_pane = pn.pane.Bokeh(height=100) cov_pane = pn.pane.Bokeh(height=60) loc_pane = pn.pane.Str(50,width=250,style={'margin': '4pt'}) feat_pane = pn.pane.Bokeh(height=60) ref_pane = pn.pane.Bokeh(height=60) xzoom_slider = pnw.IntSlider(name='x zoom',start=50,end=8000,value=1000,step=10,width=100) yzoom_slider = pnw.IntSlider(name='y zoom',start=10,end=100,value=20,step=5,width=100)#,orientation='vertical') panleft_btn = pnw.Button(name='<',width=50,button_type='primary') panright_btn = pnw.Button(name='>',width=50,button_type='primary') chroms_select = pnw.Select(name='Chromosome', options=[], width=250) colorby = pnw.Select(name='Color by', options=['quality','pair orientation','read strand'], width=180) search_pane = pnw.TextInput(name='search',width=200) trans_option = pnw.Checkbox(name='show translation') debug_pane = pn.pane.Markdown() def pan_right(event): plot = main_pane.object plot.x_range start = slider.value loc = slider.value+100 slider.value=loc update(event) return def pan_left(event): loc = slider.value-100 if loc<1: return slider.value=loc update(event) return def update_features(): """Load features""" if gff_file is None: return ext = os.path.splitext(gff_file)[1] if ext in ['.gff','.gff3']: feats = utils.gff_to_features(gff_file) elif ext in ['.gb','.gbff']: feats = utils.genbank_to_features(gff_file) p = feat_pane.object = plotters.plot_features(feats, plot_width=width, plot_height=100) return p def search_features(event): """Find a feature""" term = search_pane.value feats = utils.gff_to_features(gff_file) df = utils.features_to_dataframe(feats) df['gene'] = df.gene.fillna('') f = df[df.gene.str.contains(term)].iloc[0] debug_pane.object = str(f.start) slider.value = int(f.start) update(event) return def update_ref(filename, start, end): """Update reference sequence""" if filename == None: return seqlen = utils.get_fasta_length(filename) slider.end = seqlen refseq = Fasta(filename) chroms = list(refseq.keys()) chroms_select.options = chroms key = chroms[0] seq = refseq[key][int(start):int(end)].seq ref_pane.object = plotters.plot_sequence(seq, plot_height=50,fontsize='9pt',xaxis=False) return def update(event): """Update viewers on widget change""" xzoom = xzoom_slider.value yzoom = yzoom_slider.value start = slider.value N = xzoom/2 end = start+N chrom = utils.get_chrom(bam_file) loc_pane.object = '%s:%s-%s' %(chrom,start,int(end)) cov = utils.get_coverage(bam_file,chrom,start,end) cov_pane.object = plotters.plot_coverage(cov,plot_width=width) main_pane.object = plotters.plot_bam_alignment(bam_file,chrom,start,end,height=yzoom,plot_width=width,plot_height=height) update_ref(ref_file, start, end) if feature_plot: feature_plot.x_range.start = start feature_plot.x_range.end = end debug_pane.object = '' return slider.param.watch(update, 'value') xzoom_slider.param.watch(update, 'value') yzoom_slider.param.watch(update, 'value') panright_btn.param.watch(pan_right, 'clicks') panleft_btn.param.watch(pan_left, 'clicks') search_pane.param.watch(search_features, 'value') feature_plot = update_features() #initialise slider slider.param.trigger('value') #menus = pn.Row(bam_input, ref_input, gff_input) top = pn.Row(slider,xzoom_slider,yzoom_slider,panleft_btn,panright_btn,loc_pane) bottom = pn.Row(chroms_select, search_pane,colorby,trans_option) app = pn.Column(top,cov_pane,main_pane,ref_pane,feat_pane,bottom,debug_pane) return app