예제 #1
0
    def gen_real_view(self, resetting, x_range, y_range):

        if x_range is None:
            x_range = self.xrange
        if y_range is None:
            y_range = self.yrange0
        # ipdb.set_trace()
        if resetting:
            return dynspread(datashade(self.real_curve,
                                       cmap='blue',
                                       normalization='linear',
                                       x_range=self.xrange,
                                       y_range=self.yrange0,
                                       min_alpha=self.min_alpha,
                                       dynamic=False),
                             max_px=max_px)
        else:
            return dynspread(datashade(self.real_curve,
                                       cmap='blue',
                                       normalization='linear',
                                       x_range=x_range,
                                       y_range=y_range,
                                       min_alpha=self.min_alpha,
                                       dynamic=False),
                             max_px=max_px)
예제 #2
0
def plot_phase_diagrams(filenames,fileout):
    for i in range(len(filenames)):
        print 'i: ',i
        ds = yt.load(filenames[i])
        center_guess = initial_center_guess(ds,track_name)
        halo_center = get_halo_center(ds,center_guess)
        rb = sym_refine_box(ds,halo_center)

        args = filenames[i].split('/')
        sim_label = args[-3]

        dens = np.log10(rb['H_nuclei_density'])
        temp = np.log10(rb['Temperature'])

        df = pd.DataFrame({'temp':temp, 'dens':dens})
        phase_scatter = hv.Scatter(df,kdims=['dens'],vdims=['temp'],label=sim_label)

        #phase_data = np.zeros((len(rb['H_nuclei_density']),2))
        #phase_data[:,0] = np.log10(rb['H_nuclei_density'])
        #phase_data[:,1] = np.log10(rb['Temperature'])

        #points = hv.Points(phase_data,kdims=['nH','temp'],label=sim_label)
        hv.opts({'Histogram': {'style': {'alpha':0.3, 'fill_color':'k'}}})
        xhist = (histogram(phase_scatter, bin_range=(-7.5, 1), dimension='dens',normed=True)) #,alpha=0.3, fill_color='k'))
        yhist = (histogram(phase_scatter, bin_range=(3, 8.5), dimension='temp',normed=True)) #,alpha=0.3, fill_color='k'))

        if i == 0:
            phase_plot = (datashade(phase_scatter,cmap=cm.plasma, dynamic=False,x_range=(-7.5,1),y_range=(3,8.5))) << yhist(plot=dict(width=125)) << xhist(plot=dict(height=125))
        else:
            plot2 = (datashade(phase_scatter,cmap=cm.plasma, dynamic=False,x_range=(-7.5,1),y_range=(3,8.5))) << yhist(plot=dict(width=125)) << xhist(plot=dict(height=125))
            phase_plot = phase_plot + plot2

        renderer = hv.renderer('bokeh').instance(fig='html')
        renderer.save(phase_plot, fileout)
    return
예제 #3
0
    def grid(self, tiles=False, **kwargs):

        x = kwargs.get("x", self._obj.SCHISM_hgrid_node_x[:].values)
        y = kwargs.get("y", self._obj.SCHISM_hgrid_node_y[:].values)
        tes = kwargs.get("tri3", self._obj.SCHISM_hgrid_face_nodes.values)

        width = kwargs.get("width", 800)
        height = kwargs.get("height", 600)
        opts.defaults(opts.WMTS(width=width, height=height))

        tile = gv.WMTS("https://b.tile.openstreetmap.org/{Z}/{X}/{Y}.png")

        nodes = pd.DataFrame({"longitude": x, "latitude": y})

        points = gv.operation.project_points(gv.Points(nodes))

        if tes.shape[1] == 3:

            elems = pd.DataFrame(tes, columns=["a", "b", "c"])

            trimesh = gv.TriMesh((elems, points)).edgepaths

            if tiles:
                return tile * datashade(
                    trimesh, precompute=True, cmap=["black"])
            else:
                return datashade(trimesh, precompute=True,
                                 cmap=["green"]).opts(width=width,
                                                      height=height)

        else:  # there are quads

            elems = pd.DataFrame(tes, columns=["a", "b", "c", "d"])

            quads = elems.loc[~elems.d.isna()].copy()
            quads = quads.reset_index(drop=True)
            ap = nodes.loc[quads.a, ["longitude", "latitude"]]
            bp = nodes.loc[quads.b, ["longitude", "latitude"]]
            cp = nodes.loc[quads.c, ["longitude", "latitude"]]
            dp = nodes.loc[quads.d, ["longitude", "latitude"]]
            quads["ap"] = ap.values.tolist()
            quads["bp"] = bp.values.tolist()
            quads["cp"] = cp.values.tolist()
            quads["dp"] = dp.values.tolist()

            q = gv.Polygons([
                quads.loc[i, ["ap", "bp", "cp", "dp"]].tolist()
                for i in range(quads.shape[0])
            ]).options(fill_alpha=0, line_color="black")

            triangles = elems.loc[elems.d.isna()]

            trimesh = gv.TriMesh((triangles, points)).edgepaths

            g1 = datashade(trimesh, precompute=True, cmap=["black"])

            if tiles:
                return tile * g1 * q
            else:
                return g1 * q
예제 #4
0
    def view_datashade(self):
        """
        Use of datashade for performance and line for hover tool capabilities
        :return: Panel of this combination
        """
        # Select only sufficient data
        if self.x in self.y:
            self.y.remove(self.x)
        if self.y == []:
            return self.gif

        df = self.dataframe[[self.x] + self.y].copy()
        plot_opts = {
            'Scatter': {
                'color': self.color_key,
                'marker': self.marker_keys,
                'size': 10
            },
            'Curve': {
                'color': self.color_key
            }
        }
        lines_overlay = df.hvplot.scatter(
            **self.plot_options).options(plot_opts)

        def hover_curve(x_range=[df.index.min(),
                                 df.index.max()]):  # , y_range):
            # Compute
            dataframe = df.copy()
            if x_range is not None:
                dataframe = dataframe[(dataframe[self.x] > x_range[0])
                                      & (dataframe[self.x] < x_range[1])]
            data_length = len(dataframe) * len(dataframe.columns)
            step = 1 if data_length < self.max_step else data_length // self.max_step

            plot_df = dataframe[::step].hvplot.line(**self.plot_options) * \
                dataframe[::step*60].hvplot.scatter(**self.plot_options)
            plot_opts = {
                'Scatter': {
                    'color': 'k',
                    'marker': self.marker_keys,
                    'size': 10
                },
                'Curve': {
                    'color': self.color_key
                }
            }
            if len(self.y) != 1:
                plot_opts['Scatter']['color'] = self.color_key
            return plot_df.options(plot_opts)

        # Define a RangeXY stream linked to the image
        rangex = hv.streams.RangeX(source=lines_overlay)
        data_shade_plot = hv.DynamicMap(hover_curve, streams=[rangex])
        if len(self.y) == 1:
            data_shade_plot *= datashade(lines_overlay)
        else:
            data_shade_plot *= datashade(lines_overlay,
                                         aggregator=ds.count_cat('Variable'))
        return pn.panel(data_shade_plot)
예제 #5
0
    def _get_points(self):
        embeddings = self.embeddings
        classes = self.classes
        if (self.label_flag) and (classes is not None):
            data = pd.DataFrame(embeddings)
            data.columns = ['ivis 1', 'ivis 2']
            data['label'] = classes
            num_ks = len(np.unique(classes))
            color_key = list(enumerate(Sets1to3[0:num_ks]))

            embed = {
                k: hv.Points(data.values[classes == k, :],
                             ['ivis 1', 'ivis 2'],
                             'k',
                             label=str(k)).opts(color=v, size=0)
                for k, v in color_key
            }
            dse = dynspread(
                datashade(hv.NdOverlay(embed, kdims=['k']),
                          aggregator=ds.by('k', ds.count())))
            color_points = hv.NdOverlay({
                k: hv.Points([0, 0]).opts(color=v, size=0)
                for k, v in color_key
            })
            points = color_points * dse
        else:
            points = datashade(hv.Points(embeddings))

        points.opts(height=400, width=500, xaxis=None, yaxis=None)
        return points
예제 #6
0
    def test_datashade_in_overlay_selection(self):
        points = Points(self.data)
        layout = points * dynspread(datashade(points))

        lnk_sel = link_selections.instance(unselected_color='#ff0000')
        linked = lnk_sel(layout)
        current_obj = linked[()]

        # Check base points layer
        self.check_base_points_like(current_obj[()].Points.I, lnk_sel)

        # Check selection layer
        self.check_overlay_points_like(current_obj[()].Points.II, lnk_sel,
                                       self.data)

        # Check RGB base layer
        self.assertEqual(
            current_obj[()].RGB.I,
            dynspread(
                datashade(points, cmap=lnk_sel.unselected_cmap,
                          alpha=255))[()])

        # Check RGB selection layer
        self.assertEqual(
            current_obj[()].RGB.II,
            dynspread(datashade(points, cmap=lnk_sel.selected_cmap,
                                alpha=255))[()])

        # Perform selection of second and third point
        selectionxy = TestLinkSelections.get_value_with_key_type(
            lnk_sel._selection_expr_streams,
            hv.Points).input_streams[0].input_stream.input_streams[0]

        self.assertIsInstance(selectionxy, SelectionXY)
        selectionxy.event(bounds=(0, 1, 5, 5))
        current_obj = linked[()]

        # Check that base points layer is unchanged
        self.check_base_points_like(current_obj[()].Points.I, lnk_sel)

        # Check points selection layer
        self.check_overlay_points_like(current_obj[()].Points.II, lnk_sel,
                                       self.data.iloc[1:])

        # Check that base RGB layer is unchanged
        self.assertEqual(
            current_obj[()].RGB.I,
            dynspread(
                datashade(points, cmap=lnk_sel.unselected_cmap,
                          alpha=255))[()])

        # Check selection RGB layer
        self.assertEqual(
            current_obj[()].RGB.II,
            dynspread(
                datashade(points.iloc[1:],
                          cmap=lnk_sel.selected_cmap,
                          alpha=255))[()])
