Skip to content

Commit

Permalink
feat: python3 support
Browse files Browse the repository at this point in the history
  • Loading branch information
felixpatzelt committed Jan 7, 2019
1 parent 1a4e3a9 commit fa6934d
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 3,185 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
Changelog
=========

:Version: 1.0.1 of 2019-01-07

Add Python 3 support.

:Version: 1.0.0 of 2017-09-29

Some minor documentation and metadata changes. This version is included with
Expand Down
3,204 changes: 48 additions & 3,156 deletions examples/scorr_periodic_vs_aperiodic_vs_unbiased.ipynb

Large diffs are not rendered by default.

13 changes: 7 additions & 6 deletions scorr/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from corr2 import *
from corr3 import *
from .corr2 import *
from .corr3 import *
import imp

def __reload_submodules__():
import helpers
reload(helpers)
reload(corr2)
reload(corr3)
from . import helpers
imp.reload(helpers)
imp.reload(corr2)
imp.reload(corr3)
16 changes: 8 additions & 8 deletions scorr/corr2.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
except ImportError:
from logging import getLogger

from helpers import is_number_like, is_string_like, get_nfft
from .helpers import is_number_like, is_string_like, get_nfft

# Helpers
# ===========================================================================
Expand Down Expand Up @@ -41,15 +41,15 @@ def corr_mat(x, maxlag=None):
def xcorrshift(x, maxlag=None, as_pandas=False):
"""Return shifted (cross- / auto) correlation to center lag zero."""
if not maxlag:
maxlag = len(x) / 2
maxlag = len(x) // 2
# force pandas output?
if as_pandas and not hasattr(x, 'iloc'):
if len(np.shape(x)) > 1:
x = pd.DataFrame(x)
else:
x = pd.Series(x)
# slice
ix = np.arange(-maxlag,maxlag+1)
ix = np.arange(-maxlag, maxlag+1, dtype=int)
if hasattr(x, 'iloc'):
xs = x.iloc[ix]
xs.index = ix
Expand Down Expand Up @@ -112,7 +112,7 @@ def xcorr(
Time series to analyse.
norm: [optional]
How to normalise the result
"corr": Return correlation, i.e. r \in [-1, 1] (default).
"corr": Return correlation, i.e. r \\in [-1, 1] (default).
"cov": Return covariance. E.g. the peak of an autocorrelation
will have the height var(x) = var(y)
int, float:
Expand Down Expand Up @@ -302,7 +302,7 @@ def xcorr_grouped_df(

# determine fft segment size
nfft, events_required = get_nfft(nfft, g)
maxlag = int(min(nfft/2, events_required))
maxlag = int(min(nfft//2, events_required))

# allocate
acd = np.zeros((2*maxlag, len(g)))
Expand Down Expand Up @@ -385,7 +385,7 @@ def xcorr_grouped_df(

# done
if return_df:
lag = pd.Index(range(-maxlag,maxlag+1), name='lag')
lag = pd.Index(list(range(-maxlag,maxlag+1)), name='lag')
return pd.DataFrame({
'xcorr': xcorrshift(acdm, maxlag),
'xcorr_std': xcorrshift(acde, maxlag),
Expand Down Expand Up @@ -461,7 +461,7 @@ def acorr_grouped_df(

# determine fft segment size
nfft, events_required = get_nfft(nfft, g)
maxlag = int(min(nfft/2, events_required))
maxlag = int(min(nfft//2, events_required))

# allocate
acd = np.zeros((maxlag + 1, len(g)))
Expand Down Expand Up @@ -532,7 +532,7 @@ def acorr_grouped_df(

# done
if return_df:
lag = pd.Index(range(maxlag+1), name='lag')
lag = pd.Index(list(range(maxlag+1)), name='lag')
return pd.DataFrame({
'acorr': acdm,
'acorr_std': acde,
Expand Down
8 changes: 4 additions & 4 deletions scorr/corr3.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
except ImportError:
from logging import getLogger

from helpers import is_number_like, is_string_like, get_nfft
from .helpers import is_number_like, is_string_like, get_nfft

def fft2x(x, y, z, nfft=None):
"""Return Bi-cross-spectrum."""
Expand Down Expand Up @@ -49,11 +49,11 @@ def fft2x(x, y, z, nfft=None):
## create indices aligned with fftpack's fft2 quadrants
lm = nfft / 2.
i0 = np.roll(
np.arange(int(np.ceil(lm-1)), int(-lm-1), -1),
np.arange(int(np.ceil(lm-1)), int(-lm-1), -1, dtype=int),
int(np.floor(lm)) + 1
)
i = i0 * np.ones((nfft, nfft), dtype=int)
j0 = np.arange(0, -nfft, -1)
j0 = np.arange(0, -nfft, -1, dtype=int)
j = hankel(j0, np.roll(j0,1))

# B
Expand All @@ -79,7 +79,7 @@ def padded_x3corr_norm(nfft, pad=0, segments=1, debias=True):
with two-point xcorr defaults.
"""
ndat = nfft - pad
nmid = min(ndat, nfft/2) # symmetry axis
nmid = min(ndat, nfft//2) # symmetry axis
nemp = -min(0, ndat-pad) # completely empty
w3 = np.ones((nfft,nfft))

Expand Down
2 changes: 1 addition & 1 deletion scorr/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def get_nfft(nfft, g):
# largest power of two < n
nfft = int(2**np.floor(np.log2(n)))
else:
raise ValueError, "Can't understand nfft='%s'" % nfft
raise ValueError("Can't understand nfft='%s'" % nfft)
else:
nfft = int(nfft)
events_required = int(np.ceil(nfft / 2.))
Expand Down
8 changes: 4 additions & 4 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

setup(
name='scorr',
version='1.0.0',
version='1.0.1',
description=(
'Fast and flexible two- and three-point correlation analysis '
'for time series using spectral methods.'
Expand All @@ -22,8 +22,8 @@
'License :: OSI Approved :: MIT License',
'Programming Language :: Python :: 2 :: Only',
'Programming Language :: Python :: 2.7',
#'Programming Language :: Python :: 3',
#'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.6',
'Topic :: Scientific/Engineering',
'Topic :: Software Development :: Libraries',
'Topic :: Software Development :: Libraries :: Python Modules'
Expand All @@ -34,7 +34,7 @@
],
url='http://github.com/felixpatzelt/scorr',
download_url=(
'https://github.com/felixpatzelt/scorr/archive/1.0.0.tar.gz'
'https://github.com/felixpatzelt/scorr/archive/1.0.1.tar.gz'
),
author='Felix Patzelt',
author_email='[email protected]',
Expand Down
12 changes: 6 additions & 6 deletions tests/test_corr2.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ def test_acorr_grouped_df(self):
rdf = corr2.acorr_grouped_df(df, nfft='auto pad')
self.assertTrue(
(
abs(r[:self.dlen/2+1] - rdf['acorr'].loc[0:self.dlen/2])
< 3*rdf['acorr_std'].loc[0:self.dlen/2]
abs(r[:self.dlen//2+1] - rdf['acorr'].loc[0:self.dlen//2])
< 3*rdf['acorr_std'].loc[0:self.dlen//2]
).all()
)

Expand All @@ -123,14 +123,14 @@ def test_xcorr_grouped_df(self):
# positive lags
self.assertTrue(
(
abs(r[:self.dlen/2+1] - rdf['xcorr'].loc[0:self.dlen/2])
< 3*rdf['xcorr_std'].loc[0:self.dlen/2]
abs(r[:self.dlen//2+1] - rdf['xcorr'].loc[0:self.dlen//2])
< 3*rdf['xcorr_std'].loc[0:self.dlen//2]
).all()
)
# negative lags
self.assertTrue(
(
abs(r[-self.dlen/2-1:] - rdf['xcorr'].loc[-self.dlen/2:0])
< 3*rdf['xcorr_std'].loc[-self.dlen/2:0]
abs(r[-self.dlen//2-1:] - rdf['xcorr'].loc[-self.dlen//2:0])
< 3*rdf['xcorr_std'].loc[-self.dlen//2:0]
).all()
)

0 comments on commit fa6934d

Please sign in to comment.