def metrics_doc(doc: Document) -> None: settings = Settings() logdir = settings.log_path / doc.session_context.request.arguments['model'][0].decode('utf-8') plots = get_plots() doc.add_root(row(plots.loss.plot, plots.score.plot)) doc.theme = Theme(filename="visualization/theme.yaml") threading.Thread(target=load_data_for_model, args=(doc, plots, logdir)).start()
def test_setting_built_in_theme_str(self) -> None: obj = SomeModel() doc = Document() doc.add_root(obj) doc.theme = DARK_MINIMAL assert "#20262B" == doc.theme._json['attrs']['Figure'][ 'background_fill_color']
def test_setting_built_in_theme_obj(self) -> None: obj = SomeModel() doc = Document() doc.add_root(obj) doc.theme = built_in_themes[LIGHT_MINIMAL] assert "#5B5B5B" == doc.theme._json['attrs']['ColorBar'][ 'title_text_color']
def sea_surface_handler(doc: Document) -> None: df = sea_surface_temperature.copy() source = ColumnDataSource(data=df) plot = figure(x_axis_type="datetime", y_range=(0, 25), y_axis_label="Temperature (Celsius)", title="Sea Surface Temperature at 43.18, -70.43") plot.line("time", "temperature", source=source) def callback(attr: str, old: Any, new: Any) -> None: if new == 0: data = df else: data = df.rolling("{0}D".format(new)).mean() source.data = dict(ColumnDataSource(data=data).data) slider = Slider(start=0, end=30, value=0, step=1, title="Smoothing by N Days") slider.on_change("value", callback) doc.theme = theme doc.add_root(column(slider, plot))
def profiler_doc(doc: Document) -> None: settings = Settings() model_name = doc.session_context.request.arguments['model'][0].decode('utf-8') logdir = settings.log_path / model_name plots = get_profiler_plots() doc.add_root(row(plots.full_epoch.plot)) doc.theme = Theme(filename="visualization/theme.yaml") threading.Thread(target=load_profiling_data, args=(doc, plots, logdir, model_name)).start()
def test_setting_document_theme_to_none(self) -> None: theme = Theme(json={'attrs': {'ThemedModel': {'string': 'w00t'}}}) obj = ThemedModel() doc = Document() doc.add_root(obj) changes = dict(calls=[]) def record_trigger(attr, old, new_): changes['calls'].append((attr, old, new_)) obj.on_change('string', record_trigger) doc.theme = theme assert 'w00t' == obj.string # setting to None reverts to default theme doc.theme = None assert doc.theme is not None assert 'hello' == obj.string assert [('string', 'hello', 'w00t'), ('string', 'w00t', 'hello')] == changes['calls']
def test_setting_document_theme_to_none(self): theme = Theme(json={'attrs': {'ThemedModel': {'string': 'w00t'}}}) obj = ThemedModel() doc = Document() doc.add_root(obj) changes = dict(calls=[]) def record_trigger(attr, old, new_): changes['calls'].append((attr, old, new_)) obj.on_change('string', record_trigger) doc.theme = theme self.assertEqual('w00t', obj.string) # setting to None reverts to default theme doc.theme = None self.assertIsNot(doc.theme, None) self.assertEqual('hello', obj.string) self.assertEqual([('string', 'hello', 'w00t'), ('string', 'w00t', 'hello')], changes['calls'])
def get_plot(self_or_cls, obj, doc=None, renderer=None): """ Given a HoloViews Viewable return a corresponding plot instance. Allows supplying a document attach the plot to, useful when combining the bokeh model with another plot. """ if doc is None: doc = Document() if self_or_cls.notebook_context else curdoc() doc.theme = self_or_cls.theme plot = super(BokehRenderer, self_or_cls).get_plot(obj, renderer) plot.document = doc return plot
def test_setting_document_theme_to_none(self): theme = Theme(json={ 'attrs' : { 'ThemedModel' : { 'string' : 'w00t' } } }) obj = ThemedModel() doc = Document() doc.add_root(obj) changes = dict(calls=[]) def record_trigger(attr, old, new_): changes['calls'].append((attr, old, new_)) obj.on_change('string', record_trigger) doc.theme = theme assert 'w00t' == obj.string # setting to None reverts to default theme doc.theme = None assert doc.theme is not None assert 'hello' == obj.string assert [('string', 'hello', 'w00t'), ('string', 'w00t', 'hello')] == changes['calls']
def test_setting_document_theme_to_none(self): theme = Theme(json={ 'attrs' : { 'ThemedModel' : { 'string' : 'w00t' } } }) obj = ThemedModel() doc = Document() doc.add_root(obj) changes = dict(calls=[]) def record_trigger(attr, old, new_): changes['calls'].append((attr, old, new_)) obj.on_change('string', record_trigger) doc.theme = theme self.assertEqual('w00t', obj.string) # setting to None reverts to default theme doc.theme = None self.assertIsNot(doc.theme, None) self.assertEqual('hello', obj.string) self.assertEqual([('string', 'hello', 'w00t'), ('string', 'w00t', 'hello')], changes['calls'])
def get_plot(self_or_cls, obj, doc=None, renderer=None, **kwargs): """ Given a HoloViews Viewable return a corresponding plot instance. Allows supplying a document attach the plot to, useful when combining the bokeh model with another plot. """ if doc is None: doc = Document() if self_or_cls.notebook_context else curdoc() if self_or_cls.notebook_context: curdoc().theme = self_or_cls.theme doc.theme = self_or_cls.theme plot = super(BokehRenderer, self_or_cls).get_plot(obj, renderer, **kwargs) plot.document = doc return plot
def test_directory_has_theme_file(self) -> None: doc = Document() def load(filename: str): handler = bahd.DirectoryHandler(filename=filename) handler.modify_document(doc) if handler.failed: raise RuntimeError(handler.error) custom_theme = """ attrs: AnotherModelInTestDirectoryTheme: bar: 42 SomeModelInTestDirectoryTheme: foo: 14 """ with_directory_contents( { 'main.py': script_adds_two_roots('SomeModelInTestDirectoryTheme', 'AnotherModelInTestDirectoryTheme') + """ # we're testing that the script can override the theme some = next(m for m in curdoc().roots if isinstance(m, SomeModelInTestDirectoryTheme)) some.foo = 57 """, 'theme.yaml': custom_theme }, load) assert len(doc.roots) == 2 some_model = next( m for m in doc.roots if m.__class__.__name__ == 'SomeModelInTestDirectoryTheme') another_model = next( m for m in doc.roots if m.__class__.__name__ == 'AnotherModelInTestDirectoryTheme') assert another_model.bar == 42 assert some_model.foo == 57 # test that we use the theme if we delete our explicit-set value del some_model.foo assert some_model.foo == 14 # test that removing the theme gets us back to the base doc.theme = None assert some_model.foo == 2 assert another_model.bar == 1
def test_theming_a_document_before_adding_root(self) -> None: theme = Theme(json={'attrs': {'ThemedModel': {'string': 'w00t'}}}) obj = ThemedModel() doc = Document() assert 'hello' == obj.string doc.theme = theme assert doc.theme is theme changes = dict(calls=[]) def record_trigger(attr, old, new_): changes['calls'].append((attr, old, new_)) obj.on_change('string', record_trigger) doc.add_root(obj) assert 'w00t' == obj.string doc.remove_root(obj) assert 'hello' == obj.string assert [('string', 'hello', 'w00t'), ('string', 'w00t', 'hello')] == changes['calls']
def test_theming_a_document_before_adding_root(self): theme = Theme(json={'attrs': {'ThemedModel': {'string': 'w00t'}}}) obj = ThemedModel() doc = Document() self.assertEqual('hello', obj.string) doc.theme = theme self.assertIs(doc.theme, theme) changes = dict(calls=[]) def record_trigger(attr, old, new_): changes['calls'].append((attr, old, new_)) obj.on_change('string', record_trigger) doc.add_root(obj) self.assertEqual('w00t', obj.string) doc.remove_root(obj) self.assertEqual('hello', obj.string) self.assertEqual([('string', 'hello', 'w00t'), ('string', 'w00t', 'hello')], changes['calls'])
def test_theming_a_document_before_adding_root(self): theme = Theme(json={ 'attrs' : { 'ThemedModel' : { 'string' : 'w00t' } } }) obj = ThemedModel() doc = Document() assert 'hello' == obj.string doc.theme = theme assert doc.theme is theme changes = dict(calls=[]) def record_trigger(attr, old, new_): changes['calls'].append((attr, old, new_)) obj.on_change('string', record_trigger) doc.add_root(obj) assert 'w00t' == obj.string doc.remove_root(obj) assert 'hello' == obj.string assert [('string', 'hello', 'w00t'), ('string', 'w00t', 'hello')] == changes['calls']
def test_directory_has_theme_file(self): doc = Document() def load(filename): handler = DirectoryHandler(filename=filename) handler.modify_document(doc) if handler.failed: raise RuntimeError(handler.error) custom_theme = """ attrs: AnotherModelInTestDirectoryTheme: bar: 42 SomeModelInTestDirectoryTheme: foo: 14 """ _with_directory_contents({ 'main.py' : script_adds_two_roots('SomeModelInTestDirectoryTheme', 'AnotherModelInTestDirectoryTheme') + """ # we're testing that the script can override the theme some = next(m for m in curdoc().roots if isinstance(m, SomeModelInTestDirectoryTheme)) some.foo = 57 """, 'theme.yaml' : custom_theme }, load) self.assertEqual(2, len(doc.roots)) some_model = next(m for m in doc.roots if m.__class__.__name__ == 'SomeModelInTestDirectoryTheme') another_model = next(m for m in doc.roots if m.__class__.__name__ == 'AnotherModelInTestDirectoryTheme') self.assertEqual(42, another_model.bar) self.assertEqual(57, some_model.foo) # test that we use the theme if we delete our explicit-set value del some_model.foo self.assertEqual(14, some_model.foo) # test that removing the theme gets us back to the base doc.theme = None self.assertEqual(2, some_model.foo) self.assertEqual(1, another_model.bar)
def test_theming_a_document_before_adding_root(self): theme = Theme(json={ 'attrs' : { 'ThemedModel' : { 'string' : 'w00t' } } }) obj = ThemedModel() doc = Document() self.assertEqual('hello', obj.string) doc.theme = theme self.assertIs(doc.theme, theme) changes = dict(calls=[]) def record_trigger(attr, old, new_): changes['calls'].append((attr, old, new_)) obj.on_change('string', record_trigger) doc.add_root(obj) self.assertEqual('w00t', obj.string) doc.remove_root(obj) self.assertEqual('hello', obj.string) self.assertEqual([('string', 'hello', 'w00t'), ('string', 'w00t', 'hello')], changes['calls'])
def test_setting_built_in_theme_error(self): obj = Model() doc = Document() doc.add_root(obj) with pytest.raises(ValueError): doc.theme = 1337
def test_setting_built_in_theme_missing(self): obj = Model() doc = Document() doc.add_root(obj) with pytest.raises(ValueError): doc.theme = 'some_theme_i_guess'
def test_setting_built_in_theme_obj(self): obj = Model() doc = Document() doc.add_root(obj) doc.theme = built_in_themes[LIGHT_MINIMAL] assert "#5B5B5B" == doc.theme._json['attrs']['ColorBar']['title_text_color']
def test_setting_built_in_theme_str(self): obj = Model() doc = Document() doc.add_root(obj) doc.theme = DARK_MINIMAL assert "#20262B" == doc.theme._json['attrs']['Figure']['background_fill_color']
def distanceApp(doc: Document) -> None: distances = lipid_residue_distances[0] time = lipid_residue_distances[1]['time'] proteins = lipid_residue_distances[1]['protein'] lipids = list(distances.keys()) residues = list(distances[lipids[0]].keys()) protein_id = list(distances[lipids[0]][residues[0]].keys()) residues = [str(x) for x in residues] protein_id = [str(x) for x in protein_id] source = ColumnDataSource(data=dict(x=[], y=[])) protein_name = Select(title="Protein", value=proteins[0], options=proteins, width=100) pc = Select(title="Protein Copy", value=protein_id[0], options=protein_id, width=100) res = Select(title="Residue Selection", value=residues[0], options=residues, width=100) lip = Select(title="Lipid Selection", value=lipids[0], options=lipids, width=100) p = figure(plot_width=1500, plot_height=400, tools='pan, box_zoom, ywheel_zoom, save, reset, help') color = '#%02x%02x%02x' % tuple(np.random.choice(range(256), size=3)) p.line(x='x', y='y', line_color=color, source=source, line_width=4) p.y_range = Range1d(0, 4) p.x_range = Range1d(time[0], time[-1]) p.toolbar.autohide = True p.axis.axis_label_text_font_size = "12pt" p.axis.axis_label_text_font_style = "bold" p.title.align = 'center' p.xaxis.axis_label = "Trajectory Time" p.yaxis.axis_label = "Distance (nm)" def update(): protein_selected = protein_name.value copy_selected = pc.value residue_selected = res.value lipid_selected = lip.value residues = list(distances[lipid_selected].keys()) if int(residue_selected) not in residues: residue_selected = residues[0] res.options = [str(x) for x in residues] y = distances[lipid_selected][int(residue_selected)][int( copy_selected)] source.data = dict(x=time, y=y) controls = [protein_name, pc, res, lip] for control in controls: control.on_change('value', lambda attr, old, new: update()) inputs2 = row([*controls]) layout1 = layout([[inputs2]]) layout2 = layout([p]) update() doc.add_root(layout1) doc.add_root(layout2) doc.title = "Distance Calculations" doc.theme = Theme(json=yaml.load(""" attrs: Figure: toolbar_location: above height: 500 width: 1000 Grid: grid_line_dash: [6, 4] grid_line_color: black """, Loader=yaml.FullLoader))
def line_handler(doc: Document) -> None: task_id = doc.session_context.request.arguments.get('task_id') username = doc.session_context.request.arguments.get('username') user_dir = os.path.join(settings.USER_DATA, username) path_to_db = os.path.join(user_dir, task_id) conn = os.path.join(path_to_db, 'distances.p') with open(conn, 'rb') as fp: lipid_residue_distances = pickle.load(fp) # variables distances = lipid_residue_distances[0] time = lipid_residue_distances[1]['time'] proteins = lipid_residue_distances[1]['protein'] lipids = list(distances.keys()) residues = list(distances[lipids[0]].keys()) protein_id = list(distances[lipids[0]][residues[0]].keys()) residues = [str(x) for x in residues] protein_id = [str(x) for x in protein_id] # columndatasource source = ColumnDataSource(data=dict(x=[], y=[])) #widgets protein_name = Select(title="Protein", value=proteins[0], options=proteins, width=100) pc = Select(title="Protein Copy", value=protein_id[0], options=protein_id, width=100) res = Select(title="Residue Selection", value=residues[0], options=residues, width=100) lip = Select(title="Lipid Selection", value=lipids[0], options=lipids, width=100) p = figure(plot_width=1500, plot_height=400, tools='pan, box_zoom, ywheel_zoom, save, reset, help') # line with random color color = '#%02x%02x%02x' % tuple(np.random.choice(range(256), size=3)) p.line(x='x', y='y', line_color=color, source=source, line_width=2) # make pretty p.y_range = Range1d(0, 4) p.x_range = Range1d(time[0], time[-1]) p.toolbar.autohide = True p.axis.axis_label_text_font_size = "12pt" p.axis.axis_label_text_font_style = "bold" p.title.align = 'center' p.xaxis.axis_label = "Trajectory Time" p.yaxis.axis_label = "Distance (nm)" def update(): protein_selected = protein_name.value copy_selected = pc.value residue_selected = res.value lipid_selected = lip.value residues = list(distances[lipid_selected].keys()) if int(residue_selected) not in residues: residue_selected = residues[0] res.options = [str(x) for x in residues] y = distances[lipid_selected][int(residue_selected)][int( copy_selected)] source.data = dict(x=time, y=y) # add controls controls = [protein_name, pc, res, lip] for control in controls: control.on_change('value', lambda attr, old, new: update()) # align plot elements inputs2 = row([*controls]) layout1 = layout([[inputs2]]) layout2 = layout([p]) # render update() doc.add_root(layout1) doc.add_root(layout2) doc.title = "Distance Calculations" doc.theme = Theme(json=yaml.load(""" attrs: Figure: toolbar_location: above height: 500 width: 1000 Grid: grid_line_dash: [6, 4] grid_line_color: black """, Loader=yaml.FullLoader))