示例#1
0
def test_variables_samples(achr: "ACHRSampler", optgp: OptGPSampler) -> None:
    """Test variable samples."""
    vnames = np.array([v.name for v in achr.model.variables])
    s = optgp.sample(10, fluxes=False)
    assert s.shape == (10, optgp.warmup.shape[1])
    assert (s.columns == vnames).all()
    assert (optgp.validate(s) == "v").all()
示例#2
0
def optgp(model: "Model") -> OptGPSampler:
    """Return OptGPSampler instance for tests."""
    sampler = OptGPSampler(model, processes=1, thinning=1)
    assert (sampler.n_warmup > 0) and (sampler.n_warmup <=
                                       2 * len(model.variables))
    assert all(sampler.validate(sampler.warmup) == "v")

    return sampler
示例#3
0
def test_reproject(optgp: OptGPSampler) -> None:
    """Test reprojection of sampling."""
    s = optgp.sample(10, fluxes=False).values
    proj = np.apply_along_axis(optgp._reproject, 1, s)
    assert all(optgp.validate(proj) == "v")

    s = np.random.rand(10, optgp.warmup.shape[1])
    proj = np.apply_along_axis(optgp._reproject, 1, s)
    assert all(optgp.validate(proj) == "v")
示例#4
0
def optgp(model):
    """Return OptGPSampler instance for tests."""

    sampler = OptGPSampler(model, processes=1, thinning=1)
    assert ((sampler.n_warmup > 0) and
            (sampler.n_warmup <= 2 * len(model.variables)))
    assert all(sampler.validate(sampler.warmup) == "v")

    return sampler
示例#5
0
def test_complicated_model():
    """Test a complicated model.

    Difficult model since the online mean calculation is numerically
    unstable so many samples weakly violate the equality constraints.

    """

    model = Model('flux_split')

    reaction1 = Reaction('V1')
    reaction2 = Reaction('V2')
    reaction3 = Reaction('V3')
    reaction1.bounds = (0, 6)
    reaction2.bounds = (0, 8)
    reaction3.bounds = (0, 10)

    A = Metabolite('A')

    reaction1.add_metabolites({A: -1})
    reaction2.add_metabolites({A: -1})
    reaction3.add_metabolites({A: 1})

    model.add_reactions([reaction1, reaction2, reaction3])

    optgp = OptGPSampler(model, 1, seed=42)
    achr = ACHRSampler(model, seed=42)

    optgp_samples = optgp.sample(100)
    achr_samples = achr.sample(100)

    assert any(optgp_samples.corr().abs() < 1.0)
    assert any(achr_samples.corr().abs() < 1.0)

    # > 95% are valid
    assert sum(optgp.validate(optgp_samples) == "v") > 95
    assert sum(achr.validate(achr_samples) == "v") > 95
示例#6
0
def test_complicated_model():
    """Test a complicated model.

    Difficult model since the online mean calculation is numerically
    unstable so many samples weakly violate the equality constraints.

    """

    model = Model('flux_split')

    reaction1 = Reaction('V1')
    reaction2 = Reaction('V2')
    reaction3 = Reaction('V3')
    reaction1.bounds = (0, 6)
    reaction2.bounds = (0, 8)
    reaction3.bounds = (0, 10)

    A = Metabolite('A')

    reaction1.add_metabolites({A: -1})
    reaction2.add_metabolites({A: -1})
    reaction3.add_metabolites({A: 1})

    model.add_reactions([reaction1, reaction2, reaction3])

    optgp = OptGPSampler(model, 1, seed=42)
    achr = ACHRSampler(model, seed=42)

    optgp_samples = optgp.sample(100)
    achr_samples = achr.sample(100)

    assert any(optgp_samples.corr().abs() < 1.0)
    assert any(achr_samples.corr().abs() < 1.0)

    # > 95% are valid
    assert sum(optgp.validate(optgp_samples) == "v") > 95
    assert sum(achr.validate(achr_samples) == "v") > 95
示例#7
0
def test_batch_sampling(optgp: OptGPSampler) -> None:
    """Test batch sampling."""
    for b in optgp.batch(5, 4):
        assert all(optgp.validate(b) == "v")
示例#8
0
def test_sampling(optgp: OptGPSampler) -> None:
    """Test sampling."""
    s = optgp.sample(10)
    assert all(optgp.validate(s) == "v")
示例#9
0
def test_optgp_init_benchmark(model: "Model", benchmark: Callable) -> None:
    """Benchmark inital OptGP sampling."""
    benchmark(lambda: OptGPSampler(model, processes=2))
示例#10
0
def test_optgp_init_benchmark(model, benchmark):
    """Benchmark inital OptGP sampling."""

    benchmark(lambda: OptGPSampler(model, processes=2))