コード例 #1
0
ファイル: bokeh_app.py プロジェクト: Minhir/laser-polarimeter
    def create_fig(data_names: list,
                   colors: list,
                   y_axis_name: str,
                   ers: str = None):
        """Создаёт график data_names : time. Если в data_names несколько имён,
        то они будут на одном графике. Возвращает fig.

        :param data_names: список с именами полей данных из data_storage
        :param colors: список цветов, соотв. элементам из fig_names
        :param y_axis_name: имя оси Y
        :param ers: 'err', 'pretty' --- вид усов (у данных должны быть поля '_up_error', '_down_error'),
                       'err' --- усы обыкновенные
                       'pretty' --- усы без шляпки и цветом совпадающим с цветом точки
        :return fig --- Bokeh figure
        """

        if len(data_names) != len(colors):
            raise IndexError('Кол-во цветов и графиков не совпадает')

        fig = figure(plot_width=width_,
                     plot_height=300,
                     tools="box_zoom, wheel_zoom, pan, save, reset",
                     active_scroll="wheel_zoom",
                     lod_threshold=100,
                     x_axis_type="datetime")

        for fig_name, color in zip(data_names, colors):

            if ers == 'err':
                fig.add_layout(
                    Whisker(source=data_source,
                            base="time",
                            upper=fig_name + '_up_error',
                            lower=fig_name + '_down_error'))
            elif ers == 'pretty':
                fig.add_layout(
                    Whisker(source=data_source,
                            base="time",
                            upper=fig_name + '_up_error',
                            lower=fig_name + '_down_error',
                            line_color=color,
                            lower_head=None,
                            upper_head=None))

            fig.circle('time',
                       fig_name,
                       source=data_source,
                       size=5,
                       color=color,
                       nonselection_alpha=1,
                       nonselection_color=color)

        fig.yaxis.axis_label = y_axis_name
        fig.xaxis.axis_label = 'Время'
        fig.xaxis.formatter = datetime_formatter
        fig.x_range = asym_fig.x_range

        return fig
コード例 #2
0
def show_graph(x, y, xlabel, ylabel, ey=None, eylow=None, eyhigh=None,
               xtype='linear', ytype='linear',
               point_labels=None):
    '''
    Function to display a simple "y vs. x" graph, with y error bars
    Parameters
    ----------
    x: ndarray, x coordinates
    y: ndarray, y coordinates
    ey: ndarray, size of y error bars
    xlabel: x-axis label
    ylabel: y-axis label
    xtype: 'log', 'linear', 'datetime'
    ytype: 'log', 'linear', 'datetime'
    point_labels: one label per point, to be displayed when mouse overs near
                  point

    Returns
    -------
    A bokeh.plotting.figure with the y vs. x graph
    '''

    fig = figure(background_fill_color='#ffffff', x_axis_label=xlabel,
                 x_axis_type=xtype, y_axis_type=ytype, y_axis_label=ylabel)
    source = ColumnDataSource(data=dict(x=x, y=y))
    if point_labels is not None:
        source.data['point_labels'] = point_labels
    fig.circle(x='x', y='y', size=2, source=source)

    if eylow is None:
        eylow = ey
    if eyhigh is None:
        eyhigh = ey

    if eylow is not None or eyhigh is not None:
        yhigh = y
        ylow = y
        if eylow is not None:
            ylow = y - eylow
        if eyhigh is not None:
            yhigh = y + eyhigh
        source_error = ColumnDataSource(data=dict(base=x, lower=ylow, upper=yhigh))
        error_bars = Whisker(source=source_error, base="base", lower="lower",
                             upper="upper")
        error_bars.line_color = 'steelblue'
        error_bars.upper_head.line_color = 'steelblue'
        error_bars.lower_head.line_color = 'steelblue'
        error_bars.upper_head.size = 4
        error_bars.lower_head.size = 4
        fig.add_layout(error_bars)

    if point_labels is not None:
        fig.add_tools(HoverTool(tooltips=[('value', '@y'),
                                          ('point id', '@point_labels')],
                                mode='mouse',
                                point_policy='snap_to_data'))

    return fig
コード例 #3
0
def plot_quality(df):
    """Use bokeh to plot team strengths

    Keyword arguments:
    df -- quality dataframe with columns:
        - Team
        - attack
        - attacksd
        - attack_low
        - attack_high
        - defend
        - defendsd
        - defend_low
        - defend_high
    """
    source = ColumnDataSource(df)
    p = figure(x_range=(-1, 1.5), y_range=(-1, 1.5))
    p.add_layout(
        Whisker(
            lower="attack_low",
            upper="attack_high",
            base="defend",
            dimension="width",
            source=source,
        ))
    p.add_layout(
        Whisker(
            lower="defend_low",
            upper="defend_high",
            base="attack",
            dimension="height",
            source=source,
        ))
    p.circle(x="attack", y="defend", source=source)

    p.title.text = "Team strengths"
    p.xaxis.axis_label = "Attacking"
    p.yaxis.axis_label = "Defending"

    hover = HoverTool()
    hover.tooltips = [
        ("Team", "@Team"),
        ("Attacking", "@attack"),
        ("Attacking sd", "@attacksd"),
        ("Defending", "@defend"),
        ("Defending sd", "@defendsd"),
    ]
    p.add_tools(hover)

    show(p)
コード例 #4
0
def test_Whisker() -> None:
    whisker = Whisker()
    assert whisker.level == 'underlay'
    assert whisker.lower == field("lower")
    assert whisker.lower_units == 'data'
    assert isinstance(whisker.lower_head, ArrowHead)
    assert whisker.lower_head.size == 10
    assert whisker.upper == field("upper")
    assert whisker.upper_units == 'data'
    assert isinstance(whisker.upper_head, ArrowHead)
    assert whisker.upper_head.size == 10
    assert whisker.base == field("base")
    assert whisker.dimension == 'height'
    assert isinstance(whisker.source, ColumnDataSource)
    assert whisker.x_range_name == 'default'
    assert whisker.y_range_name == 'default'
    check_line_properties(whisker, "")
    check_properties_existence(
        whisker, ANNOTATION + [
            "lower",
            "lower_units",
            "lower_head",
            "upper",
            "upper_units",
            "upper_head",
            "base",
            "base_units",
            "dimension",
            "source",
        ], LINE)
コード例 #5
0
    def update(self):
        if self.test_done and not self.analysed:
            self.btn.label = 'Test Complete'
            np.savetxt('test.txt', np.column_stack((self.x, self.y)))
            x = np.array(self.x)
            y = np.array(self.y)
            results = analyse_data(x, y, 7, 3)
            tmeans, fmeans, e_fmeans, msg, _critical_load, load_asymptote, predicted_force = results
            self.results_div.text = msg

            fill_src = ColumnDataSource(dict(x=tmeans, upper=predicted_force,
                                             lower=load_asymptote*np.ones_like(tmeans)))
            self.fig.add_layout(
                Band(base='x', lower='lower', upper='upper', source=fill_src, fill_alpha=0.7)
            )
            self.fig.circle(tmeans, fmeans, color='red', size=5, line_alpha=0)

            esource = ColumnDataSource(dict(x=tmeans, upper=fmeans+e_fmeans, lower=fmeans-e_fmeans))
            self.fig.add_layout(
                Whisker(source=esource, base='x', upper='upper', lower='lower', level='overlay')
            )
            self.analysed = True
        else:
            if self.tindeq is not None:
                self.btn.label = 'Start Test'
            self.state.update(self)
            self.source.stream({'x': self.xnew, 'y': self.ynew})
            nlaps = self.duration // 10
            self.laps.text = f"Rep {1 + nlaps - self.reps}/{nlaps}"
            self.reset()
