예제 #1
0
    def generateHoloview(self, df, SSC, FSC, type, counter_max=50):
        renderer = hv.renderer('bokeh')
        body_points = hv.Scatter(df, SSC, FSC).opts(color='r', title='SSC vs FSC Default Gating')
        body_hist = body_points.hist(num_bins=50, dimension=[SSC, FSC])
        body = body_hist

        counter = 0

        for index in range(len(df.columns)):
            for index2 in range(index+1, len(df.columns)):
                col = df.columns[index]
                col2 = df.columns[index2]
                if col2 != col and col not in (SSC, FSC) and col2 not in (SSC, FSC) and counter < counter_max:
                    points = hv.Scatter(df, col, col2)
                    hist = points.hist(num_bins=50, dimension=[col, col2])
                    body += hist
                    counter += 1
        print(counter)
        try:
            body = body.opts(
                opts.Scatter(tools=['box_select', 'lasso_select']),
                opts.Layout(shared_axes=True, shared_datasource=True)).cols(2)
        except:
            body = body.opts(
                opts.Scatter(tools=['box_select', 'lasso_select']),
                opts.Layout(shared_axes=True, shared_datasource=True))
        renderer.save(body, os.path.join(self.directory, str(type)+"gating"))
예제 #2
0
def swatches(*args, group=None, not_group=None, only_aliased=False, cols=None, **kwargs):
    """Show swatches for given names or names in group"""
    args = args or all_original_names(group=group, not_group=not_group,
                                      only_aliased=only_aliased)
    if not cols:
        cols = 3 if len(args) >= 3 else 1

    backends = hv.Store.loaded_backends()
    if 'matplotlib' in backends:
        if 'aspect' not in kwargs:
            kwargs['aspect'] = 12 // cols
        if 'fig_size' not in kwargs:
            kwargs['fig_size'] = 500 // cols
    if 'bokeh' in backends:
        if 'height' not in kwargs:
            kwargs['height'] = 100
        if 'width' not in kwargs:
            kwargs['width'] = (9 * kwargs['height']) // cols

    images = [swatch(arg, **kwargs) if isinstance(arg, str) else
              swatch(*arg, **kwargs) for
              arg in args]

    plot = hv.Layout(images).opts(plot=dict(transpose=True)).cols(int(np.ceil(len(images)*1.0/cols)))

    if 'matplotlib' in backends:
        plot.opts(opts.Layout(backend='matplotlib', sublabel_format=None,
                              fig_size=kwargs.get('fig_size', 150)))
    return plot
예제 #3
0
def swatches(names=None, cols=None, **kwargs):
    """Show swatches for all names or given names"""
    if not names:
        names = sorted([name for name in palette.keys()])

    if not cols:
        cols = 2 if len(names) >= 2 else 1

    backends = hv.Store.loaded_backends()
    if 'matplotlib' in backends:
        if 'aspect' not in kwargs:
            kwargs['aspect'] = 12 // cols
        if 'fig_size' not in kwargs:
            kwargs['fig_size'] = 500 // cols
    if 'bokeh' in backends:
        if 'height' not in kwargs:
            kwargs['height'] = 100
        if 'width' not in kwargs:
            kwargs['width'] = (9 * kwargs['height']) // cols

    images = [swatch(name, **kwargs) for name in names]
    plot = hv.Layout(images).opts(plot=dict(transpose=True)).cols(int(np.ceil(len(images)*1.0/cols)))

    if 'matplotlib' in backends:
        plot.opts(opts.Layout(backend='matplotlib', sublabel_format=None,
                              fig_size=kwargs.get('fig_size', 150)))
    return plot
예제 #4
0
    def generateCombined(self, decimated, SSC, FSC, cachebust, counter_max=20):
        renderer = hv.renderer('bokeh')
        body = None
        points = None
        point_collect = []
        for key in decimated.keys():
            print(key)
            point = hv.Scatter(decimated[key], SSC, FSC, label=key)
            point_collect.append(point)
            if points is None:
                points = point
            else:
                points *= point
        if body is None:
            body = points.opts(title='Default {0}: SSC vs FSC'.format("Combined"), height=450, width=450)
        else:
            body += points.opts(title='Default {0}: SSC vs FSC'.format("Combined"))

        for dim in (SSC, FSC):
            hists = None
            for point in point_collect:
                hist = histogram(point, dimension=dim)
                if hists is None:
                    hists = hist
                else:
                    hists *= hist
            body += hists

        potentialCols = [c for c in decimated[list(decimated.keys())[0]].columns if c != SSC and c != FSC]
        for i in range(len(potentialCols)):
            for j in range(i+1, len(potentialCols)):
                points = None
                point_collect = []
                for key in decimated.keys():
                    point = hv.Scatter(decimated[key], potentialCols[i], potentialCols[j], label=key)
                    point_collect.append(point)
                    if points is None:
                        points = point
                    else:
                        points *= point
                body += points.opts(title='Combined: {0} vs {1}'.format(potentialCols[i], potentialCols[j]), height=450, width=450)

                for dim in (potentialCols[i], potentialCols[j]):
                    hists = None
                    for point in point_collect:
                        hist = histogram(point, dimension=dim)
                        if hists is None:
                            hists = hist
                        else:
                            hists *= hist
                    body += hists
        body = body.opts(
            opts.Scatter(alpha=0.9),
            opts.Histogram(alpha=0.9, height=450),
            opts.Layout(shared_axes=True, shared_datasource=True)).cols(3)
        renderer.save(body, os.path.join(self.directory, cachebust+"combined_gating"))
