Пример #1
0
 def _apply_spreading(self, array):
     img = tf.Image(array)
     return tf.dynspread(img,
                         max_px=self.p.max_px,
                         threshold=self.p.threshold,
                         how=self.p.how,
                         shape=self.p.shape).data
Пример #2
0
def render_map(source: MapSource,  # noqa: C901
               xmin: float = None, ymin: float = None,
               xmax: float = None, ymax: float = None,
               x: float = None, y: float = None,
               z: float = None,
               height: int = None, width: int = None, ):

    if x is not None and y is not None and z is not None:
        xmin, ymin, xmax, ymax = tile_def.get_tile_meters(x, y, z)

    sxmin, symin, sxmax, symax = source.full_extent

    # handle null extent
    if xmin is None:
        xmin = sxmin

    if ymin is None:
        ymin = symin

    if xmax is None:
        xmax = sxmax

    if ymax is None:
        ymax = symax

    # handle null h/w
    if height is None and width is None:
        width = 1000

    if height is None:
        x_range, y_range = ((xmin, xmax), (ymin, ymax))
        height = height_implied_by_aspect_ratio(width, x_range, y_range)

    if width is None:
        x_range, y_range = ((xmin, xmax), (ymin, ymax))
        width = height_implied_by_aspect_ratio(height, y_range, x_range)

    # handle out of bounds
    if xmin < sxmin and ymin < symin and xmax > symax and ymax > symax:
        agg = tf.Image(np.zeros(shape=(height, width), dtype=np.uint32),
                       coords={'x': np.linspace(xmin, xmax, width),
                               'y': np.linspace(ymin, ymax, height)},
                       dims=['x', 'y'])
        img = shade_agg(source, agg, xmin, ymin, xmax, ymax)
        return img

    agg = create_agg(source, xmin, ymin, xmax, ymax, x, y, z, height, width)

    if source.span and isinstance(source.span, (list, tuple)):
        agg = agg.where((agg >= source.span[0]) & (agg <= source.span[1]))

    source, agg = apply_additional_transforms(source, agg)
    img = shade_agg(source, agg, xmin, ymin, xmax, ymax)

    # apply dynamic spreading ----------
    if source.dynspread and source.dynspread > 0:
        img = tf.dynspread(img, threshold=1, max_px=int(source.dynspread))

    return img
def merged_images(x_range, y_range, w, h, how='log'):
    cvs = ds.Canvas(plot_width=w, plot_height=h, x_range=x_range, y_range=y_range)
    picks = cvs.points(sep_trips, 'pickup_x',  'pickup_y',  ds.count('passenger_count'))
    drops = cvs.points(sep_trips, 'dropoff_x', 'dropoff_y', ds.count('passenger_count'))
    more_drops = tf.interpolate(drops.where(drops > picks), cmap=["lightblue", 'blue'], how=how)
    more_picks = tf.interpolate(picks.where(picks > drops), cmap=["lightpink", 'red'],  how=how)
    img = tf.stack(more_picks,more_drops)
    return tf.dynspread(img, threshold=0.1, max_px=4)
Пример #4
0
def create_image(x_range, y_range, w, h):
    cvs = ds.Canvas(plot_width=w,
                    plot_height=h,
                    x_range=x_range,
                    y_range=y_range)
    agg = cvs.points(df, 'web_long', 'web_lat', ds.count('passenger_count'))
    img = tf.interpolate(agg, cmap=Hot, how='eq_hist')
    return tf.dynspread(img, threshold=0.5, max_px=1)
Пример #5
0
def create_image(x_range, y_range, w=plot_width, h=plot_height):
    cvs = dshader.Canvas(plot_width=w,
                         plot_height=h,
                         x_range=x_range,
                         y_range=y_range)
    agg = cvs.points(df, 'x', 'z', dshader.count_cat('phase'))
    img = tf.colorize(agg, phase_color_key, how='eq_hist')
    return tf.dynspread(img, threshold=0.3, max_px=4)
Пример #6
0
 def create_image(x_range, y_range, w=plot_width, h=plot_height):
     cvs = ds.Canvas(plot_width=w,
                     plot_height=h,
                     x_range=x_range,
                     y_range=y_range)
     agg = cvs.points(data, 'wm_lon', 'wm_lat', ds.count('velo'))
     img = tf.shade(agg, cmap=viridis, how='eq_hist')
     return tf.dynspread(img, threshold=0.5, max_px=4)
Пример #7
0
 def create_image(x_range, y_range, w=plot_width, h=plot_height):
     canvas = ds.Canvas(plot_width=plot_width,
                        plot_height=plot_height,
                        x_range=x_range,
                        y_range=y_range)
     agg = canvas.points(df, 'x', 'y')
     img = tf.interpolate(agg, cmap=ds.colors.Hot, how='log')
     return tf.dynspread(img, threshold=0.5, max_px=4)
Пример #8
0
 def create_image(x_range, y_range, w=plot_width, h=plot_height):
     #set up canvas and populate with datapoints
     cvs = ds.Canvas(plot_width=w,
                     plot_height=h,
                     x_range=x_range,
                     y_range=y_range)
     agg = cvs.points(df, 'x_col', 'y_col')
     img = tf.shade(agg, cmap=Hot, how='eq_hist')
     return tf.dynspread(img, threshold=0.5, max_px=4)