예제 #7
0
    def grid(self, tiles=False, **kwargs):

        x = kwargs.get('x',self._obj.SCHISM_hgrid_node_x[:].values)
        y = kwargs.get('y',self._obj.SCHISM_hgrid_node_y[:].values)
        tes = kwargs.get('tri3',self._obj.SCHISM_hgrid_face_nodes.values)

        width = kwargs.get('width', 800)
        height = kwargs.get('height', 600)
        opts.defaults(opts.WMTS(width=width, height=height))

        tile = gv.WMTS('https://b.tile.openstreetmap.org/{Z}/{X}/{Y}.png')

        nodes = pd.DataFrame({'longitude':x,'latitude':y})

        points = gv.operation.project_points(gv.Points(nodes))

        if tes.shape[1] == 3 :

            elems  = pd.DataFrame(tes, columns=['a', 'b', 'c'])

            trimesh=gv.TriMesh((elems, points)).edgepaths

            if tiles:
                return tile * datashade(trimesh, precompute=True, cmap=['black'])
            else:
                return datashade(trimesh, precompute=True, cmap=['green']).opts(width=width,height=height)


        else: # there are quads

            elems  = pd.DataFrame(tes, columns=['a', 'b', 'c', 'd'])

            quads = elems.loc[~elems.d.isna()].copy()
            quads = quads.reset_index(drop=True)
            ap = nodes.loc[quads.a,['longitude','latitude']]
            bp = nodes.loc[quads.b,['longitude','latitude']]
            cp = nodes.loc[quads.c,['longitude','latitude']]
            dp = nodes.loc[quads.d,['longitude','latitude']]
            quads['ap'] = ap.values.tolist()
            quads['bp'] = bp.values.tolist()
            quads['cp'] = cp.values.tolist()
            quads['dp'] = dp.values.tolist()

            q = gv.Polygons([quads.loc[i,['ap','bp','cp','dp']].tolist() for i in range(quads.shape[0])]).options(fill_alpha=0, line_color='black')

            triangles = elems.loc[elems.d.isna()]

            trimesh=gv.TriMesh((triangles, points)).edgepaths

            g1 = datashade(trimesh, precompute=True, cmap=['black'])

            if tiles : 
                return tile * g1 * q
            else:
                return g1 * q
예제 #8
0
    def test_datashade_selection(self):
        scatter = hv.Scatter(self.data, kdims='x', vdims='y')
        layout = scatter + dynspread(datashade(scatter))

        lnk_sel = link_selections.instance()
        linked = lnk_sel(layout)
        current_obj = linked[()]

        # Check base scatter layer
        self.check_base_scatter_like(current_obj[0][()].Scatter.I, lnk_sel)

        # Check selection layer
        self.check_overlay_scatter_like(current_obj[0][()].Scatter.II, lnk_sel,
                                        self.data)

        # Check RGB base layer
        self.assertEqual(
            current_obj[1][()].RGB.I,
            dynspread(
                datashade(scatter, cmap=lnk_sel.unselected_cmap,
                          alpha=255))[()])

        # Check RGB selection layer
        self.assertEqual(
            current_obj[1][()].RGB.II,
            dynspread(datashade(scatter, cmap=lnk_sel.selected_cmap,
                                alpha=0))[()])

        # Perform selection of second and third point
        boundsxy = lnk_sel._selection_expr_streams[0]._source_streams[0]
        self.assertIsInstance(boundsxy, hv.streams.BoundsXY)
        boundsxy.event(bounds=(0, 1, 5, 5))
        current_obj = linked[()]

        # Check that base scatter layer is unchanged
        self.check_base_scatter_like(current_obj[0][()].Scatter.I, lnk_sel)

        # Check scatter selection layer
        self.check_overlay_scatter_like(current_obj[0][()].Scatter.II, lnk_sel,
                                        self.data.iloc[1:])

        # Check that base RGB layer is unchanged
        self.assertEqual(
            current_obj[1][()].RGB.I,
            dynspread(
                datashade(scatter, cmap=lnk_sel.unselected_cmap,
                          alpha=255))[()])

        # Check selection RGB layer
        self.assertEqual(
            current_obj[1][()].RGB.II,
            dynspread(
                datashade(scatter.iloc[1:],
                          cmap=lnk_sel.selected_cmap,
                          alpha=255))[()])
