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 c2740b9 commit 2984470
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 70 deletions.
5 changes: 5 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ LABEL \
about.tags="kinetic modeling,dynamical simulation,systems biology,biochemical networks,SBML,SED-ML,COMBINE,OMEX,BioSimulators" \
maintainer="BioSimulators Team <[email protected]>"

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

# Copy code for command-line interface into image and install it
COPY . /root/Biosimulators_COPASI
RUN pip install /root/Biosimulators_COPASI \
Expand Down
8 changes: 4 additions & 4 deletions biosimulators.json
Original file line number Diff line number Diff line change
Expand Up @@ -1245,7 +1245,7 @@
"modelingFrameworks": [
{
"namespace": "SBO",
"id": "SBO_0000295"
"id": "SBO_0000690"
}
],
"modelFormats": [
Expand Down Expand Up @@ -1481,7 +1481,7 @@
"modelingFrameworks": [
{
"namespace": "SBO",
"id": "SBO_0000295"
"id": "SBO_0000690"
}
],
"modelFormats": [
Expand Down Expand Up @@ -1768,7 +1768,7 @@
"modelingFrameworks": [
{
"namespace": "SBO",
"id": "SBO_0000295"
"id": "SBO_0000690"
}
],
"modelFormats": [
Expand Down Expand Up @@ -1989,7 +1989,7 @@
"modelingFrameworks": [
{
"namespace": "SBO",
"id": "SBO_0000293"
"id": "SBO_0000676"
}
],
"modelFormats": [
Expand Down
47 changes: 18 additions & 29 deletions biosimulators_copasi/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"""

from biosimulators_utils.combine.exec import exec_sedml_docs_in_archive
from biosimulators_utils.config import get_config
from biosimulators_utils.config import get_config, Config # noqa: F401
from biosimulators_utils.log.data_model import CombineArchiveLog, TaskLog # 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 @@ -32,11 +32,7 @@
__all__ = ['exec_sedml_docs_in_combine_archive', 'exec_sed_task']


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 @@ -48,12 +44,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 @@ -64,21 +55,17 @@ def exec_sedml_docs_in_combine_archive(archive_filename, out_dir,
sed_doc_executer = functools.partial(exec_sed_doc, exec_sed_task)
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,
raise_exceptions=raise_exceptions)
config=config)


def exec_sed_task(task, variables, log=None):
def exec_sed_task(task, variables, log=None, config=None):
''' Execute a task and save its results
Args:
task (:obj:`Task`): task
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 @@ -91,9 +78,10 @@ def exec_sed_task(task, variables, log=None):
could not be recorded
:obj:`NotImplementedError`: if the task is not of a supported type or involves an unsuported feature
'''
config = get_config()
config = config or get_config()

log = log or TaskLog()
if config.LOG and not log:
log = TaskLog()

model = task.model
sim = task.simulation
Expand Down Expand Up @@ -129,7 +117,7 @@ def exec_sed_task(task, variables, log=None):

# determine the algorithm to execute
alg_kisao_id = sim.algorithm.kisao_id
exec_alg_kisao_id, alg_copasi_id = get_algorithm_id(alg_kisao_id, events=copasi_model.getNumEvents() >= 1)
exec_alg_kisao_id, alg_copasi_id = get_algorithm_id(alg_kisao_id, events=copasi_model.getNumEvents() >= 1, config=config)

# initialize COPASI task
copasi_task = copasi_data_model.getTask('Time-Course')
Expand All @@ -142,7 +130,7 @@ def exec_sed_task(task, variables, log=None):

# Apply the algorithm parameter changes specified by `simulation.algorithm_parameter_changes`
method_parameters = {}
algorithm_substitution_policy = get_algorithm_substitution_policy()
algorithm_substitution_policy = get_algorithm_substitution_policy(config=config)
if exec_alg_kisao_id == alg_kisao_id:
for change in sim.algorithm.changes:
try:
Expand Down Expand Up @@ -288,12 +276,13 @@ def exec_sed_task(task, variables, log=None):
))

# log action
log.algorithm = exec_alg_kisao_id
log.simulator_details = {
'methodName': KISAO_ALGORITHMS_MAP[exec_alg_kisao_id]['id'],
'methodCode': alg_copasi_id,
'parameters': method_parameters,
}
if config.LOG:
log.algorithm = exec_alg_kisao_id
log.simulator_details = {
'methodName': KISAO_ALGORITHMS_MAP[exec_alg_kisao_id]['id'],
'methodCode': alg_copasi_id,
'parameters': method_parameters,
}

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

from .data_model import KISAO_ALGORITHMS_MAP, KISAO_PARAMETERS_MAP, Units
from biosimulators_utils.config import Config # noqa: F401
from biosimulators_utils.data_model import ValueType
from biosimulators_utils.simulator.utils import get_algorithm_substitution_policy
from biosimulators_utils.utils.core import validate_str_value, parse_value
Expand All @@ -23,13 +24,14 @@
]


def get_algorithm_id(kisao_id, events=False):
def get_algorithm_id(kisao_id, events=False, config=None):
""" Get the COPASI id for an algorithm
Args:
kisao_id (:obj:`str`): KiSAO algorithm id
events (:obj:`bool`, optional): whether an algorithm that supports
events is needed
config (:obj:`Config`, optional): configuration
Returns:
:obj:`tuple`:
Expand All @@ -43,7 +45,7 @@ def get_algorithm_id(kisao_id, events=False):
if not events or props['supports_events']
]

substitution_policy = get_algorithm_substitution_policy()
substitution_policy = get_algorithm_substitution_policy(config=config)
try:
exec_kisao_id = get_preferred_substitute_algorithm_by_ids(
kisao_id, possible_alg_kisao_ids,
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
biosimulators_utils[logging,sbml] >= 0.1.104
biosimulators_utils[logging,sbml] >= 0.1.114
kisao
numpy
python_copasi >= 4.33.246
93 changes: 59 additions & 34 deletions tests/test_core_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from biosimulators_utils.combine import data_model as combine_data_model
from biosimulators_utils.combine.exceptions import CombineArchiveExecutionError
from biosimulators_utils.combine.io import CombineArchiveWriter
from biosimulators_utils.config import get_config
from biosimulators_utils.report import data_model as report_data_model
from biosimulators_utils.report.io import ReportReader
from biosimulators_utils.simulator.exec import exec_sedml_docs_in_archive_with_containerized_simulator
Expand Down Expand Up @@ -454,27 +455,36 @@ def test_exec_sed_task_copasi_error_handling(self):
_, log = exec_sed_task(task, variables)
self.assertEqual(log.algorithm, 'KISAO_0000304')

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

with self.assertRaises(CombineArchiveExecutionError):
with mock.patch.object(COPASI.CCopasiTask, 'processRaw', return_value=False):
exec_sedml_docs_in_combine_archive(archive_filename, out_dir,
report_formats=[
report_data_model.ReportFormat.h5,
report_data_model.ReportFormat.csv,
],
bundle_outputs=True,
keep_individual_outputs=True)
_, log = exec_sedml_docs_in_combine_archive(archive_filename, out_dir, config=config)
if log.exception:
raise log.exception

def test_exec_sedml_docs_in_combine_archive(self):
doc, archive_filename = self._build_combine_archive()

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

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

_, log = 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 @@ -487,14 +497,19 @@ def test_exec_sedml_docs_in_combine_archive_with_continuous_model_all_algorithms
var_targets=[None, 'A', 'C', 'DA'])

out_dir = os.path.join(self.dirname, alg.kisao_id)

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

try:
exec_sedml_docs_in_combine_archive(archive_filename, out_dir,
report_formats=[
report_data_model.ReportFormat.h5,
report_data_model.ReportFormat.csv,
],
bundle_outputs=True,
keep_individual_outputs=True)
_, log = 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)
except CombineArchiveExecutionError:
errored_algs.append(alg.kisao_id)
Expand All @@ -518,13 +533,18 @@ def test_exec_sedml_docs_in_combine_archive_with_stochastic_model_all_algorithms
var_targets=[None, 'Mdm2', 'p53', 'Mdm2_p53'])

out_dir = os.path.join(self.dirname, alg.kisao_id)
exec_sedml_docs_in_combine_archive(archive_filename, out_dir,
report_formats=[
report_data_model.ReportFormat.h5,
report_data_model.ReportFormat.csv,
],
bundle_outputs=True,
keep_individual_outputs=True)

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

_, log = 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)

def _build_combine_archive(self, algorithm=None, orig_model_filename='model.xml', var_targets=[None, 'A', 'C', 'DA']):
Expand Down Expand Up @@ -691,12 +711,17 @@ def _assert_combine_archive_outputs(self, doc, out_dir):
def test_exec_sedml_docs_in_combine_archive_real_example(self):
archive_filename = os.path.join(os.path.dirname(__file__), 'fixtures', 'Ciliberto-J-Cell-Biol-2003-morphogenesis-checkpoint.omex')
out_dir = os.path.join(self.dirname, 'out')
exec_sedml_docs_in_combine_archive(archive_filename, out_dir,
report_formats=[
report_data_model.ReportFormat.h5,
],
bundle_outputs=True,
keep_individual_outputs=False)

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

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

report = sedml_data_model.Report(
data_sets=[
Expand Down

0 comments on commit 2984470

Please sign in to comment.