def assert_warns_message(expected_warning, expected_message, *args, **kwargs): """Assert that the correct warning message is raised. Handles regex better than the default. """ expected_regex = re.escape(expected_message) return assert_warns_regex(expected_warning, expected_regex, *args, **kwargs)
def test_duplicate_data_many(): with assert_warns_regex( RuntimeWarning, "Detected zero distance between ([0-9]*) pairs of samples. Consider removing duplicates to avoid errors in downstream processing.", ): build_graph(np.vstack([data, data[:21]]), n_pca=20, decay=10, thresh=1e-4)
def assert_warns(exception=Warning, *args, glob=None, regex=None, match_case=None, **kwargs): if glob is None and regex is None: return tools.assert_warns(exception, *args, **kwargs) pattern = get_pattern(glob, regex, match_case) return tools.assert_warns_regex(exception, pattern, *args, **kwargs)
def test_hrg(): ID, info = random.choice(list(species_dict.items())) m = info['mass'] g = info['degen'] sign = -1 if info['boson'] else 1 prefactor = g / (2 * np.pi**2 * hbarc**3) if info['has_anti']: prefactor *= 2 T = random.uniform(.13, .15) hrg = HRG(T, species=[ID], res_width=False) assert_almost_equal(hrg.T, T, delta=1e-15, msg='incorrect temperature') n = np.arange(1, 50) density = prefactor * m * m * T * ( (-sign)**(n - 1) / n * special.kn(2, n * m / T)).sum() assert_almost_equal(hrg.density(), density, delta=1e-12, msg='incorrect density') def integrand(p): E = np.sqrt(m * m + p * p) return p * p * E / (np.exp(E / T) + sign) energy_density = prefactor * integrate.quad(integrand, 0, 10)[0] assert_almost_equal(hrg.energy_density(), energy_density, delta=1e-12, msg='incorrect energy density') def integrand(p): E = np.sqrt(m * m + p * p) return p**4 / (3 * E) / (np.exp(E / T) + sign) pressure = prefactor * integrate.quad(integrand, 0, 10)[0] assert_almost_equal(hrg.pressure(), pressure, delta=1e-12, msg='incorrect pressure') with assert_warns_regex(Warning, 'high particlization temperature'): HRG(.193)
def assert_warns_message(expected_warning, expected_message, *args, **kwargs): expected_regex = re.escape(expected_message) return assert_warns_regex(expected_warning, expected_regex, *args, **kwargs)
def test_surface(): volume = np.random.uniform(10, 100) tau = np.random.uniform(.5, 5.) x = np.atleast_2d([tau, 0., 0.]) sigma = np.atleast_2d([volume/tau, 0., 0.]) v = np.zeros((1, 2)) surf = Surface(x, sigma, v) assert surf.boost_invariant, 'Surface should be boost-invariant.' assert_almost_equal( surf.volume, volume, delta=1e-12, msg='incorrect volume' ) ymax = np.random.uniform(.5, 2.) surf = Surface(x, sigma, v, ymax=ymax) assert_almost_equal( surf.volume, 2*ymax*volume, delta=1e-12, msg='incorrect volume' ) x = np.random.uniform(0, 10, size=(1, 4)) # ensure sigma is timelike (sigma^2 > 0) so that the volume is positive sigma = np.random.uniform([3, -1, -1, -1], [4, 1, 1, 1], size=(1, 4)) v = np.random.uniform(-.5, .5, size=(1, 3)) surf = Surface(x, sigma, v) assert not surf.boost_invariant, 'Surface should not be boost-invariant.' u = np.insert(v, 0, 1) / np.sqrt(1 - (v*v).sum()) volume = np.inner(sigma, u) assert_almost_equal( surf.volume, volume, delta=1e-12, msg='incorrect volume' ) with assert_warns_regex(Warning, 'ymax has no effect for 3D surfaces'): Surface(x, sigma, v, ymax=1.) with assert_warns_regex(Warning, 'total freeze-out volume is negative'): Surface(x, np.concatenate([[[0]], -v], axis=1), v) with assert_raises_regex(ValueError, 'invalid shape'): Surface( np.ones((1, 1)), np.ones((1, 1)), np.ones((1, 1)), ) with assert_raises_regex(ValueError, 'invalid shape'): Surface( np.ones((1, 4)), np.ones((1, 3)), np.ones((1, 2)), ) with assert_raises_regex(ValueError, 'invalid shape'): Surface( np.ones((2, 4)), np.ones((2, 4)), np.ones((3, 3)), )
def test_surface(): volume = np.random.uniform(10, 100) tau = np.random.uniform(.5, 5.) x = [tau, 0, 0] sigma = [volume/tau, 0, 0] v = [0, 0] surf = Surface(x, sigma, v) assert surf.boost_invariant, 'Surface should be boost-invariant.' assert_almost_equal( surf.volume, volume, delta=1e-12, msg='incorrect volume' ) ymax = np.random.uniform(.5, 2.) surf = Surface(x, sigma, v, ymax=ymax) assert_almost_equal( surf.volume, 2*ymax*volume, delta=1e-12, msg='incorrect volume' ) x = np.random.uniform(0, 10, size=(1, 4)) # ensure sigma is timelike (sigma^2 > 0) so that the volume is positive sigma = np.random.uniform([3, -1, -1, -1], [4, 1, 1, 1], size=(1, 4)) v = np.random.uniform(-.5, .5, size=(1, 3)) surf = Surface(x, sigma, v) assert not surf.boost_invariant, 'Surface should not be boost-invariant.' u = np.insert(v, 0, 1) / np.sqrt(1 - (v*v).sum()) volume = np.inner(sigma, u) assert_almost_equal( surf.volume, volume, delta=1e-12, msg='incorrect volume' ) with assert_warns_regex(Warning, 'ymax has no effect for a 3D surface'): Surface(x, sigma, v, ymax=np.random.rand()) with assert_warns_regex(Warning, 'total freeze-out volume is negative'): Surface(x, np.concatenate([[[0]], -v], axis=1), v) with assert_raises_regex( ValueError, 'number of spacetime dimensions of x, sigma, and/or v' ): Surface([1, 0], [1, 0], 0) with assert_raises_regex( ValueError, 'number of spacetime dimensions of x, sigma, and/or v' ): Surface( np.ones((1, 4)), np.ones((1, 3)), np.ones((1, 2)), ) with assert_raises_regex( ValueError, 'number of elements of x, sigma, and/or v do not match' ): Surface( np.ones((2, 4)), np.ones((2, 4)), np.ones((3, 3)), ) with assert_raises_regex( ValueError, 'number of elements of pi components do not match' ): Surface( np.ones((3, 4)), np.ones((3, 4)), np.ones((3, 3)), pi=dict( xx=np.ones(3), yy=np.ones(3), xy=np.ones(3), xz=np.ones(3), yz=np.ones(4), ) ) with assert_raises_regex( ValueError, 'number of elements of Pi do not match' ): Surface( np.ones((3, 4)), np.ones((3, 4)), np.ones((3, 3)), Pi=np.ones(4) )
def test_duplicate_data(): with assert_warns_regex( RuntimeWarning, r"Detected zero distance between samples ([0-9and,\s]*). Consider removing duplicates to avoid errors in downstream processing.", ): build_graph(np.vstack([data, data[:10]]), n_pca=20, decay=10, thresh=0)