Пример #1
0
def entropy(y, bins):
   """
   Return the entropy of the data in y

   \sum p_i log2(p_i) where p_i is the probability of observing y in
   the ith bin of bins.  bins can be a number of bins or a range of
   bins; see hist

   Compare S with analytic calculation for a Gaussian
   x = mu + sigma*randn(200000)
   Sanalytic = 0.5  * ( 1.0 + log(2*pi*sigma**2.0) ) 

   """

   
   n,bins = hist(y, bins)
   n = n.astype(Float)

   n = take(n, nonzero(n))         # get the positive

   p = divide(n, len(y))

   delta = bins[1]-bins[0]
   S = -1.0*asum(p*log(p)) + log(delta)
   #S = -1.0*asum(p*log(p))
   return S
Пример #2
0
def entropy(y, bins):
    """
   Return the entropy of the data in y

   \sum p_i log2(p_i) where p_i is the probability of observing y in
   the ith bin of bins.  bins can be a number of bins or a range of
   bins; see hist

   Compare S with analytic calculation for a Gaussian
   x = mu + sigma*randn(200000)
   Sanalytic = 0.5  * ( 1.0 + log(2*pi*sigma**2.0) ) 

   """

    n, bins = hist(y, bins)
    n = n.astype(Float)

    n = take(n, nonzero(n))  # get the positive

    p = divide(n, len(y))

    delta = bins[1] - bins[0]
    S = -1.0 * asum(p * log(p)) + log(delta)
    #S = -1.0*asum(p*log(p))
    return S
 def __call__(self, value, clip=None):
     if clip is None:
         clip = self.clip
     if isinstance(value, (int, float)):
         vtype = 'scalar'
         val = ma.array([value])
     else:
         vtype = 'array'
         val = ma.asarray(value)
     self.autoscale(val)
     vmin, vmax = self.vmin, self.vmax
     if vmin > vmax:
         raise ValueError("minvalue must be less than or equal to maxvalue")
     elif vmin<=0:
         raise ValueError("values must all be positive")
     elif vmin==vmax:
         return 0.*value
     else:
         if clip:
             mask = ma.getmask(val)
             val = ma.array(nx.clip(val.filled(vmax), vmin, vmax),
                             mask=mask)
         result = (ma.log(val)-nx.log(vmin))/(nx.log(vmax)-nx.log(vmin))
     if vtype == 'scalar':
         result = result[0]
     return result
Пример #4
0
    def release_zoom(self, event):
        'the release mouse button callback in zoom to rect mode'        
        if self._xypress is None: return
        x, y = event.x, event.y

        
        lastx, lasty, a, ind, lim, trans = self._xypress
        # ignore singular clicks - 5 pixels is a threshold
        if abs(x-lastx)<5 or abs(y-lasty)<5 or not a.in_axes(x,y):
            self._xypress = None
            self.release(event)
            self.draw()
            return

        xmin, ymin, xmax, ymax = lim

        # zoom to rect
        lastx, lasty = a.transData.inverse_xy_tup( (lastx, lasty) )
        x, y = a.transData.inverse_xy_tup( (x, y) )            

        if x<lastx:  xmin, xmax = x, lastx
        else: xmin, xmax = lastx, x

        if y<lasty:  ymin, ymax = y, lasty
        else: ymin, ymax = lasty, y

        if self._button_pressed == 1:  
            a.set_xlim((xmin, xmax))
            a.set_ylim((ymin, ymax))
        elif self._button_pressed == 3: 
            Xmin,Xmax=a.get_xlim()
            Ymin,Ymax=a.get_ylim()
            if a.get_xscale()=='log':
                alpha=log(Xmax/Xmin)/log(xmax/xmin)
                x1=pow(Xmin/xmin,alpha)*Xmin
                x2=pow(Xmax/xmin,alpha)*Xmin
            else:    
                alpha=(Xmax-Xmin)/(xmax-xmin)
                x1=alpha*(Xmin-xmin)+Xmin
                x2=alpha*(Xmax-xmin)+Xmin
            if a.get_yscale()=='log':
                alpha=log(Ymax/Ymin)/log(ymax/ymin)
                y1=pow(Ymin/ymin,alpha)*Ymin
                y2=pow(Ymax/ymin,alpha)*Ymin
            else:    
                alpha=(Ymax-Ymin)/(ymax-ymin)
                y1=alpha*(Ymin-ymin)+Ymin
                y2=alpha*(Ymax-ymin)+Ymin
            a.set_xlim((x1, x2))
            a.set_ylim((y1, y2))    

        self.draw()
        self._xypress = None
        self._button_pressed == None

        self.push_current()
        self.release(event)
