Ejemplo n.º 1
0
def power_pollution():
    '''
  Input:
        This function inputs data from local downloaded csv and xsl files.
  Output:
        The output data are in pandas dataframe format and do not requires
        further process for plotting.
  '''
    state_p = power[np.logical_and(power['STATE'] != 'US-TOTAL',
                                   power['STATE'] != 'US-Total')]
    state_p = state_p[np.logical_and(power['STATE'] != 'DC',
                                     power['STATE'] != 'HI')]
    state_p = state_p[power['STATE'] != 'AK']
    state_p = state_p[state_p['ENERGY SOURCE'] == 'Total']
    state_p_f = state_p.groupby(
        ['YEAR', 'STATE'])['GENERATION (Megawatthours)'].sum().reset_index()
    edata = hv.Dataset(data=state_p_f,
                       kdims=['YEAR', 'STATE', 'GENERATION (Megawatthours)'])

    PM_data = pollution[
        pollution['MeasureName'] ==
        'Annual average ambient concentrations of PM 2.5 in micrograms per cubic meter, based on seasonal averages and daily measurement (monitor and modeled data)']
    PM_data = PM_data[PM_data['StateName'] != 'District of Columbia']
    PM_data = PM_data.replace({'StateName': us_state_abbrev})
    PM_data_f = PM_data.groupby(['ReportYear',
                                 'StateName'])['Value'].sum().reset_index()
    PM_data_f = PM_data_f.rename(
        index=str,
        columns={'Value': 'Annual average ambient concentrations of PM 2.5'})
    edata_PM = hv.Dataset(data=PM_data_f,
                          kdims=[
                              'ReportYear', 'StateName',
                              'Annual average ambient concentrations of PM 2.5'
                          ])
    return state_p, state_p_f, PM_data, PM_data_f
Ejemplo n.º 2
0
    def update_hist(self):
        hv.extension("matplotlib")
        dcrop = (self.da_masked *
                 self.da_zone.sel(id=self.id_i)).isel(step=self.step)
        dcrop.name = self.da.name
        var_name = self.da.name
        if self.levels:
            hv_img = hv.Image(hv.Dataset(dcrop),
                              kdims=["longitude", "latitude"
                                     ]).opts(colorbar=True,
                                             color_levels=self.levels,
                                             cmap=self.colors)
            html_map = hv.renderer("matplotlib").html(hv_img)
        else:
            hv_img = hv.Image(hv.Dataset(dcrop),
                              kdims=["longitude",
                                     "latitude"]).opts(colorbar=True,
                                                       color_levels=30)
            html_map = hv.renderer("matplotlib").html(hv_img)

    # Si besoin de specifier le range : hv_img.redim.__getattribute__("range")(**{var_name:(self.vmin,self.vmax)}))

        dstack = dcrop.stack(z=("latitude", "longitude")).reset_index("z")
        dstack["z"] = range(len(dstack.z))
        dstack.name = self.da.name
        hv_dst = hv.Dataset(dstack)
        k = histogram(hv_dst, dynamic=True, name="Test")

        html = ''' <h4> Histogramme sur {} </h4> de {}'''.format(
            self.hist_name, self.variable)
        self.html2.value = html + hv.renderer("matplotlib").html(k) + html_map
Ejemplo n.º 3
0
    def _update_filter(self):
        print('update_filter:',self.select_file.value,self.ds.isel(time_slice=self.time_slice_deep_slider.value).amplitude.values.shape,self.filter_.shape)
        if self.selection.selection_expr is not None:
            hvds = hv.Dataset(
                (
                    np.linspace(-0.5, 0.5, self.filter_.shape[1]),
                    np.linspace(-0.5, 0.5, self.filter_.shape[0]),
                    np.zeros(self.filter_.shape),
                ),
                ["x", "y"],
                "val",
            )
            hvds = hv.Dataset(hvds.dframe())
            hvds.data["val"].loc[
                hvds.select(self.selection.selection_expr).data.index
            ] = 1
            data = hvds["val"].reshape(self.filter_.shape).copy().T[::-1]

            gauss_kernel = utils.scipy_gaussian_2D(int(self.filter_.shape[1]/40))
            filter00 = signal.fftconvolve(data, gauss_kernel, mode="same")
            filter00 = utils.normalise(filter00)

            self.filter_ = self.filter_ + filter00

        filter_ = hv.Image(self.filter_, group="filter")
        return filter_
