diff options
author | Travis Oliphant <oliphant@enthought.com> | 2007-03-30 23:47:39 +0000 |
---|---|---|
committer | Travis Oliphant <oliphant@enthought.com> | 2007-03-30 23:47:39 +0000 |
commit | e784b53e64e22bb4a1a65853505b74d774716cc2 (patch) | |
tree | dd60be8f0456741f8cb5d83ca7433521f3e9766c | |
parent | 72dea1afaeea8deab2fccb599fba2c7f1f811af3 (diff) | |
download | numpy-e784b53e64e22bb4a1a65853505b74d774716cc2.tar.gz |
Add tests for clipping. Also some good tests on choose and convert_tocommontype. Fix some mixed-type problems. Fix problems with clipping. More to be done to close ticket #425
-rw-r--r-- | numpy/core/include/numpy/ndarrayobject.h | 3 | ||||
-rw-r--r-- | numpy/core/src/arrayobject.c | 14 | ||||
-rw-r--r-- | numpy/core/src/arraytypes.inc.src | 12 | ||||
-rw-r--r-- | numpy/core/src/multiarraymodule.c | 85 | ||||
-rw-r--r-- | numpy/core/tests/test_multiarray.py | 24 | ||||
-rw-r--r-- | numpy/core/tests/test_numeric.py | 415 |
6 files changed, 502 insertions, 51 deletions
diff --git a/numpy/core/include/numpy/ndarrayobject.h b/numpy/core/include/numpy/ndarrayobject.h index b10ab2e5e..b86f7bdd7 100644 --- a/numpy/core/include/numpy/ndarrayobject.h +++ b/numpy/core/include/numpy/ndarrayobject.h @@ -1760,6 +1760,9 @@ typedef struct { #define PyArray_ISBEHAVED_RO(m) PyArray_FLAGSWAP(m, NPY_ALIGNED) +#define PyDataType_ISNOTSWAPPED(d) PyArray_ISNBO(((PyArray_Descr *)(d))->byteorder) + + /* This is the form of the struct that's returned pointed by the PyCObject attribute of an array __array_struct__. See diff --git a/numpy/core/src/arrayobject.c b/numpy/core/src/arrayobject.c index 5a5747b1f..4de6bd3be 100644 --- a/numpy/core/src/arrayobject.c +++ b/numpy/core/src/arrayobject.c @@ -10389,6 +10389,7 @@ PyArray_MultiIterNew(int n, ...) PyErr_Format(PyExc_ValueError, "Need between 2 and (%d) " \ "array objects (inclusive).", NPY_MAXARGS); + return NULL; } /* fprintf(stderr, "multi new...");*/ @@ -11972,6 +11973,17 @@ arrayflags_print(PyArrayFlagsObject *self) } +static int +arrayflags_compare(PyArrayFlagsObject *self, PyArrayFlagsObject *other) +{ + if (self->flags == other->flags) + return 0; + else if (self->flags < other->flags) + return -1; + else + return 1; +} + static PyMappingMethods arrayflags_as_mapping = { #if PY_VERSION_HEX >= 0x02050000 (lenfunc)NULL, /*mp_length*/ @@ -12009,7 +12021,7 @@ static PyTypeObject PyArrayFlags_Type = { 0, /* tp_print */ 0, /* tp_getattr */ 0, /* tp_setattr */ - 0, /* tp_compare */ + (cmpfunc)arrayflags_compare, /* tp_compare */ (reprfunc)arrayflags_print, /* tp_repr */ 0, /* tp_as_number */ 0, /* tp_as_sequence */ diff --git a/numpy/core/src/arraytypes.inc.src b/numpy/core/src/arraytypes.inc.src index 4872a73ca..1e32e1361 100644 --- a/numpy/core/src/arraytypes.inc.src +++ b/numpy/core/src/arraytypes.inc.src @@ -2069,7 +2069,7 @@ static void @type@ max_val, min_val; max_val = *max; - min_val = *min; + min_val = *min; for (i = 0; i < ni; i++) { if (in[i] < min_val) { @@ -2093,14 +2093,14 @@ static void register npy_intp i; @type@ max_val, min_val; - max_val = *max; min_val = *min; - + max_val = *max; + for (i = 0; i < ni; i++) { - if (PyArray_CLT(in[i], max_val)) { - out[i] = max_val; + if (PyArray_CLT(in[i], min_val)) { + out[i] = min_val; } else if (PyArray_CGT(in[i], max_val)) { - out[i] = max_val; + out[i] = max_val; } } return; diff --git a/numpy/core/src/multiarraymodule.c b/numpy/core/src/multiarraymodule.c index c4f48ff60..2b826ec1e 100644 --- a/numpy/core/src/multiarraymodule.c +++ b/numpy/core/src/multiarraymodule.c @@ -1145,7 +1145,7 @@ PyArray_Clip(PyArrayObject *self, PyObject *min, PyObject *max, PyArrayObject *o PyArrayObject *maxa=NULL; PyArrayObject *mina=NULL; PyArrayObject *newout=NULL, *newin=NULL; - PyArray_Descr *indescr; + PyArray_Descr *indescr, *newdescr; PyObject *zero; func = self->descr->f->fastclip; @@ -1155,20 +1155,40 @@ PyArray_Clip(PyArrayObject *self, PyObject *min, PyObject *max, PyArrayObject *o /* Use the fast scalar clip function */ - if (PyArray_ISNOTSWAPPED(self)) { - indescr = PyArray_DescrNewByteorder(self->descr, '='); - if (indescr == NULL) goto fail; + /* First we need to figure out the correct type */ + indescr = PyArray_DescrFromObject(min, NULL); + if (indescr == NULL) return NULL; + newdescr = PyArray_DescrFromObject(max, indescr); + Py_DECREF(indescr); + + if (newdescr == NULL) return NULL; + /* Use the scalar descriptor only if it is of a bigger + KIND than the input array (and then find the + type that matches both). + */ + if (PyArray_ScalarKind(newdescr->type_num, NULL) > + PyArray_ScalarKind(self->descr->type_num, NULL)) { + indescr = _array_small_type(newdescr, self->descr); + func = indescr->f->fastclip; } - else { + else { indescr = self->descr; Py_INCREF(indescr); } + Py_DECREF(newdescr); + + if (!PyDataType_ISNOTSWAPPED(indescr)) { + PyArray_Descr *descr2; + descr2 = PyArray_DescrNewByteorder(indescr, '='); + Py_DECREF(indescr); + if (descr2 == NULL) goto fail; + indescr = descr2; + } /* Convert max to an array */ maxa = (NPY_AO *)PyArray_FromAny(max, indescr, 0, 0, NPY_DEFAULT, NULL); if (maxa == NULL) return NULL; - Py_INCREF(indescr); /* If we are unsigned, then make sure min is not <0 */ @@ -1197,6 +1217,7 @@ PyArray_Clip(PyArrayObject *self, PyObject *min, PyObject *max, PyArrayObject *o } /* Convert min to an array */ + Py_INCREF(indescr); mina = (NPY_AO *)PyArray_FromAny(min, indescr, 0, 0, NPY_DEFAULT, NULL); Py_DECREF(min); @@ -1206,7 +1227,7 @@ PyArray_Clip(PyArrayObject *self, PyObject *min, PyObject *max, PyArrayObject *o /* Check to see if input is single-segment, aligned, and in native byteorder */ if (PyArray_ISONESEGMENT(self) && PyArray_CHKFLAGS(self, ALIGNED) && - PyArray_ISNOTSWAPPED(self)) + PyArray_ISNOTSWAPPED(self) && (self->descr == indescr)) ingood = 1; if (!ingood) { @@ -1223,8 +1244,10 @@ PyArray_Clip(PyArrayObject *self, PyObject *min, PyObject *max, PyArrayObject *o } /* At this point, newin is a single-segment, aligned, and correct - byte-order array if ingood == 0, then it is a copy, otherwise, - it is the input + byte-order array of the correct type + + if ingood == 0, then it is a copy, otherwise, + it is the original input. */ /* If we have already made a copy of the data, then use @@ -1254,8 +1277,8 @@ PyArray_Clip(PyArrayObject *self, PyObject *min, PyObject *max, PyArrayObject *o outgood = 1; } if (!outgood && PyArray_ISONESEGMENT(out) && - PyArray_CHKFLAGS(out, ALIGNED) && - PyArray_ISNOTSWAPPED(out)) { + PyArray_CHKFLAGS(out, ALIGNED) && PyArray_ISNOTSWAPPED(out) && + PyArray_EquivTypes(out->descr, indescr)) { outgood = 1; } @@ -1263,20 +1286,12 @@ PyArray_Clip(PyArrayObject *self, PyObject *min, PyObject *max, PyArrayObject *o /* Create one, now */ if (!outgood) { int oflags; - PyArray_Descr *odescr; if (PyArray_ISFORTRAN(out)) - oflags = NPY_FARRAY | NPY_UPDATEIFCOPY; + oflags = NPY_FARRAY; else - oflags = NPY_CARRAY | NPY_UPDATEIFCOPY; - if (PyArray_ISNOTSWAPPED(out)) { - odescr = PyArray_DescrNewByteorder(out->descr, '='); - if (odescr == NULL) goto fail; - } - else { - odescr = out->descr; - Py_INCREF(odescr); - } - newout = (NPY_AO*)PyArray_FromArray(out, odescr, oflags); + oflags = NPY_CARRAY; + oflags |= NPY_UPDATEIFCOPY | NPY_FORCECAST; + newout = (NPY_AO*)PyArray_FromArray(out, indescr, oflags); if (newout == NULL) goto fail; } else { @@ -1284,6 +1299,13 @@ PyArray_Clip(PyArrayObject *self, PyObject *min, PyObject *max, PyArrayObject *o Py_INCREF(newout); } + /* make sure the shape of the output array is the same */ + if (!PyArray_SAMESHAPE(newin, newout)) { + PyErr_SetString(PyExc_ValueError, "clip: Output array must have the" + "same shape as the input."); + goto fail; + } + if (newout->data != newin->data) { memcpy(newout->data, newin->data, PyArray_NBYTES(newin)); } @@ -1305,6 +1327,7 @@ PyArray_Clip(PyArrayObject *self, PyObject *min, PyObject *max, PyArrayObject *o Py_XDECREF(maxa); Py_XDECREF(mina); Py_XDECREF(newin); + PyArray_XDECREF_ERR(newout); return NULL; } @@ -2130,19 +2153,21 @@ PyArray_ConvertToCommonType(PyObject *op, int *retn) handles both intype and stype and don't forcecast the scalars. */ - + if (!PyArray_CanCoerceScalar(stype->type_num, intype->type_num, scalarkind)) { + newtype = _array_small_type(intype, stype); Py_XDECREF(intype); - intype = stype; - Py_INCREF(stype); + intype = newtype; } for (i=0; i<n; i++) { Py_XDECREF(mps[i]); mps[i] = NULL; } } + + /* Make sure all arrays are actual array objects. */ for(i=0; i<n; i++) { int flags = CARRAY; @@ -2217,25 +2242,23 @@ PyArray_Choose(PyArrayObject *ip, PyObject *op, PyArrayObject *ret, sizes[i] = PyArray_NBYTES(mps[i]); } - Py_INCREF(mps[0]->descr); if (!ret) { + Py_INCREF(mps[0]->descr); ret = (PyArrayObject *)PyArray_NewFromDescr(ap->ob_type, mps[0]->descr, ap->nd, ap->dimensions, NULL, NULL, 0, (PyObject *)ap); - if (ret == NULL) goto fail; } else { PyArrayObject *obj; - int flags = NPY_CARRAY | NPY_UPDATEIFCOPY; + int flags = NPY_CARRAY | NPY_UPDATEIFCOPY | NPY_FORCECAST; if (PyArray_SIZE(ret) != PyArray_SIZE(ap)) { PyErr_SetString(PyExc_TypeError, "invalid shape for output array."); ret = NULL; - Py_DECREF(mps[0]->descr); goto fail; } if (clipmode == NPY_RAISE) { @@ -2245,12 +2268,14 @@ PyArray_Choose(PyArrayObject *ip, PyObject *op, PyArrayObject *ret, */ flags |= NPY_ENSURECOPY; } + Py_INCREF(mps[0]->descr); obj = (PyArrayObject *)PyArray_FromArray(ret, mps[0]->descr, flags); if (obj != ret) copyret = 1; ret = obj; } + if (ret == NULL) goto fail; elsize = ret->descr->elsize; m = PyArray_SIZE(ret); self_data = (intp *)ap->data; diff --git a/numpy/core/tests/test_multiarray.py b/numpy/core/tests/test_multiarray.py index b2b09b36e..d4c042a50 100644 --- a/numpy/core/tests/test_multiarray.py +++ b/numpy/core/tests/test_multiarray.py @@ -371,7 +371,6 @@ class test_clip(NumpyTestCase): for byteorder in byte_orders: dtype = N.dtype(T).newbyteorder(byteorder) - if dtype.byteorder == '|': byteorder = '|' x = (N.random.random(1000) * array_max).astype(dtype) if inplace: @@ -379,28 +378,27 @@ class test_clip(NumpyTestCase): else: x = x.clip(clip_min,clip_max) + if x.dtype.byteorder == '|': byteorder = '|' assert_equal(byteorder,x.dtype.byteorder) self._check_range(x,expected_min,expected_max) return x def check_basic(self): - for inplace in [False]: # XXX fixme -> ,True]: - self._clip_type('float',1024,-12.8,100.2) - self._clip_type('float',1024,0,0) + 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) - self._clip_type('int',1024,0,0) + self._clip_type('int',1024,-120,100.5, inplace=inplace) + self._clip_type('int',1024,0,0, inplace=inplace) - # XXX fixme - #x = self._check_type('uint',1024,-120,100,expected_min=0) - x = self._clip_type('uint',1024,0,0) + x = self._clip_type('uint',1024,-120,100,expected_min=0, inplace=inplace) + x = self._clip_type('uint',1024,0,0, inplace=inplace) - # XXX fixme - def check_record_array(self,level=2): + def check_record_array(self): rec = N.array([(-5, 2.0, 3.0), (5.0, 4.0, 3.0)], dtype=[('x', '<f8'), ('y', '<f8'), ('z', '<f8')]) - rec['x'].clip(-0.3,0.5) - self._check_range(rec['x'],-0.3,0.5) + y = rec['x'].clip(-0.3,0.5) + self._check_range(y,-0.3,0.5) # Import tests from unicode set_local_path() diff --git a/numpy/core/tests/test_numeric.py b/numpy/core/tests/test_numeric.py index 7242d17d0..1689bb2cc 100644 --- a/numpy/core/tests/test_numeric.py +++ b/numpy/core/tests/test_numeric.py @@ -1,5 +1,5 @@ from numpy.core import * -from numpy.random import rand, randint +from numpy.random import rand, randint, randn from numpy.testing import * from numpy.core.multiarray import dot as dot_ import sys @@ -248,6 +248,419 @@ class test_binary_repr(NumpyTestCase): def test_large(self): assert_equal(binary_repr(10736848),'101000111101010011010000') + +def assert_array_strict_equal(x, y): + assert_array_equal(x, y) + # Check flags + assert x.flags == y.flags + # check endianness + assert x.dtype.isnative == y.dtype.isnative + + +class test_clip(NumpyTestCase): + def setUp(self): + self.nr = 5 + self.nc = 3 + + def fastclip(self, a, m, M, out=None): + if out is None: + return a.clip(m,M) + else: + return a.clip(m,M,out) + + def clip(self, a, m, M, out=None): + # use slow-clip + selector = less(a, m)+2*greater(a, M) + return selector.choose((a, m, M), out=out) + + # Handy functions + def _generate_data(self, n, m): + return randn(n, m) + + def _generate_data_complex(self, n, m): + return randn(n, m) + 1.j *rand(n, m) + + def _generate_flt_data(self, n, m): + return (randn(n, m)).astype(float32) + + def _neg_byteorder(self, a): + import sys + a = asarray(a) + if sys.byteorder == 'little': + a = a.astype(a.dtype.newbyteorder('>')) + else: + a = a.astype(a.dtype.newbyteorder('<')) + return a + + def _generate_non_native_data(self, n, m): + data = randn(n, m) + data = self._neg_byteorder(data) + assert not data.dtype.isnative + return data + + def _generate_int_data(self, n, m): + return (10 * rand(n, m)).astype(int64) + + def _generate_int32_data(self, n, m): + return (10 * rand(n, m)).astype(int32) + + # Now the real test cases + def test_simple_double(self): + print "=== testing native double input with scalar min/max ===" + a = self._generate_data(self.nr, self.nc) + m = 0.1 + M = 0.6 + ac = self.fastclip(a, m, M) + act = self.clip(a, m, M) + assert_array_strict_equal(ac, act) + + def test_simple_int(self): + print "=== testing native int input with scalar min/max ===" + a = self._generate_int_data(self.nr, self.nc) + a = a.astype(int) + m = -2 + M = 4 + ac = self.fastclip(a, m, M) + act = self.clip(a, m, M) + assert_array_strict_equal(ac, act) + + def test_array_double(self): + print "=== testing native double input with array min/max ===" + a = self._generate_data(self.nr, self.nc) + m = zeros(a.shape) + M = m + 0.5 + ac = self.fastclip(a, m, M) + act = self.clip(a, m, M) + assert_array_strict_equal(ac, act) + + def test_simple_nonnative(self): + print "=== testing non native double input with scalar min/max ===" + a = self._generate_non_native_data(self.nr, self.nc) + m = -0.5 + M = 0.6 + ac = self.fastclip(a, m, M) + act = self.clip(a, m, M) + assert_array_equal(ac, act) + + print "=== testing native double input with non native double scalar min/max ===" + a = self._generate_data(self.nr, self.nc) + m = -0.5 + M = self._neg_byteorder(0.6) + assert not M.dtype.isnative + ac = self.fastclip(a, m, M) + act = self.clip(a, m, M) + assert_array_equal(ac, act) + + def test_simple_complex(self): + print "=== testing native complex input with native double scalar min/max ===" + a = 3 * self._generate_data_complex(self.nr, self.nc) + m = -0.5 + M = 1. + ac = self.fastclip(a, m, M) + act = self.clip(a, m, M) + assert_array_strict_equal(ac, act) + + print "=== testing native input with complex double scalar min/max ===" + a = 3 * self._generate_data(self.nr, self.nc) + m = -0.5 + 1.j + M = 1. + 2.j + ac = self.fastclip(a, m, M) + act = self.clip(a, m, M) + assert_array_strict_equal(ac, act) + + def test_clip_non_contig(self): + print "=== testing clip for non contiguous native input and native scalar min/max ===" + a = self._generate_data(self.nr * 2, self.nc * 3) + a = a[::2, ::3] + assert not a.flags['F_CONTIGUOUS'] + assert not a.flags['C_CONTIGUOUS'] + ac = self.fastclip(a, -1.6, 1.7) + act = self.clip(a, -1.6, 1.7) + assert_array_strict_equal(ac, act) + + def test_simple_out(self): + print "=== testing native double input with scalar min/max ===" + a = self._generate_data(self.nr, self.nc) + m = -0.5 + M = 0.6 + ac = zeros(a.shape) + act = zeros(a.shape) + self.fastclip(a, m, M, ac) + self.clip(a, m, M, act) + assert_array_strict_equal(ac, act) + + def test_simple_int32_inout(self): + print "=== testing native int32 input with double min/max and int32 out ===" + a = self._generate_int32_data(self.nr, self.nc) + m = float64(0) + M = float64(2) + ac = zeros(a.shape, dtype = int32) + act = ac.copy() + self.fastclip(a, m, M, ac) + self.clip(a, m, M, act) + assert_array_strict_equal(ac, act) + + def test_simple_int64_out(self): + print "=== testing native int32 input with int32 scalar min/max and int64 out ===" + a = self._generate_int32_data(self.nr, self.nc) + m = int32(-1) + M = int32(1) + ac = zeros(a.shape, dtype = int64) + act = ac.copy() + self.fastclip(a, m, M, ac) + self.clip(a, m, M, act) + assert_array_strict_equal(ac, act) + + def test_simple_int64_inout(self): + print "=== testing native in32 input with double array min/max and int32 out ===" + a = self._generate_int32_data(self.nr, self.nc) + m = zeros(a.shape, float64) + M = float64(1) + ac = zeros(a.shape, dtype = int32) + act = ac.copy() + self.fastclip(a, m, M, ac) + self.clip(a, m, M, act) + assert_array_strict_equal(ac, act) + + def test_simple_int32_out(self): + print "=== testing native double input with scalar min/max and int out ===" + a = self._generate_data(self.nr, self.nc) + m = -1.0 + M = 2.0 + ac = zeros(a.shape, dtype = int32) + act = ac.copy() + self.fastclip(a, m, M, ac) + self.clip(a, m, M, act) + assert_array_strict_equal(ac, act) + + def test_simple_inplace(self): + print "=== INPLACE: testing native double input with array min/max ===" + a = self._generate_data(self.nr, self.nc) + ac = a.copy() + m = zeros(a.shape) + M = 1.0 + self.fastclip(a, m, M, a) + self.clip(a, m, M, ac) + assert_array_strict_equal(a, ac) + + print "=== INPLACE: testing native double input with scalar min/max ===" + a = self._generate_data(self.nr, self.nc) + ac = a.copy() + m = -0.5 + M = 0.6 + self.fastclip(a, m, M, a) + self.clip(a, m, M, ac) + assert_array_strict_equal(a, ac) + + def test_noncontig_inplace(self): + print "=== INPLACE: testing non contiguous double input with double scalar min/max ===" + a = self._generate_data(self.nr * 2, self.nc * 3) + a = a[::2, ::3] + assert not a.flags['F_CONTIGUOUS'] + assert not a.flags['C_CONTIGUOUS'] + ac = a.copy() + m = -0.5 + M = 0.6 + self.fastclip(a, m, M, a) + self.clip(a, m, M, ac) + assert_array_equal(a, ac) + + def test_type_cast(self): + print "\n===================" + print " Test cast " + print "===================" + + print "=== testing native double input with scalar min/max ===" + a = self._generate_data(self.nr, self.nc) + m = -0.5 + M = 0.6 + ac = self.fastclip(a, m, M) + act = self.clip(a, m, M) + assert_array_strict_equal(ac, act) + + print "=== testing native int32 input with int32 scalar min/max ===" + a = self._generate_int_data(self.nr, self.nc) + a = a.astype(int32) + m = -2 + M = 4 + ac = self.fastclip(a, m, M) + act = self.clip(a, m, M) + assert_array_strict_equal(ac, act) + + print "=== testing native int32 input with float64 scalar min/max ===" + a = self._generate_int32_data(self.nr, self.nc) + m = -2 + M = 4 + ac = self.fastclip(a, float64(m), float64(M)) + act = self.clip(a, float64(m), float64(M)) + assert_array_strict_equal(ac, act) + + print "=== testing native int32 input with float32 scalar min/max ===" + a = self._generate_int32_data(self.nr, self.nc) + m = float32(-2) + M = float32(4) + act = self.fastclip(a,m,M) + ac = self.clip(a,m,M) + assert_array_strict_equal(ac, act) + + print "=== testing native int32 with double arrays min/max ===" + a = self._generate_int_data(self.nr, self.nc) + m = -0.5 + M = 1. + ac = self.fastclip(a, m * zeros(a.shape), M) + act = self.clip(a, m * zeros(a.shape), M) + assert_array_strict_equal(ac, act) + + + print "=== testing native with NON native scalar min/max===" + a = self._generate_data(self.nr, self.nc) + m = 0.5 + m_s = self._neg_byteorder(m) + M = 1. + act = self.clip(a, m_s, M) + ac = self.fastclip(a, m_s, M) + assert_array_strict_equal(ac, act) + + print "=== testing NON native with native array min/max===" + a = self._generate_data(self.nr, self.nc) + m = -0.5 * ones(a.shape) + M = 1. + a_s = self._neg_byteorder(a) + assert not a_s.dtype.isnative + act = a_s.clip(m, M) + ac = self.fastclip(a_s, m, M) + assert_array_strict_equal(ac, act) + + print "=== testing NON native with native scalar min/max===" + a = self._generate_data(self.nr, self.nc) + m = -0.5 + M = 1. + a_s = self._neg_byteorder(a) + assert not a_s.dtype.isnative + ac = self.fastclip(a_s, m , M) + act = a_s.clip(m, M) + assert_array_strict_equal(ac, act) + + print "=== testing native with NON native array min/max===" + a = self._generate_data(self.nr, self.nc) + m = -0.5 * ones(a.shape) + M = 1. + m_s = self._neg_byteorder(m) + assert not m_s.dtype.isnative + ac = self.fastclip(a, m_s , M) + act = self.clip(a, m_s, M) + assert_array_strict_equal(ac, act) + + print "=== OUT arg: testing native int32 with float min/max and float out ===" + a = self._generate_int_data(self.nr, self.nc) + b = zeros(a.shape, dtype = float32) + m = float32(-0.5) + M = float32(1) + act = self.clip(a, m, M, out = b) + ac = self.fastclip(a, m , M, out = b) + assert_array_strict_equal(ac, act) + +## # This looks like a data-type descriptor is not reference +## # counted right. +## print "=== OUT arg: testing non native with native scalar, min/max, " + \ +## "out non native===" +## a = self._generate_non_native_data(self.nr, self.nc) +## b = a.copy() +## b = b.astype(b.dtype.newbyteorder('>')) +## bt = b.copy() +## m = -0.5 +## M = 1. +## self.fastclip(a, m , M, out = b) +## self.clip(a, m, M, out = bt) +## assert_array_strict_equal(b, bt) + +## print "=== OUT arg: testing native int32 input and min/max and float out ===" +## print "\t NOT TESTED (current clip is buggy in this case)" +## a = self._generate_int_data(self.nr, self.nc) +## b = zeros(a.shape, dtype = float32) +## m = int32(0) +## M = int32(1) +## act = self.clip(a, m, M, out = b) +## ac = self.fastclip(a, m , M, out = b) +## assert_array_strict_equal(ac, act) + +## def test_clip_with_out_simple(self): +## print "=== WITH OUT testing native double input with scalar min/max ===" +## a = self._generate_data(self.nr, self.nc) +## m = -0.5 +## M = 0.6 +## ac = zeros(a.shape) +## act = zeros(a.shape) +## self.fastclip(a, m, M, ac) +## self.clip(a, m, M, act) +## assert_array_strict_equal(ac, act) + +## def test_clip_with_out_simple2(self): +## print "=== WITH OUT testing native int32 input "+\ +## "with double min/max and int32 out ===" +## a = self._generate_int32_data(self.nr, self.nc) +## m = float64(0) +## M = float64(2) +## ac = zeros(a.shape, dtype = int32) +## act = ac.copy() +## self.fastclip(a, m, M, ac) +## self.clip(a, m, M, act) +## assert_array_strict_equal(ac, act) + +## def test_clip_with_out_simple_int32(self): +## print "=== WITH OUT testing native int32 input with int32 scalar min/max and int64 out ===" +## a = self._generate_int32_data(self.nr, self.nc) +## m = int32(-1) +## M = int32(1) +## ac = zeros(a.shape, dtype = int64) +## act = ac.copy() +## self.fastclip(a, m, M, ac) +## self.clip(a, m, M, act) +## assert_array_strict_equal(ac, act) + +## def test_clip_with_out_array_int32(self): +## print "=== WITH OUT testing native int32 input with double array min/max and int32 out ===" +## a = self._generate_int32_data(self.nr, self.nc) +## m = zeros(a.shape, float64) +## M = float64(1) +## ac = zeros(a.shape, dtype = int32) +## act = ac.copy() +## self.fastclip(a, m, M, ac) +## self.clip(a, m, M, act) +## assert_array_strict_equal(ac, act) + +## def test_clip_with_out_array_outint32(self): +## print "=== WITH OUT testing native double input with scalar min/max and int out ===" +## a = self._generate_data(self.nr, self.nc) +## m = -1.0 +## M = 2.0 +## ac = zeros(a.shape, dtype = int32) +## act = ac.copy() +## self.fastclip(a, m, M, ac) +## self.clip(a, m, M, act) +## assert_array_strict_equal(ac, act) + +## def test_clip_inplace_array(self): +## print "=== INPLACE: testing native double input with array min/max ===" +## a = self._generate_data(self.nr, self.nc) +## ac = a.copy() +## m = zeros(a.shape) +## M = 1.0 +## self.fastclip(a, m, M, a) +## self.clip(a, m, M, ac) +## assert_array_strict_equal(a, ac) + +## def test_clip_inplace_simple(self): +## print "=== INPLACE: testing native double input with scalar min/max ===" +## a = self._generate_data(self.nr, self.nc) +## ac = a.copy() +## m = -0.5 +## M = 0.6 +## self.fastclip(a, m, M, a) +## self.clip(a, m, M, ac) +## assert_array_strict_equal(a, ac) + + import sys if sys.version_info[:2] >= (2, 5): set_local_path() |