Пример #9
0
def create_image(x_range, y_range, w=plot_width, h=plot_height):
    cvs = ds.Canvas(plot_width=w,
                    plot_height=h,
                    x_range=x_range,
                    y_range=y_range)
    agg = cvs.points(df, 'dropoff_longitude', 'dropoff_latitude',
                     ds.count('passenger_count'))
    img = tr_fns.shade(agg, cmap=Hot, how='eq_hist')
    return tr_fns.dynspread(img, threshold=0.5, max_px=4)
Пример #10
0
def plot_data_points(longitude, latitude, data_frame, focus_point):
    '''
    This function plots the rides on a map of chicago
    '''
    cvs = ds.Canvas(plot_width=500, plot_height=400)
    export = partial(export_image, export_path="export", background="black")
    agg = cvs.points(data_frame, longitude, latitude, ds.count())
    img = transfer_functions.shade(agg, cmap=hot, how='eq_hist')
    image_xpt = transfer_functions.dynspread(img, threshold=0.5, max_px=4)

    return export(image_xpt, "map")
Пример #11
0
 def _apply_spreading(self, array):
     if cupy and isinstance(array.data, cupy.ndarray):
         # Convert img.data to numpy array before passing to nb.jit kernels
         array.data = cupy.asnumpy(array.data)
     return tf.dynspread(
         array,
         max_px=self.p.max_px,
         threshold=self.p.threshold,
         how=self.p.how,
         shape=self.p.shape,
     )
def test_categorical_dynspread():
    a_data = np.array([[0, 1, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0],
                       [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]],
                      dtype='int32')

    b_data = np.array([[0, 0, 0, 0, 0], [0, 1, 0, 0, 0], [0, 0, 0, 0, 0],
                       [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]],
                      dtype='int32')

    c_data = np.array([[1, 0, 0, 0, 0], [1, 0, 0, 0, 0], [0, 0, 0, 0, 0],
                       [0, 0, 0, 1, 0], [0, 0, 0, 0, 0]],
                      dtype='int32')

    data = np.dstack([a_data, b_data, c_data])
    coords = [np.arange(5), np.arange(5)]
    arr = xr.DataArray(data,
                       coords=coords + [['a', 'b', 'c']],
                       dims=dims + ['cat'])
    assert tf.dynspread(arr).equals(tf.spread(arr, 1))
    assert tf.dynspread(arr, threshold=0.9).equals(tf.spread(arr, 2))
    assert tf.dynspread(arr, threshold=0).equals(arr)
    assert tf.dynspread(arr, max_px=0).equals(arr)
Пример #13
0
 def create_image(x_range=x_range, y_range=y_range, w=w, h=h):
     """
     """
     cvs = ds.Canvas(x_range=x_range, y_range=y_range, plot_height=h, plot_width=w)
 
     if len(fdf[col].unique())>10:
         colormap = fire
     else:
         colormap = bkr
     agg = cvs.points(fdf, 'x_web', 'y_web', agg=ds.mean(col))
     image = dtf.shade(agg, cmap=colormap)
     ds.utils.export_image(image,filename=col+'.png')
     return dtf.dynspread(image, threshold=0.75, max_px=8)
Пример #14
0
        def viewInteractiveImage(x_range, y_range, w, h, data_source,
                                 **kwargs):
            dd = data_source[[self.x, self.y, self.aggregate_col]]
            dd[self.x] = self._to_xaxis_type(dd[self.x])
            dd[self.y] = self._to_yaxis_type(dd[self.y])

            x_range = self._to_xaxis_type(x_range)
            y_range = self._to_yaxis_type(y_range)

            cvs = ds.Canvas(plot_width=w,
                            plot_height=h,
                            x_range=x_range,
                            y_range=y_range)
            aggregator, cmap = _compute_datashader_assets(
                dd,
                self.x,
                self.aggregate_col,
                self.aggregate_fn,
                self.color_palette,
            )
            agg = cvs.points(
                dd,
                self.x,
                self.y,
                aggregator,
            )

            if self.constant_limit is None or self.aggregate_fn == "count":
                self.constant_limit = [
                    float(cp.nanmin(agg.data)),
                    float(cp.nanmax(agg.data)),
                ]
                self.render_legend()

            span = {"span": self.constant_limit}
            if self.pixel_shade_type == "eq_hist":
                span = {}

            img = tf.shade(agg, how=self.pixel_shade_type, **cmap, **span)

            if self.pixel_spread == "dynspread":
                return tf.dynspread(
                    img,
                    threshold=self.pixel_density,
                    max_px=self.point_size,
                    shape=self.point_shape,
                )
            else:
                return tf.spread(img,
                                 px=self.point_size,
                                 shape=self.point_shape)
