Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge branch 'py3k'

  • Loading branch information...
commit 057d9895469dd25ce4dd59454866add8b18deeec 2 parents 93aa17f + 63665b7
@kwgoodman authored
View
4 README.rst
@@ -122,9 +122,9 @@ After you have installed ``la``, run the suite of unit tests::
>>> import la
>>> la.test()
<snip>
- Ran 3020 tests in 9.225s
+ Ran 3012 tests in 9.225s
OK
- <nose.result.TextTestResult run=3020 errors=0 failures=0>
+ <nose.result.TextTestResult run=3012 errors=0 failures=0>
The ``la`` package contains C extensions that speed up common alignment
operations such as adding two unaligned larrys. If the C extensions don't
View
4 la/__init__.py
@@ -1,5 +1,5 @@
"la init"
-
+from __future__ import print_function
# Classes
from la.deflarry import larry
@@ -22,7 +22,7 @@
test = Tester().test
del Tester
except (ImportError, ValueError):
- print "No la unit testing available."
+ print("No la unit testing available.")
try:
# Namespace cleaning
View
6 la/data/yahoo.py
@@ -1,4 +1,4 @@
-
+from __future__ import print_function
import datetime
import numpy as np
import la
@@ -110,10 +110,10 @@ def quotes(tickers, date1=None, date2=None, adjust=True, verbose=False):
lar = None
items = ['open', 'close', 'high', 'low', 'volume', 'adjclose']
if verbose:
- print "Load data"
+ print("Load data")
for ticker in tickers:
if verbose:
- print "\t" + ticker
+ print("\t" + ticker)
data, dates = quotes_historical_yahoo(ticker, date1, date2)
data = np.array(data).T
qlar = la.larry(data, [items, dates])
View
310 la/deflarry.py
@@ -101,7 +101,7 @@ def __init__(self, x, label=None, dtype=None, validate=True):
x = np.asarray(x, dtype=dtype)
except:
msg = "x must be array_like and dtype must be known."
- raise ValueError, msg
+ raise ValueError(msg)
elif dtype != None:
x = x.astype(dtype)
if label is None:
@@ -109,17 +109,17 @@ def __init__(self, x, label=None, dtype=None, validate=True):
if validate:
ndim = x.ndim
if ndim != len(label):
- raise ValueError, 'Exactly one label per dimension needed'
+ raise ValueError('Exactly one label per dimension needed')
if ndim == 0:
# A 0d larry can be created if you comment out this ndim == 0
# test. The reason that 0d is not allowed is that not all
# methods support 0d larrys
- raise ValueError, '0d larrys are not supported'
+ raise ValueError('0d larrys are not supported')
for i, l in enumerate(label):
nlabel = len(l)
if x.shape[i] != nlabel:
msg = 'Length mismatch in label and x along axis %d'
- raise ValueError, msg % i
+ raise ValueError(msg % i)
if len(frozenset(l)) != nlabel:
# We have duplicates in the label, give an example
count = {}
@@ -130,9 +130,9 @@ def __init__(self, x, label=None, dtype=None, validate=True):
break
msg = "Elements of label not unique along axis %d. "
msg += "There are %d labels named `%s`."
- raise ValueError, msg % (i, value, key)
+ raise ValueError(msg % (i, value, key))
if type(l) is not list:
- raise ValueError, 'label must be a list of lists'
+ raise ValueError('label must be a list of lists')
self.x = x
self.label = label
@@ -345,7 +345,7 @@ def cumsum(self, axis):
"""
if axis == None:
- raise ValueError, 'axis cannot be None'
+ raise ValueError('axis cannot be None')
y = self.copy()
idx = np.isnan(y.x)
np.putmask(y.x, idx, 0)
@@ -411,7 +411,7 @@ def cumprod(self, axis):
"""
if axis == None:
- raise ValueError, 'axis cannot be None'
+ raise ValueError('axis cannot be None')
y = self.copy()
idx = np.isnan(y.x)
np.putmask(y.x, idx, 1)
@@ -455,7 +455,7 @@ def clip(self, lo, hi):
"""
if lo > hi:
- raise ValueError, 'lo should be less than or equal to hi'
+ raise ValueError('lo should be less than or equal to hi')
y = self.copy()
y.x.clip(lo, hi, y.x)
return y
@@ -631,8 +631,8 @@ def invert(self):
array([False, True], dtype=bool)
"""
- if self.dtype != bool:
- raise TypeError, 'Only larrys with bool dtype can be inverted.'
+ if self.dtype.type is not np.bool_:
+ raise TypeError('Only larrys with bool dtype can be inverted.')
return larry(~self.x, self.copylabel(), validate=False)
# Binary Functions -------------------------------------------------------
@@ -684,7 +684,7 @@ def __add__(self, other):
x = self.x + other
label = self.copylabel()
return larry(x, label, validate=False)
- raise TypeError, 'Input must be scalar, array, or larry.'
+ raise TypeError('Input must be scalar, array, or larry.')
__radd__ = __add__
@@ -729,7 +729,7 @@ def __sub__(self, other):
x = self.x - other
label = self.copylabel()
return larry(x, label, validate=False)
- raise TypeError, 'Input must be scalar, array, or larry.'
+ raise TypeError('Input must be scalar, array, or larry.')
def __rsub__(self, other):
"Right subtract a larry with a another larry, Numpy array, or scalar."
@@ -778,19 +778,133 @@ def __div__(self, other):
x = self.x / other
label = self.copylabel()
return larry(x, label, validate=False)
- raise TypeError, 'Input must be scalar, array, or larry.'
+ raise TypeError('Input must be scalar, array, or larry.')
def __rdiv__(self, other):
"Right divide a larry with a another larry, Numpy array, or scalar."
if isinstance(other, larry):
msg = 'I could not come up with a problem that used this code '
msg += 'so I removed it. Send me your example and I will fix.'
- raise RuntimeError, msg
+ raise RuntimeError(msg)
if np.isscalar(other) or isinstance(other, np.ndarray):
label = self.copylabel()
x = other / self.x
return larry(x, label, validate=False)
- raise TypeError, 'Input must be scalar, array, or larry.'
+ raise TypeError('Input must be scalar, array, or larry.')
+
+ def __truediv__(self, other):
+ """
+ True divide a larry with another larry, Numpy array, or scalar.
+
+ If two larrys are divided then the larrys are joined with an inner
+ join (i.e., the intersection of the labels).
+
+ See Also
+ --------
+ la.divide: divide two larrys element-wise using given join method.
+
+ Examples
+ --------
+
+ >>> larry([1.0, 2.0]) / larry([2.0, 3.0])
+ label_0
+ 0
+ 1
+ x
+ array([ 0.5 , 0.66666667])
+
+ >>> y1 = larry([1,2], [['a', 'b']])
+ >>> y2 = larry([1,2], [['b', 'c']])
+ >>> y1 / y2
+ label_0
+ b
+ x
+ array([2.])
+
+ """
+ if isinstance(other, larry):
+ if self.label == other.label:
+ x = self.x / other.x
+ label = self.copylabel()
+ return larry(x, label, validate=False)
+ else:
+ x, y, label = self.__align(other)
+ x = x / y
+ return larry(x, label, validate=False)
+ if np.isscalar(other) or isinstance(other, np.ndarray):
+ x = self.x / other
+ label = self.copylabel()
+ return larry(x, label, validate=False)
+ raise TypeError('Input must be scalar, array, or larry.')
+
+ def __floordiv__(self, other):
+ """
+ Floor divide a larry with another larry, Numpy array, or scalar.
+
+ If two larrys are divided then the larrys are joined with an inner
+ join (i.e., the intersection of the labels).
+
+ See Also
+ --------
+ la.divide: divide two larrys element-wise using given join method.
+
+ Examples
+ --------
+
+ >>> larry([1.0, 2.0]) // larry([2.0, 3.0])
+ label_0
+ 0
+ 1
+ x
+ array([ 0., 0.])
+
+ >>> y1 = larry([1,2], [['a', 'b']])
+ >>> y2 = larry([1,2], [['b', 'c']])
+ >>> y1 // y2
+ label_0
+ b
+ x
+ array([2])
+
+ """
+ if isinstance(other, larry):
+ if self.label == other.label:
+ x = self.x // other.x
+ label = self.copylabel()
+ return larry(x, label, validate=False)
+ else:
+ x, y, label = self.__align(other)
+ x = x // y
+ return larry(x, label, validate=False)
+ if np.isscalar(other) or isinstance(other, np.ndarray):
+ x = self.x // other
+ label = self.copylabel()
+ return larry(x, label, validate=False)
+ raise TypeError('Input must be scalar, array, or larry.')
+
+ def __rtruediv__(self, other):
+ "Right true divide a larry with a another larry, Numpy array, or scalar."
+ if isinstance(other, larry):
+ msg = 'I could not come up with a problem that used this code '
+ msg += 'so I removed it. Send me your example and I will fix.'
+ raise RuntimeError(msg)
+ if np.isscalar(other) or isinstance(other, np.ndarray):
+ label = self.copylabel()
+ x = other / self.x
+ return larry(x, label, validate=False)
+ raise TypeError('Input must be scalar, array, or larry.')
+
+ def __rfloordiv__(self, other):
+ "Right floor divide a larry with a another larry, Numpy array, or scalar."
+ if isinstance(other, larry):
+ msg = 'I could not come up with a problem that used this code '
+ msg += 'so I removed it. Send me your example and I will fix.'
+ raise RuntimeError(msg)
+ if np.isscalar(other) or isinstance(other, np.ndarray):
+ label = self.copylabel()
+ x = other // self.x
+ return larry(x, label, validate=False)
+ raise TypeError('Input must be scalar, array, or larry.')
def __mul__(self, other):
"""
@@ -834,7 +948,7 @@ def __mul__(self, other):
x = self.x * other
label = self.copylabel()
return larry(x, label, validate=False)
- raise TypeError, 'Input must be scalar, array, or larry.'
+ raise TypeError('Input must be scalar, array, or larry.')
__rmul__ = __mul__
@@ -878,7 +992,7 @@ def __and__(self, other):
x = np.logical_and(self.x, other)
label = self.copylabel()
return larry(x, label, validate=False)
- raise TypeError, 'Input must be scalar, array, or larry.'
+ raise TypeError('Input must be scalar, array, or larry.')
__rand__ = __and__
@@ -922,7 +1036,7 @@ def __or__(self, other):
x = np.logical_or(self.x, other)
label = self.copylabel()
return larry(x, label, validate=False)
- raise TypeError, 'Input must be scalar, array, or larry.'
+ raise TypeError('Input must be scalar, array, or larry.')
__ror__ = __or__
@@ -930,7 +1044,7 @@ def __align(self, other):
"Align larrys for binary operations."
if self.ndim != other.ndim:
msg = 'Binary operation on two larrys with different dimension'
- raise IndexError, msg
+ raise IndexError(msg)
label = []
x = self.x
y = other.x
@@ -1370,7 +1484,7 @@ def __reduce(self, op, default=np.nan, **kwargs):
elif axis is None:
return op(self.x, **kwargs)
else:
- raise ValueError, 'axis should be an integer or None'
+ raise ValueError('axis should be an integer or None')
def any(self, axis=None):
"""
@@ -1579,7 +1693,7 @@ def __compare(self, other, op):
elif op == '>=':
x = self.x >= other
else:
- raise ValueError, 'Unknown comparison operator'
+ raise ValueError('Unknown comparison operator')
if isinstance(x, np.ndarray):
y = larry(x, self.copylabel(), validate=False)
else:
@@ -1600,10 +1714,10 @@ def __compare(self, other, op):
elif op == '>=':
x = x >= y
else:
- raise ValueError, 'Unknown comparison operator'
+ raise ValueError('Unknown comparison operator')
return larry(x, label, validate=False)
else:
- raise TypeError, 'Input must be scalar, numpy array, or larry.'
+ raise TypeError('Input must be scalar, numpy array, or larry.')
# Get and set ------------------------------------------------------------
@@ -1637,7 +1751,7 @@ def __getitem__(self, index):
if isscalar(index):
index = int(index)
if index >= self.shape[0]:
- raise IndexError, 'index out of range'
+ raise IndexError('index out of range')
x = self.x[index]
if self.ndim <= 1:
return x
@@ -1653,31 +1767,31 @@ def __getitem__(self, index):
typ = type(idx)
if isscalar(idx):
if idx >= self.shape[ax]:
- raise IndexError, 'index out of range'
+ raise IndexError('index out of range')
lab = None
elif typ is list or typ is tuple:
try:
lab = [self.label[ax][z] for z in idx]
except IndexError:
- raise IndexError, 'index out of range'
+ raise IndexError('index out of range')
allscalar = False
validate = True
elif typ is np.ndarray:
if idx.ndim != 1:
msg = 'You can use a Numpy array for indexing, '
msg += 'but it must be 1d.'
- raise IndexError, msg
+ raise IndexError(msg)
if idx.dtype.type == np.bool_:
try:
lab = [self.label[ax][j] for j, z in
enumerate(idx) if z]
except IndexError:
- raise IndexError, 'index out of range'
+ raise IndexError('index out of range')
else:
try:
lab = [self.label[ax][z] for z in idx]
except IndexError:
- raise IndexError, 'index out of range'
+ raise IndexError('index out of range')
validate = True
allscalar = False
elif typ is slice:
@@ -1694,7 +1808,7 @@ def __getitem__(self, index):
lab = [self.label[ax][j] for j, z in
enumerate(idx.x) if z]
except IndexError:
- raise IndexError, 'index out of range'
+ raise IndexError('index out of range')
index = list(index)
index[ax] = index[ax].x
index = tuple(index)
@@ -1702,7 +1816,7 @@ def __getitem__(self, index):
validate = True
else:
msg = 'I do not recognize the way you are indexing'
- raise IndexError, msg
+ raise IndexError(msg)
else:
lab = self.label[ax]
if lab is not None:
@@ -1728,19 +1842,19 @@ def __getitem__(self, index):
if index.ndim != 1:
msg = 'You can use a Numpy array for indexing, '
msg += 'but it must be 1d.'
- raise IndexError, msg
+ raise IndexError(msg)
if index.dtype.type == np.bool_:
try:
lab = [self.label[0][j] for j, z in
enumerate(index) if z]
except IndexError:
- raise IndexError, 'index out of range'
+ raise IndexError('index out of range')
x = self.x[index]
else:
try:
lab = [self.label[0][z] for z in index]
except IndexError:
- raise IndexError, 'index out of range'
+ raise IndexError('index out of range')
if len(set(lab)) != len(lab):
raise IndexError("Duplicate labels along axis 0.")
x = self.x.take(index, axis=0)
@@ -1757,7 +1871,7 @@ def __getitem__(self, index):
lab = [self.label[0][j] for j, z in
enumerate(index) if z]
except IndexError:
- raise IndexError, 'index out of range'
+ raise IndexError('index out of range')
label = self.copylabel()
label[0] = lab
x = self.x[index.x]
@@ -1992,7 +2106,7 @@ def __setitem__(self, index, value):
# Could use morph to do this, if every row and column of self
# is in index, but I think it is better to raise an IndexError
msg = 'Indexing with a larry that is not aligned'
- raise IndexError, msg
+ raise IndexError(msg)
else:
if isinstance(value, larry):
# TODO The line below (self[index].label) is slow. Need a
@@ -2001,7 +2115,7 @@ def __setitem__(self, index, value):
if self[index].label == value.label:
self.x[index] = value.x
else:
- raise IndexError, 'larrys are not aligned.'
+ raise IndexError('larrys are not aligned.')
else:
self.x[index] = value
@@ -2046,7 +2160,7 @@ def set(self, label, value):
"""
if len(label) != self.ndim:
- raise ValueError, 'Must have exactly one label per dimension'
+ raise ValueError('Must have exactly one label per dimension')
index = []
for i in xrange(self.ndim):
index.append(self.labelindex(label[i], axis=i))
@@ -2083,7 +2197,7 @@ def get(self, label):
"""
if len(label) != self.ndim:
- raise ValueError, 'Must have exactly one label per dimension'
+ raise ValueError('Must have exactly one label per dimension')
index = []
for i in xrange(self.ndim):
index.append(self.labelindex(label[i], axis=i))
@@ -2178,7 +2292,7 @@ def getlabel(self, axis, copy=True):
"""
if axis >= self.ndim:
- raise IndexError, 'axis out of range'
+ raise IndexError('axis out of range')
label = self.label[axis]
if copy:
label = list(label)
@@ -2236,7 +2350,7 @@ def pull(self, name, axis):
"""
if axis is None:
- raise ValueError, 'axis cannot be None'
+ raise ValueError('axis cannot be None')
label = list(self.label)
label.pop(axis)
idx = self.labelindex(name, axis)
@@ -2321,15 +2435,37 @@ def keep_label(self, op, value, axis):
"""
ops = ('==', '>', '<', '>=', '<=', '!=', 'in', 'not in')
if op not in ops:
- raise ValueError, 'Unknown op'
+ raise ValueError('Unknown op')
if axis is None:
- raise ValueError, 'axis cannot be None'
+ raise ValueError('axis cannot be None')
if axis >= self.ndim:
- raise IndexError, 'axis is out of range'
- y = self.copy()
- cmd = '[(idx, z) for idx, z in enumerate(y.label[axis]) if z '
- cmd = cmd + op + ' value]'
- idxlabel = eval(cmd)
+ raise IndexError('axis is out of range')
+ y = self.copy()
+
+ if op == '==':
+ idxlabel = [(idx, z) for idx, z in enumerate(y.label[axis])
+ if z == value]
+ elif op == '>':
+ idxlabel = [(idx, z) for idx, z in enumerate(y.label[axis])
+ if z > value]
+ elif op == '<':
+ idxlabel = [(idx, z) for idx, z in enumerate(y.label[axis])
+ if z < value]
+ elif op == '>=':
+ idxlabel = [(idx, z) for idx, z in enumerate(y.label[axis])
+ if z >= value]
+ elif op == '<=':
+ idxlabel = [(idx, z) for idx, z in enumerate(y.label[axis])
+ if z <= value]
+ elif op == '!=':
+ idxlabel = [(idx, z) for idx, z in enumerate(y.label[axis])
+ if z != value]
+ elif op == 'in':
+ idxlabel = [(idx, z) for idx, z in enumerate(y.label[axis])
+ if z in value]
+ elif op == 'not in':
+ idxlabel = [(idx, z) for idx, z in enumerate(y.label[axis])
+ if z not in value]
if len(idxlabel) == 0:
return larry([])
else:
@@ -2380,10 +2516,10 @@ def keep_x(self, op, value, vacuum=True):
"""
if (vacuum == True) and (self.ndim != 2):
- raise ValueError, 'When vacuum is True, larry must be 2d'
+ raise ValueError('When vacuum is True, larry must be 2d')
ops = ('==', '>', '<', '>=', '<=', '!=')
if op not in ops:
- raise ValueError, 'Unknown op'
+ raise ValueError('Unknown op')
y = self.copy()
idx = eval('y.x ' + op + ' value')
y.x[~idx] = np.nan
@@ -2486,19 +2622,19 @@ def labelindex(self, name, axis, exact=True):
1
"""
- if axis >= self.ndim:
- raise IndexError, 'axis out of range'
if axis is None:
- raise ValueError, 'axis cannot be None'
+ raise ValueError('axis cannot be None')
+ if axis >= self.ndim:
+ raise IndexError('axis out of range')
try:
index = self.label[axis].index(name)
except ValueError:
if exact:
- raise IndexError, 'name not in label along axis %d' % axis
+ raise IndexError('name not in label along axis %d' % axis)
else:
idx = [i for i, z in enumerate(self.label[axis]) if z <= name]
if len(idx) == 0:
- raise IndexError, 'name not in label along axis %d' % axis
+ raise IndexError('name not in label along axis %d' % axis)
index = max(idx)
return index
@@ -3151,11 +3287,11 @@ def _group_align(self, group, axis=0):
be a subset of the row labels of the group.
"""
if not isinstance(group, larry):
- raise TypeError, 'group must be a larry'
+ raise TypeError('group must be a larry')
if group.ndim != 1:
- raise ValueError, 'group must be a 1d larry'
+ raise ValueError('group must be a 1d larry')
if len(frozenset(self.label[axis]) - frozenset(group.label[0])):
- raise IndexError, 'label is not a subset of group label'
+ raise IndexError('label is not a subset of group label')
g = group.morph(self.label[axis], 0)
g = g.x.tolist()
return g
@@ -3204,7 +3340,7 @@ def morph(self, label, axis):
"""
if axis >= self.ndim:
- raise IndexError, 'axis out of range'
+ raise IndexError('axis out of range')
if self.label[axis] == label:
return self.copy()
else:
@@ -3285,7 +3421,7 @@ def morph_like(self, lar):
"""
if self.ndim != lar.ndim:
- raise IndexError, 'larrys must be of the same dimension.'
+ raise IndexError('larrys must be of the same dimension.')
if self.ndim == 0:
y = self.copy()
else:
@@ -3337,7 +3473,7 @@ def merge(self, other, update=False):
ndim = self.ndim
if ndim != other.ndim:
- raise IndexError, 'larrys must be of the same dimension.'
+ raise IndexError('larrys must be of the same dimension.')
lar1 = self
lar2 = other
for ax in range(ndim):
@@ -3351,22 +3487,22 @@ def merge(self, other, update=False):
dtype1 = self.dtype
if dtype1 == object:
mask1 = lar1.x != [None]
- elif self.dtype.type == np.string_:
+ elif self.dtype.type == np.str_:
mask1 = lar1.x != ''
else:
mask1 = np.isfinite(lar1.x)
dtype2 = other.dtype
if dtype2 == object:
mask2 = lar2.x != [None]
- elif self.dtype.type == np.string_:
- mask2 = lar2.x != ''
+ elif self.dtype.type == np.str_:
+ mask2 = lar2.x != ''
else:
mask2 = np.isfinite(lar2.x)
# Trap cases that merge cannot handle
if dtype1 in (np.string_, object) or dtype2 in (np.string_, object):
if dtype1 != dtype2:
- raise TypeError, 'Incompatible dtypes'
+ raise TypeError('Incompatible dtypes')
# Check for overlap if requested
if (not update) and np.logical_and(mask1, mask2).any():
@@ -3466,7 +3602,7 @@ def lag(self, nlag, axis=-1):
"""
if axis is None:
- raise IndexError, 'axis cannot be None.'
+ raise IndexError('axis cannot be None.')
if nlag > 0:
y = self.copy()
y.label[axis] = y.label[axis][nlag:]
@@ -4258,7 +4394,7 @@ def unflatten(self):
# Check input
if self.ndim != 1:
- raise ValueError, 'Only 1d larrys can be unflattened.'
+ raise ValueError('Only 1d larrys can be unflattened.')
if self.shape == (0,):
return larry([])
@@ -4266,7 +4402,7 @@ def unflatten(self):
# Determine labels, shape, and index into array
if not isscalar(self.x.flat[0]):
msg = 'Only scalar dtype is currently supported.'
- raise NotImplementedError, msg
+ raise NotImplementedError(msg)
labels = zip(*self.label[0])
x, label = fromlists(self.x, labels)
return larry(x, label)
@@ -4320,7 +4456,7 @@ def insertaxis(self, axis, label):
"""
if axis is None:
- raise ValueError, "`axis` cannot be None."
+ raise ValueError("`axis` cannot be None.")
x = self.getx(copy=True)
x = np.expand_dims(x, axis)
lab = self.copylabel()
@@ -4751,7 +4887,7 @@ def tofile(self, file, delimiter=','):
if ndim not in (1, 2):
msg = "Only 1d and 2d larrys supported; "
msg +="try the IO function or tocsv method."
- raise ValueError, msg
+ raise ValueError(msg)
# Open file if needed
if type(file) == str:
@@ -4794,7 +4930,7 @@ def tofile(self, file, delimiter=','):
if opened:
f.close()
msg = "Please report this bug; the code should never reach here."
- raise RuntimeError, msg
+ raise RuntimeError(msg)
# Close file if opened (i.e., if file was a str)
if opened:
@@ -4895,7 +5031,7 @@ def __getitem__(self2, index):
elif 0 in y.shape:
msg = 'lix does not support shapes that contain 0 '
msg += 'such as (0,) and (2, 0 ,3).'
- raise ValueError, msg
+ raise ValueError(msg)
typ = type(index)
if typ == list:
# Example: lar.lix[['a', 'b', 'c']]
@@ -4915,7 +5051,7 @@ def __getitem__(self2, index):
elif len(index) < y.ndim:
index3 = list(index) + [slice(None)] * (y.ndim - len(index))
else:
- raise IndexError, 'Invalid index'
+ raise IndexError('Invalid index')
for ax, idx in enumerate(index3):
typ = type(idx)
if typ == list:
@@ -4933,7 +5069,7 @@ def __getitem__(self2, index):
elif isscalar(idx):
index2.append([idx])
else:
- raise IndexError, 'Unsupported indexing operation.'
+ raise IndexError('Unsupported indexing operation.')
x = np.squeeze(y.x[np.ix_(*index2)])
if x.ndim == 0:
return x[()]
@@ -4943,7 +5079,7 @@ def __getitem__(self2, index):
# Example: lar.lix[0]
return y[index]
else:
- raise IndexError, 'Unsupported indexing operation.'
+ raise IndexError('Unsupported indexing operation.')
def __setitem__(self2, index, value):
# Note: getitem uses the same (slightly modified) code
@@ -4954,7 +5090,7 @@ def __setitem__(self2, index, value):
elif 0 in y.shape:
msg = 'lix does not support shapes that contain 0 '
msg += 'such as (0,) and (2, 0 ,3).'
- raise ValueError, msg
+ raise ValueError(msg)
typ = type(index)
if typ == list:
# Example: lar.lix[['a', 'b', 'c']]
@@ -4974,7 +5110,7 @@ def __setitem__(self2, index, value):
elif len(index) < y.ndim:
index3 = list(index) + [slice(None)] * (y.ndim - len(index))
else:
- raise IndexError, 'Invalid index'
+ raise IndexError('Invalid index')
for ax, idx in enumerate(index3):
typ = type(idx)
if typ == list:
@@ -4992,14 +5128,14 @@ def __setitem__(self2, index, value):
elif isscalar(idx):
index2.append([idx])
else:
- raise IndexError, 'Unsupported indexing operation.'
+ raise IndexError('Unsupported indexing operation.')
if isinstance(value, larry):
if value.ndim != len(index2):
- raise IndexError, '`value` has wrong ndim'
+ raise IndexError('`value` has wrong ndim')
for ax, ix2 in enumerate(index2):
lab = [y.label[ax][i] for i in ix2]
if lab != value.label[ax]:
- raise IndexError, 'larry labels are not aligned'
+ raise IndexError('larry labels are not aligned')
y.x[np.ix_(*index2)] = value.x
else:
y.x[np.ix_(*index2)] = value
@@ -5007,7 +5143,7 @@ def __setitem__(self2, index, value):
# Example: lar.lix[0]
y[index] = value
else:
- raise IndexError, 'Unsupported indexing operation.'
+ raise IndexError('Unsupported indexing operation.')
def slicemaker(index, labelindex, axis):
"Convert a slice that may contain labels to a slice with indices."
@@ -5017,22 +5153,22 @@ def slicemaker(index, labelindex, axis):
start = None
elif type(index.start) is list:
if len(index.start) > 1:
- raise ValueError, msg2 % 'start'
+ raise ValueError(msg2 % 'start')
start = labelindex(index.start[0], axis=axis)
elif isscalar(index.start):
start = index.start
else:
- raise ValueError, msg1 % 'start'
+ raise ValueError(msg1 % 'start')
if index.stop is None:
stop = None
elif type(index.stop) is list:
if len(index.stop) > 1:
- raise ValueError, msg2 % 'start'
+ raise ValueError(msg2 % 'start')
stop = labelindex(index.stop[0], axis=axis)
elif isscalar(index.stop):
stop = index.stop
else:
- raise ValueError, msg1 % 'stop'
+ raise ValueError(msg1 % 'stop')
return slice(start, stop, index.step)
def labels2indices(label, labels):
@@ -5040,5 +5176,5 @@ def labels2indices(label, labels):
try:
indices = map(label.index, labels)
except ValueError:
- raise ValueError, 'Could not map label to index value.'
+ raise ValueError('Could not map label to index value.')
return indices
View
2  la/external/matplotlib.py
@@ -95,7 +95,7 @@ def quotes_historical_yahoo(ticker, date1, date2):
data, dates = parse_yahoo_historical(fh)
if len(data) == 0:
return None
- except IOError, exc:
+ except IOError as exc:
warnings.warn('urlopen() failure\n' + exc.strerror[1])
return None
return data, dates
View
21 la/external/prettytable.py
@@ -1,7 +1,7 @@
"""
pretty table code was copied from http://code.activestate.com/recipes/267662/
"""
-
+from __future__ import print_function
import cStringIO, operator
def indent(rows, hasHeader=False, headerChar='-', delim=' | ', justify='left',
@@ -35,14 +35,13 @@ def rowWrapper(row):
# select the appropriate justify method
justify = {'center':str.center, 'right':str.rjust, 'left':str.ljust}[justify.lower()]
output=cStringIO.StringIO()
- if separateRows: print >> output, rowSeparator
+ if separateRows: print(rowSeparator, file=output)
for physicalRows in logicalRows:
for row in physicalRows:
- print >> output, \
- prefix \
+ print(prefix \
+ delim.join([justify(str(item),width) for (item,width) in zip(row,maxWidths)]) \
- + postfix
- if separateRows or hasHeader: print >> output, rowSeparator; hasHeader=False
+ + postfix, file=output)
+ if separateRows or hasHeader: print(rowSeparator, file=output); hasHeader=False
return output.getvalue()
# written by Mike Brown
@@ -84,15 +83,15 @@ def wrap_always(text, width):
Aristidis,Papageorgopoulos,28,Senior Reseacher'''
rows = [row.strip().split(',') for row in data.splitlines()]
- print 'Without wrapping function\n'
- print indent([labels]+rows, hasHeader=True)
+ print('Without wrapping function\n')
+ print(indent([labels]+rows, hasHeader=True))
# test indent with different wrapping functions
width = 10
for wrapper in (wrap_always,wrap_onspace,wrap_onspace_strict):
- print 'Wrapping function: %s(x,width=%d)\n' % (wrapper.__name__,width)
- print indent([labels]+rows, hasHeader=True, separateRows=True,
+ print('Wrapping function: %s(x,width=%d)\n' % (wrapper.__name__,width))
+ print(indent([labels]+rows, hasHeader=True, separateRows=True,
prefix='| ', postfix=' |',
- wrapfunc=lambda x: wrapper(x,width))
+ wrapfunc=lambda x: wrapper(x,width)))
# output:
#
View
8 la/farray/misc.py
@@ -15,7 +15,7 @@ def geometric_mean(x, axis=-1, check_for_greater_than_zero=True):
"""
if (x <= 0).any() and check_for_greater_than_zero:
msg = 'All elements of x (except NaNs) must be greater than zero.'
- raise ValueError, msg
+ raise ValueError(msg)
x = x.copy()
m = np.isnan(x)
np.putmask(x, m, 1.0)
@@ -84,7 +84,7 @@ def correlation(arr1, arr2, axis=None):
mask = np.logical_or(np.isnan(arr1), np.isnan(arr2))
if mask.any():
# arr1 and/or arr2 contain NaNs, so use slower NaN functions if needed
- if axis == None:
+ if axis is None:
x1 = arr1.flatten()
x2 = arr2.flatten()
idx = ~mask.flatten()
@@ -111,7 +111,7 @@ def correlation(arr1, arr2, axis=None):
den = np.sqrt(bn.nansum(x1**2, axis) * bn.nansum(x2**2, axis))
else:
# Neither arr1 or arr2 contains nans, so use faster non-nan functions
- if axis == None:
+ if axis is None:
x1 = arr1.flatten()
x2 = arr2.flatten()
x1 = x1 - x1.mean()
@@ -153,7 +153,7 @@ def covMissing(R):
normalization = np.dot(mask, mask.T)
if np.any(normalization < 2):
- raise ValueError, 'covMissing: not enough observations'
+ raise ValueError('covMissing: not enough observations')
C = np.dot(R, R.T) / normalization
View
36 la/farray/move.py
@@ -52,7 +52,7 @@ def move_nanmedian(arr, window, axis=-1, method='loop'):
y = move_func_loop(bn.nanmedian, arr, window, axis=axis)
else:
msg = "`method` must be 'strides' or 'loop'."
- raise ValueError, msg
+ raise ValueError(msg)
return y
# RANKING -------------------------------------------------------------------
@@ -119,7 +119,7 @@ def move_nanranking(arr, window, axis=-1, method='strides'):
y = move_func_loop(lastrank, arr, window, axis=axis)
else:
msg = "`method` must be 'strides' or 'loop'."
- raise ValueError, msg
+ raise ValueError(msg)
return y
# GENERAL --------------------------------------------------------------------
@@ -171,17 +171,17 @@ def move_func(func, arr, window, axis=-1, method='loop', **kwargs):
y = move_func_loop(func, arr, window, axis=axis, **kwargs)
else:
msg = "`method` must be 'strides' or 'loop'."
- raise ValueError, msg
+ raise ValueError(msg)
return y
def move_func_loop(func, arr, window, axis=-1, **kwargs):
"Generic moving window function implemented with a python loop."
- if axis == None:
- raise ValueError, "An `axis` value of None is not supported."
+ if axis is None:
+ raise ValueError("An `axis` value of None is not supported.")
if window < 1:
- raise ValueError, "`window` must be at least 1."
+ raise ValueError("`window` must be at least 1.")
if window > arr.shape[axis]:
- raise ValueError, "`window` is too long."
+ raise ValueError("`window` is too long.")
y = nans(arr.shape)
idx1 = [slice(None)] * arr.ndim
idx2 = list(idx1)
@@ -193,19 +193,19 @@ def move_func_loop(func, arr, window, axis=-1, **kwargs):
def move_func_strides(func, arr, window, axis=-1, **kwargs):
"Generic moving window function implemented with strides."
- if axis == None:
- raise ValueError, "An `axis` value of None is not supported."
+ if axis is None:
+ raise ValueError("An `axis` value of None is not supported.")
if window < 1:
- raise ValueError, "`window` must be at least 1."
+ raise ValueError("`window` must be at least 1.")
if window > arr.shape[axis]:
- raise ValueError, "`window` is too long."
+ raise ValueError("`window` is too long.")
ndim = arr.ndim
as_strided = np.lib.stride_tricks.as_strided
idx = range(ndim)
axis = idx[axis]
arrshape0 = tuple(arr.shape)
if axis >= ndim:
- raise IndexError, "`axis` is out of range."
+ raise IndexError("`axis` is out of range.")
if ndim == 1:
strides = arr.strides
shape = (arr.size - window + 1, window)
@@ -233,7 +233,7 @@ def move_func_strides(func, arr, window, axis=-1, **kwargs):
if axis > 0:
y = y.swapaxes(0, axis)
else:
- raise ValueError, "Only 1d, 2d, and 3d input arrays are supported."
+ raise ValueError("Only 1d, 2d, and 3d input arrays are supported.")
ynan = nans(arrshape0)
index = [slice(None)] * ndim
index[axis] = slice(window - 1, None)
@@ -288,11 +288,11 @@ def movingsum(arr, window, skip=0, axis=-1, norm=False):
# Check input
if window < 1:
- raise ValueError, 'window must be at least 1'
+ raise ValueError('window must be at least 1')
if window > arr.shape[axis]:
- raise ValueError, 'Window is too big.'
+ raise ValueError('Window is too big.')
if skip > arr.shape[axis]:
- raise IndexError, 'Your skip is too large.'
+ raise IndexError('Your skip is too large.')
# Set missing values to 0
m = ismissing(arr)
@@ -347,9 +347,9 @@ def movingrank(x, window, axis=-1):
If a window is all NaNs except last, this is returned as NaN
"""
if window > x.shape[axis]:
- raise ValueError, 'Window is too big.'
+ raise ValueError('Window is too big.')
if window < 2:
- raise ValueError, 'Window is too small.'
+ raise ValueError('Window is too small.')
nt = x.shape[axis]
mr = np.nan * np.zeros(x.shape)
for i in xrange(window-1, nt):
View
8 la/farray/normalize.py
@@ -107,7 +107,7 @@ def lastrank(x, axis=-1, decay=0.0):
r = (g + g + e - w.flat[-1]) / 2.0
r = r / (n - w.flat[-1])
elif decay < 0:
- raise ValueError, 'decay must be greater than or equal to zero.'
+ raise ValueError('decay must be greater than or equal to zero.')
else:
# Special case the most common case, decay = 0, for speed
g = (x[indlast] > x).sum(axis)
@@ -270,7 +270,7 @@ def quantile(x, q, axis=0):
"""
if q < 1:
- raise ValueError, 'q must be one or greater.'
+ raise ValueError('q must be one or greater.')
elif q == 1:
y = np.zeros(x.shape)
np.putmask(y, np.isnan(x), np.nan)
@@ -279,13 +279,13 @@ def quantile(x, q, axis=0):
if q > x.size:
msg = 'q must be less than or equal to the number of elements '
msg += 'in x.'
- raise ValueError, msg
+ raise ValueError(msg)
y = np.apply_along_axis(_quantileraw1d, 0, x.flat, q)
y = y.reshape(x.shape)
else:
if q > x.shape[axis]:
msg = 'q must be less than or equal to the number of rows in x.'
- raise ValueError, msg
+ raise ValueError(msg)
y = np.apply_along_axis(_quantileraw1d, axis, x, q)
y = y - 1.0
y = 1.0 * y / (q - 1.0)
View
2  la/flabel.py
@@ -171,7 +171,7 @@ def flattenlabel(label, order='C'):
"""
if order not in ('C', 'F'):
- raise ValueError, "order must be 'C' or 'F'"
+ raise ValueError("order must be 'C' or 'F'")
label = list(label)
if order == 'C':
label = label[::-1]
View
34 la/flarry.py
@@ -246,7 +246,7 @@ def align_raw(lar1, lar2, join='inner', cast=True):
ndim = lar2.ndim
if lar1.ndim != ndim:
msg = "'lar1' and 'lar2' must have the same number of dimensions."
- raise ValueError, msg
+ raise ValueError(msg)
# Check join type
typejoin = type(join)
@@ -255,9 +255,9 @@ def align_raw(lar1, lar2, join='inner', cast=True):
elif typejoin is list:
if len(join) != ndim:
msg = "Length of `join` list equal number of dimension of `lar1`."
- raise ValueError, msg
+ raise ValueError(msg)
else:
- raise TypeError, "`join` must be a string or a list."
+ raise TypeError("`join` must be a string or a list.")
# Initialize missing markers, set value later (in loop) only if needed.
# The weird initialization value ensures a user would never pick the same
@@ -295,7 +295,7 @@ def align_raw(lar1, lar2, join='inner', cast=True):
elif joinax == 'outer':
if list1 == list2:
list3 = list(list1)
- else:
+ else:
list3 = list(set(list1).union(list2))
list3.sort()
idx1, idx1_miss = listmap_fill(list1, list3, fill=0)
@@ -310,7 +310,7 @@ def align_raw(lar1, lar2, join='inner', cast=True):
x1 = x1.astype(float)
miss1 = missing_marker(x1)
else:
- raise TypeError, msg
+ raise TypeError(msg)
index1 = [slice(None)] * ndim
index1[ax] = idx1_miss
x1[index1] = miss1
@@ -322,7 +322,7 @@ def align_raw(lar1, lar2, join='inner', cast=True):
x2 = x2.astype(float)
miss2 = missing_marker(x2)
else:
- raise TypeError, msg
+ raise TypeError(msg)
index2 = [slice(None)] * ndim
index2[ax] = idx2_miss
x2[index2] = miss2
@@ -345,7 +345,7 @@ def align_raw(lar1, lar2, join='inner', cast=True):
x2 = x2.astype(float)
miss2 = missing_marker(x2)
else:
- raise TypeError, msg
+ raise TypeError(msg)
index2 = [slice(None)] * ndim
index2[ax] = idx2_miss
x2[index2] = miss2
@@ -363,7 +363,7 @@ def align_raw(lar1, lar2, join='inner', cast=True):
x1 = x1.astype(float)
miss1 = missing_marker(x1)
else:
- raise TypeError, msg
+ raise TypeError(msg)
index1 = [slice(None)] * ndim
index1[ax] = idx1_miss
x1[index1] = miss1
@@ -371,7 +371,7 @@ def align_raw(lar1, lar2, join='inner', cast=True):
elif joinax == 'skip':
list3 = None
else:
- raise ValueError, 'join type not recognized'
+ raise ValueError('join type not recognized')
label.append(list3)
return x1, x2, label, x1isview, x2isview
@@ -603,7 +603,7 @@ def union(axis, *args):
if isinstance(arg, larry):
rc = rc.union(arg.label[axis])
else:
- raise TypeError, 'One or more input is not a larry'
+ raise TypeError('One or more input is not a larry')
rc = list(rc)
rc.sort()
return rc
@@ -645,7 +645,7 @@ def intersection(axis, *args):
if isinstance(arg, larry):
rc = rc.intersection(arg.label[axis])
else:
- raise TypeError, 'One or more input is not a larry'
+ raise TypeError('One or more input is not a larry')
rc = list(rc)
rc.sort()
return rc
@@ -1383,13 +1383,13 @@ def stack(mode, **kwargs):
"""
if not np.all([kwargs[key].ndim == 2 for key in kwargs]):
- raise ValueError, 'All input larrys must be 2d'
+ raise ValueError('All input larrys must be 2d')
if mode == 'union':
logic = union
elif mode == 'intersection':
logic = intersection
else:
- raise ValueError, 'mode must be union or intersection'
+ raise ValueError('mode must be union or intersection')
row = logic(0, *kwargs.values())
col = logic(1, *kwargs.values())
x = np.zeros((len(kwargs), len(row), len(col)))
@@ -1461,7 +1461,7 @@ def panel(lar):
"""
if lar.ndim != 3:
- raise ValueError, "lar must be 3d."
+ raise ValueError("lar must be 3d.")
y = lar.copy()
y.label = [flattenlabel([y.label[1], y.label[2]])[0], y.label[0]]
y.x = y.x.T.reshape(-1, y.shape[0], order="F")
@@ -1495,7 +1495,7 @@ def cov(lar):
"""
if lar.ndim != 2:
- raise ValueError, 'This function only works on 2d larrys'
+ raise ValueError('This function only works on 2d larrys')
label = [list(lar.label[0]), list(lar.label[0])]
x = covMissing(lar.x)
return larry(x, label, validate=False)
@@ -1597,7 +1597,7 @@ def rand(*args, **kwargs):
elif (len(args) == 0) and (len(kwargs) == 1) and ('rand' in kwargs):
return randfunc()
else:
- raise ValueError, 'Input parameters not recognized'
+ raise ValueError('Input parameters not recognized')
def randn(*args, **kwargs):
"""
@@ -1695,7 +1695,7 @@ def randn(*args, **kwargs):
elif (len(args) == 0) and (len(kwargs) == 1) and ('randn' in kwargs):
return randnfunc()
else:
- raise ValueError, 'Input parameters not recognized'
+ raise ValueError('Input parameters not recognized')
def sortby(lar, element, axis, reverse=False):
"""
View
66 la/io.py
@@ -2,6 +2,7 @@
import os
import datetime
+import sys
import numpy as np
import h5py
@@ -192,17 +193,17 @@ def __getitem__(self, key):
return lara(self.f[key])
else:
msg = "%s is in the archive but it is not a larry."
- raise KeyError, msg % key
+ raise KeyError(msg % key)
else:
- raise KeyError, "A larry named %s is not in the archive." % key
+ raise KeyError("A larry named %s is not in the archive." % key)
def __setitem__(self, key, value):
# Make sure the data looks OK before saving
if type(key) != str:
- raise TypeError, 'key must be a string of type str.'
+ raise TypeError('key must be a string of type str.')
if not isinstance(value, larry):
- raise TypeError, 'value must be a larry.'
+ raise TypeError('value must be a larry.')
# Does an item (larry or otherwise) with given key already exist? If
# so delete. Note that self.f.keys() [all keys] is used instead of
@@ -226,7 +227,7 @@ def __repr__(self):
# but I don't want to load the array, I just want the shape
shape = str(self.f[key]['x'].shape)
dtype = str(self.f[key]['x'].dtype)
- table.append([key, dtype, shape])
+ table.append([key, dtype, shape])
return indent(table, hasHeader=True, delim=' ')
@property
@@ -321,15 +322,24 @@ def __init__(self, group):
self.x = group['x']
self.label = _load_label(group, len(self.x.shape))
- # Grab these methods from larry
- __getitem__ = larry.__getitem__.im_func
- __setitem__ = larry.__setitem__.im_func
- maxlabel = larry.maxlabel.im_func
- minlabel = larry.minlabel.im_func
- getlabel = larry.getlabel.im_func
- labelindex = larry.labelindex.im_func
+ # Grab these methods from larry
+ if sys.version_info[0] < 3:
+ __getitem__ = larry.__getitem__.im_func
+ __setitem__ = larry.__setitem__.im_func
+ maxlabel = larry.maxlabel.im_func
+ minlabel = larry.minlabel.im_func
+ getlabel = larry.getlabel.im_func
+ labelindex = larry.labelindex.im_func
+ else:
+ __getitem__ = larry.__getitem__
+ __setitem__ = larry.__setitem__
+ maxlabel = larry.maxlabel
+ minlabel = larry.minlabel
+ getlabel = larry.getlabel
+ labelindex = larry.labelindex
+
shape = larry.shape
- dtype = larry.dtype
+ dtype = larry.dtype
@property
def ndim(self):
@@ -402,9 +412,9 @@ def save(file, lar, key):
# Check input
if type(lar) != larry:
- raise TypeError, 'lar must be a larry.'
+ raise TypeError('lar must be a larry.')
if type(key) != str:
- raise TypeError, 'key must be a string.'
+ raise TypeError('key must be a string.')
# Get a h5py.File instance
f, opened = _openfile(file)
@@ -473,12 +483,12 @@ def load(file, key):
# Check input
if type(key) != str:
- raise TypeError, 'key must be a string.'
+ raise TypeError('key must be a string.')
f, opened = _openfile(file)
if key not in f:
- raise KeyError, "A larry named '%s' is not in archive." % key
+ raise KeyError("A larry named '%s' is not in archive." % key)
if not _is_archived_larry(f[key]):
- raise KeyError, 'key (%s) is not a larry.' % key
+ raise KeyError('key (%s) is not a larry.' % key)
# Load larry
group = f[key]
@@ -531,12 +541,12 @@ def delete(file, key):
# Check input
if type(key) != str:
- raise TypeError, 'key must be a string.'
+ raise TypeError('key must be a string.')
f, opened = _openfile(file)
if key not in f:
- raise KeyError, "A larry named '%s' is not in archive." % key
+ raise KeyError("A larry named '%s' is not in archive." % key)
if not _is_archived_larry(f[key]):
- raise KeyError, 'key (%s) is not a larry.' % key
+ raise KeyError('key (%s) is not a larry.' % key)
# Delete
del f[key]
@@ -587,7 +597,7 @@ def is_archived_larry(file, key):
if key in f:
answer = _is_archived_larry(f[key])
else:
- raise ValueError, 'key (%s) is not in archive.' % str(key)
+ raise ValueError('key (%s) is not in archive.' % str(key))
if opened:
f.close()
return answer
@@ -624,12 +634,12 @@ def _load_label(group, ndim):
def _list2array(x):
"Convert list to array if elements are of the same type, raise otherwise."
if type(x) != list:
- raise TypeError, 'x must be a list'
+ raise TypeError('x must be a list')
type0 = type(x[0])
if not all([type(i)==type0 for i in x]):
msg = 'Elements of a label along any one dimension must be of the '
msg += 'same type.'
- raise TypeError, msg
+ raise TypeError(msg)
datetime_type = 'not_datetime'
dtype = None
if type0 == datetime.date:
@@ -670,7 +680,7 @@ def _openfile(file):
opened = True
else:
msg = "file must be a h5py.File object or a string (path)."
- raise TypeError, msg
+ raise TypeError(msg)
return f, opened
def _is_archived_larry(obj):
@@ -696,7 +706,7 @@ def _create_nested_groups(f, path):
else:
if not isinstance(f[group], h5py.Group):
msg = '%s already exists and is not a h5.py.Group object.'
- raise ValueError, msg % group
+ raise ValueError(msg % group)
def datetime2tuple(dt):
"Convert datetime.datetime to tuple; tzinfo, if any, is lost."
@@ -705,7 +715,7 @@ def datetime2tuple(dt):
def tuple2datetime(i):
"Convert tuple to a datetime.datetime object."
- return datetime.datetime(*i)
+ return datetime.datetime(*i)
def time2tuple(t):
"Convert datetime.time to tuple; tzinfo, if any, is lost."
@@ -714,4 +724,4 @@ def time2tuple(t):
def tuple2time(i):
"Convert tuple to a datetime.time object."
return datetime.time(*i)
-
+
View
4 la/missing.py
@@ -53,14 +53,14 @@ def nans(shape, dtype=float):
return a
elif a.dtype == np.bool_:
msg = 'Missing values are not supported for bool dtype.'
- raise TypeError, msg
+ raise TypeError(msg)
elif a.dtype == object:
a.fill(None)
return a
# int dtype can't be filled with NaN
msg = 'Inexact scalar dtype, such as float, needed for NaN '
msg += 'fill value.'
- raise TypeError, msg
+ raise TypeError(msg)
def missing_marker(data):
"""
View
947 la/tests/deflarry_test.py
445 additions, 502 deletions not shown
View
9 la/tests/empty_larry_test.py
@@ -156,8 +156,8 @@ def test_empty():
# Check that the right shape and value are returned by the reducing methods
# when the input has a shape tuple that contains 0
-False = np.False_
-True = ~False
+FALSE = np.False_
+TRUE = ~FALSE
def reduce_methods():
r = [{'la': 'sum', 'np': 'sum', 'dflt': nan, 'kw': {}},
@@ -169,8 +169,8 @@ def reduce_methods():
{'la': 'min', 'np': 'sum', 'dflt': nan, 'kw': {}},
{'la': 'std', 'np': 'sum', 'dflt': nan, 'kw': {}},
{'la': 'var', 'np': 'sum', 'dflt': nan, 'kw': {}},
- {'la': 'any', 'np': 'sum', 'dflt': False, 'kw': {}},
- {'la': 'all', 'np': 'sum', 'dflt': True, 'kw': {}},
+ {'la': 'any', 'np': 'sum', 'dflt': FALSE, 'kw': {}},
+ {'la': 'all', 'np': 'sum', 'dflt': TRUE, 'kw': {}},
{'la': 'lastrank', 'np': 'sum', 'dflt': nan, 'kw': {}},
{'la': 'lastrank', 'np': 'sum', 'dflt': nan, 'kw': {'decay': 10}}]
return r
@@ -200,6 +200,7 @@ def test_reduce_shape():
ymethod = getattr(y, method['la'])
lar = ymethod(axis=axis, **method['kw'])
yield ale, lar, arr, msg % (method['la'], shape, axis)
+
def test_50():
"Regression #50"
actual = larry([], dtype=np.int).sum(0)
View
66 la/tests/flarry_test.py
@@ -23,10 +23,10 @@ def test_union_1(self):
y2 = larry([[1, 2], [3, 4]], [['e', 'b'], ['f', 'd']])
actual = union(0, y1, y2)
desired = ['a', 'b', 'e']
- self.assert_(actual == desired, 'union axis=0')
+ self.assertTrue(actual == desired, 'union axis=0')
actual = union(1, y1, y2)
desired = ['c', 'd', 'f']
- self.assert_(actual == desired, 'union axis=1')
+ self.assertTrue(actual == desired, 'union axis=1')
def test_intersection_1(self):
"func.intersection_1"
@@ -34,45 +34,45 @@ def test_intersection_1(self):
y2 = larry([[1, 2], [3, 4]], [['e', 'b'], ['f', 'd']])
actual = intersection(0, y1, y2)
desired = ['b']
- self.assert_(actual == desired, 'intersection axis=0')
+ self.assertTrue(actual == desired, 'intersection axis=0')
actual = intersection(1, y1, y2)
desired = ['d']
- self.assert_(actual == desired, 'intersection axis=1')
+ self.assertTrue(actual == desired, 'intersection axis=1')
def test_isaligned_1(self):
"isaligned_1"
lar1 = larry([[1, 2], [3, 4]], [['r1', 'r2'], ['c1', 'c2']])
lar2 = larry([[5, 6], [7, 8]], [['r2', 'r1'], ['c1', 'c2']])
a = isaligned(lar1, lar2)
- self.assert_(~a, "Should return False")
+ self.assertTrue(~a, "Should return False")
def test_isaligned_2(self):
"isaligned_2"
lar1 = larry([[1, 2], [3, 4]], [['r1', 'r2'], ['c1', 'c2']])
lar2 = larry([[4, 5], [6, 7]], [['r2', 'r1'], ['c1', 'c2']])
a = isaligned(lar1, lar2, axis=0)
- self.assert_(~a, "Should return False")
+ self.assertTrue(~a, "Should return False")
def test_isaligned_3(self):
"isaligned_3"
lar1 = larry([[1, 2], [3, 4]], [['r1', 'r2'], ['c1', 'c2']])
lar2 = larry([[5, 6], [7, 8]], [['r2', 'r1'], ['c1', 'c2']])
a = isaligned(lar1, lar2, axis=1)
- self.assert_(a, "Should return True")
+ self.assertTrue(a, "Should return True")
def test_isaligned_4(self):
"isaligned_4"
lar1 = larry([[1, 2], [3, 4]], [['r1', 'r2'], ['c1', 'c2']])
lar2 = larry([5, 6], [['r1', 'r2']])
a = isaligned(lar1, lar2, axis=0)
- self.assert_(a, "Should return True")
+ self.assertTrue(a, "Should return True")
def test_isaligned_5(self):
"isaligned_5"
lar1 = larry([[1, 2], [3, 4]], [['r1', 'r2'], ['c1', 'c2']])
lar2 = larry([5, 6], [['r1', 'r2']])
a = isaligned(lar1, lar2)
- self.assert_(~a, "Should return False")
+ self.assertTrue(~a, "Should return False")
def test_panel_1(self):
"func.panel_1"
@@ -171,9 +171,9 @@ def test_unique_2(self):
def test_unique_3(self):
lar = larry(['a', 'b', 'b', 'c', 'a'])
u, indices = unique(lar, return_index=True)
- assert_array_equal(u, np.array(['a', 'b', 'c'], dtype='|S1'))
+ assert_array_equal(u, np.array(['a', 'b', 'c']))
assert_array_equal(indices, np.array([0, 1, 3]))
- assert_array_equal(lar[indices], np.array(['a','b','c'], dtype='|S1'))
+ assert_array_equal(lar[indices], np.array(['a','b','c']))
def test_unique_4(self):
lar = larry([1, 2, 6, 4, 2, 3, 2])
@@ -233,7 +233,7 @@ def test_1d5(self):
"align 1d test #5"
y1 = larry([1, 2])
y2 = larry([1, 2, 3])
- self.failUnlessRaises(TypeError, align, y1, y2, 'outer', False)
+ self.assertRaises(TypeError, align, y1, y2, 'outer', False)
def test_1d6(self):
"align 1d test #6"
@@ -304,10 +304,10 @@ def test_1d11(self):
def test_1d12(self):
"align 1d test #12"
y1 = larry([1, 2])
- y2 = larry([1, 2], [['a', 'b']])
+ y2 = larry([1, 2], [[3, 4]])
a1, a2 = align(y1, y2, join='outer')
- d1 = larry([1, 2, nan, nan], [[0, 1, 'a', 'b']], dtype=float)
- d2 = larry([nan, nan, 1, 2], [[0, 1, 'a', 'b']], dtype=float)
+ d1 = larry([1, 2, nan, nan], [[0, 1, 3, 4]], dtype=float)
+ d2 = larry([nan, nan, 1, 2], [[0, 1, 3, 4]], dtype=float)
msg = "align 1d fail on %s larry"
ale(a1, d1, msg % 'left', original=y1)
ale(a2, d2, msg % 'right', original=y2)
@@ -316,7 +316,7 @@ def test_1d13(self):
"align 1d test #13"
y1 = larry([1, 2])
y2 = larry([1, 2], [['a', 'b']])
- self.failUnlessRaises(TypeError, align, y1, y2, 'outer', False)
+ self.assertRaises(TypeError, align, y1, y2, 'outer', False)
def test_1d14(self):
"align 1d test #14"
@@ -355,7 +355,7 @@ def test_1d17(self):
"align 1d test #17"
y1 = larry([1, 2])
y2 = larry([1, 2], [['a', 'b']])
- self.failUnlessRaises(TypeError, align, y1, y2, 'outer', False)
+ self.assertRaises(TypeError, align, y1, y2, 'outer', False)
def test_1d18(self):
"align 1d test #18"
@@ -452,7 +452,7 @@ def test_2d5(self):
"align 2d test #5"
y1 = larry([[1, 2], [3, 4]])
y2 = larry([[1, 2, 5], [3, 4, 6]])
- self.failUnlessRaises(TypeError, align, y1, y2, 'outer', False)
+ self.assertRaises(TypeError, align, y1, y2, 'outer', False)
def test_2d6(self):
"align 2d test #6"
@@ -470,7 +470,7 @@ def test_2d7(self):
y1 = larry([[1, 2], [3, 4]])
y2 = larry([[1, 2, 5], [3, 4, 6]])
j = ['outer', 'inner', 'left']
- self.failUnlessRaises(ValueError, align, y1, y2, j, False)
+ self.assertRaises(ValueError, align, y1, y2, j, False)
def test_2d8(self):
"align 2d test #8"
@@ -685,10 +685,10 @@ def test_lrange_4(self):
ale(a, d, "lrange failed.")
def test_lrange_5(self):
- self.failUnlessRaises(ValueError, lrange, (2,), ['a', 'b', 'c'])
+ self.assertRaises(ValueError, lrange, (2,), ['a', 'b', 'c'])
def test_empty_1(self):
- self.failUnlessRaises(ValueError, empty, (2,), ['a', 'b', 'c'])
+ self.assertRaises(ValueError, empty, (2,), ['a', 'b', 'c'])
def test_ones_1(self):
a = ones(5)
@@ -906,9 +906,9 @@ def test_binaryop_17(self):
def test_binaryop_18(self):
"binaryop test #18"
y1 = larry([1, nan, nan, 1])
- y2 = larry([1, 1, nan, 1], [[0, 1, 2, 'a']])
+ y2 = larry([1, 1, nan, 1], [[0, 1, 2, 9]])
actual = binaryop(np.add, y1, y2, join='outer')
- desired = larry([2, nan, nan, nan, nan], [[0, 1, 2, 3, 'a']])
+ desired = larry([2, nan, nan, nan, nan], [[0, 1, 2, 3, 9]])
msg = "binaryop failed"
ale(actual, desired, msg, original=y1)
ale(actual, desired, msg, original=y2)
@@ -916,9 +916,9 @@ def test_binaryop_18(self):
def test_binaryop_19(self):
"binaryop test #19"
y1 = larry([1, nan, nan, 1])
- y2 = larry([1, 1, nan, 1], [[0, 1, 2, 'a']])
+ y2 = larry([1, 1, nan, 1], [[0, 1, 2, 9]])
actual = binaryop(np.add, y1, y2, join='outer', missone=0)
- desired = larry([2, 1, nan, 1, 1], [[0, 1, 2, 3, 'a']])
+ desired = larry([2, 1, nan, 1, 1], [[0, 1, 2, 3, 9]])
msg = "binaryop failed"
ale(actual, desired, msg, original=y1)
ale(actual, desired, msg, original=y2)
@@ -926,9 +926,9 @@ def test_binaryop_19(self):
def test_binaryop_20(self):
"binaryop test #20"
y1 = larry([1, nan, nan, 1])
- y2 = larry([1, 1, nan, 1], [[0, 1, 2, 'a']])
+ y2 = larry([1, 1, nan, 1], [[0, 1, 2, 9]])
actual = binaryop(np.add, y1, y2, join='outer', missone=0, misstwo=0)
- desired = larry([2, 1, 0, 1, 1], [[0, 1, 2, 3, 'a']], dtype=float)
+ desired = larry([2, 1, 0, 1, 1], [[0, 1, 2, 3, 9]], dtype=float)
msg = "binaryop failed"
ale(actual, desired, msg, original=y1)
ale(actual, desired, msg, original=y2)
@@ -1133,9 +1133,9 @@ def test_add_17(self):
def test_add_18(self):
"add test #18"
y1 = larry([1, nan, nan, 1])
- y2 = larry([1, 1, nan, 1], [[0, 1, 2, 'a']])
+ y2 = larry([1, 1, nan, 1], [[0, 1, 2, 9]])
actual = add(y1, y2, join='outer')
- desired = larry([2, nan, nan, nan, nan], [[0, 1, 2, 3, 'a']])
+ desired = larry([2, nan, nan, nan, nan], [[0, 1, 2, 3, 9]])
msg = "add failed"
ale(actual, desired, msg, original=y1)
ale(actual, desired, msg, original=y2)
@@ -1143,9 +1143,9 @@ def test_add_18(self):
def test_add_19(self):
"add test #19"
y1 = larry([1, nan, nan, 1])
- y2 = larry([1, 1, nan, 1], [[0, 1, 2, 'a']])
+ y2 = larry([1, 1, nan, 1], [[0, 1, 2, 9]])
actual = add(y1, y2, join='outer', missone=0)
- desired = larry([2, 1, nan, 1, 1], [[0, 1, 2, 3, 'a']])
+ desired = larry([2, 1, nan, 1, 1], [[0, 1, 2, 3, 9]])
msg = "add failed"
ale(actual, desired, msg, original=y1)
ale(actual, desired, msg, original=y2)
@@ -1153,9 +1153,9 @@ def test_add_19(self):
def test_add_20(self):
"add test #20"
y1 = larry([1, nan, nan, 1])
- y2 = larry([1, 1, nan, 1], [[0, 1, 2, 'a']])
+ y2 = larry([1, 1, nan, 1], [[0, 1, 2, 9]])
actual = add(y1, y2, join='outer', missone=0, misstwo=0)
- desired = larry([2, 1, 0, 1, 1], [[0, 1, 2, 3, 'a']], dtype=float)
+ desired = larry([2, 1, 0, 1, 1], [[0, 1, 2, 3, 9]], dtype=float)
msg = "add failed"
ale(actual, desired, msg, original=y1)
ale(actual, desired, msg, original=y2)
View
19 la/tests/more_test.py
@@ -1,4 +1,4 @@
-# these are tests for use with nose using generators, not for unittest.
+"This only tests that functions run and return appropriate types (arrays, scalars)"
# For support of python 2.5
from __future__ import with_statement
@@ -62,17 +62,8 @@ def test_return_array():
args = (xc, sectors)
with np.errstate(invalid='ignore', divide='ignore'):
yield check_return_array, func, args
-
-def check_3d(func, args):
- res = func(*args)
- if type(res) is tuple:
- res1 = res[0]
- else:
- res1 = res
- assert_(np.shape(res1)>0, repr(func)+'does not return array for 3d')
def test_3d():
- # many of these tests fail, skip to reduce noise during testing
x = np.array([[9.0, 3.0, nan, nan, 9.0, nan],
[1.0, 1.0, 1.0, nan, nan, nan],
[2.0, 2.0, 0.1, nan, 1.0, nan], # 0.0 kills geometric mean
@@ -86,22 +77,22 @@ def test_3d():
xc = x.copy()
args = (xc,)
with np.errstate(invalid='ignore', divide='ignore'):
- yield check_3d, func, args
+ yield check_return_array, func, args
for func in funcs_oneint:
xc = x.copy()
args = (xc, 2)
with np.errstate(invalid='ignore', divide='ignore'):
- yield check_3d, func, args
+ yield check_return_array, func, args
for func in funcs_onefrac:
xc = x.copy()
args = (xc, -1, 0.5)
with np.errstate(invalid='ignore', divide='ignore'):
- yield check_3d, func, args
+ yield check_return_array, func, args
for func in funcs_sect:
xc = x.copy()
args = (xc, sectors)
with np.errstate(invalid='ignore', divide='ignore'):
- yield check_3d, func, args
+ yield check_return_array, func, args
View
2  la/util/misc.py
@@ -7,7 +7,7 @@
from la.flabel import list2index
-C = string.letters + string.digits
+C = string.ascii_letters + string.digits
def randstring(n):
"Random characters string selected from lower, upper letters and digits."
s = []
View
4 la/util/report.py
@@ -1,5 +1,5 @@
"Information about your particular installation of the la package."
-
+from __future__ import print_function
from la.external.prettytable import indent
def info():
@@ -42,4 +42,4 @@ def info():
table.append(['HDF5 archiving', io])
table.append(['listmap', listmap])
table.append(['listmap_fill', listmap_fill])
- print indent(table, hasHeader=False, delim=' ')
+ print(indent(table, hasHeader=False, delim=' '))
View
12 la/util/resample.py
@@ -48,13 +48,13 @@ def cross_validation(n, kfold, shuffle=None):
"""
if n < 2:
- raise ValueError, "`n` must be two or greater."
+ raise ValueError("`n` must be two or greater.")
if kfold < 2:
- raise ValueError, "`kfold` must be two or greater."
+ raise ValueError("`kfold` must be two or greater.")
if kfold > n:
- raise ValueError, "`kfold` cannot be greater than `n`"
+ raise ValueError("`kfold` cannot be greater than `n`")
index = range(n)
- if shuffle == None:
+ if shuffle is None:
np.random.shuffle(index)
else:
shuffle(index)
@@ -115,9 +115,9 @@ def bootstrap(n, nboot, randint=None):
"""
if n < 2:
- raise ValueError, "`n` must be at least 2"
+ raise ValueError("`n` must be at least 2")
if nboot < 1:
- raise ValueError, "`nboot` must be at least 1"
+ raise ValueError("`nboot` must be at least 1")
if randint == None:
randint2 = np.random.randint
else:
View
50 la/util/testing.py
@@ -109,11 +109,11 @@ def assert_larry_equal(actual, desired, msg='', dtype=True, original=None,
# Check input
if dtype not in (True, False):
- raise TypeError, 'dtype must be True or False.'
+ raise TypeError('dtype must be True or False.')
if iscopy not in (True, False):
- raise TypeError, 'iscopy must be True or False.'
+ raise TypeError('iscopy must be True or False.')
if type(msg) is not str:
- raise TypeError, 'msg must be a string.'
+ raise TypeError('msg must be a string.')
# Function to make section headings
def heading(text):
@@ -127,12 +127,12 @@ def heading(text):
try:
assert_almost_equal(actual, desired)
- except AssertionError, err:
+ except AssertionError as err:
fail.append(heading('SCALARS') + str(err))
if dtype:
try:
assert_equal(type(actual), type(desired))
- except AssertionError, err: