コード例 #1
0
ファイル: systems.py プロジェクト: thermostatix/pyentropy
    def _calc_external(self, method):
        """Calculate NSB or BUB corrected entropy

        """
        if method == 'nsb-ext':
            from utils import nsb_entropy as entropy_fun
        elif method == 'nsb':
            # don't catch exceptions - just fail if can't import
            from statk.wrap import nsb_entropy as entropy_fun
        elif method == 'bub':
            from statk.wrap import bub_entropy as entropy_fun
        else:
            raise ValueError, "Unknown external entropy method %s in _calc_external" % method

        calc = self.calc
        Hres = {}
        if 'HX' in calc:
            H = entropy_fun(self.PX, self.N, self.X_dim)
            Hres['HX'] = H
        if 'HY' in calc:
            H = entropy_fun(self.PY, self.N, self.Y_dim)
            Hres['HY'] = H
        if 'HXY' in calc:
            H = 0.0
            for y in xrange(self.Y_dim):
                H += self.PY[y] * entropy_fun(self.PXY[:, y], self.Ny[y],
                                              self.X_dim)
            Hres['HXY'] = H
        if 'SiHXi' in calc:
            H = 0.0
            for i in xrange(self.X_n):
                H += entropy_fun(self.PXi[:, i], self.N, self.X_m)
            Hres['SiHXi'] = H
        if 'HiXY' in calc:
            H = 0.0
            for i in xrange(self.X_n):
                for y in xrange(self.Y_dim):
                    H += self.PY[y] * entropy_fun(self.PXiY[:, i, y],
                                                  self.Ny[y], self.X_m)
            Hres['HiXY'] = H
        if 'HiX' in calc:
            H = entropy_fun(self.PiX, self.N, self.X_dim)
            Hres['HiX'] = H
        if 'ChiX' in calc:
            print "Warning: No NSB or BUB correction applied for ChiX"
            H = -(self.PX * malog2(
                np.ma.array(self.PiX,
                            copy=False,
                            mask=(self.PiX <= np.finfo(np.float).eps)))).sum(
                                axis=0)
            Hres['ChiX'] = H
        # save result
        self.__dict__['H_%s' % method] = Hres
コード例 #2
0
    def _calc_nsb(self, ext=False):
        """Calculate NSB corrected entropy
        
        :Parameters:
          ext : {False, True}, optional
            Use external 'nsb-entropy' program vs STAT version

        """
        if ext:
            from utils import nsb_entropy
        else:
            # don't catch exceptions - just fail if can't import
            from statk.wrap import nsb_entropy as _nsb_entropy
            # for debugging
            #nsb_entropy = lambda x,y,z: _nsb_entropy(x,y,z,verbose=True)
            nsb_entropy = _nsb_entropy
        calc = self.calc
        H_nsb = {}
        if 'HX' in calc:
            H = nsb_entropy(self.PX, self.N, self.X_dim) 
            H_nsb['HX'] = H
        if 'HY' in calc:
            H = nsb_entropy(self.PY, self.N, self.Y_dim)
            H_nsb['HY'] = H
        if 'HXY' in calc:
            H = 0.0
            for y in xrange(self.Y_dim):
                H += self.PY[y] * nsb_entropy(self.PXY[:,y], self.Ny[y], self.X_dim) 
            H_nsb['HXY'] = H
        if 'SiHXi' in calc:
            H = 0.0
            for i in xrange(self.X_n):
                H += nsb_entropy(self.PXi[:,i], self.N, self.X_m) 
            H_nsb['SiHXi'] = H
        if 'HiXY' in calc:
            H = 0.0
            for i in xrange(self.X_n):
                for y in xrange(self.Y_dim):
                    H += self.PY[y] * nsb_entropy(self.PXiY[:,i,y], self.Ny[y], self.X_m) 
            H_nsb['HiXY'] = H
        if 'HiX' in calc:
            H = nsb_entropy(self.PiX, self.N, self.X_dim)
            H_nsb['HiX'] = H
        if 'ChiX' in calc:
            print "Warning: No NSB correction applied for ChiX"
            H = -(self.PX*malog2(np.ma.array(self.PiX,copy=False,
                    mask=(self.PiX<=np.finfo(np.float).eps)))).sum(axis=0)
            H_nsb['ChiX'] = H
        if ext:
            self.H_nsbext = H_nsb
        else:
            self.H_nsb = H_nsb