def test_array_dynspread():
    data = np.array([[1, 1, 0, 0, 0], [1, 1, 0, 0, 0], [0, 0, 0, 0, 0],
                     [0, 0, 0, 1, 0], [0, 0, 0, 0, 0]],
                    dtype='uint32')
    coords = [np.arange(5), np.arange(5)]
    arr = xr.DataArray(data, coords=coords, dims=dims)
    assert tf.dynspread(arr).equals(tf.spread(arr, 1))
    assert tf.dynspread(arr, threshold=0.9).equals(tf.spread(arr, 2))
    assert tf.dynspread(arr, threshold=0).equals(arr)
    assert tf.dynspread(arr, max_px=0).equals(arr)

    pytest.raises(ValueError, lambda: tf.dynspread(arr, threshold=1.1))
    pytest.raises(ValueError, lambda: tf.dynspread(arr, max_px=-1))
Пример #16
0
def plot_ds(ax, mdf, col, title):
    import datashader as ds
    from datashader import transfer_functions as tf
    from datashader.colors import Sets1to3
    mdf[col] = mdf[col].astype('category')
    cvs = ds.Canvas(plot_width=960, plot_height=540)
    agg = cvs.points(mdf, 'x', 'y', ds.count_cat(col))

    color_key = dict(zip(mdf[col].cat.categories, Sets1to3))
    img = tf.shade(agg, name=title, min_alpha=64, color_key=color_key)
    img = tf.dynspread(img)
    img = img.data.view(np.uint8).reshape(*img.shape, 4)[::-1, :, :]

    ax.imshow(img)
    from matplotlib.lines import Line2D
    ax.legend([Line2D([0], [0], color=c, lw=4) for c in color_key.values()],
              color_key.keys(),
              loc='upper left',
              markerscale=5)
Пример #17
0
def plot_ds_bespoke(ax, mdf, cats, explicit_colors):
    import datashader as ds
    from datashader import transfer_functions as tf
    from datashader.colors import Sets1to3
    cvs = ds.Canvas(plot_width=960, plot_height=640)
    agg = cvs.points(mdf, 'x', 'y', ds.count_cat('cat'))

    color_key = dict(zip(cats, Sets1to3))
    color_key.update(explicit_colors)
    img = tf.shade(agg, min_alpha=128, color_key=color_key)
    img = tf.dynspread(img, threshold=0.5, max_px=1000)
    img = img.data.view(np.uint8).reshape(*img.shape, 4)[::-1, :, :]

    ax.imshow(img)
    from matplotlib.lines import Line2D
    ax.legend([Line2D([0], [0], color=color_key[c], lw=4) for c in cats],
              cats,
              loc='upper left',
              markerscale=5)
def test_dynspread():
    b = 0xffff0000
    data = np.array([[b, b, 0, 0, 0], [b, b, 0, 0, 0], [0, 0, 0, 0, 0],
                     [0, 0, 0, b, 0], [0, 0, 0, 0, 0]],
                    dtype='uint32')
    coords = [np.arange(5), np.arange(5)]
    img = tf.Image(data, coords=coords, dims=dims)
    assert tf.dynspread(img).equals(tf.spread(img, 1))
    assert tf.dynspread(img, threshold=0.9).equals(tf.spread(img, 2))
    assert tf.dynspread(img, threshold=0).equals(img)
    assert tf.dynspread(img, max_px=0).equals(img)

    pytest.raises(ValueError, lambda: tf.dynspread(img, threshold=1.1))
    pytest.raises(ValueError, lambda: tf.dynspread(img, max_px=-1))
Пример #19
0
def render_map(source: MapSource,
               xmin: float = None,
               ymin: float = None,
               xmax: float = None,
               ymax: float = None,
               x: float = None,
               y: float = None,
               z: float = None,
               height: int = 256,
               width: int = 256):

    agg = create_agg(source, xmin, ymin, xmax, ymax, x, y, z, height, width)
    source, agg = apply_additional_transforms(source, agg)
    img = shade_agg(source, agg, xmin, ymin, xmax, ymax)

    # apply dynamic spreading ----------
    if source.dynspread and source.dynspread > 0:
        img = tf.dynspread(img, threshold=1, max_px=int(source.dynspread))

    return img
Пример #20
0
def plot_ds_highlight(ax, mdf, col, value, color, title):
    global img
    import datashader as ds
    from datashader import transfer_functions as tf
    mdf = mdf[['x',
               'y']].assign(highlight=(mdf[col] == value).astype('category'))
    cvs = ds.Canvas(plot_width=410, plot_height=260)
    agg = cvs.points(mdf, 'x', 'y', ds.count_cat('highlight'))

    img = tf.shade(agg,
                   name=title,
                   min_alpha=64,
                   color_key={
                       False: 'grey',
                       True: tuple(np.array(color) * 255)
                   })
    img = tf.dynspread(img)
    img = img.data.view(np.uint8).reshape(*img.shape, 4)[::-1, :, :]

    ax.imshow(img)
