Skip to content

Commit

Permalink
chore: debugged model changing
Browse files Browse the repository at this point in the history
  • Loading branch information
jonrkarr committed Sep 11, 2021
1 parent e9fba6d commit 4a12617
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 32 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

FROM python:3.9-slim-buster

ARG VERSION="0.1.14"
ARG VERSION="0.1.15"
ARG SIMULATOR_VERSION=2.6.0

# metadata
Expand Down
2 changes: 1 addition & 1 deletion biosimulators_bionetgen/_version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '0.1.14'
__version__ = '0.1.15'
14 changes: 12 additions & 2 deletions biosimulators_bionetgen/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"""

from .io import read_task
from .utils import (exec_bionetgen_task, preprocess_model_attribute_change, add_model_attribute_change_to_task, add_simulation_to_task,
from .utils import (exec_bionetgen_task, preprocess_model_attribute_change, add_model_attribute_change_to_task,
create_actions_for_simulation,
get_variables_results_from_observable_results, add_variables_to_model)
from .warnings import IgnoredBnglFileContentWarning
from biosimulators_utils.combine.exec import exec_sedml_docs_in_archive
Expand All @@ -21,6 +22,7 @@
UniformTimeCourseSimulation, Variable)
from biosimulators_utils.sedml.exec import exec_sed_doc as base_exec_sed_doc
from biosimulators_utils.utils.core import raise_errors_warnings
import copy
import warnings

__all__ = ['exec_sedml_docs_in_combine_archive', 'exec_sed_doc', 'exec_sed_task', 'preprocess_sed_task']
Expand Down Expand Up @@ -145,6 +147,8 @@ def exec_sed_task(task, variables, preprocessed_task=None, log=None, config=None

# read the model from the BNGL file
bionetgen_task = preprocessed_task['bionetgen_task']
preprocessed_actions = bionetgen_task.actions
bionetgen_task.actions = copy.deepcopy(preprocessed_actions)

# validate and apply the model attribute changes to the BioNetGen task
for change in task.model.changes:
Expand All @@ -154,6 +158,8 @@ def exec_sed_task(task, variables, preprocessed_task=None, log=None, config=None
alg_kisao_id = preprocessed_task['algorithm_kisao_id']

# execute the task
bionetgen_task.actions.extend(preprocessed_task['simulation_actions'])

observable_results = exec_bionetgen_task(bionetgen_task)

# get predicted values of the variables
Expand All @@ -168,6 +174,9 @@ def exec_sed_task(task, variables, preprocessed_task=None, log=None, config=None
'actions': bionetgen_task.actions,
}

# clean up
bionetgen_task.actions = preprocessed_actions

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

Expand Down Expand Up @@ -226,11 +235,12 @@ def preprocess_sed_task(task, variables, config=None):
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, task.simulation)
simulation_actions, alg_kisao_id = create_actions_for_simulation(task.simulation)

# return the values of the variables and log
return {
'bionetgen_task': bionetgen_task,
'model_changes': model_changes,
'simulation_actions': simulation_actions,
'algorithm_kisao_id': alg_kisao_id,
}
29 changes: 17 additions & 12 deletions biosimulators_bionetgen/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
'preprocess_model_attribute_change',
'add_model_attribute_change_to_task',
'add_variables_to_model',
'add_simulation_to_task',
'create_actions_for_simulation',
'exec_bionetgen_task',
'get_variables_results_from_observable_results',
]
Expand Down Expand Up @@ -70,7 +70,8 @@ def preprocess_model_attribute_change(task, change):
'type': 'replace_line_in_block',
'block': block,
'i_line': i_line,
'new_line': lambda new_value: '{} {} {} {}'.format(obj_id, match.group(1), new_value, (match.group(3) or '').strip()).strip(),
'new_line': lambda new_value: '{} {} {} {}'.format(
obj_id, match.group(1), new_value, (match.group(3) or '').strip()).strip(),
}

if not comp_changed:
Expand Down Expand Up @@ -241,11 +242,10 @@ def add_variables_to_model(model, variables):
raise NotImplementedError(msg)


def add_simulation_to_task(task, simulation, config=None):
""" Add a SED simulation to a BioNetGen task
def create_actions_for_simulation(simulation, config=None):
""" Create BioNetGen actions for a SED simulation
Args:
task (:obj:`Task`): BioNetGen task
simulation (:obj:`UniformTimeCourseSimulation`): SED simulation
config (:obj:`Config`, optional): configuration
Expand All @@ -254,7 +254,10 @@ def add_simulation_to_task(task, simulation, config=None):
algorithm parameters
Returns:
:obj:`str`: KiSAO id of the algorithm that will be executed
:obj:`tuple`:
* :obj:`list` of :obj:`str`: actions for SED simulation
* :obj:`str`: KiSAO id of the algorithm that will be executed
"""
simulate_args = OrderedDict()