Ejemplo n.º 4
0
def show(ds):
    if ds.data.trace.iloc[-1] == 0:
        ds = hv.Dataset([[.01, 0, 0], [0, 0, .01]],
                        kdims=['x', 'sample'],
                        vdims=['y'])
    else:
        ds = hv.Dataset(ds.data.set_index(['trace', 'sample',
                                           'variable']).value.unstack(),
                        kdims=['x', 'sample'],
                        vdims=['y'])
    return ds.to(hv.Path, kdims=['x', 'y'], vdims=[]).overlay()
Ejemplo n.º 5
0
def investigateOptimalAlgorithms(kmerId, kmerCount):
    plot.setLibrary('bokeh')

    plots  = {}
    params = {'n_components':N_PCA_COMPONENTS, 'random_state':42}
    algos  = (
        ('PCA', decomposition.PCA(**params)),
        ('LLE', manifold.LocallyLinearEmbedding(method='standard', **params)),
        ('LTSA', manifold.LocallyLinearEmbedding(method='ltsa', **params)),
        ('Hessian LLE', manifold.LocallyLinearEmbedding(method='hessian',
            n_neighbors=10, **params)),
        ('Modified LLE', manifold.LocallyLinearEmbedding(method='modified',
            **params)),
        ('tSNE', manifold.TSNE(**params)),
        ('Isomap', manifold.Isomap(n_components=N_PCA_COMPONENTS)),
        ('MDS', manifold.MDS(**params)),
        ('SE', manifold.SpectralEmbedding(**params)))

    ## Visualise data and manually determine which algorithm will be good
    for i, (name, algo) in enumerate(algos, 1):
        com     = _getComponents(algo, kmerCount)
        com     = pd.DataFrame(com, columns=PCA_DATA_COL_NAMES)
        kmerDf  = pd.concat([kmerId, com], axis=1)

        dataset = hv.Dataset(kmerDf, PCA_DATA_COL_NAMES)
        scatter = dataset.to(hv.Scatter, PCA_DATA_COL_NAMES)
        scatter.opts(opts.Scatter(size=10, show_legend=True))
        plots[name] = scatter

    plots = hv.HoloMap(plots, kdims='algo')
    plots = plots.collate()
    return plots
def distribution(df, feature, date, num_buck=10, date_freq='Q'):
    """
    Строит график распределения признака во времени

    Parameters
    ----------
    df: Объект pandas.DataFrame
    feature: Название признака числового или категориального
    date: Наименование временной переменной
    num_buck: Количество бакетов, если признак числовой
    date_freq: Частота агрегации времени
    """
    agg = df.pipe(make_bucket, feature, num_buck)\
            .assign(obj_cnt=1)\
            .groupby([pd.Grouper(key=date, freq=date_freq), 'bucket'])\
            .agg({'obj_cnt': sum})\
            .reset_index()\
            .assign(obj_total=lambda x: (x.groupby([pd.Grouper(key=date,
                                                               freq=date_freq)])['obj_cnt'].transform('sum')))\
            .assign(obj_rate=lambda x: x.obj_cnt / x.obj_total)\
            .reset_index()\
            .assign(objects_rate=lambda x:
                    x.groupby(date).apply(
                        lambda y: y.obj_rate.cumsum().to_frame())
                    .reset_index(drop=True))\
        .assign(obj_rate_u=0, obj_rate_b=lambda x: x['obj_rate'])

    data = hv.Dataset(agg,
                      kdims=['bucket', date],
                      vdims=['objects_rate', 'obj_rate_b', 'obj_rate_u'])

    return data.to.spread(kdims=[date],
                          vdims=['objects_rate', 'obj_rate_b', 'obj_rate_u'],
                          group='Objects rate',
                          label=feature).overlay('bucket')
