def create_dropdown(name): dd = Dropdown(value=value, options=trait.values) def callback(dummy, value): setattr(obj, name, value) dd.on_trait_change(callback, 'value') items.append(dd)
def _field_folder(self, fields, **kwargs): folder = super(UniverseWidget, self)._field_folder(**kwargs) folder.deactivate('nx', 'ny', 'nz') fopts = Dropdown(options=fields) def _fopts(c): for scn in self.active(): scn.field_idx = c.new fopts.observe(_fopts, names='value') folder['fopts'] = fopts return folder
def _field_folder(self, **kwargs): """Folder that houses field GUI controls.""" folder = super(DemoContainer, self)._field_folder(**kwargs) fopts = Dropdown(options=['null', 'Sphere', 'Torus', 'Ellipsoid']) fopts.active = True fopts.disabled = False def _field(c): for scn in self.active(): scn.field = c.new fopts.observe(_field, names='value') folder.insert(1, 'options', fopts) return folder
def _make_repr_name_choices(self, component_slider, repr_slider): repr_choices = Dropdown(options=[" ",]) def on_chose(change): repr_name = change['new'] repr_index = repr_choices.options.index(repr_name) repr_slider.value = repr_index repr_choices.observe(on_chose, names='value') repr_choices.layout.width = default.DEFAULT_TEXT_WIDTH self.widget_repr_choices = repr_choices return self.widget_repr_choices
def __init__(self, atoms, xsize=500, ysize=500): import nglview from ipywidgets import Dropdown, FloatSlider, IntSlider, HBox, VBox self.atoms = atoms if isinstance(atoms[0], Atoms): # Assume this is a trajectory or struct list self.view = nglview.show_asetraj(atoms) self.frm = IntSlider(value=0, min=0, max=len(atoms) - 1) self.frm.observe(self._update_frame) self.struct = atoms[0] else: # Assume this is just a single structure self.view = nglview.show_ase(atoms) self.struct = atoms self.frm = None self.colors = {} self.view._remote_call('setSize', target='Widget', args=['%dpx' % (xsize,), '%dpx' % (ysize,)]) self.view.add_unitcell() self.view.add_spacefill() self.view.camera = 'orthographic' self.view.update_spacefill(radiusType='covalent', scale=0.7) self.view.center() self.asel = Dropdown(options=['All'] + list(set(self.struct.get_chemical_symbols())), value='All', description='Show') self.rad = FloatSlider(value=0.8, min=0.0, max=1.5, step=0.01, description='Ball size') self.asel.observe(self._select_atom) self.rad.observe(self._update_repr) wdg = [self.asel, self.rad] if self.frm: wdg.append(self.frm) self.gui = HBox([self.view, VBox(wdg)]) # Make useful shortcuts for the user of the class self.gui.view = self.view self.gui.control_box = self.gui.children[1] self.gui.custom_colors = self.custom_colors
def __init__(self, layer_state): self.state = layer_state self.widget_lighting = Checkbox(description='lighting', value=self.state.lighting) link((self.state, 'lighting'), (self.widget_lighting, 'value')) render_methods = 'NORMAL MAX_INTENSITY'.split() self.widget_render_method = Dropdown(options=render_methods, value=self.state.render_method, description='method') link((self.state, 'render_method'), (self.widget_render_method, 'value')) self.size_options = [32, 64, 128, 128 + 64, 256, 256 + 128, 512] options = [(str(k), k) for k in self.size_options] self.widget_max_resolution = Dropdown(options=options, value=128, description='max resolution') link((self.state, 'max_resolution'), (self.widget_max_resolution, 'value')) if self.state.vmin is None: self.state.vmin = 0 self.widget_data_min = FloatSlider(description='min', min=0, max=1, value=self.state.vmin, step=0.001) link((self.state, 'vmin'), (self.widget_data_min, 'value')) link((self.state, 'data_min'), (self.widget_data_min, 'min')) link((self.state, 'data_max'), (self.widget_data_min, 'max')) if self.state.vmax is None: self.state.vmax = 1 self.widget_data_max = FloatSlider(description='max', min=0, max=1, value=self.state.vmax, step=0.001) link((self.state, 'vmax'), (self.widget_data_max, 'value')) link((self.state, 'data_min'), (self.widget_data_max, 'min')) link((self.state, 'data_max'), (self.widget_data_max, 'max')) self.widget_clamp_min = Checkbox(description='clamp minimum', value=self.state.clamp_min) link((self.state, 'clamp_min'), (self.widget_clamp_min, 'value')) self.widget_clamp_max = Checkbox(description='clamp maximum', value=self.state.clamp_max) link((self.state, 'clamp_max'), (self.widget_clamp_max, 'value')) self.widget_color = ColorPicker(value=self.state.color, description='color') link((self.state, 'color'), (self.widget_color, 'value')) if self.state.alpha is None: self.state.alpha = 1 self.widget_opacity = FloatSlider(description='opacity', min=0, max=1, value=self.state.alpha, step=0.001) link((self.state, 'alpha'), (self.widget_opacity, 'value')) self.widget_opacity_scale = FloatLogSlider( description='opacity scale', base=10, min=-3, max=3, value=self.state.opacity_scale, step=0.01) link((self.state, 'opacity_scale'), (self.widget_opacity_scale, 'value')) # FIXME: this should be fixed # self.widget_reset_zoom = Button(description="Reset zoom") # self.widget_reset_zoom.on_click(self.state.viewer_state.reset_limits) super().__init__([ self.widget_render_method, self.widget_lighting, self.widget_data_min, self.widget_data_max, self.widget_clamp_min, self.widget_clamp_max, self.widget_max_resolution, # self.widget_reset_zoom, self.widget_color, self.widget_opacity, self.widget_opacity_scale ])
class FileChooser(VBox, ValueWidget): """FileChooser class.""" _LBL_TEMPLATE = '<span style="margin-left:10px; color:{1};">{0}</span>' _LBL_NOFILE = 'No file selected' def __init__(self, path=os.getcwd(), filename='', title='', width=300, select_desc='Select', change_desc='Change', show_hidden=False, select_default=False, use_dir_icons=False, show_only_dirs=False, filter_pattern=None, **kwargs): """Initialize FileChooser object.""" self._default_path = path.rstrip(os.path.sep) self._default_filename = filename self._selected_path = None self._selected_filename = None self._show_hidden = show_hidden self._select_desc = select_desc self._change_desc = change_desc self._callback = None self._select_default = select_default self._use_dir_icons = use_dir_icons self._show_only_dirs = show_only_dirs self._filter_pattern = filter_pattern # Widgets self._pathlist = Dropdown(description="", layout=Layout(width='auto', grid_area='pathlist')) self._filename = Text( placeholder='output filename', layout=Layout(width='auto', grid_area='filename', display=(None, "none")[self._show_only_dirs]), disabled=self._show_only_dirs) self._dircontent = Select(rows=8, layout=Layout(width='auto', grid_area='dircontent')) self._cancel = Button(description='Cancel', layout=Layout(width='auto', display='none')) self._select = Button(description=self._select_desc, layout=Layout(width='auto')) self._title = HTML(value=title) if title == '': self._title.layout.display = 'none' # Widget observe handlers self._pathlist.observe(self._on_pathlist_select, names='value') self._dircontent.observe(self._on_dircontent_select, names='value') self._filename.observe(self._on_filename_change, names='value') self._select.on_click(self._on_select_click) self._cancel.on_click(self._on_cancel_click) # Selected file label self._label = HTML(value=self._LBL_TEMPLATE.format( self._LBL_NOFILE, 'black'), placeholder='', description='') # Layout self._gb = VBox( children=[self._pathlist, self._filename, self._dircontent], layout=Layout(width=f'{width}px')) # self._gb = GridBox( # children=[ # self._pathlist, # self._filename, # self._dircontent # ], # layout=Layout( # display='none', # width=f'{width}px', # grid_gap='0px 0px', # grid_template_rows='auto auto', # grid_template_columns='60% 40%', # grid_template_areas=''' # 'pathlist {}' # 'dircontent dircontent' # '''.format(('filename', 'pathlist')[self._show_only_dirs]) # ) # ) # buttonbar = HBox( # children=[ # self._select, # self._cancel, # self._label # ], # layout=Layout(width='auto') # ) buttonbar = VBox(children=[self._select, self._cancel, self._label], layout=Layout(width='auto')) # Call setter to set initial form values self._set_form_values(self._default_path, self._default_filename) # Use the defaults as the selected values if self._select_default: self._apply_selection() # Call VBox super class __init__ super().__init__(children=[ self._title, self._gb, buttonbar, ], layout=Layout(width='auto'), **kwargs) def _set_form_values(self, path, filename): """Set the form values.""" # Disable triggers to prevent selecting an entry in the Select # box from automatically triggering a new event. self._pathlist.unobserve(self._on_pathlist_select, names='value') self._dircontent.unobserve(self._on_dircontent_select, names='value') self._filename.unobserve(self._on_filename_change, names='value') # In folder only mode zero out the filename if self._show_only_dirs: filename = '' # Set form values self._pathlist.options = get_subpaths(path) self._pathlist.value = path self._filename.value = filename # file/folder real names dircontent_real_names = get_dir_contents( path, show_hidden=self._show_hidden, prepend_icons=False, show_only_dirs=self._show_only_dirs, filter_pattern=self._filter_pattern) # file/folder display names dircontent_display_names = get_dir_contents( path, show_hidden=self._show_hidden, prepend_icons=self._use_dir_icons, show_only_dirs=self._show_only_dirs, filter_pattern=self._filter_pattern) # Dict to map real names to display names self._map_name_to_disp = { real_name: disp_name for real_name, disp_name in zip(dircontent_real_names, dircontent_display_names) } # Dict to map display names to real names self._map_disp_to_name = dict( reversed(item) for item in self._map_name_to_disp.items()) # Set _dircontent form value to display names self._dircontent.options = dircontent_display_names # If the value in the filename Text box equals a value in the # Select box and the entry is a file then select the entry. if ((filename in dircontent_real_names) and os.path.isfile(os.path.join(path, filename))): self._dircontent.value = self._map_name_to_disp[filename] else: self._dircontent.value = None # Reenable triggers again self._pathlist.observe(self._on_pathlist_select, names='value') self._dircontent.observe(self._on_dircontent_select, names='value') self._filename.observe(self._on_filename_change, names='value') # Update the state of the select button if self._gb.layout.display is None: # Disable the select button if path and filename # - equal an existing folder in the current view # - equal the already selected values # - don't match the provided filter pattern(s) check1 = filename in dircontent_real_names check2 = os.path.isdir(os.path.join(path, filename)) check3 = False check4 = False # Only check selected if selected is set if ((self._selected_path is not None) and (self._selected_filename is not None)): selected = os.path.join(self._selected_path, self._selected_filename) check3 = os.path.join(path, filename) == selected # Ensure only allowed extensions are used if self._filter_pattern: check4 = not match_item(filename, self._filter_pattern) if (check1 and check2) or check3 or check4: self._select.disabled = True else: self._select.disabled = False def _on_pathlist_select(self, change): """Handle selecting a path entry.""" self._set_form_values(change['new'], self._filename.value) def _on_dircontent_select(self, change): """Handle selecting a folder entry.""" new_path = os.path.realpath( os.path.join(self._pathlist.value, self._map_disp_to_name[change['new']])) # Check if folder or file if os.path.isdir(new_path): path = new_path filename = self._filename.value elif os.path.isfile(new_path): path = self._pathlist.value filename = self._map_disp_to_name[change['new']] self._set_form_values(path, filename) def _on_filename_change(self, change): """Handle filename field changes.""" self._set_form_values(self._pathlist.value, change['new']) def _on_select_click(self, _b): """Handle select button clicks.""" if self._gb.layout.display == 'none': # If not shown, open the dialog self._show_dialog() else: # If shown, close the dialog and apply the selection self._apply_selection() # Execute callback function if self._callback is not None: try: self._callback(self) except TypeError: # Support previous behaviour of not passing self self._callback() def _show_dialog(self): """Show the dialog.""" # Show dialog and cancel button self._gb.layout.display = None self._cancel.layout.display = None # Show the form with the correct path and filename if ((self._selected_path is not None) and (self._selected_filename is not None)): path = self._selected_path filename = self._selected_filename else: path = self._default_path filename = self._default_filename self._set_form_values(path, filename) def _apply_selection(self): """Close the dialog and apply the selection.""" self._gb.layout.display = 'none' self._cancel.layout.display = 'none' self._select.description = self._change_desc self._selected_path = self._pathlist.value self._selected_filename = self._filename.value selected = os.path.join(self._selected_path, self._selected_filename) if os.path.isfile(selected): self._label.value = self._LBL_TEMPLATE.format(selected, 'orange') else: self._label.value = self._LBL_TEMPLATE.format(selected, 'green') def _on_cancel_click(self, _b): """Handle cancel button clicks.""" self._gb.layout.display = 'none' self._cancel.layout.display = 'none' self._select.disabled = False def reset(self, path=None, filename=None): """Reset the form to the default path and filename.""" self._selected_path = None self._selected_filename = None # Reset select button and label self._select.description = self._select_desc self._label.value = self._LBL_TEMPLATE.format(self._LBL_NOFILE, 'black') if path is not None: self._default_path = path.rstrip(os.path.sep) if filename is not None: self._default_filename = filename # Set a proper filename value if self._show_only_dirs: filename = '' else: filename = self._default_filename self._set_form_values(self._default_path, filename) # Use the defaults as the selected values if self._select_default: self._apply_selection() def refresh(self): """Re-render the form.""" self._set_form_values(self._pathlist.value, self._filename.value) @property def show_hidden(self): """Get _show_hidden value.""" return self._show_hidden @show_hidden.setter def show_hidden(self, hidden): """Set _show_hidden value.""" self._show_hidden = hidden self.refresh() @property def use_dir_icons(self): """Get _use_dir_icons value.""" return self._use_dir_icons @use_dir_icons.setter def use_dir_icons(self, dir_icons): """Set _use_dir_icons value.""" self._use_dir_icons = dir_icons self.refresh() @property def rows(self): """Get current number of rows.""" return self._dircontent.rows @rows.setter def rows(self, rows): """Set number of rows.""" self._dircontent.rows = rows @property def title(self): """Get the title.""" return self._title.value @title.setter def title(self, title): """Set the title.""" self._title.value = title if title == '': self._title.layout.display = 'none' else: self._title.layout.display = None @property def default(self): """Get the default value.""" return os.path.join(self._default_path, self._default_filename) @property def default_path(self): """Get the default_path value.""" return self._default_path @default_path.setter def default_path(self, path): """Set the default_path.""" self._default_path = path.rstrip(os.path.sep) self._set_form_values(self._default_path, self._filename.value) @property def default_filename(self): """Get the default_filename value.""" return self._default_filename @default_filename.setter def default_filename(self, filename): """Set the default_filename.""" self._default_filename = filename self._set_form_values(self._pathlist.value, self._default_filename) @property def show_only_dirs(self): """Get show_only_dirs property value.""" return self._show_only_dirs @show_only_dirs.setter def show_only_dirs(self, show_only_dirs): """Set show_only_dirs property value.""" self._show_only_dirs = show_only_dirs # Update widget layout self._filename.disabled = self._show_only_dirs self._filename.layout.display = (None, "none")[self._show_only_dirs] self._gb.layout.children = [self._pathlist, self._dircontent] if not self._show_only_dirs: self._gb.layout.children.insert(1, self._filename) self._gb.layout.grid_template_areas = ''' 'pathlist {}' 'dircontent dircontent' '''.format(('filename', 'pathlist')[self._show_only_dirs]) # Reset the dialog self.reset() @property def filter_pattern(self): """Get file name filter pattern.""" return self._filter_pattern @filter_pattern.setter def filter_pattern(self, filter_pattern): """Set file name filter pattern.""" self._filter_pattern = filter_pattern self.refresh() @property def selected(self): """Get selected value.""" try: return os.path.join(self._selected_path, self._selected_filename) except TypeError: return None @property def selected_path(self): """Get selected_path value.""" return self._selected_path @property def selected_filename(self): """Get the selected_filename.""" return self._selected_filename def __repr__(self): """Build string representation.""" str_ = ("FileChooser(" "path='{0}', " "filename='{1}', " "title='{2}', " "show_hidden='{3}', " "use_dir_icons='{4}', " "show_only_dirs='{5}', " "select_desc='{6}', " "change_desc='{7}')").format( self._default_path, self._default_filename, self._title, self._show_hidden, self._use_dir_icons, self._show_only_dirs, self._select_desc, self._change_desc) return str_ def register_callback(self, callback): """Register a callback function.""" self._callback = callback def get_interact_value(self): """Return the value which should be passed to interactive functions.""" return self.selected
def dsc_config(dsc_value): values = config.read() ds_db = Dropdown( options=list(values['db'].keys()), value="main", description='Database:', disabled=False, layout=Layout(width='200px') ) try: with open(normpath(join(f"{values['paths']['temp']}", 'tb_prefix')), 'r') as f: code_value = f.read().split('_')[0] except Exception: code_value = dsc_value.split('_')[0] ds_code = Combobox( value=code_value, placeholder='abc', options=[m for m in data_options.eu_ms()] + [''], description='AOI code:', ensure_option=False, disabled=False, layout=Layout(width='200px'), tooltip='Lowercase AOI code name for the dataset (5chr max).' ) ds_year = BoundedIntText( value=int(dsc_value.split('_')[1]), min=1980, max=2100, step=1, description='Dataset year:', disabled=False, layout=Layout(width='180px') ) ds_desc = Text( value=values['dataset'][dsc_value]['description'], description='Description:', disabled=False ) info_map_text = ["Set default map view options. ", "You can get automatically the dataset ", "center coordinates."] lat, lon = values['dataset'][dsc_value]['center'].split(",") map_cent_lat = FloatText( value=float(lat), description='Lat:', disabled=False, layout=Layout(width='160px') ) map_cent_lon = FloatText( value=float(lon), description='Lon:', disabled=False, layout=Layout(width='160px') ) map_zoom = BoundedIntText( value=values['dataset'][dsc_value]['zoom'], min=0, max=20, step=1, description='Zoom:', disabled=False, layout=Layout(width='140px') ) bt_get_center = Button( layout=Layout(width='40px'), icon='bullseye', tooltip='Get center point from database.' ) ds_box = HBox([ds_code, ds_year, ds_desc, ds_db]) map_box = HBox([Label("Map center: "), map_cent_lat, map_cent_lon, bt_get_center, map_zoom]) info_config = Label( """Change 'AOI code' or 'Year' value to create a new configuration set or leave the same 'AOI code' value to configure the selected one.""") db_set = values['dataset'][dsc_value]['db'] def get_tb_list(): tbls = db.tables(db_set, None, False) if tbls is None: return [] else: return tbls tb_dc = Dropdown( options=get_tb_list(), value=config.autoselect( values['dataset'][dsc_value]['tables']['dias_catalog'], get_tb_list(), False), description='DIAS catalog:', disabled=False ) tb_pr = Dropdown( options=get_tb_list(), value=config.autoselect( values['dataset'][dsc_value]['tables']['parcels'], get_tb_list(), False), description='Parcels:', disabled=False ) def get_pr_columns(): try: colms = db.table_columns(tb_pr.value, ds_db.value, None) if colms is None: return [] else: return colms except Exception: return [] tc_id = Dropdown( options=get_pr_columns(), value=config.autoselect( values['dataset'][dsc_value]['pcolumns']['parcels_id'], get_pr_columns(), False), description='Parcels ID:', disabled=False, layout=Layout(width='180px') ) tc_cn = Dropdown( options=get_pr_columns(), value=config.autoselect( values['dataset'][dsc_value]['pcolumns']['crop_names'], get_pr_columns(), False), description='Crop names:', disabled=False, layout=Layout(width='180px') ) tc_cc = Dropdown( options=get_pr_columns(), value=config.autoselect( values['dataset'][dsc_value]['pcolumns']['crop_codes'], get_pr_columns(), False), description='Crop codes:', disabled=False, layout=Layout(width='180px') ) def on_tb_pr_change(change): tc_id.options = get_pr_columns() tc_cn.options = get_pr_columns() tc_cc.options = get_pr_columns() tb_pr.observe(on_tb_pr_change, 'value') tb_s2 = Dropdown( options=get_tb_list(), value=config.autoselect( values['dataset'][dsc_value]['tables']['s2'], get_tb_list(), False), description='S2 signatures:', disabled=False ) tb_bs = Dropdown( options=get_tb_list(), value=config.autoselect( values['dataset'][dsc_value]['tables']['bs'], get_tb_list(), False), description='Backscattering:', disabled=False ) tb_6c = Dropdown( options=get_tb_list(), value=config.autoselect( values['dataset'][dsc_value]['tables']['c6'], get_tb_list(), False), description='6 day coherence:', disabled=False ) wb_save = Button( description='Save', disabled=False, icon='save' ) @bt_get_center.on_click def bt_get_center_on_click(b): import json center_json = json.loads( db_queries.getTableCentroid(tb_pr.value)['center'][0]) map_cent_lat.value = round(center_json['coordinates'][1], 2) map_cent_lon.value = round(center_json['coordinates'][0], 2) map_zoom.value = 10 @wb_save.on_click def wb_save_on_click(b): progress.clear_output() dscode = ds_code.value config.set_value(['dataset', f'{dscode}_{str(ds_year.value)}', 'tables', 'dias_catalog'], str(tb_dc.value)) config.set_value(['dataset', f'{dscode}_{str(ds_year.value)}', 'tables', 'parcels'], str(tb_pr.value)) config.set_value(['dataset', f'{dscode}_{str(ds_year.value)}', 'pcolumns', 'parcels_id'], str(tc_id.value)) config.set_value(['dataset', f'{dscode}_{str(ds_year.value)}', 'pcolumns', 'crop_names'], str(tc_cn.value)) config.set_value(['dataset', f'{dscode}_{str(ds_year.value)}', 'pcolumns', 'crop_codes'], str(tc_cc.value)) config.set_value(['dataset', f'{dscode}_{str(ds_year.value)}', 'tables', 's2'], str(tb_s2.value)) config.set_value(['dataset', f'{dscode}_{str(ds_year.value)}', 'tables', 'bs'], str(tb_bs.value)) config.set_value(['dataset', f'{dscode}_{str(ds_year.value)}', 'tables', 'c6'], str(tb_6c.value)) config.set_value(['dataset', f'{dscode}_{str(ds_year.value)}', 'db'], str(ds_db.value)) config.set_value(['dataset', f'{dscode}_{str(ds_year.value)}', 'description'], str(ds_desc.value)) config.set_value( ['dataset', f'{dscode}_{str(ds_year.value)}', 'center'], f"{map_cent_lat.value},{map_cent_lon.value}") config.set_value(['dataset', f'{dscode}_{str(ds_year.value)}', 'zoom'], str(map_zoom.value)) config.set_value(['set', 'dataset'], f'{dscode}_{str(ds_year.value)}') config.set_value(['set', 'ds_year'], str(ds_year.value)) values = config.read() ds_c = values['set']['dataset'] dsc.options = [d for d in values['dataset']] dsc.value = ds_c outlog("The configurations are saved.") db_box = HBox([VBox([Label('Tables:'), tb_pr, tb_dc, tb_s2, tb_bs, tb_6c]), VBox([Label('Columns:'), HBox([tc_id, tc_cn, tc_cc])])]) return VBox([info_config, ds_box, db_box, Label(''.join(info_map_text)), map_box, wb_save])
class TrajectoryPlayer(DOMWidget): # should set default values here different from desired defaults # so `observe` can be triggered step = Int(0).tag(sync=True) sync_frame = Bool(True).tag(sync=True) interpolate = Bool(False).tag(sync=False) delay = Float(0.0).tag(sync=True) parameters = Dict().tag(sync=True) iparams = Dict().tag(sync=False) _interpolation_t = Float().tag(sync=False) _iterpolation_type = CaselessStrEnum(['linear', 'spline']).tag(sync=False) spin = Bool(False).tag(sync=False) _spin_x = Int(1).tag(sync=False) _spin_y = Int(0).tag(sync=False) _spin_z = Int(0).tag(sync=False) _spin_speed = Float(0.005).tag(sync=False) camera = CaselessStrEnum(['perspective', 'orthographic'], default_value='perspective').tag(sync=False) _render_params = Dict().tag(sync=False) _real_time_update = Bool(False).tag(sync=False) widget_tab = Any(None).tag(sync=False) widget_repr = Any(None).tag(sync=False) widget_repr_parameters = Any(None).tag(sync=False) widget_quick_repr = Any(None).tag(sync=False) widget_general = Any(None).tag(sync=False) widget_picked = Any(None).tag(sync=False) widget_preference = Any(None).tag(sync=False) widget_extra = Any(None).tag(sync=False) widget_theme = Any(None).tag(sync=False) widget_help = Any(None).tag(sync=False) widget_export_image = Any(None).tag(sync=False) widget_component_slider = Any(None).tag(sync=False) widget_repr_slider = Any(None).tag(sync=False) widget_repr_choices = Any(None).tag(sync=False) widget_repr_control_buttons = Any(None).tag(sync=False) widget_repr_add = Any(None).tag(sync=False) widget_accordion_repr_parameters = Any(None).tag(sync=False) widget_repr_parameters_dialog = Any(None).tag(sync=False) widget_repr_name = Any(None).tag(sync=False) widget_component_dropdown = Any(None).tag(sync=False) widget_drag = Any(None).tag(sync=False) def __init__(self, view, step=1, delay=100, sync_frame=False, min_delay=40): self._view = view self.step = step self.sync_frame = sync_frame self.delay = delay self.min_delay = min_delay self._interpolation_t = 0.5 self._iterpolation_type = 'linear' self.iparams = dict( t=self._interpolation_t, step=1, type=self._iterpolation_type) self._render_params = dict(factor=4, antialias=True, trim=False, transparent=False) self._widget_names = [w for w in dir(self) if w.startswith('wiget_')] self.observe(self._on_widget_built, names=['widget_repr_parameters', 'widget_repr', 'widget_preference']) self._movie_maker = None def _on_widget_built(self, change): widget = change['new'] if widget is not None: widget.layout.padding = '5%' def _update_padding(self, padding=default.DEFAULT_PADDING): widget_collection = [ self.widget_general, self.widget_repr, self.widget_preference, self.widget_repr_parameters, self.widget_help, self.widget_extra, self.widget_picked ] for widget in widget_collection: if widget is not None: widget.layout.padding = padding def _create_all_widgets(self): if self.widget_tab is None: self.widget_tab = self._display() old_index = self.widget_tab.selected_index for index, _ in enumerate(self.widget_tab.children): self.widget_tab.selected_index = index self.widget_tab.selected_index = old_index def smooth(self): self.interpolate = True @observe('camera') def on_camera_changed(self, change): camera_type = change['new'] self._view._remote_call("setParameters", target='Stage', kwargs=dict(cameraType=camera_type)) @property def frame(self): return self._view.frame @frame.setter def frame(self, value): self._view.frame = value @property def count(self): return self._view.count @observe('sync_frame') def update_sync_frame(self, change): value = change['new'] if value: self._view._set_sync_frame() else: self._view._set_unsync_frame() @observe("delay") def update_delay(self, change): delay = change['new'] self._view._set_delay(delay) @observe('parameters') def update_parameters(self, change): params = change['new'] self.sync_frame = params.get("sync_frame", self.sync_frame) self.delay = params.get("delay", self.delay) self.step = params.get("step", self.step) @observe('_interpolation_t') def _interpolation_t_changed(self, change): self.iparams['t'] = change['new'] @observe('spin') def on_spin_changed(self, change): self.spin = change['new'] if self.spin: self._view._set_spin([self._spin_x, self._spin_y, self._spin_z], self._spin_speed) else: # stop self._view._set_spin(None, None) @observe('_spin_x') def on_spin_x_changed(self, change): self._spin_x = change['new'] if self.spin: self._view._set_spin([self._spin_x, self._spin_y, self._spin_z], self._spin_speed) @observe('_spin_y') def on_spin_y_changed(self, change): self._spin_y = change['new'] if self.spin: self._view._set_spin([self._spin_x, self._spin_y, self._spin_z], self._spin_speed) @observe('_spin_z') def on_spin_z_changed(self, change): self._spin_z = change['new'] if self.spin: self._view._set_spin([self._spin_x, self._spin_y, self._spin_z], self._spin_speed) @observe('_spin_speed') def on_spin_speed_changed(self, change): self._spin_speed = change['new'] if self.spin: self._view._set_spin([self._spin_x, self._spin_y, self._spin_z], self._spin_speed) def _display(self): box_factory = [(self._make_general_box, 'General'), (self._make_widget_repr, 'Representation'), (self._make_widget_preference, 'Preference'), (self._make_theme_box, 'Theme'), (self._make_extra_box, 'Extra'), (self._show_website, 'Help')] tab = _make_delay_tab(box_factory, selected_index=-1) # tab = _make_autofit(tab) tab.layout.align_self = 'center' tab.layout.align_items = 'stretch' self.widget_tab = tab return self.widget_tab def _make_widget_tab(self): return self._display() def _make_button_center(self): button = Button(description=' Center', icon='fa-bullseye') @button.on_click def on_click(button): self._view.center() return button def _make_button_theme(self): button = Button(description='Oceans16') @button.on_click def on_click(button): from nglview import theme display(theme.oceans16()) self._view._remote_call('cleanOutput', target='Widget') return button def _make_button_reset_theme(self, hide_toolbar=False): from nglview import theme if hide_toolbar: button = Button(description='Simplified Default') @button.on_click def on_click(button): theme.reset(hide_toolbar=True) else: button = Button(description='Default') @button.on_click def on_click(button): theme.reset() return button def _make_button_clean_error_output(self): button = Button(description='Clear Error') @button.on_click def on_click(_): js_utils.clean_error_output() return button def _make_widget_preference(self, width='100%'): def make_func(): parameters = self._view._full_stage_parameters def func(pan_speed=parameters.get('panSpeed', 0.8), rotate_speed=parameters.get('rotateSpeed', 2), zoom_speed=parameters.get('zoomSpeed', 1.2), clip_dist=parameters.get('clipDist', 10), camera_fov=parameters.get('cameraFov', 40), clip_far=parameters.get('clipFar', 100), clip_near=parameters.get('clipNear', 0), fog_far=parameters.get('fogFar', 100), fog_near=parameters.get('fogNear', 50), impostor=parameters.get('impostor', True), light_intensity=parameters.get('lightIntensity', 1), quality=parameters.get('quality', 'medium'), sample_level=parameters.get('sampleLevel', 1)): self._view.parameters = dict( panSpeed=pan_speed, rotateSpeed=rotate_speed, zoomSpeed=zoom_speed, clipDist=clip_dist, clipFar=clip_far, clipNear=clip_near, cameraFov=camera_fov, fogFar=fog_far, fogNear=fog_near, impostor=impostor, lightIntensity=light_intensity, quality=quality, sampleLevel=sample_level) return func def make_widget_box(): widget_sliders = interactive(make_func(), pan_speed=(0, 10, 0.1), rotate_speed=(0, 10, 1), zoom_speed=(0, 10, 1), clip_dist=(0, 200, 5), clip_far=(0, 100, 1), clip_near=(0, 100, 1), camera_fov=(15, 120, 1), fog_far=(0, 100, 1), fog_near=(0, 100, 1), light_intensity=(0, 10, 0.02), quality=['low', 'medium', 'high'], sample_level=(-1, 5, 1)) for child in widget_sliders.children: if isinstance(child, (IntSlider, FloatSlider)): child.layout.width = default.DEFAULT_SLIDER_WIDTH return widget_sliders if self.widget_preference is None: widget_sliders = make_widget_box() reset_button = Button(description='Reset') widget_sliders.children = [reset_button,] + list(widget_sliders.children) @reset_button.on_click def on_click(reset_button): self._view.parameters = self._view._original_stage_parameters self._view._full_stage_parameters = self._view._original_stage_parameters widget_sliders.children = [reset_button,] + list(make_widget_box().children) self.widget_preference = _relayout_master(widget_sliders, width=width) return self.widget_preference def _show_download_image(self): # "interactive" does not work for True/False in ipywidgets 4 yet. button = Button(description=' Screenshot', icon='fa-camera') @button.on_click def on_click(button): self._view.download_image() return button def _make_button_url(self, url, description): button = Button(description=description) @button.on_click def on_click(button): display(Javascript(js_utils.open_url_template.format(url=url))) return button def _show_website(self, ngl_base_url=default.NGL_BASE_URL): buttons = [self._make_button_url(url.format(ngl_base_url), description) for url, description in [("'http://arose.github.io/nglview/latest/'", "nglview"), ("'{}/index.html'", "NGL"), ("'{}/tutorial-selection-language.html'", "Selection"), ("'{}/tutorial-molecular-representations.html'", "Representation")] ] self.widget_help = _make_autofit(HBox(buttons)) return self.widget_help def _make_button_qtconsole(self): from nglview import js_utils button = Button(description='qtconsole', tooltip='pop up qtconsole') @button.on_click def on_click(button): js_utils.launch_qtconsole() return button def _make_text_picked(self): ta = Textarea(value=json.dumps(self._view.picked), description='Picked atom') ta.layout.width = '300px' return ta def _refresh(self, component_slider, repr_slider): """update representation and component information """ self._view._request_repr_parameters(component=component_slider.value, repr_index=repr_slider.value) self._view._remote_call('requestReprInfo', target='Widget') self._view._handle_repr_dict_changed(change=dict(new=self._view._repr_dict)) def _make_button_repr_control(self, component_slider, repr_slider, repr_selection): button_refresh = Button(description=' Refresh', tooltip='Get representation info', icon='fa-refresh') button_center_selection = Button(description=' Center', tooltip='center selected atoms', icon='fa-bullseye') button_center_selection._ngl_name = 'button_center_selection' button_hide = Button(description=' Hide', icon='fa-eye-slash', tooltip='Hide/Show current representation') button_remove = Button(description=' Remove', icon='fa-trash', tooltip='Remove current representation') button_repr_parameter_dialog = Button(description=' Dialog', tooltip='Pop up representation parameters control dialog') @button_refresh.on_click def on_click_refresh(button): self._refresh(component_slider, repr_slider) @button_center_selection.on_click def on_click_center(center_selection): self._view.center_view(selection=repr_selection.value, component=component_slider.value) @button_hide.on_click def on_click_hide(button_hide): component=component_slider.value repr_index=repr_slider.value if button_hide.description == 'Hide': hide = True button_hide.description = 'Show' else: hide = False button_hide.description = 'Hide' self._view._remote_call('setVisibilityForRepr', target='Widget', args=[component, repr_index, not hide]) @button_remove.on_click def on_click_remove(button_remove): self._view._remove_representation(component=component_slider.value, repr_index=repr_slider.value) self._view._request_repr_parameters(component=component_slider.value, repr_index=repr_slider.value) @button_repr_parameter_dialog.on_click def on_click_repr_dialog(_): from nglview.widget_box import DraggableBox if self.widget_repr_parameters is not None and self.widget_repr_choices: self.widget_repr_parameters_dialog = DraggableBox([self.widget_repr_choices, self.widget_repr_parameters]) self.widget_repr_parameters_dialog._ipython_display_() self.widget_repr_parameters_dialog._dialog = 'on' bbox = _make_autofit(HBox([button_refresh, button_center_selection, button_hide, button_remove, button_repr_parameter_dialog])) return bbox def _make_widget_repr(self): self.widget_repr_name = Text(value='', description='representation') self.widget_repr_name._ngl_name = 'repr_name_text' repr_selection = Text(value=' ', description='selection') repr_selection._ngl_name = 'repr_selection' repr_selection.width = self.widget_repr_name.width = default.DEFAULT_TEXT_WIDTH max_n_components = max(self._view.n_components-1, 0) self.widget_component_slider = IntSlider(value=0, max=max_n_components, min=0, description='component') self.widget_component_slider._ngl_name = 'component_slider' cvalue = ' ' self.widget_component_dropdown = Dropdown(value=cvalue, options=[cvalue,], description='component') self.widget_component_dropdown._ngl_name = 'component_dropdown' self.widget_repr_slider = IntSlider(value=0, description='representation', width=default.DEFAULT_SLIDER_WIDTH) self.widget_repr_slider._ngl_name = 'repr_slider' self.widget_repr_slider.visible = True self.widget_component_slider.layout.width = default.DEFAULT_SLIDER_WIDTH self.widget_repr_slider.layout.width = default.DEFAULT_SLIDER_WIDTH self.widget_component_dropdown.layout.width = self.widget_component_dropdown.max_width = default.DEFAULT_TEXT_WIDTH # turn off for now self.widget_component_dropdown.layout.display = 'none' self.widget_component_dropdown.description = '' # self.widget_accordion_repr_parameters = Accordion() self.widget_accordion_repr_parameters = Tab() self.widget_repr_parameters = self._make_widget_repr_parameters(self.widget_component_slider, self.widget_repr_slider, self.widget_repr_name) self.widget_accordion_repr_parameters.children = [self.widget_repr_parameters, Box()] self.widget_accordion_repr_parameters.set_title(0, 'Parameters') self.widget_accordion_repr_parameters.set_title(1, 'Hide') self.widget_accordion_repr_parameters.selected_index = 1 checkbox_reprlist = Checkbox(value=False, description='reprlist') checkbox_reprlist._ngl_name = 'checkbox_reprlist' self.widget_repr_choices = self._make_repr_name_choices(self.widget_component_slider, self.widget_repr_slider) self.widget_repr_choices._ngl_name = 'reprlist_choices' self.widget_repr_add = self._make_add_widget_repr(self.widget_component_slider) def on_update_checkbox_reprlist(change): self.widget_repr_choices.visible= change['new'] checkbox_reprlist.observe(on_update_checkbox_reprlist, names='value') def on_repr_name_text_value_changed(change): name = change['new'].strip() old = change['old'].strip() should_update = (self._real_time_update and old and name and name in REPRESENTATION_NAMES and name != change['old'].strip()) if should_update: component=self.widget_component_slider.value repr_index=self.widget_repr_slider.value self._view._remote_call('setRepresentation', target='Widget', args=[change['new'], {}, component, repr_index]) self._view._request_repr_parameters(component, repr_index) def on_component_or_repr_slider_value_changed(change): self._view._request_repr_parameters(component=self.widget_component_slider.value, repr_index=self.widget_repr_slider.value) self.widget_component_dropdown.options = tuple(self._view._ngl_component_names) if self.widget_accordion_repr_parameters.selected_index >= 0: self.widget_repr_parameters.name = self.widget_repr_name.value self.widget_repr_parameters.repr_index = self.widget_repr_slider.value self.widget_repr_parameters.component_index = self.widget_component_slider.value def on_repr_selection_value_changed(change): if self._real_time_update: component = self.widget_component_slider.value repr_index = self.widget_repr_slider.value self._view._set_selection(change['new'], component=component, repr_index=repr_index) def on_change_component_dropdown(change): choice = change['new'] if choice: self.widget_component_slider.value = self._view._ngl_component_names.index(choice) self.widget_component_dropdown.observe(on_change_component_dropdown, names='value') self.widget_repr_slider.observe(on_component_or_repr_slider_value_changed, names='value') self.widget_component_slider.observe(on_component_or_repr_slider_value_changed, names='value') self.widget_repr_name.observe(on_repr_name_text_value_changed, names='value') repr_selection.observe(on_repr_selection_value_changed, names='value') self.widget_repr_control_buttons = self._make_button_repr_control(self.widget_component_slider, self.widget_repr_slider, repr_selection) blank_box = Box([Label("")]) all_kids = [self.widget_repr_control_buttons, blank_box, self.widget_repr_add, self.widget_component_dropdown, self.widget_repr_name, repr_selection, self.widget_component_slider, self.widget_repr_slider, self.widget_repr_choices, self.widget_accordion_repr_parameters ] vbox = VBox(all_kids) self._view._request_repr_parameters(component=self.widget_component_slider.value, repr_index=self.widget_repr_slider.value) self.widget_repr = _relayout_master(vbox, width='100%') self._refresh(self.widget_component_slider, self.widget_repr_slider) setattr(self.widget_repr, "_saved_widgets", []) for _box in self.widget_repr.children: if hasattr(_box, 'children'): for kid in _box.children: self.widget_repr._saved_widgets.append(kid) return self.widget_repr def _make_widget_repr_parameters(self, component_slider, repr_slider, repr_name_text=None): name = repr_name_text.value if repr_name_text is not None else ' ' widget = self._view._display_repr(component=component_slider.value, repr_index=repr_slider.value, name=name) widget._ngl_name = 'repr_parameters_box' return widget def _make_button_export_image(self): slider_factor = IntSlider(value=4, min=1, max=10, description='scale') checkbox_antialias = Checkbox(value=True, description='antialias') checkbox_trim = Checkbox(value=False, description='trim') checkbox_transparent = Checkbox(value=False, description='transparent') filename_text = Text(value='Screenshot', description='Filename') delay_text = FloatText(value=1, description='delay (s)', tooltip='hello') start_text, stop_text, step_text = (IntText(value=0, description='start'), IntText(value=self._view.count, description='stop'), IntText(value=1, description='step')) start_text.layout.max_width = stop_text.layout.max_width = step_text.layout.max_width \ = filename_text.layout.max_width = delay_text.layout.max_width = default.DEFAULT_TEXT_WIDTH button_movie_images = Button(description='Export Images') def download_image(filename): self._view.download_image(factor=slider_factor.value, antialias=checkbox_antialias.value, trim=checkbox_trim.value, transparent=checkbox_transparent.value, filename=filename) @button_movie_images.on_click def on_click_images(button_movie_images): for i in range(start_text.value, stop_text.value, step_text.value): self._view.frame = i time.sleep(delay_text.value) download_image(filename=filename_text.value + str(i)) time.sleep(delay_text.value) vbox = VBox([ button_movie_images, start_text, stop_text, step_text, delay_text, filename_text, slider_factor, checkbox_antialias, checkbox_trim, checkbox_transparent, ]) form_items = _relayout(vbox, make_form_item_layout()) form = Box(form_items, layout=_make_box_layout()) # form = _relayout_master(vbox) return form def _make_resize_notebook_slider(self): resize_notebook_slider = IntSlider(min=300, max=2000, description='resize notebook') def on_resize_notebook(change): width = change['new'] self._view._remote_call('resizeNotebook', target='Widget', args=[width,]) resize_notebook_slider.observe(on_resize_notebook, names='value') return resize_notebook_slider def _make_add_widget_repr(self, component_slider): dropdown_repr_name = Dropdown(options=REPRESENTATION_NAMES, value='cartoon') repr_selection = Text(value='*', description='') repr_button = Button(description='Add', tooltip="""Add representation. You can also hit Enter in selection box""") repr_button.layout = Layout(width='auto', flex='1 1 auto') dropdown_repr_name.layout.width = repr_selection.layout.width = default.DEFAULT_TEXT_WIDTH def on_click_or_submit(button_or_text_area): self._view.add_representation(selection=repr_selection.value.strip(), repr_type=dropdown_repr_name.value, component=component_slider.value) repr_button.on_click(on_click_or_submit) repr_selection.on_submit(on_click_or_submit) add_repr_box = HBox([repr_button, dropdown_repr_name, repr_selection]) add_repr_box._ngl_name = 'add_repr_box' return add_repr_box def _make_repr_playground(self): vbox = VBox() children = [] rep_names = REPRESENTATION_NAMES[:] excluded_names = ['ball+stick', 'distance'] for name in excluded_names: rep_names.remove(name) repr_selection = Text(value='*') repr_selection.layout.width = default.DEFAULT_TEXT_WIDTH repr_selection_box = HBox([Label('selection'), repr_selection]) setattr(repr_selection_box, 'value', repr_selection.value) for index, name in enumerate(rep_names): button = ToggleButton(description=name) def make_func(): def on_toggle_button_value_change(change, button=button): selection = repr_selection.value new = change['new'] # True/False if new: self._view.add_representation(button.description, selection=selection) else: self._view._remove_representations_by_name(button.description) return on_toggle_button_value_change button.observe(make_func(), names='value') children.append(button) button_clear = Button(description='clear', button_style='info', icon='fa-eraser') @button_clear.on_click def on_clear(button_clear): self._view.clear() for kid in children: # unselect kid.value = False vbox.children = children + [repr_selection, button_clear] _make_autofit(vbox) self.widget_quick_repr = vbox return self.widget_quick_repr def _make_repr_name_choices(self, component_slider, repr_slider): repr_choices = Dropdown(options=[" ",]) def on_chose(change): repr_name = change['new'] repr_index = repr_choices.options.index(repr_name) repr_slider.value = repr_index repr_choices.observe(on_chose, names='value') repr_choices.layout.width = default.DEFAULT_TEXT_WIDTH self.widget_repr_choices = repr_choices return self.widget_repr_choices def _make_drag_widget(self): button_drag = Button(description='widget drag: off', tooltip='dangerous') drag_nb = Button(description='notebook drag: off', tooltip='dangerous') button_reset_notebook = Button(description='notebook: reset', tooltip='reset?') button_dialog = Button(description='dialog', tooltip='make a dialog') button_split_half = Button(description='split screen', tooltip='try best to make a good layout') @button_drag.on_click def on_drag(button_drag): if button_drag.description == 'widget drag: off': self._view._set_draggable(True) button_drag.description = 'widget drag: on' else: self._view._set_draggable(False) button_drag.description = 'widget drag: off' @drag_nb.on_click def on_drag_nb(button_drag): if drag_nb.description == 'notebook drag: off': js_utils._set_notebook_draggable(True) drag_nb.description = 'notebook drag: on' else: js_utils._set_notebook_draggable(False) drag_nb.description = 'notebook drag: off' @button_reset_notebook.on_click def on_reset(button_reset_notebook): js_utils._reset_notebook() @button_dialog.on_click def on_dialog(button_dialog): self._view._remote_call('setDialog', target='Widget') @button_split_half.on_click def on_split_half(button_dialog): from nglview import js_utils import time js_utils._move_notebook_to_the_left() js_utils._set_notebook_width('5%') time.sleep(0.1) self._view._remote_call('setDialog', target='Widget') drag_box = HBox([button_drag, drag_nb, button_reset_notebook, button_dialog, button_split_half]) drag_box = _make_autofit(drag_box) self.widget_drag = drag_box return drag_box def _make_spin_box(self): checkbox_spin = Checkbox(self.spin, description='spin') spin_x_slide = IntSlider( self._spin_x, min=-1, max=1, description='spin_x') spin_y_slide = IntSlider( self._spin_y, min=-1, max=1, description='spin_y') spin_z_slide = IntSlider( self._spin_z, min=-1, max=1, description='spin_z') spin_speed_slide = FloatSlider( self._spin_speed, min=0, max=0.2, step=0.001, description='spin speed') # spin link((checkbox_spin, 'value'), (self, 'spin')) link((spin_x_slide, 'value'), (self, '_spin_x')) link((spin_y_slide, 'value'), (self, '_spin_y')) link((spin_z_slide, 'value'), (self, '_spin_z')) link((spin_speed_slide, 'value'), (self, '_spin_speed')) spin_box= VBox([checkbox_spin, spin_x_slide, spin_y_slide, spin_z_slide, spin_speed_slide]) spin_box = _relayout_master(spin_box, width='75%') return spin_box def _make_widget_picked(self): self.widget_picked = self._make_text_picked() picked_box = HBox([self.widget_picked,]) return _relayout_master(picked_box, width='75%') def _make_export_image_widget(self): if self.widget_export_image is None: self.widget_export_image = HBox([self._make_button_export_image()]) return self.widget_export_image def _make_extra_box(self): if self.widget_extra is None: extra_list = [(self._make_drag_widget, 'Drag'), (self._make_spin_box, 'Spin'), (self._make_widget_picked, 'Picked'), (self._make_repr_playground, 'Quick'), (self._make_export_image_widget, 'Image'), (self._make_command_box, 'Command')] extra_box = _make_delay_tab(extra_list, selected_index=0) self.widget_extra = extra_box return self.widget_extra def _make_theme_box(self): if self.widget_theme is None: self.widget_theme = Box([self._make_button_theme(), self._make_button_reset_theme(hide_toolbar=False), self._make_button_reset_theme(hide_toolbar=True), self._make_button_clean_error_output()]) return self.widget_theme def _make_general_box(self): if self.widget_general is None: step_slide = IntSlider( value=self.step, min=-100, max=100, description='step') delay_text = IntSlider( value=self.delay, min=10, max=1000, description='delay') toggle_button_interpolate = ToggleButton(self.interpolate, description='Smoothing', tooltip='smoothing trajectory') link((toggle_button_interpolate, 'value'), (self, 'interpolate')) background_color_picker = ColorPicker(value='white', description='background') camera_type = Dropdown(value=self.camera, options=['perspective', 'orthographic'], description='camera') link((step_slide, 'value'), (self, 'step')) link((delay_text, 'value'), (self, 'delay')) link((toggle_button_interpolate, 'value'), (self, 'interpolate')) link((camera_type, 'value'), (self, 'camera')) link((background_color_picker, 'value'), (self._view, 'background')) center_button = self._make_button_center() render_button = self._show_download_image() qtconsole_button = self._make_button_qtconsole() center_render_hbox = _make_autofit(HBox([toggle_button_interpolate, center_button, render_button, qtconsole_button])) v0_left = VBox([step_slide, delay_text, background_color_picker, camera_type, center_render_hbox, ]) v0_left = _relayout_master(v0_left, width='100%') self.widget_general = v0_left return self.widget_general def _make_command_box(self): widget_text_command = Text() @widget_text_command.on_submit def _on_submit_command(_): command = widget_text_command.value js_utils.execute(command) widget_text_command.value = '' return widget_text_command def _create_all_tabs(self): tab = self._display() for index, _ in enumerate(tab.children): # trigger ceating widgets tab.selected_index = index self.widget_extra = self._make_extra_box() for index, _ in enumerate(self.widget_extra.children): self.widget_extra.selected_index = index def _simplify_repr_control(self): for widget in self.widget_repr._saved_widgets: if not isinstance(widget, Tab): widget.layout.display = 'none' self.widget_repr_choices.layout.display = 'flex' self.widget_accordion_repr_parameters.selected_index = 0
def interactive_plot(model_name): def plot_intervention(intervention, idx, num_samples=32): fig, ax = plt.subplots(1, 4, figsize=(10, 2.5), gridspec_kw=dict(wspace=0, hspace=0)) lim = 0 orig_data = prep_data(ukbb_test[idx]) x_test = orig_data['x'] pyro.clear_param_store() cond = {k: torch.tensor([[v]]) for k, v in intervention.items()} counterfactual = loaded_models[model_name].counterfactual( orig_data, cond, num_samples) x = counterfactual['x'] diff = (x_test - x).squeeze() lim = diff.abs().max() ax[1].set_title('Original') ax[1].imshow(x_test.squeeze(), 'Greys_r', vmin=0, vmax=255) ax[2].set_title(fmt_intervention(intervention)) ax[2].imshow(x.squeeze(), 'Greys_r', vmin=0, vmax=255) ax[3].set_title('Difference') ax[3].imshow(diff, 'seismic', clim=[-lim, lim]) for axi in ax: axi.axis('off') axi.xaxis.set_major_locator(plt.NullLocator()) axi.yaxis.set_major_locator(plt.NullLocator()) att_str = '$s={sex}$\n$a={age}$\n$b={brain_volume}$\n$v={ventricle_volume}$'.format( **{ att: value_fmt[att](orig_data[att].item()) for att in ('sex', 'age', 'brain_volume', 'ventricle_volume') }) ax[0].text(0.5, 0.5, att_str, horizontalalignment='center', verticalalignment='center', transform=ax[0].transAxes, fontsize=mpl.rcParams['axes.titlesize']) plt.show() from ipywidgets import interactive, IntSlider, FloatSlider, HBox, VBox, Checkbox, Dropdown def plot(image, age, sex, brain_volume, ventricle_volume, do_age, do_sex, do_brain_volume, do_ventricle_volume): intervention = {} if do_age: intervention['age'] = age if do_sex: intervention['sex'] = sex if do_brain_volume: intervention['brain_volume'] = brain_volume * 1000. if do_ventricle_volume: intervention['ventricle_volume'] = ventricle_volume * 1000. plot_intervention(intervention, image) w = interactive( plot, image=IntSlider(min=0, max=4, description='Image #'), age=FloatSlider(min=30., max=120., step=1., continuous_update=False, description='Age'), do_age=Checkbox(description='do(age)'), sex=Dropdown(options=[('female', 0.), ('male', 1.)], description='Sex'), do_sex=Checkbox(description='do(sex)'), brain_volume=FloatSlider(min=800., max=1600., step=10., continuous_update=False, description='Brain Volume (ml):', style={'description_width': 'initial'}), do_brain_volume=Checkbox(description='do(brain_volume)'), ventricle_volume=FloatSlider(min=11., max=110., step=1., continuous_update=False, description='Ventricle Volume (ml):', style={'description_width': 'initial'}), do_ventricle_volume=Checkbox(description='do(ventricle_volume)'), ) ui = VBox([ w.children[0], VBox([HBox([w.children[i + 1], w.children[i + 5]]) for i in range(4)]), w.children[-1] ]) display(ui) w.update()
def annotate(examples, options=None, shuffle=False, include_skip=True, display_fn=display, max_num_options=5): """ Build an interactive widget for annotating a list of input examples. Parameters ---------- examples: list(any), list of items to annotate options: list(any) or tuple(start, end, [step]) or None if list: list of labels for binary classification task (Dropdown or Buttons) if tuple: range for regression task (IntSlider or FloatSlider) if None: arbitrary text input (TextArea) shuffle: bool, shuffle the examples before annotating include_skip: bool, include option to skip example while annotating display_fn: func, function for displaying an example to the user Returns ------- annotations : list of tuples, list of annotated examples (example, label) """ examples = list(examples) if shuffle: random.shuffle(examples) annotations = [] current_index = -1 def set_label_text(): nonlocal count_label count_label.value = '{} examples annotated, {} examples left'.format( len(annotations), len(examples) - current_index) def show_next(): nonlocal current_index current_index += 1 set_label_text() if current_index >= len(examples): for btn in buttons: btn.disabled = True print('Annotation done.') return with out: clear_output(wait=True) display_fn(examples[current_index]) def add_annotation(annotation): annotations.append((examples[current_index], annotation)) show_next() def skip(btn): show_next() count_label = HTML() set_label_text() display(count_label) if type(options) == list: task_type = 'classification' elif type(options) == tuple and len(options) in [2, 3]: task_type = 'regression' elif options is None: task_type = 'captioning' else: raise Exception('Invalid options') buttons = [] if task_type == 'classification': use_dropdown = len(options) > max_num_options if use_dropdown: dd = Dropdown(options=options) display(dd) btn = Button(description='submit') def on_click(btn): add_annotation(dd.value) btn.on_click(on_click) buttons.append(btn) else: for label in options: btn = Button(description=label) def on_click(label, btn): add_annotation(label) btn.on_click(functools.partial(on_click, label)) buttons.append(btn) elif task_type == 'regression': target_type = type(options[0]) if target_type == int: cls = IntSlider else: cls = FloatSlider if len(options) == 2: min_val, max_val = options slider = cls(min=min_val, max=max_val) else: min_val, max_val, step_val = options slider = cls(min=min_val, max=max_val, step=step_val) display(slider) btn = Button(description='submit') def on_click(btn): add_annotation(slider.value) btn.on_click(on_click) buttons.append(btn) else: ta = Textarea() display(ta) btn = Button(description='submit') def on_click(btn): add_annotation(ta.value) btn.on_click(on_click) buttons.append(btn) if include_skip: btn = Button(description='skip') btn.on_click(skip) buttons.append(btn) box = HBox(buttons) display(box) out = Output() display(out) show_next() return annotations
def _make_widgets(self): # Header widget self.header = HTML( value=""" <h3>Experiment Explorer</h3> <p>Select a variable from the list to display metadata information. Where appropriate select a date range. Pressing the <b>Load</b> button will read the data into an <tt>xarray DataArray</tt> using the COSIMA Cookook. The command used is output and can be copied and modified as required.</p> <p>The loaded DataArray is accessible as the <tt>.data</tt> attribute of the ExperimentExplorer object.</p> <p>The selected experiment can be changed to any experiment present in the current database session.</p> """, description="", ) # Experiment selector element self.expt_selector = Dropdown( options=sorted(set(self.experiments.experiment), key=str.casefold), value=self.experiment_name, description="", layout={"width": "40%"}, ) # Frequency selection widget self.frequency = Dropdown( options=(), description="Frequency", disabled=True, ) # Cell methods selection widget self.cellmethods = Dropdown( options=(), style={"description_width": "initial"}, description="Cell methods", disabled=True, ) # Date selection widget self.daterange = SelectionRangeSlider( options=["0000", "0001"], index=(0, 1), description="Date range", layout={"width": "40%"}, disabled=True, ) # Variable filter selector combo widget. Pass in two widgets so they # can be updated by the VariableSelectorInfo widget self.var_selector = VariableSelectorInfo( self, self.variables, daterange=self.daterange, frequency=self.frequency, cellmethods=self.cellmethods, rows=20, ) # DataArray information widget self.data_box = HTML() # Data load button self.load_button = Button( description="Load", disabled=False, layout={ "width": "20%", "align": "center" }, tooltip="Click to load data", ) self.info_pane = VBox( [self.frequency, self.cellmethods, self.daterange], layout={ "padding": "10% 0", "width": "80%" }, ) self.centre_pane = HBox([VBox([self.var_selector]), self.info_pane])
def getCP(self): self.setLabel() self.plon = 0.0 self.plat = 0.0 self.dateSelection = datetime.now() self.dateSelection = datetime(2020, 4, 23) self.dateLast = datetime(1950, 1, 1) self.selectVar = 'AOD' self.selectTime = 0 self.dateSW = DatePicker(description='Date', layout=Layout(width='250px'), value=self.dateSelection, disabled=False) self.myWidget1 = Dropdown(options=[ 'AOD', 'DUST_PM', 'SALT_PM', 'ORG_CARB', 'BLK_CARB', 'SO4', 'PM2.5' ], value='AOD', layout=Layout(width='250px'), description='Varibale:', disabled=False) self.myWidget2 = Dropdown(options=[], value=None, layout=Layout(width='250px'), description='Varibale:', disabled=False) self.myWidget3 = Dropdown(options=[], value=None, description='Date:', disabled=False) self.myWidget4 = Dropdown(options=[], value=None, layout=Layout(width='250px'), description='Time:', disabled=False) self.latSW = FloatSlider(min=-90.0, max=90.0, step=0.25, description='Lat:', disabled=False, continuous_update=False, orientation='horizontal', readout=True, readout_format='.2f') self.lonSW = FloatSlider(min=-180.0, max=180.0, step=0.25, description='Lon:', disabled=False, continuous_update=False, orientation='horizontal', readout=True, readout_format='.2f') self.plotBW = Button(description='Spatial Plot', disabled=False, layout={ 'width': '200px', 'border': '3px outset' }) if self.ptype == 'space': self.inpUSR.children += (HBox([ VBox([self.dateSW, self.myWidget4, self.myWidget1]), VBox([self.plotBW]) ], layout={'overflow': 'visible'}), ) else: self.inpUSR.children += (HBox([ VBox([self.dateSW, self.myWidget1]), VBox([self.timeplot, self.latSW, self.lonSW]) ], layout={'overflow': 'visible'}), ) self.year = np.arange(41) + 1980 self.mm = np.arange(12) + 1 # formatter = "{:02d}".format # self.months=[formatter(item) for item in mm] self.dateSW.observe(self.dateSWCB) self.myWidget2.options = self.mm self.myWidget1.observe(self.myCallback1, names='value') self.myWidget2.observe(self.myCallback2, names='value') self.date = np.arange(1, 30) + 1 self.time = np.arange(24) self.myWidget3.options = self.date self.myWidget4.options = self.time self.myWidget3.observe(self.myCallback3, names='value') self.myWidget4.observe(self.myCallback4, names='value') self.latSW.observe(self.latSWCB, names='value') self.lonSW.observe(self.lonSWCB, names='value') self.plotBW.on_click(self.plotObs) return self.cp
class VariableSelector(VBox): """ Combo widget based on a Select box with a search panel above to live filter variables to Select. When a variable is selected the long name attribute is displayed under the select box. There are also two checkboxes which hide coordinates and restart variables. Note that a dict is used to populate the Select widget, so the visible value is the variable name and is accessed via the label attribute, and the long name via the value attribute. """ variables = None def __init__(self, variables, rows=10, **kwargs): """ variables is a pandas dataframe. kwargs are passed through to child widgets which, theoretically, allows for layout information to be specified """ self._make_widgets(rows) super().__init__(children=[ self.model, self.search, self.selector, self.info, self.filter_coords, self.filter_restarts, ], **kwargs) self.set_variables(variables) self._set_info() self._set_observes() def _make_widgets(self, rows): """ Instantiate all widgets """ # Experiment selector element self.model = Dropdown( options=(), layout={ "padding": "0px 5px", "width": "initial" }, description="", ) # Variable search self.search = Text( placeholder="Search: start typing", layout={ "padding": "0px 5px", "width": "auto", "overflow-x": "scroll" }, ) # Variable selection box self.selector = Select( options=(), # sorted(self.variables.name, key=str.casefold), rows=rows, layout=self.search.layout, ) # Variable info self.info = HTML(layout=self.search.layout) # Variable filtering elements self.filter_coords = Checkbox( value=True, indent=False, description="Hide coordinates", ) self.filter_restarts = Checkbox( value=True, indent=False, description="Hide restarts", ) def _set_observes(self): """ Set event handlers """ self.filter_coords.observe(self._filter_eventhandler, names="value") self.filter_restarts.observe(self._filter_eventhandler, names="value") self.model.observe(self._model_eventhandler, names="value") self.search.observe(self._search_eventhandler, names="value") self.selector.observe(self._selector_eventhandler, names="value") def set_variables(self, variables): """ Change variables """ # Add a new column to keep track of visibility in widget self.variables = variables.assign(visible=True) # Set default filtering self._filter_variables() # Update selector self._update_selector(self.variables[self.variables.visible]) def _update_selector(self, variables): """ Update the variables in the selector. The variable are passed as an argument, so can differ from the internal variable list. This allows for easy filtering """ # Populate model selector. Note label and value differ options = {"All models": ""} for model in variables.model.cat.categories.values: if len(model) > 0 and model != "none": options["{} only".format(model.capitalize())] = model self.model.options = options options = dict() firstvar = None for vals in variables.sort_values(["name" ])[["name", "long_name", "units"]].values: var, name, units = map(str, vals) if firstvar is None: firstvar = var if name.lower() == "none" or name == "": name = var # Add units string if suitable value exists if (units.lower() == "none" or units.lower() == "nounits" or units.lower() == "no units" or units.lower() == "dimensionless" or units.lower() == "1" or units == ""): options[var] = "{}".format(name) else: options[var] = "{} ({})".format(name, units) # Populate variable selector self.selector.options = options # Highlight first value, otherwise accessors like .value are not # immediately accessible if firstvar is not None: self.selector.value = options[firstvar] def _reset_filters(self): """ Reset filters to default values """ self.filter_coords.value = True self.filter_restarts.value = True def _model_eventhandler(self, event=None): """ Filter by model """ model = self.model.value # Reset the coord and restart filters when a model changed self._reset_filters() self._filter_variables(model=model) def _filter_eventhandler(self, event=None): """ Called when filter button pushed """ self._filter_variables(self.filter_coords.value, self.filter_restarts.value, self.model.value) def _filter_variables(self, coords=True, restarts=True, model=""): """ Optionally hide some variables """ # Set up a mask with all true values mask = self.variables["name"] != "" # Filter for matching models if model != "": mask = mask & (self.variables["model"] == model) # Conditionally filter out restarts and coordinates if coords: mask = mask & ~self.variables["coordinate"] if restarts: mask = mask & ~self.variables["restart"] # Mask out hidden variables self.variables["visible"] = mask # Update the variable selector self._update_selector(self.variables[self.variables.visible]) # Reset the search self.search.value = "" self.selector.value = None def _search_eventhandler(self, event=None): """ Live search bar, updates the selector options dynamically, does not alter visible mask in variables """ search_term = self.search.value variables = self.variables[self.variables.visible] if search_term is not None or search_term != "": try: variables = variables[variables.name.str.contains( search_term, case=False, na=False) | variables.long_name.str.contains( search_term, case=False, na=False)] except: warnings.warn("Illegal character in search!", UserWarning) search_term = self.search.value self._update_selector(variables) def _selector_eventhandler(self, event=None): """ Update variable info when variable selected """ self._set_info(self.selector.value) def _set_info(self, long_name=None): """ Set long name info widget """ if long_name is None or long_name == "": long_name = " " style = "<style>.breakword { word-wrap: break-word; font-size: 90%; line-height: 1.1;}</style>" self.info.value = style + '<p class="breakword">{long_name}</p>'.format( long_name=long_name) def delete(self, variable_names=None): """ Remove variables """ # If no variable specified just delete the currently selected one if variable_names is None: if self.selector.label is None: return None else: variable_names = [ self.selector.label, ] if isinstance(variable_names, str): variable_names = [ variable_names, ] mask = self.variables["name"].isin(variable_names) deleted = self.variables[mask] # Delete variables self.variables = self.variables[~mask] # Update selector. Use search eventhandler so the selector preserves any # current search term. It is annoying to have that reset and type in again # if multiple variables are to be added self._search_eventhandler() return deleted def add(self, variables): """ Add variables """ # Concatenate existing and new variables self.variables = pd.concat([self.variables, variables]) # Need to recalculate the visible flag as new variables have been added self._filter_eventhandler(None) def get_selected(self): """ Return currently selected variable name """ return self.selector.label
class ExperimentExplorer(VBox): session = None _loaded_data = None experiment_name = None variables = None experiments = None def __init__(self, session=None, experiment=None): if session is None: session = database.create_session() self.session = session self.experiments = querying.get_experiments(session=self.session, all=True) if self.experiments.size == 0: raise ValueError("No experiments found in database") if experiment is None: experiment = self.experiments.iloc[0].experiment self.experiment_name = experiment self.variables = querying.get_variables(self.session, self.experiment_name, inferred=True) self._make_widgets() # Call super init and pass widgets as children super().__init__(children=[ self.header, self.expt_selector, self.centre_pane, self.load_button, self.data_box, ]) # self._load_experiment(self.experiment_name) self._load_variables() self._set_handlers() def _make_widgets(self): # Header widget self.header = HTML( value=""" <h3>Experiment Explorer</h3> <p>Select a variable from the list to display metadata information. Where appropriate select a date range. Pressing the <b>Load</b> button will read the data into an <tt>xarray DataArray</tt> using the COSIMA Cookook. The command used is output and can be copied and modified as required.</p> <p>The loaded DataArray is accessible as the <tt>.data</tt> attribute of the ExperimentExplorer object.</p> <p>The selected experiment can be changed to any experiment present in the current database session.</p> """, description="", ) # Experiment selector element self.expt_selector = Dropdown( options=sorted(set(self.experiments.experiment), key=str.casefold), value=self.experiment_name, description="", layout={"width": "40%"}, ) # Frequency selection widget self.frequency = Dropdown( options=(), description="Frequency", disabled=True, ) # Cell methods selection widget self.cellmethods = Dropdown( options=(), style={"description_width": "initial"}, description="Cell methods", disabled=True, ) # Date selection widget self.daterange = SelectionRangeSlider( options=["0000", "0001"], index=(0, 1), description="Date range", layout={"width": "40%"}, disabled=True, ) # Variable filter selector combo widget. Pass in two widgets so they # can be updated by the VariableSelectorInfo widget self.var_selector = VariableSelectorInfo( self, self.variables, daterange=self.daterange, frequency=self.frequency, cellmethods=self.cellmethods, rows=20, ) # DataArray information widget self.data_box = HTML() # Data load button self.load_button = Button( description="Load", disabled=False, layout={ "width": "20%", "align": "center" }, tooltip="Click to load data", ) self.info_pane = VBox( [self.frequency, self.cellmethods, self.daterange], layout={ "padding": "10% 0", "width": "80%" }, ) self.centre_pane = HBox([VBox([self.var_selector]), self.info_pane]) def _set_handlers(self): """ Define routines to handle button clicks and experiment selection """ self.load_button.on_click(self._load_data) self.expt_selector.observe(self._expt_eventhandler, names="value") def _expt_eventhandler(self, selector): """ Called when experiment dropdown menu changes """ self._load_experiment(selector.new) def _load_data(self, b): """ Called when load_button clicked """ varname = self.var_selector.get_selected() (start_time, end_time) = self.daterange.value frequency = self.frequency.value cellmethods = self.cellmethods.value # Create a dict to build load command and the # string representation of the same load command kwargs = { "session": self.session, "expt": self.expt_selector.value, "variable": varname, "frequency": frequency, "attrs": { "cell_methods": cellmethods }, "start_time": str(start_time), "end_time": str(end_time), "n": 1, } load_command = """cc.querying.getvar(expt='{expt}', variable='{variable}', session=session, frequency='{frequency}'""" if cellmethods is not None: load_command = (load_command + """, attrs={attrs}""") if frequency == "static": load_command = load_command + ", n={n})" else: load_command = (load_command + """, start_time='{start_time}', end_time='{end_time}')""") # Format load_command string load_command = load_command.format(**kwargs) load_command = "<pre><code>" + load_command + "</code></pre>" # Interim message to tell user what is happening self.data_box.value = ("Loading data, using following command ..." + load_command + "Please wait ... ") if frequency == "static": del kwargs["start_time"] del kwargs["end_time"] else: del kwargs["n"] if cellmethods is None: del kwargs["attrs"] try: self._loaded_data = querying.getvar(**kwargs) except Exception as e: self.data_box.value = ( self.data_box.value + "Error loading variable {} data: {}".format(varname, e)) return # Update data box with message about command used and pretty HTML # representation of DataArray self.data_box.value = ("Loaded data with" + load_command + self._loaded_data._repr_html_() + "Data can be accessed through .data attribute") def _load_experiment(self, experiment_name): """ When first instantiated, or experiment changed, the variable selector widget needs to be refreshed """ self.experiment_name = experiment_name self.variables = querying.get_variables(self.session, self.experiment_name, inferred=True) self._load_variables() def _load_variables(self): """ Populate the variable selector dialog """ self.var_selector.set_variables(self.variables) self.var_selector._filter_eventhandler(None) @property def data(self): """ Return xarray DataArray if one has been loaded """ if self._loaded_data is None: print("No data can be returned: no variable has been loaded") return self._loaded_data
def upload_shp(path_data): # Upload l_up = Label("a. Upload .shp to the server.") accept_files = ".shp, .cpg, .dbf, .prj, .shx, .sbn, .sbx, .xml" shp_dist_folder = Text(value=f"{path_data}vector", placeholder='tmp/', description='Folder:', disabled=False) shp_select = FileUpload( description='Select files:', icon='plus', accept=accept_files, multiple=True # True to accept multiple files upload else False ) shp_clear = Button(value=False, disabled=False, button_style='info', tooltip='Clear selections.', icon='broom', layout=Layout(width='40px')) shp_upload = Button(value=False, disabled=False, button_style='info', tooltip='Upload foi reference data (.shp).', icon='fa-upload', layout=Layout(width='40px')) progress = Output() def outlog(*text): with progress: print(*text) @shp_clear.on_click def shp_clear_on_click(b): shp_select.value.clear() shp_select._counter = 0 @shp_upload.on_click def shp_upload_on_click(b): progress.clear_output() os.makedirs(shp_dist_folder.value, exist_ok=True) for key in shp_select.value: content = shp_select.value[key]['content'] with open(f'{shp_dist_folder.value}/{key}', 'wb') as f: f.write(content) outlog("All files are uploaded.") shp_select.value.clear() shp_select._counter = 0 shp_box = HBox([shp_dist_folder, shp_select, shp_clear, shp_upload]) # Import the .shp to the database l_imp = Label("""b. Import uploaded .shp to the database. Add a short name, max 15 characters for the parcels table e.g.:escat2020.""") imp_select = Dropdown(options=[ s for s in glob.glob(f'{shp_dist_folder.value}/*') if '.shp' in s ], description='Select .shp:', disabled=False) imp_refresh = Button(layout=Layout(width='35px'), icon='fa-refresh') imp_proc = Button( description='Import .shp file', value=False, disabled=False, button_style='info', tooltip='Run', icon='fa-database', ) imp_truncate = Checkbox(value=False, description='Remove old entries', disabled=False) try: with open(f"{config.get_value(['paths','temp'])}tb_prefix", 'r') as f: name_from_prfix = f.read() except Exception: name_from_prfix = None imp_tb_name = Text(value=name_from_prfix, placeholder='ms2010', description='Table name:', tooltip='A short name max 10 char.', disabled=False) def on_imp_tb_name(change): with open(f"{config.get_value(['paths','temp'])}tb_prefix", 'w+') as f: f.write(imp_tb_name.value) imp_tb_name.observe(on_imp_tb_name, 'value') @imp_proc.on_click def imp_proc_on_click(b): if imp_tb_name is not None: import subprocess progress.clear_output() # tb_name = imp_select.value.split('/')[-1].split('.')[0] tb_name = imp_tb_name.value.replace(' ', '').lower()[:15] outlog("Importing .shp to database...") command = [ 'ogr2ogr', '-f', 'PostgreSQL', 'PG:' + database.conn_str(), '-nln', tb_name, '-nlt', 'PROMOTE_TO_MULTI', '-nlt', 'GEOMETRY', imp_select.value ] if imp_truncate.value is True: command.extend(['--config', 'OGR_TRUNCATE', 'YES']) if subprocess.call(command) == 0: progress.clear_output() outlog( f"Completed. Total number of rows in table '{tb_name}':", database.exact_count(tb_name)) else: outlog("Could not import shp file.") else: outlog( "Please add a name for the parcels table (MAX 15 char e.g.: escat2020)." ) @imp_refresh.on_click def imp_refresh_on_click(b): imp_select.options = [ s for s in glob.glob(f'{shp_dist_folder.value}/*') if '.shp' in s ] imp_box = HBox( [imp_tb_name, imp_select, imp_refresh, imp_proc, imp_truncate]) return VBox([l_up, shp_box, l_imp, imp_box, progress])
def get(): """Get the parcel's dataset for the given location or ids""" info = Label("1. Select the region and the year to get parcel information.") values = config.read() # Set the max number of parcels that can be downloaded at once. plimit = int(values['set']['plimit']) def aois_options(): values = config.read() options = {} if values['set']['data_source'] == '0': for desc in values['api']['options']['aois']: aoi = f"{values['api']['options']['aois'][desc]}" options[(desc, aoi)] = values['api']['options']['years'][aoi] elif values['set']['data_source'] == '1': for aoi in values['ds_conf']: desc = f"{values['ds_conf'][aoi]['desc']}" confgs = values['ds_conf'][aoi]['years'] options[(f'{desc} ({aoi})', aoi)] = [y for y in confgs] return options def aois_years(): values = config.read() years = {} if values['set']['data_source'] == '0': for desc in values['api']['options']['aois']: aoi = values['api']['options']['aois'][desc] years[aoi] = values['api']['options']['years'][aoi] elif values['set']['data_source'] == '1': for aoi in values['ds_conf']: desc = f"{values['ds_conf'][aoi]['desc']}" years[aoi] = [y for y in values['ds_conf'][aoi]['years']] return years try: aois = Dropdown( options=tuple(aois_options()), value=values['set']['ds_conf'], description='AOI:', disabled=False, ) except: aois = Dropdown( options=tuple(aois_options()), description='AOI:', disabled=False, ) year = Dropdown( options=next(iter(aois_options().values())), description='Year:', disabled=False, ) button_refresh = Button(layout=Layout(width='35px'), icon='fa-refresh') @button_refresh.on_click def button_refresh_on_click(b): aois.options = tuple(aois_options()) year.options = aois_years()[aois.value] def table_options_change(change): try: year.options = aois_years()[change.new] except: aois.options = tuple(aois_options()) year.options = aois_years()[aois.value] aois.observe(table_options_change, 'value') info_method = Label("2. Select a method to get the data.") method = ToggleButtons( options=[('Parcel ID', 2), ('Coordinates', 1), ('Map marker', 3), ('Polygon', 4)], value=None, description='', disabled=False, button_style='info', tooltips=['Enter lat lon', 'Enter parcel ID', 'Select a point on a map', 'Get parcels id in a polygon'], ) plon = Text( value='5.664', placeholder='Add lon', description='Lon:', disabled=False ) plat = Text( value='52.694', placeholder='Add lat', description='Lat:', disabled=False ) wbox_lat_lot = VBox(children=[plat, plon]) info_pid = Label( "Multiple parcel id codes can be added (comma ',' separated, e.g.: 11111, 22222).") pid = Textarea( value='34296', placeholder='12345, 67890', description='Parcel(s) ID:', disabled=False ) wbox_pids = VBox(children=[info_pid, pid]) bt_get_ids = Button( description="Find parcels", disabled=False, button_style='info', tooltip='Find parcels within the polygon.', icon='' ) get_ids_box = HBox([bt_get_ids, Label( "Find the parcels that are in the polygon.")]) ppoly_out = Output() progress = Output() def outlog(*text): with progress: print(*text) def outlog_poly(*text): with ppoly_out: print(*text) @bt_get_ids.on_click def bt_get_ids_on_click(b): with ppoly_out: try: get_requests = data_source() ppoly_out.clear_output() polygon = get_maps.polygon_map.feature_collection[ 'features'][-1]['geometry']['coordinates'][0] polygon_str = '-'.join(['_'.join(map(str, c)) for c in polygon]) outlog_poly(f"Geting parcel ids within the polygon...") polyids = json.loads(get_requests.ppoly(aois.value, year.value, polygon_str, False, True)) outlog_poly(f"'{len(polyids['ogc_fid'])}' parcels where found:") outlog_poly(polyids['ogc_fid']) file = config.get_value(['files', 'pids_poly']) with open(file, "w") as text_file: text_file.write('\n'.join(map(str, polyids['ogc_fid']))) except Exception as err: outlog("No parcel ids found:", err) method_out = Output(layout=Layout(border='1px solid black')) def method_options(obj): with method_out: method_out.clear_output() if obj['new'] == 1: display(wbox_lat_lot) elif obj['new'] == 2: display(wbox_pids) elif obj['new'] == 3: display(get_maps.base_map(aois.value, int(config.get_value(['set', 'data_source'])))) elif obj['new'] == 4: display(VBox([get_maps.polygon(aois.value, int(config.get_value(['set', 'data_source']))), get_ids_box, ppoly_out])) method.observe(method_options, 'value') info_type = Label("3. Select datasets to download.") table_options = HBox([aois, button_refresh, year]) # ########### Time series options ######################################### pts_bt = ToggleButton( value=False, description='Time series', disabled=False, button_style='success', # success tooltip='Get parcel information', icon='toggle-off', layout=Layout(width='50%') ) pts_bands = data_options.pts_bands() pts_tstype = SelectMultiple( options=data_options.pts_tstype(), value=['s2'], rows=3, description='TS type:', disabled=False, ) pts_band = Dropdown( options=list(pts_bands['s2']), value='', description='Band:', disabled=False, ) def pts_tstype_change(change): if len(pts_tstype.value) <= 1: pts_band.disabled = False try: pts_b = change.new[0] pts_band.options = pts_bands[pts_b] except: pass else: pts_band.value = '' pts_band.disabled = True pts_tstype.observe(pts_tstype_change, 'value') pts_options = VBox(children=[pts_tstype, pts_band]) # ########### Chip images options ######################################### pci_bt = ToggleButton( value=False, description='Chip images', disabled=False, button_style='success', tooltip='Get parcel information', icon='toggle-off', layout=Layout(width='50%') ) pci_start_date = DatePicker( value=datetime.date(2019, 6, 1), description='Start Date', disabled=False ) pci_end_date = DatePicker( value=datetime.date(2019, 6, 30), description='End Date', disabled=False ) pci_plevel = RadioButtons( options=['LEVEL2A', 'LEVEL1C'], value='LEVEL2A', description='Proces. level:', # Processing level disabled=False, layout=Layout(width='50%') ) pci_chipsize = IntSlider( value=640, min=100, max=5120, step=10, description='Chip size:', disabled=False, continuous_update=False, orientation='horizontal', readout=True, readout_format='d' ) pci_bands = data_options.pci_bands() pci_satellite = RadioButtons( options=list(pci_bands), value='Sentinel 2', disabled=True, layout=Layout(width='100px') ) pci_band = SelectMultiple( options=list(pci_bands['Sentinel 2']), value=['B04'], rows=11, description='Band:', disabled=False ) sats_plevel = HBox([pci_satellite, pci_plevel]) def on_sat_change(change): sat = change.new pci_band.options = pci_bands[sat] pci_satellite.observe(on_sat_change, 'value') pci_options = VBox(children=[pci_start_date, pci_end_date, sats_plevel, pci_chipsize, pci_band]) # ########### General options ############################################# pts_wbox = VBox(children=[]) pci_wbox = VBox(children=[]) def pts_observe(button): if button['new']: pts_bt.icon = 'toggle-on' pts_wbox.children = [pts_options] else: pts_bt.icon = 'toggle-off' pts_wbox.children = [] def pci_observe(button): if button['new']: pci_bt.icon = 'toggle-on' pci_wbox.children = [pci_options] else: pci_bt.icon = 'toggle-off' pci_wbox.children = [] pts_bt.observe(pts_observe, names='value') pci_bt.observe(pci_observe, names='value') pts = VBox(children=[pts_bt, pts_wbox], layout=Layout(width='40%')) pci = VBox(children=[pci_bt, pci_wbox], layout=Layout(width='40%')) data_types = HBox(children=[pts, pci]) info_get = Label("4. Download the selected data.") bt_get = Button( description='Download', disabled=False, button_style='warning', tooltip='Send the request', icon='download' ) path_temp = config.get_value(['paths', 'temp']) path_data = config.get_value(['paths', 'data']) info_paths = HTML("".join([ "<style>div.c {line-height: 1.1;}</style>", "<div class='c';>By default data will be stored in the temp folder ", f"({path_temp}), you will be asked to empty the temp folder each time ", "you start the notebook.<br>In your personal data folder ", f"({path_data}) you can permanently store the data.</div>"])) paths = RadioButtons( options=[(f"Temporary folder: '{path_temp}'.", path_temp), (f"Personal data folder: '{path_data}'.", path_data)], layout={'width': 'max-content'}, value=path_temp ) paths_box = Box([Label(value="Select folder:"), paths]) def file_len(fname): with open(fname) as f: for i, l in enumerate(f): pass return i + 1 def get_data(parcel): values = config.read() get_requests = data_source() pid = parcel['ogc_fid'][0] source = int(config.get_value(['set', 'data_source'])) if source == 0: datapath = f'{paths.value}{aois.value}{year.value}/parcel_{pid}/' elif source == 1: ds_conf = config.get_value(['set', 'ds_conf']) datapath = f'{paths.value}{ds_conf}/parcel_{pid}/' file_pinf = f"{datapath}{pid}_information" outlog(data_handler.export(parcel, 10, file_pinf)) if pts_bt.value is True: outlog(f"Getting time series for parcel: '{pid}',", f"({pts_tstype.value} {pts_band.value}).") for pts in pts_tstype.value: ts = json.loads(get_requests.pts(aois.value, year.value, pid, pts, pts_band.value)) band = '' if pts_band.value != '': band = f"_{pts_band.value}" file_ts = f"{datapath}{pid}_time_series_{pts}{band}" outlog(data_handler.export(ts, 11, file_ts)) if pci_bt.value is True: files_pci = f"{datapath}{pid}_chip_images/" outlog(f"Getting '{pci_band.value}' chip images for parcel: {pid}") with progress: get_requests.rcbl(parcel, pci_start_date.value, pci_end_date.value, pci_band.value, pci_satellite.value, pci_chipsize.value, files_pci) filet = f'{datapath}/{pid}_chip_images/{pid}_images_list.{pci_band.value[0]}.csv' if file_len(filet) > 1: outlog(f"Completed, all GeoTIFFs for bands '{pci_band.value}' are ", f"downloaded in the folder: '{datapath}/{pid}_chip_images'") else: outlog("No files where downloaded, please check your configurations") def get_from_location(lon, lat): get_requests = data_source() outlog(f"Finding parcel information for coordinates: {lon}, {lat}") parcel = json.loads(get_requests.ploc(aois.value, year.value, lon, lat, True)) pid = parcel['ogc_fid'][0] outlog(f"The parcel '{pid}' was found at this location.") try: get_data(parcel) except Exception as err: print(err) def get_from_id(pids): get_requests = data_source() outlog(f"Getting parcels information for: '{pids}'") for pid in pids: try: parcel = json.loads(get_requests.pid(aois.value, year.value, pid, True)) get_data(parcel) except Exception as err: print(err) @bt_get.on_click def bt_get_on_click(b): progress.clear_output() if method.value == 1: try: with progress: get_requests = data_source() lon, lat = plon.value, plat.value get_from_location(lon, lat) except Exception as err: outlog(f"Could not get parcel information for location '{lon}', '{lat}': {err}") elif method.value == 2: try: with progress: pids = pid.value.replace(" ", "").split(",") get_from_id(pids) except Exception as err: outlog(f"Could not get parcel information: {err}") elif method.value == 3: try: marker = get_maps.base_map.map_marker lon = str(round(marker.location[1], 2)) lat = str(round(marker.location[0], 2)) get_from_location(lon, lat) except Exception as err: outlog(f"Could not get parcel information: {err}") elif method.value == 4: try: file = config.get_value(['files', 'pids_poly']) with open(file, "r") as text_file: pids = text_file.read().split('\n') outlog("Geting data form the parcels:") outlog(pids) if len(pids) <= plimit: get_from_id(pids) else: outlog("You exceeded the maximum amount of selected parcels ", f"({plimit}) to get data. Please select smaller area.") except Exception as err: outlog("No pids file found.", err) else: outlog(f"Please select method to get parcel information.") return VBox([info, table_options, info_method, method, method_out, info_type, data_types, info_get, info_paths, paths_box, bt_get, progress])
def single_run(data_dir="", id=""): if not os.path.exists(data_dir): raise FileNotFoundError("No directory found named: " + data_dir) run_summary = RunSummary(data_dir=data_dir, id=id) sedov_solution = SedovSolution(E_0, run_summary.overview.background_density, run_summary.overview.metallicity) #### PASS TO PLOTTER #### num_checkpoints = len(run_summary.filenames) # log_R_max = round(np.log10(run_summary.df["Radius"].max()), 2) # log_R_min = max(log_R_max-4, # round(np.log10(run_summary.df["Radius"].min()), 2)+1) R_min = run_summary.df["Radius"].min() R_min = 0 R_max = run_summary.df["Radius"].max() if type(single_run.previous_widget) is widgets.Box: single_run.previous_widget.close() w = interactive( plotter, run_summary=fixed(run_summary), sedov_solution=fixed(sedov_solution), xlim=FloatRangeSlider( min=R_min, max=R_max, step=0.01 * (R_max - R_min), value=(R_min, R_max), ), checkpoint_index=IntSlider(min=0, max=num_checkpoints - 1, step=0, value=num_checkpoints - 1), y_axis_variable=Dropdown(options=cols_plot, value="Density"), x_axis_variable=RadioButtons(options=["Radius", "M_int", "zones"]), label=fixed("numeric"), density_in_mH_cm3=fixed(False), verbose=fixed(True), ) w1 = widgets.Button(description=u"\u23EA") def show_first(b): checkpoint_slider = w.children[5] checkpoint_slider.value = checkpoint_slider.min w1.on_click(show_first) wl = widgets.Button(description=u"\u276E") def show_prev(b): checkpoint_slider = w.children[5] if checkpoint_slider.value > checkpoint_slider.min: checkpoint_slider.value -= 1 wl.on_click(show_prev) w2 = widgets.Button(description=u"\u25BA") # w2.stop = False def play(b): checkpoint_slider = w.children[5] for i in range(checkpoint_slider.value + 1, checkpoint_slider.max + 1): plt.gcf() checkpoint_slider.value = i # plt.show() # time.sleep(.1) # if b.stop: # break w2.on_click(play) # wp = widgets.Button(description=u"p") # def pause(b): # w2.stop=True # wp.on_click(pause) wr = widgets.Button(description=u"\u276F") def show_next(b): checkpoint_slider = w.children[5] if checkpoint_slider.value < checkpoint_slider.max: checkpoint_slider.value += 1 wr.on_click(show_next) w3 = widgets.Button(description=u"\u23E9") def show_last(b): checkpoint_slider = w.children[5] checkpoint_slider.value = checkpoint_slider.max w3.on_click(show_last) w_buttons = widgets.HBox(children=[w1, wl, w2, wr, w3]) w.children = tuple([_w for _w in w.children] + [w_buttons]) single_run.previous_widget = w display(w) # display(w_buttons) return run_summary
def _tensor_folder(self): alo = Layout(width='70px') rlo = Layout(width='220px') scale = FloatSlider(max=10.0, step=0.001, readout=True, value=1.0) xs = [Text(layout=alo,disabled=True), Text(layout=alo,disabled=True), Text(layout=alo,disabled=True)] ys = [Text(layout=alo,disabled=True), Text(layout=alo,disabled=True), Text(layout=alo,disabled=True)] zs = [Text(layout=alo,disabled=True), Text(layout=alo,disabled=True), Text(layout=alo,disabled=True)] cs = [Text(layout=alo,disabled=True), Text(layout=alo,disabled=True), Text(layout=alo,disabled=True)] cidx = HBox([Text(disabled=True,description='Atom Index',layout=rlo)]) xbox = HBox(xs, layout=rlo) ybox = HBox(ys, layout=rlo) zbox = HBox(zs, layout=rlo) cbox = HBox(cs, layout=rlo) tens = Button(description=' Tensor', icon='bank') tensor_cont = VBox([xbox,ybox,zbox]) tensorIndex = Dropdown(options=[0],value=0,description='Tensor') # sceneIndex = Dropdown(options=[0],value=0,description='Scene') ten_label = Label(value="Change selected tensor:") sel_label = Label(value="Selected tensor in gray frame") cod_label = Label(value="Center of selected tensor: (x,y,z)") tensor = [] self.coords = [] def _changeTensor(tensor, tdx): carts = ['x','y','z'] for i,bra in enumerate(carts): for j,ket in enumerate(carts): tensor_cont.children[i].children[j].disabled=False tensor_cont.children[i].children[j].value = \ str(tensor[0][tdx][bra+ket]) tensor_cont.children[i].children[j].disabled=True adx = tensor[0][tdx]['atom'] cidx.children[0].value = str(adx) cbox.children[0].value = str(self.coords[0][int(adx)]) cbox.children[1].value = str(self.coords[1][int(adx)]) cbox.children[2].value = str(self.coords[2][int(adx)]) # scale.value = tensor[0][tdx]['scale'] def _tens(c): for scn in self.active(): scn.tens = not scn.tens self.coords = self._filter_coords() # sceneIndex.options = [x for x in range(len(self.active()))] # sceneIndex.value = sceneIndex.options[0] tensor = self.active()[0].tensor_d tensorIndex.options = [x for x in range(len(tensor[0]))] tensorIndex.value = tensorIndex.options[0] tdx = tensorIndex.value _changeTensor(tensor, tdx) def _scale(c): for scn in self.active(): scn.scale = c.new # tdx = tensorIndex.value # tensor = self.active()[0].tensor_d # tensor[0][tdx]['scale'] = c.new def _idx(c): for scn in self.active(): scn.tidx = c.new tensor = self.active()[0].tensor_d tdx = c.new _changeTensor(tensor, tdx) # def _sdx(c): # tensor = self.active()[sceneIndex.value].tensor_d # tensorIndex.options = [x for x in range(len(tensor[0]))] # tensorIndex.value = tensorIndex.options[0] # tdx = tensorIndex.value # _changeTensor(tensor, tdx) tens.on_click(_tens) scale.observe(_scale, names='value') tensorIndex.observe(_idx, names='value') # sceneIndex.observe(_sdx, names='value') content = _ListDict([ ('scale', scale), ('ten', ten_label), # ('sdx', sceneIndex), ('tdx', tensorIndex), ('tensor', tensor_cont), ('sel', sel_label), ('cidx', cidx), ('center', cod_label), ('coord', cbox)]) return Folder(tens, content)
def __init__(self, layer_state): self.state = layer_state self.widget_visible = Checkbox(description='visible', value=self.state.visible) link((self.state, 'visible'), (self.widget_visible, 'value')) self.widget_attribute = LinkedDropdown(self.state, 'attribute', label='attribute') self.widget_opacity = FloatSlider(min=0, max=1, step=0.01, value=self.state.alpha, description='opacity') link((self.state, 'alpha'), (self.widget_opacity, 'value')) self.widget_contrast = FloatSlider(min=0, max=4, step=0.01, value=self.state.contrast, description='contrast') link((self.state, 'contrast'), (self.widget_contrast, 'value')) self.widget_bias = FloatSlider(min=0, max=1, step=0.01, value=self.state.bias, description='bias') link((self.state, 'bias'), (self.widget_bias, 'value')) self.widget_stretch = LinkedDropdown(self.state, 'stretch', label='stretch') self.widget_percentile = LinkedDropdown(self.state, 'percentile', ui_name='limits', label='percentile') self.widget_v_min = FloatText(description='min', value=self.state.v_min) link((self.state, 'v_min'), (self.widget_v_min, 'value')) self.widget_v_max = FloatText(description='max', value=self.state.v_max) link((self.state, 'v_max'), (self.widget_v_max, 'value')) self.widget_color = ColorPicker(description='color') link((self.state, 'color'), (self.widget_color, 'value')) self.widget_colormap = Dropdown(options=colormaps.members, description='colormap') link((self.state, 'cmap'), (self.widget_colormap, 'value')) super().__init__() self.state.viewer_state.add_callback('color_mode', self.setup_widgets) self.setup_widgets()
def __init__(self): self.output_dir = '.' # self.output_dir = 'tmpdir' self.figsize_width_substrate = 15.0 # allow extra for colormap self.figsize_height_substrate = 12.5 self.figsize_width_svg = 12.0 self.figsize_height_svg = 12.0 # self.fig = plt.figure(figsize=(7.2,6)) # this strange figsize results in a ~square contour plot self.first_time = True self.modulo = 1 self.use_defaults = True self.svg_delta_t = 1 self.substrate_delta_t = 1 self.svg_frame = 1 self.substrate_frame = 1 self.customized_output_freq = False self.therapy_activation_time = 1000000 self.max_svg_frame_pre_therapy = 1000000 self.max_substrate_frame_pre_therapy = 1000000 self.svg_xmin = 0 # Probably don't want to hardwire these if we allow changing the domain size # self.svg_xrange = 2000 # self.xmin = -1000. # self.xmax = 1000. # self.ymin = -1000. # self.ymax = 1000. # self.x_range = 2000. # self.y_range = 2000. self.show_nucleus = False self.show_edge = True # initial value self.field_index = 4 # self.field_index = self.mcds_field.value + 4 # define dummy size of mesh (set in the tool's primary module) self.numx = 0 self.numy = 0 self.title_str = '' tab_height = '600px' tab_height = '500px' constWidth = '180px' constWidth2 = '150px' tab_layout = Layout( width='900px', # border='2px solid black', height=tab_height, ) #overflow_y='scroll') max_frames = 1 # self.mcds_plot = interactive(self.plot_substrate, frame=(0, max_frames), continuous_update=False) # self.i_plot = interactive(self.plot_plots, frame=(0, max_frames), continuous_update=False) self.i_plot = interactive(self.plot_substrate, frame=(0, max_frames), continuous_update=False) # "plot_size" controls the size of the tab height, not the plot (rf. figsize for that) # NOTE: the Substrates Plot tab has an extra row of widgets at the top of it (cf. Cell Plots tab) svg_plot_size = '700px' svg_plot_size = '600px' svg_plot_size = '700px' svg_plot_size = '900px' self.i_plot.layout.width = svg_plot_size self.i_plot.layout.height = svg_plot_size self.fontsize = 20 # description='# cell frames', self.max_frames = BoundedIntText( min=0, max=99999, value=max_frames, description='# frames', layout=Layout(width='160px'), ) self.max_frames.observe(self.update_max_frames) # self.field_min_max = {'dummy': [0., 1.]} # NOTE: manually setting these for now (vs. parsing them out of data/initial.xml) self.field_min_max = { 'director signal': [0., 1.], 'cargo signal': [0., 1.] } # hacky I know, but make a dict that's got (key,value) reversed from the dict in the Dropdown below # self.field_dict = {0:'dummy'} self.field_dict = {0: 'director signal', 1: 'cargo signal'} self.mcds_field = Dropdown( options={ 'director signal': 0, 'cargo signal': 1 }, value=0, # description='Field', layout=Layout(width=constWidth)) # print("substrate __init__: self.mcds_field.value=",self.mcds_field.value) # self.mcds_field.observe(self.mcds_field_cb) self.mcds_field.observe(self.mcds_field_changed_cb) # self.field_cmap = Text( # value='viridis', # description='Colormap', # disabled=True, # layout=Layout(width=constWidth), # ) self.field_cmap = Dropdown( options=['viridis', 'jet', 'YlOrRd'], value='YlOrRd', # description='Field', layout=Layout(width=constWidth)) # self.field_cmap.observe(self.plot_substrate) self.field_cmap.observe(self.mcds_field_cb) self.cmap_fixed = Checkbox( description='Fix', disabled=False, # layout=Layout(width=constWidth2), ) self.save_min_max = Button( description='Save', #style={'description_width': 'initial'}, button_style= 'success', # 'success', 'info', 'warning', 'danger' or '' tooltip='Save min/max for this substrate', disabled=True, layout=Layout(width='90px')) def save_min_max_cb(b): # field_name = self.mcds_field.options[] # field_name = next(key for key, value in self.mcds_field.options.items() if value == self.mcds_field.value) field_name = self.field_dict[self.mcds_field.value] # print(field_name) # self.field_min_max = {'oxygen': [0., 30.], 'glucose': [0., 1.], 'H+ ions': [0., 1.], 'ECM': [0., 1.], 'NP1': [0., 1.], 'NP2': [0., 1.]} self.field_min_max[field_name][0] = self.cmap_min.value self.field_min_max[field_name][1] = self.cmap_max.value # print(self.field_min_max) self.save_min_max.on_click(save_min_max_cb) self.cmap_min = FloatText( description='Min', value=0, step=0.1, disabled=True, layout=Layout(width=constWidth2), ) self.cmap_min.observe(self.mcds_field_cb) self.cmap_max = FloatText( description='Max', value=38, step=0.1, disabled=True, layout=Layout(width=constWidth2), ) self.cmap_max.observe(self.mcds_field_cb) def cmap_fixed_cb(b): if (self.cmap_fixed.value): self.cmap_min.disabled = False self.cmap_max.disabled = False self.save_min_max.disabled = False else: self.cmap_min.disabled = True self.cmap_max.disabled = True self.save_min_max.disabled = True # self.mcds_field_cb() self.cmap_fixed.observe(cmap_fixed_cb) field_cmap_row2 = HBox([self.field_cmap, self.cmap_fixed]) # field_cmap_row3 = HBox([self.save_min_max, self.cmap_min, self.cmap_max]) items_auto = [ self.save_min_max, #layout=Layout(flex='3 1 auto', width='auto'), self.cmap_min, self.cmap_max, ] box_layout = Layout(display='flex', flex_flow='row', align_items='stretch', width='80%') field_cmap_row3 = Box(children=items_auto, layout=box_layout) #--------------------- self.cell_nucleus_toggle = Checkbox( description='nuclei', disabled=False, value=self.show_nucleus, # layout=Layout(width=constWidth2), ) def cell_nucleus_toggle_cb(b): # self.update() if (self.cell_nucleus_toggle.value): self.show_nucleus = True else: self.show_nucleus = False self.i_plot.update() self.cell_nucleus_toggle.observe(cell_nucleus_toggle_cb) #---- self.cell_edges_toggle = Checkbox( description='edges', disabled=False, value=self.show_edge, # layout=Layout(width=constWidth2), ) def cell_edges_toggle_cb(b): # self.update() if (self.cell_edges_toggle.value): self.show_edge = True else: self.show_edge = False self.i_plot.update() self.cell_edges_toggle.observe(cell_edges_toggle_cb) self.cells_toggle = Checkbox( description='Cells', disabled=False, value=True, # layout=Layout(width=constWidth2), ) def cells_toggle_cb(b): # self.update() self.i_plot.update() if (self.cells_toggle.value): self.cell_edges_toggle.disabled = False self.cell_nucleus_toggle.disabled = False else: self.cell_edges_toggle.disabled = True self.cell_nucleus_toggle.disabled = True self.cells_toggle.observe(cells_toggle_cb) #--------------------- self.substrates_toggle = Checkbox( description='Substrates', disabled=False, value=True, # layout=Layout(width=constWidth2), ) def substrates_toggle_cb(b): if (self.substrates_toggle.value): # seems bass-ackwards self.cmap_fixed.disabled = False self.cmap_min.disabled = False self.cmap_max.disabled = False self.mcds_field.disabled = False self.field_cmap.disabled = False else: self.cmap_fixed.disabled = True self.cmap_min.disabled = True self.cmap_max.disabled = True self.mcds_field.disabled = True self.field_cmap.disabled = True self.substrates_toggle.observe(substrates_toggle_cb) self.grid_toggle = Checkbox( description='grid', disabled=False, value=True, # layout=Layout(width=constWidth2), ) def grid_toggle_cb(b): # self.update() self.i_plot.update() self.grid_toggle.observe(grid_toggle_cb) # field_cmap_row3 = Box([self.save_min_max, self.cmap_min, self.cmap_max]) # mcds_tab = widgets.VBox([mcds_dir, mcds_plot, mcds_play], layout=tab_layout) # mcds_params = VBox([self.mcds_field, field_cmap_row2, field_cmap_row3, self.max_frames]) # mcds_dir # mcds_params = VBox([self.mcds_field, field_cmap_row2, field_cmap_row3,]) # mcds_dir # self.tab = HBox([mcds_params, self.mcds_plot], layout=tab_layout) help_label = Label('select slider: drag or left/right arrows') # row1 = Box([help_label, Box( [self.max_frames, self.mcds_field, self.field_cmap], layout=Layout(border='0px solid black', row1a = Box([self.max_frames, self.mcds_field, self.field_cmap], layout=Layout(border='1px solid black', width='50%', height='', align_items='stretch', flex_direction='row', display='flex')) row1b = Box([ self.cells_toggle, self.cell_nucleus_toggle, self.cell_edges_toggle ], layout=Layout(border='1px solid black', width='50%', height='', align_items='stretch', flex_direction='row', display='flex')) row1 = HBox([row1a, Label('.....'), row1b]) row2a = Box([self.cmap_fixed, self.cmap_min, self.cmap_max], layout=Layout(border='1px solid black', width='50%', height='', align_items='stretch', flex_direction='row', display='flex')) # row2b = Box( [self.substrates_toggle, self.grid_toggle], layout=Layout(border='1px solid black', row2b = Box([ self.substrates_toggle, ], layout=Layout(border='1px solid black', width='50%', height='', align_items='stretch', flex_direction='row', display='flex')) # row2 = HBox( [row2a, self.substrates_toggle, self.grid_toggle]) row2 = HBox([row2a, Label('.....'), row2b]) if (hublib_flag): self.download_button = Download('mcds.zip', style='warning', icon='cloud-download', tooltip='Download data', cb=self.download_cb) self.download_svg_button = Download( 'svg.zip', style='warning', icon='cloud-download', tooltip='You need to allow pop-ups in your browser', cb=self.download_svg_cb) download_row = HBox([ self.download_button.w, self.download_svg_button.w, Label("Download all cell plots (browser must allow pop-ups).") ]) # box_layout = Layout(border='0px solid') controls_box = VBox([row1, row2]) # ,width='50%', layout=box_layout) self.tab = VBox([controls_box, self.i_plot, download_row]) else: # self.tab = VBox([row1, row2]) self.tab = VBox([row1, row2, self.i_plot])
def widget_box(): """Update the repository. Args: None Returns: update_widget : A widget for general settings. Raises: Error: Example: """ # User settings user_info = Label("General settings.") values = config.read() user_name = Text(value=values['set']['user'], placeholder='user name', description='User:'******'set']['email'], placeholder='[email protected]', description='email:', disabled=False) user_institution = Text(value=values['set']['institution'], placeholder='EU-', description='Institution:', disabled=False) ms_list = data_options.eu_ms() ms = Dropdown( options=[(ms_list[m], m) for m in ms_list] + [('', '')], value=values['set']['member_state'], description='Member state:', disabled=False, ) wbox_user = VBox([user_info, user_name, user_email, user_institution, ms], layout=Layout(border='1px solid black')) # System settings sys_info = Label("System settings.") paths_info = Label( "Select the personal data folder and the temporary folder.") jupyterlab = Checkbox( value=eval(values['set']['jupyterlab']), description= 'Workin in Jupyter Lab (Uncheck for Voila and classical jupyter environment)', disabled=False, indent=False) def on_jupyterlab_change(change): config.update(['set', 'jupyterlab'], str(jupyterlab.value)) jupyterlab.observe(on_jupyterlab_change, 'value') path_data = Text(value=values['paths']['data'], description='Data path:') path_temp = Text(value=values['paths']['temp'], description='Temp path:') files_info = Label("Select where to store the parcel IDs list file from:") file_pids_poly = Text(value=values['files']['pids_poly'], description='Polygon:') file_pids_dist = Text(value=values['files']['pids_dist'], description='Distance:') plimit_info = Label( "Warning: No more than 25 parcels are tested, unexpected results may occur." ) plimit = BoundedIntText(value=int(values['set']['plimit']), max=100_000_000, min=1, step=1, description='Max parcels that can be downloaded:', disabled=False) wbox_sys = VBox([ sys_info, jupyterlab, plimit_info, plimit, paths_info, path_data, path_temp, files_info, file_pids_poly, file_pids_dist ], layout=Layout(border='1px solid black')) # Git settings git_info = Label( "Git Settings. (To easily get the latest version of notebooks and scripts.)" ) git_url, git_user, git_pass = config.credentials('git') git_url = Text(value=git_url, description='Url:') git_user = Text(value=git_user, description='User name:') git_pass = Password(value=git_pass, placeholder='******', description='Password:'******'1px solid black')) btn_save = Button(description='Save', disabled=False, icon='save') progress = Output() def outlog(*text): with progress: print(*text) @btn_save.on_click def btn_save_on_click(b): progress.clear_output() config.update(['set', 'user'], str(user_name.value)) config.update(['set', 'email'], str(user_email.value)) config.update(['set', 'institution'], str(user_institution.value)) config.update(['set', 'member_state'], str(user_email.value)) config.update(['set', 'plimit'], str(plimit.value)) config.update(['git', 'url'], str(git_url.value)) config.update(['git', 'user'], str(git_user.value)) config.update(['paths', 'data'], str(path_data.value)) config.update(['paths', 'temp'], str(path_temp.value)) config.update(['files', 'pids_poly'], str(file_pids_poly.value)) config.update(['files', 'pids_dist'], str(file_pids_dist.value)) if git_pass.value != '': config.update(['git', 'pass'], str(git_pass.value)) outlog("The new settings are saved.") wbox = VBox([ config.clean_temp(), wbox_user, wbox_sys, wbox_git, HBox([btn_save, update.btn_update()]), progress ]) return wbox
class MerraAQTseries(PanelObject): def __init__(self, ptype='time', *args, **kwargs): self.title = 'MERRA2 AQ Time Series' self.stateChange = True self.init = True self.progVal = -1 self.progMax = 1 PanelObject.__init__(self, *args, **kwargs) self.ptype = ptype self.baseURL = 'https://goldsmr4.gesdisc.eosdis.nasa.gov/opendap/MERRA2/M2T1NXAER.5.12.4' self.lat = 24.42 self.lon = 54.43 self.endDate = datetime.now() self.startDate = self.endDate - timedelta(days=1) self.endDate = datetime(2020, 4, 30) self.startDate = datetime(2020, 4, 20) self.varlab = { 'AOD': 'AOD (Unitless)', 'DUST_PM': r'Dust PM$_{2.5}(\mu g m^{-3})$', 'SALT_PM': r'Salt PM$_{2.5}(\mu g m^{-3})$', 'ORG_CARB': r'Org Carbon PM$_{2.5} (\mu g m^{-3})$', 'BLK_CARB': r'Black Carbon PM$_{2.5}(\mu g m^{-3})$', 'SO4': r'SO$_4 PM_{2.5} (\mu g m^{-3})$', 'PM2.5': r'Total PM$_{2.5}(\mu g m^{-3})$' } def initSession(self): username = self.pwdDict['NASA Earth Data']['user'] password = self.pwdDict['NASA Earth Data']['password'] self.session = setup_session(username, password, check_url=self.baseURL) firstDay = datetime(1980, 1, 1) dataset = open_url(self.getUrlMERRA(firstDay), session=self.session) self.lon = dataset['lon'][:] self.lat = dataset['lat'][:] def getCP(self): self.setLabel() self.plon = 54.43 self.plat = 24.42 self.dateSelection = datetime.now() self.dateLast = datetime(1950, 1, 1) self.selectVar = 'AOD' self.selectTime = 0 self.sdateSW = DatePicker(description='Start Date', layout=Layout(width='auto'), value=self.startDate, disabled=False) self.edateSW = DatePicker(description='End Date', layout=Layout(width='auto'), value=self.endDate, disabled=False) self.varSW = Dropdown(options=[ 'AOD', 'DUST_PM', 'SALT_PM', 'ORG_CARB', 'BLK_CARB', 'SO4', 'PM2.5' ], value='AOD', layout=Layout(width='280px'), description='Variable:', disabled=False) self.latSW = Text(description='Latitude:', disabled=False, value='24.42', layout=Layout(width='180px')) self.lonSW = Text(description='Longitude:', disabled=False, value='54.43', layout=Layout(width='180px')) self.plotPB = Button(description='Time Series Plot', disabled=False, layout={ 'width': 'auto', 'border': '3px outset' }) self.inpUSR.children += (HBox([ VBox([self.sdateSW, self.edateSW, self.varSW]), VBox([self.latSW, self.lonSW, self.plotPB]) ], layout={'overflow': 'visible'}), ) self.sdateSW.observe(self.sdateSWCB) self.edateSW.observe(self.edateSWCB) self.varSW.observe(self.varSWCB, names='value') self.latSW.observe(self.latSWCB, names='value') self.lonSW.observe(self.lonSWCB, names='value') self.plotPB.on_click(self.plotTS) return self.cp def sdateSWCB(self, change): if change['type'] == 'change': self.startDate = self.sdateSW.value self.stateChange = True def edateSWCB(self, change): if change['type'] == 'change': self.endDate = self.edateSW.value self.stateChange = True def varSWCB(self, b): self.selectVar = self.varSW.value def latSWCB(self, change): self.plat = float(self.latSW.value) self.stateChange = True def lonSWCB(self, change): self.plon = float(self.lonSW.value) self.stateChange = True def lonlatToIndex(self, plon, plat): self.ilat = int( np.interp(plat, (self.lat.data.min(), self.lat.data.max()), (0, self.lat.shape[0] - 1))) self.ilon = int( np.interp(plon, (self.lon.data.min(), self.lon.data.max()), (0, self.lon.shape[0] - 1))) def updateProg(self, prog): while self.progVal < self.progMax: if self.progVal != prog.value: prog.value = self.progVal def getTS(self, startDate, endDate): ndays = (endDate - startDate).days + 1 if self.init: ndays += 1 currentDate = startDate delta = timedelta(days=1) self.df = pd.DataFrame(columns=[ 'datetime', 'AOD', 'DUST_PM', 'SALT_PM', 'ORG_CARB', 'BLK_CARB', 'SO4', 'PM2.5' ]) with self.out_cp: self.out_cp.clear_output() pbar = IntProgress(min=0, max=int(ndays)) pbar.description = 'Progress:' info1 = Label('0%') info2 = Label(' ') display( VBox([ HBox([pbar, info1]), HBox([info2], layout=Layout(justify_content='center')) ])) progVal = 0 if self.init: info2.value = 'Initializing NASA Earth Data Connection..' self.initSession() self.init = False pbar.value += 1 progVal += 1 info1.value = '{:.1f}%'.format( (float(progVal) / float(ndays)) * 100.0) self.lonlatToIndex(self.plon, self.plat) while currentDate <= endDate: url = self.getUrlMERRA(currentDate) info2.value = 'Accessing data for {}'.format(currentDate) dataset = open_url(url, session=self.session) aod = np.squeeze(dataset['TOTEXTTAU'][:, self.ilat, self.ilon]) dust_pm = np.squeeze( dataset['DUSMASS25'][:, self.ilat, self.ilon]) * 1000000000.0 salt_pm = np.squeeze( dataset['SSSMASS25'][:, self.ilat, self.ilon]) * 1000000000.0 org_carb = np.squeeze( dataset['OCSMASS'][:, self.ilat, self.ilon]) * 1000000000.0 blk_carb = np.squeeze( dataset['BCSMASS'][:, self.ilat, self.ilon]) * 1000000000.0 so4 = np.squeeze(dataset['SO4SMASS'][:, self.ilat, self.ilon]) * 1000000000.0 pm25 = (1.375 * so4 + 1.6 * org_carb + blk_carb + dust_pm + salt_pm) dt = pd.date_range(currentDate, periods=24, freq='H') vardict = { 'datetime': dt, 'AOD': aod, 'DUST_PM': dust_pm, 'SALT_PM': salt_pm, 'ORG_CARB': org_carb, 'BLK_CARB': blk_carb, 'SO4': so4, 'PM2.5': pm25 } df_add = pd.DataFrame(vardict) self.df = pd.concat([self.df, df_add]) currentDate += delta progVal += 1 info1.value = '{:.1f}%'.format( (float(progVal) / float(ndays)) * 100.0) pbar.value += 1 self.stateChange = False def plotTS(self, b): if self.stateChange: self.getTS(self.startDate, self.endDate) with self.out_cp: plt.ioff() fig, ax = plt.subplots(1, figsize=(12, 6)) ax = self.df.plot(ax=ax, x='datetime', y=self.selectVar) ax.set_xlabel('Time (UTC)') ax.set_ylabel(self.varlab[self.selectVar]) self.out_cp.clear_output() plt.show() def getUrlMERRA(self, dayObj): if dayObj.year < 1992: Vnumber = '100' elif dayObj.year < 2001: Vnumber = '200' elif dayObj.year < 2011: Vnumber = '300' else: Vnumber = '400' yy = dayObj.strftime('%Y') mm = dayObj.strftime('%m') dd = dayObj.strftime('%d') self.opendap_url = self.baseURL + '/{}/{}/MERRA2_{}.tavg1_2d_aer_Nx.{}{}{}.nc4'.format( yy, mm, Vnumber, yy, mm, dd) return self.opendap_url
def create_coding_quiz_question_true_false(question_num, correct_answer): #Import modules: from ipywidgets import FloatText, Dropdown, Button, Output, HBox, Valid, Label from IPython.display import clear_output #Create a Dropdown widget (with True-False options): py_q = Dropdown( options=['Do not know', 'Yes', 'No'], value='Do not know', #description=str(question_num)+'.', disabled=False) #Create button widget: answer_btn = Button( description='Check answer', disabled=False, button_style= 'danger', # 'success', 'info', 'warning', 'danger', 'primary' or '' tooltip='Click the button to check your answer!') #Style button: answer_btn.style.button_color = '#3973ac' answer_btn.layout.width = '140px' #Create output widget: feedback_out = Output() #Create Valid widget to mark answer: #feedback = Valid(value=True, #description='', #readout='') def on_btn_click(btn): #Check user's answer: if (py_q.value == correct_answer): #Change feedback-value to TRUE: #feedback.value = True #Correct answer: feedback_text = '\033[1;32m' + u'\u2713' + '\033[0m' else: #Change feedback-value to FALSE: #feedback.value = False #Wrong answer: feedback_text = '\033[1;33m' + 'X' + '\033[0m' #Clear previous feedback output and add mark: with feedback_out: #Clear previous output: clear_output() #display(feedback) #Display new feedback: print(feedback_text) return #Call function on button_click-event: answer_btn.on_click(on_btn_click) #Display widget: return HBox( [Label(str(question_num) + '.'), py_q, answer_btn, feedback_out])
# # Смотря на поверхность целевой функции мы старались предсказать, к каким весам мы придём, если начнём спуск с той или иной точки. Оказалось что всё не так очевидно, как мы думали. # # На графике ниже видно, что при "хорошем" раскладе (мы попали в большую яму) значение целевой функции всё время уменьшалось (хоть и по чуть-чуть), пока мы не достигали порога по количеству итераций. При плохом мы могли просто застрять в локальном минимуме или выйти на плато. # # \\\\ Как можно бороться с подобными ловушками мы поговорим на третьей и четвёртой неделях. # # ***Если в предыдущем задании вы "подпортили" данные - график будет строиться именно для подпорченных =)*** # In[ ]: @interact(b=BoundedFloatText(value=str(g_bias), min=min_b, max=max_b, description="Enter $b$:"), w1=BoundedFloatText(value="0", min=min_w1, max=max_w1, description="Enter $w_1$:"), w2=BoundedFloatText(value="0", min=min_w2, max=max_w2, description="Enter $w_2$:"), learning_rate=Dropdown(options=["0.01", "0.05", "0.1", "0.5", "1", "5", "10"], value="0.01", description="Learning rate: ") ) def learning_curve_for_starting_point(b, w1, w2, learning_rate=0.1): w = np.array([b, w1, w2]).reshape(X_corrupted.shape[1], 1) learning_rate=float(learning_rate) neuron = Neuron(w, activation_function=sigmoid, activation_function_derivative=sigmoid_prime) story = [J_quadratic(neuron, X_corrupted, y_corrupted)] for _ in range(2000): neuron.SGD(X_corrupted, y_corrupted, 2, learning_rate=learning_rate, max_steps=2) story.append(J_quadratic(neuron, X_corrupted, y_corrupted)) plt.plot(story) plt.title("Learning curve.\n Final $b={0:.3f}$, $w_1={1:.3f}, w_2={2:.3f}$".format(*neuron.w.ravel())) plt.ylabel("$J(w_1, w_2)$") plt.xlabel("Weight and bias update number")
if type(options) == list: task_type = 'classification' elif type(options) == tuple and len(options) in [2, 3]: task_type = 'regression' elif options is None: task_type = 'captioning' else: raise Exception('Invalid options') buttons = [] if task_type == 'classification': use_dropdown = len(options) > 15 if use_dropdown: dd = Dropdown(options=options) display(dd) btn = Button(description='submit') def on_click(btn): add_annotation(dd.value) btn.on_click(on_click) buttons.append(btn) else: for label in options: btn = Button(description=label) def on_click(label, btn): add_annotation(label) btn.on_click(functools.partial(on_click, label)) buttons.append(btn)
def general(): """General settings. Args: None Returns: update_widget : A widget for general settings. Raises: Error: Example: """ # User settings user_info = Label("General settings.") values = config.read() user_name = Text(value=values['set']['user'], placeholder='user name', description='User:'******'set']['email'], placeholder='[email protected]', description='email:', disabled=False) user_institution = Text(value=values['set']['institution'], placeholder='EU-', description='Institution:', disabled=False) ms_list = data_options.eu_ms() ms = Dropdown( options=[(ms_list[m], m) for m in ms_list] + [('', '')], value=values['set']['member_state'], description='Member state:', disabled=False, ) wbox_user = VBox([user_info, user_name, user_email, user_institution, ms], layout=Layout(border='1px solid black')) # System settings sys_info = Label("System settings.") paths_info = Label("Select the personal data folder.") jupyterlab = Checkbox( value=eval(values['set']['jupyterlab']), description= 'Workin in Jupyter Lab (Uncheck for Voila and classical jupyter environment)', disabled=False, indent=False) def on_jupyterlab_change(change): config.set_value(['set', 'jupyterlab'], str(jupyterlab.value)) jupyterlab.observe(on_jupyterlab_change, 'value') path_data = Text(value=values['paths']['data'], description='Data path:') path_temp = Text(value=values['paths']['temp'], description='Temp path:', disabled=True) files_info = Label("Select where to store the parcel IDs list file from:") plimit_info = Label( "Warning: No more than 25 parcels are tested, unexpected results may occur." ) plimit = BoundedIntText(value=int(values['set']['plimit']), max=100_000_000, min=1, step=1, description='Max parcels that can be downloaded:', disabled=False) wbox_sys = VBox([ sys_info, jupyterlab, plimit_info, plimit, paths_info, path_data, path_temp, files_info ], layout=Layout(border='1px solid black')) btn_save = Button(description='Save', icon='save') progress = Output() def outlog(*text): with progress: print(*text) @btn_save.on_click def btn_save_on_click(b): progress.clear_output() config.set_value(['set', 'user'], str(user_name.value)) config.set_value(['set', 'email'], str(user_email.value)) config.set_value(['set', 'institution'], str(user_institution.value)) config.set_value(['set', 'member_state'], str(user_email.value)) config.set_value(['set', 'plimit'], str(plimit.value)) config.set_value(['paths', 'data'], str(path_data.value)) config.set_value(['paths', 'temp'], str(path_temp.value)) outlog("The new settings are saved.") wbox = VBox( [clean_temp(), wbox_user, wbox_sys, HBox([btn_save]), progress]) return wbox
class NGLDisplay: """Structure display class Provides basic structure/trajectory display in the notebook and optional gui which can be used to enhance its usability. It is also possible to extend the functionality of the particular instance of the viewer by adding further widgets manipulating the structure. """ def __init__(self, atoms, xsize=500, ysize=500): import nglview from ipywidgets import Dropdown, FloatSlider, IntSlider, HBox, VBox self.atoms = atoms if isinstance(atoms[0], Atoms): # Assume this is a trajectory or struct list self.view = nglview.show_asetraj(atoms) self.frm = IntSlider(value=0, min=0, max=len(atoms) - 1) self.frm.observe(self._update_frame) self.struct = atoms[0] else: # Assume this is just a single structure self.view = nglview.show_ase(atoms) self.struct = atoms self.frm = None self.colors = {} self.view._remote_call('setSize', target='Widget', args=['%dpx' % (xsize,), '%dpx' % (ysize,)]) self.view.add_unitcell() self.view.add_spacefill() self.view.camera = 'orthographic' self.view.update_spacefill(radiusType='covalent', scale=0.7) self.view.center() self.asel = Dropdown(options=['All'] + list(set(self.struct.get_chemical_symbols())), value='All', description='Show') self.rad = FloatSlider(value=0.8, min=0.0, max=1.5, step=0.01, description='Ball size') self.asel.observe(self._select_atom) self.rad.observe(self._update_repr) wdg = [self.asel, self.rad] if self.frm: wdg.append(self.frm) self.gui = HBox([self.view, VBox(wdg)]) # Make useful shortcuts for the user of the class self.gui.view = self.view self.gui.control_box = self.gui.children[1] self.gui.custom_colors = self.custom_colors def _update_repr(self, chg=None): self.view.update_spacefill(radiusType='covalent', scale=self.rad.value) def _update_frame(self, chg=None): self.view.frame = self.frm.value return def _select_atom(self, chg=None): sel = self.asel.value self.view.remove_spacefill() for e in set(self.struct.get_chemical_symbols()): if (sel == 'All' or e == sel): if e in self.colors: self.view.add_spacefill(selection='#' + e, color=self.colors[e]) else: self.view.add_spacefill(selection='#' + e) self._update_repr() def custom_colors(self, clr=None): """ Define custom colors for some atoms. Pass a dictionary of the form {'Fe':'red', 'Au':'yellow'} to the function. To reset the map to default call the method without parameters. """ if clr: self.colors = clr else: self.colors = {} self._select_atom()
def direct_conn(db='main'): values = config.read() info_db = Label("Database connection settings.") db_host = Text( value=values['db'][db]['host'], placeholder='Database host', description='db Host:', disabled=False ) db_port = Text( value=values['db'][db]['port'], placeholder='Database port', description='db Port:', disabled=False ) db_name = Text( value=values['db'][db]['name'], placeholder='Database name', description='db Name:', disabled=False ) db_user = Text( value=values['db'][db]['user'], placeholder='Database user', description='db User:'******'db'][db]['pass'], placeholder='******', description='db Pass:'******'EOSC', 'CREODIAS', 'SOBLOO', 'MUNDI', 'ONDA', 'WEKEO', ''], value=values['s3']['dias'], description='DIAS:', disabled=False, ) os_host = Text( value=values['s3']['host'], placeholder='Storage host', description='s3 Host:', disabled=False ) os_bucket = Text( value=values['s3']['bucket'], placeholder='Bucket name', description='Bucket name:', disabled=False ) os_access_key = Text( value=values['s3']['access_key'], placeholder='Access key', description='Access Key:', disabled=False ) os_secret_key = Password( value=values['s3']['secret_key'], placeholder='Secret key', description='Secret Key:', disabled=False ) wb_save = Button( description='Save', disabled=False, icon='save' ) progress = Output() def outlog(*text): with progress: print(*text) @wb_save.on_click def wb_save_on_click(b): progress.clear_output() # Save database connection information config.set_value(['db', db, 'host'], str(db_host.value)) config.set_value(['db', db, 'port'], str(db_port.value)) config.set_value(['db', db, 'name'], str(db_name.value)) config.set_value(['db', db, 'user'], str(db_user.value)) config.set_value(['db', db, 'pass'], str(db_pass.value)) # Save Object storage connection information config.set_value(['s3', 'dias'], str(os_dias.value)) config.set_value(['s3', 'host'], str(os_host.value)) config.set_value(['s3', 'bucket'], str(os_bucket.value)) config.set_value(['s3', 'access_key'], str(os_access_key.value)) config.set_value(['s3', 'secret_key'], str(os_secret_key.value)) outlog("All changes are saved.") wbox = VBox([info_db, db_host, db_port, db_name, db_user, db_pass, info_os, os_dias, os_host, os_bucket, os_access_key, os_secret_key, wb_save, progress]) return wbox
def annotate(examples, task_type='classification', options=None, shuffle=False, include_skip=True, include_back=False, use_dropdown=False, buttons_in_a_row=4, reset_buttons_after_click=False, example_process_fn=None, final_process_fn=None, display_fn=display): """ Build an interactive widget for annotating a list of input examples. Parameters ---------- examples : list(any), list of items to annotate task_type : possible options are: - classification - multilabel-classification - captioning options : depending on the task this can be: - list of options - tuple with a range for regression tasks shuffle : bool, shuffle the examples before annotating include_skip: bool, include option to skip example while annotating include_back: bool, include option to navigate to previous example use_dropdown: use a dropdown or buttons during classification buttons_in_a_row: number of buttons in a row during classification reset_buttons_after_click: reset multi-label buttons after each click example_process_fn: hooked function to call after each example fn(ix, labels) final_process_fn: hooked function to call after annotation is done fn(annotations) display_fn : func, function for displaying an example to the user Returns ------- annotations : list of tuples, list of annotated examples (example, label(s)) """ examples = list(examples) if shuffle: random.shuffle(examples) annotations = {} current_index = -1 def set_label_text(index): nonlocal count_label count_label.value = f'{len(annotations)} of {len(examples)} Examples annotated, Current Position: {index + 1} ' def render(index): set_label_text(index) if index >= len(examples): print('Annotation done.') if final_process_fn is not None: final_process_fn(list(annotations.items())) for button in buttons: button.disabled = True count_label.value = \ f'{len(annotations)} of {len(annotations)} Examples annotated, Current Position: {len(annotations)} ' return for button in buttons: if button.description == 'prev': button.disabled = index <= 0 elif button.description == 'skip': button.disabled = index >= len(examples) - 1 elif examples[index] in annotations: if isinstance(annotations[examples[index]], list): button.value = button.description in annotations[ examples[index]] else: button.value = button.description == annotations[ examples[index]] with out: clear_output(wait=True) display_fn(examples[index]) def add_annotation(annotation): annotations[examples[current_index]] = annotation if example_process_fn is not None: example_process_fn(examples[current_index], annotation) next_example() def next_example(button=None): nonlocal current_index if current_index < len(examples): current_index += 1 render(current_index) def prev_example(button=None): nonlocal current_index if current_index > 0: current_index -= 1 render(current_index) count_label = HTML() set_label_text(current_index) display(count_label) buttons = [] if task_type == 'classification': if use_dropdown: dd = Dropdown(options=options) display(dd) btn = Button(description='submit') def on_click(button): add_annotation(dd.value) btn.on_click(on_click) buttons.append(btn) else: for label in options: btn = Button(description=label) def on_click(lbl, button): add_annotation(lbl) btn.on_click(functools.partial(on_click, label)) buttons.append(btn) elif task_type == 'multilabel-classification': for label in options: tgl = ToggleButton(description=label) buttons.append(tgl) btn = Button(description='submit', button_style='info') def on_click(button): labels_on = [] for tgl_btn in buttons: if isinstance(tgl_btn, ToggleButton): if tgl_btn.value: labels_on.append(tgl_btn.description) if reset_buttons_after_click: tgl_btn.value = False add_annotation(labels_on) btn.on_click(on_click) buttons.append(btn) elif task_type == 'regression': target_type = type(options[0]) if target_type == int: cls = IntSlider else: cls = FloatSlider if len(options) == 2: min_val, max_val = options slider = cls(min=min_val, max=max_val) else: min_val, max_val, step_val = options slider = cls(min=min_val, max=max_val, step=step_val) display(slider) btn = Button(description='submit', value='submit') def on_click(button): add_annotation(slider.value) btn.on_click(on_click) buttons.append(btn) elif task_type == 'captioning': ta = Textarea() display(ta) btn = Button(description='submit') def on_click(button): add_annotation(ta.value) btn.on_click(on_click) buttons.append(btn) else: raise ValueError('invalid task type') if include_back: btn = Button(description='prev', button_style='info') btn.on_click(prev_example) buttons.append(btn) if include_skip: btn = Button(description='skip', button_style='info') btn.on_click(next_example) buttons.append(btn) if len(buttons) > buttons_in_a_row: box = VBox([ HBox(buttons[x:x + buttons_in_a_row]) for x in range(0, len(buttons), buttons_in_a_row) ]) else: box = HBox(buttons) display(box) out = Output() display(out) next_example() return annotations
def _make_widget_repr(self): self.widget_repr_name = Text(value='', description='representation') self.widget_repr_name._ngl_name = 'repr_name_text' repr_selection = Text(value=' ', description='selection') repr_selection._ngl_name = 'repr_selection' repr_selection.width = self.widget_repr_name.width = default.DEFAULT_TEXT_WIDTH max_n_components = max(self._view.n_components-1, 0) self.widget_component_slider = IntSlider(value=0, max=max_n_components, min=0, description='component') self.widget_component_slider._ngl_name = 'component_slider' cvalue = ' ' self.widget_component_dropdown = Dropdown(value=cvalue, options=[cvalue,], description='component') self.widget_component_dropdown._ngl_name = 'component_dropdown' self.widget_repr_slider = IntSlider(value=0, description='representation', width=default.DEFAULT_SLIDER_WIDTH) self.widget_repr_slider._ngl_name = 'repr_slider' self.widget_repr_slider.visible = True self.widget_component_slider.layout.width = default.DEFAULT_SLIDER_WIDTH self.widget_repr_slider.layout.width = default.DEFAULT_SLIDER_WIDTH self.widget_component_dropdown.layout.width = self.widget_component_dropdown.max_width = default.DEFAULT_TEXT_WIDTH # turn off for now self.widget_component_dropdown.layout.display = 'none' self.widget_component_dropdown.description = '' # self.widget_accordion_repr_parameters = Accordion() self.widget_accordion_repr_parameters = Tab() self.widget_repr_parameters = self._make_widget_repr_parameters(self.widget_component_slider, self.widget_repr_slider, self.widget_repr_name) self.widget_accordion_repr_parameters.children = [self.widget_repr_parameters, Box()] self.widget_accordion_repr_parameters.set_title(0, 'Parameters') self.widget_accordion_repr_parameters.set_title(1, 'Hide') self.widget_accordion_repr_parameters.selected_index = 1 checkbox_reprlist = Checkbox(value=False, description='reprlist') checkbox_reprlist._ngl_name = 'checkbox_reprlist' self.widget_repr_choices = self._make_repr_name_choices(self.widget_component_slider, self.widget_repr_slider) self.widget_repr_choices._ngl_name = 'reprlist_choices' self.widget_repr_add = self._make_add_widget_repr(self.widget_component_slider) def on_update_checkbox_reprlist(change): self.widget_repr_choices.visible= change['new'] checkbox_reprlist.observe(on_update_checkbox_reprlist, names='value') def on_repr_name_text_value_changed(change): name = change['new'].strip() old = change['old'].strip() should_update = (self._real_time_update and old and name and name in REPRESENTATION_NAMES and name != change['old'].strip()) if should_update: component=self.widget_component_slider.value repr_index=self.widget_repr_slider.value self._view._remote_call('setRepresentation', target='Widget', args=[change['new'], {}, component, repr_index]) self._view._request_repr_parameters(component, repr_index) def on_component_or_repr_slider_value_changed(change): self._view._request_repr_parameters(component=self.widget_component_slider.value, repr_index=self.widget_repr_slider.value) self.widget_component_dropdown.options = tuple(self._view._ngl_component_names) if self.widget_accordion_repr_parameters.selected_index >= 0: self.widget_repr_parameters.name = self.widget_repr_name.value self.widget_repr_parameters.repr_index = self.widget_repr_slider.value self.widget_repr_parameters.component_index = self.widget_component_slider.value def on_repr_selection_value_changed(change): if self._real_time_update: component = self.widget_component_slider.value repr_index = self.widget_repr_slider.value self._view._set_selection(change['new'], component=component, repr_index=repr_index) def on_change_component_dropdown(change): choice = change['new'] if choice: self.widget_component_slider.value = self._view._ngl_component_names.index(choice) self.widget_component_dropdown.observe(on_change_component_dropdown, names='value') self.widget_repr_slider.observe(on_component_or_repr_slider_value_changed, names='value') self.widget_component_slider.observe(on_component_or_repr_slider_value_changed, names='value') self.widget_repr_name.observe(on_repr_name_text_value_changed, names='value') repr_selection.observe(on_repr_selection_value_changed, names='value') self.widget_repr_control_buttons = self._make_button_repr_control(self.widget_component_slider, self.widget_repr_slider, repr_selection) blank_box = Box([Label("")]) all_kids = [self.widget_repr_control_buttons, blank_box, self.widget_repr_add, self.widget_component_dropdown, self.widget_repr_name, repr_selection, self.widget_component_slider, self.widget_repr_slider, self.widget_repr_choices, self.widget_accordion_repr_parameters ] vbox = VBox(all_kids) self._view._request_repr_parameters(component=self.widget_component_slider.value, repr_index=self.widget_repr_slider.value) self.widget_repr = _relayout_master(vbox, width='100%') self._refresh(self.widget_component_slider, self.widget_repr_slider) setattr(self.widget_repr, "_saved_widgets", []) for _box in self.widget_repr.children: if hasattr(_box, 'children'): for kid in _box.children: self.widget_repr._saved_widgets.append(kid) return self.widget_repr
class Visualizer(object): def __init__(self, fper='morgan', smiles='c1ccccc1O', dpi=200): self.initialize_ipython() if isinstance(fper, str): self.fper = features.get(fper) else: self.fper = fper self.smiles_input = Text(smiles, description='smiles') self.smiles_input.on_submit(self.update_smiles) self.smiles_input.observe(self.typing) self.valid = Valid(True) self.dropdown = Dropdown(options=[], description='bit') self.dropdown.observe(self.plot) self.dpi_input = Text(str(dpi), description='dpi') self.dpi_input.on_submit(self.plot) self.ui = VBox([ HTML('<h2>Visualizer</h2>'), HBox([self.smiles_input, self.valid]), self.dropdown, self.dpi_input ]) self.update_smiles(None) self.display() @staticmethod def initialize_ipython(): ipython = get_ipython() try: ipython.magic('matplotlib inline') except: pass def typing(self, _): self.valid.visible = False @property def dpi(self): try: return int(self.dpi_input.value) except: return 50 @dpi.setter def dpi(self, value): self.dpi_input.value = str(value) def display(self): display(self.ui) def update_smiles(self, _): try: self._mol = core.Mol.from_smiles(self.smiles_input.value) self.valid.value = True except ValueError: self.valid.value = False return finally: self.valid.visible = True return self.calculate() def calculate(self): fp = self.fper.transform(self.mol) self.fp = fp[fp == 1].index self.fpg = self.fper.grad(self.mol).ix[self.fp] return self.update_dropdown() def update_dropdown(self): self.dropdown.options.append(self.fp[0]) self.dropdown.value = self.fp[0] self.dropdown.options = self.fp.tolist() return self.plot(self.dropdown.value) @property def mol(self): return self._mol @mol.setter def mol(self, mol): self._mol = mol self.smiles_input.value = mol.to_smiles() self.calculate() @property def current_smiles(self): return self.smiles_input.value @property def current_bit(self): return self.dropdown.value def plot(self, _): clear_output() plt.clf() plt.rcParams['savefig.dpi'] = self.dpi vis.plot_weights(self.mol, self.fpg.ix[self.current_bit], quality=4, ax=plt.gca())
import numpy as np anatomicalROI = WMA_pyFuncs.roiFromAtlas(atlasImg, roiNum) get_ipython().run_line_magic('matplotlib', 'inline') plotting.plot_roi(roi_img=WMA_pyFuncs.alignROItoReference( anatomicalROI, t1img), bg_img=t1img, cut_coords=[xCoord, yCoord, zCoord]) from ipywidgets import Dropdown interact(rotateAndPlotWrapper, roiNum=Dropdown(options=dropDownList, value=2, description="anatomicalLabel"), xCoord=IntSlider(min=np.min(convertedBoundCoords[:, 0].astype(int)), max=np.max(convertedBoundCoords[:, 0].astype(int)), step=1, continuous_update=False), yCoord=IntSlider(min=np.min(convertedBoundCoords[:, 1].astype(int)), max=np.max(convertedBoundCoords[:, 1].astype(int)), step=1, continuous_update=False), zCoord=IntSlider(min=np.min(convertedBoundCoords[:, 2].astype(int)), max=np.max(convertedBoundCoords[:, 2].astype(int)), step=1, continuous_update=False)) # In[ ]:
def __init__(self, path=os.getcwd(), filename='', title='', width=300, select_desc='Select', change_desc='Change', show_hidden=False, select_default=False, use_dir_icons=False, show_only_dirs=False, filter_pattern=None, **kwargs): """Initialize FileChooser object.""" self._default_path = path.rstrip(os.path.sep) self._default_filename = filename self._selected_path = None self._selected_filename = None self._show_hidden = show_hidden self._select_desc = select_desc self._change_desc = change_desc self._callback = None self._select_default = select_default self._use_dir_icons = use_dir_icons self._show_only_dirs = show_only_dirs self._filter_pattern = filter_pattern # Widgets self._pathlist = Dropdown(description="", layout=Layout(width='auto', grid_area='pathlist')) self._filename = Text( placeholder='output filename', layout=Layout(width='auto', grid_area='filename', display=(None, "none")[self._show_only_dirs]), disabled=self._show_only_dirs) self._dircontent = Select(rows=8, layout=Layout(width='auto', grid_area='dircontent')) self._cancel = Button(description='Cancel', layout=Layout(width='auto', display='none')) self._select = Button(description=self._select_desc, layout=Layout(width='auto')) self._title = HTML(value=title) if title == '': self._title.layout.display = 'none' # Widget observe handlers self._pathlist.observe(self._on_pathlist_select, names='value') self._dircontent.observe(self._on_dircontent_select, names='value') self._filename.observe(self._on_filename_change, names='value') self._select.on_click(self._on_select_click) self._cancel.on_click(self._on_cancel_click) # Selected file label self._label = HTML(value=self._LBL_TEMPLATE.format( self._LBL_NOFILE, 'black'), placeholder='', description='') # Layout self._gb = VBox( children=[self._pathlist, self._filename, self._dircontent], layout=Layout(width=f'{width}px')) # self._gb = GridBox( # children=[ # self._pathlist, # self._filename, # self._dircontent # ], # layout=Layout( # display='none', # width=f'{width}px', # grid_gap='0px 0px', # grid_template_rows='auto auto', # grid_template_columns='60% 40%', # grid_template_areas=''' # 'pathlist {}' # 'dircontent dircontent' # '''.format(('filename', 'pathlist')[self._show_only_dirs]) # ) # ) # buttonbar = HBox( # children=[ # self._select, # self._cancel, # self._label # ], # layout=Layout(width='auto') # ) buttonbar = VBox(children=[self._select, self._cancel, self._label], layout=Layout(width='auto')) # Call setter to set initial form values self._set_form_values(self._default_path, self._default_filename) # Use the defaults as the selected values if self._select_default: self._apply_selection() # Call VBox super class __init__ super().__init__(children=[ self._title, self._gb, buttonbar, ], layout=Layout(width='auto'), **kwargs)
def file_loading(name, old, new): """Update self.model when user requests a list of files to be uploaded""" traits = [("value_{}".format(i), Unicode(sync=True)) for i in range(len(new))] file_widget.add_traits(**dict(traits)) for i in range(len(new)): file_widget.on_trait_change(file_loaded, "value_{}".format(i)) # file_names are uniqe, we ignore any duplicates old_fnames = self.model.file_names file_names = [fn for fn in new if fn not in old_fnames] old_len = len(old_fnames) new_len = len(file_names) self.model.file_names.extend(file_names) self.model.atom_types.extend(["H" for i in range(new_len)]) self.model.centrum_nums.extend([[1, 1, 1] for i in range(new_len)]) old_children = list(upload_area.children) new_children = [] options = {"-": 0} for i, file_name in zip(range(old_len, new_len), file_names): j = "_{}".format(i) element_picker = Dropdown( description=file_name, value="", options=[""], color="Black", height=32, width=32, font_size=14 ) element_picker.add_traits(**{j: Unicode(sync=True)}) link((element_picker, "value"), (element_picker, j)) element_picker.on_trait_change(centrum_changed, j) atomnum_picker1 = Dropdown(options=options, value=0, color="Black", height=32, font_size=14) atomnum_picker2 = Dropdown(options=options, value=0, color="Black", height=32, font_size=14) atomnum_picker3 = Dropdown(options=options, value=0, color="Black", height=32, font_size=14) link((atomnum_picker1, "options"), (atomnum_picker2, "options")) link((atomnum_picker2, "options"), (atomnum_picker3, "options")) atomnum_picker1.add_traits(**{j: Int(sync=True)}) atomnum_picker2.add_traits(**{j: Int(sync=True)}) atomnum_picker3.add_traits(**{j: Int(sync=True)}) link((atomnum_picker1, "value"), (atomnum_picker1, j)) link((atomnum_picker2, "value"), (atomnum_picker2, j)) link((atomnum_picker3, "value"), (atomnum_picker3, j)) atomnum_picker1.on_trait_change(atomnum_changed, j) atomnum_picker2.on_trait_change(atomnum_changed, j) atomnum_picker3.on_trait_change(atomnum_changed, j) line = HBox([element_picker, atomnum_picker1, atomnum_picker2, atomnum_picker3]) new_children.append(line) upload_area.children = old_children + new_children
def __init__(self, settings, test_name=None, default_window='hanning'): if default_window is None: default_window = 'None' self.settings = settings self.test_name = test_name self.dataset = datastructure.DataSet() # the 'out' construction is to refresh the text output at each update # to stop text building up in the widget display self.out = Output() self.out_top = Output(layout=Layout(height='120px')) # Initialise variables self.current_view = 'Time' self.N_frames = 1 self.overlap = 0.5 self.iw_fft_power = 0 self.iw_tf_power = 0 self.legend_loc = 'lower right' # puts plot inside widget so can have buttons next to plot self.outplot = Output(layout=Layout(width='85%')) # BUTTONS items_measure = [ 'Log Data', 'Delete Last Measurement', 'Reset (clears all data)', 'Load Data' ] items_view = ['View Time', 'View FFT', 'View TF'] items_calc = ['Calc FFT', 'Calc TF', 'Calc TF average'] items_axes = ['xmin', 'xmax', 'ymin', 'ymax'] items_save = ['Save Dataset', 'Save Figure'] items_iw = ['multiply iw', 'divide iw'] items_legend = ['Left', 'on/off', 'Right'] self.buttons_measure = [ Button(description=i, layout=Layout(width='33%')) for i in items_measure ] self.buttons_measure[0].button_style = 'success' self.buttons_measure[0].style.font_weight = 'bold' self.buttons_measure[1].button_style = 'warning' self.buttons_measure[1].style.font_weight = 'bold' self.buttons_measure[2].button_style = 'danger' self.buttons_measure[2].style.font_weight = 'bold' self.buttons_measure[3].button_style = 'primary' self.buttons_measure[3].style.font_weight = 'bold' self.buttons_view = [ Button(description=i, layout=Layout(width='95%')) for i in items_view ] self.buttons_view[0].button_style = 'info' self.buttons_view[1].button_style = 'info' self.buttons_view[2].button_style = 'info' self.buttons_calc = [ Button(description=i, layout=Layout(width='99%')) for i in items_calc ] self.buttons_calc[0].button_style = 'primary' self.buttons_calc[1].button_style = 'primary' self.buttons_calc[2].button_style = 'primary' self.buttons_iw_fft = [ Button(description=i, layout=Layout(width='50%')) for i in items_iw ] self.buttons_iw_fft[0].button_style = 'info' self.buttons_iw_fft[1].button_style = 'info' self.buttons_iw_tf = [ Button(description=i, layout=Layout(width='50%')) for i in items_iw ] self.buttons_iw_tf[0].button_style = 'info' self.buttons_iw_tf[1].button_style = 'info' self.buttons_match = Button(description='Match Amplitudes', layout=Layout(width='99%')) self.buttons_match.button_style = 'info' self.buttons_save = [ Button(description=i, layout=Layout(width='50%')) for i in items_save ] self.buttons_save[0].button_style = 'success' self.buttons_save[0].style.font_weight = 'bold' self.buttons_save[1].button_style = 'success' self.buttons_save[1].style.font_weight = 'bold' self.button_warning = Button( description= 'WARNING: Data may be clipped. Press here to delete last measurement.', layout=Layout(width='100%')) self.button_warning.button_style = 'danger' self.button_warning.style.font_weight = 'bold' self.button_X = Button(description='Auto X', layout=Layout(width='95%')) self.button_Y = Button(description='Auto Y', layout=Layout(width='95%')) self.button_X.button_style = 'success' self.button_Y.button_style = 'success' self.buttons_legend = [ Button(description=i, layout=Layout(width='31%')) for i in items_legend ] self.buttons_legend_toggle = ToggleButton(description='Click-and-drag', layout=Layout( width='95%', alignitems='start')) # TEXT/LABELS/DROPDOWNS self.item_iw_fft_label = Label(value='iw power={}'.format(0), layout=Layout(width='100%')) self.item_iw_tf_label = Label(value='iw power={}'.format(0), layout=Layout(width='100%')) self.item_label = Label(value="Frame length = {:.2f} seconds.".format( settings.stored_time / (self.N_frames - self.overlap * self.N_frames + self.overlap))) self.item_axis_label = Label(value="Axes control:", layout=Layout(width='95%')) self.item_view_label = Label(value="View data:", layout=Layout(width='95%')) self.item_legend_label = Label(value="Legend position:", layout=Layout(width='95%')) self.item_blank_label = Label(value="", layout=Layout(width='95%')) self.text_axes = [ FloatText(value=0, description=i, layout=Layout(width='95%')) for i in items_axes ] self.text_axes = [self.button_X] + [self.button_Y] + self.text_axes self.drop_window = Dropdown(options=['None', 'hanning'], value=default_window, description='Window:', layout=Layout(width='99%')) self.slide_Nframes = IntSlider(value=1, min=1, max=30, step=1, description='N_frames:', continuous_update=True, readout=False, layout=Layout(width='99%')) self.text_Nframes = IntText(value=1, description='N_frames:', layout=Layout(width='99%')) # VERTICAL GROUPS group0 = VBox([ self.buttons_calc[0], self.drop_window, HBox(self.buttons_iw_fft) ], layout=Layout(width='33%')) group1 = VBox([ self.buttons_calc[1], self.drop_window, self.slide_Nframes, self.text_Nframes, self.item_label, HBox(self.buttons_iw_tf), self.buttons_match ], layout=Layout(width='33%')) group2 = VBox([ self.buttons_calc[2], self.drop_window, HBox(self.buttons_iw_tf), self.buttons_match ], layout=Layout(width='33%')) group_view = VBox( [self.item_axis_label] + self.text_axes + [self.item_legend_label, self.buttons_legend_toggle] + [HBox(self.buttons_legend), self.item_view_label] + self.buttons_view, layout=Layout(width='20%')) # ASSEMBLE display(self.out_top) display(HBox([self.button_warning])) display(HBox(self.buttons_measure)) display(HBox([self.outplot, group_view])) display(HBox([group0, group1, group2])) display(HBox(self.buttons_save)) self.button_warning.layout.visibility = 'hidden' # second part to putting plot inside widget with self.outplot: self.p = plotting.PlotData(figsize=(7.5, 4)) ## Make buttons/boxes interactive self.text_axes[2].observe(self.xmin, names='value') self.text_axes[3].observe(self.xmax, names='value') self.text_axes[4].observe(self.ymin, names='value') self.text_axes[5].observe(self.ymax, names='value') self.button_X.on_click(self.auto_x) self.button_Y.on_click(self.auto_y) self.buttons_legend[0].on_click(self.legend_left) self.buttons_legend[1].on_click(self.legend_onoff) self.buttons_legend[2].on_click(self.legend_right) self.buttons_legend_toggle.observe(self.legend_toggle) self.slide_Nframes.observe(self.nframes_slide) self.text_Nframes.observe(self.nframes_text) self.buttons_measure[0].on_click(self.measure) self.buttons_measure[1].on_click(self.undo) self.buttons_measure[2].on_click(self.reset) self.buttons_measure[3].on_click(self.load_data) self.buttons_view[0].on_click(self.view_time) self.buttons_view[1].on_click(self.view_fft) self.buttons_view[2].on_click(self.view_tf) self.buttons_calc[0].on_click(self.fft) self.buttons_calc[1].on_click(self.tf) self.buttons_calc[2].on_click(self.tf_av) self.buttons_iw_fft[0].on_click(self.xiw_fft) self.buttons_iw_fft[1].on_click(self.diw_fft) self.buttons_iw_tf[0].on_click(self.xiw_tf) self.buttons_iw_tf[1].on_click(self.diw_tf) self.buttons_match.on_click(self.match) self.buttons_save[0].on_click(self.save_data) self.buttons_save[1].on_click(self.save_fig) self.button_warning.on_click(self.undo) self.refresh_buttons() # Put output text at bottom of display display(self.out) with self.out_top: try: streams.start_stream(settings) self.rec = streams.REC except: print('Data stream not initialised.') print( 'Possible reasons: pyaudio or PyDAQmx not installed, or acquisition hardware not connected.' ) print('Please note that it won' 't be possible to log data.')
def _field_folder(self, **kwargs): """Folder that houses field GUI controls.""" folder = super(DemoUniverse, self)._field_folder(**kwargs) uni_field_lists = _ListDict([ ('Hydrogenic', ['1s', '2s', '2px', '2py', '2pz', '3s', '3px', '3py', '3pz', '3d-2', '3d-1', '3d0', '3d+1', '3d+2']), ('Gaussian', ['s', 'px', 'py', 'pz', 'd200', 'd110', 'd101', 'd020', 'd011', 'd002', 'f300', 'f210', 'f201', 'f120', 'f111', 'f102', 'f030', 'f021', 'f012', 'f003']), ('SolidHarmonic', [str(i) for i in range(8)])]) kind_widgets = _ListDict([ (key, Dropdown(options=vals)) for key, vals in uni_field_lists.items()]) ml_widgets = _ListDict([ (str(l), Dropdown(options=[str(i) for i in range(-l, l+1)])) for l in range(8)]) fopts = list(uni_field_lists.keys()) folder.update(kind_widgets, relayout=True) folder.update(ml_widgets, relayout=True) def _field(c): fk = uni_field_lists[c.new][0] for scn in self.active(): scn.field = c.new scn.field_kind = fk folder.deactivate(c.old) folder[c.new].value = fk folder.activate(c.new, enable=True) if c.new == 'SolidHarmonic': folder.activate(fk, enable=True) else: aml = [key for key in folder._get(keys=True) if key.isnumeric()] if aml: folder.deactivate(*aml) folder._set_gui() def _field_kind(c): for scn in self.active(): scn.field_kind = c.new if scn.field == 'SolidHarmonic': scn.field_ml = folder[c.new].options[0] folder.activate(c.new, enable=True) folder.deactivate(c.old) if scn.field_ml != '0': folder.deactivate('0') else: aml = [i for i in folder._get(keys=True) if i.isnumeric()] if aml: folder.deactivate(*aml) folder._set_gui() def _field_ml(c): for scn in self.active(): scn.field_ml = c.new for key, obj in kind_widgets.items(): folder.deactivate(key) obj.observe(_field_kind, names='value') for key, obj in ml_widgets.items(): folder.deactivate(key) obj.observe(_field_ml, names='value') fopts = Dropdown(options=fopts) fopts.observe(_field, names='value') folder.insert(1, 'fopts', fopts) folder.activate('Hydrogenic', enable=True, update=True) folder.move_to_end('alpha', 'iso', 'nx', 'ny', 'nz') return folder
def _init_gui(self, **kwargs): """Initialize generic GUI controls and observe callbacks.""" mainopts = super(TensorContainer, self)._init_gui(**kwargs) scn = self.scenes[0] alo = Layout(width='74px') rlo = Layout(width='235px') if self._df is not None: scn.txx = self._df.loc[0, 'xx'] scn.txy = self._df.loc[0, 'xy'] scn.txz = self._df.loc[0, 'xz'] scn.tyx = self._df.loc[0, 'yx'] scn.tyy = self._df.loc[0, 'yy'] scn.tyz = self._df.loc[0, 'yz'] scn.tzx = self._df.loc[0, 'zx'] scn.tzy = self._df.loc[0, 'zy'] scn.tzz = self._df.loc[0, 'zz'] xs = [ FloatText(value=scn.txx, layout=alo), FloatText(value=scn.txy, layout=alo), FloatText(value=scn.txz, layout=alo) ] ys = [ FloatText(value=scn.tyx, layout=alo), FloatText(value=scn.tyy, layout=alo), FloatText(value=scn.tyz, layout=alo) ] zs = [ FloatText(value=scn.tzx, layout=alo), FloatText(value=scn.tzy, layout=alo), FloatText(value=scn.tzz, layout=alo) ] opt = [0] if self._df is None else [ int(x) for x in self._df.index.values ] tensorIndex = Dropdown(options=opt, value=opt[0], layout=rlo) tdxlabel = Label(value='Select the tensor index:') def _x0(c): for scn in self.active(): scn.txx = c.new def _x1(c): for scn in self.active(): scn.txy = c.new def _x2(c): for scn in self.active(): scn.txz = c.new def _y0(c): for scn in self.active(): scn.tyx = c.new def _y1(c): for scn in self.active(): scn.tyy = c.new def _y2(c): for scn in self.active(): scn.tyz = c.new def _z0(c): for scn in self.active(): scn.tzx = c.new def _z1(c): for scn in self.active(): scn.tzy = c.new def _z2(c): for scn in self.active(): scn.tzz = c.new xs[0].observe(_x0, names='value') xs[1].observe(_x1, names='value') xs[2].observe(_x2, names='value') ys[0].observe(_y0, names='value') ys[1].observe(_y1, names='value') ys[2].observe(_y2, names='value') zs[0].observe(_z0, names='value') zs[1].observe(_z1, names='value') zs[2].observe(_z2, names='value') rlo = Layout(width='234px') xbox = HBox(xs, layout=rlo) ybox = HBox(ys, layout=rlo) zbox = HBox(zs, layout=rlo) geom = Button(icon='cubes', description=' Geometry', layout=_wlo) def _change_tensor(tdx=0): carts = ['x', 'y', 'z'] for i, bra in enumerate(carts): for j, ket in enumerate(carts): if i == 0: xs[j].value = self._df.loc[tdx, bra + ket] elif i == 1: ys[j].value = self._df.loc[tdx, bra + ket] elif i == 2: zs[j].value = self._df.loc[tdx, bra + ket] def _geom(b): for scn in self.active(): scn.geom = not scn.geom def _tdx(c): for scn in self.active(): scn.tdx = c.new _change_tensor(c.new) geom.on_click(_geom) tensorIndex.observe(_tdx, names="value") mainopts.update([('geom', geom), ('tlbl', tdxlabel), ('tidx', tensorIndex), ('xbox', xbox), ('ybox', ybox), ('zbox', zbox)]) return mainopts
def _field_folder(self, **kwargs): """Folder that houses field GUI controls.""" folder = super(DemoUniverse, self)._field_folder(**kwargs) uni_field_lists = _ListDict([ ('Hydrogenic', [ '1s', '2s', '2px', '2py', '2pz', '3s', '3px', '3py', '3pz', '3d-2', '3d-1', '3d0', '3d+1', '3d+2' ]), ('Gaussian', [ 's', 'px', 'py', 'pz', 'd200', 'd110', 'd101', 'd020', 'd011', 'd002', 'f300', 'f210', 'f201', 'f120', 'f111', 'f102', 'f030', 'f021', 'f012', 'f003' ]), ('SolidHarmonic', [str(i) for i in range(8)]) ]) kind_widgets = _ListDict([(key, Dropdown(options=vals)) for key, vals in uni_field_lists.items()]) ml_widgets = _ListDict([ (str(l), Dropdown(options=[str(i) for i in range(-l, l + 1)])) for l in range(8) ]) fopts = list(uni_field_lists.keys()) folder.update(kind_widgets, relayout=True) folder.update(ml_widgets, relayout=True) def _field(c): fk = uni_field_lists[c.new][0] for scn in self.active(): scn.field = c.new scn.field_kind = fk folder.deactivate(c.old) folder[c.new].value = fk folder.activate(c.new, enable=True) if c.new == 'SolidHarmonic': folder.activate(fk, enable=True) else: aml = [ key for key in folder._get(keys=True) if key.isnumeric() ] if aml: folder.deactivate(*aml) folder._set_gui() def _field_kind(c): for scn in self.active(): scn.field_kind = c.new if scn.field == 'SolidHarmonic': scn.field_ml = folder[c.new].options[0] folder.activate(c.new, enable=True) folder.deactivate(c.old) if scn.field_ml != '0': folder.deactivate('0') else: aml = [i for i in folder._get(keys=True) if i.isnumeric()] if aml: folder.deactivate(*aml) folder._set_gui() def _field_ml(c): for scn in self.active(): scn.field_ml = c.new for key, obj in kind_widgets.items(): folder.deactivate(key) obj.observe(_field_kind, names='value') for key, obj in ml_widgets.items(): folder.deactivate(key) obj.observe(_field_ml, names='value') fopts = Dropdown(options=fopts) fopts.observe(_field, names='value') folder.insert(1, 'fopts', fopts) folder.activate('Hydrogenic', enable=True, update=True) folder.move_to_end('alpha', 'iso', 'nx', 'ny', 'nz') return folder
class MerraAQSpatial(PanelObject): def __init__(self, ptype='space', *args, **kwargs): self.title = 'MERRA2 AQ Spatial Plots' PanelObject.__init__(self, *args, **kwargs) self.ptype = ptype def getCP(self): self.setLabel() self.plon = 0.0 self.plat = 0.0 self.dateSelection = datetime.now() self.dateSelection = datetime(2020, 4, 23) self.dateLast = datetime(1950, 1, 1) self.selectVar = 'AOD' self.selectTime = 0 self.dateSW = DatePicker(description='Date', layout=Layout(width='250px'), value=self.dateSelection, disabled=False) self.myWidget1 = Dropdown(options=[ 'AOD', 'DUST_PM', 'SALT_PM', 'ORG_CARB', 'BLK_CARB', 'SO4', 'PM2.5' ], value='AOD', layout=Layout(width='250px'), description='Varibale:', disabled=False) self.myWidget2 = Dropdown(options=[], value=None, layout=Layout(width='250px'), description='Varibale:', disabled=False) self.myWidget3 = Dropdown(options=[], value=None, description='Date:', disabled=False) self.myWidget4 = Dropdown(options=[], value=None, layout=Layout(width='250px'), description='Time:', disabled=False) self.latSW = FloatSlider(min=-90.0, max=90.0, step=0.25, description='Lat:', disabled=False, continuous_update=False, orientation='horizontal', readout=True, readout_format='.2f') self.lonSW = FloatSlider(min=-180.0, max=180.0, step=0.25, description='Lon:', disabled=False, continuous_update=False, orientation='horizontal', readout=True, readout_format='.2f') self.plotBW = Button(description='Spatial Plot', disabled=False, layout={ 'width': '200px', 'border': '3px outset' }) if self.ptype == 'space': self.inpUSR.children += (HBox([ VBox([self.dateSW, self.myWidget4, self.myWidget1]), VBox([self.plotBW]) ], layout={'overflow': 'visible'}), ) else: self.inpUSR.children += (HBox([ VBox([self.dateSW, self.myWidget1]), VBox([self.timeplot, self.latSW, self.lonSW]) ], layout={'overflow': 'visible'}), ) self.year = np.arange(41) + 1980 self.mm = np.arange(12) + 1 # formatter = "{:02d}".format # self.months=[formatter(item) for item in mm] self.dateSW.observe(self.dateSWCB) self.myWidget2.options = self.mm self.myWidget1.observe(self.myCallback1, names='value') self.myWidget2.observe(self.myCallback2, names='value') self.date = np.arange(1, 30) + 1 self.time = np.arange(24) self.myWidget3.options = self.date self.myWidget4.options = self.time self.myWidget3.observe(self.myCallback3, names='value') self.myWidget4.observe(self.myCallback4, names='value') self.latSW.observe(self.latSWCB, names='value') self.lonSW.observe(self.lonSWCB, names='value') self.plotBW.on_click(self.plotObs) return self.cp def dateSWCB(self, change): if change['type'] == 'change': self.dateSelection = self.dateSW.value def myCallback1(self, b): self.selectVar = self.myWidget1.value # print(self.selectYear) def myCallback2(self, b): formatter = "{:02d}".format self.selectMonth = self.myWidget2.value self.selectMonth = formatter(self.selectMonth) # print(self.selectMonth) def myCallback3(self, b): formatter = "{:02d}".format self.selectDate = formatter(self.myWidget3.value) def myCallback4(self, b): self.selectTime = self.myWidget4.value def latSWCB(self, change): self.plat = self.latSW.value def lonSWCB(self, change): self.plon = self.lonSW.value def plotObs(self, b): if self.dateSelection.year < 1992: self.Vnumber = '100' elif self.dateSelection.year < 2001: self.Vnumber = '200' elif self.dateSelection.year < 2011: self.Vnumber = '300' else: self.Vnumber = '400' self.selectYear = self.dateSelection.strftime('%Y') self.selectMonth = self.dateSelection.strftime('%m') self.selectDate = self.dateSelection.strftime('%d') if self.dateSelection != self.dateLast: username = self.pwdDict['NASA Earth Data']['user'] password = self.pwdDict['NASA Earth Data']['password'] with self.out_cp: print('Accessing data...') self.opendap_url = 'https://goldsmr4.gesdisc.eosdis.nasa.gov/opendap/MERRA2/M2T1NXAER.5.12.4/' + self.selectYear + '/' + self.selectMonth + '/MERRA2_' + self.Vnumber + '.tavg1_2d_aer_Nx.' + self.selectYear + self.selectMonth + self.selectDate + '.nc4' session = setup_session(username, password, check_url=self.opendap_url) dataset = open_url(self.opendap_url, session=session) #dataset = open_url(opendap_url) lon = dataset['lon'][:] lat = dataset['lat'][:] self.lons, self.lats = np.meshgrid(lon, lat) t = int(self.selectTime) aod = np.squeeze(dataset['TOTEXTTAU'][t, :, :], axis=0) dust_pm = np.squeeze(dataset['DUSMASS25'][t, :, :], axis=0) * 1000000000.0 salt_pm = np.squeeze(dataset['SSSMASS25'][t, :, :], axis=0) * 1000000000.0 org_carb = np.squeeze(dataset['OCSMASS'][t, :, :], axis=0) * 1000000000.0 blk_carb = np.squeeze(dataset['BCSMASS'][t, :, :], axis=0) * 1000000000.0 so4 = np.squeeze(dataset['SO4SMASS'][t, :, :], axis=0) * 1000000000.0 pm25 = (1.375 * so4 + 1.6 * org_carb + blk_carb + dust_pm + salt_pm) self.vardict = { 'AOD': aod, 'DUST_PM': dust_pm, 'SALT_PM': salt_pm, 'ORG_CARB': org_carb, 'BLK_CARB': blk_carb, 'SO4': so4, 'PM2.5': pm25 } self.varlab = { 'AOD': 'Unitless', 'DUST_PM': r'$\mu g m^{-3}$', 'SALT_PM': r'$\mu g m^{-3}$', 'ORG_CARB': r'$\mu g m^{-3}$', 'BLK_CARB': r'$\mu g m^{-3}$', 'SO4': r'$\mu g m^{-3}$', 'PM2.5': r'$\mu g m^{-3}$' } var = self.vardict[self.selectVar] # Set the figure size, projection, and extent with self.out_cp: plt.ioff() self.out_cp.clear_output() fig = plt.figure(figsize=(8, 4)) ax = plt.axes(projection=ccrs.PlateCarree()) ax.set_global() #ax.set_extent([65, 100, 5.0,35.0]) ax.coastlines(resolution="110m", linewidth=1) ax.gridlines(linestyle='--', color='black') # Set contour levels, then draw the plot and a colorbar clevs = np.arange(230, 311, 5) plt.contourf(self.lons, self.lats, var, transform=ccrs.PlateCarree(), cmap=plt.cm.jet) formatter = "{:02d}".format tt = formatter(self.selectTime) plt.title('MERRA2 ' + self.selectVar + ',' + tt + ':00 UTC ' + self.selectYear + '-' + self.selectMonth + '-' + self.selectDate, size=14) cb = plt.colorbar(ax=ax, orientation="vertical", pad=0.02, aspect=16, shrink=0.8) cb.set_label(self.varlab[self.selectVar], size=12, rotation=90, labelpad=15) cb.ax.tick_params(labelsize=10) plt.show() self.dateLast = self.dateSelection
def _init_gui(self, **kwargs): """Initialize generic GUI controls and observe callbacks.""" mainopts = super(TensorContainer, self)._init_gui(**kwargs) scn = self.scenes[0] alo = Layout(width='74px') rlo = Layout(width='235px') if self._df is not None: scn.txx = self._df.loc[0,'xx'] scn.txy = self._df.loc[0,'xy'] scn.txz = self._df.loc[0,'xz'] scn.tyx = self._df.loc[0,'yx'] scn.tyy = self._df.loc[0,'yy'] scn.tyz = self._df.loc[0,'yz'] scn.tzx = self._df.loc[0,'zx'] scn.tzy = self._df.loc[0,'zy'] scn.tzz = self._df.loc[0,'zz'] xs = [FloatText(value=scn.txx , layout=alo), FloatText(value=scn.txy , layout=alo), FloatText(value=scn.txz , layout=alo)] ys = [FloatText(value=scn.tyx , layout=alo), FloatText(value=scn.tyy , layout=alo), FloatText(value=scn.tyz , layout=alo)] zs = [FloatText(value=scn.tzx , layout=alo), FloatText(value=scn.tzy , layout=alo), FloatText(value=scn.tzz , layout=alo)] #scale = FloatSlider(max=10.0, step=0.01, readout=True, value=1.0) opt = [0] if self._df is None else [int(x) for x in self._df.index.values] tensorIndex = Dropdown(options=opt, value=opt[0], layout=rlo) tdxlabel = Label(value='Select the tensor index:') def _x0(c): for scn in self.active(): scn.txx = c.new def _x1(c): for scn in self.active(): scn.txy = c.new def _x2(c): for scn in self.active(): scn.txz = c.new def _y0(c): for scn in self.active(): scn.tyx = c.new def _y1(c): for scn in self.active(): scn.tyy = c.new def _y2(c): for scn in self.active(): scn.tyz = c.new def _z0(c): for scn in self.active(): scn.tzx = c.new def _z1(c): for scn in self.active(): scn.tzy = c.new def _z2(c): for scn in self.active(): scn.tzz = c.new xs[0].observe(_x0, names='value') xs[1].observe(_x1, names='value') xs[2].observe(_x2, names='value') ys[0].observe(_y0, names='value') ys[1].observe(_y1, names='value') ys[2].observe(_y2, names='value') zs[0].observe(_z0, names='value') zs[1].observe(_z1, names='value') zs[2].observe(_z2, names='value') rlo = Layout(width='234px') xbox = HBox(xs, layout=rlo) ybox = HBox(ys, layout=rlo) zbox = HBox(zs, layout=rlo) geom = Button(icon='cubes', description=' Geometry', layout=_wlo) def _change_tensor(tdx=0): carts = ['x','y','z'] for i, bra in enumerate(carts): for j, ket in enumerate(carts): if i == 0: xs[j].value = self._df.loc[tdx,bra+ket] elif i == 1: ys[j].value = self._df.loc[tdx,bra+ket] elif i == 2: zs[j].value = self._df.loc[tdx,bra+ket] def _geom(b): for scn in self.active(): scn.geom = not scn.geom def _tdx(c): for scn in self.active(): scn.tdx = c.new _change_tensor(c.new) geom.on_click(_geom) tensorIndex.observe(_tdx, names="value") mainopts.update([('geom', geom), ('tlbl', tdxlabel), ('tidx', tensorIndex), ('xbox', xbox), ('ybox', ybox), ('zbox', zbox)]) return mainopts
class SubstrateTab(object): def __init__(self): self.output_dir = '.' # self.output_dir = 'tmpdir' self.figsize_width_substrate = 15.0 # allow extra for colormap self.figsize_height_substrate = 12.5 self.figsize_width_svg = 12.0 self.figsize_height_svg = 12.0 # self.fig = plt.figure(figsize=(7.2,6)) # this strange figsize results in a ~square contour plot self.first_time = True self.modulo = 1 self.use_defaults = True self.svg_delta_t = 1 self.substrate_delta_t = 1 self.svg_frame = 1 self.substrate_frame = 1 self.customized_output_freq = False self.therapy_activation_time = 1000000 self.max_svg_frame_pre_therapy = 1000000 self.max_substrate_frame_pre_therapy = 1000000 self.svg_xmin = 0 # Probably don't want to hardwire these if we allow changing the domain size # self.svg_xrange = 2000 # self.xmin = -1000. # self.xmax = 1000. # self.ymin = -1000. # self.ymax = 1000. # self.x_range = 2000. # self.y_range = 2000. self.show_nucleus = False self.show_edge = True # initial value self.field_index = 4 # self.field_index = self.mcds_field.value + 4 # define dummy size of mesh (set in the tool's primary module) self.numx = 0 self.numy = 0 self.title_str = '' tab_height = '600px' tab_height = '500px' constWidth = '180px' constWidth2 = '150px' tab_layout = Layout( width='900px', # border='2px solid black', height=tab_height, ) #overflow_y='scroll') max_frames = 1 # self.mcds_plot = interactive(self.plot_substrate, frame=(0, max_frames), continuous_update=False) # self.i_plot = interactive(self.plot_plots, frame=(0, max_frames), continuous_update=False) self.i_plot = interactive(self.plot_substrate, frame=(0, max_frames), continuous_update=False) # "plot_size" controls the size of the tab height, not the plot (rf. figsize for that) # NOTE: the Substrates Plot tab has an extra row of widgets at the top of it (cf. Cell Plots tab) svg_plot_size = '700px' svg_plot_size = '600px' svg_plot_size = '700px' svg_plot_size = '900px' self.i_plot.layout.width = svg_plot_size self.i_plot.layout.height = svg_plot_size self.fontsize = 20 # description='# cell frames', self.max_frames = BoundedIntText( min=0, max=99999, value=max_frames, description='# frames', layout=Layout(width='160px'), ) self.max_frames.observe(self.update_max_frames) # self.field_min_max = {'dummy': [0., 1.]} # NOTE: manually setting these for now (vs. parsing them out of data/initial.xml) self.field_min_max = { 'director signal': [0., 1.], 'cargo signal': [0., 1.] } # hacky I know, but make a dict that's got (key,value) reversed from the dict in the Dropdown below # self.field_dict = {0:'dummy'} self.field_dict = {0: 'director signal', 1: 'cargo signal'} self.mcds_field = Dropdown( options={ 'director signal': 0, 'cargo signal': 1 }, value=0, # description='Field', layout=Layout(width=constWidth)) # print("substrate __init__: self.mcds_field.value=",self.mcds_field.value) # self.mcds_field.observe(self.mcds_field_cb) self.mcds_field.observe(self.mcds_field_changed_cb) # self.field_cmap = Text( # value='viridis', # description='Colormap', # disabled=True, # layout=Layout(width=constWidth), # ) self.field_cmap = Dropdown( options=['viridis', 'jet', 'YlOrRd'], value='YlOrRd', # description='Field', layout=Layout(width=constWidth)) # self.field_cmap.observe(self.plot_substrate) self.field_cmap.observe(self.mcds_field_cb) self.cmap_fixed = Checkbox( description='Fix', disabled=False, # layout=Layout(width=constWidth2), ) self.save_min_max = Button( description='Save', #style={'description_width': 'initial'}, button_style= 'success', # 'success', 'info', 'warning', 'danger' or '' tooltip='Save min/max for this substrate', disabled=True, layout=Layout(width='90px')) def save_min_max_cb(b): # field_name = self.mcds_field.options[] # field_name = next(key for key, value in self.mcds_field.options.items() if value == self.mcds_field.value) field_name = self.field_dict[self.mcds_field.value] # print(field_name) # self.field_min_max = {'oxygen': [0., 30.], 'glucose': [0., 1.], 'H+ ions': [0., 1.], 'ECM': [0., 1.], 'NP1': [0., 1.], 'NP2': [0., 1.]} self.field_min_max[field_name][0] = self.cmap_min.value self.field_min_max[field_name][1] = self.cmap_max.value # print(self.field_min_max) self.save_min_max.on_click(save_min_max_cb) self.cmap_min = FloatText( description='Min', value=0, step=0.1, disabled=True, layout=Layout(width=constWidth2), ) self.cmap_min.observe(self.mcds_field_cb) self.cmap_max = FloatText( description='Max', value=38, step=0.1, disabled=True, layout=Layout(width=constWidth2), ) self.cmap_max.observe(self.mcds_field_cb) def cmap_fixed_cb(b): if (self.cmap_fixed.value): self.cmap_min.disabled = False self.cmap_max.disabled = False self.save_min_max.disabled = False else: self.cmap_min.disabled = True self.cmap_max.disabled = True self.save_min_max.disabled = True # self.mcds_field_cb() self.cmap_fixed.observe(cmap_fixed_cb) field_cmap_row2 = HBox([self.field_cmap, self.cmap_fixed]) # field_cmap_row3 = HBox([self.save_min_max, self.cmap_min, self.cmap_max]) items_auto = [ self.save_min_max, #layout=Layout(flex='3 1 auto', width='auto'), self.cmap_min, self.cmap_max, ] box_layout = Layout(display='flex', flex_flow='row', align_items='stretch', width='80%') field_cmap_row3 = Box(children=items_auto, layout=box_layout) #--------------------- self.cell_nucleus_toggle = Checkbox( description='nuclei', disabled=False, value=self.show_nucleus, # layout=Layout(width=constWidth2), ) def cell_nucleus_toggle_cb(b): # self.update() if (self.cell_nucleus_toggle.value): self.show_nucleus = True else: self.show_nucleus = False self.i_plot.update() self.cell_nucleus_toggle.observe(cell_nucleus_toggle_cb) #---- self.cell_edges_toggle = Checkbox( description='edges', disabled=False, value=self.show_edge, # layout=Layout(width=constWidth2), ) def cell_edges_toggle_cb(b): # self.update() if (self.cell_edges_toggle.value): self.show_edge = True else: self.show_edge = False self.i_plot.update() self.cell_edges_toggle.observe(cell_edges_toggle_cb) self.cells_toggle = Checkbox( description='Cells', disabled=False, value=True, # layout=Layout(width=constWidth2), ) def cells_toggle_cb(b): # self.update() self.i_plot.update() if (self.cells_toggle.value): self.cell_edges_toggle.disabled = False self.cell_nucleus_toggle.disabled = False else: self.cell_edges_toggle.disabled = True self.cell_nucleus_toggle.disabled = True self.cells_toggle.observe(cells_toggle_cb) #--------------------- self.substrates_toggle = Checkbox( description='Substrates', disabled=False, value=True, # layout=Layout(width=constWidth2), ) def substrates_toggle_cb(b): if (self.substrates_toggle.value): # seems bass-ackwards self.cmap_fixed.disabled = False self.cmap_min.disabled = False self.cmap_max.disabled = False self.mcds_field.disabled = False self.field_cmap.disabled = False else: self.cmap_fixed.disabled = True self.cmap_min.disabled = True self.cmap_max.disabled = True self.mcds_field.disabled = True self.field_cmap.disabled = True self.substrates_toggle.observe(substrates_toggle_cb) self.grid_toggle = Checkbox( description='grid', disabled=False, value=True, # layout=Layout(width=constWidth2), ) def grid_toggle_cb(b): # self.update() self.i_plot.update() self.grid_toggle.observe(grid_toggle_cb) # field_cmap_row3 = Box([self.save_min_max, self.cmap_min, self.cmap_max]) # mcds_tab = widgets.VBox([mcds_dir, mcds_plot, mcds_play], layout=tab_layout) # mcds_params = VBox([self.mcds_field, field_cmap_row2, field_cmap_row3, self.max_frames]) # mcds_dir # mcds_params = VBox([self.mcds_field, field_cmap_row2, field_cmap_row3,]) # mcds_dir # self.tab = HBox([mcds_params, self.mcds_plot], layout=tab_layout) help_label = Label('select slider: drag or left/right arrows') # row1 = Box([help_label, Box( [self.max_frames, self.mcds_field, self.field_cmap], layout=Layout(border='0px solid black', row1a = Box([self.max_frames, self.mcds_field, self.field_cmap], layout=Layout(border='1px solid black', width='50%', height='', align_items='stretch', flex_direction='row', display='flex')) row1b = Box([ self.cells_toggle, self.cell_nucleus_toggle, self.cell_edges_toggle ], layout=Layout(border='1px solid black', width='50%', height='', align_items='stretch', flex_direction='row', display='flex')) row1 = HBox([row1a, Label('.....'), row1b]) row2a = Box([self.cmap_fixed, self.cmap_min, self.cmap_max], layout=Layout(border='1px solid black', width='50%', height='', align_items='stretch', flex_direction='row', display='flex')) # row2b = Box( [self.substrates_toggle, self.grid_toggle], layout=Layout(border='1px solid black', row2b = Box([ self.substrates_toggle, ], layout=Layout(border='1px solid black', width='50%', height='', align_items='stretch', flex_direction='row', display='flex')) # row2 = HBox( [row2a, self.substrates_toggle, self.grid_toggle]) row2 = HBox([row2a, Label('.....'), row2b]) if (hublib_flag): self.download_button = Download('mcds.zip', style='warning', icon='cloud-download', tooltip='Download data', cb=self.download_cb) self.download_svg_button = Download( 'svg.zip', style='warning', icon='cloud-download', tooltip='You need to allow pop-ups in your browser', cb=self.download_svg_cb) download_row = HBox([ self.download_button.w, self.download_svg_button.w, Label("Download all cell plots (browser must allow pop-ups).") ]) # box_layout = Layout(border='0px solid') controls_box = VBox([row1, row2]) # ,width='50%', layout=box_layout) self.tab = VBox([controls_box, self.i_plot, download_row]) else: # self.tab = VBox([row1, row2]) self.tab = VBox([row1, row2, self.i_plot]) #--------------------------------------------------- def update_dropdown_fields(self, data_dir): # print('update_dropdown_fields called --------') self.output_dir = data_dir tree = None try: fname = os.path.join(self.output_dir, "initial.xml") tree = ET.parse(fname) xml_root = tree.getroot() except: print("Cannot open ", fname, " to read info, e.g., names of substrate fields.") return xml_root = tree.getroot() self.field_min_max = {} self.field_dict = {} dropdown_options = {} uep = xml_root.find('.//variables') comment_str = "" field_idx = 0 if (uep): for elm in uep.findall('variable'): # print("-----> ",elm.attrib['name']) self.field_min_max[elm.attrib['name']] = [0., 1.] self.field_dict[field_idx] = elm.attrib['name'] dropdown_options[elm.attrib['name']] = field_idx field_idx += 1 # constWidth = '180px' # print('options=',dropdown_options) self.mcds_field.value = 0 self.mcds_field.options = dropdown_options # self.mcds_field = Dropdown( # # options={'oxygen': 0, 'glucose': 1}, # options=dropdown_options, # value=0, # # description='Field', # layout=Layout(width=constWidth) # ) # def update_max_frames_expected(self, value): # called when beginning an interactive Run # self.max_frames.value = value # assumes naming scheme: "snapshot%08d.svg" # self.mcds_plot.children[0].max = self.max_frames.value #------------------------------------------------------------------------------ def update_params(self, config_tab, user_params_tab): # xml_root.find(".//x_min").text = str(self.xmin.value) # xml_root.find(".//x_max").text = str(self.xmax.value) # xml_root.find(".//dx").text = str(self.xdelta.value) # xml_root.find(".//y_min").text = str(self.ymin.value) # xml_root.find(".//y_max").text = str(self.ymax.value) # xml_root.find(".//dy").text = str(self.ydelta.value) # xml_root.find(".//z_min").text = str(self.zmin.value) # xml_root.find(".//z_max").text = str(self.zmax.value) # xml_root.find(".//dz").text = str(self.zdelta.value) self.xmin = config_tab.xmin.value self.xmax = config_tab.xmax.value self.x_range = self.xmax - self.xmin self.svg_xrange = self.xmax - self.xmin self.ymin = config_tab.ymin.value self.ymax = config_tab.ymax.value self.y_range = self.ymax - self.ymin self.numx = math.ceil( (self.xmax - self.xmin) / config_tab.xdelta.value) self.numy = math.ceil( (self.ymax - self.ymin) / config_tab.ydelta.value) if (self.x_range > self.y_range): ratio = self.y_range / self.x_range self.figsize_width_substrate = 15.0 # allow extra for colormap self.figsize_height_substrate = 12.5 * ratio self.figsize_width_svg = 12.0 self.figsize_height_svg = 12.0 * ratio else: # x < y ratio = self.x_range / self.y_range self.figsize_width_substrate = 15.0 * ratio self.figsize_height_substrate = 12.5 self.figsize_width_svg = 12.0 * ratio self.figsize_height_svg = 12.0 self.svg_flag = config_tab.toggle_svg.value self.substrates_flag = config_tab.toggle_mcds.value # print("substrates: update_params(): svg_flag, toggle=",self.svg_flag,config_tab.toggle_svg.value) # print("substrates: update_params(): self.substrates_flag = ",self.substrates_flag) self.svg_delta_t = config_tab.svg_interval.value self.substrate_delta_t = config_tab.mcds_interval.value self.modulo = int(self.substrate_delta_t / self.svg_delta_t) # print("substrates: update_params(): modulo=",self.modulo) if self.customized_output_freq: # self.therapy_activation_time = user_params_tab.therapy_activation_time.value # NOTE: edit for user param name # print("substrates: update_params(): therapy_activation_time=",self.therapy_activation_time) self.max_svg_frame_pre_therapy = int(self.therapy_activation_time / self.svg_delta_t) self.max_substrate_frame_pre_therapy = int( self.therapy_activation_time / self.substrate_delta_t) #------------------------------------------------------------------------------ # def update(self, rdir): # Called from driver module (e.g., pc4*.py) (among other places?) def update(self, rdir=''): # with debug_view: # print("substrates: update rdir=", rdir) # print("substrates: update rdir=", rdir) if rdir: self.output_dir = rdir # print('update(): self.output_dir = ', self.output_dir) if self.first_time: # if True: self.first_time = False full_xml_filename = Path( os.path.join(self.output_dir, 'config.xml')) # print("substrates: update(), config.xml = ",full_xml_filename) # self.num_svgs = len(glob.glob(os.path.join(self.output_dir, 'snap*.svg'))) # self.num_substrates = len(glob.glob(os.path.join(self.output_dir, 'output*.xml'))) # print("substrates: num_svgs,num_substrates =",self.num_svgs,self.num_substrates) # argh - no! If no files created, then denom = -1 # self.modulo = int((self.num_svgs - 1) / (self.num_substrates - 1)) # print("substrates: update(): modulo=",self.modulo) if full_xml_filename.is_file(): tree = ET.parse( full_xml_filename ) # this file cannot be overwritten; part of tool distro xml_root = tree.getroot() self.svg_delta_t = int(xml_root.find(".//SVG//interval").text) self.substrate_delta_t = int( xml_root.find(".//full_data//interval").text) # print("substrates: svg,substrate delta_t values=",self.svg_delta_t,self.substrate_delta_t) self.modulo = int(self.substrate_delta_t / self.svg_delta_t) # print("substrates: update(): modulo=",self.modulo) # all_files = sorted(glob.glob(os.path.join(self.output_dir, 'output*.xml'))) # if the substrates/MCDS all_files = sorted( glob.glob(os.path.join(self.output_dir, 'snap*.svg'))) # if .svg if len(all_files) > 0: last_file = all_files[-1] self.max_frames.value = int( last_file[-12:-4]) # assumes naming scheme: "snapshot%08d.svg" else: substrate_files = sorted( glob.glob(os.path.join(self.output_dir, 'output*.xml'))) if len(substrate_files) > 0: last_file = substrate_files[-1] self.max_frames.value = int(last_file[-12:-4]) def download_svg_cb(self): file_str = os.path.join(self.output_dir, '*.svg') # print('zip up all ',file_str) with zipfile.ZipFile('svg.zip', 'w') as myzip: for f in glob.glob(file_str): myzip.write(f, os.path.basename( f)) # 2nd arg avoids full filename path in the archive def download_cb(self): file_xml = os.path.join(self.output_dir, '*.xml') file_mat = os.path.join(self.output_dir, '*.mat') # print('zip up all ',file_str) with zipfile.ZipFile('mcds.zip', 'w') as myzip: for f in glob.glob(file_xml): myzip.write(f, os.path.basename( f)) # 2nd arg avoids full filename path in the archive for f in glob.glob(file_mat): myzip.write(f, os.path.basename(f)) def update_max_frames(self, _b): self.i_plot.children[0].max = self.max_frames.value def mcds_field_changed_cb(self, b): # print("mcds_field_changed_cb: self.mcds_field.value=",self.mcds_field.value) if (self.mcds_field.value == None): return self.field_index = self.mcds_field.value + 4 field_name = self.field_dict[self.mcds_field.value] # print('mcds_field_cb: '+field_name) self.cmap_min.value = self.field_min_max[field_name][0] self.cmap_max.value = self.field_min_max[field_name][1] self.i_plot.update() def mcds_field_cb(self, b): #self.field_index = self.mcds_field.value # self.field_index = self.mcds_field.options.index(self.mcds_field.value) + 4 # self.field_index = self.mcds_field.options[self.mcds_field.value] self.field_index = self.mcds_field.value + 4 # field_name = self.mcds_field.options[self.mcds_field.value] # self.cmap_min.value = self.field_min_max[field_name][0] # oxygen, etc # self.cmap_max.value = self.field_min_max[field_name][1] # oxygen, etc # self.field_index = self.mcds_field.value + 4 # print('field_index=',self.field_index) self.i_plot.update() #--------------------------------------------------------------------------- def circles(self, x, y, s, c='b', vmin=None, vmax=None, **kwargs): """ See https://gist.github.com/syrte/592a062c562cd2a98a83 Make a scatter plot of circles. Similar to plt.scatter, but the size of circles are in data scale. Parameters ---------- x, y : scalar or array_like, shape (n, ) Input data s : scalar or array_like, shape (n, ) Radius of circles. c : color or sequence of color, optional, default : 'b' `c` can be a single color format string, or a sequence of color specifications of length `N`, or a sequence of `N` numbers to be mapped to colors using the `cmap` and `norm` specified via kwargs. Note that `c` should not be a single numeric RGB or RGBA sequence because that is indistinguishable from an array of values to be colormapped. (If you insist, use `color` instead.) `c` can be a 2-D array in which the rows are RGB or RGBA, however. vmin, vmax : scalar, optional, default: None `vmin` and `vmax` are used in conjunction with `norm` to normalize luminance data. If either are `None`, the min and max of the color array is used. kwargs : `~matplotlib.collections.Collection` properties Eg. alpha, edgecolor(ec), facecolor(fc), linewidth(lw), linestyle(ls), norm, cmap, transform, etc. Returns ------- paths : `~matplotlib.collections.PathCollection` Examples -------- a = np.arange(11) circles(a, a, s=a*0.2, c=a, alpha=0.5, ec='none') plt.colorbar() License -------- This code is under [The BSD 3-Clause License] (http://opensource.org/licenses/BSD-3-Clause) """ if np.isscalar(c): kwargs.setdefault('color', c) c = None if 'fc' in kwargs: kwargs.setdefault('facecolor', kwargs.pop('fc')) if 'ec' in kwargs: kwargs.setdefault('edgecolor', kwargs.pop('ec')) if 'ls' in kwargs: kwargs.setdefault('linestyle', kwargs.pop('ls')) if 'lw' in kwargs: kwargs.setdefault('linewidth', kwargs.pop('lw')) # You can set `facecolor` with an array for each patch, # while you can only set `facecolors` with a value for all. zipped = np.broadcast(x, y, s) patches = [Circle((x_, y_), s_) for x_, y_, s_ in zipped] collection = PatchCollection(patches, **kwargs) if c is not None: c = np.broadcast_to(c, zipped.shape).ravel() collection.set_array(c) collection.set_clim(vmin, vmax) ax = plt.gca() ax.add_collection(collection) ax.autoscale_view() # plt.draw_if_interactive() if c is not None: plt.sci(collection) # return collection #------------------------------------------------------------ # def plot_svg(self, frame, rdel=''): def plot_svg(self, frame): # global current_idx, axes_max global current_frame current_frame = frame fname = "snapshot%08d.svg" % frame full_fname = os.path.join(self.output_dir, fname) # with debug_view: # print("plot_svg:", full_fname) # print("-- plot_svg:", full_fname) if not os.path.isfile(full_fname): print("Once output files are generated, click the slider.") return xlist = deque() ylist = deque() rlist = deque() rgb_list = deque() # print('\n---- ' + fname + ':') # tree = ET.parse(fname) tree = ET.parse(full_fname) root = tree.getroot() # print('--- root.tag ---') # print(root.tag) # print('--- root.attrib ---') # print(root.attrib) # print('--- child.tag, child.attrib ---') numChildren = 0 for child in root: # print(child.tag, child.attrib) # print("keys=",child.attrib.keys()) if self.use_defaults and ('width' in child.attrib.keys()): self.axes_max = float(child.attrib['width']) # print("debug> found width --> axes_max =", axes_max) if child.text and "Current time" in child.text: svals = child.text.split() # remove the ".00" on minutes self.title_str += " cells: " + svals[2] + "d, " + svals[ 4] + "h, " + svals[7][:-3] + "m" # self.cell_time_mins = int(svals[2])*1440 + int(svals[4])*60 + int(svals[7][:-3]) # self.title_str += " cells: " + str(self.cell_time_mins) + "m" # rwh # print("width ",child.attrib['width']) # print('attrib=',child.attrib) # if (child.attrib['id'] == 'tissue'): if ('id' in child.attrib.keys()): # print('-------- found tissue!!') tissue_parent = child break # print('------ search tissue') cells_parent = None for child in tissue_parent: # print('attrib=',child.attrib) if (child.attrib['id'] == 'cells'): # print('-------- found cells, setting cells_parent') cells_parent = child break numChildren += 1 num_cells = 0 # print('------ search cells') for child in cells_parent: # print(child.tag, child.attrib) # print('attrib=',child.attrib) for circle in child: # two circles in each child: outer + nucleus # circle.attrib={'cx': '1085.59','cy': '1225.24','fill': 'rgb(159,159,96)','r': '6.67717','stroke': 'rgb(159,159,96)','stroke-width': '0.5'} # print(' --- cx,cy=',circle.attrib['cx'],circle.attrib['cy']) xval = float(circle.attrib['cx']) # map SVG coords into comp domain # xval = (xval-self.svg_xmin)/self.svg_xrange * self.x_range + self.xmin xval = xval / self.x_range * self.x_range + self.xmin s = circle.attrib['fill'] # print("s=",s) # print("type(s)=",type(s)) if (s[0:3] == "rgb" ): # if an rgb string, e.g. "rgb(175,175,80)" rgb = list(map(int, s[4:-1].split(","))) rgb[:] = [x / 255. for x in rgb] else: # otherwise, must be a color name rgb_tuple = mplc.to_rgb(mplc.cnames[s]) # a tuple rgb = [x for x in rgb_tuple] # test for bogus x,y locations (rwh TODO: use max of domain?) too_large_val = 10000. if (np.fabs(xval) > too_large_val): print("bogus xval=", xval) break yval = float(circle.attrib['cy']) # yval = (yval - self.svg_xmin)/self.svg_xrange * self.y_range + self.ymin yval = yval / self.y_range * self.y_range + self.ymin if (np.fabs(yval) > too_large_val): print("bogus xval=", xval) break rval = float(circle.attrib['r']) # if (rgb[0] > rgb[1]): # print(num_cells,rgb, rval) xlist.append(xval) ylist.append(yval) rlist.append(rval) rgb_list.append(rgb) # For .svg files with cells that *have* a nucleus, there will be a 2nd if (not self.show_nucleus): #if (not self.show_nucleus): break num_cells += 1 # if num_cells > 3: # for debugging # print(fname,': num_cells= ',num_cells," --- debug exit.") # sys.exit(1) # break # print(fname,': num_cells= ',num_cells) xvals = np.array(xlist) yvals = np.array(ylist) rvals = np.array(rlist) rgbs = np.array(rgb_list) # print("xvals[0:5]=",xvals[0:5]) # print("rvals[0:5]=",rvals[0:5]) # print("rvals.min, max=",rvals.min(),rvals.max()) # rwh - is this where I change size of render window?? (YES - yipeee!) # plt.figure(figsize=(6, 6)) # plt.cla() # if (self.substrates_toggle.value): self.title_str += " (" + str(num_cells) + " agents)" # title_str = " (" + str(num_cells) + " agents)" # else: # mins= round(int(float(root.find(".//current_time").text))) # TODO: check units = mins # hrs = int(mins/60) # days = int(hrs/24) # title_str = '%dd, %dh, %dm' % (int(days),(hrs%24), mins - (hrs*60)) plt.title(self.title_str) plt.xlim(self.xmin, self.xmax) plt.ylim(self.ymin, self.ymax) # plt.xlim(axes_min,axes_max) # plt.ylim(axes_min,axes_max) # plt.scatter(xvals,yvals, s=rvals*scale_radius, c=rgbs) # TODO: make figsize a function of plot_size? What about non-square plots? # self.fig = plt.figure(figsize=(9, 9)) # axx = plt.axes([0, 0.05, 0.9, 0.9]) # left, bottom, width, height # axx = fig.gca() # print('fig.dpi=',fig.dpi) # = 72 # im = ax.imshow(f.reshape(100,100), interpolation='nearest', cmap=cmap, extent=[0,20, 0,20]) # ax.xlim(axes_min,axes_max) # ax.ylim(axes_min,axes_max) # convert radii to radii in pixels # ax2 = self.fig.gca() # N = len(xvals) # rr_pix = (ax2.transData.transform(np.vstack([rvals, rvals]).T) - # ax2.transData.transform(np.vstack([np.zeros(N), np.zeros(N)]).T)) # rpix, _ = rr_pix.T # markers_size = (144. * rpix / self.fig.dpi)**2 # = (2*rpix / fig.dpi * 72)**2 # markers_size = markers_size/4000000. # print('max=',markers_size.max()) #rwh - temp fix - Ah, error only occurs when "edges" is toggled on if (self.show_edge): try: # plt.scatter(xvals,yvals, s=markers_size, c=rgbs, edgecolor='black', linewidth=0.5) self.circles(xvals, yvals, s=rvals, color=rgbs, edgecolor='black', linewidth=0.5) # cell_circles = self.circles(xvals,yvals, s=rvals, color=rgbs, edgecolor='black', linewidth=0.5) # plt.sci(cell_circles) except (ValueError): pass else: # plt.scatter(xvals,yvals, s=markers_size, c=rgbs) self.circles(xvals, yvals, s=rvals, color=rgbs) # if (self.show_tracks): # for key in self.trackd.keys(): # xtracks = self.trackd[key][:,0] # ytracks = self.trackd[key][:,1] # plt.plot(xtracks[0:frame],ytracks[0:frame], linewidth=5) # plt.xlim(self.axes_min, self.axes_max) # plt.ylim(self.axes_min, self.axes_max) # ax.grid(False) # axx.set_title(title_str) # plt.title(title_str) #--------------------------------------------------------------------------- # assume "frame" is cell frame #, unless Cells is togggled off, then it's the substrate frame # # def plot_substrate(self, frame, grid): def plot_substrate(self, frame): # global current_idx, axes_max, gFileId, field_index # print("plot_substrate(): frame*self.substrate_delta_t = ",frame*self.substrate_delta_t) # print("plot_substrate(): frame*self.svg_delta_t = ",frame*self.svg_delta_t) self.title_str = '' # Recall: # self.svg_delta_t = config_tab.svg_interval.value # self.substrate_delta_t = config_tab.mcds_interval.value # self.modulo = int(self.substrate_delta_t / self.svg_delta_t) # self.therapy_activation_time = user_params_tab.therapy_activation_time.value # print("plot_substrate(): pre_therapy: max svg, substrate frames = ",max_svg_frame_pre_therapy, max_substrate_frame_pre_therapy) # Assume: # .svg files >= # substrate files # if (self.cells_toggle.value): # if (self.substrates_toggle.value and frame*self.substrate_delta_t <= self.svg_frame*self.svg_delta_t): # if (self.substrates_toggle.value and (frame % self.modulo == 0)): if (self.substrates_toggle.value): # self.fig = plt.figure(figsize=(14, 15.6)) # self.fig = plt.figure(figsize=(15.0, 12.5)) self.fig = plt.figure(figsize=(self.figsize_width_substrate, self.figsize_height_substrate)) # rwh - funky way to figure out substrate frame for pc4cancerbots (due to user-defined "save_interval*") # self.cell_time_mins # self.substrate_frame = int(frame / self.modulo) if (self.customized_output_freq and (frame > self.max_svg_frame_pre_therapy)): # max_svg_frame_pre_therapy = int(self.therapy_activation_time/self.svg_delta_t) # max_substrate_frame_pre_therapy = int(self.therapy_activation_time/self.substrate_delta_t) self.substrate_frame = self.max_substrate_frame_pre_therapy + ( frame - self.max_svg_frame_pre_therapy) else: self.substrate_frame = int(frame / self.modulo) # print("plot_substrate(): self.substrate_frame=",self.substrate_frame) # if (self.substrate_frame > (self.num_substrates-1)): # self.substrate_frame = self.num_substrates-1 # print('self.substrate_frame = ',self.substrate_frame) # if (self.cells_toggle.value): # self.modulo = int((self.num_svgs - 1) / (self.num_substrates - 1)) # self.substrate_frame = frame % self.modulo # else: # self.substrate_frame = frame fname = "output%08d_microenvironment0.mat" % self.substrate_frame xml_fname = "output%08d.xml" % self.substrate_frame # fullname = output_dir_str + fname # fullname = fname full_fname = os.path.join(self.output_dir, fname) # print("--- plot_substrate(): full_fname=",full_fname) full_xml_fname = os.path.join(self.output_dir, xml_fname) # self.output_dir = '.' # if not os.path.isfile(fullname): if not os.path.isfile(full_fname): print("Once output files are generated, click the slider." ) # No: output00000000_microenvironment0.mat return # tree = ET.parse(xml_fname) tree = ET.parse(full_xml_fname) xml_root = tree.getroot() mins = round(int(float(xml_root.find( ".//current_time").text))) # TODO: check units = mins self.substrate_mins = round( int(float(xml_root.find( ".//current_time").text))) # TODO: check units = mins hrs = int(mins / 60) days = int(hrs / 24) self.title_str = 'substrate: %dd, %dh, %dm' % (int(days), (hrs % 24), mins - (hrs * 60)) # self.title_str = 'substrate: %dm' % (mins ) # rwh info_dict = {} # scipy.io.loadmat(fullname, info_dict) scipy.io.loadmat(full_fname, info_dict) M = info_dict['multiscale_microenvironment'] # global_field_index = int(mcds_field.value) # print('plot_substrate: field_index =',field_index) f = M[ self. field_index, :] # 4=tumor cells field, 5=blood vessel density, 6=growth substrate # plt.clf() # my_plot = plt.imshow(f.reshape(400,400), cmap='jet', extent=[0,20, 0,20]) # self.fig = plt.figure(figsize=(18.0,15)) # this strange figsize results in a ~square contour plot # plt.subplot(grid[0:1, 0:1]) # main_ax = self.fig.add_subplot(grid[0:1, 0:1]) # works, but tiny upper-left region #main_ax = self.fig.add_subplot(grid[0:2, 0:2]) # main_ax = self.fig.add_subplot(grid[0:, 0:2]) #main_ax = self.fig.add_subplot(grid[:-1, 0:]) # nrows, ncols #main_ax = self.fig.add_subplot(grid[0:, 0:]) # nrows, ncols #main_ax = self.fig.add_subplot(grid[0:4, 0:]) # nrows, ncols # main_ax = self.fig.add_subplot(grid[0:3, 0:]) # nrows, ncols # main_ax = self.fig.add_subplot(111) # nrows, ncols # plt.rc('font', size=10) # TODO: does this affect the Cell plots fonts too? YES. Not what we want. # fig.set_tight_layout(True) # ax = plt.axes([0, 0.05, 0.9, 0.9 ]) #left, bottom, width, height # ax = plt.axes([0, 0.0, 1, 1 ]) # cmap = plt.cm.viridis # Blues, YlOrBr, ... # im = ax.imshow(f.reshape(100,100), interpolation='nearest', cmap=cmap, extent=[0,20, 0,20]) # ax.grid(False) # print("substrates.py: ------- numx, numy = ", self.numx, self.numy ) # if (self.numx == 0): # need to parse vals from the config.xml # # print("--- plot_substrate(): full_fname=",full_fname) # fname = os.path.join(self.output_dir, "config.xml") # tree = ET.parse(fname) # xml_root = tree.getroot() # self.xmin = float(xml_root.find(".//x_min").text) # self.xmax = float(xml_root.find(".//x_max").text) # dx = float(xml_root.find(".//dx").text) # self.ymin = float(xml_root.find(".//y_min").text) # self.ymax = float(xml_root.find(".//y_max").text) # dy = float(xml_root.find(".//dy").text) # self.numx = math.ceil( (self.xmax - self.xmin) / dx) # self.numy = math.ceil( (self.ymax - self.ymin) / dy) try: xgrid = M[0, :].reshape(self.numy, self.numx) ygrid = M[1, :].reshape(self.numy, self.numx) except: print( "substrates.py: mismatched mesh size for reshape: numx,numy=", self.numx, self.numy) pass # xgrid = M[0, :].reshape(self.numy, self.numx) # ygrid = M[1, :].reshape(self.numy, self.numx) num_contours = 15 levels = MaxNLocator(nbins=num_contours).tick_values( self.cmap_min.value, self.cmap_max.value) contour_ok = True if (self.cmap_fixed.value): try: # substrate_plot = main_ax.contourf(xgrid, ygrid, M[self.field_index, :].reshape(self.numy, self.numx), levels=levels, extend='both', cmap=self.field_cmap.value, fontsize=self.fontsize) substrate_plot = plt.contourf( xgrid, ygrid, M[self.field_index, :].reshape(self.numy, self.numx), levels=levels, extend='both', cmap=self.field_cmap.value, fontsize=self.fontsize) except: contour_ok = False # print('got error on contourf 1.') else: try: # substrate_plot = main_ax.contourf(xgrid, ygrid, M[self.field_index, :].reshape(self.numy,self.numx), num_contours, cmap=self.field_cmap.value) substrate_plot = plt.contourf( xgrid, ygrid, M[self.field_index, :].reshape(self.numy, self.numx), num_contours, cmap=self.field_cmap.value) except: contour_ok = False # print('got error on contourf 2.') if (contour_ok): # main_ax.set_title(self.title_str, fontsize=self.fontsize) plt.title(self.title_str, fontsize=self.fontsize) # main_ax.tick_params(labelsize=self.fontsize) # cbar = plt.colorbar(my_plot) # cbar = self.fig.colorbar(substrate_plot, ax=main_ax) cbar = self.fig.colorbar(substrate_plot) cbar.ax.tick_params(labelsize=self.fontsize) # cbar = main_ax.colorbar(my_plot) # cbar.ax.tick_params(labelsize=self.fontsize) # axes_min = 0 # axes_max = 2000 # main_ax.set_xlim([self.xmin, self.xmax]) # main_ax.set_ylim([self.ymin, self.ymax]) plt.xlim(self.xmin, self.xmax) plt.ylim(self.ymin, self.ymax) # if (frame == 0): # maybe allow substrate grid display later # xs = np.linspace(self.xmin,self.xmax,self.numx) # ys = np.linspace(self.ymin,self.ymax,self.numy) # hlines = np.column_stack(np.broadcast_arrays(xs[0], ys, xs[-1], ys)) # vlines = np.column_stack(np.broadcast_arrays(xs, ys[0], xs, ys[-1])) # grid_lines = np.concatenate([hlines, vlines]).reshape(-1, 2, 2) # line_collection = LineCollection(grid_lines, color="gray", linewidths=0.5) # # ax = main_ax.gca() # main_ax.add_collection(line_collection) # # ax.set_xlim(xs[0], xs[-1]) # # ax.set_ylim(ys[0], ys[-1]) # Now plot the cells (possibly on top of the substrate) if (self.cells_toggle.value): if (not self.substrates_toggle.value): # self.fig = plt.figure(figsize=(12, 12)) self.fig = plt.figure(figsize=(self.figsize_width_svg, self.figsize_height_svg)) # self.plot_svg(frame) self.svg_frame = frame # print('plot_svg with frame=',self.svg_frame) self.plot_svg(self.svg_frame)