From 886110907a2659671797b04f06190322c9c03fb0 Mon Sep 17 00:00:00 2001 From: Eran Date: Thu, 31 Aug 2023 12:58:15 -0400 Subject: [PATCH] add parameters port, custom print of results, and plotting results --- vivarium_tellurium/library/__init__.py | 0 vivarium_tellurium/library/printing.py | 13 +++++ .../processes/tellurium_process.py | 48 ++++++++++++++----- 3 files changed, 50 insertions(+), 11 deletions(-) create mode 100644 vivarium_tellurium/library/__init__.py create mode 100644 vivarium_tellurium/library/printing.py diff --git a/vivarium_tellurium/library/__init__.py b/vivarium_tellurium/library/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/vivarium_tellurium/library/printing.py b/vivarium_tellurium/library/printing.py new file mode 100644 index 0000000..34bb11f --- /dev/null +++ b/vivarium_tellurium/library/printing.py @@ -0,0 +1,13 @@ +def custom_pretty_print(data): + if isinstance(data, dict): + for key, value in data.items(): + if isinstance(value, list): + print(f"{key}: ", end="") + print(value) + else: + print(f"{key}:\n", end="") + custom_pretty_print(value) + elif isinstance(data, list): + print(data) + else: + print(data) diff --git a/vivarium_tellurium/processes/tellurium_process.py b/vivarium_tellurium/processes/tellurium_process.py index 08553a1..f506d6b 100644 --- a/vivarium_tellurium/processes/tellurium_process.py +++ b/vivarium_tellurium/processes/tellurium_process.py @@ -4,7 +4,9 @@ from vivarium.core.process import Process from vivarium.core.engine import Engine, pf +from vivarium.plots.simulation_output import plot_simulation_output import tellurium as te +from vivarium_tellurium.library.printing import custom_pretty_print class TelluriumProcess(Process): @@ -28,6 +30,7 @@ def __init__(self, config=None): self.input_ports = [ 'floating_species', 'boundary_species', + 'model_parameters' # 'time', # 'compartments', # 'parameters', @@ -39,20 +42,27 @@ def __init__(self, config=None): # 'time', ] - # extract the variables - # TODO -- get these according to ports + # Get the species (floating and boundary self.floating_species_list = self.simulator.getFloatingSpeciesIds() self.boundary_species_list = self.simulator.getBoundarySpeciesIds() self.floating_species_initial = self.simulator.getFloatingSpeciesConcentrations() self.boundary_species_initial = self.simulator.getBoundarySpeciesConcentrations() + + # Get the list of parameters and their values + self.model_parameters_list = self.simulator.getGlobalParameterIds() + self.model_parameter_values = self.simulator.getGlobalParameterValues() + + # Get a list of reactions self.reaction_list = self.simulator.getReactionIds() def initial_state(self, config=None): floating_species_dict = dict(zip(self.floating_species_list, self.floating_species_initial)) boundary_species_dict = dict(zip(self.boundary_species_list, self.boundary_species_initial)) + model_parameters_dict = dict(zip(self.model_parameters_list, self.model_parameter_values)) return { 'floating_species': floating_species_dict, 'boundary_species': boundary_species_dict, + 'model_parameters': model_parameters_dict } def ports_schema(self): @@ -60,7 +70,7 @@ def ports_schema(self): 'time': { '_default': 0.0, '_updater': 'set', - '_emit': True, + # '_emit': True, }, 'floating_species': { species_id: { @@ -76,6 +86,13 @@ def ports_schema(self): '_emit': True, } for species_id in self.boundary_species_list }, + 'model_parameters': { + param_id: { + '_default': 1.0, + '_updater': 'set', + '_emit': True, + } for param_id in self.model_parameters_list + }, 'reactions': { '_default': self.reaction_list }, @@ -104,34 +121,37 @@ def next_update(self, interval, states): # functions to configure and run the process def test_tellurium_process(): - total_time = 5 + total_time = 10.0 + time_step = 0.1 sbml_model_path = 'vivarium_tellurium/models/BIOMD0000000061_url.xml' # Caravagna2010.xml' # Create the simulation run parameters for the simulator config = { 'sbml_model_path': sbml_model_path, + 'time_step': time_step, } # Initialize the process by passing in a config dict - template_process = TelluriumProcess(config) + te_process = TelluriumProcess(config) # Get the ports for the process - process_ports = template_process.ports_schema() + process_ports = te_process.ports_schema() # Get the initial state - initial_state = template_process.initial_state() + initial_state = te_process.initial_state() # Feed the Simulator Process you just created to the vivarium Engine sim = Engine( processes={ - 'template': template_process, + 'te': te_process, }, topology={ - 'template': { + 'te': { port_id: (port_id,) for port_id in process_ports.keys() }, }, - initial_state=initial_state + initial_state=initial_state, + global_time_precision=5, ) # Call update with that sim object, which calls the next_update method in the implementation you created above using total_time @@ -143,7 +163,13 @@ def test_tellurium_process(): data = sim.emitter.get_timeseries() # Observe the data which is return from running the process: - print(f'RESULTS: {pf(data)}') + custom_pretty_print(data) + + # Plot output + plot_simulation_output( + data, + out_dir='out', + filename='te_process') # def test_load_from_antimony():