Example #1
0
    def pointsApp(doc):

        radii = [str(x) for x in df.Radius.unique()]

        # Create Input controls
        number = Slider(title="Value Cutoff",
                        value=0,
                        start=0,
                        end=4,
                        step=0.1,
                        width=150)

        residue = TextInput(title="Residue name (3 letter code):", width=200)

        gpcr = Select(title="Proteins",
                      value=list(df.Protein.unique())[0],
                      options=list(df.Protein.unique()),
                      width=100)

        lipid = Select(title="Lipids",
                       value=list(df.Lipids.unique())[0],
                       options=list(df.Lipids.unique()),
                       width=100)

        radius = Select(title="Radius",
                        value=radii[-1],
                        options=radii,
                        width=100)

        options = list(df.columns)[:-5] + ['ResID']
        x_axis = Select(title="X Axis",
                        options=options,
                        value="ResID",
                        width=150)
        y_axis = Select(title="Y Axis",
                        options=options,
                        value=options[0],
                        width=150)

        cc_colors = [
            x for x in cc.all_original_names()
            if x.startswith('linear') or x.startswith('rainbow')
        ]
        cmap = Select(title="Colormap",
                      options=cc_colors,
                      value='linear_kryw_0_100_c71',
                      width=150)

        # Create Column Data Source that will be used by the plot
        source = ColumnDataSource(
            data=dict(x=[], y=[], ResName=[], ResID=[], Protein=[]))

        TOOLTIPS = [("ResName", "@ResName"), ("ResID", "@ResID"),
                    ("Value", "@y")]

        mapper = linear_cmap(
            field_name='y',
            palette=cc.CET_L19,
            low=df[df.Protein == gpcr.value][y_axis.value].min(),
            high=df[df.Protein == gpcr.value][y_axis.value].max())

        # p = figure(plot_height=400, plot_width=800, title="", toolbar_location="right", tooltips=TOOLTIPS)
        p = figure(tooltips=TOOLTIPS, )

        global c
        c = p.circle(x="x",
                     y="y",
                     source=source,
                     line_color='black',
                     fill_color=mapper,
                     **kwargs)

        p.toolbar.autohide = True
        p.axis.axis_label_text_font_size = "12pt"
        p.axis.axis_label_text_font_style = "bold"
        p.title.align = 'center'

        def update(df):
            y_value = y_axis.value
            x_value = x_axis.value

            df = df[(df[y_value] >= number.value)
                    & (df['Protein'] == gpcr.value) &
                    (df['Lipids'] == lipid.value) &
                    (df['Radius'] == float(radius.value))]
            if (residue.value != ""):
                df = df[df.ResName.str.contains(residue.value.upper()) == True]

            # x_value = x_value
            # y_value = y_value

            mapper = linear_cmap(
                field_name='y',
                palette=cc.palette[cmap.value],
                low=df[df.Protein == gpcr.value][y_value].min(),
                high=df[df.Protein == gpcr.value][y_value].max())

            c.glyph.fill_color = mapper

            p.xaxis.axis_label = x_value
            p.yaxis.axis_label = y_value
            p.title.text = "Showing %d Data Points  " % len(df)

            source.data = dict(
                x=df[x_value],
                y=df[y_value],
                ResName=df["ResName"],
                ResID=df["ResID"],
                Protein=df["Protein"],
            )

        controls = [number, gpcr, lipid, radius, y_axis, x_axis, residue, cmap]
        for control in controls:
            control.on_change('value', lambda attr, old, new: update(df))

        sizing_mode = 'scale_width'

        inputs = row(*controls, sizing_mode=sizing_mode)
        inputs2 = row([gpcr, lipid, radius, residue], sizing_mode=sizing_mode)
        inputs3 = row([number, x_axis, y_axis, cmap], sizing_mode=sizing_mode)

        layout1 = layout([[inputs2]], sizing_mode=sizing_mode)
        layout2 = layout([p], sizing_mode=sizing_mode)
        layout3 = layout([inputs3], sizing_mode="scale_width")

        update(df)

        doc.add_root(layout1)
        doc.add_root(layout2)
        doc.add_root(layout3)
        doc.title = "Scatter Application"
        doc.theme = Theme(json=yaml.load("""
            attrs:
                Figure:
                    toolbar_location: above
                    height: 500
                    width: 800
                Grid:
                    grid_line_dash: [6, 4]
                    grid_line_color: black
        """,
                                         Loader=yaml.FullLoader))
