def test_gaussian_pointytop(self, pd):
        bins = buh.hexbin(x, y, 2)
        assert list(bins.q) == [0, 0, 1, 1, 1, 2, 2]
        assert list(bins.r) == [-1, 0, -2, -1, 0, -2, -1]
        assert list(bins.counts) == [9, 54, 1, 313, 98, 3, 22]

        assert bins.equals(buh.hexbin(x, y, 2, "pointytop"))
Exemple #2
0
    def test_gaussian_pointytop(self):
        bins = buh.hexbin(x, y, 2)
        assert list(bins.q) == [0,0,1,1,1,2,2]
        assert list(bins.r) == [-1,0,-2,-1,0,-2,-1]
        assert list(bins.counts) == [9,54,1,313,98,3,22]

        assert bins.equals(buh.hexbin(x, y, 2, "pointytop"))
Exemple #3
0
def update():
    """
    update the scatter plot
    """
    df = select_subset()
    bins = hexbin(df.x, df.y, 0.08)
    source.data = dict(x=bins["q"], y=bins["r"], counts=bins["counts"])
Exemple #4
0
def hexBinPlot():
    p = figure(
        title="Hex Bin Plot",
        sizing_mode="fixed",
        plot_width=400,
        plot_height=400,
        tools="wheel_zoom,reset",
        match_aspect=True,
        background_fill_color="#440154",
    )

    n = 50000
    x = np.random.standard_normal(n)
    y = np.random.standard_normal(n)

    bins = hexbin(x, y, 0.1)

    p.grid.visible = False

    p.hex_tile(
        q="q",
        r="r",
        size=0.1,
        line_color=None,
        source=bins,
        fill_color=linear_cmap("counts", "Viridis256", 0, max(bins.counts)),
    )
    return p
Exemple #5
0
def hexmap(x,
           y,
           name=None,
           log=False,
           match_aspect=True,
           kernel_size=0.1,
           xlim=(None, None),
           ylim=(None, None),
           **kwargs):
    """Create a heatmap from two columns.

    :param x: x-axis coordinates.
    :param y: y-axis coordinates.
    :param name: title for z-axis.
    :param kwargs: kwargs for bokeh figure.

    :returns: a bokeh plot.

    """
    bins = hexbin(x, y, kernel_size)

    defaults = {
        "output_backend": "webgl",
        "height": 400,
        "width": 500,
        "background_fill_color": "#440154"
    }
    defaults.update(kwargs)
    p = figure(**defaults)

    cmap = log_cmap if log else linear_cmap
    mapper = cmap('counts', 'Viridis256', 0, max(bins.counts))
    p.hex_tile(q="q",
               r="r",
               size=kernel_size,
               line_color=None,
               source=bins,
               fill_color=mapper)

    color_bar = ColorBar(title="observations",
                         color_mapper=mapper['transform'],
                         label_standoff=10,
                         location=(0, 0))
    p.add_layout(color_bar, 'right')
    p.grid.visible = False

    x_lim = util.Limiter().accumulate(x).fix(*xlim)
    y_lim = util.Limiter().accumulate(y).fix(*ylim)
    # limit the display range
    p.x_range = Range1d(start=x_lim.min,
                        end=x_lim.max,
                        bounds=(x_lim.min, x_lim.max))
    p.y_range = Range1d(start=y_lim.min,
                        end=y_lim.max,
                        bounds=(y_lim.min, y_lim.max))
    return p, bins
Exemple #6
0
def update(x, y, x_hex, y_hex):
    source.stream(dict(x=[x], y=[y]))
    bin_size = 0.15
    bins = hexbin(x_hex, y_hex, bin_size)
    l_hex = p.hex_tile(
        q="q",
        r="r",
        size=0.1,
        line_color=None,
        source=bins,
        fill_color=linear_cmap("counts", "Viridis256", 0, max(bins.counts)),
    )
