This is an automated email from the git hooks/post-receive script. afif-guest pushed a commit to branch master in repository python-cobra.
commit 831ebc4315c2e02fbdb5141387768dc79411abe4 Author: Afif Elghraoui <[email protected]> Date: Sun Sep 13 02:07:01 2015 -0700 Imported Upstream version 0.4.0b4 --- appveyor.yml | 2 +- cobra/VERSION | 2 +- cobra/io/mat.py | 8 +++++-- cobra/solvers/__init__.py | 2 ++ cobra/solvers/cplex_solver.py | 48 +++++++++++++++++++++++++----------------- cobra/solvers/gurobi_solver.py | 30 +++++++++++++++++++++----- 6 files changed, 64 insertions(+), 28 deletions(-) diff --git a/appveyor.yml b/appveyor.yml index 6f2033e..c0feca7 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -32,7 +32,7 @@ init: install: - "powershell appveyor\\install.ps1" - - ps: Start-FileDownload 'https://opencobra.github.io/pypi_cobrapy_travis/six.py' + - ps: Start-FileDownload 'https://bitbucket.org/gutworth/six/raw/default/six.py' # Download glpk.h and glpk.lib (separate files for 32/64 bit) - ps: Start-FileDownload 'https://opencobra.github.io/pypi_cobrapy_travis/glpk.h' - "appveyor DownloadFile https://opencobra.github.io/pypi_cobrapy_travis/glpk.lib%PYTHON_ARCH% -FileName glpk.lib" diff --git a/cobra/VERSION b/cobra/VERSION index de998de..d4e083c 100644 --- a/cobra/VERSION +++ b/cobra/VERSION @@ -1 +1 @@ -0.4.0b3 +0.4.0b4 diff --git a/cobra/io/mat.py b/cobra/io/mat.py index 3865e65..a7fb7f6 100644 --- a/cobra/io/mat.py +++ b/cobra/io/mat.py @@ -74,7 +74,7 @@ def load_matlab_model(infile_path, variable_name=None): raise Exception("no COBRA model found") -def save_matlab_model(model, file_name): +def save_matlab_model(model, file_name, varname=None): """Save the cobra model as a .mat file. This .mat file can be used directly in the MATLAB version of COBRA. @@ -84,8 +84,12 @@ def save_matlab_model(model, file_name): file_name : str or file-like object """ + if varname is None: + varname = str(model.id) \ + if model.id is not None and len(model.id) > 0 \ + else "exported_model" mat = create_mat_dict(model) - savemat(file_name, {str(model.id): mat}, + savemat(file_name, {varname: mat}, appendmat=True, oned_as="column") diff --git a/cobra/solvers/__init__.py b/cobra/solvers/__init__.py index ecdb24a..afd7d14 100644 --- a/cobra/solvers/__init__.py +++ b/cobra/solvers/__init__.py @@ -85,6 +85,8 @@ def get_solver_name(mip=False, qp=False): for solver_name in lp_order: if solver_name in solver_dict: return solver_name + # none of them are in the list order - so return the first one + return list(solver_dict)[0] elif qp: # mip does not yet matter for this determination for solver_name in qp_order: if solver_name in solver_dict: diff --git a/cobra/solvers/cplex_solver.py b/cobra/solvers/cplex_solver.py index 2e8d4dc..3b1ea14 100644 --- a/cobra/solvers/cplex_solver.py +++ b/cobra/solvers/cplex_solver.py @@ -11,6 +11,18 @@ from ..core.Solution import Solution from six.moves import zip from six import string_types, iteritems +try: + from sympy import Basic, Number +except: + class Basic: + pass + +def _float(value): + if isinstance(value, Basic) and not isinstance(value, Number): + return 0. + else: + return float(value) + solver_name = 'cplex' _SUPPORTS_MILP = True @@ -155,8 +167,8 @@ def create_problem(cobra_model, quadratic_component=None, **kwargs): set_parameter(lp, k, v) objective_coefficients = [float(x.objective_coefficient) for x in cobra_model.reactions] - lower_bounds = [float(x.lower_bound) for x in cobra_model.reactions] - upper_bounds = [float(x.upper_bound) for x in cobra_model.reactions] + lower_bounds = [_float(x.lower_bound) for x in cobra_model.reactions] + upper_bounds = [_float(x.upper_bound) for x in cobra_model.reactions] variable_names = cobra_model.reactions.list_attr("id") variable_kinds = [variable_kind_dict[x.variable_kind] for x in cobra_model.reactions] @@ -174,28 +186,23 @@ def create_problem(cobra_model, quadratic_component=None, **kwargs): ub=upper_bounds, names=variable_names) - ## if relax_b: - ## range_values = zeros(len(cobra_model.metabolites)) - ## b_values = array([x._bound for x in cobra_model.metabolties]) - ## for the_nonzero in list(b_values.nonzero()[0]): - ## range_values[the_nonzero] = -relax_b - constraint_sense = [] constraint_names = [] constraint_limits = [] - [(constraint_sense.append(x._constraint_sense), - constraint_names.append(x.id), - constraint_limits.append(float(x._bound))) - for x in cobra_model.metabolites] + + for x in cobra_model.metabolites: + constraint_sense.append(x._constraint_sense) + constraint_names.append(x.id) + constraint_limits.append(float(x._bound)) the_linear_expressions = [] - #NOTE: This won't work with metabolites that aren't in any reaction + # NOTE: This won't work with metabolites that aren't in any reaction for the_metabolite in cobra_model.metabolites: variable_list = [] coefficient_list = [] for the_reaction in the_metabolite._reaction: variable_list.append(the_reaction.id) - coefficient_list.append(float(the_reaction._metabolites[the_metabolite])) + coefficient_list.append(_float(the_reaction._metabolites[the_metabolite])) the_linear_expressions.append(SparsePair(ind=variable_list, val=coefficient_list)) # Set objective to quadratic program @@ -215,7 +222,7 @@ def create_problem(cobra_model, quadratic_component=None, **kwargs): senses=constraint_sense, names=constraint_names) - #Set the problem type as cplex doesn't appear to do this correctly + # Set the problem type as cplex doesn't appear to do this correctly problem_type = Cplex.problem_type.LP if Cplex.variables.type.integer in variable_kinds: if quadratic_component is not None: @@ -245,10 +252,12 @@ def set_quadratic_objective(lp, quadratic_objective): for k, v in quadratic_component_scaled.items(): lp.objective.set_quadratic_coefficients(int(k[0]), int(k[1]), v) + def change_variable_bounds(lp, index, lower_bound, upper_bound): lp.variables.set_lower_bounds(index, lower_bound) lp.variables.set_upper_bounds(index, upper_bound) + def change_variable_objective(lp, index, objective): lp.objective.set_linear(index, objective) @@ -256,6 +265,7 @@ def change_variable_objective(lp, index, objective): def change_coefficient(lp, met_index, rxn_index, value): lp.linear_constraints.set_coefficients(met_index, rxn_index, value) + def update_problem(lp, cobra_model, new_objective=None, **kwargs): """A performance tunable method for updating a model problem file @@ -301,8 +311,8 @@ def solve(cobra_model, **kwargs): """ """ - #Start out with default parameters and then modify if - #new onese are provided + # Start out with default parameters and then modify if + # new onese are provided for i in ["new_objective", "update_problem", "the_problem"]: if i in kwargs: raise Exception("Option %s removed" % i) @@ -313,7 +323,7 @@ def solve(cobra_model, **kwargs): # Create problem will get parameter defaults lp = create_problem(cobra_model, **kwargs) - ###Try to solve the problem using other methods if the first method doesn't work + # Try to solve the problem using other methods if first method doesn't work try: lp_method = the_parameters['lp_method'] except: @@ -321,7 +331,7 @@ def solve(cobra_model, **kwargs): the_methods = [1, 2, 3, 4, 5, 6] if lp_method in the_methods: the_methods.remove(lp_method) - #Start with the user specified method + # Start with the user specified method the_methods.insert(0, lp_method) for the_method in the_methods: try: diff --git a/cobra/solvers/gurobi_solver.py b/cobra/solvers/gurobi_solver.py index d69308e..57ad4ee 100644 --- a/cobra/solvers/gurobi_solver.py +++ b/cobra/solvers/gurobi_solver.py @@ -5,10 +5,24 @@ from itertools import izip from gurobipy import Model, LinExpr, GRB, QuadExpr - from ..core.Solution import Solution + from six import string_types +try: + from sympy import Basic, Number +except: + class Basic: + pass + Number = Basic + + +def _float(value): + if isinstance(value, Basic) and not isinstance(value, Number): + return 0. + else: + return float(value) + solver_name = 'gurobi' _SUPPORTS_MILP = True @@ -47,6 +61,7 @@ objective_senses = {'maximize': GRB.MAXIMIZE, 'minimize': GRB.MINIMIZE} status_dict = {GRB.OPTIMAL: 'optimal', GRB.INFEASIBLE: 'infeasible', GRB.UNBOUNDED: 'unbounded', GRB.TIME_LIMIT: 'time_limit'} + def get_status(lp): status = lp.status if status in status_dict: @@ -55,9 +70,11 @@ def get_status(lp): status = 'failed' return status + def get_objective_value(lp): return lp.ObjVal + def format_solution(lp, cobra_model, **kwargs): status = get_status(lp) if status not in ('optimal', 'time_limit'): @@ -70,11 +87,13 @@ def format_solution(lp, cobra_model, **kwargs): y = y_dict = None # MIP's don't have duals else: y = [c.Pi for c in lp.getConstrs()] - y_dict = {m.id: value for m, value in izip(cobra_model.metabolites, y)} + y_dict = {m.id: value for m, value + in izip(cobra_model.metabolites, y)} the_solution = Solution(objective_value, x=x, x_dict=x_dict, y=y, y_dict=y_dict, status=status) return(the_solution) + def set_parameter(lp, parameter_name, parameter_value): if parameter_name == 'ModelSense' or parameter_name == "objective_sense": lp.setAttr('ModelSense', objective_senses[parameter_value]) @@ -87,6 +106,7 @@ def set_parameter(lp, parameter_name, parameter_value): parameter_value = METHODS[parameter_value] lp.setParam(parameter_name, parameter_value) + def change_variable_bounds(lp, index, lower_bound, upper_bound): variable = lp.getVarByName(str(index)) variable.lb = lower_bound @@ -150,8 +170,8 @@ def create_problem(cobra_model, quadratic_component=None, **kwargs): # Create variables #TODO: Speed this up - variable_list = [lp.addVar(float(x.lower_bound), - float(x.upper_bound), + variable_list = [lp.addVar(_float(x.lower_bound), + _float(x.upper_bound), float(x.objective_coefficient), variable_kind_dict[x.variable_kind], str(i)) @@ -169,7 +189,7 @@ def create_problem(cobra_model, quadratic_component=None, **kwargs): constraint_coefficients = [] constraint_variables = [] for the_reaction in the_metabolite._reaction: - constraint_coefficients.append(the_reaction._metabolites[the_metabolite]) + constraint_coefficients.append(_float(the_reaction._metabolites[the_metabolite])) constraint_variables.append(reaction_to_variable[the_reaction]) #Add the metabolite to the problem lp.addConstr(LinExpr(constraint_coefficients, constraint_variables), -- Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-med/python-cobra.git _______________________________________________ debian-med-commit mailing list [email protected] http://lists.alioth.debian.org/cgi-bin/mailman/listinfo/debian-med-commit
