[Numpy-svn] r3620 - in trunk/numpy/core: . src tests

numpy-svn@scip... numpy-svn@scip...
Fri Mar 30 13:19:14 CDT 2007


Author: oliphant
Date: 2007-03-30 13:19:07 -0500 (Fri, 30 Mar 2007)
New Revision: 3620

Modified:
   trunk/numpy/core/ma.py
   trunk/numpy/core/src/arrayobject.c
   trunk/numpy/core/tests/test_ma.py
Log:
Allow Boolean mask indexing for 0-d arrays.

Modified: trunk/numpy/core/ma.py
===================================================================
--- trunk/numpy/core/ma.py	2007-03-30 15:39:22 UTC (rev 3619)
+++ trunk/numpy/core/ma.py	2007-03-30 18:19:07 UTC (rev 3620)
@@ -1264,14 +1264,6 @@
                     value = numeric.array(value, dtype=object)
                     d = d.astype(object)
                     result = fromnumeric.choose(m, (d, value))
-                except IndexError:
-                    #ok, if scalar
-                    if d.shape:
-                        raise
-                    elif m:
-                        result = numeric.array(value, dtype=d.dtype)
-                    else:
-                        result = d
             return result
 
     def ids (self):

Modified: trunk/numpy/core/src/arrayobject.c
===================================================================
--- trunk/numpy/core/src/arrayobject.c	2007-03-30 15:39:22 UTC (rev 3619)
+++ trunk/numpy/core/src/arrayobject.c	2007-03-30 18:19:07 UTC (rev 3620)
@@ -2720,6 +2720,25 @@
                                 return NULL;
                         return add_new_axes_0d(self, nd);
                 }
+                /* Allow Boolean mask selection also */
+                if (PyBool_Check(op) || PyArray_IsScalar(op, Bool) || 
+                    (PyArray_Check(op) && (PyArray_DIMS(op)==0) &&
+                     PyArray_ISBOOL(op))) {
+                        if (PyObject_IsTrue(op)) {
+                                Py_INCREF(self);
+                                return (PyObject *)self;
+                        }
+                        else {
+                                oned = 0;
+                                Py_INCREF(self->descr);
+                                return PyArray_NewFromDescr(self->ob_type,
+                                                            self->descr,
+                                                            1, &oned,
+                                                            NULL, NULL,
+                                                            NPY_DEFAULT,
+                                                            NULL);
+                        }
+                }
                 PyErr_SetString(PyExc_IndexError,
                                 "0-d arrays can't be indexed.");
                 return NULL;
@@ -2886,10 +2905,27 @@
         }
 
         if (self->nd == 0) {
+                /* Several different exceptions to the 0-d no-indexing rule
+
+                   1) ellipses
+                   2) empty tuple
+                   3) Using newaxis (None)
+                   4) Boolean mask indexing
+                */
                 if (index == Py_Ellipsis || index == Py_None ||         \
                     (PyTuple_Check(index) && (0 == PyTuple_GET_SIZE(index) || \
                                               count_new_axes_0d(index) > 0)))
                         return self->descr->f->setitem(op, self->data, self);
+                if (PyBool_Check(index) || PyArray_IsScalar(index, Bool) ||
+                    (PyArray_Check(index) && (PyArray_DIMS(index)==0) &&
+                     PyArray_ISBOOL(index))) {
+                        if (PyObject_IsTrue(index)) {
+                                return self->descr->f->setitem(op, self->data, self);
+                        }
+                        else { /* don't do anything */
+                                return 0;
+                        }
+                }
                 PyErr_SetString(PyExc_IndexError,
                                 "0-d arrays can't be indexed.");
                 return -1;
@@ -3008,6 +3044,10 @@
                 Bool noellipses = TRUE;
                 if (op == Py_Ellipsis)
                         noellipses = FALSE;
+                else if (PyBool_Check(op) || PyArray_IsScalar(op, Bool) || 
+                         (PyArray_Check(op) && (PyArray_DIMS(op)==0) &&
+                          PyArray_ISBOOL(op))) 
+                        noellipses = FALSE;
                 else if (PySequence_Check(op)) {
                         int n, i;
                         PyObject *temp;

Modified: trunk/numpy/core/tests/test_ma.py
===================================================================
--- trunk/numpy/core/tests/test_ma.py	2007-03-30 15:39:22 UTC (rev 3619)
+++ trunk/numpy/core/tests/test_ma.py	2007-03-30 18:19:07 UTC (rev 3620)
@@ -611,7 +611,7 @@
         self.failUnless(minimum(xm, xm).mask)
         self.failUnless(xm.filled().dtype is xm.data.dtype)
         x = array(0, mask=0)
-        self.failUnless(x.filled() is x.data)
+        self.failUnless(x.filled() == x.data)
         self.failUnlessEqual(str(xm), str(masked_print_option))
 
     def check_testArrayMethods(self):



More information about the Numpy-svn mailing list