Skip to content

Commit

Permalink
feat: updated to Biosimulators-utils 0.1.114: organized options, adde…
Browse files Browse the repository at this point in the history
…d option to control logging; added Python package to specs; added fonts to Docker image
  • Loading branch information
jonrkarr committed Sep 1, 2021
1 parent 3a95a71 commit f17f305
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 46 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,9 @@ jobs:
--workdir /home/opencor/Biosimulators_OpenCOR \
ghcr.io/biosimulators/biosimulators_opencor/opencor:latest \
-c "
/home/opencor/OpenCOR/python/bin/python -m pip install pytest pytest-cov coverage; \
/home/opencor/OpenCOR/python/bin/python -m pip install pytest pytest-cov; \
/home/opencor/OpenCOR/python/bin/python -m pip install -r tests/requirements.txt; \
pythonshell -m pytest tests/ --cov=./ --cov-report=xml; \
/home/opencor/OpenCOR/python/bin/coverage html; \
exit
"
Expand Down
7 changes: 6 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# VERSION
ARG VERSION=0.0.6
ARG VERSION=0.0.7
ARG SIMULATOR_VERSION="2021-08-19"

# Base OS
Expand Down Expand Up @@ -33,6 +33,11 @@ LABEL \
ENV PYTHON="OpenCOR -c PythonShell" \
PIP="${OPENCORDIR}/python/bin/python -m pip"

