def test_hypersphere(dimensions, rng): n = 150 * dimensions if dimensions < 1: with pytest.raises(ValueError): dist = dists.UniformHypersphere().sample(1, dimensions) else: dist = dists.UniformHypersphere() samples = dist.sample(n, dimensions, rng=rng) assert samples.shape == (n, dimensions) assert np.allclose(np.mean(samples, axis=0), 0, atol=0.1) hist, _ = np.histogramdd(samples, bins=5) assert np.allclose(hist - np.mean(hist), 0, atol=0.1 * n)
def test_hypersphere_surface(dimensions, rng): n = 150 * dimensions dist = dists.UniformHypersphere(surface=True) samples = dist.sample(n, dimensions, rng=rng) assert samples.shape == (n, dimensions) assert np.allclose(npext.norm(samples, axis=1), 1) assert np.allclose(np.mean(samples, axis=0), 0, atol=0.25 / dimensions)
def test_hypersphere_volume(min_magnitude, d, rng): n = 150 * d dist = dists.UniformHypersphere(min_magnitude=min_magnitude) samples = dist.sample(n, d, rng=rng) assert samples.shape == (n, d) assert np.allclose(np.mean(samples, axis=0), 0, atol=0.1) assert np.all(npext.norm(samples, axis=1) >= min_magnitude)
def test_cosine_intercept(d, p, rng): """Tests CosineSimilarity inverse cdf for finding intercepts.""" pytest.importorskip('scipy') # betaincinv num_samples = 250 exp_dist = dists.UniformHypersphere(surface=True) act_dist = dists.CosineSimilarity(d) dots = exp_dist.sample(num_samples, d, rng=rng)[:, 0] # Find the desired intercept so that dots >= c with probability p c = act_dist.ppf(1 - p) assert np.allclose(np.sum(dots >= c) / float(num_samples), p, atol=0.05)
def test_distorarrayparam(): """DistOrArrayParams can be distributions or samples.""" class Test(object): dp = dists.DistOrArrayParam(default=None, sample_shape=['*', '*']) inst = Test() inst.dp = dists.UniformHypersphere() assert isinstance(inst.dp, dists.UniformHypersphere) inst.dp = np.array([[1], [2], [3]]) assert np.all(inst.dp == np.array([[1], [2], [3]])) with pytest.raises(ValueError): inst.dp = 'a' # Sample must have correct dims with pytest.raises(ValueError): inst.dp = np.array([1])
def test_distorarrayparam_sample_shape(): """sample_shape dictates the shape of the sample that can be set.""" class Test(object): dp = dists.DistOrArrayParam(default=None, sample_shape=['d1', 10]) d1 = 4 inst = Test() # Distributions are still cool inst.dp = dists.UniformHypersphere() assert isinstance(inst.dp, dists.UniformHypersphere) # Must be shape (4, 10) inst.dp = np.ones((4, 10)) assert np.all(inst.dp == np.ones((4, 10))) with pytest.raises(ValueError): inst.dp = np.ones((10, 4)) assert np.all(inst.dp == np.ones((4, 10)))
def test_cosine_similarity(d, rng): """Tests CosineSimilarity sampling.""" num_samples = 2500 num_bins = 5 # Check that it gives a single dimension from UniformHypersphere exp_dist = dists.UniformHypersphere(surface=True) act_dist = dists.CosineSimilarity(d) exp = exp_dist.sample(num_samples, d, rng=rng)[:, 0] act = act_dist.sample(num_samples, rng=rng) exp_hist, _ = np.histogram(exp, bins=num_bins) act_hist, _ = np.histogram(act, bins=num_bins) assert np.all(np.abs(np.asfarray(exp_hist - act_hist) / num_samples) < 0.1)
def test_hypersphere_warns(rng): with pytest.warns(UserWarning): dists.UniformHypersphere(surface=True, min_magnitude=0.1)
def test_hypersphere_dimension_fail(rng): with pytest.raises(ValueError): dists.UniformHypersphere(0).sample(1, 0)