From 1725a5a5a83149b8c9be5314593a281d429ab1bb Mon Sep 17 00:00:00 2001 From: Bharat123rox Date: Wed, 24 Apr 2019 14:23:35 +0530 Subject: Resolve confusion regarding hashtag in header line of csv --- numpy/lib/npyio.py | 1 + 1 file changed, 1 insertion(+) (limited to 'numpy/lib') diff --git a/numpy/lib/npyio.py b/numpy/lib/npyio.py index ed2e26aac..26bf727c5 100644 --- a/numpy/lib/npyio.py +++ b/numpy/lib/npyio.py @@ -1572,6 +1572,7 @@ def genfromtxt(fname, dtype=float, comments='#', delimiter=None, column, individually. comments : str, optional The character used to indicate the start of a comment. + By default, the character '#' starts a comment. All the characters occurring on a line after a comment are discarded delimiter : str, int, or sequence, optional The string used to separate values. By default, any consecutive -- cgit v1.2.1 From 4ebd08df74769ad0628a05e1d0654244008ab10b Mon Sep 17 00:00:00 2001 From: Bharat123rox Date: Wed, 24 Apr 2019 22:08:10 +0530 Subject: Clarify deletechars in docs --- numpy/lib/npyio.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'numpy/lib') diff --git a/numpy/lib/npyio.py b/numpy/lib/npyio.py index 26bf727c5..24c857a75 100644 --- a/numpy/lib/npyio.py +++ b/numpy/lib/npyio.py @@ -1572,7 +1572,9 @@ def genfromtxt(fname, dtype=float, comments='#', delimiter=None, column, individually. comments : str, optional The character used to indicate the start of a comment. - By default, the character '#' starts a comment. + By default, the character '#' starts a comment. If some other + character is used, override `deletechars` to preserve + special characters in the header names. All the characters occurring on a line after a comment are discarded delimiter : str, int, or sequence, optional The string used to separate values. By default, any consecutive @@ -1611,7 +1613,8 @@ def genfromtxt(fname, dtype=float, comments='#', delimiter=None, for example, `file` would become `file_`. deletechars : str, optional A string combining invalid characters that must be deleted from the - names. + names. In case `comments` is something other than '#', this must be + an empty string in order to preserve special characters in the names. defaultfmt : str, optional A format used to define default field names, such as "f%i" or "f_%02i". autostrip : bool, optional -- cgit v1.2.1 From d7de4ad70d6794b36e4789e4f6146a884113bd66 Mon Sep 17 00:00:00 2001 From: ayir Date: Wed, 10 Apr 2019 17:00:25 +0200 Subject: ENH: add clearer error message for diff(0-d) --- numpy/lib/function_base.py | 2 ++ numpy/lib/tests/test_function_base.py | 3 +++ 2 files changed, 5 insertions(+) (limited to 'numpy/lib') diff --git a/numpy/lib/function_base.py b/numpy/lib/function_base.py index fb40ef927..7a405b382 100644 --- a/numpy/lib/function_base.py +++ b/numpy/lib/function_base.py @@ -1235,6 +1235,8 @@ def diff(a, n=1, axis=-1, prepend=np._NoValue, append=np._NoValue): a = asanyarray(a) nd = a.ndim + if nd == 0: + raise ValueError("diff requires input that is at least one dimensional") axis = normalize_axis_index(axis, nd) combined = [] diff --git a/numpy/lib/tests/test_function_base.py b/numpy/lib/tests/test_function_base.py index 1e04bfaec..8f1b56e3f 100644 --- a/numpy/lib/tests/test_function_base.py +++ b/numpy/lib/tests/test_function_base.py @@ -695,6 +695,9 @@ class TestDiff(object): assert_raises(np.AxisError, diff, x, axis=3) assert_raises(np.AxisError, diff, x, axis=-4) + x = np.array(1.11111111111, np.float64) + assert_raises(ValueError, diff, x) + def test_nd(self): x = 20 * rand(10, 20, 30) out1 = x[:, :, 1:] - x[:, :, :-1] -- cgit v1.2.1 From 666d92ac85a6adf0fec7361c3340fc6ab12c8330 Mon Sep 17 00:00:00 2001 From: mattip Date: Tue, 30 Apr 2019 06:47:29 -0400 Subject: BUG: handle subarrays in descr_to_dtype --- numpy/lib/format.py | 7 ++++--- numpy/lib/tests/test_format.py | 1 + 2 files changed, 5 insertions(+), 3 deletions(-) (limited to 'numpy/lib') diff --git a/numpy/lib/format.py b/numpy/lib/format.py index 553c9371d..abd98dd22 100644 --- a/numpy/lib/format.py +++ b/numpy/lib/format.py @@ -261,12 +261,13 @@ def dtype_to_descr(dtype): def descr_to_dtype(descr): ''' descr may be stored as dtype.descr, which is a list of - (name, format, [shape]) tuples. Offsets are not explicitly saved, rather - empty fields with name,format == '', '|Vn' are added as padding. + (name, format, [shape]) tuples where format may be a str or a tuple. + Offsets are not explicitly saved, rather empty fields with + name, format == '', '|Vn' are added as padding. This function reverses the process, eliminating the empty padding fields. ''' - if isinstance(descr, (str, dict)): + if isinstance(descr, (str, dict, tuple)): # No padding removal needed return numpy.dtype(descr) diff --git a/numpy/lib/tests/test_format.py b/numpy/lib/tests/test_format.py index 2ebd483d5..9d12fafc0 100644 --- a/numpy/lib/tests/test_format.py +++ b/numpy/lib/tests/test_format.py @@ -411,6 +411,7 @@ record_arrays = [ np.array(NbufferT, dtype=np.dtype(Ndescr).newbyteorder('<')), np.array(PbufferT, dtype=np.dtype(Pdescr).newbyteorder('>')), np.array(NbufferT, dtype=np.dtype(Ndescr).newbyteorder('>')), + np.zeros(1, dtype=[('c', (' Date: Sun, 14 Apr 2019 15:49:15 -0700 Subject: BUG: Always return views from structured_to_unstructured when possible Also applies to unstructured_to_structured While producing correct resutls, the test added in this commit would previously make an unecessary copy, causing the assertion to fail. The cause was `astype` was being asked to convert from a subarray of shape `(x, y)` to one of `(x*y,)`, which it cannot do without making a copy. This changes the approach used to skip the step of flattening subarrays to 1d --- numpy/lib/recfunctions.py | 4 ++-- numpy/lib/tests/test_recfunctions.py | 9 +++++++++ 2 files changed, 11 insertions(+), 2 deletions(-) (limited to 'numpy/lib') diff --git a/numpy/lib/recfunctions.py b/numpy/lib/recfunctions.py index bf588a490..d412281ff 100644 --- a/numpy/lib/recfunctions.py +++ b/numpy/lib/recfunctions.py @@ -972,7 +972,7 @@ def structured_to_unstructured(arr, dtype=None, copy=False, casting='unsafe'): # next cast to a packed format with all fields converted to new dtype packed_fields = np.dtype({'names': names, - 'formats': [(out_dtype, c) for c in counts]}) + 'formats': [(out_dtype, dt.shape) for dt in dts]}) arr = arr.astype(packed_fields, copy=copy, casting=casting) # finally is it safe to view the packed fields as the unstructured type @@ -1065,7 +1065,7 @@ def unstructured_to_structured(arr, dtype=None, names=None, align=False, # first view as a packed structured array of one dtype packed_fields = np.dtype({'names': names, - 'formats': [(arr.dtype, c) for c in counts]}) + 'formats': [(arr.dtype, dt.shape) for dt in dts]}) arr = np.ascontiguousarray(arr).view(packed_fields) # next cast to an unpacked but flattened format with varied dtypes diff --git a/numpy/lib/tests/test_recfunctions.py b/numpy/lib/tests/test_recfunctions.py index d1fcf2153..d4d317d30 100644 --- a/numpy/lib/tests/test_recfunctions.py +++ b/numpy/lib/tests/test_recfunctions.py @@ -243,6 +243,15 @@ class TestRecFunctions(object): assert_(dd.base is d) assert_(ddd.base is d) + # including uniform fields with subarrays unpacked + d = np.array([(1, [2, 3], [[ 4, 5], [ 6, 7]]), + (8, [9, 10], [[11, 12], [13, 14]])], + dtype=[('x0', 'i4'), ('x1', ('i4', 2)), ('x2', ('i4', (2, 2)))]) + dd = structured_to_unstructured(d) + ddd = unstructured_to_structured(dd, d.dtype) + assert_(dd.base is d) + assert_(ddd.base is d) + # test that nested fields with identical names don't break anything point = np.dtype([('x', int), ('y', int)]) triangle = np.dtype([('a', point), ('b', point), ('c', point)]) -- cgit v1.2.1 From f16c558fc3c3b57c4f2a93a80a778746ddd2f8ef Mon Sep 17 00:00:00 2001 From: psschand Date: Thu, 2 May 2019 06:06:30 +0530 Subject: BUG: fix unravel_index when dimension is greater than 'intp' The PR is: gh13439 Closes gh-9538 --- numpy/lib/tests/test_index_tricks.py | 3 +++ 1 file changed, 3 insertions(+) (limited to 'numpy/lib') diff --git a/numpy/lib/tests/test_index_tricks.py b/numpy/lib/tests/test_index_tricks.py index e687e2f54..2f7e97831 100644 --- a/numpy/lib/tests/test_index_tricks.py +++ b/numpy/lib/tests/test_index_tricks.py @@ -106,6 +106,9 @@ class TestRavelUnravelIndex(object): np.ravel_multi_index(arr, (41, 7, 120, 36, 2706, 8, 6)), [5627771580, 117259570957]) + # test unravel_index for big indices (issue #9538) + assert_raises(ValueError, np.unravel_index, 1, (2**32-1, 2**31+1)) + # test overflow checking for too big array (issue #7546) dummy_arr = ([0],[0]) half_max = np.iinfo(np.intp).max // 2 -- cgit v1.2.1 From b90addd13f17d967d81dc1d7515887d4fcd6ef59 Mon Sep 17 00:00:00 2001 From: mattip Date: Thu, 2 May 2019 11:29:10 -0400 Subject: BUG: parse more subarrays in descr_to_dtype --- numpy/lib/format.py | 9 +++++++-- numpy/lib/tests/test_format.py | 44 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 2 deletions(-) (limited to 'numpy/lib') diff --git a/numpy/lib/format.py b/numpy/lib/format.py index abd98dd22..05fcbc0bc 100644 --- a/numpy/lib/format.py +++ b/numpy/lib/format.py @@ -267,10 +267,15 @@ def descr_to_dtype(descr): This function reverses the process, eliminating the empty padding fields. ''' - if isinstance(descr, (str, dict, tuple)): + if isinstance(descr, (str, dict)): # No padding removal needed return numpy.dtype(descr) - + elif isinstance(descr, tuple): + if isinstance(descr[0], list): + # subtype, will always have a shape descr[1] + dt = descr_to_dtype(descr[0]) + return numpy.dtype((dt, descr[1])) + return numpy.dtype(descr) fields = [] offset = 0 for field in descr: diff --git a/numpy/lib/tests/test_format.py b/numpy/lib/tests/test_format.py index 9d12fafc0..a3ac89cdd 100644 --- a/numpy/lib/tests/test_format.py +++ b/numpy/lib/tests/test_format.py @@ -629,6 +629,50 @@ def test_pickle_disallow(): assert_raises(ValueError, np.save, path, np.array([None], dtype=object), allow_pickle=False) +@pytest.mark.parametrize('dt', [ + np.dtype(np.dtype([('a', np.int8), + ('b', np.int16), + ('c', np.int32), + ], align=True), + (3,)), + np.dtype([('x', ([('a', '|i1'), + ('', '|V3'), + ('b', '|i1'), + ('', '|V3'), + ], + (3,)), + (4,), + )]), + np.dtype([('x', np.dtype({'names':['a','b'], + 'formats':['i1','i1'], + 'offsets':[0,4], + 'itemsize':8, + }, + (3,)), + (4,), + )]), + np.dtype([('x', + (' Date: Sun, 5 May 2019 13:10:03 -0500 Subject: DOC: have notes in histogram_bin_edges match parameter style bins only accepts lowercase strings, but the notes are capitalized. --- numpy/lib/histograms.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'numpy/lib') diff --git a/numpy/lib/histograms.py b/numpy/lib/histograms.py index bd44d2732..ee9a3053c 100644 --- a/numpy/lib/histograms.py +++ b/numpy/lib/histograms.py @@ -555,14 +555,14 @@ def histogram_bin_edges(a, bins=10, range=None, weights=None): using the `ptp` of the data. The final bin count is obtained from ``np.round(np.ceil(range / h))``. - 'Auto' (maximum of the 'Sturges' and 'FD' estimators) + 'auto' (maximum of the 'sturges' and 'fd' estimators) A compromise to get a good value. For small datasets the Sturges value will usually be chosen, while larger datasets will usually default to FD. Avoids the overly conservative behaviour of FD and Sturges for small and large datasets respectively. Switchover point is usually :math:`a.size \approx 1000`. - 'FD' (Freedman Diaconis Estimator) + 'fd' (Freedman Diaconis Estimator) .. math:: h = 2 \frac{IQR}{n^{1/3}} The binwidth is proportional to the interquartile range (IQR) @@ -570,7 +570,7 @@ def histogram_bin_edges(a, bins=10, range=None, weights=None): conservative for small datasets, but is quite good for large datasets. The IQR is very robust to outliers. - 'Scott' + 'scott' .. math:: h = \sigma \sqrt[3]{\frac{24 * \sqrt{\pi}}{n}} The binwidth is proportional to the standard deviation of the @@ -580,14 +580,14 @@ def histogram_bin_edges(a, bins=10, range=None, weights=None): outliers. Values are very similar to the Freedman-Diaconis estimator in the absence of outliers. - 'Rice' + 'rice' .. math:: n_h = 2n^{1/3} The number of bins is only proportional to cube root of ``a.size``. It tends to overestimate the number of bins and it does not take into account data variability. - 'Sturges' + 'sturges' .. math:: n_h = \log _{2}n+1 The number of bins is the base 2 log of ``a.size``. This @@ -595,7 +595,7 @@ def histogram_bin_edges(a, bins=10, range=None, weights=None): larger, non-normal datasets. This is the default method in R's ``hist`` method. - 'Doane' + 'doane' .. math:: n_h = 1 + \log_{2}(n) + \log_{2}(1 + \frac{|g_1|}{\sigma_{g_1}}) @@ -607,7 +607,7 @@ def histogram_bin_edges(a, bins=10, range=None, weights=None): estimates for non-normal datasets. This estimator attempts to account for the skew of the data. - 'Sqrt' + 'sqrt' .. math:: n_h = \sqrt n The simplest and fastest estimator. Only takes into account the -- cgit v1.2.1 From 92d74d04ef5de9dfa7f5c91cdb742f5e9a318bc5 Mon Sep 17 00:00:00 2001 From: mattip Date: Mon, 6 May 2019 08:55:27 -0400 Subject: TEST: tweak two slow tests to speed them up --- numpy/lib/tests/test_histograms.py | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) (limited to 'numpy/lib') diff --git a/numpy/lib/tests/test_histograms.py b/numpy/lib/tests/test_histograms.py index c96b01d42..afaa526af 100644 --- a/numpy/lib/tests/test_histograms.py +++ b/numpy/lib/tests/test_histograms.py @@ -554,15 +554,11 @@ class TestHistogramOptimBinNums(object): return a / (a + b) ll = [[nbins_ratio(seed, size) for size in np.geomspace(start=10, stop=100, num=4).round().astype(int)] - for seed in range(256)] + for seed in range(10)] # the average difference between the two methods decreases as the dataset size increases. - assert_almost_equal(abs(np.mean(ll, axis=0) - 0.5), - [0.1065248, - 0.0968844, - 0.0331818, - 0.0178057], - decimal=3) + avg = abs(np.mean(ll, axis=0) - 0.5) + assert_almost_equal(avg, [0.15, 0.09, 0.08, 0.03], decimal=2) def test_simple_range(self): """ -- cgit v1.2.1 From c2de04da9b0feb2853b9814c7953aa51a29a058e Mon Sep 17 00:00:00 2001 From: mattip Date: Wed, 13 Feb 2019 18:14:05 +0200 Subject: ENH: add 'order' keyword to packbits, unpackbits --- numpy/lib/tests/test_packbits.py | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) (limited to 'numpy/lib') diff --git a/numpy/lib/tests/test_packbits.py b/numpy/lib/tests/test_packbits.py index 00d5ca827..5f650996f 100644 --- a/numpy/lib/tests/test_packbits.py +++ b/numpy/lib/tests/test_packbits.py @@ -2,7 +2,7 @@ from __future__ import division, absolute_import, print_function import numpy as np from numpy.testing import assert_array_equal, assert_equal, assert_raises - +import pytest def test_packbits(): # Copied from the docstring. @@ -50,8 +50,8 @@ def test_packbits_empty_with_axis(): assert_equal(b.dtype, np.uint8) assert_equal(b.shape, out_shape) - -def test_packbits_large(): +@pytest.mark.parametrize('order', ('l', 'b')) +def test_packbits_large(order): # test data large enough for 16 byte vectorization a = np.array([1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, @@ -71,7 +71,7 @@ def test_packbits_large(): a = a.repeat(3) for dtype in '?bBhHiIlLqQ': arr = np.array(a, dtype=dtype) - b = np.packbits(arr, axis=None) + b = np.packbits(arr, axis=None, order=order) assert_equal(b.dtype, np.uint8) r = [252, 127, 192, 3, 254, 7, 252, 0, 7, 31, 240, 0, 28, 1, 255, 252, 113, 248, 3, 255, 192, 28, 15, 192, 28, 126, 0, 224, 127, 255, @@ -81,9 +81,10 @@ def test_packbits_large(): 255, 224, 1, 255, 252, 126, 63, 0, 1, 192, 252, 14, 63, 0, 15, 199, 252, 113, 255, 3, 128, 56, 252, 14, 7, 0, 113, 255, 255, 142, 56, 227, 129, 248, 227, 129, 199, 31, 128] - assert_array_equal(b, r) + if order == 'big': + assert_array_equal(b, r) # equal for size being multiple of 8 - assert_array_equal(np.unpackbits(b)[:-4], a) + assert_array_equal(np.unpackbits(b, order=order)[:-4], a) # check last byte of different remainders (16 byte vectorization) b = [np.packbits(arr[:-i], axis=None)[-1] for i in range(1, 16)] @@ -229,6 +230,20 @@ def test_unpackbits(): [0, 0, 0, 0, 0, 1, 1, 1], [0, 0, 0, 1, 0, 1, 1, 1]])) +def test_pack_unpack_order(): + a = np.array([[2], [7], [23]], dtype=np.uint8) + b = np.unpackbits(a, axis=1) + assert_equal(b.dtype, np.uint8) + b_little = np.unpackbits(a, axis=1, order='little') + b_big = np.unpackbits(a, axis=1, order='big') + assert_array_equal(b, b_big) + assert_array_equal(a, np.packbits(b_little, axis=1, order='little')) + assert_array_equal(b[:,::-1], b_little) + assert_array_equal(a, np.packbits(b_big, axis=1, order='big')) + assert_raises(ValueError, np.unpackbits, a, order='r') + assert_raises(TypeError, np.unpackbits, a, order=10) + + def test_unpackbits_empty(): a = np.empty((0,), dtype=np.uint8) -- cgit v1.2.1 From fa709605401b5acde6fa515239ba29a548c53755 Mon Sep 17 00:00:00 2001 From: mattip Date: Wed, 27 Feb 2019 17:28:35 +0200 Subject: BUG: parametrize tests, fix for interaction of count, order --- numpy/lib/tests/test_packbits.py | 110 ++++++++++++++++++++++++--------------- 1 file changed, 69 insertions(+), 41 deletions(-) (limited to 'numpy/lib') diff --git a/numpy/lib/tests/test_packbits.py b/numpy/lib/tests/test_packbits.py index 5f650996f..d6e96f3ea 100644 --- a/numpy/lib/tests/test_packbits.py +++ b/numpy/lib/tests/test_packbits.py @@ -3,6 +3,7 @@ from __future__ import division, absolute_import, print_function import numpy as np from numpy.testing import assert_array_equal, assert_equal, assert_raises import pytest +from itertools import chain def test_packbits(): # Copied from the docstring. @@ -242,7 +243,7 @@ def test_pack_unpack_order(): assert_array_equal(a, np.packbits(b_big, axis=1, order='big')) assert_raises(ValueError, np.unpackbits, a, order='r') assert_raises(TypeError, np.unpackbits, a, order=10) - + def test_unpackbits_empty(): @@ -283,8 +284,7 @@ def test_unpackbits_large(): assert_array_equal(np.packbits(np.unpackbits(d, axis=0), axis=0), d) -def test_unpackbits_count(): - # test complete invertibility of packbits and unpackbits with count +class TestCount(): x = np.array([ [1, 0, 1, 0, 0, 1, 0], [0, 1, 1, 1, 0, 0, 0], @@ -294,53 +294,81 @@ def test_unpackbits_count(): [0, 0, 1, 1, 1, 0, 0], [0, 1, 0, 1, 0, 1, 0], ], dtype=np.uint8) - padded1 = np.zeros(57, dtype=np.uint8) padded1[:49] = x.ravel() + padded1b = np.zeros(57, dtype=np.uint8) + padded1b[:49] = x[::-1].copy().ravel() + padded2 = np.zeros((9, 9), dtype=np.uint8) + padded2[:7, :7] = x - packed = np.packbits(x) - for count in range(58): - unpacked = np.unpackbits(packed, count=count) + @pytest.mark.parametrize('order', ('l', 'b')) + @pytest.mark.parametrize('count', chain(range(58), range(-1, -57, -1))) + def test_roundtrip(self, order, count): + if count < 0: + # one extra zero of padding + cutoff = count - 1 + else: + cutoff = count + # test complete invertibility of packbits and unpackbits with count + packed = np.packbits(self.x, order=order) + unpacked = np.unpackbits(packed, count=count, order=order) assert_equal(unpacked.dtype, np.uint8) - assert_array_equal(unpacked, padded1[:count]) - for count in range(-1, -57, -1): - unpacked = np.unpackbits(packed, count=count) - assert_equal(unpacked.dtype, np.uint8) - # count -1 because padded1 has 57 instead of 56 elements - assert_array_equal(unpacked, padded1[:count-1]) - for kwargs in [{}, {'count': None}]: + assert_array_equal(unpacked, self.padded1[:cutoff]) + + @pytest.mark.parametrize('kwargs', [ + {}, {'count': None}, + ]) + def test_count(self, kwargs): + packed = np.packbits(self.x) unpacked = np.unpackbits(packed, **kwargs) assert_equal(unpacked.dtype, np.uint8) - assert_array_equal(unpacked, padded1[:-1]) - assert_raises(ValueError, np.unpackbits, packed, count=-57) - - padded2 = np.zeros((9, 9), dtype=np.uint8) - padded2[:7, :7] = x - - packed0 = np.packbits(x, axis=0) - packed1 = np.packbits(x, axis=1) - for count in range(10): - unpacked0 = np.unpackbits(packed0, axis=0, count=count) - assert_equal(unpacked0.dtype, np.uint8) - assert_array_equal(unpacked0, padded2[:count, :x.shape[1]]) - unpacked1 = np.unpackbits(packed1, axis=1, count=count) - assert_equal(unpacked1.dtype, np.uint8) - assert_array_equal(unpacked1, padded2[:x.shape[1], :count]) - for count in range(-1, -9, -1): - unpacked0 = np.unpackbits(packed0, axis=0, count=count) + assert_array_equal(unpacked, self.padded1[:-1]) + + @pytest.mark.parametrize('order', ('l', 'b')) + # delta==-1 when count<0 because one extra zero of padding + @pytest.mark.parametrize('count', chain(range(8), range(-1, -9, -1))) + def test_roundtrip_axis(self, order, count): + if count < 0: + # one extra zero of padding + cutoff = count - 1 + else: + cutoff = count + packed0 = np.packbits(self.x, axis=0, order=order) + unpacked0 = np.unpackbits(packed0, axis=0, count=count, order=order) assert_equal(unpacked0.dtype, np.uint8) - # count -1 because one extra zero of padding - assert_array_equal(unpacked0, padded2[:count-1, :x.shape[1]]) - unpacked1 = np.unpackbits(packed1, axis=1, count=count) + assert_array_equal(unpacked0, self.padded2[:cutoff, :self.x.shape[1]]) + + packed1 = np.packbits(self.x, axis=1, order=order) + unpacked1 = np.unpackbits(packed1, axis=1, count=count, order=order) assert_equal(unpacked1.dtype, np.uint8) - assert_array_equal(unpacked1, padded2[:x.shape[0], :count-1]) - for kwargs in [{}, {'count': None}]: + assert_array_equal(unpacked1, self.padded2[:self.x.shape[0], :cutoff]) + + @pytest.mark.parametrize('kwargs', [ + {}, {'count': None}, + {'order' : 'l'}, {'order': 'l', 'count': None}, + {'order' : 'b'}, {'order': 'b', 'count': None}, + ]) + def test_axis_count(self, kwargs): + packed0 = np.packbits(self.x, axis=0) unpacked0 = np.unpackbits(packed0, axis=0, **kwargs) assert_equal(unpacked0.dtype, np.uint8) - assert_array_equal(unpacked0, padded2[:-1, :x.shape[1]]) + if kwargs.get('order', 'b') == 'b': + assert_array_equal(unpacked0, self.padded2[:-1, :self.x.shape[1]]) + else: + assert_array_equal(unpacked0[::-1, :], self.padded2[:-1, :self.x.shape[1]]) + + packed1 = np.packbits(self.x, axis=1) unpacked1 = np.unpackbits(packed1, axis=1, **kwargs) assert_equal(unpacked1.dtype, np.uint8) - assert_array_equal(unpacked1, padded2[:x.shape[0], :-1]) - assert_raises(ValueError, np.unpackbits, packed0, axis=0, count=-9) - assert_raises(ValueError, np.unpackbits, packed1, axis=1, count=-9) - + if kwargs.get('order', 'b') == 'b': + assert_array_equal(unpacked1, self.padded2[:self.x.shape[0], :-1]) + else: + assert_array_equal(unpacked1[:, ::-1], self.padded2[:self.x.shape[0], :-1]) + + def test_bad_count(self): + packed0 = np.packbits(self.x, axis=0) + assert_raises(ValueError, np.unpackbits, packed0, axis=0, count=-9) + packed1 = np.packbits(self.x, axis=1) + assert_raises(ValueError, np.unpackbits, packed1, axis=1, count=-9) + packed = np.packbits(self.x) + assert_raises(ValueError, np.unpackbits, packed, count=-57) -- cgit v1.2.1 From 16afbcd7138d5b0124b6e2031a7f7a68e9fab677 Mon Sep 17 00:00:00 2001 From: Eric Wieser Date: Wed, 24 Apr 2019 21:35:15 -0700 Subject: DOC: Show the default value of deletechars in the signature of genfromtxt --- numpy/lib/npyio.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'numpy/lib') diff --git a/numpy/lib/npyio.py b/numpy/lib/npyio.py index ed2e26aac..de165e843 100644 --- a/numpy/lib/npyio.py +++ b/numpy/lib/npyio.py @@ -1549,7 +1549,8 @@ def fromregex(file, regexp, dtype, encoding=None): def genfromtxt(fname, dtype=float, comments='#', delimiter=None, skip_header=0, skip_footer=0, converters=None, missing_values=None, filling_values=None, usecols=None, - names=None, excludelist=None, deletechars=None, + names=None, excludelist=None, + deletechars=''.join(sorted(NameValidator.defaultdeletechars)), replace_space='_', autostrip=False, case_sensitive=True, defaultfmt="f%i", unpack=None, usemask=False, loose=True, invalid_raise=True, max_rows=None, encoding='bytes'): -- cgit v1.2.1 From 599dbade766ad3fecc82afa308803a2e0082c149 Mon Sep 17 00:00:00 2001 From: Eric Wieser Date: Thu, 28 Dec 2017 11:17:52 +0000 Subject: API: Make MaskedArray.mask return a view, rather than the underlying mask This prevents consumers from reshaping the mask in place, which breaks things As a result, `x.mask is x.mask` returns `False`, but this was already true of `x.data is x.data`. May also be related to gh-10270 --- numpy/lib/tests/test_function_base.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'numpy/lib') diff --git a/numpy/lib/tests/test_function_base.py b/numpy/lib/tests/test_function_base.py index 6d32c365a..0702c3856 100644 --- a/numpy/lib/tests/test_function_base.py +++ b/numpy/lib/tests/test_function_base.py @@ -945,7 +945,7 @@ class TestGradient(object): assert_equal(type(out), type(x)) # And make sure that the output and input don't have aliased mask # arrays - assert_(x.mask is not out.mask) + assert_(x._mask is not out._mask) # Also check that edge_order=2 doesn't alter the original mask x2 = np.ma.arange(5) x2[2] = np.ma.masked -- cgit v1.2.1 From b415ffad5327c5ff656b94c6aaf683209c9c8104 Mon Sep 17 00:00:00 2001 From: mattip Date: Wed, 13 Mar 2019 10:05:40 +0200 Subject: ENH: changes from review --- numpy/lib/tests/test_packbits.py | 54 +++++++++++++++++++++------------------- 1 file changed, 29 insertions(+), 25 deletions(-) (limited to 'numpy/lib') diff --git a/numpy/lib/tests/test_packbits.py b/numpy/lib/tests/test_packbits.py index d6e96f3ea..95a465c36 100644 --- a/numpy/lib/tests/test_packbits.py +++ b/numpy/lib/tests/test_packbits.py @@ -51,8 +51,8 @@ def test_packbits_empty_with_axis(): assert_equal(b.dtype, np.uint8) assert_equal(b.shape, out_shape) -@pytest.mark.parametrize('order', ('l', 'b')) -def test_packbits_large(order): +@pytest.mark.parametrize('bitorder', ('little', 'big')) +def test_packbits_large(bitorder): # test data large enough for 16 byte vectorization a = np.array([1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, @@ -72,7 +72,7 @@ def test_packbits_large(order): a = a.repeat(3) for dtype in '?bBhHiIlLqQ': arr = np.array(a, dtype=dtype) - b = np.packbits(arr, axis=None, order=order) + b = np.packbits(arr, axis=None, bitorder=bitorder) assert_equal(b.dtype, np.uint8) r = [252, 127, 192, 3, 254, 7, 252, 0, 7, 31, 240, 0, 28, 1, 255, 252, 113, 248, 3, 255, 192, 28, 15, 192, 28, 126, 0, 224, 127, 255, @@ -82,10 +82,10 @@ def test_packbits_large(order): 255, 224, 1, 255, 252, 126, 63, 0, 1, 192, 252, 14, 63, 0, 15, 199, 252, 113, 255, 3, 128, 56, 252, 14, 7, 0, 113, 255, 255, 142, 56, 227, 129, 248, 227, 129, 199, 31, 128] - if order == 'big': + if bitorder == 'big': assert_array_equal(b, r) # equal for size being multiple of 8 - assert_array_equal(np.unpackbits(b, order=order)[:-4], a) + assert_array_equal(np.unpackbits(b, bitorder=bitorder)[:-4], a) # check last byte of different remainders (16 byte vectorization) b = [np.packbits(arr[:-i], axis=None)[-1] for i in range(1, 16)] @@ -235,14 +235,14 @@ def test_pack_unpack_order(): a = np.array([[2], [7], [23]], dtype=np.uint8) b = np.unpackbits(a, axis=1) assert_equal(b.dtype, np.uint8) - b_little = np.unpackbits(a, axis=1, order='little') - b_big = np.unpackbits(a, axis=1, order='big') + b_little = np.unpackbits(a, axis=1, bitorder='little') + b_big = np.unpackbits(a, axis=1, bitorder='big') assert_array_equal(b, b_big) - assert_array_equal(a, np.packbits(b_little, axis=1, order='little')) + assert_array_equal(a, np.packbits(b_little, axis=1, bitorder='little')) assert_array_equal(b[:,::-1], b_little) - assert_array_equal(a, np.packbits(b_big, axis=1, order='big')) - assert_raises(ValueError, np.unpackbits, a, order='r') - assert_raises(TypeError, np.unpackbits, a, order=10) + assert_array_equal(a, np.packbits(b_big, axis=1, bitorder='big')) + assert_raises(ValueError, np.unpackbits, a, bitorder='r') + assert_raises(TypeError, np.unpackbits, a, bitorder=10) @@ -301,17 +301,17 @@ class TestCount(): padded2 = np.zeros((9, 9), dtype=np.uint8) padded2[:7, :7] = x - @pytest.mark.parametrize('order', ('l', 'b')) + @pytest.mark.parametrize('bitorder', ('little', 'big')) @pytest.mark.parametrize('count', chain(range(58), range(-1, -57, -1))) - def test_roundtrip(self, order, count): + def test_roundtrip(self, bitorder, count): if count < 0: # one extra zero of padding cutoff = count - 1 else: cutoff = count # test complete invertibility of packbits and unpackbits with count - packed = np.packbits(self.x, order=order) - unpacked = np.unpackbits(packed, count=count, order=order) + packed = np.packbits(self.x, bitorder=bitorder) + unpacked = np.unpackbits(packed, count=count, bitorder=bitorder) assert_equal(unpacked.dtype, np.uint8) assert_array_equal(unpacked, self.padded1[:cutoff]) @@ -324,35 +324,39 @@ class TestCount(): assert_equal(unpacked.dtype, np.uint8) assert_array_equal(unpacked, self.padded1[:-1]) - @pytest.mark.parametrize('order', ('l', 'b')) + @pytest.mark.parametrize('bitorder', ('little', 'big')) # delta==-1 when count<0 because one extra zero of padding @pytest.mark.parametrize('count', chain(range(8), range(-1, -9, -1))) - def test_roundtrip_axis(self, order, count): + def test_roundtrip_axis(self, bitorder, count): if count < 0: # one extra zero of padding cutoff = count - 1 else: cutoff = count - packed0 = np.packbits(self.x, axis=0, order=order) - unpacked0 = np.unpackbits(packed0, axis=0, count=count, order=order) + packed0 = np.packbits(self.x, axis=0, bitorder=bitorder) + unpacked0 = np.unpackbits(packed0, axis=0, count=count, + bitorder=bitorder) assert_equal(unpacked0.dtype, np.uint8) assert_array_equal(unpacked0, self.padded2[:cutoff, :self.x.shape[1]]) - packed1 = np.packbits(self.x, axis=1, order=order) - unpacked1 = np.unpackbits(packed1, axis=1, count=count, order=order) + packed1 = np.packbits(self.x, axis=1, bitorder=bitorder) + unpacked1 = np.unpackbits(packed1, axis=1, count=count, + bitorder=bitorder) assert_equal(unpacked1.dtype, np.uint8) assert_array_equal(unpacked1, self.padded2[:self.x.shape[0], :cutoff]) @pytest.mark.parametrize('kwargs', [ {}, {'count': None}, - {'order' : 'l'}, {'order': 'l', 'count': None}, - {'order' : 'b'}, {'order': 'b', 'count': None}, + {'bitorder' : 'little'}, + {'bitorder': 'little', 'count': None}, + {'bitorder' : 'big'}, + {'bitorder': 'big', 'count': None}, ]) def test_axis_count(self, kwargs): packed0 = np.packbits(self.x, axis=0) unpacked0 = np.unpackbits(packed0, axis=0, **kwargs) assert_equal(unpacked0.dtype, np.uint8) - if kwargs.get('order', 'b') == 'b': + if kwargs.get('bitorder', 'big') == 'big': assert_array_equal(unpacked0, self.padded2[:-1, :self.x.shape[1]]) else: assert_array_equal(unpacked0[::-1, :], self.padded2[:-1, :self.x.shape[1]]) @@ -360,7 +364,7 @@ class TestCount(): packed1 = np.packbits(self.x, axis=1) unpacked1 = np.unpackbits(packed1, axis=1, **kwargs) assert_equal(unpacked1.dtype, np.uint8) - if kwargs.get('order', 'b') == 'b': + if kwargs.get('bitorder', 'big') == 'big': assert_array_equal(unpacked1, self.padded2[:self.x.shape[0], :-1]) else: assert_array_equal(unpacked1[:, ::-1], self.padded2[:self.x.shape[0], :-1]) -- cgit v1.2.1 From bd73a15363295658e150754edf6d1073cbdb3975 Mon Sep 17 00:00:00 2001 From: mattip Date: Sun, 5 May 2019 19:50:06 -0400 Subject: MAINT: remove uneeded code --- numpy/lib/format.py | 10 ++++------ numpy/lib/tests/test_format.py | 29 ++++++++++++++++++++--------- 2 files changed, 24 insertions(+), 15 deletions(-) (limited to 'numpy/lib') diff --git a/numpy/lib/format.py b/numpy/lib/format.py index 05fcbc0bc..86f71eda9 100644 --- a/numpy/lib/format.py +++ b/numpy/lib/format.py @@ -267,15 +267,13 @@ def descr_to_dtype(descr): This function reverses the process, eliminating the empty padding fields. ''' - if isinstance(descr, (str, dict)): + if isinstance(descr, str): # No padding removal needed return numpy.dtype(descr) elif isinstance(descr, tuple): - if isinstance(descr[0], list): - # subtype, will always have a shape descr[1] - dt = descr_to_dtype(descr[0]) - return numpy.dtype((dt, descr[1])) - return numpy.dtype(descr) + # subtype, will always have a shape descr[1] + dt = descr_to_dtype(descr[0]) + return numpy.dtype((dt, descr[1])) fields = [] offset = 0 for field in descr: diff --git a/numpy/lib/tests/test_format.py b/numpy/lib/tests/test_format.py index a3ac89cdd..4a3fbdf57 100644 --- a/numpy/lib/tests/test_format.py +++ b/numpy/lib/tests/test_format.py @@ -635,14 +635,6 @@ def test_pickle_disallow(): ('c', np.int32), ], align=True), (3,)), - np.dtype([('x', ([('a', '|i1'), - ('', '|V3'), - ('b', '|i1'), - ('', '|V3'), - ], - (3,)), - (4,), - )]), np.dtype([('x', np.dtype({'names':['a','b'], 'formats':['i1','i1'], 'offsets':[0,4], @@ -665,8 +657,27 @@ def test_pickle_disallow(): )), (4,) ))) - ]) + ]), + np.dtype([ + ('a', np.dtype(( + np.dtype(( + np.dtype(( + np.dtype([ + ('a', int), + ('b', np.dtype({'names':['a','b'], + 'formats':['i1','i1'], + 'offsets':[0,4], + 'itemsize':8})), + ]), + (3,), + )), + (4,), + )), + (5,), + ))) + ]), ]) + def test_descr_to_dtype(dt): dt1 = format.descr_to_dtype(dt.descr) assert_equal_(dt1, dt) -- cgit v1.2.1 From 5f432a1851beaddd737a7925d47fae217405d43a Mon Sep 17 00:00:00 2001 From: Bharat123rox Date: Sun, 12 May 2019 23:37:30 +0530 Subject: Add suggested example --- numpy/lib/npyio.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) (limited to 'numpy/lib') diff --git a/numpy/lib/npyio.py b/numpy/lib/npyio.py index 504a71fe7..862c8c9dc 100644 --- a/numpy/lib/npyio.py +++ b/numpy/lib/npyio.py @@ -1573,9 +1573,6 @@ def genfromtxt(fname, dtype=float, comments='#', delimiter=None, column, individually. comments : str, optional The character used to indicate the start of a comment. - By default, the character '#' starts a comment. If some other - character is used, override `deletechars` to preserve - special characters in the header names. All the characters occurring on a line after a comment are discarded delimiter : str, int, or sequence, optional The string used to separate values. By default, any consecutive @@ -1614,8 +1611,7 @@ def genfromtxt(fname, dtype=float, comments='#', delimiter=None, for example, `file` would become `file_`. deletechars : str, optional A string combining invalid characters that must be deleted from the - names. In case `comments` is something other than '#', this must be - an empty string in order to preserve special characters in the names. + names. defaultfmt : str, optional A format used to define default field names, such as "f%i" or "f_%02i". autostrip : bool, optional @@ -1721,6 +1717,14 @@ def genfromtxt(fname, dtype=float, comments='#', delimiter=None, array((1, 1.3, b'abcde'), dtype=[('intvar', '>> f = StringIO(''' + ... text,# of chars + ... hello world,11 + ... numpy,5''') + >>> np.genfromtxt(f, dtype='S12,S12', delimiter=',') + """ if max_rows is not None: if skip_footer: -- cgit v1.2.1 From 6879dcd5041a64327d0245263347a91f35e96a95 Mon Sep 17 00:00:00 2001 From: Bharat123rox Date: Sun, 12 May 2019 23:43:18 +0530 Subject: Complete the example --- numpy/lib/npyio.py | 2 ++ 1 file changed, 2 insertions(+) (limited to 'numpy/lib') diff --git a/numpy/lib/npyio.py b/numpy/lib/npyio.py index 862c8c9dc..1845305d1 100644 --- a/numpy/lib/npyio.py +++ b/numpy/lib/npyio.py @@ -1724,6 +1724,8 @@ def genfromtxt(fname, dtype=float, comments='#', delimiter=None, ... hello world,11 ... numpy,5''') >>> np.genfromtxt(f, dtype='S12,S12', delimiter=',') + array([(b'text', b''), (b'hello world', b'11'), (b'numpy', b'5')], + dtype=[('f0', 'S12'), ('f1', 'S12')]) """ if max_rows is not None: -- cgit v1.2.1 From a3a19daff9330f0196aba90582450d022fc8798c Mon Sep 17 00:00:00 2001 From: Eric Wieser Date: Sun, 12 May 2019 18:35:52 -0700 Subject: ENH: Allow broadcast to be called with zero arguments Follows on from gh-6905 which reduced the limit from 2 to 1. Let's go all the way to zero. Just as for `broadcast(broadcast(a), b)` is interpreted as `broadcast(a, b)` , this change interprets `broadcast(broadcast(), a)` as `broadcast(a)`. --- numpy/lib/stride_tricks.py | 2 -- 1 file changed, 2 deletions(-) (limited to 'numpy/lib') diff --git a/numpy/lib/stride_tricks.py b/numpy/lib/stride_tricks.py index 0dc36e41c..fd401c57c 100644 --- a/numpy/lib/stride_tricks.py +++ b/numpy/lib/stride_tricks.py @@ -186,8 +186,6 @@ def _broadcast_shape(*args): """Returns the shape of the arrays that would result from broadcasting the supplied arrays against each other. """ - if not args: - return () # use the old-iterator because np.nditer does not handle size 0 arrays # consistently b = np.broadcast(*args[:32]) -- cgit v1.2.1 From 11109b8ad2e69dbce2c2e7867d6d1c0c03150bc2 Mon Sep 17 00:00:00 2001 From: Bharat123rox Date: Thu, 16 May 2019 00:46:33 +0530 Subject: DOC: Mention that expand_dims returns a view --- numpy/lib/shape_base.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'numpy/lib') diff --git a/numpy/lib/shape_base.py b/numpy/lib/shape_base.py index ac2a25604..21070ce36 100644 --- a/numpy/lib/shape_base.py +++ b/numpy/lib/shape_base.py @@ -533,7 +533,7 @@ def expand_dims(a, axis): ------- res : ndarray Output array. The number of dimensions is one greater than that of - the input array. + the input array. This is a view on the original array `a`. See Also -------- -- cgit v1.2.1 From 5aa6c71774a04494458a354586ef19e6726250e3 Mon Sep 17 00:00:00 2001 From: Bharat123rox Date: Thu, 16 May 2019 12:27:23 +0530 Subject: Make wording changes according to @mattip --- numpy/lib/shape_base.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'numpy/lib') diff --git a/numpy/lib/shape_base.py b/numpy/lib/shape_base.py index 21070ce36..8ebe7a695 100644 --- a/numpy/lib/shape_base.py +++ b/numpy/lib/shape_base.py @@ -532,8 +532,7 @@ def expand_dims(a, axis): Returns ------- res : ndarray - Output array. The number of dimensions is one greater than that of - the input array. This is a view on the original array `a`. + View of `a` with the number of dimensions increased by one. See Also -------- -- cgit v1.2.1