예제 #9
0
def holoviews_radial_profiles(weight_by=None):
    dens = np.log10(rb['H_nuclei_density'])
    temp = np.log10(rb['Temperature'])
    Zgas = np.log10(rb['metallicity'])
    cell_mass = rb['cell_mass'].in_units('Msun')
    cell_volume = rb['cell_volume'].in_units('kpc**3')
    x = rb['x']
    y = rb['y']
    z = rb['z']

    halo_center = ds.arr(rb_center,'code_length')
    dist = np.sqrt((halo_center[0]-rb['x'])**2.+(halo_center[1]-rb['y'])**2.+(halo_center[2]-rb['z'])**2.).in_units('kpc')

    df = pd.DataFrame({'temp':temp, 'dens':dens, 'Zgas':Zgas,'cell_volume':cell_volume,
                        'x':x,'y':y,'z':z,'dist':dist,'cell_mass':cell_mass})

    temp_dist = hv.Scatter(df,kdims=['dist'],vdims=['temp'],label="Temperature ")
    dens_dist = hv.Scatter(df,kdims=['dist'],vdims=['dens'],label='Hydrogen Number Density')
    metal_dist = hv.Scatter(df,kdims=['dist'],vdims=['Zgas'],label='Metallicity')

    if weight_by == None:
        dist_plots = (datashade(temp_dist,cmap=cm.Reds, dynamic=False,x_range=(0,60),y_range=(2,8.4)).opts(plot=dict(aspect='square'))
                    + datashade(dens_dist,cmap=cm.Blues, dynamic=False,x_range=(0,60),y_range=(-6.5,2)).opts(plot=dict(aspect='square'))
                    + datashade(metal_dist,cmap=cm.BuGn, dynamic=False,x_range=(0,60),y_range=(-8.5,1.4)).opts(plot=dict(aspect='square')))
        fileout= 'basic_profile_'+args[-3]+'_'+args[-1]

    if weight_by == 'cell_mass':
        temp_shade = aggregate(hv.Scatter(df,['dist','temp']),y_range=(2,8.4),aggregator=dshade.sum('cell_mass'))
        temp_shade = temp_shade.opts(plot=dict(colorbar=True,aspect='square',logz=True),style=dict(cmap=cm.Reds))
        dens_shade = aggregate(hv.Scatter(df,['dist','dens']),y_range=(-7,2.5),aggregator=dshade.sum('cell_mass'))
        dens_shade = dens_shade.opts(plot=dict(colorbar=True,aspect='square',logz=True),style=dict(cmap=cm.Blues))
        metal_shade = aggregate(hv.Scatter(df,['dist','Zgas']),y_range=(-7,2.5),aggregator=dshade.sum('cell_mass'))
        metal_shade = metal_shade.opts(plot=dict(colorbar=True,aspect='square',logz=True),style=dict(cmap=cm.BuGn))

        dist_plots = (temp_shade + dens_shade + metal_shade)
        fileout = 'basic_profile_cell_mass_'+args[-3]+'_'+args[-1]

    if weight_by == 'cell_volume':
        temp_shade = aggregate(hv.Scatter(df,['dist','temp']),y_range=(2,8.4),aggregator=dshade.sum('cell_volume'))
        temp_shade = temp_shade.opts(plot=dict(colorbar=True,aspect='square',logz=True),style=dict(cmap=cm.Reds))
        dens_shade = aggregate(hv.Scatter(df,['dist','dens']),y_range=(-7,2.5),aggregator=dshade.sum('cell_volume'))
        dens_shade = dens_shade.opts(plot=dict(colorbar=True,aspect='square',logz=True),style=dict(cmap=cm.Blues))
        metal_shade = aggregate(hv.Scatter(df,['dist','Zgas']),y_range=(-7,2.5),aggregator=dshade.sum('cell_volume'))
        metal_shade = metal_shade.opts(plot=dict(colorbar=True,aspect='square',logz=True),style=dict(cmap=cm.BuGn))

        dist_plots = (temp_shade + dens_shade + metal_shade)
        fileout = 'basic_profile_cell_vol_'+args[-3]+'_'+args[-1]

    renderer = Store.renderers['matplotlib'].instance(fig='pdf', holomap='gif')
    renderer.save(dist_plots, fileout)
    return
예제 #10
0
def plot_performance(ds_preds, full=False):
    """Multiple plots using xr_preds"""
    p = hv_plot_prediction(ds_preds.isel(t_source=10))
    display(p)

    n = len(ds_preds.t_source)
    d_ahead = ds_preds.mean(['t_source'])['nll'].groupby('t_ahead_hours').mean()
    nll_vs_tahead = (hv.Curve(
        (d_ahead.t_ahead_hours,
         d_ahead)).redim(x='hours ahead',
                         y='nll').opts(
                                       title=f'NLL vs time ahead (no. samples={n})'))
    display(nll_vs_tahead)

    # Make a plot of the NLL over time. Does this solution get worse with time?
    if full:
        d_source = ds_preds.mean(['t_ahead'])['nll'].groupby('t_source').mean()
        nll_vs_time = (hv.Curve(d_source).opts(
                                               title='Error vs time of prediction'))
        display(nll_vs_time)

    # A scatter plot is easy with xarray
    if full:
        tlim = (ds_preds.y_true.min().item(), ds_preds.y_true.max().item())
        true_vs_pred = datashade(hv.Scatter(
            (ds_preds.y_true,
             ds_preds.y_pred))).redim(x='true', y='pred').opts(width=400,
                                                               height=400,
                                                               xlim=tlim,
                                                               ylim=tlim,
                                                               title='Scatter plot')
        true_vs_pred = dynspread(true_vs_pred)
        true_vs_pred
        display(true_vs_pred)
