本文整理汇总了Python中cobra.core.Reaction.lower_bound方法的典型用法代码示例。如果您正苦于以下问题:Python Reaction.lower_bound方法的具体用法?Python Reaction.lower_bound怎么用?Python Reaction.lower_bound使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类cobra.core.Reaction
的用法示例。
在下文中一共展示了Reaction.lower_bound方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_solve_mip
# 需要导入模块: from cobra.core import Reaction [as 别名]
# 或者: from cobra.core.Reaction import lower_bound [as 别名]
def test_solve_mip(self, solver_test):
solver, old_solution, infeasible_model = solver_test
if not hasattr(solver, "_SUPPORTS_MILP") or not solver._SUPPORTS_MILP:
pytest.skip("no milp support")
cobra_model = Model('MILP_implementation_test')
constraint = Metabolite("constraint")
constraint._bound = 2.5
x = Reaction("x")
x.lower_bound = 0.
x.objective_coefficient = 1.
x.add_metabolites({constraint: 2.5})
y = Reaction("y")
y.lower_bound = 0.
y.objective_coefficient = 1.
y.add_metabolites({constraint: 1.})
cobra_model.add_reactions([x, y])
float_sol = solver.solve(cobra_model)
# add an integer constraint
y.variable_kind = "integer"
int_sol = solver.solve(cobra_model)
assert abs(float_sol.f - 2.5) < 10 ** -5
assert abs(float_sol.x_dict["y"] - 2.5) < 10 ** -5
assert int_sol.status == "optimal"
assert abs(int_sol.f - 2.2) < 10 ** -3
assert abs(int_sol.x_dict["y"] - 2.0) < 10 ** -3
示例2: test_inequality
# 需要导入模块: from cobra.core import Reaction [as 别名]
# 或者: from cobra.core.Reaction import lower_bound [as 别名]
def test_inequality(self, solver_test):
solver, old_solution, infeasible_model = solver_test
# The space enclosed by the constraints is a 2D triangle with
# vertexes as (3, 0), (1, 2), and (0, 1)
# c1 encodes y - x > 1 ==> y > x - 1
# c2 encodes y + x < 3 ==> y < 3 - x
c1 = Metabolite("c1")
c2 = Metabolite("c2")
x = Reaction("x")
x.lower_bound = 0
y = Reaction("y")
y.lower_bound = 0
x.add_metabolites({c1: -1, c2: 1})
y.add_metabolites({c1: 1, c2: 1})
c1._bound = 1
c1._constraint_sense = "G"
c2._bound = 3
c2._constraint_sense = "L"
m = Model()
m.add_reactions([x, y])
# test that optimal values are at the vertices
m.objective = "x"
assert abs(solver.solve(m).f - 1.0) < 10 ** -3
assert abs(solver.solve(m).x_dict["y"] - 2.0) < 10 ** -3
m.objective = "y"
assert abs(solver.solve(m).f - 3.0) < 10 ** -3
assert abs(
solver.solve(m, objective_sense="minimize").f - 1.0) < 10 ** -3
示例3: test_change_coefficient
# 需要导入模块: from cobra.core import Reaction [as 别名]
# 或者: from cobra.core.Reaction import lower_bound [as 别名]
def test_change_coefficient(self, solver_test):
solver, old_solution, infeasible_model = solver_test
c = Metabolite("c")
c._bound = 6
x = Reaction("x")
x.lower_bound = 1.
y = Reaction("y")
y.lower_bound = 0.
x.add_metabolites({c: 1})
z = Reaction("z")
z.add_metabolites({c: 1})
z.objective_coefficient = 1
m = Model("test_model")
m.add_reactions([x, y, z])
# change an existing coefficient
lp = solver.create_problem(m)
solver.solve_problem(lp)
sol1 = solver.format_solution(lp, m)
assert sol1.status == "optimal"
solver.change_coefficient(lp, 0, 0, 2)
solver.solve_problem(lp)
sol2 = solver.format_solution(lp, m)
assert sol2.status == "optimal"
assert abs(sol1.f - 5.0) < 10 ** -3
assert abs(sol2.f - 4.0) < 10 ** -3
# change a new coefficient
z.objective_coefficient = 0.
y.objective_coefficient = 1.
lp = solver.create_problem(m)
solver.change_coefficient(lp, 0, 1, 2)
solver.solve_problem(lp)
solution = solver.format_solution(lp, m)
assert solution.status == "optimal"
assert abs(solution.x_dict["y"] - 2.5) < 10 ** -3
示例4: test_complicated_model
# 需要导入模块: from cobra.core import Reaction [as 别名]
# 或者: from cobra.core.Reaction import lower_bound [as 别名]
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: test_quadratic
# 需要导入模块: from cobra.core import Reaction [as 别名]
# 或者: from cobra.core.Reaction import lower_bound [as 别名]
def test_quadratic(self, solver_test):
solver, old_solution, infeasible_model = solver_test
if not hasattr(solver, "set_quadratic_objective"):
pytest.skip("no qp support")
c = Metabolite("c")
c._bound = 2
x = Reaction("x")
x.objective_coefficient = -0.5
x.lower_bound = 0.
y = Reaction("y")
y.objective_coefficient = -0.5
y.lower_bound = 0.
x.add_metabolites({c: 1})
y.add_metabolites({c: 1})
m = Model()
m.add_reactions([x, y])
lp = solver.create_problem(m)
quadratic_obj = scipy.sparse.eye(2) * 2
solver.set_quadratic_objective(lp, quadratic_obj)
solver.solve_problem(lp, objective_sense="minimize")
solution = solver.format_solution(lp, m)
assert solution.status == "optimal"
# Respecting linear objectives also makes the objective value 1.
assert abs(solution.f - 1.) < 10 ** -3
assert abs(solution.x_dict["y"] - 1.) < 10 ** -3
assert abs(solution.x_dict["y"] - 1.) < 10 ** -3
# When the linear objectives are removed the objective value is 2.
solver.change_variable_objective(lp, 0, 0.)
solver.change_variable_objective(lp, 1, 0.)
solver.solve_problem(lp, objective_sense="minimize")
solution = solver.format_solution(lp, m)
assert solution.status == "optimal"
assert abs(solution.f - 2.) < 10 ** -3
# test quadratic from solve function
solution = solver.solve(m, quadratic_component=quadratic_obj,
objective_sense="minimize")
assert solution.status == "optimal"
assert abs(solution.f - 1.) < 10 ** -3
c._bound = 6
z = Reaction("z")
x.objective_coefficient = 0.
y.objective_coefficient = 0.
z.lower_bound = 0.
z.add_metabolites({c: 1})
m.add_reaction(z)
solution = solver.solve(m, quadratic_component=scipy.sparse.eye(3),
objective_sense="minimize")
# should be 12 not 24 because 1/2 (V^T Q V)
assert solution.status == "optimal"
assert abs(solution.f - 6) < 10 ** -3
assert abs(solution.x_dict["x"] - 2) < 10 ** -6
assert abs(solution.x_dict["y"] - 2) < 10 ** -6
assert abs(solution.x_dict["z"] - 2) < 10 ** -6
示例6: test__normalize_pseudoreaction_sink
# 需要导入模块: from cobra.core import Reaction [as 别名]
# 或者: from cobra.core.Reaction import lower_bound [as 别名]
def test__normalize_pseudoreaction_sink():
reaction = Reaction('SInk_gone')
reaction.add_metabolites({Metabolite('glu__L_c'): -1})
reaction.lower_bound = -1000
reaction.upper_bound = 0
pseudo_id = _normalize_pseudoreaction(reaction.id, reaction)
assert pseudo_id == 'SK_glu__L_c'
assert reaction.subsystem == 'Intracellular source/sink'
示例7: test_add_reactions_single_existing
# 需要导入模块: from cobra.core import Reaction [as 别名]
# 或者: from cobra.core.Reaction import lower_bound [as 别名]
def test_add_reactions_single_existing(self, model):
rxn = model.reactions[0]
r1 = Reaction(rxn.id)
r1.add_metabolites({Metabolite('A'): -1, Metabolite('B'): 1})
r1.lower_bound, r1.upper_bound = -999999., 999999.
model.add_reactions([r1])
assert rxn in model.reactions
assert r1 is not model.reactions.get_by_id(rxn.id)
示例8: test__normalize_pseudoreaction_exchange
# 需要导入模块: from cobra.core import Reaction [as 别名]
# 或者: from cobra.core.Reaction import lower_bound [as 别名]
def test__normalize_pseudoreaction_exchange():
reaction = Reaction('EX_gone')
reaction.add_metabolites({Metabolite('glu__L_e'): -1})
reaction.lower_bound = -1000
reaction.upper_bound = 0
pseudo_id = _normalize_pseudoreaction(reaction.id, reaction)
assert pseudo_id == 'EX_glu__L_e'
assert reaction.subsystem == 'Extracellular exchange'
示例9: test__normalize_pseudoreaction_demand
# 需要导入模块: from cobra.core import Reaction [as 别名]
# 或者: from cobra.core.Reaction import lower_bound [as 别名]
def test__normalize_pseudoreaction_demand():
reaction = Reaction('DM_gone')
reaction.add_metabolites({Metabolite('glu__L_c'): -1})
reaction.lower_bound = 0
reaction.upper_bound = 1000
pseudo_id = _normalize_pseudoreaction(reaction.id, reaction)
assert pseudo_id == 'DM_glu__L_c'
assert reaction.subsystem == 'Intracellular demand'
示例10: test__normalize_pseudoreaction_sink_reversed
# 需要导入模块: from cobra.core import Reaction [as 别名]
# 或者: from cobra.core.Reaction import lower_bound [as 别名]
def test__normalize_pseudoreaction_sink_reversed():
reaction = Reaction('Sink_gone')
reaction.add_metabolites({Metabolite('glu__L_c'): 1})
reaction.lower_bound = 0
reaction.upper_bound = 50
pseudo_id = _normalize_pseudoreaction(reaction.id, reaction)
assert list(reaction.metabolites.values()) == [-1]
assert reaction.lower_bound == -50
assert reaction.upper_bound == 0
assert pseudo_id == 'SK_glu__L_c'
示例11: test__normalize_pseudoreaction_demand_reversed_prefer_sink_name
# 需要导入模块: from cobra.core import Reaction [as 别名]
# 或者: from cobra.core.Reaction import lower_bound [as 别名]
def test__normalize_pseudoreaction_demand_reversed_prefer_sink_name():
reaction = Reaction('sink_gone')
reaction.add_metabolites({Metabolite('glu__L_c'): 1})
reaction.lower_bound = -1000
reaction.upper_bound = 0
_normalize_pseudoreaction(reaction)
assert list(reaction.metabolites.values()) == [-1]
assert reaction.lower_bound == 0
assert reaction.upper_bound == 1000
assert reaction.id == 'SK_glu__L_c'
示例12: test__normalize_pseudoreaction_demand_reversed
# 需要导入模块: from cobra.core import Reaction [as 别名]
# 或者: from cobra.core.Reaction import lower_bound [as 别名]
def test__normalize_pseudoreaction_demand_reversed():
reaction = Reaction('DM_gone')
reaction.add_metabolites({Metabolite('glu__L_c'): 1})
reaction.lower_bound = -1000
reaction.upper_bound = 0
pseudo_id = _normalize_pseudoreaction(reaction.id, reaction)
assert list(reaction.metabolites.values()) == [-1]
assert reaction.lower_bound == 0
assert reaction.upper_bound == 1000
assert pseudo_id == 'DM_glu__L_c'
示例13: test__normalize_pseudoreaction_exchange_reversed
# 需要导入模块: from cobra.core import Reaction [as 别名]
# 或者: from cobra.core.Reaction import lower_bound [as 别名]
def test__normalize_pseudoreaction_exchange_reversed():
reaction = Reaction('EX_gone')
reaction.add_metabolites({Metabolite('glu__L_e'): 1})
reaction.lower_bound = 0
reaction.upper_bound = 1000
pseudo_id = _normalize_pseudoreaction(reaction.id, reaction)
assert pseudo_id == 'EX_glu__L_e'
assert reaction.lower_bound == -1000
assert reaction.upper_bound == 0
assert list(reaction.metabolites.values()) == [-1]
示例14: test_add_reactions_duplicate
# 需要导入模块: from cobra.core import Reaction [as 别名]
# 或者: from cobra.core.Reaction import lower_bound [as 别名]
def test_add_reactions_duplicate(self, model):
rxn = model.reactions[0]
r1 = Reaction('r1')
r1.add_metabolites({Metabolite('A'): -1, Metabolite('B'): 1})
r1.lower_bound, r1.upper_bound = -999999., 999999.
r2 = Reaction(rxn.id)
r2.add_metabolites(
{Metabolite('A'): -1, Metabolite('C'): 1, Metabolite('D'): 1})
model.add_reactions([r1, r2])
assert r1 in model.reactions
assert rxn in model.reactions
assert r2 is not model.reactions.get_by_id(rxn.id)
示例15: construct_loopless_model
# 需要导入模块: from cobra.core import Reaction [as 别名]
# 或者: from cobra.core.Reaction import lower_bound [as 别名]
def construct_loopless_model(cobra_model):
"""Construct a loopless model.
This adds MILP constraints to prevent flux from proceeding in a loop, as
done in http://dx.doi.org/10.1016/j.bpj.2010.12.3707
Please see the documentation for an explanation of the algorithm.
This must be solved with an MILP capable solver.
"""
# copy the model and make it irreversible
model = cobra_model.copy()
convert_to_irreversible(model)
max_ub = max(model.reactions.list_attr("upper_bound"))
# a dict for storing S^T
thermo_stoic = {"thermo_var_" + metabolite.id: {}
for metabolite in model.metabolites}
# Slice operator is so that we don't get newly added metabolites
original_metabolites = model.metabolites[:]
for reaction in model.reactions[:]:
# Boundary reactions are not subjected to these constraints
if len(reaction._metabolites) == 1:
continue
# populate the S^T dict
bound_id = "thermo_bound_" + reaction.id
for met, stoic in iteritems(reaction._metabolites):
thermo_stoic["thermo_var_" + met.id][bound_id] = stoic
# I * 1000 > v --> I * 1000 - v > 0
reaction_ind = Reaction(reaction.id + "_indicator")
reaction_ind.variable_kind = "integer"
reaction_ind.upper_bound = 1
reaction_ub = Metabolite(reaction.id + "_ind_ub")
reaction_ub._constraint_sense = "G"
reaction.add_metabolites({reaction_ub: -1})
reaction_ind.add_metabolites({reaction_ub: max_ub})
# This adds a compensating term for 0 flux reactions, so we get
# S^T x - (1 - I) * 1001 < -1 which becomes
# S^T x < 1000 for 0 flux reactions and
# S^T x < -1 for reactions with nonzero flux.
reaction_bound = Metabolite(bound_id)
reaction_bound._constraint_sense = "L"
reaction_bound._bound = max_ub
reaction_ind.add_metabolites({reaction_bound: max_ub + 1})
model.add_reaction(reaction_ind)
for metabolite in original_metabolites:
metabolite_var = Reaction("thermo_var_" + metabolite.id)
metabolite_var.lower_bound = -max_ub
model.add_reaction(metabolite_var)
metabolite_var.add_metabolites(
{model.metabolites.get_by_id(k): v
for k, v in iteritems(thermo_stoic[metabolite_var.id])})
return model