Exemple #7
0
def get_global_fpeak_fint_over_log_snr():
    """Get global survey data for fpeak/fint vs log(snr) plot.

    This function computes bokeh.util.hex.hexbins for all galaxies in the catalog,
    where the x axis is galaxies.f_peak/galaxies.f_int, and the y axis is log10(snr),
    with snr=(galaxies.fpeak - 0.25)/galaxies.rms

    Returns
    -------
    hexbins:
        bins to be plotted by bokeh.
    """
    # getting pickle files location
    fpeak_fint_pickle = settings.MEDIA_ROOT + fpeak_fint_pickle_name
    log_snr_pickle = settings.MEDIA_ROOT + log_snr_pickle_name

    # checking pickle exists? if yes, loading from the file
    if os.path.isfile(fpeak_fint_pickle) and os.path.isfile(log_snr_pickle):
        with open(fpeak_fint_pickle, 'rb') as fpeak_fint_pickle_in:
            fpeak_fint = pickle.load(fpeak_fint_pickle_in)

        with open(log_snr_pickle, 'rb') as log_snr_pickle_in:
            log_snr = pickle.load(log_snr_pickle_in)

    else:  # otherwise reading from database and saving in the pickle files
        # cleaning up pickle files if exists (can only happen if one is missing)
        if os.path.isfile(fpeak_fint_pickle):
            os.remove(fpeak_fint_pickle)

        if os.path.isfile(log_snr_pickle):
            os.remove(log_snr_pickle)

        galaxies = Galaxy.objects.all()

        fpeak_fint = []
        log_snr = []

        for galaxy in galaxies:
            fpeak_fint.append(galaxy.fpeak / galaxy.fint)
            log_snr.append(np.log10((galaxy.fpeak - 0.25) / galaxy.rms))

        with open(fpeak_fint_pickle, "wb") as fpeak_fint_pickle_out:
            pickle.dump(fpeak_fint, fpeak_fint_pickle_out)

        with open(log_snr_pickle, "wb") as log_snr_pickle_out:
            pickle.dump(log_snr, log_snr_pickle_out)

    return hexbin(np.array(log_snr), np.array(fpeak_fint), 0.1)
Exemple #8
0
def sort_all_bins_freq(shots):

    basic_zones = [
        'Above the Break 3', 'Above the Break 3', 'Above the Break 3',
        'Above the Break 3', 'Backcourt', 'In The Paint (Non-RA)',
        'In The Paint (Non-RA)', 'In The Paint (Non-RA)',
        'In The Paint (Non-RA)', 'Left Corner 3', 'Mid-Range', 'Mid-Range',
        'Mid-Range', 'Mid-Range', 'Mid-Range', 'Mid-Range', 'Mid-Range',
        'Mid-Range', 'Restricted Area', 'Right Corner 3'
    ]
    zone_areas = [
        'Back Court(BC)', 'Center(C)', 'Left Side Center(LC)',
        'Right Side Center(RC)', 'Back Court(BC)', 'Center(C)', 'Center(C)',
        'Left Side(L)', 'Right Side(R)', 'Left Side(L)', 'Center(C)',
        'Center(C)', 'Left Side Center(LC)', 'Left Side(L)', 'Left Side(L)',
        'Right Side Center(RC)', 'Right Side(R)', 'Right Side(R)', 'Center(C)',
        'Right Side(R)'
    ]
    ranges = [
        'Back Court Shot', '24+ ft.', '24+ ft.', '24+ ft.', 'Back Court Shot',
        '8-16 ft.', 'Less Than 8 ft.', '8-16 ft.', '8-16 ft.', '24+ ft.',
        '8-16 ft.', '16-24 ft.', '16-24 ft.', '16-24 ft.', '8-16 ft.',
        '16-24 ft.', '16-24 ft.', '8-16 ft.', 'Less Than 8 ft.', '24+ ft.'
    ]

    list_of_bins = []

    for i in range(len(ranges)):
        if i == 0 or i == 4:
            pass
        else:
            xs, ys, freq = sort_hex_bin_freq(shots, zone_areas[i], ranges[i],
                                             basic_zones[i])
            bin = hexbin(np.array(xs), np.array(ys), 7.5)
            if len(bin) == 0:
                bin['counts'] = 0
            else:
                bin['counts'] = freq.iloc[0]
            list_of_bins.append(bin)

    all_bins = pd.concat(list_of_bins, ignore_index=True)

    return all_bins
Exemple #9
0
def hexbin_viz(
    df: pd.DataFrame,
    x: str,
    y: str,
    plot_width: int,
    plot_height: int,
    tile_size: Optional[float] = None,
) -> Panel:
    """
    Render a hexbin plot
    """
    # pylint: disable=too-many-arguments,too-many-locals
    xmin, xmax = df[x].min(), df[x].max()
    ymin, ymax = df[y].min(), df[y].max()
    if tile_size is None:
        tile_size = (xmax - xmin) / 25
    title = f"{y} by {x}"
    aspect_scale = (ymax - ymin) / (xmax - xmin)
    bins = hexbin(
        x=df[x],
        y=df[y],
        size=tile_size,
        orientation="flattop",
        aspect_scale=aspect_scale,
    )
    fig = figure(
        title=title,
        tools=[],
        match_aspect=False,
        background_fill_color="#f5f5f5",
        toolbar_location=None,
        plot_width=plot_width,
        plot_height=plot_height,
    )

    palette = list(reversed(viridis(256)))
    rend = fig.hex_tile(
        q="q",
        r="r",
        size=tile_size,
        line_color=None,
        source=bins,
        orientation="flattop",
        fill_color=linear_cmap(
            field_name="counts",
            palette=palette,
            low=min(bins.counts),
            high=max(bins.counts),
        ),
        aspect_scale=aspect_scale,
    )
    fig.add_tools(
        HoverTool(
            tooltips=[("Count", "@counts")],
            renderers=[rend],
        ))
    mapper = LinearColorMapper(palette=palette,
                               low=min(bins.counts),
                               high=max(bins.counts))
    color_bar = ColorBar(color_mapper=mapper, width=8, location=(0, 0))
    color_bar.label_standoff = 8
    fig.add_layout(color_bar, "right")
    tweak_figure(fig, "hex")
    fig.xaxis.ticker = list(np.linspace(xmin, xmax, 10))
    fig.yaxis.ticker = list(np.linspace(ymin, ymax, 10))
    fig.xaxis.axis_label = x
    fig.yaxis.axis_label = y

    return Panel(child=fig, title="hexbin plot")
