You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
For future reference: With symengine installed the constraints added by the following function are evaluated to be quadratic, raising an error, without symengine they work perfectly. A sample error message:
defadjust_fluxes2model(model, observations, uncertainties=None, linear=True,
big_m=1E05):
""" Minimize the distance to observed fluxes accounting for multiple directions. If your observations include uncertainties the objective function, i.e., minimizing the distance to the observations, is weighted by the inverse of the uncertainties. Parameters ---------- model : cobra.Model The metabolic model observations : pandas.Series The observed fluxes. The index should contain reaction identifiers. uncertainties : pandas.Series, optional The uncertainties of the individual measurements, e.g., standard error. The index of the series should correspond at least partially to the ``observations``. linear : bool, optional Whether to minimize the linear or quadratic distance. big_m : float, optional Big M method value. This is used to resolve greater than inequalities and should be an adequately large number. Returns ------- cobra.Solution """flux_col="flux"weight_col="weight"ifuncertaintiesisNone:
data=observations.to_frame().join(Series([], name=weight_col))
else:
uncertainties.name=weight_coldata=observations.to_frame().join(uncertainties)
data.columns= [flux_col, weight_col]
# replace missing and zero valuesdata.loc[data[weight_col].isnull() |isinf(data[weight_col]) |
(data[weight_col] ==0), weight_col] =1prob=model.problemto_add=list()
new_obj=list()
withmodel:
model.objective=prob.Objective(prob.Zero, direction="min", sloppy=True)
forrxn_id, flux, weightindata[[flux_col, weight_col]].itertuples():
try:
rxn=model.reactions.get_by_id(rxn_id)
exceptKeyError:
LOGGER.warning("'%s' not found in the model. Ignored.", rxn_id)
continuedirection=prob.Variable("direction_"+rxn_id, type="binary")
dist=prob.Variable("dist_"+rxn_id)
forward_pos=prob.Constraint(
flux-rxn.flux_expression-big_m* (1-direction) -dist,
ub=0, name="forward_pos_"+rxn_id)
forward_neg=prob.Constraint(
rxn.flux_expression-flux-big_m* (1-direction) -dist,
ub=0, name="forward_neg_"+rxn_id)
reverse_pos=prob.Constraint(
(-flux) -rxn.flux_expression-big_m*direction-dist,
ub=0, name="reverse_pos_"+rxn_id)
reverse_neg=prob.Constraint(
rxn.flux_expression- (-flux) -big_m*direction-dist,
ub=0, name="reverse_neg_"+rxn_id)
iflinear:
new_obj.append((dist, 1/weight))
else:
raiseNotImplementedError("Need to set quadratic coefficients.")
# new_obj.append((dist * dist, 1 / weight))to_add.extend([direction, dist, forward_pos, forward_neg,
reverse_pos, reverse_neg])
model.add_cons_vars(to_add)
model.objective.set_linear_coefficients({v: fforv, finnew_obj})
solution=model.optimize(raise_error=True)
returnsolution
The text was updated successfully, but these errors were encountered:
Sympy does automatic expansions for certain types of nested expressions, e.g. coef * (var1 - var2). Symengine does not appear to do this at all, instead expressions are structured exactly as they are formulated. The problem can be easily fixed by uncommenting line 466 in optlang.interface. However this will have a negative impact on general performance. At the very least it should only be done when optlang._USING_SYMENGINE is true - since symengine is so fast it may actually not have a huge effect there.
@Midnighter If you have some cobrapy benchmarks, can you maybe check how this would impact performance with symengine?
For future reference: With symengine installed the constraints added by the following function are evaluated to be quadratic, raising an error, without symengine they work perfectly. A sample error message:
The text was updated successfully, but these errors were encountered: