def runTest(self): a = plotting.get_display(True) assert a != None a = plotting.get_display(False) assert a == None a = plotting.get_display(1234) assert a == 1234
def plot(self, id_list=None, v_thresh=None, display=True, kwargs={}): """ Plot all cells in the AnalogSignalList defined by id_list Inputs: id_list - can be a integer (and then N cells are randomly selected) or a list of ids. If None, we use all the ids of the SpikeList display - if True, a new figure is created. Could also be a subplot kwargs - dictionary contening extra parameters that will be sent to the plot function Examples: >> z = subplot(221) >> aslist.plot(5, display=z, kwargs={'color':'r'}) """ subplot = get_display(display) id_list = self._AnalogSignalList__sub_id_list(id_list) time_axis = self.time_axis() if not subplot or not HAVE_PYLAB: print PYLAB_ERROR else: xlabel = "Time (ms)" ylabel = "Conductance (nS)" set_labels(subplot, xlabel, ylabel) for id in id_list: subplot.plot(time_axis, self.analog_signals[id].signal, **kwargs) subplot.hold(1) pylab.draw()
def runTest(self): f = plotting.get_display(True) x = range(10) pylab.plot(x) plotting.set_axis_limits(pylab, 0., 123., -123., 456.) # set up a SimpleMultiplot with arbitrary values self.nrows = 1 self.ncolumns = 1 title = 'testMultiplot' xlabel = 'testXlabel' ylabel = 'testYlabel' scaling = ('linear','log') self.smt = plotting.SimpleMultiplot(nrows=self.nrows, ncolumns=self.ncolumns, title=title, xlabel=xlabel, ylabel=ylabel, scaling=scaling) plotting.set_axis_limits(self.smt.panel(0), 0., 123., -123., 456.)
def plot(self, ylabel="Analog Signal", display=True, kwargs={}): """ Plot the AnalogSignal Inputs: ylabel - A string to sepcify the label on the yaxis. display - if True, a new figure is created. Could also be a subplot kwargs - dictionary contening extra parameters that will be sent to the plot function Examples: >> z = subplot(221) >> signal.plot(ylabel="Vm", display=z, kwargs={'color':'r'}) """ subplot = get_display(display) time_axis = self.time_axis() if not subplot or not HAVE_PYLAB: print PYLAB_ERROR else: xlabel = "Time (ms)" set_labels(subplot, xlabel, ylabel) subplot.plot(time_axis, self.signal, **kwargs) pylab.draw()
def plot(self, id_list=None, v_thresh=None, display=True, kwargs={}): """ Plot all cells in the AnalogSignalList defined by id_list Inputs: id_list - can be a integer (and then N cells are randomly selected) or a list of ids. If None, we use all the ids of the SpikeList v_thresh- For graphical purpose, plot a spike when Vm > V_thresh. If None, just plot the raw Vm display - if True, a new figure is created. Could also be a subplot kwargs - dictionary contening extra parameters that will be sent to the plot function Examples: >> z = subplot(221) >> aslist.plot(5, v_thresh = -50, display=z, kwargs={'color':'r'}) """ subplot = get_display(display) id_list = self._AnalogSignalList__sub_id_list(id_list) time_axis = self.time_axis() if not subplot or not HAVE_MATPLOTLIB: print MATPLOTLIB_ERROR else: xlabel = "Time (ms)" ylabel = "Membrane Potential (mV)" set_labels(subplot, xlabel, ylabel) for id in id_list: to_be_plot = self.analog_signals[id].signal if v_thresh is not None: to_be_plot = pylab.where(to_be_plot>=v_thresh-0.02, v_thresh+0.5, to_be_plot) if len(time_axis) > len(to_be_plot): time_axis = time_axis[:-1] if len(to_be_plot) > len(time_axis): to_be_plot = to_be_plot[:-1] subplot.plot(time_axis, to_be_plot, **kwargs) subplot.hold(1)
def event_triggered_average(self, eventdict, events_ids = None, analogsignal_ids = None, average = True, t_min = 0, t_max = 100, ylim = None, display = False, mode = 'same', kwargs={}): """ Returns the event triggered averages of the analog signals inside the list. The events can be a SpikeList object or a dict containing times. The average is performed on a time window t_spikes - tmin, t_spikes + tmax Can return either the averaged waveform (average = True), or an array of all the waveforms triggered by all the spikes. Inputs: events - Can be a SpikeList object (and events will be the spikes) or just a dict of times average - If True, return a single vector of the averaged waveform. If False, return an array of all the waveforms. mode - 'same': the average is only done on same ids --> return {'eventids':average}; 'all': for all ids in the eventdict the average from all ananlog signals is returned --> return {'eventids':{'analogsignal_ids':average}} t_min - Time (>0) to average the signal before an event, in ms (default 0) t_max - Time (>0) to average the signal after an event, in ms (default 100) events_ids - when given only perform average over these ids analogsignal_ids = when given only perform average on these ids display - if True, a new figure is created for each average. Could also be a subplot. ylim - ylim of the plot kwargs - dictionary contening extra parameters that will be sent to the plot function Examples >> vmlist.event_triggered_average(spikelist, average=False, t_min = 50, t_max = 150, mode = 'same') >> vmlist.event_triggered_average(spikelist, average=True, mode = 'all') >> vmlist.event_triggered_average({'1':[200,300,'3':[234,788]]}, average=False, display=True) """ if isinstance(eventdict, SpikeList): eventdict = eventdict.spiketrains figure = get_display(display) subplotcount = 1 if events_ids is None: events_ids = eventdict.keys() if analogsignal_ids is None: analogsignal_ids = self.analog_signals.keys() x = numpy.ceil(numpy.sqrt(len(analogsignal_ids))) y = x results = {} first_done = False for id in events_ids: events = eventdict[id] if len(events) <= 0: continue if mode is 'same': if self.analog_signals.has_key(id) and id in analogsignal_ids: sp = pylab.subplot(x,y,subplotcount) results[id] = self.analog_signals[id].event_triggered_average(events,average=average,t_min=t_min,t_max=t_max,display=sp,kwargs=kwargs) pylab.ylim(ylim) pylab.title('Event: %g; Signal: %g'%(id,id)) subplotcount += 1 elif mode is 'all': if first_done: figure = get_display(display) first_done = True subplotcount_all = 1 results[id] = {} for id_analog in analogsignal_ids: analog_signal = self.analog_signals[id_analog] sp = pylab.subplot(x,y,subplotcount_all) results[id][id_analog] = analog_signal.event_triggered_average(events,average=average,t_min=t_min,t_max=t_max,display=sp,kwargs=kwargs) pylab.ylim(ylim) pylab.title('Event: %g; Signal: %g'%(id,id_analog)) subplotcount_all += 1 if not figure or not HAVE_PYLAB: return results
def event_triggered_average(self, events, average = True, t_min = 0, t_max = 100, display = False, with_time = False, kwargs={}): """ Return the spike triggered averaged of an analog signal according to selected events, on a time window t_spikes - tmin, t_spikes + tmax Can return either the averaged waveform (average = True), or an array of all the waveforms triggered by all the spikes. Inputs: events - Can be a SpikeTrain object (and events will be the spikes) or just a list of times average - If True, return a single vector of the averaged waveform. If False, return an array of all the waveforms. t_min - Time (>0) to average the signal before an event, in ms (default 0) t_max - Time (>0) to average the signal after an event, in ms (default 100) display - if True, a new figure is created. Could also be a subplot. kwargs - dictionary contening extra parameters that will be sent to the plot function Examples: >> vm.event_triggered_average(spktrain, average=False, t_min = 50, t_max = 150) >> vm.event_triggered_average(spktrain, average=True) >> vm.event_triggered_average(range(0,1000,10), average=False, display=True) """ if isinstance(events, SpikeTrain): events = events.spike_times ylabel = "Spike Triggered Average" else: assert numpy.iterable(events), "events should be a SpikeTrain object or an iterable object" ylabel = "Event Triggered Average" assert (t_min >= 0) and (t_max >= 0), "t_min and t_max should be greater than 0" assert len(events) > 0, "events should not be empty and should contained at least one element" time_axis = numpy.linspace(-t_min, t_max, (t_min+t_max)/self.dt) N = len(time_axis) Nspikes = 0. subplot = get_display(display) if average: result = numpy.zeros(N, float) else: result = [] # recalculate everything into timesteps, is more stable against rounding errors # and subsequent cutouts with different sizes events = numpy.floor(numpy.array(events)/self.dt) t_min_l = numpy.floor(t_min/self.dt) t_max_l = numpy.floor(t_max/self.dt) t_start = numpy.floor(self.t_start/self.dt) t_stop = numpy.floor(self.t_stop/self.dt) for spike in events: if ((spike-t_min_l) >= t_start) and ((spike+t_max_l) < t_stop): spike = spike - t_start if average: result += self.signal[(spike-t_min_l):(spike+t_max_l)] else: result.append(self.signal[(spike-t_min_l):(spike+t_max_l)]) Nspikes += 1 if average: result = result/Nspikes else: result = numpy.array(result) if not subplot or not HAVE_PYLAB: if with_time: return result, time_axis else: return result else: xlabel = "Time (ms)" set_labels(subplot, xlabel, ylabel) if average: subplot.plot(time_axis, result, **kwargs) else: for idx in xrange(len(result)): subplot.plot(time_axis, result[idx,:], c='0.5', **kwargs) subplot.hold(1) result = numpy.sum(result, axis=0)/Nspikes subplot.plot(time_axis, result, c='k', **kwargs) xmin, xmax, ymin, ymax = subplot.axis() subplot.plot([0,0],[ymin, ymax], c='r') set_axis_limits(subplot, -t_min, t_max, ymin, ymax) pylab.draw()
def crosscorrelate(sua1, sua2, lag=None, n_pred=1, predictor=None, display=False, kwargs={}): """Cross-correlation between two series of discrete events (e.g. spikes). Calculates the cross-correlation between two vectors containing event times. Returns ``(differeces, pred, norm)``. See below for details. Adapted from original script written by Martin P. Nawrot for the FIND MATLAB toolbox [1]_. Parameters ---------- sua1, sua2 : 1D row or column `ndarray` or `SpikeTrain` Event times. If sua2 == sua1, the result is the autocorrelogram. lag : float Lag for which relative event timing is considered with a max difference of +/- lag. A default lag is computed from the inter-event interval of the longer of the two sua arrays. n_pred : int Number of surrogate compilations for the predictor. This influences the total length of the predictor output array predictor : {None, 'shuffle'} Determines the type of bootstrap predictor to be used. 'shuffle' shuffles interevent intervals of the longer input array and calculates relative differences with the shorter input array. `n_pred` determines the number of repeated shufflings, resulting differences are pooled from all repeated shufflings. display : boolean If True the corresponding plots will be displayed. If False, int, int_ and norm will be returned. kwargs : dict Arguments to be passed to np.histogram. Returns ------- differences : np array Accumulated differences of events in `sua1` minus the events in `sua2`. Thus positive values relate to events of `sua2` that lead events of `sua1`. Units are the same as the input arrays. pred : np array Accumulated differences based on the prediction method. The length of `pred` is ``n_pred * length(differences)``. Units are the same as the input arrays. norm : float Normalization factor used to scale the bin heights in `differences` and `pred`. ``differences/norm`` and ``pred/norm`` correspond to the linear correlation coefficient. Examples -------- >> crosscorrelate(np_array1, np_array2) >> crosscorrelate(spike_train1, spike_train2) >> crosscorrelate(spike_train1, spike_train2, lag = 150.0) >> crosscorrelate(spike_train1, spike_train2, display=True, kwargs={'bins':100}) See also -------- ccf .. [1] Meier R, Egert U, Aertsen A, Nawrot MP, "FIND - a unified framework for neural data analysis"; Neural Netw. 2008 Oct; 21(8):1085-93. """ assert predictor is 'shuffle' or predictor is None, "predictor must be \ either None or 'shuffle'. Other predictors are not yet implemented." #Check whether sua1 and sua2 are SpikeTrains or arrays sua = [] for x in (sua1, sua2): #if isinstance(x, SpikeTrain): if hasattr(x, 'spike_times'): sua.append(x.spike_times) elif x.ndim == 1: sua.append(x) elif x.ndim == 2 and (x.shape[0] == 1 or x.shape[1] == 1): sua.append(x.ravel()) else: raise TypeError("sua1 and sua2 must be either instances of the" \ "SpikeTrain class or column/row vectors") sua1 = sua[0] sua2 = sua[1] if sua1.size < sua2.size: if lag is None: lag = np.ceil(10*np.mean(np.diff(sua1))) reverse = False else: if lag is None: lag = np.ceil(20*np.mean(np.diff(sua2))) sua1, sua2 = sua2, sua1 reverse = True #construct predictor if predictor is 'shuffle': isi = np.diff(sua2) sua2_ = np.array([]) for ni in xrange(1,n_pred+1): idx = np.random.permutation(isi.size-1) sua2_ = np.append(sua2_, np.add(np.insert( (np.cumsum(isi[idx])), 0, 0), sua2.min() + ( np.random.exponential(isi.mean())))) #calculate cross differences in spike times differences = np.array([]) pred = np.array([]) for k in xrange(0, sua1.size): differences = np.append(differences, sua1[k] - sua2[np.nonzero( (sua2 > sua1[k] - lag) & (sua2 < sua1[k] + lag))]) if predictor == 'shuffle': for k in xrange(0, sua1.size): pred = np.append(pred, sua1[k] - sua2_[np.nonzero( (sua2_ > sua1[k] - lag) & (sua2_ < sua1[k] + lag))]) if reverse is True: differences = -differences pred = -pred norm = np.sqrt(sua1.size * sua2.size) # Plot the results if display=True if display: subplot = get_display(display) if not subplot or not HAVE_PYLAB: return differences, pred, norm else: # Plot the cross-correlation try: counts, bin_edges = np.histogram(differences, **kwargs) edge_distances = np.diff(bin_edges) bin_centers = bin_edges[1:] - edge_distances/2 counts = counts / norm xlabel = "Time" ylabel = "Cross-correlation coefficient" #NOTE: the x axis corresponds to the upper edge of each bin subplot.plot(bin_centers, counts, label='cross-correlation', color='b') if predictor is None: set_labels(subplot, xlabel, ylabel) pylab.draw() elif predictor is 'shuffle': # Plot the predictor norm_ = norm * n_pred counts_, bin_edges_ = np.histogram(pred, **kwargs) counts_ = counts_ / norm_ subplot.plot(bin_edges_[1:], counts_, label='predictor') subplot.legend() pylab.draw() except ValueError: print "There are no correlated events within the selected lag"\ " window of %s" % lag else: return differences, pred, norm
def crosscorrelate(sua1, sua2, lag=None, n_pred=1, predictor=None, display=False, kwargs={}): """Cross-correlation between two series of discrete events (e.g. spikes). Calculates the cross-correlation between two vectors containing event times. Returns ``(differeces, pred, norm)``. See below for details. Adapted from original script written by Martin P. Nawrot for the FIND MATLAB toolbox [1]_. Parameters ---------- sua1, sua2 : 1D row or column `ndarray` or `SpikeTrain` Event times. If sua2 == sua1, the result is the autocorrelogram. lag : float Lag for which relative event timing is considered with a max difference of +/- lag. A default lag is computed from the inter-event interval of the longer of the two sua arrays. n_pred : int Number of surrogate compilations for the predictor. This influences the total length of the predictor output array predictor : {None, 'shuffle'} Determines the type of bootstrap predictor to be used. 'shuffle' shuffles interevent intervals of the longer input array and calculates relative differences with the shorter input array. `n_pred` determines the number of repeated shufflings, resulting differences are pooled from all repeated shufflings. display : boolean If True the corresponding plots will be displayed. If False, int, int_ and norm will be returned. kwargs : dict Arguments to be passed to np.histogram. Returns ------- differences : np array Accumulated differences of events in `sua1` minus the events in `sua2`. Thus positive values relate to events of `sua2` that lead events of `sua1`. Units are the same as the input arrays. pred : np array Accumulated differences based on the prediction method. The length of `pred` is ``n_pred * length(differences)``. Units are the same as the input arrays. norm : float Normalization factor used to scale the bin heights in `differences` and `pred`. ``differences/norm`` and ``pred/norm`` correspond to the linear correlation coefficient. Examples -------- >> crosscorrelate(np_array1, np_array2) >> crosscorrelate(spike_train1, spike_train2) >> crosscorrelate(spike_train1, spike_train2, lag = 150.0) >> crosscorrelate(spike_train1, spike_train2, display=True, kwargs={'bins':100}) See also -------- ccf .. [1] Meier R, Egert U, Aertsen A, Nawrot MP, "FIND - a unified framework for neural data analysis"; Neural Netw. 2008 Oct; 21(8):1085-93. """ assert predictor is 'shuffle' or predictor is None, "predictor must be \ either None or 'shuffle'. Other predictors are not yet implemented." #Check whether sua1 and sua2 are SpikeTrains or arrays sua = [] for x in (sua1, sua2): #if isinstance(x, SpikeTrain): if hasattr(x, 'spike_times'): sua.append(x.spike_times) elif x.ndim == 1: sua.append(x) elif x.ndim == 2 and (x.shape[0] == 1 or x.shape[1] == 1): sua.append(x.ravel()) else: raise TypeError("sua1 and sua2 must be either instances of the" \ "SpikeTrain class or column/row vectors") sua1 = sua[0] sua2 = sua[1] if sua1.size < sua2.size: if lag is None: lag = np.ceil(10 * np.mean(np.diff(sua1))) reverse = False else: if lag is None: lag = np.ceil(20 * np.mean(np.diff(sua2))) sua1, sua2 = sua2, sua1 reverse = True #construct predictor if predictor is 'shuffle': isi = np.diff(sua2) sua2_ = np.array([]) for ni in xrange(1, n_pred + 1): idx = np.random.permutation(isi.size - 1) sua2_ = np.append( sua2_, np.add(np.insert((np.cumsum(isi[idx])), 0, 0), sua2.min() + (np.random.exponential(isi.mean())))) #calculate cross differences in spike times differences = np.array([]) pred = np.array([]) for k in xrange(0, sua1.size): differences = np.append( differences, sua1[k] - sua2[np.nonzero((sua2 > sua1[k] - lag) & (sua2 < sua1[k] + lag))]) if predictor == 'shuffle': for k in xrange(0, sua1.size): pred = np.append( pred, sua1[k] - sua2_[np.nonzero((sua2_ > sua1[k] - lag) & (sua2_ < sua1[k] + lag))]) if reverse is True: differences = -differences pred = -pred norm = np.sqrt(sua1.size * sua2.size) # Plot the results if display=True if display: subplot = get_display(display) if not subplot or not HAVE_PYLAB: return differences, pred, norm else: # Plot the cross-correlation try: counts, bin_edges = np.histogram(differences, **kwargs) edge_distances = np.diff(bin_edges) bin_centers = bin_edges[1:] - edge_distances / 2 counts = counts / norm xlabel = "Time" ylabel = "Cross-correlation coefficient" #NOTE: the x axis corresponds to the upper edge of each bin subplot.plot(bin_centers, counts, label='cross-correlation', color='b') if predictor is None: set_labels(subplot, xlabel, ylabel) pylab.draw() elif predictor is 'shuffle': # Plot the predictor norm_ = norm * n_pred counts_, bin_edges_ = np.histogram(pred, **kwargs) counts_ = counts_ / norm_ subplot.plot(bin_edges_[1:], counts_, label='predictor') subplot.legend() pylab.draw() except ValueError: print "There are no correlated events within the selected lag"\ " window of %s" % lag else: return differences, pred, norm