Example #2
0
def scatter_handler(doc: Document) -> None:
    """
    Handler function for the scatter application.
    """

    # Load data
    task_id = doc.session_context.request.arguments.get('task_id')
    username = doc.session_context.request.arguments.get('username')
    user_dir = os.path.join(settings.USER_DATA, username)
    path_to_db = os.path.join(user_dir, task_id)
    conn = os.path.join(path_to_db, task_id + '.csv')
    df = pd.read_csv(conn)

    for col in df.columns:
        if col.endswith('Error'):
            if df[col].max() == 0:
                del df[col]

    # widgets
    radii = [str(x) for x in df.Radius.unique()]
    number = Slider(title="Value Cutoff", value=0, start=0, end=4, step=0.1, width=150)
    residue = TextInput(title="Residue name (3 letter code):", width=200)
    gpcr = Select(title="Proteins", value=list(df.Protein.unique())[0],
                options=list(df.Protein.unique()), width=100)
    lipid = Select(title="Lipids", value=list(df.Lipids.unique())[0],
                options=list(df.Lipids.unique()), width=100)
    radius = Select(title="Radius", value=radii[-1], options=radii, width=100)
    options = list(df.columns)[:-5] + ['ResID']
    x_axis = Select(title="X Axis", options=options, value="ResID", width=150)
    y_axis = Select(title="Y Axis", options=options, value=options[0], width=150)

    # colors and plotting
    cc_colors = [x for x in cc.all_original_names() if x.startswith('linear') or x.startswith('rainbow')]
    cmap = Select(title="Colormap", options=cc_colors, value='linear_kryw_0_100_c71', width=150)
    # Create Column Data Source that will be used by the plot
    source = ColumnDataSource(data=dict(x=[], y=[], ResName=[], ResID=[], Protein=[]))
    TOOLTIPS=[
        ("ResName", "@ResName"),
        ("ResID", "@ResID"),
        ("Value", "@y")
    ]
    point_color_mapper = linear_cmap(field_name='y', palette=cc.CET_L19,
                        low=df[df.Protein == gpcr.value][y_axis.value].min(),
                        high=df[df.Protein == gpcr.value][y_axis.value].max())
    p = figure(plot_height=400, plot_width=800, tooltips=TOOLTIPS,)

    circle_plot = p.circle(x="x", y="y", source=source, line_color='black', fill_color=point_color_mapper, size=7)
    circle_object = {'circle': circle_plot}

    # make plot pretty
    p.toolbar.autohide = True
    p.axis.axis_label_text_font_size = "12pt"
    p.axis.axis_label_text_font_style = "bold"
    p.title.align = 'center'

    def update(df):
        y_value = y_axis.value
        x_value = x_axis.value

        df = df[
            (df[y_value] >= number.value) &
            (df['Protein'] == gpcr.value) &
            (df['Lipids'] == lipid.value) &
            (df['Radius'] == float(radius.value))
        ]
        if (residue.value != ""):
            df = df[df.ResName.str.contains(residue.value.upper())==True]

        point_color_mapper = linear_cmap(field_name='y', palette=cc.palette[cmap.value],
                            low=df[df.Protein == gpcr.value][y_value].min(),
                            high=df[df.Protein == gpcr.value][y_value].max())

        circle_object['circle'].glyph.fill_color = point_color_mapper

        p.xaxis.axis_label = x_value
        p.yaxis.axis_label = y_value
        p.title.text = "Showing %d Data Points  " % len(df)

        source.data = dict(
            x=df[x_value],
            y=df[y_value],
            ResName=df["ResName"],
            ResID=df["ResID"],
            Protein=df["Protein"],
        )

    # add controls
    controls = [number, gpcr, lipid, radius, y_axis, x_axis, residue, cmap]
    for control in controls:
        control.on_change('value', lambda attr, old, new: update(df))

    # build layout
    # TODO: this should be made easier to read
    sizing_mode = 'scale_width'
    inputs2 = row([gpcr, lipid, radius, residue], sizing_mode=sizing_mode)
    inputs3 = row([number, x_axis, y_axis, cmap], sizing_mode=sizing_mode)
    layout1 = layout([[inputs2]], sizing_mode=sizing_mode)
    layout2 = layout([p], sizing_mode=sizing_mode)
    layout3 = layout([inputs3], sizing_mode="scale_width")

    # render
    update(df)
    doc.add_root(layout1)
    doc.add_root(layout2)
    doc.add_root(layout3)
    doc.title = "Point Application"
import colorcet as cc
from colorcet.plotting import swatches, sine_combs

import holoviews as hv
import panel as pn

hv.extension('bokeh')

diverging_n = cc.all_original_names(group='diverging', only_aliased=True)
linear_n = cc.all_original_names(group='linear',
                                 not_group='diverging',
                                 only_aliased=True)
cat_n = cc.all_original_names(group='glasbey', only_aliased=True)
misc_n = sorted(
    [k for k in cc.aliases if k not in cat_n + diverging_n + linear_n])

diverging_col = pn.Column(
    '#Diverging',
    sine_combs(*diverging_n, width=400, height=150).opts(toolbar=None))
linear_col = pn.Column(
    '#Linear',
    sine_combs(*linear_n, width=400, height=150).opts(toolbar=None))
cat_col = pn.Column(
    '#Categorical',
    swatches(*cat_n, width=400, height=150, cols=1).opts(toolbar=None))
misc_col = pn.Column(
    '#Misc',
    sine_combs(*misc_n, width=400, height=150).opts(toolbar=None))

all_named = pn.Row(
    linear_col, pn.Spacer(width=100),
Example #4
0
def test_bokeh_palette_glasbey_do_not_start_with_bw():
    for name in cc.all_original_names(group='glasbey'):
        cmap = cc.palette[name]
        assert isinstance(cmap, list)
        assert len(cmap) == 256
        assert {cmap[0], cmap[1]} != {'#00000', '#ffffff'}
def test_all_original_names_nopic_and_only_aliased():
    assert len(cc.all_original_names(group='nopic', only_aliased=True)) == 2
def test_all_original_names_not_glasbey():
    assert len(cc.all_original_names(not_group='glasbey')) == 71
def test_all_original_names_nopic():
    assert len(cc.all_original_names(group='nopic')) == 10
def test_all_original_names_only_aliased():
    assert len(cc.all_original_names(only_aliased=True)) == 28
def test_all_original_names():
    assert len(cc.all_original_names()) == 79