Skip to content

Commit

Permalink
fix: fixed handling of duplicately defined XPP auxiliary variables
Browse files Browse the repository at this point in the history
  • Loading branch information
jonrkarr committed Feb 27, 2022
1 parent c051c37 commit 06401a2
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
21 changes: 19 additions & 2 deletions biosimulators_utils/model_lang/xpp/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from ...config import Config # noqa: F401
from .data_model import SIMULATION_METHOD_KISAO_MAP
from biosimulators_utils.sedml.data_model import Symbol
import collections
import glob
import os
Expand Down Expand Up @@ -129,8 +130,10 @@ def validate_model(filename,
'output': {},
'ui': {},
'other': {},
'outfile_column_names': [Symbol.time.value],
}
block = None
duplicate_ids = set()
with open(var_param_filename, 'r') as file:
for line in file:
line = line.strip()
Expand All @@ -141,7 +144,20 @@ def validate_model(filename,
block = 'initial_conditions'
elif block:
id, _, value = line.partition(' ')
simulation[block][id] = float(value)
if id in simulation[block]:
duplicate_ids.add("{} '{}'".format(block[0:1].upper() + block[1:].replace('_', ' '), id))
else:
simulation[block][id] = float(value)

if block == 'initial_conditions':
simulation['outfile_column_names'].append(id)

if duplicate_ids:
msg = '{} parameters and variables were duplicately defined:\n - {}'.format(
len(duplicate_ids),
'\n - '.join(sorted(duplicate_ids)),
)
warnings.append([msg])

parameter_ids = {key.lower(): key for key in simulation['parameters'].keys()}
variable_ids = {key.upper(): key for key in simulation['initial_conditions'].keys()}
Expand Down Expand Up @@ -169,7 +185,8 @@ def validate_model(filename,
name, _, start_end = name[0:-1].partition('[')
start, _, end = start_end.partition('..')
for i_var in range(int(start), int(end) + 1):
simulation['auxiliary_variables'][name + str(i_var)] = expr.replace('[j]', f'[{i_var}]')
el_name = name + str(i_var)
simulation['auxiliary_variables'][el_name] = expr.replace('[j]', f'[{i_var}]')

else:
simulation['auxiliary_variables'][name] = expr
Expand Down
9 changes: 9 additions & 0 deletions tests/model_lang/xpp/test_xpp_validation.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from biosimulators_utils.model_lang.xpp import validation
from biosimulators_utils.sedml.data_model import Symbol
from biosimulators_utils.utils.core import flatten_nested_list_of_strings
from unittest import mock
import collections
Expand Down Expand Up @@ -40,6 +41,7 @@ def test(self):
('U', .1),
('V', .05),
])
outfile_column_names = [Symbol.time.value, 'U', 'V']
simulation_method = {
'total': '40',
}
Expand Down Expand Up @@ -74,6 +76,7 @@ def test(self):
'output': None,
'ui': None,
'other': None,
'outfile_column_names': outfile_column_names,
})
self.assertEqual(model['initial_conditions'], initial_conditions)

Expand Down Expand Up @@ -243,6 +246,7 @@ def test_from_directory(self):
('U', .2),
('V', .1),
])
outfile_column_names = [Symbol.time.value, 'U', 'V']
simulation_method = {
'total': '40',
}
Expand Down Expand Up @@ -282,6 +286,7 @@ def test_from_directory(self):
'output': None,
'ui': None,
'other': None,
'outfile_column_names': outfile_column_names,
})

def test_with_sets(self):
Expand All @@ -308,6 +313,7 @@ def test_with_sets(self):
('U', .3),
('V', .05),
])
outfile_column_names = [Symbol.time.value, 'U', 'V']
sets = {
'set1': {
'parameters': {'aee': 12.},
Expand Down Expand Up @@ -356,6 +362,7 @@ def test_with_sets(self):
'output': None,
'ui': None,
'other': None,
'outfile_column_names': outfile_column_names,
})

def test_from_directory_2(self):
Expand All @@ -382,6 +389,7 @@ def test_from_directory_2(self):
('U', .1),
('V', .04),
])
outfile_column_names = [Symbol.time.value, 'U', 'V']
sets = {}
simulation_method = {
'total': '40',
Expand Down Expand Up @@ -436,6 +444,7 @@ def test_from_directory_2(self):
'output': None,
'ui': None,
'other': None,
'outfile_column_names': outfile_column_names,
})

def test_cannot_parse_other_formats(self):
Expand Down

0 comments on commit 06401a2

Please sign in to comment.