Ejemplo n.º 1
0
 def wrapPlotsOverEdges(self):
     """Wrap all plots at the edges of the specified curve (should
     be a stimulus or a spiketrain."""
     if not self.__selectedCurves:
         return
     wrapcurve = self.__selectedCurves[-1]
     path = self.curve_path_dict[wrapcurve]
     times = []
     xdata = numpy.array(wrapcurve.data().xData())
     ydata = numpy.array(wrapcurve.data().yData())
     # It is a spike train, x values are spike times, wrap around those
     if 'spikes' in path:
         times = xdata
     # It is a stimulus: take the leadin edges
     elif 'stim' in path:
         times = xdata[numpy.r_[False, numpy.diff(ydata) < 0].nonzero()[0]]
     else:
         ydata = analyzer.smooth(ydata)
         mid = numpy.mean(ydata)
         ydata = ydata[ydata > mid] # Threshold at midpoint
         times = xdata[numpy.r_[True, ydata[1:] > ydata[:-1]] & numpy.r_[ydata[:-1] > ydata[1:], True]]
     # start from the first edge, ignoring everything before it
     # and put end of simulation as the upper bound
     for curve in self.itemList():
         ydata = numpy.array(curve.data().yData())
         xdata = numpy.array(curve.data().xData())            
         path = self.curve_path_dict[curve]
         path_curve_list = self.path_curve_dict[path]
         path_curve_list.pop(path_curve_list.index(curve))
         self.curve_path_dict.pop(curve)
         curve.detach()
         start = 0
         end = len(xdata)
         for ii in range(-1, - len(times) - 1, -1):
             points = numpy.nonzero(xdata >= times[ii])[0]
             if len(points) == 0:
                 continue
             start = points[0]
             xx = numpy.array(xdata[start:end] - times[ii])
             xdata[start:end] = -1.0
             new_curve = Qwt.QwtPlotCurve('%s #%d' % (curve.title().text(), len(times) + ii, ))
             new_curve.setData(xx, ydata[start:end])
             new_curve.setStyle(curve.style())
             new_curve.setPen(QtGui.QPen(curve.pen()))
             new_curve.setSymbol(Qwt.QwtSymbol(curve.symbol()))
             new_curve.attach(self)
             self.curve_path_dict[new_curve] = path
             self.path_curve_dict[path].append(new_curve)
             end = start                    
     self.replot()
Ejemplo n.º 2
0
    def plotPSTH(self, stimpath,
                 stimdata,
                 spikesdict,
                 simtime,
                 offset=0,
                 binsize=10e-3,
                 legendSuffix='',
                 rate=False,
                 normcells=True
                 ):
        """Plot the distribution of spike times in a time window.

        Bug in waiting: This should not be called for selections from
        multiple files because simtime may vary.
        """
        if not spikesdict:
            return 0
        stimdata = stimdata[:]
        times = []
        # It is a spike train, x values are spike times, wrap around those
        if 'spikes' in stimpath:
            times = stimdata
        # It is a stimulus: take the leadin edges
        elif 'stim' in stimpath:
            times = numpy.linspace(0, simtime, stimdata.shape[0])[numpy.r_[False, numpy.diff(stimdata) < 0].nonzero()[0]]
        else:
            stimdata = analyzer.smooth(stimdata)
            mid = numpy.mean(stimdata)
            stimdata = stimdata[stimdata > mid] # Threshold at midpoint
            times = numpy.linspace(0, simtime, stimdata.shape[0])[numpy.r_[True, stimdata[1:] > stimdata[:-1]] & numpy.r_[stimdata[:-1] > stimdata[1:], True]]
        if  (times is None) or (len(times) == 0):
            return 0
        start = times + offset
        end = numpy.zeros(times.shape)
        end[:-1] = start[1:]
        end[-1] = simtime + offset # We assume
        accumulated_data = []
        for spikedata in spikesdict.values():
            tpoints = spikedata[:]
            for ii in range(len(times)):
                ix = numpy.nonzero((tpoints >= start[ii]) & (tpoints < end[ii]))[0]
                accumulated_data = numpy.r_[accumulated_data, tpoints[ix] - times[ii]]
        if len(accumulated_data) == 0:
            return 0
        # set the bins by splitting interstimulus interval
        interval = numpy.mean(numpy.diff(times))
        bins = numpy.arange(offset, interval+offset, binsize)
        bins = numpy.r_[bins, bins[-1] + binsize]
        hist = numpy.histogram(accumulated_data, bins=bins)
        xx = (hist[1][:-1] + hist[1][1:])/2.0
        if rate:
            yy = hist[0] / binsize
        else:
            yy = hist[0]
        if normcells:
            yy /= len(spikesdict)
        path = stimpath + '_psth' + legendSuffix
        new_curve = Qwt.QwtPlotCurve(path)
        new_curve.setData(xx, yy)
        pen = Qt.QPen(Qt.Qt.blue, 1, Qt.Qt.DashDotLine)
        new_curve.setStyle(Qwt.QwtPlotCurve.Lines)
        new_curve.setPen(pen)
        pen = Qt.QPen(Qt.Qt.red, 1)
        new_curve.setSymbol(Qwt.QwtSymbol(Qwt.QwtSymbol.XCross,
                                          Qt.QBrush(),
                                          pen,
                                          Qt.QSize(3,3)))        
        new_curve.attach(self)
        self.curve_path_dict[new_curve] = path
        self.path_curve_dict[path].append(new_curve)
        path = stimpath + '_bins' + legendSuffix
        histmarkers = Qwt.QwtPlotCurve(path)
        height = int(max(yy) + 0.5)
        yy = numpy.ones(hist[1].shape) * height
        histmarkers.setData(hist[1], yy)
        pen = Qt.QPen(Qt.Qt.black, 1, Qt.Qt.DotLine)
        histmarkers.setPen(pen)
        histmarkers.setStyle(Qwt.QwtPlotCurve.Sticks)
        histmarkers.attach(self)
        self.curve_path_dict[histmarkers] = path
        self.path_curve_dict[path].append(new_curve)
        self.clearZoomStack()
        self.replot()
        return 1