Пример #21
0
def render_map(source: MapSource,
               xmin: float = None,
               ymin: float = None,
               xmax: float = None,
               ymax: float = None,
               x: float = None,
               y: float = None,
               z: float = None,
               height: int = 256,
               width: int = 256):

    if x is not None and y is not None and z is not None:
        xmin, ymin, xmax, ymax = tile_def.get_tile_meters(x, y, z)

    sxmin, symin, sxmax, symax = source.full_extent

    # handle out of bounds
    if xmin < sxmin and ymin < symin and xmax > symax and ymax > symax:
        agg = tf.Image(np.zeros(shape=(height, width), dtype=np.uint32),
                       coords={
                           'x': np.linspace(xmin, xmax, width),
                           'y': np.linspace(ymin, ymax, height)
                       },
                       dims=['x', 'y'])
        img = shade_agg(source, agg, xmin, ymin, xmax, ymax)
        return img

    agg = create_agg(source, xmin, ymin, xmax, ymax, x, y, z, height, width)

    if source.span and isinstance(source.span, (list, tuple)):
        agg = agg.where((agg >= source.span[0]) & (agg <= source.span[1]))

    source, agg = apply_additional_transforms(source, agg)
    img = shade_agg(source, agg, xmin, ymin, xmax, ymax)

    # apply dynamic spreading ----------
    if source.dynspread and source.dynspread > 0:
        img = tf.dynspread(img, threshold=1, max_px=int(source.dynspread))

    return img
Пример #22
0
def test_dynspread():
    b = 0xffff0000
    data = np.array([[b, b, 0, 0, 0],
                     [b, b, 0, 0, 0],
                     [0, 0, 0, 0, 0],
                     [0, 0, 0, b, 0],
                     [0, 0, 0, 0, 0]], dtype='uint32')
    coords = [np.arange(5), np.arange(5)]
    img = tf.Image(data, coords=coords, dims=dims)
    assert tf.dynspread(img).equals(tf.spread(img, 1))
    assert tf.dynspread(img, threshold=0.9).equals(tf.spread(img, 2))
    assert tf.dynspread(img, threshold=0).equals(img)
    assert tf.dynspread(img, max_px=0).equals(img)

    pytest.raises(ValueError, lambda: tf.dynspread(img, threshold=1.1))
    pytest.raises(ValueError, lambda: tf.dynspread(img, max_px=-1))
Пример #23
0
 def viewInteractiveImage(x_range, y_range, w, h, data_source):
     cvs = cds.Canvas(
         plot_width=w, plot_height=h, x_range=x_range, y_range=y_range
     )
     agg = cvs.points(
         data_source,
         self.x,
         self.y,
         getattr(cds, self.aggregate_fn)(self.aggregate_col),
     )
     img = tf.shade(
         agg, cmap=self.color_palette, how=self.pixel_shade_type
     )
     if self.pixel_spread == "dynspread":
         return tf.dynspread(
             img,
             threshold=self.pixel_density,
             max_px=self.point_size,
             shape=self.point_shape,
         )
     else:
         return tf.spread(
             img, px=self.point_size, shape=self.point_shape
         )
def create_image(x_range, y_range, w, h):
    cvs = ds.Canvas(plot_width=w, plot_height=h, x_range=x_range, y_range=y_range)
    agg = cvs.points(df, 'pickup_longitude', 'pickup_latitude',  ds.count('passenger_count'))
    img = tf.interpolate(agg, cmap=Hot, how='eq_hist')
    return tf.dynspread(img, threshold=0.5, max_px=4)
Пример #25
0
def create_image(x_range, y_range, w, h):
    cvs = ds.Canvas(plot_width=w, plot_height=h, x_range=x_range, y_range=y_range)
    agg = cvs.points(picks, 'x', 'y',  ds.count('visibility'))
    img = tf.interpolate(agg, cmap=Hot, how='eq_hist')
    return tf.dynspread(img, threshold=0.5, max_px=4)
Пример #26
0
def image_callback(x_range, y_range, w, h):
    cvs = ds.Canvas(
          plot_width=w,plot_height=h,x_range=x_range,y_range=y_range)
    agg = cvs.points(df,'meterswest','metersnorth', ds.mean('temp'))
    img = tf.interpolate(agg, cmap = cmap, how='cbrt',span=[0.0,1.0])
    return tf.dynspread(img,threshold=0.5, max_px=4)
