def packbits(myarray):
 if (myarray.dtype.kind not in 'biu'):
  raise TypeError('Expected an input array of integer or boolean data type')
 myarray = myarray.ravel()
 packed_size = ((myarray.size + 7) // 8)
 packed = cupy.zeros((packed_size,), dtype=cupy.uint8)
 cupy.ElementwiseKernel('raw T myarray, raw int32 myarray_size', 'uint8 packed', 'for (int j = 0; j < 8; ++j) {\n            int k = i * 8 + j;\n            int bit = k < myarray_size && myarray[k] != 0;\n            packed |= bit << (7 - j);\n        }', 'packbits_kernel')(myarray, myarray.size, packed)
 return packed

def packbits ( myarray ) : if ( myarray . dtype . kind not in 'biu' ) : raise TypeError ( 'Expected an input array of integer or boolean data type' ) myarray = myarray . ravel ( ) packed_size = ( ( myarray . size + 7 ) // 8 ) packed = cupy . zeros ( ( packed_size , ) , dtype = cupy . uint8 ) cupy . ElementwiseKernel ( 'raw T myarray, raw int32 myarray_size' , 'uint8 packed' , 'for (int j = 0; j < 8; ++j) {\n            int k = i * 8 + j;\n            int bit = k < myarray_size && myarray[k] != 0;\n            packed |= bit << (7 - j);\n        }' , 'packbits_kernel' ) ( myarray , myarray . size , packed ) return packed
packs the elements of a binary-valued array into bits in a uint8 array .