コード例 #6
0
def features_bar(order, xs, ys, yerrs):
    feature_dict = {
        'gdp': 'GDP',
        'population': "Population",
        'md2do': "MD:DO Ratio",
        'nurses_never': "Nurse Comm. Nev.",
        'nurses_usually': "Nurses Comm. Usual.",
        'nurses_always': "Nurses Comm. Alw.",
        'doctors_never': "Docs Comm. Nev.",
        'doctors_usually': "Docs Comm. Usual.",
        'doctors_always': "Docs Comm. Alw.",
        'error_disc': "Error Disclosure",
        'time_after_treatment': "Time All. Aft. Treatment",
        'time_after_discovery': "Time All. Aft. Discovery",
        'max_time': "Max. Time All.",
        'cap': "P.O. Cap",
        'cap_death_injury': "P.O. Cap (Death/Serious Injury)"
    }
    p = figure(plot_width=400,
               plot_height=600,
               y_axis_label='Feature Importance',
               y_range=ranges.Range1d(start=0, end=0.35))
    p.yaxis.axis_label_text_font_size = "15pt"
    base, lower, upper = [], [], []
    err_xs = []
    err_ys_low = []
    err_ys_up = []
    #xs=range(X.shape[1])
    #ys=importances[indices]
    #yerrs=std[indices]
    for x, y, yerr in zip(xs, ys, yerrs):
        err_xs.append(x)
        err_ys_low.append(y - yerr)
        err_ys_up.append(y + yerr)
    base = xs
    lower = err_ys_low
    upper = err_ys_up
    source_error = ColumnDataSource(
        data=dict(base=base, x=order, lower=lower, upper=upper, values=ys))
    p.vbar(source=source_error,
           x='base',
           top='values',
           bottom=0,
           width=0.8,
           color='firebrick')
    p.add_layout(
        Whisker(source=source_error,
                base="base",
                upper="upper",
                lower="lower",
                level='overlay'))
    p.xaxis.ticker = base
    label_dict = {}
    for num, label in zip(base, order):
        label_dict[num] = feature_dict[label]
    p.xaxis.major_label_overrides = label_dict
    p.xaxis.major_label_orientation = np.pi / 4
    p.xaxis.major_label_text_font_size = "12pt"
    return (p)
コード例 #7
0
ファイル: simple_plot.py プロジェクト: stelais/MOA_inference
def plotter(x_axis,
            y_axis,
            y_error,
            p,
            legend_label='',
            y_label_name='Magnification',
            color='purple',
            plot_errorbar=False,
            t0_error_plot=False,
            t0=None,
            t0_error=None):
    """
    Produce plot for the event

    :return: None
    """

    p.xaxis.axis_label = 'Days'
    p.yaxis.axis_label = y_label_name

    p.circle(x_axis,
             y_axis,
             fill_alpha=0.2,
             size=5,
             legend_label=legend_label,
             color=color)

    if plot_errorbar:
        upper = [x + e for x, e in zip(y_axis, y_error)]
        lower = [x - e for x, e in zip(y_axis, y_error)]
        source = ColumnDataSource(
            data=dict(groups=x_axis, counts=y_axis, upper=upper, lower=lower))

        p.add_layout(
            Whisker(source=source,
                    base="groups",
                    upper="upper",
                    lower="lower",
                    level="overlay"))

    if t0_error_plot:
        t0_location = Span(location=t0,
                           dimension='height',
                           line_color='red',
                           line_dash='dashed',
                           line_width=1)
        p.add_layout(t0_location)

        box = BoxAnnotation(left=(t0 - t0_error),
                            right=(t0 + t0_error),
                            line_width=1,
                            line_color='black',
                            line_dash='dashed',
                            fill_alpha=0.2,
                            fill_color='orange')

        p.add_layout(box)
    return p
コード例 #8
0
def test_Whisker_and_Band_accept_negative_values() -> None:
    whisker = Whisker(base=-1., lower=-1.5, upper=-0.5)
    assert whisker.base == -1.
    assert whisker.lower == -1.5
    assert whisker.upper == -0.5
    band = Band(base=-1., lower=-1.5, upper=-0.5)
    assert band.base == -1.
    assert band.lower == -1.5
    assert band.upper == -0.5
コード例 #9
0
ファイル: bokeh_app.py プロジェクト: Minhir/laser-polarimeter
    def create_whisker(data_name: str):
        """ Создает усы для data_name от time

        :param data_name: имя поля данных из data_storage
                (у данных должны быть поля '_up_error', '_down_error')
        :return: Bokeh Whisker
        """
        return Whisker(source=data_source,
                       base="time",
                       upper=data_name + "_up_error",
                       lower=data_name + "_down_error")
コード例 #10
0
def test_whisker(output_file_url, selenium, screenshot):

    x_range = Range1d(0, 10)
    y_range = Range1d(0, 10)

    # Have to specify x/y range as labels aren't included in the plot area solver
    plot = Plot(plot_height=HEIGHT, plot_width=WIDTH,
                x_range=x_range, y_range=y_range, toolbar_location=None)

    source = ColumnDataSource(data=dict(
        x1 = [1,3,5,7,9],
        lower1 = [1,2,1,2,1],
        upper1 = [2,3,2,3,2],
        x2 = [200, 250, 350, 450, 550],
        lower2 = [400, 300, 400, 300, 400],
        upper2 = [500, 400, 500, 400, 500],
    ))

    whisker1 = Whisker(base='x1', lower='lower1', upper='upper1',
                       line_width=3, line_color='red', line_dash='dashed',
                       source=source)

    whisker2 = Whisker(base='x2', lower='lower2', upper='upper2', upper_head=OpenHead(),
                       base_units='screen', lower_units='screen', upper_units='screen',
                       dimension='width', line_width=3, line_color='green',
                       source=source)

    plot.add_layout(whisker1)
    plot.add_layout(whisker2)

    # Save the plot and start the test
    save(plot)
    selenium.get(output_file_url)
    assert has_no_console_errors(selenium)

    # Take screenshot
    screenshot.assert_is_valid()
コード例 #11
0
def boxplot_trajectories_rf(*args):
    dataframe = pd.read_csv("plot_datasets/rf_score_traj_results.tsv", 0, "\t")
    source = ColumnDataSource(dataframe)

    tr_low = dataframe['mcc_tr_lower'].replace(',', '.').astype(float)
    tr_mean = dataframe['mcc_tr_mean'].replace(',', '.').astype(float)
    tr_high = dataframe['mcc_tr_upper'].replace(',', '.').astype(float)
    ts_low = dataframe['mcc_ts_lower'].replace(',', '.').astype(float)
    ts_mean = dataframe['mcc_ts_mean'].replace(',', '.').astype(float)
    ts_high = dataframe['mcc_ts_upper'].replace(',', '.').astype(float)

    list_traj = [item for item in range(2, 17)]

    p = plotting.figure(tools="",
                        background_fill_color="#efefef",
                        toolbar_location=None)
    p.circle(list_traj, tr_mean, color='navy')
    p.add_layout(
        Whisker(source=source,
                base='traj_id',
                upper="mcc_tr_upper",
                lower="mcc_tr_lower",
                level="overlay"))
    p.circle(list_traj, ts_mean, color='red')
    p.add_layout(
        Whisker(source=source,
                base='traj_id',
                upper="mcc_ts_upper",
                lower="mcc_ts_lower",
                level="overlay"))
    p.yaxis.axis_label = "MCC"
    p.yaxis.axis_label_text_font_size = "25pt"
    p.xaxis.axis_label = "Trajectories"
    p.xaxis.axis_label_text_font_size = "25pt"

    return p
コード例 #12
0
    def render_sources(self, src_dict, **render_kwargs):
        super().render_sources(src_dict, y='deltaG', **render_kwargs)

        #todo make sure that if a new deltaG get plotted the graph is redrawn
        for name, data_source in src_dict.items():
            if 'covariance' in data_source.source.data.keys():
                # y = data_source.source.data['deltaG']
                # cov = data_source.source.data['covariance']
                # data_source.source.data['__upper'] = y + cov
                # data_source.source.data['__lower'] = y - cov

                whiskers = Whisker(source=data_source.source,
                                   base='r_number',
                                   upper='__upper',
                                   lower='__lower')
                self.figure.add_layout(whiskers)
コード例 #13
0
def plotlc(outputfile,indexlist,columnfile,namelist=''):
    colorlist = ['red','blue','green','yellow']
    p = figure(plot_width=900, plot_height=500, title='',active_drag='pan', active_scroll='wheel_zoom',
                  x_axis_type='datetime',y_axis_label='VEGAMAG',x_axis_label='Date_Obs')
    
    
    for i,indexnumber in enumerate(indexlist):
        color = colorlist[i]
        
        datagood = createdatadicclean(outputfile,indexnumber,columnfile,name=namelist[i])

        source = ColumnDataSource(data=datagood)
   


        


        #Tool to get wavelength
        hover2 = HoverTool(
                tooltips=[
                    ('Date', '(@obnames)')
                ]
            )


        p.add_tools(hover2)



        # add a circle renderer with a size, color, and alpha
        p.circle('time','VEGAMAG', source=source, color=color, name='name',legend='name')
        p.add_layout(
            Whisker(source=source, base="time", upper="upper", lower="lower")
        )


    p.y_range.flipped = True



    show(p)
