Skip to content

Commit

Permalink
Merge pull request #759 from opencobra/feat-set-solver-early
Browse files Browse the repository at this point in the history
feat: allow setting solver backend early
  • Loading branch information
Midnighter authored Nov 21, 2023
2 parents 6fc7a44 + d0934d9 commit d5d9494
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
2 changes: 2 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ History
Next Release
------------

* Set the solver backend earlier in the process, before a COBRApy model is created.

0.16.0 (2023-11-19)
-------------------
* Allow the hybrid interface as a solver.
Expand Down
11 changes: 9 additions & 2 deletions src/memote/suite/cli/reports.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
from multiprocessing import Pool, cpu_count

import click
import cobra
import git
from libsbml import SBMLError
from sqlalchemy.exc import ArgumentError
Expand Down Expand Up @@ -143,6 +144,10 @@ def snapshot(
MODEL: Path to model file. Can also be supplied via the environment variable
MEMOTE_MODEL or configured in 'setup.cfg' or 'memote.ini'.
"""
# Configure the chosen solver before attempting to load the model.
config = cobra.Configuration()
config.solver = solver

model_obj, sbml_ver, notifications = api.validate_model(model)
if model_obj is None:
LOGGER.critical(
Expand All @@ -159,7 +164,6 @@ def snapshot(
# Update the default test configuration with custom ones (if any).
for custom in custom_config:
config.merge(ReportConfiguration.load(custom))
model_obj.solver = solver
_, results = api.test_model(
model_obj,
sbml_version=sbml_ver,
Expand Down Expand Up @@ -363,6 +367,10 @@ def diff(
MODELS: List of paths to two or more model files.
"""
# Configure the chosen solver before attempting to load the model.
config = cobra.Configuration()
config.solver = solver

if not any(a.startswith("--tb") for a in pytest_args):
pytest_args = ["--tb", "no"] + pytest_args
# Add further directories to search for tests.
Expand Down Expand Up @@ -390,7 +398,6 @@ def diff(
"reported in {}.".format(model_filename, report_path)
)
continue
model.solver = solver
model_and_model_ver_tuple.append((model, model_ver))
except (IOError, SBMLError):
LOGGER.debug(exc_info=True)
Expand Down
14 changes: 10 additions & 4 deletions src/memote/suite/cli/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

import click
import click_log
import cobra
import git
from cookiecutter.main import cookiecutter, get_user_config
from github import Github
Expand Down Expand Up @@ -231,6 +232,11 @@ def is_verbose(arg):
sys.exit(0)
# Add further directories to search for tests.
pytest_args.extend(custom_tests)

# Configure the chosen solver before attempting to load the model.
config = cobra.Configuration()
config.solver = solver

# Check if the model can be loaded at all.
model, sbml_ver, notifications = api.validate_model(model)
if model is None:
Expand All @@ -239,7 +245,7 @@ def is_verbose(arg):
)
stdout_notifications(notifications)
sys.exit(1)
model.solver = solver

code, result = api.test_model(
model=model,
sbml_version=sbml_ver,
Expand Down Expand Up @@ -339,7 +345,6 @@ def _model_from_stream(stream, filename):
def _test_history(
model,
sbml_ver,
solver,
solver_timeout,
manager,
commit,
Expand All @@ -348,7 +353,6 @@ def _test_history(
exclusive,
experimental,
):
model.solver = solver
# Load the experimental configuration using model information.
if experimental is not None:
experimental.load(model)
Expand Down Expand Up @@ -480,6 +484,9 @@ def history(
previous = repo.active_branch
LOGGER.info("Checking out deployment branch {}.".format(deployment))
repo.git.checkout(deployment)
# Configure the chosen solver before attempting to load the model.
config = cobra.Configuration()
config.solver = solver
# Temporarily move the results to a new location so that they are
# available while checking out the various commits.
engine = None
Expand Down Expand Up @@ -545,7 +552,6 @@ def history(
args=(
model_obj,
sbml_ver,
solver,
solver_timeout,
manager,
commit,
Expand Down

0 comments on commit d5d9494

Please sign in to comment.