/usr/lib64/python2.6/site-packages/numpy/core/tests
Edit: /usr/lib64/python2.6/site-packages/numpy/core/tests/test_multiarray.py (51858B)
import tempfile
import sys
import os
import numpy as np
from numpy.testing import *
from numpy.core import *
from numpy.core.multiarray_tests import test_neighborhood_iterator, test_neighborhood_iterator_oob
from test_print import in_foreign_locale
class TestFlags(TestCase):
def setUp(self):
self.a = arange(10)
def test_writeable(self):
mydict = locals()
self.a.flags.writeable = False
self.assertRaises(RuntimeError, runstring, 'self.a[0] = 3', mydict)
self.a.flags.writeable = True
self.a[0] = 5
self.a[0] = 0
def test_otherflags(self):
assert_equal(self.a.flags.carray, True)
assert_equal(self.a.flags.farray, False)
assert_equal(self.a.flags.behaved, True)
assert_equal(self.a.flags.fnc, False)
assert_equal(self.a.flags.forc, True)
assert_equal(self.a.flags.owndata, True)
assert_equal(self.a.flags.writeable, True)
assert_equal(self.a.flags.aligned, True)
assert_equal(self.a.flags.updateifcopy, False)
class TestAttributes(TestCase):
def setUp(self):
self.one = arange(10)
self.two = arange(20).reshape(4,5)
self.three = arange(60,dtype=float64).reshape(2,5,6)
def test_attributes(self):
assert_equal(self.one.shape, (10,))
assert_equal(self.two.shape, (4,5))
assert_equal(self.three.shape, (2,5,6))
self.three.shape = (10,3,2)
assert_equal(self.three.shape, (10,3,2))
self.three.shape = (2,5,6)
assert_equal(self.one.strides, (self.one.itemsize,))
num = self.two.itemsize
assert_equal(self.two.strides, (5*num, num))
num = self.three.itemsize
assert_equal(self.three.strides, (30*num, 6*num, num))
assert_equal(self.one.ndim, 1)
assert_equal(self.two.ndim, 2)
assert_equal(self.three.ndim, 3)
num = self.two.itemsize
assert_equal(self.two.size, 20)
assert_equal(self.two.nbytes, 20*num)
assert_equal(self.two.itemsize, self.two.dtype.itemsize)
assert_equal(self.two.base, arange(20))
def test_dtypeattr(self):
assert_equal(self.one.dtype, dtype(int_))
assert_equal(self.three.dtype, dtype(float_))
assert_equal(self.one.dtype.char, 'l')
assert_equal(self.three.dtype.char, 'd')
self.failUnless(self.three.dtype.str[0] in '<>')
assert_equal(self.one.dtype.str[1], 'i')
assert_equal(self.three.dtype.str[1], 'f')
def test_stridesattr(self):
x = self.one
def make_array(size, offset, strides):
return ndarray([size], buffer=x, dtype=int,
offset=offset*x.itemsize,
strides=strides*x.itemsize)
assert_equal(make_array(4, 4, -1), array([4, 3, 2, 1]))
self.failUnlessRaises(ValueError, make_array, 4, 4, -2)
self.failUnlessRaises(ValueError, make_array, 4, 2, -1)
self.failUnlessRaises(ValueError, make_array, 8, 3, 1)
#self.failUnlessRaises(ValueError, make_array, 8, 3, 0)
#self.failUnlessRaises(ValueError, lambda: ndarray([1], strides=4))
def test_set_stridesattr(self):
x = self.one
def make_array(size, offset, strides):
try:
r = ndarray([size], dtype=int, buffer=x, offset=offset*x.itemsize)
except:
pass
r.strides = strides=strides*x.itemsize
return r
assert_equal(make_array(4, 4, -1), array([4, 3, 2, 1]))
self.failUnlessRaises(ValueError, make_array, 4, 4, -2)
self.failUnlessRaises(ValueError, make_array, 4, 2, -1)
self.failUnlessRaises(ValueError, make_array, 8, 3, 1)
#self.failUnlessRaises(ValueError, make_array, 8, 3, 0)
def test_fill(self):
for t in "?bhilqpBHILQPfdgFDGO":
x = empty((3,2,1), t)
y = empty((3,2,1), t)
x.fill(1)
y[...] = 1
assert_equal(x,y)
x = array([(0,0.0), (1,1.0)], dtype='i4,f8')
x.fill(x[0])
assert_equal(x['f1'][1], x['f1'][0])
class TestDtypedescr(TestCase):
def test_construction(self):
d1 = dtype('i4')
assert_equal(d1, dtype(int32))
d2 = dtype('f8')
assert_equal(d2, dtype(float64))
class TestZeroRank(TestCase):
def setUp(self):
self.d = array(0), array('x', object)
def test_ellipsis_subscript(self):
a,b = self.d
self.failUnlessEqual(a[...], 0)
self.failUnlessEqual(b[...], 'x')
self.failUnless(a[...] is a)
self.failUnless(b[...] is b)
def test_empty_subscript(self):
a,b = self.d
self.failUnlessEqual(a[()], 0)
self.failUnlessEqual(b[()], 'x')
self.failUnless(type(a[()]) is a.dtype.type)
self.failUnless(type(b[()]) is str)
def test_invalid_subscript(self):
a,b = self.d
self.failUnlessRaises(IndexError, lambda x: x[0], a)
self.failUnlessRaises(IndexError, lambda x: x[0], b)
self.failUnlessRaises(IndexError, lambda x: x[array([], int)], a)
self.failUnlessRaises(IndexError, lambda x: x[array([], int)], b)
def test_ellipsis_subscript_assignment(self):
a,b = self.d
a[...] = 42
self.failUnlessEqual(a, 42)
b[...] = ''
self.failUnlessEqual(b.item(), '')
def test_empty_subscript_assignment(self):
a,b = self.d
a[()] = 42
self.failUnlessEqual(a, 42)
b[()] = ''
self.failUnlessEqual(b.item(), '')
def test_invalid_subscript_assignment(self):
a,b = self.d
def assign(x, i, v):
x[i] = v
self.failUnlessRaises(IndexError, assign, a, 0, 42)
self.failUnlessRaises(IndexError, assign, b, 0, '')
self.failUnlessRaises(ValueError, assign, a, (), '')
def test_newaxis(self):
a,b = self.d
self.failUnlessEqual(a[newaxis].shape, (1,))
self.failUnlessEqual(a[..., newaxis].shape, (1,))
self.failUnlessEqual(a[newaxis, ...].shape, (1,))
self.failUnlessEqual(a[..., newaxis].shape, (1,))
self.failUnlessEqual(a[newaxis, ..., newaxis].shape, (1,1))
self.failUnlessEqual(a[..., newaxis, newaxis].shape, (1,1))
self.failUnlessEqual(a[newaxis, newaxis, ...].shape, (1,1))
self.failUnlessEqual(a[(newaxis,)*10].shape, (1,)*10)
def test_invalid_newaxis(self):
a,b = self.d
def subscript(x, i): x[i]
self.failUnlessRaises(IndexError, subscript, a, (newaxis, 0))
self.failUnlessRaises(IndexError, subscript, a, (newaxis,)*50)
def test_constructor(self):
x = ndarray(())
x[()] = 5
self.failUnlessEqual(x[()], 5)
y = ndarray((),buffer=x)
y[()] = 6
self.failUnlessEqual(x[()], 6)
def test_output(self):
x = array(2)
self.failUnlessRaises(ValueError, add, x, [1], x)
class TestScalarIndexing(TestCase):
def setUp(self):
self.d = array([0,1])[0]
def test_ellipsis_subscript(self):
a = self.d
self.failUnlessEqual(a[...], 0)
self.failUnlessEqual(a[...].shape,())
def test_empty_subscript(self):
a = self.d
self.failUnlessEqual(a[()], 0)
self.failUnlessEqual(a[()].shape,())
def test_invalid_subscript(self):
a = self.d
self.failUnlessRaises(IndexError, lambda x: x[0], a)
self.failUnlessRaises(IndexError, lambda x: x[array([], int)], a)
def test_invalid_subscript_assignment(self):
a = self.d
def assign(x, i, v):
x[i] = v
self.failUnlessRaises(TypeError, assign, a, 0, 42)
def test_newaxis(self):
a = self.d
self.failUnlessEqual(a[newaxis].shape, (1,))
self.failUnlessEqual(a[..., newaxis].shape, (1,))
self.failUnlessEqual(a[newaxis, ...].shape, (1,))
self.failUnlessEqual(a[..., newaxis].shape, (1,))
self.failUnlessEqual(a[newaxis, ..., newaxis].shape, (1,1))
self.failUnlessEqual(a[..., newaxis, newaxis].shape, (1,1))
self.failUnlessEqual(a[newaxis, newaxis, ...].shape, (1,1))
self.failUnlessEqual(a[(newaxis,)*10].shape, (1,)*10)
def test_invalid_newaxis(self):
a = self.d
def subscript(x, i): x[i]
self.failUnlessRaises(IndexError, subscript, a, (newaxis, 0))
self.failUnlessRaises(IndexError, subscript, a, (newaxis,)*50)
class TestCreation(TestCase):
def test_from_attribute(self):
class x(object):
def __array__(self, dtype=None):
pass
self.failUnlessRaises(ValueError, array, x())
def test_from_string(self) :
types = np.typecodes['AllInteger'] + np.typecodes['Float']
nstr = ['123','123']
result = array([123, 123], dtype=int)
for type in types :
msg = 'String conversion for %s' % type
assert_equal(array(nstr, dtype=type), result, err_msg=msg)
class TestBool(TestCase):
def test_test_interning(self):
a0 = bool_(0)
b0 = bool_(False)
self.failUnless(a0 is b0)
a1 = bool_(1)
b1 = bool_(True)
self.failUnless(a1 is b1)
self.failUnless(array([True])[0] is a1)
self.failUnless(array(True)[()] is a1)
class TestMethods(TestCase):
def test_test_round(self):
assert_equal(array([1.2,1.5]).round(), [1,2])
assert_equal(array(1.5).round(), 2)
assert_equal(array([12.2,15.5]).round(-1), [10,20])
assert_equal(array([12.15,15.51]).round(1), [12.2,15.5])
def test_transpose(self):
a = array([[1,2],[3,4]])
assert_equal(a.transpose(), [[1,3],[2,4]])
self.failUnlessRaises(ValueError, lambda: a.transpose(0))
self.failUnlessRaises(ValueError, lambda: a.transpose(0,0))
self.failUnlessRaises(ValueError, lambda: a.transpose(0,1,2))
def test_sort(self):
# test ordering for floats and complex containing nans. It is only
# necessary to check the lessthan comparison, so sorts that
# only follow the insertion sort path are sufficient. We only
# test doubles and complex doubles as the logic is the same.
# check doubles
msg = "Test real sort order with nans"
a = np.array([np.nan, 1, 0])
b = sort(a)
assert_equal(b, a[::-1], msg)
# check complex
msg = "Test complex sort order with nans"
a = np.zeros(9, dtype=np.complex128)
a.real += [np.nan, np.nan, np.nan, 1, 0, 1, 1, 0, 0]
a.imag += [np.nan, 1, 0, np.nan, np.nan, 1, 0, 1, 0]
b = sort(a)
assert_equal(b, a[::-1], msg)
# all c scalar sorts use the same code with different types
# so it suffices to run a quick check with one type. The number
# of sorted items must be greater than ~50 to check the actual
# algorithm because quick and merge sort fall over to insertion
# sort for small arrays.
a = np.arange(100)
b = a[::-1].copy()
for kind in ['q','m','h'] :
msg = "scalar sort, kind=%s" % kind
c = a.copy();
c.sort(kind=kind)
assert_equal(c, a, msg)
c = b.copy();
c.sort(kind=kind)
assert_equal(c, a, msg)
# test complex sorts. These use the same code as the scalars
# but the compare fuction differs.
ai = a*1j + 1
bi = b*1j + 1
for kind in ['q','m','h'] :
msg = "complex sort, real part == 1, kind=%s" % kind
c = ai.copy();
c.sort(kind=kind)
assert_equal(c, ai, msg)
c = bi.copy();
c.sort(kind=kind)
assert_equal(c, ai, msg)
ai = a + 1j
bi = b + 1j
for kind in ['q','m','h'] :
msg = "complex sort, imag part == 1, kind=%s" % kind
c = ai.copy();
c.sort(kind=kind)
assert_equal(c, ai, msg)
c = bi.copy();
c.sort(kind=kind)
assert_equal(c, ai, msg)
# test string sorts.
s = 'aaaaaaaa'
a = np.array([s + chr(i) for i in range(100)])
b = a[::-1].copy()
for kind in ['q', 'm', 'h'] :
msg = "string sort, kind=%s" % kind
c = a.copy();
c.sort(kind=kind)
assert_equal(c, a, msg)
c = b.copy();
c.sort(kind=kind)
assert_equal(c, a, msg)
# test unicode sort.
s = 'aaaaaaaa'
a = np.array([s + chr(i) for i in range(100)], dtype=np.unicode)
b = a[::-1].copy()
for kind in ['q', 'm', 'h'] :
msg = "unicode sort, kind=%s" % kind
c = a.copy();
c.sort(kind=kind)
assert_equal(c, a, msg)
c = b.copy();
c.sort(kind=kind)
assert_equal(c, a, msg)
# todo, check object array sorts.
# check axis handling. This should be the same for all type
# specific sorts, so we only check it for one type and one kind
a = np.array([[3,2],[1,0]])
b = np.array([[1,0],[3,2]])
c = np.array([[2,3],[0,1]])
d = a.copy()
d.sort(axis=0)
assert_equal(d, b, "test sort with axis=0")
d = a.copy()
d.sort(axis=1)
assert_equal(d, c, "test sort with axis=1")
d = a.copy()
d.sort()
assert_equal(d, c, "test sort with default axis")
# using None is known fail at this point
# d = a.copy()
# d.sort(axis=None)
#assert_equal(d, c, "test sort with axis=None")
def test_sort_order(self):
# Test sorting an array with fields
x1=np.array([21,32,14])
x2=np.array(['my','first','name'])
x3=np.array([3.1,4.5,6.2])
r=np.rec.fromarrays([x1,x2,x3],names='id,word,number')
r.sort(order=['id'])
assert_equal(r.id, array([14,21,32]))
assert_equal(r.word, array(['name','my','first']))
assert_equal(r.number, array([6.2,3.1,4.5]))
r.sort(order=['word'])
assert_equal(r.id, array([32,21,14]))
assert_equal(r.word, array(['first','my','name']))
assert_equal(r.number, array([4.5,3.1,6.2]))
r.sort(order=['number'])
assert_equal(r.id, array([21,32,14]))
assert_equal(r.word, array(['my','first','name']))
assert_equal(r.number, array([3.1,4.5,6.2]))
def test_argsort(self):
# all c scalar argsorts use the same code with different types
# so it suffices to run a quick check with one type. The number
# of sorted items must be greater than ~50 to check the actual
# algorithm because quick and merge sort fall over to insertion
# sort for small arrays.
a = np.arange(100)
b = a[::-1].copy()
for kind in ['q','m','h'] :
msg = "scalar argsort, kind=%s" % kind
assert_equal(a.copy().argsort(kind=kind), a, msg)
assert_equal(b.copy().argsort(kind=kind), b, msg)
# test complex argsorts. These use the same code as the scalars
# but the compare fuction differs.
ai = a*1j + 1
bi = b*1j + 1
for kind in ['q','m','h'] :
msg = "complex argsort, kind=%s" % kind
assert_equal(ai.copy().argsort(kind=kind), a, msg)
assert_equal(bi.copy().argsort(kind=kind), b, msg)
ai = a + 1j
bi = b + 1j
for kind in ['q','m','h'] :
msg = "complex argsort, kind=%s" % kind
assert_equal(ai.copy().argsort(kind=kind), a, msg)
assert_equal(bi.copy().argsort(kind=kind), b, msg)
# test string argsorts.
s = 'aaaaaaaa'
a = np.array([s + chr(i) for i in range(100)])
b = a[::-1].copy()
r = arange(100)
rr = r[::-1].copy()
for kind in ['q', 'm', 'h'] :
msg = "string argsort, kind=%s" % kind
assert_equal(a.copy().argsort(kind=kind), r, msg)
assert_equal(b.copy().argsort(kind=kind), rr, msg)
# test unicode argsorts.
s = 'aaaaaaaa'
a = np.array([s + chr(i) for i in range(100)], dtype=np.unicode)
b = a[::-1].copy()
r = arange(100)
rr = r[::-1].copy()
for kind in ['q', 'm', 'h'] :
msg = "unicode argsort, kind=%s" % kind
assert_equal(a.copy().argsort(kind=kind), r, msg)
assert_equal(b.copy().argsort(kind=kind), rr, msg)
# todo, check object array argsorts.
# check axis handling. This should be the same for all type
# specific argsorts, so we only check it for one type and one kind
a = np.array([[3,2],[1,0]])
b = np.array([[1,1],[0,0]])
c = np.array([[1,0],[1,0]])
assert_equal(a.copy().argsort(axis=0), b)
assert_equal(a.copy().argsort(axis=1), c)
assert_equal(a.copy().argsort(), c)
# using None is known fail at this point
#assert_equal(a.copy().argsort(axis=None, c)
# check that stable argsorts are stable
r = np.arange(100)
# scalars
a = np.zeros(100)
assert_equal(a.argsort(kind='m'), r)
# complex
a = np.zeros(100, dtype=np.complex)
assert_equal(a.argsort(kind='m'), r)
# string
a = np.array(['aaaaaaaaa' for i in range(100)])
assert_equal(a.argsort(kind='m'), r)
# unicode
a = np.array(['aaaaaaaaa' for i in range(100)], dtype=np.unicode)
assert_equal(a.argsort(kind='m'), r)
def test_searchsorted(self):
# test for floats and complex containing nans. The logic is the
# same for all float types so only test double types for now.
# The search sorted routines use the compare functions for the
# array type, so this checks if that is consistent with the sort
# order.
# check double
a = np.array([np.nan, 1, 0])
a = np.array([0, 1, np.nan])
msg = "Test real searchsorted with nans, side='l'"
b = a.searchsorted(a, side='l')
assert_equal(b, np.arange(3), msg)
msg = "Test real searchsorted with nans, side='r'"
b = a.searchsorted(a, side='r')
assert_equal(b, np.arange(1,4), msg)
# check double complex
a = np.zeros(9, dtype=np.complex128)
a.real += [0, 0, 1, 1, 0, 1, np.nan, np.nan, np.nan]
a.imag += [0, 1, 0, 1, np.nan, np.nan, 0, 1, np.nan]
msg = "Test complex searchsorted with nans, side='l'"
b = a.searchsorted(a, side='l')
assert_equal(b, np.arange(9), msg)
msg = "Test complex searchsorted with nans, side='r'"
b = a.searchsorted(a, side='r')
assert_equal(b, np.arange(1,10), msg)
def test_flatten(self):
x0 = np.array([[1,2,3],[4,5,6]], np.int32)
x1 = np.array([[[1,2],[3,4]],[[5,6],[7,8]]], np.int32)
y0 = np.array([1,2,3,4,5,6], np.int32)
y0f = np.array([1,4,2,5,3,6], np.int32)
y1 = np.array([1,2,3,4,5,6,7,8], np.int32)
y1f = np.array([1,5,3,7,2,6,4,8], np.int32)
assert_equal(x0.flatten(), y0)
assert_equal(x0.flatten('F'), y0f)
assert_equal(x0.flatten('F'), x0.T.flatten())
assert_equal(x1.flatten(), y1)
assert_equal(x1.flatten('F'), y1f)
assert_equal(x1.flatten('F'), x1.T.flatten())
class TestSubscripting(TestCase):
def test_test_zero_rank(self):
x = array([1,2,3])
self.failUnless(isinstance(x[0], int))
self.failUnless(type(x[0, ...]) is ndarray)
class TestPickling(TestCase):
def test_both(self):
import pickle
carray = array([[2,9],[7,0],[3,8]])
tarray = transpose(carray)
assert_equal(carray, pickle.loads(carray.dumps()))
assert_equal(tarray, pickle.loads(tarray.dumps()))
# version 0 pickles, using protocol=2 to pickle
# version 0 doesn't have a version field
def test_version0_int8(self):
s = '\x80\x02cnumpy.core._internal\n_reconstruct\nq\x01cnumpy\nndarray\nq\x02K\x00\x85U\x01b\x87Rq\x03(K\x04\x85cnumpy\ndtype\nq\x04U\x02i1K\x00K\x01\x87Rq\x05(U\x01|NNJ\xff\xff\xff\xffJ\xff\xff\xff\xfftb\x89U\x04\x01\x02\x03\x04tb.'
a = array([1,2,3,4], dtype=int8)
p = loads(s)
assert_equal(a, p)
def test_version0_float32(self):
s = '\x80\x02cnumpy.core._internal\n_reconstruct\nq\x01cnumpy\nndarray\nq\x02K\x00\x85U\x01b\x87Rq\x03(K\x04\x85cnumpy\ndtype\nq\x04U\x02f4K\x00K\x01\x87Rq\x05(U\x01
= g2, [g1[i] >= g2[i] for i in [0,1,2]])
assert_array_equal(g1 < g2, [g1[i] < g2[i] for i in [0,1,2]])
assert_array_equal(g1 > g2, [g1[i] > g2[i] for i in [0,1,2]])
def test_mixed(self):
g1 = array(["spam","spa","spammer","and eggs"])
g2 = "spam"
assert_array_equal(g1 == g2, [x == g2 for x in g1])
assert_array_equal(g1 != g2, [x != g2 for x in g1])
assert_array_equal(g1 < g2, [x < g2 for x in g1])
assert_array_equal(g1 > g2, [x > g2 for x in g1])
assert_array_equal(g1 <= g2, [x <= g2 for x in g1])
assert_array_equal(g1 >= g2, [x >= g2 for x in g1])
def test_unicode(self):
g1 = array([u"This",u"is",u"example"])
g2 = array([u"This",u"was",u"example"])
assert_array_equal(g1 == g2, [g1[i] == g2[i] for i in [0,1,2]])
assert_array_equal(g1 != g2, [g1[i] != g2[i] for i in [0,1,2]])
assert_array_equal(g1 <= g2, [g1[i] <= g2[i] for i in [0,1,2]])
assert_array_equal(g1 >= g2, [g1[i] >= g2[i] for i in [0,1,2]])
assert_array_equal(g1 < g2, [g1[i] < g2[i] for i in [0,1,2]])
assert_array_equal(g1 > g2, [g1[i] > g2[i] for i in [0,1,2]])
class TestArgmax(TestCase):
def test_all(self):
a = np.random.normal(0,1,(4,5,6,7,8))
for i in xrange(a.ndim):
amax = a.max(i)
aargmax = a.argmax(i)
axes = range(a.ndim)
axes.remove(i)
assert all(amax == aargmax.choose(*a.transpose(i,*axes)))
class TestMinMax(TestCase):
def test_scalar(self):
assert_raises(ValueError, np.amax, 1, 1)
assert_raises(ValueError, np.amin, 1, 1)
assert_equal(np.amax(1, axis=0), 1)
assert_equal(np.amin(1, axis=0), 1)
assert_equal(np.amax(1, axis=None), 1)
assert_equal(np.amin(1, axis=None), 1)
def test_axis(self):
assert_raises(ValueError, np.amax, [1,2,3], 1000)
assert_equal(np.amax([[1,2,3]], axis=1), 3)
class TestNewaxis(TestCase):
def test_basic(self):
sk = array([0,-0.1,0.1])
res = 250*sk[:,newaxis]
assert_almost_equal(res.ravel(),250*sk)
class TestClip(TestCase):
def _check_range(self,x,cmin,cmax):
assert np.all(x >= cmin)
assert np.all(x <= cmax)
def _clip_type(self,type_group,array_max,
clip_min,clip_max,inplace=False,
expected_min=None,expected_max=None):
if expected_min is None:
expected_min = clip_min
if expected_max is None:
expected_max = clip_max
for T in np.sctypes[type_group]:
if sys.byteorder == 'little':
byte_orders = ['=','>']
else:
byte_orders = ['<','=']
for byteorder in byte_orders:
dtype = np.dtype(T).newbyteorder(byteorder)
x = (np.random.random(1000) * array_max).astype(dtype)
if inplace:
x.clip(clip_min,clip_max,x)
else:
x = x.clip(clip_min,clip_max)
byteorder = '='
if x.dtype.byteorder == '|': byteorder = '|'
assert_equal(x.dtype.byteorder,byteorder)
self._check_range(x,expected_min,expected_max)
return x
def test_basic(self):
for inplace in [False, True]:
self._clip_type('float',1024,-12.8,100.2, inplace=inplace)
self._clip_type('float',1024,0,0, inplace=inplace)
self._clip_type('int',1024,-120,100.5, inplace=inplace)
self._clip_type('int',1024,0,0, inplace=inplace)
x = self._clip_type('uint',1024,-120,100,expected_min=0, inplace=inplace)
x = self._clip_type('uint',1024,0,0, inplace=inplace)
def test_record_array(self):
rec = np.array([(-5, 2.0, 3.0), (5.0, 4.0, 3.0)],
dtype=[('x', '= 3)
x = val.clip(min=3)
assert np.all(x >= 3)
x = val.clip(max=4)
assert np.all(x <= 4)
class TestPutmask(TestCase):
def tst_basic(self,x,T,mask,val):
np.putmask(x,mask,val)
assert np.all(x[mask] == T(val))
assert x.dtype == T
def test_ip_types(self):
unchecked_types = [str, unicode, np.void, object]
x = np.random.random(1000)*100
mask = x < 40
for val in [-100,0,15]:
for types in np.sctypes.itervalues():
for T in types:
if T not in unchecked_types:
yield self.tst_basic,x.copy().astype(T),T,mask,val
def test_mask_size(self):
self.failUnlessRaises(ValueError, np.putmask,
np.array([1,2,3]), [True], 5)
def tst_byteorder(self,dtype):
x = np.array([1,2,3],dtype)
np.putmask(x,[True,False,True],-1)
assert_array_equal(x,[-1,2,-1])
def test_ip_byteorder(self):
for dtype in ('>i4','f8'), ('z', 'i4','f8'), ('z', '']:
for dtype in [float,int,np.complex]:
dt = np.dtype(dtype).newbyteorder(byteorder)
x = (np.random.random((4,7))*5).astype(dt)
buf = x.tostring()
yield self.tst_basic,buf,x.flat,{'dtype':dt}
class TestResize(TestCase):
def test_basic(self):
x = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]])
x.resize((5,5))
assert_array_equal(x.flat[:9],np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]]).flat)
assert_array_equal(x[9:].flat,0)
def test_check_reference(self):
x = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]])
y = x
self.failUnlessRaises(ValueError,x.resize,(5,1))
class TestRecord(TestCase):
def test_field_rename(self):
dt = np.dtype([('f',float),('i',int)])
dt.names = ['p','q']
assert_equal(dt.names,['p','q'])
class TestView(TestCase):
def test_basic(self):
x = np.array([(1,2,3,4),(5,6,7,8)],dtype=[('r',np.int8),('g',np.int8),
('b',np.int8),('a',np.int8)])
# We must be specific about the endianness here:
y = x.view(dtype='