Expand Down Expand Up @@ -311,15 +314,17 @@ def add_simulation_to_task(task, simulation, config=None):
])
warn(msg, BioSimulatorsWarning)

# if necessary add network generation to the BioNetGen task
# if necessary create a network generation action
actions = []

if simulation_method['generate_network']:
task.actions.append("generate_network({overwrite => 1})")
actions.append("generate_network({overwrite => 1})")

# add the simulation to the BioNetGen task
task.actions.append('simulate({{{}}})'.format(', '.join('{} => {}'.format(key, val) for key, val in simulate_args.items())))
# create a simulation action
actions.append('simulate({{{}}})'.format(', '.join('{} => {}'.format(key, val) for key, val in simulate_args.items())))

# return the KiSAO id of the algorithm that will be executed
return exec_kisao_id
# return actions and the KiSAO id of the algorithm that will be executed
return actions, exec_kisao_id


def exec_bionetgen_task(task):
Expand Down
28 changes: 24 additions & 4 deletions tests/test_core_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from biosimulators_utils.simulator.exec import exec_sedml_docs_in_archive_with_containerized_simulator
from biosimulators_utils.simulator.specs import gen_algorithms_from_specs
from unittest import mock
import copy
import datetime
import dateutil.tz
import numpy
Expand Down Expand Up @@ -52,6 +53,25 @@ def test_exec_sed_task(self):
self.assertEqual(variable_results['var_time'].size, sim.number_of_points + 1)
numpy.testing.assert_allclose(variable_results['var_time'],
numpy.linspace(sim.output_start_time, sim.output_end_time, sim.number_of_points + 1))
numpy.testing.assert_allclose(variable_results['var_A'][0], 4, rtol=1e-1)

doc2 = copy.deepcopy(doc)
doc2.tasks[0].model.changes.append(sedml_data_model.ModelAttributeChange(
target='species.A().initialCount',
new_value='5',
))
variable_results_2, _ = exec_sed_task(doc2.tasks[0], variables)
numpy.testing.assert_allclose(variable_results_2['var_A'][0], 5, rtol=1e-1)
self.assertGreater(variable_results_2['var_A'][0], variable_results['var_A'][0])

doc3 = copy.deepcopy(doc)
doc3.tasks[0].model.changes.append(sedml_data_model.ModelAttributeChange(
target='species.A().initialCount',
new_value=6,
))
variable_results_3, _ = exec_sed_task(doc3.tasks[0], variables)
numpy.testing.assert_allclose(variable_results_3['var_A'][0], 6, rtol=1e-1)
self.assertGreater(variable_results_3['var_A'][0], variable_results_2['var_A'][0])

def test_exec_sed_task_non_zero_initial_time(self):
doc = self._build_sed_doc()
Expand All @@ -75,7 +95,7 @@ def test_exec_sedml_docs_in_combine_archive(self):
out_dir = os.path.join(self.dirname, 'out')

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

Expand All @@ -92,7 +112,7 @@ def test_exec_sedml_docs_in_combine_archive_with_all_algorithms(self):
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.REPORT_FORMATS = [report_data_model.ReportFormat.h5, report_data_model.ReportFormat.csv]
config.BUNDLE_OUTPUTS = True
config.KEEP_INDIVIDUAL_OUTPUTS = True

