def get_parameter_widget(obj, **kwargs): """Creates interactive notebook widgets for the parameter, if available. """ if obj._number_of_elements == 1: return _get_value_widget(obj) else: wdict = {} par_widgets = [] for i in range(obj._number_of_elements): thiswd = _get_value_widget(obj=obj, index=i) par_widgets.append(thiswd["widget"]) wdict["element{}".format(i)] = thiswd["wdict"] update = Button( description="Update", tooltip="Unlike most other widgets, the multivalue parameter " "widgets do not update automatically when the value of the " "changes by other means. Use this button to update the values" "manually") def on_update_clicked(b): for value, container in zip(obj.value, par_widgets): minwidget = container.children[0] vwidget = container.children[1] maxwidget = container.children[2] if value < vwidget.min: minwidget.value = value elif value > vwidget.max: maxwidget.value = value vwidget.value = value update.on_click(on_update_clicked) wdict["update_button"] = update container = Accordion([VBox([update] + par_widgets)]) set_title_container(container, [obj.name]) return { "widget": container, "wdict": wdict, }
def create_object_output(object): ''' Create a output Widget for Images, Geometries and Features ''' ty = object.__class__.__name__ if ty == 'Image': info = object.getInfo() image_id = info['id'] if 'id' in info else 'No Image ID' prop = info['properties'] bands = info['bands'] bands_names = [band['id'] for band in bands] bands_types = [band['data_type']['precision'] for band in bands] bands_crs = [band['crs'] for band in bands] new_band_names = [ '<li>{} - {} - {}</li>'.format(name, ty, epsg) for name, ty, epsg in zip(bands_names, bands_types, bands_crs) ] new_properties = [ '<li><b>{}</b>: {}</li>'.format(key, val) for key, val in prop.items() ] header = HTML('<b>Image id:</b> {id} </br>'.format(id=image_id)) bands_wid = HTML('<ul>' + ''.join(new_band_names) + '</ul>') prop_wid = HTML('<ul>' + ''.join(new_properties) + '</ul>') acc = Accordion([bands_wid, prop_wid]) acc.set_title(0, 'Bands') acc.set_title(1, 'Properties') acc.selected_index = None # this will unselect all return VBox([header, acc]) elif ty == 'FeatureCollection': try: info = object.getInfo() except: print('FeatureCollection limited to 4000 features') info = object.limit(4000) return create_accordion(info) else: info = object.getInfo() return create_accordion(info)
def box(self): layout = Accordion(children=[ VBox([ self.ui_th_std0, self.ui_qv0, self.ui_p0, self.ui_kappa, self.ui_amplitude ]), VBox([ *self.ui_processes # , self.ui_ept # TODO ]), VBox([ self.ui_nx, self.ui_nz, self.ui_sdpg, self.ui_dt, self.ui_n_steps, self.ui_condensation_rtol_x, self.ui_condensation_rtol_thd, self.ui_adaptive, self.ui_condensation_coord, *self.ui_mpdata_options ]), # VBox([]) # TODO ]) layout.set_title(0, 'environment parameters') layout.set_title(1, 'processes') layout.set_title(2, 'discretisation') # layout.set_title(3, 'parallelisation') # TODO return layout
def update_ipywidget(self): w = [] root_accordion = self._ipywidget.accordion toolbar = self._ipywidget.toolbar for n in self._process_tree.walk(): address = n.address if len(address) > 1: parent_accordion = self.accordion_by_id(address[:-1]) else: parent_accordion = root_accordion child = list(parent_accordion.children) if len(child) <= address[-1] or child[ address[-1]].children[2].source is not n.data: node_w = n.data.log_ipywidget(toolbar=False) acc = Accordion( layout=Layout(width='100%', max_height='500px')) #acc.selected_index = -1 new_item = VBox([acc, VSpace(), node_w], layout=Layout(overflow_y='auto')) if len(child) <= address[-1]: child.append(new_item) else: child[address[-1]] = new_item name = n.data.name if isinstance(n.data, Process) and n.data.state in ('success', 'failed'): name += ' (ended in %s)' % second_to_str( n.data.elapsed_time()) parent_accordion.set_title(address[-1], name) parent_accordion.children = tuple(child) else: node_w = parent_accordion.children[address[-1]].children[2] w.append(node_w) toolbar.logviews = w
def fit_component_ipy(obj, **kwargs): wdict = {} only_current = Checkbox() wdict["only_current"] = only_current help = HTML( "Click on the signal figure and drag to the right to select a" "range. Press `Fit` to fit the component in that range. If only " "current is unchecked the fit is performed in the whole dataset.", layout=ipywidgets.Layout(width="auto")) wdict["help"] = only_current help = Accordion(children=[help]) help.set_title(0, "Help") link((obj, "only_current"), (only_current, "value")) fit = Button(description="Fit", tooltip="Fit in the selected signal range") close = Button( description="Close", tooltip="Close widget and remove span selector from the signal figure." ) wdict["close_button"] = close wdict["fit_button"] = fit def on_fit_clicked(b): obj._fit_fired() fit.on_click(on_fit_clicked) box = VBox( [labelme("Only current", only_current), help, HBox((fit, close))]) def on_close_clicked(b): obj.span_selector_switch(False) box.close() close.on_click(on_close_clicked) return { "widget": box, "wdict": wdict, }
def _build_highlights_accordion(self) -> DOMWidget: """ Creates a widget that displays highlights for this `Tier` in an `ipywidgets.Accordion` - which is nice! """ highlights = self.tier.get_highlights() if not highlights: return None widget = Accordion() for i, (name, highlight) in enumerate(highlights.items()): out = Output() widget.children = (*widget.children, out) with out: for item in highlight: publish_display_data(**item) widget.set_title(i, name) widget.selected_index = None return widget
def create_accordion(dictionary): """ Create an Accordion output from a dict object """ widlist = [] ini = 0 widget = Accordion() widget.selected_index = None # this will unselect all for key, val in dictionary.items(): if isinstance(val, dict): newwidget = create_accordion(val) widlist.append(newwidget) elif isinstance(val, list): # tranform list to a dictionary dictval = {k: v for k, v in enumerate(val)} newwidget = create_accordion(dictval) widlist.append(newwidget) else: value = HTML(str(val)) widlist.append(value) widget.set_title(ini, key) ini += 1 widget.children = widlist return widget
def show_items_feature_form(table, periods): tree = defaultdict(dict) for period in periods: section = period.section tree[section.group][section.name] = period.features def update(button, table, feature): clear_output() plot_items_feature(table, feature) group_pages = [] group_titles = [] for group_title, group in tree.iteritems(): section_pages = [] section_titles = [] for section_title, features in group.iteritems(): buttons = [] for feature in features: button = Button(description=feature.name) button.on_click(partial( update, table=table, feature=feature) ) buttons.append(button) section_pages.append(VBox(children=buttons)) section_titles.append(section_title) accordion = Accordion(children=section_pages) for index, title in enumerate(section_titles): accordion.set_title(index, title) group_pages.append(accordion) group_titles.append(group_title) accordion = Accordion(children=group_pages) for index, title in enumerate(group_titles): accordion.set_title(index, title) return accordion
def create_object_output(object): ''' Create a output Widget for Images, Geometries and Features ''' ty = object.__class__.__name__ if ty == 'Image': info = object.getInfo() image_id = info['id'] if 'id' in info else 'No Image ID' prop = info.get('properties') bands = info.get('bands') bands_names = [band.get('id') for band in bands] # BAND PRECISION bands_precision = [] for band in bands: data = band.get('data_type') if data: precision = data.get('precision') bands_precision.append(precision) # BAND CRS bands_crs = [] for band in bands: crs = band.get('crs') bands_crs.append(crs) # BAND MIN AND MAX bands_min = [] for band in bands: data = band.get('data_type') if data: bmin = data.get('min') bands_min.append(bmin) bands_max = [] for band in bands: data = band.get('data_type') if data: bmax = data.get('max') bands_max.append(bmax) # BANDS new_band_names = [] zipped_data = zip(bands_names, bands_precision, bands_min, bands_max, bands_crs) for name, ty, mn, mx, epsg in zipped_data: value = '<li><b>{}</b> ({}) {} to {} - {}</li>'.format( name, ty, mn, mx, epsg) new_band_names.append(value) bands_wid = HTML('<ul>' + ''.join(new_band_names) + '</ul>') # PROPERTIES if prop: new_properties = [] for key, val in prop.items(): value = '<li><b>{}</b>: {}</li>'.format(key, val) new_properties.append(value) prop_wid = HTML('<ul>' + ''.join(new_properties) + '</ul>') else: prop_wid = HTML('Image has no properties') # ID header = HTML('<b>Image id:</b> {id} </br>'.format(id=image_id)) acc = Accordion([bands_wid, prop_wid]) acc.set_title(0, 'Bands') acc.set_title(1, 'Properties') acc.selected_index = None # thisp will unselect all return VBox([header, acc]) elif ty == 'FeatureCollection': try: info = object.getInfo() except: print('FeatureCollection limited to 4000 features') info = object.limit(4000) return create_accordion(info) else: info = object.getInfo() return create_accordion(info)
def _widget(self): """ Create IPython widget for display within a notebook """ try: return self._cached_widget except AttributeError: pass from ipywidgets import Layout, VBox, HBox, IntText, Button, HTML, Accordion layout = Layout(width='150px') if 'bokeh' in self.scheduler.services: link = self.dashboard_link link = '<p><b>Dashboard: </b><a href="%s" target="_blank">%s</a></p>\n' % (link, link) else: link = '' title = '<h2>%s</h2>' % type(self).__name__ title = HTML(title) dashboard = HTML(link) status = HTML(self._widget_status(), layout=Layout(min_width='150px')) request = IntText(0, description='Workers', layout=layout) scale = Button(description='Scale', layout=layout) minimum = IntText(0, description='Minimum', layout=layout) maximum = IntText(0, description='Maximum', layout=layout) adapt = Button(description='Adapt', layout=layout) accordion = Accordion([HBox([request, scale]), HBox([minimum, maximum, adapt])], layout=Layout(min_width='500px')) accordion.selected_index = None accordion.set_title(0, 'Manual Scaling') accordion.set_title(1, 'Adaptive Scaling') box = VBox([title, HBox([status, accordion]), dashboard]) self._cached_widget = box def adapt_cb(b): self.adapt(minimum=minimum.value, maximum=maximum.value) adapt.on_click(adapt_cb) def scale_cb(b): with log_errors(): n = request.value with ignoring(AttributeError): self._adaptive.stop() self.scale(n) scale.on_click(scale_cb) scheduler_ref = ref(self.scheduler) def update(): status.value = self._widget_status() pc = PeriodicCallback(update, 500, io_loop=self.scheduler.loop) self.scheduler.periodic_callbacks['cluster-repr'] = pc pc.start() return box
class __Databases(object): """Database browser implementation Args: spark (SparkSession): Spark Session object """ def __init__(self, spark): self.spark = spark def create(self): """Create the sidecar view""" self.sc = Sidecar( title="Databases-%s" % os.environ["DBJL_CLUSTER"].split("-")[-1], layout=Layout(width="300px"), ) self.refresh = Button(description="refresh") self.refresh.on_click(self.on_refresh) self.output = Output( layout=Layout( height="600px", width="320px", overflow_x="scroll", overflow_y="scroll" ) ) self.output.add_class("db-detail") self.selects = [] self.accordion = Accordion(children=[]) with self.sc: display(VBox([self.refresh, self.accordion, self.output])) self.update() self.set_css() def on_refresh(self, b): """Refresh handler Args: b (ipywidgets.Button): clicked button """ self.selects = [] self.update() def update(self): """Update the view when an element was selected""" tables = {} for obj in self.spark.sql("show tables").rdd.collect(): db = obj[0] table = obj[1] temp = obj[2] if temp and db == "": db = "temp" if tables.get(db, None) is None: tables[db] = [] if temp: tables[db].append("%s (temp)" % table) else: tables[db].append(table) for db in sorted(tables.keys()): select = Select(options=[""] + sorted(tables[db]), disabled=False) select.observe(self.on_click(db, self), names="value") self.selects.append(select) self.accordion.children = self.selects for i, db in enumerate(sorted(tables.keys())): self.accordion.set_title(i, db) def on_click(self, db, parent): """Click handler providing db and parent as context Args: db (str): database name parent (object): parent object """ def f(change): if change["old"] is not None: parent.output.clear_output() with parent.output: if db == "temp": table = change["new"] else: table = "%s.%s" % (db, change["new"]) if table.endswith(" (temp)"): table = table[:-7] try: schema = parent.spark.sql("describe extended %s" % table) rows = int(parent.spark.conf.get("spark.sql.repl.eagerEval.maxNumRows")) parent.spark.conf.set("spark.sql.repl.eagerEval.maxNumRows", 1000) display(schema) parent.spark.conf.set("spark.sql.repl.eagerEval.maxNumRows", rows) except: print("schema cannot be accessed, table most probably broken") return f def close(self): """Close view""" self.selects = [] self.sc.close() def set_css(self): """Set CSS""" display( HTML( """ <style> .db-detail .p-Widget { overflow: visible; } </style> """ ) )
def _widget(self): """ Create IPython widget for display within a notebook """ try: return self._cached_widget except AttributeError: pass if self.asynchronous: return None try: from ipywidgets import Layout, VBox, HBox, IntText, Button, HTML, Accordion except ImportError: self._cached_widget = None return None layout = Layout(width="150px") title = HTML("<h2>YarnCluster</h2>") status = HTML(self._widget_status(), layout=Layout(min_width="150px")) request = IntText(0, description="Workers", layout=layout) scale = Button(description="Scale", layout=layout) minimum = IntText(0, description="Minimum", layout=layout) maximum = IntText(0, description="Maximum", layout=layout) adapt = Button(description="Adapt", layout=layout) accordion = Accordion( [HBox([request, scale]), HBox([minimum, maximum, adapt])], layout=Layout(min_width="500px"), ) accordion.selected_index = None accordion.set_title(0, "Manual Scaling") accordion.set_title(1, "Adaptive Scaling") @adapt.on_click def adapt_cb(b): self.adapt(minimum=minimum.value, maximum=maximum.value) @scale.on_click def scale_cb(b): with log_errors(): self.scale(request.value) app_id = HTML("<p><b>Application ID: </b>{0}</p>".format(self.app_id)) elements = [title, HBox([status, accordion]), app_id] if self.dashboard_link is not None: link = HTML( '<p><b>Dashboard: </b><a href="{0}" target="_blank">{0}' "</a></p>\n".format(self.dashboard_link)) elements.append(link) self._cached_widget = box = VBox(elements) self._status_widget = status return box
def _widget(self): """ Create IPython widget for display within a notebook """ try: return self._cached_widget except AttributeError: pass try: from ipywidgets import Layout, VBox, HBox, IntText, Button, HTML, Accordion except ImportError: self._cached_widget = None return None layout = Layout(width="150px") if self.dashboard_link: link = '<p><b>Dashboard: </b><a href="%s" target="_blank">%s</a></p>\n' % ( self.dashboard_link, self.dashboard_link, ) else: link = "" title = "<h2>%s</h2>" % self._cluster_class_name title = HTML(title) dashboard = HTML(link) status = HTML(self._widget_status(), layout=Layout(min_width="150px")) if self._supports_scaling: request = IntText(0, description="Workers", layout=layout) scale = Button(description="Scale", layout=layout) minimum = IntText(0, description="Minimum", layout=layout) maximum = IntText(0, description="Maximum", layout=layout) adapt = Button(description="Adapt", layout=layout) accordion = Accordion( [HBox([request, scale]), HBox([minimum, maximum, adapt])], layout=Layout(min_width="500px"), ) accordion.selected_index = None accordion.set_title(0, "Manual Scaling") accordion.set_title(1, "Adaptive Scaling") def adapt_cb(b): self.adapt(minimum=minimum.value, maximum=maximum.value) update() adapt.on_click(adapt_cb) def scale_cb(b): with log_errors(): n = request.value with suppress(AttributeError): self._adaptive.stop() self.scale(n) update() scale.on_click(scale_cb) else: accordion = HTML("") box = VBox([title, HBox([status, accordion]), dashboard]) self._cached_widget = box def update(): status.value = self._widget_status() cluster_repr_interval = parse_timedelta( dask.config.get("distributed.deploy.cluster-repr-interval", default="ms")) pc = PeriodicCallback(update, cluster_repr_interval * 1000) self.periodic_callbacks["cluster-repr"] = pc pc.start() return box
def _create_gui(self): """ Create the tree gui elements. Notes ----- Each node of the tree is either an `IPython.html.widgets.Accordion`, if the node has child nodes, or a `IPython.html.widgets.Select`, if the node has a list. Note well this does **not** allow for the case of child nodes and a list, so this does not really suffice as a file browser. List nodes monkey with their parents by editing the description to include the number of list items in the node. """ for parents, children, index in self._tree.walk(): if children and index: # This should be impossible... raise RuntimeError("What the ???") parent_string = self._id_string(parents) depth = len(parents) try: key = self._tree.tree_keys[depth] except IndexError: key = '' if depth == 0: self._top = Accordion() self._top.description = key # self._top.selected_index = -1 self._gui_objects[parent_string] = self._top parent = self._gui_objects[parent_string] # Do I have children? If so, add them as sub-accordions if children: child_objects = [] for child in children: desc = ": ".join([key, str(child)]) child_container = Accordion() child_container.description = desc # Make sure all panels start out closed. # child_container.selected_index = -1 child_container.parent = self._gui_objects[parent_string] child_string = os.path.join(parent_string, str(child)) self._gui_objects[child_string] = child_container child_objects.append(child_container) parent.children = child_objects # Do I have only a list? Populate a select box with those... if index: new_text = widgets.Select(options=index) new_text.layout.width = '100%' index_string = self._id_string([parent_string, 'files']) self._gui_objects[index_string] = new_text # On the last pass an Accordion will have been created for # this item. We need to replace that Accordion with a Select. # The Select should be inside a box so that we can set a # description on the box that won't be displayed on the # Select. When titles are built for the image viewer tree # later on they are based on the description of the Accordions # and their immediate children. old_parent = parent grandparent = old_parent.parent desc = old_parent.description s_or_not = ['', 's'] n_files = len(index) desc += " ({0} image{1})".format(n_files, s_or_not[n_files > 1]) # Place the box between the Select and the parent Accordion parent = widgets.Box() parent.description = desc parent.children = [new_text] parent.parent = grandparent self._replace_child(grandparent, old=old_parent, new=parent)
def _create_gui(self): """ Create the tree gui elements. Notes ----- Each node of the tree is either an `IPython.html.widgets.Accordion`, if the node has child nodes, or a `IPython.html.widgets.Select`, if the node has a list. Note well this does **not** allow for the case of child nodes and a list, so this does not really suffice as a file browser. List nodes monkey with their parents by editing the description to include the number of list items in the node. """ for parents, children, index in self._tree.walk(): if children and index: # This should be impossible... raise RuntimeError("What the ???") parent_string = self._id_string(parents) depth = len(parents) try: key = self._tree.tree_keys[depth] except IndexError: key = '' if depth == 0: self._top = Accordion() self._top.description = key # self._top.selected_index = -1 self._gui_objects[parent_string] = self._top parent = self._gui_objects[parent_string] # Do I have children? If so, add them as sub-accordions if children: child_objects = [] for child in children: desc = ": ".join([key, str(child)]) child_container = Accordion() child_container.description = desc # Make sure all panels start out closed. # child_container.selected_index = -1 child_container.parent = self._gui_objects[parent_string] child_string = os.path.join(parent_string, str(child)) self._gui_objects[child_string] = child_container child_objects.append(child_container) parent.children = child_objects # Do I have only a list? Populate a select box with those... if index: new_text = widgets.Select(options=index) new_text.layout.width = '100%' index_string = self._id_string([parent_string, 'files']) self._gui_objects[index_string] = new_text # On the last pass an Accordion will have been created for # this item. We need to replace that Accordion with a Select. # The Select should be inside a box so that we can set a # description on the box that won't be displayed on the # Select. When titles are built for the image viewer tree # later on they are based on the description of the Accordions # and their immediate children. old_parent = parent grandparent = old_parent.parent desc = old_parent.description s_or_not = ['', 's'] n_files = len(index) desc += " ({0} image{1})".format(n_files, s_or_not[n_files > 1]) # Place the box between the Select and the parent Accordion parent = widgets.Box() parent.description = desc parent.children = [new_text] parent.parent = grandparent self._replace_child(grandparent, old=old_parent, new=parent)
def _widget(self): """Create IPython widget for display within a notebook""" try: return self._cached_widget except AttributeError: pass try: from ipywidgets import ( HTML, Accordion, Button, HBox, IntText, Layout, Tab, VBox, ) except ImportError: self._cached_widget = None return None layout = Layout(width="150px") status = HTML(self._repr_html_()) if self._supports_scaling: request = IntText(0, description="Workers", layout=layout) scale = Button(description="Scale", layout=layout) minimum = IntText(0, description="Minimum", layout=layout) maximum = IntText(0, description="Maximum", layout=layout) adapt = Button(description="Adapt", layout=layout) accordion = Accordion( [HBox([request, scale]), HBox([minimum, maximum, adapt])], layout=Layout(min_width="500px"), ) accordion.selected_index = None accordion.set_title(0, "Manual Scaling") accordion.set_title(1, "Adaptive Scaling") def adapt_cb(b): self.adapt(minimum=minimum.value, maximum=maximum.value) update() adapt.on_click(adapt_cb) def scale_cb(b): with log_errors(): n = request.value with suppress(AttributeError): self._adaptive.stop() self.scale(n) update() scale.on_click(scale_cb) else: accordion = HTML("") scale_status = HTML(self._scaling_status()) tab = Tab() tab.children = [status, VBox([scale_status, accordion])] tab.set_title(0, "Status") tab.set_title(1, "Scaling") self._cached_widget = tab def update(): status.value = self._repr_html_() scale_status.value = self._scaling_status() cluster_repr_interval = parse_timedelta( dask.config.get("distributed.deploy.cluster-repr-interval", default="ms")) pc = PeriodicCallback(update, cluster_repr_interval * 1000) self.periodic_callbacks["cluster-repr"] = pc pc.start() return tab
class ExplorerFeatureSelect(ExplorerPane): def __init__(self, ed: ExplorerData, box_universe, box_name, parent_widget=None): super().__init__(parent_widget=parent_widget) self.data = ed self._box_universe = box_universe self._box_name = box_name self._suspend_updates = False try: clusterdef = ChainedBox(self._box_universe, self._box_name) clusterdef_relevant = clusterdef.relevant_and_demanded_features clusterdef_demanded = clusterdef.demanded_features self.checkers = [] for i in self.data.all_performance_measures: controller = Checkbox( value=i in clusterdef_relevant, description=i, disabled=i in clusterdef_demanded, style={'description_width': 'initial'}, tooltip=i if i not in clusterdef_demanded else _demanded_tooltip, ) self.checkers.append(controller) for i in self.data.all_strategy_names: controller = Checkbox( value=i in clusterdef_relevant, description=i, disabled=i in clusterdef_demanded, style={'description_width': 'initial'}, tooltip=i if i not in clusterdef_demanded else _demanded_tooltip, ) self.checkers.append(controller) for i in self.data.all_risk_factors: controller = Checkbox( value=i in clusterdef_relevant, description=i, disabled=i in clusterdef_demanded, style={'description_width': 'initial'}, tooltip=i if i not in clusterdef_demanded else _demanded_tooltip, ) self.checkers.append(controller) for s in range(len(self.checkers)): self.checkers[s].observe(self.toggle_check) self.ui_risks = VBox([ ch for ch in self.checkers if ch.description in self.data.all_risk_factors ], ) self.ui_strategies = VBox([ ch for ch in self.checkers if ch.description in self.data.all_strategy_names ], ) self.ui_perform = VBox([ ch for ch in self.checkers if ch.description in self.data.all_performance_measures ], ) self.accordion = Accordion( children=[self.ui_strategies, self.ui_risks, self.ui_perform], layout=Layout(width='600px', )) self.accordion.set_title(0, 'Policy Levers') self.accordion.set_title(1, 'Exogenous Uncertainties') self.accordion.set_title(2, 'Performance Measures') self.accordion_filter = Text(value='.*', placeholder='.*', description='Filter:', disabled=False) self.accordion_filter.observe(self.filter_checkboxes) self.current_relevant_attributes = Select( options=[], # value='OSX', rows=30, description='', disabled=False) # self.load_current_relevant_attributes_button = Button( # description=f"Next", # disabled=False, # button_style='', # 'success', 'info', 'warning', 'danger' or '' # tooltip='Load', # # icon='check' # ) # self.load_current_relevant_attributes_button.on_click( # self.load_selected_features_into_thresholds # ) self.next_button.on_click( self.load_selected_features_into_thresholds) # self.stack = VBox([ # self.header_area, # HBox([ # VBox([ # self.accordion_filter, # self.accordion, # ]), # VBox([ # HTML_widget(value=f"<b>{RELEVANT}</b>", placeholder=f"These things will be loaded"), # self.current_relevant_attributes, # self.load_current_relevant_attributes_button, # ]), # ]), # self.footer, # ]) self.make_stack( HBox([ VBox([ self.accordion_filter, self.accordion, ]), VBox([ HTML_widget( value=f"<b>{RELEVANT}</b>", placeholder=f"These things will be loaded"), self.current_relevant_attributes, # self.load_current_relevant_attributes_button, ]), ]), ) self.set_header(clusterdef.names) self.recompile_list_of_active_features( None) # initial interactive plots except: logger.exception("error in initialize_sliders") raise @property def joint_data(self): return self.data.joint_data def set_header(self, names): logger.info(f"{self.__class__.__name__}.set_header( {names} )") self.header.clear_output() with self.header: for name in names[:-1]: display(HTML(f"<h4>▽ {name}</h4>")) display(HTML(f"<h1>▷ {names[-1]}</h1>")) def toggle_check(self, action_info): try: logger.debug(f"toggle_check: {action_info}") owner = action_info.get('owner') if owner is None: return label = owner.description if action_info.get('name') == 'value': if label is not None: value = action_info.get('new') if value is not None: logger.info(f"toggle_check: set {label} to {value}") if value: self._box_universe[ self._box_name].relevant_features.add(label) self.recompile_list_of_active_features(None) else: self._box_universe[ self._box_name].relevant_features.discard( label) self.recompile_list_of_active_features(None) except: logger.exception("error in ExplorerFeatureSelect.toggle_check") raise def recompile_list_of_active_features(self, content): if content is not None and content['name'] == '_property_lock': return if self._suspend_updates: return try: owner = content['owner'] except: owner = None on_risks = [] on_strategies = [] on_perform = [] for ch in self.checkers: if ch.value: if ch.description in self.data.all_risk_factors_: on_risks.append(ch.description) if ch.description in self.data.all_strategy_names_: on_strategies.append(ch.description) if ch.description in self.data.all_performance_measures_: on_perform.append(ch.description) current = [] if on_strategies: current.append("-- STRATEGIES --") current.extend(on_strategies) if on_risks: current.append("-- RISK FACTORS --") current.extend(on_risks) if on_perform: current.append("-- PERFORMANCE MEASURES --") current.extend(on_perform) self.current_relevant_attributes.options = current def filter_checkboxes(self, event): if event.get('name') == 'value': logger.debug(f"filter_checkboxes:{event}") filter = event.get('new') if filter is not None: if filter == "" or filter == "*": for ch in self.checkers: # ch.layout.visibility = 'visible' ch.layout.display = 'flex' else: import re pattern = re.compile(filter) for ch in self.checkers: if pattern.search(ch.description): #ch.layout.visibility = 'visible' ch.layout.display = 'flex' else: #ch.layout.visibility = 'hidden' ch.layout.display = 'none' def load_selected_features_into_thresholds(self, event): execution_tracer(logger, event) try: self._parent_widget.load_cluster(self._box_name) [h.flush() for h in logger.handlers[0]] except: logger.exception() raise execution_tracer(logger, "END")
class SageExplorer(VBox): """Sage Explorer in Jupyter Notebook""" value = Any() def __init__(self, obj=None): """ TESTS:: sage: from sage_explorer.sage_explorer import SageExplorer sage: S = StandardTableaux(15) sage: t = S.random_element() sage: widget = SageExplorer(t) """ super(SageExplorer, self).__init__() self.title = Title() self.propsbox = VBox() # Will be a VBox full of HBoxes, one for each property self.titlebox = VBox() self.titlebox.add_class('titlebox') self.titlebox.add_class('lightborder') self.titlebox.children = [self.title, self.propsbox] self.visualbox = Box() self.visualtext = Textarea('', rows=8) self.visualwidget = None self.visualbox.add_class('visualbox') self.visualbox.children = [self.visualtext] self.top = HBox([self.titlebox, self.visualbox], layout=justified_h_layout) self.menus = Accordion(selected_index=None) self.menusbox = VBox([Title("Menus", 2), self.menus]) self.menusbox.add_class('catalog-menus') self.menusbox.add_class('lightborder') self.inputs = HBox() self.gobutton = Button(description='Run!', tooltip='Run the function or method, with specified arguments') self.output = HTML() self.worktab = VBox((self.inputs, self.gobutton, self.output)) self.doc = HTML() self.doctab = HTML() # For the method docstring self.tabs = Tab((self.worktab, self.doctab)) # Will be used when a method is selected self.tabs.add_class('tabs') self.tabs.set_title(0, 'Call') self.tabs.set_title(1, 'Help') self.main = Box((self.doc, self.tabs)) self.main.add_class('lightborder') self.tabs.add_class('invisible') # Hide tabs at first display self.bottom = HBox((self.menusbox, self.main), layout=main_h_layout) self.children = (self.top, self.bottom) self.history = [] self.set_value(obj) def init_selected_menu_value(self): r""" From a menu selection, compute display elements for the widgets. TESTS:: sage: from sage_explorer.sage_explorer import SageExplorer, ExploredMember sage: from sage.monoids.string_monoid import AlphabeticStrings sage: e = SageExplorer() sage: m = ExploredMember('AlphabeticStrings', member=AlphabeticStrings) sage: e.selected_menu_value = m sage: e.init_selected_menu_value() sage: 'string monoid on generators A-Z' in e.doc.value True """ if self.value: """If we are exploring an object, all menu items are functions""" self.init_selected_func() return """We are on the catalogs page""" selected_obj = self.selected_menu_value # An ExplorerMember if not hasattr(selected_obj, 'member_type'): selected_obj.compute_member_type() if not hasattr(selected_obj, 'doc'): selected_obj.compute_doc() if 'function' in selected_obj.member_type or 'method' in selected_obj.member_type: self.doctab.value = to_html(selected_obj.doc) if not hasattr(selected_obj, 'args'): try: selected_obj.member = selected_obj.member() except: pass elif hasattr(selected_obj, 'defaults') and len(selected_obj.defaults) == len(selected_obj.args): try: selected_obj.member = selected_obj.member(selected_obj.defaults) except: pass return if 'class' in selected_obj.member_type: self.doc.value = to_html(selected_obj.doc) self.doctab.value = '' self.inputs.children = [] self.tabs.remove_class('visible') self.tabs.add_class('invisible') self.doc.remove_class('invisible') self.doc.add_class('visible') return def init_selected_func(self): r""" From a menu selection, compute display elements for the widgets. TESTS:: sage: from sage_explorer.sage_explorer import SageExplorer, ExploredMember sage: from sage.combinat.partition import Partition sage: p = Partition([3,3,2,1]) sage: e = SageExplorer(p) sage: m = ExploredMember('conjugate', parent=p) sage: e.selected_menu_value = m sage: e.init_selected_func() sage: str(e.doctab.value[:100]) # For Python3 compatibility '<div class="docstring">\n \n <blockquote>\n<div><p>Return the conjugate partition of the partition ' """ self.output.value = '' func = self.selected_menu_value # An ExplorerMember if not hasattr(func, 'doc'): func.compute_doc() if not hasattr(func, 'origin'): func.compute_origin() self.doctab.value = to_html(func.doc) if func.overrides: self.doctab.value += to_html("Overrides:") self.doctab.value += to_html(', '.join([extract_classname(x, element_ok=True) for x in func.overrides])) inputs = [] if not hasattr(func, 'args'): func.compute_argspec() try: shift = 0 for i in range(len(func.args)): argname = func.args[i] if argname in ['self']: shift = 1 continue default = '' if func.defaults and len(func.defaults) > i - shift and func.defaults[i - shift]: default = func.defaults[i - shift] inputs.append(Text(description=argname, placeholder=str(default))) except: print (func, "attr?") print (func.args, func.defaults) self.inputs.children = inputs self.doc.remove_class('visible') self.doc.add_class('invisible') self.tabs.remove_class('invisible') self.tabs.add_class('visible') def get_title(self): r""" Get explorer general title. TESTS: sage: from sage_explorer import SageExplorer sage: from sage.combinat.partition import Partition sage: p = Partition([3,3,2,1]) sage: e = SageExplorer(p) sage: e.get_title() 'Exploring: [3, 3, 2, 1]' """ return "Exploring: %s" % repr(self.value) def get_members(self): r""" Get all members for object self.value. OUTPUT: List of `Member` named tuples. TESTS:: sage: from sage_explorer import SageExplorer sage: from sage.combinat.partition import Partition sage: p = Partition([3,3,2,1]) sage: e = SageExplorer(p) sage: e.get_members() sage: e.members[2].name, e.members[2].privacy ('__class__', 'python_special') sage: e.members[68].name, e.members[68].origin, e.members[68].privacy ('_doccls', <class 'sage.combinat.partition.Partitions_all_with_category.element_class'>, 'private') sage: [(e.members[i].name, e.members[i].overrides, e.members[i].prop_label) for i in range(len(e.members)) if e.members[i].name == '_reduction'] [('_reduction', [<class 'sage.categories.infinite_enumerated_sets.InfiniteEnumeratedSets.element_class'>, <class 'sage.categories.enumerated_sets.EnumeratedSets.element_class'>, <class 'sage.categories.sets_cat.Sets.Infinite.element_class'>, <class 'sage.categories.sets_cat.Sets.element_class'>, <class 'sage.categories.sets_with_partial_maps.SetsWithPartialMaps.element_class'>, <class 'sage.categories.objects.Objects.element_class'>], None)] sage: e = SageExplorer(Partition) sage: e.get_members() """ if isclass(self.value): c0 = self.value else: c0 = self.value.__class__ self.valueclass = c0 members = [] for name, member in getmembers(c0): if isabstract(member) or 'deprecated' in str(type(member)).lower(): continue m = ExploredMember(name, member=member, parent=self.value) m.compute_member_type() m.compute_origin() m.compute_privacy() m.compute_property_label(CONFIG_PROPERTIES) members.append(m) self.members = members def get_attributes(self): r""" Get all attributes for object self.value. OUTPUT: List of `Attribute` named tuples. TESTS:: sage: from sage_explorer import SageExplorer sage: from sage.combinat.partition import Partition sage: p = Partition([3,3,2,1]) sage: e = SageExplorer(p) sage: e.get_attributes() sage: e.attributes[0].privacy 'python_special' sage: [e.attributes[i].origin for i in range(len(e.attributes)) if e.attributes[i].name in ['_doccls', '_dummy_attribute', 'young_subgroup']] [<class 'sage.combinat.partition.Partitions_all_with_category.element_class'>, <class 'sage.categories.sets_cat.Sets.element_class'>, <class 'sage.combinat.partition.Partitions_all_with_category.element_class'>] """ if not hasattr(self, 'members'): self.get_members() attributes = [] for m in self.members: if m.member_type.startswith('attribute'): attributes.append(m) self.attributes = attributes def get_methods(self): r""" Get all methods specifications for object self.value. TESTS:: sage: from sage_explorer import SageExplorer sage: from sage.combinat.partition import Partition sage: p = Partition([3,3,2,1]) sage: e = SageExplorer(p) sage: e.get_methods() sage: [e.methods[i].privacy for i in range(len(e.methods)) if e.methods[i].name in ['_latex_coeff_repr', '_sage_', 'is_zero']] ['private', 'sage_special', None] sage: [e.methods[i].args for i in range(len(e.methods)) if e.methods[i].name in ['_unicode_art_', 'dual_equivalence_graph', 'upper_hook']] [['self'], ['self', 'directed', 'coloring'], ['self', 'i', 'j', 'alpha']] sage: e = SageExplorer(Partition) sage: e.get_methods() """ if not hasattr(self, 'members'): self.get_members() methods = [] for m in self.members: if not 'method' in m.member_type and not 'function' in m.member_type: continue m.compute_argspec() methods.append(m) self.methods = methods def compute(self): """Get some properties, depending on the object Create links between menus and output tabs""" obj = self.value if obj is None: self.make_index() return if isclass(obj): c0 = obj else: c0 = obj.__class__ if not hasattr(self, 'objclass') or c0 != self.objclass: self.objclass = c0 self.get_members() self.get_attributes() self.get_methods() self.classname = extract_classname(c0, element_ok=False) self.title.value = self.get_title() replace_widget_w_css(self.tabs, self.doc) visualwidget = get_widget(obj) if visualwidget: # Reset if necessary, then replace with visualbox self.visualbox.children = [self.visualtext] self.visualwidget = visualwidget def graphical_change(change): self.set_value(change.new) self.visualwidget.observe(graphical_change, names='value') replace_widget_hard(self.visualbox, self.visualtext, self.visualwidget) else: try: self.visualtext.value = repr(obj._ascii_art_()) except: self.visualtext.value = repr(obj) if self.visualwidget: replace_widget_hard(self.visualbox, self.visualwidget, self.visualtext) self.visualwidget = None attributes_as_properties = [m for m in self.attributes if m.prop_label] methods_as_properties = [m for m in self.methods if m.prop_label] attributes = [m for m in self.attributes if not m in attributes_as_properties and not m.name in EXCLUDED_MEMBERS and not m.privacy in ['private', 'sage_special']] methods = [m for m in self.methods if not m in methods_as_properties and not m.name in EXCLUDED_MEMBERS and not m.privacy in ['private', 'sage_special']] props = [Title('Properties', 2)] # a list of HBoxes, to become self.propsbox's children # Properties for p in attributes_as_properties + methods_as_properties: try: value = p.member(obj) except: print ("Warning: Error in finding method %s" % p.name) value = None button = self.make_new_page_button(value) b_label = p.prop_label if type(value) is type(True): b_label += '?' else: b_label += ':' props.append(HBox([ Label(b_label), button ])) if len(self.history) > 1: self.propsbox.children = props + [self.make_back_button()] else: self.propsbox.children = props # Object doc self.doc.value = to_html(obj.__doc__) # Initialize to object docstring # Methods (sorted by definition classes) self.selected_menu_value = c0 bases = [] basemembers = {} for c in getmro(c0): bases.append(c) basemembers[c] = [] for m in methods: basemembers[m.origin].append(m.name) for c in basemembers: if not basemembers[c]: bases.remove(c) menus = [] for i in range(len(bases)): c = bases[i] menus.append(Select(rows=12, options = [(m.name, m) for m in methods if m.name in basemembers[c]] )) self.menus.children = menus for i in range(len(bases)): c = bases[i] self.menus.set_title(i, extract_classname(c)) def menu_on_change(change): self.selected_menu_value = change.new self.init_selected_menu_value() for menu in self.menus.children: menu.observe(menu_on_change, names='value') def compute_selected_method(button): args = [] for i in self.inputs.children: try: arg = i.value or i.placeholder evaled_arg = eval_in_main(arg) if not arg: self.output.value = to_html("Argument '%s' is empty!" % i.description) return args.append(evaled_arg) except: self.output.value = to_html("Could not evaluate argument '%s'" % i.description) return try: if AlarmInterrupt: alarm(TIMEOUT) out = self.selected_menu_value.member(obj, *args) if AlarmInterrupt: cancel_alarm() except AlarmInterrupt: self.output.value = to_html("Timeout!") except Exception as e: self.output.value = to_html(e) return self.output.value = to_html(out) self.gobutton.description = 'Run!' self.gobutton.on_click(compute_selected_method) def make_back_button(self): r""" Make a button for getting back to the previous object. TESTS:: sage: from sage_explorer import SageExplorer sage: from sage.combinat.partition import Partition sage: p1 = Partition([3,3,2,1]) sage: p2 = Partition([5,3,2]) sage: e = SageExplorer(p1) sage: e.make_back_button() sage: e.set_value(p2) sage: e.make_back_button() Button(description=u'Back', icon=u'history', layout=Layout(width=u'7em'), style=ButtonStyle(), tooltip=u'Go back to previous object page') """ if len(self.history) <= 1: return button = Button(description='Back', icon='history', tooltip="Go back to previous object page", layout=back_button_layout) button.on_click(lambda event: self.pop_value()) # No back button in this new (previous object) page return button def make_new_page_button(self, obj): r""" Make a button for fetching a new explorer with value `obj`. TESTS:: sage: from sage_explorer import SageExplorer sage: from sage.combinat.partition import Partition sage: p1 = Partition([3,3,2,1]) sage: p2 = Partition([5,3,2]) sage: e = SageExplorer(p1) sage: e.make_new_page_button(p2) Button(description=u'[5, 3, 2]', style=ButtonStyle(), tooltip=u'Will close current explorer and open a new one') """ button = Button(description=str(obj), tooltip="Will close current explorer and open a new one") button.on_click(lambda b:self.set_value(obj)) return button def display_new_value(self, obj): r""" A callback for the navigation button. """ self.visualbox.children[0].value = str(obj) def get_value(self): r""" Return math object currently explored. TESTS:: sage: from sage_explorer.sage_explorer import SageExplorer sage: from sage.combinat.partition import Partition sage: p = Partition([3,3,2,1]) sage: e = SageExplorer(p) sage: e.get_value() [3, 3, 2, 1] """ return self.value def set_value(self, obj): r""" Set new math object `obj` to the explorer. TESTS:: sage: from sage_explorer.sage_explorer import SageExplorer sage: from sage.combinat.partition import Partition sage: p = Partition([3,3,2,1]) sage: e = SageExplorer(p) sage: e.get_value() [3, 3, 2, 1] sage: from sage.combinat.tableau import Tableau sage: t = Tableau([[1,2,3,4], [5,6]]) sage: e.set_value(t) sage: e.get_value() [[1, 2, 3, 4], [5, 6]] """ self.history.append(obj) self.value = obj self.compute() def pop_value(self): r""" Set again previous math object to the explorer. TESTS:: sage: from sage_explorer.sage_explorer import SageExplorer sage: from sage.combinat.partition import Partition sage: p = Partition([3,3,2,1]) sage: e = SageExplorer(p) sage: from sage.combinat.tableau import Tableau sage: t = Tableau([[1,2,3,4], [5,6]]) sage: e.set_value(t) sage: e.get_value() [[1, 2, 3, 4], [5, 6]] sage: e.pop_value() sage: e.get_value() [3, 3, 2, 1] """ if self.history: self.history.pop() if self.history: self.value = self.history[-1] else: self.value = None self.compute() def make_index(self): try: from ._catalogs import catalogs except: print("To build the index page, we need some catalogs.") catalogs = [] self.selected_object = None self.title.value = "Sage Explorer" self.visualbox.children = [Title("Index Page")] self.tabs.remove_class('invisible') self.tabs.add_class('visible') self.gobutton.description = 'Go!' menus = [] for label, catalog in catalogs: menu = Select(rows=12, options=make_catalog_menu_options(catalog)) menus.append(menu) self.menus.children = menus for i, (label, _) in enumerate(catalogs): self.menus.set_title(i, label) def menu_on_change(change): self.selected_object = change.new self.display_new_value(self.selected_object.name) self.doctab.value = to_html(change.new.doc) self.gobutton.on_click(lambda b:self.set_value(self.selected_object.member)) for menu in self.menus.children: menu.observe(menu_on_change, names='value')
def make_config(self): layout = Layout() style = {"description_width": "initial"} checkbox1 = Checkbox(description="Show Targets", value=self.net.config["show_targets"], layout=layout, style=style) checkbox1.observe(lambda change: self.set_attr(self.net.config, "show_targets", change["new"]), names='value') checkbox2 = Checkbox(description="Errors", value=self.net.config["show_errors"], layout=layout, style=style) checkbox2.observe(lambda change: self.set_attr(self.net.config, "show_errors", change["new"]), names='value') hspace = IntText(value=self.net.config["hspace"], description="Horizontal space between banks:", style=style, layout=layout) hspace.observe(lambda change: self.set_attr(self.net.config, "hspace", change["new"]), names='value') vspace = IntText(value=self.net.config["vspace"], description="Vertical space between layers:", style=style, layout=layout) vspace.observe(lambda change: self.set_attr(self.net.config, "vspace", change["new"]), names='value') self.feature_bank = Select(description="Details:", value=self.net.config["dashboard.features.bank"], options=[""] + [layer.name for layer in self.net.layers], rows=1) self.feature_bank.observe(self.regenerate, names='value') self.control_select = Select( options=['Test', 'Train'], value=self.net.config["dashboard.dataset"], description='Dataset:', rows=1 ) self.control_select.observe(self.change_select, names='value') column1 = [self.control_select, self.zoom_slider, hspace, vspace, HBox([checkbox1, checkbox2]), self.feature_bank, self.feature_columns, self.feature_scale ] ## Make layer selectable, and update-able: column2 = [] layer = self.net.layers[-1] self.layer_select = Select(description="Layer:", value=layer.name, options=[layer.name for layer in self.net.layers], rows=1) self.layer_select.observe(self.update_layer_selection, names='value') column2.append(self.layer_select) self.layer_visible_checkbox = Checkbox(description="Visible", value=layer.visible, layout=layout) self.layer_visible_checkbox.observe(self.update_layer, names='value') column2.append(self.layer_visible_checkbox) self.layer_colormap = Select(description="Colormap:", options=[""] + AVAILABLE_COLORMAPS, value=layer.colormap if layer.colormap is not None else "", layout=layout, rows=1) self.layer_colormap_image = HTML(value="""<img src="%s"/>""" % self.net._image_to_uri(self.make_colormap_image(layer.colormap))) self.layer_colormap.observe(self.update_layer, names='value') column2.append(self.layer_colormap) column2.append(self.layer_colormap_image) ## get dynamic minmax; if you change it it will set it in layer as override: minmax = layer.get_act_minmax() self.layer_mindim = FloatText(description="Leftmost color maps to:", value=minmax[0], style=style) self.layer_maxdim = FloatText(description="Rightmost color maps to:", value=minmax[1], style=style) self.layer_mindim.observe(self.update_layer, names='value') self.layer_maxdim.observe(self.update_layer, names='value') column2.append(self.layer_mindim) column2.append(self.layer_maxdim) output_shape = layer.get_output_shape() self.layer_feature = IntText(value=layer.feature, description="Feature to show:", style=style) self.svg_rotate = Checkbox(description="Rotate", value=layer.visible, layout=layout) self.layer_feature.observe(self.update_layer, names='value') column2.append(self.layer_feature) self.svg_rotate = Checkbox(description="Rotate network", value=self.net.config["svg_rotate"], style={"description_width": 'initial'}, layout=Layout(width="52%")) self.svg_rotate.observe(lambda change: self.set_attr(self.net.config, "svg_rotate", change["new"]), names='value') self.save_config_button = Button(icon="save", layout=Layout(width="10%")) self.save_config_button.on_click(self.save_config) column2.append(HBox([self.svg_rotate, self.save_config_button])) config_children = HBox([VBox(column1, layout=Layout(width="100%")), VBox(column2, layout=Layout(width="100%"))]) accordion = Accordion(children=[config_children]) accordion.set_title(0, self.net.name) accordion.selected_index = None return accordion
def _init_widget(self): state = self._state widget = dict() fig = dict(x=self._create_slice_fig(), y=self._create_slice_fig(), z=self._create_slice_fig()) widget['fig'] = fig topomap_fig = dict(mag=create_topomap_fig(), grad=create_topomap_fig(), eeg=create_topomap_fig()) widget['topomap_fig'] = topomap_fig label = dict() label['axis'] = dict(x=HTML(f"<b>{state['label_text']['x']}</b>"), y=HTML(f"<b>{state['label_text']['y']}</b>"), z=HTML(f"<b>{state['label_text']['z']}</b>")) label['topomap_mag'] = HTML(f"<b>{state['label_text']['topomap_mag']}</b>") label['topomap_grad'] = HTML(f"<b>{state['label_text']['topomap_grad']}</b>") label['topomap_eeg'] = HTML(f"<b>{state['label_text']['topomap_eeg']}</b>") label['dipole_pos'] = Label('Not set') label['dipole_ori'] = Label('Not set') label['dipole_pos_'] = Label('Dipole origin:') label['dipole_ori_'] = Label('Dipole orientation:') label['status'] = Label('Status:') label['updating'] = Label('Ready.') widget['label'] = label widget['tab'] = Tab(layout=Layout(width='700')) toggle_buttons = dict( mode_selector=ToggleButtons( options=['Slice Browser', 'Set Dipole Origin', 'Set Dipole Orientation'], button_style='primary', layout=Layout(width='auto')) ) toggle_buttons['mode_selector'].observe(self._handle_view_mode_change, 'value') widget['toggle_buttons'] = toggle_buttons widget['reset_button'] = Button(description='Reset', button_style='danger', layout=Layout(width='auto')) widget['reset_button'].on_click(self._handle_reset_button_click) checkbox = dict(exact_solution=Checkbox( value=self._exact_solution, description='Exact solution (slow!)', tooltip='Calculate an exact forward projection. This is SLOW!')) checkbox['exact_solution'].observe(self._toggle_exact_solution, 'value') widget['checkbox'] = checkbox widget['amplitude_slider'] = IntSlider( value=int(self._state['dipole_amplitude'] * 1e9), min=5, max=100, step=5, continuous_update=False) widget['amplitude_slider'].observe(self._handle_amp_change, names='value') widget['label']['amplitude_slider'] = Label('Dipole amplitude in nAm') widget['quickstart_text'] = HTML( value=('<ul>' '<li>Select the desired brain slices in the ' '<b>Slice Browser (or use a preset).</b></li>' '<li>Choose the location of the dipole via ' '<b>Set Dipole Origin.</b></li>' '<li>Orient the dipole via ' '<b>Set Dipole Orientation.</b></li>' '<li>Adjust the <b>dipole amplitude</b> ' 'using the slider below the topographic maps.</li>' '</ul>\n' '<p><b>This application is still ' '<a href="https://github.com/hoechenberger/dipoles_demo/issues/26">' 'work in progress</a>.</b></p>')) widget['quickstart_accordion'] = Accordion( children=[widget['quickstart_text']]) widget['quickstart_accordion'].set_title(0, 'Quickstart') widget['preset_dropdown'] = Dropdown( options=['Select Preset…', 'Preset 1', 'Preset 2', 'Preset 3'], value='Select Preset…', layout=Layout(width='auto')) widget['preset_dropdown'].observe(self._handle_preset_selection_change, 'value') widget['title'] = HTML(value='<h2>Dipole Simulator</h2>') widget['output'] = output_widget return widget
def __init__(self, ed: ExplorerData, box_universe, box_name, parent_widget=None): super().__init__(parent_widget=parent_widget) self.data = ed self._box_universe = box_universe self._box_name = box_name self._suspend_updates = False try: clusterdef = ChainedBox(self._box_universe, self._box_name) clusterdef_relevant = clusterdef.relevant_and_demanded_features clusterdef_demanded = clusterdef.demanded_features self.checkers = [] for i in self.data.all_performance_measures: controller = Checkbox( value=i in clusterdef_relevant, description=i, disabled=i in clusterdef_demanded, style={'description_width': 'initial'}, tooltip=i if i not in clusterdef_demanded else _demanded_tooltip, ) self.checkers.append(controller) for i in self.data.all_strategy_names: controller = Checkbox( value=i in clusterdef_relevant, description=i, disabled=i in clusterdef_demanded, style={'description_width': 'initial'}, tooltip=i if i not in clusterdef_demanded else _demanded_tooltip, ) self.checkers.append(controller) for i in self.data.all_risk_factors: controller = Checkbox( value=i in clusterdef_relevant, description=i, disabled=i in clusterdef_demanded, style={'description_width': 'initial'}, tooltip=i if i not in clusterdef_demanded else _demanded_tooltip, ) self.checkers.append(controller) for s in range(len(self.checkers)): self.checkers[s].observe(self.toggle_check) self.ui_risks = VBox([ ch for ch in self.checkers if ch.description in self.data.all_risk_factors ], ) self.ui_strategies = VBox([ ch for ch in self.checkers if ch.description in self.data.all_strategy_names ], ) self.ui_perform = VBox([ ch for ch in self.checkers if ch.description in self.data.all_performance_measures ], ) self.accordion = Accordion( children=[self.ui_strategies, self.ui_risks, self.ui_perform], layout=Layout(width='600px', )) self.accordion.set_title(0, 'Policy Levers') self.accordion.set_title(1, 'Exogenous Uncertainties') self.accordion.set_title(2, 'Performance Measures') self.accordion_filter = Text(value='.*', placeholder='.*', description='Filter:', disabled=False) self.accordion_filter.observe(self.filter_checkboxes) self.current_relevant_attributes = Select( options=[], # value='OSX', rows=30, description='', disabled=False) # self.load_current_relevant_attributes_button = Button( # description=f"Next", # disabled=False, # button_style='', # 'success', 'info', 'warning', 'danger' or '' # tooltip='Load', # # icon='check' # ) # self.load_current_relevant_attributes_button.on_click( # self.load_selected_features_into_thresholds # ) self.next_button.on_click( self.load_selected_features_into_thresholds) # self.stack = VBox([ # self.header_area, # HBox([ # VBox([ # self.accordion_filter, # self.accordion, # ]), # VBox([ # HTML_widget(value=f"<b>{RELEVANT}</b>", placeholder=f"These things will be loaded"), # self.current_relevant_attributes, # self.load_current_relevant_attributes_button, # ]), # ]), # self.footer, # ]) self.make_stack( HBox([ VBox([ self.accordion_filter, self.accordion, ]), VBox([ HTML_widget( value=f"<b>{RELEVANT}</b>", placeholder=f"These things will be loaded"), self.current_relevant_attributes, # self.load_current_relevant_attributes_button, ]), ]), ) self.set_header(clusterdef.names) self.recompile_list_of_active_features( None) # initial interactive plots except: logger.exception("error in initialize_sliders") raise
PXTAL_UI["button"] = Button(description="Build") PXTAL_UI["button"].layout = Layout(width="99%") PXTAL_UI["sim_progress"] = Output() PXTAL_UI["a1"] = VBox([ PXTAL_UI["infinite"], PXTAL_UI["polymer_type"], PXTAL_UI["helicity"], PXTAL_UI["monomers"], PXTAL_UI["tacticity"], PXTAL_UI["chiriality"], PXTAL_UI["defect"], ]) PXTAL_UI["a2"] = VBox([PXTAL_UI["lmpdata_file"]]) PXTAL_UI["a"] = Accordion(children=[PXTAL_UI["a1"], PXTAL_UI["a2"]]) PXTAL_UI["a"].set_title(0, "Structure") PXTAL_UI["a"].set_title(1, "Output") PXTAL_UI["l1"] = VBox( [PXTAL_UI["a"], PXTAL_UI["button"], PXTAL_UI["sim_progress"]]) PXTAL_UI["l2"] = Tab(children=[]) PXTAL_UI["bs"] = HBox([PXTAL_UI["l1"], PXTAL_UI["l2"]]) PXTAL_UI["l1"].layout = Layout(width="500px", border="1px") PXTAL_UI["l2"].layout = Layout(width="100%", border="1px") PXTAL_UI["bs"].layout = Layout(width="100%", border="1px") PXTAL_UI["display"] = ui.Form([PXTAL_UI["bs"]], name="PolymerXtal - Polymer Helix Chain Builder")
def _widget(self): """ Create IPython widget for display within a notebook """ try: return self._cached_widget except AttributeError: pass from ipywidgets import Layout, VBox, HBox, IntText, Button, HTML, Accordion layout = Layout(width="150px") if "dashboard" in self.scheduler.services: link = self.dashboard_link link = '<p><b>Dashboard: </b><a href="%s" target="_blank">%s</a></p>\n' % ( link, link, ) else: link = "" title = "<h2>%s</h2>" % type(self).__name__ title = HTML(title) dashboard = HTML(link) status = HTML(self._widget_status(), layout=Layout(min_width="150px")) request = IntText(0, description="Workers", layout=layout) scale = Button(description="Scale", layout=layout) minimum = IntText(0, description="Minimum", layout=layout) maximum = IntText(0, description="Maximum", layout=layout) adapt = Button(description="Adapt", layout=layout) accordion = Accordion( [HBox([request, scale]), HBox([minimum, maximum, adapt])], layout=Layout(min_width="500px"), ) accordion.selected_index = None accordion.set_title(0, "Manual Scaling") accordion.set_title(1, "Adaptive Scaling") box = VBox([title, HBox([status, accordion]), dashboard]) self._cached_widget = box def adapt_cb(b): self.adapt(minimum=minimum.value, maximum=maximum.value) adapt.on_click(adapt_cb) def scale_cb(b): with log_errors(): n = request.value with ignoring(AttributeError): self._adaptive.stop() self.scale(n) scale.on_click(scale_cb) scheduler_ref = ref(self.scheduler) def update(): status.value = self._widget_status() pc = PeriodicCallback(update, 500, io_loop=self.scheduler.loop) self.scheduler.periodic_callbacks["cluster-repr"] = pc pc.start() return box
def __display_interface(widget_tuples: List[Tuple[str, Widget]]) -> None: interface: Accordion = Accordion(children=list( map(lambda widget_tuple: widget_tuple[1], widget_tuples))) for i in range(len(widget_tuples)): interface.set_title(i, widget_tuples[i][0]) display(interface)
def _widget(self): """ Create IPython widget for display within a notebook """ try: return self._cached_widget except AttributeError: pass from ipywidgets import ( Layout, VBox, HBox, IntText, Button, HTML, Accordion, Text, ) layout = Layout(width="150px") if "dashboard" in self.scheduler.services: link = self.dashboard_link link = '<p><b>Dashboard: </b><a href="%s" target="_blank">%s</a></p>\n' % ( link, link, ) else: link = "" title = "<h2>%s</h2>" % type(self).__name__ title = HTML(title) dashboard = HTML(link) status = HTML(self._widget_status(), layout=Layout(min_width="150px")) request = IntText(0, description="Workers", layout=layout) scale = Button(description="Scale", layout=layout) request_cores = IntText(0, description="Cores", layout=layout) scale_cores = Button(description="Scale", layout=layout) request_memory = Text("O GB", description="Memory", layout=layout) scale_memory = Button(description="Scale", layout=layout) minimum = IntText(0, description="Minimum", layout=layout) maximum = IntText(0, description="Maximum", layout=layout) adapt = Button(description="Adapt", layout=layout) minimum_cores = IntText(0, description="Min cores", layout=layout) maximum_cores = IntText(0, description="Max cores", layout=layout) adapt_cores = Button(description="Adapt", layout=layout) minimum_mem = Text("0 GB", description="Min memory", layout=layout) maximum_mem = Text("0 GB", description="Max memory", layout=layout) adapt_mem = Button(description="Adapt", layout=layout) scale_hbox = [HBox([request, scale])] adapt_hbox = [HBox([minimum, maximum, adapt])] if hasattr(self, "jobqueue_worker_spec"): scale_hbox.append(HBox([request_cores, scale_cores])) scale_hbox.append(HBox([request_memory, scale_memory])) adapt_hbox.append(HBox([minimum_cores, maximum_cores, adapt_cores])) adapt_hbox.append(HBox([minimum_mem, maximum_mem, adapt_mem])) accordion = Accordion( [VBox(scale_hbox), VBox(adapt_hbox)], layout=Layout(min_width="500px")) accordion.selected_index = None accordion.set_title(0, "Manual Scaling") accordion.set_title(1, "Adaptive Scaling") box = VBox([title, HBox([status, accordion]), dashboard]) self._cached_widget = box def adapt_cb(b): self.adapt(minimum=minimum.value, maximum=maximum.value) def adapt_cores_cb(b): self.adapt(minimum_cores=minimum_cores.value, maximum_cores=maximum_cores.value) def adapt_mem_cb(b): self.adapt(minimum_memory=minimum_mem.value, maximum_memory=maximum_mem.value) adapt.on_click(adapt_cb) adapt_cores.on_click(adapt_cores_cb) adapt_mem.on_click(adapt_mem_cb) def scale_cb(request, kwarg): def request_cb(b): with log_errors(): arg = request.value with ignoring(AttributeError): self._adaptive.stop() local_kwargs = dict() local_kwargs[kwarg] = arg self.scale(**local_kwargs) return request_cb scale.on_click(scale_cb(request, "n")) scale_cores.on_click(scale_cb(request_cores, "cores")) scale_memory.on_click(scale_cb(request_memory, "memory")) def update(): status.value = self._widget_status() pc = PeriodicCallback(update, 500, io_loop=self.scheduler.loop) self.scheduler.periodic_callbacks["cluster-repr"] = pc pc.start() return box
class ParCoordsViewer(VBox): def __init__( self, data, scope, robustness_functions=None, initial_max_active_measures = 5, ): self.data = data self.scope = scope self.robustness_functions = robustness_functions if self.robustness_functions is None: self.robustness_functions = () self.parcoords = parallel_coords( self.data, scope=self.scope, flip_dims=(), robustness_functions=robustness_functions, color_dim=0, colorscale='Viridis', title=None, ) self.dim_activators = [] self.dim_activators_by_name = {} self.out_logger = widget.Output() prefix_chars = _prefix_symbols(scope, robustness_functions) n_active_measures = 0 measure_names = set(self.scope.get_measure_names()) if robustness_functions is not None: measure_names |= set(rf.name for rf in robustness_functions) for i in self.data.columns: if i in self.scope.get_constant_names(): continue short_i = i if self.scope is not None: short_i = self.scope.shortname(i) i_value = True if i in measure_names: if n_active_measures >= initial_max_active_measures: i_value = False for dim in self.parcoords.data[0].dimensions: if dim.name == i: dim.visible = False else: n_active_measures += 1 cb = NamedCheckbox(description=prefix_chars.get(i,'')+short_i, value=i_value, name=i, description_tooltip=i) cb.observe(self._on_dim_choose_toggle, names='value') self.dim_activators.append(cb) self.dim_activators_by_name[i] = cb self.dim_choose = Accordion( children=[ widget.Box( self.dim_activators, layout=widget.Layout(flex_flow='row wrap') ) ], layout=widget.Layout(width='100%') ) self.dim_choose.set_title(0, 'Axes') self.dim_choose.selected_index = None self.color_dim_choose = widget.Dropdown( options=['< None >']+list(self.data.columns), description='Colorize:', value=self.data.columns[0], ) self.color_dim_choose.observe(self._on_color_choose, names='value') self.select_menu = widget.Dropdown( options=[ _NOTHING, _CLEAR_CONSTRAINT_RANGES, "-- (X) Uncertainties --", _SELECT_ALL_UNCS , _DESELECT_ALL_UNCS , "-- (L) Levers --", _SELECT_ALL_LEVERS , _DESELECT_ALL_LEVERS, "-- (M) Measures --", _SELECT_ALL_MEAS , _DESELECT_ALL_MEAS , ], description='View:', value=_NOTHING, ) self.select_menu.observe(self._on_select_menu, names='value') self.menus = HBox([ self.color_dim_choose, self.select_menu, ]) self.symbol_legend = widget.HTML(f""" {SYMBOL_MIMIMIZE} Performance Measure to Minimize<br> {SYMBOL_MAXIMIZE} Performance Measure to Maximize<br> {SYMBOL_INFOMEASURE} Performance Measure without preferred direction<br> {SYMBOL_LEVER} Policy Lever<br> {SYMBOL_UNCERTAINTY} Exogenous Uncertainty """) super().__init__( [ self.parcoords, self.menus, self.dim_choose, self.symbol_legend, ], layout=dict( align_items='center', ) ) def clear_all_constraint_ranges(self): """ Clear any constraint ranges across all dimensions. """ with self.parcoords.batch_update(): for dim in self.parcoords.data[0].dimensions: dim.constraintrange = None def _on_dim_choose_toggle(self, payload): for dim in self.parcoords.data[0].dimensions: if dim.name == payload['owner'].name: dim.visible = payload['new'] def _on_color_choose(self, payload): with self.out_logger: try: with self.parcoords.batch_update(): color_dim_name = payload['new'] if color_dim_name == '< None >': self.parcoords.data[0].line.color = 'rgb(200,0,0)' self.parcoords.data[0].line.showscale = False else: color_data = self.data[payload['new']] self.parcoords.data[0].line.color = color_data self.parcoords.data[0].line.showscale = True if self.scope is not None: self.parcoords.data[0].line.colorbar.title.text = self.scope.shortname(payload['new']) else: self.parcoords.data[0].line.colorbar.title.text = payload['new'] if color_data.dtype == numpy.bool_: self.parcoords.data[0].line.cmin = 0 self.parcoords.data[0].line.cmax = 1 else: self.parcoords.data[0].line.cmin = color_data.min() self.parcoords.data[0].line.cmax = color_data.max() except: import traceback traceback.print_exc() raise def _on_select_menu(self, payload): with self.out_logger: try: with self.parcoords.batch_update(): command_name = payload['new'] if command_name == _NOTHING: pass else: if command_name == _SELECT_ALL_MEAS: for meas in itertools.chain(self.scope.get_measure_names(), [_.name for _ in self.robustness_functions]): if meas in self.dim_activators_by_name: self.dim_activators_by_name[meas].value = True elif command_name == _DESELECT_ALL_MEAS: for meas in itertools.chain(self.scope.get_measure_names(), [_.name for _ in self.robustness_functions]): if meas in self.dim_activators_by_name: self.dim_activators_by_name[meas].value = False elif command_name == _SELECT_ALL_LEVERS: for meas in self.scope.get_lever_names(): if meas in self.dim_activators_by_name: self.dim_activators_by_name[meas].value = True elif command_name == _DESELECT_ALL_LEVERS: for meas in self.scope.get_lever_names(): if meas in self.dim_activators_by_name: self.dim_activators_by_name[meas].value = False elif command_name == _SELECT_ALL_UNCS: for meas in self.scope.get_uncertainty_names(): if meas in self.dim_activators_by_name: self.dim_activators_by_name[meas].value = True elif command_name == _DESELECT_ALL_UNCS: for meas in self.scope.get_uncertainty_names(): if meas in self.dim_activators_by_name: self.dim_activators_by_name[meas].value = False elif command_name == _CLEAR_CONSTRAINT_RANGES: self.clear_all_constraint_ranges() self.select_menu.value = _NOTHING except: import traceback traceback.print_exc() raise
class Map(ipyleaflet.Map): def __init__(self, **kwargs): # Change defaults kwargs.setdefault('center', [0, 0]) kwargs.setdefault('zoom', 2) super(Map, self).__init__(**kwargs) # self.added_geometries = {} # self.added_images = {} self.is_shown = False self.EELayers = {} # CREATE TABS self.tabs = Tab() tab_names = ['Inspector', 'Assets', 'Tasks'] ## widgets self.inspectorWid = Accordion() # Inspector Widget self.assetsWid = Accordion() # Assets Widget self.tasksWid = HTML() # Tasks Widget childrenName = ['Inspector', 'Assets', 'Tasks'] childrenWid = [self.inspectorWid, self.assetsWid, self.tasksWid] # Dictonary to hold tab's widgets # (tab's name:widget) self.childrenDict = OrderedDict(zip(childrenName, childrenWid)) # Set tabs children self.tabs.children = self.childrenDict.values() # Set tabs names for i, name in enumerate(tab_names): self.tabs.set_title(i, name) # Handlers self.tabs.observe(self.handle_change_tab) self.handlers = {'Inspector': self.handle_inspector} # First handler: Inspector self.on_interaction(self.handlers['Inspector']) @property def added_images(self): return sum( [1 for val in self.EELayers.values() if val['type'] == 'Image']) @property def added_geometries(self): return sum( [1 for val in self.EELayers.values() if val['type'] == 'Geometry']) def create_assets_tab(self): # ASSETS TAB # Get assets root rootid = ee.data.getAssetRoots()[0]['id'] assets_list = ee.data.getList({'id': rootid}) widlist = [] namelist = [] for asset in assets_list: wid = HTML('') widlist.append(wid) name = asset['id'].split('/')[-1] ty = asset['type'] namelist.append('{} ({})'.format(name, ty)) self.assetsWid.children = widlist for i, name in enumerate(namelist): self.assetsWid.set_title(i, name) def show(self, inspector=True): """ Show the Map on the Notebook """ if not self.is_shown: # Layers Control lc = ipyleaflet.LayersControl() self.add_control(lc) self.is_shown = True if inspector: # Create Assets Tab self.create_assets_tab() # Display display(self, self.tabs) else: display(self) elif inspector: display(self, self.tabs) else: display(self) def addLayer(self, eeObject, visParams=None, name=None, show=True, opacity=None, inspect={ 'data': None, 'reducer': None, 'scale': None }): """ Adds a given EE object to the map as a layer. :param eeObject: Earth Engine object to add to map :type eeObject: ee.Image || ee.Geometry || ee.Feature :param visParams: visualization parameters. For Images can have the following arguments: bands, min, max. :type visParams: dict :param name: name for the layer :type name: str :param inspect: when adding a geometry or a feature you can pop up data from a desired layer. Params are: :data: the EEObject where to get the data from :reducer: the reducer to use :scale: the scale to reduce :return: the added layer :rtype: """ def addImage(image, name): # Check if layer exists if name in self.EELayers.keys(): print("Layer with name {} exists already, please choose" + "another name".format(name)) return params = get_image_tile(image, visParams, show, opacity) layer = ipyleaflet.TileLayer(url=params['url'], attribution=params['attribution'], name=name) self.add_layer(layer) self.EELayers[name] = { 'type': 'Image', 'object': image, 'visParams': visParams, 'layer': layer } return layer def addGeoJson(geometry, name): # Check if layer exists if name in self.EELayers.keys(): print("Layer with name {} exists already, please choose" + "another name".format(name)) return params = get_geojson_tile(geometry, inspect) layer = ipyleaflet.GeoJSON(data=params['geojson'], name=name, popup=HTML(params['pop'])) self.add_layer(layer) self.EELayers[name] = { 'type': 'Geometry', 'object': geometry, 'visParams': None, 'layer': layer } return layer # CASE: ee.Image if isinstance(eeObject, ee.Image): thename = name if name else 'Image {}'.format(self.added_images) addImage(eeObject, thename) elif isinstance(eeObject, ee.Geometry): thename = name if name else 'Geometry {}'.format( self.added_geometries) addGeoJson(eeObject, thename) elif isinstance(eeObject, ee.Feature): geom = eeObject.geometry() addGeoJson(geom) elif isinstance(eeObject, ee.ImageCollection): proxy = eeObject.sort('system:time_start') mosaic = ee.Image(proxy.mosaic()) thename = name if name else 'Mosaic {}'.format(self.added_images) addImage(mosaic, thename) else: print("`addLayer` doesn't support adding the specified object to" "the map") def removeLayer(self, name): """ Remove a layer by its name """ if name in self.EELayers.keys(): layer = self.EELayers[name]['layer'] self.remove_layer(layer) self.EELayers.pop(name) else: print('Layer {} is not present in the map'.format(name)) return def centerObject(self, eeObject, zoom=None, method=1): """ Center an eeObject :param eeObject: :param zoom: :param method: experimetal methods to estimate zoom for fitting bounds Currently: 1 or 2 :type: int """ bounds = get_bounds(eeObject) if isinstance(eeObject, ee.Geometry): centroid = eeObject.centroid().getInfo()['coordinates'] elif isinstance(eeObject, ee.Feature) or isinstance( eeObject, ee.Image): centroid = eeObject.geometry().centroid().getInfo()['coordinates'] elif isinstance(eeObject, list): pol = ee.Geometry.Polygon(inverse_coordinates(list)) centroid = pol.centroid().getInfo()['coordinates'] self.center = inverse_coordinates(centroid) if zoom: self.zoom = zoom else: self.zoom = get_zoom(bounds, method) def getCenter(self): """ Returns the coordinates at the center of the map. No arguments. Returns: Geometry.Point :return: """ center = self.center coords = inverse_coordinates(center) return ee.Geometry.Point(coords) def getBounds(self, asGeoJSON=True): """ Returns the bounds of the current map view, as a list in the format [west, south, east, north] in degrees. Arguments: asGeoJSON (Boolean, optional): If true, returns map bounds as GeoJSON. Returns: GeoJSONGeometry|List<Number>|String """ bounds = inverse_coordinates(self.bounds) if asGeoJSON: return ee.Geometry.Rectangle(bounds) else: return bounds def addTab(self, name, handler, widget=None): """ Add a Tab to the Panel. The handler is for the Map :param name: name for the new tab :type name: str :param handler: handle function for the new tab. Arguments of the function are: :type: the type of the event (click, mouseover, etc..) :coordinates: coordinates where the event occured [lon, lat] :widget: the widget inside the Tab :map: the Map instance :param widget: widget inside the Tab. Defaults to HTML('') :type widget: ipywidgets.Widget """ # Widget wid = widget if widget else HTML('') # Get tab's children as a list tab_children = list(self.tabs.children) # Get a list of tab's titles titles = [ self.tabs.get_title(i) for i, child in enumerate(tab_children) ] # Check if tab already exists if name not in titles: ntabs = len(tab_children) # Add widget as a new children self.childrenDict[name] = wid tab_children.append(wid) # Overwrite tab's children self.tabs.children = tab_children # Set name of the new tab self.tabs.set_title(ntabs, name) # Set the handler for the new tab def proxy_handler(f): def wrap(**kwargs): # Add widget to handler arguments kwargs['widget'] = self.childrenDict[name] coords = kwargs['coordinates'] kwargs['coordinates'] = inverse_coordinates(coords) kwargs['map'] = self return f(**kwargs) return wrap self.handlers[name] = proxy_handler(handler) else: print('Tab {} already exists, please choose another name'.format( name)) def handle_change_tab(self, change): """ Handle function to trigger when tab changes """ # Remove all handlers if change['name'] == 'selected_index': old = change['old'] new = change['new'] old_name = self.tabs.get_title(old) new_name = self.tabs.get_title(new) # Remove all handlers for handl in self.handlers.values(): self.on_interaction(handl, True) # Set new handler if new_name in self.handlers.keys(): self.on_interaction(self.handlers[new_name]) def handle_inspector(self, **change): """ Handle function for the Inspector Widget """ # Get click coordinates coords = inverse_coordinates(change['coordinates']) event = change['type'] # event type if event == 'click': # If the user clicked # Clear children // Loading self.inspectorWid.children = [HTML('wait a second please..')] self.inspectorWid.set_title(0, 'Loading...') # create a point where the user clicked point = ee.Geometry.Point(coords) # First Accordion row text (name) first = 'Point {} at {} zoom'.format(coords, self.zoom) namelist = [first] wids4acc = [HTML('')] # first row has no content for name, obj in self.EELayers.items(): # for every added layer # name = obj['name'] # IMAGES if obj['type'] == 'Image': # Get the image's values image = obj['object'] values = tools.get_value(image, point, 10, 'client') values = tools.sort_dict(values) # Create the content img_html = '' for band, value in values.items(): img_html += '<b>{}</b>: {}</br>'.format(band, value) wid = HTML(img_html) # append widget to list of widgets wids4acc.append(wid) namelist.append(name) # GEOMETRIES elif obj['type'] == 'Geometry': geom = obj['object'] data = str(geom.getInfo()) wid = HTML(data) wids4acc.append(wid) namelist.append(name) # Set children and children's name of inspector widget self.inspectorWid.children = wids4acc for i, n in enumerate(namelist): self.inspectorWid.set_title(i, n)
def __init__( self, data, scope, robustness_functions=None, initial_max_active_measures = 5, ): self.data = data self.scope = scope self.robustness_functions = robustness_functions if self.robustness_functions is None: self.robustness_functions = () self.parcoords = parallel_coords( self.data, scope=self.scope, flip_dims=(), robustness_functions=robustness_functions, color_dim=0, colorscale='Viridis', title=None, ) self.dim_activators = [] self.dim_activators_by_name = {} self.out_logger = widget.Output() prefix_chars = _prefix_symbols(scope, robustness_functions) n_active_measures = 0 measure_names = set(self.scope.get_measure_names()) if robustness_functions is not None: measure_names |= set(rf.name for rf in robustness_functions) for i in self.data.columns: if i in self.scope.get_constant_names(): continue short_i = i if self.scope is not None: short_i = self.scope.shortname(i) i_value = True if i in measure_names: if n_active_measures >= initial_max_active_measures: i_value = False for dim in self.parcoords.data[0].dimensions: if dim.name == i: dim.visible = False else: n_active_measures += 1 cb = NamedCheckbox(description=prefix_chars.get(i,'')+short_i, value=i_value, name=i, description_tooltip=i) cb.observe(self._on_dim_choose_toggle, names='value') self.dim_activators.append(cb) self.dim_activators_by_name[i] = cb self.dim_choose = Accordion( children=[ widget.Box( self.dim_activators, layout=widget.Layout(flex_flow='row wrap') ) ], layout=widget.Layout(width='100%') ) self.dim_choose.set_title(0, 'Axes') self.dim_choose.selected_index = None self.color_dim_choose = widget.Dropdown( options=['< None >']+list(self.data.columns), description='Colorize:', value=self.data.columns[0], ) self.color_dim_choose.observe(self._on_color_choose, names='value') self.select_menu = widget.Dropdown( options=[ _NOTHING, _CLEAR_CONSTRAINT_RANGES, "-- (X) Uncertainties --", _SELECT_ALL_UNCS , _DESELECT_ALL_UNCS , "-- (L) Levers --", _SELECT_ALL_LEVERS , _DESELECT_ALL_LEVERS, "-- (M) Measures --", _SELECT_ALL_MEAS , _DESELECT_ALL_MEAS , ], description='View:', value=_NOTHING, ) self.select_menu.observe(self._on_select_menu, names='value') self.menus = HBox([ self.color_dim_choose, self.select_menu, ]) self.symbol_legend = widget.HTML(f""" {SYMBOL_MIMIMIZE} Performance Measure to Minimize<br> {SYMBOL_MAXIMIZE} Performance Measure to Maximize<br> {SYMBOL_INFOMEASURE} Performance Measure without preferred direction<br> {SYMBOL_LEVER} Policy Lever<br> {SYMBOL_UNCERTAINTY} Exogenous Uncertainty """) super().__init__( [ self.parcoords, self.menus, self.dim_choose, self.symbol_legend, ], layout=dict( align_items='center', ) )
def _widget(self): """ Create IPython widget for display within a notebook """ try: return self._cached_widget except AttributeError: pass try: from ipywidgets import Layout, VBox, HBox, IntText, Button, HTML, Accordion except ImportError: self._cached_widget = None return None layout = Layout(width="150px") if self.dashboard_link: dashboard_link = ( '<p><b>Dashboard: </b><a href="%s" target="_blank">%s</a></p>\n' % (self.dashboard_link, self.dashboard_link) ) else: dashboard_link = "" if self.jupyter_link: jupyter_link = ( '<p><b>Jupyter: </b><a href="%s" target="_blank">%s</a></p>\n' % (self.jupyter_link, self.jupyter_link) ) else: jupyter_link = "" title = "<h2>%s</h2>" % self._cluster_class_name title = HTML(title) dashboard = HTML(dashboard_link) jupyter = HTML(jupyter_link) status = HTML(self._widget_status(), layout=Layout(min_width="150px")) if self._supports_scaling: request = IntText( self.initial_node_count, description="Nodes", layout=layout ) scale = Button(description="Scale", layout=layout) minimum = IntText(0, description="Minimum", layout=layout) maximum = IntText(0, description="Maximum", layout=layout) adapt = Button(description="Adapt", layout=layout) accordion = Accordion( [HBox([request, scale]), HBox([minimum, maximum, adapt])], layout=Layout(min_width="500px"), ) accordion.selected_index = None accordion.set_title(0, "Manual Scaling") accordion.set_title(1, "Adaptive Scaling") def adapt_cb(b): self.adapt(minimum=minimum.value, maximum=maximum.value) update() adapt.on_click(adapt_cb) def scale_cb(b): with log_errors(): n = request.value with suppress(AttributeError): self._adaptive.stop() self.scale(n) update() scale.on_click(scale_cb) else: accordion = HTML("") box = VBox([title, HBox([status, accordion]), jupyter, dashboard]) self._cached_widget = box def update(): self.close_when_disconnect() status.value = self._widget_status() pc = PeriodicCallback(update, 500) # , io_loop=self.loop) self.periodic_callbacks["cluster-repr"] = pc pc.start() return box
def qa(): # path_plug = "cbm/foi/foi_db_func/" path_data = normpath(join(config.get_value(['paths', 'temp']), 'qa')) progress = Output() def outlog(*text): with progress: print(*text) qa_info = HTML( value="""The CbM Quality Assurance (QA) is a framework intended to enable the Member State (MS) to report to the Commission about the state of one of the components inside the control and management system.<br> To run the Quality Assessment (QA) procedures direct access to the database and the object storage with CARD data is required.<br> """, placeholder='QA Information', ) # Connect db_select = Dropdown(options=[db for db in config.get_value(['db'])], description='Configure:', disabled=True, layout=Layout(width='140px')) db_config = Button(value=False, disabled=False, button_style='info', tooltip='Configure db connection.', icon='cogs', layout=Layout(width='40px')) db_box = HBox([db_select, db_config]) db_conf_box = HBox([]) @db_config.on_click def db_config_on_click(b): if db_conf_box.children == (): db_conf_box.children = [settings_ds.direct_conn()] else: db_conf_box.children = () accor = Accordion(children=[ VBox([db_box, db_conf_box]), ext_func.upload_shp(path_data), ext_func.create_tables(), ext_card2db.dias_cat(), settings_ds.direct(), ext_func.extraction(), VBox([get_panel.get()], layout=Layout(border='1px solid black')), view_panel.view() ]) def on_tb_name(change): # Get to the widget that is inside the accordion widget # Any change of the order of the widgets within the widgets # box will break this connection. new_value = accor.children[1].children[3].children[0].value accor.children[2].children[1].value = new_value accor.children[1].children[3].children[0].observe(on_tb_name, 'value') def on_start(change): new_value = accor.children[3].children[2].children[0].value accor.children[5].children[0].children[0].value = new_value accor.children[3].children[2].children[0].observe(on_start, 'value') def on_end(change): new_value = accor.children[3].children[2].children[1].value accor.children[5].children[0].children[1].value = new_value accor.children[3].children[2].children[1].observe(on_end, 'value') accor.set_title(0, "1. Connect to database and object storage.") accor.set_title( 1, "2. Upload .shp, with all the required files (cpg, dbf, prj, shx).") accor.set_title(2, "3. Create the essential CbM tables.") accor.set_title( 3, "4. Add CARD metadata to database table 'xx_dias_catalogue'.") accor.set_title(4, "5. Configure database tables.") accor.set_title(5, "6. Run parcel extraction routines.") accor.set_title(6, "7. Get parcel information, time series and images.") accor.set_title(7, "8. View parcel data.") wbox = VBox([qa_info, accor]) return wbox
def _widget(self): """ Create IPython widget for display within a notebook """ try: return self._cached_widget except AttributeError: pass from ipywidgets import Layout, VBox, HBox, IntText, Button, HTML, Accordion layout = Layout(width='150px') if 'bokeh' in self.scheduler.services: template = config.get('diagnostics-link', 'http://{host}:{port}/status') host = self.scheduler.address.split('://')[1].split(':')[0] port = self.scheduler.services['bokeh'].port link = template.format(host=host, port=port, **os.environ) link = '<p><b>Dashboard: </b><a href="%s" target="_blank">%s</a></p>\n' % (link, link) else: link = '' title = '<h2>%s</h2>' % type(self).__name__ title = HTML(title) dashboard = HTML(link) status = HTML(self._widget_status(), layout=Layout(min_width='150px')) request = IntText(0, description='Workers', layout=layout) scale = Button(description='Scale', layout=layout) minimum = IntText(0, description='Minimum', layout=layout) maximum = IntText(0, description='Maximum', layout=layout) adapt = Button(description='Adapt', layout=layout) accordion = Accordion([HBox([request, scale]), HBox([minimum, maximum, adapt])], layout=Layout(min_width='500px')) accordion.selected_index = None accordion.set_title(0, 'Manual Scaling') accordion.set_title(1, 'Adaptive Scaling') box = VBox([title, HBox([status, accordion]), dashboard]) self._cached_widget = box def adapt_cb(b): self.adapt(minimum=minimum.value, maximum=maximum.value) adapt.on_click(adapt_cb) def scale_cb(b): with log_errors(): n = request.value with ignoring(AttributeError): self._adaptive.stop() self.scale(n) scale.on_click(scale_cb) scheduler_ref = ref(self.scheduler) def update(): status.value = self._widget_status() pc = PeriodicCallback(update, 500, io_loop=self.scheduler.loop) self.scheduler.periodic_callbacks['cluster-repr'] = pc pc.start() return box
def _build_instance_creation_widgets(self) -> Tuple[list, VBox]: """ Build widgets for creating instance and put them into their respective accordions :return: list of instance creation widgets (for easier manipulation and getting values from them), VBox (layout) to be displayed in a tab """ instance_widgets = [] style = {'description_width': '60%', 'width': 'auto'} # create widget for each variable for e, column in enumerate(self.feature_names): if e in self.categorical_names: # for categoricals make dropdown with name, but value as a number (from 0) options = { key: val for val, key in enumerate(self.categorical_names[e]) } instance_widgets.append( Dropdown(description=f"{column}:", options=options, style=style)) else: instance_widgets.append( FloatText(description=f"{column}: ", style=style)) # fill grid with widgets widgets_grid = GridspecLayout(ceil(self.X_train.shape[1] / 2), 2, layout=Layout(overflow='scroll', max_height='25em')) for e, item in enumerate(instance_widgets): widgets_grid[e // 2, e % 2] = item # add the selection of instance in train data fill_from_df = widgets.BoundedIntText( value=0, min=0, max=len(self.X_train) - 1, step=1, description='Instance id:', style=style, description_tooltip= 'When changed all values in "Build your own instance" will be set accordingly' ) # connect instance selection to builder widgets def on_value_change(change): instance = self.X_train[change['new']] for e, val in enumerate(instance): instance_widgets[e].value = val fill_from_df.observe(on_value_change, names='value') # importing instance from variable instance_variable = UpdatingCombobox( options_keys=self.kernel_globals.keys(), value='', description='Variable with instance:', description_tooltip= 'Variable with your instance as numpy array, pandas dataframe or series.\n' 'After clicking on "import instance", the values in "Build your own instance"' ' will be set accordingly.', style=style) def fill_from_variable(orig_btn): # param ignored instance = self._get_variable_from_kernel(instance_variable.value) try: for e, val in enumerate(instance): instance_widgets[e].value = val # reset to without error import_box.children = [instance_variable, import_btn] except: # add error to box import_box.children = [ instance_variable, import_btn, import_error ] import_error = HTML("Import from variable failed\n") import_btn = Button(description='Import') import_btn.on_click(fill_from_variable) import_box = VBox(children=(instance_variable, import_btn)) # put both into separate accordions to allow both opened at the same time accordion1 = Accordion(children=[fill_from_df, import_box]) accordion1.set_title(0, 'Select instance from dataset') accordion1.set_title(1, 'Import instance from variable') accordion2 = Accordion(children=[widgets_grid]) accordion2.set_title(0, 'Build your own instance') accordions = VBox([accordion1, accordion2]) # set everything to first instance on_value_change({'new': 0}) return instance_widgets, accordions
def __init__(self, **kwargs): desc = 'Select one or more layers' super(CustomInspector, self).__init__(description=desc, **kwargs) self.selector = SelectMultiple() self.main = Accordion() self.children = [self.selector, self.main]
def make_config(self): layout = Layout() style = {"description_width": "initial"} checkbox1 = Checkbox(description="Show Targets", value=self.net.config["show_targets"], layout=layout, style=style) checkbox1.observe(lambda change: self.set_attr(self.net.config, "show_targets", change["new"]), names='value') checkbox2 = Checkbox(description="Errors", value=self.net.config["show_errors"], layout=layout, style=style) checkbox2.observe(lambda change: self.set_attr(self.net.config, "show_errors", change["new"]), names='value') hspace = IntText(value=self.net.config["hspace"], description="Horizontal space between banks:", style=style, layout=layout) hspace.observe(lambda change: self.set_attr(self.net.config, "hspace", change["new"]), names='value') vspace = IntText(value=self.net.config["vspace"], description="Vertical space between layers:", style=style, layout=layout) vspace.observe(lambda change: self.set_attr(self.net.config, "vspace", change["new"]), names='value') self.feature_bank = Select(description="Features:", value=self.net.config["dashboard.features.bank"], options=[""] + [layer.name for layer in self.net.layers if self.net._layer_has_features(layer.name)], rows=1) self.feature_bank.observe(self.regenerate, names='value') self.control_select = Select( options=['Test', 'Train'], value=self.net.config["dashboard.dataset"], description='Dataset:', rows=1 ) self.control_select.observe(self.change_select, names='value') column1 = [self.control_select, self.zoom_slider, hspace, vspace, HBox([checkbox1, checkbox2]), self.feature_bank, self.feature_columns, self.feature_scale ] ## Make layer selectable, and update-able: column2 = [] layer = self.net.layers[-1] self.layer_select = Select(description="Layer:", value=layer.name, options=[layer.name for layer in self.net.layers], rows=1) self.layer_select.observe(self.update_layer_selection, names='value') column2.append(self.layer_select) self.layer_visible_checkbox = Checkbox(description="Visible", value=layer.visible, layout=layout) self.layer_visible_checkbox.observe(self.update_layer, names='value') column2.append(self.layer_visible_checkbox) self.layer_colormap = Select(description="Colormap:", options=[""] + AVAILABLE_COLORMAPS, value=layer.colormap if layer.colormap is not None else "", layout=layout, rows=1) self.layer_colormap_image = HTML(value="""<img src="%s"/>""" % self.net._image_to_uri(self.make_colormap_image(layer.colormap))) self.layer_colormap.observe(self.update_layer, names='value') column2.append(self.layer_colormap) column2.append(self.layer_colormap_image) ## get dynamic minmax; if you change it it will set it in layer as override: minmax = layer.get_act_minmax() self.layer_mindim = FloatText(description="Leftmost color maps to:", value=minmax[0], style=style) self.layer_maxdim = FloatText(description="Rightmost color maps to:", value=minmax[1], style=style) self.layer_mindim.observe(self.update_layer, names='value') self.layer_maxdim.observe(self.update_layer, names='value') column2.append(self.layer_mindim) column2.append(self.layer_maxdim) output_shape = layer.get_output_shape() self.layer_feature = IntText(value=layer.feature, description="Feature to show:", style=style) self.svg_rotate = Checkbox(description="Rotate", value=layer.visible, layout=layout) self.layer_feature.observe(self.update_layer, names='value') column2.append(self.layer_feature) self.svg_rotate = Checkbox(description="Rotate network", value=self.net.config["svg_rotate"], style={"description_width": 'initial'}, layout=Layout(width="52%")) self.svg_rotate.observe(lambda change: self.set_attr(self.net.config, "svg_rotate", change["new"]), names='value') self.save_config_button = Button(icon="save", layout=Layout(width="10%")) self.save_config_button.on_click(self.save_config) column2.append(HBox([self.svg_rotate, self.save_config_button])) config_children = HBox([VBox(column1, layout=Layout(width="100%")), VBox(column2, layout=Layout(width="100%"))]) accordion = Accordion(children=[config_children]) accordion.set_title(0, self.net.name) accordion.selected_index = None return accordion