コード例 #3
0
ファイル: systems.py プロジェクト: artemyk/pyentropy
    def _calc_external(self, method):
        """Calculate NSB or BUB corrected entropy

        """
        if method == 'nsb-ext':
            from utils import nsb_entropy as entropy_fun
        elif method == 'nsb':
            # don't catch exceptions - just fail if can't import
            from statk.wrap import nsb_entropy as entropy_fun
        elif method == 'bub':
            from statk.wrap import bub_entropy as entropy_fun
        else:
            raise ValueError, "Unknown external entropy method %s in _calc_external"%method
            
        calc = self.calc
        Hres = {}
        if 'HX' in calc:
            H = entropy_fun(self.PX, self.N, self.X_dim) 
            Hres['HX'] = H
        if 'HY' in calc:
            H = entropy_fun(self.PY, self.N, self.Y_dim)
            Hres['HY'] = H
        if 'HXY' in calc:
            H = 0.0
            for y in xrange(self.Y_dim):
                H += self.PY[y] * entropy_fun(self.PXY[:,y], self.Ny[y], self.X_dim) 
            Hres['HXY'] = H
        if 'SiHXi' in calc:
            H = 0.0
            for i in xrange(self.X_n):
                H += entropy_fun(self.PXi[:,i], self.N, self.X_m) 
            Hres['SiHXi'] = H
        if 'HiXY' in calc:
            H = 0.0
            for i in xrange(self.X_n):
                for y in xrange(self.Y_dim):
                    H += self.PY[y] * entropy_fun(self.PXiY[:,i,y], self.Ny[y], self.X_m) 
            Hres['HiXY'] = H
        if 'HiX' in calc:
            H = entropy_fun(self.PiX, self.N, self.X_dim)
            Hres['HiX'] = H
        if 'ChiX' in calc:
            print "Warning: No NSB or BUB correction applied for ChiX"
            H = -(self.PX*malog2(np.ma.array(self.PiX,copy=False,
                    mask=(self.PiX<=np.finfo(np.float).eps)))).sum(axis=0)
            Hres['ChiX'] = H
        # save result
        self.__dict__['H_%s'%method] = Hres
コード例 #4
0
ファイル: systems.py プロジェクト: thermostatix/pyentropy
 def _calc_pt_plugin(self, pt):
     """Calculate direct entropies and apply PT correction if required """
     calc = self.calc
     pt_corr = lambda R: (R - 1) / (2 * self.N * np.log(2))
     self.H_plugin = {}
     if pt: self.H_pt = {}
     # compute basic entropies
     if 'HX' in calc:
         H = ent(self.PX)
         self.H_plugin['HX'] = H
         if pt:
             self.H_pt['HX'] = H + pt_corr(pt_bayescount(self.PX, self.N))
     if 'HY' in calc:
         H = ent(self.PY)
         self.H_plugin['HY'] = H
         if pt:
             self.H_pt['HY'] = H + pt_corr(pt_bayescount(self.PY, self.N))
     if 'HXY' in calc:
         H = (self.PY * ent(self.PXY)).sum()
         self.H_plugin['HXY'] = H
         if pt:
             for y in xrange(self.Y_dim):
                 H += pt_corr(pt_bayescount(self.PXY[:, y], self.Ny[y]))
             self.H_pt['HXY'] = H
     if 'SiHXi' in calc:
         H = ent(self.PXi).sum()
         self.H_plugin['SiHXi'] = H
         if pt:
             for x in xrange(self.X_n):
                 H += pt_corr(pt_bayescount(self.PXi[:, x], self.N))
             self.H_pt['SiHXi'] = H
     if 'HiXY' in calc:
         H = (self.PY * ent(self.PXiY)).sum()
         self.H_plugin['HiXY'] = H
         if pt:
             for x in xrange(self.X_n):
                 for y in xrange(self.Y_dim):
                     H += pt_corr(
                         pt_bayescount(self.PXiY[:, x, y], self.Ny[y]))
             self.H_pt['HiXY'] = H
     if 'HiX' in calc:
         H = ent(self.PiX)
         self.H_plugin['HiX'] = H
         if pt:
             # no PT correction for HiX
             self.H_pt['HiX'] = H
     if 'ChiX' in calc:
         H = -(self.PX * malog2(
             np.ma.array(self.PiX,
                         copy=False,
                         mask=(self.PiX <= np.finfo(np.float).eps)))).sum(
                             axis=0)
         self.H_plugin['ChiX'] = H
         if pt:
             # no PT correction for ChiX
             self.H_pt['ChiX'] = H
     # for adelman style I(k;spike) (bits/spike)
     if 'HXY1' in calc:
         if self.Y_m != 2:
             raise ValueError, \
             "HXY1 calculation only makes sense for spike data, ie Y_m = 2"
         H = ent(self.PXY[:, 1])
         self.H_plugin['HXY1'] = H
         if pt:
             self.H_pt['HXY1'] = H + pt_corr(
                 pt_bayescount(self.PXY[:, 1], self.Ny[1]))
     if 'ChiXY1' in calc:
         if self.Y_m != 2:
             raise ValueError, \
             "ChiXY1 calculation only makes sense for spike data, ie Y_m = 2"
         H = -np.ma.array(self.PXY[:, 1] * np.log2(self.PX),
                          copy=False,
                          mask=(self.PX <= np.finfo(np.float).eps)).sum()
         self.H_plugin['ChiXY1'] = H
         if pt:
             # no PT for ChiXY1
             self.H_pt['ChiXY1'] = H
