def L_selected_index(index): """Create image of L detector corresponding to selected point. """ x_img = tomap['ex']['x'] y_img = tomap['ex']['y'] times_img = tomap['ex']['times'] if index: if plot_ex: ex_len = tomap['ex']['x'].shape[0] else: ex_len = 0 # ex_len = tomap['ex']['x'].shape[0] H_len = tomap['H']['x'].shape[0] if index[0] < ex_len: selected = hv.RGB(x_img[index]).opts(width=width, height=height) label = 'No selection' else: x_img = tomap['L']['x'] y_img = tomap['L']['y'] times_img = tomap['L']['times'] if index[0] < ex_len + H_len: selected = hv.RGB(x_img[index[0] - ex_len]).opts(width=width, height=height) label = 'L1 (H1): %s, %f, %d selected' % (y_img[index[0] - ex_len], times_img[index[0] - ex_len], len(index)) else: selected = hv.RGB(x_img[index[0] - (ex_len + H_len)]).opts(width=width, height=height) label = 'L1 (L1): %s, %f, %d selected' % (y_img[index[0] - (ex_len + H_len)], times_img[index[0] - (ex_len + H_len)], len(index)) else: selected = hv.RGB(x_img[index]).opts(width=width, height=height) label = 'No selection' return selected.relabel(label).opts(labelled=[])
def selected_index(index): """Create image corresponding to selected image in point plot. """ if index: selected = hv.RGB(x[index[0]]).opts(width=500, height=500) label = '%s, %f, %d, %d selected' % (y[index[0]], times[index[0]], index[0], len(index)) else: selected = hv.RGB(x[index]).opts(width=500, height=500) label = 'No selection' return selected.relabel(label).opts(labelled=[])
def blend_overlay(elems): '''Transforms a hv.Overlay of hv.Image into a hv.RGB''' if not isinstance(elems, hv.Overlay): # probably a single channel, do nothing return elems imgs = [e.dimension_values(2, flat=False) for e in elems] if imgs[0].dtype != np.uint8: raise ValueError( '8 bit images are expected to stack overlays, got {}'.format( imgs[0].dtype)) # embed colormap,opacity and blend # Note somehow hv.RGB inverts the y axis but not hv.Image??? cmaps = [e.opts.get().options['cmap'] for e in elems] alphas = [e.opts.get().options['alpha'] for e in elems] imgs = [(a * img).astype(int) if a < 1.0 else img for a, img in zip(alphas, imgs)] rgb = make_composite(imgs, cmaps, mode='max')[::-1] xr = elems.range(0) yr = elems.range(1) bounds = (xr[1], yr[0], xr[0], yr[1]) height, width = rgb.shape[:-1] options = list(elems)[0].opts.get().options options = { key: val for key, val in options.items() if key in valid_rgb_options } return hv.RGB(rgb, bounds=bounds, group='composite').opts(**options)
def get_pred_masks(trained, dataloader, device, show=False): """ Run inference on all data from iterated via dataloader Args: - dataloader (SegDataset): must be a SegDataset object with `get_label=False` Returns: - preds (n_tests, H, W, n_test ) numpy array, representing predicted label masks """ trained = trained.to(device) trained.eval() preds_np = [] with torch.no_grad(): for x in dataloader: # print(x.shape) x = x.to(device) pred_y = model(x) #input x should be 4dim: (BS=1, n_classes=21, H,W) _, pred_label = torch.max(pred_y, 1) #output would be 3dim (BS=1, H,W) pred_label_np = pred_label.cpu().numpy().squeeze() preds_np.append(pred_label_np) if show: ## Visualize x_np = x.squeeze().cpu().numpy().squeeze().transpose(1,2,0) print(f'pred_label shape: {pred_label.shape}') print(f'unique labels: ', np.unique(pred_label_np)) overlay = hv.RGB(x_np) * hv.Image(pred_label_np, group='mask') display(overlay.opts(shared_axes=False)) # pdb.set_trace() return np.asarray(preds_np)
def hv_dataloader(dataloader, idx): """ Creates an overlay of x and y pair from the ith iteration of the dataloader If the dataloader's batchsize > 1, this shows just the first pair of (x,y) Args: - dataloader: a dataloader that returns a pair of (batch_x, batch_y) - idx: ith iteration of dataloader. Currently this is not the most efficient way to visualize the dataloader. Example: dmap_sample_dl = hv.DynamicMap(lambda idx: hv_dataloader(sample_train_dl, idx), kdims=['idx']) dmap_sample_dl.redim.values(idx=list(range(len(sample_train_dl)))) """ for i, (x, y) in enumerate(dataloader): if i != idx: continue if x.dim() == 4 and y.dim() == 3: print( 'More than one image in the batch. Showing only the first one...' ) x, y = x[0], y[0] assert x.dim() == 3 and y.dim() == 2 x_np = x.detach().numpy().transpose((1, 2, 0)) y_np = y.detach().numpy().astype(np.int) overlay = hv.RGB(x_np) + hv.Image(y_np, group='mask') return overlay
def combine_bands(r, g, b): """ Recombines colorbands """ xs, ys = r['y'], r['x'] r, g, b = [ds.utils.orient_array(im) for im in (r, g, b)] return hv.RGB((xs, ys[::-1], r, g, b), vdims=list('RGB'))
def test_rgb_ellipsis_slice_value_missing(self): rgb = hv.RGB(np.random.rand(10, 10, 3)) try: rgb[..., 'Non-existent'] except Exception as e: if str(e) != repr( "'Non-existent' is not an available value dimension"): raise AssertionError("Incorrect exception raised.")
def plotImg(self, array): rgb = np.ma.dstack(array) img = hv.RGB(rgb) rangexy = hv.streams.RangeXY(source=img) img << hv.DynamicMap(self.selected_hist, streams=[rangexy]) return img << hv.DynamicMap(self.selected_hist, streams=[rangexy])
def infer_on_train(trained, dataset, idx, device, return_hv=False): """ Run inference on the `idx`th sample from dataloader using `trained` model Args: - dataset (Dataset): returns a (batch of) x - idx (int): index into the dataset samples Returns - `Holoviews` layout object if `return_hv` is True else None """ trained = trained.to(device) trained.eval() with torch.no_grad(): x,y = dataset[idx] print(x.shape, y.shape) x,y = x.to(device), y.to(device) x.unsqueeze_(0) y.unsqueeze_(0) print(x.shape, y.shape) pred_y = trained(x) _, pred_label = torch.max(pred_y, 1) pred_label_np = pred_label.cpu().numpy().squeeze() x_np = x.squeeze().cpu().numpy().transpose(1,2,0) y_np = y.squeeze().cpu().numpy() print(f'pred_label shape: {pred_label.shape}') print(f'unique labels: ', np.unique(pred_label_np)) if return_hv: overlay_gt = hv.RGB(x_np) * hv.Image(y_np, group='mask').opts(axiswise=True) overlay_pred = hv.RGB(x_np) * hv.Image(pred_label_np, group='mask').opts(axiswise=True) overlay_masks = ( hv.Image(y_np *(y_np <= 21), group='mask').opts(axiswise=True, shared_axes=False) + hv.Image(pred_label_np, group='mask').opts(axiswise=True, shared_axes=False) ) # return overlay1.opts(shared_axes=False) # return (overlay_gt+overlay_pred).opts(shared_axes=False) return (overlay_masks) else: return pred_label_np
def get_image(self, data=None, x_range=None, y_range=None, x=None, y=None): rlow, rhigh = self.rscale glow, ghigh = self.gscale blow, bhigh = self.bscale if not data: data = {} scl = self.ds.compute_scale(x_range, y_range, res=self.resolution) self.scl = scl n = data.get("section", 1) self.current_section = n r, g, b = self.get_rgb(n, scl) try: chunksize = r.chunksize[0] y1, y2 = ( y_range[0] // scl, y_range[1] // scl, ) x1, x2 = ( x_range[0] // scl, x_range[1] // scl, ) x1, x2, y1, y2 = ( int(x1) - chunksize // 2, int(x2) + chunksize // 2, int(y1) - chunksize // 2, int(y2) + chunksize // 2, ) x1 = max(0, x1) x2 = min(x2, self.ds.shape[1] // scl) y1 = max(0, y1) y2 = min(y2, self.ds.shape[0] // scl) except: # noqa: E722 x1 = y1 = 0 y2 = self.ds.shape[0] // scl x2 = self.ds.shape[1] // scl ir = r[y1:y2, x1:x2] ig = g[y1:y2, x1:x2] ib = b[y1:y2, x1:x2] ir, ig, ib = dask.compute([ir, ig, ib])[0] im = hv.RGB(( range(x1 * scl, x2 * scl, scl), range(y1 * scl, y2 * scl, scl), ir, ig, ib, )) return im
def generate_holo_map(rgb_images, height, width): frame_map = {} for i, image in enumerate(rgb_images): # print('image type: ' + str(type(image))) hv_rgb = hv.RGB(np.array(image)) shape = image.shape frame_map[i] = hv_rgb holomap = hv.HoloMap(frame_map) holomap = holomap.options(width=int(width), height=int(height)) return holomap
def plot_img(x, y, classes): """ Display a single image nicely :param x: an array shaped (width, height, 3) :param y: an array shaped (n) :param classes: a list of string shaped (n) :returns: hv.RGB """ label = f'{classes[np.argmax(y)]} at {np.max(y):.0%}' return hv.RGB(x / 255, label=label).opts(xaxis='bare', yaxis='bare', width=x.shape[0], height=x.shape[1])
def one_band(band, time): xs, ys = dataset[band].sel(time=time)[dims[0]], dataset[band].sel( time=time)[dims[1]] b = ds.utils.orient_array(dataset[band].sel(time=time)) a = (np.where(np.logical_or(np.isnan(b), b <= nodata), 0, 255)).astype(np.uint8) return shade(regrid( hv.RGB((xs, ys[::-1], b, b, b, a), vdims=list('RGBA'))), cmap=cmap, clims=clims, normalization=norm).redim(x=dims[0], y=dims[1]).opts(width=width, height=height)
def get_image_zoom(self, data=None, x=None, y=None): rlow, rhigh = self.rscale glow, ghigh = self.gscale blow, bhigh = self.bscale if not data: data = {} width = 160 n = data.get("section", 1) scl = 1 r, g, b = self.get_rgb(n, scl) try: x1 = int(x) - width // 2 y1 = int(y) - width // 2 x2 = int(x) + width // 2 y2 = int(y) + width // 2 except Exception: x1 = y1 = 0 x2 = y2 = width if x1 < 0: x1 = 0 x2 = x1 + width if y1 < 0: y1 = 0 y2 = y1 + width if x2 >= self.ds.shape[1]: x2 = self.ds.shape[1] - 1 x1 = x2 - width if y2 >= self.ds.shape[1]: y2 = self.ds.shape[1] - 1 y1 = y2 - width ir = r[y1:y2, x1:x2] ig = g[y1:y2, x1:x2] ib = b[y1:y2, x1:x2] ir, ig, ib = dask.compute([ir, ig, ib])[0] im = hv.RGB(( range(0, width), range(0, width), ir, ig, ib, )) return im
def show_batch(batch, max_num=2): """ batch (tuple of tensors): returned by Dataloader Visualize the batch x and y overlay as hv Elements """ batch_x, batch_y = batch bs = len(batch_x) assert bs == len(batch_y), len(batch_y) for i, (x, y) in enumerate(zip(batch_x, batch_y)): if i >= max_num: break x_np = x.detach().numpy().transpose((1, 2, 0)) y_np = y.detach().numpy() overlay = hv.RGB(x_np) + hv.Image(y_np, group='mask') display(overlay)
def get_plot(butler, tract, filt, description, style, visit=None, kind='coadd', scale=None): filename = get_plot_filename(butler, tract, filt, description, style, visit=visit, kind=kind) try: rgb = hv.RGB.load_image(filename, bare=True) # back out the aspect ratio from bounds l,b,r,t = rgb.bounds.lbrt() aspect = (r-l)/(t-b) h = 480 w = int(h * aspect) rgb = rgb.opts(plot={'width':w, 'height':h}) if scale is not None: rgb = rgb.opts(plot={'width':int(w*scale), 'height':int(h*scale)}) return rgb except FileNotFoundError: return hv.RGB(np.zeros((2,2))).opts(plot={'width':640, 'height':480})
def background(func, size=(500, 500)): """ Given the ODE y'=f(x,y), bg,vec,xaxis_line,yaxis_line = background() returns a grayscale image of the slopes y', a vector field representation of the slopes, and a set of axis lines for -5<x<5, -5<y<5 """ # compute the data vals = np.linspace(-5, 5, num=150) X, Y = np.meshgrid(vals, vals) clines = func(X, Y) # f(x,y) theta = np.arctan2(clines, 1) # angle of the slope y' at x,y # Obtain the vector field (subsample the grid) h, w = size vf_opts = dict(size_index=3, height=h, width=w, xticks=9, yticks=9, alpha=0.3, muted_alpha=0.05) vec_field = hv.VectorField( (vals[::3], vals[::3], theta[::3, ::3], 0 * clines[::3, ::3] + 1), label='vector_field').options(**vf_opts) # Normalize the given array so that it can be used with the RGB element's alpha channel def norm(arr): arr = (arr - arr.min()) return arr / arr.max() normXY = norm(clines) img_field = hv.RGB( (vals, vals, normXY, normXY, normXY, 0*clines+0.1), vdims=['R','G','B','A'] )\ .options(width=size[0], height=size[1], shared_axes=False) # finally, we add the axes as VLine, HLine and return an array of the plot Elements hv_opts = dict(color='k', alpha=0.8, line_width=1.5) return [ img_field, vec_field, hv.HLine(0).options(**hv_opts), hv.VLine(0).options(**hv_opts) ]
def combine_ch_colors_cropped(self): channels = self.selected_data_cropped min_values = self.mins_cropped max_values = self.maxs_cropped colours = self.colors_cropped xs, ys = channels[0]["x"], channels[0]["y"] color_arrays = [] for col in colours: arr = self.select_color_rgb(col) color_arrays.append(arr) rgb_tot = np.zeros((channels[0].shape[0], channels[0].shape[1], 3)) for n in range(len(channels)): ch_data = channels[n].data if "p" in str(max_values[n]): percentile = float(max_values[n][1:]) threshold = np.percentile(ch_data, percentile) print( "Auto Max value for channel: " + str(n) + " is: " + str(threshold) ) if threshold == 0: threshold = 0.01 else: threshold = max_values[n] if "p" in str(min_values[n]): percentile_min = float(min_values[n][1:]) threshold_min = np.percentile(ch_data, percentile_min) print( "Auto Min value for channel: " + str(n) + " is: " + str(threshold_min) ) if threshold_min == 0: threshold_min = 0.01 else: threshold_min = min_values[n] ch = ds.utils.orient_array(channels[n]) out = norm_colorize(ch, threshold_min, threshold, color_arrays[n], rgb_tot) rgb_tot = out self.rendered_image_cropped = hv.RGB( (xs, ys[::-1], rgb_tot[:, :, 0], rgb_tot[:, :, 1], rgb_tot[:, :, 2]) ) self.rgb_tot_cropped = rgb_tot
def get_holomaps(): # dict with jpeg byte strings of all images from the parameter study # {pset_id: jpegstr} jpegstr_dct = common.pkread('img_dct_rgb.pk') # shape: assume all imgs have the same shape shape = common.jpegstr2imgarr(jpegstr_dct[list( jpegstr_dct.keys())[0]]).shape # the parameter sweep database (created by the psweep package) df = ps.df_read('results.pk') df = df[df.fail_state.isna()] vary_cols = [ 'style_weight', 'tv_weight', 'learning_rate', 'style_scales', 'content_weight_blend', 'style_layer_weight_exp' ] holos = {} print("creating holomaps ...") for study in vary_cols: print(" " + study) this_df = df[df.study == study].sort_values(study) # {value of varied param (study): array shape (width, height, 3),...} imgs = dict((this_df.loc[this_df._pset_id == pset_id, study][0], hv.RGB(common.jpegstr2imgarr(jpegstr_dct[pset_id]))) for pset_id in this_df._pset_id) holos[study] = hv.HoloMap(imgs, kdims=study) # holoviews settings for matplotlib hv.util.opts({ 'RGB': { 'plot': { 'fig_latex': False, 'aspect': shape[1] / shape[0], 'fig_size': 200, 'xaxis': False, 'yaxis': False } } }) print("\nhang tight, we're rendering stuff ...") return holos
def combine_bands(): xs, ys = dataset[bands[0]].sel( time=timestep)[dims[0]], dataset[bands[0]].sel( time=timestep)[dims[1]] r, g, b = [ ds.utils.orient_array(img) for img in (dataset[bands[0]].sel(time=timestep), dataset[bands[1]].sel(time=timestep), dataset[bands[2]].sel(time=timestep)) ] a = (np.where(np.logical_or(np.isnan(r), r <= nodata), 0, 255)).astype(np.uint8) r = (normalize_data(r)).astype(np.uint8) g = (normalize_data(g)).astype(np.uint8) b = (normalize_data(b)).astype(np.uint8) return regrid(hv.RGB((xs, ys[::-1], r, g, b, a), vdims=list('RGBA'))).redim(x=dims[0], y=dims[1])
def hv_batch(batch): """ Creates a holoviews overlay of RGB (for x data) and Image (for y, the label mask) batch is a tuple of (4d tensor, 3d tensor) as returned by a dataloader or a (3d tensor, 2d tensor) corresponding to a data pair returned by a dataset """ x, y = batch if x.dim() == 4 and y.dim() == 3: print( 'More than one image in the batch. Showing only the first one...') x, y = x[0], y[0] assert x.dim() == 3 and y.dim() == 2 x_np = x.detach().numpy().transpose((1, 2, 0)) y_np = y.detach().numpy() overlay = hv.RGB(x_np) + hv.Image(y_np, group='mask') return overlay
def get_color_plot(butler, tract=8766, description='color_wPerp', style='psfMagHist', scale=None): dataId = {'tract':tract} filenamer = Filenamer(butler, 'plotColor', dataId) filename = filenamer(description=description, dataId=dataId, style=style) try: rgb = hv.RGB.load_image(filename, bare=True) # back out the aspect ratio from bounds l,b,r,t = rgb.bounds.lbrt() aspect = (r-l)/(t-b) h = 480 w = int(h * aspect) rgb = rgb.opts(plot={'width':w, 'height':h}) if scale is not None: rgb = rgb.opts(plot={'width':int(w*scale), 'height':int(h*scale)}) return rgb except FileNotFoundError: return hv.RGB(np.zeros((2,2))).opts(plot={'width':640, 'height':480})
def combine_bands(r, g, b, time): xs, ys = dataset[r].sel(time=time)[dims[0]], dataset[r].sel( time=time)[dims[1]] r, g, b = [ ds.utils.orient_array(img) for img in ( dataset[r].sel(time=time), dataset[g].sel(time=time), dataset[b].sel(time=time), ) ] a = (np.where(np.logical_or(np.isnan(r), r <= nodata), 0, 255)).astype(np.uint8) r = (normalize_data(r)).astype(np.uint8) g = (normalize_data(g)).astype(np.uint8) b = (normalize_data(b)).astype(np.uint8) return (regrid(hv.RGB( (xs, ys[::-1], r, g, b, a), vdims=list("RGBA"))).redim(x=dims[0], y=dims[1]).opts(width=width, height=height))
def rgb_plot(rgb, da, label='RGB Plot', width=800, height=800): '''Use Holoviews for an RGB plot of a rgb data array''' TOOLTIPS = [ ("(x,y)", "($x{0,0.0}, $y{0,0.0})"), ] hover = HoverTool(tooltips=TOOLTIPS) xmin, ymax = da.transform[2], da.transform[5] xmax = xmin + da.transform[0] * da.shape[2] ymin = ymax + da.transform[4] * da.shape[1] bounds = (xmin, ymin, xmax, ymax) epsg = da.crs.split(':')[1] kdims = [f'Easting [m] (EPSG:{epsg})', 'Northing [m]'] hv_rgb = hv.RGB(rgb, bounds=bounds, kdims=kdims, label=label) hv_rgb = hv_rgb.options( opts.RGB(width=width, height=height, tools=[hover], xformatter='%.0f', yformatter='%.0f')) return hv_rgb
def clip(self, lower_lim, upper_lim): '''this functions cuts off the values below/above the lower/upper limit, respectively, and then stretches/squeezes the remaining values on [0, 1].''' bands_array = np.asarray(self.rgbscaled).astype(float) image_array_clip = np.ma.masked_array(np.ones(self.rgbscaled.shape), mask=self.rgbscaled.mask.copy) image_array = np.ma.masked_array(np.empty(self.rgbscaled.shape), mask=self.rgbscaled.mask.copy) image_array_clip[:3, :, :] = np.clip(self.rgbscaled.data[:3, :, :], lower_lim, upper_lim) image_array[:3, :, :] = self.array_normalisation( image_array_clip.data[:3, :, :]) image_array.data[-1] = self.rgbscaled.data[-1] image_array.mask = self.rgbscaled.mask arr = np.ma.dstack(image_array) #return self.plotImg(image_array) return hv.RGB(arr)
def show_image(ds): shape = ds['R'].shape aspect = shape[1] / shape[0] wheel_zoom = WheelZoomTool(zoom_on_axis=False) image = (hv.RGB(ds, ['X', 'Y'], ['R', 'G', 'B'])).opts( 'RGB', default_tools=['pan', wheel_zoom, 'tap', 'reset'], active_tools=['tap', 'wheel_zoom'], xaxis=None, yaxis=None, aspect=aspect, responsive=True, hooks=[remove_white_borders], ).opts(toolbar='above') tap = hv.streams.Tap(source=image, x=shape[1], y=shape[0]) tap.param.watch(tap_update, ['x', 'y']) return image
def plot_image_with_boxes(self): self.analyzer.score_th = self.score_th image_with_boxes = self.analyzer.visualize_example( key=self.frame_id, show_predictions=True, show_ground_truth=True, class_names=self.analyzer.class_names, bgr2rgb=True, resize_factor=self.resize_factor, filter_pred_by_score=True, ) # TODO: try to improve speed by updating figure content, instead of creating new figure in every function call # create holoviews figure fig = hv.RGB(image_with_boxes) fig_width = image_with_boxes.shape[1] #// 20 fig_height = image_with_boxes.shape[0] #// 20 fig.options(width=fig_width, height=fig_height) fig = pn.pane.HoloViews(fig, width=fig_width, height=fig_height, sizing_mode='scale_both') # create matplotlib figure # ax = self.ax_image_with_boxes # if ax is None: # plt.ioff() # fig, ax = plt.subplots() # ax.clear() # ax.imshow(image_with_boxes) # title_str = '{}'.format(self.frame_id) # plt.title(title_str) # fig = ax.figure # fig.tight_layout() # self.ax_image_with_boxes = ax return fig
def infer_on_test(trained, dataset, idx, device, return_hv=False): """ Test `idx`th sample from dataloader using `trained` model Args: - dataset (Dataset): returns a (batch of) x - idx (int): index into the dataset samples Returns: - pred_label_np (np.ndarray): (H,W) shaped np array for predicition mask - if return_hv is True: - returns the `Holoviews` layout object of the x, prediciton mask images """ trained = trained.to(device) trained.eval() with torch.no_grad(): x = dataset[idx] print(x.shape) x = x.to(device) x.unsqueeze_(0) print(x.shape) pred_y = trained(x) _, pred_label = torch.max(pred_y, 1) pred_label_np = pred_label.cpu().numpy().squeeze() x_np = x.squeeze().cpu().numpy().transpose(1,2,0) print(f'pred_label shape: {pred_label.shape}') print(f'unique labels: ', np.unique(pred_label_np)) if return_hv: overlay = hv.RGB(x_np) * hv.Image(pred_label_np, group='mask') # display(overlay.opts(shared_axes=False)) return overlay else: return pred_label_np
def create_plot(ddf, xdatum, ydatum, adatum, ared, cdatum, cmap, bmap, dmap, normalize, xlabel, ylabel, title, pngname, options=None): figx = options.xcanvas / 60 figy = options.ycanvas / 60 bgcol = "#" + options.bgcol.lstrip("#") xaxis = xdatum.label yaxis = ydatum.label aaxis = adatum and adatum.label caxis = cdatum and cdatum.label color_key = ncolors = color_mapping = color_labels = agg_alpha = raster_alpha = None xmin, xmax = xdatum.minmax ymin, ymax = ydatum.minmax canvas = datashader.Canvas(options.xcanvas, options.ycanvas, x_range=[xmin, xmax] if xmin is not None else None, y_range=[ymin, ymax] if ymin is not None else None) if aaxis is not None: agg_alpha = getattr(datashader.reductions, ared, None) if agg_alpha is None: raise ValueError(f"unknown alpha reduction function {ared}") agg_alpha = agg_alpha(aaxis) ared = ared or 'count' if cdatum is not None: if agg_alpha is not None and not USE_REDUCE_BY: log.debug(f'rasterizing alpha channel using {ared}(aaxis)') raster_alpha = canvas.points(ddf, xaxis, yaxis, agg=agg_alpha) if data_mappers.USE_COUNT_CAT: color_bins = [int(x) for x in getattr(ddf.dtypes, caxis).categories] log.debug(f'colourizing with count_cat, {len(color_bins)} bins') if USE_REDUCE_BY and agg_alpha: agg = datashader.by(caxis, agg_alpha) else: agg = datashader.count_cat(caxis) else: color_bins = list(range(cdatum.nlevels)) log.debug(f'colourizing with count_integer, {len(color_bins)} bins') if USE_REDUCE_BY and agg_alpha: agg = by_integers(caxis, agg_alpha, cdatum.nlevels) else: agg = count_integers(caxis, cdatum.nlevels) raster = canvas.points(ddf, xaxis, yaxis, agg=agg) non_empty = numpy.array(raster.any(axis=(0, 1))) if not non_empty.any(): log.info(": no valid data in plot. Check your flags and/or plot limits.") return None # true if axis is continuous discretized if cdatum.discretized_delta is not None: # color labels are bin centres bin_centers = [cdatum.discretized_bin_centers[i] for i in color_bins] # map to colors pulled from 256 color map color_key = [bmap[(i*256)//cdatum.nlevels] for i in color_bins] color_labels = list(map(str, bin_centers)) log.info(f": shading using {len(color_bins)} colors (bin centres are {' '.join(color_labels)})") # else a discrete axis else: # discard empty bins non_empty = numpy.where(non_empty)[0] raster = raster[..., non_empty] # just use bin numbers to look up a color directly color_bins = [color_bins[i] for i in non_empty] color_key = [dmap[bin] for bin in color_bins] # the numbers may be out of order -- reorder for color bar purposes bin_color = sorted(zip(color_bins, color_key)) if cdatum.discretized_labels and len(cdatum.discretized_labels) <= cdatum.nlevels: color_labels = [cdatum.discretized_labels[bin] for bin, _ in bin_color] else: color_labels = [str(bin) for bin, _ in bin_color] color_mapping = [col for _, col in bin_color] log.info(f": rendering using {len(color_bins)} colors (values {' '.join(color_labels)})") if raster_alpha is not None: amin, amax = numpy.nanmin(raster_alpha), numpy.nanmax(raster_alpha) raster = raster*(raster_alpha-amin)/(amax-amin) log.info(f": adjusting alpha (alpha raster was {amin} to {amax})") img = datashader.transfer_functions.shade(raster, color_key=color_key, how=normalize) else: log.debug(f'rasterizing using {ared}') raster = canvas.points(ddf, xaxis, yaxis, agg=agg_alpha) if not raster.data.any(): log.info(": no valid data in plot. Check your flags and/or plot limits.") return None log.debug('shading') img = datashader.transfer_functions.shade(raster, cmap=cmap, how=normalize) if options.spread_pix: img = datashader.transfer_functions.dynspread(img, options.spread_thr, max_px=options.spread_pix) log.info(f": spreading ({options.spread_thr} {options.spread_pix})") rgb = holoviews.RGB(holoviews.operation.datashader.shade.uint32_to_uint8_xr(img)) log.debug('done') # Set plot limits based on data extent or user values for axis labels data_xmin = numpy.min(raster.coords[xaxis].values) data_xmax = numpy.max(raster.coords[xaxis].values) data_ymin = numpy.min(raster.coords[yaxis].values) data_ymax = numpy.max(raster.coords[yaxis].values) xmin = data_xmin if xmin is None else xdatum.minmax[0] xmax = data_xmax if xmax is None else xdatum.minmax[1] ymin = data_ymin if ymin is None else ydatum.minmax[0] ymax = data_ymax if ymax is None else ydatum.minmax[1] log.debug('rendering image') def match(artist): return artist.__module__ == 'matplotlib.text' fig = pylab.figure(figsize=(figx, figy)) ax = fig.add_subplot(111, facecolor=bgcol) ax.imshow(X=rgb.data, extent=[data_xmin, data_xmax, data_ymin, data_ymax], aspect='auto', origin='lower') ax.set_title("\n".join(textwrap.wrap(title, 90)), loc='left') ax.set_xlabel(xlabel) ax.set_ylabel(ylabel) # ax.plot(xmin,ymin,'.',alpha=0.0) # ax.plot(xmax,ymax,'.',alpha=0.0) dx, dy = xmax - xmin, ymax - ymin ax.set_xlim([xmin - dx/100, xmax + dx/100]) ax.set_ylim([ymin - dy/100, ymax + dy/100]) # set fontsize on everything rendered so far for textobj in fig.findobj(match=match): textobj.set_fontsize(options.fontsize) # colorbar? if color_key: import matplotlib.colors # discrete axis if color_mapping is not None: norm = matplotlib.colors.Normalize(-0.5, len(color_bins)-0.5) ticks = numpy.arange(len(color_bins)) colormap = matplotlib.colors.ListedColormap(color_mapping) # discretized axis else: norm = matplotlib.colors.Normalize(cdatum.minmax[0], cdatum.minmax[1]) colormap = matplotlib.colors.ListedColormap(color_key) # auto-mark colorbar, since it represents a continuous range of values ticks = None cb = fig.colorbar(matplotlib.cm.ScalarMappable(norm=norm, cmap=colormap), ax=ax, ticks=ticks) # adjust ticks for discrete axis if color_mapping is not None: rot = 0 # adjust fontsize for number of labels fs = max(options.fontsize*min(1, 32./len(color_labels)), 6) fontdict = dict(fontsize=fs) if max([len(lbl) for lbl in color_labels]) > 3 and len(color_labels) < 8: rot = 90 fontdict['verticalalignment'] ='center' cb.ax.set_yticklabels(color_labels, rotation=rot, fontdict=fontdict) fig.savefig(pngname, bbox_inches='tight') pylab.close() return pngname
def test_rgb_ellipsis_slice_value(self): data = np.random.rand(10, 10, 3) sliced = hv.RGB(data)[:, :, 'R'] self.assertEqual(sliced.data, data[:, :, 0])