Changeset 321

Show
Ignore:
Timestamp:
11/12/08 10:31:01 (2 months ago)
Author:
pierre
Message:

Add some basic tests for the analysis module, and fix the docstrings. Nevertheless, this module is still quite empty...

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/src/analysis.py

    r311 r321  
    1 #!/usr/bin/env python 
    21# -*- coding: utf8 -*- 
    32""" 
    4 analysis.py: 
     3NeuroTools.analysis 
     4================== 
    55 
    6 $Id$ 
     6A collection of analysis functions that may be used by NeuroTools.signals or other packages. 
     7 
     8Classes 
     9------- 
     10 
     11TuningCurve - A tuning curve object (not very documented) 
     12 
     13 
     14Functions 
     15--------- 
     16 
     17ccf                       - fast cross correlation function based on fft 
     18simple_frequency_spectrum - Simple frequencxy spectrum 
     19arrays_almost_equal       - comparison of two arrays 
    720""" 
    821 
     
    1528 
    1629def 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 from 
    19         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. 
    2235         
    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 
    3146    """ 
    3247    assert(x.ndim == y.ndim, "Inconsistent shape !") 
  • trunk/src/signals.py

    r320 r321  
    115115        # several    : t_start = min(time), t_stop = max(time) 
    116116         
    117         size = len(spike_times) 
     117        size = len(self.spike_times) 
    118118        if size == 0: 
    119119            if self.t_start is None:  
     
    12161216        See also: 
    12171217            cv_isi_hist, cv_local, cv_isi, SpikeTrain.cv_kl 
    1218              
    12191218        """ 
    12201219        ids = self.id_list() 
  • trunk/test/test_analysis.py

    r245 r321  
    44 
    55from NeuroTools import analysis 
    6 import numpy 
     6import numpy, unittest 
    77from numpy import pi, sin 
    88 
    99# test simple_frequency_spectrum 
    1010 
    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 
     12class 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 
     39if __name__ == "__main__": 
     40    unittest.main()