def hash_from_ndarray(data): """Return a hash from an ndarray It takes care of the data, shapes, strides and dtype. """ # We need to hash the shapes and strides as hash_from_code only hashes # the data buffer. Otherwise, this will cause problem with shapes like: # (1, 0) and (2, 0) and problem with inplace transpose. # We also need to add the dtype to make the distinction between # uint32 and int32 of zeros with the same shape and strides. # python hash are not strong, so I always use md5 in order not to have a # too long hash, I call it again on the concatenation of all parts. if not data.flags["C_CONTIGUOUS"]: # Version 1.7.1 and previous of NumPy allowed calling # hash_from_code on an F-contiguous array, but more recent # versions need a C-contiguous one. data = numpy.ascontiguousarray(data) return hash_from_code( hash_from_code(data) + hash_from_code(str(data.shape)) + hash_from_code(str(data.strides)) + hash_from_code(str(data.dtype)) )
def hash_from_ndarray(data): """ Return a hash from an ndarray. It takes care of the data, shapes, strides and dtype. """ # We need to hash the shapes and strides as hash_from_code only hashes # the data buffer. Otherwise, this will cause problem with shapes like: # (1, 0) and (2, 0) and problem with inplace transpose. # We also need to add the dtype to make the distinction between # uint32 and int32 of zeros with the same shape and strides. # python hash are not strong, so I always use md5 in order not to have a # too long hash, I call it again on the concatenation of all parts. if not data.flags["C_CONTIGUOUS"]: # hash_from_code needs a C-contiguous array. data = np.ascontiguousarray(data) return hash_from_code( hash_from_code(data) + hash_from_code(str(data.shape)) + hash_from_code(str(data.strides)) + hash_from_code(str(data.dtype)))
def hash_from_ndarray(data): """ Return a hash from an ndarray. It takes care of the data, shapes, strides and dtype. """ # We need to hash the shapes and strides as hash_from_code only hashes # the data buffer. Otherwise, this will cause problem with shapes like: # (1, 0) and (2, 0) and problem with inplace transpose. # We also need to add the dtype to make the distinction between # uint32 and int32 of zeros with the same shape and strides. # python hash are not strong, so use sha256 (md5 is not # FIPS compatible). To not have too long of hash, I call it again on # the concatenation of all parts. if not data.flags["C_CONTIGUOUS"]: # hash_from_code needs a C-contiguous array. data = np.ascontiguousarray(data) return hash_from_code(hash_from_code(data) + hash_from_code(str(data.shape)) + hash_from_code(str(data.strides)) + hash_from_code(str(data.dtype)))
def hash_from_sparse(data): # We need to hash the shapes as hash_from_code only hashes # the data buffer. Otherwise, this will cause problem with shapes like: # (1, 0) and (2, 0) # We also need to add the dtype to make the distinction between # uint32 and int32 of zeros with the same shape. # Python hash is not strong, so use sha256 instead. To avoid having a too # long hash, I call it again on the contatenation of all parts. return hash_from_code( hash_from_code(data.data) + hash_from_code(data.indices) + hash_from_code(data.indptr) + hash_from_code(str(data.shape)) + hash_from_code(str(data.dtype)) + hash_from_code(data.format))
def hash_from_sparse(data): # We need to hash the shapes as hash_from_code only hashes # the data buffer. Otherwise, this will cause problem with shapes like: # (1, 0) and (2, 0) # We also need to add the dtype to make the distinction between # uint32 and int32 of zeros with the same shape. # Python hash is not strong, so I always use md5. To avoid having a too # long hash, I call it again on the contatenation of all parts. return hash_from_code(hash_from_code(data.data) + hash_from_code(data.indices) + hash_from_code(data.indptr) + hash_from_code(str(data.shape)) + hash_from_code(str(data.dtype)) + hash_from_code(data.format))