This file is indexed.

/usr/share/pyshared/cogent/maths/period.py is in python-cogent 1.5.1-2.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
from numpy import zeros, array, exp, pi, cos, fft, arange, power, sqrt, sum,\
                    multiply, float64, polyval

__author__ = "Hua Ying, Julien Epps and Gavin Huttley"
__copyright__ = "Copyright 2007-2011, The Cogent Project"
__credits__ = ["Julien Epps", "Hua Ying", "Gavin Huttley", "Peter Maxwell"]
__license__ = "GPL"
__version__ = "1.5.1"
__maintainer__ = "Gavin Huttley"
__email__ = "Gavin.Huttley@anu.edu.au"
__status__ = "Production"

def _goertzel_inner(x, N, period):
    coeff = 2.0 * cos(2 * pi / period)
    s_prev = 0.0
    s_prev2 = 0.0
    for n in range(N):
        s = x[n] + coeff * s_prev - s_prev2
        s_prev2 = s_prev
        s_prev = s
    pwr = sqrt(s_prev2**2 + s_prev**2 - coeff * s_prev2 * s_prev)
    return pwr

def _ipdft_inner(x, X, W, ulim, N): # naive python
    for p in range(ulim):
        w = 1
        for n in range(N):
            if n != 0:
                w *= W[p]
            X[p] = X[p] + x[n] * w
    return X

def _ipdft_inner2(x, X, W, ulim, N): # fastest python
    p = x[::-1] # reversed
    X = polyval(p, W)
    return X

def _autocorr_inner2(x, xc, N): # fastest python
    products = multiply.outer(x, x)
    v = [products.trace(offset=m) for m in range(-len(x)+1, len(x))]
    xc.put(xrange(xc.shape[0]), v)

def _autocorr_inner(x, xc, N): # naive python
    for m in range(-N+1, N):
        for n in range(N):
            if 0 <= n-m < N:
                xc[m+N-1] += (x[n]*x[n-m])

try:
    # try using pyrexed versions
    from _period import ipdft_inner, autocorr_inner, goertzel_inner
    # raise ImportError # for profiling
except ImportError:
    # fastest python versions
    ipdft_inner = _ipdft_inner2
    autocorr_inner = _autocorr_inner2
    goertzel_inner = _goertzel_inner

def goertzel(x, period):
    """returns the array(power), array(period) from series x for period
    result objects are arrays for consistency with that other period
    estimation functions"""
    calc = Goertzel(len(x), period=period)
    return calc(x)

class _PeriodEstimator(object):
    """parent class for period estimation"""
    def __init__(self, length, llim=None, ulim=None, period=None):
        super(_PeriodEstimator, self).__init__()
        self.length = length
        self.llim = llim or 2
        self.ulim = ulim or (length-1)
        
        if self.ulim > length:
            raise RuntimeError, 'Error: ulim > length'
        
        self.period = period
    
    def getNumStats(self):
        """returns the number of statistics computed by this calculator"""
        return 1
    

class AutoCorrelation(_PeriodEstimator):
    def __init__(self, length, llim=None, ulim=None, period=None):
        """class for repetitive calculation of autocorrelation for series of
        fixed length
        
        e.g. if x = [1,1,1,1], xc = [1,2,3,4,3,2,1]
        The middle element of xc corresponds to a lag (period) of 0
        xc is always symmetric for real x
        N is the length of x"""
        super(AutoCorrelation, self).__init__(length, llim, ulim, period)
        
        periods = range(-length+1, length)
        
        self.min_idx = periods.index(self.llim)
        self.max_idx = periods.index(self.ulim)
        self.periods = array(periods[self.min_idx: self.max_idx + 1])
        self.xc = zeros(2*self.length-1)
    
    def evaluate(self, x):
        x = array(x, float64)
        self.xc.fill(0.0)
        autocorr_inner(x, self.xc, self.length)
        xc = self.xc[self.min_idx: self.max_idx + 1]
        if self.period is not None:
            return xc[self.period-self.llim]
        
        return xc, self.periods
    
    __call__ = evaluate

def auto_corr(x, llim=None, ulim=None):
    """returns the autocorrelation of x
    e.g. if x = [1,1,1,1], xc = [1,2,3,4,3,2,1]
    The middle element of xc corresponds to a lag (period) of 0
    xc is always symmetric for real x
    N is the length of x
    """
    _autocorr = AutoCorrelation(len(x), llim=llim, ulim=ulim)
    return _autocorr(x)