Ejemplo n.º 7
0
    def __post_init__(self):
        """
        :return:
        """
        data = self.spectral_cube.data

        self.ds = hv.Dataset((np.arange(data.shape[2]), np.arange(
            data.shape[1]), np.arange(data.shape[0]), data),
                             [self.spectral_axis_name, 'x', 'y'], 'Cube')
        # maybe PolyEdit as well
        # polys = hv.Polygons([hv.Box(int(self.image_width / 2), int(self.image_height / 2), int(self.image_height / 2))])
        # self.box_stream = streams.PolyEdit(source=polys)
        polys = hv.Polygons([])
        self.box_stream = streams.BoxEdit(source=polys)

        hlines = hv.HoloMap({i: hv.VLine(i)
                             for i in range(data.shape[2])}, 'wavelengths')
        dmap = hv.DynamicMap(self.roi_curves, streams=[self.box_stream])

        im = self.ds.to(hv.Image, ['x', 'y'], dynamic=True)
        self.layout = (im * polys + dmap * hlines).opts(
            opts.Image(cmap=self.color_map,
                       width=self.image_width,
                       height=self.image_height),
            opts.Curve(width=650, height=450, framewise=True),
            opts.Polygons(fill_alpha=0.2, line_color='white'),
            opts.VLine(color='black'))
    def plot(self, category_name: str) -> hv.Layout:
        """Plot the titration curve as a function of pH.

        Parameters
        ----------
        category 
        """
        category_data: Optional[np.ndarray] = None
        if category_name == 'population':
            category_data = self.populations
        elif category_name == 'free_energy':
            category_data = self.free_energies
        else:
            raise ValueError(
                "Pick population or free_energy as category names.")

        data_series = dict(pH=self.ph_values)
        state_vars = []
        for s, state in enumerate(category_data):
            if self._state_ids is None:
                state_var = "{}".format(s + 1)
            else:
                state_var = self._state_ids[s]
            data_series[state_var] = state
            state_vars.append(state_var)

        df = pd.DataFrame.from_dict(data_series)
        df_pop = df.melt(id_vars=["pH"],
                         value_vars=state_vars,
                         var_name="State",
                         value_name=category_name)
        ds_pop = hv.Dataset(df_pop, vdims=category_name)
        return ds_pop.to(hv.Curve, 'pH', groupby='State').overlay()
Ejemplo n.º 9
0
def covid_viewer_v2(ds):
    '''
    covid viewer, for actives_vs_beds
    '''
    opts.defaults(
        opts.Curve(tools=['hover'], width=800, height = 600, ylabel='')
    )
    logtog = pn.widgets.Toggle(name='Log (Y-axis)', button_type='default', value=False)
    xlim=(np.datetime64('2020-03-01'), np.datetime64('2020-03-25'))


    hv_ds = hv.Dataset(ds, ['date', 'place'], ['active_per_beds'])
    avb = hv_ds.to(hv.Curve, 'date', 'active_per_beds').overlay('place').opts(
        legend_position='top_left', shared_axes=True,
        ylim=(0, 0.13),
        xlim=xlim, title='Severe Cases per Open Hospital Bed')
    avb_log = hv_ds.to(hv.Curve, 'date', 'active_per_beds').overlay('place').opts(
        legend_position='top_left', shared_axes=True, logy=True,
        ylim=(1e-6, 10),
        xlim=xlim, title='Severe Cases per Open Hospital Bed (Log Scale)')
    max_line = hv.HLine(1).opts( opts.HLine(color='red', line_width=6),
                                opts.Points(color='#D3D3D3'))


    # layout = (avb_log)
    # layout.opts(
    #     opts.Curve(width=400, height=300, framewise=True))
    # pn_layout = pn.pane.HoloViews(layout)
    # return pn.Row(logtog, pn_layout)
    return avb
