def large_plot(n): from bokeh.models import (Plot, LinearAxis, Grid, GlyphRenderer, ColumnDataSource, DataRange1d, PanTool, ZoomInTool, ZoomOutTool, WheelZoomTool, BoxZoomTool, BoxSelectTool, SaveTool, ResetTool) from bokeh.models.layouts import Column from bokeh.models.glyphs import Line col = Column() objects = set([col]) for i in xrange(n): source = ColumnDataSource(data=dict(x=[0, i + 1], y=[0, i + 1])) xdr = DataRange1d() ydr = DataRange1d() plot = Plot(x_range=xdr, y_range=ydr) xaxis = LinearAxis(plot=plot) yaxis = LinearAxis(plot=plot) xgrid = Grid(plot=plot, dimension=0) ygrid = Grid(plot=plot, dimension=1) tickers = [ xaxis.ticker, xaxis.formatter, yaxis.ticker, yaxis.formatter ] glyph = Line(x='x', y='y') renderer = GlyphRenderer(data_source=source, glyph=glyph) plot.renderers.append(renderer) pan = PanTool() zoom_in = ZoomInTool() zoom_out = ZoomOutTool() wheel_zoom = WheelZoomTool() box_zoom = BoxZoomTool() box_select = BoxSelectTool() save = SaveTool() reset = ResetTool() tools = [ pan, zoom_in, zoom_out, wheel_zoom, box_zoom, box_select, save, reset ] plot.add_tools(*tools) col.children.append(plot) objects |= set([ xdr, ydr, xaxis, yaxis, xgrid, ygrid, renderer, renderer.view, glyph, source, source.selected, source.selection_policy, plot, plot.x_scale, plot.y_scale, plot.toolbar, plot.title, box_zoom.overlay, box_select.overlay, ] + tickers + tools) return col, objects
def create_module(user, cfg): # start controller controller = Controller(user, cfg) # hover hover1 = create_hover(1) # module fig1 = figure(plot_width=600, plot_height=300, css_classes=['monitoring_fig1'], tools='pan,box_zoom,reset', name='fig1', title='Cantidad de Clientes x día de Lectura') fig1.toolbar.logo = None fig1.toolbar_location = 'above' fig1.line(x=[0, 1], y=[0, 1], line_color="darkgray", line_width=2, alpha=0.6, legend='Planificación', name='line1') fig1.line(x=[0, 1], y=[0, 1], line_color="blue", line_width=2, alpha=0.8, legend='Real', name='line1.2') fig1.legend.click_policy = "hide" fig1.legend.location = "top_left" fig1.legend.background_fill_color = "white" fig1.legend.background_fill_alpha = 0.5 fig1.legend.label_text_color = "#505050" fig1.legend.orientation = "vertical" fig1.xaxis.axis_label = 'Días del mes' fig1.add_tools(hover1) fig1.xaxis.formatter = DatetimeTickFormatter(days=["%d"]) fig1.yaxis[0].formatter = NumeralTickFormatter(format="0.0a") fig2 = figure(plot_width=600, plot_height=300, css_classes=['monitoring_fig2'], tools='pan,box_zoom,reset', name='fig2', title='Cantidad de Itinerarios x día de Lectura') fig2.toolbar.logo = None fig2.toolbar_location = 'above' fig2.line(x=[0, 1], y=[0, 1], line_color="darkgray", line_width=2, alpha=0.6, legend='Planificación', name='line2') fig2.line(x=[0, 1], y=[0, 1], line_color="blue", line_width=2, alpha=0.8, legend='Real', name='line2.2') fig2.legend.click_policy = "hide" fig2.legend.location = "top_left" fig2.legend.background_fill_color = "white" fig2.legend.background_fill_alpha = 0.5 fig2.legend.label_text_color = "#505050" fig2.legend.orientation = "vertical" fig2.xaxis.axis_label = 'Días del mes' fig2.add_tools(hover1) fig2.xaxis.formatter = DatetimeTickFormatter(days=["%d"]) map_options = GMapOptions(lat=10.032663, lng=-74.042470, map_type="roadmap", zoom=7) fig3 = GMapPlot(x_range=Range1d(), y_range=Range1d(), map_options=map_options, plot_width=600, plot_height=450, css_classes=['monitoring_fig3'], name='fig3') fig3.toolbar.logo = None fig3.toolbar_location = 'above' fig3.add_tools(PanTool(), WheelZoomTool()) fig3.title.text = 'Dispersión Geográfica de Itinerarios' fig3.api_key = 'AIzaSyATl81v4Wnm4udDvlNTcgw4oWMzWJndkfQ' x = np.linspace(-2, 2, 10) source = ColumnDataSource(data=dict( lat=x, lon=x**2, sizes=np.linspace(10, 20, 10), colors=controller.day_colors[0:10], )) circle = Circle(x="lon", y="lat", size='sizes', fill_color='colors', fill_alpha=0.6, line_color='black') fig3.add_glyph(source, circle, name='circles1') fig3.add_tools(create_hover(2)) menu1 = Select(title="Periodo:", value="opt1", name='menu1', options=["opt1", "opt2", "opt3", "opt4"], width=150, css_classes=['monitoring_menu1']) menu1.options = controller.periodos_str menu1.value = controller.periodos_str[controller.now.month - 1] menu2 = Select(title="Delegación:", value='TODOS', name='menu2', options=['TODOS'], width=200, css_classes=['monitoring_menu2']) menu3 = Select(title="Unicom:", value="TODOS", name='menu3', options=["TODOS"], width=150, css_classes=['monitoring_menu3']) menu4 = Select(title="Día:", value="TODOS", name='menu4', options=["TODOS"], width=150, css_classes=['monitoring_menu4']) menu5 = Select(title="Municipio:", value="TODOS", name='menu5', options=["TODOS"], width=200, css_classes=['monitoring_menu5']) menu6 = Select(title="Tipología:", value="TODOS", name='menu6', options=["TODOS"], width=150, css_classes=['monitoring_menu6']) fig4 = figure(plot_width=600, plot_height=300, css_classes=['monitoring_fig4'], tools='pan,box_zoom,reset', name='fig4', title='Promedio de Días Facturados') fig4.toolbar.logo = None fig4.toolbar_location = 'above' fig4.line(x=[0, 1], y=[0, 1], line_color="darkgray", line_width=2, alpha=0.6, legend='Planificación', name='line4') fig4.line(x=[0, 1], y=[0, 1], line_color="blue", line_width=2, alpha=0.8, legend='Real', name='line4.2') fig4.legend.click_policy = "hide" fig4.legend.location = "top_left" fig4.legend.background_fill_color = "white" fig4.legend.background_fill_alpha = 0.5 fig4.legend.label_text_color = "#505050" fig4.legend.orientation = "vertical" fig4.xaxis.axis_label = 'Mes del Año' fig4.add_tools(create_hover(4)) fig4.yaxis[0].formatter = NumeralTickFormatter(format="0.0a") # TODO: visualizar el promedio y el total dias facturados al año fig5 = figure(plot_width=600, plot_height=300, css_classes=['monitoring_fig5'], tools='pan,box_zoom,reset', name='fig5', title='Histograma de Días Facturados') fig5.toolbar.logo = None fig5.toolbar_location = 'above' fig5.vbar(x=[1, 2, 3], width=0.5, bottom=0, top=[1.2, 2.5, 3.7], color="darkcyan", fill_alpha=0.6, line_color='black', name='vbar1', legend='Planificación') fig5.line(x=[0, 1], y=[0, 1], line_color="blue", line_width=2, alpha=0.8, legend='Real', name='line5.2') fig5.legend.click_policy = "hide" fig5.legend.location = "top_left" fig5.legend.background_fill_color = "white" fig5.legend.background_fill_alpha = 0.5 fig5.legend.label_text_color = "#505050" fig5.legend.orientation = "vertical" fig5.xaxis.axis_label = 'Días Facturados' fig5.add_tools(create_hover(3)) fig5.yaxis[0].formatter = NumeralTickFormatter(format="0.0a") # TODO: agregar curva de suma acumulativa fig6 = figure(plot_width=600, plot_height=300, css_classes=['monitoring_fig6'], tools='pan,box_zoom,reset', name='fig6', title='Traslados') fig6.toolbar.logo = None fig6.toolbar_location = 'above' fig6.line(x=[0, 1], y=[0, 1], line_color="darkgray", line_width=2, alpha=0.6, legend='Planificación', name='line6') fig6.line(x=[0, 1], y=[0, 1], line_color="blue", line_width=2, alpha=0.8, legend='Real', name='line6.2') fig6.legend.click_policy = "hide" fig6.legend.location = "top_left" fig6.legend.background_fill_color = "white" fig6.legend.background_fill_alpha = 0.5 fig6.legend.label_text_color = "#505050" fig6.legend.orientation = "vertical" fig6.xaxis.axis_label = 'Mes del Año' fig6.add_tools(create_hover(4)) fig6.yaxis[0].formatter = NumeralTickFormatter(format="0.0a") button_group1 = RadioButtonGroup( labels=["Itinerarios", "Energía", "Importe"], active=0, name='button_group1', css_classes=['monitoring_button_group1']) button_group1.on_change('active', controller.on_change_menus) widget1 = layout([[fig1], [fig2]], sizing_mode='fixed') widget2 = layout([ [fig3], [menu1, menu2, menu3], [menu4, menu5, menu6], ], sizing_mode='fixed') widget3 = layout([[fig6], [button_group1]], sizing_mode='fixed') dashboard = layout([ [widget1, widget2], [fig4, fig5], [widget3], ], sizing_mode='fixed') dashboard.name = 'monitoring' # ini module data curdoc().add_root(dashboard) controller.get_user_data() controller.populate_menus(controller.info_itin, controller.fechas_itin) menu1.on_change('value', controller.on_change_menus) menu2.on_change('value', controller.on_change_menus) menu3.on_change('value', controller.on_change_menus) menu4.on_change('value', controller.on_change_menus) menu5.on_change('value', controller.on_change_menus) menu6.on_change('value', controller.on_change_menus) controller.on_change_menus(None, None, None)
def make_plot(): # Initialize Plot plot = figure(x_range=(-9790000, -9745000), y_range=(5120000, 5170000), x_axis_type="mercator", y_axis_type="mercator", tools=['pan'], sizing_mode='stretch_both') plot.add_tile(CARTODBPOSITRON) plot.toolbar.active_scroll = "auto" plot.xaxis.major_tick_line_color = None # turn off x-axis major ticks plot.xaxis.minor_tick_line_color = None # turn off x-axis minor ticks plot.yaxis.major_tick_line_color = None # turn off y-axis major ticks plot.yaxis.minor_tick_line_color = None # turn off y-axis minor ticks plot.xaxis.major_label_text_font_size = '0pt' # turn off x-axis tick labels plot.yaxis.major_label_text_font_size = '0pt' # turn off y-axis tick labels plot.toolbar.logo = None plot.toolbar_location = None # Read in train line data sf = shapefile.Reader("CTA_New/CTA_New") features = sf.shapeRecords() Lines = [] Coords_x = [] Coords_y = [] for shape in features: Line = shape.record[5] Coord = shape.shape.points X_s = [] Y_s = [] for coord in Coord: Trans = merc(tuple(reversed(coord))) X_s.append(Trans[0]) Y_s.append(Trans[1]) Coords_x.append(X_s) Coords_y.append(Y_s) CTA_Lines = ['Red', 'G', 'Blue', 'P', 'Brn', 'Pink', 'Org', 'Y'] # Set up data sources # - Live CTA Data source = AjaxDataSource(data_url=request.url_root + 'cta_data/', polling_interval=5000, mode='replace') source.data = dict(x=[], y=[], next_station=[], destination=[], direction=[], color=[], line_name=[]) # - Station Coordinate Data L_Map = pd.read_csv('Stations.csv') station_source = ColumnDataSource( dict(x=L_Map['coords_x'], y=L_Map['coords_y'], name=L_Map['STATION_NAME'])) # Color Map for trains color_mapper = CategoricalColorMapper(factors=CTA_Lines, palette=[ 'Red', 'Green', 'Blue', 'Purple', 'Brown', 'Pink', 'Orange', 'Yellow' ]) # Plot Glyphs for i in range(len(Coords_x)): plot.line(x=Coords_x[i], y=Coords_y[i], line_color="black", alpha=0.7) stations = plot.circle(x='x', y='y', source=station_source, size=5, line_color="black", fill_color='white') circles = plot.circle(x='x', y='y', angle='heading', source=source, color={ 'field': 'color', 'transform': color_mapper }, size=14, line_color="black", line_width=0.8, legend='line_name') triangles = plot.triangle(x='x', y='y', angle='heading', source=source, size=8, color='white') # Set Up Tools hover = HoverTool(tooltips=[("Next Stop", "@next_station"), ("Destination", "@destination")], renderers=[circles]) station_hover = HoverTool(tooltips=[("Station", "@name")], renderers=[stations]) wheel = WheelZoomTool() plot.add_tools(hover) plot.add_tools(station_hover) plot.add_tools(wheel) plot.toolbar.active_scroll = wheel plot.legend.location = "top_left" script, div = components(plot) return script, div
f.title.text_color = "black" f.title.text_font = "times" f.title.text_font_size = "25px" f.title.align = "center" hover = HoverTool(tooltips=""" <div> <div> <span style="font-size: 15px; font-weight: bold;">@country</span> </div> <div> <span style="font-size: 10px; color: #696;">GDP: Gross domestic product (million current US$): @GDP</span><br> <span style="font-size: 10px; color: #696;">GDP per capita (current US$): @GDPpercapita</span><br> <span style="font-size: 10px; color: #696;">Surface area (km2): @Surface_area</span><br> <span style="font-size: 10px; color: #696;">Population density (per km2, 2017): @Population_density</span> </div> </div> """) f.tools = [PanTool(),WheelZoomTool(),BoxZoomTool(),ResetTool(),SaveTool()] f.add_tools(hover) f.toolbar_location = 'above' f.toolbar.logo = None f.xaxis.axis_label = "GDP: Gross domestic product (million current US$)" f.yaxis.axis_label = "Unemployment (% of labour force)" #create layout and add to curdoc lay_out=layout([[slider]]) curdoc().add_root(f) curdoc().add_root(lay_out)
def bokey_plot(dictionary_input, folder_bokey_default, mode="single", output=False, id_=None, info="", color_map_mode="continuous"): """ as input dictionnary of the form - label : {x:, y: , label:[labels]} mode allows you to specify if you want everything on one single plot or if you want distinct plots """ reset_output() source = {} i = 0 color = ["blue", "#ee6666"] assert color_map_mode in ["continuous", "divergent"] if color_map_mode == "continuous": color_map = cm.OrRd elif color_map_mode == "divergent": color_map = cm.rainbow if id_ is None: id_ = str(uuid.uuid4())[0:8] if mode == "single": p = figure( plot_width=800, plot_height=1000, tools=[BoxZoomTool(), ResetTool(), WheelZoomTool()], toolbar_sticky=False, toolbar_location="right", title='T-SNE ' + info ) # x_range=Range1d(-6,6),y_range=Range1d(int(min(y))-1,int(max(y))+1)) for key in dictionary_input.keys(): if mode == "distinct": p = figure( plot_width=800, plot_height=1000, title='T-SNE ' + info ) # x_range=Range1d(-6,6),y_range=Range1d(int(min(y))-1,int(max(y))+1)) source[key] = ColumnDataSource( data=dict(height=dictionary_input[key]["x"], weight=dictionary_input[key]["y"], names=dictionary_input[key]["label"])) colors = [ "#%02x%02x%02x" % (int(r), int(g), int(b)) for r, g, b, _ in (255) * color_map( Normalize(vmin=0, vmax=5)(dictionary_input[key]["color"])) ] colors_legend = [ "#%02x%02x%02x" % (int(r), int(g), int(b)) for r, g, b, _ in (255) * color_map( Normalize(vmin=0, vmax=5) (np.sort(list(set(dictionary_input[key]["color"]))))) ] color_mapper = LinearColorMapper(palette=colors_legend) ticker = FixedTicker(ticks=[0, 1, 2, 3, 4, 5]) formatter = FuncTickFormatter(code=""" function(tick) { data = {0: '0-10', 1: '10-20', 2: '20-30', 3: '30-40', 4: '40-50',50: '50plus'} return data[tick], " , } """) cbar = ColorBar(color_mapper=color_mapper, ticker=ticker, formatter=formatter, major_tick_out=0, major_tick_in=0, major_label_text_align='left', major_label_text_font_size='100pt', label_standoff=5) p.scatter(x='weight', y='height', size=8, source=source[key], legend=key) # color=colors) p.add_layout(cbar) labels = LabelSet(x='weight', y='height', text='names', level='glyph', x_offset=5, y_offset=5, source=source[key], render_mode='canvas') p.add_layout(labels) i += 1 if output: output_file(folder_bokey_default + id_ + "_tsne_" + key + "_" + info + "_bok.html") print(folder_bokey_default + id_ + "_tsne_" + key + "_" + info + "_bok.html") show(p) #if mode == "distinct": # output_notebook() # show(p) if mode == "single": output_notebook() show(p)
def draw(S, position=None, with_labels=False): """Plot the given signed social network. Args: S: The network position (dict, optional): The position for the nodes. If no position is provided, a layout will be calculated. If the nodes have 'color' attributes, a Kamanda-Kawai layout will be used to group nodes of the same color together. Otherwise, a circular layout will be used. Returns: A dictionary of positions keyed by node. Examples: >>> import dwave_structural_imbalance_demo as sbdemo >>> gssn = sbdemo.GlobalSignedSocialNetwork() >>> nld_before = gssn.get_node_link_data('Syria', 2013) >>> nld_after = gssn.solve_structural_imbalance('Syria', 2013) # draw Global graph before solving; save node layout for reuse >>> position = sbdemo.draw('syria.png', nld_before) # draw the Global graph; reusing the above layout, and calculating a new grouped layout >>> sbdemo.draw('syria_imbalance.png', nld_after, position) >>> sbdemo.draw('syria_imbalance_grouped', nld_after) """ # we need a consistent ordering of the edges edgelist = S.edges() nodelist = S.nodes() def layout_wrapper(S): pos = position if pos is None: try: # group bipartition if nodes are colored dist = defaultdict(dict) for u, v in product(nodelist, repeat=2): if u == v: # node has no distance from itself dist[u][v] = 0 elif nodelist[u]['color'] == nodelist[v][ 'color']: # make same color nodes closer together dist[u][v] = 1 else: # make different color nodes further apart dist[u][v] = 2 pos = nx.kamada_kawai_layout(S, dist) except KeyError: # default to circular layout if nodes aren't colored pos = nx.circular_layout(S) return pos # call layout wrapper once with all nodes to store position for calls with partial graph position = layout_wrapper(S) plot = Plot(plot_width=600, plot_height=400, x_range=Range1d(-1.2, 1.2), y_range=Range1d(-1.2, 1.2)) tools = [WheelZoomTool(), ZoomInTool(), ZoomOutTool(), PanTool()] plot.add_tools(*tools) plot.toolbar.active_scroll = tools[0] def get_graph_renderer(S, line_dash): # we need a consistent ordering of the edges edgelist = S.edges() nodelist = S.nodes() # get the colors assigned to each edge based on friendly/hostile sign_edge_color = [ '#87DACD' if S[u][v]['sign'] == 1 else '#FC9291' for u, v in edgelist ] # get the colors assigned to each node by coloring try: coloring_node_color = [ '#4378F8' if nodelist[v]['color'] else '#FFE897' for v in nodelist ] except KeyError: coloring_node_color = ['#FFFFFF' for __ in nodelist] graph_renderer = from_networkx(S, layout_wrapper) circle_size = 10 graph_renderer.node_renderer.data_source.add(coloring_node_color, 'color') graph_renderer.node_renderer.glyph = Circle(size=circle_size, fill_color='color') edge_size = 2 graph_renderer.edge_renderer.data_source.add(sign_edge_color, 'color') try: graph_renderer.edge_renderer.data_source.add( [S[u][v]['event_year'] for u, v in edgelist], 'event_year') graph_renderer.edge_renderer.data_source.add( [S[u][v]['event_description'] for u, v in edgelist], 'event_description') plot.add_tools( HoverTool(tooltips=[("Year", "@event_year"), ("Description", "@event_description")], line_policy="interp")) except KeyError: pass graph_renderer.edge_renderer.glyph = MultiLine(line_color='color', line_dash=line_dash) graph_renderer.inspection_policy = EdgesAndLinkedNodes() return graph_renderer try: S_dash = S.edge_subgraph( ((u, v) for u, v in edgelist if S[u][v]['frustrated'])) S_solid = S.edge_subgraph( ((u, v) for u, v in edgelist if not S[u][v]['frustrated'])) plot.renderers.append(get_graph_renderer(S_dash, 'dashed')) plot.renderers.append(get_graph_renderer(S_solid, 'solid')) except KeyError: plot.renderers.append(get_graph_renderer(S, 'solid')) plot.background_fill_color = "#202239" positions = layout_wrapper(S) if with_labels: data = {'xpos': [], 'ypos': [], 'label': []} for label, pos in positions.items(): data['label'].append(label) data['xpos'].append(pos[0]) data['ypos'].append(pos[1]) labels = LabelSet(x='xpos', y='ypos', text='label', level='glyph', source=ColumnDataSource(data), x_offset=-5, y_offset=10, text_color="#F5F7FB", text_font_size='12pt') plot.add_layout(labels) show(Row(plot)) return positions
def add_graph(self, field_names, legends, window='hann', window_length=256, noverlap=128): """ add a spectrogram plot to the graph field_names: can be a list of fields from the data set, or a list of functions with the data set as argument and returning a tuple of (field_name, data) legends: description for the field_names that will appear in the title of the plot window: the type of window to use for the frequency analysis. check scipy documentation for available window types. window_length: length of the analysis window in samples. noverlap: number of overlapping samples between windows. """ if self._had_error: return try: data_set = {} data_set['timestamp'] = self._cur_dataset.data['timestamp'] # calculate the sampling frequency # (Note: logging dropouts are not taken into account here) delta_t = ((data_set['timestamp'][-1] - data_set['timestamp'][0]) * 1.0e-6) / len(data_set['timestamp']) if delta_t < 0.000001: # avoid division by zero self._had_error = True return sampling_frequency = int(1.0 / delta_t) if sampling_frequency < 100: # require min sampling freq self._had_error = True return field_names_expanded = self._expand_field_names( field_names, data_set) # calculate the spectrogram psd = dict() for key in field_names_expanded: frequency, time, psd[key] = scipy.signal.spectrogram( data_set[key], fs=sampling_frequency, window=window, nperseg=window_length, noverlap=noverlap, scaling='density') # sum all psd's key_it = iter(psd) sum_psd = psd[next(key_it)] for key in key_it: sum_psd += psd[key] # offset = int(((1024/2.0)/250.0)*1e6) # scale time to microseconds and add start time as offset time = time * 1.0e6 + self._cur_dataset.data['timestamp'][0] color_mapper = LinearColorMapper(palette=viridis(256), low=-80, high=0) image = [10 * np.log10(sum_psd)] title = self.title for legend in legends: title += " " + legend title += " [dB]" # assume maximal data points per pixel at full resolution max_num_data_points = 2.0 * self._config['plot_width'] if len(time) > max_num_data_points: step_size = int(len(time) / max_num_data_points) time = time[::step_size] image[0] = image[0][:, ::step_size] self._p.y_range = Range1d(frequency[0], frequency[-1]) self._p.toolbar_location = 'above' self._p.image(image=image, x=time[0], y=frequency[0], dw=(time[-1] - time[0]), dh=(frequency[-1] - frequency[0]), color_mapper=color_mapper) color_bar = ColorBar(color_mapper=color_mapper, major_label_text_font_size="5pt", ticker=BasicTicker(desired_num_ticks=5), formatter=PrintfTickFormatter(format="%f"), title='[dB]', label_standoff=6, border_line_color=None, location=(0, 0)) self._p.add_layout(color_bar, 'right') # add plot zoom tool that only zooms in time axis wheel_zoom = WheelZoomTool() self._p.toolbar.tools = [ PanTool(), wheel_zoom, BoxZoomTool(dimensions="width"), ResetTool(), SaveTool() ] # updated_tools self._p.toolbar.active_scroll = wheel_zoom except (KeyError, IndexError, ValueError, ZeroDivisionError) as error: print(type(error), "(" + self._data_name + "):", error) self._had_error = True
def do_plot(self, inputDir, plotOutDir, plotOutFileName, simDataFile, validationDataFile, metadata): if metadata["variant"] != "tfActivity": print "This plot only runs for the 'tfActivity' variant." return if not os.path.isdir(inputDir): raise Exception, "inputDir does not currently exist as a directory" ap = AnalysisPaths(inputDir, variant_plot=True) variants = sorted(ap._path_data['variant'].tolist() ) # Sorry for accessing private data if 0 in variants: variants.remove(0) if len(variants) == 0: return all_cells = sorted( ap.get_cells(variant=variants, seed=[0], generation=[0])) if not os.path.exists(plotOutDir): os.mkdir(plotOutDir) expectedProbBound = [] simulatedProbBound = [] expectedSynthProb = [] simulatedSynthProb = [] targetId = [] targetCondition = [] targetToTfType = {} for variant, simDir in zip(variants, all_cells): sim_data = cPickle.load(open(ap.get_variant_kb(variant), "rb")) shape = sim_data.process.transcription_regulation.recruitmentData[ "shape"] hI = sim_data.process.transcription_regulation.recruitmentData[ "hI"] hJ = sim_data.process.transcription_regulation.recruitmentData[ "hJ"] hV = sim_data.process.transcription_regulation.recruitmentData[ "hV"] H = np.zeros(shape, np.float64) H[hI, hJ] = hV colNames = sim_data.process.transcription_regulation.recruitmentColNames tfList = ["basal (no TF)"] + sorted( sim_data.tfToActiveInactiveConds) simOutDir = os.path.join(simDir, "simOut") tf = tfList[(variant + 1) // 2] tfStatus = None if variant % 2 == 1: tfStatus = "active" else: tfStatus = "inactive" bulkMoleculesReader = TableReader( os.path.join(simOutDir, "BulkMolecules")) bulkMoleculeIds = bulkMoleculesReader.readAttribute("objectNames") rnaSynthProbReader = TableReader( os.path.join(simOutDir, "RnaSynthProb")) rnaIds = rnaSynthProbReader.readAttribute("rnaIds") tfTargetBoundIds = [] tfTargetBoundIndices = [] tfTargetSynthProbIds = [] tfTargetSynthProbIndices = [] for tfTarget in sorted(sim_data.tfToFC[tf]): tfTargetBoundIds.append(tfTarget + "__" + tf) tfTargetBoundIndices.append( bulkMoleculeIds.index(tfTargetBoundIds[-1])) tfTargetSynthProbIds.append(tfTarget + "[c]") tfTargetSynthProbIndices.append( rnaIds.index(tfTargetSynthProbIds[-1])) tfTargetBoundCountsAll = bulkMoleculesReader.readColumn( "counts")[:, tfTargetBoundIndices] tfTargetSynthProbAll = rnaSynthProbReader.readColumn( "rnaSynthProb")[:, tfTargetSynthProbIndices] for targetIdx, tfTarget in enumerate(sorted(sim_data.tfToFC[tf])): tfTargetBoundCounts = tfTargetBoundCountsAll[:, targetIdx].reshape( -1) expectedProbBound.append(sim_data.pPromoterBound[tf + "__" + tfStatus][tf]) simulatedProbBound.append(tfTargetBoundCounts[5:].mean()) tfTargetSynthProbId = [tfTarget + "[c]"] tfTargetSynthProbIndex = np.array( [rnaIds.index(x) for x in tfTargetSynthProbId]) tfTargetSynthProb = tfTargetSynthProbAll[:, targetIdx].reshape(-1) rnaIdx = np.where( sim_data.process.transcription.rnaData["id"] == tfTarget + "[c]")[0][0] regulatingTfIdxs = np.where(H[rnaIdx, :]) for i in regulatingTfIdxs[0]: if colNames[i].split("__")[1] != "alpha": if tfTarget not in targetToTfType: targetToTfType[tfTarget] = [] targetToTfType[tfTarget].append( sim_data.process.transcription_regulation. tfToTfType[colNames[i].split("__")[1]]) expectedSynthProb.append( sim_data.process.transcription.rnaSynthProb[ tf + "__" + tfStatus][rnaIdx]) simulatedSynthProb.append(tfTargetSynthProb[5:].mean()) targetId.append(tfTarget) targetCondition.append(tf + "__" + tfStatus) bulkMoleculesReader.close() rnaSynthProbReader.close() expectedProbBound = np.array(expectedProbBound) simulatedProbBound = np.array(simulatedProbBound) expectedSynthProb = np.array(expectedSynthProb) simulatedSynthProb = np.array(simulatedSynthProb) regressionResult = scipy.stats.linregress( np.log10(expectedProbBound[expectedProbBound > NUMERICAL_ZERO]), np.log10(simulatedProbBound[expectedProbBound > NUMERICAL_ZERO])) regressionResultLargeValues = scipy.stats.linregress( np.log10(expectedProbBound[expectedProbBound > 1e-2]), np.log10(simulatedProbBound[expectedProbBound > 1e-2])) ax = plt.subplot(2, 1, 1) ax.scatter(np.log10(expectedProbBound), np.log10(simulatedProbBound)) plt.xlabel("log10(Expected probability bound)", fontsize=6) plt.ylabel("log10(Simulated probability bound)", fontsize=6) plt.title( "Slope: %0.3f Intercept: %0.3e (Without Small Values: Slope: %0.3f Intercept: %0.3e)" % (regressionResult.slope, regressionResult.intercept, regressionResultLargeValues.slope, regressionResultLargeValues.intercept), fontsize=6) ax.tick_params(which='both', direction='out', labelsize=6) regressionResult = scipy.stats.linregress( np.log10(expectedSynthProb[expectedSynthProb > NUMERICAL_ZERO]), np.log10(simulatedSynthProb[expectedSynthProb > NUMERICAL_ZERO])) ax = plt.subplot(2, 1, 2) ax.scatter(np.log10(expectedSynthProb), np.log10(simulatedSynthProb)) plt.xlabel("log10(Expected synthesis probability)", fontsize=6) plt.ylabel("log10(Simulated synthesis probability)", fontsize=6) plt.title("Slope: %0.3f Intercept: %0.3e" % (regressionResult.slope, regressionResult.intercept), fontsize=6) ax.tick_params(which='both', direction='out', labelsize=6) plt.tight_layout() exportFigure(plt, plotOutDir, plotOutFileName, metadata) plt.close("all") # Probability bound - hover for ID source1 = ColumnDataSource(data=dict(x=np.log10(expectedProbBound), y=np.log10(simulatedProbBound), ID=targetId, condition=targetCondition)) hover1 = HoverTool(tooltips=[("ID", "@ID"), ("condition", "@condition")]) tools1 = [ hover1, BoxZoomTool(), LassoSelectTool(), PanTool(), WheelZoomTool(), ResizeTool(), UndoTool(), RedoTool(), "reset" ] s1 = figure(x_axis_label="log10(Expected probability bound)", y_axis_label="log10(Simulated probability bound)", width=800, height=500, tools=tools1) s1.scatter("x", "y", source=source1) if not os.path.exists(os.path.join(plotOutDir, "html_plots")): os.makedirs(os.path.join(plotOutDir, "html_plots")) bokeh.io.output_file(os.path.join( plotOutDir, "html_plots", plotOutFileName + "__probBound" + ".html"), title=plotOutFileName, autosave=False) bokeh.io.save(s1) # Synthesis probability - hover for ID source2 = ColumnDataSource(data=dict(x=np.log10(expectedSynthProb), y=np.log10(simulatedSynthProb), ID=targetId, condition=targetCondition)) hover2 = HoverTool(tooltips=[("ID", "@ID"), ("condition", "@condition")]) tools2 = [ hover2, BoxZoomTool(), LassoSelectTool(), PanTool(), WheelZoomTool(), ResizeTool(), UndoTool(), RedoTool(), "reset" ] s2 = figure(x_axis_label="log10(Expected synthesis probability)", y_axis_label="log10(Simulated synthesis probability)", width=800, height=500, tools=tools2) s2.scatter("x", "y", source=source2) bokeh.io.output_file(os.path.join( plotOutDir, "html_plots", plotOutFileName + "__synthProb" + ".html"), title=plotOutFileName, autosave=False) bokeh.io.save(s2) # Synthesis probability - filter targets by TF type bokeh.io.output_file(os.path.join( plotOutDir, "html_plots", plotOutFileName + "__synthProb__interactive" + ".html"), title=plotOutFileName, autosave=False) tfTypes = [] for i in targetId: if i in targetToTfType: uniqueSet = np.unique(targetToTfType[i]) if uniqueSet.shape[0] == 1: tfTypes.append(uniqueSet[0]) elif uniqueSet.shape[0] == 3: tfTypes.append("all") else: tfTypes.append(uniqueSet[0] + "_" + uniqueSet[1]) else: tfTypes.append("none") tfTypes = np.array(tfTypes) x0 = np.copy(expectedSynthProb) x0[np.where(tfTypes != "0CS")] = np.nan x1 = np.copy(expectedSynthProb) x1[np.where(tfTypes != "1CS")] = np.nan x2 = np.copy(expectedSynthProb) x2[np.where(tfTypes != "2CS")] = np.nan x01 = np.copy(expectedSynthProb) x01[np.where(tfTypes != "0CS_1CS")] = np.nan x02 = np.copy(expectedSynthProb) x02[np.where(tfTypes != "0CS_2CS")] = np.nan x12 = np.copy(expectedSynthProb) x12[np.where(tfTypes != "1CS_2CS")] = np.nan y0 = np.copy(simulatedSynthProb) y0[np.where(tfTypes != "0CS")] = np.nan y1 = np.copy(simulatedSynthProb) y1[np.where(tfTypes != "1CS")] = np.nan y2 = np.copy(simulatedSynthProb) y2[np.where(tfTypes != "2CS")] = np.nan y01 = np.copy(simulatedSynthProb) y01[np.where(tfTypes != "0CS_1CS")] = np.nan y02 = np.copy(simulatedSynthProb) y02[np.where(tfTypes != "0CS_2CS")] = np.nan y12 = np.copy(simulatedSynthProb) x12[np.where(tfTypes != "1CS_2CS")] = np.nan source_all = ColumnDataSource(data=dict(x=np.log10(expectedSynthProb), y=np.log10(simulatedSynthProb), ID=targetId, condition=targetCondition)) source_tf = ColumnDataSource( data=dict(x0=np.log10(x0), y0=np.log10(y0), x1=np.log10(x1), y1=np.log10(y1), x2=np.log10(x2), y2=np.log10(y2), x01=np.log10(x01), y01=np.log10(y01), x02=np.log10(x02), y02=np.log10(y02), x12=np.log10(x12), y12=np.log10(y12), x123=np.log10(expectedSynthProb), y123=np.log10(simulatedSynthProb), ID=targetId, condition=targetCondition)) hover3 = HoverTool(tooltips=[("ID", "@ID"), ("condition", "@condition")]) tools3 = [ hover3, BoxZoomTool(), LassoSelectTool(), PanTool(), WheelZoomTool(), ResizeTool(), UndoTool(), RedoTool(), "reset" ] axis_max = np.ceil(np.log10(expectedSynthProb).max()) for i in np.sort(expectedSynthProb): if i > 0: break axis_min = np.floor(np.log10(i)) s3 = figure( x_axis_label="log10(Expected synthesis probability)", y_axis_label="log10(Simulated synthesis probability)", plot_width=800, plot_height=500, x_range=(axis_min, axis_max), y_range=(axis_min, axis_max), tools=tools3, ) s3.scatter("x", "y", source=source_all) callback = CustomJS(args=dict(source_all=source_all, source_tf=source_tf), code=""" var data_all = source_all.get('data'); var data_tf = source_tf.get('data'); data_all['x'] = data_tf['x' + cb_obj.get("name")]; data_all['y'] = data_tf['y' + cb_obj.get("name")]; source_all.trigger('change'); """) toggle0 = Button(label="0CS", callback=callback, name="0") toggle1 = Button(label="1CS", callback=callback, name="1") toggle2 = Button(label="2CS", callback=callback, name="2") toggle3 = Button(label="0CS and 1CS", callback=callback, name="01") toggle4 = Button(label="0CS and 2CS", callback=callback, name="02") toggle5 = Button(label="1CS and 2CS", callback=callback, name="12") toggle6 = Button(label="All", callback=callback, name="123") layout = vplot(toggle0, toggle1, toggle2, toggle3, toggle4, toggle5, toggle6, s3) bokeh.io.save(layout) bokeh.io.curstate().reset()
# colors of the bars colorsReg = ["#2874A6", "#85C1E9"] # first dark color, then light color colorsSemi = ["#B03A2E", "#F5B7B1"] colorsIntens = ["#B7950B", "#F9E79F"] # creating the bars # bar chart according to structure in documentation: https://docs.bokeh.org/en/latest/docs/user_guide/categorical.html # and https://docs.bokeh.org/en/latest/docs/user_guide/categorical.html#basic # and https://docs.bokeh.org/en/latest/docs/user_guide/categorical.html#stacked p1 = figure( x_range=ageDevision, title= "Percentage of age group in hospital ward seperated by COVID-19 test result", toolbar_location="right", tools=([WheelZoomTool(), ResetTool(), PanTool(), "save"]), y_axis_label="Age group specific percentage per hospital ward") p1.vbar_stack(positiveReg, x=dodge('age group', -0.25, range=p1.x_range), width=0.2, source=dictDataReg, color=colorsReg, legend_label=positiveReg) p1.vbar_stack(positiveSemi, x=dodge('age group', 0.0, range=p1.x_range), width=0.2, source=dictDataSemi, color=colorsSemi,
def rca2(functionNode): logger = functionNode.get_logger() logger.info("==>>>> in rca2 (root cause analysis " + functionNode.get_browse_path()) progressNode = functionNode.get_child("control").get_child("progress") progressNode.set_value(0.1) m = functionNode.get_model() report = '<i>REPORT</i><br><div style="font-size:85%">' annotations = functionNode.get_child("annotations").get_leaves() #order = ["Step"+str(no) for no in range(1,19)] order = ["Phase" + str(no) for no in range(3, 28)] order = functionNode.get_child("annotationsOrder").get_value() annotations = data_cleaning(annotations, order=order, logger=logger) #Step1,Step2,...Step18 report += (f"found {len(annotations)} valid processes <br>") #for now, flatten them out annotations = [ subprocess for process in annotations for subprocess in process ] algo = functionNode.get_child("selectedAlgorithm").get_value() target = functionNode.get_child("selectedTarget").get_target() progressNode.set_value(0.3) #now we are building up the table by iterating all the children in "selection" entries = functionNode.get_child("selection").get_children() table = {"target": []} firstVariable = True for entry in entries: logger.debug(f"entry {entry.get_name()}") #each entry is a combination of variable, tags and feature vars = entry.get_child("selectedVariables").get_targets() tags = entry.get_child("selectedTags").get_value() features = entry.get_child("selectedFeatures").get_value() #for iterate over variables for var in vars: logger.debug( f"processing variable: {var.get_name()} with tags {tags} and features {features}" ) #columnName = var.get_name()+str(tags)+m.getRandomId() for tag in tags: row = 0 #table[columnName]=[]# make a column for idx, anno in enumerate(annotations): if anno.get_child("type").get_value() != "time": continue if tag in anno.get_child("tags").get_value(): startTime = anno.get_child("startTime").get_value() endTime = anno.get_child("endTime").get_value() data = var.get_time_series(startTime, endTime)["values"] #we take only the values "inside" the annotation if len(data) > 2: data = data[1:-1] #now create the features for feature in features: feat = calc_feature(data, feature) columnName = var.get_name( ) + "_" + tag + "_" + feature if not columnName in table: table[columnName] = [] table[columnName].append(feat) targetValue = get_target( target, (date2secs(startTime) + date2secs(endTime)) / 2) if targetValue: if firstVariable: #for the first variable we also write the target table["target"].append(targetValue) else: #for all others we make sure we have the same target value for that case (sanity check) if table["target"][row] != targetValue: logger.warning( f'problem target {table["target"][row]} !=> {targetValue}' ) row = row + 1 else: logger.warning( f"no corrrect target value for {startTime} - {endTime}" ) firstVariable = False #now we have the table, plot it import json #print(json.dumps(table,indent=2)) progressNode.set_value(0.5) #try a model algo = functionNode.get_child("selectedAlgorithm").get_value() if algo == "lasso": reg = linear_model.LassoCV() report += " using lasso Regression with auto-hyperparams <br>" else: #default report += " using linear Regression <br>" reg = linear_model.LinearRegression() #try rigde, lasso columnNames = [] dataTable = [] for k, v in table.items(): if k == "target": continue dataTable.append(v) columnNames.append(k) dataTable = numpy.asarray(dataTable) x = dataTable.T y = table["target"] x_train, x_test, y_train, y_test = train_test_split(x, y) reg.fit(x_train, y_train) print(reg.coef_) y_hat = reg.predict(x_test) y_repeat = reg.predict(x_train) print(f"predict: {y_hat} vs real: {y_test}") #check over/underfitting r_train = r2_score(y_train, y_repeat) r_test = r2_score(y_test, y_hat) report += "R<sup>2</sup> train= %.4g, R<sup>2</sup> test = %.4g <br>" % ( r_train, r_test) pearsons = [] for col in x.T: pearsons.append(pearsonr(col, y)[0]) #and finally the correlations between y and yhat y_pearson_train = pearsonr(y_train, y_repeat)[0] y_pearson_test = pearsonr(y_test, y_hat)[0] report += "pearsonCorr y/y_hat train:%.4g , test:%.4g <br>" % ( y_pearson_train, y_pearson_test) report += "regression coefficients, pearsons correlations:<br>" for col, coef, pear in zip(columnNames, reg.coef_, pearsons): report += "    %s:%.4g,   %.4g <br>" % (col, coef, pear) #write report progressNode.set_value(0.8) report += "<div>" #close the style div functionNode.get_child("report").set_value(report) #make a plot hover1 = HoverTool(tooltips=[('x,y', '$x,$y')], mode='mouse') hover1.point_policy = 'snap_to_data' hover1.line_policy = "nearest" tools = [ PanTool(), WheelZoomTool(), BoxZoomTool(), ResetTool(), SaveTool(), hover1 ] title = "prediction results on " + functionNode.get_child( "selectedAlgorithm").get_value() fig = figure(title=title, tools=tools, plot_height=400, plot_width=500) fig.toolbar.logo = None curdoc().theme = Theme(json=themes.darkTheme) fig.xaxis.major_label_text_color = themes.darkTickColor fig.yaxis.major_label_text_color = themes.darkTickColor fig.xaxis.axis_label = target.get_name() fig.xaxis.axis_label_text_color = "white" fig.yaxis.axis_label = "predicted Values for " + target.get_name() fig.yaxis.axis_label_text_color = "white" fig.circle(y_train, y_repeat, size=4, line_color="white", fill_color="white", name="train", legend_label="train") fig.circle(y_test, y_hat, line_color="#d9b100", fill_color="#d9b100", size=4, name="test", legend_label="test") fileName = functionNode.get_child("outputFileName").get_value() filePath = os.path.join(myDir, './../web/customui/' + fileName) fig.legend.location = "top_left" output_file(filePath, mode="inline") save(fig) return True
def create(palm): energy_min = palm.energy_range.min() energy_max = palm.energy_range.max() energy_npoints = palm.energy_range.size current_results = (0, 0, 0, 0) doc = curdoc() # Streaked and reference waveforms plot waveform_plot = Plot( title=Title(text="eTOF waveforms"), x_range=DataRange1d(), y_range=DataRange1d(), plot_height=PLOT_CANVAS_HEIGHT, plot_width=PLOT_CANVAS_WIDTH, toolbar_location='right', ) # ---- tools waveform_plot.toolbar.logo = None waveform_plot.add_tools(PanTool(), BoxZoomTool(), WheelZoomTool(), ResetTool()) # ---- axes waveform_plot.add_layout(LinearAxis(axis_label='Photon energy, eV'), place='below') waveform_plot.add_layout(LinearAxis(axis_label='Intensity', major_label_orientation='vertical'), place='left') # ---- grid lines waveform_plot.add_layout(Grid(dimension=0, ticker=BasicTicker())) waveform_plot.add_layout(Grid(dimension=1, ticker=BasicTicker())) # ---- line glyphs waveform_source = ColumnDataSource( dict(x_str=[], y_str=[], x_ref=[], y_ref=[])) waveform_ref_line = waveform_plot.add_glyph( waveform_source, Line(x='x_ref', y='y_ref', line_color='blue')) waveform_str_line = waveform_plot.add_glyph( waveform_source, Line(x='x_str', y='y_str', line_color='red')) # ---- legend waveform_plot.add_layout( Legend(items=[("reference", [waveform_ref_line]), ("streaked", [waveform_str_line])])) waveform_plot.legend.click_policy = "hide" # Cross-correlation plot xcorr_plot = Plot( title=Title(text="Waveforms cross-correlation"), x_range=DataRange1d(), y_range=DataRange1d(), plot_height=PLOT_CANVAS_HEIGHT, plot_width=PLOT_CANVAS_WIDTH, toolbar_location='right', ) # ---- tools xcorr_plot.toolbar.logo = None xcorr_plot.add_tools(PanTool(), BoxZoomTool(), WheelZoomTool(), ResetTool()) # ---- axes xcorr_plot.add_layout(LinearAxis(axis_label='Energy shift, eV'), place='below') xcorr_plot.add_layout(LinearAxis(axis_label='Cross-correlation', major_label_orientation='vertical'), place='left') # ---- grid lines xcorr_plot.add_layout(Grid(dimension=0, ticker=BasicTicker())) xcorr_plot.add_layout(Grid(dimension=1, ticker=BasicTicker())) # ---- line glyphs xcorr_source = ColumnDataSource(dict(lags=[], xcorr1=[], xcorr2=[])) xcorr_plot.add_glyph( xcorr_source, Line(x='lags', y='xcorr1', line_color='purple', line_dash='dashed')) xcorr_plot.add_glyph(xcorr_source, Line(x='lags', y='xcorr2', line_color='purple')) # ---- vertical span xcorr_center_span = Span(location=0, dimension='height') xcorr_plot.add_layout(xcorr_center_span) # Delays plot pulse_delay_plot = Plot( title=Title(text="Pulse delays"), x_range=DataRange1d(), y_range=DataRange1d(), plot_height=PLOT_CANVAS_HEIGHT, plot_width=PLOT_CANVAS_WIDTH, toolbar_location='right', ) # ---- tools pulse_delay_plot.toolbar.logo = None pulse_delay_plot.add_tools(PanTool(), BoxZoomTool(), WheelZoomTool(), ResetTool()) # ---- axes pulse_delay_plot.add_layout(LinearAxis(axis_label='Pulse number'), place='below') pulse_delay_plot.add_layout( LinearAxis(axis_label='Pulse delay (uncalib), eV', major_label_orientation='vertical'), place='left', ) # ---- grid lines pulse_delay_plot.add_layout(Grid(dimension=0, ticker=BasicTicker())) pulse_delay_plot.add_layout(Grid(dimension=1, ticker=BasicTicker())) # ---- line glyphs pulse_delay_source = ColumnDataSource(dict(pulse=[], delay=[])) pulse_delay_plot.add_glyph( pulse_delay_source, Line(x='pulse', y='delay', line_color='steelblue')) # ---- vertical span pulse_delay_plot_span = Span(location=0, dimension='height') pulse_delay_plot.add_layout(pulse_delay_plot_span) # Pulse lengths plot pulse_length_plot = Plot( title=Title(text="Pulse lengths"), x_range=DataRange1d(), y_range=DataRange1d(), plot_height=PLOT_CANVAS_HEIGHT, plot_width=PLOT_CANVAS_WIDTH, toolbar_location='right', ) # ---- tools pulse_length_plot.toolbar.logo = None pulse_length_plot.add_tools(PanTool(), BoxZoomTool(), WheelZoomTool(), ResetTool()) # ---- axes pulse_length_plot.add_layout(LinearAxis(axis_label='Pulse number'), place='below') pulse_length_plot.add_layout( LinearAxis(axis_label='Pulse length (uncalib), eV', major_label_orientation='vertical'), place='left', ) # ---- grid lines pulse_length_plot.add_layout(Grid(dimension=0, ticker=BasicTicker())) pulse_length_plot.add_layout(Grid(dimension=1, ticker=BasicTicker())) # ---- line glyphs pulse_length_source = ColumnDataSource(dict(x=[], y=[])) pulse_length_plot.add_glyph(pulse_length_source, Line(x='x', y='y', line_color='steelblue')) # ---- vertical span pulse_length_plot_span = Span(location=0, dimension='height') pulse_length_plot.add_layout(pulse_length_plot_span) # Folder path text input def path_textinput_callback(_attr, _old, new): save_textinput.value = new path_periodic_update() path_textinput = TextInput(title="Folder Path:", value=os.path.join(os.path.expanduser('~')), width=510) path_textinput.on_change('value', path_textinput_callback) # Saved runs dropdown menu def h5_update(pulse, delays, debug_data): prep_data, lags, corr_res_uncut, corr_results = debug_data waveform_source.data.update( x_str=palm.energy_range, y_str=prep_data['1'][pulse, :], x_ref=palm.energy_range, y_ref=prep_data['0'][pulse, :], ) xcorr_source.data.update(lags=lags, xcorr1=corr_res_uncut[pulse, :], xcorr2=corr_results[pulse, :]) xcorr_center_span.location = delays[pulse] pulse_delay_plot_span.location = pulse pulse_length_plot_span.location = pulse # this placeholder function should be reassigned in 'saved_runs_dropdown_callback' h5_update_fun = lambda pulse: None def saved_runs_dropdown_callback(_attr, _old, new): if new != "Saved Runs": nonlocal h5_update_fun, current_results saved_runs_dropdown.label = new filepath = os.path.join(path_textinput.value, new) tags, delays, lengths, debug_data = palm.process_hdf5_file( filepath, debug=True) current_results = (new, tags, delays, lengths) if autosave_checkbox.active: save_button_callback() pulse_delay_source.data.update(pulse=np.arange(len(delays)), delay=delays) pulse_length_source.data.update(x=np.arange(len(lengths)), y=lengths) h5_update_fun = partial(h5_update, delays=delays, debug_data=debug_data) pulse_slider.end = len(delays) - 1 pulse_slider.value = 0 h5_update_fun(0) saved_runs_dropdown = Dropdown(label="Saved Runs", button_type='primary', menu=[]) saved_runs_dropdown.on_change('value', saved_runs_dropdown_callback) # ---- saved run periodic update def path_periodic_update(): new_menu = [] if os.path.isdir(path_textinput.value): for entry in os.scandir(path_textinput.value): if entry.is_file() and entry.name.endswith(('.hdf5', '.h5')): new_menu.append((entry.name, entry.name)) saved_runs_dropdown.menu = sorted(new_menu, reverse=True) doc.add_periodic_callback(path_periodic_update, 5000) # Pulse number slider def pulse_slider_callback(_attr, _old, new): h5_update_fun(pulse=new) pulse_slider = Slider( start=0, end=99999, value=0, step=1, title="Pulse ID", callback_policy='throttle', callback_throttle=500, ) pulse_slider.on_change('value', pulse_slider_callback) # Energy maximal range value text input def energy_max_textinput_callback(_attr, old, new): nonlocal energy_max try: new_value = float(new) if new_value > energy_min: energy_max = new_value palm.energy_range = np.linspace(energy_min, energy_max, energy_npoints) saved_runs_dropdown_callback('', '', saved_runs_dropdown.label) else: energy_max_textinput.value = old except ValueError: energy_max_textinput.value = old energy_max_textinput = TextInput(title='Maximal Energy, eV:', value=str(energy_max)) energy_max_textinput.on_change('value', energy_max_textinput_callback) # Energy minimal range value text input def energy_min_textinput_callback(_attr, old, new): nonlocal energy_min try: new_value = float(new) if new_value < energy_max: energy_min = new_value palm.energy_range = np.linspace(energy_min, energy_max, energy_npoints) saved_runs_dropdown_callback('', '', saved_runs_dropdown.label) else: energy_min_textinput.value = old except ValueError: energy_min_textinput.value = old energy_min_textinput = TextInput(title='Minimal Energy, eV:', value=str(energy_min)) energy_min_textinput.on_change('value', energy_min_textinput_callback) # Energy number of interpolation points text input def energy_npoints_textinput_callback(_attr, old, new): nonlocal energy_npoints try: new_value = int(new) if new_value > 1: energy_npoints = new_value palm.energy_range = np.linspace(energy_min, energy_max, energy_npoints) saved_runs_dropdown_callback('', '', saved_runs_dropdown.label) else: energy_npoints_textinput.value = old except ValueError: energy_npoints_textinput.value = old energy_npoints_textinput = TextInput( title='Number of interpolation points:', value=str(energy_npoints)) energy_npoints_textinput.on_change('value', energy_npoints_textinput_callback) # Save location save_textinput = TextInput(title="Save Folder Path:", value=os.path.join(os.path.expanduser('~'))) # Autosave checkbox autosave_checkbox = CheckboxButtonGroup(labels=["Auto Save"], active=[], width=250) # Save button def save_button_callback(): if current_results[0]: filename, tags, delays, lengths = current_results save_filename = os.path.splitext(filename)[0] + '.csv' df = pd.DataFrame({ 'pulse_id': tags, 'pulse_delay': delays, 'pulse_length': lengths }) df.to_csv(os.path.join(save_textinput.value, save_filename), index=False) save_button = Button(label="Save Results", button_type='default', width=250) save_button.on_click(save_button_callback) # assemble tab_layout = column( row( column(waveform_plot, xcorr_plot), Spacer(width=30), column( path_textinput, saved_runs_dropdown, pulse_slider, Spacer(height=30), energy_min_textinput, energy_max_textinput, energy_npoints_textinput, Spacer(height=30), save_textinput, autosave_checkbox, save_button, ), ), row(pulse_delay_plot, Spacer(width=10), pulse_length_plot), ) return Panel(child=tab_layout, title="HDF5 File")
def rca(functionNode): logger = functionNode.get_logger() logger.info("==>>>> in rca (root cause analysis " + functionNode.get_browse_path()) progressNode = functionNode.get_child("control").get_child("progress") progressNode.set_value(0.1) variables = functionNode.get_child("selectedVariables").get_leaves() tag = functionNode.get_child("selectedTags").get_value() #only one tag annotations = functionNode.get_child("annotations").get_leaves() feature = functionNode.get_child("selectedFeatures").get_value() algo = functionNode.get_child("selectedAlgorithms").get_value() target = functionNode.get_child("selectedTarget").get_target() p = Progress(progressNode) p.set_divisor(len(annotations) / 0.5) p.set_offset(0.1) #now create the data as x-y results = {"x": [], "y": []} var = variables[0] #now iterate over all annotations of the matching type and create feature for idx, anno in enumerate(annotations): p.set_progress(idx) if (anno.get_child("type").get_value() == "time") and (tag in anno.get_child("tags").get_value()): startTime = anno.get_child("startTime").get_value() endTime = anno.get_child("endTime").get_value() data = var.get_time_series(startTime, endTime) #now create the feature feat = calc_feature(data["values"], feature) targetValue = get_target( target, (date2secs(startTime) + date2secs(endTime)) / 2) if feat and targetValue and numpy.isfinite( feat) and numpy.isfinite(targetValue): results["x"].append(feat) results["y"].append(targetValue) else: logger.warning( f"no result for {var.get_name} @ {startTime}, anno:{tag}, feat:{feat}, target: {target}" ) #now we have all the x-y progressNode.set_value(0.7) fig = figure(title="x-y Correlation Plot " + var.get_name(), tools=[PanTool(), WheelZoomTool(), ResetTool(), SaveTool()], plot_height=300, x_axis_label=feature + "(" + var.get_name() + ") @ " + tag, y_axis_label=target.get_name()) fig.toolbar.logo = None curdoc().theme = Theme(json=themes.darkTheme) fig.xaxis.major_label_text_color = themes.darkTickColor fig.yaxis.major_label_text_color = themes.darkTickColor fig.scatter(x=results["x"], y=results["y"], size=5, fill_color="#d9b100", marker="o") fileName = functionNode.get_child("outputFileName").get_value() filePath = os.path.join(myDir, './../web/customui/' + fileName) progressNode.set_value(0.8) output_file( filePath, mode="inline" ) #inline: put the bokeh .js into this html, otherwise the default cdn will be taken, might cause CORS problems) save(fig) #print(results) return True
def plot_data(data_df, connections, year, geoSource_new): data_df_countries = data_df #.drop_duplicates(subset=None, keep='first', inplace=True) connections_df = connections node_source = ColumnDataSource( data_df_countries[["country_id", "Country", "Longitude", "Latitude"]]) edge_source = ColumnDataSource(connections_df[["start", "end"]]) node_renderer = GlyphRenderer(data_source=node_source, glyph=node_glyph, selection_glyph=node_selection, nonselection_glyph=node_nonselection) ## Create edge_renderer edge_renderer = GlyphRenderer(data_source=edge_source, glyph=edge_glyph, hover_glyph=edge_hover, selection_glyph=edge_selection, nonselection_glyph=edge_nonselection) ## Create layout_provider graph_layout = dict( zip(data_df_countries.country_id.astype(str), zip(data_df_countries.Longitude, data_df_countries.Latitude))) layout_provider = StaticLayoutProvider(graph_layout=graph_layout) ## Create graph renderer graph = GraphRenderer(edge_renderer=edge_renderer, node_renderer=node_renderer, layout_provider=layout_provider, inspection_policy=NodesAndLinkedEdges(), selection_policy=NodesAndLinkedEdges()) plot = Plot(x_range=Range1d(-150, 150), y_range=Range1d(15, 75), plot_width=800, plot_height=600, background_fill_color=Set3_12[4], background_fill_alpha=0.2) plot.title.text = "Human Trafficing Visualization for " + str(year) # plot.add_glyph( geoSource_data, Patches(xs='xs', ys='ys', line_color='grey' # , line_width=.5, fill_color=Set3_12[6], fill_alpha=0.25)) plot.add_glyph( geoSource_new, Patches(xs='xs', ys='ys', line_color='grey', line_width=.2, fill_color={ 'field': 'Tier', 'transform': mapper2 }, fill_alpha=0.25)) plot.renderers.append(graph) plot.add_layout(LinearAxis(axis_label="Latitude"), "below") plot.add_layout(LinearAxis(axis_label="Longitude"), "left") hover = HoverTool( show_arrow=True, # tooltips= # [("Country Involved: ", "@Country")], tooltips=""" <div> <div> <span style="font-size: 15px;">Country Information </span> <span style="font-size: 12px; color: #696;">@Destination_Country </span> </div> </div> """, renderers=[graph]) hover_no_tooltips = HoverTool(tooltips=None, renderers=[graph]) box_zoom = BoxZoomTool() plot.add_tools(hover, hover_no_tooltips, box_zoom, TapTool(), BoxSelectTool(), ResetTool(), WheelZoomTool()) plot.toolbar.active_inspect = [hover, hover_no_tooltips] plot.toolbar.active_drag = box_zoom plot.outline_line_color = "navy" plot.outline_line_alpha = 0.3 plot.outline_line_width = 3 plot.add_tile(STAMEN_TONER_LABELS) return plot
data = dict( x = [1, 2, 3, 4, 4, 5, 5], y = [5, 4, 3, 2, 2.1, 1, 1.1], color = ["rgb(0, 100, 120)", "green", "blue", "#2c7fb8", "#2c7fb8", "rgba(120, 230, 150, 0.5)", "rgba(120, 230, 150, 0.5)"] ) ) xdr = DataRange1d() ydr = DataRange1d() plot = Plot(x_range=xdr, y_range=ydr) circle = Circle(x="x", y="y", radius=0.2, fill_color="color", line_color="black") circle_renderer = plot.add_glyph(source, circle) plot.add_layout(LinearAxis(), 'below') plot.add_layout(LinearAxis(), 'left') tap = TapTool(renderers=[circle_renderer], callback=Popup(message="Selected color: @color")) plot.add_tools(PanTool(), WheelZoomTool(), tap) doc = Document() doc.add_root(MyRow(children=[plot])) if __name__ == "__main__": filename = "custom.html" with open(filename, "w") as f: f.write(file_html(doc, INLINE, "Demonstration of user-defined models")) print("Wrote %s" % filename) view(filename)
y_range=Range1d( ), # changed to Range1d from DataRange1d based on the JS update issue as discussed here https://github.com/bokeh/bokeh/issues/5826 map_options=map_options, plot_width=600, plot_height=200) gmap.title.text = "Trip map" #Set it up with IllBeHome webapp gmap.api_key = "nnnnnnnnnnnnnnn" #Get your own API key! circle = Circle(x="Lon", y="Lat", size=5, fill_color="steelblue", fill_alpha=0.4, line_color=None) gmap.add_glyph(source, circle) gmap.add_tools(PanTool(), WheelZoomTool(), BoxSelectTool()) #selection circles gmap (map plot) select_gmap = Circle(x='Lon', y='Lat', fill_color='gray', fill_alpha=0.0, line_color=None, size=10) #display circles callback on gmap invis_circle = Circle(x='Lon', y='Lat', fill_color='gray', fill_alpha=0.0, line_color=None, size=20) vis_circle = Circle(x='Lon',
def line( df_in: pd.DataFrame, *args, plot_height: int = 500, plot_width: int = 1400, toolbar_location: str = 'below', legend_location: str = 'right', **kwargs, ) -> figure: """Lineplot in bokeh.""" #df = df.reset_index() df = df_in.copy() if isinstance(df, pd.Series): df = pd.DataFrame(df) df.columns = ['data'] if 'datetime' in df.columns: df = df.drop('datetime', axis=1) df.index.name = None for column in df.columns: if df[column].dtypes == 'object': for index, category in enumerate( df[column].astype('category').cat.categories): print(index, category) df[column] = df[column].astype('category').cat.codes df_cds = ColumnDataSource(df) p = figure( x_axis_type='datetime', plot_height=plot_height, plot_width=plot_width, title='', x_axis_label='timestamp', y_axis_label='value', toolbar_location=toolbar_location, tools="reset,box_zoom", # buggy: # x_range=DataRange1d( # # df.index[0], # # df.index[-1], # bounds=(df.index[0] - dt.timedelta(weeks=52), # df.index[-1] + dt.timedelta(weeks=52)), ), x_range=DataRange1d(bounds='auto'), ) col_num = 0 colors = color(len(df.columns)) legends = [] tooltips = [] for column in df.columns: r = p.line( x='index', y=column, name='value', color=colors[col_num], source=df_cds, ) col_num += 1 legends.append((column, [r])) tooltips.append((column, '@{%s}' % column)) tooltips.append(('index', '@index{%F}')) p.add_tools( HoverTool( tooltips=tooltips, renderers=[r], mode='vline', point_policy='follow_mouse', line_policy='none', formatters={'index': 'datetime'}, )) legend = Legend(items=legends, location=(0, 0)) p.add_tools(CrosshairTool()) wheel_zoom_tool = WheelZoomTool() wheel_zoom_tool.dimensions = 'width' p.add_tools(wheel_zoom_tool) p.toolbar.active_scroll = wheel_zoom_tool pan_tool = PanTool() pan_tool.dimensions = 'width' p.add_tools(pan_tool) p.add_layout(legend, legend_location) p.legend.click_policy = 'hide' show(p) return p
source = ColumnDataSource(data=dict(x=pca_data2D[:, 0], y=pca_data2D[:, 1], index=list(range(0, len(pca_data2D))), detail=corpus, labels=labels, colors=colors)) # Below is to define tools to use for interaction. # HoverTool can be added with title, author, etc. But I think they should be first included in source hover = HoverTool(tooltips=[("index", "$index"), ("(x,y)", "($x, $y)")]) TOOLS = [ hover, BoxZoomTool(), LassoSelectTool(), WheelZoomTool(), PanTool(), ResetTool(), SaveTool() ] # Below is to create the figure p = figure(plot_height=600, plot_width=700, title="Trial plot", tools=TOOLS, x_axis_label="x_PCA2D", y_axis_label="y_PCA2D", toolbar_location="above") p.circle(y="y", x="x", source=source, color='colors', size=7, alpha=0.4)
from bokeh.models import HoverTool, BoxAnnotation, BoxSelectTool, BoxZoomTool, WheelZoomTool, ResetTool from bokeh.resources import CDN from bokeh.embed import file_html # Import csv into pandas dataframe, direct to KNIME version to follow linkdata = pd.read_csv(r'.\output.csv') df = linkdata[['keyword','slope','searchVolume', 'competition', 'size']] df = df.rename(columns = {'keyword':'Keyword', 'slope':'Growth', 'searchVolume':'Search Volume', 'competition':'Organic Competition' }) # print(df) # Bokefy things source = ColumnDataSource(data=dict(x=df['Organic Competition']*100, y=df['Growth'], desc=df['Keyword'], moredesc=df['Search Volume'])) TOOLS = [HoverTool(tooltips=[("Keyword", "@desc"),("Organic Competition", "@x"),("Growth", "@y"),("Search Volume", "@moredesc")]), BoxZoomTool(), WheelZoomTool(), ResetTool()] p = figure(plot_width=900, plot_height=600, tools=TOOLS, title="Automated Keyword Research Example", x_axis_label="Organic Competition", y_axis_label="Growth") # May require playing around with bubble sizing/scaling p.circle('x', 'y', size=df['Search Volume'], line_color="black", fill_color="orange", source=source) p.line(df['Organic Competition'].mean()*100, 'y', line_width=5, source=source) p.line('x', df['Growth'].mean(), line_width=5, source=source) # Output embeddable, interactive growth matrix for keywords. html = file_html(p, CDN, "Keyword Research") output_file = 'embeddable_keyword_research_plot.html' with open(output_file, 'w') as f: f.write(html)
def plot_map(ulog, config, map_type='plain', api_key=None, setpoints=False, bokeh_plot=None): """ Do a 2D position plot :param map_type: one of 'osm', 'google', 'plain' :param bokeh_plot: if None, create a new bokeh plot, otherwise use the supplied one (only for 'plain' map_type) :return: bokeh plot object """ try: cur_dataset = ulog.get_dataset('vehicle_gps_position') t = cur_dataset.data['timestamp'] indices = cur_dataset.data['fix_type'] > 2 # use only data with a fix t = t[indices] lon = cur_dataset.data['lon'][indices] / 1e7 # degrees lat = cur_dataset.data['lat'][indices] / 1e7 altitude = cur_dataset.data['alt'][indices] / 1e3 # meters plots_width = config['plot_width'] plots_height = config['plot_height']['large'] anchor_lat = 0 anchor_lon = 0 if len(t) == 0: raise ValueError('No valid GPS position data') if map_type == 'google': data_source = ColumnDataSource(data=dict(lat=lat, lon=lon)) lon_center = (np.amin(lon) + np.amax(lon)) / 2 lat_center = (np.amin(lat) + np.amax(lat)) / 2 map_options = GMapOptions(lat=lat_center, lng=lon_center, map_type="hybrid", zoom=19) # possible map types: satellite, roadmap, terrain, hybrid p = GMapPlot(x_range=Range1d(), y_range=Range1d(), map_options=map_options, api_key=api_key, plot_width=plots_width, plot_height=plots_height) pan = PanTool() wheel_zoom = WheelZoomTool() p.add_tools(pan, wheel_zoom) p.toolbar.active_scroll = wheel_zoom line = Line(x="lon", y="lat", line_width=2, line_color=config['maps_line_color']) p.add_glyph(data_source, line) elif map_type == 'osm': # OpenStreetMaps # transform coordinates lon, lat = WGS84_to_mercator(lon, lat) data_source = ColumnDataSource(data=dict(lat=lat, lon=lon)) p = figure(tools=MAPTOOLS, active_scroll=ACTIVE_SCROLL_TOOLS) p.plot_width = plots_width p.plot_height = plots_height plot_set_equal_aspect_ratio(p, lon, lat) p.background_fill_color = "lightgray" p.axis.visible = False tile_options = {} # thunderforest tile_options[ 'url'] = 'http://b.tile.thunderforest.com/landscape/{z}/{x}/{y}.png' tile_options[ 'attribution'] = 'Maps © <a href="http://www.thunderforest.com">Thunderforest</a>, Data © <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors ' # default OpenStreetMaps # tile_options['url'] = 'http://c.tile.openstreetmap.org/{Z}/{X}/{Y}.png' # tile_options['attribution'] = '© <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors ' # FIXME: tiles disabled for now due to a rendering bug # tile_source = WMTSTileSource(**tile_options) # tile_renderer_options = {} # p.add_tile(tile_source, **tile_renderer_options) # stamen (black & white) # STAMEN_TONER = WMTSTileSource( # url='http://tile.stamen.com/toner/{Z}/{X}/{Y}.png', # attribution=( # 'Map tiles by <a href="http://stamen.com">Stamen Design</a>, ' # 'under <a href="http://creativecommons.org/licenses/by/3.0">CC BY 3.0</a>.' # 'Data by <a href="http://openstreetmap.org">OpenStreetMap</a>, ' # 'under <a href="http://www.openstreetmap.org/copyright">ODbL</a>' # ) # ) # p.add_tile(STAMEN_TONER) p.line(x='lon', y='lat', source=data_source, line_width=2, line_color=config['maps_line_color']) else: # plain # transform coordinates lat = np.deg2rad(lat) lon = np.deg2rad(lon) anchor_lat = lat[0] anchor_lon = lon[0] # try to get the anchor position from the dataset try: local_pos_data = ulog.get_dataset('vehicle_local_position') indices = np.nonzero(local_pos_data.data['ref_timestamp']) if len(indices[0]) > 0: anchor_lat = np.deg2rad( local_pos_data.data['ref_lat'][indices[0][0]]) anchor_lon = np.deg2rad( local_pos_data.data['ref_lon'][indices[0][0]]) except: pass lat, lon = map_projection(lat, lon, anchor_lat, anchor_lon) data_source = ColumnDataSource(data=dict(lat=lat, lon=lon)) if bokeh_plot is None: p = figure(tools=MAPTOOLS, active_scroll=ACTIVE_SCROLL_TOOLS, x_axis_label='[m]', y_axis_label='[m]') p.plot_width = plots_width p.plot_height = plots_height plot_set_equal_aspect_ratio(p, lon, lat) else: p = bokeh_plot # TODO: altitude line coloring p.line(x='lon', y='lat', source=data_source, line_width=2, line_color=config['maps_line_color'], legend_label='GPS (projected)') if setpoints: # draw (mission) setpoint as circles try: cur_dataset = ulog.get_dataset('position_setpoint_triplet') lon = cur_dataset.data['current.lon'] # degrees lat = cur_dataset.data['current.lat'] if map_type == 'osm': lon, lat = WGS84_to_mercator(lon, lat) elif map_type == 'plain': lat = np.deg2rad(lat) lon = np.deg2rad(lon) lat, lon = map_projection(lat, lon, anchor_lat, anchor_lon) data_source = ColumnDataSource(data=dict(lat=lat, lon=lon)) p.circle(x='lon', y='lat', source=data_source, line_width=2, size=6, line_color=config['mission_setpoint_color'], fill_color=None, legend_label='Position Setpoints') except: pass except (KeyError, IndexError, ValueError) as error: # log does not contain the value we are looking for print(type(error), "(vehicle_gps_position):", error) return None p.toolbar.logo = None # make it possible to hide graphs by clicking on the label p.legend.click_policy = "hide" return p
x = arange(-2 * pi, 2 * pi, 0.1) y = sin(x) # Create an array of times, starting at the current time, and extending # for len(x) number of hours. times = np.arange(len(x)) * 3600000 + time.time() source = ColumnDataSource(data=dict(x=x, y=y, times=times)) plot = Plot(min_border=80) circle = Circle(x="times", y="y", fill_color="red", size=5, line_color="black") plot.add_glyph(source, circle) plot.add_layout(DatetimeAxis(), 'below') plot.add_layout(DatetimeAxis(), 'left') plot.add_tools(PanTool(), WheelZoomTool(zoom_on_axis=False, speed=1 / 5000.)) doc = Document() doc.add_root(plot) if __name__ == "__main__": doc.validate() filename = "dateaxis.html" with open(filename, "w") as f: f.write(file_html(doc, INLINE, "Date Axis Example")) print("Wrote %s" % filename) view(filename)
def generateAreaSelection(dataframe, userWidth, userHeight, position_min, position_max): snps = dataframe snps["log10"] = -numpy.log10(snps.pvalue_assoc) #transformation snps = snps.sort_values(by="pvalue_assoc") # SORT BY P-VALUE max_pvalue = int(snps.log10[0:1]) # GET MINIMUM P-VALUE #----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------# #NEW COLUMNS AND RENAMING #----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------# snps['imp'] = numpy.where(snps['info_assoc'] == 1, snps['log10'], 'NaN') # gather information on imputed snps snps['Imputed'] = numpy.where( snps['info_assoc'] == 1, True, False) #discriminate between imputed and genotyped for table snps['interest'] = numpy.where(snps['log10'] >= (-numpy.log10(0.00000005)), snps['log10'], 'NaN') #select snp of interest #----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------# source = ColumnDataSource(snps) # SOURCE DATA FOR BOKEH PLOT TOOLS = [ HoverTool(tooltips=[("SNP", "@rs_id_assoc"), ( "Gene", "@gene"), ("P-value", "@pvalue_assoc"), ("Region", "@func")]), CrosshairTool(), WheelZoomTool(), BoxSelectTool(), BoxZoomTool(), ResizeTool(), ResetTool(), PanTool(), PreviewSaveTool(), TapTool() ] stringLegend = "pvalue < " + str(0.001) plot = figure(webgl=True, tools=TOOLS, x_axis_label='Position', y_axis_label='-log10(p)', plot_width=userWidth, plot_height=userHeight, x_range=(position_min - 150000, position_max + 150000), y_range=(-3.2, max_pvalue + 3) # y_range = (-1, max_pvalue+1) ) plot.circle('position', 'log10', source=source, size=7, legend='Genotyped') plot.square('position', 'imp', source=source, size=7, color="olive", legend='Imputed') plot.circle('position', 'interest', source=source, size=7, color="red", legend=stringLegend) snps = snps.sort_values(by="position") # SORT POSITIONS snps.drop_duplicates(subset=('gene'), inplace=True, keep="last") # TAKE GENE NAME DUPLICATES OFF # for i in range(0,10): # snps['ligne'+str(i+1)] = snps.start_gen[i:len(snps):10] # snps['Fligne'+str(i+1)] = snps.end_gen[i:len(snps):10] # # positions = { # 'ligne1' : -0.30, # 'ligne2' : -0.55, # 'ligne3' : -0.85, # 'ligne4' : -1.15, # 'ligne5' : -2.95, # 'ligne6' : -2.65, # 'ligne7' : -2.35, # 'ligne8' : -2.05, # 'ligne9' : -1.75, # 'ligne10' : -1.45 # } # for key, value in positions.items(): # print key, value # plot.segment(snps[key], [value]*(len(snps)), snps['F'+key],[value]*(len(snps)), line_width=6, line_color="#8b4513",) #ligne 1 # # plot.text(snps[key]+((snps['F'+key]-snps[key])/2), [value-0.05]*(len(snps)), text=snps.gene, text_color='black', text_align='center', text_font_size='5pt', text_font_style='bold') snps['ligne1'] = snps.start_gen[0:len(snps):10] snps['Fligne1'] = snps.end_gen[0:len(snps):10] snps['ligne2'] = snps.start_gen[1:len(snps):10] snps['Fligne2'] = snps.end_gen[1:len(snps):10] snps['ligne3'] = snps.start_gen[2:len(snps):10] snps['Fligne3'] = snps.end_gen[2:len(snps):10] snps['ligne4'] = snps.start_gen[3:len(snps):10] snps['Fligne4'] = snps.end_gen[3:len(snps):10] snps['ligne5'] = snps.start_gen[4:len(snps):10] snps['Fligne5'] = snps.end_gen[4:len(snps):10] snps['ligne6'] = snps.start_gen[5:len(snps):10] snps['Fligne6'] = snps.end_gen[5:len(snps):10] snps['ligne7'] = snps.start_gen[6:len(snps):10] snps['Fligne7'] = snps.end_gen[6:len(snps):10] snps['ligne8'] = snps.start_gen[7:len(snps):10] snps['Fligne8'] = snps.end_gen[7:len(snps):10] snps['ligne9'] = snps.start_gen[8:len(snps):10] snps['Fligne9'] = snps.end_gen[8:len(snps):10] snps['ligne10'] = snps.start_gen[9:len(snps):10] snps['Fligne10'] = snps.end_gen[9:len(snps):10] plot.segment( snps.ligne1, [-0.30] * (len(snps)), snps.Fligne1, [-0.30] * (len(snps)), line_width=6, line_color="#8b4513", ) #ligne 1 plot.text(snps.ligne1 + ((snps.Fligne1 - snps.ligne1) / 2), [-0.25] * (len(snps)), text=snps.gene, text_color='black', text_align='center', text_font_size='1em', text_font_style='bold') plot.segment( snps.ligne2, [-0.55] * (len(snps)), snps.Fligne2, [-0.55] * (len(snps)), line_width=6, line_color="#8b4513", ) #ligne 2 plot.text(snps.ligne2 + ((snps.Fligne2 - snps.ligne2) / 2), [-0.50] * (len(snps)), text=snps.gene, text_color='black', text_align='center', text_font_size='1em', text_font_style='bold') plot.segment( snps.ligne3, [-0.85] * (len(snps)), snps.Fligne3, [-0.85] * (len(snps)), line_width=6, line_color="#8b4513", ) #ligne 3 plot.text(snps.ligne3 + ((snps.Fligne3 - snps.ligne3) / 2), [-0.80] * (len(snps)), text=snps.gene, text_color='black', text_align='center', text_font_size='1em', text_font_style='bold') plot.segment( snps.ligne4, [-1.15] * (len(snps)), snps.Fligne4, [-1.15] * (len(snps)), line_width=6, line_color="#8b4513", ) #ligne 4 plot.text(snps.ligne4 + ((snps.Fligne4 - snps.ligne4) / 2), [-1.10] * (len(snps)), text=snps.gene, text_color='black', text_align='center', text_font_size='1em', text_font_style='bold') plot.segment( snps.ligne10, [-1.45] * (len(snps)), snps.Fligne10, [-1.45] * (len(snps)), line_width=6, line_color="#8b4513", ) #ligne 5 plot.text(snps.ligne10 + ((snps.Fligne10 - snps.ligne10) / 2), [-1.40] * (len(snps)), text=snps.gene, text_color='black', text_align='center', text_font_size='1em', text_font_style='bold') plot.segment( snps.ligne9, [-1.75] * (len(snps)), snps.Fligne9, [-1.75] * (len(snps)), line_width=6, line_color="#8b4513", ) #ligne 6 plot.text(snps.ligne9 + ((snps.Fligne9 - snps.ligne9) / 2), [-1.70] * (len(snps)), text=snps.gene, text_color='black', text_align='center', text_font_size='1em', text_font_style='bold') plot.segment( snps.ligne8, [-2.05] * (len(snps)), snps.Fligne8, [-2.05] * (len(snps)), line_width=6, line_color="#8b4513", ) #ligne 7 plot.text(snps.ligne8 + ((snps.Fligne8 - snps.ligne8) / 2), [-2.00] * (len(snps)), text=snps.gene, text_color='black', text_align='center', text_font_size='1em', text_font_style='bold') plot.segment( snps.ligne7, [-2.35] * (len(snps)), snps.Fligne7, [-2.35] * (len(snps)), line_width=6, line_color="#8b4513", ) #ligne 8 plot.text(snps.ligne7 + ((snps.Fligne7 - snps.ligne7) / 2), [-2.30] * (len(snps)), text=snps.gene, text_color='black', text_align='center', text_font_size='1em', text_font_style='bold') plot.segment( snps.ligne6, [-2.65] * (len(snps)), snps.Fligne6, [-2.65] * (len(snps)), line_width=6, line_color="#8b4513", ) #ligne 9 plot.text(snps.ligne6 + ((snps.Fligne6 - snps.ligne6) / 2), [-2.60] * (len(snps)), text=snps.gene, text_color='black', text_align='center', text_font_size='1em', text_font_style='bold') plot.segment( snps.ligne5, [-2.95] * (len(snps)), snps.Fligne5, [-2.95] * (len(snps)), line_width=6, line_color="#8b4513", ) #ligne 10 plot.text(snps.ligne5 + ((snps.Fligne5 - snps.ligne5) / 2), [-2.90] * (len(snps)), text=snps.gene, text_color='black', text_align='center', text_font_size='1em', text_font_style='bold') plot.grid.grid_line_color = None # TAKE GRID LINES OFF THE GRAPH graph, div1 = components(plot, CDN) return graph, div1
df_dead, columns=["infectionSourceCountry"]) most_infected_loc = df_count_loc.iloc[-1].sort_values().index.tolist() most_infected_orig = df_count_orig.iloc[-1].sort_values().index.tolist() hoover = HoverTool(tooltips=[ ("status", "@case"), ("id", "@id"), ("infection from", "@infection_from"), ("location", "@healthCareDistrict"), ("date", "@date{%F}"), ("origin", "@origin"), ], formatters={'date': 'datetime'}) toolbox = [PanTool(), WheelZoomTool(), hoover, ResetTool(), BoxZoomTool()] datasource = get_datasource(df) p_net = get_network_plot(datasource, df, toolbox) data_table = get_datatable(datasource) p_obs = get_timeline_plot(datasource, toolbox, names=most_infected_loc, y="healthCareDistrict", title="Observations per location") p_area = get_timeseries_plot(df_count_loc, toolbox, p_obs.x_range, names=most_infected_loc, title="Active cases per location")
plot = GMapPlot( x_range=DataRange1d(), y_range=DataRange1d(), map_options=map_options) plot.title.text = "London Crime Correlation Heatmap" # For GMaps to function, Google requires you obtain and enable an API key: plot.api_key = "[ENTER YOUR GOOGLE MAP API KEY HERE]" hover = HoverTool(tooltips=[("CountySel", "@acounty"), ("CountyComp", "@county"), ("Crime", "@acrime"), ("Lag","@lag")]) circle = Circle(x="longitude", y="latitude", size="size_mult", fill_color="color", fill_alpha=0.8, line_color=None) plot.add_glyph(source, circle) plot.add_tools(ResetTool(), PanTool(), WheelZoomTool(), BoxSelectTool(),hover) def generic_update(attr,old, new): global df, colors df2 = df[(df.acounty==county_select.value) & (df.acrime == crime_select.value)] source.data = dict( acounty = df2.acounty, acrime = df2.acrime, county = df2.bcounty, crime = df2.bcrime, lag = df2.lag, calc_cor = df2.calc_cor, latitude= df2.latitude, longitude = df2.longitude, size_mult = df2.size_mult, color = df2.color)
PanTool, WheelZoomTool) from bokeh.resources import INLINE x = arange(-2 * pi, 2 * pi, 0.1) y = sin(x) # Create an array of times, starting at the current time, and extending # for len(x) number of hours. # times = np.arange(len(x)) * 3600000 + time.time() times = np.arange(len(x)) * 3600 + time.time() source = ColumnDataSource(data=dict(x=x, y=y, times=times)) xdr = DataRange1d() ydr = DataRange1d() print('xdr=', xdr, 'ydr=', ydr) p = figure(title="test", toolbar_location="above") p.grid.grid_line_color = "navy" p.background_fill_color = "#eeeeee" p.scatter(x, y, size=15, line_color="navy", fill_color="orange", alpha=0.5) #circle = Circle(x="times", y="y", fill_color="red", size=5, line_color="black") #p.add_glyph(source, circle) p.add_layout(DatetimeAxis(), 'below') p.add_layout(DatetimeAxis(), 'left') p.add_tools(PanTool(), WheelZoomTool()) show(p)
z = cos(x) source = ColumnDataSource(data=dict(x=x, y=y)) xdr = DataRange1d() ydr = DataRange1d() plot = Plot(x_range=xdr, y_range=ydr, min_border=50) line_glyph = Line(x="x", y="y", line_color="blue") plot.add_glyph(source, line_glyph) plot.add_layout(LinearAxis(), 'below') plot.add_layout(LinearAxis(), 'left') pan = PanTool() wheel_zoom = WheelZoomTool() preview_save = PreviewSaveTool() plot.add_tools(pan, wheel_zoom, preview_save) doc = Document() doc.add(plot) if __name__ == "__main__": filename = "line.html" with open(filename, "w") as f: f.write(file_html(doc, INLINE, "Line Glyph Example")) print("Wrote %s" % filename) view(filename)
def plotGeoPoints(data_dict, data_title, offset=0.25, pt_size=7, save_dir=None): if save_dir is not None: path = os.path.expanduser(save_dir) if os.path.exists(path) and os.path.isdir(path): os.chdir(path) else: print( "Warning: either {} does not exist or is not a valid directory; writing html file to {}" .format(path, os.getcwd())) noPunc = re.compile("[%s\\…]" % re.escape(string.punctuation)) out_file_name = "{}.html".format( noPunc.sub("", data_title).replace(" ", "_")) ''' Plot the points collected from GeoMesa''' fid, x, y, what, who, when, why = zip(*[(key, row['x'], row['y'], row['what'], row['who'], row['when'], row['why']) for key, row in data_dict.items()]) ''' Put the time fields into mm/dd/yyyy h:m am/pm format: ''' when = [tm.strftime("%M/%d/%Y %I:%M %p") for tm in when] data_source = ColumnDataSource( dict(fid=fid, x=x, y=y, what=what, who=who, when=when, why=why)) hover = HoverTool(tooltips=[ ("fid", "@fid"), ("(x,y)", "($x, $y)"), ("Who", "@who"), ("What", "@what"), ("When", "@when"), ("Why", "@why"), ]) tools = [ PanTool(), BoxZoomTool(), CrosshairTool(), hover, WheelZoomTool(), ZoomInTool(), ZoomOutTool(), ResetTool(), SaveTool() ] ''' Set the values to show with hover: ''' output_file(out_file_name, title="Results of {}".format(data_title)) geo_plt = figure(title="GeoMesa--{}".format(data_title), tools=tools, plot_width=600, plot_height=600) geo_plt.title.align = 'center' geo_plt.toolbar.logo = None geo_plt.toolbar_sticky = False geo_plt.xaxis.axis_label = "Lat" geo_plt.yaxis.axis_label = "Long" geo_plt.y_range = Range1d(start=min(y) - offset, end=max(y) + offset) geo_plt.x_range = Range1d(start=min(x) - offset, end=max(x) + offset) geo_plt.square("x", "y", fill_color="red", line_color=None, source=data_source, size=pt_size) show(geo_plt) reset_output()
def __init__(self, worker, height=300, **kwargs): with log_errors(): self.worker = worker names = [ "start", "stop", "middle", "duration", "who", "y", "hover", "alpha", "bandwidth", "total", ] self.incoming = ColumnDataSource({name: [] for name in names}) self.outgoing = ColumnDataSource({name: [] for name in names}) x_range = DataRange1d(range_padding=0) y_range = DataRange1d(range_padding=0) fig = figure(title="Peer Communications", x_axis_type="datetime", x_range=x_range, y_range=y_range, height=height, tools="", **kwargs) fig.rect( source=self.incoming, x="middle", y="y", width="duration", height=0.9, color="red", alpha="alpha", ) fig.rect( source=self.outgoing, x="middle", y="y", width="duration", height=0.9, color="blue", alpha="alpha", ) hover = HoverTool(point_policy="follow_mouse", tooltips="""@hover""") fig.add_tools( hover, ResetTool(), PanTool(dimensions="width"), WheelZoomTool(dimensions="width"), ) self.root = fig self.last_incoming = 0 self.last_outgoing = 0 self.who = dict()
def create_bokeh_figure(df_all_pts, df_label): df_label['url'] = waypoint_url w_image = 25 h_image = 600 / 390 * w_image df_all_pts['leg'] = df_all_pts['leg'].astype(str) df_source_labels = ColumnDataSource(df_label) unique_legs = df_all_pts['leg'].astype(str).unique().tolist() # range bounds supplied in web mercator coordinates xrange = (df_all_pts['Longitude'].round(decimals=2).min(), df_all_pts['Longitude'].round(decimals=2).max()) yrange = (df_all_pts['Latitude'].round(decimals=2).min(), df_all_pts['Latitude'].round(decimals=2).max()) tooltips = [ ("Address", "@label"), ] tools = [ResetTool(), PanTool(), WheelZoomTool()] p = figure(x_range=xrange, y_range=yrange, # tooltips=tooltips, x_axis_type="mercator", y_axis_type="mercator", plot_width=1000, plot_height=800, tools=tools ) p.add_tile(OSM_tile_source) # p.add_tile(OSM_tile_source) # Add lines: for leg in unique_legs: df_sub_source = ColumnDataSource(df_all_pts[df_all_pts['leg'] == leg]) p.line(x='Longitude', y='Latitude', color='cornflowerblue', source=df_sub_source, line_width=3) p.line(x='Longitude', y='Latitude', color='cornflowerblue', source=df_sub_source, line_width=15, alpha=0.3) circle_renderer = p.circle(x='Longitude', y='Latitude', fill_color='white', fill_alpha=0.05, line_color='midnightblue', line_alpha=0.01, source=df_source_labels, size=100, # hover_line_color='black', line_width=0) tool_circle_hover = HoverTool(renderers=[circle_renderer], tooltips=tooltips) p.image_url(url='url', anchor='bottom_center', x='Longitude', y='Latitude', w=w_image, h=h_image, source=df_source_labels, w_units='screen', h_units='screen') p.text(x='Longitude', y=jitter('Latitude', mean=500, width=1000), text='label', source=df_source_labels, text_color='midnightblue') p.add_tools(tool_circle_hover) p.xaxis.axis_label = 'Longitude' p.yaxis.axis_label = 'Latitude' p.xaxis.visible = False p.yaxis.visible = False return p
'#FDE724', '#B2DD2C', '#6BCD59', '#35B778', '#1E9C89', '#25828E', '#30678D', '#3E4989', '#472777', '#440154' ], low=int(min(source.data.get('searches')) / 50000), high=int(max(source.data.get('searches')) / 50000)) color_bar = ColorBar(color_mapper=color_mapper, orientation='horizontal', location='bottom_left', scale_alpha=0.7) plot.add_layout(color_bar) hover = HoverTool(tooltips=[ ("Market", "@dim_market"), ("Country", "@dim_country_name"), ]) plot.add_tools(PanTool(), WheelZoomTool(), hover) # --------------------------------- # # Map Animation Interaction # # --------------------------------- # def animate_update(): date_ix = slider.value + 1 if date_ix > END_IDX: date_ix = START_IDX slider.value = date_ix def slider_update(attrname, old, new): date_ix = slider.value label.text = str(zip_date_ranges[date_ix][1])
def makeplot_2(df, FC_P, PV_P, Inhibitor): df = df[df['Kinase'] != ''] # Drop of data with no Kinase allocated df.loc[(df['Fold_change'] > FC_P) & (df['p_value'] < PV_P), 'color'] = "Blue" # upregulated #df.loc - Selects single row or subset of rows from the DataFrame by label df.loc[(df['Fold_change'] <= FC_P) & (df['p_value'] < PV_P), 'color'] = "Purple" # downregulated df['color'].fillna('grey', inplace=True) df["log_pvalue"] = -np.log10(df['p_value']) df["log_FC"] = np.log2(df['Fold_change']) df.head() output_notebook() category = 'Substrate' category_items = df[category].unique() title = Inhibitor + " :Data with identified kinases" #feeding data into ColumnDataSource source = ColumnDataSource(df) #Editing the hover that need to displayed while hovering hover = HoverTool( tooltips=[('Kinase', '@Kinase'), ('Substrate', '@Substrate'), ('Sub_gene', '@Sub_gene'), ('Phosphosite', '@Phosphosite'), ('Fold_change', '@Fold_change'), ('p_value', '@p_value')]) #tools that are need to explote data tools = [ hover, WheelZoomTool(), PanTool(), BoxZoomTool(), ResetTool(), SaveTool() ] #finally making figure with scatter plot p = figure( tools=tools, title=title, plot_width=700, plot_height=400, toolbar_location='right', toolbar_sticky=False, ) p.scatter(x='log_FC', y='log_pvalue', source=source, size=10, color='color') #displaying the graph return (p)