コード例 #14
0
def plot_MLE_conf_int(df_estimates, variable_name, x_axis, unit=""):
    """Plot the confidence interval of estimates over our MLE
    Arguments:
        df_estimates = dataframe with column parameter indicating the name of the
            parameter of the MLE, column values with the value of the parameter
            optimized with the ground truth data, columns 2.5 percentile and
            97.5 percentile with the lower and upper bound of the confidence
            interval
        variable_name = the name of the parameter that we want to graph the
            confidence interval of
        x_axis = The values that we want to graph as the x_axis

    Returns:
        p: bohek graph object that can be shown with show()
    """

    df_var_estimates = df_estimates[df_estimates['parameter'] == variable_name]

    p = figure(plot_width=600,
               plot_height=300,
               title="Estimates of {} with 95% confidence intervals".format(
                   variable_name),
               y_axis_label="{} {}".format(variable_name, unit),
               x_axis_label=x_axis,
               y_range=(min(df_var_estimates['2.5 percentile']) * 0.9,
                        max(df_var_estimates['97.5 percentile']) * 1.1))

    base, lower, upper = [], [], []
    source_error = ColumnDataSource(
        data=dict(base=df_var_estimates[x_axis],
                  lower=df_var_estimates['2.5 percentile'],
                  upper=df_var_estimates['97.5 percentile']))

    p.add_layout(
        Whisker(source=source_error, base="base", upper="upper",
                lower="lower"))
    for i, tif_ind in enumerate(list(df_var_estimates[x_axis].unique())):
        y = df_var_estimates[df_var_estimates[x_axis] == tif_ind]['value']
        color = colors[i % len(colors)]
        p.circle(x=tif_ind, y=y, color=color)
    return p
コード例 #15
0
ファイル: count_hbonds.py プロジェクト: omwan/hbonds-web
def build_means_scatter(data_file, bucket_size):
    """
    Generate scatter plot from hbond means data.

    :param data_file:   path to means file
    :param bucket_size: only display buckets with at least this many values
    :return: scatter plot
    """
    data = pandas.read_csv(data_file)
    data = data[data["count"] > bucket_size]

    x = data["value"]
    y = data["mean"]
    error = data["std"]
    upper = [y_val + e for y_val, e in zip(y, error)]
    lower = [y_val - e for y_val, e in zip(y, error)]

    p = figure(y_range=[1, 4], title="Mean values")
    p.xaxis.axis_label = "# Hydrogen Bonds / # Residues"
    p.yaxis.axis_label = "Resolution (Angstroms)"

    source = ColumnDataSource(data=dict(
        x=x,
        y=y,
        upper=upper,
        lower=lower,
    ))

    p.add_layout(
        Whisker(source=source, base="x", upper="upper",
                lower="lower", level="overlay")
    )

    p.scatter('x', 'y', source=source)

    return p
コード例 #16
0
def make_plot_corr(src):

    TOOLS = "pan,wheel_zoom,box_select,lasso_select,reset,hover,save"
    p = figure(tools=TOOLS,
               plot_width=2 * 900,
               plot_height=400,
               min_border=10,
               min_border_left=50,
               toolbar_location="above",
               y_axis_label='cross corr value',
               x_axis_label='corresponding shift in minutes',
               title="Shift vs cross correlation",
               y_range=None,
               x_range=None)

    p.line(x='lag_in_minutes', y='cross_corr', source=src)

    p.add_layout(
        Whisker(source=src,
                base="lag_in_minutes",
                upper="upper",
                lower="lower"))

    return p
コード例 #17
0
def test_Whisker():
    whisker = Whisker()
    assert whisker.level == 'underlay'
    assert whisker.lower is None
    assert whisker.lower_units == 'data'
    assert isinstance(whisker.lower_head, ArrowHead)
    assert whisker.lower_head.size == 10
    assert whisker.lower_head.level == 'underlay'
    assert whisker.upper is None
    assert whisker.upper_units == 'data'
    assert isinstance(whisker.upper_head, ArrowHead)
    assert whisker.upper_head.size == 10
    assert whisker.upper_head.level == 'underlay'
    assert whisker.base is None
    assert whisker.dimension == 'height'
    assert isinstance(whisker.source, ColumnDataSource)
    assert whisker.x_range_name == 'default'
    assert whisker.y_range_name == 'default'
    check_line_properties(whisker, "")
    check_properties_existence(whisker, [
        "visible", "level", "lower", "lower_units", "lower_head", "upper",
        "upper_units", "upper_head", "base", "base_units", "dimension",
        "source", "x_range_name", "y_range_name"
    ], LINE)
コード例 #18
0
glyph = VBar(x='n_participants',
             top='mean_error',
             bottom=0.1,
             width="n_participants",
             hatch_scale=None)
p_error_stat.add_glyph(source_stat, glyph)
p_error_stat.y_range.start = 0
p_error_stat.x_range.range_padding = 0.1
p_error_stat.xaxis.major_label_orientation = 1
p_error_stat.xgrid.grid_line_color = None

# overlay for the standard deviation
p_error_stat.add_layout(
    Whisker(source=source_stat,
            base="n_participants",
            upper="upper",
            lower="lower",
            level="overlay"))


@linear()
def refine_stat(step):
    """Refine the experimental computation of the mean and deviation of
       the error"""
    sanitize_params()
    eps_val = float(eps.value)
    small_delta_val = float(small_delta.value)
    big_delta_val = float(big_delta.value)
    gamma_val = float(gamma.value)

    if not enable_refine_btn.active:
コード例 #19
0
    def bar_chart(self,
                  pool,
                  gr_data,
                  data,
                  x,
                  y,
                  title,
                  y_label,
                  x_range,
                  y_range,
                  std=None,
                  y_axis_type=None):
        if y_axis_type:
            bottom = 0.0000000001
        else:
            bottom = 0
            y_axis_type = 'auto'

        bar = figure(title=title,
                     y_axis_label=y_label,
                     x_range=x_range,
                     width=1500,
                     y_axis_type=y_axis_type)
        bar.y_range.start = bottom
        bar.xaxis.major_label_orientation = pi / 4
        if std:
            base = data[x]
            upper = [u + data[std][i] for i, u in enumerate(data[y])]
            lower = [u - data[std][i] for i, u in enumerate(data[y])]
            errors = ColumnDataSource(
                data=dict(base=base, lower=lower, upper=upper))
            bar.add_layout(
                Whisker(source=errors,
                        base='base',
                        upper='upper',
                        lower='lower',
                        level='annotation'))
        palette_key = 0
        if len(gr_data) > 10:
            max_groups = 20
            category = 'Category20'
        else:
            max_groups = 10
            category = 'Category10'

        for group, d in gr_data.items():
            bar.vbar(x=d[x],
                     width=0.5,
                     top=d[y],
                     bottom=bottom,
                     legend=group,
                     color=d3[category][max_groups][palette_key])
            palette_key += 1
        # TODO: clear all somehow and select all
        bar.legend.click_policy = 'hide'
        bar.output_backend = 'svg'
        # save the png for the zip file
        chart_file_png = 'class_chart_' + title.replace(' ', '_').replace(
            ',', '').lower() + '.png'
        chart_path_png = self.root_path + chart_file_png
        #pool.apply_async(export_png, (bar,), {'filename': chart_path_png})
        export_png(bar, filename=chart_path_png)
        # save paths to zip
        self.paths_to_zip[chart_file_png] = chart_path_png
        return bar
コード例 #20
0
p = Figure(tools=TOOLS, plot_width=500, plot_height=500, min_border=10, min_border_left=50,
           title="Inputted Data",
           toolbar_location="above",
           active_scroll='wheel_zoom', 
           active_drag = "pan",
           x_axis_label = "x",
           y_axis_label = "y"
           )
p.background_fill_color = "#fafafa"

r = p.scatter(source = srcData, x='x', y='y', size=10, color="blue", alpha=0.6)

#y error bars:
p.add_layout(
        Whisker(source = srcData, base="x", upper="err_y_up", lower="err_y_down")
    )

#x error bars:
p.add_layout(
        Whisker(source = srcData, base="y", upper="err_x_up", lower="err_x_down", dimension="width")
    )

#JS callbacks