Ejemplo n.º 10
0
def GenDepth(sonar_data, chan_id, regen_cache=False):
    normalized_channel = NormalizeChannel(sonar_data,
                                          chan_id,
                                          regen_cache=regen_cache)

    hv_ds = holoviews.Dataset(normalized_channel)
    img = hv_ds.to(holoviews.Image, kdims=["frame_index", "depth"])
    img = img.opts(cmap='viridis', logz=False)
    img = img.opts(tools=['hover', 'crosshair'])

    channel = sonar_data.sel(channel=chan_id)
    depth_data = holoviews.Table((channel.frame_index, channel.water_depth),
                                 'frame_index', 'depth')
    depth_curve = holoviews.Curve(depth_data)
    depth_curve = depth_curve.opts(line_width=0.5, color='red',
                                   alpha=0.5)  #, line_dash='dashed')
    depth_curve = depth_curve.opts(tools=['hover', 'crosshair'])
    depth_curve = depth_curve.opts(active_tools=['box_zoom'])

    # @todo We should consider using the native height instead of 1024 as we will see more detail.
    #y_size = len(normalized_channel.depth)
    x_size = 1024
    y_size = 768
    rasterized_img = holoviews.operation.datashader.rasterize(img,
                                                              width=x_size,
                                                              height=y_size,
                                                              precompute=True)
    rasterized_img = rasterized_img.opts(invert_yaxis=True)

    graph = holoviews.Overlay([rasterized_img, depth_curve])
    graph = graph.collate()

    return graph
Ejemplo n.º 11
0
def select_tg(data_dir, tgs, index):
    # https://github.com/holoviz/hvplot/issues/180
    hover = HoverTool(
        tooltips=[("Time", "@Time{%F}"), ("Elevation", "@Elevation")],
        formatters={"@Time":
                    "datetime"},  # use 'datetime' formatter for '@date' field
    )
    if not index:
        name = ""
        df = pd.DataFrame({"Time": [], "Elevation": []})
        df = df.hvplot("Time",
                       "Elevation",
                       color="green",
                       width=833,
                       height=250,
                       padding=0.1)
    else:
        name = tgs.data.iloc[index[0]].Name
        df = get_station_dataframe(data_dir, name)
    dataset = hv.Dataset(df)
    curve = hv.Curve(dataset, kdims=["Time"], vdims=["Elevation"])
    curve = curve.opts(color="green",
                       width=833,
                       height=350,
                       padding=0.1,
                       framewise=True,
                       xformatter=DTF,
                       title=name,
                       tools=[hover])
    return curve
Ejemplo n.º 12
0
    def _flow_to_sankey(self, match, flow):
        nodes = []
        node_mapping = collections.defaultdict(dict)
        spans = {'s': match.doc_span, 't': match.query}

        def token(name, i):
            idx = node_mapping[name]
            k = idx.get(i)
            if k is not None:
                return k
            idx[i] = len(nodes)
            nodes.append(' %s [%d] ' % (spans[name][i].text, i))
            return idx[i]

        edges = [(token('t', t), token('s', s), f)
                 for t, s, f in flow_edges(flow, self._cutoff)]

        if len(edges) < 1:
            logging.warning("no edges found")

        n = max(len(set(x[0] for x in edges)), len(set(x[1] for x in edges)))

        nodes = hv.Dataset(enumerate(nodes), 'index', 'label')
        return hv.Sankey((edges, nodes)).opts(width=self._width,
                                              height=n * self._height_per_node,
                                              labels='label',
                                              label_position='inner',
                                              cmap=self._cmap,
                                              node_padding=self._node_padding,
                                              show_values=False)
Ejemplo n.º 13
0
def investigateOptimalAlgorithms(kmerId, kmerPca):
    plot.setLibrary('bokeh')

    pca   = kmerPca.loc[:, PCA_DATA_COL_NAMES]
    plots = {}
    algos = (
        ('Elliptic', EllipticEnvelope()),
        ('SVM', OneClassSVM()),
        ('Forest', IsolationForest()),
        ('Local', LocalOutlierFactor()))

    ## Visualise data and manually determine which algorithm will be good
    for i, (name, algo) in enumerate(algos, 1):
        labels   = _getLabels(algo, pca)
        labels   = pd.DataFrame(labels, columns=[OLABEL_COL_NAME])
        kmerDf   = pd.concat([kmerId, pca, labels], axis=1)

        dataset  = hv.Dataset(kmerDf, PCA_DATA_COL_NAMES)
        scatter  = dataset.to(hv.Scatter, PCA_DATA_COL_NAMES, groupby=OLABEL_COL_NAME).overlay()
        scatter.opts(opts.Scatter(size=10, show_legend=True))
        plots[name] = scatter

    plots = hv.HoloMap(plots, kdims='algo')
    plots = plots.collate()
    return plots
