def test_cyclic_vector_dist(d, k, rng): n = 1000 v = cyclic_vector(d, k, n=n, rng=rng) v = np.round(v, decimals=8) v[np.abs(v) < 1e-8] = 0 hashmap = {} counts = {} for i in range(n): vi = v[i] hi = array_hash(vi) hashmap.setdefault(hi, vi) if hi not in counts: counts[hi] = 1 else: counts[hi] += 1 # check that all generated values are cyclic for b in hashmap.values(): a = rng.normal(scale=1. / np.sqrt(d), size=d) c = circconv(a, b, k=k) assert np.allclose(a, c, atol=1e-7), (a, c) # check that the std. dev. of the counts is roughly a Binomial dist. values = np.array(list(counts.values())) p = 1. / len(values) std = np.sqrt(n * p * (1 - p)) assert np.allclose(values.std(), std, rtol=0.3, atol=0)
def hashvalue(self, instance): d = self.__get__(instance, None) if d is None: return hash(d) return hash( tuple((k, array_hash(v) if is_array_like(v) else hash(v)) for k, v in d.items()))
def test_array_hash_sparse(nnz, rng): scipy_sparse = pytest.importorskip("scipy.sparse") if nnz == 7: shape = (5, 5) rows_a = [0, 0, 1, 2, 3, 3, 4] rows_b = [0, 1, 1, 2, 3, 3, 4] cols_a = [0, 2, 3, 4, 2, 4, 0] cols_b = [1, 2, 3, 4, 2, 4, 0] data_a = [1.0, 2.0, 1.5, 2.3, 1.2, 2.5, 1.8] data_b = [1.0, 1.0, 1.5, 2.3, 1.2, 2.5, 1.8] else: shape = (100, 100) inds_a = rng.permutation(np.prod(shape))[:nnz] inds_b = rng.permutation(np.prod(shape))[:nnz] rows_a, cols_a = np.unravel_index(inds_a, shape) rows_b, cols_b = np.unravel_index(inds_b, shape) data_a = rng.uniform(-1, 1, size=nnz) data_b = rng.uniform(-1, 1, size=nnz) matrices = [[] for _ in range(6)] for (rows, cols), data in itertools.product( ((rows_a, cols_a), (rows_b, cols_b)), (data_a, data_b)): csr = scipy_sparse.csr_matrix((data, (rows, cols)), shape=shape) matrices[0].append(csr) matrices[1].append(csr.tocsc()) matrices[2].append(csr.tocoo()) matrices[3].append(csr.tobsr()) matrices[4].append(csr.todok()) matrices[5].append(csr.tolil()) # matrices[6].append(csr.todia()) # warns about inefficiency # ensure hash is reproducible for matrix in (m for kind in matrices for m in kind): assert array_hash(matrix) == array_hash(matrix) # ensure hash is different for different matrices for kind in matrices: hashes = [array_hash(matrix) for matrix in kind] assert len(np.unique(hashes)) == len(kind), ( "Different matrices should have different hashes: %s" % hashes)
def hashvalue(self, instance): return array_hash(self.__get__(instance, None))
def hashvalue(self, instance): return hash( frozenset( tuple((k, array_hash(v)) for k, v in self.data[instance].items())))