Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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
)
shade_opts['span'] = self.p.clims
elif ds_version > '0.5.0' and self.p.normalization != 'eq_hist':
shade_opts['span'] = element.range(vdim)
for d in kdims:
if array[d.name].dtype.kind == 'M':
array[d.name] = array[d.name].astype('datetime64[ns]').astype('int64') * 10e-4
with warnings.catch_warnings():
warnings.filterwarnings('ignore', r'invalid value encountered in true_divide')
if np.isnan(array.data).all():
arr = np.zeros(array.data.shape, dtype=np.uint32)
img = array.copy()
img.data = arr
else:
img = tf.shade(array, **shade_opts)
params = dict(get_param_values(element), kdims=kdims,
bounds=bounds, vdims=RGB.vdims[:])
return RGB(self.uint32_to_uint8(img.data), **params)
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.line(source=data_source, x=self.x, y=self.y)
img = tf.shade(
agg, cmap=["white", self.color], how=self.pixel_shade_type
)
return img
def timed_agg(df, filepath, plot_width=int(900), plot_height=int(900*7.0/12), cache_ranges=True):
global CACHED_RANGES
start = time.time()
cvs = ds.Canvas(plot_width, plot_height, x_range=CACHED_RANGES[0], y_range=CACHED_RANGES[1])
agg = cvs.points(df, p.x, p.y)
end = time.time()
if cache_ranges:
CACHED_RANGES = (cvs.x_range, cvs.y_range)
img = export_image(tf.shade(agg),filepath,export_path=".")
return img, end-start
bin_size = (max_val - min_val) / 256.0
data['val_cat'] = pd.Categorical(np.round((values - min_val) / bin_size).astype(np.int16))
aggregation = canvas.points(data, 'x', 'y', agg=ds.count_cat('val_cat'))
color_key = _to_hex(plt.get_cmap(cmap)(np.linspace(0, 1, 256)))
result = tf.shade(aggregation, color_key=color_key, how='eq_hist')
else:
data['val_cat'] = pd.Categorical(values)
aggregation = canvas.points(data, 'x', 'y', agg=ds.count_cat('val_cat'))
color_key_cols = _to_hex(plt.get_cmap(cmap)(np.linspace(0, 1, unique_values.shape[0])))
color_key = dict(zip(unique_values, color_key_cols))
result = tf.shade(aggregation, color_key=color_key, how='eq_hist')
# Color by density (default datashader option)
else:
aggregation = canvas.points(data, 'x', 'y', agg=ds.count())
result = tf.shade(aggregation, cmap=plt.get_cmap(cmap))
if background is not None:
result = tf.set_background(result, background)
if ax is not None:
_embed_datashader_in_an_axis(result, ax)
return ax
else:
return result
agg : xarray
Datashader aggregate object (e.g. result of Canvas.points())
cmap : list of colors or matplotlib.colors.Colormap, optional
The colormap to use. Can be either a list of colors (in any of the
formats described above), or a matplotlib colormap object.
how : str
Datashader transfer function name (either linear or log)
width : int
Width in pixels of resulting legend figure (default=600)
'''
vals_arr, min_val, max_val = summarize_aggregate_values(agg, how=how)
img = tf.shade(vals_arr, cmap=cmap, how=how)
x_axis_type = how
assert x_axis_type == 'linear' or x_axis_type == 'log'
legend_fig = Figure(x_range=(min_val, max_val),
plot_height=50,
plot_width=width,
lod_threshold=None,
toolbar_location=None,
y_range=(0, 18),
x_axis_type=x_axis_type)
legend_fig.min_border_top = 0
legend_fig.min_border_bottom = 10
legend_fig.min_border_left = 15
legend_fig.min_border_right = 15
legend_fig.yaxis.visible = False
legend_fig.grid.grid_line_alpha = 0
plot_height=height,
x_range=(extent[0], extent[1]),
y_range=(extent[2], extent[3]))
data = pd.DataFrame(points, columns=('x', 'y'))
# Color by labels
if labels is not None:
if labels.shape[0] != points.shape[0]:
raise ValueError('Labels must have a label for '
'each sample (size mismatch: {} {})'.format(labels.shape[0],
points.shape[0]))
data['label'] = pd.Categorical(labels)
aggregation = canvas.points(data, 'x', 'y', agg=ds.count_cat('label'))
if color_key is None and color_key_cmap is None:
result = tf.shade(aggregation, how='eq_hist')
elif color_key is None:
unique_labels = np.unique(labels)
num_labels = unique_labels.shape[0]
color_key = _to_hex(plt.get_cmap(color_key_cmap)(np.linspace(0, 1, num_labels)))
result = tf.shade(aggregation, color_key=color_key, how='eq_hist')
else:
result = tf.shade(aggregation, color_key=color_key, how='eq_hist')
# Color by values
elif values is not None:
if values.shape[0] != points.shape[0]:
raise ValueError('Values must have a value for '
'each sample (size mismatch: {} {})'.format(values.shape[0],
points.shape[0]))
unique_values = np.unique(values)
if unique_values.shape[0] >= 256:
# # Default plot ranges:
x_range = (start, end)
y_range = (1.2 * signal.min(), 1.2 * signal.max())
# Create a dataframe
data['Time'] = np.linspace(start, end, n)
df = pd.DataFrame(data)
time_start = df['Time'].values[0]
time_end = df['Time'].values[-1]
cvs = ds.Canvas(x_range=x_range, y_range=y_range)
aggs = OrderedDict((c, cvs.line(df, 'Time', c)) for c in cols)
img = tf.shade(aggs['Signal'])
arr = np.array(img)
z = arr.tolist()
# axes
dims = len(z[0]), len(z)
x = np.linspace(x_range[0], x_range[1], dims[0])
y = np.linspace(y_range[0], y_range[1], dims[0])
#######################################################################################################################
# Layout
#######################################################################################################################
external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css', '/assets/style.css']
app = dash.Dash(__name__, external_stylesheets=external_stylesheets)