Ejemplo n.º 14
0
def test_split_flights_raises_AttributeError():
    """This occurs when the input dataframe lacks the time_position column."""
    df_in = pd.DataFrame({"latitude": [51.509865], "longitude": [-0.118092]})
    df = exp_hdf5.transform_coords(df_in)
    dataset = hv.Dataset(df)
    with pytest.raises(AttributeError):
        exp_hdf5.split_flights(dataset)
Ejemplo n.º 15
0
def make_sankey_data(data, year_range, number):
    '''this function generate the data for the slank chart given 
    the year and value thresholds'''

    # select year range
    data_y = year_range_sum(data=data, year_range=(1990, 2016))

    # select data over a particular value
    data_values = data_y[data_y['value'] > number].reset_index(
        drop=True).dropna()

    data_values['value'] = np.round(data_values['value'] / 1000)
    data_values['value'] = data_values['value'].astype('int')

    # this function generates the nodes dataset based on to and from variables
    data, nodes = gen_nodes(data=data_values)

    data.dropna(inplace=True)
    nodes.dropna(inplace=True)

    # add in unique identifiers from 0 to len(nodes)
    data_edges = add_ids(data=data, nodes=nodes)

    # define the edges (connection of the data)
    edges = zip_edges(data_edges)

    # place nodes in holoviews dataset for plotting
    nodes_plot = hv.Dataset(enumerate(nodes['country_name']), 'index', 'label')

    # return edges and nodes to plot
    return edges, nodes_plot
Ejemplo n.º 16
0
    def slider(self,
               data,
               kdims=['z', 'x', 'y'],
               vdims=['v'],
               flat=['x', 'y'],
               slider='z',
               invert_yaxis=False,
               sub_sample=4):
        '''Test for interactive display 3D grid with slider. 

        '''
        import holoviews as hv
        hv.extension('bokeh', logo=False)

        data = self._user_to_array(data)

        ds = hv.Dataset((np.arange(
            np.shape(data)[2]), np.linspace(
                0., 1.,
                np.shape(data)[1]), np.linspace(0., 1.,
                                                np.shape(data)[0]), data),
                        kdims=kdims,
                        vdims=vdims)
        return ds.to(hv.Image, flat).redim(slider).options(
            colorbar=True, invert_yaxis=invert_yaxis).hist()
Ejemplo n.º 17
0
 def evaluate(*args, **kwargs):
     ds = hv.Dataset(self._obj)
     obj = self._transform.apply(ds, keep_index=True, compute=False)
     if self._plot:
         return Interactive._fig
     else:
         return obj
Ejemplo n.º 18
0
def investigateOptimalAlgorithms(kmerId, kmerPca):
    plot.setLibrary('bokeh')

    pca = kmerPca.loc[:, PCA_DATA_COL_NAMES]
    plots = {}
    algos = (('KMeans', cluster.KMeans()), ('Affinity',
                                            cluster.AffinityPropagation()),
             ('MeanShift',
              cluster.MeanShift()), ('Spectral', cluster.SpectralClustering()),
             ('Agglomerative',
              cluster.AgglomerativeClustering(linkage='average')),
             ('Agglomerative',
              cluster.AgglomerativeClustering(linkage='ward')),
             ('DBSCAN', cluster.DBSCAN()), ('Gaussian', GaussianMixture()))

    ## Visualise data and manually determine which algorithm will be good
    for i, (name, algo) in enumerate(algos, 1):
        labels = _getLabels(algo, pca)
        labels = pd.DataFrame(labels, columns=[CLABEL_COL_NAME])
        kmerDf = pd.concat([kmerId, pca, labels], axis=1)

        dataset = hv.Dataset(kmerDf, PCA_DATA_COL_NAMES)
        scatter = dataset.to(hv.Scatter,
                             PCA_DATA_COL_NAMES,
                             groupby=CLABEL_COL_NAME).overlay()
        scatter.opts(opts.Scatter(size=10, show_legend=True))
        plots[name] = scatter

    plots = hv.HoloMap(plots, kdims='algo')
    plots = plots.collate()
    return plots