コード例 #5
0
ファイル: systems.py プロジェクト: Cecitux/pyentropy
 def _calc_pt_plugin(self, pt):
     """Calculate direct entropies and apply PT correction if required """
     calc = self.calc
     pt_corr = lambda R: (R-1)/(2*self.N*np.log(2))
     self.H_plugin = {}
     if pt: self.H_pt = {}
     # compute basic entropies
     if 'HX' in calc:
         H = ent(self.PX)
         self.H_plugin['HX'] = H
         if pt:
             self.H_pt['HX'] = H + pt_corr(pt_bayescount(self.PX, self.N))
     if 'HY' in calc:
         H = ent(self.PY)
         self.H_plugin['HY'] = H
         if pt:
             self.H_pt['HY'] = H + pt_corr(pt_bayescount(self.PY, self.N))
     if 'HXY' in calc:
         H = (self.PY * ent(self.PXY)).sum()
         self.H_plugin['HXY'] = H
         if pt:
             for y in xrange(self.Y_dim):
                 H += pt_corr(pt_bayescount(self.PXY[:,y], self.Ny[y]))
             self.H_pt['HXY'] = H
     if 'SiHXi' in calc:
         H = ent(self.PXi).sum()
         self.H_plugin['SiHXi'] = H
         if pt:
             for x in xrange(self.X_n):
                 H += pt_corr(pt_bayescount(self.PXi[:,x],self.N))
             self.H_pt['SiHXi'] = H
     if 'HiXY' in calc:
         H = (self.PY * ent(self.PXiY)).sum()
         self.H_plugin['HiXY'] = H
         if pt:
             for x in xrange(self.X_n):
                 for y in xrange(self.Y_dim):
                     H += pt_corr(pt_bayescount(self.PXiY[:,x,y],self.Ny[y]))
             self.H_pt['HiXY'] = H
     if 'HiX' in calc:
         H = ent(self.PiX)
         self.H_plugin['HiX'] = H
         if pt:
             # no PT correction for HiX
             self.H_pt['HiX'] = H
     if 'ChiX' in calc:
         H = -(self.PX*malog2(np.ma.array(self.PiX,copy=False,
                 mask=(self.PiX<=np.finfo(np.float).eps)))).sum(axis=0)
         self.H_plugin['ChiX'] = H
         if pt:
             # no PT correction for ChiX
             self.H_pt['ChiX'] = H
     # for adelman style I(k;spike) (bits/spike)
     if 'HXY1' in calc:
         if self.Y_m != 2:
             raise ValueError, \
             "HXY1 calculation only makes sense for spike data, ie Y_m = 2"
         H = ent(self.PXY[:,1])
         self.H_plugin['HXY1'] = H
         if pt:
             self.H_pt['HXY1'] = H + pt_corr(pt_bayescount(self.PXY[:,1],self.Ny[1])) 
     if 'ChiXY1' in calc:
         if self.Y_m != 2:
             raise ValueError, \
             "ChiXY1 calculation only makes sense for spike data, ie Y_m = 2"
         H = -np.ma.array(self.PXY[:,1]*np.log2(self.PX),copy=False,
                 mask=(self.PX<=np.finfo(np.float).eps)).sum()
         self.H_plugin['ChiXY1'] = H
         if pt:
             # no PT for ChiXY1
             self.H_pt['ChiXY1'] = H