callbackPlot = CustomJS(args=dict(srcData=srcData, p=p, xaxis=p.xaxis[0], yaxis=p.yaxis[0]), code="""
    p.reset.emit();

    let input_data = getData();

    let x_data = input_data[0];
コード例 #21
0
ファイル: bar.py プロジェクト: awarn314/Spotwater
def bar_tab(df_means, df_stdev, Time, Treatments, number_cmpds_run):
    colors = [
        "firebrick", "navy", 'green', 'orange', 'violet', 'lawngreen',
        'powderblue', 'lightgreen', 'yellow', 'olive', 'red', 'grey',
        'skyblue', 'indigo', 'slategray', 'hotpink', 'peachpuff', 'powderblue'
    ]
    Cmpd0 = df_means.columns[len(Treatments) + len(Time)]
    cmpd_options = cmpd_options_func(df_means,
                                     len(Treatments) + len(Time),
                                     number_cmpds_run)
    time_vals = df_means[Time[0]].drop_duplicates().tolist()
    time_vals = natsorted(time_vals)
    df_means = df_means.reindex(index=order_by_index(
        df_means.index, index_natsorted(df_means[Time[0]])))

    MEANs = df_means.groupby(Treatments)[Cmpd0].apply(list).to_dict()
    STDs = df_stdev.groupby(Treatments)[Cmpd0].apply(list).to_dict()
    keys = []
    u_keys = []
    l_keys = []
    results = {'time_vals': time_vals}

    for h in range(len(MEANs)):
        kk = list(MEANs.keys())[h][0]
        for tot in range(1, len(Treatments)):
            sk = list(MEANs.keys())[h][tot]
            if type(sk).__name__ != 'str':
                sk = str(sk)
            kk += '_' + sk
        keys.append(kk)
        u_keys.append('upper ' + kk)
        l_keys.append('lower ' + kk)
        mu = list(MEANs.values())[h]
        sd = list(STDs.values())[h]
        upper = [x + e for x, e in zip(mu, sd)]
        lower = [x - e for x, e in zip(mu, sd)]
        results.update({keys[h]: mu})
        results.update({u_keys[h]: upper})
        results.update({l_keys[h]: lower})
    source = ColumnDataSource(data=results)

    p = figure(x_range=time_vals,
               plot_height=1000,
               plot_width=1000,
               title=Cmpd0,
               toolbar_location="right")
    legend_it = []
    for hh in range(len(MEANs)):
        c = p.vbar(x=dodge('time_vals',
                           -0.4 + (.8 * hh / len(MEANs)),
                           range=p.x_range),
                   top=keys[hh],
                   width=(0.8 / len(MEANs)),
                   source=source,
                   color=colors[hh])
        p.add_layout(
            Whisker(source=source,
                    base=dodge('time_vals',
                               -0.4 + (.8 * hh / len(MEANs)),
                               range=p.x_range),
                    upper=u_keys[hh],
                    lower=l_keys[hh],
                    level="overlay"))
        legend_it.append((keys[hh], [c]))
    legend = Legend(items=legend_it, location=(0, 0))
    legend.click_policy = "mute"
    p.add_layout(legend, 'right')
    p.x_range.range_padding = 0.1
    p.xgrid.grid_line_color = None
    p.legend.orientation = "vertical"

    #This is where the widget is setup
    select = Select(title='Select your compound:',
                    value=Cmpd0,
                    options=cmpd_options)
    select_sd = Select(title="Standard Deviation:",
                       value='1',
                       options=['0', '1', '2', '3'])

    def update_title(attrname, old, new):
        p.title.text = select.value

    select.on_change('value', update_title)

    def update_data(attrname, old, new):
        cmpd = select.value
        std = int(select_sd.value)
        MEANs = df_means.groupby(Treatments)[cmpd].apply(list).to_dict()
        STDs = df_stdev.groupby(Treatments)[cmpd].apply(list).to_dict()

        results1 = {'time_vals': time_vals}
        for y in range(len(MEANs)):
            mu = list(MEANs.values())[y]
            sd = list(STDs.values())[y]
            upper = [x + std * e for x, e in zip(mu, sd)]
            lower = [x - std * e for x, e in zip(mu, sd)]
            results1.update({keys[y]: mu})
            results1.update({u_keys[y]: upper})
            results1.update({l_keys[y]: lower})
        source.data = results1

    for w in [select, select_sd]:
        w.on_change('value', update_data)

    # Set up layouts and add to document
    inputs = widgetbox(select, select_sd)
    layout = row(column(inputs), p, width=1000)
    tab = Panel(child=layout, title='Bar Charts')

    return tab
コード例 #22
0
def get_bokeh_std_fullres(plot_data: object,
                          full_res: bool,
                          size: list,
                          label: str = "") -> object:
    """
    Plot with full resolution, hence one line with errorbars, no band

    :param plot_data: dict - pandas dataframe to plot and some additional information
    :param full_res: boolean - True if data is complete else only latest n values
    :param size: list - size of plot
    :param label: str - title for plot
    :return:
    """
    nan_in_data = plot_data['nan_in_data']
    source = ColumnDataSource(plot_data['df'])
    missing_source = ColumnDataSource(plot_data['missing_data'])
    if full_res:
        title = ''
    else:
        title = gettext("Showing only latest {0} datapoints.").format(
            str(max_size_preview_plot))
    # Plot average as main plot
    mainplot = figure(
        x_axis_label='Time',
        x_axis_type="datetime",
        y_axis_label=label,
        title=title,
        # sizing_mode='stretch_both',
        plot_width=size[0],
        plot_height=int(size[1] * 0.9),
        toolbar_location="above",
        tools="pan,wheel_zoom,box_zoom,reset, save",
        active_drag="box_zoom")

    # plot value line
    mainplot.line(x='tstamp', y='value', source=source,
                  line_width=3)  #, legend_label="measured values")

    mainplot.add_tools(
        HoverTool(tooltips=[(gettext("Value"), "@value"),
                            (gettext("Time"), "@tstamp{%T %Z}"),
                            (gettext("Date"), "@tstamp{%d %b %Y}")],
                  formatters={"@tstamp": "datetime"},
                  mode="mouse"))

    # plot white line to hide small band for no data areas
    if nan_in_data:
        mainplot.line(
            x='tstamp',
            y='value',
            line_width=3,
            line_color='red',
            line_cap='round',
            legend_label=gettext('Missing values'),
            source=missing_source,
            # visible=False
        )
        mainplot.legend.click_policy = "hide"
        mis_list = [*range(0, len(plot_data['missing_data']), 4)]
        for i in mis_list:
            box = BoxAnnotation(
                left=plot_data['missing_data']['tstamp'][i + 1],
                right=plot_data['missing_data']['tstamp'][i + 2],
                fill_alpha=0.2,
                fill_color='red')
            mainplot.add_layout(box)

    # plot first average precision to have it in the background
    if plot_data['has_preci']:
        # add errorbars
        mainplot.add_layout(
            Whisker(source=error_source,
                    base="date",
                    upper="upper",
                    lower="lower",
                    line_width=0.5))

    # Style plot
    mainplot.title.text_font_size = "14pt"
    mainplot.xaxis.axis_label_text_font_size = "14pt"
    mainplot.xaxis.formatter = DatetimeTickFormatter(days=["%d %b %Y"],
                                                     months=["%d %b %Y"],
                                                     years=["%d %b %Y"])
    mainplot.yaxis.axis_label_text_font_size = "14pt"
    script, div = components(column(mainplot, sizing_mode="scale_both"),
                             wrap_script=False)
    return {'script': script, 'div': div}
コード例 #23
0
def load_page(experiment_df, experiment_db):

    ########## bokeh plots ##########

    # general plot tools
    plot_tools = 'wheel_zoom, pan, reset, save, hover'
    hover = HoverTool(tooltips=[("(x,y)", "($x, $y)")])

    # progress curve plots
    global raw_source
    raw_source = ColumnDataSource(data=dict(x=[], y=[], yr=[], yfit=[]))
    global raw
    raw = figure(title="Initial Rate Fit",
                 x_axis_label="Time",
                 y_axis_label="Signal",
                 plot_width=350,
                 plot_height=300,
                 tools=plot_tools)
    raw.circle('x',
               'y',
               size=2,
               source=raw_source,
               color='gray',
               selection_color="black",
               alpha=0.6,
               nonselection_alpha=0.2,
               selection_alpha=0.6)
    raw.line('x', 'yfit', source=raw_source, color='red')
    global warning_source
    warning_source = ColumnDataSource(data=dict(
        x=[0],
        y=[0],
        t=[
            'Please enter transform equation! \nMust convert signal to [substrate] \nin Schnell-Mendoza mode (e.g. via \nx/6.22/0.45/0.001 for sample data). \nNote: this transform may need \nto be inverted through multiplying \nby -1 when analyzing experiments \nthat measure increasing product \nconcentration over time)'
        ]))
    global warning
    warning = raw.text(x='x',
                       y='y',
                       text='t',
                       text_font_size='12pt',
                       angle=0,
                       source=warning_source)
    warning.visible = False
    global circles_source
    circles_source = ColumnDataSource(
        data=dict(x=[-.05, -.05, 1.6, 1.6], y=[0, 0.6, 0, 0.6]))
    global circles
    circles = raw.circle(x='x', y='y', alpha=0., source=circles_source)
    circles.visible = False
    global resi
    resi = figure(title="Initial Rate Fit Residuals",
                  x_axis_label="Time",
                  y_axis_label="Residual",
                  plot_width=700,
                  plot_height=200,
                  tools='wheel_zoom,pan,reset')
    resi.yaxis.formatter = BasicTickFormatter(precision=2, use_scientific=True)
    resi.circle('x', 'yr', size=5, source=raw_source, color='grey', alpha=0.6)

    # model plot for titration experiments
    global model_data_source
    model_data_source = ColumnDataSource(
        data=dict(xt=[], yt=[], n=[], ct=[], et=[]))
    global model_plot_source
    model_plot_source = ColumnDataSource(
        data=dict(xp=[], yp=[], l=[], u=[], cp=[], ep=[]))
    global model_fit_source
    model_fit_source = ColumnDataSource(data=dict(x=[], y=[]))
    global varea_source
    varea_source = ColumnDataSource(data=dict(x=[], r1=[], r2=[]))
    global model
    model = figure(title='Model Fit',
                   x_axis_label='Concentration',
                   y_axis_label='Rate',
                   plot_width=350,
                   plot_height=300,
                   tools=plot_tools)
    model.circle('xp',
                 'yp',
                 size=8,
                 source=model_plot_source,
                 color='cp',
                 alpha=0.6)
    model.add_layout(
        Whisker(source=model_plot_source, base='xp', upper='u', lower='l'))
    model.line('x',
               'y',
               source=model_fit_source,
               line_width=3,
               color='black',
               alpha=0.8)
    model.varea('x', 'r1', 'r2', source=varea_source, color='grey', alpha=0.3)

    ########## bokeh widgets ##########

    # button for selecting progress curve fitting routine
    global fit_button
    fit_button = RadioButtonGroup(labels=[
        'Maximize Slope Magnitude', 'Linear Fit', 'Logarithmic Fit',
        'Schnell-Mendoza'
    ],
                                  active=0,
                                  width=375)
    fit_button.on_change('active', widget_callback)

    # button for selecting progress curve fitting routine
    global scalex_box
    scalex_box = CheckboxButtonGroup(
        labels=["transform x-axis to Log10 scale"], active=[])
    scalex_box.on_change('active', widget_callback)

    # dropdown menu for selecting titration experiment model
    global model_select
    model_select = Select(
        title='Choose Model',
        value='Michaelis-Menten',
        options=['Michaelis-Menten', 'pEC50/pIC50', 'High-Throughput Screen'],
        width=350)
    model_select.on_change('value', widget_callback)

    # dropdown menu for selecting blank sample to subtract from remaining titration samples
    global subtract_select
    subtract_select = Select(title='Select Blank Sample for Subtraction',
                             value='',
                             options=list(experiment_df)[1:] + [''],
                             width=350)
    subtract_select.on_change('value', widget_callback)

    # dropdown menu for selecting titration sample to plot in current view
    global sample_select
    sample_select = Select(title='Y Axis Sample',
                           value=list(experiment_df)[-1],
                           options=list(experiment_df)[1:],
                           width=350)
    sample_select.on_change('value', sample_callback)

    # text input box for transforming slopes to rates
    global transform_input
    transform_input = TextInput(value='',
                                title="Enter Transform Equation",
                                width=350)
    transform_input.on_change('value', widget_callback)

    # text input box for setting delay time in logarithmic progress curve fitting
    global offset_input
    offset_input = TextInput(value='',
                             title="Enter Time Between Mixing and First Read",
                             width=350)
    offset_input.on_change('value', widget_callback)

    # text input boxes for fixing EC/IC50 parameters
    global bottom_fix
    bottom_fix = TextInput(value='', title="Fix pIC50/pEC50 Bottom")
    bottom_fix.on_change('value', widget_callback)

    global top_fix
    top_fix = TextInput(value='', title="Fix pIC50/pEC50 Top")
    top_fix.on_change('value', widget_callback)

    global slope_fix
    slope_fix = TextInput(value='', title="Fix pIC50/pEC50 Hill Slope")
    slope_fix.on_change('value', widget_callback)

    # text input boxes for progress curve xrange selection
    global start_time
    start_time = TextInput(value=str(
        experiment_df[list(experiment_df)[0]].values[0]),
                           title="Enter Start Time")
    global end_time
    end_time = TextInput(value=str(
        experiment_df[list(experiment_df)[0]].values[-1]),
                         title='Enter End Time')
    start_time.on_change('value', xbox_callback)
    end_time.on_change('value', xbox_callback)

    # range slider to select threshold for hit detection in HTS mode
    global threshold_slider
    threshold_slider = Slider(start=0,
                              end=5,
                              value=2,
                              step=0.1,
                              title='HTS Hit Threshold (Standard Deviation)',
                              width=350)
    threshold_slider.on_change('value', threshold_callback)

    # range slider to update plots according to progress cuve xrange selection
    xmin = experiment_df[experiment_df.columns[0]].values[0]
    xmax = experiment_df[experiment_df.columns[0]].values[-1]
    global range_slider
    range_slider = RangeSlider(
        start=xmin,
        end=xmax,
        value=(xmin, xmax),
        step=experiment_df[experiment_df.columns[0]].values[1] - xmin,
        title='Fine Tune X-Axis Range',
        width=650)
    range_slider.on_change('value', slider_callback)

    # button to upload local data file
    global file_source
    file_source = ColumnDataSource(data=dict(file_contents=[], file_name=[]))
    file_source.on_change('data', file_callback)
    try:
        output_filename = file_source.data['file_name'] + '-out.csv'
    except:
        output_filename = 'output.csv'
    global upload_button
    upload_button = Button(label="Upload Local File",
                           button_type="success",
                           width=350)
    upload_button.callback = CustomJS(args=dict(file_source=file_source),
                                      code=open(
                                          join(dirname(__file__),
                                               "upload.js")).read())

    # table containing rate fits and errors
    template = """
    <div style="background:<%=ct%>"; color="white";>
    <%= value %></div>
    """
    formatter = HTMLTemplateFormatter(template=template)
    columns = [
        TableColumn(field='n', title='Sample'),
        TableColumn(field='yt',
                    title='Slope (Initial Rate)',
                    formatter=formatter),
        TableColumn(field='et', title='Std. Error')
    ]
    global rate_table
    rate_table = DataTable(source=model_data_source,
                           columns=columns,
                           width=350,
                           height=250,
                           selectable=True,
                           editable=True)

    # tables containing model fits and errors
    global mm_source
    mm_source = ColumnDataSource(dict(label=[], Km=[], Vmax=[]))
    columns = [
        TableColumn(field='label', title=''),
        TableColumn(field='Vmax', title='Vmax'),
        TableColumn(field='Km', title='Km')
    ]
    global mm_table
    mm_table = DataTable(source=mm_source,
                         columns=columns,
                         width=350,
                         height=75,
                         selectable=True,
                         editable=True)
    global ic_source
    ic_source = ColumnDataSource(
        dict(label=[], Bottom=[], Top=[], Slope=[], p50=[]))
    columns = [
        TableColumn(field='label', title=''),
        TableColumn(field='Bottom', title='Bottom'),
        TableColumn(field='Top', title='Top'),
        TableColumn(field='Slope', title='Slope'),
        TableColumn(field='p50', title='pEC/IC50')
    ]
    global ic_table
    ic_table = DataTable(source=ic_source,
                         columns=columns,
                         width=350,
                         height=75,
                         selectable=True,
                         editable=True)

    # button for copying rate data table to clipboard
    global copy_button
    copy_button = Button(label="Copy Table to Clipboard",
                         button_type="primary",
                         width=350)
    copy_button.callback = CustomJS(args=dict(source=model_data_source),
                                    code=open(
                                        join(dirname(__file__),
                                             "copy.js")).read())

    # button for downloading rate data table to local csv file
    global download_button
    download_button = Button(label="Download Table to CSV",
                             button_type="primary",
                             width=350)
    download_button.callback = CustomJS(args=dict(source=model_data_source,
                                                  file_name=output_filename),
                                        code=open(
                                            join(dirname(__file__),
                                                 "download.js")).read())

    ########## document formatting #########

    desc = Div(text=open(join(dirname(__file__), "description.html")).read(),
               width=1400)

    advanced = Div(
        text="""<strong>Advanced Settings for \npEC/IC50 Analysis</strong>""")

    widgets = widgetbox(model_select, sample_select, subtract_select,
                        transform_input, offset_input, advanced, scalex_box,
                        bottom_fix, top_fix, slope_fix)
    table = widgetbox(rate_table)
    main_row = row(
        column(upload_button, widgets),
        column(fit_button, row(raw, model), resi, row(start_time, end_time),
               range_slider),
        column(download_button, copy_button, table, mm_table, ic_table,
               threshold_slider))

    sizing_mode = 'scale_width'
    l = layout([[desc], [main_row]], sizing_mode=sizing_mode)

    update()
    curdoc().clear()
    curdoc().add_root(l)
    curdoc().title = "ICEKAT"
コード例 #24
0
def get_bokeh_standard(plot_data: object,
                       size: list,
                       label: str = "") -> object:
    """

    :param plot_data: pandas dataframe to plot
    :param size:
    :param label:
    :return:
    """
    # plot_data = fill_data_gaps(db_data)

    stepsize = plot_data['stepsize']
    has_precision = plot_data['has_precision']
    nan_in_data = plot_data['nan_in_data']
    source = ColumnDataSource(plot_data['df'])
    error_source = ColumnDataSource(plot_data['error_source'])
    # missing_source = ColumnDataSource(plot_data['missing_data'])

    # Plot average as main plot
    mainplot = figure(title='Daily average, min and max values',
                      x_axis_label='Time',
                      x_axis_type="datetime",
                      y_axis_label=label,
                      plot_width=size[0],
                      plot_height=int(size[1] * 0.9),
                      toolbar_location="above",
                      tools="pan,wheel_zoom,box_zoom,reset, save",
                      active_drag="box_zoom")

    # plot average line
    mainplot.line(x='date',
                  y='y',
                  source=source,
                  line_width=2,
                  legend_label="average",
                  level="overlay")

    # TODO: Figure out how to use 'source' for multi_line.
    #  Maybe use Glyph? (https://docs.bokeh.org/en/latest/docs/reference/models/glyphs/multi_line.html)
    #  Glyphs maybe also helpful for hover_tool on multiline?
    mainplot.add_tools(
        HoverTool(tooltips=[("value", "@y"), ("Date", "@date{%d %b %Y}")],
                  formatters={"@date": "datetime"},
                  mode="mouse"))

    # plot min/max as multiline and fill area with band
    mainplot.multi_line(
        xs=[plot_data['source']['date'], plot_data['source']['date']],
        ys=[plot_data['source']['ymin'], plot_data['source']['ymax']],
        level='underlay',
        color=['lightblue', 'lightblue'],
        legend_label="min & max values")
    mainplot.add_layout(
        Band(base='date',
             lower='ymin',
             upper='ymax',
             source=source,
             level='underlay',
             fill_color='lightblue',
             fill_alpha=0.5))

    # plot white line to hide small band for no data areas
    if nan_in_data:
        mainplot.line(x='tstamp',
                      y='value',
                      source=source,
                      line_width=2,
                      line_color='white',
                      line_cap='round')

    # plot first average precision to have it in the background
    if has_precision:
        # add errorbars
        mainplot.add_layout(
            Whisker(source=error_source,
                    base="date",
                    upper="upper",
                    lower="lower",
                    line_width=0.5))
        mainplot.vbar(x='date',
                      width=1000 * 60 * 59 * 24,
                      top='upper_avg',
                      bottom='lower_avg',
                      source=error_source,
                      fill_color="darksalmon",
                      line_color="black",
                      fill_alpha=0.3,
                      line_width=0.5,
                      legend_label="Average precision")

    # plot bars for the number of values in each group as secondary 'by'plot
    mapper = linear_cmap(field_name='count',
                         palette=Oranges9,
                         low=0,
                         high=plot_data['axis']['y2max'])
    bin_width = stepsize
    distriplot = distribution_plot(source, mapper, bin_width,
                                   'Number of available values per day',
                                   size[0])
    distriplot.x_range = mainplot.x_range

    # Style plot
    mainplot.title.text_font_size = "14pt"
    mainplot.xaxis.axis_label_text_font_size = "14pt"
    mainplot.xaxis.formatter = DatetimeTickFormatter(days=["%d %b %Y"],
                                                     months=["%d %b %Y"],
                                                     years=["%d %b %Y"])
    mainplot.yaxis.axis_label_text_font_size = "14pt"
    mainplot.legend.click_policy = "hide"
    script, div = components(column(distriplot,
                                    mainplot,
                                    sizing_mode="scale_both"),
                             wrap_script=False)
    return {'script': script, 'div': div}
コード例 #25
0
def plot_bokeh(self, filename=None, show=True, savepng=False):
    curdoc().theme = Theme(BOKEH_THEME_FILE)

    if filename is not None:
        if not filename.endswith('.html'):
            filename += '.html'
        else:
            pass
        bkp.output_file(filename)
    else:
        pass

    # Format the date correction
    self.data['utcstr'] = self.data['utc'].apply(lambda x: x.isoformat()[0:19])

    # Put the dataframe in a useable form
    self.data['lower'] = self.data.radiance - self.data.sigma
    self.data['upper'] = self.data.radiance + self.data.sigma
    self.data['lattandeg'] = self.data.lattan * 180 / np.pi

    mask = self.data.alttan != self.data.alttan.max()
    if np.any(mask):
        m = self.data[self.data.alttan != self.data.alttan.max()].alttan.max()
    else:
        m = 1e10
    col = np.interp(self.data.alttan, np.linspace(0, m, 256),
                    np.arange(256)).astype(int)
    self.data['color'] = [Turbo256[c] for c in col]
    source = bkp.ColumnDataSource(self.data)

    # Tools
    tools = [
        'pan', 'box_zoom', 'wheel_zoom', 'xbox_select', 'hover', 'reset',
        'save'
    ]

    # tool tips
    tips = [('index', '$index'), ('UTC', '@utcstr'),
            ('Radiance', '@radiance{0.2f} kR'),
            ('LTtan', '@loctimetan{2.1f} hr'),
            ('Lattan', '@lattandeg{3.1f} deg'), ('Alttan', '@alttan{0.f} km')]

    # Make the figure
    width, height = 1200, 600
    fig0 = bkp.figure(plot_width=width,
                      plot_height=height,
                      x_axis_type='datetime',
                      title=f'{self.species}, {self.query}',
                      x_axis_label='UTC',
                      y_axis_label='Radiance (kR)',
                      y_range=[0, self.data.radiance.max() * 1.5],
                      tools=tools,
                      active_drag="xbox_select")

    # plot the data
    dplot = fig0.circle(x='utc',
                        y='radiance',
                        size=7,
                        color='black',
                        legend_label='Data',
                        hover_color='yellow',
                        source=source,
                        selection_color='orange')
    fig0.line(x='utc',
              y='radiance',
              color='black',
              legend_label='Data',
              source=source)
    fig0.xaxis.ticker = DatetimeTicker(num_minor_ticks=5)

    # Add error bars
    fig0.add_layout(
        Whisker(source=source, base='utc', upper='upper', lower='lower'))
    renderers = [dplot]

    # Plot the model
    col = color_generator()
    modplots, maskedplots = [], []
    for modkey, result in self.model_result.items():
        c = next(col)
        # fig0.line(x='utc', y=modkey, source=source,
        #           legend_label=result.label, color=c)

        maskkey = modkey.replace('model', 'mask')
        mask = (self.data[maskkey]).to_list()
        view = CDSView(source=source, filters=[BooleanFilter(mask)])
        modplots.append(
            fig0.circle(x='utc',
                        y=modkey,
                        size=9,
                        color=c,
                        source=source,
                        legend_label=result.label,
                        view=view))
        maskkey = modkey.replace('model', 'mask')
        mask = np.logical_not(self.data[maskkey]).to_list()
        view = CDSView(source=source, filters=[BooleanFilter(mask)])
        maskedplots.append(
            fig0.circle(x='utc',
                        y=modkey,
                        size=9,
                        source=source,
                        line_color=c,
                        fill_color='yellow',
                        view=view,
                        legend_label=result.label + ' (Data Point Not Used)'))
        renderers.extend(modplots)
        renderers.extend(maskedplots)

    datahover = HoverTool(tooltips=tips, renderers=renderers)
    fig0.add_tools(datahover)

    ##############
    # Plot tangent point
    color_mapper = LinearColorMapper(palette="Turbo256", low=0, high=m)

    width, height = 1200, 600
    tools = [
        'pan', 'box_zoom', 'wheel_zoom', 'box_select', 'hover', 'reset', 'save'
    ]
    fig1 = bkp.figure(plot_width=width,
                      plot_height=height,
                      title=f'Tangent Point Location',
                      x_axis_label='Local Time (hr)',
                      y_axis_label='Latitude (deg)',
                      x_range=[0, 24],
                      y_range=[-90, 90],
                      tools=tools,
                      active_drag="box_select")
    tanplot = fig1.circle(x='loctimetan',
                          y='lattandeg',
                          size=5,
                          selection_color='orange',
                          hover_color='purple',
                          source=source,
                          color='color')
    fig1.xaxis.ticker = SingleIntervalTicker(interval=6, num_minor_ticks=6)
    fig1.yaxis.ticker = SingleIntervalTicker(interval=45, num_minor_ticks=3)
    color_bar = ColorBar(color_mapper=color_mapper,
                         title='Altitude (km)',
                         label_standoff=12,
                         border_line_color=None,
                         location=(0, 0))
    fig1.add_layout(color_bar, 'right')
    datahover = HoverTool(tooltips=tips, renderers=[tanplot])
    fig1.add_tools(datahover)

    grid = column(fig0, fig1)

    if filename is not None:
        bkp.output_file(filename)
        if savepng:
            export_png(grid, filename=filename.replace('.html', '.png'))
        else:
            pass
        bkp.save(grid)
    else:
        pass

    if show:
        bkp.show(grid)

    return fig0, fig1
コード例 #26
0
var = ['fok','log_novelty','log_cit_10','bt','fail']
varnames = ['Field-original knowledge','New comb. (log)','Fwd cit 10y (log)','Breakthrough rate','Failure rate']
plot_size = [(500,200),(500,200),(round(1000/3),200),(round(1000/3),200),(round(1000/3),200)]
color = palette[3:]
plots = []
for i in range(5):
    plots.append(figure(title=varnames[i],
                        x_axis_label='Year',
                        plot_width=plot_size[i][0],
                        plot_height=plot_size[i][1]))
    plots[i].y_range=Range1d(source_counts_class_pyear.data['{}_low'.format(var[i])].min()*.98,
                             source_counts_class_pyear.data['{}_hi'.format(var[i])].max()*1.02)
    plots[i].line(x='pyear', y=var[i],
                  source=source_counts_class_pyear,
                  color=color[i])
    w = Whisker(source=source_counts_class_pyear
                base="pyear", upper="{}_hi".format(var[i]),
                lower="{}_low".format(var[i]),
                level="overlay",
                line_color=color[i])
    w.upper_head.line_color = color[i]
    w.lower_head.line_color = color[i]
    plots[i].add_layout(w)    
    plots[i].add_tools(HoverTool(tooltips=[('Year','@pyear'),(varnames[i], '@{}'.format(var[i]))],
                                 mode='vline'))
fig_1_b = plots[0]
fig_1_c = plots[1]
fig_1_d = plots[2]
fig_1_e = plots[3]
fig_1_f = plots[4]
for fig in [fig_1_c, fig_1_d, fig_1_e, fig_1_f]:
コード例 #27
0
    y_ = rand_jitter(y)
    s2.step(x_, y_, color="firebrick", alpha=0.5)

y = np.clip(y, a_min=0, a_max=0.5)
for _ in range(int(num * (1 / 3))):
    x_ = rand_jitter(x)
    y_ = rand_jitter(y)
    s2.step(x_, y_, color="firebrick", alpha=0.5)

# "heatmap" for V
s3 = figure(plot_width=250, plot_height=250, title=None)
x = list(np.linspace(0, 1, 10)) * 10
y = [[i] * 10 for i in np.linspace(0, 1, 10)]
y = [a for b in y for a in b]

s3.add_layout(Whisker(base=0.5, upper=0.9, lower=0.1))

# create and another
s4 = figure(plot_width=250, plot_height=250, title=None)
y = np.linspace(0.2, 0.8, 5)
right = np.array([0.8, 0.1, 0.8, 0.1, 0.8])
src = ColumnDataSource(dict(y=y, right=right))
s4.add_glyph(
    src, HBar(y="y", right="right", left=0, height=0.1, fill_color="#b3de69"))

for s in [s1, s2, s3, s4]:
    s.axis.visible = False
    s.toolbar.logo = None
    s.toolbar_location = None
    s.x_range = Range1d(0, 1)
    s.y_range = Range1d(0, 1)
コード例 #28
0
def make_fitted_plot(self,
                     result,
                     filestart='fitted',
                     show=True,
                     ut=None,
                     smooth=False,
                     savepng=False):
    curdoc().theme = Theme(BOKEH_THEME_FILE)

    if smooth:
        kernel = Gaussian2DKernel(x_stddev=1)
        source = convolve(result['abundance'], kernel, boundary='wrap')
        packets = convolve(result['p_available'], kernel, boundary='wrap')
    else:
        source = result['abundance']
        packets = result['p_available']

    # Tools
    tools = ['save']

    local_time = (result['longitude'].value * 12 / np.pi + 12) % 24
    arg = np.argsort(local_time[:-1])
    source, packets = source[arg, :], packets[arg, :]

    # Distribution of available packets
    fig0 = bkp.figure(plot_width=WIDTH,
                      plot_height=HEIGHT,
                      title=f'{self.species}, {self.query}, Available Packets',
                      x_axis_label='Local Time (hr)',
                      y_axis_label='Latitude (deg)',
                      x_range=[0, 24],
                      y_range=[-90, 90],
                      tools=tools)
    fig0.title.text_font_size = FONTSIZE
    fig0.xaxis.axis_label_text_font_size = FONTSIZE
    fig0.yaxis.axis_label_text_font_size = FONTSIZE
    fig0.xaxis.major_label_text_font_size = NUMFONTSIZE
    fig0.yaxis.major_label_text_font_size = NUMFONTSIZE
    fig0.xaxis.ticker = FixedTicker(ticks=[0, 6, 12, 18, 24])
    fig0.yaxis.ticker = FixedTicker(ticks=[-90, 45, 0, 45, 90])

    fig0.image(image=[packets.transpose()],
               x=0,
               y=-90,
               dw=24,
               dh=180,
               palette='Spectral11')

    # Distribution of packets used in the final model
    fig1 = bkp.figure(plot_width=WIDTH,
                      plot_height=HEIGHT,
                      title=f'{self.species}, {self.query}, Packets Used',
                      x_axis_label='Local Time (hr)',
                      y_axis_label='Latitude (deg)',
                      x_range=[0, 24],
                      y_range=[-90, 90],
                      tools=tools)
    fig1.title.text_font_size = FONTSIZE
    fig1.xaxis.axis_label_text_font_size = FONTSIZE
    fig1.yaxis.axis_label_text_font_size = FONTSIZE
    fig1.xaxis.major_label_text_font_size = NUMFONTSIZE
    fig1.yaxis.major_label_text_font_size = NUMFONTSIZE
    fig1.xaxis.ticker = FixedTicker(ticks=[0, 6, 12, 18, 24])
    fig1.yaxis.ticker = FixedTicker(ticks=[-90, 45, 0, 45, 90])

    fig1.image(image=[source.transpose()],
               x=0,
               y=-90,
               dw=24,
               dh=180,
               palette='Spectral11')

    fig2 = bkp.figure(
        plot_width=WIDTH,
        plot_height=HEIGHT,
        title=f'{self.species}, {self.query}, Speed Distribution',
        x_axis_label='Speed (km/s)',
        y_axis_label='Relative Number',
        y_range=[0, 1.2],
        tools=tools)
    fig2.title.text_font_size = FONTSIZE
    fig2.xaxis.axis_label_text_font_size = FONTSIZE
    fig2.yaxis.axis_label_text_font_size = FONTSIZE
    fig2.xaxis.major_label_text_font_size = NUMFONTSIZE
    fig2.yaxis.major_label_text_font_size = NUMFONTSIZE

    fig2.line(x=result['velocity'][:-1],
              y=result['v_available'],
              legend_label='Packets Available',
              color='red')
    fig2.line(x=result['velocity'][:-1],
              y=result['vdist'],
              legend_label='Packets Used',
              color='blue')

    # Full orbit time series
    # Format the date correction
    self.data['utcstr'] = self.data['utc'].apply(lambda x: x.isoformat()[0:19])

    # Put the dataframe in a useable form
    self.data['lower'] = self.data.radiance - self.data.sigma
    self.data['upper'] = self.data.radiance + self.data.sigma
    self.data['lattandeg'] = self.data.lattan * 180 / np.pi

    m = self.data[self.data.alttan != self.data.alttan.max()].alttan.max()
    col = np.interp(self.data.alttan, np.linspace(0, m, 256),
                    np.arange(256)).astype(int)
    self.data['color'] = [Turbo256[c] for c in col]
    source = bkp.ColumnDataSource(self.data)

    # Tools
    tools = [
        'pan', 'box_zoom', 'wheel_zoom', 'xbox_select', 'hover', 'reset',
        'save'
    ]

    # tool tips
    tips = [('index', '$index'), ('UTC', '@utcstr'),
            ('Radiance', '@radiance{0.2f} kR'),
            ('LTtan', '@loctimetan{2.1f} hr'),
            ('Lattan', '@lattandeg{3.1f} deg'), ('Alttan', '@alttan{0.f} km')]

    # Make the radiance figure
    title_ = f'{self.species}, {self.query}'
    if ut is not None:
        title_ += f', UTC = {ut.isoformat()}'
    else:
        pass

    fig3 = bkp.figure(plot_width=WIDTH,
                      plot_height=HEIGHT,
                      x_axis_type='datetime',
                      title=title_,
                      x_axis_label='UTC',
                      y_axis_label='Radiance (kR)',
                      y_range=[0, self.data.radiance.max() * 1.5],
                      tools=tools,
                      active_drag="xbox_select")
    fig3.title.text_font_size = FONTSIZE
    fig3.xaxis.axis_label_text_font_size = FONTSIZE
    fig3.yaxis.axis_label_text_font_size = FONTSIZE
    fig3.xaxis.major_label_text_font_size = NUMFONTSIZE
    fig3.yaxis.major_label_text_font_size = NUMFONTSIZE

    # plot the data
    dplot = fig3.circle(x='utc',
                        y='radiance',
                        size=7,
                        color='black',
                        legend_label='Data',
                        hover_color='yellow',
                        source=source,
                        selection_color='orange')
    fig3.line(x='utc',
              y='radiance',
              color='black',
              legend_label='Data',
              source=source)
    fig3.xaxis.ticker = DatetimeTicker(num_minor_ticks=5)

    # Add error bars
    fig3.add_layout(
        Whisker(source=source, base='utc', upper='upper', lower='lower'))
    renderers = [dplot]

    # Plot the model
    col = color_generator()
    modplots, maskedplots = [], []
    for modkey, result in self.model_result.items():
        if result.fitted:
            c = next(col)
            fig3.line(x='utc',
                      y=modkey,
                      source=source,
                      legend_label=result.label,
                      color=c)

            maskkey = modkey.replace('model', 'mask')
            mask = self.data[maskkey].to_list()
            view = CDSView(source=source, filters=[BooleanFilter(mask)])
            modplots.append(
                fig3.circle(x='utc',
                            y=modkey,
                            size=7,
                            color=c,
                            source=source,
                            legend_label=result.label,
                            view=view))

            maskkey = modkey.replace('model', 'mask')
            mask = np.logical_not(self.data[maskkey]).to_list()
            view = CDSView(source=source, filters=[BooleanFilter(mask)])
            maskedplots.append(
                fig3.circle(x='utc',
                            y=modkey,
                            size=7,
                            source=source,
                            line_color=c,
                            fill_color='yellow',
                            view=view,
                            legend_label=result.label +
                            '(Data Point Not Used)'))
            renderers.extend(modplots)
            renderers.extend(maskedplots)

        if ut is not None:
            yr = fig3.y_range
            fig3.line(x=[ut, ut], y=[0, 1e5], color='purple')
            fig3.y_range = yr
        else:
            pass

    datahover = HoverTool(tooltips=tips, renderers=renderers)
    fig3.add_tools(datahover)

    grid = gridplot([[fig3, fig2], [fig0, fig1]])

    # Save png version
    if savepng:
        export_png(grid, filename=filestart + '.png')
    else:
        pass

    bkp.output_file(filestart + '.html')
    bkp.save(grid)  # html files not needed

    if show:
        bkp.show(grid)
    else:
        pass

    return grid
コード例 #29
0
ファイル: whisker.py プロジェクト: zmj2008/bokeh
            y_range=y_range,
            toolbar_location=None)

source = ColumnDataSource(data=dict(
    x1=[1, 3, 5, 7, 9],
    lower1=[1, 2, 1, 2, 1],
    upper1=[2, 3, 2, 3, 2],
    x2=[200, 250, 350, 450, 550],
    lower2=[400, 300, 400, 300, 400],
    upper2=[500, 400, 500, 400, 500],
))

whisker1 = Whisker(base='x1',
                   lower='lower1',
                   upper='upper1',
                   line_width=3,
                   line_color='red',
                   line_dash='dashed',
                   source=source)

whisker2 = Whisker(base='x2',
                   lower='lower2',
                   upper='upper2',
                   upper_head=OpenHead(),
                   base_units='screen',
                   lower_units='screen',
                   upper_units='screen',
                   dimension='width',
                   line_width=3,
                   line_color='green',
                   source=source)
コード例 #30
0
def learning_curve():
    source = ColumnDataSource(
        data=dict(TSS=Total_Sample_Size,
                  NBC_training_acc=nbc_training_accuracies_avg,
                  LR_training_acc=lr_training_accuracies_avg,
                  SVM_training_acc=svm_training_accuracies_avg,
                  NBC_testing_acc=nbc_testing_accuracies_avg,
                  LR_testing_acc=lr_testing_accuracies_avg,
                  SVM_testing_acc=svm_testing_accuracies_avg,
                  NBC_lower=NBC_lower,
                  NBC_upper=NBC_upper,
                  LR_lower=LR_lower,
                  LR_upper=LR_upper,
                  SVM_lower=SVM_lower,
                  SVM_upper=SVM_upper))

    p = figure(title="Model Evaluation")

    p.circle(x='TSS',
             y='NBC_testing_acc',
             source=source,
             size=9,
             legend="NBC",
             alpha=0.6)
    p.line(x='TSS', y='NBC_testing_acc', source=source, legend="NBC")

    p.circle(x='TSS',
             y='LR_testing_acc',
             fill_color=None,
             line_color="red",
             source=source,
             size=9,
             legend="LR")
    p.line(x='TSS',
           y='LR_testing_acc',
           line_color="red",
           line_width=2,
           source=source,
           legend="LR")

    p.square(x='TSS',
             y='SVM_testing_acc',
             fill_color="green",
             line_color="green",
             source=source,
             size=9,
             legend="SVM",
             alpha=0.6)
    p.line(x='TSS',
           y='SVM_testing_acc',
           line_color="green",
           source=source,
           legend="SVM")

    p.xaxis.axis_label = 'Total Training Data Size'
    p.yaxis.axis_label = 'Accuracy'

    w1 = Whisker(source=source,
                 base='TSS',
                 upper='NBC_upper',
                 lower='NBC_lower',
                 line_color='blue')
    w1.upper_head.line_color = 'blue'
    w1.lower_head.line_color = 'blue'
    p.add_layout(w1)
    w2 = Whisker(source=source,
                 base='TSS',
                 upper='LR_upper',
                 lower='LR_lower',
                 line_color='red')
    w2.upper_head.line_color = 'red'
    w2.lower_head.line_color = 'red'
    p.add_layout(w2)
    w3 = Whisker(source=source,
                 base='TSS',
                 upper='SVM_upper',
                 lower='SVM_lower',
                 line_color='green')
    w3.upper_head.line_color = 'green'
    w3.lower_head.line_color = 'green'
    p.add_layout(w3)
    p.legend.location = "top_center"
    show(p)