Skip to content

Commit

Permalink
add parameters port, custom print of results, and plotting results
Browse files Browse the repository at this point in the history
  • Loading branch information
eagmon committed Aug 31, 2023
1 parent c782f21 commit 8861109
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 11 deletions.
Empty file.
13 changes: 13 additions & 0 deletions vivarium_tellurium/library/printing.py
Original file line number Diff line number Diff line change
@@ -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)
48 changes: 37 additions & 11 deletions vivarium_tellurium/processes/tellurium_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -28,6 +30,7 @@ def __init__(self, config=None):
self.input_ports = [
'floating_species',
'boundary_species',
'model_parameters'
# 'time',
# 'compartments',
# 'parameters',
Expand All @@ -39,28 +42,35 @@ 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):
return {
'time': {
'_default': 0.0,
'_updater': 'set',
'_emit': True,
# '_emit': True,
},
'floating_species': {
species_id: {
Expand All @@ -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
},
Expand Down Expand Up @@ -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
Expand All @@ -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():
Expand Down

0 comments on commit 8861109

Please sign in to comment.