Пример #27
0
def build_datashader_plot(
        df, aggregate, aggregate_column, colorscale_name, colorscale_transform,
        new_coordinates, position, x_range, y_range
):
    """
    Build choropleth figure

    Args:
        df: pandas or cudf DataFrame
        aggregate: Aggregate operation (count, mean, etc.)
        aggregate_column: Column to perform aggregate on. Ignored for 'count' aggregate
        colorscale_name: Name of plotly colorscale
        colorscale_transform: Colorscale transformation
        clear_selection: If true, clear choropleth selection. Otherwise leave
            selection unchanged

    Returns:
        Choropleth figure dictionary
    """

    global data_3857, data_center_3857, data_4326, data_center_4326

    x0, x1 = x_range
    y0, y1 = y_range

    # Build query expressions
    query_expr_xy = f"(x >= {x0}) & (x <= {x1}) & (y >= {y0}) & (y <= {y1})"
    datashader_color_scale = {}

    if aggregate == 'count_cat':
        datashader_color_scale['color_key'] = colors[aggregate_column] 
    else:
        datashader_color_scale['cmap'] = [i[1] for i in build_colorscale(colorscale_name, colorscale_transform, aggregate, aggregate_column)]
        if not isinstance(df, cudf.DataFrame):
            df[aggregate_column] = df[aggregate_column].astype('int8')

    cvs = ds.Canvas(
        plot_width=1400,
        plot_height=1400,
        x_range=x_range, y_range=y_range
    )
    agg = cvs.points(
        df, x='x', y='y', agg=getattr(ds, aggregate)(aggregate_column)
    )
    

    # Count the number of selected towers
    temp = agg.sum()
    temp.data = cupy.asnumpy(temp.data)
    n_selected = int(temp)
    
    if n_selected == 0:
        # Nothing to display
        lat = [None]
        lon = [None]
        customdata = [None]
        marker = {}
        layers = []
    # elif n_selected < 5000:
    #     # Display each individual point using a scattermapbox trace. This way we can
    #     # give each individual point a tooltip

    #     ddf_gpu_small_expr = ' & '.join(
    #         [query_expr_xy]
    #     )
    #     ddf_gpu_small = df.query(ddf_gpu_small_expr).to_pandas()

    #     x, y, sex, edu, inc, cow = (
    #         ddf_gpu_small.x, ddf_gpu_small.y, ddf_gpu_small.sex, ddf_gpu_small.education, ddf_gpu_small.income, ddf_gpu_small.cow
    #     )

    #     # Format creation date column for tooltip
    #     # created = pd.to_datetime(created.tolist()).strftime('%x')


    #     # Build array of the integer category codes to use as the numeric color array
    #     # for the scattermapbox trace
    #     sex_codes = sex.unique().tolist()

    #     # Build marker properties dict
    #     marker = {
    #         'color': sex_codes,
    #         'colorscale': colors[aggregate_column],
    #         'cmin': 0,
    #         'cmax': 3,
    #         'size': 5,
    #         'opacity': 0.6,
    #     }
    #     lat = list(zip(
    #         x.astype(str)
    #     ))
    #     lon = list(zip(
    #         y.astype(str)
    #     ))
    #     customdata = list(zip(
    #         sex.astype(str),
    #         edu.astype(str),
    #         inc.astype(str),
    #         cow.astype(str)
    #     ))
    #     layers = []
    else:
        # Shade aggregation into an image that we can add to the map as a mapbox
        # image layer
        max_px = 1
        if n_selected<5000:
            max_px=10
        img = tf.shade(agg, **datashader_color_scale)
        img = tf.dynspread(
                    img,
                    threshold=0.5,
                    max_px=max_px,
                    shape='circle',
                ).to_pil()

        # Add image as mapbox image layer. Note that as of version 4.4, plotly will
        # automatically convert the PIL image object into a base64 encoded png string
        layers = [
            {
                "sourcetype": "image",
                "source": img,
                "coordinates": new_coordinates
            }
        ]

        # Do not display any mapbox markers
        lat = [None]
        lon = [None]
        customdata = [None]
        marker = {}

    # Build map figure
    map_graph = {
        'data': [{
            'type': 'scattermapbox',
            'lat': lat, 'lon': lon,
            'customdata': customdata,
            'marker': marker,
            'hovertemplate': (
                "sex: %{customdata[0]}<br>"
                "<extra></extra>"
            )
        }],
        'layout': {
            'template': template,
            'uirevision': True,
            'mapbox': {
                'style': "dark",
                'accesstoken': token,
                'layers': layers,
            },
            'margin': {"r": 0, "t": 0, "l": 0, "b": 0},
            'height': 500,
            'shapes': [{
                'type': 'rect',
                'xref': 'paper',
                'yref': 'paper',
                'x0': 0,
                'y0': 0,
                'x1': 1,
                'y1': 1,
                'line': {
                    'width': 1,
                    'color': '#191a1a',
                }
            }]
        },
    }

    map_graph['layout']['mapbox'].update(position)

    return map_graph
