Skip to content

Commit

Permalink
Merge pull request #12 from danielballan/remove-unneeded-copy
Browse files Browse the repository at this point in the history
Remove unneeded copying.
  • Loading branch information
danielballan authored Feb 7, 2020
2 parents dd47b01 + 8b00512 commit cc939bb
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
10 changes: 5 additions & 5 deletions autocorr/multitau.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ def even(x):
return x if x % 2 == 0 else x - 1

# 1-D data hack
if len(signal.shape) == 1:
if signal.ndim == 1:
N = even(signal.shape[0])
a = np.array(signal[np.newaxis, :N], copy=True)
elif len(signal.shape) == 2:
a = signal[np.newaxis, :N]
elif signal.ndim == 2:
# copy data a local array
N = even(signal.shape[1])
a = np.array(signal[:, :N], copy=True)
elif len(signal.shape) > 2:
a = signal[:, :N]
elif signal.ndim > 2:
raise ValueError('Flatten the [2,3,..] dimensions before passing to autocorrelate.')

if N < lags_per_level:
Expand Down
10 changes: 10 additions & 0 deletions autocorr/tests/test_multitau.py
Original file line number Diff line number Diff line change
@@ -1,36 +1,46 @@
import numpy as np
from numpy.testing import assert_array_equal
import autocorr
import time

N = 10240
np.random.seed(0)
t = np.arange(N)
A = np.exp(-0.05 * t)[:, np.newaxis] + np.random.rand(N, 24) * 0.1
original = A.copy()


def test_multitau():
t0 = time.time()
g1, tau = autocorr.multitau(A.T, 16)
t1 = time.time()
# Check that the input array has not been mutated.
assert_array_equal(A, original)
print('pure python time = %f' % (t1 - t0))


def test_multitau_mt():
t0 = time.time()
g1, tau = autocorr.multitau_mt(A.T, 16)
t1 = time.time()
# Check that the input array has not been mutated.
assert_array_equal(A, original)
print('accelerated version = %f' % (t1 - t0))


def test_fftautocorr():
t0 = time.time()
g2, tau = autocorr.fftautocorr(A.T)
t1 = time.time()
# Check that the input array has not been mutated.
assert_array_equal(A, original)
print('fft version = %f' % (t1 - t0))


def test_cfftautocorr():
t0 = time.time()
g2, tau = autocorr.fftautocorr_mt(A.T)
t1 = time.time()
# Check that the input array has not been mutated.
assert_array_equal(A, original)
print('fft version = %f' % (t1 - t0))

0 comments on commit cc939bb

Please sign in to comment.