Ejemplo n.º 19
0
 def __init__(self,
              obj,
              transform=None,
              plot=False,
              depth=0,
              loc='top_left',
              center=False,
              dmap=False,
              inherit_kwargs={},
              **kwargs):
     self._obj = obj
     self._method = None
     if transform is None:
         import xarray as xr
         if isinstance(obj, xr.DataArray):
             self._transform = hv.dim(obj.name)
         else:
             self._transform = hv.dim('*')
     else:
         self._transform = transform
     self._plot = plot
     self._depth = depth
     self._loc = loc
     self._center = center
     self._dmap = dmap
     self._inherit_kwargs = inherit_kwargs
     self._kwargs = kwargs
     ds = hv.Dataset(self._obj)
     self._current = self._transform.apply(ds,
                                           keep_index=True,
                                           compute=False)
Ejemplo n.º 20
0
def make_spatio_temporal_vel_curve_obj(spatio_temporal_vel_data_tab_df):
    kdims = [('starttime', 'Date'), ('station', 'Station')]
    vdims = [("e_max", "Velocity Changes (%)")]
    emax_avg_ds = hv.Dataset(spatio_temporal_vel_data_tab_df, kdims, vdims)
    spatio_temporal_vel_curve_obj = emax_avg_ds.to(hv.Curve, "starttime",
                                                   "e_max")
    return (spatio_temporal_vel_curve_obj)
Ejemplo n.º 21
0
    def pmt_map(t_0, t_1, array='top', **kwargs):
        # Compute the PMT pattern (fast)
        ps = points[(t_0 <= points['time']) & (points['time'] < t_1)]
        areas = np.bincount(ps['channel'],
                            weights=ps['area'],
                            minlength=len(pmt_locs))

        # Which PMTs should we include?
        pmt_mask = pmt_locs['array'] == array
        d = pmt_locs[pmt_mask].copy()
        d['area'] = areas[pmt_mask]

        # Convert to holoviews points
        d = hv.Dataset(d,
                       kdims=[
                           hv.Dimension('x',
                                        unit='cm',
                                        range=(-tpc_r * f, tpc_r * f)),
                           hv.Dimension('y',
                                        unit='cm',
                                        range=(-tpc_r * f, tpc_r * f)),
                           hv.Dimension('i', label='PMT number'),
                           hv.Dimension('area', label='Area', unit='PE')
                       ])

        return d.to(hv.Points,
                    vdims=['area', 'i'],
                    group='PMTPattern',
                    label=array.capitalize(),
                    **kwargs).opts(plot=dict(color_index=2,
                                             tools=['hover'],
                                             show_grid=False),
                                   style=dict(size=17, cmap='magma'))
def _get_linked_plots(backend: str = "plotly") -> Tuple:
    """Returns a tuple (scatter, hist) of linked plots

    Args:
        backend (str, optional): "plotly" or "bokeh". Defaults to "plotly".

    Returns:
        [Tuple]: Returns a tuple (scatter, hist) of linked plots
    """

    dataset = hv.Dataset(IRIS_DATASET)

    scatter = hv.Scatter(dataset,
                         kdims=["sepal_length"],
                         vdims=["sepal_width"])
    hist = hv.operation.histogram(dataset,
                                  dimension="petal_width",
                                  normed=False)

    # pylint: disable=no-value-for-parameter
    selection_linker = hv.selection.link_selections.instance()
    scatter = selection_linker(scatter).opts(
        opts.Scatter(**OPTS["all"]["scatter"], **OPTS[backend]["scatter"]))
    hist = selection_linker(hist).opts(
        opts.Histogram(**OPTS["all"]["hist"], **OPTS[backend]["hist"]))

    return scatter, hist
