def array2bytes(arr, bytes_type=bytes):
    """Wraps NumPy's save function to return bytes.

    We use :func:`numpy.save` rather than :meth:`numpy.ndarray.tobytes` because
    it encodes endianness and order.

    Args:
        arr (:obj:`numpy.ndarray`):
            Array to be saved.

        bytes_type (class, optional, default=bytes):
            This class will be used to wrap the bytes objects in the
            serialization if `use_bytes` is true. Useful for when using
            Python 2 and using BSON encoding, which will not accept the raw
            `bytes` type, so `bson.Binary` can be used instead.


    Returns:
        bytes_type

    """
    bio = io.BytesIO()
    np.save(bio, arr, allow_pickle=False)
    return bytes_type(bio.getvalue())