########################### ### using the csv files ### ########################### anotations_all = pd.read_csv('/media/pedro/DATAPART1/AGOnIA/datasets_figure/notazione finale/NEU LARGE/test.csv') anotations_all.columns=['experiment','xmin','ymin','xmax','ymax','id'] anotations_all.loc[anotations_all['experiment']=='neurofinder.01.01.bmp'] boxes_csv = [] for index, row in anotations_all.loc[anotations_all['experiment']=='neurofinder.01.01.bmp'].iterrows(): boxes_csv.append([row['xmin'],row['ymin'],row['xmax'],row['ymax']]) np.shape(boxes_csv) np.shape(boxes) ################################## ### load agonia boxes and plot ### ################################## data_path = '/media/pedro/DATAPART1/AGOnIA/datasets_figure/neurofinder/neurofinder.01.01' data_name,median_projection,fnames,fname_new,results_caiman_path,boxes_path = ut.get_files_names(data_path) boxes_agonia = pickle.load(open(boxes_path,'rb')) boxes_agonia=boxes_agonia[boxes_agonia[:,4]>.15].astype(int)[:,:4] boxes_agonia.shape roi_bounds = hv.Path([hv.Bounds(tuple([roi[0],median_projection.shape[0]-roi[1],roi[2],median_projection.shape[0]-roi[3]])) for roi in boxes_agonia[:,:4]]).options(color='red') roi_bounds_true = hv.Path([hv.Bounds(tuple([roi[0],median_projection.shape[0]-roi[1],roi[2],median_projection.shape[0]-roi[3]])) for roi in boxes_csv]).options(color='green') img = hv.Image(median_projection,bounds=(0,0,median_projection.shape[1],median_projection.shape[0])).options(cmap='gray') (img*roi_bounds*roi_bounds_true).opts(fig_size=300)
def test_image_ellipsis_slice_value(self): data = np.random.rand(10, 10) sliced = hv.Image(data)[..., 'z'] self.assertEqual(sliced.data, data)
def task_fMRI_plots(SBJ, PURE, WL_sec, corr_range): # Define segment data # ------------------- if WL_sec == 30: if PURE == 'pure': seg_df = WL30pure_taskseg_df else: seg_df = WL30_taskseg_df else: if PURE == 'pure': seg_df = WL45pure_taskseg_df else: seg_df = WL45_taskseg_df # Define PURE varaible based on widget # ------------------------------------ if PURE == 'not pure': PURE = '' # Load data with non pure windows # Load task fMRI data # ------------------- file_name = SBJ + '_CTask001_WL0' + str( WL_sec) + '_WS01' + PURE + '_NROI0200_dF.mat' # Data file name data_path = osp.join( '/data/SFIMJGC_HCP7T/PRJ_CognitiveStateDetection02/PrcsData_PNAS2015', SBJ, 'D02_CTask001', file_name) # Path to data data_df = loadmat(data_path)['CB']['snapshots'][0][0] # Read data num_samp = data_df.shape[0] # Save number of samples as a varable # Create sleep segments plots # --------------------------- task_color_map = { 'Rest': 'gray', 'Memory': 'blue', 'Video': 'yellow', 'Math': 'green', 'Inbetween': 'black' } # Color key for task segments seg_x = hv.Segments(seg_df, [ hv.Dimension('start', range=(-10, num_samp - 1.5)), hv.Dimension('start_event', range=(-5, num_samp - 1.5)), 'end', 'end_event' ], 'task').opts(color='task', cmap=task_color_map, line_width=7, show_legend=True) # x axis segments seg_y = hv.Segments(seg_df, [ hv.Dimension('start_event', range=(-10, num_samp - 1.5)), hv.Dimension('start', range=(-5, num_samp - 1.5)), 'end_event', 'end' ], 'task').opts(color='task', cmap=task_color_map, line_width=7, show_legend=False) # y axis segments seg_plot = (seg_x * seg_y).opts(xlabel=' ', ylabel=' ', show_legend=False) # All segments # Compute correlation and distance matrix # --------------------------------------- data_corr = np.corrcoef(data_df) # Correlation matrix data_dist = pairwise_distances(data_df, metric='euclidean') # Distance matrix # Compute distribution of correlation and distance matrix # ------------------------------------------------------- triangle = np.mask_indices(num_samp, np.triu, k=1) # Top triangle mask for matricies corr_freq, corr_edges = np.histogram( np.array(data_corr)[triangle], 100 ) # Compute histogram of top triangle of correlation matrix (100 bars) dist_freq, dist_edges = np.histogram( np.array(data_dist)[triangle], 100) # Compute histogram of top triangle of distance matrix (100 bars) # Create matrix and histogram plots # --------------------------------- corr_img = hv.Image( np.rot90(data_corr), bounds=(-0.5, -0.5, num_samp - 1.5, num_samp - 1.5)).opts( cmap='viridis', colorbar=True, height=300, width=400, title='Correlation Matrix').redim.range(z=corr_range) dist_img = hv.Image(np.rot90(data_dist), bounds=(-0.5, -0.5, num_samp - 1.5, num_samp - 1.5)).opts(cmap='viridis', colorbar=True, height=300, width=400, title='Distance Matrix') corr_his = hv.Histogram( (corr_edges, corr_freq)).opts(xlabel='Correlation', height=300, width=400, title='Correlation Histogram') dist_his = hv.Histogram( (dist_edges, dist_freq)).opts(xlabel='Distance', height=300, width=400, title='Distance Histogram') corr_img_wseg = (corr_img * seg_plot).opts( width=600, height=300, legend_position='right' ) # Overlay task segemnt plot with correlation matrix dist_img_wseg = (dist_img * seg_plot).opts( width=600, height=300, legend_position='right' ) # Overlay task segemnt plot with distance matrix dash = (corr_img_wseg + corr_his + dist_img_wseg + dist_his).opts( opts.Layout(shared_axes=False)).cols(2) # Dashboard of all plots return dash
def plot_grids(data,fields,plot_type): if len(data) == 2: ds,grid_ds = data if fields == 'Sources': print('Plotting Sources..') event_times_seconds = xr.DataArray(ds.event_time.values.astype('<m8[s]')/86400,dims='number_of_events') event_times_seconds.shape,ds.event_time.shape dss = ds.copy() dss['times'] = event_times_seconds.astype(int) xr_dataset = gv.Dataset(hv.Points((dss.event_longitude.values, dss.event_latitude.values,dss.times.values), kdims=[hv.Dimension('x'),hv.Dimension('y')],vdims=['time'])) fig = gv.tile_sources.Wikipedia.opts(width=900, height=900) * xr_dataset.to.points(['x','y']).opts(opts.Points(color='time')) else: print(plot_type) z = grid_ds.flash_extent_density x = grid_ds.grid_longitude.values y = grid_ds.grid_latitude.values # get xarray dataset, suited for handling raster data in pyviz xr_dataset = gv.Dataset(hv.Image((x, y, np.log10(z.mean(axis=0))), bounds=(x.min(),y.min(),x.max(),y.max()), kdims=[hv.Dimension('x'), hv.Dimension('y')], datatype=['grid'])) # create contours from image gv.FilledContours(xr_dataset) fig = gv.tile_sources.Wikipedia.opts(width=900, height=900) * xr_dataset.to.image(['x', 'y']).opts(cmap='cubehelix', alpha=0.8)#gv.FilledContours(xr_dataset).opts(cmap='viridis', alpha=0.5) responsive = pn.pane.HoloViews(fig, config={'responsive': True}) return(responsive) else: grid_ds = data print('No Data Processed') if fields == 'Sources': xr_dataset = gv.Dataset(hv.Points((np.repeat(-101.1,1), np.repeat(32,2),np.repeat(10,2)), kdims=[hv.Dimension('x'),hv.Dimension('y')],vdims=['time'])) fig = gv.tile_sources.Wikipedia.opts(width=900, height=900) * xr_dataset.to.points(['x','y']).opts(opts.Points(color='time')) elif plot_type == 'Grids': z = np.random.randn(10,(100,100)) x = np.repeat(-101.1,100) y = np.repeat(32,100) # get xarray dataset, suited for handling raster data in pyviz xr_dataset = gv.Dataset(hv.Image((x, y, np.log10(z)), bounds=(x.min(),y.min(),x.max(),y.max()), kdims=[hv.Dimension('x'), hv.Dimension('y')], datatype=['grid'])) # create contours from image gv.FilledContours(xr_dataset) fig = gv.tile_sources.Wikipedia.opts(width=900, height=900) * xr_dataset.to.image(['x', 'y']).opts(cmap='cubehelix', alpha=0.8)#gv.FilledContours(xr_dataset).opts(cmap='viridis', alpha=0.5) responsive = pn.pane.HoloViews(fig, config={'responsive': True}) return(responsive)
""" import numpy as np import holoviews as hv from bokeh.io import curdoc from bokeh.layouts import layout from bokeh.models import Slider, Button renderer = hv.renderer('bokeh') # Declare the HoloViews object start = 0 end = 10 hmap = hv.HoloMap( {i: hv.Image(np.random.rand(10, 10)) for i in range(start, end + 1)}) # Convert the HoloViews object into a plot plot = renderer.get_plot(hmap) def animate_update(): year = slider.value + 1 if year > end: year = start slider.value = year def slider_update(attrname, old, new): plot.update(slider.value)
def LoadAndCrop(video_dict, stretch={ 'width': 1, 'height': 1 }, cropmethod='none'): #if batch processing, set file to first file to be processed if 'file' not in video_dict.keys(): video_dict['file'] = video_dict['FileNames'][0] #Upoad file and check that it exists video_dict['fpath'] = os.path.join(os.path.normpath(video_dict['dpath']), video_dict['file']) if os.path.isfile(video_dict['fpath']): print('file: {file}'.format(file=video_dict['fpath'])) cap = cv2.VideoCapture(video_dict['fpath']) else: raise FileNotFoundError( '{file} not found. Check that directory and file names are correct' .format(file=video_dict['fpath'])) #Get maxiumum frame of file. Note that max frame is updated later if fewer frames detected cap_max = int(cap.get(7)) #7 is index of total frames print('total frames: {frames}'.format(frames=cap_max)) #Set first frame cap.set( 1, video_dict['start'] ) #first index references frame property, second specifies next frame to grab ret, frame = cap.read() frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) cap.release() print('dimensions: {x}'.format(x=frame.shape)) #Make first image reference frame on which cropping can be performed image = hv.Image( (np.arange(frame.shape[1]), np.arange(frame.shape[0]), frame)) image.opts(width=int(frame.shape[1] * stretch['width']), height=int(frame.shape[0] * stretch['height']), invert_yaxis=True, cmap='gray', colorbar=True, toolbar='below', title="First Frame. Crop if Desired") #Create polygon element on which to draw and connect via stream to poly drawing tool if cropmethod == 'none': image.opts(title="First Frame") return image, None, video_dict if cropmethod == 'Box': box = hv.Polygons([]) box.opts(alpha=.5) box_stream = streams.BoxEdit(source=box, num_objects=1) return (image * box), box_stream, video_dict if cropmethod == 'HLine': points = hv.Points([]) points.opts(active_tools=['point_draw'], color='white', size=1) pointerXY_stream = streams.PointerXY(x=0, y=0, source=image) pointDraw_stream = streams.PointDraw(source=points, num_objects=1) def h_track(x, y): #function to track pointer y = int(np.around(y)) text = hv.Text(x, y, str(y), halign='left', valign='bottom') return hv.HLine(y) * text track = hv.DynamicMap(h_track, streams=[pointerXY_stream]) def h_line(data): #function to draw line try: hline = hv.HLine(data['y'][0]) return hline except: hline = hv.HLine(0) return hline line = hv.DynamicMap(h_line, streams=[pointDraw_stream]) def h_text(data): #function to write ycrop value center = frame.shape[1] // 2 try: y = int(np.around(data['y'][0])) htext = hv.Text(center, y + 10, 'ycrop: {x}'.format(x=y)) return htext except: htext = hv.Text(center, 10, 'ycrop: 0') return htext text = hv.DynamicMap(h_text, streams=[pointDraw_stream]) return image * track * points * line * text, pointDraw_stream, video_dict
def waves_image(alpha, beta): return hv.Image(np.sin(((ys / alpha)**alpha + beta) * xs)) #.opts(plot={'width':640, 'height':480})
# generate symmetric matrix options_sym = copy.deepcopy(options) options_sym["Image"]['width'] = 490 options_sym["Image"]['height'] = 430 options_sym["Image"]['cmap'] = 'blues' #options_sym["Image"]['symmetric'] = True n = 40 np.random.seed(2) matrix = np.random.random((n, n)) # generates values between 0 and 1 matrix_tril = np.tril(matrix, 0) # extract lower triangular matrix matrix_triu = matrix_tril.T # transpose lower triangular matrix matrix_sym = matrix_tril + matrix_triu # create symmetric matrix, not that this doubles the values on the diagonal matrix_sym_viz = hv.Image(matrix_sym).options(options_sym) # symmetric matrices with noise np.random.seed(2) ## example 1, randomly change 100 entries matrix_sym_1 = matrix_sym + 0 n_values_to_change = 100 for i in range(n_values_to_change): matrix_sym_1[np.random.randint(0, n), np.random.randint(0, n)] += np.random.normal(0, 0.15) hv_matrix_sym_1 = hv.Image(matrix_sym_1).options(options).options( title_format='Example 1 (random replacements)') ## example 2, add a small amount of noise to all entries matrix_sym_2 = matrix_sym + np.random.normal(0, 0.02, size=(n, n))
def show_mask(self): ''' If xarray object has attribute M, shows the mask. :return: Mask as a hv.Image. You might want to use magic to see heatmap ''' return hv.Image(self._obj.M.mask_as_xarray(), vdims=['Value'])
# Each diget coresponds to 64 pixels, giving you an 8x8 picture. Each pixel value ranges from 0-16. # + idx = 8 # Index value for the number you wish to display (index determined by 'data_df') dig = pd.Series(data_df.loc[idx]).array # Turn pixel values for that diget into an array dig_df = pd.DataFrame() # Emptly data frame for diget image # Append every 8 pixels into 'dig_df' as a column x=0 for row in [1,2,3,4,5,6,7,8]: dig_df[row] = dig[x:x+7] x=x+8 # Display dig_df as an image to display diget hv.Image(dig_df.to_numpy().T).opts(cmap='Greys') # - # *** # ## Affinity Matrix Analysis # These scripts are desighned to demonstrate how the affinity matrix for the Laplacian Eigenmap is computed. To show this we use a simple example of 6 2D points and find the nearest neighbor affnitnity matrix with an n value of 2. In this example we use Sci-kit Learns function's sklearn.neighbors.NearestNeighbors(). from sklearn.neighbors import NearestNeighbors import numpy as np import holoviews as hv import plotly.express as px hv.extension('matplotlib') X = np.array([[-3, -2], [-2, -1], [-1, -1], [1, 1], [2, 1], [3, 2]]) # Example points hv.Points(X) # Plot the example points
def plot_colorbar(cmap, bounds, label=None, orientation='vertical', backend='matplotlib'): if label is None: label = '' cbar_img = np.linspace(0, 1, 256) vmin, vmax = bounds opts_kwargs = { 'cmap': cmap, 'show_frame': False, 'framewise': True, 'axiswise': True } if orientation == 'vertical': cbar_img = cbar_img[:, None] bounds = (0, vmin, 1, vmax) opts_kwargs['xaxis'] = None opts_kwargs['yaxis'] = 'right' opts_kwargs['ylabel'] = '' opts_kwargs['yticks'] = 5 elif orientation == 'horizontal': cbar_img = cbar_img[None] bounds = (vmin, 0, vmax, 1) opts_kwargs['xaxis'] = 'bottom' opts_kwargs['yaxis'] = None opts_kwargs['xlabel'] = '' opts_kwargs['xticks'] = 5 else: raise ValueError( 'orientation not recognized. expects vertical|horizontal, got {}'. format(orientation)) if backend == 'matplotlib': opts_kwargs['sublabel_size'] = 0 opts_kwargs['fontsize'] = {'title': 14} if orientation == 'vertical': opts_kwargs['aspect'] = 0.02 else: opts_kwargs['aspect'] = 1 / 0.02 elif backend == 'bokeh': opts_kwargs['fontsize'] = {'title': 8} opts_kwargs['toolbar'] = None if orientation == 'vertical': opts_kwargs['frame_width'] = 15 else: opts_kwargs['frame_height'] = 15 else: raise ValueError( 'backend not supported. expects matplotlib|bokeh, got {}'.format( backend)) return hv.Image(cbar_img, label=label, bounds=bounds).opts(**opts_kwargs)
def gem_raster(counts, use_datashader=True): if use_datashader: return datashade(hv.Image(counts.values)) else: return hv.Image(counts.values)
bokeh serve --show player.py """ import numpy as np import holoviews as hv from bokeh.io import curdoc from bokeh.layouts import layout from bokeh.models import Slider, Button renderer = hv.renderer('bokeh') # Declare the HoloViews object start = 0 end = 10 hmap = hv.HoloMap({i: hv.Image(np.random.rand(10,10)) for i in range(start, end+1)}) # Convert the HoloViews object into a plot plot = renderer.get_plot(hmap) def animate_update(): year = slider.value + 1 if year > end: year = start slider.value = year def slider_update(attrname, old, new): plot.update(slider.value) slider = Slider(start=start, end=end, value=0, step=1, title="Year") slider.on_change('value', slider_update)
from holoviews import streams, opts, dim # ## 2D plots with interactive slicing # ls = np.linspace(0, 10, 200) xx, yy = np.meshgrid(ls, ls) bounds = (0, 0, 10, 10) # Coordinate system: (left, bottom, right, top) energy = hv.Dimension('energy', label='E', unit='MeV') distance = hv.Dimension('distance', label='d', unit='m') charge = hv.Dimension('charge', label='Q', unit='pC') # + image = hv.Image(np.sin(xx) * np.cos(yy), bounds=bounds, kdims=[energy, distance], vdims=charge) pointer = streams.PointerXY(x=5, y=5, source=image) dmap = hv.DynamicMap(lambda x, y: hv.VLine(x) * hv.HLine(y), streams=[pointer]) x_sample = hv.DynamicMap( lambda x, y: image.sample(energy=x).opts(color='darkred'), streams=[pointer]) y_sample = hv.DynamicMap( lambda x, y: image.sample(distance=y).opts(color='lightsalmon'), streams=[pointer]) pointer_dmap = hv.DynamicMap(lambda x, y: hv.Points([(x, y)]), streams=[pointer]) pointer_x_sample = hv.DynamicMap(lambda x, y: hv.Points([(y, image[x, y])]), streams=[pointer])
def plot_frames(data, backend='matplotlib', mode='mosaic', rows=1, vmax=None, vmin=None, circle=None, circle_alpha=0.8, circle_color='white', circle_linestyle='-', circle_radius=6, circle_label=False, circle_label_color='white', arrow=None, arrow_alpha=0.8, arrow_length=10, arrow_shiftx=5, arrow_label=None, label=None, label_pad=5, label_size=12, label_color='white',grid=False, grid_alpha=0.4, grid_color='#f7f7f7', grid_spacing=None, cross=None, cross_alpha=0.4, lab_fontsize=8, cross_color='white', ang_scale=False, ang_ticksep=50, ndec=1, pxscale=0.01, auscale=1., ang_legend=False, au_legend=False, axis=True, show_center=False, cmap=None, log=False, colorbar=True, colorbar_ticks=None, dpi=100, size_factor=6, horsp=0.4, versp=0.2, width=400, height=400, title=None, tit_size=16, sampling=1, save=None, transparent=False): """ Plot a 2d array or a tuple of 2d arrays. Supports the ``matplotlib`` and ``bokeh`` backends. When having a tuple of 2d arrays, the plot turns into a mosaic. For ``matplotlib``, instead of a mosaic of images, we can create a mosaic of surface plots. Also, when using the ``matplotlib`` backend, this function allows to annotate and customize the plot and produce publication quality figures. Parameters ---------- data : numpy.ndarray or tuple A single 2d array or a tuple of 2d arrays. backend : {'matplotlib', 'bokeh'}, str optional Selects the backend used to display the plots. ``Matplotlib`` plots are static and allow customization (leading to publication quality figures). ``Bokeh`` plots are interactive, allowing the used to zoom, pan, inspect pixel values, etc. mode : {'mosaic', 'surface'}, str optional [backend='matplotlib'] Controls whether to turn the images into surface plots. rows : int, optional How many rows (subplots in a grid) in the case ``data`` is a tuple of 2d arrays. vmax : None, float/int or tuple of float/int, optional For defining the data range that the colormap covers. When set to None, the colormap covers the complete value range of the supplied data. vmin : None, float/int or tuple of float/int, optional For stretching the displayed pixels values. When set to None, the colormap covers the complete value range of the supplied data. circle : None, tuple or tuple of tuples, optional [backend='matplotlib'] To show a circle at the given px coordinates. The circles are shown on all subplots. circle_alpha : float or tuple of floats, optional [backend='matplotlib'] Alpha transparency for each circle. circle_color : str, optional [backend='matplotlib'] Color of the circles. White by default. circle_radius : int, optional [backend='matplotlib'] Radius of the circles, 6 px by default. circle_label : bool or string, optional [backend='matplotlib'] Whether to show the coordinates next to each circle. If a string: the string to be printed. If a tuple, should be a tuple of strings with same length as 'circle'. circle_label_color : str, optional [backend='matplotlib'] Default 'white'. Sets the color of the circle label arrow : None or tuple of floats, optional [backend='matplotlib'] To show an arrow pointing to the given pixel coordinates. arrow_alpha : float, optional [backend='matplotlib'] Alpha transparency for the arrow. arrow_length : int, optional [backend='matplotlib'] Length of the arrow, 10 px by default. arrow_shiftx : int, optional [backend='matplotlib'] Shift in x of the arrow pointing position, 5 px by default. arrow_label : bool or string, optional [backend='matplotlib'] Label to be printed next to the arrow. label : None, str or tuple of str/None, optional [backend='matplotlib'] Text for labeling each subplot. The label is shown at the bottom-left corner if each subplot. label_pad : int or tuple of int, optional [backend='matplotlib'] Padding of the label from the left bottom corner. 5 by default. If a tuple, sets the padding in x and y. label_size : int, optional [backend='matplotlib'] Size of the labels font. grid : bool or tuple of bools, optional [backend='matplotlib'] If True, a grid is displayed over the image, off by default. grid_alpha : None, float/int or tuple of None/float/int, optional [backend='matplotlib'] Alpha transparency of the grid. grid_color : str, optional [backend='matplotlib'] Color of the grid lines. grid_spacing : None, float/int or tuple of None/float/int, optional [backend='matplotlib'] Separation of the grid lines in pixels. cross : None or tuple of floats, optional [backend='matplotlib'] If provided, a crosshair is displayed at given pixel coordinates. cross_alpha : float, optional [backend='matplotlib'] Alpha transparency of the crosshair. cross_color : string, optional [backend='matplotlib'] Color of the crosshair. ang_scale : bool or tuple of bools, optional [backend='matplotlib'] If True, the axes are displayed in angular scale (arcsecs). ang_ticksep : int, optional [backend='matplotlib'] Separation for the ticks when using axis in angular scale. ndec : int, optional [backend='matplotlib'] Number of decimals for axes labels. pxscale : float, optional [backend='matplotlib'] Pixel scale in arcseconds/px. Default 0.01 (Keck/NIRC2, SPHERE-IRDIS). auscale : float, optional [backend='matplotlib'] Pixel scale in au/px. Default 1. ang_legend : bool or tuple of bools, optional [backend='matplotlib'] If True a scaling bar (1 arcsec or 500 mas) will be added on the bottom-right corner of the subplots. au_legend : bool or tuple of bools, optional [backend='matplotlib'] If True (and ang_legend is False) a scaling bar (10 au, 20 au or 50 au) will be added on the top-right corner of the subplots. axis : bool, optional [backend='matplotlib'] Show the axis, on by default. show_center : bool or tuple of bools, optional [backend='matplotlib'] To show a cross at the center of the frame. cmap : None, str or tuple of str, optional Colormap to be used. When None, the value of the global variable ``default_cmap`` will be used. Any string corresponding to a valid ``matplotlib`` colormap can be used. Additionally, 'ds9cool', 'ds9heat' and 'binary' (for binary maps) are valid colormaps for this function. log : bool or tuple of bool, optional [backend='matplotlib'] Log color scale. colorbar : bool or tuple of bool, optional To attach a colorbar, on by default. colorbar_ticks : None, tuple or tuple of tuples, optional [backend='matplotlib'] Custom ticks for the colorbar of each plot. dpi : int, optional [backend='matplotlib'] Dots per inch, determines how many pixels the figure comprises (which affects the plot quality). size_factor : int, optional [backend='matplotlib'] Determines the size of the plot by setting the figsize parameter (width x height [inches]) as size_factor * ncols, size_factor * nrows. horsp : float, optional [backend='matplotlib'] Horizontal gap between subplots. versp : float, optional [backend='matplotlib'] Vertical gap between subplots. width : int, optional [backend='bokeh'] Controls the width of each subplot. height : int, optional [backend='bokeh'] Controls the height of each subplot. title : None or str, optional [backend='matplotlib'] Title of the whole figure, None by default. tit_size: int, optional Size of the title font. sampling : int, optional [mode='surface'] Sets the stride used to sample the input data to generate the surface graph. save : None or str, optional If a string is provided the plot is saved using ``save`` as the path/filename. transparent : bool, optional [save=True] Whether to have a transparent background between subplots. If False, then a white background is shown. """ def check_bool_param(param, name): msg_type = '`' + name + '` must be a bool or tuple of bools' if isinstance(param, bool): param = [param] * num_plots elif isinstance(param, tuple): if not num_plots == len(param): msg = 'The len of `' + name + '` ({}) does not match the ' + \ 'number of plots ({})' raise ValueError(msg.format(len(param), num_plots)) else: for elem in param: if not isinstance(elem, bool): raise TypeError(msg_type) else: raise TypeError(msg_type) return param def check_numeric_param(param, name): msg_type = '`' + name + '` must be a None, float/int or tuple of ' + \ 'None/float/ints' if param is None: param = [None] * num_plots elif isinstance(param, (int, float)): param = [param] * num_plots elif isinstance(param, tuple): if not num_plots == len(param): msg = 'The len of `' + name + '` ({}) does not match the ' + \ 'number of plots ({})' raise ValueError(msg.format(len(param), num_plots)) else: for elem in param: if elem and not isinstance(elem, (float, int)): raise TypeError(msg_type) else: raise TypeError(msg_type) return param def check_str_param(param, name, default_value=None): msg_type = '`' + name + '` must be a None, str or tuple of ' + \ 'None/str' if param is None: param = [default_value] * num_plots elif isinstance(param, str): param = [param] * num_plots elif isinstance(param, tuple): if not num_plots == len(param): msg = 'The len of `' + name + '` ({}) does not match the ' + \ 'number of plots ({})' raise ValueError(msg.format(len(param), num_plots)) else: for elem in param: if elem and not isinstance(elem, str): raise TypeError(msg_type) else: raise TypeError(msg_type) return param # -------------------------------------------------------------------------- # Checking inputs: a frame (1 or 3 channels) or tuple of them msg_data_type = "`data` must be a frame (2d array) or tuple of frames" if isinstance(data, np.ndarray): if data.ndim == 2: data = [data] elif data.ndim == 3: raise TypeError(msg_data_type) elif isinstance(data, tuple): for i in range(len(data)): # checking the elements are 2d (excepting the case of 3 channels) if not data[i].ndim == 2:# and data[i].shape[2] != 3: raise ValueError(msg_data_type) else: raise ValueError(msg_data_type) if not isinstance(backend, str): raise TypeError('`backend` must be a string. ' + msg_data_type) if backend == 'bokeh': if mode == 'surface': raise ValueError('Surface plotting only supported with matplotlib ' 'backend') if save is not None: raise ValueError('Saving is only supported with matplotlib backend') if isinstance(label_pad, tuple): label_pad_x, label_pad_y = label_pad else: label_pad_x = label_pad label_pad_y = label_pad num_plots = len(data) if rows == 0: raise ValueError('Rows must be a positive integer') if num_plots % rows == 0: cols = int(num_plots / rows) else: cols = int((num_plots / rows) + 1) # CIRCLE ------------------------------------------------------------------- if circle is not None: if isinstance(circle, tuple): show_circle = True if isinstance(circle[0], tuple): n_circ = len(circle) coor_circle = circle elif isinstance(circle[0], (float, int)): n_circ = 1 coor_circle = [circle] * n_circ else: print("`circle` must be a tuple (X,Y) or tuple of tuples (X,Y)") show_circle = False else: show_circle = False if show_circle: if isinstance(circle_radius, (float, int)): # single value is provided, used for all circles circle_radius = [circle_radius] * n_circ elif isinstance(circle_radius, tuple): # a different value for each circle if not n_circ == len(circle_radius): msg = '`circle_radius` must have the same len as `circle`' raise ValueError(msg) else: raise TypeError("`circle_rad` must be a float or tuple of floats") if show_circle: if isinstance(circle_alpha, (float, int)): circle_alpha = [circle_alpha] * n_circ elif isinstance(circle_alpha, tuple): # a different value for each circle if not n_circ == len(circle_alpha): msg = '`circle_alpha` must have the same len as `circle`' raise ValueError(msg) # ARROW -------------------------------------------------------------------- if arrow is not None: if isinstance(arrow, tuple): show_arrow = True else: raise ValueError("`arrow` must be a tuple (X,Y)") else: show_arrow = False # VMAX-VMIN ---------------------------------------------------------------- vmin = check_numeric_param(vmin, 'vmin') vmax = check_numeric_param(vmax, 'vmax') # CROSS -------------------------------------------------------------------- if cross is not None: if not isinstance(cross, tuple): raise ValueError("`crosshair` must be a tuple (X,Y)") else: coor_cross = cross show_cross = True else: show_cross = False # AXIS, GRID, ANG_SCALE ---------------------------------------------------- axis = check_bool_param(axis, 'axis') grid = check_bool_param(grid, 'grid') grid_alpha = check_numeric_param(grid_alpha, 'grid_alpha') grid_spacing = check_numeric_param(grid_spacing, 'grid_spacing') show_center = check_bool_param(show_center, 'show_center') ang_scale = check_bool_param(ang_scale, 'ang_scale') ang_legend = check_bool_param(ang_legend, 'ang_legend') au_legend = check_bool_param(au_legend, 'au_legend') if isinstance(grid_color, str): grid_color = [grid_color] * num_plots if any(ang_scale) and save is not None: print("`Pixel scale set to {}`".format(pxscale)) # LABEL -------------------------------------------------------------------- label = check_str_param(label, 'label') # CMAP --------------------------------------------------------------------- custom_cmap = check_str_param(cmap, 'cmap', default_cmap) # COLORBAR ----------------------------------------------------------------- colorbar = check_bool_param(colorbar, 'colorbar') if colorbar_ticks is not None: cbar_ticks = colorbar_ticks # must be a tuple if isinstance(cbar_ticks, tuple): # tuple of tuples if isinstance(cbar_ticks[0], tuple): if not num_plots == len(cbar_ticks): raise ValueError('`colorbar_ticks` does not contain enough ' 'items') # single tuple elif isinstance(cbar_ticks[0], (float, int)): cbar_ticks = [colorbar_ticks] * num_plots else: raise TypeError('`colorbar_ticks` must be a tuple or tuple of ' 'tuples') else: cbar_ticks = [None] * num_plots # LOG ---------------------------------------------------------------------- logscale = check_bool_param(log, 'log') # if any(logscale): # # Showing bad/nan pixels with the darkest color in current colormap # current_cmap = mplcm.get_cmap() # current_cmap.set_bad(current_cmap.colors[0]) # -------------------------------------------------------------------------- if backend == 'matplotlib': # Creating the figure -------------------------------------------------- fig = figure(figsize=(cols * size_factor, rows * size_factor), dpi=dpi) if title is not None: fig.suptitle(title, fontsize=tit_size, va='center', x=0.51, #y=1-0.08*(28/tit_size)**(0.5)) y=1-0.1*(16/tit_size)) if mode == 'surface': plot_mosaic = False elif mode == 'mosaic': plot_mosaic = True else: raise ValueError("`mode` value was not recognized") for i, v in enumerate(range(num_plots)): image = data[i].copy() frame_size = image.shape[0] # assuming square frames cy = image.shape[0] / 2 - 0.5 cx = image.shape[1] / 2 - 0.5 v += 1 if plot_mosaic: ax = subplot(rows, cols, v) ax.set_aspect('auto') if logscale[i]: image += np.abs(np.nanmin(image)) if vmin[i] is None: linthresh = 1e-2 else: linthresh = vmin[i] norm = colors.SymLogNorm(linthresh, base=10) else: norm = None if image.dtype == bool: image = image.astype(int) if custom_cmap[i] == 'binary' and image.max() == 1 and \ image.min() == 0: cucmap = cmap_binary cbticks = (0, 0.5, 1) else: cucmap = custom_cmap[i] cbticks = cbar_ticks[i] im = ax.imshow(image, cmap=cucmap, origin='lower', norm=norm, interpolation='nearest', vmin=vmin[i], vmax=vmax[i]) # if colorbar[i]: # divider = make_axes_locatable(ax) # # the width of cax is 5% of ax and the padding between cax # # and ax wis fixed at 0.05 inch # cax = divider.append_axes("right", size="5%", pad=0.05) # cb = plt_colorbar(im, ax=ax, cax=cax, drawedges=False, # ticks=cbticks) # cb.outline.set_linewidth(0.1) # cb.ax.tick_params(labelsize=lab_fontsize) # cbw = frame_size/10 # else: # cbw = 0 else: # Leave the import to make porjection='3d' work #from mpl_toolkits.mplot3d import Axes3D x = np.outer(np.arange(0, frame_size, 1), np.ones(frame_size)) y = x.copy().T ax = subplot(rows, cols, v, projection='3d') ax.set_aspect('auto') surf = ax.plot_surface(x, y, image, rstride=sampling, cstride=sampling, linewidth=2, cmap=custom_cmap[i], antialiased=True, vmin=vmin[i], vmax=vmax[i]) ax.set_xlabel('x') ax.set_ylabel('y') ax.dist = 10 if title is not None: ax.set_title(title) if colorbar[i]: fig.colorbar(surf, aspect=10, pad=0.05, fraction=0.04) if ang_legend[i] and plot_mosaic: scaleng = 1. / pxscale scalab = '1 arcsec' scalabloc = scaleng / 2. - 8 if scaleng > frame_size / 2.: scaleng /= 2. scalab = '500 mas' scalabloc = scaleng / 2. - 8 scapad = 4 xma = frame_size - scapad xmi = xma - scaleng hlines(y=scapad, xmin=xmi, xmax=xma, colors='white', lw=1., linestyles='solid') annotate(scalab, (xmi + scalabloc, scapad + 2), color='white', size=label_size) if au_legend[i] and plot_mosaic: pxsc_fac = (0.012265/pxscale) labsz_fac = (label_size/12) scaleng = 50. / auscale scalab = '50 au' scalabloc = scaleng / 2. - 6.4*pxsc_fac*labsz_fac if scaleng > frame_size / 3.: scaleng = 20. / auscale scalab = '20 au' scalabloc = scaleng / 2. - 6.4*pxsc_fac*labsz_fac if scaleng > frame_size / 3.: scaleng = 10. / auscale scalab = '10 au' scalabloc = scaleng / 2. - 6.4*pxsc_fac*labsz_fac scapad = 5*pxsc_fac*labsz_fac xma = frame_size - scapad xmi = xma - scaleng hlines(y=xma-scapad, xmin=xmi, xmax=xma, colors='white', lw=1., linestyles='solid') annotate(scalab, (xmi + scalabloc, xma-0.5*scapad), color='white', size=label_size) if show_circle and plot_mosaic: if isinstance(circle_linestyle,tuple): c_offset = circle_linestyle[0] circle_linestyle = circle_linestyle[1] else: c_offset = lab_fontsize+1 # vertical offset is equal to the font size + 1, was 2 for j in range(n_circ): if isinstance(circle_color, (list, tuple)): circle_color_tmp = circle_color[j] else: circle_color_tmp = circle_color if isinstance(circle_linestyle, (list, tuple)): circle_linestyle_tmp = circle_linestyle[j] else: circle_linestyle_tmp = circle_linestyle circ = Circle(coor_circle[j], radius=circle_radius[j], fill=False, color=circle_color_tmp, alpha=circle_alpha[j], ls=circle_linestyle_tmp) ax.add_artist(circ) if circle_label: x = coor_circle[j][0] y = coor_circle[j][1] if isinstance(circle_label,str): cirlabel = circle_label elif isinstance(circle_label,tuple): cirlabel = circle_label[j] else: cirlabel = str(int(x))+','+str(int(y)) ax.text(x, y + circle_radius[j] + c_offset, cirlabel, fontsize=lab_fontsize, color=circle_label_color, family='monospace', ha='center', va='center', weight='bold', alpha=circle_alpha[j]) if show_cross and plot_mosaic: ax.axhline(coor_cross[0], xmin=0, xmax=frame_size, alpha=cross_alpha, lw=0.6, linestyle='dashed', color='white') ax.axvline(coor_cross[1], ymin=0, ymax=frame_size, alpha=cross_alpha, lw=0.6, linestyle='dashed', color='white') if show_center[i] and plot_mosaic: ax.scatter([cy], [cx], marker='+', color=cross_color, alpha=cross_alpha) if show_arrow and plot_mosaic: ax.arrow(arrow[0] + arrow_length + arrow_shiftx, arrow[1], -arrow_length, 0, color='white', head_width=6, head_length=4, width=2, length_includes_head=True, alpha=arrow_alpha) if arrow_label: x = arrow[0] y = arrow[1] if isinstance(arrow_label,str): arrlabel = arrow_label else: arrlabel = str(int(x))+','+str(int(y)) if len(arrlabel) < 5: arr_fontsize=14 else: arr_fontsize=lab_fontsize ax.text(x + arrow_length + 1.3*arrow_shiftx, y, arrlabel, fontsize=arr_fontsize, color='white', family='monospace', ha='left', va='center', weight='bold', alpha=arrow_alpha) if label[i] is not None and plot_mosaic: ax.annotate(label[i], xy=(label_pad_x, label_pad_y), color=label_color, xycoords='axes pixels', weight='bold', size=label_size) if grid[i] and plot_mosaic: if grid_spacing[i] is None: if cy < 10: gridspa = 1 elif cy >= 10: if cy % 2 == 0: gridspa = 4 else: gridspa = 5 else: gridspa = grid_spacing[i] ax.tick_params(axis='both', which='minor') minor_ticks = np.arange(0, data[i].shape[0], gridspa) ax.set_xticks(minor_ticks, minor=True) ax.set_yticks(minor_ticks, minor=True) ax.grid(True, which='minor', color=grid_color[i], linewidth=0.5, alpha=grid_alpha[i], linestyle='dashed') else: ax.grid(False) if ang_scale[i] and plot_mosaic: # Converting axes from pixels to arcseconds half_num_ticks = int(np.round(cy // ang_ticksep)) # Calculate the pixel locations at which to put ticks ticks = [] for t in range(half_num_ticks, -half_num_ticks-1, -1): # Avoid ticks not showing on the last pixel if not cy - t * ang_ticksep == frame_size: ticks.append(cy - t * ang_ticksep) else: ticks.append((cy - t * ang_ticksep)-1) ax.set_xticks(ticks) ax.set_yticks(ticks) # Corresponding distance in arcseconds, measured from the center labels_y = [] labels_x = [] for t in range(half_num_ticks, -half_num_ticks-1, -1): labels_y.append(round(Decimal(-t * (ang_ticksep * pxscale)),ndec)) labels_x.append(round(Decimal(t * (ang_ticksep * pxscale)),ndec)) ax.set_xticklabels(labels_x) ax.set_yticklabels(labels_y) ax.set_xlabel('\u0394RA["]', fontsize=label_size) ax.set_ylabel('\u0394Dec["]', fontsize=label_size) ax.tick_params(axis='both', which='major', labelsize=label_size) else: ax.set_xlabel("x", fontsize=label_size) ax.set_ylabel("y", fontsize=label_size) if not axis[i]: ax.set_axis_off() if colorbar[i]: divider = make_axes_locatable(ax) # the width of cax is 5% of ax and the padding between cax # and ax wis fixed at 0.05 inch cax = divider.append_axes("right", size="5%", pad=0.05) cb = plt_colorbar(im, ax=ax, cax=cax, drawedges=False, ticks=cbticks) cb.outline.set_linewidth(0.1) cb.ax.tick_params(labelsize=lab_fontsize) fig.subplots_adjust(wspace=horsp, hspace=versp) if save is not None and isinstance(save, str): savefig(save, dpi=dpi, bbox_inches='tight', pad_inches=0, transparent=transparent) close() else: show() elif backend == 'bokeh': hv.extension(backend) subplots = [] # options = "Image (cmap='" + custom_cmap[0] + "')" # taking first item # hv.opts(options) for i, v in enumerate(range(num_plots)): image = data[i].copy() if vmin[i] is None: vmin[i] = image.min() if vmax[i] is None: vmax[i] = image.max() im = hv.Image((range(image.shape[1]), range(image.shape[0]), image)) subplots.append(im.opts(tools=['hover'], colorbar=colorbar[i], colorbar_opts={'width': 15}, width=width, height=height, clim=(vmin[i], vmax[i]), cmap=custom_cmap[0])) return hv.Layout(subplots).cols(cols) else: raise ValueError('`backend` not supported')
output_group = pn.widgets.RadioButtonGroup(options=[HEXCODE, RGB_255, RGB_1], margin=(15, 10, 5, 10)) num_slider = pn.widgets.IntSlider(name='Number of colors', start=2, end=255, step=1, value=1, margin=(10, 15)) data = np.load('tmp_ds.npy')[::-1] plot_da = xr.DataArray(data, name='tmp', dims=('y', 'x')) hv_plot = hv.Image(plot_da, ['x', 'y'], ['tmp']).opts( responsive=True, toolbar=None, colorbar=True, default_tools=[], colorbar_opts={'background_fill_color': WHITE_SMOKE}, cmap=DEFAULT_CMAP, xaxis=None, yaxis=None, hooks=[remove_white_borders], aspect='equal') plot_pane = pn.pane.HoloViews(min_height=300, max_height=500, object=hv_plot, sizing_mode='scale_both', align='center', margin=(0, 3)) code_markdown = pn.pane.Markdown(EXAMPLE_CODE.format(colors=''), sizing_mode=STR_WIDTH, margin=(0, 15, 0, 15))
def generate_spectrum_from_RDC(filename, numFrames=500, numADCSamples=128, numTxAntennas=3, numRxAntennas=4, numLoopsPerFrame=128, numAngleBins=64, chirpPeriod=0.06, logGabor=False, accumulate=True, save_full=False): numChirpsPerFrame = numTxAntennas * numLoopsPerFrame # ============================================================================= # numADCSamples = number of range bins # numLoopsPerFrame = number of doppler bins # ============================================================================= range_resolution, bandwidth = dsp.range_resolution(numADCSamples) doppler_resolution = dsp.doppler_resolution(bandwidth) if filename[-4:] != '.bin': filename += '.bin' adc_data = np.fromfile(filename, dtype=np.int16) adc_data = adc_data.reshape(numFrames, -1) adc_data = np.apply_along_axis(DCA1000.organize, 1, adc_data, num_chirps=numChirpsPerFrame, num_rx=numRxAntennas, num_samples=numADCSamples) print("Data Loaded!") dataCube = adc_data micro_doppler_data = np.zeros((numFrames, numLoopsPerFrame, numADCSamples), dtype=np.float64) theta_data = np.zeros((numFrames, numLoopsPerFrame, numTxAntennas * numRxAntennas, numADCSamples), dtype=np.complex) for i, frame in enumerate(dataCube): # (2) Range Processing from mmwave.dsp.utils import Window radar_cube = dsp.range_processing(frame, window_type_1d=Window.BLACKMAN) assert radar_cube.shape == ( numChirpsPerFrame, numRxAntennas, numADCSamples), "[ERROR] Radar cube is not the correct shape!" # (3) Doppler Processing det_matrix, theta_data[i] = dsp.doppler_processing( radar_cube, num_tx_antennas=3, clutter_removal_enabled=True, window_type_2d=Window.HAMMING) # --- Shifts & Store det_matrix_vis = np.fft.fftshift(det_matrix, axes=1) micro_doppler_data[i, :, :] = det_matrix_vis # Data should now be ready. Needs to be in micro_doppler_data, a 3D-numpy array with shape [numDoppler, numRanges, numFrames] # LOG GABOR if logGabor: if accumulate: image = micro_doppler_data.sum(axis=1).T else: image = micro_doppler_data.T from LogGabor import LogGabor import holoviews as hv lg = LogGabor("default_param.py") lg.set_size(image) lg.pe.datapath = 'database/' image = lg.normalize(image, center=True) # display input image # hv.Image(image) # display log gabor'd image image = lg.whitening(image) * lg.mask hv.Image(image) uDoppler = image elif accumulate: uDoppler = micro_doppler_data.sum(axis=1).T else: uDoppler = micro_doppler_data.T if save_full: return range_resolution, doppler_resolution, uDoppler, theta_data else: return range_resolution, doppler_resolution, uDoppler
import numpy as np import holoviews as hv import holoviews.plotting.mpl print(hv.__version__) r = hv.renderer('bokeh') curve = hv.Curve(range(10)) img = hv.Image(np.random.rand(10, 10)) _ = r.save([curve, img, curve + img], plot.html)
def Batch_Process_select_reference(video_dict, tracking_params, reference_image, bin_dict, region_names, stretch, crop, poly_stream=None): #get polygon if poly_stream != None: lst = [] for poly in range(len(poly_stream.data['xs'])): x = np.array(poly_stream.data['xs'][poly]) #x coordinates y = np.array(poly_stream.data['ys'][poly]) #y coordinates lst.append([(x[vert], y[vert]) for vert in range(len(x))]) poly = hv.Polygons(lst).opts(fill_alpha=0.1, line_dash='dashed') heatmaps = [] for file in video_dict['FileNames']: print('Processing File: {f}'.format(f=file)) video_dict[ 'file'] = file #used both to set the path and to store filenames when saving video_dict['fpath'] = os.path.join( os.path.normpath(video_dict['dpath']), file) reference = reference_image location = TrackLocation(video_dict, tracking_params, reference, crop) if region_names != None: location = ROI_Location(reference, poly_stream, region_names, location) location.to_csv( os.path.splitext(video_dict['fpath'])[0] + '_LocationOutput.csv') file_summary = Summarize_Location(location, video_dict, bin_dict=bin_dict, region_names=region_names) try: #Add summary info for individual file to larger summary of all files summary_all = pd.concat([summary_all, file_summary]) except NameError: #to be done for first file in list, before summary_all is created summary_all = file_summary #Plot Heat Map image = hv.Image( (np.arange(reference.shape[1]), np.arange(reference.shape[0]), reference)).opts(width=int(reference.shape[1] * stretch['width']), height=int(reference.shape[0] * stretch['height']), invert_yaxis=True, cmap='gray', toolbar='below', title=file + ": Motion Trace") points = hv.Scatter(np.array([location['X'], location['Y']]).T).opts(color='navy', alpha=.2) heatmaps.append(image * poly * points) if poly_stream != None else heatmaps.append( image * points) #Write summary data to csv file sum_pathout = os.path.join(os.path.normpath(video_dict['dpath']), 'BatchSummary.csv') summary_all.to_csv(sum_pathout) layout = hv.Layout(heatmaps) return layout
def update(pattern, counter, x, y): if x and y: pattern = np.array(shapes[pattern]) r, c = pattern.shape y, x = img.sheet2matrixidx(x, y) img.data[y:y + r, x:x + c] = pattern[::-1] else: img.data = step(img.data) return hv.Image(img) # Set up plot which advances on counter and adds pattern on tap title = 'Game of Life - Tap to place pattern, Doubletap to clear' img = hv.Image(np.zeros((100, 200), dtype=np.uint8)) counter, tap = Counter(transient=True), Tap(transient=True), pattern_dim = hv.Dimension('Pattern', values=sorted(shapes.keys())) dmap = hv.DynamicMap(update, kdims=[pattern_dim], streams=[counter, tap]) plot = dmap.opts( opts.Image(cmap='gray', clim=(0, 1), toolbar=None, responsive=True, min_height=800, title=title, xaxis=None, yaxis=None))
# styling options cmap = 'blues' scale_factor = 0.73 # to scale images options = {'Image': dict(cmap = cmap, colorbar = True, colorbar_opts = {'padding':12}, height = int(380*scale_factor), width = int(440*scale_factor), border = 10, xaxis = None, tools=['hover'], yaxis = None, toolbar='above')} opts_layout = {'Layout': dict(shared_axes=False, normalize=False)} # visualize standard matrices ## generate identity matrix n = 25 # set up matrix size unit_matrix = np.identity(n) hv_unit_matrix = hv.Image(unit_matrix).options(options) ## generate band matrices ### create function that returns banded matrix def band_matrix_fct(n,value_diag,band_a): band_matrix = np.zeros((n,n)) band_matrix[np.arange(n-1), np.arange(n-1)+1] = np.repeat(band_a,n-1) band_matrix[np.arange(n), np.arange(n)] = np.repeat(value_diag,n) band_matrix[np.arange(n), np.arange(n)-1] = np.repeat(band_a,n) band_matrix[n-1,0] = band_a return band_matrix ### styling options options_divergence = copy.deepcopy(options) options_divergence['Image']['cmap'] = 'RdBu'
def distance_matrix(SBJ, RUN, WL_sec, x_dim, y_dim, z_dim): """ This fuction plots a heat map of the distnaces of each window for a given run. The inputs for the fuction (subject, run, and window leght) allows the user to choose what run and window leghth they with to plot for a given subject. The distance between two windows (i.e. points on the 3D plot) is computed using numpys squareform(pdist()). The fuction plots the heat map using holoviews hv.Image(). The x and y axes of the plot are the two windows in which you are finding the distance. The z value is that distance. A plot of the sleep staging segments are ploted along the x and y axis of the image using hv.Segments(). If all runs are being displayed a plot of the run segments are ploted along the x and y axis of the image using hv.Segments(). """ LE3D_df = load_data(SBJ, RUN, WL_sec) # Load embedding data LE3D_df = LE3D_df.infer_objects( ) # Infer objects to be int, float, or string apropriatly LE3D_df = LE3D_df[[ 'x' + str(x_dim).zfill(2), 'x' + str(y_dim).zfill(2), 'x' + str(z_dim).zfill(2), 'x' + str(x_dim).zfill(2) + '_norm', 'x' + str(y_dim).zfill(2) + '_norm', 'x' + str(z_dim).zfill(2) + '_norm', 'Run', 'Sleep Value', 'Sleep Stage', 'mean FD', 'label' ]] LE3D_df.columns = [ 'x', 'y', 'z', 'x_norm', 'y_norm', 'z_norm', 'Run', 'Sleep Value', 'Sleep Stage', 'mean FD', 'label' ] data_path = osp.join(PRJDIR, 'PrcsData', SBJ, 'D02_Preproc_fMRI', SBJ + '_' + RUN + '_WL_' + str(WL_sec) + 'sec_Sleep_Segments.pkl') # path to segment data sleep_segments_df = pd.read_pickle(data_path) # Load segment data data_df = LE3D_df[[ 'x_norm', 'y_norm', 'z_norm', 'Sleep Stage' ]].copy() # New data frame of only x_norm, y_norm, and z_norm values num_win = data_df.shape[0] # Number of windwos in data data_array = data_df[['x_norm', 'y_norm', 'z_norm']].to_numpy() # Data as a numpy array dist_array = squareform(pdist( data_array, 'euclidean')) # Calculate distance matrix and rehape into one vecotr dist_array = xr.DataArray(dist_array, dims=['Time [Window ID]', 'Time [Window ID] Y' ]) # Distances as x_array data frame sleep_color_map = { 'Wake': 'orange', 'Stage 1': 'yellow', 'Stage 2': 'green', 'Stage 3': 'blue', 'Undetermined': 'gray' } # Color key for sleep staging # Plot of sleep staging segements along the x and y axis # Range is from (-10, num_win) so we have space to display segments sleep_seg_x = hv.Segments(sleep_segments_df, [ hv.Dimension('start', range=(-10, num_win)), hv.Dimension('start_event', range=(-5, num_win)), 'end', 'end_event' ], 'stage').opts(color='stage', cmap=sleep_color_map, line_width=10, show_legend=False) sleep_seg_y = hv.Segments(sleep_segments_df, [ hv.Dimension('start_event', range=(-10, num_win)), hv.Dimension('start', range=(-5, num_win)), 'end_event', 'end' ], 'stage').opts(color='stage', cmap=sleep_color_map, line_width=10, show_legend=False) # If plotting all runs add segent to x and y axis for coloring by run if RUN == 'All': run_list = [ SubDict[SBJ][i][0] for i in range(0, len(SubDict[SBJ]) - 1) ] # List of all runs time_list = [ SubDict[SBJ][i][1] for i in range(0, len(SubDict[SBJ]) - 1) ] # List of all run lenghts in TR's (in the same order as runs in list above) WL_trs = int(WL_sec / 2) # Window length in TR's (TR = 2.0 sec) run_segments_df = pd.DataFrame( columns=['run', 'start', 'end']) # Emptly data frame for segment legths of runs # For each run a row is appended into the data frame created above (run_segments_df) with the run name and the start and end window of the run # For the windows that overlap runs the run will be called 'Inbetween Runs' x = 0 # Starting at 0th window for i in range(len(run_list)): time = time_list[i] # Number of windows in run run = run_list[i] # Name of run end = x + time - WL_trs # Last window of run if i == len( run_list ) - 1: # If its the last run no need to append inbetween run run_segments_df = run_segments_df.append( { 'run': run, 'start': x, 'end': end }, ignore_index=True) # Append run info else: run_segments_df = run_segments_df.append( { 'run': run, 'start': x, 'end': end }, ignore_index=True) # Append run info x = end + 1 run_segments_df = run_segments_df.append( { 'run': 'Inbetween Runs', 'start': x, 'end': (x - 1) + (WL_trs - 1) }, ignore_index=True) # Append inbetween run info x = x + (WL_trs - 1) # Add 0.5 to each end of segment to span entire heat map run_segments_df['start'] = run_segments_df['start'] - 0.5 run_segments_df['end'] = run_segments_df['end'] + 0.5 # 'start_event' and 'end_event' represent the axis along which the segments will be (-50 so it is not on top of the heat map or sleep segments) run_segments_df['start_event'] = -50 run_segments_df['end_event'] = -50 # Color key for runs run_color_map = { 'SleepAscending': '#DE3163', 'SleepDescending': '#FF7F50', 'SleepRSER': '#FFBF00', 'WakeAscending': '#6495ED', 'WakeDescending': '#40E0D0', 'WakeRSER': '#CCCCFF', 'Inbetween Runs': 'gray' } # Plot of run segements along the x and y axis # Range is from (-80, num_win) so we have space to display both segments run_seg_x = hv.Segments(run_segments_df, [ hv.Dimension('start', range=(-80, num_win)), hv.Dimension('start_event', range=(-80, num_win)), 'end', 'end_event' ], 'run').opts(color='run', cmap=run_color_map, line_width=10, show_legend=False) run_seg_y = hv.Segments(run_segments_df, [ hv.Dimension('start_event', range=(-80, num_win)), hv.Dimension('start', range=(-80, num_win)), 'end_event', 'end' ], 'run').opts(color='run', cmap=run_color_map, line_width=10, show_legend=False) segment_plot = ( sleep_seg_x * sleep_seg_y * run_seg_x * run_seg_y).opts( xlabel=' ', ylabel=' ', show_legend=False) # All segments (run and sleep) overlayed else: segment_plot = (sleep_seg_x * sleep_seg_y).opts( xlabel=' ', ylabel=' ', show_legend=False) # All segments (not including runs)overlayed # Plot heat map using hv.Image # Set bounds to (-0.5,-0.5,num_win-0.5,num_win-0.5) to corespond with acurate windows plot = hv.Image(dist_array, bounds=(-0.5, -0.5, num_win - 0.5, num_win - 0.5)).opts(cmap='jet', colorbar=True, ylabel='Time [Window ID]') # Overlay segment plots and heat map output = (plot * segment_plot).opts(width=600, height=390) return output
def view(self): if self.lsd is None: return panel.pane.Markdown("No data selected.") try: if self.intercylinder_only: name = "ringmap_intercyl" else: name = "ringmap" container = self.data.load_file(self.revision, self.lsd, name) except DataError as err: return panel.pane.Markdown( f"Error: {str(err)}. Please report this problem." ) # Index map for ra (x-axis) index_map_ra = container.index_map["ra"] axis_name_ra = "RA [degrees]" # Index map for sin(ZA)/sin(theta) (y-axis) index_map_el = container.index_map["el"] axis_name_el = "sin(\u03B8)" # Apply data selections sel_beam = np.where(container.index_map["beam"] == self.beam)[0] sel_freq = np.where( [f[0] for f in container.index_map["freq"]] == self.frequency )[0] if self.polarization == self.mean_pol_text: sel_pol = np.where( (container.index_map["pol"] == "XX") | (container.index_map["pol"] == "YY") )[0] rmap = np.squeeze(container.map[sel_beam, sel_pol, sel_freq]) rmap = np.nanmean(rmap, axis=0) else: sel_pol = np.where(container.index_map["pol"] == self.polarization)[0] rmap = np.squeeze(container.map[sel_beam, sel_pol, sel_freq]) if self.flag_mask: rmap = np.where(self._flags_mask(container.index_map["ra"]), np.nan, rmap) if self.weight_mask: try: rms = np.squeeze(container.rms[sel_pol, sel_freq]) except IndexError: logger.error( f"rms dataset of ringmap file for rev {self.revision} lsd " f"{self.lsd} is missing [{sel_pol}, {sel_freq}] (polarization, " f"frequency). rms has shape {container.rms.shape}" ) self.weight_mask = False else: rmap = np.where(self._weights_mask(rms), np.nan, rmap) # Set flagged data to nan rmap = np.where(rmap == 0, np.nan, rmap) if self.crosstalk_removal: # The mean of an all-nan slice (masked?) is nan. We don't need a warning about that. with warnings.catch_warnings(): warnings.filterwarnings("ignore", r"All-NaN slice encountered") rmap -= np.nanmedian(rmap, axis=0) if self.template_subtraction: try: rm_stack = self.data.load_file_from_path( self._stack_path, ccontainers.RingMap ) except DataError as err: return panel.pane.Markdown( f"Error: {str(err)}. Please report this problem." ) # The stack file has all polarizations, so we can't reuse sel_pol if self.polarization == self.mean_pol_text: stack_sel_pol = np.where( (rm_stack.index_map["pol"] == "XX") | (rm_stack.index_map["pol"] == "YY") )[0] else: stack_sel_pol = np.where( rm_stack.index_map["pol"] == self.polarization )[0] try: rm_stack = np.squeeze(rm_stack.map[sel_beam, stack_sel_pol, sel_freq]) except IndexError as err: logger.error( f"map dataset of ringmap stack file " f"is missing [{sel_beam}, {stack_sel_pol}, {sel_freq}] (beam, polarization, " f"frequency). map has shape {rm_stack.map.shape}:\n{err}" ) self.template_subtraction = False else: if self.polarization == self.mean_pol_text: rm_stack = np.nanmean(rm_stack, axis=0) # FIXME: this is a hack. remove when rinmap stack file fixed. rmap -= rm_stack.reshape(rm_stack.shape[0], -1, 2).mean(axis=-1) if self.transpose: rmap = rmap.T index_x = index_map_ra index_y = index_map_el axis_names = [axis_name_ra, axis_name_el] xlim, ylim = self.ylim, self.xlim else: index_x = index_map_el index_y = index_map_ra axis_names = [axis_name_el, axis_name_ra] xlim, ylim = self.xlim, self.ylim img = hv.Image( (index_x, index_y, rmap), datatype=["image", "grid"], kdims=axis_names, ).opts( clim=self.colormap_range, logz=self.logarithmic_colorscale, cmap=process_cmap("inferno", provider="matplotlib"), colorbar=True, xlim=xlim, ylim=ylim, ) if self.serverside_rendering is not None: # set colormap cmap_inferno = copy.copy(matplotlib_cm.get_cmap("inferno")) cmap_inferno.set_under("black") cmap_inferno.set_bad("lightgray") # Set z-axis normalization (other possible values are 'eq_hist', 'cbrt'). if self.logarithmic_colorscale: normalization = "log" else: normalization = "linear" # datashade/rasterize the image img = self.serverside_rendering( img, cmap=cmap_inferno, precompute=True, x_range=xlim, y_range=ylim, normalization=normalization, ) if self.mark_moon: # Put a ring around the location of the moon if it transits on this day eph = skyfield_wrapper.ephemeris # Start and end times of the CSD st = csd_to_unix(self.lsd.lsd) et = csd_to_unix(self.lsd.lsd + 1) moon_time, moon_dec = chime.transit_times( eph["moon"], st, et, return_dec=True ) if len(moon_time): lunar_transit = unix_to_csd(moon_time[0]) lunar_dec = moon_dec[0] lunar_ra = (lunar_transit % 1) * 360.0 lunar_za = np.sin(np.radians(lunar_dec - 49.0)) if self.transpose: img *= hv.Ellipse(lunar_ra, lunar_za, (5.5, 0.15)) else: img *= hv.Ellipse(lunar_za, lunar_ra, (0.04, 21)) if self.mark_day_time: # Calculate the sun rise/set times on this sidereal day # Start and end times of the CSD start_time = csd_to_unix(self.lsd.lsd) end_time = csd_to_unix(self.lsd.lsd + 1) times, rises = chime.rise_set_times( skyfield_wrapper.ephemeris["sun"], start_time, end_time, diameter=-10, ) sun_rise = 0 sun_set = 0 for t, r in zip(times, rises): if r: sun_rise = (unix_to_csd(t) % 1) * 360 else: sun_set = (unix_to_csd(t) % 1) * 360 # Highlight the day time data opts = { "color": "grey", "alpha": 0.5, "line_width": 1, "line_color": "black", "line_dash": "dashed", } if self.transpose: if sun_rise < sun_set: img *= hv.VSpan(sun_rise, sun_set).opts(**opts) else: img *= hv.VSpan(self.ylim[0], sun_set).opts(**opts) img *= hv.VSpan(sun_rise, self.ylim[1]).opts(**opts) else: if sun_rise < sun_set: img *= hv.HSpan(sun_rise, sun_set).opts(**opts) else: img *= hv.HSpan(self.ylim[0], sun_set).opts(**opts) img *= hv.HSpan(sun_rise, self.ylim[1]).opts(**opts) img.opts( # Fix height, but make width responsive height=self.height, responsive=True, shared_axes=True, bgcolor="lightgray", ) return panel.Row(img, width_policy="max")
def datashade_surfaces(xarray_list, name_list, domain_list=None, wells=None, wells_label='Wells', cols=1, plot_size=(750, 400)): ''' Plot several surfaces with dynamic datashader xarray_list: list of xarrays to plot name_list: list of str domain_list: list of int to choose colormaps (0: depth, 1: time) wells: optional pandas.DataFrame of welltops, added to all plots wells_label: optional str to label the well locations cols: number of columns plot_size: Tuple of int: The individual size of every subplot ''' import holoviews as hv from holoviews.operation.datashader import datashade from bokeh.palettes import viridis, inferno, RdBu, Spectral from bokeh.models import NumeralTickFormatter #, WheelZoomTool hv.extension('bokeh') # styling HoloView plots def axes_formatter(plot, element): plot.handles['xaxis'].formatter = NumeralTickFormatter(format='7') plot.handles['yaxis'].formatter = NumeralTickFormatter(format='7') # plot.state.toolbar.active_scroll = plot.state.tools[2] # plot.handles['active_scroll'] = 'wheel_zoom' # plot.state.toolbar.active_scroll = WheelZoomTool() # domain specific cmaps cmaps = [viridis(256), inferno(256)] if domain_list is None: domain_list = [0] * len(xarray_list) # to modify axes, modify in any case the RGB instances of HoloView with the plot arguments! opts = { 'RGB': { 'style': dict(), 'plot': dict(width=plot_size[0], height=plot_size[1], aspect='square', show_grid=True, colorbar=True, finalize_hooks=[axes_formatter]) }, 'Points': { 'style': dict(color='black', marker='*', size=10), 'plot': dict(tools=['hover']) } } plot_list = [] for i, x in enumerate(xarray_list): image = hv.Image(x, ['X', 'Y'], 'Z') plot = (datashade(image, cmap=cmaps[domain_list[i]])) if wells is not None: points = hv.Points(wells, ['X', 'Y'], vdims=['Z', 'well', 'formation'], label=wells_label) plot = plot * points plot = plot.relabel(name_list[i]) # maybe for holoviews 1.11 # plot = plot.options(active_tools=['wheel_zoom']) plot_list.append(plot) layout = hv.Layout(plot_list).cols(cols).opts(opts) return layout
n_est = 100 # number of estimates for each a_est and b_est a_est_min = 0.7 a_est_max = 1.3 b_est_min = -4.0 b_est_max = -2.0 a_est_vector = np.linspace(a_est_min,a_est_max,n_est) b_est_vector = np.linspace(b_est_min,b_est_max,n_est) # create matrix with squared error depending on a_est and b_est in the goal to draw contour lines matrix = np.zeros((n_est,n_est)) for i in range(n_est): for j in range(n_est): matrix[i,j] = predictions(a_est_vector[j], b_est_vector[-i-1])[2] # these are the sum of squares bounds = (a_est_min,b_est_min,a_est_max,b_est_max) # bounds for the coming plot a_est_grid, b_est_grid = np.meshgrid(a_est_vector, b_est_vector) # create input grid hv_img = hv.Image(a_est_grid + b_est_grid, ['a estimate','b estimate'], bounds = bounds) # initialize plot; hv.Image content is added in next line hv_img.data = matrix # overwrite data with data we want contour_levels=50 hv_contours = hv_img * hv.operation.contours(hv_img, levels=contour_levels) # set up dimensions for kdims for coming dynamic maps dim_a_est = hv.Dimension('a_est', range=(a_est_min, a_est_max), default=1) dim_b_est = hv.Dimension('b_est', range=(b_est_min, b_est_max), default=-3.0) # dynamic maps dmap_coords_a_b_est = hv.DynamicMap(hv_point_est, kdims=[dim_a_est,dim_b_est]) dmap_squares = hv.DynamicMap(hv_squares, kdims=[dim_a_est,dim_b_est]) dmap_line_est = hv.DynamicMap(hv_line_est, kdims=[dim_a_est,dim_b_est]) ls_layout = (dmap_squares * dmap_line_est * hv_points.options(marker='o', size=5)).redim.range(x=(-0.5,12.5), y=(-3,10)) # plot styling options
lats = np.linspace(latlngbox_num[0], latlngbox_num[2], num=150) lons = np.linspace(latlngbox_num[1], latlngbox_num[3], num=151) meshgrid_shape = (lats.size, lons.size) xi, yi = np.meshgrid(lons, lats) xi, yi = xi.ravel(), yi.ravel() df = df.dropna() x, y = df.Lon.values, df.Lat.values z = df.PM2_5.values zi = simple_idw(x, y, z, xi, yi) zi = zi.reshape(meshgrid_shape) interpolated = hv.Image(zi[::-1, :]) interpolated.opts(colorbar=True, alpha=0.7,cmap='magma', tools=['hover'], width=500, height=400) ds = xr.DataArray(zi, dims=['Lat', 'Lon'], coords={'Lat': lats, 'Lon': lons}).to_dataset(name='PM2_5') aqi_ds = gv.Dataset(ds, ['Lon', 'Lat'], 'PM2_5') background = gvts.OSM * gv.Image(aqi_ds).opts(alpha=0.7, width=500, height=400, colorbar=True, cmap='magma') #contour = gvts.CartoEco * aqi_ds.to(gv.FilledContours, # ['Lon', 'Lat']).opts(alpha=0.5, width=500, height=400, # colorbar=True, cmap='magma', levels=10, color_levels=10, # tools=['hover'])
def rs_fMRI_plots(SBJ, RUN, WL_sec, corr_range): # Load rs fMRI data # ----------------- file_name = SBJ + '_fanaticor_Craddock_T2Level_0200_wl' + str( WL_sec).zfill( 3) + 's_ws002s_' + RUN + '_PCA_vk97.5.swcorr.pkl' # Data file name data_path = osp.join('/data/SFIM_Vigilance/PRJ_Vigilance_Smk02/PrcsData', SBJ, 'D02_Preproc_fMRI', file_name) # Path to data data_df = pd.read_pickle(data_path).T # Read data into pandas data frame num_samp = data_df.shape[0] # Save number of samples as a varable # Load sleep segmenting data # -------------------------- seg_path = osp.join(PRJDIR, 'Data', 'Samika_DSet02', 'Sleep_Segments', SBJ + '_' + RUN + '_WL_' + str(WL_sec) + 'sec_Sleep_Segments.pkl') # Path to segment data seg_df = pd.read_pickle(seg_path) # Load segment data # Compute correlation and distance matrix # --------------------------------------- data_corr = np.corrcoef(data_df) # Correlation matrix data_dist = pairwise_distances(data_df, metric='euclidean') # Distance matrix # Compute distribution of correlation and distance matrix # ------------------------------------------------------- triangle = np.mask_indices(num_samp, np.triu, k=1) # Top triangle mask for matricies corr_freq, corr_edges = np.histogram( np.array(data_corr)[triangle], 100 ) # Compute histogram of top triangle of correlation matrix (100 bars) dist_freq, dist_edges = np.histogram( np.array(data_dist)[triangle], 100) # Compute histogram of top triangle of distance matrix (100 bars) # Create sleep segments plots # --------------------------- sleep_color_map = { 'Wake': 'orange', 'Stage 1': 'yellow', 'Stage 2': 'green', 'Stage 3': 'blue', 'Undetermined': 'gray' } # Color key for sleep staging seg_x = hv.Segments(seg_df, [ hv.Dimension('start', range=(-10, num_samp - 1.5)), hv.Dimension('start_event', range=(-5, num_samp - 1.5)), 'end', 'end_event' ], 'stage').opts(color='stage', cmap=sleep_color_map, line_width=7, show_legend=True) # x axis segments seg_y = hv.Segments(seg_df, [ hv.Dimension('start_event', range=(-10, num_samp - 1.5)), hv.Dimension('start', range=(-5, num_samp - 1.5)), 'end_event', 'end' ], 'stage').opts(color='stage', cmap=sleep_color_map, line_width=7, show_legend=False) # y axis segments seg_plot = (seg_x * seg_y).opts(xlabel=' ', ylabel=' ', show_legend=False) # All segments # Create matrix and histogram plots # --------------------------------- # raterize() fucntion used for big data set corr_img = rasterize( hv.Image(np.rot90(data_corr), bounds=(-0.5, -0.5, num_samp - 1.5, num_samp - 1.5)).opts( cmap='viridis', colorbar=True, title='Correlation Matrix')).redim.range(z=corr_range) dist_img = rasterize( hv.Image(np.rot90(data_dist), bounds=(-0.5, -0.5, num_samp - 1.5, num_samp - 1.5)).opts(cmap='viridis', colorbar=True, title='Distance Matrix')) corr_his = rasterize( hv.Histogram( (corr_edges, corr_freq)).opts(xlabel='Correlation', height=300, width=400, title='Correlation Histogram')) dist_his = rasterize( hv.Histogram((dist_edges, dist_freq)).opts(xlabel='Distance', height=300, width=400, title='Distance Histogram')) corr_img_wseg = (corr_img * seg_plot).opts( width=600, height=300, legend_position='right' ) # Overlay sleep segemnt plot with correlation matrix dist_img_wseg = (dist_img * seg_plot).opts( width=600, height=300, legend_position='right' ) # Overlay sleep segemnt plot with distance matrix dash = (corr_img_wseg + corr_his + dist_img_wseg + dist_his).opts( opts.Layout(shared_axes=False)).cols(2) # Dashboard of all plots return dash
scatter = hv.Scatter(station_info, 'services', 'ridership') scatter # %% layout = scatter + hv.Histogram(np.histogram(station_info['opened'], bins=24), kdims=['opened']) layout # %% taxi_dropoffs = { hour: arr for hour, arr in np.load( '../holoviews-examples/assets/hourly_taxi_data.npz').items() } print('Hours: {hours}'.format(hours=', '.join(taxi_dropoffs.keys()))) print( 'Taxi data contains {num} arrays (one per hour).\nDescription of the first array:\n' .format(num=len(taxi_dropoffs))) np.info(taxi_dropoffs['0']) # %% bounds = (-74.05, 40.70, -73.90, 40.80) image = hv.Image(taxi_dropoffs['0'], ['lon', 'lat'], bounds=bounds) image # %% points = hv.Points(station_info, ['lon', 'lat']) image + image * points # %%
def get_fractal(x_range, y_range): (x0, x1), (y0, y1) = x_range, y_range image = np.zeros((600, 600), dtype=np.uint8) return hv.Image(create_fractal(x0, x1, -y1, -y0, image, 200), bounds=(x0, y0, x1, y1))
# print("Datashaded..") num_genes, num_samples = df.shape print("To Dask Array..") da = dask_df.to_dask_array(True).persist() import pdb pdb.set_trace() print("To Image..") x_size = 1000 y_size = 1000 img = hv.Image((np.arange(num_samples), np.arange(num_genes), da)) rasterized_img = rasterize(img, width=x_size, height=y_size) rasterized_img.opts(width=x_size, height=y_size, cmap='viridis', logz=True) import pdb pdb.set_trace() # You have two options, bokeh requires selenium and phantomjs for png export, if you have those you can do # hv.save(hv_obj, 'test.png') # or you could use the matplotlib backend using # hv.save(hv_obj, 'test.png', backend='matplotlib') hv.save(rasterized_img, "rasterized" + str(time.time()) + ".png") print("Wrote output..")