예제 #5
0
def plot_curve():
    df = download_data(index.value)
    future_df = download_data_predicted(index.value)

    title = index.value + " Exchange Rate"
    # Create stock curve
    past_label = "Past " + title
    future_label = "Predicted Future " + title
    df['label'] = past_label
    future_df['label'] = future_label

    new_df = pd.concat([df, future_df], axis=0)
    curve = hv.Curve(df, 'Date', ('Close', 'label'))
    curve_pred = hv.Curve(future_df, 'Date', ('Close', 'Price'))
    # Labels and layout
    tgt = curve.relabel("Past " + title).opts(  #width=width,
        height=600,
        show_grid=True,
        labelled=['y'],
        default_tools=[hover],
        hooks=[set_tools],
        title=title,
        responsive=True)
    tgt_pred = curve_pred.relabel("Future " + title).opts(  #width=width,
        height=600,
        show_grid=True,
        labelled=['y'],
        default_tools=[hover],
        hooks=[set_tools],
        title=title,
        responsive=True)
    src = curve.opts(height=100,
                     yaxis=None,
                     default_tools=[],
                     color='green',
                     responsive=True)
    src_pred = curve_pred.opts(height=100,
                               yaxis=None,
                               default_tools=[],
                               color='green',
                               responsive=True)

    circle = hv.Scatter(df, 'Date', ('Close', 'Price')).opts(color='green')
    circle_pred = hv.Scatter(future_df, 'Date',
                             ('Close', 'Price')).opts(color='blue')

    RangeToolLink(src, tgt)
    # Merge rangetool
    layout = ((tgt * tgt_pred * circle * circle_pred) +
              (src * src_pred)).cols(1)
    layout.opts(opts.Layout(shared_axes=False, merge_tools=False),
                opts.Curve(toolbar=None), opts.Scatter(size=3))
    print("kepanggil nih viz")
    print(df["Close"][0])
    print(index.value)
    return layout
예제 #6
0
def hv_adjust_image_landmarks(image,
                              landmarks,
                              landmarks_match=None,
                              bregma_offset=None,
                              resolution=0.0194,
                              msize=40):
    '''
    TODO: merge part of this with the one for the landmarks
    landmarks are in allen reference space
    '''
    h, w = image.shape
    if bregma_offset is None:
        # then it is the center of the image
        bregma_offset = np.array([int(w / 2), int(h / 2)
                                  ])  # place bregma in the center of the image

    landmarks_im = allen_landmarks_to_image_space(landmarks.copy(),
                                                  bregma_offset, resolution)
    if landmarks_match is None:
        landmarks_match = landmarks_im
    import holoviews as hv
    from holoviews import opts, streams
    from holoviews.plotting.links import DataLink

    bounds = np.array([0, 0, w, h])
    im = hv.Image(image[::-1, :],
                  bounds=tuple(bounds.tolist())).opts(invert_yaxis=True,
                                                      cmap='gray')

    points = hv.Points(landmarks_match, vdims='color').opts(marker='+',
                                                            size=msize)
    point_stream = streams.PointDraw(data=points.columns(),
                                     add=False,
                                     num_objects=4,
                                     source=points,
                                     empty_value='black')
    table = hv.Table(points, ['x', 'y', 'name'],
                     'color').opts(title='Annotation location')
    DataLink(points, table)

    from bokeh.models import HoverTool
    hoverpts = HoverTool(tooltips=[("i", "$index")])

    widget = (im * points + table).opts(
        opts.Layout(merge_tools=False),
        opts.Points(invert_yaxis=True,
                    active_tools=['point_draw'],
                    color='color',
                    tools=[hoverpts],
                    width=int(w),
                    height=int(h)), opts.Table(editable=True))
    return widget, point_stream, landmarks_im
