Skip to content

Commit

Permalink
feat(cellml): add flag to skip over non observable variables in cellm…
Browse files Browse the repository at this point in the history
…l files

add a flag to  skips over variables in the cellML that have been labeled as `"public_interface"="in"` or `"private_interface"="in"` as these are not exposed by OpenCor after simulation. No information is lost from the reports, as these variables have a connection to another component that contains the same variable, with the public interface set to "out". Accessing the variable through that component works fine.
  • Loading branch information
bilalshaikh42 committed Apr 11, 2022
1 parent a152d78 commit 9355bad
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 7 deletions.
15 changes: 12 additions & 3 deletions biosimulators_utils/model_lang/cellml/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@


def get_parameters_variables_outputs_for_simulation(model_filename, model_language, simulation_type, algorithm_kisao_id=None,
change_level=SedDocument, native_ids=False, native_data_types=False,
change_level=SedDocument, native_ids=False, native_data_types=False, observable_only=False,
config=None):
""" Get the possible observables for a simulation of a model
Expand All @@ -37,6 +37,7 @@ def get_parameters_variables_outputs_for_simulation(model_filename, model_langua
native_ids (:obj:`bool`, optional): whether to return the raw id and name of each model component rather than the suggested name
for the variable of an associated SED-ML data generator
native_data_types (:obj:`bool`, optional): whether to return new_values in their native data types
observable_only (:obj:`bool`, optional): whether to skip the variables that are not exposed by OpenCor
config (:obj:`Config`, optional): whether to fail on missing includes
Returns:
Expand Down Expand Up @@ -66,15 +67,16 @@ def get_parameters_variables_outputs_for_simulation(model_filename, model_langua
or default_ns.startswith('http://www.cellml.org/cellml/1.1')
):
return get_parameters_variables_for_simulation_version_1(model, root, simulation_type, algorithm_kisao_id=algorithm_kisao_id,
native_ids=native_ids, native_data_types=native_data_types)
native_ids=native_ids, native_data_types=native_data_types,
observable_only=observable_only)

else:
return get_parameters_variables_for_simulation_version_2(model, root, simulation_type, algorithm_kisao_id=algorithm_kisao_id,
native_ids=native_ids, native_data_types=native_data_types)


def get_parameters_variables_for_simulation_version_1(model, xml_root, simulation_type,
algorithm_kisao_id=None, native_ids=False, native_data_types=False):
algorithm_kisao_id=None, native_ids=False, native_data_types=False, observable_only=False):
""" Get the possible observables for a simulation of a model
Args:
Expand All @@ -86,6 +88,7 @@ def get_parameters_variables_for_simulation_version_1(model, xml_root, simulatio
native_ids (:obj:`bool`, optional): whether to return the raw id and name of each model component rather than the suggested name
for the variable of an associated SED-ML data generator
native_data_types (:obj:`bool`, optional): whether to return new_values in their native data types
observable_only (:obj:`bool`, optional): whether to skip the variables that are not exposed by OpenCor
Returns:
:obj:`list` of :obj:`ModelAttributeChange`: possible attributes of a model that can be changed and their default values
Expand Down Expand Up @@ -124,6 +127,12 @@ def get_parameters_variables_for_simulation_version_1(model, xml_root, simulatio
component_name = component.attrib['name']
for variable in component.xpath('cellml:variable', namespaces=namespaces):
variable_name = variable.attrib['name']
public_interface_in = variable.attrib.get('public_interface', None) == 'in'
private_interface_in = variable.attrib.get('private_interface', None) == 'in'
variable_is_hidden = private_interface_in or public_interface_in

if(variable_is_hidden and observable_only):
continue
initial_value = variable.attrib.get('initial_value', None)
if initial_value is not None:
params.append(ModelAttributeChange(
Expand Down
92 changes: 88 additions & 4 deletions tests/model_lang/cellml/test_cellml_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,53 @@ def test_get_parameters_variables_for_simulation_version_1(self):
target_namespaces=namespaces,
)))

def test_get_parameters_variables_for_simulation_version_1_observable_only(self):
params, sims, vars, plots = get_parameters_variables_outputs_for_simulation(
self.V1_FIXTURE_FILENAME, None, OneStepSimulation, None, observable_only=True)
params, sims, vars, plots = get_parameters_variables_outputs_for_simulation(
self.V1_FIXTURE_FILENAME, None, UniformTimeCourseSimulation, None, observable_only=True)

self.assertEqual(len(params), 13)
self.assertEqual(len(vars), 30)

namespaces = {
'cellml': 'http://www.cellml.org/cellml/1.0#',
}

self.assertTrue(params[0].is_equal(ModelAttributeChange(
id='initial_value_component_total_cytoplasmic_Ca_flux_variable_F',
name='Initial value of variable "F" of component "total_cytoplasmic_Ca_flux"',
target=(
"/cellml:model"
"/cellml:component[@name='total_cytoplasmic_Ca_flux']"
"/cellml:variable[@name='F']"
"/@initial_value"
),
target_namespaces=namespaces,
new_value='96.5',
)))

self.assertEqual(len(sims), 1)

sim = sims[0]
self.assertIsInstance(sim, UniformTimeCourseSimulation)

self.assertTrue(vars[0].is_equal(Variable(
id='value_component_environment_variable_time',
name='Value of variable "time" of component "environment"',
target=(
"/cellml:model"
"/cellml:component[@name='environment']"
"/cellml:variable[@name='time']"
),
target_namespaces=namespaces,
)))

def test_get_parameters_variables_for_simulation_version_1_native_ids_data_types(self):
params, sims, vars, plots = get_parameters_variables_outputs_for_simulation(self.V1_FIXTURE_FILENAME, None, OneStepSimulation, None,
native_ids=True, native_data_types=True)
native_ids=True, native_data_types=True)
params, sims, vars, plots = get_parameters_variables_outputs_for_simulation(self.V1_FIXTURE_FILENAME, None, UniformTimeCourseSimulation, None,
native_ids=True, native_data_types=True)
native_ids=True, native_data_types=True)

