Skip to content

Commit

Permalink
Fixed species bug if named S0 or S1
Browse files Browse the repository at this point in the history
  • Loading branch information
fabriciocravo committed Jun 27, 2024
1 parent f5dbb90 commit 98b118d
Show file tree
Hide file tree
Showing 10 changed files with 106 additions and 83 deletions.
7 changes: 6 additions & 1 deletion for_local_use.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@
import matplotlib.pyplot as plt
import os
import basico
import pint


if __name__ == '__main__':

pass
_S0 = BaseSpecies()

_S0 >> Zero [1]

S = Simulation(_S0)
print(S.compile())


6 changes: 3 additions & 3 deletions mobspy/modules/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,10 +305,10 @@ def compile(cls, meta_species_to_simulate, reactions_set, species_counts, orthog
if continuous_sim:
end_event = {'trigger': ending_condition.generate_string(orthogonal_vector_structure),
'delay': '0',
'assignments': [('End_Flag_MetaSpecies', '1')]}
'assignments': [('_End_Flag_MetaSpecies', '1')]}

reactions_for_sbml['phantom_reaction_end'] = {'re': [(10, 'End_Flag_MetaSpecies')], 'pr': [],
'kin': 'End_Flag_MetaSpecies * 1e-100'}
reactions_for_sbml['phantom_reaction_end'] = {'re': [(10, '_End_Flag_MetaSpecies')], 'pr': [],
'kin': '_End_Flag_MetaSpecies * 1e-100'}
events_for_sbml['end_event'] = end_event

set_to_double_parameter = \
Expand Down
23 changes: 14 additions & 9 deletions mobspy/modules/meta_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ def __init__(self, object_reference, characteristics, stoichiometry=1, label=Non
:param label: (int, float, str) value for the label for matching used in this reaction
"""
super(Reacting_Species, self).__init__()
if object_reference.get_name() == 'S0' and characteristics == set():
if object_reference.get_name() == '_S0' and characteristics == set():
self.list_of_reactants = []
else:
self.list_of_reactants = [{'object': object_reference, 'characteristics': characteristics,
Expand Down Expand Up @@ -1062,7 +1062,7 @@ def __init__(self, name):
:param name: (str) Name of the species (can be placeholder if named with N$)
"""
super(Species, self).__init__()
self._name = name
self.name(name)
self._characteristics = set()
self._references = {self}
self._ordered_references = []
Expand All @@ -1088,6 +1088,13 @@ def name(self, name):
:param name: (str) name of the species
"""
if '_' == name[0]:
simlog_error(f"In species name {name}: Species cannot have a name starting with underscore")

name = clean_species_name(name)
self._name = name

def _bypass_name(self, name):
name = clean_species_name(name)
self._name = name

Expand Down Expand Up @@ -1255,7 +1262,7 @@ def _Create_Species(species, code_line, number_or_names=None):
if species is None:
to_return.append(Species(name))
else:
temp = __S1 * species
temp = One * species
temp.name(name)
to_return.append(temp)

Expand Down Expand Up @@ -1292,12 +1299,10 @@ def New(species, number_or_names=None):
return _Create_Species(species, code_line, number_or_names)


__S0, __S1, __SF = BaseSpecies(3)
__S0.name('S0')
__S1.name('S1')
__SF.name('End_Flag_MetaSpecies')
EndFlagSpecies = __SF
Zero = __S0
Zero, One, EndFlagSpecies = BaseSpecies(3)
Zero._bypass_name('_S0')
One._bypass_name('_S1')
EndFlagSpecies._bypass_name('_End_Flag_MetaSpecies')
_methods_Species = set(dir(Species))

if __name__ == '__main__':
Expand Down
6 changes: 3 additions & 3 deletions mobspy/sbml_simulator/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def __single_run(packed):
reformatted_data = reformat_time_series(data)

if sim_par['_continuous_simulation']:
if reformatted_data['End_Flag_MetaSpecies'][-1] > 0:
if reformatted_data['_End_Flag_MetaSpecies'][-1] > 0:
reformatted_data = __filter_condition_event_time_data(reformatted_data)
end_condition_not_satisfied = False
else:
Expand Down Expand Up @@ -144,7 +144,7 @@ def __filter_condition_event_time_data(data):
"""
new_data = {}

for i, e in enumerate(data['End_Flag_MetaSpecies']):
for i, e in enumerate(data['_End_Flag_MetaSpecies']):
if e == 1:
stop_index = i
break
Expand Down Expand Up @@ -186,7 +186,7 @@ def __sbml_new_initial_values(data, model, sim_para, new_model=False):

if new_model:
try:
species_for_sbml['End_Flag_MetaSpecies'] = 0
species_for_sbml['_End_Flag_MetaSpecies'] = 0
except KeyError:
pass

Expand Down
Binary file modified test_plot_images/constant_tree.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test_plot_images/deterministic_tree.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test_plot_images/stochastic_tree.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 12 additions & 1 deletion test_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -1823,6 +1823,17 @@ def test_assignment_similar_species():
assert compare_model(S.compile(), 'test_tools/model_55.txt')


def test_blocked_names():

try:
_S0 = BaseSpecies()

_S0 >> Zero[1]
assert False
except SystemExit:
assert True


# This is here because pytest is slow - but this script works fine with pytest. Just make sure that the
# python version in terminal is above 3.10
test_list = [test_model_1, test_model_2, test_model_3, test_model_4, test_model_5, test_model_6, test_model_7,
Expand Down Expand Up @@ -1851,7 +1862,7 @@ def test_assignment_similar_species():
test_illegal_unit_op_in_assignment, test_all_asgn_ops, test_no_species_in_asg, text_complex_assignments,
text_assign_context_exit, text_even_more_complex_assignments, test_assign_context_complex,
test_assign_context_constant, test_duration_with_run, test_rev, test_dimensionless_count,
test_assignment_similar_species]
test_assignment_similar_species, test_blocked_names]

sub_test = test_list

Expand Down
Loading

0 comments on commit 98b118d

Please sign in to comment.