Expand Down Expand Up @@ -171,8 +191,8 @@ def _build_sed_doc(self, algorithm=None):
changes=[
sedml_data_model.ModelAttributeChange(target='functions.gfunc.expression', new_value='0.5*Atot^2/(10 + Atot^2)'),
sedml_data_model.ModelAttributeChange(target='functions.gfunc().expression', new_value='0.5*Atot^2/(10 + Atot^2)'),
sedml_data_model.ModelAttributeChange(target='species.A().initialCount', new_value='5'),
sedml_data_model.ModelAttributeChange(target='parameters.g1.value', new_value='16.0'),
sedml_data_model.ModelAttributeChange(target='species.A().initialCount', new_value='4'),
sedml_data_model.ModelAttributeChange(target='parameters.g1.value', new_value='18.0'),
],
))
doc.simulations.append(sedml_data_model.UniformTimeCourseSimulation(
Expand Down
27 changes: 15 additions & 12 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from biosimulators_bionetgen.data_model import Task
from biosimulators_bionetgen.utils import (add_model_attribute_change_to_task,
add_variables_to_model,
add_simulation_to_task,
create_actions_for_simulation,
exec_bionetgen_task,
get_variables_results_from_observable_results,)
from biosimulators_bionetgen.io import read_task, read_simulation_results, write_task
Expand Down Expand Up @@ -63,6 +63,10 @@ def test_add_model_attribute_change_to_task(self):
add_model_attribute_change_to_task(task, change)
self.assertEqual(task.actions[-1], 'setParameter("k_1", 1.0)')

change = ModelAttributeChange(target='parameters.k_1.value', new_value=2.0)
add_model_attribute_change_to_task(task, change)
self.assertEqual(task.actions[-1], 'setParameter("k_1", 2.0)')

change = ModelAttributeChange(target='species.GeneA_00().initialCount', new_value='1')
add_model_attribute_change_to_task(task, change)
self.assertEqual(task.actions[-1], 'setConcentration("GeneA_00()", 1)')
Expand Down Expand Up @@ -140,9 +144,8 @@ def test_add_variables_to_task(self):
with self.assertRaisesRegex(NotImplementedError, 'targets are not supported'):
add_variables_to_model(task.model, [Variable(id='X', target='x')])

def test_add_simulation_to_task(self):
def test_create_actions_for_simulation(self):
# CVODE
task = Task()
simulation = UniformTimeCourseSimulation(
initial_time=0.,
output_start_time=10.,
Expand All @@ -155,44 +158,44 @@ def test_add_simulation_to_task(self):
]
),
)
add_simulation_to_task(task, simulation)
self.assertEqual(task.actions, [
actions, _ = create_actions_for_simulation(simulation)
self.assertEqual(actions, [
'generate_network({overwrite => 1})',
'simulate({t_start => 0.0, t_end => 20.0, n_steps => 20, method => "ode", atol => 1e-6})',
])

# Error handling: non-integer steps
simulation.output_end_time = 20.1
with self.assertRaisesRegex(NotImplementedError, 'must specify an integer number of steps'):
add_simulation_to_task(task, simulation)
create_actions_for_simulation(simulation)

# Error handling: non-zero initial time
simulation.output_end_time = 20.0
simulation.initial_time = 5.
simulation.algorithm.kisao_id = 'KISAO_0000019'
add_simulation_to_task(task, simulation)
create_actions_for_simulation(simulation)

simulation.initial_time = 5.
simulation.algorithm.kisao_id = 'KISAO_0000263'
with self.assertRaisesRegex(NotImplementedError, 'must be 0'):
add_simulation_to_task(task, simulation)
create_actions_for_simulation(simulation)

# Error handling: unknown algorithm
simulation.algorithm.kisao_id = 'KISAO_0000448'
with self.assertRaisesRegex(AlgorithmCannotBeSubstitutedException, 'No algorithm can be substituted'):
add_simulation_to_task(task, simulation)
create_actions_for_simulation(simulation)

# Error handling: unknown algorithm parameter
simulation.algorithm.kisao_id = 'KISAO_0000019'
simulation.algorithm.changes[0].kisao_id = 'KISAO_0000001'

with mock.patch.dict('os.environ', {'ALGORITHM_SUBSTITUTION_POLICY': 'NONE'}):
with self.assertRaisesRegex(NotImplementedError, 'is not supported. Parameter must have'):
add_simulation_to_task(task, simulation)
create_actions_for_simulation(simulation)

with mock.patch.dict('os.environ', {'ALGORITHM_SUBSTITUTION_POLICY': 'SIMILAR_VARIABLES'}):
with pytest.warns(BioSimulatorsWarning, match='is not supported. Parameter must have'):
add_simulation_to_task(task, simulation)
create_actions_for_simulation(simulation)

def test_exec_bionetgen_task(self):
model_filename = os.path.join(os.path.dirname(__file__), 'fixtures', 'test.bngl')
Expand Down Expand Up @@ -273,7 +276,7 @@ def test_get_parameters_variables_outputs_for_simulation(self):
model_filename_2 = os.path.join(self.dirname, 'task.bngl')
write_task(task, model_filename_2)

changes_2, sim, variables_2,plots_2 = get_parameters_variables_outputs_for_simulation(
changes_2, sim, variables_2, plots_2 = get_parameters_variables_outputs_for_simulation(
model_filename_2, None, UniformTimeCourseSimulation, None)
for change, change_2 in zip(changes, changes_2):
self.assertTrue(change_2.is_equal(change))
Expand Down

0 comments on commit 4a12617

Please sign in to comment.