Пример #5
0
    def release_zoom(self, event):
        'the release mouse button callback in zoom to rect mode'
        if self._xypress is None: return
        x, y = event.x, event.y

        lastx, lasty, a, ind, lim, trans = self._xypress
        # ignore singular clicks - 5 pixels is a threshold
        if abs(x - lastx) < 5 or abs(y - lasty) < 5 or not a.in_axes(x, y):
            self._xypress = None
            self.release(event)
            self.draw()
            return

        xmin, ymin, xmax, ymax = lim

        # zoom to rect
        lastx, lasty = a.transData.inverse_xy_tup((lastx, lasty))
        x, y = a.transData.inverse_xy_tup((x, y))

        if x < lastx: xmin, xmax = x, lastx
        else: xmin, xmax = lastx, x

        if y < lasty: ymin, ymax = y, lasty
        else: ymin, ymax = lasty, y

        if self._button_pressed == 1:
            a.set_xlim((xmin, xmax))
            a.set_ylim((ymin, ymax))
        elif self._button_pressed == 3:
            Xmin, Xmax = a.get_xlim()
            Ymin, Ymax = a.get_ylim()
            if a.get_xscale() == 'log':
                alpha = log(Xmax / Xmin) / log(xmax / xmin)
                x1 = pow(Xmin / xmin, alpha) * Xmin
                x2 = pow(Xmax / xmin, alpha) * Xmin
            else:
                alpha = (Xmax - Xmin) / (xmax - xmin)
                x1 = alpha * (Xmin - xmin) + Xmin
                x2 = alpha * (Xmax - xmin) + Xmin
            if a.get_yscale() == 'log':
                alpha = log(Ymax / Ymin) / log(ymax / ymin)
                y1 = pow(Ymin / ymin, alpha) * Ymin
                y2 = pow(Ymax / ymin, alpha) * Ymin
            else:
                alpha = (Ymax - Ymin) / (ymax - ymin)
                y1 = alpha * (Ymin - ymin) + Ymin
                y2 = alpha * (Ymax - ymin) + Ymin
            a.set_xlim((x1, x2))
            a.set_ylim((y1, y2))

        self.draw()
        self._xypress = None
        self._button_pressed == None

        self.push_current()
        self.release(event)
Пример #6
0
def specgram(x,
             NFFT=256,
             Fs=2,
             detrend=detrend_none,
             window=window_hanning,
             noverlap=128):
    """
    Compute a spectrogram of data in x.  Data are split into NFFT
    length segements and the PSD of each section is computed.  The
    windowing function window is applied to each segment, and the
    amount of overlap of each segment is specified with noverlap

    See pdf for more info.

    The returned times are the midpoints of the intervals over which
    the ffts are calculated
    """
    x = asarray(x)
    assert (NFFT > noverlap)
    if log(NFFT) / log(2) != int(log(NFFT) / log(2)):
        raise ValueError, 'NFFT must be a power of 2'

    # zero pad x up to NFFT if it is shorter than NFFT
    if len(x) < NFFT:
        n = len(x)
        x = resize(x, (NFFT, ))
        x[n:] = 0

    # for real x, ignore the negative frequencies
    if typecode(x) == Complex: numFreqs = NFFT
    else: numFreqs = NFFT // 2 + 1

    windowVals = window(ones((NFFT, ), typecode(x)))
    step = NFFT - noverlap
    ind = arange(0, len(x) - NFFT + 1, step)
    n = len(ind)
    Pxx = zeros((numFreqs, n), Float)
    # do the ffts of the slices

    for i in range(n):
        thisX = x[ind[i]:ind[i] + NFFT]
        thisX = windowVals * detrend(thisX)
        fx = absolute(fft(thisX))**2
        # Scale the spectrum by the norm of the window to compensate for
        # windowing loss; see Bendat & Piersol Sec 11.5.2
        Pxx[:, i] = divide(fx[:numFreqs], norm(windowVals)**2)
    t = 1 / Fs * (ind + NFFT / 2)
    freqs = Fs / NFFT * arange(numFreqs)

    return Pxx, freqs, t
