Changeset 321
- Timestamp:
- 11/12/08 10:31:01 (2 months ago)
- Files:
-
- trunk/src/analysis.py (modified) (2 diffs)
- trunk/src/signals.py (modified) (2 diffs)
- trunk/test/test_analysis.py (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/src/analysis.py
r311 r321 1 #!/usr/bin/env python2 1 # -*- coding: utf8 -*- 3 2 """ 4 analysis.py: 3 NeuroTools.analysis 4 ================== 5 5 6 $Id$ 6 A collection of analysis functions that may be used by NeuroTools.signals or other packages. 7 8 Classes 9 ------- 10 11 TuningCurve - A tuning curve object (not very documented) 12 13 14 Functions 15 --------- 16 17 ccf - fast cross correlation function based on fft 18 simple_frequency_spectrum - Simple frequencxy spectrum 19 arrays_almost_equal - comparison of two arrays 7 20 """ 8 21 … … 15 28 16 29 def ccf(x, y, axis=None): 17 """ Computes the cross-correlation function of two series `x` and `y`.18 Note that the computations are performed on anomalies (deviations from19 average).20 Returns the values of the cross-correlation at different lags.21 Lags are given as [0,1,2,...,n,n-1,n-2,...,-2,-1] (not any more)30 """ 31 Computes the cross-correlation function of two series x and y. 32 Note that the computations are performed on anomalies (deviations from 33 average). 34 Returns the values of the cross-correlation at different lags. 22 35 23 :Parameters: 24 `x` : 1D MaskedArray 25 Time series. 26 `y` : 1D MaskedArray 27 Time series. 28 `axis` : integer *[None]* 29 Axis along which to compute (0 for rows, 1 for cols). 30 If `None`, the array is flattened first. 36 Inputs: 37 x - 1D MaskedArray of a Time series. 38 y - 1D MaskedArray of a Time series. 39 axis - integer *[None]* Axis along which to compute (0 for rows, 1 for cols). 40 If `None`, the array is flattened first. 41 42 Examples: 43 >> z= arange(1000) 44 >> ccf(z,z) 45 31 46 """ 32 47 assert(x.ndim == y.ndim, "Inconsistent shape !") trunk/src/signals.py
r320 r321 115 115 # several : t_start = min(time), t_stop = max(time) 116 116 117 size = len(s pike_times)117 size = len(self.spike_times) 118 118 if size == 0: 119 119 if self.t_start is None: … … 1216 1216 See also: 1217 1217 cv_isi_hist, cv_local, cv_isi, SpikeTrain.cv_kl 1218 1219 1218 """ 1220 1219 ids = self.id_list() trunk/test/test_analysis.py
r245 r321 4 4 5 5 from NeuroTools import analysis 6 import numpy 6 import numpy, unittest 7 7 from numpy import pi, sin 8 8 9 9 # test simple_frequency_spectrum 10 10 11 def test_simple_frequency_spectrum(): 12 13 ph = [0.5*pi, 1.2*pi, 0.3*pi] 14 x = lambda A, ph, f, t: A[0] + A[1]*sin(2*pi*f/1000.0*t + ph[0]) + A[2]*sin(2*pi*2*f/1000.0*t + ph[1]) + A[3]*sin(2*pi*3*f/1000.0*t + ph[2]) 15 durations = [10.0, 10.0, 30.0] 16 binwidths = [1.0, 5.0, 10.0] 17 18 for i in range(10): 19 A = numpy.random.uniform(-10, 10, size=4) 20 #print A 21 for f in 0.5, 1.0, 2.0, 10.0, 99.0: # cycles/sec 22 for duration, binwidth in zip(durations, binwidths): 23 if 3*f < 1000.0/binwidth: # binwidth limits the max frequency 24 X = x(A, ph, f, numpy.arange(0, duration*1000.0, binwidth)) 25 spect = analysis.simple_frequency_spectrum(X) 26 samples = numpy.array([0, 1, 2, 3])*int(f*duration) 27 components = spect[samples] 28 #print f, duration, binwidth, components 29 assert numpy.all(abs(abs(A) - components) < 1e-13), abs(A)-components 30 return True 11 12 class AnalysisTest(unittest.TestCase): 13 14 def testSimpleFrequencySpectrum(self): 15 16 ph = [0.5*pi, 1.2*pi, 0.3*pi] 17 x = lambda A, ph, f, t: A[0] + A[1]*sin(2*pi*f/1000.0*t + ph[0]) + A[2]*sin(2*pi*2*f/1000.0*t + ph[1]) + A[3]*sin(2*pi*3*f/1000.0*t + ph[2]) 18 durations = [10.0, 10.0, 30.0] 19 binwidths = [1.0, 5.0, 10.0] 20 21 for i in range(10): 22 A = numpy.random.uniform(-10, 10, size=4) 23 #print A 24 for f in 0.5, 1.0, 2.0, 10.0, 99.0: # cycles/sec 25 for duration, binwidth in zip(durations, binwidths): 26 if 3*f < 1000.0/binwidth: # binwidth limits the max frequency 27 X = x(A, ph, f, numpy.arange(0, duration*1000.0, binwidth)) 28 spect = analysis.simple_frequency_spectrum(X) 29 samples = numpy.array([0, 1, 2, 3])*int(f*duration) 30 components = spect[samples] 31 #print f, duration, binwidth, components 32 assert numpy.all(abs(abs(A) - components) < 1e-13), abs(A)-components 33 34 def testCCF(self): 35 a=numpy.arange(1000) 36 z=analysis.ccf(a,a) 37 assert z[len(z)/2] == 1 38 39 if __name__ == "__main__": 40 unittest.main()