class Ipdft(_PeriodEstimator):
    
    def __init__(self, length, llim=None, ulim=None, period=None, abs_ft_sig=True):
        """factory function for computing the integer period discrete Fourier
        transform for repeated application to signals of the same length.
    
        Argument:
            - length: the signal length
            - llim: lower limit
            - ulim: upper limit
            - period: a specific period to return the IPDFT power for
            - abs_ft_sig: if True, returns absolute value of signal
        """
        if period is not None:
            llim = period
            ulim = period
        super(Ipdft, self).__init__(length, llim, ulim, period)
        self.periods = array(range(self.llim, self.ulim+1))
        self.W = exp(-1j * 2 * pi / arange(1, self.ulim+1))
        self.X = array([0+0j] * self.length)
        self.abs_ft_sig = abs_ft_sig
    
    def evaluate(self, x):
        x = array(x, float64)
        self.X.fill(0+0j)
        self.X = ipdft_inner(x, self.X, self.W, self.ulim, self.length)
        pwr = self.X[self.llim-1:self.ulim]
        
        if self.abs_ft_sig:
            pwr = abs(pwr)
        
        if self.period is not None:
            return pwr[self.period-self.llim]
        
        return array(pwr), self.periods
    
    __call__ = evaluate
    

class Goertzel(_PeriodEstimator):
    """Computes the power of a signal for a specific period"""
    def __init__(self, length=None, llim=None, ulim=None, period=None, abs_ft_sig=True):
        assert period is not None, "Goertzel requires a period"
        super(Goertzel, self).__init__(length=length, period=period)
    
    def evaluate(self, x):
        x = array(x, float64)
        return _goertzel_inner(x, self.length, self.period)
    
    __call__ = evaluate


class Hybrid(_PeriodEstimator):
    """hybrid statistic and corresponding periods for signal x
    
    See Epps. EURASIP Journal on Bioinformatics and Systems Biology, 2009"""
    
    def __init__(self, length, llim=None, ulim=None, period=None, abs_ft_sig=True, return_all=False):
        """Arguments:
            - length: the length of signals to be encountered
            - period: specified period at which to return the signal
            - llim, ulim: the smallest, largest periods to evaluate
            - return_all: whether to return the hybrid, ipdft, autocorr
              statistics as a numpy array, or just the hybrid statistic
        """
        super(Hybrid, self).__init__(length, llim, ulim, period)
        self.ipdft = Ipdft(length, llim, ulim, period, abs_ft_sig)
        self.auto = AutoCorrelation(length, llim, ulim, period)
        self._return_all = return_all
    
    def getNumStats(self):
        """the number of stats computed by this calculator"""
        num = [1, 3][self._return_all]
        return num
    
    def evaluate(self, x):
        if self.period is None:
            auto_sig, auto_periods = self.auto(x)
            ft_sig, ft_periods = self.ipdft(x)
            hybrid = auto_sig * ft_sig
            if self._return_all:
                result = array([hybrid, ft_sig, auto_sig]), ft_periods
            else:
                result = hybrid, ft_periods
        else:
            auto_sig = self.auto(x)
            # ft_sig = goertzel(x, period) # performance slower than ipdft!
            ft_sig = self.ipdft(x)
            hybrid = auto_sig * ft_sig
            if self._return_all:
                result = array([abs(hybrid), ft_sig, auto_sig])
            else:
                result = abs(hybrid)
        return result
    
    __call__ = evaluate


def ipdft(x, llim=None, ulim=None, period=None):
    """returns the integer period discrete Fourier transform of the signal x
    
    Arguments:
        - x: series of symbols
        - llim: lower limit
        - ulim: upper limit
    """
    x = array(x, float64)
    ipdft_calc = Ipdft(len(x), llim, ulim, period)
    return ipdft_calc(x)

def hybrid(x, llim=None, ulim=None, period=None, return_all=False):
    """
    Return hybrid statistic and corresponding periods for signal x
    
    Arguments:
        - return_all: whether to return the hybrid, ipdft, autocorr
          statistics as a numpy array, or just the hybrid statistic
    
    See Epps. EURASIP Journal on Bioinformatics and Systems Biology, 2009, 9
    """
    hybrid_calc = Hybrid(len(x), llim, ulim, period, return_all=return_all)
    x = array(x, float)
    return hybrid_calc(x)

def dft(x, **kwargs):
    """
    Return discrete fft and corresponding periods for signal x
    """
    n = len(x) / 2 * 2
    x = array(x[:n])
    pwr = fft.rfft(x, n)[1:]
    freq = (arange(n/2+1)/(float(n)))[1:]
    pwr = list(pwr)
    periods = [1/f for f in freq]
    pwr.reverse()
    periods.reverse()
    return array(pwr), array(periods)

if __name__ == "__main__":
    from numpy import sin
    x = sin(2*pi/5*arange(1,9))
    print x
    print goertzel(x, 4)
    print goertzel(x, 8)