Пример #7
0
def liaupunov(x, fprime):
   """
   x is a very long trajectory from a map, and derivs is the analytic
   derivative of the map.  Return lambda = 1/n\sum ln|fprime(x_i)|.
   See Sec 10.5 Strogatz (1994) "Nonlinear Dynamics and Chaos".   
   """
   return mean(log(fprime(x)))
Пример #8
0
def liaupunov(x, fprime):
    """
   x is a very long trajectory from a map, and derivs is the analytic
   derivative of the map.  Return lambda = 1/n\sum ln|fprime(x_i)|.
   See Sec 10.5 Strogatz (1994) "Nonlinear Dynamics and Chaos".   
   """
    return mean(log(fprime(x)))
Пример #9
0
def specgram(x, NFFT=256, Fs=2, detrend=detrend_none,
             window=window_hanning, noverlap=128):
    """
    Compute a spectrogram of data in x.  Data are split into NFFT
    length segements and the PSD of each section is computed.  The
    windowing function window is applied to each segment, and the
    amount of overlap of each segment is specified with noverlap

    See pdf for more info.

    The returned times are the midpoints of the intervals over which
    the ffts are calculated
    """

    assert(NFFT>noverlap)
    if log(NFFT)/log(2) != int(log(NFFT)/log(2)):
       raise ValueError, 'NFFT must be a power of 2'

    # zero pad x up to NFFT if it is shorter than NFFT
    if len(x)<NFFT:
        n = len(x)
        x = resize(x, (NFFT,))
        x[n:] = 0
    

    # for real x, ignore the negative frequencies
    if x.typecode()==Complex: numFreqs = NFFT
    else: numFreqs = NFFT//2+1
        
    windowVals = window(ones((NFFT,),x.typecode()))
    step = NFFT-noverlap
    ind = arange(0,len(x)-NFFT+1,step)
    n = len(ind)
    Pxx = zeros((numFreqs,n), Float)
    # do the ffts of the slices

    for i in range(n):
        thisX = x[ind[i]:ind[i]+NFFT]
        thisX = windowVals*detrend(thisX)
        fx = absolute(fft(thisX))**2
        # Scale the spectrum by the norm of the window to compensate for
        # windowing loss; see Bendat & Piersol Sec 11.5.2
        Pxx[:,i] = divide(fx[:numFreqs], norm(windowVals)**2)
    t = 1/Fs*(ind+NFFT/2)
    freqs = Fs/NFFT*arange(numFreqs)

    return Pxx, freqs, t
Пример #10
0
def pyxplothist(graph, x_sample, Nbins=80, bin_range=None, norm=False, bars=0,
                lw=0, lt=0, color=(0,0,0), xlogscale=False, ylogscale=False,
                y_given=None, title=False):
    """ Plots a histogram of 'x_sample'.
    
        Arguments:
        'x_sampe'    - data
        'Nbins'      - number of bins
        'bin_range'  - intervall to be divided into 'Nbins' equidistant parts
        'norm'       - normalization of histogram 
                       (comparison with density function)
        'bars'       - style parameter: bars (bars=1) or steps (bars=0)   
        'lw', 'lt'   - linewidth, linetype
        'title'      - title
        'color'      - color of histogram (r,g,b)
        'xlogscale'  - takes bins with logarithmically constant width,
                       makes only sense, when 'xaxistype' of 'graph' 
                       is set to 'log' 
        'ylogscale'  - activates necessary changes, if y-axis is logarithmic, 
                       makes only sense, when 'yaxistype' of 'graph' 
                       is set to 'log'
        'ygiven'     - if you already know the y-values
    """
    steps = 1-bars
    
    # determine max. x-value for the bins
    x_max = max(x_sample)
    if bin_range != None:
        x_max = bin_range[1]
    
    # changes due to logarithmic x-axis:
    #  take log of the data, check if data or range is <= zero
    if xlogscale:
        if min(x_sample) > 0:
            x_sample = log(x_sample)
            if bin_range != None:
                if bin_range[0] > 0 and bin_range[1] > 0:
                    bin_range = (log(bin_range[0]),log(bin_range[1]))
                else:
                    print "Given range includes values <= 0!"
                    print "Ignoring given range..."
                    bin_range = None
        else:
            print "Data is smaller than zero, no logarithmic x-axis possible!"
            print "Continuing linearly..."
            xlogscale = False
    
    if y_given != None:
        x_l = x_sample
        x_hist = y_given
        Nbins = len(x_l)
    else:
        ## histogram of 'x_sample': gives number 'x_hist' of sample points within 
        ##   'Nbins' different of bins within 'bin_range', where the bins are 
        ##   specified by their left edge coordinate 'x_l'
        x_hist, x_l = histogram(x_sample, bins=Nbins, range=bin_range, normed=norm)
        
    # if logarithmic x-axis: exponentiate bin positions and data
    if xlogscale:
        x_l = exp(x_l)
        x_sample = exp(x_sample)
        
    y_min = 0
    # offset for logarithmic y-axis
    if ylogscale:
        x_hist = x_hist+1e-16
        y_min = 1e-16

    # plot histogram manually
    for i in xrange(Nbins):
        # horzontal lines
        if i < Nbins-1:       
            graph.pyxplot(([x_l[i],x_l[i+1]],[x_hist[i],x_hist[i]]), 
                          style="l", color=color, lt=lt, lw=lw, title=False)
        else:
            graph.pyxplot(([x_l[i],x_max],[x_hist[i],x_hist[i]]), 
                          style="l", color=color, lt=lt, lw=lw, title=False)
                      
        # vertical lines
        if i != Nbins-1:
            graph.pyxplot(([x_l[i+1],x_l[i+1]],[x_hist[i],x_hist[i+1]]), 
                          style="l", color=color, lt=lt, lw=lw, title=False)
        if i == 0:
            graph.pyxplot(([x_l[i],x_l[i]],[x_hist[i],y_min]), 
                          style="l", color=color, lt=lt, lw=lw, title=title)
        if i == Nbins-1:
            graph.pyxplot(([x_max,x_max],[x_hist[i],y_min]), 
                          style="l", color=color, lt=lt, lw=lw, title=False)
