Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add assert_model_parameters_in_condition_or_parameter_table (#43) #62

Merged
merged 1 commit into from
Feb 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions petab/lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import re
import copy
import logging
import libsbml
import pandas as pd

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -360,6 +362,18 @@ def lint_problem(problem: 'core.Problem'):
logger.error(e)
errors_occurred = True

if problem.sbml_model is not None and problem.condition_df is not None \
and problem.parameter_df is not None:
try:
assert_model_parameters_in_condition_or_parameter_table(
problem.sbml_model,
problem.condition_df,
problem.parameter_df
)
except AssertionError as e:
logger.error(e)
errors_occurred = True

if errors_occurred:
logger.error('Not OK')
elif problem.measurement_df is None or problem.condition_df is None \
Expand All @@ -370,3 +384,44 @@ def lint_problem(problem: 'core.Problem'):
logger.info('OK')

return errors_occurred


def assert_model_parameters_in_condition_or_parameter_table(
sbml_model: libsbml.Model,
condition_df: pd.DataFrame,
parameter_df: pd.DataFrame):
"""Model non-placeholder model parameters must be either specified in the
condition or parameter table, unless they are AssignmentRule target, in
which case they must not occur in either.
Check that.

NOTE: SBML local parameters are ignored here"""

for parameter in sbml_model.getListOfParameters():
parameter_id = parameter.getId()

if parameter_id.startswith('observableParameter'):
continue
if parameter_id.startswith('noiseParameter'):
continue

is_assignee = \
sbml_model.getAssignmentRuleByVariable(parameter_id) is not None
in_parameter_df = parameter_id in parameter_df.index
in_condition_df = parameter_id in condition_df.columns

if is_assignee and (in_parameter_df or in_condition_df):
raise AssertionError(f"Model parameter '{parameter_id}' is target "
"of AssignmentRule, and thus, must not be "
"present in condition table or in parameter "
"table.")

if not is_assignee and not in_parameter_df and not in_condition_df:
raise AssertionError(f"Model parameter '{parameter_id}' neither "
"present in condition table nor in parameter "
"table.")

if in_parameter_df and in_condition_df:
raise AssertionError(f"Model parameter '{parameter_id}' present "
"in both condition table and parameter "
"table.")
39 changes: 38 additions & 1 deletion tests/test_lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import pandas as pd

sys.path.append(os.getcwd())
from petab import lint # noqa: E402
from petab import (lint, sbml) # noqa: E402


def test_assert_measured_observables_present_in_model():
Expand Down Expand Up @@ -147,3 +147,40 @@ def test_assert_no_leading_trailing_whitespace():

lint.assert_no_leading_trailing_whitespace(
test_df['testNone'].values, "testNone")


def test_assert_model_parameters_in_condition_or_parameter_table():
document = libsbml.SBMLDocument(3, 1)
model = document.createModel()
model.setTimeUnits("second")
model.setExtentUnits("mole")
model.setSubstanceUnits('mole')
sbml.add_global_parameter(model, 'parameter1')
sbml.add_global_parameter(model, 'noiseParameter1_')
sbml.add_global_parameter(model, 'observableParameter1_')

with pytest.raises(AssertionError):
lint.assert_model_parameters_in_condition_or_parameter_table(
model, pd.DataFrame(), pd.DataFrame())

lint.assert_model_parameters_in_condition_or_parameter_table(
model, pd.DataFrame(columns=['parameter1']), pd.DataFrame()
)

lint.assert_model_parameters_in_condition_or_parameter_table(
model, pd.DataFrame(), pd.DataFrame(index=['parameter1']))

with pytest.raises(AssertionError):
lint.assert_model_parameters_in_condition_or_parameter_table(
model,
pd.DataFrame(columns=['parameter1']),
pd.DataFrame(index=['parameter1']))

with pytest.raises(AssertionError):
lint.assert_model_parameters_in_condition_or_parameter_table(
model, pd.DataFrame(), pd.DataFrame())

sbml.create_assigment_rule(model, assignee_id='parameter1',
formula='parameter2')
lint.assert_model_parameters_in_condition_or_parameter_table(
model, pd.DataFrame(), pd.DataFrame())