예제 #7
0
def sine_combs(*args, group=None, not_group=None, only_aliased=False, cols=1, **kwargs):
    """Show sine_combs for given names or names in group"""
    args = args or all_original_names(group=group, not_group=not_group,
                                      only_aliased=only_aliased)
    images = [sine_comb(arg, **kwargs) if isinstance(arg, str) else
              sine_comb(*arg, **kwargs) for
              arg in args]

    plot = hv.Layout(images).opts(plot=dict(transpose=True)).cols(int(np.ceil(len(images)*1.0/cols)))

    backends = hv.Store.loaded_backends()
    if 'matplotlib' in backends:
        plot.opts(opts.Layout(backend='matplotlib', sublabel_format=None,
                              fig_size=kwargs.get('fig_size', 200)))
    return plot
예제 #8
0
def hv_adjust_reference_landmarks(landmarks, ccf_regions, msize=40):
    '''
    landmarks = {'x': [-1.95, 0, 1.95, 0],
                 'y': [-3.45, -3.45, -3.45, 3.2],
                 'name': ['OB_left', 'OB_center', 'OB_right', 'RSP_base'],
                 'color': ['#fc9d03', '#0367fc', '#fc9d03', '#fc4103']}
    landmarks = pd.DataFrame(landmarks)
    # adjust landmarks
    wid,landmark_wid = hv_adjust_reference_landmarks(landmarks,ccf_regions)
    wid # to display
    # use the following to retrieve (on another cell) 
    landmarks = pd.DataFrame(landmark_wid.data)[['x','y','name','color']]
    '''
    import holoviews as hv
    from holoviews import opts, streams
    from holoviews.plotting.links import DataLink
    referenceplt = hv_plot_allen_regions(ccf_regions).options(
        {'Curve': {
            'color': 'black',
            'width': 600
        }})

    points = hv.Points(landmarks, vdims='color').opts(marker='+', size=msize)
    point_stream = streams.PointDraw(data=points.columns(),
                                     add=False,
                                     num_objects=4,
                                     source=points,
                                     empty_value='black')
    table = hv.Table(points, ['x', 'y', 'name'],
                     'color').opts(title='Landmarks location')
    DataLink(points, table)
    widget = (referenceplt * points + table).opts(
        opts.Layout(merge_tools=False),
        opts.Points(invert_yaxis=True,
                    active_tools=['point_draw'],
                    color='color',
                    height=500,
                    tools=['hover'],
                    width=500), opts.Table(editable=True))
    return widget, point_stream
def plot_countries(df,
                   col,
                   round_val=1,
                   col_tooltip='',
                   nr_countries=10,
                   reverse=False):
    '''
    Plot the overview for the top x (default 10) countries and for the overall countries as well.
    Returns a HoloViews plot layout.
        Arguments:
        df - Dataframe to process, must have the columns 'Country' and 'Year' within.
        col - Column in Dataframe where values are evaluated for the plotting process
        round_val (optional) - single numeric value to set the y axis limit on max found within col
        col_tooltip (optional) - tooltip to be set in the plots for the col values
        nr_countries (int) (optional) - number of countries to plot in the top views
        reverse (bool) (optional) - if True the bottom countries are listed 
    '''
    max_y = np.ceil(np.nanmax(df[col].values))
    max_y = max_y - max_y % round_val + 2 * round_val
    if col_tooltip == '':
        col_tooltip = '@{' + col.replace(
            " ", "_"
        ) + '}{0,0.000}'  # Holoviews auto-replaces spaces with underscores
    years_list = list(df['Year'].unique())
    if reverse == True:
        label = 'Bottom' + str(nr_countries)
        plot_df = df[-nr_countries * len(years_list):]
    else:
        label = 'Top' + str(nr_countries)
        plot_df = df[:nr_countries * len(years_list)][::-1]
    plot_df_invert = plot_df[::-1].copy()
    df_invert = df[::-1].copy()

    # Plot settings and parameters
    top_hover = HoverTool(tooltips=[('Country',
                                     '@Country'), ('Year',
                                                   '@Year'), (col,
                                                              col_tooltip)])
    country_hover = HoverTool(tooltips=[("Year", "@Year"), (col, col_tooltip)])
    year_hover = HoverTool(tooltips=[("Country", "@Country"), (col,
                                                               col_tooltip)])

    top_plot_arguments = dict(x='Year', y=col, by='Country', tools=[top_hover])
    options_shared = dict(height=700,
                          ylim=(0, max_y),
                          hooks=[set_bokeh_plot],
                          active_tools=['wheel_zoom'],
                          padding=(0.1, 0.1))
    options = [
        opts.Bars(width=700, show_grid=True, **options_shared),
        opts.Scatter(xticks=years_list, marker='o', size=10, **options_shared),
        opts.NdOverlay(width=650, xticks=years_list, **options_shared),
        opts.Layout(tabs=True)
    ]

    # Create the multiplot
    layout = (
        plot_df_invert.hvplot(
            kind='barh', label=label + 'BarPlot', **top_plot_arguments) +
        plot_df.hvplot(
            kind='line', label=label + 'LinePlot', **top_plot_arguments) *
        plot_df.hvplot(
            kind='scatter', label=label + 'LinePlot', **top_plot_arguments) +
        df.hvplot(kind='bar',
                  x='Year',
                  y=col,
                  groupby='Country',
                  label='SingleCountryDropdown',
                  tools=[country_hover]) +
        df_invert.hvplot(kind='barh',
                         x='Country',
                         y=col,
                         groupby='Year',
                         label='AllCountriesYearSlider',
                         tools=[year_hover])).opts(options)
    return layout
