diff options
author | Aron Ahmadia <aron@ahmadia.net> | 2012-07-17 16:59:50 -0500 |
---|---|---|
committer | Aron Ahmadia <aron@ahmadia.net> | 2012-07-17 16:59:50 -0500 |
commit | a419a3036aa8202d00eb6e857c79d66adc56bed0 (patch) | |
tree | 4a73e6fff2ee13b35c154c43bd7b58bb6c2af633 /numpy/lib | |
parent | 7316499dd60baa7bb260875b79f7d22be491c986 (diff) | |
parent | 6c772fab57934d24b66638ea5001eb02d1662f5e (diff) | |
download | numpy-a419a3036aa8202d00eb6e857c79d66adc56bed0.tar.gz |
Merge branch 'master' of https://github.com/numpy/numpy into patch-2
Diffstat (limited to 'numpy/lib')
-rw-r--r-- | numpy/lib/_iotools.py | 11 | ||||
-rw-r--r-- | numpy/lib/arraysetops.py | 16 | ||||
-rw-r--r-- | numpy/lib/function_base.py | 18 | ||||
-rw-r--r-- | numpy/lib/npyio.py | 9 | ||||
-rw-r--r-- | numpy/lib/src/_compiled_base.c | 11 | ||||
-rw-r--r-- | numpy/lib/tests/test_arraysetops.py | 73 | ||||
-rw-r--r-- | numpy/lib/tests/test_io.py | 57 | ||||
-rw-r--r-- | numpy/lib/type_check.py | 12 |
8 files changed, 153 insertions, 54 deletions
diff --git a/numpy/lib/_iotools.py b/numpy/lib/_iotools.py index 7921b4116..2f2a4bc57 100644 --- a/numpy/lib/_iotools.py +++ b/numpy/lib/_iotools.py @@ -203,13 +203,17 @@ class LineSplitter(object): self._handyman = _handyman # def _delimited_splitter(self, line): - line = line.split(self.comments)[0].strip(asbytes(" \r\n")) + if self.comments is not None: + line = line.split(self.comments)[0] + line = line.strip(asbytes(" \r\n")) if not line: return [] return line.split(self.delimiter) # def _fixedwidth_splitter(self, line): - line = line.split(self.comments)[0].strip(asbytes("\r\n")) + if self.comments is not None: + line = line.split(self.comments)[0] + line = line.strip(asbytes("\r\n")) if not line: return [] fixed = self.delimiter @@ -217,7 +221,8 @@ class LineSplitter(object): return [line[s] for s in slices] # def _variablewidth_splitter(self, line): - line = line.split(self.comments)[0] + if self.comments is not None: + line = line.split(self.comments)[0] if not line: return [] slices = self.delimiter diff --git a/numpy/lib/arraysetops.py b/numpy/lib/arraysetops.py index 91dd96f9c..20a0e7151 100644 --- a/numpy/lib/arraysetops.py +++ b/numpy/lib/arraysetops.py @@ -44,7 +44,7 @@ def ediff1d(ary, to_end=None, to_begin=None): Returns ------- - ed : ndarray + ediff1d : ndarray The differences. Loosely, this is ``ary.flat[1:] - ary.flat[:-1]``. See Also @@ -212,7 +212,7 @@ def intersect1d(ar1, ar2, assume_unique=False): Returns ------- - out : ndarray + intersect1d : ndarray Sorted 1D array of common and unique elements. See Also @@ -251,7 +251,7 @@ def setxor1d(ar1, ar2, assume_unique=False): Returns ------- - xor : ndarray + setxor1d : ndarray Sorted 1D array of unique values that are in only one of the input arrays. @@ -287,7 +287,7 @@ def in1d(ar1, ar2, assume_unique=False): Parameters ---------- - ar1 : array_like, shape (M,) + ar1 : (M,) array_like Input array. ar2 : array_like The values against which to test each value of `ar1`. @@ -297,8 +297,8 @@ def in1d(ar1, ar2, assume_unique=False): Returns ------- - mask : ndarray of bools, shape(M,) - The values `ar1[mask]` are in `ar2`. + in1d : (M,) ndarray, bool + The values `ar1[in1d]` are in `ar2`. See Also -------- @@ -365,7 +365,7 @@ def union1d(ar1, ar2): Returns ------- - union : ndarray + union1d : ndarray Unique, sorted union of the input arrays. See Also @@ -399,7 +399,7 @@ def setdiff1d(ar1, ar2, assume_unique=False): Returns ------- - difference : ndarray + setdiff1d : ndarray Sorted 1D array of values in `ar1` that are not in `ar2`. See Also diff --git a/numpy/lib/function_base.py b/numpy/lib/function_base.py index f3df3b96b..a0781ebf9 100644 --- a/numpy/lib/function_base.py +++ b/numpy/lib/function_base.py @@ -843,7 +843,7 @@ def gradient(f, *varargs): Returns ------- - g : ndarray + gradient : ndarray N arrays of the same shape as `f` giving the derivative of `f` with respect to each dimension. @@ -948,7 +948,7 @@ def diff(a, n=1, axis=-1): Returns ------- - out : ndarray + diff : ndarray The `n` order differences. The shape of the output is the same as `a` except along `axis` where the dimension is smaller by `n`. @@ -1284,6 +1284,11 @@ def extract(condition, arr): arr : array_like Input array of the same size as `condition`. + Returns + ------- + extract : ndarray + Rank 1 array of values from `arr` where `condition` is True. + See Also -------- take, put, copyto, compress @@ -1316,9 +1321,10 @@ def place(arr, mask, vals): """ Change elements of an array based on conditional and input values. - Similar to ``np.copyto(arr, vals, where=mask)``, the difference is that `place` - uses the first N elements of `vals`, where N is the number of True values - in `mask`, while `copyto` uses the elements where `mask` is True. + Similar to ``np.copyto(arr, vals, where=mask)``, the difference is that + `place` uses the first N elements of `vals`, where N is the number of + True values in `mask`, while `copyto` uses the elements where `mask` + is True. Note that `extract` does the exact opposite of `place`. @@ -2713,7 +2719,7 @@ def kaiser(M,beta): A beta value of 14 is probably a good starting point. Note that as beta gets large, the window narrows, and so the number of samples needs to be - large enough to sample the increasingly narrow spike, otherwise nans will + large enough to sample the increasingly narrow spike, otherwise NaNs will get returned. Most references to the Kaiser window come from the signal processing diff --git a/numpy/lib/npyio.py b/numpy/lib/npyio.py index 221529929..b63003f80 100644 --- a/numpy/lib/npyio.py +++ b/numpy/lib/npyio.py @@ -359,7 +359,6 @@ def load(file, mmap_mode=None): own_fid = True elif isinstance(file, gzip.GzipFile): fid = seek_gzip_factory(file) - own_fid = True else: fid = file @@ -371,7 +370,7 @@ def load(file, mmap_mode=None): fid.seek(-N, 1) # back-up if magic.startswith(_ZIP_PREFIX): # zip-file (assume .npz) own_fid = False - return NpzFile(fid, own_fid=True) + return NpzFile(fid, own_fid=own_fid) elif magic == format.MAGIC_PREFIX: # .npy file if mmap_mode: return format.open_memmap(file, mode=mmap_mode) @@ -471,8 +470,7 @@ def savez(file, *args, **kwds): -------- save : Save a single array to a binary file in NumPy format. savetxt : Save an array to a file as plain text. - numpy.savez_compressed : Save several arrays into a compressed .npz file - format + savez_compressed : Save several arrays into a compressed .npz file format Notes ----- @@ -1293,7 +1291,8 @@ def genfromtxt(fname, dtype=float, comments='#', delimiter=None, """ # Py3 data conversions to bytes, for convenience - comments = asbytes(comments) + if comments is not None: + comments = asbytes(comments) if isinstance(delimiter, unicode): delimiter = asbytes(delimiter) if isinstance(missing, unicode): diff --git a/numpy/lib/src/_compiled_base.c b/numpy/lib/src/_compiled_base.c index c31ee5cd8..d389b7f8e 100644 --- a/numpy/lib/src/_compiled_base.c +++ b/numpy/lib/src/_compiled_base.c @@ -7,6 +7,10 @@ #include "numpy/ufuncobject.h" #include "string.h" +#if (PY_VERSION_HEX < 0x02060000) +#define Py_TYPE(o) (((PyObject*)(o))->ob_type) +#endif + static npy_intp incr_slot_(double x, double *bins, npy_intp lbins) { @@ -670,7 +674,10 @@ arr_interp(PyObject *NPY_UNUSED(self), PyObject *args, PyObject *kwdict) /* only pre-calculate slopes if there are relatively few of them. */ if (lenxp <= lenx) { - slopes = (double *) PyDataMem_NEW((lenxp - 1)*sizeof(double)); + slopes = (double *) PyArray_malloc((lenxp - 1)*sizeof(double)); + if (! slopes) { + goto fail; + } NPY_BEGIN_ALLOW_THREADS; for (i = 0; i < lenxp - 1; i++) { slopes[i] = (dy[i + 1] - dy[i])/(dx[i + 1] - dx[i]); @@ -692,7 +699,7 @@ arr_interp(PyObject *NPY_UNUSED(self), PyObject *args, PyObject *kwdict) } } NPY_END_ALLOW_THREADS; - PyDataMem_FREE(slopes); + PyArray_free(slopes); } else { NPY_BEGIN_ALLOW_THREADS; diff --git a/numpy/lib/tests/test_arraysetops.py b/numpy/lib/tests/test_arraysetops.py index e40c155a4..b0d2ca7c3 100644 --- a/numpy/lib/tests/test_arraysetops.py +++ b/numpy/lib/tests/test_arraysetops.py @@ -8,31 +8,62 @@ from numpy.lib.arraysetops import * import warnings -class TestAso(TestCase): - def test_unique( self ): - a = np.array( [5, 7, 1, 2, 1, 5, 7] ) - - ec = np.array( [1, 2, 5, 7] ) - c = unique( a ) - assert_array_equal( c, ec ) - - vals, indices = unique( a, return_index=True ) +class TestSetOps(TestCase): - ed = np.array( [2, 3, 0, 1] ) - assert_array_equal(vals, ec) - assert_array_equal(indices, ed) - - vals, ind0, ind1 = unique( a, return_index=True, - return_inverse=True ) - + def test_unique( self ): - ee = np.array( [2, 3, 0, 1, 0, 2, 3] ) - assert_array_equal(vals, ec) - assert_array_equal(ind0, ed) - assert_array_equal(ind1, ee) + def check_all(a, b, i1, i2, dt): + msg = "check values failed for type '%s'" % dt + v = unique(a) + assert_array_equal(v, b, msg) + + msg = "check indexes failed for type '%s'" % dt + v, j = unique(a, 1, 0) + assert_array_equal(v, b, msg) + assert_array_equal(j, i1, msg) + + msg = "check reverse indexes failed for type '%s'" % dt + v, j = unique(a, 0, 1) + assert_array_equal(v, b, msg) + assert_array_equal(j, i2, msg) + + msg = "check with all indexes failed for type '%s'" % dt + v, j1, j2 = unique(a, 1, 1) + assert_array_equal(v, b, msg) + assert_array_equal(j1, i1, msg) + assert_array_equal(j2, i2, msg) + + a = [5, 7, 1, 2, 1, 5, 7]*10 + b = [1, 2, 5, 7] + i1 = [2, 3, 0, 1] + i2 = [2, 3, 0, 1, 0, 2, 3]*10 + + # test for numeric arrays + types = [] + types.extend(np.typecodes['AllInteger']) + types.extend(np.typecodes['AllFloat']) + types.append('datetime64[D]') + types.append('timedelta64[D]') + for dt in types: + aa = np.array(a, dt) + bb = np.array(b, dt) + check_all(aa, bb, i1, i2, dt) + + # test for object arrays + dt = 'O' + aa = np.empty(len(a), dt) + aa[:] = a + bb = np.empty(len(b), dt) + bb[:] = b + check_all(aa, bb, i1, i2, dt) + + # test for structured arrays + dt = [('', 'i'), ('', 'i')] + aa = np.array(zip(a,a), dt) + bb = np.array(zip(b,b), dt) + check_all(aa, bb, i1, i2, dt) - assert_array_equal([], unique([])) def test_intersect1d( self ): # unique inputs diff --git a/numpy/lib/tests/test_io.py b/numpy/lib/tests/test_io.py index 8922070df..f8caeedb6 100644 --- a/numpy/lib/tests/test_io.py +++ b/numpy/lib/tests/test_io.py @@ -167,6 +167,52 @@ class TestSavezLoad(RoundtripTest, TestCase): if errors: raise AssertionError(errors) + def test_not_closing_opened_fid(self): + # Test that issue #2178 is fixed: + # verify could seek on 'loaded' file + + fd, tmp = mkstemp(suffix='.npz') + os.close(fd) + try: + fp = open(tmp, 'wb') + np.savez(fp, data='LOVELY LOAD') + fp.close() + + fp = open(tmp, 'rb', 10000) + fp.seek(0) + assert_(not fp.closed) + _ = np.load(fp)['data'] + assert_(not fp.closed) # must not get closed by .load(opened fp) + fp.seek(0) + assert_(not fp.closed) + + finally: + fp.close() + os.remove(tmp) + + def test_closing_fid(self): + # Test that issue #1517 (too many opened files) remains closed + # It might be a "week" test since failed to get triggered on + # e.g. Debian sid of 2012 Jul 05 but was reported to + # trigger the failure on Ubuntu 10.04: + # http://projects.scipy.org/numpy/ticket/1517#comment:2 + fd, tmp = mkstemp(suffix='.npz') + os.close(fd) + + try: + fp = open(tmp, 'wb') + np.savez(fp, data='LOVELY LOAD') + fp.close() + + for i in range(1, 1025): + try: + np.load(tmp)["data"] + except Exception, e: + raise AssertionError("Failed to load data from a file: %s" % e) + finally: + os.remove(tmp) + + class TestSaveTxt(TestCase): def test_array(self): a = np.array([[1, 2], [3, 4]], float) @@ -1381,8 +1427,6 @@ M 33 21.99 usecols=("A", "C", "E"), names=True) assert_equal(test.dtype.names, ctrl_names) - - def test_fixed_width_names(self): "Test fix-width w/ names" data = " A B C\n 0 1 2.3\n 45 67 9." @@ -1406,6 +1450,14 @@ M 33 21.99 test = np.ndfromtxt(StringIO(data), **kwargs) assert_equal(test, ctrl) + def test_comments_is_none(self): + # Github issue 329 (None was previously being converted to 'None'). + test = np.genfromtxt(StringIO("test1,testNonetherestofthedata"), + dtype=None, comments=None, delimiter=',') + assert_equal(test[1], asbytes('testNonetherestofthedata')) + test = np.genfromtxt(StringIO("test1, testNonetherestofthedata"), + dtype=None, comments=None, delimiter=',') + assert_equal(test[1], asbytes(' testNonetherestofthedata')) def test_recfromtxt(self): # @@ -1426,7 +1478,6 @@ M 33 21.99 assert_equal(test.mask, control.mask) assert_equal(test.A, [0, 2]) - def test_recfromcsv(self): # data = StringIO('A,B\n0,1\n2,3') diff --git a/numpy/lib/type_check.py b/numpy/lib/type_check.py index c116c7e4a..e22d63156 100644 --- a/numpy/lib/type_check.py +++ b/numpy/lib/type_check.py @@ -233,11 +233,10 @@ def isreal(x): def iscomplexobj(x): """ - Return True if x is a complex type or an array of complex numbers. + Check for a complex type or an array of complex numbers. - The type of the input is checked, not the value. So even if the input - has an imaginary part equal to zero, `iscomplexobj` evaluates to True - if the data type is complex. + The type of the input is checked, not the value. Even if the input + has an imaginary part equal to zero, `iscomplexobj` evaluates to True. Parameters ---------- @@ -246,8 +245,9 @@ def iscomplexobj(x): Returns ------- - y : bool - The return value, True if `x` is of a complex type. + iscomplexobj : bool + The return value, True if `x` is of a complex type or has at least + one complex element. See Also -------- |