def test_hamming_core(self): Q = (np.random.random((100, 4)) * 255).astype(np.uint8) D = (np.random.random((1000, 4)) * 255).astype(np.uint8) D1 = cext.hamming(Q, D) D2 = sh.hammingDist(Q, D) D3 = sh.hammingDist2(Q, D) self.assertEqual(self.ndarray_diff(D1, D2), 0) self.assertEqual(self.ndarray_diff(D1, D3), 0)
def hammingDist2(B1, B2): """ Compute hamming distance between two sets of samples (B1, B2) Dh=hammingDist(B1, B2); Input B1, B2: compact bit vectors. Each datapoint is one row. size(B1) = [ndatapoints1, nwords] size(B2) = [ndatapoints2, nwords] It is faster if ndatapoints1 < ndatapoints2 Output Dh = hamming distance. size(Dh) = [ndatapoints1, ndatapoints2] example query Dhamm = hammingDist(B2, B1); this will give the same result than: Dhamm = distMat(U2>0, U1>0).^2; the size of the distance matrix is: size(Dhamm) = [Ntest x Ntraining] """ if B1.ndim == 1: B1 = B1.reshape((1, -1)) if B2.ndim == 1: B2 = B2.reshape((1, -1)) npt1, dim1 = B1.shape npt2, dim2 = B2.shape if dim1 != dim2: raise Exception("Dimension not consists: %d, %d" % (dim1, dim2)) Dh = cext.hamming(B1, B2) return Dh