Exemple #10
0
from bokeh.io import output_file, show
from bokeh.models import ColumnDataSource
from bokeh.palettes import Viridis256
from bokeh.plotting import figure
from bokeh.transform import linear_cmap
from bokeh.util.hex import hexbin

orientation = "flattop"
size = 0.1

n = 50000
x = 2 + np.random.standard_normal(n)
y = 2 + 5 * np.random.standard_normal(n)

# import
bins = hexbin(x, y, size, orientation, 5)

p = figure(title="Manual hex bin for 50000 points",
           match_aspect=True,
           aspect_scale=5,
           tools="wheel_zoom,reset",
           background_fill_color='#440154')
p.grid.visible = False

source = ColumnDataSource(data=dict(q=bins.q, r=bins.r, c=bins.counts))

p.hex_tile(q="q",
           r="r",
           size=size,
           fill_color=linear_cmap('c', Viridis256, 0, max(bins.counts)),
           line_color=None,
 def test_gaussian_flattop(self, pd):
     bins = buh.hexbin(x, y, 2, "flattop")
     assert list(bins.q) == [0, 0, 1, 1, 1, 2]
     assert list(bins.r) == [-1, 0, -2, -1, 0, -2]
     assert list(bins.counts) == [95, 57, 14, 324, 8, 2]
Exemple #12
0
xdiff = xupp-xlow
ydiff = ylow-yupp
aspect = ydiff/xdiff

# Initialize Light Curve Plot
fig_cmd = figure(plot_height=280, plot_width=800, sizing_mode='scale_both',
                 x_range=[xlow,xupp], y_range=[ylow,yupp],
                 tools="pan,wheel_zoom,box_zoom,reset",
                 title=None,
                 min_border=10, min_border_left=50, min_border_right=50,
                 toolbar_location="above", border_fill_color="whitesmoke")
fig_cmd.toolbar.logo = None
fig_cmd.xaxis.axis_label = 'Color (BP-RP)'
fig_cmd.yaxis.axis_label = 'Absolute Magnitude (G)'
# Generate Hexbin CMD for full Gaia Sample
bins = hexbin(bprp, mag, 0.05, aspect_scale=aspect)
palette = Greys256[::-1]
fill_color = log_cmap('c', palette, 1, max(bins.counts))
source = ColumnDataSource(data=dict(q=bins.q, r=bins.r, c=bins.counts))
r = fig_cmd.hex_tile(q="q", r="r", size=0.05, aspect_scale=aspect,
                     source=source, line_color=None, fill_color=fill_color)
# Remove Grid Lines
fig_cmd.xgrid.grid_line_color = None
fig_cmd.ygrid.grid_line_color = None

# Add a simple hover tool
fig_cmd.add_tools(HoverTool(
        tooltips=[("count", "@c"), ("(q,r)", "(@q, @r)")],
        mode="mouse", point_policy="follow_mouse", renderers=[r]))

scat = fig_cmd.scatter(X, Y, size=10, color="#3A5785", alpha=0.8) #, xlabel="G-R",ylabel="R")
Exemple #13
0
import numpy as np

from bokeh.io import output_file, show
from bokeh.plotting import figure
from bokeh.transform import linear_cmap
from bokeh.util.hex import hexbin

n = 50000
x = np.random.standard_normal(n)
y = np.random.standard_normal(n)

bins = hexbin(x, y, 0.1)

p = figure(title="Manual hex bin for 50000 points", tools="wheel_zoom,pan,reset",
           match_aspect=True, background_fill_color='#440154')
p.grid.visible = False

p.hex_tile(q="q", r="r", size=0.1, line_color=None, source=bins,
           fill_color=linear_cmap('counts', 'Viridis256', 0, max(bins.counts)))

output_file("hex_tile.html")

show(p)
Exemple #14
0
#n = 50000
#x = np.random.standard_normal(n)
#y = np.random.standard_normal(n)
#

df = pd.read_csv("location.csv")
source = ColumnDataSource(df)
print(type(source))
print(df.columns)
print(df.describe())

print(type(df.Latitude))
for i in df.Latitude:
    print(i)
bins = hexbin(x, y, 0.05)

p = figure(title="DC locations",
           tools="wheel_zoom,pan,reset",
           match_aspect=True,
           background_fill_color='#440154')
p.grid.visible = False

p.hex_tile(q="q",
           r="r",
           size=0.1,
           line_color=None,
           source=bins,
           fill_color=linear_cmap('counts', 'Viridis256', 0, max(bins.counts)))

output_file("hex_tile.html")
Exemple #15
0
    source.stream(dict(x=[x], y=[y]))
    bin_size = 0.15
    bins = hexbin(x_hex, y_hex, bin_size)
    l_hex = p.hex_tile(
        q="q",
        r="r",
        size=0.1,
        line_color=None,
        source=bins,
        fill_color=linear_cmap("counts", "Viridis256", 0, max(bins.counts)),
    )


x_vec = [1.5, 1.5]
y_vec = [1.5, 1.5]
bins = hexbin(np.array(x_vec), np.array(y_vec), 0.1)


def blocking_task():
    while True:
        try:
        # try:
            with open(fname) as fn:
                data = fn.readline()
            X = np.array(eval(data))
            # do some blocking computation
            time.sleep(0.01)
            # for loop -----------
            # make a loop to plot all the data in the data-array.txt and not random selection of data
            x, y = choice(X[:, 0]), choice(X[:, 1]) # input a list here to plot <<-------
            print("x:", x, "y:", y)
Exemple #16
0
    """
    update the scatter plot
    """
    df = select_subset()
    bins = hexbin(df.x, df.y, 0.08)
    source.data = dict(x=bins["q"], y=bins["r"], counts=bins["counts"])


year = Slider(title="Min", start=1940, end=2020, value=1950, step=1)
source = ColumnDataSource(data=dict(x=[], y=[], counts=[]))

df = pd.read_csv(
    'Z:/github_packages/csd_visualisation/csd-visualisations/chemical_space/data/merge.csv'
)
df = df.loc[df['subset'] == 'drug']
bins = hexbin(df.x, df.y, 0.08)
#print bins

# Initialise boundaries
left = min(set(df.x)) - 1
right = max(set(df.x)) + 1
top = max(set(df.y)) + 1
bottom = min(set(df.y)) - 1
TOOLTIPS = [("x", "@x"), ("y", "@y")]

p = figure(title="CSD drug subset",
           tools="wheel_zoom,pan,reset",
           match_aspect=True,
           x_range=(left, right),
           y_range=(bottom, top),
           background_fill_color='#440154',
Exemple #17
0
# ### Bokeh figures

# Bokeh is a better plotting utility that allows scalable interactive plots

# +
from bokeh.plotting import figure
from bokeh.transform import linear_cmap
from bokeh.util.hex import hexbin
from bokeh.embed import file_html
import bokeh

n = 50000
x = np.random.standard_normal(n)
y = np.random.standard_normal(n)

bins = hexbin(x, y, 0.1)

p = figure(title="Manual hex bin for 50000 points",
           tools="wheel_zoom,pan,reset",
           match_aspect=True,
           sizing_mode='stretch_both',
           plot_width=300,
           plot_height=300,
           background_fill_color='#440154')
p.grid.visible = False

p.hex_tile(q="q",
           r="r",
           size=0.1,
           line_color=None,
           source=bins,
Exemple #18
0
 def test_gaussian_flattop(self):
     bins = buh.hexbin(x, y, 2, "flattop")
     assert list(bins.q) == [0, 0, 1, 1, 1, 2]
     assert list(bins.r) == [-1, 0, -2, -1, 0, -2]
     assert list(bins.counts) == [95, 57, 14, 324, 8, 2]
Exemple #19
0
def plot_ratio(x1, y1, names1, x2, y2, names2, term1, term2, lb):
    '''
    This function recieves the (repeated) x,y coördinates, names of the chemicals, and the terms of two different searches.
    Counts are determined for the hexagons. After normalisation for total counts, a log(ratio) is calculated for every hexagon.
    Then, a blur is applied to the log(ratio), but the original log(ratio) is shown in the tooltip.
    The hexagon figure is saved in a html file.
    '''

    length = len(x1) + len(x2)
    title = 'Hexbin plot for ' + str(
        length) + ' annotated chemicals with ratios of counts from ' + str(
            term1) + '/' + str(term2)

    x_min = min(min(x1), min(x2))
    x_max = max(max(x1), max(x2))
    y_min = min(min(y1), min(y2))
    y_max = max(max(y1), max(y2))

    # hexagon properties
    orientation = 'flattop'
    size = 20
    ratio = (y_max - y_min) / (x_max - x_min)
    if orientation == 'flattop':
        size = size / ratio
    h = (sqrt(3) * size) * ratio
    w = (2 * size)

    # boundaries plot / grid

    x_grid, y_grid = make_grid(x_min, x_max, y_min, y_max, w, h)

    # make figure
    p = figure(title=title,
               x_range=[x_min - 0.5, x_max + 0.5],
               y_range=[0 - (h / 2), y_max + 100 - h],
               tools="wheel_zoom,reset",
               background_fill_color='#FFFFFF')
    #'#D3D3D3'
    p.grid.visible = False

    x1 = np.append(x1, x_grid)
    y1 = np.append(y1, y_grid)
    x2 = np.append(x2, x_grid)
    y2 = np.append(y2, y_grid)

    df1 = hexbin(x1, y1, size, orientation, ratio)
    df2 = hexbin(x2, y2, size, orientation, ratio)

    df1, df2 = normalise_df_test(df1, df2, len(x_grid))

    df3 = calculate_ratio_test(df1, df2)
    df3['log'] = np.log(df3['ratio'])

    print(df3['log'].sum())
    # df3['blur'] = df3['log']
    df3 = add_gaussian_blur(df3)

    print(df3['log'].sum())

    highest_abs_value = max(max(df3.blur), abs(min(df3.blur)))

    df_low = df3[df3['blur'] < 0]
    df_high = df3[df3['blur'] > 0]
    df_eq = df3[df3['blur'] == 0]

    red.reverse()
    pbr = blue + ['#FFFFFF'] + red

    p.hex_tile(q="q",
               r="r",
               size=size,
               line_color=None,
               source=df_low,
               aspect_scale=ratio,
               orientation=orientation,
               hover_color="#39FF14",
               fill_color=linear_cmap('blur', blue, -highest_abs_value, 0))

    p.hex_tile(q="q",
               r="r",
               size=size,
               line_color=None,
               source=df_high,
               aspect_scale=ratio,
               orientation=orientation,
               hover_color="#39FF14",
               fill_color=linear_cmap('blur', red, 0, highest_abs_value))

    p.hex_tile(q="q",
               r="r",
               size=size,
               line_color=None,
               source=df_eq,
               aspect_scale=ratio,
               orientation=orientation,
               hover_color="#39FF14",
               fill_color='#FFFFFF')

    #hover tool
    hover = HoverTool(tooltips=[('blur', '@blur')])
    # hover = HoverTool(tooltips=[("log(ratio)", "@log"),("#1", "@names1"),("#2", "@names2"),("#3", "@names3"),("#4", "@names4"),("#5", "@names5")],callback=callback_hover, show_arrow=False)
    p.add_tools(hover)

    #output
    file_name = "plots/compare_ratios_" + str(term1) + "_" + str(
        term2) + ".html"
    output_file(file_name)

    # r = row(p,dummy)

    show(p)

    # p.hex_tile(q="q", r="r", size=size, line_color=None, source=df3, aspect_scale=ratio, orientation=orientation, hover_color='#39FF14',
    # fill_color=linear_cmap('ratio', 'Viridis256', 0, max(df3.ratio) ))
    #
    # print(len(df_low))
    # print(len(df_high))
    # print(len(df_eq))

    # print(df_low['log'].sum())
    # print(df_high['log'].sum())
    # get coordinates for every x,y point
    # df1 = hexbin(x3, y3, size, orientation, ratio)
    # df1 = hex_to_bin(x1, y1, size, ratio, orientation)
    # df2 = hex_to_bin(x2, y2, size, ratio, orientation)
    #
    # # add the names to the dataframe
    # df1['names'] = names1.append(['']*len(x3))
    # df2['names'] = names2.append(['']*len(x3))
    #
    # # group by coordinates for hexagons
    # df1 = df1.groupby(['q', 'r'])['names'].apply(list).reset_index(name='names')
    # df2 = df2.groupby(['q', 'r'])['names'].apply(list).reset_index(name='names')
    #
    # # count chemicals per hexagon, also add column with a list of counts of individual chemicals # (SHOULD THESE BE NORMALISED?)
    # df1 = transform_df(df1)
    # df2 = transform_df(df2)
    # #
    # # print(len(df1))
    # # print(len(df2))
    # #
    # # normalize counts against total counts
    # df1_normalized = normalize_df(df1)
    # df2_normalized = normalize_df(df2)
    #
    # # print(df1_normalized['counts'].sum())
    # # print(df2_normalized['counts'].sum())
    # #
    # # # calculate ratio's and find most discriminating chemicals (+ difference? in counts)
    # df_ratio = calculate_ratio_test(df1_normalized, df2_normalized)
    #
    # df_ratio_low = df_ratio[df_ratio['ratio'] < 1]
    # df_ratio_eq = df_ratio[df_ratio['ratio'] == 1]
    # df_ratio_high = df_ratio[df_ratio['ratio'] > 1]
    #
    # df_ratio_low['log'] = np.log(df_ratio_low['ratio'])
    # df_ratio_high['log'] = np.log(df_ratio_high['ratio'])

    # print(len(df_ratio_low))
    # print(len(df_ratio_high))
    # print(df_ratio_high['log'].sum())
    # print(df_ratio_low['log'].sum())
    # print(df_ratio[0:10])
    #
    # # log transformation
    # df_ratio['log'] = np.log(df_ratio['ratio'])
    # df_ratio['blur'] = np.log(df_ratio['ratio'])
    # # finding most discriminating chemicals per hexagon
    # # df_ratio = find_discriminating_chemicals(df_ratio)
    # # df_ratio = df_ratio.drop(columns="dif")
    #
    # print(df_ratio.columns)
    # # add blur to log ratio's
    # df_ratio = add_gaussian_blur(df_ratio)
    # print(df_ratio.columns)
    #
    # # make two dataframes to create two glyphs
    # df_ratio_low = df_ratio[df_ratio['log'] < 0]
    # df_ratio_zero = df_ratio[df_ratio['log'] == 0]
    # df_ratio_high = df_ratio[df_ratio['log'] > 0]
    #
    # print(len(df_ratio_low))
    # print(len(df_ratio_high ))
    # print(len(df_ratio_zero))
    # # blue.append('#FFFFFF')
    # # red.append('#FFFFFF')

    # # color bar
    # color_mapper = LinearColorMapper(palette=pbr, low=-5, high=5)
    #
    # color_bar = ColorBar(color_mapper=color_mapper, ticker=BasicTicker(),major_label_overrides={4: 'More '+str(term1)+' counts',-4:"More "+str(term2)+" counts"},major_label_text_align='left',
    #                      label_standoff=6, border_line_color=None, location=(0,0))
    #
    # # color bar is added to a dummy figure, to prevent the shrinking of the original plot
    # dummy = figure(
    #            toolbar_location=None,
    #            min_border=0,
    #            outline_line_color=None)
    # dummy.add_layout(color_bar, 'left')
    # dummy.title.align = 'center'
    # dummy.title.text_font_size = '10pt'

    #hover position
    callback_hover = CustomJS(code="""
        var tooltips = document.getElementsByClassName("bk-tooltip");
        for (var i = 0, len = tooltips.length; i < len; i ++) {
            tooltips[i].style.top = ""; // unset what bokeh.js sets
            tooltips[i].style.left = "";
            tooltips[i].style.bottom = "280px";
            tooltips[i].style.left = "700px";
            tooltips[i].style.width = "300px";
        }
        """)
Exemple #20
0
def create_figure():
    df_loc = df
    if not df.empty:
        # applying cut on data
        cut_cmd = ""
        if (str(cut_1.value) in cut_1.options) and (str(cut_1_op.value) in cut_1_op.options) and \
                        str(cut_1.value) is not 'None' and cut_1_input.value != "":
            cut_cmd += "(df[\"" + str(cut_1.value) + "\"]" + str(cut_1_op.value) + \
                       "%d" % float(cut_1_input.value) + ")"

        if (str(cut_2.value) in cut_2.options) and (str(cut_2_op.value) in cut_2_op.options) and \
                        str(cut_2.value) is not 'None' and cut_2_input.value != "":
            if cut_cmd != "":
                cut_cmd += " & "
            cut_cmd += "(df[\"" + str(cut_2.value) + "\"]" + str(cut_2_op.value) + \
                       "%d" % float(cut_2_input.value) + ")"

        if (str(cut_3.value) in cut_3.options) and (str(cut_3_op.value) in cut_3_op.options) and \
                        str(cut_3.value) is not 'None' and cut_3_input.value != "":
            if cut_cmd != "":
                cut_cmd += " & "
            cut_cmd += "(df[\"" + str(cut_3.value) + "\"]" + str(cut_3_op.value) + \
                       "%d" % float(cut_3_input.value) + ")"

        if (str(cut_4.value) in cut_4.options) and (str(cut_4_op.value) in cut_4_op.options) and \
                        str(cut_4.value) is not 'None' and cut_4_input.value != "":
            if cut_cmd != "":
                cut_cmd += " & "
            cut_cmd += "(df[\"" + str(cut_4.value) + "\"]" + str(cut_4_op.value) + \
                       "%d" % float(cut_4_input.value) + ")"

        if cut_cmd != "":
            cut_cmd += " & (df[\"event_id\"]>" + str(range_select.value) + ") & " + \
                       "(df[\"event_id\"]<" + str(int(range_select.value) + int(num_event.value)) + ")"
        else:
            cut_cmd += "(df[\"event_id\"]>" + str(range_select.value) + ") & " + \
                       "(df[\"event_id\"]<" + str(int(range_select.value) + int(num_event.value)) + ")"

        cut_cmd += ", ["
        for i, v in enumerate(columns):
            if i == 0:
                cut_cmd += "\"" + v + "\""
            else:
                cut_cmd += ",\"" + v + "\""
        cut_cmd += "]"
        df_loc = df.loc[eval(cut_cmd)]

        x_title = x.value.title()
        y_title = y.value.title()

        sz = 6
        if size.value != 'None':
            groups = pd.qcut(df_loc[size.value].values, len(SIZES))
            sz = [SIZES[xx] for xx in groups.codes]

        c = "#31AADE"
        if color.value != 'None':
            groups = pd.qcut(df_loc[color.value].values, len(COLORS))
            c = [COLORS[xx] for xx in groups.codes]

        if type(int()) != type(sz):
            df_loc['size'] = sz

        if type(str()) != type(c):
            df_loc['color'] = c

        source = ColumnDataSource(data=df_loc)

        kw = dict()
        if x.value in discrete:
            kw['x_range'] = sorted(set(source[x.value]))
        if y.value in discrete:
            kw['y_range'] = sorted(set(source[y.value]))
        kw['title'] = "%s vs %s" % (x_title, y_title)

        tuple_hover_var = []
        for i in columns:
            if i != 'None':
                hover_list = "(\"" + str(i) + "\",\"@" + str(i) + "\")"
                tuple_hover_var.append(eval(hover_list))

        TOOLS = ["pan,box_zoom,reset,wheel_zoom, hover, save"]
        if x.value == 'time' and y.value != 'time':
            p = figure(plot_height=800, plot_width=800, x_axis_type="datetime", tools=TOOLS,
                       toolbar_location='above', toolbar_sticky=False, **kw)
            p.xaxis.formatter = DatetimeTickFormatter(microseconds=["%m/%d/%y %I:%M:%S %p"],
                                                      milliseconds=["%m/%d/%y %I:%M:%S %p"],
                                                      seconds=["%m/%d/%y %I:%M:%S %p"],
                                                      minsec=["%m/%d/%y %I:%M:%S %p"],
                                                      minutes=["%m/%d/%y %I:%M:%S %p"],
                                                      hourmin=["%m/%d/%y %I:%M:%S %p"],
                                                      hours=["%m/%d/%y %I:%M:%S %p"],
                                                      days=["%m/%d/%y %I:%M:%S %p"],
                                                      months=["%m/%d/%y %I:%M:%S %p"],
                                                      years=["%m/%d/%y %I:%M:%S %p"])
        elif x.value != 'time' and y.value == 'time':
            p = figure(plot_height=800, plot_width=800, y_axis_type="datetime", tools=TOOLS, toolbar_location='above',
                       toolbar_sticky=False, **kw)
            p.yaxis.formatter = DatetimeTickFormatter(microseconds=["%m/%d/%y %I:%M:%S %p"],
                                                      milliseconds=["%m/%d/%y %I:%M:%S %p"],
                                                      seconds=["%m/%d/%y %I:%M:%S %p"],
                                                      minsec=["%m/%d/%y %I:%M:%S %p"],
                                                      minutes=["%m/%d/%y %I:%M:%S %p"],
                                                      hourmin=["%m/%d/%y %I:%M:%S %p"],
                                                      hours=["%m/%d/%y %I:%M:%S %p"],
                                                      days=["%m/%d/%y %I:%M:%S %p"],
                                                      months=["%m/%d/%y %I:%M:%S %p"],
                                                      years=["%m/%d/%y %I:%M:%S %p"])
        elif x.value == 'time' and y.value == 'time':
            p = figure(plot_height=800, plot_width=800, x_axis_type="datetime", y_axis_type="datetime",
                       toolbar_location='above', toolbar_sticky=False, tools=TOOLS, **kw)
            p.xaxis.formatter = DatetimeTickFormatter(microseconds=["%m/%d/%y %I:%M:%S %p"],
                                                      milliseconds=["%m/%d/%y %I:%M:%S %p"],
                                                      seconds=["%m/%d/%y %I:%M:%S %p"],
                                                      minsec=["%m/%d/%y %I:%M:%S %p"],
                                                      minutes=["%m/%d/%y %I:%M:%S %p"],
                                                      hourmin=["%m/%d/%y %I:%M:%S %p"],
                                                      hours=["%m/%d/%y %I:%M:%S %p"],
                                                      days=["%m/%d/%y %I:%M:%S %p"],
                                                      months=["%m/%d/%y %I:%M:%S %p"],
                                                      years=["%m/%d/%y %I:%M:%S %p"])
            p.yaxis.formatter = DatetimeTickFormatter(microseconds=["%m/%d/%y %I:%M:%S %p"],
                                                      milliseconds=["%m/%d/%y %I:%M:%S %p"],
                                                      seconds=["%m/%d/%y %I:%M:%S %p"],
                                                      minsec=["%m/%d/%y %I:%M:%S %p"],
                                                      minutes=["%m/%d/%y %I:%M:%S %p"],
                                                      hourmin=["%m/%d/%y %I:%M:%S %p"],
                                                      hours=["%m/%d/%y %I:%M:%S %p"],
                                                      days=["%m/%d/%y %I:%M:%S %p"],
                                                      months=["%m/%d/%y %I:%M:%S %p"],
                                                      years=["%m/%d/%y %I:%M:%S %p"])
        else:
            p = figure(plot_height=800, plot_width=800, tools=TOOLS, toolbar_location='above',
                       toolbar_sticky=False, **kw)

        if x.value == 'local_time' or x.value == 'time':
            p.xaxis.major_label_orientation = np.pi / 4

        if y.value == 'local_time' or y.value == 'time':
            p.yaxis.major_label_orientation = np.pi / 4

        p.xaxis.axis_label = x_title
        p.yaxis.axis_label = y_title

        if type(int()) == type(sz) and type(c) == type(str()):
            if radio_button_group.active == 0:
                p.circle(x.value, y.value, color=c, size=sz, line_color="white", alpha=0.6, hover_color="red",
                         source=source)
            else:
                nbins_x = get_nbins(df_loc[x.value])
                nbins_y = get_nbins(df_loc[y.value])
                if nbins_x >= nbins_y:
                    scale = (df_loc[x.value].max() - df_loc[x.value].min()) / float(nbins_x)
                else:
                    scale = (df_loc[y.value].max() - df_loc[y.value].min()) / float(nbins_y)

                bins = hexbin(df_loc[x.value], df_loc[y.value], scale)
                mapper = LinearColorMapper(palette=Inferno256, low=0, high=max(bins.counts))
                lin_mapper = linear_cmap('counts', 'Inferno256', 0, max(bins.counts))
                p.hex_tile(q="q", r="r", size=scale, line_color=None, source=bins, fill_color=lin_mapper)
                color_bar = ColorBar(color_mapper=mapper, ticker=BasicTicker(desired_num_ticks=10),
                                     border_line_color=None, background_fill_color='#2F2F2F', label_standoff=12,
                                     major_label_text_color='white', margin=0, title_text_color='white')
                p.add_layout(color_bar, place='right')

        elif type(int()) != type(sz) and type(c) == type(str()):
            p.circle(x.value, y.value, color=c, size='size', line_color="white", alpha=0.6, hover_color="red",
                     source=source)
        elif type(int()) == type(sz) and type(c) != type(str()):
            p.circle(x.value, y.value, color='color', size=sz, line_color="white", alpha=0.6, hover_color="red",
                     source=source)
            mapper = LinearColorMapper(palette=Inferno256, low=df_loc[color.value].min(),
                                       high=df_loc[color.value].max())
            color_bar = ColorBar(color_mapper=mapper, ticker=BasicTicker(desired_num_ticks=10), border_line_color=None,
                                 background_fill_color='#2F2F2F', label_standoff=12, major_label_text_color='white',
                                 margin=0, title=color.value, title_text_color='white')
            p.add_layout(color_bar, place='right')

        else:
            p.circle(x.value, y.value, color='color', size='size', line_color="white", alpha=0.6, hover_color="red",
                     source=source)
            mapper = LinearColorMapper(palette=Inferno256, low=df_loc[color.value].min(),
                                       high=df_loc[color.value].max())
            color_bar = ColorBar(color_mapper=mapper, ticker=BasicTicker(desired_num_ticks=10), border_line_color=None,
                                 background_fill_color='#2F2F2F', label_standoff=12, major_label_text_color='white',
                                 margin=0, title=color.value, title_text_color='white')
            p.add_layout(color_bar, place='right')

        hover = p.select_one(HoverTool)
        hover.mode = "mouse"

        if radio_button_group.active == 0:
            hover.tooltips = tuple_hover_var
        else:
            hover.tooltips = [("count", "@c"), ("(q,r)", "(@q, @r)")]

        # create the horizontal histogram
        hhist, hedges = np.histogram(np.array(df_loc[x.value]), bins=get_nbins(np.array(df_loc[x.value])))
        hmax = max(hhist) * 1.1

        LINE_ARGS = dict(color="#3A5785", line_color=None)

        ph = figure(toolbar_location=None, plot_width=p.plot_width, plot_height=200, x_range=p.x_range,
                    y_range=(0, hmax), min_border=10, min_border_left=50, y_axis_location="right")
        ph.xgrid.grid_line_color = None
        ph.yaxis.major_label_orientation = np.pi / 4
        ph.background_fill_color = "#2F2F2F"

        ph.quad(bottom=0, left=hedges[:-1], right=hedges[1:], top=hhist, line_color="#3A5785")

        # create the vertical histogram
        vhist, vedges = np.histogram(np.array(df_loc[y.value]), bins=get_nbins(np.array(df_loc[y.value])))
        vmax = max(vhist) * 1.1

        pv = figure(toolbar_location=None, plot_width=200, plot_height=p.plot_height, x_range=(0, vmax),
                    y_range=p.y_range, min_border=10, y_axis_location="right")
        pv.ygrid.grid_line_color = None
        pv.xaxis.major_label_orientation = np.pi / 4
        pv.background_fill_color = "#2F2F2F"

        pv.quad(left=0, bottom=vedges[:-1], top=vedges[1:], right=vhist, line_color="#3A5785")

        plot_layout = column(row(p, pv), row(ph, Spacer(width=200, height=200)))

        return plot_layout
    else:
        p = figure(plot_height=800, plot_width=800)
        p.text(x=[1, 2, 3], y=[1, 2, 3], text=['SST-1M', 'SST-1M', 'SST-1M'])
        return p