Ejemplo n.º 1
0
def generate_delta_report(df,
                          delta_cols,
                          target_col,
                          title='Delta report',
                          save_to='unnamed_delta_report.html'):
    p1 = plot_cat_bokeh(df[delta_cols + [target_col]], title=title)
    p1.title.text_font_size = '18pt'
    # deltas
    df_delta = pd.DataFrame(index=df.index)
    df_drel = pd.DataFrame(index=df.index)
    for col in delta_cols:
        df_delta['delta_' + col] = df[col] - df[target_col]
        df_drel['pct_delta_' +
                col] = 100 * (df[col] - df[target_col]) / df[target_col]
    p2 = plot_cat_bokeh(df_delta,
                        title='Delta analysis vs ' + target_col,
                        color_offset=0)
    p3 = plot_cat_bokeh(df_drel,
                        title='Percent delta vs ' + target_col,
                        color_offset=0)
    # histogram
    p4 = plot_hist_bokeh(df_delta, bins=100, color_offset=0)
    # box plot
    p5 = plot_boxplot_bokeh(df_delta)
    p6 = plot_acf_bokeh(df_delta)
    p = column(p1, p2, p3, Row(p4, p5), p6)
    if save_to is None:
        return p
    else:
        output_file(save_to, title=title)
        save(p)
Ejemplo n.º 2
0
def plot_bar_bokeh_from_dict(dict_df,
                             title='Bar plot',
                             plot_width=800,
                             plot_height=300):
    p = [
        plot_bar_bokeh(dict_df[k],
                       title=title + ' ' + k,
                       plot_height=plot_height,
                       plot_width=plot_width) for k in dict_df.keys()
    ]
    return Row(*p)
Ejemplo n.º 3
0
def generate_error_report(df,
                          title='Error report',
                          x_type='event_date',
                          save_to='unnamed_error_report.html'):
    # normal is already deltas
    p1 = plot_cat_bokeh(df.loc[:, df.count() > 0], title=title, x_type=x_type)
    p1.title.text_font_size = '18pt'
    # histogram
    p2 = plot_hist_bokeh(df, bins=100, color_offset=0)
    # box plot
    p3 = plot_boxplot_bokeh(df)
    p4 = plot_acf_bokeh(df)
    p = Column(p1, Row(p2, p3), p4)
    if save_to is None:
        return p
    else:
        output_file(save_to, title=title)
        save(p)
Ejemplo n.º 4
0
def plot_scatter_bokeh_size_vars(df,
                                 x,
                                 y,
                                 c,
                                 size_vars,
                                 title="Scatter plot",
                                 save_to="unnamed_scatter_plot.html"):
    output_file(save_to, title=title)
    f = []
    for sv in size_vars:
        f.append(
            plot_scatter_bokeh(df,
                               x,
                               y,
                               c,
                               sv,
                               title=title + " - sized by " + sv))
    p = Row(*f)
    save(p)
