Demonstrate using the simulator for a surface simulation.

Run time: approximately 2 min (workstation circa 2010).

Memory requirement: ~ 1 GB


In [2]:
from tvb.datatypes.cortex import Cortex

from tvb.simulator.lab import *

Perform the simulation


In [3]:
LOG.info("Configuring...")

In [4]:
#Initialise a Model, Coupling, and Connectivity.
oscillator = models.Generic2dOscillator()

white_matter = connectivity.Connectivity(load_default=True)
white_matter.speed = numpy.array([4.0])
white_matter_coupling = coupling.Linear(a=2 ** -7)

In [5]:
#Initialise an Integrator
heunint = integrators.HeunDeterministic(dt=2 ** -4)

In [6]:
#Initialise some Monitors with period in physical time
mon_tavg = monitors.TemporalAverage(period=2 ** -2)
mon_savg = monitors.SpatialAverage(period=2 ** -2)
mon_eeg = monitors.EEG(period=2 ** -2)

#Bundle them
what_to_watch = (mon_tavg, mon_savg, mon_eeg)

In [7]:
#Initialise a surface
local_coupling_strength = numpy.array([2 ** -6])
default_cortex = Cortex(load_default=True)
default_cortex.coupling_strength = local_coupling_strength

In [8]:
##NOTE: THIS IS AN EXAMPLE OF DESCRIBING A SURFACE STIMULUS AT REGIONS LEVEL. 
#       SURFACES ALSO SUPPORT STIMULUS SPECIFICATION BY A SPATIAL FUNCTION 
#       CENTRED AT A VERTEX (OR VERTICES).
#Define the stimulus
#Specify a weighting for regions to receive stimuli...
white_matter.configure()    # Because we want access to number_of_regions
nodes = [0, 7, 13, 33, 42]
#NOTE: here, we specify space at region level simulator will map to surface 
#Specify a weighting for regions to receive stimuli... 
weighting = numpy.zeros((white_matter.number_of_regions, 1))
weighting[nodes] = numpy.array([2.0 ** -2, 2.0 ** -3, 2.0 ** -4, 2.0 ** -5, 2.0 ** -6])[:, numpy.newaxis]

eqn_t = equations.Gaussian()
eqn_t.parameters["midpoint"] = 8.0

stimulus = patterns.StimuliRegion(temporal=eqn_t,
                                  connectivity=white_matter,
                                  weight=weighting)

In [9]:
#Initialise Simulator -- Model, Connectivity, Integrator, Monitors, and surface.
sim = simulator.Simulator(model=oscillator,
                          connectivity=white_matter,
                          coupling=white_matter_coupling,
                          integrator=heunint,
                          monitors=what_to_watch,
                          surface=default_cortex, stimulus=stimulus)
sim.configure()

In [10]:
#Clear the initial transient, so that the effect of the stimulus is clearer.
#NOTE: this is ignored, stimuli are defined relative to each simulation call.
LOG.info("Initial integration to clear transient...")
for _, _, _ in sim(simulation_length=128):
    pass

In [11]:
LOG.info("Starting simulation...")
#Perform the simulation
tavg_data = []
tavg_time = []
savg_data = []
savg_time = []
eeg_data = []
eeg_time = []

for tavg, savg, eeg in sim(simulation_length=2 ** 5):
    if not tavg is None:
        tavg_time.append(tavg[0])
        tavg_data.append(tavg[1])

    if not savg is None:
        savg_time.append(savg[0])
        savg_data.append(savg[1])
        
    if not eeg is None:
        eeg_time.append(eeg[0])
        eeg_data.append(eeg[1])
    
LOG.info("finished simulation.")

Plot pretty pictures of what we just did


In [13]:
#Plot the stimulus
plot_pattern(sim.stimulus)

if IMPORTED_MAYAVI:
    surface_pattern(sim.surface, sim.stimulus.spatial_pattern)

#Make the lists numpy.arrays for easier use.
TAVG = numpy.array(tavg_data)
SAVG = numpy.array(savg_data)
EEG = numpy.array(eeg_data)

#Plot region averaged time series
figure(3)
plot(savg_time, SAVG[:, 0, :, 0])
title("Region average")

#Plot EEG time series
figure(4)
plot(eeg_time, EEG[:, 0, :, 0])
title("EEG")

#Show them
show()

#Surface movie, requires mayavi.malb
if IMPORTED_MAYAVI:
    st = surface_timeseries(sim.surface, TAVG[:, 0, :, 0])


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-13-7445d6fe7d49> in <module>()
      1 #Plot the stimulus
----> 2 plot_pattern(sim.stimulus)
      3 
      4 if IMPORTED_MAYAVI:
      5     surface_pattern(sim.surface, sim.stimulus.spatial_pattern)

/home/tim/Work/Models/python/TVB/trunk/scientific_library/tvb/simulator/plot/tools.pyc in plot_pattern(pattern_object)
    275     pyplot.figure(42)
    276     pyplot.subplot(221)
--> 277     pyplot.plot(pattern_object.spatial_pattern, "k*")
    278     pyplot.title("Space")
    279     #pyplot.plot(pattern_object.space, pattern_object.spatial_pattern, "k*")

/home/tim/anaconda/lib/python2.7/site-packages/matplotlib/pyplot.pyc in plot(*args, **kwargs)
   3097         ax.hold(hold)
   3098     try:
-> 3099         ret = ax.plot(*args, **kwargs)
   3100         draw_if_interactive()
   3101     finally:

/home/tim/anaconda/lib/python2.7/site-packages/matplotlib/axes/_axes.pyc in plot(self, *args, **kwargs)
   1371         lines = []
   1372 
-> 1373         for line in self._get_lines(*args, **kwargs):
   1374             self.add_line(line)
   1375             lines.append(line)

/home/tim/anaconda/lib/python2.7/site-packages/matplotlib/axes/_base.pyc in _grab_next_args(self, *args, **kwargs)
    302                 return
    303             if len(remaining) <= 3:
--> 304                 for seg in self._plot_args(remaining, kwargs):
    305                     yield seg
    306                 return

/home/tim/anaconda/lib/python2.7/site-packages/matplotlib/axes/_base.pyc in _plot_args(self, tup, kwargs)
    280             x = np.arange(y.shape[0], dtype=float)
    281 
--> 282         x, y = self._xy_from_xy(x, y)
    283 
    284         if self.command == 'plot':

/home/tim/anaconda/lib/python2.7/site-packages/matplotlib/axes/_base.pyc in _xy_from_xy(self, x, y)
    223             raise ValueError("x and y must have same first dimension")
    224         if x.ndim > 2 or y.ndim > 2:
--> 225             raise ValueError("x and y can be no greater than 2-D")
    226 
    227         if x.ndim == 1:

ValueError: x and y can be no greater than 2-D