diff options
author | Joseph Fox-Rabinovitz <joseph.r.fox-rabinovitz@nasa.gov> | 2018-04-06 13:53:02 -0400 |
---|---|---|
committer | Joseph Fox-Rabinovitz <joseph.r.fox-rabinovitz@nasa.gov> | 2019-02-25 10:22:27 -0500 |
commit | df00dbf6e5d2c4957da9f3e0c171bda292009c2f (patch) | |
tree | f55d52ae7d2833270e7e14755ec049c23ac671fd /numpy/core/multiarray.py | |
parent | 7aebced7e5ac26ba0ab2171b24991350523e9aa3 (diff) | |
download | numpy-df00dbf6e5d2c4957da9f3e0c171bda292009c2f.tar.gz |
ENH: Added count parameter to unpackbits
Tests are included.
Couple of minor fixes:
- Fixed packbits/unpackbits docs to reflect proper keyword names
- Added .pytest_cache to .gitignore
Diffstat (limited to 'numpy/core/multiarray.py')
-rw-r--r-- | numpy/core/multiarray.py | 57 |
1 files changed, 43 insertions, 14 deletions
diff --git a/numpy/core/multiarray.py b/numpy/core/multiarray.py index f2d7a9487..54b3a3e5e 100644 --- a/numpy/core/multiarray.py +++ b/numpy/core/multiarray.py @@ -1107,9 +1107,9 @@ def putmask(a, mask, values): @array_function_from_c_func_and_dispatcher(_multiarray_umath.packbits) -def packbits(myarray, axis=None): +def packbits(a, axis=None): """ - packbits(myarray, axis=None) + packbits(a, axis=None) Packs the elements of a binary-valued array into bits in a uint8 array. @@ -1117,7 +1117,7 @@ def packbits(myarray, axis=None): Parameters ---------- - myarray : array_like + a : array_like An array of integers or booleans whose elements should be packed to bits. axis : int, optional @@ -1154,28 +1154,39 @@ def packbits(myarray, axis=None): and 32 = 0010 0000. """ - return (myarray,) + return (a,) @array_function_from_c_func_and_dispatcher(_multiarray_umath.unpackbits) -def unpackbits(myarray, axis=None): +def unpackbits(a, axis=None, count=None): """ - unpackbits(myarray, axis=None) + unpackbits(a, axis=None, count=None) Unpacks elements of a uint8 array into a binary-valued output array. - Each element of `myarray` represents a bit-field that should be unpacked - into a binary-valued output array. The shape of the output array is either - 1-D (if `axis` is None) or the same shape as the input array with unpacking - done along the axis specified. + Each element of `a` represents a bit-field that should be unpacked + into a binary-valued output array. The shape of the output array is + either 1-D (if `axis` is ``None``) or the same shape as the input + array with unpacking done along the axis specified. Parameters ---------- - myarray : ndarray, uint8 type + a : ndarray, uint8 type Input array. axis : int, optional The dimension over which bit-unpacking is done. ``None`` implies unpacking the flattened array. + count : int or None, optional + The number of elements to unpack along `axis`, provided as a way + of undoing the effect of packing a size that is not a multiple + of eight. A non-negative number means to only unpack `count` + bits. A negative number means to trim off that many bits from + the end. ``None`` means to unpack the entire array (the + default). Counts larger than the available number of bits will + add zero padding to the output. Negative counts must not + exceed the available number of bits. + + .. versionadded:: 1.17.0 Returns ------- @@ -1184,8 +1195,8 @@ def unpackbits(myarray, axis=None): See Also -------- - packbits : Packs the elements of a binary-valued array into bits in a uint8 - array. + packbits : Packs the elements of a binary-valued array into bits in + a uint8 array. Examples -------- @@ -1199,9 +1210,27 @@ def unpackbits(myarray, axis=None): array([[0, 0, 0, 0, 0, 0, 1, 0], [0, 0, 0, 0, 0, 1, 1, 1], [0, 0, 0, 1, 0, 1, 1, 1]], dtype=uint8) + >>> c = np.unpackbits(a, axis=1, count=-3) + >>> c + array([[0, 0, 0, 0, 0], + [0, 0, 0, 0, 0], + [0, 0, 0, 1, 0]], dtype=uint8) + + >>> p = np.packbits(b, axis=0) + >>> np.unpackbits(p, axis=0) + array([[0, 0, 0, 0, 0, 0, 1, 0], + [0, 0, 0, 0, 0, 1, 1, 1], + [0, 0, 0, 1, 0, 1, 1, 1], + [0, 0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 0]], dtype=uint8) + >>> np.array_equal(b, np.unpackbits(p, axis=0, count=b.shape[0])) + True """ - return (myarray,) + return (a,) @array_function_from_c_func_and_dispatcher(_multiarray_umath.shares_memory) |