#!/usr/bin/env python # coding: utf-8 # # Plotting routines in `touchsim` # The `touchsim` package uses `holoviews` for plotting. # In[ ]: import touchsim as ts from touchsim.plotting import plot, figsave import numpy as np import holoviews as hv hv.notebook_extension() get_ipython().run_line_magic('output', "holomap='scrubber' # animate holomaps") import warnings warnings.simplefilter("ignore") # ## Hand model # In[ ]: plot() # short for plot(ts.hand_surface) # Regions labels and the coordinate system can be overlaid on the plot. # In[ ]: get_ipython().run_cell_magic('output', 'size=250', 'plot(tags=True,coord=10) # coord sets the lengths of the coordinate axes in mm\n') # ## Visualising `AfferentPopulation` objects # In[ ]: a = ts.affpop_hand(region='D2d') plot(a,size=10) # Plots can be overlaid using the `*` operator. # # Plots are `holoviews` objects and can be indexed to only show, say, a specific afferent population. The `*` operator overlays different plots (e.g. the hand outline and the afferent locations, as shown below). # In[ ]: plot(region='D2d') * plot(a,size=10)['PC'] # New subpanels can be added using the `+` operator. # In[ ]: (plot(region='D2d') * plot(a,size=10)['SA1']) + (plot(region='D2d') * plot(a,size=10)['RA']) + (plot(region='D2d') * plot(a,size=10)['PC']) # The first index of an `AfferentPopulation` plot object is the afferent type, while the next two indices are pixel coordinates. # In[ ]: # zoom into fingertip plot(a,size=10)[:,120:140,450:475] # ## Visualising `Stimulus` objects # Plotting a `Stimulus` object shows the trace of all pins by default. # In[ ]: s = ts.stim_ramp(len=0.25,amp=.1,ramp_len=0.05) plot(s) # In[ ]: s += ts.stim_sine(freq=25.,len=.25,loc=[1.,1.]) plot(s) # Pin traces can also be shown in a grid view. # In[ ]: plot(s,grid=True) # Pin positions can also be shown spatially. # In[ ]: s = ts.stim_indent_shape(ts.shape_circle(hdiff=0.5,pins_per_mm=2,radius=3),ts.stim_ramp(len=0.1)) plot(s,spatial=True) # The spatial view can be animated with pin depths indicated by color. # In[ ]: plot(region='D2d') * plot(s,spatial=True,bin=10) # ## Visualising `Response` objects # Plotting a `Response` object shows the spike trains of all included neurons # In[ ]: a = ts.affpop_hand(region='D2') s = ts.stim_sine(freq=50.,amp=0.1,len=0.5) r = a.response(s) plot(r) # The second index is the time index. # In[ ]: plot(r)[:,0:0.2] # Responses can also be plotted spatially, in which case the size of each dot scaled with the neuron's firing rate. # In[ ]: get_ipython().run_cell_magic('output', 'size=150 # increase size of plot by 150%', "plot(region='D2') * plot(r,spatial=True,scaling_factor=.1)\n") # Alternatively, firing rate can be indicated by color instead: # In[ ]: plot(region='D2') * plot(r,spatial=True,scale=False)[:,'RA'] + plot(region='D2') * plot(r,spatial=True,scale=False)[:,'PC'] # Responses in spatial view can also be shown animated if a bin size (in ms) is given. # In[ ]: plot(region='D2') * plot(r,spatial=True,bin=10) # ### Advanced example (might be slow to compute) # In[ ]: contact_locs = np.zeros((2,2)) contact_locs[0] = np.array([0.,0.]) contact_locs[1] = np.array([150.,0]) a = ts.affpop_hand(noisy=False) s = ts.stim_indent_shape(contact_locs,ts.stim_ramp(amp=0.75,len=.2,ramp_len=0.05,ramp_type='lin',pin_radius=5.,pad_len=0.025)) r = a.response(s) plot(r) # In[ ]: plot() * plot(r,spatial=True,bin=10) # ## Saving figures and animations # Figures can be saved using the `figsave` function; figure size and resolution can be controlled using the `size` and `dpi` parameters, respectively. # In[ ]: figsave(plot(),'test_fig',size=150,dpi=50) # When the `gif` format is selected, animations will be saved as animated gifs; their framerate can be controlled with the `fps` parameter # In[ ]: bin = 50 fig = plot(s,bin=bin) + plot()*plot(r,spatial=True,bin=bin) figsave(fig,'test_gif',size=150,fps=5,fmt='gif')