def test_smooth_grid__development_target(): """This is how we hope to work with SmoothGrid objects.""" grid = SmoothGrid([0, 5, 10, 20], [2000, 2010]) # Assign a default value to all points. grid.value[:, :] = Gaussian(mean=0.01, standard_deviation=5.0, lower=0.0, upper=10.0) assert grid.value[5, 2010].density == "gaussian" assert grid.value[5, 2010].mean == 0.01 grid.value[10, 2000:] = Gaussian(mean=0.1, standard_deviation=5.0) # Assign a prior to points by region of age or time. grid.value[2:15, 2005:] = Gaussian(mean=0.1, standard_deviation=3.0, lower=1e-4, upper=12) assert grid.value[5, 2010].mean == 0.1 for a, t in grid.age_time(): grid.value[a, t] = Gaussian(mean=0.01, standard_deviation=0.001) for a, t, da, dt in grid.age_time_diff(): grid.dage[a, t] = Gaussian(mean=0, standard_deviation=0.01 * da) for a, t in grid.age_time(): grid.value[a, t] = grid.value[a, t].assign(standard_deviation=0.2) assert grid.value[20, 2000].standard_deviation == 0.2
def test_out_of_bounds_setitem(): grid = SmoothGrid([0, 5, 10, 20], [2000, 2010]) with pytest.raises(ValueError): grid.value[-0.5, 2000] = Gaussian(mean=0.1, standard_deviation=5.0) with pytest.raises(ValueError): grid.value[-0.5, 2015:] = Gaussian(mean=0.1, standard_deviation=5.0) with pytest.raises(ValueError): grid.value[:, 2015:] = Gaussian(mean=0.1, standard_deviation=5.0)
def test_happy_construction(): Uniform(-1, 1, 0, name="test") Uniform(-1, 1, 0, 0.5, name="test") Gaussian(0, 1, -10, 10, name="test2") Gaussian(0, 1, -10, 10, 0.5, name="test2") Laplace(0, 1, -10, 10, name="test3") Laplace(0, 1, -10, 10, 0.5, name="test3") StudentsT(0, 1, 2.5, -10, 10, name="test4") LogGaussian(0, 1, 0.5, -10, 10, name="test5") LogLaplace(0, 1, 0.5, -10, 10, name="test6") LogStudentsT(0, 1, 2.5, 0.5, -10, 10, name="test7")
def test_prior_hashing(): s = { Gaussian(0, 1), Uniform(0, 1), Gaussian(0, 1), Uniform(0, 2), Uniform(0, 1) } assert len(s) == 3 assert Gaussian(0, 1) in s assert Uniform(0, 10) not in s
def test_prior_nonequality(): a = Gaussian(0, 1) b = Gaussian(1, 1) assert a != b a = Uniform(0, 1) b = Uniform(-1, 0) assert a != b a = Gaussian(0, 1, name="test_prior") b = Gaussian(0, 1, name="other_test_prior") assert a != b a = Gaussian(0, 1) b = Uniform(0, 1) assert a != b
def test_prior_equality(): a = Gaussian(0, 1) b = Gaussian(0, 1) assert a == b a = Gaussian(0, 1, -1, 1) b = Gaussian(0, 1, -1, 1) assert a == b a = Uniform(0, 10) b = Uniform(0, 10) assert a == b a = Uniform(0, 10, name="test_prior") b = Uniform(0, 10, name="test_prior") assert a == b
def test_prior_sort(): priors = [ Uniform(lower=1e-10, upper=1, mean=5e-5, name="iota"), Gaussian(0, 1, name="other_test_prior"), Uniform(0, 1), ] # NOTE: This is a weak test of actual sorting behavior however all I # actually care about is that the sort is stable, I don't really care # what the order is assert sorted(priors) == sorted(reversed(priors))
def get_mulcov_priors(model_version_id: int): convert_type = { 'rate_value': 'alpha', 'meas_value': 'beta', 'meas_std': 'gamma' } mulcov_prior = {} ctx = Context(model_version_id=model_version_id) path = os.path.join(ctx.outputs_dir, 'mulcov_stats.csv') mulcov_stats_df = pd.read_csv(path) for _, row in mulcov_stats_df.iterrows(): if row['rate_name'] is not None: mulcov_prior[(convert_type[row['mulcov_type']], row['c_covariate_name'], row['rate_name'])] = Gaussian( mean=row['mean'], standard_deviation=row['std']) if row['integrand_name'] is not None: mulcov_prior[(convert_type[row['mulcov_type']], row['c_covariate_name'], row['integrand_name'])] = Gaussian( mean=row['mean'], standard_deviation=row['std']) return mulcov_prior
def get_mulcov_priors( model_version_id: int) -> Dict[Tuple[str, str, str], _Prior]: """ Read in covariate multiplier statistics from a specific model version ID and returns a dictionary with a prior object for that covariate multiplier type, covariate name, and rate or integrand. Parameters ---------- model_version_id The model version ID to pull covariate multiplier statistics from """ convert_type = { 'rate_value': 'alpha', 'meas_value': 'beta', 'meas_noise': 'gamma' } mulcov_prior = {} ctx = Context(model_version_id=model_version_id) path = os.path.join(ctx.outputs_dir, 'mulcov_stats.csv') if not os.path.exists(path): return {} mulcov_stats_df = pd.read_csv(path) if mulcov_stats_df.empty: return {} for _, row in mulcov_stats_df.iterrows(): if row['rate_name'] != 'none': mulcov_prior[(convert_type[row['mulcov_type']], row['c_covariate_name'], row['rate_name'])] = Gaussian( mean=row['mean'], standard_deviation=row['std']) if row['integrand_name'] != 'none': mulcov_prior[(convert_type[row['mulcov_type']], row['c_covariate_name'], row['integrand_name'])] = Gaussian( mean=row['mean'], standard_deviation=row['std']) return mulcov_prior
def test_smooth_grid__edge_cases_as_written(): """If we set a particular age and time, that exact one is set.""" grid = SmoothGrid([0, 5, 10, 20], [2000, 2010]) for a, t in grid.age_time(): low = a * 0.01 high = t - 1999 grid.value[a, t] = Gaussian(mean=(low + high) / 2, standard_deviation=5.0, lower=low, upper=high) assert isclose(grid.value[0, 2000].lower, 0) assert isclose(grid.value[0, 2000].upper, 1) assert isclose(grid.value[5, 2010].lower, 0.05) assert isclose(grid.value[5, 2010].upper, 11)
def test_smooth_grid_mulstd(): grid = SmoothGrid([0, 5, 10, 20], [2000, 2010]) grid.value.mulstd_prior = Gaussian(mean=0.1, standard_deviation=0.02) assert grid.value.mulstd_prior.standard_deviation == 0.02 assert isinstance(grid.value.mulstd_prior, Gaussian)
def test_prior_hashing__near_miss(): assert hash(Gaussian(0, 1.0000000000000001)) == hash(Gaussian(0, 1)) assert hash(Gaussian(0, 1.000000000000001)) != hash(Gaussian(0, 1))
def test_validate_standard_deviation(): with pytest.raises(PriorError) as excinfo: Gaussian(0, -1) assert "must be positive" in str(excinfo.value)