"Fossies" - the Fresh Open Source Software Archive

Member "numpy-1.16.4/numpy/lib/tests/test_packbits.py" (27 May 2019, 12851 Bytes) of package /linux/misc/numpy-1.16.4.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file.

    1 from __future__ import division, absolute_import, print_function
    2 
    3 import numpy as np
    4 from numpy.testing import assert_array_equal, assert_equal, assert_raises
    5 
    6 
    7 def test_packbits():
    8     # Copied from the docstring.
    9     a = [[[1, 0, 1], [0, 1, 0]],
   10          [[1, 1, 0], [0, 0, 1]]]
   11     for dt in '?bBhHiIlLqQ':
   12         arr = np.array(a, dtype=dt)
   13         b = np.packbits(arr, axis=-1)
   14         assert_equal(b.dtype, np.uint8)
   15         assert_array_equal(b, np.array([[[160], [64]], [[192], [32]]]))
   16 
   17     assert_raises(TypeError, np.packbits, np.array(a, dtype=float))
   18 
   19 
   20 def test_packbits_empty():
   21     shapes = [
   22         (0,), (10, 20, 0), (10, 0, 20), (0, 10, 20), (20, 0, 0), (0, 20, 0),
   23         (0, 0, 20), (0, 0, 0),
   24     ]
   25     for dt in '?bBhHiIlLqQ':
   26         for shape in shapes:
   27             a = np.empty(shape, dtype=dt)
   28             b = np.packbits(a)
   29             assert_equal(b.dtype, np.uint8)
   30             assert_equal(b.shape, (0,))
   31 
   32 
   33 def test_packbits_empty_with_axis():
   34     # Original shapes and lists of packed shapes for different axes.
   35     shapes = [
   36         ((0,), [(0,)]),
   37         ((10, 20, 0), [(2, 20, 0), (10, 3, 0), (10, 20, 0)]),
   38         ((10, 0, 20), [(2, 0, 20), (10, 0, 20), (10, 0, 3)]),
   39         ((0, 10, 20), [(0, 10, 20), (0, 2, 20), (0, 10, 3)]),
   40         ((20, 0, 0), [(3, 0, 0), (20, 0, 0), (20, 0, 0)]),
   41         ((0, 20, 0), [(0, 20, 0), (0, 3, 0), (0, 20, 0)]),
   42         ((0, 0, 20), [(0, 0, 20), (0, 0, 20), (0, 0, 3)]),
   43         ((0, 0, 0), [(0, 0, 0), (0, 0, 0), (0, 0, 0)]),
   44     ]
   45     for dt in '?bBhHiIlLqQ':
   46         for in_shape, out_shapes in shapes:
   47             for ax, out_shape in enumerate(out_shapes):
   48                 a = np.empty(in_shape, dtype=dt)
   49                 b = np.packbits(a, axis=ax)
   50                 assert_equal(b.dtype, np.uint8)
   51                 assert_equal(b.shape, out_shape)
   52 
   53 
   54 def test_packbits_large():
   55     # test data large enough for 16 byte vectorization
   56     a = np.array([1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0,
   57                   0, 0, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1,
   58                   1, 1, 0, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0,
   59                   1, 1, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1,
   60                   1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1,
   61                   1, 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 1,
   62                   1, 0, 0, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 1, 1,
   63                   0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1,
   64                   1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0,
   65                   1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1,
   66                   1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0,
   67                   0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 1,
   68                   1, 1, 0, 1, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 0, 1, 1, 0, 0,
   69                   1, 0, 0, 1, 0, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0,
   70                   1, 0, 1, 0, 0, 1, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 0])
   71     a = a.repeat(3)
   72     for dtype in '?bBhHiIlLqQ':
   73         arr = np.array(a, dtype=dtype)
   74         b = np.packbits(arr, axis=None)
   75         assert_equal(b.dtype, np.uint8)
   76         r = [252, 127, 192, 3, 254, 7, 252, 0, 7, 31, 240, 0, 28, 1, 255, 252,
   77              113, 248, 3, 255, 192, 28, 15, 192, 28, 126, 0, 224, 127, 255,
   78              227, 142, 7, 31, 142, 63, 28, 126, 56, 227, 240, 0, 227, 128, 63,
   79              224, 14, 56, 252, 112, 56, 255, 241, 248, 3, 240, 56, 224, 112,
   80              63, 255, 255, 199, 224, 14, 0, 31, 143, 192, 3, 255, 199, 0, 1,
   81              255, 224, 1, 255, 252, 126, 63, 0, 1, 192, 252, 14, 63, 0, 15,
   82              199, 252, 113, 255, 3, 128, 56, 252, 14, 7, 0, 113, 255, 255, 142, 56, 227,
   83              129, 248, 227, 129, 199, 31, 128]
   84         assert_array_equal(b, r)
   85         # equal for size being multiple of 8
   86         assert_array_equal(np.unpackbits(b)[:-4], a)
   87 
   88         # check last byte of different remainders (16 byte vectorization)
   89         b = [np.packbits(arr[:-i], axis=None)[-1] for i in range(1, 16)]
   90         assert_array_equal(b, [128, 128, 128, 31, 30, 28, 24, 16, 0, 0, 0, 199,
   91                                198, 196, 192])
   92 
   93 
   94         arr = arr.reshape(36, 25)
   95         b = np.packbits(arr, axis=0)
   96         assert_equal(b.dtype, np.uint8)
   97         assert_array_equal(b, [[190, 186, 178, 178, 150, 215, 87, 83, 83, 195,
   98                                 199, 206, 204, 204, 140, 140, 136, 136, 8, 40, 105,
   99                                 107, 75, 74, 88],
  100                                [72, 216, 248, 241, 227, 195, 202, 90, 90, 83,
  101                                 83, 119, 127, 109, 73, 64, 208, 244, 189, 45,
  102                                 41, 104, 122, 90, 18],
  103                                [113, 120, 248, 216, 152, 24, 60, 52, 182, 150,
  104                                 150, 150, 146, 210, 210, 246, 255, 255, 223,
  105                                 151, 21, 17, 17, 131, 163],
  106                                [214, 210, 210, 64, 68, 5, 5, 1, 72, 88, 92,
  107                                 92, 78, 110, 39, 181, 149, 220, 222, 218, 218,
  108                                 202, 234, 170, 168],
  109                                [0, 128, 128, 192, 80, 112, 48, 160, 160, 224,
  110                                 240, 208, 144, 128, 160, 224, 240, 208, 144,
  111                                 144, 176, 240, 224, 192, 128]])
  112 
  113         b = np.packbits(arr, axis=1)
  114         assert_equal(b.dtype, np.uint8)
  115         assert_array_equal(b, [[252, 127, 192,   0],
  116                                [  7, 252,  15, 128],
  117                                [240,   0,  28,   0],
  118                                [255, 128,   0, 128],
  119                                [192,  31, 255, 128],
  120                                [142,  63,   0,   0],
  121                                [255, 240,   7,   0],
  122                                [  7, 224,  14,   0],
  123                                [126,   0, 224,   0],
  124                                [255, 255, 199,   0],
  125                                [ 56,  28, 126,   0],
  126                                [113, 248, 227, 128],
  127                                [227, 142,  63,   0],
  128                                [  0,  28, 112,   0],
  129                                [ 15, 248,   3, 128],
  130                                [ 28, 126,  56,   0],
  131                                [ 56, 255, 241, 128],
  132                                [240,   7, 224,   0],
  133                                [227, 129, 192, 128],
  134                                [255, 255, 254,   0],
  135                                [126,   0, 224,   0],
  136                                [  3, 241, 248,   0],
  137                                [  0, 255, 241, 128],
  138                                [128,   0, 255, 128],
  139                                [224,   1, 255, 128],
  140                                [248, 252, 126,   0],
  141                                [  0,   7,   3, 128],
  142                                [224, 113, 248,   0],
  143                                [  0, 252, 127, 128],
  144                                [142,  63, 224,   0],
  145                                [224,  14,  63,   0],
  146                                [  7,   3, 128,   0],
  147                                [113, 255, 255, 128],
  148                                [ 28, 113, 199,   0],
  149                                [  7, 227, 142,   0],
  150                                [ 14,  56, 252,   0]])
  151 
  152         arr = arr.T.copy()
  153         b = np.packbits(arr, axis=0)
  154         assert_equal(b.dtype, np.uint8)
  155         assert_array_equal(b, [[252, 7, 240, 255, 192, 142, 255, 7, 126, 255,
  156                                 56, 113, 227, 0, 15, 28, 56, 240, 227, 255,
  157                                 126, 3, 0, 128, 224, 248, 0, 224, 0, 142, 224,
  158                                 7, 113, 28, 7, 14],
  159                                 [127, 252, 0, 128, 31, 63, 240, 224, 0, 255,
  160                                  28, 248, 142, 28, 248, 126, 255, 7, 129, 255,
  161                                  0, 241, 255, 0, 1, 252, 7, 113, 252, 63, 14,
  162                                  3, 255, 113, 227, 56],
  163                                 [192, 15, 28, 0, 255, 0, 7, 14, 224, 199, 126,
  164                                  227, 63, 112, 3, 56, 241, 224, 192, 254, 224,
  165                                  248, 241, 255, 255, 126, 3, 248, 127, 224, 63,
  166                                  128, 255, 199, 142, 252],
  167                                 [0, 128, 0, 128, 128, 0, 0, 0, 0, 0, 0, 128, 0,
  168                                  0, 128, 0, 128, 0, 128, 0, 0, 0, 128, 128,
  169                                  128, 0, 128, 0, 128, 0, 0, 0, 128, 0, 0, 0]])
  170 
  171         b = np.packbits(arr, axis=1)
  172         assert_equal(b.dtype, np.uint8)
  173         assert_array_equal(b, [[190,  72, 113, 214,   0],
  174                                [186, 216, 120, 210, 128],
  175                                [178, 248, 248, 210, 128],
  176                                [178, 241, 216,  64, 192],
  177                                [150, 227, 152,  68,  80],
  178                                [215, 195,  24,   5, 112],
  179                                [ 87, 202,  60,   5,  48],
  180                                [ 83,  90,  52,   1, 160],
  181                                [ 83,  90, 182,  72, 160],
  182                                [195,  83, 150,  88, 224],
  183                                [199,  83, 150,  92, 240],
  184                                [206, 119, 150,  92, 208],
  185                                [204, 127, 146,  78, 144],
  186                                [204, 109, 210, 110, 128],
  187                                [140,  73, 210,  39, 160],
  188                                [140,  64, 246, 181, 224],
  189                                [136, 208, 255, 149, 240],
  190                                [136, 244, 255, 220, 208],
  191                                [  8, 189, 223, 222, 144],
  192                                [ 40,  45, 151, 218, 144],
  193                                [105,  41,  21, 218, 176],
  194                                [107, 104,  17, 202, 240],
  195                                [ 75, 122,  17, 234, 224],
  196                                [ 74,  90, 131, 170, 192],
  197                                [ 88,  18, 163, 168, 128]])
  198 
  199 
  200     # result is the same if input is multiplied with a nonzero value
  201     for dtype in 'bBhHiIlLqQ':
  202         arr = np.array(a, dtype=dtype)
  203         rnd = np.random.randint(low=np.iinfo(dtype).min,
  204                                 high=np.iinfo(dtype).max, size=arr.size,
  205                                 dtype=dtype)
  206         rnd[rnd == 0] = 1
  207         arr *= rnd.astype(dtype)
  208         b = np.packbits(arr, axis=-1)
  209         assert_array_equal(np.unpackbits(b)[:-4], a)
  210 
  211     assert_raises(TypeError, np.packbits, np.array(a, dtype=float))
  212 
  213 
  214 def test_packbits_very_large():
  215     # test some with a larger arrays gh-8637
  216     # code is covered earlier but larger array makes crash on bug more likely
  217     for s in range(950, 1050):
  218         for dt in '?bBhHiIlLqQ':
  219             x = np.ones((200, s), dtype=bool)
  220             np.packbits(x, axis=1)
  221 
  222 
  223 def test_unpackbits():
  224     # Copied from the docstring.
  225     a = np.array([[2], [7], [23]], dtype=np.uint8)
  226     b = np.unpackbits(a, axis=1)
  227     assert_equal(b.dtype, np.uint8)
  228     assert_array_equal(b, np.array([[0, 0, 0, 0, 0, 0, 1, 0],
  229                                     [0, 0, 0, 0, 0, 1, 1, 1],
  230                                     [0, 0, 0, 1, 0, 1, 1, 1]]))
  231 
  232 
  233 def test_unpackbits_empty():
  234     a = np.empty((0,), dtype=np.uint8)
  235     b = np.unpackbits(a)
  236     assert_equal(b.dtype, np.uint8)
  237     assert_array_equal(b, np.empty((0,)))
  238 
  239 
  240 def test_unpackbits_empty_with_axis():
  241     # Lists of packed shapes for different axes and unpacked shapes.
  242     shapes = [
  243         ([(0,)], (0,)),
  244         ([(2, 24, 0), (16, 3, 0), (16, 24, 0)], (16, 24, 0)),
  245         ([(2, 0, 24), (16, 0, 24), (16, 0, 3)], (16, 0, 24)),
  246         ([(0, 16, 24), (0, 2, 24), (0, 16, 3)], (0, 16, 24)),
  247         ([(3, 0, 0), (24, 0, 0), (24, 0, 0)], (24, 0, 0)),
  248         ([(0, 24, 0), (0, 3, 0), (0, 24, 0)], (0, 24, 0)),
  249         ([(0, 0, 24), (0, 0, 24), (0, 0, 3)], (0, 0, 24)),
  250         ([(0, 0, 0), (0, 0, 0), (0, 0, 0)], (0, 0, 0)),
  251     ]
  252     for in_shapes, out_shape in shapes:
  253         for ax, in_shape in enumerate(in_shapes):
  254             a = np.empty(in_shape, dtype=np.uint8)
  255             b = np.unpackbits(a, axis=ax)
  256             assert_equal(b.dtype, np.uint8)
  257             assert_equal(b.shape, out_shape)
  258 
  259 
  260 def test_unpackbits_large():
  261     # test all possible numbers via comparison to already tested packbits
  262     d = np.arange(277, dtype=np.uint8)
  263     assert_array_equal(np.packbits(np.unpackbits(d)), d)
  264     assert_array_equal(np.packbits(np.unpackbits(d[::2])), d[::2])
  265     d = np.tile(d, (3, 1))
  266     assert_array_equal(np.packbits(np.unpackbits(d, axis=1), axis=1), d)
  267     d = d.T.copy()
  268     assert_array_equal(np.packbits(np.unpackbits(d, axis=0), axis=0), d)