Пример #28
0
def connectivity(
    umap_object,
    edge_bundling=None,
    edge_cmap="gray_r",
    show_points=False,
    labels=None,
    values=None,
    theme=None,
    cmap="Blues",
    color_key=None,
    color_key_cmap="Spectral",
    background="white",
    width=800,
    height=800,
):
    """Plot connectivity relationships of the underlying UMAP
    simplicial set data structure. Internally UMAP will make
    use of what can be viewed as a weighted graph. This graph
    can be plotted using the layout provided by UMAP as a
    potential diagnostic view of the embedding. Currently this only works
    for 2D embeddings. While there are many optional parameters
    to further control and tailor the plotting, you need only
    pass in the trained/fit umap model to get results. This plot
    utility will attempt to do the hard work of avoiding
    overplotting issues and provide options for plotting the
    points as well as using edge bundling for graph visualization.

    Parameters
    ----------
    umap_object: trained UMAP object
        A trained UMAP object that has a 2D embedding.

    edge_bundling: string or None (optional, default None)
        The edge bundling method to use. Currently supported
        are None or 'hammer'. See the datashader docs
        on graph visualization for more details.

    edge_cmap: string (default 'gray_r')
        The name of a matplotlib colormap to use for shading/
        coloring the edges of the connectivity graph. Note that
        the ``theme``, if specified, will override this.

    show_points: bool (optional False)
        Whether to display the points over top of the edge
        connectivity. Further options allow for coloring/
        shading the points accordingly.

    labels: array, shape (n_samples,) (optional, default None)
        An array of labels (assumed integer or categorical),
        one for each data sample.
        This will be used for coloring the points in
        the plot according to their label. Note that
        this option is mutually exclusive to the ``values``
        option.

    values: array, shape (n_samples,) (optional, default None)
        An array of values (assumed float or continuous),
        one for each sample.
        This will be used for coloring the points in
        the plot according to a colorscale associated
        to the total range of values. Note that this
        option is mutually exclusive to the ``labels``
        option.

    theme: string (optional, default None)
        A color theme to use for plotting. A small set of
        predefined themes are provided which have relatively
        good aesthetics. Available themes are:
           * 'blue'
           * 'red'
           * 'green'
           * 'inferno'
           * 'fire'
           * 'viridis'
           * 'darkblue'
           * 'darkred'
           * 'darkgreen'

    cmap: string (optional, default 'Blues')
        The name of a matplotlib colormap to use for coloring
        or shading points. If no labels or values are passed
        this will be used for shading points according to
        density (largely only of relevance for very large
        datasets). If values are passed this will be used for
        shading according the value. Note that if theme
        is passed then this value will be overridden by the
        corresponding option of the theme.

    color_key: dict or array, shape (n_categories) (optional, default None)
        A way to assign colors to categoricals. This can either be
        an explicit dict mapping labels to colors (as strings of form
        '#RRGGBB'), or an array like object providing one color for
        each distinct category being provided in ``labels``. Either
        way this mapping will be used to color points according to
        the label. Note that if theme
        is passed then this value will be overridden by the
        corresponding option of the theme.

    color_key_cmap: string (optional, default 'Spectral')
        The name of a matplotlib colormap to use for categorical coloring.
        If an explicit ``color_key`` is not given a color mapping for
        categories can be generated from the label list and selecting
        a matching list of colors from the given colormap. Note
        that if theme
        is passed then this value will be overridden by the
        corresponding option of the theme.

    background: string (optional, default 'white)
        The color of the background. Usually this will be either
        'white' or 'black', but any color name will work. Ideally
        one wants to match this appropriately to the colors being
        used for points etc. This is one of the things that themes
        handle for you. Note that if theme
        is passed then this value will be overridden by the
        corresponding option of the theme.

    width: int (optional, default 800)
        The desired width of the plot in pixels.

    height: int (optional, default 800)
        The desired height of the plot in pixels

    Returns
    -------
    result: matplotlib axis
        The result is a matplotlib axis with the relevant plot displayed.
        If you are using a notbooks and have ``%matplotlib inline`` set
        then this will simply display inline.
    """
    if theme is not None:
        cmap = _themes[theme]["cmap"]
        color_key_cmap = _themes[theme]["color_key_cmap"]
        edge_cmap = _themes[theme]["edge_cmap"]
        background = _themes[theme]["background"]

    points = umap_object.embedding_
    point_df = pd.DataFrame(points, columns=("x", "y"))

    point_size = 100.0 / np.sqrt(points.shape[0])
    if point_size > 1:
        px_size = int(np.round(point_size))
    else:
        px_size = 1

    if show_points:
        edge_how = "log"
    else:
        edge_how = "eq_hist"

    coo_graph = umap_object.graph_.tocoo()
    edge_df = pd.DataFrame(
        np.vstack([coo_graph.row, coo_graph.col, coo_graph.data]).T,
        columns=("source", "target", "weight"),
    )
    edge_df["source"] = edge_df.source.astype(np.int32)
    edge_df["target"] = edge_df.target.astype(np.int32)

    extent = _get_extent(points)
    canvas = ds.Canvas(
        plot_width=width,
        plot_height=height,
        x_range=(extent[0], extent[1]),
        y_range=(extent[2], extent[3]),
    )

    if edge_bundling is None:
        edges = bd.directly_connect_edges(point_df, edge_df, weight="weight")
    elif edge_bundling == "hammer":
        warn("Hammer edge bundling is expensive for large graphs!\n"
             "This may take a long time to compute!")
        edges = bd.hammer_bundle(point_df, edge_df, weight="weight")
    else:
        raise ValueError(
            "{} is not a recognised bundling method".format(edge_bundling))

    edge_img = tf.shade(
        canvas.line(edges, "x", "y", agg=ds.sum("weight")),
        cmap=plt.get_cmap(edge_cmap),
        how=edge_how,
    )
    edge_img = tf.set_background(edge_img, background)

    if show_points:
        point_img = _datashade_points(
            points,
            None,
            labels,
            values,
            cmap,
            color_key,
            color_key_cmap,
            None,
            width,
            height,
            False,
        )
        if px_size > 1:
            point_img = tf.dynspread(point_img, threshold=0.5, max_px=px_size)
        result = tf.stack(edge_img, point_img, how="over")
    else:
        result = edge_img

    font_color = _select_font_color(background)

    dpi = plt.rcParams["figure.dpi"]
    fig = plt.figure(figsize=(width / dpi, height / dpi))
    ax = fig.add_subplot(111)

    _embed_datashader_in_an_axis(result, ax)

    ax.set(xticks=[], yticks=[])
    ax.text(
        0.99,
        0.01,
        "UMAP: n_neighbors={}, min_dist={}".format(umap_object.n_neighbors,
                                                   umap_object.min_dist),
        transform=ax.transAxes,
        horizontalalignment="right",
        color=font_color,
    )

    return ax
