diff options
Diffstat (limited to 'numpy')
-rw-r--r-- | numpy/core/fromnumeric.py | 6 | ||||
-rw-r--r-- | numpy/core/src/multiarray/arraytypes.c.src | 9 | ||||
-rw-r--r-- | numpy/core/src/multiarray/ctors.c | 3 | ||||
-rw-r--r-- | numpy/core/src/multiarray/einsum.c.src | 2 | ||||
-rw-r--r-- | numpy/core/src/multiarray/item_selection.c | 2 | ||||
-rw-r--r-- | numpy/core/src/multiarray/methods.c | 3 | ||||
-rw-r--r-- | numpy/core/src/multiarray/new_iterator.c.src | 2 | ||||
-rw-r--r-- | numpy/core/src/umath/loops.c.src | 60 | ||||
-rw-r--r-- | numpy/core/src/umath/ufunc_object.c | 18 | ||||
-rw-r--r-- | numpy/core/tests/test_regression.py | 8 | ||||
-rw-r--r-- | numpy/core/tests/test_umath.py | 84 | ||||
-rw-r--r-- | numpy/distutils/fcompiler/intel.py | 8 | ||||
-rw-r--r-- | numpy/distutils/tests/test_fcompiler_intel.py | 34 | ||||
-rw-r--r-- | numpy/doc/structured_arrays.py | 28 | ||||
-rw-r--r-- | numpy/lib/function_base.py | 18 | ||||
-rw-r--r-- | numpy/lib/tests/test_function_base.py | 81 | ||||
-rw-r--r-- | numpy/testing/utils.py | 182 |
17 files changed, 385 insertions, 163 deletions
diff --git a/numpy/core/fromnumeric.py b/numpy/core/fromnumeric.py index d66e5cb68..602c0ebc5 100644 --- a/numpy/core/fromnumeric.py +++ b/numpy/core/fromnumeric.py @@ -1788,8 +1788,10 @@ def amax(a, axis=None, out=None): Returns ------- - amax : ndarray - A new array or scalar array with the result. + amax : ndarray or scalar + Maximum of `a`. If `axis` is None, the result is a scalar value. + If `axis` is given, the result is an array of dimension + ``a.ndim - 1``. See Also -------- diff --git a/numpy/core/src/multiarray/arraytypes.c.src b/numpy/core/src/multiarray/arraytypes.c.src index a5e47139e..3f2a9ac71 100644 --- a/numpy/core/src/multiarray/arraytypes.c.src +++ b/numpy/core/src/multiarray/arraytypes.c.src @@ -548,7 +548,7 @@ VOID_getitem(char *ip, PyArrayObject *ap) int itemsize; descr = ap->descr; - if (descr->names) { + if (descr->names != NULL) { PyObject *key; PyObject *names; int i, n; @@ -560,9 +560,6 @@ VOID_getitem(char *ip, PyArrayObject *ap) /* get the names from the fields dictionary*/ names = descr->names; - if (!names) { - goto finish; - } n = PyTuple_GET_SIZE(names); ret = PyTuple_New(n); savedflags = ap->flags; @@ -604,7 +601,7 @@ VOID_getitem(char *ip, PyArrayObject *ap) Py_INCREF(descr->subarray->base); ret = PyArray_NewFromDescr(&PyArray_Type, descr->subarray->base, shape.len, shape.ptr, - NULL, ip, ap->flags, NULL); + NULL, ip, ap->flags&(~NPY_F_CONTIGUOUS), NULL); PyDimMem_FREE(shape.ptr); if (!ret) { return NULL; @@ -1596,6 +1593,7 @@ static void } /* convert from Python object to needed one */ #if @convert@ + { PyObject *new, *args; /* call out to the Python builtin given by convstr */ args = Py_BuildValue("(N)", temp); @@ -1611,6 +1609,7 @@ static void if (temp == NULL) { return; } + } #endif /* @convert@ */ if (@to@_setitem(temp,(char *)op, aop)) { Py_DECREF(temp); diff --git a/numpy/core/src/multiarray/ctors.c b/numpy/core/src/multiarray/ctors.c index 94602a5c9..97a6fc915 100644 --- a/numpy/core/src/multiarray/ctors.c +++ b/numpy/core/src/multiarray/ctors.c @@ -2429,7 +2429,6 @@ PyArray_CopyAnyIntoOrdered(PyArrayObject *dst, PyArrayObject *src, PyArray_StridedTransferFn *stransfer = NULL; void *transferdata = NULL; NpyIter *dst_iter, *src_iter; - NPY_BEGIN_THREADS_DEF; NpyIter_IterNext_Fn dst_iternext, src_iternext; char **dst_dataptr, **src_dataptr; @@ -2442,6 +2441,8 @@ PyArray_CopyAnyIntoOrdered(PyArrayObject *dst, PyArrayObject *src, npy_intp dst_size, src_size; int needs_api; + NPY_BEGIN_THREADS_DEF; + if (!PyArray_ISWRITEABLE(dst)) { PyErr_SetString(PyExc_RuntimeError, "cannot write to array"); diff --git a/numpy/core/src/multiarray/einsum.c.src b/numpy/core/src/multiarray/einsum.c.src index 9200c7cdf..5233ea524 100644 --- a/numpy/core/src/multiarray/einsum.c.src +++ b/numpy/core/src/multiarray/einsum.c.src @@ -50,7 +50,7 @@ #if NPY_EINSUM_DBG_TRACING #define NPY_EINSUM_DBG_PRINTF(...) printf(__VA_ARGS__) #else -#define NPY_EINSUM_DBG_PRINTF(...) +#define NPY_EINSUM_DBG_PRINTF #endif /**********************************************/ diff --git a/numpy/core/src/multiarray/item_selection.c b/numpy/core/src/multiarray/item_selection.c index db379e566..022f3d9c0 100644 --- a/numpy/core/src/multiarray/item_selection.c +++ b/numpy/core/src/multiarray/item_selection.c @@ -1880,8 +1880,8 @@ finish: } else { for (i = 0; i < ndim; ++i) { - stride = ndim*NPY_SIZEOF_INTP; PyArrayObject *view; + stride = ndim*NPY_SIZEOF_INTP; view = (PyArrayObject *)PyArray_New(Py_TYPE(self), 1, &nonzero_count, diff --git a/numpy/core/src/multiarray/methods.c b/numpy/core/src/multiarray/methods.c index 4c568a959..ed86e208c 100644 --- a/numpy/core/src/multiarray/methods.c +++ b/numpy/core/src/multiarray/methods.c @@ -303,7 +303,8 @@ PyArray_GetField(PyArrayObject *self, PyArray_Descr *typed, int offset) self->nd, self->dimensions, self->strides, self->data + offset, - self->flags, (PyObject *)self); + self->flags&(~NPY_F_CONTIGUOUS), + (PyObject *)self); if (ret == NULL) { return NULL; } diff --git a/numpy/core/src/multiarray/new_iterator.c.src b/numpy/core/src/multiarray/new_iterator.c.src index 1208e5d3c..3d8566f05 100644 --- a/numpy/core/src/multiarray/new_iterator.c.src +++ b/numpy/core/src/multiarray/new_iterator.c.src @@ -50,7 +50,7 @@ #if NPY_IT_DBG_TRACING #define NPY_IT_DBG_PRINTF(...) printf(__VA_ARGS__) #else -#define NPY_IT_DBG_PRINTF(...) +#define NPY_IT_DBG_PRINTF #endif /**********************************************/ diff --git a/numpy/core/src/umath/loops.c.src b/numpy/core/src/umath/loops.c.src index 79d9da077..5212207da 100644 --- a/numpy/core/src/umath/loops.c.src +++ b/numpy/core/src/umath/loops.c.src @@ -760,10 +760,19 @@ NPY_NO_EXPORT void NPY_NO_EXPORT void @S@@TYPE@_@kind@(char **args, intp *dimensions, intp *steps, void *NPY_UNUSED(func)) { - BINARY_LOOP { - const @s@@type@ in1 = *(@s@@type@ *)ip1; - const @s@@type@ in2 = *(@s@@type@ *)ip2; - *((@s@@type@ *)op1) = (in1 @OP@ in2) ? in1 : in2; + if (IS_BINARY_REDUCE) { + BINARY_REDUCE_LOOP(@s@@type@) { + const @s@@type@ in2 = *(@type@ *)ip2; + io1 = (io1 @OP@ in2) ? io1 : in2; + } + *((@s@@type@ *)iop1) = io1; + } + else { + BINARY_LOOP { + const @s@@type@ in1 = *(@s@@type@ *)ip1; + const @s@@type@ in2 = *(@s@@type@ *)ip2; + *((@s@@type@ *)op1) = (in1 @OP@ in2) ? in1 : in2; + } } } /**end repeat2**/ @@ -998,11 +1007,20 @@ NPY_NO_EXPORT void NPY_NO_EXPORT void @TYPE@_@kind@(char **args, intp *dimensions, intp *steps, void *NPY_UNUSED(func)) { + if (IS_BINARY_REDUCE) { + BINARY_REDUCE_LOOP(@type@) { + const @type@ in2 = *(@type@ *)ip2; + io1 = (io1 @OP@ in2) ? io1 : in2; + } + *((@type@ *)iop1) = io1; + } + else { BINARY_LOOP { const @type@ in1 = *(@type@ *)ip1; const @type@ in2 = *(@type@ *)ip2; *((@type@ *)op1) = (in1 @OP@ in2) ? in1 : in2; } + } } /**end repeat1**/ @@ -1216,10 +1234,19 @@ NPY_NO_EXPORT void @TYPE@_@kind@(char **args, intp *dimensions, intp *steps, void *NPY_UNUSED(func)) { /* */ - BINARY_LOOP { - const @type@ in1 = *(@type@ *)ip1; - const @type@ in2 = *(@type@ *)ip2; - *((@type@ *)op1) = (in1 @OP@ in2 || npy_isnan(in1)) ? in1 : in2; + if (IS_BINARY_REDUCE) { + BINARY_REDUCE_LOOP(@type@) { + const @type@ in2 = *(@type@ *)ip2; + io1 = (io1 @OP@ in2 || npy_isnan(io1)) ? io1 : in2; + } + *((@type@ *)iop1) = io1; + } + else { + BINARY_LOOP { + const @type@ in1 = *(@type@ *)ip1; + const @type@ in2 = *(@type@ *)ip2; + *((@type@ *)op1) = (in1 @OP@ in2 || npy_isnan(in1)) ? in1 : in2; + } } } /**end repeat1**/ @@ -1232,10 +1259,19 @@ NPY_NO_EXPORT void @TYPE@_@kind@(char **args, intp *dimensions, intp *steps, void *NPY_UNUSED(func)) { /* */ - BINARY_LOOP { - const @type@ in1 = *(@type@ *)ip1; - const @type@ in2 = *(@type@ *)ip2; - *((@type@ *)op1) = (in1 @OP@ in2 || npy_isnan(in2)) ? in1 : in2; + if (IS_BINARY_REDUCE) { + BINARY_REDUCE_LOOP(@type@) { + const @type@ in2 = *(@type@ *)ip2; + io1 = (io1 @OP@ in2 || npy_isnan(in2)) ? io1 : in2; + } + *((@type@ *)iop1) = io1; + } + else { + BINARY_LOOP { + const @type@ in1 = *(@type@ *)ip1; + const @type@ in2 = *(@type@ *)ip2; + *((@type@ *)op1) = (in1 @OP@ in2 || npy_isnan(in2)) ? in1 : in2; + } } } /**end repeat1**/ diff --git a/numpy/core/src/umath/ufunc_object.c b/numpy/core/src/umath/ufunc_object.c index e2e999b5b..07a308e66 100644 --- a/numpy/core/src/umath/ufunc_object.c +++ b/numpy/core/src/umath/ufunc_object.c @@ -47,7 +47,7 @@ #if NPY_UF_DBG_TRACING #define NPY_UF_DBG_PRINTF(...) printf(__VA_ARGS__) #else -#define NPY_UF_DBG_PRINTF(...) +#define NPY_UF_DBG_PRINTF #endif /**********************************************/ @@ -1762,7 +1762,6 @@ iterator_loop(PyUFuncObject *self, NpyIter *iter; char *baseptrs[NPY_MAXARGS]; int needs_api; - NPY_BEGIN_THREADS_DEF; NpyIter_IterNext_Fn iternext; char **dataptr; @@ -1771,6 +1770,8 @@ iterator_loop(PyUFuncObject *self, PyArrayObject **op_it; + NPY_BEGIN_THREADS_DEF; + /* Set up the flags */ for (i = 0; i < nin; ++i) { op_flags[i] = NPY_ITER_READONLY| @@ -2792,7 +2793,6 @@ PyUFunc_ReductionOp(PyUFuncObject *self, PyArrayObject *arr, npy_uint32 op_flags[2]; int i, idim, ndim, otype_final; int needs_api, need_outer_iterator; - NPY_BEGIN_THREADS_DEF; NpyIter *iter = NULL, *iter_inner = NULL; @@ -2806,6 +2806,8 @@ PyUFunc_ReductionOp(PyUFuncObject *self, PyArrayObject *arr, int buffersize = 0, errormask = 0; PyObject *errobj = NULL; + NPY_BEGIN_THREADS_DEF; + NPY_UF_DBG_PRINTF("\nEvaluating ufunc %s.%s\n", ufunc_name, opname); #if 0 @@ -3424,7 +3426,6 @@ PyUFunc_Reduceat(PyUFuncObject *self, PyArrayObject *arr, PyArrayObject *ind, npy_uint32 op_flags[3]; int i, idim, ndim, otype_final; int needs_api, need_outer_iterator; - NPY_BEGIN_THREADS_DEF; NpyIter *iter = NULL; @@ -3442,6 +3443,8 @@ PyUFunc_Reduceat(PyUFuncObject *self, PyArrayObject *arr, PyArrayObject *ind, int buffersize = 0, errormask = 0; PyObject *errobj = NULL; + NPY_BEGIN_THREADS_DEF; + reduceat_ind = (npy_intp *)PyArray_DATA(ind); ind_size = PyArray_DIM(ind, 0); red_axis_size = PyArray_DIM(arr, axis); @@ -3599,6 +3602,8 @@ PyUFunc_Reduceat(PyUFuncObject *self, PyArrayObject *arr, PyArrayObject *ind, NpyIter_IterNext_Fn iternext; char **dataptr; + npy_intp count_m1; + npy_intp stride0, stride1; npy_intp *stride; npy_intp *count_ptr; npy_intp stride0_ind = PyArray_STRIDE(op[0], axis); @@ -3616,8 +3621,9 @@ PyUFunc_Reduceat(PyUFuncObject *self, PyArrayObject *arr, PyArrayObject *ind, /* Execute the loop with just the outer iterator */ - npy_intp count_m1 = PyArray_DIM(op[1], axis)-1; - npy_intp stride0 = 0, stride1 = PyArray_STRIDE(op[1], axis); + count_m1 = PyArray_DIM(op[1], axis)-1; + stride0 = 0; + stride1 = PyArray_STRIDE(op[1], axis); NPY_UF_DBG_PRINTF("UFunc: Reduce loop with just outer iterator\n"); diff --git a/numpy/core/tests/test_regression.py b/numpy/core/tests/test_regression.py index 599100719..5458fac33 100644 --- a/numpy/core/tests/test_regression.py +++ b/numpy/core/tests/test_regression.py @@ -1554,5 +1554,13 @@ class TestRegression(TestCase): a2 = np.array([s[:i]]*5) assert_equal(a1, a2) + def test_fields_strides(self): + "Ticket #1760" + r=np.fromstring('abcdefghijklmnop'*4*3, dtype='i4,(2,3)u2') + assert_equal(r[0:3:2]['f1'], r['f1'][0:3:2]) + assert_equal(r[0:3:2]['f1'][0], r[0:3:2][0]['f1']) + assert_equal(r[0:3:2]['f1'][0][()], r[0:3:2][0]['f1'][()]) + assert_equal(r[0:3:2]['f1'][0].strides, r[0:3:2][0]['f1'].strides) + if __name__ == "__main__": run_module_suite() diff --git a/numpy/core/tests/test_umath.py b/numpy/core/tests/test_umath.py index 40b78fdb5..b5f9d5745 100644 --- a/numpy/core/tests/test_umath.py +++ b/numpy/core/tests/test_umath.py @@ -413,6 +413,27 @@ class TestLdexp(TestCase): class TestMaximum(TestCase): + def test_reduce(self): + dflt = np.typecodes['AllFloat'] + dint = np.typecodes['AllInteger'] + seq1 = np.arange(11) + seq2 = seq1[::-1] + func = np.maximum.reduce + for dt in dint: + tmp1 = seq1.astype(dt) + tmp2 = seq2.astype(dt) + assert_equal(func(tmp1), 10) + assert_equal(func(tmp2), 10) + for dt in dflt: + tmp1 = seq1.astype(dt) + tmp2 = seq2.astype(dt) + assert_equal(func(tmp1), 10) + assert_equal(func(tmp2), 10) + tmp1[::2] = np.nan + tmp2[::2] = np.nan + assert_equal(func(tmp1), np.nan) + assert_equal(func(tmp2), np.nan) + def test_reduce_complex(self): assert_equal(np.maximum.reduce([1,2j]),1) assert_equal(np.maximum.reduce([1+3j,2j]),1+3j) @@ -434,6 +455,27 @@ class TestMaximum(TestCase): class TestMinimum(TestCase): + def test_reduce(self): + dflt = np.typecodes['AllFloat'] + dint = np.typecodes['AllInteger'] + seq1 = np.arange(11) + seq2 = seq1[::-1] + func = np.minimum.reduce + for dt in dint: + tmp1 = seq1.astype(dt) + tmp2 = seq2.astype(dt) + assert_equal(func(tmp1), 0) + assert_equal(func(tmp2), 0) + for dt in dflt: + tmp1 = seq1.astype(dt) + tmp2 = seq2.astype(dt) + assert_equal(func(tmp1), 0) + assert_equal(func(tmp2), 0) + tmp1[::2] = np.nan + tmp2[::2] = np.nan + assert_equal(func(tmp1), np.nan) + assert_equal(func(tmp2), np.nan) + def test_reduce_complex(self): assert_equal(np.minimum.reduce([1,2j]),2j) assert_equal(np.minimum.reduce([1+3j,2j]),2j) @@ -455,6 +497,27 @@ class TestMinimum(TestCase): class TestFmax(TestCase): + def test_reduce(self): + dflt = np.typecodes['AllFloat'] + dint = np.typecodes['AllInteger'] + seq1 = np.arange(11) + seq2 = seq1[::-1] + func = np.fmax.reduce + for dt in dint: + tmp1 = seq1.astype(dt) + tmp2 = seq2.astype(dt) + assert_equal(func(tmp1), 10) + assert_equal(func(tmp2), 10) + for dt in dflt: + tmp1 = seq1.astype(dt) + tmp2 = seq2.astype(dt) + assert_equal(func(tmp1), 10) + assert_equal(func(tmp2), 10) + tmp1[::2] = np.nan + tmp2[::2] = np.nan + assert_equal(func(tmp1), 9) + assert_equal(func(tmp2), 9) + def test_reduce_complex(self): assert_equal(np.fmax.reduce([1,2j]),1) assert_equal(np.fmax.reduce([1+3j,2j]),1+3j) @@ -476,6 +539,27 @@ class TestFmax(TestCase): class TestFmin(TestCase): + def test_reduce(self): + dflt = np.typecodes['AllFloat'] + dint = np.typecodes['AllInteger'] + seq1 = np.arange(11) + seq2 = seq1[::-1] + func = np.fmin.reduce + for dt in dint: + tmp1 = seq1.astype(dt) + tmp2 = seq2.astype(dt) + assert_equal(func(tmp1), 0) + assert_equal(func(tmp2), 0) + for dt in dflt: + tmp1 = seq1.astype(dt) + tmp2 = seq2.astype(dt) + assert_equal(func(tmp1), 0) + assert_equal(func(tmp2), 0) + tmp1[::2] = np.nan + tmp2[::2] = np.nan + assert_equal(func(tmp1), 1) + assert_equal(func(tmp2), 1) + def test_reduce_complex(self): assert_equal(np.fmin.reduce([1,2j]),2j) assert_equal(np.fmin.reduce([1+3j,2j]),2j) diff --git a/numpy/distutils/fcompiler/intel.py b/numpy/distutils/fcompiler/intel.py index d7effb01e..b593a91c7 100644 --- a/numpy/distutils/fcompiler/intel.py +++ b/numpy/distutils/fcompiler/intel.py @@ -136,9 +136,9 @@ class IntelItaniumFCompiler(IntelFCompiler): class IntelEM64TFCompiler(IntelFCompiler): compiler_type = 'intelem' compiler_aliases = () - description = 'Intel Fortran Compiler for EM64T-based apps' + description = 'Intel Fortran Compiler for 64-bit apps' - version_match = intel_version_match('EM64T-based|Intel\\(R\\) 64') + version_match = intel_version_match('EM64T-based|Intel\\(R\\) 64|64|IA-64|64-bit') possible_executables = ['ifort', 'efort', 'efc'] @@ -165,9 +165,9 @@ class IntelVisualFCompiler(BaseIntelFCompiler): compiler_type = 'intelv' description = 'Intel Visual Fortran Compiler for 32-bit apps' version_match = intel_version_match('32-bit|IA-32') - + def update_executables(self): - f = dummy_fortran_file() + f = dummy_fortran_file() self.executables['version_cmd'] = ['<F77>', '/FI', '/c', f + '.f', '/o', f + '.o'] diff --git a/numpy/distutils/tests/test_fcompiler_intel.py b/numpy/distutils/tests/test_fcompiler_intel.py new file mode 100644 index 000000000..ad03daeea --- /dev/null +++ b/numpy/distutils/tests/test_fcompiler_intel.py @@ -0,0 +1,34 @@ +from numpy.testing import * + +import numpy.distutils.fcompiler + +intel_32bit_version_strings = [ + ("Intel(R) Fortran Intel(R) 32-bit Compiler Professional for applications"\ + "running on Intel(R) 32, Version 11.1", '11.1'), +] + +intel_64bit_version_strings = [ + ("Intel(R) Fortran IA-64 Compiler Professional for applications"\ + "running on IA-64, Version 11.0", '11.0'), + ("Intel(R) Fortran Intel(R) 64 Compiler Professional for applications"\ + "running on Intel(R) 64, Version 11.1", '11.1') +] + +class TestIntelFCompilerVersions(TestCase): + def test_32bit_version(self): + fc = numpy.distutils.fcompiler.new_fcompiler(compiler='intel') + for vs, version in intel_32bit_version_strings: + v = fc.version_match(vs) + assert_(v == version) + + +class TestIntelEM64TFCompilerVersions(TestCase): + def test_64bit_version(self): + fc = numpy.distutils.fcompiler.new_fcompiler(compiler='intelem') + for vs, version in intel_64bit_version_strings: + v = fc.version_match(vs) + assert_(v == version) + + +if __name__ == '__main__': + run_module_suite() diff --git a/numpy/doc/structured_arrays.py b/numpy/doc/structured_arrays.py index a0d84bd79..af777efa4 100644 --- a/numpy/doc/structured_arrays.py +++ b/numpy/doc/structured_arrays.py @@ -26,7 +26,7 @@ position we get the second record: :: Conveniently, one can access any field of the array by indexing using the string that names that field. In this case the fields have received the -default names 'f0', 'f1' and 'f2'. +default names 'f0', 'f1' and 'f2'. :: >>> y = x['f1'] >>> y @@ -118,7 +118,7 @@ like Fortran equivalencing). 3) List argument: In this case the record structure is defined with a list of tuples. Each tuple has 2 or 3 elements specifying: 1) The name of the field ('' is permitted), 2) the type of the field, and 3) the shape (optional). -For example: +For example:: >>> x = np.zeros(3, dtype=[('x','f4'),('y',np.float32),('value','f4',(2,2))]) >>> x @@ -142,7 +142,7 @@ to be strings), where the value of None is permitted. As an example: :: dtype=[('col1', '>i4'), ('col2', '>f4')]) The other dictionary form permitted is a dictionary of name keys with tuple -values specifying type, offset, and an optional title. +values specifying type, offset, and an optional title. :: >>> x = np.zeros(3, dtype={'col1':('i1',0,'title 1'), 'col2':('f4',1,'title 2')}) >>> x @@ -168,7 +168,7 @@ Accessing field titles ==================================== The field titles provide a standard place to put associated info for fields. -They do not have to be strings. +They do not have to be strings. :: >>> x.dtype.fields['x'][2] 'title 1' @@ -181,7 +181,7 @@ You can access multiple fields at once using a list of field names: :: >>> x = np.array([(1.5,2.5,(1.0,2.0)),(3.,4.,(4.,5.)),(1.,3.,(2.,6.))], dtype=[('x','f4'),('y',np.float32),('value','f4',(2,2))]) -Notice that `x` is created with a list of tuples. +Notice that `x` is created with a list of tuples. :: >>> x[['x','y']] array([(1.5, 2.5), (3.0, 4.0), (1.0, 3.0)], @@ -192,12 +192,28 @@ Notice that `x` is created with a list of tuples. dtype=[('x', '<f4'), ('value', '<f4', (2, 2))]) Notice that the fields are always returned in the same order regardless of -the sequence they are asked for. +the sequence they are asked for. :: >>> x[['y','x']] array([(1.5, 2.5), (3.0, 4.0), (1.0, 3.0)], dtype=[('x', '<f4'), ('y', '<f4')]) +Filling structured arrays +========================= + +Structured arrays can be filled by field or row by row. :: + + >>> arr = np.zeros((5,), dtype=[('var1','f8'),('var2','f8')]) + >>> arr['var1'] = np.arange(5) + +If you fill it in row by row, it takes a take a tuple +(but not a list or array!):: + + >>> arr[0] = (10,20) + >>> arr + array([(10.0, 20.0), (1.0, 0.0), (2.0, 0.0), (3.0, 0.0), (4.0, 0.0)], + dtype=[('var1', '<f8'), ('var2', '<f8')]) + More information ==================================== You can find some more information on recarrays and structured arrays diff --git a/numpy/lib/function_base.py b/numpy/lib/function_base.py index b2d2856d6..dd792c509 100644 --- a/numpy/lib/function_base.py +++ b/numpy/lib/function_base.py @@ -1447,8 +1447,7 @@ def nanmin(a, axis=None): Positive infinity is treated as a very large number and negative infinity is treated as a very small (i.e. negative) number. - If the input has a integer type, an integer type is returned unless - the input contains NaNs and infinity. + If the input has a integer type the function is equivalent to np.min. Examples @@ -1469,7 +1468,11 @@ def nanmin(a, axis=None): -inf """ - return _nanop(np.min, np.inf, a, axis) + a = np.asanyarray(a) + if axis is not None: + return np.fmin.reduce(a, axis) + else: + return np.fmin.reduce(a.flat) def nanargmin(a, axis=None): """ @@ -1541,8 +1544,7 @@ def nanmax(a, axis=None): Positive infinity is treated as a very large number and negative infinity is treated as a very small (i.e. negative) number. - If the input has a integer type, an integer type is returned unless - the input contains NaNs and infinity. + If the input has a integer type the function is equivalent to np.max. Examples -------- @@ -1562,7 +1564,11 @@ def nanmax(a, axis=None): inf """ - return _nanop(np.max, -np.inf, a, axis) + a = np.asanyarray(a) + if axis is not None: + return np.fmax.reduce(a, axis) + else: + return np.fmax.reduce(a.flat) def nanargmax(a, axis=None): """ diff --git a/numpy/lib/tests/test_function_base.py b/numpy/lib/tests/test_function_base.py index 700798e76..13930c79e 100644 --- a/numpy/lib/tests/test_function_base.py +++ b/numpy/lib/tests/test_function_base.py @@ -13,13 +13,13 @@ class TestAny(TestCase): y1 = [0, 0, 1, 0] y2 = [0, 0, 0, 0] y3 = [1, 0, 1, 0] - assert(any(y1)) - assert(any(y3)) - assert(not any(y2)) + assert_(any(y1)) + assert_(any(y3)) + assert_(not any(y2)) def test_nd(self): y1 = [[0, 0, 0], [0, 1, 0], [1, 1, 0]] - assert(any(y1)) + assert_(any(y1)) assert_array_equal(sometrue(y1, axis=0), [1, 1, 0]) assert_array_equal(sometrue(y1, axis=1), [0, 1, 1]) @@ -29,14 +29,14 @@ class TestAll(TestCase): y1 = [0, 1, 1, 0] y2 = [0, 0, 0, 0] y3 = [1, 1, 1, 1] - assert(not all(y1)) - assert(all(y3)) - assert(not all(y2)) - assert(all(~array(y2))) + assert_(not all(y1)) + assert_(all(y3)) + assert_(not all(y2)) + assert_(all(~array(y2))) def test_nd(self): y1 = [[0, 0, 1], [0, 1, 1], [1, 1, 1]] - assert(not all(y1)) + assert_(not all(y1)) assert_array_equal(alltrue(y1, axis=0), [0, 0, 1]) assert_array_equal(alltrue(y1, axis=1), [0, 0, 1]) @@ -44,11 +44,11 @@ class TestAll(TestCase): class TestAverage(TestCase): def test_basic(self): y1 = array([1, 2, 3]) - assert(average(y1, axis=0) == 2.) + assert_(average(y1, axis=0) == 2.) y2 = array([1., 2., 3.]) - assert(average(y2, axis=0) == 2.) + assert_(average(y2, axis=0) == 2.) y3 = [0., 0., 0.] - assert(average(y3, axis=0) == 0.) + assert_(average(y3, axis=0) == 0.) y4 = ones((4, 4)) y4[0, 1] = 0 @@ -407,7 +407,7 @@ class TestDigitize(TestCase): def test_random(self): x = rand(10) bin = linspace(x.min(), x.max(), 10) - assert all(digitize(x, bin) != 0) + assert_(all(digitize(x, bin) != 0)) class TestUnwrap(TestCase): @@ -415,7 +415,7 @@ class TestUnwrap(TestCase): #check that unwrap removes jumps greather that 2*pi assert_array_equal(unwrap([1, 1 + 2 * pi]), [1, 1]) #check that unwrap maintans continuity - assert(all(diff(unwrap(rand(10) * 100)) < pi)) + assert_(all(diff(unwrap(rand(10) * 100)) < pi)) class TestFilterwindows(TestCase): @@ -520,7 +520,7 @@ class TestTrapz(TestCase): class TestSinc(TestCase): def test_simple(self): - assert(sinc(0) == 1) + assert_(sinc(0) == 1) w = sinc(linspace(-1, 1, 100)) #check symmetry assert_array_almost_equal(w, flipud(w), 7) @@ -565,7 +565,7 @@ class TestHistogram(TestCase): area = sum(a * diff(b)) assert_almost_equal(area, 1) - warnings.filterwarnings('ignore', + warnings.filterwarnings('ignore', message="\s*This release of NumPy fixes a normalization bug") # Check with non-constant bin widths v = np.arange(10) @@ -573,20 +573,20 @@ class TestHistogram(TestCase): a, b = histogram(v, bins, normed=True) assert_array_equal(a, .1) assert_equal(sum(a*diff(b)), 1) - + # Variale bin widths are especially useful to deal with # infinities. v = np.arange(10) bins = [0,1,3,6,np.inf] a, b = histogram(v, bins, normed=True) assert_array_equal(a, [.1,.1,.1,0.]) - + # Taken from a bug report from N. Becker on the numpy-discussion - # mailing list Aug. 6, 2010. + # mailing list Aug. 6, 2010. counts, dmy = np.histogram([1,2,3,4], [0.5,1.5,np.inf], normed=True) assert_equal(counts, [.25, 0]) warnings.filters.pop(0) - + def test_outliers(self): # Check that outliers are not tallied a = arange(10) + .5 @@ -615,16 +615,16 @@ class TestHistogram(TestCase): # Check the type of the returned histogram a = arange(10) + .5 h, b = histogram(a) - assert(issubdtype(h.dtype, int)) + assert_(issubdtype(h.dtype, int)) h, b = histogram(a, normed=True) - assert(issubdtype(h.dtype, float)) + assert_(issubdtype(h.dtype, float)) h, b = histogram(a, weights=ones(10, int)) - assert(issubdtype(h.dtype, int)) + assert_(issubdtype(h.dtype, int)) h, b = histogram(a, weights=ones(10, float)) - assert(issubdtype(h.dtype, float)) + assert_(issubdtype(h.dtype, float)) def test_weights(self): v = rand(100) @@ -655,7 +655,7 @@ class TestHistogram(TestCase): weights=[2,1,1,1,1,1,1,1,1], normed=True) assert_almost_equal(a, [.2, .1, .1, .075]) warnings.filters.pop(0) - + class TestHistogramdd(TestCase): def test_simple(self): @@ -668,7 +668,7 @@ class TestHistogramdd(TestCase): # Check normalization ed = [[-2, 0, 2], [0, 1, 2, 3], [0, 1, 2, 3]] H, edges = histogramdd(x, bins=ed, normed=True) - assert(all(H == answer / 12.)) + assert_(all(H == answer / 12.)) # Check that H has the correct shape. H, edges = histogramdd(x, (2, 3, 4), range=[[-1, 1], [0, 3], [0, 4]], normed=True) @@ -697,7 +697,7 @@ class TestHistogramdd(TestCase): r = rand(10, 3) for b in bins: H, edges = histogramdd(r, b) - assert(H.shape == b) + assert_(H.shape == b) def test_shape_4d(self): # All possible permutations for bins of different lengths in 4D. @@ -711,7 +711,7 @@ class TestHistogramdd(TestCase): r = rand(10, 4) for b in bins: H, edges = histogramdd(r, b) - assert(H.shape == b) + assert_(H.shape == b) def test_weights(self): v = rand(100, 2) @@ -733,12 +733,12 @@ class TestHistogramdd(TestCase): class TestUnique(TestCase): def test_simple(self): x = array([4, 3, 2, 1, 1, 2, 3, 4, 0]) - assert(all(unique(x) == [0, 1, 2, 3, 4])) - assert(unique(array([1, 1, 1, 1, 1])) == array([1])) + assert_(all(unique(x) == [0, 1, 2, 3, 4])) + assert_(unique(array([1, 1, 1, 1, 1])) == array([1])) x = ['widget', 'ham', 'foo', 'bar', 'foo', 'ham'] - assert(all(unique(x) == ['bar', 'foo', 'ham', 'widget'])) + assert_(all(unique(x) == ['bar', 'foo', 'ham', 'widget'])) x = array([5 + 6j, 1 + 1j, 1 + 10j, 10, 5 + 6j]) - assert(all(unique(x) == [1 + 1j, 1 + 10j, 5 + 6j, 10])) + assert_(all(unique(x) == [1 + 1j, 1 + 10j, 5 + 6j, 10])) class TestCheckFinite(TestCase): @@ -792,7 +792,7 @@ class TestNaNFuncts(TestCase): array([[ 0.01319214, 0.11704017, 0.1630199 ], [ 0.37910852, 0.87964135, 0.34306596], [ 0.72687499, 0.23913896, 0.33850425]])) - assert nanmin([nan, nan]) is nan + assert_(np.isnan(nanmin([nan, nan]))) def test_nanargmin(self): assert_almost_equal(nanargmin(self.A), 1) @@ -823,6 +823,7 @@ class TestNaNFuncts(TestCase): array([[ 0.01620964, 0.75157887, 0.28333658], [ 0.59541557, 0.87964135, 0.70543747], [ 0.91084584, 0.84386844, 0.37068164]])) + assert_(np.isnan(nanmax([nan, nan]))) def test_nanmin_allnan_on_axis(self): assert_array_equal(isnan(nanmin([[nan] * 2] * 3, axis=1)), @@ -923,7 +924,7 @@ class Test_i0(TestCase): class TestKaiser(TestCase): def test_simple(self): assert_almost_equal(kaiser(0, 1.0), array([])) - assert isfinite(kaiser(1, 1.0)) + assert_(isfinite(kaiser(1, 1.0))) assert_almost_equal(kaiser(2, 1.0), array([ 0.78984831, 0.78984831])) assert_almost_equal(kaiser(5, 1.0), array([ 0.78984831, 0.94503323, 1. , @@ -950,14 +951,14 @@ class TestMsort(TestCase): class TestMeshgrid(TestCase): def test_simple(self): [X, Y] = meshgrid([1, 2, 3], [4, 5, 6, 7]) - assert all(X == array([[1, 2, 3], + assert_(all(X == array([[1, 2, 3], [1, 2, 3], [1, 2, 3], - [1, 2, 3]])) - assert all(Y == array([[4, 4, 4], + [1, 2, 3]]))) + assert_(all(Y == array([[4, 4, 4], [5, 5, 5], [6, 6, 6], - [7, 7, 7]])) + [7, 7, 7]]))) class TestPiecewise(TestCase): @@ -1001,8 +1002,8 @@ class TestPiecewise(TestCase): def test_0d(self): x = array(3) y = piecewise(x, x > 3, [4, 0]) - assert y.ndim == 0 - assert y == 0 + assert_(y.ndim == 0) + assert_(y == 0) class TestBincount(TestCase): diff --git a/numpy/testing/utils.py b/numpy/testing/utils.py index e4ef9d3cb..0c0483f39 100644 --- a/numpy/testing/utils.py +++ b/numpy/testing/utils.py @@ -205,25 +205,25 @@ def assert_equal(actual,desired,err_msg='',verbose=True): """ Raise an assertion if two objects are not equal. - Given two objects (lists, tuples, dictionaries or numpy arrays), check - that all elements of these objects are equal. An exception is raised at - the first conflicting values. + Given two objects (scalars, lists, tuples, dictionaries or numpy arrays), + check that all elements of these objects are equal. An exception is raised + at the first conflicting values. Parameters ---------- - actual : list, tuple, dict or ndarray - The object to check. - desired : list, tuple, dict or ndarray - The expected object. - err_msg : string - The error message to be printed in case of failure. - verbose : bool - If True, the conflicting values are appended to the error message. + actual : array_like + The object to check. + desired : array_like + The expected object. + err_msg : str, optional + The error message to be printed in case of failure. + verbose : bool, optional + If True, the conflicting values are appended to the error message. Raises ------ AssertionError - If actual and desired are not equal. + If actual and desired are not equal. Examples -------- @@ -357,7 +357,12 @@ def assert_almost_equal(actual,desired,decimal=7,err_msg='',verbose=True): """ Raise an assertion if two items are not equal up to desired precision. - The test is equivalent to abs(desired-actual) < 0.5 * 10**(-decimal) + .. note:: It is recommended to use one of `assert_allclose`, + `assert_array_almost_equal_nulp` or `assert_array_max_ulp` + instead of this function for more consistent floating point + comparisons. + + The test is equivalent to ``abs(desired-actual) < 0.5 * 10**(-decimal)``. Given two objects (numbers or ndarrays), check that all elements of these objects are almost equal. An exception is raised at conflicting values. @@ -365,16 +370,16 @@ def assert_almost_equal(actual,desired,decimal=7,err_msg='',verbose=True): Parameters ---------- - actual : number or ndarray - The object to check. - desired : number or ndarray - The expected object. - decimal : integer (decimal=7) - desired precision - err_msg : string - The error message to be printed in case of failure. - verbose : bool - If True, the conflicting values are appended to the error message. + actual : array_like + The object to check. + desired : array_like + The expected object. + decimal : int, optional + Desired precision, default is 7. + err_msg : str, optional + The error message to be printed in case of failure. + verbose : bool, optional + If True, the conflicting values are appended to the error message. Raises ------ @@ -383,9 +388,9 @@ def assert_almost_equal(actual,desired,decimal=7,err_msg='',verbose=True): See Also -------- - assert_array_almost_equal: compares array_like objects - assert_equal: tests objects for equality - + assert_allclose: Compare two array_like objects for equality with desired + relative and/or absolute precision. + assert_array_almost_equal_nulp, assert_array_max_ulp, assert_equal Examples -------- @@ -399,7 +404,7 @@ def assert_almost_equal(actual,desired,decimal=7,err_msg='',verbose=True): DESIRED: 2.3333333399999998 >>> npt.assert_almost_equal(np.array([1.0,2.3333333333333]), - \t\t\tnp.array([1.0,2.33333334]), decimal=9) + ... np.array([1.0,2.33333334]), decimal=9) ... <type 'exceptions.AssertionError'>: Arrays are not almost equal @@ -467,22 +472,27 @@ def assert_approx_equal(actual,desired,significant=7,err_msg='',verbose=True): """ Raise an assertion if two items are not equal up to significant digits. + .. note:: It is recommended to use one of `assert_allclose`, + `assert_array_almost_equal_nulp` or `assert_array_max_ulp` + instead of this function for more consistent floating point + comparisons. + Given two numbers, check that they are approximately equal. Approximately equal is defined as the number of significant digits that agree. Parameters ---------- - actual : number - The object to check. - desired : number - The expected object. - significant : integer (significant=7) - desired precision - err_msg : string - The error message to be printed in case of failure. - verbose : bool - If True, the conflicting values are appended to the error message. + actual : scalar + The object to check. + desired : scalar + The expected object. + significant : int, optional + Desired precision, default is 7. + err_msg : str, optional + The error message to be printed in case of failure. + verbose : bool, optional + If True, the conflicting values are appended to the error message. Raises ------ @@ -491,10 +501,9 @@ def assert_approx_equal(actual,desired,significant=7,err_msg='',verbose=True): See Also -------- - assert_almost_equal: compares objects by decimals - assert_array_almost_equal: compares array_like objects by decimals - assert_equal: tests objects for equality - + assert_allclose: Compare two array_like objects for equality with desired + relative and/or absolute precision. + assert_array_almost_equal_nulp, assert_array_max_ulp, assert_equal Examples -------- @@ -638,36 +647,36 @@ def assert_array_equal(x, y, err_msg='', verbose=True): Parameters ---------- x : array_like - The actual object to check. + The actual object to check. y : array_like - The desired, expected object. - err_msg : string - The error message to be printed in case of failure. - verbose : bool + The desired, expected object. + err_msg : str, optional + The error message to be printed in case of failure. + verbose : bool, optional If True, the conflicting values are appended to the error message. Raises ------ AssertionError - If actual and desired objects are not equal. + If actual and desired objects are not equal. See Also -------- - assert_array_almost_equal: test objects for equality up to precision - assert_equal: tests objects for equality - + assert_allclose: Compare two array_like objects for equality with desired + relative and/or absolute precision. + assert_array_almost_equal_nulp, assert_array_max_ulp, assert_equal Examples -------- - the first assert does not raise an exception + The first assert does not raise an exception: >>> np.testing.assert_array_equal([1.0,2.33333,np.nan], - \t\t\t[np.exp(0),2.33333, np.nan]) + ... [np.exp(0),2.33333, np.nan]) - assert fails with numerical inprecision with floats + Assert fails with numerical inprecision with floats: >>> np.testing.assert_array_equal([1.0,np.pi,np.nan], - \t\t\t[1, np.sqrt(np.pi)**2, np.nan]) + ... [1, np.sqrt(np.pi)**2, np.nan]) ... <type 'exceptions.ValueError'>: AssertionError: @@ -677,10 +686,12 @@ def assert_array_equal(x, y, err_msg='', verbose=True): x: array([ 1. , 3.14159265, NaN]) y: array([ 1. , 3.14159265, NaN]) - use assert_array_almost_equal for these cases instead + Use `assert_allclose` or one of the nulp (number of floating point values) + functions for these cases instead: - >>> np.testing.assert_array_almost_equal([1.0,np.pi,np.nan], - \t\t\t[1, np.sqrt(np.pi)**2, np.nan], decimal=15) + >>> np.testing.assert_allclose([1.0,np.pi,np.nan], + ... [1, np.sqrt(np.pi)**2, np.nan], + ... rtol=1e-10, atol=0) """ assert_array_compare(operator.__eq__, x, y, err_msg=err_msg, @@ -690,8 +701,13 @@ def assert_array_almost_equal(x, y, decimal=6, err_msg='', verbose=True): """ Raise an assertion if two objects are not equal up to desired precision. + .. note:: It is recommended to use one of `assert_allclose`, + `assert_array_almost_equal_nulp` or `assert_array_max_ulp` + instead of this function for more consistent floating point + comparisons. + The test verifies identical shapes and verifies values with - abs(desired-actual) < 0.5 * 10**(-decimal) + ``abs(desired-actual) < 0.5 * 10**(-decimal)``. Given two array_like objects, check that the shape is equal and all elements of these objects are almost equal. An exception is raised at @@ -702,26 +718,26 @@ def assert_array_almost_equal(x, y, decimal=6, err_msg='', verbose=True): Parameters ---------- x : array_like - The actual object to check. + The actual object to check. y : array_like - The desired, expected object. - decimal : integer (decimal=6) - desired precision - err_msg : string + The desired, expected object. + decimal : int, optional + Desired precision, default is 6. + err_msg : str, optional The error message to be printed in case of failure. - verbose : bool + verbose : bool, optional If True, the conflicting values are appended to the error message. Raises ------ AssertionError - If actual and desired are not equal up to specified precision. + If actual and desired are not equal up to specified precision. See Also -------- - assert_almost_equal: simple version for comparing numbers - assert_array_equal: tests objects for equality - + assert_allclose: Compare two array_like objects for equality with desired + relative and/or absolute precision. + assert_array_almost_equal_nulp, assert_array_max_ulp, assert_equal Examples -------- @@ -731,7 +747,7 @@ def assert_array_almost_equal(x, y, decimal=6, err_msg='', verbose=True): [1.0,2.333,np.nan]) >>> np.testing.assert_array_almost_equal([1.0,2.33333,np.nan], - \t\t\t[1.0,2.33339,np.nan], decimal=5) + ... [1.0,2.33339,np.nan], decimal=5) ... <type 'exceptions.AssertionError'>: AssertionError: @@ -742,7 +758,7 @@ def assert_array_almost_equal(x, y, decimal=6, err_msg='', verbose=True): y: array([ 1. , 2.33339, NaN]) >>> np.testing.assert_array_almost_equal([1.0,2.33333,np.nan], - \t\t\t[1.0,2.33333, 5], decimal=5) + ... [1.0,2.33333, 5], decimal=5) <type 'exceptions.ValueError'>: ValueError: Arrays are not almost equal @@ -1099,21 +1115,23 @@ def assert_allclose(actual, desired, rtol=1e-7, atol=0, """ Raise an assertion if two objects are not equal up to desired tolerance. - The test is equivalent to ``allclose(actual, desired, rtol, atol)`` + The test is equivalent to ``allclose(actual, desired, rtol, atol)``. + It compares the difference between `actual` and `desired` to + ``atol + rtol * abs(desired)``. Parameters ---------- actual : array_like Array obtained. desired : array_like - Array desired + Array desired. rtol : float, optional - Relative tolerance + Relative tolerance. atol : float, optional - Absolute tolerance - err_msg : string + Absolute tolerance. + err_msg : str, optional The error message to be printed in case of failure. - verbose : bool + verbose : bool, optional If True, the conflicting values are appended to the error message. Raises @@ -1121,6 +1139,16 @@ def assert_allclose(actual, desired, rtol=1e-7, atol=0, AssertionError If actual and desired are not equal up to specified precision. + See Also + -------- + assert_array_almost_equal_nulp, assert_array_max_ulp + + Examples + -------- + >>> x = [1e-5, 1e-3, 1e-1] + >>> y = np.arccos(np.cos(x)) + >>> assert_allclose(x, y, rtol=1e-5, atol=0) + """ import numpy as np def compare(x, y): @@ -1236,7 +1264,7 @@ def assert_array_max_ulp(a, b, maxulp=1, dtype=None): return ret def nulp_diff(x, y, dtype=None): - """For each item in x and y, eeturn the number of representable floating + """For each item in x and y, return the number of representable floating points between them. Parameters |