Ejemplo n.º 1
0
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
Ejemplo n.º 2
0
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)
Ejemplo n.º 3
0
Archivo: gears.py Proyecto: xnx/bokeh
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
Ejemplo n.º 4
0
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
Ejemplo n.º 5
0
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)
Ejemplo n.º 6
0
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 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()
Ejemplo n.º 8
0
    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
Ejemplo n.º 9
0
    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
Ejemplo n.º 10
0
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 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()
Ejemplo n.º 12
0
    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()
Ejemplo n.º 13
0
                          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)
Ejemplo n.º 14
0
    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()
Ejemplo n.º 15
0
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']
categoricaldata = state_data.categories.str.contains('')
Ejemplo n.º 16
0
           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)
Ejemplo n.º 17
0
    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
Ejemplo n.º 18
0
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)
Ejemplo n.º 19
0
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)
	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()
Ejemplo n.º 21
0
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))