예제 #11
0
    def make_view(self, x_range=None, y_range=None, **kwargs):
        #map_tiles = tiles.opts(style=dict(alpha=self.alpha), plot=options)

        points = hv.Points(
            df,
            kdims=['X_CORD', 'Y_CORD'],
            vdims=['EVENT_COUNT', 'EventType', 'SUB', 'day', 'FEEDER_ID'])

        if (self.BySUB & self.ByFeeder & self.ByDay):
            selected = points.select(EventType=self.plot,
                                     EVENT_COUNT=self.numEvents,
                                     SUB=self.Substations,
                                     day=self.DayNum,
                                     FEEDER_ID=self.Feeder)
        elif (self.BySUB & self.ByFeeder & (not self.ByDay)):
            selected = points.select(EventType=self.plot,
                                     EVENT_COUNT=self.numEvents,
                                     SUB=self.Substations,
                                     FEEDER_ID=self.Feeder)
        elif (self.BySUB & (not self.ByFeeder) & self.ByDay):
            selected = points.select(EventType=self.plot,
                                     EVENT_COUNT=self.numEvents,
                                     SUB=self.Substations,
                                     day=self.DayNum)
        elif (self.BySUB & (not self.ByFeeder) & (not self.ByDay)):
            selected = points.select(EventType=self.plot,
                                     EVENT_COUNT=self.numEvents,
                                     SUB=self.Substations)
        elif ((not self.BySUB) & self.ByFeeder & self.ByDay):
            selected = points.select(EventType=self.plot,
                                     EVENT_COUNT=self.numEvents,
                                     day=self.DayNum,
                                     FEEDER_ID=self.Feeder)
        elif ((not self.BySUB) & self.ByFeeder & (not self.ByDay)):
            selected = points.select(EventType=self.plot,
                                     EVENT_COUNT=self.numEvents,
                                     FEEDER_ID=self.Feeder)
        elif ((not self.BySUB) & (not self.ByFeeder) & self.ByDay):
            selected = points.select(EventType=self.plot,
                                     EVENT_COUNT=self.numEvents,
                                     day=self.DayNum)
        else:
            selected = points.select(EventType=self.plot,
                                     EVENT_COUNT=self.numEvents)

        SagSwellPts = datashade(selected,
                                x_sampling=1,
                                y_sampling=1,
                                cmap=self.colormap,
                                dynamic=False,
                                x_range=x_range,
                                y_range=y_range,
                                width=640,
                                height=380)
        dsss = dynspread(SagSwellPts,
                         shape='circle',
                         max_px=self.maxpix,
                         threshold=self.threshhold)
        #return map_tiles * dsss
        return dsss
예제 #12
0
 def view(self):
     if datashaded and self.nodeCount > 1:
         plot = dynspread(
             datashade(self.plot,
                       normalization='linear',
                       width=self.size,
                       height=self.size,
                       cmap=self.colorMap[self.color_palette]))
         self.points.opts(cmap=self.colorMap[self.color_palette],
                          color=self.node_color,
                          size=self.node_size)
         return (plot, self.points)
     else:
         if (min(self.plot.data['weight']) < max(
                 self.plot.data['weight'])):
             self.points.opts(cmap=self.colorMap[self.color_palette],
                              color=self.node_color,
                              size=self.node_size)
             self.plot.opts(
                 edge_cmap=self.colorMap[self.color_palette],
                 edge_color='weight',
                 edge_line_width=dim('weight').norm() * 5 + 0.1,
                 edge_line_alpha=0.1 + dim('weight').norm() * 0.9)
         else:
             self.points.opts(cmap=self.colorMap[self.color_palette],
                              color=self.node_color,
                              size=self.node_size)
             self.plot.opts(edge_cmap=self.colorMap[self.color_palette],
                            edge_color=dim('weight'),
                            edge_line_width=dim('weight') * 3,
                            edge_line_alpha=dim('weight'))
     return (self.plot, self.points)
예제 #13
0
    def _process(self, element, key=None):

        vdim = self.p.vdim
        if self.p.aggregator == 'mean':
            aggregator = ds.mean(vdim)
        elif self.p.aggregator == 'std':
            aggregator = ds.std(vdim)
        elif self.p.aggregator == 'count':
            aggregator = ds.count()

        kwargs = dict(cmap=cc.palette[self.p.cmap], aggregator=aggregator)
        if self.p.width is not None:
            kwargs.update(width=self.p.width,
                          height=self.p.height,
                          streams=[hv.streams.RangeXY])

        datashaded = dynspread(datashade(element, **kwargs))

        # decimate_opts = dict(plot={'tools':['hover', 'box_select']},
        #                     style={'alpha':0, 'size':self.p.decimate_size,
        #                            'nonselection_alpha':0})

        # decimated = decimate(element, max_samples=self.p.max_samples).opts(**decimate_opts)

        return datashaded  # * decimated
예제 #14
0
    def scatter_dist_by_mappings(dataset, x_kdims, y_kdims,
                                 mappings,
                                 selection_dim="Gene",
                                 datashade_=False,
                                 dynspread_=False,
                                 ):

        data_groups = {name: dataset.sel({selection_dim: genes}) for name, genes in mappings.items()}
        data_group_dfs = {k: v[[x_kdims, y_kdims]].to_dataframe() for k, v in data_groups.items()}

        points = {k: hv.Points(val, kdims=[x_kdims, y_kdims]) for k, val in data_group_dfs.items()}

        dist_x = {k: univariate_kde(hv.Distribution(p, kdims=[y_kdims], group="dist_x"), n_samples=1000)
                  for k, p in points.items()}
        dist_y = {k: univariate_kde(hv.Distribution(p, kdims=[x_kdims], group="dist_y"), n_samples=1000)
                  for k, p in points.items()}

        if datashade_:
            points_overlay = datashade(hv.NdOverlay(points))
            if dynspread_:
                points_overlay = dynspread(points_overlay)
        else:
            points_overlay = hv.NdOverlay(points)

        return points_overlay << hv.NdOverlay(dist_x) << hv.NdOverlay(dist_y)
