コード例 #1
0
def achr(model):
    """Return ACHRSampler instance for tests."""
    sampler = ACHRSampler(model, thinning=1)
    assert ((sampler.n_warmup > 0) and
            (sampler.n_warmup <= 2 * len(model.variables)))
    assert all(sampler.validate(sampler.warmup) == "v")
    return sampler
コード例 #2
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
コード例 #3
0
    def test_complicated_model(self):
        """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.lower_bound = 0
        reaction2.lower_bound = 0
        reaction3.lower_bound = 0
        reaction1.upper_bound = 6
        reaction2.upper_bound = 8
        reaction3.upper_bound = 10
        A = Metabolite('A')
        reaction1.add_metabolites({A: -1})
        reaction2.add_metabolites({A: -1})
        reaction3.add_metabolites({A: 1})
        model.add_reactions([reaction1])
        model.add_reactions([reaction2])
        model.add_reactions([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)
コード例 #4
0
    def test_complicated_model(self):
        """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.lower_bound = 0
        reaction2.lower_bound = 0
        reaction3.lower_bound = 0
        reaction1.upper_bound = 6
        reaction2.upper_bound = 8
        reaction3.upper_bound = 10
        A = Metabolite('A')
        reaction1.add_metabolites({A: -1})
        reaction2.add_metabolites({A: -1})
        reaction3.add_metabolites({A: 1})
        model.add_reactions([reaction1])
        model.add_reactions([reaction2])
        model.add_reactions([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)
コード例 #5
0
    def setup_class(self):
        from . import create_test_model
        model = create_test_model("textbook")
        achr = ACHRSampler(model, thinning=1)
        assert ((achr.n_warmup > 0) and
                (achr.n_warmup <= 2 * len(model.variables)))
        assert all(achr.validate(achr.warmup) == "v")
        self.achr = achr

        optgp = OptGPSampler(model, processes=1, thinning=1)
        assert ((optgp.n_warmup > 0) and
                (optgp.n_warmup <= 2 * len(model.variables)))
        assert all(optgp.validate(optgp.warmup) == "v")
        self.optgp = optgp
コード例 #6
0
    def setup_class(self):
        from . import create_test_model
        model = create_test_model("textbook")
        achr = ACHRSampler(model, thinning=1)
        assert ((achr.n_warmup > 0)
                and (achr.n_warmup <= 2 * len(model.variables)))
        assert all(achr.validate(achr.warmup) == "v")
        self.achr = achr

        optgp = OptGPSampler(model, processes=1, thinning=1)
        assert ((optgp.n_warmup > 0)
                and (optgp.n_warmup <= 2 * len(model.variables)))
        assert all(optgp.validate(optgp.warmup) == "v")
        self.optgp = optgp
コード例 #7
0
def capitulo_7():
    file = open("resultados_capitulo_7.txt", "w")
    model = create_test_model("textbook")
    s = sample(model, 100)
    s.head()
    print("One process:")
    s = sample(model, 1000)
    print("Two processes:")
    s = sample(model, 1000, processes=2)
    s = sample(model, 100, method="achr")
    from cobra.flux_analysis.sampling import OptGPSampler, ACHRSampler
    achr = ACHRSampler(model, thinning=10)
    optgp = OptGPSampler(model, processes=4)
    s1 = achr.sample(100)
    s2 = optgp.sample(100)
    import numpy as np
    bad = np.random.uniform(-1000, 1000, size=len(model.reactions))
    achr.validate(np.atleast_2d(bad))
    achr.validate(s1)
    counts = [
        np.mean(s.Biomass_Ecoli_core > 0.1) for s in optgp.batch(100, 10)
    ]
    file.write("Usually {:.2f}% +- {:.2f}% grow...".format(
        np.mean(counts) * 100.0,
        np.std(counts) * 100.0))
    file.write("\n")
    co = model.problem.Constraint(
        model.reactions.Biomass_Ecoli_core.flux_expression, lb=0.1)
    model.add_cons_vars([co])
    s = sample(model, 10)
    file.write(s.Biomass_Ecoli_core)
    file.write("\n")
    file.close()
コード例 #8
0
def fluxSampling(model):
    from cobra.test import create_test_model
    from cobra.flux_analysis import sample
    model = create_test_model("textbook")
    s = sample(model, 100)  #number of samples to generate
    s.head()
    s = sample(model, 1000)
    s

    #The sampling process can be controlled on a lower level by using the sampler classes directly.
    from cobra.flux_analysis.sampling import OptGPSampler, ACHRSampler
    achr = ACHRSampler(
        model, thinning=10
    )  #“Thinning” means only recording samples every n iterations. A higher thinning factor means less correlated samples but also larger computation times.
    optgp = OptGPSampler(
        model, processes=4
    )  #an additional processes argument specifying how many processes are used to create parallel sampling chains.

    #For OptGPSampler the number of samples should be a multiple of the number of
    # processes, otherwise it will be increased to the nearest multiple automatically.
    s1 = achr.sample(100)
    s2 = optgp.sample(100)

    # Sampling and validation
    import numpy as np
    bad = np.random.uniform(-1000, 1000, size=len(model.reactions))
    achr.validate(np.atleast_2d(bad))  #should not be feasible
    achr.validate(s1)

    # Batch sampling
    counts = [
        np.mean(s.Biomass_Ecoli_core > 0.1) for s in optgp.batch(100, 10)
    ]
    print("Usually {:.2f}% +- {:.2f}% grow...".format(
        np.mean(counts) * 100.0,
        np.std(counts) * 100.0))

    # Adding constraints
    co = model.problem.Constraint(
        model.reactions.Biomass_Ecoli_core.flux_expression, lb=0.1)
    model.add_cons_vars([co])

    # Note that this is only for demonstration purposes. usually you could set
    # the lower bound of the reaction directly instead of creating a new constraint.
    s = sample(model, 10)
    print(s.Biomass_Ecoli_core)
コード例 #9
0
def test_achr_init_benchmark(model, benchmark):
    """Benchmark inital ACHR sampling."""
    benchmark(lambda: ACHRSampler(model))
コード例 #10
0
 def test_achr_init_benchmark(self, model, benchmark):
     benchmark(lambda: ACHRSampler(model))