# fonts for matplotlib
RUN apt-get update -y \
&& apt-get install -y --no-install-recommends libfreetype6 \
&& rm -rf /var/lib/apt/lists/*

# setup matplotlib
RUN mkdir -p /.config/matplotlib \
&& mkdir -p /.cache/matplotlib \
Expand Down
10 changes: 9 additions & 1 deletion biosimulators.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
},
"operatingSystemType": "Linux"
},
"pythonApi": {
"package": "biosimulators-opencor",
"module": "biosimulators_opencor"
},
"authors": [
{
"firstName": "Alan",
Expand Down Expand Up @@ -56,7 +60,11 @@
}
],
"references": {
"identifiers": [],
"identifiers": [{
"namespace": "pypi",
"id": "biosimulators-opencor",
"url": "https://pypi.org/project/biosimulators-opencor/"
}],
"citations": [
{
"title": "OpenCOR: a modular and interoperable approach to computational biology",
Expand Down
2 changes: 1 addition & 1 deletion biosimulators_opencor/_version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '0.0.6'
__version__ = '0.0.7'
35 changes: 14 additions & 21 deletions biosimulators_opencor/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from .utils import (validate_simulation, load_opencor_simulation,
get_results_from_opencor_simulation, log_opencor_execution, get_mock_libcellml)
from biosimulators_utils.combine.exec import exec_sedml_docs_in_archive
from biosimulators_utils.config import get_config, Config # noqa: F401
from biosimulators_utils.log.data_model import CombineArchiveLog, TaskLog, StandardOutputErrorCapturerLevel # noqa: F401
from biosimulators_utils.viz.data_model import VizFormat # noqa: F401
from biosimulators_utils.report.data_model import ReportFormat, VariableResults, SedDocumentResults # noqa: F401
Expand All @@ -22,11 +23,7 @@
]


def exec_sedml_docs_in_combine_archive(archive_filename, out_dir,
return_results=False,
report_formats=None, plot_formats=None,
bundle_outputs=None, keep_individual_outputs=None,
raise_exceptions=True):
def exec_sedml_docs_in_combine_archive(archive_filename, out_dir, config=None):
""" Execute the SED tasks defined in a COMBINE/OMEX archive and save the outputs
Args:
Expand All @@ -38,12 +35,7 @@ def exec_sedml_docs_in_combine_archive(archive_filename, out_dir,
* HDF5: directory in which to save a single HDF5 file (``{ out_dir }/reports.h5``),
with reports at keys ``{ relative-path-to-SED-ML-file-within-archive }/{ report.id }`` within the HDF5 file
return_results (:obj:`bool`, optional): whether to return the result of each output of each SED-ML file
report_formats (:obj:`list` of :obj:`ReportFormat`, optional): report format (e.g., csv or h5)
plot_formats (:obj:`list` of :obj:`VizFormat`, optional): report format (e.g., pdf)
bundle_outputs (:obj:`bool`, optional): if :obj:`True`, bundle outputs into archives for reports and plots
keep_individual_outputs (:obj:`bool`, optional): if :obj:`True`, keep individual output files
raise_exceptions (:obj:`bool`, optional): whether to raise exceptions
config (:obj:`Config`, optional): BioSimulators common configuration
Returns:
:obj:`tuple`:
Expand All @@ -56,22 +48,18 @@ def exec_sedml_docs_in_combine_archive(archive_filename, out_dir,
with mock.patch.dict('sys.modules', libcellml=get_mock_libcellml()):
return exec_sedml_docs_in_archive(sed_doc_executer, archive_filename, out_dir,
apply_xml_model_changes=True,
return_results=return_results,
report_formats=report_formats,
plot_formats=plot_formats,
bundle_outputs=bundle_outputs,
keep_individual_outputs=keep_individual_outputs,
log_level=StandardOutputErrorCapturerLevel.python,
raise_exceptions=raise_exceptions)
config=config)


def exec_sed_task(sed_task, sed_variables, log=None):
def exec_sed_task(sed_task, sed_variables, log=None, config=None):
''' Execute a task and save its results
Args:
sed_task (:obj:`Task`): task
sed_variables (:obj:`list` of :obj:`Variable`): variables that should be recorded
log (:obj:`TaskLog`, optional): log for the task
config (:obj:`Config`, optional): BioSimulators common configuration
Returns:
:obj:`tuple`:
Expand All @@ -84,11 +72,15 @@ def exec_sed_task(sed_task, sed_variables, log=None):
could not be recorded
:obj:`NotImplementedError`: if the task is not of a supported type or involves an unsupported feature
'''
if not config:
config = get_config()

# initialize a log of the execution of this task
log = log or TaskLog()
if config.LOG and not log:
log = TaskLog()

# check that a simulation (or a similar simulation) can be executed with OpenCOR
opencor_sed_task, opencor_variable_names = validate_simulation(sed_task, sed_variables)
opencor_sed_task, opencor_variable_names = validate_simulation(sed_task, sed_variables, config=config)

# load an OpenCOR simulation
opencor_sim = load_opencor_simulation(opencor_sed_task, sed_variables)
Expand All @@ -101,7 +93,8 @@ def exec_sed_task(sed_task, sed_variables, log=None):
variable_results = get_results_from_opencor_simulation(opencor_sim, sed_task, sed_variables, opencor_variable_names)

# log action
log_opencor_execution(opencor_sed_task, log)
if config.LOG:
log_opencor_execution(opencor_sed_task, log)

# return results and log
return variable_results, log
14 changes: 8 additions & 6 deletions biosimulators_opencor/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"""

from .data_model import KISAO_ALGORITHM_MAP
from biosimulators_utils.config import get_config # noqa: F401
from biosimulators_utils.config import get_config, Config # noqa: F401
from biosimulators_utils.data_model import ValueType # noqa: F401
from biosimulators_utils.log.data_model import TaskLog # noqa: F401
from biosimulators_utils.report.data_model import VariableResults # noqa: F401
Expand Down Expand Up @@ -44,20 +44,21 @@
]


def validate_simulation(task, variables):
def validate_simulation(task, variables, config=None):
""" Validate that a simulation can be executed with OpenCOR
Args:
task (:obj:`Task`): request simulation task
variables (:obj:`list` of :obj:`Variable`): variables that should be recorded
config (:obj:`Config`, optional): BioSimulators common configuration
Returns:
:obj:`tuple:`:
:obj:`Task`: possibly alternate task that OpenCOR should execute
:obj:`dict`: dictionary that maps the id of each SED variable to the name that OpenCOR uses to reference it
"""
config = get_config()
config = config or get_config()
model = task.model
sim = task.simulation

Expand Down Expand Up @@ -102,7 +103,7 @@ def validate_simulation(task, variables):
opencor_sim.number_of_steps = int(opencor_sim.number_of_steps)

# check that OpenCOR can execute the request algorithm (or a similar one)
opencor_task.simulation.algorithm = get_opencor_algorithm(opencor_task.simulation.algorithm)
opencor_task.simulation.algorithm = get_opencor_algorithm(opencor_task.simulation.algorithm, config=config)

return opencor_task, opencor_variable_names

Expand Down Expand Up @@ -163,18 +164,19 @@ def validate_variable_xpaths(sed_variables, model_filename):
return opencor_variable_names


def get_opencor_algorithm(requested_alg):
def get_opencor_algorithm(requested_alg, config=None):
""" Get a possibly alternative algorithm that OpenCOR should execute
Args:
requested_alg (:obj:`Algorithm`): requested algorithm
config (:obj:`Config`, optional): configuration
Returns:
:obj:`Algorithm`: possibly alternative algorithm that OpenCOR should execute
"""
exec_alg = copy.deepcopy(requested_alg)

algorithm_substitution_policy = get_algorithm_substitution_policy()
algorithm_substitution_policy = get_algorithm_substitution_policy(config=config)
exec_alg.kisao_id = get_preferred_substitute_algorithm_by_ids(
requested_alg.kisao_id, KISAO_ALGORITHM_MAP.keys(),
substitution_policy=algorithm_substitution_policy)
Expand Down
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
biosimulators_utils[cellml,logging] >= 0.1.104
kisao >= 2.23
biosimulators_utils[cellml,logging] >= 0.1.114
kisao >= 2.28
lxml
numpy
# opencor
31 changes: 19 additions & 12 deletions tests/test_core_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from biosimulators_opencor.data_model import KISAO_ALGORITHM_MAP
from biosimulators_utils.combine import data_model as combine_data_model
from biosimulators_utils.combine.io import CombineArchiveWriter
from biosimulators_utils.config import get_config
from biosimulators_utils.log.data_model import TaskLog
from biosimulators_utils.report import data_model as report_data_model
from biosimulators_utils.report.io import ReportReader
Expand Down Expand Up @@ -51,12 +52,15 @@ def test_exec_sedml_docs_in_combine_archive(self):
doc, archive_filename = self._build_combine_archive()

out_dir = os.path.join(self.dirname, 'out')
core.exec_sedml_docs_in_combine_archive(archive_filename, out_dir,
report_formats=[
report_data_model.ReportFormat.h5,
],
bundle_outputs=True,
keep_individual_outputs=True)

config = get_config()
config.REPORT_FORMATS = [report_data_model.ReportFormat.h5]
config.BUNDLE_OUTPUTS = True
config.KEEP_INDIVIDUAL_OUTPUTS = True

_, log = core.exec_sedml_docs_in_combine_archive(archive_filename, out_dir, config=config)
if log.exception:
raise log.exception

self._assert_combine_archive_outputs(doc, out_dir)

Expand All @@ -72,12 +76,15 @@ def test_exec_sedml_docs_in_combine_archive_with_all_algorithms(self):
doc, archive_filename = self._build_combine_archive(algorithm=alg)

out_dir = os.path.join(self.dirname, 'out')
core.exec_sedml_docs_in_combine_archive(archive_filename, out_dir,
report_formats=[
report_data_model.ReportFormat.h5,
],
bundle_outputs=True,
keep_individual_outputs=True)

config = get_config()
config.REPORT_FORMATS = [report_data_model.ReportFormat.h5]
config.BUNDLE_OUTPUTS = True
config.KEEP_INDIVIDUAL_OUTPUTS = True

_, log = core.exec_sedml_docs_in_combine_archive(archive_filename, out_dir, config=config)
if log.exception:
raise log.exception

self._assert_combine_archive_outputs(doc, out_dir)

Expand Down

0 comments on commit f17f305

Please sign in to comment.