def create_image(x_range, y_range, w, h):
    cvs = ds.Canvas(plot_width=w, plot_height=h, x_range=x_range, y_range=y_range)
    agg = cvs.points(sep_trips, 'dropoff_x', 'dropoff_y',  ds.count('passenger_count'))
    img = tf.interpolate(agg, cmap=Hot, how='eq_hist')
    return tf.dynspread(img, threshold=0.5, max_px=4)
Пример #30
0
def image_callback(x_range, y_range, w, h):
    cvs = ds.Canvas(
          plot_width=w,plot_height=h,x_range=x_range,y_range=y_range)
    agg = cvs.points(df,'meterswest','metersnorth')
    img = tf.interpolate(agg, cmap = cmap, how='eq_hist')
    return tf.dynspread(img,threshold=0.75, max_px=8)
def create_image(x_range, y_range, w, h):
    cvs = ds.Canvas(plot_width=w, plot_height=h, x_range=x_range, y_range=y_range)
    agg = cvs.points(df, 'web_long', 'web_lat',  ds.count('trip_distance'))
    img = tf.interpolate(agg, cmap=Hot, how='eq_hist')
    return tf.dynspread(img, threshold=0.5, max_px=4)
Пример #32
0
def connectivity_base(
    x: int,
    y: int,
    edge_df: pd.DataFrame,
    highlights: Optional[list] = None,
    edge_bundling: Optional[str] = None,
    edge_cmap: str = "gray_r",
    show_points: bool = True,
    labels: Optional[list] = None,
    values: Optional[list] = None,
    theme: Optional[str] = None,
    cmap: str = "Blues",
    color_key: Union[dict, list, None] = None,
    color_key_cmap: str = "Spectral",
    background: str = "black",
    figsize: tuple = (7, 5),
    ax: Optional[Axes] = None,
    sort: str = "raw",
    save_show_or_return: str = "return",
    save_kwargs: dict = {},
) -> Union[None, Axes]:
    """Plot connectivity relationships of the underlying UMAP
    simplicial set data structure. Internally UMAP will make
    use of what can be viewed as a weighted graph. This graph
    can be plotted using the layout provided by UMAP as a
    potential diagnostic view of the embedding. Currently this only works
    for 2D embeddings. While there are many optional parameters
    to further control and tailor the plotting, you need only
    pass in the trained/fit umap model to get results. This plot
    utility will attempt to do the hard work of avoiding
    overplotting issues and provide options for plotting the
    points as well as using edge bundling for graph visualization.

    Parameters
    ----------
        x: `int`
            The first component of the embedding.
        y: `int`
            The second component of the embedding.
        edge_df `pd.DataFrame`
            The dataframe denotes the graph edge pairs. The three columns
            include 'source', 'target' and 'weight'.
        highlights: `list`, `list of list` or None (default: `None`)
            The list that cells will be restricted to.
        edge_bundling: string or None (optional, default None)
            The edge bundling method to use. Currently supported
            are None or 'hammer'. See the datashader docs
            on graph visualization for more details.
        edge_cmap: string (default 'gray_r')
            The name of a matplotlib colormap to use for shading/
            coloring the edges of the connectivity graph. Note that
            the ``theme``, if specified, will override this.
        show_points: bool (optional False)
            Whether to display the points over top of the edge
            connectivity. Further options allow for coloring/
            shading the points accordingly.
        labels: array, shape (n_samples,) (optional, default None)
            An array of labels (assumed integer or categorical),
            one for each data sample.
            This will be used for coloring the points in
            the plot according to their label. Note that
            this option is mutually exclusive to the ``values``
            option.
        values: array, shape (n_samples,) (optional, default None)
            An array of values (assumed float or continuous),
            one for each sample.
            This will be used for coloring the points in
            the plot according to a colorscale associated
            to the total range of values. Note that this
            option is mutually exclusive to the ``labels``
            option.
        theme: string (optional, default None)
            A color theme to use for plotting. A small set of
            predefined themes are provided which have relatively
            good aesthetics. Available themes are:
               * 'blue'
               * 'red'
               * 'green'
               * 'inferno'
               * 'fire'
               * 'viridis'
               * 'darkblue'
               * 'darkred'
               * 'darkgreen'
        cmap: string (optional, default 'Blues')
            The name of a matplotlib colormap to use for coloring
            or shading points. If no labels or values are passed
            this will be used for shading points according to
            density (largely only of relevance for very large
            datasets). If values are passed this will be used for
            shading according the value. Note that if theme
            is passed then this value will be overridden by the
            corresponding option of the theme.
        color_key: dict or array, shape (n_categories) (optional, default None)
            A way to assign colors to categoricals. This can either be
            an explicit dict mapping labels to colors (as strings of form
            '#RRGGBB'), or an array like object providing one color for
            each distinct category being provided in ``labels``. Either
            way this mapping will be used to color points according to
            the label. Note that if theme
            is passed then this value will be overridden by the
            corresponding option of the theme.
        color_key_cmap: string (optional, default 'Spectral')
            The name of a matplotlib colormap to use for categorical coloring.
            If an explicit ``color_key`` is not given a color mapping for
            categories can be generated from the label list and selecting
            a matching list of colors from the given colormap. Note
            that if theme
            is passed then this value will be overridden by the
            corresponding option of the theme.
        background: string (optional, default 'white)
            The color of the background. Usually this will be either
            'white' or 'black', but any color name will work. Ideally
            one wants to match this appropriately to the colors being
            used for points etc. This is one of the things that themes
            handle for you. Note that if theme
            is passed then this value will be overridden by the
            corresponding option of the theme.
        width: int (optional, default 800)
            The desired width of the plot in pixels.
        height: int (optional, default 800)
            The desired height of the plot in pixels
        sort: `str` (optional, default `raw`)
            The method to reorder data so that high values points will be on top of background points. Can be one of
            {'raw', 'abs'}, i.e. sorted by raw data or sort by absolute values.
        save_show_or_return: {'show', 'save', 'return'} (default: `return`)
            Whether to save, show or return the figure.
        save_kwargs: `dict` (default: `{}`)
            A dictionary that will passed to the save_fig function. By default it is an empty dictionary and the save_fig function
            will use the {"path": None, "prefix": 'connectivity_base', "dpi": None, "ext": 'pdf', "transparent": True, "close":
            True, "verbose": True} as its parameters. Otherwise you can provide a dictionary that properly modify those keys
            according to your needs.

    Returns
    -------
    result:
        Either return None or a matplotlib axis with the relevant plot displayed based on arguments.
        If you are using a notbooks and have ``%matplotlib inline`` set
        then this will simply display inline.
    """

    import matplotlib.pyplot as plt
    import datashader as ds
    import datashader.transfer_functions as tf
    import datashader.bundling as bd

    dpi = plt.rcParams["figure.dpi"]

    if theme is not None:
        cmap = _themes[theme]["cmap"]
        color_key_cmap = _themes[theme]["color_key_cmap"]
        edge_cmap = _themes[theme]["edge_cmap"]
        background = _themes[theme]["background"]

    points = np.array([x, y]).T
    point_df = pd.DataFrame(points, columns=("x", "y"))

    point_size = 500.0 / np.sqrt(points.shape[0])
    if point_size > 1:
        px_size = int(np.round(point_size))
    else:
        px_size = 1

    if show_points:
        edge_how = "log"
    else:
        edge_how = "eq_hist"

    extent = _get_extent(points)
    canvas = ds.Canvas(
        plot_width=int(figsize[0] * dpi),
        plot_height=int(figsize[1] * dpi),
        x_range=(extent[0], extent[1]),
        y_range=(extent[2], extent[3]),
    )

    if edge_bundling is None:
        edges = bd.directly_connect_edges(point_df, edge_df, weight="weight")
    elif edge_bundling == "hammer":
        warn("Hammer edge bundling is expensive for large graphs!\n"
             "This may take a long time to compute!")
        edges = bd.hammer_bundle(point_df, edge_df, weight="weight")
    else:
        raise ValueError(
            "{} is not a recognised bundling method".format(edge_bundling))

    edge_img = tf.shade(
        canvas.line(edges, "x", "y", agg=ds.sum("weight")),
        cmap=plt.get_cmap(edge_cmap),
        how=edge_how,
    )
    edge_img = tf.set_background(edge_img, background)

    if show_points:
        point_img = _datashade_points(
            points,
            None,
            labels,
            values,
            highlights,
            cmap,
            color_key,
            color_key_cmap,
            None,
            figsize[0] * dpi,
            figsize[1] * dpi,
            True,
            sort=sort,
        )
        if px_size > 1:
            point_img = tf.dynspread(point_img, threshold=0.5, max_px=px_size)
        result = tf.stack(edge_img, point_img, how="over")
    else:
        result = edge_img

    if ax is None:
        fig = plt.figure(figsize=figsize)
        ax = fig.add_subplot(111)

    _embed_datashader_in_an_axis(result, ax)

    ax.set(xticks=[], yticks=[])

    if save_show_or_return == "save":
        s_kwargs = {
            "path": None,
            "prefix": "connectivity_base",
            "dpi": None,
            "ext": "pdf",
            "transparent": True,
            "close": True,
            "verbose": True,
        }
        s_kwargs = update_dict(s_kwargs, save_kwargs)

        save_fig(**s_kwargs)
    elif save_show_or_return == "show":
        plt.tight_layout()
        plt.show()
    elif save_show_or_return == "return":
        return ax
		def create_image(x_range,y_range,w=plot_width,h=plot_height):
			canvas = ds.Canvas(plot_width=plot_width,plot_height=plot_height,
			x_range=x_range, y_range=y_range)
			agg = canvas.points(df,'x','y')
			img = tf.interpolate(agg,cmap=ds.colors.Hot,how='log')
			return tf.dynspread(img,threshold=0.5,max_px=4)