Skip to content

Commit 4d623ec

Browse files
committed
feat: enabling simulations with positive initial times
1 parent 827e4cd commit 4d623ec

File tree

5 files changed

+97
-8
lines changed

5 files changed

+97
-8
lines changed

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Base OS
22
FROM python:3.9-slim-buster
33

4-
ARG VERSION=0.1.30
4+
ARG VERSION=0.1.31
55
ARG SIMULATOR_VERSION="1.6.2"
66

77
# metadata
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = '0.1.30'
1+
__version__ = '0.1.31'

biosimulators_gillespy2/core.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,16 +173,18 @@ def exec_sed_task(task, variables, log=None):
173173
warn(msg, BioSimulatorsWarning)
174174

175175
# Validate that start time is 0 because this is the only option that GillesPy2 supports
176-
if simulation.initial_time != 0:
177-
raise NotImplementedError('Initial simulation time {} is not supported. Initial time must be 0.'.format(simulation.initial_time))
176+
if simulation.initial_time < 0:
177+
raise NotImplementedError(
178+
'Negative initial simulation time {} is not supported. Initial time must be >= 0.'.format(simulation.initial_time))
178179

179180
# set the simulation time span
180181
number_of_points = (simulation.output_end_time - simulation.initial_time) / \
181182
(simulation.output_end_time - simulation.output_start_time) * simulation.number_of_points
182183
if number_of_points != math.floor(number_of_points):
183184
raise NotImplementedError('Time course must specify an integer number of time points')
184185
number_of_points = int(number_of_points)
185-
model.timespan(numpy.linspace(simulation.initial_time, simulation.output_end_time, number_of_points + 1))
186+
timespan = numpy.linspace(simulation.initial_time, simulation.output_end_time, number_of_points + 1)
187+
model.timespan(timespan)
186188

187189
# determine allowed variable targets
188190
predicted_ids = list(model.get_all_species().keys()) + list(model.get_all_parameters().keys())

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
biosimulators_utils[logging] >= 0.1.107
2-
gillespy2 >= 1.5.11
2+
gillespy2 >= 1.6.0
33
kisao
44
lxml # optional dependency of python_libsbml
55
numpy

tests/test_core_main.py

Lines changed: 89 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,93 @@ def test_exec_sed_task(self):
134134
numpy.full((task.simulation.number_of_points + 1,), 1.)
135135
)
136136

137+
def test_exec_sed_task_positive_initial_time(self):
138+
task = sedml_data_model.Task(
139+
model=sedml_data_model.Model(
140+
source=os.path.join(os.path.dirname(__file__), 'fixtures', 'BIOMD0000000297.edited', 'ex1', 'BIOMD0000000297.xml'),
141+
language=sedml_data_model.ModelLanguage.SBML.value,
142+
changes=[],
143+
),
144+
simulation=sedml_data_model.UniformTimeCourseSimulation(
145+
algorithm=sedml_data_model.Algorithm(
146+
kisao_id='KISAO_0000088',
147+
),
148+
initial_time=10.,
149+
output_start_time=10.,
150+
output_end_time=30.,
151+
number_of_points=20,
152+
),
153+
)
154+
155+
variables = [
156+
sedml_data_model.Variable(
157+
id='time',
158+
symbol=sedml_data_model.Symbol.time,
159+
task=task,
160+
),
161+
sedml_data_model.Variable(
162+
id='BE',
163+
target="/sbml:sbml/sbml:model/sbml:listOfSpecies/sbml:species[@id='BE']",
164+
target_namespaces=self.NAMESPACES,
165+
task=task,
166+
),
167+
]
168+
169+
variable_results, _ = core.exec_sed_task(task, variables, TaskLog())
170+
171+
self.assertTrue(sorted(variable_results.keys()), sorted([var.id for var in variables]))
172+
self.assertEqual(variable_results[variables[0].id].shape, (task.simulation.number_of_points + 1,))
173+
numpy.testing.assert_almost_equal(
174+
variable_results['time'],
175+
numpy.linspace(task.simulation.output_start_time, task.simulation.output_end_time, task.simulation.number_of_points + 1),
176+
)
177+
for variable in variables:
178+
self.assertFalse(numpy.any(numpy.isnan(variable_results[variable.id])))
179+
180+
@unittest.expectedFailure
181+
def test_exec_sed_task_negative_initial_time(self):
182+
task = sedml_data_model.Task(
183+
model=sedml_data_model.Model(
184+
source=os.path.join(os.path.dirname(__file__), 'fixtures', 'BIOMD0000000297.edited', 'ex1', 'BIOMD0000000297.xml'),
185+
language=sedml_data_model.ModelLanguage.SBML.value,
186+
changes=[],
187+
),
188+
simulation=sedml_data_model.UniformTimeCourseSimulation(
189+
algorithm=sedml_data_model.Algorithm(
190+
kisao_id='KISAO_0000088',
191+
),
192+
initial_time=-10.,
193+
output_start_time=-10.,
194+
output_end_time=10.,
195+
number_of_points=20,
196+
),
197+
)
198+
199+
variables = [
200+
sedml_data_model.Variable(
201+
id='time',
202+
symbol=sedml_data_model.Symbol.time,
203+
task=task,
204+
),
205+
sedml_data_model.Variable(
206+
id='BE',
207+
target="/sbml:sbml/sbml:model/sbml:listOfSpecies/sbml:species[@id='BE']",
208+
target_namespaces=self.NAMESPACES,
209+
task=task,
210+
),
211+
]
212+
213+
variable_results, _ = core.exec_sed_task(task, variables, TaskLog())
214+
215+
self.assertTrue(sorted(variable_results.keys()), sorted([var.id for var in variables]))
216+
self.assertEqual(variable_results[variables[0].id].shape, (task.simulation.number_of_points + 1,))
217+
numpy.testing.assert_almost_equal(
218+
variable_results['time'],
219+
numpy.linspace(task.simulation.output_start_time, task.simulation.output_end_time, task.simulation.number_of_points + 1),
220+
)
221+
for variable in variables:
222+
self.assertFalse(numpy.any(numpy.isnan(variable_results[variable.id])))
223+
137224
def test_exec_sed_task_errors(self):
138225
with mock.patch.dict('os.environ', {'ALGORITHM_SUBSTITUTION_POLICY': 'NONE'}):
139226
task = sedml_data_model.Task()
@@ -150,7 +237,7 @@ def test_exec_sed_task_errors(self):
150237
task.simulation = sedml_data_model.UniformTimeCourseSimulation(
151238
id='simulation',
152239
algorithm=sedml_data_model.Algorithm(kisao_id='KISAO_0000448'),
153-
initial_time=10.,
240+
initial_time=-10.,
154241
output_start_time=10.,
155242
output_end_time=20.1,
156243
number_of_points=10,
@@ -178,7 +265,7 @@ def test_exec_sed_task_errors(self):
178265
core.exec_sed_task(task, variables, TaskLog())
179266
task.simulation.algorithm.changes[0].new_value = '10'
180267

181-
with self.assertRaisesRegex(NotImplementedError, 'is not supported. Initial time must be 0'):
268+
with self.assertRaisesRegex(NotImplementedError, 'is not supported. Initial time must be >= 0'):
182269
core.exec_sed_task(task, variables, TaskLog())
183270
task.simulation.initial_time = 0.
184271

0 commit comments

Comments
 (0)