def classical_gear(module, large_teeth, small_teeth): xdr = Range1d(start=-300, end=150) ydr = Range1d(start=-100, end=100) plot = Plot(x_range=xdr, y_range=ydr, plot_width=800, plot_height=800) plot.add_tools(PanTool(), WheelZoomTool(), BoxZoomTool(), UndoTool(), RedoTool(), ResetTool()) radius = pitch_radius(module, large_teeth) angle = 0 glyph = Gear(x=-radius, y=0, module=module, teeth=large_teeth, angle=angle, fill_color=fill_color[0], line_color=line_color) plot.add_glyph(glyph) radius = pitch_radius(module, small_teeth) angle = half_tooth(small_teeth) glyph = Gear(x=radius, y=0, module=module, teeth=small_teeth, angle=angle, fill_color=fill_color[1], line_color=line_color) plot.add_glyph(glyph) return plot
def main(): """Invoke when run directly as a program.""" args = parse_arguments() df = pd.read_csv(args.infile, sep='\s+', names=["amplicon", "meancov", "gene"]) df['offsetcov'] = df['meancov'] + 0.1 # shift zero values by 0.1 df = df.dropna().reset_index(drop=True) # Make a hover (show amplicon name on mouse-over) hover = HoverTool(tooltips=[("Amplicon", "@names"), ("Y value", "$y")]) tools = [PanTool(), BoxZoomTool(), WheelZoomTool(), RedoTool(), UndoTool(), ResetTool(), hover, SaveTool()] # Produce plot output_file(args.outfile) fig = figure(tools=tools, width=1200, height=600, y_axis_type='log') # Fill plot with one point for each amplicon: xvals, yvals, labels, colors = [], [], [], [] for i, (name, group) in enumerate(df.groupby('gene', sort=False)): xvals.extend(list(group.index)) yvals.extend(list(group.offsetcov)) labels.extend(list(group.amplicon)) # Elements in the same group should have the same color. Cycle between colors in COLOR_CYCLE: colors.extend([COLOR_CYCLE[i % len(COLOR_CYCLE)]] * len(list(group.index))) data = ColumnDataSource(data=dict(x=xvals, y=yvals, names=labels)) fig.circle('x', 'y', size=10, source=data, color=colors) # Make span lines on 0.05, 0.1, 0.2, 1 and 5 mutiples of mean amplicon coverage: mean_coverage = df.offsetcov.mean() span_lines = [(5.0, 'Blue'), (1.0, 'Green'), (0.2, 'Red'), (0.1, 'Purple'), (0.05, 'Magenta')] xmin, xmax = min(xvals) - 1, max(xvals) + 1 for ratio, color in span_lines: fig.line([xmin, xmax], [mean_coverage * ratio] * 2, line_color=color, line_dash='dashed', legend='{:.0f} % of mean coverage'.format(ratio * 100)) # Customize plot: ymax = 2.0 * max(df.offsetcov.max() + 1000, mean_coverage * 5) ymin = 0.2 fig.y_range = Range1d(ymin, ymax) fig.x_range = Range1d(xmin, xmax) fig.xaxis.major_tick_line_color = None # Turn off x-axis major ticks fig.xaxis.minor_tick_line_color = None # Turn off x-axis minor ticks fig.xaxis.major_label_text_font_size = '0pt' # Hack to remove tick labels fig.xaxis.axis_label = 'Amplicon' fig.yaxis.axis_label = 'Log10 (Amplicon coverage)' fig.legend.location = "bottom_right" script, div = components(layouts.row(fig)) with open(os.path.join(os.path.dirname(args.outfile), 'plot.js'), 'wt') as jfile: jfile.write('\n'.join(script.split('\n')[2:-1])) with open(args.outfile, 'wt') as ofile: env = Environment(loader=FileSystemLoader(os.path.dirname(args.template))) page_template = env.get_template(os.path.basename(args.template)) html_text = page_template.render({'bokeh_div': div}) # pylint: disable=no-member ofile.write(html_text)
def drawgraph(self, ds, filterMin, filterMax, layout): G = nx.Graph() # create an empty graph with no nodes and no edges # nodes = [] # for i in range(len(ds.getNodes())): # nodes.append([]) # # print(ds.getNodes()[i].getName()) # for j in range(len(ds.getNodes())): # nodes[i].append(ds.getNodes()[i].getLinks()[j][1]) # # print(" " + str(ds.getNodes()[i].getLinks()[j][1])) # ds.toMinSpanTree() nodes = ds.getDoubleList(filterMin, filterMax, False) x = filterMin adj = np.array(nodes) G = nx.from_numpy_matrix(adj) for i in range(len(G.node)): G.node[i]['name'] = ds.getNames()[i] #pos = nx.drawing.layout.circular_layout(G, 1, None, 2) #nx.draw_networkx(G, pos, with_labels=True) # pt.show() #plt.show() plot = Plot(plot_width=500, plot_height=500, x_range=Range1d(-1.1, 1.1), y_range=Range1d(-1.1, 1.1)) node_hover_tool = HoverTool(tooltips=[("Name of this node", "@name")]) # plot.add_tools(node_hover_tool, BoxZoomTool(), ResetTool()) plot.add_tools(node_hover_tool, TapTool(), BoxSelectTool(), BoxZoomTool(), UndoTool(), RedoTool(), SaveTool(), ResetTool()) plot.toolbar_location = 'left' if layout == 0: graph_renderer = from_networkx(G, nx.circular_layout, scale=1, center=(0, 0)) elif layout == 1: graph_renderer = from_networkx(G, nx.spring_layout, scale=1, center=(0, 0)) else: graph_renderer = from_networkx(G, nx.random_layout) graph_renderer.node_renderer.glyph = Circle(size=15, fill_color=Spectral4[0]) graph_renderer.node_renderer.selection_glyph = Circle(size=15, fill_color=Spectral4[2]) graph_renderer.node_renderer.hover_glyph = Circle(size=15, fill_color=Spectral4[1]) graph_renderer.edge_renderer.glyph = MultiLine(line_color="#CCCCCC", line_alpha=0.8, line_width=5) graph_renderer.edge_renderer.selection_glyph = MultiLine(line_color=Spectral4[2], line_width=5) graph_renderer.edge_renderer.hover_glyph = MultiLine(line_color=Spectral4[1], line_width=5) graph_renderer.selection_policy = NodesAndLinkedEdges() #graph_renderer.inspection_policy = EdgesAndLinkedNodes() plot.renderers.append(graph_renderer) output_file("interactive_graphs.html") return plot
def sample_gear(): xdr = Range1d(start=-30, end=30) ydr = Range1d(start=-30, end=30) plot = Plot(x_range=xdr, y_range=ydr, plot_width=800, plot_height=800) plot.add_tools(PanTool(), WheelZoomTool(), BoxZoomTool(), UndoTool(), RedoTool(), ResetTool()) glyph = Gear(x=0, y=0, module=5, teeth=8, angle=0, shaft_size=0.2, fill_color=fill_color[2], line_color=line_color) plot.add_glyph(glyph) return plot
def epicyclic_gear(module, sun_teeth, planet_teeth): xdr = Range1d(start=-150, end=150) ydr = Range1d(start=-150, end=150) plot = Plot(title=None, x_range=xdr, y_range=ydr, plot_width=800, plot_height=800) plot.add_tools(PanTool(), WheelZoomTool(), BoxZoomTool(), UndoTool(), RedoTool(), ResetTool()) annulus_teeth = sun_teeth + 2 * planet_teeth glyph = Gear(x=0, y=0, module=module, teeth=annulus_teeth, angle=0, fill_color=fill_color[0], line_color=line_color, internal=True) plot.add_glyph(glyph) glyph = Gear(x=0, y=0, module=module, teeth=sun_teeth, angle=0, fill_color=fill_color[2], line_color=line_color) plot.add_glyph(glyph) sun_radius = pitch_radius(module, sun_teeth) planet_radius = pitch_radius(module, planet_teeth) radius = sun_radius + planet_radius angle = half_tooth(planet_teeth) for i, j in [(+1, 0), (0, +1), (-1, 0), (0, -1)]: glyph = Gear(x=radius * i, y=radius * j, module=module, teeth=planet_teeth, angle=angle, fill_color=fill_color[1], line_color=line_color) plot.add_glyph(glyph) return plot
def _set_initial_figure(self): boxzoom = BoxZoomTool() pan = PanTool(dimensions='width') wheelzoom = WheelZoomTool(dimensions='width') reset = ResetTool() undo = UndoTool() tools = [pan, boxzoom, wheelzoom, reset, undo] p = figure(plot_width=1000, plot_height=300, tools=tools) p.toolbar.active_drag = pan p.toolbar.active_scroll = wheelzoom p.toolbar.active_tap = None p.toolbar.logo = None x_range_obj = Range1d(start=0, end=25) p.x_range = x_range_obj p.y_range = Range1d(start=-0.1, end=1.1) initial_ticks = list(range(30)) x_ticker = FixedTicker(ticks=initial_ticks) p.xaxis.ticker = x_ticker p.xgrid.ticker = x_ticker initial_ticks = list([i / 10 for i in range(11)]) y_ticker = FixedTicker(ticks=initial_ticks) p.yaxis.ticker = y_ticker p.ygrid.ticker = y_ticker p.title.text = 'Segment Matching Of Structures' p.title.align = 'center' p.yaxis.axis_label = 'Score' p.x("xs", 'ys', source=self.CDS, line_color='color', line_dash='solid', line_width=2) p.multi_line(xs="xs_line", ys='ys_line', color='color_line', source=self.line_CDS, line_width=2) return p, x_range_obj, x_ticker
def _set_logo(self): boxzoom = BoxZoomTool() pan = PanTool(dimensions='width') wheelzoom = WheelZoomTool(dimensions='width') reset = ResetTool() undo = UndoTool() tools = [pan, boxzoom, wheelzoom, reset, undo] p = figure(width=1000, height=200, tools=tools) glyph = ImageURL(url="url", x='x', y=4.3, w=30, h=4.4) p.add_glyph(self.logo_CDS, glyph) p.x_range = self.fig_x_range p.y_range = Range1d(start=0, end=4.4) p.toolbar.active_drag = pan p.toolbar.logo = None p.xaxis.ticker = self.fig_x_ticker p.xaxis.axis_label = 'Residue Position' p.yaxis.axis_label = 'Information Bits' return p
def NLD_add_tools(plot): plot.add_tools(WheelZoomTool()) plot.add_tools(ZoomInTool()) plot.add_tools(ZoomOutTool()) plot.add_tools(ResetTool()) plot.add_tools(UndoTool()) plot.add_tools(RedoTool()) plot.add_tools(PanTool()) plot.add_tools(TapTool()) plot.add_tools(SaveTool()) plot.add_tools(BoxSelectTool()) plot.add_tools(LassoSelectTool()) plot.add_tools(BoxZoomTool()) # !!! Hover the node attributes !!! node_hover = HoverTool(tooltips=[('Name', '@index'), ('Degree', '@degree'), ('Min Weight', '@minweight'), ('Max Weight', '@maxweight'), ('Average Weight', '@avrweight'), ('Sum Weight', '@sumweight')]) plot.add_tools(node_hover)
def plot_feature(df, feature_name, vehicle, trip): source = ColumnDataSource(df) datetime = EXPERIMENTS[vehicle][trip] TOOLS = [PanTool(), ResetTool(), SaveTool(), UndoTool(), WheelZoomTool()] p = figure( width=790, height=395, title=vehicle[4:] + " on " + datetime[:10] + " @ " + datetime[11:], toolbar_location="above", tools=TOOLS, x_axis_type="datetime", ) p.line(x="DATETIME", y=feature_name, line_color="blue", line_width=2, source=source) show(p) return None
graph_circle.node_renderer.hover_glyph = Circle(size=10, fill_alpha=0.8, fill_color='yellow') graph_circle.edge_renderer.glyph = MultiLine(line_width=3, line_alpha=0.8, line_color='color') graph_circle.edge_renderer.selection_glyph = MultiLine(line_width=4, line_alpha=0.8, line_color='red') graph_circle.edge_renderer.hover_glyph = MultiLine(line_width=4, line_alpha=0.8, line_color='yellow') graph_circle.edge_renderer.glyph.line_width = {'field': 'weight'} graph_circle.selection_policy = NodesAndLinkedEdges() graph_circle.inspection_policy = NodesAndLinkedEdges() plot_circle.add_tools(WheelZoomTool()) plot_circle.add_tools(ZoomOutTool()) plot_circle.add_tools(ZoomInTool()) plot_circle.add_tools(ResetTool()) plot_circle.add_tools(UndoTool()) plot_circle.add_tools(RedoTool()) plot_circle.add_tools(PanTool()) plot_circle.add_tools(TapTool()) plot_circle.add_tools(SaveTool()) plot_circle.add_tools(BoxSelectTool()) plot_circle.add_tools(LassoSelectTool()) # !!! Hover the node attributes !!! node_hover = HoverTool(tooltips=[('Name', '@index'), ('Degree', '@degree'), ('Min Weight', '@minweight'), ('Max Weight', '@maxweight'), ('Sum Weight', '@sumweight')]) plot_circle.add_tools(node_hover) plot_circle.add_layout(color_bar, 'right') plot_circle.renderers.append(graph_circle)
def makeMatrix(self, ds, filterMin, filterMax, reorder): # getting data from FileLoader and creating the right format dataSetSort = DataSetSort() if reorder == 0: nodes = ds.getDoubleList(filterMin, filterMax, True) elif reorder == 1: dsCopy = DataSet(ds.getNodes()) dsCopy.makeUndirectionalAdd() nodes = dsCopy.getDoubleList(filterMin, filterMax, True) elif reorder == 2: dataSetSort.DesConnectionSort(ds) nodes = ds.getDoubleList(filterMin, filterMax, True) elif reorder == 3: dataSetSort.DesStrengthSort(ds) nodes = ds.getDoubleList(filterMin, filterMax, True) elif reorder == 4: nodes = ds.distanceMatrix(True) elif reorder == 5: dataSetSort.robinsonSort(ds) nodes = ds.getDoubleList(filterMin, filterMax, True) names = ds.getNames() yNames = names.copy() yNames.reverse() df = pd.DataFrame( nodes, columns=yNames, index=names) df.index.name = 'X' df.columns.name = 'Y' # Prepare data.frame in the right format df = df.stack().rename("value").reset_index() # Making the plot html file output_file("matrixPlot.html") # Creating the array containing the colors colorList = [] i = 0 while i < 256: color = wc.rgb_to_hex((255-(i-10), 255-(i-20), 255-(i-30))) colorList.append(color) i = i + 10 #colorList = [(23, 165, 137), (19, 141, 117), (40, 180, 99), (36, 113, 163) # , (31, 97, 141), (17, 122, 101), (46, 134, 193), (34, 153, 84)]#,(202, 111, 30), (186, 74, 0)] #colors = [] #for i in colorList: # color = wc.rgb_to_hex(i) # colors.append(color) colors = colorList # This part maps the colors at intervals mapper = LinearColorMapper( palette=colors, low=df.value.min(), high=df.value.max()) # Creating the figure p = figure( plot_width=500, plot_height=500, x_range=list(df.X.drop_duplicates()), y_range=list(df.Y.drop_duplicates()), # Adding a toolbar #toolbar_location="right", #tools="hover,pan,box_zoom,undo,redo,reset,save", x_axis_location="above") node_hover_tool = HoverTool(tooltips=[("Name X Axis", "@X"), ("Name Y Axis", "@Y"), ("Relation Strength", "@value")]) # plot.add_tools(node_hover_tool, BoxZoomTool(), ResetTool()) p.add_tools(node_hover_tool, TapTool(), BoxSelectTool(), BoxZoomTool(), UndoTool(), RedoTool()) p.toolbar_location = 'right' # Create rectangle for heatmap p.rect( x="X", y="Y", width=1, height=1, source=ColumnDataSource(df), line_color=None, fill_color=transform('value', mapper)) # Add legend color_bar = ColorBar( color_mapper=mapper, location=(0, 0), ticker=BasicTicker(desired_num_ticks=len(colors))) if len(list(df.X.drop_duplicates())) > p.plot_width / 10: p.xaxis.visible = False else: pass if len(list(df.Y.drop_duplicates())) > p.plot_height / 10: p.yaxis.visible = False else: pass p.xaxis.major_label_orientation = 'vertical' p.add_layout(color_bar, 'right') return p
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()
def do_plot(self, inputDir, plotOutDir, plotOutFileName, simDataFile, validationDataFile, metadata): 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 len(variants) <= 1: return all_cells = sorted( ap.get_cells(variant=variants, seed=[0], generation=[0])) if not os.path.exists(plotOutDir): os.mkdir(plotOutDir) #make structures to hold mean flux values mean_fluxes = [] BURN_IN_STEPS = 20 n_variants = 0 IDs = [] #Puts you into the specific simulation's data. Pull fluxes from here #TODO LEARN HOW TO PULL FLUXES FROM LISTENER FILE (see kineticsflux comparison) for variant, simDir in zip(variants, all_cells): sim_data = cPickle.load(open(ap.get_variant_kb(variant), "rb")) simOutDir = os.path.join(simDir, "simOut") #crafting area enzymeKineticsReader = TableReader( os.path.join(simOutDir, "FBAResults")) # "EnzymeKinetics")) actualFluxes = enzymeKineticsReader.readColumn( "reactionFluxes") #"actualFluxes") IDs = enzymeKineticsReader.readAttribute("reactionIDs") enzymeKineticsReader.close() actualAve = np.mean(actualFluxes[BURN_IN_STEPS:, :], axis=0) mean_fluxes.append(actualAve) n_variants = n_variants + 1 ###Plot the fluxes plt.figure(figsize=(8.5, 11)) #Generalizred plotting for j in range(0, n_variants): for k in range(0, n_variants): if j <= k: continue plt.subplot(n_variants - 1, n_variants - 1, j + k) plt.plot(np.log10(mean_fluxes[j][:]), np.log10(mean_fluxes[k][:]), 'o') plt.plot([-12, 0], [-12, 0], color='k', linestyle='-', linewidth=2) plt.xlabel('Variant ' + str(j) + ' Flux') plt.ylabel('Variant ' + str(k) + ' Flux') plt.ylim((-11, 0)) plt.xlim((-11, 0)) exportFigure(plt, plotOutDir, plotOutFileName, metadata) plt.close("all") #nifty fun tool # Bokeh if len(mean_fluxes) < 2: return # Plot first metabolite to initialize plot settings x = np.log10(mean_fluxes[0][:]) y = np.log10(mean_fluxes[1][:]) source = ColumnDataSource(data=dict(x=x, y=y, rxn=IDs)) hover = HoverTool(tooltips=[ ("ID", "@rxn"), ]) TOOLS = [ hover, BoxZoomTool(), LassoSelectTool(), PanTool(), WheelZoomTool(), ResizeTool(), UndoTool(), RedoTool(), "reset" ] p = figure( x_axis_label="Variant 0 Flux", y_axis_label="Variant 1 Flux", width=800, height=800, tools=TOOLS, ) p.circle( 'x', 'y', size=5, source=source ) #np.log10(mean_fluxes[0][:]),np.log10(mean_fluxes[1][:]), size=10) p.line([-12, 0], [-12, 0], color="firebrick", line_width=2) 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 + ".html"), title=plotOutFileName, autosave=False) bokeh.io.save(p) bokeh.io.curstate().reset()
def main(): """ An example sequence of commands to make a measurement with the P1125 REST API. The internal Calibration loads will be used to plot essentially DC currents of various magnitudes. Since internal loads are used, the Probe is not connected. """ # check if the P1125 is reachable success, result = p1125.ping() logger.info("ping: {}".format(result)) if not success: return False success, result = p1125.status() logger.info("status: {}".format(result)) if not success: return False success, result = p1125.probe(connect=False) logger.info("probe: {}".format(result)) if not success: return False success, result = p1125.calibrate() logger.info("calibrate: {}".format(result)) if not success: return False success, result = p1125.set_timebase(SPAN) logger.info("set_timebase: {}".format(result)) if not success: return False success, result = p1125.set_trigger(src=P1125API.TRIG_SRC_NONE, pos=P1125API.TRIG_POS_LEFT, slope=P1125API.TRIG_SLOPE_RISE, level=1) logger.info("set_trigger: {}".format(result)) if not success: return False for vout in VOUT: success, result = p1125.set_vout(vout) logger.info("set_vout: {}".format(result)) if not success: return False for load, resistance in LOADS_TO_PLOT: expected_i_ua = float(vout) / resistance * 1000.0 if expected_i_ua < CURRENT_MIN_UA or expected_i_ua > CURRENT_MAX_UA: logger.info( "SKIP (Current out of range): {} mV, {:0.3f} Ohms, expected {:.1f} uA" .format(vout, resistance, expected_i_ua)) continue logger.info("{} mV, {}, expected {} uA".format( vout, resistance, expected_i_ua)) success, result = p1125.set_cal_load(loads=load) logger.info("set_cal_load: {}".format(result)) if not success: break sleep(0.1) # allow load time to settle success, result = p1125.acquisition_start( mode=P1125API.ACQUIRE_MODE_SINGLE) logger.info("acquisition_start: {}".format(result)) if not success: break success, result = p1125.acquisition_complete() logger.info("acquisition_complete: {}".format(result)) if not success: break p1125.set_cal_load(loads=[P1125API.DEMO_CAL_LOAD_NONE]) p1125.acquisition_stop() _, result = p1125.acquisition_get_data() samples = len(result["i"]) data["vout"].append(vout) data["min"].append(min(result["i"])) data["max"].append(max(result["i"])) data["avg"].append(sum(result["i"]) / samples) data["exp"].append(expected_i_ua) data["err"].append( (abs(data["avg"][-1] - data["exp"][-1]) * 100.0) / data["exp"][-1]) data["res"].append(resistance) # find the peak error from the acquistion peak_error = max(abs(data["exp"][-1] - data["min"][-1]), abs(data["exp"][-1] - data["max"][-1])) data["errp"].append(peak_error * 100.0 / data["exp"][-1]) sigma = np.std(result["i"]) data["sigma"].append(sigma) sigma_as_percent = sigma * 100.0 / data["exp"][-1] data["sigma_percent"].append(sigma_as_percent) logger.info( """VOUT {} mV, Expected {:9.2f} uA, min/avg/max: {:9.2f} {:9.2f} {:9.2f} uA, sigma {:8.3f} ({:3.1}%), {} samples""" .format(data["vout"][-1], data["exp"][-1], data["min"][-1], data["avg"][-1], data["max"][-1], sigma, sigma_as_percent, samples)) # large error... #if data["errp"][-1] > PLOT_CIRCLE_ERR: logger.error(result["i"]) plot.cross(x="vout", y="avg", size=10, color="blue", source=source) plot.dot(x="vout", y="exp", size=20, color="olive", source=source) plot.dash(x="vout", y="min", size=10, color="red", source=source) plot.dash(x="vout", y="max", size=10, color="red", source=source) _tooltips_sigma = [ ("Sigma", "@sigma_percent{0.0} %"), ] dotssigma = plot_sigma.circle_dot(x="vout", y="exp", size="sigma_percent", fill_alpha=0.2, line_width=1, color="red", source=source) plot_sigma.circle(x="vout", y="exp", size=PLOT_CIRCLE_ERR, fill_alpha=0.2, line_width=0, color="green", source=source) htsigma = HoverTool(tooltips=_tooltips_sigma, mode='vline', show_arrow=True, renderers=[dotssigma]) plot_sigma.tools = [ htsigma, BoxZoomTool(), ZoomInTool(), ResetTool(), UndoTool(), PanTool() ] _tooltips_peak = [ ("Error", "@errp{0.0} %"), ] dotsp = plot_errp.circle_dot(x="vout", y="exp", size="errp", fill_alpha=0.2, color="red", source=source) plot_errp.circle(x="vout", y="exp", size=PLOT_CIRCLE_ERR, fill_alpha=0.2, line_width=0, color="green", source=source) htp = HoverTool(tooltips=_tooltips_peak, mode='vline', show_arrow=True, renderers=[dotsp]) plot_errp.tools = [ htp, BoxZoomTool(), ZoomInTool(), ResetTool(), UndoTool(), PanTool() ] doc_layout.children.append(row(plot, plot_errp, plot_sigma)) show(doc_layout) return True
def do_plot(self, simOutDir, plotOutDir, plotOutFileName, simDataFile, validationDataFile, metadata): if not os.path.isdir(simOutDir): raise Exception, "simOutDir does not currently exist as a directory" if not os.path.exists(plotOutDir): os.mkdir(plotOutDir) sim_data = cPickle.load(open(simDataFile)) constraintIsKcatOnly = sim_data.process.metabolism.constraintIsKcatOnly constrainedReactions = np.array( sim_data.process.metabolism.constrainedReactionList) # read constraint data enzymeKineticsReader = TableReader( os.path.join(simOutDir, "EnzymeKinetics")) targetFluxes = enzymeKineticsReader.readColumn("targetFluxes") actualFluxes = enzymeKineticsReader.readColumn("actualFluxes") reactionConstraint = enzymeKineticsReader.readColumn( "reactionConstraint") enzymeKineticsReader.close() initialTime = TableReader(os.path.join( simOutDir, "Main")).readAttribute("initialTime") time = TableReader(os.path.join( simOutDir, "Main")).readColumn("time") - initialTime targetAve = np.mean(targetFluxes[BURN_IN_STEPS:, :], axis=0) actualAve = np.mean(actualFluxes[BURN_IN_STEPS:, :], axis=0) relError = np.abs( (actualFluxes[BURN_IN_STEPS:, :] - targetFluxes[BURN_IN_STEPS:, :]) / (targetFluxes[BURN_IN_STEPS:, :] + 1e-15)) aveError = np.mean(relError, axis=0) kcatOnlyReactions = np.all( constraintIsKcatOnly[reactionConstraint[BURN_IN_STEPS:, :]], axis=0) kmAndKcatReactions = ~np.any( constraintIsKcatOnly[reactionConstraint[BURN_IN_STEPS:, :]], axis=0) mixedReactions = ~(kcatOnlyReactions ^ kmAndKcatReactions) kmAndKcatThresholds = [2, 10] kmAndKcatCategorization = np.zeros(np.sum(kmAndKcatReactions)) for i, threshold in enumerate(kmAndKcatThresholds): kmAndKcatCategorization[ targetAve[kmAndKcatReactions] / actualAve[kmAndKcatReactions] > threshold] = i + 1 kmAndKcatCategorization[ actualAve[kmAndKcatReactions] / targetAve[kmAndKcatReactions] > threshold] = i + 1 kmAndKcatCategorization[actualAve[kmAndKcatReactions] == 0] = -1 kcatOnlyThresholds = [2, 10] kcatOnlyCategorization = np.zeros(np.sum(kcatOnlyReactions)) for i, threshold in enumerate(kcatOnlyThresholds): kcatOnlyCategorization[ actualAve[kcatOnlyReactions] / targetAve[kcatOnlyReactions] > threshold] = i + 1 kcatOnlyCategorization[actualAve[kcatOnlyReactions] == 0] = -1 targetAve += 1e-13 actualAve += 1e-13 plt.figure(figsize=(8, 8)) targetPearson = targetAve[kmAndKcatReactions] actualPearson = actualAve[kmAndKcatReactions] # plt.title(pearsonr(np.log10(targetPearson[actualPearson > 0]), np.log10(actualPearson[actualPearson > 0]))) plt.loglog(targetAve[kcatOnlyReactions][kcatOnlyCategorization == 0], actualAve[kcatOnlyReactions][kcatOnlyCategorization == 0], "og") plt.loglog(targetAve[kcatOnlyReactions][kcatOnlyCategorization == 1], actualAve[kcatOnlyReactions][kcatOnlyCategorization == 1], "o") plt.loglog(targetAve[kcatOnlyReactions][kcatOnlyCategorization == 2], actualAve[kcatOnlyReactions][kcatOnlyCategorization == 2], "or") # plt.loglog(targetAve[kmAndKcatReactions], actualAve[kmAndKcatReactions], "o") # plt.loglog(targetAve[kcatOnlyReactions], actualAve[kcatOnlyReactions], "ro") plt.loglog([1e-12, 1], [1e-12, 1], '--g') plt.loglog([1e-12, 1], [1e-11, 10], '--r') plt.xlabel("Target Flux (dmol/L/s)") plt.ylabel("Actual Flux (dmol/L/s)") exportFigure(plt, plotOutDir, plotOutFileName) plt.close("all") return source = ColumnDataSource(data=dict( x=targetAve, y=actualAve, reactionName=constrainedReactions)) hover = HoverTool(tooltips=[ ("Reaction", "@reactionName"), ]) TOOLS = [ hover, BoxZoomTool(), LassoSelectTool(), PanTool(), WheelZoomTool(), ResizeTool(), UndoTool(), RedoTool(), "reset", ] p1 = figure( x_axis_label="Target", x_axis_type="log", x_range=[min(targetAve[targetAve > 0]), max(targetAve)], y_axis_label="Actual", y_axis_type="log", y_range=[min(actualAve[actualAve > 0]), max(actualAve)], width=800, height=800, tools=TOOLS, ) p1.scatter(targetAve, actualAve, source=source, size=8) p1.line([1e-15, 10], [1e-15, 10], line_color="red", line_dash="dashed") ## bar plot of error # sortedReactions = [constrainedReactions[x] for x in np.argsort(aveError)[::-1]] # aveError[np.log10(aveError) == -np.inf] = 0 # source = ColumnDataSource( # data = dict( # x = sorted(relError, reverse = True), # reactionName = sortedReactions # ) # ) # p2 = Bar(data, values = "x") # hover2 = p2.select(dict(type=HoverTool)) # hover2.tooltips = [("Reaction", "@reactionName")] ## flux for each reaction hover2 = HoverTool(tooltips=[ ("Reaction", "@reactionName"), ]) TOOLS2 = [ hover2, BoxZoomTool(), LassoSelectTool(), PanTool(), WheelZoomTool(), ResizeTool(), UndoTool(), RedoTool(), "reset", ] p2 = figure( x_axis_label="Time(s)", y_axis_label="Flux", y_axis_type="log", y_range=[1e-8, 1], width=800, height=800, tools=TOOLS2, ) colors = COLORS_LARGE nTimesteps = len(time[BURN_IN_STEPS:]) x = time[BURN_IN_STEPS:] y = actualFluxes[BURN_IN_STEPS:, 0] reactionName = np.repeat(constrainedReactions[0], nTimesteps) source = ColumnDataSource( data=dict(x=x, y=y, reactionName=reactionName)) p2.line(x, y, line_color=colors[0], source=source) # Plot remaining metabolites onto initialized figure for m in np.arange(1, actualFluxes.shape[1]): y = actualFluxes[BURN_IN_STEPS:, m] reactionName = np.repeat(constrainedReactions[m], nTimesteps) source = ColumnDataSource( data=dict(x=x, y=y, reactionName=reactionName)) p2.line(x, y, line_color=colors[m % len(colors)], source=source) if not os.path.exists(os.path.join(plotOutDir, "html_plots")): os.makedirs(os.path.join(plotOutDir, "html_plots")) p = bokeh.io.vplot(p1, p2) bokeh.io.output_file(os.path.join(plotOutDir, "html_plots", plotOutFileName + ".html"), title=plotOutFileName, autosave=False) bokeh.io.save(p) bokeh.io.curstate().reset()
def main(): """ An example sequence of commands to make a measurement with the P1125 REST API """ p1125 = P1125(url=URL, loggerIn=logger) # check if the P1125 is reachable success, result = p1125.ping() logger.info(result) if not success: return False success, result = p1125.status() logger.info(result) if not success: return False success, result = p1125.probe(connect=False) logger.info(result) if not success: return False success, result = p1125.calibrate() logger.info(result) if not success: return False success, result = p1125.set_vout(VOUT) logger.info(result) if not success: return False success, result = p1125.set_timebase(SPAN) logger.info(result) if not success: return False success, result = p1125.set_trigger(src=P1125API.TRIG_SRC_NONE, pos=P1125API.TRIG_POS_LEFT, slope=P1125API.TRIG_SLOPE_RISE, level=1) logger.info(result) if not success: return False # connect probe success, result = p1125.probe(connect=CONNECT_PROBE) logger.info(result) if not success: return False success, result = p1125.acquisition_start(mode=P1125API.ACQUIRE_MODE_SINGLE) logger.info(result) if not success: return False success, result = p1125.acquisition_complete() logger.info(result) if not success: return False success, result = p1125.probe(connect=False) logger.info(result) if not success: return False success, result = p1125.acquisition_get_data() #logger.info(result) # a lot of data here, uncomment to explore if not success: return False result.pop("success") # bokeh requires dict to have all fields same length, 'success' is not part of data line = plot_add(result, "MyPlot", "blue") ht = HoverTool( tooltips=[("Current", "@i{0.00} uA"), ("Time", "@t{0.00} mS")], mode='vline', # display a tooltip whenever the cursor is vertically in line with a glyph show_arrow=True, renderers=[line], ) plot.tools = [ht, BoxZoomTool(), WheelZoomTool(dimensions="width"), ResetTool(), UndoTool(), PanTool(dimensions="width")] plot_fini() return True
plot_width=1200, title='S&P 500', x_axis_label='Date', y_axis_label='Close (in USD)') p.line(sp500_df['Date'], sp500_df['Close'], color='blue', legend='') # Hover tool with vline mode hover = HoverTool( tooltips=[('Close', '$@{Close}{%0.2f}'), ('Date', '@date{%F}')], formatters={ 'date': 'datetime', # use 'datetime' formatter for 'date' field 'Close': 'printf', # use 'printf' formatter for 'adj close' field # use default 'numeral' formatter for other fields }, mode='vline') p.add_tools(hover) p.add_tools(LassoSelectTool()) p.add_tools(PolySelectTool()) p.add_tools(TapTool()) p.add_tools(UndoTool()) p.add_tools(RedoTool()) p.add_tools(ResetTool()) p.add_tools(ZoomInTool()) p.add_tools(ZoomOutTool()) p.add_tools(CrosshairTool()) html = file_html(p, CDN, "my plot") with open("./test_bokeh.html", "w") as html_file: html_file.write(html)
def make_plot(self,title, x, y): """ :param title: :param x: :param y: :return: """ print(title,x,y) t0 = time() pt = PanTool() lst = LassoSelectTool() pst = PolySelectTool() bst = BoxSelectTool() wzt = WheelZoomTool() tt = TapTool() st = SaveTool() ut = UndoTool() rt = RedoTool() p = figure( tools=[pt,lst,pst,bst,wzt,tt,st,ut,rt], plot_width=400, plot_height=400, title=self.initial_plot_2_data_mapper[x]+" vs. "+self.initial_plot_2_data_mapper[y], webgl=accelerator) # configure so that no drag tools are active p.toolbar.active_drag = pt # configure so that Bokeh chooses what (if any) scroll tool is active p.toolbar.active_scroll = wzt # configure so that a specific PolySelect tap tool is active p.toolbar.active_tap = tt p.xaxis.axis_label = self.initial_plot_2_data_mapper[x] p.yaxis.axis_label = self.initial_plot_2_data_mapper[y] c = p.circle(x=x, y=y, size=5, color="__COLOR__", alpha=.75, source=self.source, hover_color='white', hover_alpha=1, hover_line_color='grey') c.data_source.on_change('selected', self.update) # Edge generator ''' self.graph_set = [{i: [[1,0.15],[2,0.5],[3,0.99]] for i in range(self.n)}] self.edge_colors = qual_2_color(['g'+str(i) for i,_ in enumerate(self.graph_set)]) self.edge_sources = [ColumnDataSource({'x0': [], 'y0': [], 'x1': [], 'y1': [], 'alpha': []}) for i in self.graph_set] self.edge_segments = [p.segment(x0='x0', y0='y0', x1='x1', y1='y1', color=self.edge_colors[i], alpha='alpha', line_width=3, #line_dash=[1,1], source=self.edge_sources[i]) for i, _ in enumerate(self.graph_set)] for i, _ in enumerate(self.graph_set): code1 = """ var links = %s; var data = {'x0': [], 'y0': [], 'x1': [], 'y1': [], 'alpha': []}; var cdata = circle.get('data'); var indices = cb_data.index['1d'].indices; for (i=0; i < indices.length; i++) { ind0 = indices[i] for (j=0; j < links[ind0].length; j++) { ind1 = links[ind0][j][0]; w = links[ind0][j][1]; """ % self.graph_set[i] code2 = "data['x0'].push(cdata['" + x + "'][ind0]);\n" + \ "data['y0'].push(cdata['" + y + "'][ind0]);\n" + \ "data['x1'].push(cdata['" + x + "'][ind1]);\n" + \ "data['y1'].push(cdata['" + y + "'][ind1]);\n" + \ "data['alpha'].push([w]);\n" code3 = "}}segment.set('data', data);" code = code1 + code2 + code3 callback = CustomJS(args={'circle': c.data_source, 'segment': self.edge_segments[i].data_source}, code=code) p.add_tools(HoverTool(tooltips=None, callback=callback, renderers=[c])) ''' p.select(BoxSelectTool).select_every_mousemove = False p.select(LassoSelectTool).select_every_mousemove = False # Plot Controls xdim_select = Select(title="X Dim", options=self.data_dict.keys(), value=self.initial_plot_2_data_mapper[x],width=400) ydim_select = Select(title="Y Dim", options=self.data_dict.keys(), value=self.initial_plot_2_data_mapper[y],width=400) xdim_select.on_change('value', self.plot_update) ydim_select.on_change('value', self.plot_update) remove = Button(label="Remove", button_type="danger",width=400) remove.on_click(partial(self.remove_plot,title,x,y)) self.plot_control_dict[title] = {'x':xdim_select, 'y':ydim_select, 'xprev':xdim_select.value, 'yprev':ydim_select.value, 'figure':p, 'tooltip':HoverTool(tooltips=self.tooltip_list,point_policy='snap_to_data',show_arrow=False)} # Give the hover tool a tool tip self.plot_control_dict[title]['figure'].add_tools(self.plot_control_dict[title]['tooltip']) # Form Tab plot_options = WidgetBox(xdim_select,ydim_select,remove) tab1 = Panel(child=self.plot_control_dict[title]['figure'], title=title,width=400,height=400) tab2 = Panel(child=plot_options, title="options",width=400,height=400) tabs = Tabs(tabs=[tab1, tab2],width=400,height=400) self.tab_list.append(tabs) self.circle_list.append(c) print('Plot Time: ' + str(time() - t0)) return tabs, c
def metric_rank_bar_graph(exoplanet_array, tooltip_dict=dict()): '''Funtion to generate html for an interactive bokeh bar graph, specifically for metric score vs rank''' # append to tooltip arrays for planet in exoplanet_array: for prop, vals in tooltip_dict.items(): vals.append(rgetattr(planet, prop)) # axis scale x_axis_type = 'linear' y_axis_type = 'log' # create tooltip table tooltips = [('(rank, metric)', '(@rank, @decision_metric)')] for prop in tooltip_dict.keys(): tooltips.append((prop, '@%s' % prop)) # create plot figure plot = figure( x_axis_type=x_axis_type, y_axis_type=y_axis_type, plot_height=400, sizing_mode='stretch_width', tools=[ PanTool(), BoxZoomTool(), WheelZoomTool(), TapTool(), UndoTool(), RedoTool(), CrosshairTool(), HoverTool(mode='vline'), SaveTool(), ResetTool() ], tooltips=tooltips, background_fill_alpha=0.7, border_fill_alpha=0.7, ) # link points to exoplanet.eu url = 'http://www.exoplanet.eu/catalog/@name' taptool = plot.select(type=TapTool) taptool.callback = OpenURL(url=url.replace(' ', '_')) # label axes plot.xaxis.axis_label = 'Rank' plot.yaxis.axis_label = 'Decision Metric' fontsize = '12pt' plot.xaxis.axis_label_text_font_size = fontsize plot.yaxis.axis_label_text_font_size = fontsize # make data source source_dict = dict( rank=[i for i in range(1, len(exoplanet_array) + 1)], decision_metric=[planet.decision_metric for planet in exoplanet_array], ) source_dict.update(tooltip_dict) # add tooltip info to data source source = ColumnDataSource(data=source_dict) # make colormap detection_methods = [ 'Primary Transit', 'Radial Velocity', 'Imaging', 'Microlensing', 'Timing', 'Astrometry', 'TTV', 'Default', 'Primary Transit, TTV', 'Controversial', ] cmap = factor_cmap('detection_type', palette=Category10[10], factors=detection_methods) # plot graph plot.vbar( x='rank', top='decision_metric', color=cmap, bottom=1, # so that log scale can work, width=0.9, legend_field='detection_type', source=source) # legend plot.legend.title = 'Detection method' plot.legend.location = 'top_right' plot.legend.background_fill_alpha = 0 return file_html(gridplot([[plot]], sizing_mode='stretch_both'), CDN)
def linked_scatter_graph(master_source_dict, xproperties, yproperties, cproperties, sproperties, xlogs, ylogs, clogs, slogs, errorbar_xs, errorbar_ys, x_hists, y_hists, tooltip_list): '''Funtion to generate html for an interactive bokeh scatter/errorbar graph''' # copy source_dict to avoid overmapping source_dict = copy.deepcopy(master_source_dict) # size mapping (do before making the data source) for i, (sproperty, slog) in enumerate(zip(sproperties, slogs)): if sproperty == 'None': source_dict.update({'smap%i' % i: [4] * len(source_dict['name']) }) # size 4 is default else: arr = source_dict[sproperty] if slog: arr = np.log(arr) source_dict.update({ 'smap%i' % i: np.nan_to_num( np.multiply(np.subtract(arr, np.nanmin(arr)), 15 / np.nanmax(arr))).tolist() }) # make data source source = ColumnDataSource(data=source_dict) plots = [] for i in range(len(xproperties)): # don't use i for loops inside this xproperty, yproperty, cproperty, sproperty, xlog, ylog, clog, slog, errorbar_x, errorbar_y, x_hist, y_hist = xproperties[ i], yproperties[i], cproperties[i], sproperties[i], xlogs[ i], ylogs[i], clogs[i], slogs[i], errorbar_xs[i], errorbar_ys[ i], x_hists[i], y_hists[i] if xproperty != 'None' and yproperty != 'None': # units units = [''] * 4 properties = [xproperty, yproperty, cproperty, sproperty] for j, prop in enumerate( properties): # must use enumerate, not zip if prop != 'None': if 'host.' in prop: units[j] = Star.property_dict[prop.replace( 'host.', '')] else: units[j] = Exoplanet.property_dict[prop] (xunit, yunit, cunit, sunit) = (unit for unit in units) formatters = [''] * 4 # for datetime formatting in tooltips for j, unit in enumerate(units): if unit == 'date': formatters[j] = '{%F}' if unit == 'year': formatters[j] = '{%Y}' # axis scale if xlog: x_axis_type = 'log' else: x_axis_type = 'linear' if ylog: y_axis_type = 'log' else: y_axis_type = 'linear' if xunit == 'date' or xunit == 'year': x_axis_type = 'datetime' if yunit == 'date' or yunit == 'year': y_axis_type = 'datetime' # create tooltip table tooltips = [ ('(%s, %s)' % (xproperty.replace('.', '_'), yproperty.replace('.', '_')), '(@{%s}%s, @{%s}%s)' % (xproperty, formatters[0], yproperty, formatters[1])), ('(%s, %s)' % (cproperty.replace('.', '_'), sproperty.replace('.', '_')), '(@{%s}%s, @{%s}%s)' % (cproperty, formatters[2], sproperty, formatters[3])), ] for prop in tooltip_list: tooltips.append((prop, '@%s' % prop)) # create plot figure plot = figure( x_axis_type=x_axis_type, y_axis_type=y_axis_type, sizing_mode='stretch_both', tools=[ PanTool(), BoxZoomTool(), BoxSelectTool(), LassoSelectTool(), WheelZoomTool(), TapTool(), UndoTool(), RedoTool(), CrosshairTool(), SaveTool(), ResetTool(), HoverTool(tooltips=tooltips, formatters={ '@{discovered}': 'datetime', '@{updated}': 'datetime' }) ], toolbar_location='above', # title=str(datetime.date.today()), background_fill_alpha=0.7, border_fill_alpha=0.7, ) # invert scales for magnitudes if 'mag_' in xproperty: plot.x_range.flipped = True if 'mag_' in yproperty: plot.y_range.flipped = True # link points to exoplanet.eu url = 'http://www.exoplanet.eu/catalog/@name' taptool = plot.select(type=TapTool) taptool.callback = OpenURL(url=url.replace(' ', '_')) # label axes plot.xaxis.axis_label = xproperty.replace('_', ' ').replace( '.', ' ').capitalize() + ' (%s)' % xunit plot.yaxis.axis_label = yproperty.replace('_', ' ').replace( '.', ' ').capitalize() + ' (%s)' % yunit # font sizes fontsize = '12pt' plot.xaxis.axis_label_text_font_size = fontsize plot.yaxis.axis_label_text_font_size = fontsize # color mapping if cproperty == 'None': cmap = Category10[10][0] elif cproperty == 'detection_type': # detection type is categorical so needs special treatment detection_methods = [ 'Primary Transit', 'Radial Velocity', 'Imaging', 'Microlensing', 'Timing', 'Astrometry', 'TTV', 'Default', 'Primary Transit, TTV', 'Controversial' ] cmap = factor_cmap('detection_type', palette=Category10[10], factors=detection_methods) # counts counts = {method: 0 for method in detection_methods} for j, method in enumerate(source_dict['detection_type']): if np.isfinite(source_dict[xproperty][j]) and np.isfinite( source_dict[yproperty][j]): counts[method] += 1 # need to create legend manually so we can place it outside the plot area # legend placement depends on whether histograms are present if y_hist: blank = figure( width_policy='min', height_policy='min', width=205, height=290, outline_line_color=None, background_fill_alpha=0.7, border_fill_alpha=0.7, ) legend = categorical_legend(detection_methods, counts.values(), '', Category10[10], fig=blank) else: legend = categorical_legend(detection_methods, counts.values(), '', Category10[10], fig=plot) plot.add_layout(legend, 'right') else: if clog: cmap = log_cmap(cproperty, palette=Turbo256, low=np.nanmin(source_dict[cproperty]), high=np.nanmax(source_dict[cproperty])) color_bar = ColorBar(color_mapper=cmap['transform'], ticker=LogTicker(), width=8, location=(0, 0), background_fill_alpha=0) else: cmap = linear_cmap(cproperty, palette=Turbo256, low=np.nanmin(source_dict[cproperty]), high=np.nanmax(source_dict[cproperty])) color_bar = ColorBar(color_mapper=cmap['transform'], width=8, location=(0, 0), background_fill_alpha=0) plot.add_layout(color_bar, 'right') # bokeh does not have a native colorbar label item so we will use a plot title as a substitute plot.title.text = cproperty.replace('_', ' ').replace( '.', ' ').capitalize() + ' (%s)' % cunit plot.title_location = 'right' plot.title.align = 'center' plot.title.text_font_size = fontsize plot.title.text_font_style = 'italic' # plot graph plot.scatter( xproperty, yproperty, color=cmap, size='smap%i' % i, source=source, ) # errorbars if xproperty + '_error_min' in Exoplanet.property_dict or xproperty.replace( 'host.', '') + '_error_min' in Star.property_dict: wx = Whisker( source=source, dimension='width', visible=errorbar_x, base=yproperty, upper=xproperty + '_error_max', lower=xproperty + '_error_min', line_color=cmap, upper_head=None, lower_head=None, ) plot.add_layout(wx) if yproperty + '_error_min' in Exoplanet.property_dict or yproperty.replace( 'host.', '') + '_error_min' in Star.property_dict: wy = Whisker( source=source, dimension='height', visible=errorbar_y, base=xproperty, upper=yproperty + '_error_max', lower=yproperty + '_error_min', line_color=cmap, upper_head=None, lower_head=None, ) plot.add_layout(wy) # histograms full_colour = '#756bb1' sample_colour = '#74c476' if x_hist: # list setup full_list = source_dict[xproperty] short_list = [ xval for xval, yval in zip(source_dict[xproperty], source_dict[yproperty]) if not np.isnan(yval) ] min_ = np.nanmin(full_list) max_ = np.nanmax(full_list) # plot setup hxplot = figure( x_range=plot.x_range, x_axis_type=x_axis_type, height=300, sizing_mode='stretch_width', background_fill_alpha=0.7, border_fill_alpha=0.7, ) hxplot.xaxis.axis_label = xproperty.replace('_', ' ').replace( '.', ' ').capitalize() + ' (%s)' % xunit hxplot.yaxis.axis_label = 'Frequency' hxplot.xaxis.axis_label_text_font_size = fontsize hxplot.yaxis.axis_label_text_font_size = fontsize # bins if xlog: bins = np.logspace(np.log10(min_), np.log10(max_), 100) else: bins = np.linspace(min_, max_, 100) # full range histogram hist, edges = np.histogram(full_list, bins=bins) hxplot.quad(top=hist, bottom=0, left=edges[:-1], right=edges[1:], color=full_colour, fill_alpha=0.5, legend_label='All') # only plotted histogram hist, edges = np.histogram(short_list, bins=bins) hxplot.quad(top=hist, bottom=0, left=edges[:-1], right=edges[1:], color=sample_colour, fill_alpha=0.5, legend_label='Shown') # legend setup hxplot.legend.click_policy = 'hide' hxplot.legend.background_fill_alpha = 0 if y_hist: # list setup full_list = source_dict[yproperty] short_list = [ yval for xval, yval in zip(source_dict[xproperty], source_dict[yproperty]) if not np.isnan(xval) ] min_ = np.nanmin(full_list) max_ = np.nanmax(full_list) # plot setup hyplot = figure( y_range=plot.y_range, y_axis_type=y_axis_type, width=300, sizing_mode='stretch_height', background_fill_alpha=0.7, border_fill_alpha=0.7, ) hyplot.yaxis.axis_label = yproperty.replace('_', ' ').replace( '.', ' ').capitalize() + ' (%s)' % yunit hyplot.xaxis.axis_label = 'Frequency' hyplot.xaxis.axis_label_text_font_size = fontsize hyplot.yaxis.axis_label_text_font_size = fontsize # bins if xlog: bins = np.logspace(np.log10(min_), np.log10(max_), 100) else: bins = np.linspace(min_, max_, 100) # full range histogram hist, edges = np.histogram(full_list, bins=bins) hyplot.quad(right=hist, left=0, top=edges[:-1], bottom=edges[1:], color=full_colour, fill_alpha=0.5, legend_label='All') # only plotted histogram hist, edges = np.histogram(short_list, bins=bins) hyplot.quad(right=hist, left=0, top=edges[:-1], bottom=edges[1:], color=sample_colour, fill_alpha=0.5, legend_label='Shown') # legend setup hyplot.legend.click_policy = 'hide' hyplot.legend.background_fill_alpha = 0 # layouts if cproperty != 'detection_type': blank = None # don't need this plot if no legend is present if x_hist and y_hist: layout = gridplot([[hxplot, blank], [plot, hyplot]], sizing_mode='stretch_both') elif x_hist: layout = gridplot([[hxplot], [plot]], sizing_mode='stretch_both') elif y_hist: layout = gridplot([[plot, hyplot, blank]], sizing_mode='stretch_both') else: layout = gridplot([[plot]], sizing_mode='stretch_both') plots.append(layout) # layout layout = column(row(plots), sizing_mode='stretch_both') return file_html(layout, CDN)
plot_list = [] hover = HoverTool( tooltips=[('Service', '@service_name'), ( 'Check Number', '@check_num'), ('Total Points', '@total_points'), ('Check Time', '@str_datetimes')]) TOOLS = [ PanTool(), WheelZoomTool(), BoxZoomTool(), ZoomInTool(), ZoomOutTool(), UndoTool(), hover, ResetTool(), SaveTool() ] all_services_plot = figure( title="{} - {}".format(team, service), x_axis_label='Time', y_axis_label='{} Service Points'.format(service), x_axis_type="datetime", x_range=Range1d(x_start, x_end), width=GRAPH_WIDTH, height=GRAPH_HEIGHT, tools=TOOLS) for service in by_team[team]:
hover_pie = HoverTool(tooltips=[('Percentage', '@ratio')]) select_day = Select(options=[ 'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday' ], value='Monday', title='Day of the Week') chosen_tools = [ hover, ResetTool(), BoxSelectTool(), SaveTool(), PanTool(), UndoTool(), RedoTool(), ZoomInTool(), ZoomOutTool() ] #data data = pd.read_csv(r'~/Desktop/yelp/yelp_business.csv', index_col='business_id') hour = pd.read_csv(r'~/Desktop/yelp/yelp_business_hours.csv', index_col='business_id') whole = pd.merge(data, hour, left_index=True, right_index=True) #data = pd.read_csv(r'E:\Software\WinPython\notebooks\yelp\yelp_business.csv',index_col='business_id') state_data = whole[whole.state == 'NV']
def plot_graph( G_true: nx.Graph, G_test: nx.Graph, outfile: Path, height: int, width: int, name: str, truth_threshold: int, query_threshold: int, bg_alpha: float = 0.2, ) -> None: title = f"SNP threshold (Ill./NP) = {truth_threshold}/{query_threshold}" # inline effectively allows the plot to work offline output_file(str(outfile), title=title, mode="inline") node_attrs = {} clusters = [c for c in nx.connected_components(G_true)] tpr_vals = [] ppv_vals = [] for cluster in clusters: for node in cluster: test_cluster = connected_components(G_test, node) tpr = set_recall(cluster, test_cluster) node_attrs[node] = cmap(tpr) nx.set_node_attributes(G_true, node_attrs, "node_colour") for cluster in clusters: for node in cluster: test_cluster = connected_components(G_test, node) ppv = set_precision(cluster, test_cluster) node_attrs[node] = cmap(ppv) nx.set_node_attributes(G_true, node_attrs, "line_colour") for cluster in clusters: for node in cluster: test_cluster = connected_components(G_test, node) tpr = set_recall(cluster, test_cluster) node_attrs[node] = tpr tpr_vals.append(tpr) nx.set_node_attributes(G_true, node_attrs, "tpr") for cluster in clusters: for node in cluster: test_cluster = connected_components(G_test, node) ppv = set_precision(cluster, test_cluster) node_attrs[node] = ppv ppv_vals.append(ppv) nx.set_node_attributes(G_true, node_attrs, "ppv") logging.info(f"Average Recall: {np.mean(tpr_vals):.4f}") logging.info(f"Average Precision: {np.mean(ppv_vals):.4f}") pos = nx.nx_agraph.graphviz_layout(G_true, prog="neato") xmin = -5 xmax = max((x for (x, _) in pos.values())) * 1.05 xrange = Range1d(xmin, xmax) ymin = -20 ymax = max((y for (_, y) in pos.values())) * 1.05 yrange = Range1d(ymin, ymax) p1 = Plot( plot_width=width, plot_height=height, x_range=xrange, y_range=yrange, background_fill_color="gray", background_fill_alpha=bg_alpha, ) p1.title.align = "center" p1.title.text = title graph_renderer = from_networkx(G_true, pos) graph_renderer.node_renderer.glyph = Circle(size=40, fill_color="node_colour", line_color="line_colour", line_width=12) graph_renderer.edge_renderer.glyph = MultiLine( line_width=4, line_alpha=0.5, ) p1.renderers.append(graph_renderer) xs = np.linspace(start=0, stop=1, num=256) colors = [cmap(x) for x in xs] cmapper = LinearColorMapper(palette=colors, low=0, high=1) color_bar = ColorBar( color_mapper=cmapper, major_label_text_font_size="12px", ticker=BasicTicker(desired_num_ticks=20), label_standoff=2, major_label_text_baseline="middle", major_label_text_align="left", border_line_color=None, location=(0, 0), title="", title_text_align="center", title_standoff=10, major_label_text_font_style="bold", major_tick_line_color="black", ) p1.add_layout(color_bar, "right") node_hover_tool = HoverTool(tooltips=[("sample", "@index"), ("TPR", "@tpr"), ("PPV", "@ppv")], ) p1.add_tools( node_hover_tool, BoxZoomTool(), ResetTool(), PanTool(), WheelZoomTool(), UndoTool(), SaveTool(), ) labels = [] x_vals = [] y_vals = [] for lab, (x, y) in pos.items(): labels.append(lab) x_vals.append(x) y_vals.append(y) d = {"labels": labels, "x_values": x_vals, "y_values": y_vals} src = ColumnDataSource(d) label_set = LabelSet( source=src, x="x_values", y="y_values", text="labels", y_offset=-5, text_align="center", text_font_size="10px", text_font_style="bold", text_color="black", text_font="monospace", ) p1.add_layout(label_set) node_attrs = {} clusters = [c for c in nx.connected_components(G_true)] for cluster in clusters: cluster_tprs = [] for node in cluster: test_cluster = connected_components(G_test, node) tpr = set_recall(cluster, test_cluster) cluster_tprs.append(tpr) acr = np.mean(cluster_tprs) for node in cluster: node_attrs[node] = cmap(acr) nx.set_node_attributes(G_true, node_attrs, "node_colour") for cluster in clusters: cluster_ppvs = [] for node in cluster: test_cluster = connected_components(G_test, node) ppv = round(set_precision(cluster, test_cluster), 2) cluster_ppvs.append(ppv) acp = np.mean(cluster_ppvs) for node in cluster: node_attrs[node] = cmap(acp) nx.set_node_attributes(G_true, node_attrs, "line_colour") for cluster in clusters: cluster_tprs = [] for node in cluster: test_cluster = connected_components(G_test, node) tpr = set_recall(cluster, test_cluster) cluster_tprs.append(tpr) acr = np.mean(cluster_tprs) for node in cluster: node_attrs[node] = acr nx.set_node_attributes(G_true, node_attrs, "tpr") for cluster in clusters: cluster_ppvs = [] for node in cluster: test_cluster = connected_components(G_test, node) ppv = set_precision(cluster, test_cluster) cluster_ppvs.append(ppv) acp = np.mean(cluster_ppvs) for node in cluster: node_attrs[node] = acp nx.set_node_attributes(G_true, node_attrs, "ppv") pos = nx.nx_agraph.graphviz_layout(G_true, prog="neato") xmin = -5 xmax = max((x for (x, _) in pos.values())) * 1.05 xrange = Range1d(xmin, xmax) ymin = -20 ymax = max((y for (_, y) in pos.values())) * 1.05 yrange = Range1d(ymin, ymax) p2 = Plot( plot_width=width, plot_height=height, x_range=xrange, y_range=yrange, background_fill_color="gray", background_fill_alpha=bg_alpha, ) p2.title.align = "center" p2.title.text = title graph_renderer = from_networkx(G_true, pos) graph_renderer.node_renderer.glyph = Circle(size=40, fill_color="node_colour", line_color="line_colour", line_width=12) graph_renderer.edge_renderer.glyph = MultiLine( line_width=4, line_alpha=0.5, ) p2.renderers.append(graph_renderer) color_bar = ColorBar( color_mapper=cmapper, major_label_text_font_size="12px", ticker=BasicTicker(desired_num_ticks=20), label_standoff=2, major_label_text_baseline="middle", major_label_text_align="left", border_line_color=None, location=(0, 0), title="", title_text_align="center", title_standoff=10, major_label_text_font_style="bold", major_tick_line_color="black", ) p2.add_layout(color_bar, "right") node_hover_tool = HoverTool(tooltips=[("sample", "@index"), ("TPR", "@tpr"), ("PPV", "@ppv")], ) p2.add_tools( node_hover_tool, BoxZoomTool(), ResetTool(), PanTool(), WheelZoomTool(), UndoTool(), SaveTool(), ) labels = [] x_vals = [] y_vals = [] for lab, (x, y) in pos.items(): labels.append(lab) x_vals.append(x) y_vals.append(y) d = {"labels": labels, "x_values": x_vals, "y_values": y_vals} src = ColumnDataSource(d) label_set = LabelSet( source=src, x="x_values", y="y_values", text="labels", y_offset=-5, text_align="center", text_font_size="10px", text_font_style="bold", text_color="black", text_font="monospace", ) p2.add_layout(label_set) for cluster in clusters: cluster_tprs = [] cluster_ppvs = [] for node in cluster: test_cluster = connected_components(G_test, node) tpr = set_recall(cluster, test_cluster) cluster_tprs.append(tpr) ppv = set_precision(cluster, test_cluster) cluster_ppvs.append(ppv) acr = np.mean(cluster_tprs) acp = np.mean(cluster_ppvs) logging.info(f"Cluster size: {len(cluster)}") logging.info(f"Members: {cluster}") logging.info(f"Average Cluster Recall: {acr}") logging.info(f"Average Cluster Precision: {acp}") logging.info("---------------------------------") save(column(p1, p2))
plot.on_event(DoubleTap, cb_plot) # this plot for the realtime data, if available plot_rt = figure(toolbar_location="above", y_range=(PLOT_MIN, PLOT_MAX), y_axis_type="log", width=800) source_rt = ColumnDataSource(data=dict(x=[], y=[])) l = plot_rt.line(x="t", y="i", line_width=2, source=source_rt, legend_label="Current (uA)") plot_rt.line(x="t", y="i_max", line_width=2, source=source_rt, legend_label="Peak Current (uA)", color="red") plot_rt.xaxis.axis_label = "Time (S)" ht = HoverTool( tooltips=[("Current", "@i{0.00} uA"), ("Time", "@t{0.00} S")], mode='vline', # display a tooltip whenever the cursor is vertically in line with a glyph show_arrow=True, renderers=[l], ) plot_rt.tools = [ht, BoxZoomTool(), WheelZoomTool(dimensions="width"), ResetTool(), UndoTool(), PanTool(dimensions="width")] doc_layout = curdoc() def extract_data(d, keys): """ Extract key for plotting - create a dict of 't' (time) and 'y' for plotting - keys can be one of 'mAhr' or 'iavg_max_ua', or others, it depends on what is available :param keys: list of keys to extract :param d: always p1125_log.p1125_data :return: None on error, dict on success """ data = {"t": []}
zoom=13) plot = GMapPlot(x_range=DataRange1d(), y_range=DataRange1d(), map_options=map_options, title="Washington, DC", plot_width=1280, plot_height=1280, responsive=True) # Finally plot it lines = MultiLine(xs="lons", ys="lats", line_alpha="line_alpha", line_width="line_width", line_color="red", line_cap="round") circle = Circle(x="lon", y="lat", size=10, fill_color="blue", fill_alpha=0.8, line_color=None) plot.add_glyph(source, circle) plot.add_glyph(line_source, lines) plot.add_tools(PanTool(), WheelZoomTool(), BoxZoomTool(), hover, ResetTool(), UndoTool(), RedoTool(), PreviewSaveTool()) output_file("gmap_plot.html") show(plot)
def do_plot(self, inputDir, plotOutDir, plotOutFileName, simDataFile, validationDataFile, metadata): if not os.path.isdir(inputDir): raise Exception, "inputDir does not currently exist as a directory" if not os.path.exists(plotOutDir): os.mkdir(plotOutDir) ap = AnalysisPaths(inputDir, variant_plot=True) variants = sorted(ap._path_data['variant'].tolist() ) # Sorry for accessing private data variant = variants[0] sim_data = cPickle.load(open(ap.get_variant_kb(variant), "rb")) targetToFC = {} targetToFCTF = {} for tf in sim_data.tfToActiveInactiveConds: for target in sim_data.tfToFC[tf]: if target not in targetToFC: targetToFC[target] = [] targetToFCTF[target] = [] targetToFC[target].append(np.log2(sim_data.tfToFC[tf][target])) targetToFCTF[target].append(tf) for target in targetToFC: targetToFC[target] = np.array(targetToFC[target]) targets = sorted(targetToFC) x = [] y = [] maxVals = [] tfs = [] targetIds = [] for idx, target in enumerate(targets): for FC, tf in zip(targetToFC[target], targetToFCTF[target]): x.append(idx) y.append(FC) if targetToFC[target].max() >= -1. * targetToFC[target].min(): maxVals.append(targetToFC[target].max()) else: maxVals.append(targetToFC[target].min()) tfs.append(tf) targetIds.append(target) conditions = [ sim_data.conditions[tf + "__active"]["nutrients"] for tf in tfs ] x = np.array(x) y = np.array(y) maxVals = np.array(maxVals) sortedIdxs = np.argsort(maxVals) conditions = [conditions[i] for i in sortedIdxs] tfs = [tfs[i] for i in sortedIdxs] targetIds = [targetIds[i] for i in sortedIdxs] fig = plt.figure(figsize=(11, 8.5)) ax = plt.subplot(1, 1, 1) ax.plot(x, y[sortedIdxs], ".") xlabel = "Gene targets (sorted)" ylabel = "log2 (Target expression fold change)" ax.set_xlabel(xlabel) ax.set_ylabel(ylabel) exportFigure(plt, plotOutDir, plotOutFileName, metadata) plt.close("all") source = ColumnDataSource(data=dict(x=x, y=y[sortedIdxs], targetId=targetIds, tfId=tfs, condition=conditions)) hover = HoverTool( tooltips=[("target", "@targetId"), ("TF", "@tfId"), ("condition", "@condition")]) tools = [ hover, BoxZoomTool(), LassoSelectTool(), PanTool(), WheelZoomTool(), ResizeTool(), UndoTool(), RedoTool(), "reset" ] plot = figure(x_axis_label=xlabel, y_axis_label=ylabel, width=800, height=500, tools=tools) plot.scatter("x", "y", source=source) 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(plot) bokeh.io.curstate().reset()
def do_plot(self, simOutDir, plotOutDir, plotOutFileName, simDataFile, validationDataFile, metadata): if not os.path.isdir(simOutDir): raise Exception, "simOutDir does not currently exist as a directory" if not os.path.exists(plotOutDir): os.mkdir(plotOutDir) sim_data = cPickle.load(open(simDataFile)) constraintIsKcatOnly = sim_data.process.metabolism.constraintIsKcatOnly mainListener = TableReader(os.path.join(simOutDir, "Main")) initialTime = mainListener.readAttribute("initialTime") time = mainListener.readColumn("time") - initialTime mainListener.close() massListener = TableReader(os.path.join(simOutDir, "Mass")) cellMass = massListener.readColumn("cellMass") dryMass = massListener.readColumn("dryMass") massListener.close() coefficient = dryMass / cellMass * sim_data.constants.cellDensity.asNumber(MASS_UNITS / VOLUME_UNITS) # read constraint data enzymeKineticsReader = TableReader(os.path.join(simOutDir, "EnzymeKinetics")) targetFluxes = (COUNTS_UNITS / MASS_UNITS / TIME_UNITS) * (enzymeKineticsReader.readColumn("targetFluxes").T / coefficient).T actualFluxes = (COUNTS_UNITS / MASS_UNITS / TIME_UNITS) * (enzymeKineticsReader.readColumn("actualFluxes").T / coefficient).T reactionConstraint = enzymeKineticsReader.readColumn("reactionConstraint") constrainedReactions = np.array(enzymeKineticsReader.readAttribute("constrainedReactions")) enzymeKineticsReader.close() targetFluxes = targetFluxes.asNumber(units.mmol / units.g / units.h) actualFluxes = actualFluxes.asNumber(units.mmol / units.g / units.h) targetAve = np.mean(targetFluxes[BURN_IN_STEPS:, :], axis = 0) actualAve = np.mean(actualFluxes[BURN_IN_STEPS:, :], axis = 0) kcatOnlyReactions = np.all(constraintIsKcatOnly[reactionConstraint[BURN_IN_STEPS:,:]], axis = 0) kmAndKcatReactions = ~np.any(constraintIsKcatOnly[reactionConstraint[BURN_IN_STEPS:,:]], axis = 0) mixedReactions = ~(kcatOnlyReactions ^ kmAndKcatReactions) thresholds = [2, 10] categorization = np.zeros(reactionConstraint.shape[1]) categorization[actualAve == 0] = -2 categorization[actualAve == targetAve] = -1 for i, threshold in enumerate(thresholds): # categorization[targetAve / actualAve > threshold] = i + 1 categorization[actualAve / targetAve > threshold] = i + 1 # url for ecocyc to highlight fluxes that are 0 on metabolic network diagram siteStr = "https://ecocyc.org/overviewsWeb/celOv.shtml?zoomlevel=1&orgid=ECOLI" excluded = ['RXN0-2201', 'RXN-16000', 'RXN-12583', 'RXN-11496', 'DIMESULFREDUCT-RXN', '3.6.1.41-R[4/63051]5-NUCLEOTID-RXN'] # reactions not recognized by ecocyc rxns = [] for i, reaction in enumerate(constrainedReactions): if actualAve[i] == 0: rxn = re.findall(".+RXN", reaction) if len(rxn) == 0: rxn = re.findall("RXN[^-]*-[0-9]+", reaction) if rxn[0] not in excluded: siteStr += "&rnids=%s" % rxn[0] rxns.append(rxn[0]) # print siteStr csvFile = open(os.path.join(plotOutDir, plotOutFileName + ".tsv"), "wb") output = csv.writer(csvFile, delimiter = "\t") output.writerow(["ecocyc link:", siteStr]) output.writerow(["Km and kcat", "Target", "Actual", "Category"]) for reaction, target, flux, category in zip(constrainedReactions[kmAndKcatReactions], targetAve[kmAndKcatReactions], actualAve[kmAndKcatReactions], categorization[kmAndKcatReactions]): output.writerow([reaction, target, flux, category]) output.writerow(["kcat only"]) for reaction, target, flux, category in zip(constrainedReactions[kcatOnlyReactions], targetAve[kcatOnlyReactions], actualAve[kcatOnlyReactions], categorization[kcatOnlyReactions]): output.writerow([reaction, target, flux, category]) if np.sum(mixedReactions): output.writerow(["mixed constraints"]) for reaction, target, flux, category in zip(constrainedReactions[mixedReactions], targetAve[mixedReactions], actualAve[mixedReactions], categorization[mixedReactions]): output.writerow([reaction, target, flux, category]) csvFile.close() targetAve += 1e-6 actualAve += 1e-6 axes_limits = [1e-7, 1e4] plt.figure(figsize = (8, 8)) ax = plt.axes() plt.loglog(axes_limits, axes_limits, 'k') plt.loglog(targetAve, actualAve, "ob", markeredgewidth = 0.25, alpha = 0.25) plt.xlabel("Target Flux (mmol/g/hr)") plt.ylabel("Actual Flux (mmol/g/hr)") plt.minorticks_off() whitePadSparklineAxis(ax) ax.set_ylim(axes_limits) ax.set_xlim(axes_limits) ax.set_yticks(axes_limits) ax.set_xticks(axes_limits) exportFigure(plt, plotOutDir, plotOutFileName) plt.close("all") source = ColumnDataSource( data = dict( x = targetAve, y = actualAve, reactionName = constrainedReactions) ) hover = HoverTool( tooltips = [ ("Reaction", "@reactionName"), ] ) TOOLS = [hover, BoxZoomTool(), LassoSelectTool(), PanTool(), WheelZoomTool(), ResizeTool(), UndoTool(), RedoTool(), "reset", ] p1 = figure(x_axis_label = "Target", x_axis_type = "log", x_range = [min(targetAve[targetAve > 0]), max(targetAve)], y_axis_label = "Actual", y_axis_type = "log", y_range = [min(actualAve[actualAve > 0]), max(actualAve)], width = 800, height = 800, tools = TOOLS, ) p1.scatter(targetAve, actualAve, source = source, size = 8) p1.line([1e-15, 10], [1e-15, 10], line_color = "red", line_dash = "dashed") ## bar plot of error # sortedReactions = [constrainedReactions[x] for x in np.argsort(aveError)[::-1]] # aveError[np.log10(aveError) == -np.inf] = 0 # source = ColumnDataSource( # data = dict( # x = sorted(relError, reverse = True), # reactionName = sortedReactions # ) # ) # p2 = Bar(data, values = "x") # hover2 = p2.select(dict(type=HoverTool)) # hover2.tooltips = [("Reaction", "@reactionName")] ## flux for each reaction hover2 = HoverTool( tooltips = [ ("Reaction", "@reactionName"), ] ) TOOLS2 = [hover2, BoxZoomTool(), LassoSelectTool(), PanTool(), WheelZoomTool(), ResizeTool(), UndoTool(), RedoTool(), "reset", ] p2 = figure(x_axis_label = "Time(s)", y_axis_label = "Flux", y_axis_type = "log", y_range = [1e-8, 1], width = 800, height = 800, tools = TOOLS2, ) colors = COLORS_LARGE nTimesteps = len(time[BURN_IN_STEPS:]) x = time[BURN_IN_STEPS:] y = actualFluxes[BURN_IN_STEPS:, 0] reactionName = np.repeat(constrainedReactions[0], nTimesteps) source = ColumnDataSource( data = dict( x = x, y = y, reactionName = reactionName) ) p2.line(x, y, line_color = colors[0], source = source) # Plot remaining metabolites onto initialized figure for m in np.arange(1, actualFluxes.shape[1]): y = actualFluxes[BURN_IN_STEPS:, m] reactionName = np.repeat(constrainedReactions[m], nTimesteps) source = ColumnDataSource( data = dict( x = x, y = y, reactionName = reactionName) ) p2.line(x, y, line_color = colors[m % len(colors)], source = source) if not os.path.exists(os.path.join(plotOutDir, "html_plots")): os.makedirs(os.path.join(plotOutDir, "html_plots")) p = bokeh.io.vplot(p1, p2) bokeh.io.output_file(os.path.join(plotOutDir, "html_plots", plotOutFileName + ".html"), title=plotOutFileName, autosave=False) bokeh.io.save(p) bokeh.io.curstate().reset()
hover_bar = HoverTool(tooltips=[('Rating','@x1'), ('Number of Business','@y1') ]) hover_pie = HoverTool(tooltips=[('Percentage','@ratio') ]) select_day = Select( options=['Sunday','Monday', 'Tuesday', 'Wednesday', 'Thursday','Friday','Saturday'], value='Monday', title='Day of the Week' ) chosen_tools =[hover,ResetTool(),BoxSelectTool(), SaveTool(),PanTool(), UndoTool(),RedoTool(),ZoomInTool(), ZoomOutTool() ] #data data = pd.read_csv(r'~/Desktop/yelp/yelp_business.csv',index_col='business_id') hour = pd.read_csv(r'~/Desktop/yelp/yelp_business_hours.csv',index_col='business_id') #data = pd.read_csv(r'E:\project\CIS4170\yelp\yelp_business.csv',index_col='business_id') #hour = pd.read_csv(r'E:\project\CIS4170\yelp\yelp_business_hours.csv',index_col='business_id') whole = pd.merge(data,hour, left_index=True, right_index=True) #data = pd.read_csv(r'E:\Software\WinPython\notebooks\yelp\yelp_business.csv',index_col='business_id') state_data = whole[whole.state=='NV'] categoricaldata = state_data.categories.str.contains('') open_data = state_data.loc[state_data[select_day.value.lower()] != 'None'] new_data = open_data.where(categoricaldata).dropna(thresh = 19).sort_values(by=['stars'],ascending=False)
def main(): """ Companion script to p1125_example_mahrs_logging.py Extracts data from the logging file and plots it. """ epilog = """ DO NOT RUN p1125_example_mahrs_logging_plot.py directly. Usage examples: bokeh serve --show p1125_example_mahrs_logging_plot.py --args -f 20201027-170242.py """ parser = argparse.ArgumentParser(description='p1125r_example_mahrs_logging_plot file parser, used with "boke serve"', formatter_class=argparse.RawDescriptionHelpFormatter, epilog=epilog) parser.add_argument("-f", "--file", dest="file", action='store', required=True, help='log file to parse/plot') args = parser.parse_args() if not os.path.exists(args.file): logger.error("file does not exist, {}".format(args.file)) exit(1) logger.info(args.file) try: spec = importlib.util.spec_from_file_location("p1125_log", args.file) G['d'] = importlib.util.module_from_spec(spec) spec.loader.exec_module(G['d']) except Exception as e: logger.error(e) return False logger.info(G['d'].p1125_ping) logger.info(G['d'].p1125_status) logger.info(G['d'].p1125_settings) #logger.info(G['d'].p1125_data) # uncomment to see imported fields/data plot_data = extract_data(G['d'].p1125_data, ['mAhr', 'i_max_ua']) if plot_data is not None: source = ColumnDataSource(data=plot_data) plot.circle(x="t", y="mAhr", size=5, source=source, color="green", legend_label='mAhr') l = plot.line(x="t", y="mAhr", line_width=2, source=source, color="green", legend_label='mAhr') plot.circle(x="t", y='i_max_ua', size=5, source=source, color="red", legend_label="Max uA") plot.line(x="t", y='i_max_ua', line_width=2, source=source, color="red", legend_label="Max uA") ht = HoverTool( tooltips=[("mAhr", "@mAhr{0.00}"), ("Max", "@i_max_ua{0.00} uA"), ("Time", "@t{%m/%d %H:%M}") ], mode='vline', # display a tooltip whenever the cursor is vertically in line with a glyph formatters={'@t': 'datetime', }, show_arrow=True, renderers=[l], ) plot.tools = [ht, BoxZoomTool(), WheelZoomTool(dimensions="width"), ResetTool(), UndoTool(), PanTool(dimensions="width")] else: logger.error("extract_data failed") s = create_select_widget(G['d'].p1125_data) hdr1 = Div(text="""Setup: VOUT {} mV, TIME_CAPTURE_WINDOW_S {} sec, {} sec""".format( G['d'].p1125_settings["VOUT"], G['d'].p1125_settings["TIME_CAPTURE_WINDOW_S"], G['d'].p1125_settings["TIME_TOTAL_RUN_S"])) hdr2 = Div(text="""P1125: {}, {}, {}, {} degC""".format( G['d'].p1125_ping["version"], G['d'].p1125_ping["rpi_serial"], G['d'].p1125_ping["url"], G['d'].p1125_status["temperature_degc"])) # init the first data to plot source_rt.data = G['d'].p1125_data[0]['plot'] dt = datetime.datetime.strptime(G['d'].p1125_data[0]['datetime'], '%Y%m%d-%H%M%S') source_sel.data = {'t': [dt, dt], 'y': [PLOT_MIN, PLOT_MAX]} doc_layout.add_root(column(hdr1, hdr2, s, row(plot, plot_rt))) return True
def modify_doc(doc): initial_points = {"x": [0, 1, 2], "y": [1, 3, 4]} source_points = ColumnDataSource(initial_points) source_line_rough = ColumnDataSource(compute(source_points, 10)) source_line_smooth = ColumnDataSource(compute(source_points, 100)) plot = figure(toolbar_location="above", x_range=Range1d(-10, 10, bounds=(-10, 10)), y_range=Range1d(-10, 10, bounds=(-10, 10))) line_rough = plot.line('x', 'y', source=source_line_rough, line_dash="dotted") line_smooth = plot.line('x', 'y', source=source_line_smooth, line_color="green") circles = plot.circle('x', 'y', source=source_points, size=10, nonselection_fill_alpha=0.5, nonselection_fill_color="blue", nonselection_line_color="firebrick", nonselection_line_alpha=1.0, selection_fill_alpha=0.9, selection_fill_color="red", selection_line_color="firebrick" ) plot2 = figure(toolbar_location="above") line_smooth2 = plot2.line('y', 'x', source=source_line_smooth, line_width=5) line_smooth2.glyph.line_color = "green" line_rough2 = plot2.line('y', 'x', source=source_line_rough, line_dash="dotted") circles2 = plot2.circle('y', 'x', source=source_points, size=10, nonselection_fill_alpha=0.5, nonselection_fill_color="blue", nonselection_line_color="firebrick", nonselection_line_alpha=1.0, selection_fill_alpha=0.9, selection_fill_color="red", selection_line_color="firebrick" ) def recompute(): source_line_smooth.data = compute(source_points, 100) line_smooth.glyph.line_dash = [1, 0] line_smooth.glyph.line_color = "green" line_smooth2.glyph.line_color = "green" def update_data(attr, old, new): source_line_rough.data = compute(source_points, 10) line_smooth.glyph.line_dash = [6, 3] line_smooth.glyph.line_color = "red" line_smooth2.glyph.line_color = "red" draw_tool = PointDrawTool(renderers=[circles]) undo_tool = UndoTool() redo_tool = RedoTool() plot.add_tools(draw_tool, undo_tool, redo_tool) plot.toolbar.active_drag = draw_tool draw_tool2 = PointDrawTool(renderers=[circles2]) plot2.add_tools(draw_tool2, undo_tool, redo_tool) plot2.toolbar.active_drag = draw_tool2 source_points.on_change("data", update_data) def undo(event): print("TODO : implement callback on undo tools") def redo(event): print("TODO : implement callback on undo tools") #undo_tool.on_event("click", undo) #redo_tool.on_event("click", redo) columns = [TableColumn(field="x", title="x"), TableColumn(field="y", title="y")] table_points = DataTable(source=source_points, columns=columns, editable=True) button = Button(label="Recompute") button.on_click(recompute) doc.add_root(column(button, row(plot, plot2), table_points))