예제 #10
0
opts.defaults(
    opts.Curve(xaxis=None,
               yaxis=None,
               show_grid=False,
               show_frame=False,
               color='orangered',
               framewise=True,
               width=100),
    opts.Image(width=800,
               height=400,
               shared_axes=False,
               logz=True,
               xaxis=None,
               yaxis=None,
               axiswise=True), opts.HLine(color='white', line_width=1),
    opts.Layout(shared_axes=False), opts.VLine(color='white', line_width=1))

# Read the parquet file
df = dd.read_parquet('./data/nyc_taxi_wide.parq').persist()

# Declare points
points = hv.Points(df, kdims=['pickup_x', 'pickup_y'], vdims=[])

# Use datashader to rasterize and linked streams for interactivity
agg = aggregate(points, link_inputs=True, x_sampling=0.0001, y_sampling=0.0001)
pointerx = hv.streams.PointerX(x=np.mean(points.range('pickup_x')),
                               source=points)
pointery = hv.streams.PointerY(y=np.mean(points.range('pickup_y')),
                               source=points)
vline = hv.DynamicMap(lambda x: hv.VLine(x), streams=[pointerx])
hline = hv.DynamicMap(lambda y: hv.HLine(y), streams=[pointery])
예제 #11
0
hv.extension("bokeh")
pn.extension(sizing_mode="scale_width")

# Set some defaults for the visualization of the graphs
hvopts.defaults(
    hvopts.Image(  # pylint: disable=no-member
        # Don't set both height and width, or the UI will not be responsive!
        height=600,
        # width=800,
        responsive=True,
        show_title=True,
        tools=["hover"],
        active_tools=["pan", "wheel_zoom"],
        align="end",
    ),
    hvopts.Layout(toolbar="right"),  # pylint: disable=no-member
)

ui = thalassa.ThalassaUI(
    display_variables=True,
    display_stations=True,
)

# https://panel.holoviz.org/reference/templates/Bootstrap.html
bootstrap = pn.template.BootstrapTemplate(
    site="example.com",
    title="Thalassa",
    logo="thalassa/static/logo.png",
    favicon="thalassa/static/favicon.png",
    sidebar=[ui.sidebar],
    sidebar_width=350,  # in pixels! must be an integer!
예제 #12
0
cmap_r = 'winter_r'

clr_cca = 'peru'
clr_pls = 'lightseagreen'

clr_inSample = 'steelblue'
clr_5cv = 'tomato'
clr_20x5cv = 'gold'
clr_5cvMean = 'maroon'
clr_20x5cvMean = 'darkgoldenrod'

default_fontsizes = dict(title=8, labels=8, ticks=7, minor_ticks=7, legend=7)

fig_opts = [
    opts.Layout(aspect_weight=1,
                fig_inches=(3.42, None),
                sublabel_size=10,
                fontsize=8),
    opts.Overlay(fontsize=default_fontsizes, ),
    opts.Area(fontsize=default_fontsizes),
    opts.Arrow(textsize=default_fontsizes),
    opts.Curve(fontsize=default_fontsizes),
    opts.HexTiles(fontsize=default_fontsizes),
    opts.Histogram(fontsize=default_fontsizes),
    opts.Raster(fontsize=default_fontsizes),
    opts.Scatter(fontsize=default_fontsizes),
    opts.Text(fontsize=default_fontsizes),
    opts.QuadMesh(fontsize=default_fontsizes),
    opts.Violin(fontsize=default_fontsizes),
    opts.VLine(fontsize=default_fontsizes),
]