Ejemplo n.º 5
0
        d5['end_datetime_str'].push(  d6['end_datetime_str'][draw_index]);
        d5['start_datetime_dt'].push( d6['start_datetime_dt'][draw_index]);
        d5['end_datetime_dt'].push(   d6['end_datetime_dt'][draw_index]);

        source_sx_draw_highlight.data = d5;
        source_sx_draw_highlight.change.emit();
        
    """)
silder_draw_index.js_on_change('value', callback)



columns = [
        TableColumn(field="start_datetime_dt", title="start_datetime_dt"),
        TableColumn(field="sx_value", title="sx_value")
          ]
data_table = DataTable(source=source_sx_persistency, columns=columns, width=400, height=280)

controles=Column(silder_draw_index, slider_sx, sizing_mode='stretch_width')
p_sx_layout  = Column(p_sx,  controles, margin=( 8 , 8 , 8 , 8 ))

toggles = Row(toggle1, toggle2)
p_crd_layout = Column(p_crd, toggles, slider_alpha_Value, data_table, margin=( 8 , 8 , 8 , 8 ))

#最後の部分
plots = Row(p_sx_layout, p_crd_layout)
#output_file("MOGE.html")
show(plots)
curdoc().add_root(plots)

Ejemplo n.º 6
0
    def showGUI(self, pth_to_img, y_form, pred):
        ''' 
        Method builds the bokeh GUI
        
        Parameters
        ----------
        pth_to_img: path to ultrasound image
        y_form: true form of the lesion
        pred: predicted form the lesion
        '''

        ##############
        #Set up a figure
        ##############
        p = figure(x_range=(0, self.DIMS[0]),
                   y_range=(0, self.DIMS[1]),
                   tools=self._tools_to_show,
                   plot_width=self.DIMS[0],
                   plot_height=self.DIMS[1],
                   toolbar_location="above")

        #Add image as background
        p.image_url(url=[self.root_pth + pth_to_img],
                    x=431,
                    y=302,
                    w=862,
                    h=604,
                    anchor="center")

        #Nicier plot
        self._makeShiny(plot=p)

        ##############
        #Add lines and plot them
        ##############
        src_true, src_pred = self._getData()
        self._plotLines(plot=p, src_true=src_true, src_pred=src_pred)

        ##############
        #Add table
        ##############
        table = self._addTable(src_pred=src_pred)

        ##############
        #Add polygons
        ##############
        true_pol, c_t = self._addLesionForm(form=y_form, color='red', plot=p)
        pred_pol, c_p = self._addLesionForm(form=pred, color='blue', plot=p)

        #Add toggles for polygons
        toggle_true = Toggle(label="Show true form",
                             button_type="primary",
                             active=True)
        toggle_true.js_link('active', true_pol, 'visible')
        toggle_true.js_link('active', c_t, 'visible')

        toggle_pred = Toggle(label="Show predicted form",
                             button_type="primary",
                             active=True)
        toggle_pred.js_link('active', pred_pol, 'visible')
        toggle_true.js_link('active', c_p, 'visible')

        ##############
        #Add download button
        ##############
        button_csv = Button(label="Download", button_type="primary")
        button_csv.callback = CustomJS(args=dict(source=src_pred),
                                       code=open(self.root_pth +
                                                 "download.js").read())

        ##############
        #Add title div
        ##############
        div_title = Div(text="""<div> <b>LESION ADJUSTER</b> </div>""",
                        align='center',
                        style={
                            'font-size': '150%',
                            'color': '#1f77b4'
                        })
        ##############
        #Add description to the buttons
        ##############
        div_desc = Div(text="""<div> <b>CONTROLS</b> </div>""",
                       align='center',
                       style={
                           'font-size': '110%',
                           'color': '#1f77b4'
                       })

        ##############
        #Add Div to show euclidean distance and button to recalculate it
        ##############
        div_euclid = Div(text="""
                         <b>Diameter of predicted form is:</b> 334.80 <br>
                         <b>Diameter of true form is:</b> 368.64 <br>
                         <b>RMSE is:</b> 34.13
                         """,
                         align='center',
                         style={'font-size': '100%'})

        p.js_on_event(
            events.MouseMove,
            CustomJS(args=dict(div=div_euclid,
                               source_data_pred=src_pred,
                               source_data_true=src_true),
                     code="""
               var data_p = source_data_pred.data;
               var data_t = source_data_true.data;
               
               var x_p = data_p['x']
               var y_p = data_p['y']
               
               var x_t = data_t['x']
               var y_t = data_t['y']
               
               var diam_p = 0
               var diam_t = 0
               var rmse = 0
               
               //Diameter of pred form
               diam_p = Math.sqrt(Math.pow((x_p[0]-x_p[1]),2) + Math.pow((y_p[0]-y_p[1]),2))
               
               //Diameter of true form
               diam_t = Math.sqrt(Math.pow((x_t[0]-x_t[1]),2) + Math.pow((y_t[0]-y_t[1]),2))
               
               //RMSE
               rmse = Math.sqrt(Math.pow(diam_p - diam_t,2)/1)
               
               //Result
               div.text = "<b>Diameter of predicted form is: </b>" + diam_p.toFixed(2) + "<br> <b>Diameter of true form is: </b>" + diam_t.toFixed(2) + " <br> <b>RMSE is: </b>" + rmse.toFixed(2);
               
               """))

        ##############
        #Show
        ##############
        show(
            Column(
                div_title,
                Row(
                    Column(p, table),
                    Column(div_desc, toggle_true, toggle_pred, button_csv,
                           div_euclid))))
Ejemplo n.º 7
0
def main():
    # set up main bokeh figure
    p = figure(x_range=(0, 10),
               y_range=(0, 10),
               tools=[],
               title='Draw points in the network')
    p.background_fill_color = 'lightgrey'

    # start off with sample points and their associated flows
    source = ColumnDataSource({
        'x': [2, 7, 5, 8],
        'y': [2, 2, 6, 1],
        'flow': ['-2', '-5', '8', '-1']
    })

    renderer = p.scatter(x='x', y='y', source=source, color='blue', size=10)
    columns = [
        TableColumn(field="x", title="x"),
        TableColumn(field="y", title="y"),
        TableColumn(field='flow', title='flow')
    ]
    table = DataTable(source=source,
                      columns=columns,
                      editable=True,
                      height=200)

    draw_tool = PointDrawTool(renderers=[renderer], empty_value='1')
    p.add_tools(draw_tool)
    p.toolbar.active_tap = draw_tool

    titletextbox = Div(
        text=
        "<h2>Objective: minimize construction cost of network<p>Construction cost is based on number of pipes and distance between nodes.<br>Additional constraints imposed: flows in network must be balanced.<br></h2>",
        width=1100,
        height=150)
    textbox = Div(text="", width=200, height=100)
    floating = 1.
    fixed = 0.
    button = Button(label='Solve Network')

    button.on_event(
        ButtonClick,
        partial(button_click_event, source=source, textbox=textbox, figure=p))

    p.on_event(
        PanEnd,
        partial(button_click_event, source=source, textbox=textbox, figure=p))

    # set up sliders
    lumpSumCost = Slider(title="Fixed cost pipe",
                         value=0.0,
                         start=0.0,
                         end=500.0,
                         step=50)
    floatingCost = Slider(title="Floating cost pipe",
                          value=1.0,
                          start=0.0,
                          end=500.0,
                          step=10.)

    for w in [lumpSumCost, floatingCost]:
        w.on_change(
            'value',
            partial(update_data,
                    source=source,
                    textbox=textbox,
                    figure=p,
                    lumpSumCost=lumpSumCost,
                    floatingCost=floatingCost))

    # create page layout
    curdoc().add_root(
        Column(
            titletextbox,
            Row(Column(p, table, width=800),
                Column(lumpSumCost, floatingCost, button, textbox,
                       width=300))))
    curdoc().title = "Network"
Ejemplo n.º 8
0
# print db.get(str(id)+"4"+"x")
# print db.get(str(id)+"5"+"x")

# print db.get(str(id)+"1"+"y")
# print db.get(str(id)+"2"+"y")
# print db.get(str(id)+"3"+"y")
# print db.get(str(id)+"4"+"y")
# print db.get(str(id)+"5"+"y")

# print db.getall()
#db.deldb()
#db.dump()

#db.deldb()
#db.dump()
layout = (Column(p, Row(button1, button)))

tools = ["xpan,pan,xwheel_zoom,wheel_zoom,box_zoom,reset,previewsave"]
q = figure(x_range=(0, 10),
           y_range=(0, 10),
           tools=tools,
           title='Analysis Peak')

q.y_range.start = -10
q.y_range.end = 220
q.x_range.start = -10
q.x_range.end = 180

q.xaxis.axis_label = "Extension [nm]"
q.xaxis.axis_label_text_font_size = "20pt"
q.xaxis.axis_label_text_font_style = 'bold'
Ejemplo n.º 9
0
def new_scatter():

    df = pd.read_pickle("./data/Rat_Sightings.pkl")

    def var_var_scatter(df=df,
                        var1="Borough",
                        var2="Neighborhood",
                        year="All",
                        season="All"):
        """
            Allows the user to select whichever variables
            from the dataset for comparison in the form
            of a scatter plot.
        """

        # we want the var1 to be the least granular variable
        name1, name2 = var1, var2

        # get the number of classes
        n_classes1 = df[var1].nunique()
        n_classes2 = df[var2].nunique()

        # if the num of classes in var2 is less then reassign variables
        if n_classes2 < n_classes1:
            var1 = name2
            var2 = name1

        # group by
        if year == "All" and season == "All":

            #group dataframe by indicated column name and rename columns
            groupeddf = (df.assign(n=0).groupby([
                var1, var2
            ]).n.count().reset_index().rename(columns={"n": "rat_sightings"}))

        elif year != "All" and season == "All":

            #group dataframe by indicated column name and rename columns
            groupeddf = (df.query("Year == '%s'" % year).assign(n=0).groupby([
                var1, var2
            ]).n.count().reset_index().rename(columns={"n": "rat_sightings"}))

        elif year != "All" and season != "All":

            #group dataframe by indicated column name and rename columns
            groupeddf = (df.query("Year == '%s'" % year).query(
                "Season == '%s'" % season).assign(n=0).groupby(
                    [var1, var2]).n.count().reset_index().rename(
                        columns={"n": "rat_sightings"}))

        elif year == "All" and season != "All":

            #group dataframe by indicated column name and rename columns
            groupeddf = (df.query("Season == '%s'" % season).assign(
                n=0).groupby([var1, var2]).n.count().reset_index().rename(
                    columns={"n": "rat_sightings"}))

        else:

            #group dataframe by indicated column name and rename columns
            groupeddf = (df.assign(n=0).groupby([
                var1, var2
            ]).n.count().reset_index().rename(columns={"n": "rat_sightings"}))

        # get unique values for both vars
        uniq_vals1 = [*groupeddf[var1].unique()]
        uniq_vals2 = [*groupeddf[var2].unique()]

        # now add in percentages to give more information to user
        # get the total values

        sum_data = pd.DataFrame(groupeddf.groupby(var1)["rat_sightings"].sum())

        col_name = "total_%s_sights" % var1
        sum_data.columns = [col_name]

        # merge the totals in to the dataframe
        """ Sure this can be applied to the dataframe in some fashion but opting
        for the conceptually easier solution here.
        """
        groupeddf = groupeddf.merge(sum_data.reset_index())

        # get percentages for each location type
        perc_name = "perc_sights"
        groupeddf[perc_name] = (groupeddf["rat_sightings"] /
                                groupeddf[col_name]) * 100

        # create color mapper
        mapper = LogColorMapper(palette=Blues9[::-1])

        # instantiate figure
        p = figure(plot_width=800, plot_height=600, y_range=uniq_vals1)

        p.circle(x="rat_sightings",
                 y=jitter(var1, width=0.6, range=p.y_range),
                 source=ColumnDataSource(data=groupeddf),
                 alpha=0.6,
                 size=30,
                 hover_alpha=0.9,
                 fill_color={
                     "field": "rat_sightings",
                     "transform": mapper
                 })

        p.add_tools(
            HoverTool(tooltips=[(
                "%s" % var1, "@%s" %
                var1), ("%s" % var2, "@%s" %
                        var2), ("Num. of Rat Sightings", "@rat_sightings"),
                                ("Percentage of Rat Sightings within the %ss" %
                                 var1, "@perc_sights %")]))

        return p

    # define variable selects
    var1_select = Select(value="Borough",
                         options=[*df.columns],
                         title="Select a variable: ")
    var2_select = Select(value="Neighborhood",
                         options=[*df.columns],
                         title="Select a variable: ")
    year_select = Select(value="All",
                         options=[*df["Year"].unique()] + ["All"],
                         title="Select a year to view: ")
    season_select = Select(value="All",
                           options=[*df["Season"].unique()] + ["All"],
                           title="Select a season to view: ")

    # define interactivity
    def update_plot(attr, old, new):

        layout.children[1] = var_var_scatter(var1=var1_select.value,
                                             var2=var2_select.value,
                                             year=year_select.value,
                                             season=season_select.value)

    # inc interactivity
    var1_select.on_change("value", update_plot)
    var2_select.on_change("value", update_plot)
    year_select.on_change("value", update_plot)
    season_select.on_change("value", update_plot)

    layout = Column(
        Row(Column(var1_select, var2_select), Column(year_select,
                                                     season_select)),
        var_var_scatter())
    return layout