예제 #15
0
def shade(hv_obj, reduction='any', color=None, spread=False):
    """
    Apply datashading to a holoviews object.

    hv_obj: a holovies object like Curve, Scatter, etc.
    reduction: Most common will be 'any' and 'count'.
               Supply any name here to see list of valid reductions
    color: Mostly used for 'any' aggregation to specify a color
    spread: Smear out points slightly bigger than 1 pixel for easier
            visibility
    """
    import datashader as ds
    from holoviews.operation.datashader import datashade, dynspread

    if reduction not in ALLOWED_REDUCTIONS:
        raise ValueError(
            'Allowed reductions are {}'.format(ALLOWED_REDUCTIONS))

    reducer = getattr(ds.reductions, reduction)

    kwargs = dict(aggregator=reducer())
    if color is None and reduction == 'any':
        kwargs.update(cmap=['blue'])
    else:
        kwargs.update(cmap=[color])

    obj = datashade(hv_obj, **kwargs)
    if spread:
        obj = dynspread(obj)
    return obj
예제 #16
0
def get_net(layout, bundled, shade):
    if (layout.lower() == 'circle'):
        net = hv.Graph.from_networkx(
            G, nx.layout.circular_layout).relabel('Circular Layout').opts(
                width=650, height=650, xaxis=None, yaxis=None, padding=0.1)
    elif layout.lower() == 'spring':
        net = hv.Graph.from_networkx(
            G, nx.layout.spring_layout, k=0.8, iterations=100).relabel(
                'Force-Directed Fruchterman-Reingold').opts(width=650,
                                                            height=650,
                                                            xaxis=None,
                                                            yaxis=None,
                                                            padding=0.1)
    #             , node_size=node_dict[i] for i in node_dict.keys())
    else:
        net = "Error 1: layout type must be: 'circle', 'spring'"
    net.opts(width=650,
             height=650,
             xaxis=None,
             yaxis=None,
             padding=0.1,
             edge_color_index='weight',
             edge_cmap='jet')
    if bundled:
        net = bundle_graph(net)
    if B_edge_select:
        net.opts(inspection_policy='edges')
    if shade:
        net = hd.datashade(net).opts(plot=dict(height=650, width=650))
    return net
예제 #17
0
    def make_scatter(self, object_type, x_range=None, y_range=None, **kwargs):
        self._set_data(**kwargs)
        logging.info('x_range={}, y_range={}'.format(x_range, y_range))

        if object_type == 'all':
            dset = self.ds
        else:
            dset = self.ds.select(label=object_type)

        pts = dset.to(hv.Points, kdims=['x', 'y'], vdims=['label'], groupby=[])
        print(pts.dimensions())

        scatter = dynspread(
            datashade(pts,
                      x_range=x_range,
                      y_range=y_range,
                      dynamic=False,
                      normalization='log'))
        hover = HoverTool(tooltips=[("(x,y)", "($x, $y)")])
        # hv.opts({'RGB': {'plot' : {'tools' : [hover]}}}, scatter)
        # scatter = scatter.opts(plot=dict(tools=[hover]))

        title = '{} ({}) {}'.format(object_type, len(dset),
                                    pts.get_dimension('y').label)
        scatter = scatter.opts('RGB [width=600, height=400]').relabel(title)
        return scatter
예제 #18
0
def spectrum_hires(wno, alb, legend=None, **kwargs):
    """Plot formated albedo spectrum

    Parameters
    ----------
    wno : float array, list of arrays
        wavenumber 
    alb : float array, list of arrays 
        albedo 
    legend : list of str 
        legends for plotting 
    **kwargs : dict     
        Any key word argument for hv.opts

    Returns
    -------
    bokeh plot
    """
    import holoviews as hv
    from holoviews.operation.datashader import datashade

    hv.extension('bokeh')

    kwargs['plot_height'] = kwargs.get('plot_height', 345)
    kwargs['plot_width'] = kwargs.get('plot_width', 1000)
    kwargs['y_axis_label'] = kwargs.get('y_axis_label', 'Albedo')
    kwargs['x_axis_label'] = kwargs.get('x_axis_label', 'Wavelength [μm]')
    kwargs['y_range'] = kwargs.get('y_range', [0, 1.2])
    kwargs['x_range'] = kwargs.get('x_range', [0.3, 1])

    points_og = datashade(hv.Curve((1e4 / wno, alb)))

    return points_og
예제 #19
0
def hv_plot(G, layout='circular', method='bokeh', name='', kwargs=opts):
    '''
    Returns graph plot using HoloViews wrapper for bokeh.
    Optionally, draws edges using datashade functions.

    Accepted vars:
        G => networkx.Graph() object
        layout => circular; forceatlas2; random
        method => bokeh; datashader
        name => graph title or label
        kwargs => optionals
    '''
    hv.extension("bokeh")
    nodes, edges = nx_layout(G)
    # apply parameters
    if kwargs:
        hv.opts.defaults(
            hv.opts.EdgePaths(**kwargs),
            hv.opts.Graph(**kwargs),
            hv.opts.Nodes(**kwargs))
    # plot edges with bokeh
    circle = hv.Graph(edges, label=name).opts(style=dict(node_size=10))
    # plot edges with datashader (WIP)
    if method == 'datashader':
        hnodes = circle.nodes.opts(style=dict(size=10))
        dscirc = (hd.dynspread(hd.datashade(circle))*hnodes).relabel(name)
        return dscirc
    return circle
