In [1]:
%matplotlib inline
import math
from ecell4 import *
from ecell4_base import *
from ecell4_base.core import *
In [2]:
def create_simulator(f=gillespie.Factory()):
m = NetworkModel()
A, B, C = Species('A', 0.005, 1), Species('B', 0.005, 1), Species('C', 0.005, 1)
m.add_species_attribute(A)
m.add_species_attribute(B)
m.add_species_attribute(C)
m.add_reaction_rule(create_binding_reaction_rule(A, B, C, 0.01))
m.add_reaction_rule(create_unbinding_reaction_rule(C, A, B, 0.3))
w = f.world()
w.bind_to(m)
w.add_molecules(C, 60)
sim = f.simulator(w)
sim.initialize()
return sim
One of most popular Observer
is FixedIntervalNumberObserver
, which logs the number of molecules with the given time interval. FixedIntervalNumberObserver
requires an interval and a list of serials of Species
for logging.
In [3]:
obs1 = FixedIntervalNumberObserver(0.1, ['A', 'B', 'C'])
sim = create_simulator()
sim.run(1.0, obs1)
data
function of FixedIntervalNumberObserver
returns the data logged.
In [4]:
print(obs1.data())
targets()
returns a list of Species
, which you specified as an argument of the constructor.
In [5]:
print([sp.serial() for sp in obs1.targets()])
NumberObserver
logs the number of molecules after every steps when a reaction occurs. This observer is useful to log all reactions, but not available for ode
.
In [6]:
obs1 = NumberObserver(['A', 'B', 'C'])
sim = create_simulator()
sim.run(1.0, obs1)
print(obs1.data())
TimingNumberObserver
allows you to give the times for logging as an argument of its constructor.
In [7]:
obs1 = TimingNumberObserver([0.0, 0.1, 0.2, 0.5, 1.0], ['A', 'B', 'C'])
sim = create_simulator()
sim.run(1.0, obs1)
print(obs1.data())
run
function accepts multile Observer
s at once.
In [8]:
obs1 = NumberObserver(['C'])
obs2 = FixedIntervalNumberObserver(0.1, ['A', 'B'])
sim = create_simulator()
sim.run(1.0, [obs1, obs2])
print(obs1.data())
print(obs2.data())
FixedIntervalHDF5Observedr
logs the whole data in a World
to an output file with the fixed interval. Its second argument is a prefix for output filenames. filename()
returns the name of a file scheduled to be saved next. At most one format string like %02d
is allowed to use a step count in the file name. When you do not use the format string, it overwrites the latest data to the file.
In [9]:
obs1 = FixedIntervalHDF5Observer(0.2, 'test%02d.h5')
print(obs1.filename())
sim = create_simulator()
sim.run(1.0, obs1) # Now you have steped 5 (1.0/0.2) times
print(obs1.filename())
In [10]:
w = load_world('test05.h5')
print(w.t(), w.num_molecules(Species('C')))
The usage of FixedIntervalCSVObserver
is almost same with that of FixedIntervalHDF5Observer
. It saves positions (x, y, z) of particles with the radius (r) and serial number of Species
(sid) to a CSV file.
In [11]:
obs1 = FixedIntervalCSVObserver(0.2, "test%02d.csv")
print(obs1.filename())
sim = create_simulator()
sim.run(1.0, obs1)
print(obs1.filename())
Here is the first 10 lines in the output CSV file.
In [12]:
print(''.join(open("test05.csv").readlines()[: 10]))
For particle simulations, E-Cell4 also provides Observer
to trace a trajectory of a molecule, named FixedIntervalTrajectoryObserver
. When no ParticleID
is specified, it logs all the trajectories. Once some ParticleID
is lost for the reaction during a simulation, it just stop to trace the particle any more.
In [13]:
sim = create_simulator(spatiocyte.Factory(0.005))
obs1 = FixedIntervalTrajectoryObserver(0.01)
sim.run(0.1, obs1)
In [14]:
print([tuple(pos) for pos in obs1.data()[0]])
Generally, World
assumes a periodic boundary for each plane. To avoid the big jump of a particle at the edge due to the boundary condition, FixedIntervalTrajectoryObserver
tries to keep the shift of positions. Thus, the positions stored in the Observer
are not necessarily limited in the cuboid given for the World
. To track the diffusion over the boundary condition accurately, the step interval for logging must be small enough. Of course, you can disable this option. See help(FixedIntervalTrajectoryObserver)
.
In this section, we explain the visualization tools for data logged by Observer
.
Firstly, for time course data, viz.plot_number_observer
plots the data provided by NumberObserver
, FixedIntervalNumberObserver
and TimingNumberObserver
. For the detailed usage of viz.plot_number_observer
, see help(viz.plot_number_observer)
.
In [15]:
obs1 = NumberObserver(['C'])
obs2 = FixedIntervalNumberObserver(0.1, ['A', 'B'])
sim = create_simulator()
sim.run(10.0, [obs1, obs2])
In [16]:
viz.plot_number_observer(obs1, obs2)
You can set the style for plotting, and even add an arbitrary function to plot.
In [17]:
viz.plot_number_observer(obs1, '-', obs2, ':', lambda t: 60 * (1 + 2 * math.exp(-0.9 * t)) / (2 + math.exp(-0.9 * t)), '--')
Plotting in the phase plane is also available by specifing the x-axis and y-axis.
In [18]:
viz.plot_number_observer(obs2, 'o', x='A', y='B')
For spatial simulations, to visualize the state of World
, viz.plot_world
is available. This function plots the points of particles in three-dimensional volume in the interactive way. You can save the image by clicking a right button on the drawing region.
In [19]:
sim = create_simulator(spatiocyte.Factory(0.005))
# viz.plot_world(sim.world())
viz.plot_world(sim.world(), interactive=False)
You can also make a movie from a series of HDF5 files, given as a FixedIntervalHDF5Observer
. NOTE: viz.plot_movie
requires an extra library, ffmpeg
, when interactive=False
.
In [20]:
sim = create_simulator(spatiocyte.Factory(0.005))
obs1 = FixedIntervalHDF5Observer(0.02, 'test%02d.h5')
sim.run(1.0, obs1)
viz.plot_movie(obs1)
Finally, corresponding to FixedIntervalTrajectoryObserver
, viz.plot_trajectory
provides a visualization of particle trajectories.
In [21]:
sim = create_simulator(spatiocyte.Factory(0.005))
obs1 = FixedIntervalTrajectoryObserver(1e-3)
sim.run(1, obs1)
# viz.plot_trajectory(obs1)
viz.plot_trajectory(obs1, interactive=False)