Skip to content

Commit

Permalink
feat: updating to biosimulators-utils 0.1.116
Browse files Browse the repository at this point in the history
  • Loading branch information
jonrkarr committed Sep 3, 2021
1 parent b26f39a commit b8a5f19
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 32 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ jobs:
- name: Run the tests
env:
BIONETGEN_PATH: /opt/BioNetGen/BNG2.pl
run: python -m pytest tests/ --cov=./ --cov-report=xml
run: python -m pytest tests/ --cov=./biosimulators_bionetgen/ --cov-report=xml

- name: Upload the coverage report to Codecov
uses: codecov/[email protected]
Expand Down
4 changes: 3 additions & 1 deletion biosimulators_bionetgen/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# :obj:`str`: version

from .config import Config
from .core import exec_sed_task, exec_sedml_docs_in_combine_archive # noqa: F401
from .core import exec_sed_task, preprocess_sed_task, exec_sed_doc, exec_sedml_docs_in_combine_archive # noqa: F401

import subprocess

Expand All @@ -11,6 +11,8 @@
'__version__',
'get_simulator_version',
'exec_sed_task',
'preprocess_sed_task',
'exec_sed_doc',
'exec_sedml_docs_in_combine_archive',
]

Expand Down
123 changes: 94 additions & 29 deletions biosimulators_bionetgen/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,17 @@
from .warnings import IgnoredBnglFileContentWarning
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 # 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
from biosimulators_utils.sedml import validation
from biosimulators_utils.sedml.data_model import (Task, ModelLanguage, ModelAttributeChange, # noqa: F401
UniformTimeCourseSimulation, Variable)
from biosimulators_utils.sedml.exec import exec_sed_doc
from biosimulators_utils.sedml.exec import exec_sed_doc as base_exec_sed_doc
from biosimulators_utils.utils.core import raise_errors_warnings
import functools
import warnings

__all__ = ['exec_sedml_docs_in_combine_archive', 'exec_sed_task']
__all__ = ['exec_sedml_docs_in_combine_archive', 'exec_sed_doc', 'exec_sed_task', 'preprocess_sed_task']


def exec_sedml_docs_in_combine_archive(archive_filename, out_dir, config=None):
Expand All @@ -47,20 +46,65 @@ def exec_sedml_docs_in_combine_archive(archive_filename, out_dir, config=None):
* :obj:`SedDocumentResults`: results
* :obj:`CombineArchiveLog`: log
"""
sed_doc_executer = functools.partial(exec_sed_doc, exec_sed_task)
return exec_sedml_docs_in_archive(sed_doc_executer, archive_filename, out_dir,
return exec_sedml_docs_in_archive(exec_sed_doc, archive_filename, out_dir,
apply_xml_model_changes=False,
config=config)


def exec_sed_task(sed_task, variables, log=None, config=None):
def exec_sed_doc(doc, working_dir, base_out_path, rel_out_path=None,
apply_xml_model_changes=False,
log=None, indent=0, pretty_print_modified_xml_models=False,
log_level=StandardOutputErrorCapturerLevel.c, config=None):
""" Execute the tasks specified in a SED document and generate the specified outputs
Args:
doc (:obj:`SedDocument` or :obj:`str`): SED document or a path to SED-ML file which defines a SED document
working_dir (:obj:`str`): working directory of the SED document (path relative to which models are located)
base_out_path (:obj:`str`): path to store the outputs
* CSV: directory in which to save outputs to files
``{base_out_path}/{rel_out_path}/{report.id}.csv``
* HDF5: directory in which to save a single HDF5 file (``{base_out_path}/reports.h5``),
with reports at keys ``{rel_out_path}/{report.id}`` within the HDF5 file
rel_out_path (:obj:`str`, optional): path relative to :obj:`base_out_path` to store the outputs
apply_xml_model_changes (:obj:`bool`, optional): if :obj:`True`, apply any model changes specified in the SED-ML file before
calling :obj:`task_executer`.
log (:obj:`SedDocumentLog`, optional): log of the document
indent (:obj:`int`, optional): degree to indent status messages
pretty_print_modified_xml_models (:obj:`bool`, optional): if :obj:`True`, pretty print modified XML models
log_level (:obj:`StandardOutputErrorCapturerLevel`, optional): level at which to log output
config (:obj:`Config`, optional): BioSimulators common configuration
simulator_config (:obj:`SimulatorConfig`, optional): tellurium configuration
Returns:
:obj:`tuple`:
* :obj:`ReportResults`: results of each report
* :obj:`SedDocumentLog`: log of the document
"""
return base_exec_sed_doc(exec_sed_task, doc, working_dir, base_out_path,
rel_out_path=rel_out_path,
apply_xml_model_changes=apply_xml_model_changes,
log=log,
indent=indent,
pretty_print_modified_xml_models=pretty_print_modified_xml_models,
log_level=log_level,
config=config)


def exec_sed_task(task, variables, preprocessed_task=None, log=None, config=None):
""" Execute a task and save its results
Args:
sed_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
task (:obj:`Task`): SED task
variables (:obj:`list` of :obj:`Variable`): variables that should be recorded
preprocessed_task (:obj:`object`, optional): preprocessed information about the task, including possible
model changes and variables. This can be used to avoid repeatedly executing the same initialization
for repeated calls to this method.
log (:obj:`TaskLog`, optional): log for the task
config (:obj:`Config`, optional): BioSimulators common configuration
Returns:
:obj:`tuple`:
Expand Down Expand Up @@ -95,54 +139,57 @@ def exec_sed_task(sed_task, variables, log=None, config=None):
if config.LOG and not log:
log = TaskLog()