예제 #20
0
 def make_view(self, x_range, y_range, **kwargs):
     tiles = map_tiles.options(alpha=self.alpha, **opts1)
     points = hv.Points(df, ['x', 'y'])
     return tiles * datashade(points,
                              cmap=self.colormap,
                              x_range=x_range,
                              y_range=y_range,
                              **opts2)
예제 #21
0
    def setUp(self):
        from holoviews.tests.teststreams import Sum, Val

        # kdims: a and b
        self.dmap_ab = DynamicMap(lambda a, b: Points([a, b]),
                                  kdims=['a', 'b']).redim.range(a=(0.0, 10.0),
                                                                b=(0.0, 10.0))

        # kdims: b
        self.dmap_b = DynamicMap(lambda b: Points([b, b]),
                                 kdims=['b']).redim.range(b=(0.0, 10.0))

        # no kdims, XY stream
        self.xy_stream = XY()
        self.dmap_xy = DynamicMap(lambda x, y: Points([x, y]),
                                  streams=[self.xy_stream])

        # no kdims, Z stream
        self.z_stream = Z()
        self.dmap_z = DynamicMap(lambda z: Points([z, z]),
                                 streams=[self.z_stream])

        # DynamicMap of a derived stream
        self.stream_val1 = Val()
        self.stream_val2 = Val()
        self.stream_val3 = Val()
        self.dmap_derived = DynamicMap(
            lambda v: Points([v, v]),
            streams=[
                Sum([
                    self.stream_val1,
                    Sum([self.stream_val2, self.stream_val3])
                ])
            ])

        if datashade is None:
            return

        # dmap produced by chained datashade and shade
        self.px_stream = PX()
        self.dmap_spread_points = spread(datashade(Points([0.0, 1.0])),
                                         streams=[self.px_stream])

        # data shaded with kdims: a, b
        self.dmap_datashade_kdim_points = datashade(self.dmap_ab)
예제 #22
0
 def view(self, cmap=None, shade=True):
     raster_opts = dict(aggregator=self.aggregator, precompute=True)
     cmap_opts = dict(cmap=cmap) if cmap else {}
     if shade:
         if cmap: raster_opts.update(cmap_opts)
         shaded = datashade(self.obj, **raster_opts)
     else:
         shaded = rasterize(self.obj, **raster_opts).opts(style=cmap_opts)
     return self.tiles * shaded * self.path + self.sections
예제 #23
0
def plot_datashade_dnb_figure(dataset, colormap, width=900, height=600):
    return datashade(
        hv.Scatter(dataset, kdims=["x"], vdims=['y', 'values']),
        aggregator=ds.sum('values'),
        cmap=colormap,
    ).opts(
        width=width,
        height=height,
    )
    def make_view(self, x_range=None, y_range=None, **kwargs):
        #map_tiles = tiles.opts(style=dict(alpha=self.alpha), plot=options) 

        hvmap = projected_gv.select(z=self.altitude, k=self.trackrange)
        dsmap = datashade(hvmap, x_sampling=1, y_sampling=1, cmap=self.colormap,
                          dynamic=False, x_range=x_range, y_range=y_range)
        gv_options = {'bgcolor':'black', 'show_grid':True}
        gvmap = dynspread(dsmap.opts(plot=gv_options))
        return gvmap #* hv.util.Dynamic(aggregate(hvmap, width=5, height=5, streams=[PointerX]), operation=hv.QuadMesh)
예제 #25
0
파일: plots.py 프로젝트: rynge/straxen
 def channel_map():
     return dynspread(
         datashade(
             points, y_range=(0, 260),
             streams=[xrange_stream])).opts(plot=dict(
                 width=600,
                 tools=[time_zoom, 'xpan'],
                 default_tools=['save', 'pan', 'box_zoom', 'save', 'reset'],
                 show_grid=False))
예제 #26
0
파일: mesh.py 프로젝트: erdc/genesis
 def view_elements(self, agg='any', line_color='black', cmap='black'):
     """ Method to display the mesh as wireframe elements"""
     if self.elements_toggle:
         # return datashade(self.tri_mesh.edgepaths.opts(line_color=line_color), aggregator=agg,
         #                  precompute=True, cmap=cmap)
         return datashade(
             self.tri_mesh.edgepaths.opts(
                 opts.TriMesh(edge_cmap='yellow', edge_color='yellow')))
     else:
         return hv.Curve([])
예제 #27
0
 def gen_mesh(self, **kwargs):
     if not self.draw_helper.poly_stream.element or self._clear:
         self._clear = False
         return RGB([], bounds=self.draw_helper.extent)
     self._reset_mesh()
     self.polys = self._process_polys(self.draw_helper.poly_stream.element)
     self.refine_points = self.add_refine_points(self.draw_helper)
     verts, tris = self.mesh.create_mesh()
     return datashade(viz_mesh(verts, tris).edgepaths, cmap=['#000000'],
                      dynamic=False)
