Ejemplo n.º 1
0
    def __call__(self, kind, x, y):
        kind = self.kind or kind
        method = getattr(self, kind)

        groups = self.groupby
        zs = self.kwds.get('z', [])
        if not isinstance(zs, list): zs = [zs]
        grid = []
        if self.row: grid.append(self.row)
        if self.col: grid.append(self.col)
        groups += grid
        if groups or len(zs) > 1:
            if self.streaming:
                raise NotImplementedError(
                    "Streaming and groupby not yet implemented")
            data = self.data
            if not self.gridded and any(g in self.indexes for g in groups):
                data = data.reset_index()
            dataset = Dataset(data)
            if groups:
                dataset = dataset.groupby(groups, dynamic=self.dynamic)
                if len(zs) > 1:
                    dimensions = [Dimension(self.group_label, values=zs)
                                  ] + dataset.kdims
                    if self.dynamic:
                        obj = DynamicMap(
                            lambda *args: getattr(self, kind)
                            (x, y, args[0], dataset[args[1:]].data),
                            kdims=dimensions)
                    else:
                        obj = HoloMap(
                            {(z, ) + k: getattr(self, kind)(x, y, z,
                                                            dataset[k])
                             for k, v in dataset.data.items() for z in zs},
                            kdims=dimensions)
                else:
                    obj = dataset.map(
                        lambda ds: getattr(self, kind)(x, y, data=ds.data),
                        Dataset)
            elif len(zs) > 1:
                if self.dynamic:
                    dataset = DynamicMap(
                        lambda z: getattr(self, kind)
                        (x, y, z, data=dataset.data),
                        kdims=[Dimension(self.group_label, values=zs)])
                else:
                    dataset = HoloMap(
                        {
                            z: getattr(self, kind)(x, y, z, data=dataset.data)
                            for z in zs
                        },
                        kdims=[self.group_label])
            else:
                obj = getattr(self, kind)(x, y, data=dataset.data)
            if grid:
                obj = obj.grid(grid).options(shared_xaxis=True,
                                             shared_yaxis=True)
        else:
            if self.streaming:
                cbcallable = StreamingCallable(partial(method, x, y),
                                               periodic=self.cb)
                obj = DynamicMap(cbcallable, streams=[self.stream])
            else:
                obj = method(x, y)

        if not (self.datashade or self.rasterize):
            return obj

        try:
            from holoviews.operation.datashader import datashade, rasterize, dynspread
            from datashader import count_cat
        except:
            raise ImportError('Datashading is not available')

        opts = dict(width=self._plot_opts['width'],
                    height=self._plot_opts['height'],
                    dynamic=self.dynamic)
        if 'cmap' in self._style_opts and self.datashade:
            levels = self._plot_opts.get('color_levels')
            opts['cmap'] = process_cmap(self._style_opts['cmap'], levels)
            opts['color_key'] = opts['cmap']
        if self.by:
            opts['aggregator'] = count_cat(self.by[0])
        if self.aggregator:
            opts['aggregator'] = self.aggregator
        if self.precompute:
            opts['precompute'] = self.precompute
        if self.x_sampling:
            opts['x_sampling'] = self.x_sampling
        if self.y_sampling:
            opts['y_sampling'] = self.y_sampling
        style = {}
        if self.datashade:
            operation = datashade
            eltype = 'RGB'
        else:
            operation = rasterize
            eltype = 'Image'
            if 'cmap' in self._style_opts:
                style['cmap'] = self._style_opts['cmap']

        if self.crs:
            # Apply projection before rasterizing
            import cartopy.crs as ccrs
            from geoviews import project
            projection = self._plot_opts.get('projection',
                                             ccrs.GOOGLE_MERCATOR)
            obj = project(obj, projection=projection)

        processed = operation(obj, **opts)

        if self.dynspread:
            if self.datashade:
                processed = dynspread(processed,
                                      max_px=self.kwds.get('max_px', 3),
                                      threshold=self.kwds.get(
                                          'threshold', 0.5))
            else:
                self.warning(
                    'dynspread may only be applied on datashaded plots, '
                    'use datashade=True instead of rasterize=True.')
        return processed.opts(
            {eltype: {
                'plot': self._plot_opts,
                'style': style
            }})
Ejemplo n.º 2
0
    def __call__(self, kind, x, y):
        kind = self.kind or kind
        method = getattr(self, kind)

        groups = self.groupby
        zs = self.kwds.get('z', [])
        if not isinstance(zs, list): zs = [zs]
        grid = []
        if self.row: grid.append(self.row)
        if self.col: grid.append(self.col)
        groups += grid
        if groups or len(zs) > 1:
            if self.streaming:
                raise NotImplementedError(
                    "Streaming and groupby not yet implemented")
            dataset = Dataset(self.data)
            if groups:
                dataset = dataset.groupby(groups, dynamic=self.dynamic)
                if len(zs) > 1:
                    dimensions = [Dimension(self.group_label, values=zs)
                                  ] + dataset.kdims
                    if self.dynamic:
                        obj = DynamicMap(
                            lambda *args: getattr(self, kind)
                            (x, y, args[0], dataset[args[1:]].data),
                            kdims=dimensions)
                    else:
                        obj = HoloMap(
                            {(z, ) + k: getattr(self, kind)(x, y, z,
                                                            dataset[k])
                             for k, v in dataset.data.items() for z in zs},
                            kdims=dimensions)
                else:
                    obj = dataset.map(
                        lambda ds: getattr(self, kind)(x, y, data=ds.data),
                        Dataset)
            elif len(zs) > 1:
                if self.dynamic:
                    dataset = DynamicMap(
                        lambda z: getattr(self, kind)
                        (x, y, z, data=dataset.data),
                        kdims=[Dimension(self.group_label, values=zs)])
                else:
                    dataset = HoloMap(
                        {
                            z: getattr(self, kind)(x, y, z, data=dataset.data)
                            for z in zs
                        },
                        kdims=[self.group_label])
            else:
                obj = getattr(self, kind)(x, y, data=dataset.data)
            if grid:
                obj = obj.grid(grid).options(shared_xaxis=True,
                                             shared_yaxis=True)
        else:
            if self.streaming:
                cbcallable = StreamingCallable(partial(method, x, y),
                                               periodic=self.cb)
                obj = DynamicMap(cbcallable, streams=[self.stream])
            else:
                obj = method(x, y)

        if not (self.datashade or self.rasterize):
            return obj

        try:
            from holoviews.operation.datashader import datashade, rasterize
            from datashader import count_cat
        except:
            raise ImportError('Datashading is not available')

        opts = dict(width=self._plot_opts['width'],
                    height=self._plot_opts['height'])
        if 'cmap' in self._style_opts and self.datashade:
            opts['cmap'] = self._style_opts['cmap']
        if self.by:
            opts['aggregator'] = count_cat(self.by[0])
        if self.aggregator:
            opts['aggregator'] = self.aggregator
        style = {}
        if self.datashade:
            operation = datashade
            eltype = 'RGB'
        else:
            operation = rasterize
            eltype = 'Image'
            if 'cmap' in self._style_opts:
                style['cmap'] = self._style_opts['cmap']

        if self.crs:
            # Apply projection before rasterizing
            import cartopy.crs as ccrs
            from geoviews import project
            projection = self._plot_opts.get('projection',
                                             ccrs.GOOGLE_MERCATOR)
            obj = project(obj, projection=projection)

        return operation(obj, **opts).opts(
            {eltype: {
                'plot': self._plot_opts,
                'style': style
            }})