Skip to content

Commit

Permalink
#61: automatically adjust output start time when values are used
Browse files Browse the repository at this point in the history
  • Loading branch information
fbergmann committed Nov 11, 2024
1 parent 600b49c commit ec5af2b
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 6 deletions.
10 changes: 8 additions & 2 deletions basico/model_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -4901,10 +4901,10 @@ def _set_group_from_dict(group, values, dm=None):
try:
if param_type == COPASI.CCopasiParameter.Type_STRING:
current_value = values[key]
# convert list of strings to comma separated string
# convert list of strings to space separated string
# converting other data types to string
if isinstance(current_value, list):
current_value = ', '.join(map(str, current_value))
current_value = ' '.join(map(str, current_value))
else:
current_value = str(current_value)
param.setStringValue(current_value)
Expand Down Expand Up @@ -5070,6 +5070,12 @@ def set_task_settings(task, settings, **kwargs):
problem.setStepNumber(int(settings['problem']['intervals']))
if 'StepNumber' in settings['problem']:
problem.setStepNumber(int(settings['problem']['StepNumber']))
if 'Values' in settings['problem'] and 'Use Values' in settings['problem'] and settings['problem']['Use Values']:
desired_values = problem.getValueString().split()
first_value = desired_values[0] if desired_values else None
if first_value:
# correct start time if needed
problem.setOutputStartTime(float(first_value))

if 'method' in settings:
method = task.getMethod()
Expand Down
6 changes: 6 additions & 0 deletions basico/task_timecourse.py
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,12 @@ def _setup_timecourse(args, kwargs):
for val in vals:
new_vals += ' ' + str(val)
vals = new_vals.strip()

# ensure that the output start time is correct adjusted:
first_value = vals.split(' ')[0] if vals else None
if first_value:
problem.setOutputStartTime(float(first_value))

problem.setValues(vals)
problem.setUseValues(True)
if 'use_values' in kwargs:
Expand Down
28 changes: 24 additions & 4 deletions tests/test_timecourse.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ def test_values(self):
tc = basico.run_time_course(values=[10, 20, 30])
tc2 = basico.run_time_course(values=[10, 20, 30], start_time=10)
self.assertIsNotNone(tc)
self.assertEqual(tc.shape, (4, 1))
# we decided that both should have the same shape
# see issue #61
self.assertEqual(tc.shape, (3, 1))
self.assertEqual(tc2.shape, (3, 1))


Expand Down Expand Up @@ -94,9 +96,9 @@ def test_issue_61(self):
16, 16.5, 17, 17.5, 18, 18.5, 19, 19.5, 20]

problem = {
"AutomaticStepSize": False,
"Duration": 20.0,
"OutputStartTime": 10.0,
"AutomaticStepSize": True, # should be ignored
"Duration": 200.0, # should be ignored
"OutputStartTime": 100.0, # should be ignored
"Use Values": True,
"Values": desired_values
}
Expand All @@ -122,6 +124,24 @@ def test_issue_61(self):
dm.removeInterface(dh)
self.assertListEqual(data['Time'].to_list(), desired_values)
self.assertListEqual(df2['Time'].to_list(), desired_values)


# run some more tests white different values for duration and output start time
# ensuring that the requested values are still returned
desired_values = [ 0, 10, 15, 20]
df1 = basico.run_time_course_with_output(output_elements, values=desired_values, model=dm)
self.assertListEqual(df1['Time'].to_list(), desired_values)
df2 = basico.run_time_course(**{"use_inital_values": True}).reset_index()
self.assertListEqual(df2['Time'].to_list(), desired_values)

desired_values = [ 15, 20 ]
df1 = basico.run_time_course_with_output(output_elements, values=desired_values, model=dm)
self.assertListEqual(df1['Time'].to_list(), desired_values)
df2 = basico.run_time_course(**{"use_inital_values": True, "values":desired_values}).reset_index()
self.assertListEqual(df2['Time'].to_list(), desired_values)



basico.remove_datamodel(dm)


Expand Down

0 comments on commit ec5af2b

Please sign in to comment.