self.assertEqual(len(params), 13)
self.assertEqual(len(vars), 54)
Expand Down Expand Up @@ -107,6 +149,48 @@ def test_get_parameters_variables_for_simulation_version_1_native_ids_data_types
target_namespaces=namespaces,
)))

def test_get_parameters_variables_for_simulation_version_1_native_ids_data_types_observable_only(self):
params, sims, vars, plots = get_parameters_variables_outputs_for_simulation(self.V1_FIXTURE_FILENAME, None, OneStepSimulation, None,
native_ids=True, native_data_types=True, observable_only=True)
params, sims, vars, plots = get_parameters_variables_outputs_for_simulation(self.V1_FIXTURE_FILENAME, None, UniformTimeCourseSimulation, None,
native_ids=True, native_data_types=True, observable_only=True)

self.assertEqual(len(params), 13)
self.assertEqual(len(vars), 30)

namespaces = {
'cellml': 'http://www.cellml.org/cellml/1.0#',
}

self.assertTrue(params[0].is_equal(ModelAttributeChange(
id='total_cytoplasmic_Ca_flux.F',
name=None,
target=(
"/cellml:model"
"/cellml:component[@name='total_cytoplasmic_Ca_flux']"
"/cellml:variable[@name='F']"
"/@initial_value"
),
target_namespaces=namespaces,
new_value=96.5,
)))

self.assertEqual(len(sims), 1)

sim = sims[0]
self.assertIsInstance(sim, UniformTimeCourseSimulation)

self.assertTrue(vars[0].is_equal(Variable(
id='environment.time',
name=None,
target=(
"/cellml:model"
"/cellml:component[@name='environment']"
"/cellml:variable[@name='time']"
),
target_namespaces=namespaces,
)))

def test_get_parameters_variables_for_simulation_version_2(self):
params, sims, vars, plots = get_parameters_variables_outputs_for_simulation(self.V2_FIXTURE_FILENAME, None, UniformTimeCourseSimulation, None)
params, sims, vars, plots = get_parameters_variables_outputs_for_simulation(self.V2_FIXTURE_FILENAME, None, OneStepSimulation, None)
Expand Down Expand Up @@ -149,9 +233,9 @@ def test_get_parameters_variables_for_simulation_version_2(self):

def test_get_parameters_variables_for_simulation_version_2_native_ids_data_types(self):
params, sims, vars, plots = get_parameters_variables_outputs_for_simulation(self.V2_FIXTURE_FILENAME, None, UniformTimeCourseSimulation, None,
native_ids=True, native_data_types=True)
native_ids=True, native_data_types=True)
params, sims, vars, plots = get_parameters_variables_outputs_for_simulation(self.V2_FIXTURE_FILENAME, None, OneStepSimulation, None,
native_ids=True, native_data_types=True)
native_ids=True, native_data_types=True)

self.assertEqual(len(params), 1)
self.assertEqual(len(vars), 3)
Expand Down

0 comments on commit 9355bad

Please sign in to comment.