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
|
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
Compare to numpy data.
"""
import numpy as np
from os.path import abspath, dirname, join
import sys
sys.path.insert(0, dirname(dirname(abspath(__file__))))
import multipletau
from test_correlate import get_sample_arrays_cplx
def test_corresponds_ac():
myframe = sys._getframe()
myname = myframe.f_code.co_name
print("running ", myname)
a = np.concatenate(get_sample_arrays_cplx()).real
m=16
restau = multipletau.autocorrelate(a=1*a,
m=m,
copy=True,
normalize=True,
dtype=np.float_)
reslin = multipletau.correlate_numpy(a=1*a,
v=1*a,
copy=True,
normalize=True,
dtype=np.float_)
idx = np.array(restau[:,0].real, dtype=int)[:m]
assert np.allclose(reslin[idx, 1], restau[:m,1])
def test_corresponds_ac_first_loop():
"""
numpy correlation:
G_m = sum_i(a_i*a_{i+m})
multipletau correlation 2nd order:
b_j = (a_{2i} + a_{2i+1} / 2)
G_m = sum_j(b_j*b_{j+1})
= 1/4*sum_i(a_{2i} * a_{2i+m} +
a_{2i} * a_{2i+m+1} +
a_{2i+1} * a_{2i+m} +
a_{2i+1} * a_{2i+m+1}
)
The values after the first m+1 lag times in the multipletau
correlation differ from the normal correlation, because the
traces are averaged over two consecutive items, effectively
halving the size of the trace. The multiple-tau correlation
can be compared to the regular correlation by using an even
sized sequence (here 222) in which the elements 2i and 2i+1
are equal, as is done in this test.
"""
myframe = sys._getframe()
myname = myframe.f_code.co_name
print("running ", myname)
a = [ arr / np.average(arr) for arr in get_sample_arrays_cplx() ]
a = np.concatenate(a)[:222]
# two consecutive elements are the same, so the multiple-tau method
# corresponds to the numpy correlation for the first loop.
a[::2] = a[1::2]
for m in [2,4,6,8,10,12,14,16]:
restau = multipletau.correlate(a=a,
v=a.imag+1j*a.real,
m=m,
copy=True,
normalize=False,
dtype=np.complex_)
reslin = multipletau.correlate_numpy(a=a,
v=a.imag+1j*a.real,
copy=True,
normalize=False,
dtype=np.complex_)
idtau = np.where(restau[:,0]==m+2)[0][0]
tau3 = restau[idtau, 1] #m+1 initial bins
idref = np.where(reslin[:,0]==m+2)[0][0]
tau3ref = reslin[idref, 1]
assert np.allclose(tau3, tau3ref)
def test_corresponds_ac_nonormalize():
myframe = sys._getframe()
myname = myframe.f_code.co_name
print("running ", myname)
a = np.concatenate(get_sample_arrays_cplx()).real
m=16
restau = multipletau.autocorrelate(a=1*a,
m=m,
copy=True,
normalize=False,
dtype=np.float_)
reslin = multipletau.correlate_numpy(a=1*a,
v=1*a,
copy=True,
normalize=False,
dtype=np.float_)
idx = np.array(restau[:,0].real, dtype=int)[:m+1]
assert np.allclose(reslin[idx, 1], restau[:m+1,1])
def test_corresponds_cc():
myframe = sys._getframe()
myname = myframe.f_code.co_name
print("running ", myname)
a = np.concatenate(get_sample_arrays_cplx())
m=16
restau = multipletau.correlate(a=a,
v=a.imag+1j*a.real,
m=m,
copy=True,
normalize=True,
dtype=np.complex_)
reslin = multipletau.correlate_numpy(a=a,
v=a.imag+1j*a.real,
copy=True,
normalize=True,
dtype=np.complex_)
idx = np.array(restau[:,0].real, dtype=int)[:m+1]
assert np.allclose(reslin[idx, 1], restau[:m+1,1])
def test_corresponds_cc_nonormalize():
myframe = sys._getframe()
myname = myframe.f_code.co_name
print("running ", myname)
a = np.concatenate(get_sample_arrays_cplx())
m=16
restau = multipletau.correlate(a=a,
v=a.imag+1j*a.real,
m=m,
copy=True,
normalize=False,
dtype=np.complex_)
reslin = multipletau.correlate_numpy(a=a,
v=a.imag+1j*a.real,
copy=True,
normalize=False,
dtype=np.complex_)
idx = np.array(restau[:,0].real, dtype=int)[:m+1]
assert np.allclose(reslin[idx, 1], restau[:m+1,1])
if __name__ == "__main__":
# Run all tests
loc = locals()
for key in list(loc.keys()):
if key.startswith("test_") and hasattr(loc[key], "__call__"):
loc[key]()
|