예제 #28
0
def draw_graph(G, label, cmap):
    print(f'Shading {label}')
    shaded = (datashade(G, normalization='linear', width=1000, height=1000) *
              G.nodes).opts(
                  opts.Nodes(color=label,
                             width=1000,
                             cmap=cmap,
                             legend_position='right'))

    hv.save(shaded, f'graphs/png/{label}.png')
    hv.save(shaded, f'graphs/html/{label}.html')
 def z_y_views(self, x_range, y_range):
     x_min = x_range[0]; x_max = x_range[1]
     y_min = y_range[0]; y_max = y_range[1]
     
     datas = projected_gv.select(x=x_range, y=y_range, z=self.altitude, k=self.trackrange).to(hv.Dataset)
     hvmap = datas.to(hv.Points, kdims=self.plotdims, vdims=['i', 'k'], groupby=[])
     dsmap = datashade(hvmap, cmap=self.colormap, dynamic=False, x_range=x_range, y_range=y_range)
     gv_options = {'bgcolor':'black', 'show_grid':True}
     gvmap = dynspread(dsmap.opts(plot=gv_options))
     
     return gvmap
    def datashade(self, column=None, reduction=None):
        """Return datashader shaded with reduction of given column value
        
        Arguments:
        - `column`:
        - `reduction`:
        """
        import datashader as ds
        from bokeh import palettes
        from holoviews.operation.datashader import aggregate, shade, datashade, dynspread
        if reduction is None:
            reduction = ds.mean

        #print "datashade", column, reduction
        shade_opts = {"cmap": palettes.inferno(64)}

        if column is not None:
            d = self._points.dframe()[column]
            d_min, d_max = d.min(), d.max()
            #print "Value Range:", d_min, d_max
            #shade_opts["clims"] = (d_min, d_max)

            if d_min * d_max < 0.0:
                print("Diverging palette")
                d_extreme = max(-d_min, d_max)
                shade_opts = {
                    "cmap": palettes.RdYlBu11,
                    #"clims": (-d_extreme, d_extreme)
                }

        def _linear_norm_debug(v, masked):
            import pandas as pd
            #print args
            #print kwargs
            print(v)
            print(pd.DataFrame(v).describe())
            min_v, max_v = v[~masked].min(), v[~masked].max()

            print(min_v, max_v)
            o = (v - min_v) / (max_v - min_v)
            print(o)
            return o

        #del (shade_opts["clims"])
        plot = dynspread(
            datashade(
                self._points,
                aggregator=ds.count() if column is None else reduction(column),
                normalization="linear" if "clims" in shade_opts else "eq_hist",
                **shade_opts))

        return plot
import dask.dataframe as dd
import holoviews as hv
import geoviews as gv

from bokeh.models import WMTSTileSource
from holoviews.operation.datashader import datashade

url = 'https://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/{Z}/{Y}/{X}.jpg'
wmts = WMTSTileSource(url=url)

# 1. Instead of using hv.extension, declare that we are using Bokeh and grab the renderer
renderer = hv.renderer('bokeh')


# 2. Declare points and datashade them
ddf = dd.read_csv('../data/nyc_taxi.csv', usecols=['pickup_x', 'pickup_y']).persist()
shaded = datashade(hv.Points(ddf))

# Set some plot options
app = gv.WMTS(wmts) * shaded.opts(plot=dict(width=800, height=600))

# 3. Instead of Jupyter's automatic rich display, render the object as a bokeh document
doc = renderer.server_doc(app)
doc.title = 'HoloViews Bokeh App'
import dask.dataframe as dd

from holoviews.operation.datashader import datashade, aggregate, shade
from bokeh.models import WMTSTileSource

renderer = hv.renderer('bokeh')

# Load data
usecols = ['tpep_pickup_datetime', 'dropoff_x', 'dropoff_y']
ddf = dd.read_csv('../data/nyc_taxi.csv', parse_dates=['tpep_pickup_datetime'], usecols=usecols)
ddf['hour'] = ddf.tpep_pickup_datetime.dt.hour
ddf = ddf.persist()

# Declare objects
stream = hv.streams.Counter()
points = hv.Points(ddf, kdims=['dropoff_x', 'dropoff_y'])
dmap = hv.DynamicMap(lambda counter: points.select(hour=counter%24).relabel('Hour: %s' % (counter % 24)),
                     streams=[stream])
shaded = datashade(dmap)

hv.opts('RGB [width=800, height=600, xaxis=None, yaxis=None]')

url = 'https://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/{Z}/{Y}/{X}.jpg'
wmts = gv.WMTS(WMTSTileSource(url=url))

overlay = wmts * shaded

# Create server document
doc = renderer.server_doc(overlay)
dmap.periodic(1)
import dask.dataframe as dd
import holoviews as hv
from holoviews.operation.datashader import datashade

# 1. Instead of using hv.extension, declare that we are using Bokeh and grab the renderer
renderer = hv.renderer('bokeh')

# 2. Load data and datashade it
ddf = dd.read_csv('../data/nyc_taxi.csv', usecols=['dropoff_x', 'dropoff_y']).persist()
points = hv.Points(ddf, kdims=['dropoff_x', 'dropoff_y'])
shaded = datashade(points).opts(plot=dict(width=800, height=600))

# 3. Instead of Jupyter's automatic rich display, render the object as a bokeh document
doc = renderer.server_doc(shaded)
doc.title = 'HoloViews Bokeh App'