Ejemplo n.º 23
0
def create_hv_dataset(df, stats, percentile=(1, 99)):

    _idNames = ("patch", "tract", "filter")
    _kdims = ("ra", "dec", "psfMag")
    _flags = [c for c in df.columns if df[c].dtype == np.dtype("bool")]

    kdims = []
    vdims = []

    for c in df.columns:
        if c in _kdims or c in _idNames or c in _flags:
            if c in ("ra", "dec", "psfMag"):
                cmin, cmax = stats[c]["min"].min(), stats[c]["max"].max()
                c = hv.Dimension(c, range=(cmin, cmax))
            elif c in ("filter", "patch", "tract"):
                cvalues = list(df[c].unique())
                c = hv.Dimension(c, values=cvalues)
            elif df[c].dtype.kind == "b":
                c = hv.Dimension(c, values=[True, False])
            kdims.append(c)
        else:
            if percentile is not None:
                p0, p1 = percentile
                if c in stats.columns and f"{p0}%" in stats.index and f"{p1}%" in stats.index:
                    cmin, cmax = stats[c][f"{p0}%"].min(), stats[c][f"{p1}%"].max()
                else:
                    print("percentiles not found in stats, computing")
                    darray = df[c].values
                    cmin, cmax = (np.percentile(darray, p0), np.percentile(darray, p1))
            else:
                cmin, cmax = stats[c]["min"].min(), stats[c]["max"].max()
            c = hv.Dimension(c, range=(cmin, cmax))
            vdims.append(c)

    return hv.Dataset(df, kdims=kdims, vdims=vdims)
Ejemplo n.º 24
0
def generate_chord_diagram(responses_count, thr_count=5):

    # generate dataframes as required for the plotting function
    plot_data = responses_count.loc[responses_count['count'] > 0,
                                    ['index', 'target', 'count']]
    plot_data.columns = ['source', 'target', 'value']
    plot_data.index = np.arange(len(plot_data))

    nodes = responses_count.loc[responses_count['count']>0, ['index', 'screen_name', 'party']].\
                            drop_duplicates().set_index('index').sort_index(level=0)
    nodes = hv.Dataset(nodes, 'index')
    nodes.data.head()

    # generate colormap for single accounts according to party affiliations
    person_party_cmap = dict(
        zip(responses_count['index'],
            responses_count['party'].apply(lambda row: party_cmap[row])))

    # generate plot
    chord = hv.Chord((plot_data, nodes)).select(value=(thr_count, None))
    chord.opts(
        hv_opts.Chord(cmap=party_cmap,
                      edge_cmap=person_party_cmap,
                      edge_color=hv_dim('source'),
                      labels='screen_name',
                      node_color=hv_dim('party'),
                      edge_hover_line_color='cyan',
                      node_hover_fill_color='cyan',
                      height=700,
                      width=700))

    return chord
Ejemplo n.º 25
0
def modify_doc(doc, mytabs):

	start, end = 1, 20
	samples_count = 5
	slider = Slider(start=start, end=end, value=start, step=1, title="Counts")
	select = Select(title="Count", value="aux", options=["box", "pack", "image", "user"])

	renderer = hv.renderer('bokeh')##.instance(mode='server')
	hv.extension('bokeh')
	hv.output(size=200)
	links = pd.DataFrame(data['links'])
	print(links.head(3))
	nodes = hv.Dataset(pd.DataFrame(data['nodes']), 'index')
	chord = hv.Chord((links, nodes)).select(value=(samples_count, None))
	chord.opts(opts.Chord(cmap='Category20', edge_cmap='Category20', edge_color=dim('source').str(), labels='name', node_color=dim('index').str()))    	
	# Create HoloViews plot and attach the document
	hvplot = renderer.get_plot(chord, doc)
	
	def slider_update(attrname, old, new):
		# Notify the HoloViews stream of the slider update 
		print ("update received")
		samples_count = new
		
		links = pd.DataFrame(data['links'])
		print(links.head(3))
		nodes = hv.Dataset(pd.DataFrame(data['nodes']), 'index')
		chord = hv.Chord((links, nodes)).select(value=(samples_count, None))
		chord.opts(opts.Chord(cmap='Category20', edge_cmap='Category20', edge_color=dim('source').str(), labels='name', node_color=dim('index').str()))    	
		# Create HoloViews plot and attach the document
		hvplot = renderer.get_plot(chord, doc)
		tab3 = Panel(child=row(slider, hvplot.state), title="Chord Plot")

		mytabs.append(tab3)
		views = Tabs(tabs = mytabs)
		layout=row(views)
		doc.add_root(layout)
	
		return doc

	slider.on_change('value', slider_update)

	
	def select_update(attrname, old, new):
		# Notify the HoloViews stream of the slider update 
		print ("update received. Old: {} New: {}".format(old, new))
		

	select.on_change('value', select_update)		

	# Combine the holoviews plot and widgets in a layout

	tab3 = Panel(child=row(slider, hvplot.state), title="Chord Plot")

	mytabs.append(tab3)
	views = Tabs(tabs = mytabs)
	layout=row(views)
	doc.add_root(layout)
	
	return doc
