[Numpy-svn] r3623 - in trunk/numpy/core: include/numpy src tests

numpy-svn@scip... numpy-svn@scip...
Fri Mar 30 18:47:46 CDT 2007


Author: oliphant
Date: 2007-03-30 18:47:39 -0500 (Fri, 30 Mar 2007)
New Revision: 3623

Modified:
   trunk/numpy/core/include/numpy/ndarrayobject.h
   trunk/numpy/core/src/arrayobject.c
   trunk/numpy/core/src/arraytypes.inc.src
   trunk/numpy/core/src/multiarraymodule.c
   trunk/numpy/core/tests/test_multiarray.py
   trunk/numpy/core/tests/test_numeric.py
Log:
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

Modified: trunk/numpy/core/include/numpy/ndarrayobject.h
===================================================================
--- trunk/numpy/core/include/numpy/ndarrayobject.h	2007-03-30 19:25:13 UTC (rev 3622)
+++ trunk/numpy/core/include/numpy/ndarrayobject.h	2007-03-30 23:47:39 UTC (rev 3623)
@@ -1760,7 +1760,10 @@
 #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
    http://numeric.scipy.org/array_interface.html for the full

Modified: trunk/numpy/core/src/arrayobject.c
===================================================================
--- trunk/numpy/core/src/arrayobject.c	2007-03-30 19:25:13 UTC (rev 3622)
+++ trunk/numpy/core/src/arrayobject.c	2007-03-30 23:47:39 UTC (rev 3623)
@@ -10389,6 +10389,7 @@
                 PyErr_Format(PyExc_ValueError,
                              "Need between 2 and (%d) "                 \
                              "array objects (inclusive).", NPY_MAXARGS);
+                return NULL;
         }
 
         /* fprintf(stderr, "multi new...");*/
@@ -11972,6 +11973,17 @@
 }
 
 
+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 @@
         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 */

Modified: trunk/numpy/core/src/arraytypes.inc.src
===================================================================
--- trunk/numpy/core/src/arraytypes.inc.src	2007-03-30 19:25:13 UTC (rev 3622)
+++ trunk/numpy/core/src/arraytypes.inc.src	2007-03-30 23:47:39 UTC (rev 3623)
@@ -2069,7 +2069,7 @@
         @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 @@
         register npy_intp i; 
         @type@ max_val, min_val;
         
+        min_val = *min;
         max_val = *max;
-        min_val = *min;
- 
+
         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;

Modified: trunk/numpy/core/src/multiarraymodule.c
===================================================================
--- trunk/numpy/core/src/multiarraymodule.c	2007-03-30 19:25:13 UTC (rev 3622)
+++ trunk/numpy/core/src/multiarraymodule.c	2007-03-30 23:47:39 UTC (rev 3623)
@@ -1145,7 +1145,7 @@
         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 @@
 
         /* 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 @@
         }
 
         /* 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 @@
         /* 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 @@
         }
 
         /* 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 @@
                 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 @@
         /* 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 @@
                 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 @@
         Py_XDECREF(maxa);
         Py_XDECREF(mina);
         Py_XDECREF(newin);
+        PyArray_XDECREF_ERR(newout);
         return NULL;
 }
 
@@ -2130,19 +2153,21 @@
                    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 @@
 		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 @@
                         */
                         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;

Modified: trunk/numpy/core/tests/test_multiarray.py
===================================================================
--- trunk/numpy/core/tests/test_multiarray.py	2007-03-30 19:25:13 UTC (rev 3622)
+++ trunk/numpy/core/tests/test_multiarray.py	2007-03-30 23:47:39 UTC (rev 3623)
@@ -371,7 +371,6 @@
 
             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 @@
                 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()

Modified: trunk/numpy/core/tests/test_numeric.py
===================================================================
--- trunk/numpy/core/tests/test_numeric.py	2007-03-30 19:25:13 UTC (rev 3622)
+++ trunk/numpy/core/tests/test_numeric.py	2007-03-30 23:47:39 UTC (rev 3623)
@@ -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 @@
     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()



More information about the Numpy-svn mailing list