Пример #11
0
 def convertxy_loglog(position):
     """Convert from [0.0, 1.0] coordinates to canvas coordinates 
        on a logarithmic scale in x- and y-direction"""
     return ( (log(position[0])-log(xmin))/(log(xmax)-log(xmin)),
              (log(position[1])-log(ymin))/(log(ymax)-log(ymin))  )
Пример #12
0
    def release_zoom(self, event):
        'the release mouse button callback in zoom to rect mode'
        if self._xypress is None: return
        x, y = event.x, event.y


        lastx, lasty, a, ind, lim, trans = self._xypress
        # ignore singular clicks - 5 pixels is a threshold
        if abs(x-lastx)<5 or abs(y-lasty)<5:
            self._xypress = None
            self.release(event)
            self.draw()
            return

        xmin, ymin, xmax, ymax = lim

        # zoom to rect
        lastx, lasty = a.transData.inverse_xy_tup( (lastx, lasty) )
        x, y = a.transData.inverse_xy_tup( (x, y) )
        Xmin,Xmax=a.get_xlim()
        Ymin,Ymax=a.get_ylim()

        if Xmin < Xmax:
            if x<lastx:  xmin, xmax = x, lastx
            else: xmin, xmax = lastx, x
            if xmin < Xmin: xmin=Xmin
            if xmax > Xmax: xmax=Xmax
        else:
            if x>lastx:  xmin, xmax = x, lastx
            else: xmin, xmax = lastx, x
            if xmin > Xmin: xmin=Xmin
            if xmax < Xmax: xmax=Xmax

        if Ymin < Ymax:
            if y<lasty:  ymin, ymax = y, lasty
            else: ymin, ymax = lasty, y
            if ymin < Ymin: ymin=Ymin
            if ymax > Ymax: ymax=Ymax
        else:
            if y>lasty:  ymin, ymax = y, lasty
            else: ymin, ymax = lasty, y
            if ymin > Ymin: ymin=Ymin
            if ymax < Ymax: ymax=Ymax

        if self._button_pressed == 1:
            a.set_xlim((xmin, xmax))
            a.set_ylim((ymin, ymax))
        elif self._button_pressed == 3:
            if a.get_xscale()=='log':
                alpha=log(Xmax/Xmin)/log(xmax/xmin)
                x1=pow(Xmin/xmin,alpha)*Xmin
                x2=pow(Xmax/xmin,alpha)*Xmin
            else:
                alpha=(Xmax-Xmin)/(xmax-xmin)
                x1=alpha*(Xmin-xmin)+Xmin
                x2=alpha*(Xmax-xmin)+Xmin
            if a.get_yscale()=='log':
                alpha=log(Ymax/Ymin)/log(ymax/ymin)
                y1=pow(Ymin/ymin,alpha)*Ymin
                y2=pow(Ymax/ymin,alpha)*Ymin
            else:
                alpha=(Ymax-Ymin)/(ymax-ymin)
                y1=alpha*(Ymin-ymin)+Ymin
                y2=alpha*(Ymax-ymin)+Ymin
            a.set_xlim((x1, x2))
            a.set_ylim((y1, y2))

        # Zoom with fixed aspect; modified for shared x-axes
        aspect = a.get_aspect()
        if aspect == 'equal' or aspect == 'scaled': 
            self.fix_aspect_after_zoom(a)
        else:
            aspect_shared = ''
            if a._sharex != None: aspect_shared = a._sharex.get_aspect()
            if aspect_shared == 'equal' or aspect_shared == 'scaled':
                self.fix_aspect_after_zoom(a._sharex)
                
        self.draw()
        self._xypress = None
        self._button_pressed == None

        self.push_current()
        self.release(event)