Ejemplo n.º 26
0
 def hist(self):
     ds = hv.Dataset(self.data.to_frame())
     plot_opts = dict(self._plot_opts)
     plot_opts['invert_axes'] = self.kwds.get('orientation') == 'horizontal'
     opts = dict(plot=plot_opts,
                 style=dict(alpha=self.kwds.get('alpha', 1)))
     return hv.operation.histogram(ds,
                                   dimension=self.data.name).opts(**opts)
Ejemplo n.º 27
0
def parallel_coordinates(data,
                         class_column,
                         cols=None,
                         alpha=0.5,
                         width=600,
                         height=300,
                         var_name='variable',
                         value_name='value',
                         **kwds):
    """
    Parallel coordinates plotting.

    Parameters
    ----------
    frame: DataFrame
    class_column: str
        Column name containing class names
    cols: list, optional
        A list of column names to use
    alpha: float, optional
        The transparency of the lines

    Returns
    -------
    obj : HoloViews object
        The HoloViews representation of the plot.

    See Also
    --------
    pandas.plotting.parallel_coordinates : matplotlib version of this routine
    """
    # Transform the dataframe to be used in Vega-Lite
    if cols is not None:
        data = data[list(cols) + [class_column]]
    cols = data.columns
    df = data.reset_index()
    index = (set(df.columns) - set(cols)).pop()
    assert index in df.columns
    df = df.melt([index, class_column],
                 var_name=var_name,
                 value_name=value_name)

    labelled = [] if var_name == 'variable' else ['x']
    if value_name != 'value':
        labelled.append('y')
    options = {
        'Curve':
        dict(kwds, labelled=labelled, alpha=alpha, width=width, height=height),
        'Overlay':
        dict(legend_limit=5000)
    }
    colors = _hv.plotting.util.process_cmap('Category10', categorical=True)
    dataset = _hv.Dataset(df)
    groups = dataset.to(_hv.Curve, var_name, value_name).overlay(index).items()
    return _hv.Overlay([
        curve.relabel(k).options('Curve', color=c)
        for c, (k, v) in zip(colors, groups) for curve in v
    ]).options(options)
Ejemplo n.º 28
0
 def random_allocation_cb(n_samples):
     all_weights, ret_arr, vol_arr, sharpe_arr = random_allocation(
         stocks.values, stocks.shift(1).values, num_ports=n_samples)
     vdims = [
         *('%s Weight' % c for c in stocks.columns), 'Return', 'Volatility',
         'Sharpe Ratio'
     ]
     return hv.Dataset((*all_weights.T, ret_arr, vol_arr, sharpe_arr),
                       vdims=vdims)
Ejemplo n.º 29
0
def scatter(library, kmerPca):
    ## Create plots
    d = hv.Dataset(kmerPca, ml.PCA_DATA_COL_NAMES)
    s = d.to(hv.Scatter, ml.PCA_DATA_COL_NAMES, groupby='class').overlay()

    ## Style plots
    options = _getOptions(library)
    s.opts(options)
    return s
Ejemplo n.º 30
0
def hv_imshow_stack(X,cmap = 'gray',scale=.8,title='dataset',timelabel = 'frame'):
    import holoviews as hv
    hv.extension('bokeh')
    d = X.shape
    if len(d) == 4:
        ds = hv.Dataset((np.arange(d[3]), np.arange(d[2]), np.arange(d[1]),np.arange(d[0]),
                         X),
                        ['h','w', 'ch', timelabel], title)
    elif len(d) == 3:
        ds = hv.Dataset((np.arange(d[2]), np.arange(d[1]),np.arange(d[0]),
                         X),
                        ['h','w', timelabel], title)
    else:
        print('imshow_stack only works for 3d and 4d stacks.')
        return None
    im = ds.to(hv.Image, ['h', 'w'],dynamic=True)
    im.opts(cmap=cmap,width=int(d[-1]*scale),height=int(d[-2]*scale))
    return im