if preprocessed_task is None:
preprocessed_task = preprocess_sed_task(task, variables, config=config)

if config.VALIDATE_SEDML:
raise_errors_warnings(
validation.validate_task(sed_task),
error_summary='Task `{}` is invalid.'.format(sed_task.id))
validation.validate_task(task),
error_summary='Task `{}` is invalid.'.format(task.id))
raise_errors_warnings(
validation.validate_model_language(sed_task.model.language, ModelLanguage.BNGL),
error_summary='Language for model `{}` is not supported.'.format(sed_task.model.id))
validation.validate_model_language(task.model.language, ModelLanguage.BNGL),
error_summary='Language for model `{}` is not supported.'.format(task.model.id))
raise_errors_warnings(
validation.validate_model_change_types(sed_task.model.changes, (ModelAttributeChange, )),
error_summary='Changes for model `{}` are not supported.'.format(sed_task.model.id))
validation.validate_model_change_types(task.model.changes, (ModelAttributeChange, )),
error_summary='Changes for model `{}` are not supported.'.format(task.model.id))
raise_errors_warnings(
*validation.validate_model_changes(sed_task.model),
error_summary='Changes for model `{}` are invalid.'.format(sed_task.model.id))
*validation.validate_model_changes(task.model),
error_summary='Changes for model `{}` are invalid.'.format(task.model.id))
raise_errors_warnings(
validation.validate_simulation_type(sed_task.simulation, (UniformTimeCourseSimulation, )),
validation.validate_simulation_type(task.simulation, (UniformTimeCourseSimulation, )),
error_summary='{} `{}` is not supported.'.format(
sed_task.simulation.__class__.__name__,
sed_task.simulation.id))
task.simulation.__class__.__name__,
task.simulation.id))
raise_errors_warnings(
*validation.validate_simulation(sed_task.simulation),
error_summary='Simulation `{}` is invalid.'.format(sed_task.simulation.id))
*validation.validate_simulation(task.simulation),
error_summary='Simulation `{}` is invalid.'.format(task.simulation.id))
raise_errors_warnings(
*validation.validate_data_generator_variables(variables),
error_summary='Data generator variables for task `{}` are invalid.'.format(sed_task.id))
error_summary='Data generator variables for task `{}` are invalid.'.format(task.id))

# read the model from the BNGL file
bionetgen_task = read_task(sed_task.model.source)
bionetgen_task = read_task(task.model.source)
if bionetgen_task.actions:
warnings.warn('Actions in the BNGL file were ignored.', IgnoredBnglFileContentWarning)
bionetgen_task.actions = []

# validate and apply the model attribute changes to the BioNetGen task
for change in sed_task.model.changes:
for change in task.model.changes:
add_model_attribute_change_to_task(bionetgen_task, change)

# add observables for the variables to the BioNetGen model
add_variables_to_model(bionetgen_task.model, variables)

# apply the SED algorithm and its parameters to the BioNetGen task
alg_kisao_id = add_simulation_to_task(bionetgen_task, sed_task.simulation)
alg_kisao_id = add_simulation_to_task(bionetgen_task, task.simulation)

# execute the task
observable_results = exec_bionetgen_task(bionetgen_task)

# get predicted values of the variables
variable_results = get_variables_results_from_observable_results(observable_results, variables)
for key in variable_results.keys():
variable_results[key] = variable_results[key][-(sed_task.simulation.number_of_points + 1):]
variable_results[key] = variable_results[key][-(task.simulation.number_of_points + 1):]

# log action
if config.LOG:
Expand All @@ -153,3 +200,21 @@ def exec_sed_task(sed_task, variables, log=None, config=None):

# return the values of the variables and log
return variable_results, log


def preprocess_sed_task(task, variables, config=None):
""" Preprocess a SED task, including its possible model changes and variables. This is useful for avoiding
repeatedly initializing tasks on repeated calls of :obj:`exec_sed_task`.
Args:
task (:obj:`Task`): task
variables (:obj:`list` of :obj:`Variable`): variables that should be recorded
preprocessed_task (:obj:`PreprocessedTask`, optional): preprocessed information about the task, including possible
model changes and variables. This can be used to avoid repeatedly executing the same initialization for repeated
calls to this method.
config (:obj:`Config`, optional): BioSimulators common configuration
Returns:
:obj:`object`: preprocessed information about the task
"""
pass
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
biosimulators_utils[bngl,logging] >= 0.1.114
biosimulators_utils[bngl,logging] >= 0.1.116
kisao
pandas

0 comments on commit b8a5f19

Please sign in to comment.