Пример #13
0
    def release_zoom(self, event):
        'the release mouse button callback in zoom to rect mode'
        if self._xypress is None: return
        x, y = event.x, event.y

        lastx, lasty, a, ind, lim, trans = self._xypress
        # ignore singular clicks - 5 pixels is a threshold
        if abs(x - lastx) < 5 or abs(y - lasty) < 5:
            self._xypress = None
            self.release(event)
            self.draw()
            return

        xmin, ymin, xmax, ymax = lim

        # zoom to rect
        lastx, lasty = a.transData.inverse_xy_tup((lastx, lasty))
        x, y = a.transData.inverse_xy_tup((x, y))
        Xmin, Xmax = a.get_xlim()
        Ymin, Ymax = a.get_ylim()

        if Xmin < Xmax:
            if x < lastx: xmin, xmax = x, lastx
            else: xmin, xmax = lastx, x
            if xmin < Xmin: xmin = Xmin
            if xmax > Xmax: xmax = Xmax
        else:
            if x > lastx: xmin, xmax = x, lastx
            else: xmin, xmax = lastx, x
            if xmin > Xmin: xmin = Xmin
            if xmax < Xmax: xmax = Xmax

        if Ymin < Ymax:
            if y < lasty: ymin, ymax = y, lasty
            else: ymin, ymax = lasty, y
            if ymin < Ymin: ymin = Ymin
            if ymax > Ymax: ymax = Ymax
        else:
            if y > lasty: ymin, ymax = y, lasty
            else: ymin, ymax = lasty, y
            if ymin > Ymin: ymin = Ymin
            if ymax < Ymax: ymax = Ymax

        if self._button_pressed == 1:
            a.set_xlim((xmin, xmax))
            a.set_ylim((ymin, ymax))
        elif self._button_pressed == 3:
            if a.get_xscale() == 'log':
                alpha = log(Xmax / Xmin) / log(xmax / xmin)
                x1 = pow(Xmin / xmin, alpha) * Xmin
                x2 = pow(Xmax / xmin, alpha) * Xmin
            else:
                alpha = (Xmax - Xmin) / (xmax - xmin)
                x1 = alpha * (Xmin - xmin) + Xmin
                x2 = alpha * (Xmax - xmin) + Xmin
            if a.get_yscale() == 'log':
                alpha = log(Ymax / Ymin) / log(ymax / ymin)
                y1 = pow(Ymin / ymin, alpha) * Ymin
                y2 = pow(Ymax / ymin, alpha) * Ymin
            else:
                alpha = (Ymax - Ymin) / (ymax - ymin)
                y1 = alpha * (Ymin - ymin) + Ymin
                y2 = alpha * (Ymax - ymin) + Ymin
            a.set_xlim((x1, x2))
            a.set_ylim((y1, y2))

        # Zoom with fixed aspect; modified for shared x-axes
        aspect = a.get_aspect()
        if aspect == 'equal' or aspect == 'scaled':
            self.fix_aspect_after_zoom(a)
        else:
            aspect_shared = ''
            if a._sharex != None: aspect_shared = a._sharex.get_aspect()
            if aspect_shared == 'equal' or aspect_shared == 'scaled':
                self.fix_aspect_after_zoom(a._sharex)

        self.draw()
        self._xypress = None
        self._button_pressed == None

        self.push_current()
        self.release(event)