In :
%pylab inline

Populating the interactive namespace from numpy and matplotlib


# Surface simulation tutorial¶

This extends the basic region simulation, covered in the region simulation tutorial, to include the folded cortical surface to the anatomical structure on which the simulation is based, if you haven't already looked at that tutorial you probably should do that now as here we only really discuss in detail the extra things that are specific to a simulation on the cortical surface.

In addition to the components discussed for a region simulation here we introduce one new major component, that is:

1. Cortex, the primary component of which is a mesh surface defining a 2d representation of the convoluted cortical surface embedded in 3d space. This object includes a range of ancillary information and methods required for using it in simulations.

We will also introduce two new Monitors that make more sense in the context of surface simulations.

## Setup¶

Here we'll skip quickly over the configuration that was covered in "Tutorial: Anatomy of a Region Simulation".

In :
#Import a bunch of stuff to ease command line usage
from tvb.simulator.lab import *

   INFO  log level set to INFO
WARNING  Field seems mutable and has a default value. Consider using a lambda as a value factory
attribute tvb.simulator.models.wilson_cowan.WilsonCowan.state_variable_range = Final(field_type=<class 'dict'>, default={'E': array([0., 1.]), 'I': array([0., 1.])}, required=True)
WARNING  Field seems mutable and has a default value. Consider using a lambda as a value factory
attribute tvb.simulator.models.stefanescu_jirsa.ReducedSetFitzHughNagumo.state_variable_range = Final(field_type=<class 'dict'>, default={'xi': array([-4.,  4.]), 'eta': array([-3.,  3.]), 'alpha': array([-4.,  4.]), 'beta': array([-3.,  3.])}, required=True)
WARNING  default contains values out of the declared domain. Ex 1.0
attribute  tvb.simulator.models.stefanescu_jirsa.ReducedSetHindmarshRose.a = NArray(label=':math:a', dtype=float64, default=array([1.]), dim_names=(), ndim=None, required=True)
WARNING  default contains values out of the declared domain. Ex 3.0
attribute  tvb.simulator.models.stefanescu_jirsa.ReducedSetHindmarshRose.b = NArray(label=':math:b', dtype=float64, default=array([3.]), dim_names=(), ndim=None, required=True)
WARNING  default contains values out of the declared domain. Ex 1.0
attribute  tvb.simulator.models.stefanescu_jirsa.ReducedSetHindmarshRose.c = NArray(label=':math:c', dtype=float64, default=array([1.]), dim_names=(), ndim=None, required=True)
WARNING  default contains values out of the declared domain. Ex 3.3
attribute  tvb.simulator.models.stefanescu_jirsa.ReducedSetHindmarshRose.mu = NArray(label=':math:\\mu', dtype=float64, default=array([3.3]), dim_names=(), ndim=None, required=True)
WARNING  Field seems mutable and has a default value. Consider using a lambda as a value factory
attribute tvb.simulator.models.stefanescu_jirsa.ReducedSetHindmarshRose.state_variable_range = Final(field_type=<class 'dict'>, default={'xi': array([-4.,  4.]), 'eta': array([-25.,  20.]), 'tau': array([ 2., 10.]), 'alpha': array([-4.,  4.]), 'beta': array([-20.,  20.]), 'gamma': array([ 2., 10.])}, required=True)
WARNING  default contains values out of the declared domain. Ex 0.12
attribute  tvb.simulator.models.jansen_rit.JansenRit.p_min = NArray(label=':math:p_{min}', dtype=float64, default=array([0.12]), dim_names=(), ndim=None, required=True)
WARNING  default contains values out of the declared domain. Ex 0.32
attribute  tvb.simulator.models.jansen_rit.JansenRit.p_max = NArray(label=':math:p_{max}', dtype=float64, default=array([0.32]), dim_names=(), ndim=None, required=True)
WARNING  default contains values out of the declared domain. Ex 0.22
attribute  tvb.simulator.models.jansen_rit.JansenRit.mu = NArray(label=':math:\\mu_{max}', dtype=float64, default=array([0.22]), dim_names=(), ndim=None, required=True)
WARNING  Field seems mutable and has a default value. Consider using a lambda as a value factory
attribute tvb.simulator.models.jansen_rit.JansenRit.state_variable_range = Final(field_type=<class 'dict'>, default={'y0': array([-1.,  1.]), 'y1': array([-500.,  500.]), 'y2': array([-50.,  50.]), 'y3': array([-6.,  6.]), 'y4': array([-20.,  20.]), 'y5': array([-500.,  500.])}, required=True)
WARNING  Field seems mutable and has a default value. Consider using a lambda as a value factory
attribute tvb.simulator.models.jansen_rit.ZetterbergJansen.state_variable_range = Final(field_type=<class 'dict'>, default={'v1': array([-100.,  100.]), 'y1': array([-500.,  500.]), 'v2': array([-100.,   50.]), 'y2': array([-100.,    6.]), 'v3': array([-100.,    6.]), 'y3': array([-100.,    6.]), 'v4': array([-100.,   20.]), 'y4': array([-100.,   20.]), 'v5': array([-100.,   20.]), 'y5': array([-500.,  500.]), 'v6': array([-100.,   20.]), 'v7': array([-100.,   20.])}, required=True)
WARNING  default contains values out of the declared domain. Ex 1.0
attribute  tvb.simulator.models.oscillator.Generic2dOscillator.gamma = NArray(label=':math:\\gamma', dtype=float64, default=array([1.]), dim_names=(), ndim=None, required=True)
WARNING  Field seems mutable and has a default value. Consider using a lambda as a value factory
attribute tvb.simulator.models.oscillator.Generic2dOscillator.state_variable_range = Final(field_type=<class 'dict'>, default={'V': array([-2.,  4.]), 'W': array([-6.,  6.])}, required=True)
WARNING  Field seems mutable and has a default value. Consider using a lambda as a value factory
attribute tvb.simulator.models.oscillator.Kuramoto.state_variable_range = Final(field_type=<class 'dict'>, default={'theta': array([0.        , 6.28318531])}, required=True)
WARNING  Field seems mutable and has a default value. Consider using a lambda as a value factory
attribute tvb.simulator.models.oscillator.supHopf.state_variable_range = Final(field_type=<class 'dict'>, default={'x': array([-5.,  5.]), 'y': array([-5.,  5.])}, required=True)
WARNING  default contains values out of the declared domain. Ex -0.01
attribute  tvb.simulator.models.larter_breakspear.LarterBreakspear.TCa = NArray(label=':math:T_{Ca}', dtype=float64, default=array([-0.01]), dim_names=(), ndim=None, required=True)
WARNING  default contains values out of the declared domain. Ex 0.3
attribute  tvb.simulator.models.larter_breakspear.LarterBreakspear.TNa = NArray(label=':math:T_{Na}', dtype=float64, default=array([0.3]), dim_names=(), ndim=None, required=True)
WARNING  default contains values out of the declared domain. Ex 2.0
attribute  tvb.simulator.models.larter_breakspear.LarterBreakspear.aei = NArray(label=':math:a_{ei}', dtype=float64, default=array([2.]), dim_names=(), ndim=None, required=True)
WARNING  default contains values out of the declared domain. Ex 2.0
attribute  tvb.simulator.models.larter_breakspear.LarterBreakspear.aie = NArray(label=':math:a_{ie}', dtype=float64, default=array([2.]), dim_names=(), ndim=None, required=True)
WARNING  default contains values out of the declared domain. Ex 1.0
attribute  tvb.simulator.models.larter_breakspear.LarterBreakspear.ane = NArray(label=':math:a_{ne}', dtype=float64, default=array([1.]), dim_names=(), ndim=None, required=True)
WARNING  default contains values out of the declared domain. Ex 0.3
attribute  tvb.simulator.models.larter_breakspear.LarterBreakspear.Iext = NArray(label=':math:I_{ext}', dtype=float64, default=array([0.3]), dim_names=(), ndim=None, required=True)
WARNING  default contains values out of the declared domain. Ex 1.0
attribute  tvb.simulator.models.larter_breakspear.LarterBreakspear.QV_max = NArray(label=':math:Q_{max}', dtype=float64, default=array([1.]), dim_names=(), ndim=None, required=True)
WARNING  default contains values out of the declared domain. Ex 1.0
attribute  tvb.simulator.models.larter_breakspear.LarterBreakspear.QZ_max = NArray(label=':math:Q_{max}', dtype=float64, default=array([1.]), dim_names=(), ndim=None, required=True)
WARNING  default contains values out of the declared domain. Ex 1.0
attribute  tvb.simulator.models.larter_breakspear.LarterBreakspear.t_scale = NArray(label=':math:t_{scale}', dtype=float64, default=array([1.]), dim_names=(), ndim=None, required=True)
WARNING  Field seems mutable and has a default value. Consider using a lambda as a value factory
attribute tvb.simulator.models.larter_breakspear.LarterBreakspear.state_variable_range = Final(field_type=<class 'dict'>, default={'V': array([-1.5,  1.5]), 'W': array([-1.5,  1.5]), 'Z': array([-1.5,  1.5])}, required=True)
WARNING  default contains values out of the declared domain. Ex 0.27
attribute  tvb.simulator.models.wong_wang.ReducedWongWang.a = NArray(label=':math:a', dtype=float64, default=array([0.27]), dim_names=(), ndim=None, required=True)
WARNING  Field seems mutable and has a default value. Consider using a lambda as a value factory
attribute tvb.simulator.models.wong_wang.ReducedWongWang.state_variable_range = Final(field_type=<class 'dict'>, default={'S': array([0., 1.])}, required=True)
WARNING  default contains values out of the declared domain. Ex 10.0
attribute  tvb.simulator.models.wong_wang_exc_io_inh_i.ReducedWongWangExcIOInhI.tau_i = NArray(label=':math:\\tau_i', dtype=float64, default=array([10.]), dim_names=(), ndim=None, required=True)
WARNING  Field seems mutable and has a default value. Consider using a lambda as a value factory
attribute tvb.simulator.models.wong_wang_exc_io_inh_i.ReducedWongWangExcIOInhI.state_variable_range = Final(field_type=<class 'dict'>, default={'S_e': array([0., 1.]), 'S_i': array([0., 1.])}, required=True)
WARNING  Field seems mutable and has a default value. Consider using a lambda as a value factory
attribute tvb.simulator.models.linear.Linear.state_variable_range = Final(field_type=<class 'dict'>, default={'x': array([-1,  1])}, required=True)
WARNING  Field seems mutable and has a default value. Consider using a lambda as a value factory
attribute tvb.simulator.models.hopfield.Hopfield.state_variable_range = Final(field_type=<class 'dict'>, default={'x': array([-1.,  2.]), 'theta': array([0., 1.])}, required=True)
WARNING  Field seems mutable and has a default value. Consider using a lambda as a value factory
attribute tvb.simulator.models.epileptor.Epileptor.state_variable_range = Final(field_type=<class 'dict'>, default={'x1': array([-2.,  1.]), 'y1': array([-20.,   2.]), 'z': array([2., 5.]), 'x2': array([-2.,  0.]), 'y2': array([0., 2.]), 'g': array([-1.,  1.])}, required=True)
WARNING  default contains values out of the declared domain. Ex 1.0
attribute  tvb.simulator.models.epileptor.Epileptor2D.tt = NArray(label='tt', dtype=float64, default=array([1.]), dim_names=(), ndim=None, required=True)
WARNING  Field seems mutable and has a default value. Consider using a lambda as a value factory
attribute tvb.simulator.models.epileptor.Epileptor2D.state_variable_range = Final(field_type=<class 'dict'>, default={'x1': array([-2.,  1.]), 'z': array([2., 5.])}, required=True)
WARNING  default contains values out of the declared domain. Ex 1.0
attribute  tvb.simulator.models.JCepileptor.JC_Epileptor.gamma_rs = NArray(label=":math:'\\gamma_rs'", dtype=float64, default=array([1.]), dim_names=(), ndim=None, required=True)
WARNING  Field seems mutable and has a default value. Consider using a lambda as a value factory
attribute tvb.simulator.models.JCepileptor.JC_Epileptor.state_variable_range = Final(field_type=<class 'dict'>, default={'x1': array([-1.8, -1.4]), 'y1': array([-15, -10]), 'z': array([3.6, 4. ]), 'x2': array([-1.1, -0.9]), 'y2': array([0.001, 0.01 ]), 'g': array([-1.,  1.]), 'x_rs': array([-2.,  4.]), 'y_rs': array([-6.,  6.])}, required=True)
WARNING  Field seems mutable and has a default value. Consider using a lambda as a value factory
attribute tvb.simulator.models.epileptorcodim3.EpileptorCodim3.state_variable_range = Final(field_type=<class 'dict'>, default={'x': array([0.4, 0.6]), 'y': array([-0.1,  0.1]), 'z': array([0.  , 0.15])}, required=True)
WARNING  Field seems mutable and has a default value. Consider using a lambda as a value factory
attribute tvb.simulator.models.epileptorcodim3.EpileptorCodim3SlowMod.state_variable_range = Final(field_type=<class 'dict'>, default={'x': array([0.4, 0.6]), 'y': array([-0.1,  0.1]), 'z': array([0. , 0.1]), 'uA': array([0., 0.]), 'uB': array([0., 0.])}, required=True)
WARNING  Field seems mutable and has a default value. Consider using a lambda as a value factory
attribute tvb.simulator.models.Zerlaut.Zerlaut_adaptation_first_order.state_variable_range = Final(field_type=<class 'dict'>, default={'E': array([0. , 0.1]), 'I': array([0. , 0.1]), 'W': array([  0., 100.])}, required=True)
WARNING  Field seems mutable and has a default value. Consider using a lambda as a value factory
attribute tvb.simulator.models.Zerlaut.Zerlaut_adaptation_second_order.state_variable_range = Final(field_type=<class 'dict'>, default={'E': array([0. , 0.1]), 'I': array([0. , 0.1]), 'C_ee': array([0., 0.]), 'C_ei': array([0., 0.]), 'C_ii': array([0., 0.]), 'W': array([  0., 100.])}, required=True)
WARNING  Field seems mutable and has a default value. Consider using a lambda as a value factory
attribute tvb.datatypes.time_series.TimeSeries.labels_dimensions = Attr(field_type=<class 'dict'>, default={}, required=True)
WARNING  Field seems mutable and has a default value. Consider using a lambda as a value factory
attribute tvb.datatypes.projections.ProjectionMatrix.conductances = Attr(field_type=<class 'dict'>, default={'air': 0.0, 'skin': 1.0, 'skull': 0.01, 'brain': 1.0}, required=False)
WARNING  default contains values out of the declared domain. Ex 1.0
attribute  tvb.simulator.coupling.HyperbolicTangent.b = NArray(label=':math:b', dtype=float64, default=array([1.]), dim_names=(), ndim=None, required=True)
WARNING  default contains values out of the declared domain. Ex 1.0
attribute  tvb.simulator.coupling.Kuramoto.a = NArray(label=':math:a', dtype=float64, default=array([1.]), dim_names=(), ndim=None, required=True)

In :
#Initialise a Model, Coupling, and Connectivity.
oscillator = models.Generic2dOscillator()
white_matter = connectivity.Connectivity.from_file()
white_matter.speed = numpy.array([4.0])

white_matter_coupling = coupling.Linear(a=numpy.array([0.014]))

#Initialise an Integrator
heunint = integrators.HeunDeterministic(dt=2**-4)

WARNING  File 'hemispheres' not found in ZIP.


## Surface¶

Now to the surface itself. The main two attributes of a surface that we'll typically want to modify are the LocalConnectivity, which is a function describing the drop-off in connectivity with distance (in its simplest form, think of a truncated Gaussian relative to every vertex of the surface), and the coupling_strength which is just a weighting of the LocalConnectivity function, which potentially can be specified independently for every vertex (node on the cortical surface).

Here, we'll use the default LocalConnectivity function, and scale it appropriately based on the Model Cortex and Connectivity we'll be using. Unfortunately, for the time being there is not an automated way to determine the appropriate coupling strength given the Models selection, etc, so it is necessary to get a feeling for a specific system (Models, structure, etc) by experimenting a bit -- we are working toward tools that will ease this phase of model development.

In :
#Initialise a surface
default_cortex = cortex.Cortex.from_file()
default_cortex.coupling_strength = numpy.array([2**-10])
default_cortex.local_connectivity = local_connectivity.LocalConnectivity.from_file()


## Monitors¶

The first significant thing of note about surface simulations is that certain Monitors make a lot more sense in this context than they do at the region level, and so we'll introduce a couple new Monitors here that we didn't bother with in the region based example.

The first of these new Monitors is called SpatialAverage, this Monitor will, unsurprisingly, average over the space (nodes) of the simulation. In the case of region level simulations we already have a situation of a relatively small number of nodes, with each one representing a fairly large chunk of brain. In surface simulations on the other hand we can easily have tens of thousands of nodes, and reducing this by averaging over sensible collections of these nodes can be valuable. The basic mechanism is general, in the sense that the nodes can be broken up into any non-overlapping, complete, set of sets -- said another way, each node can only be counted in one collection and all nodes must be in one collection. As a concrete example, even in surface simulations information regarding a break up into regions exists, and this breakup, where each cortical mesh node belongs to one and only one region can be used to define the spatial average. In fact this is the default behaviour of the SpatialAverage monitor when applied to a surface simulation, that is it averages over nodes and returns region based time-series.

The second of these new Monitors, which is an instantiation of a biophysical measurement process, is called EEG. This monitor, hopefully also unsurprisingly, returns the EEG signal resulting from the simulated neural dynamics. EEG signals measured on the scalp depend strongly on the location and orientation of the underlying neural sources, which is why this monitor is more realistic and useful in the case of surface based simulations -- where the simulation is run on the explicit geometry of the cortex, which can potentially have been obtained from a specific individual's brain. In addition a simulation being built on the specific anatomical structure of an individual subject, the specific electrodes used in experimental work can also be incorporated, providing as direct as possible a link between simulation and experiment.

Here, we'll once again rely on TVB's defaults, where the default SpatialAverage monitor will return region level time-series and the EEG monitor will return a relatively standard 62 channel set based on the 10-20 system.

In :
#Initialise some Monitors with period in physical time
mon_tavg = monitors.TemporalAverage(period=2**-2)
mon_savg = monitors.SpatialAverage(period=2**-2)
# load the default region mapping
rm = region_mapping.RegionMapping.from_file()
mon_eeg = monitors.EEG.from_file()
mon_eeg.region_mapping=rm
#Bundle them
what_to_watch = (mon_tavg, mon_savg, mon_eeg)


## Simulate¶

We can then configure and run the Simulator as in a region based simulation.

NOTE: When the LocalConnectivity is empty, it will be calculated for the cortical surface, and that could take a minute or two.

In :
#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)

sim.configure()

WARNING  Memory estimate exceeds total available RAM.

Out:

### Simulator

value
Type
Simulator
conduction_speed
3.0
connectivity
Connectivity
coupling
Linear
initial_conditions
None
integrator
HeunDeterministic
model
Generic2dOscillator
monitors
(, , )
simulation_length
1000.0
stimulus
None
surface
Cortex
title
Simulator
In :
#Perform the simulation
tavg_data = []
tavg_time = []
savg_data = []
savg_time = []
eeg_data = []
eeg_time = []
for tavg, savg, eeg in sim(simulation_length=2**7):
if not tavg is None:
tavg_time.append(tavg)
tavg_data.append(tavg)

if not savg is None:
savg_time.append(savg)
savg_data.append(savg)

if not eeg is None:
eeg_time.append(eeg)
eeg_data.append(eeg)


## Pretty Pictures¶

And finally, we can look at the results of our simulation in terms of time-series as we have before:

In :
#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(1)
plot(savg_time, SAVG[:, 0, :, 0])
title("Region average")

#Plot EEG time series
figure(2)

plot(eeg_time, EEG[:, 0, :, 0])
title("EEG")

#Show them
show()  ## That's all folks -- so, what's next?¶

If you're still interested in surface simulations, then we have a number of other tutorials available, which you might like to check out, on subjects like: defining a custom LocalConnectivity; and applying stimuli to a surface simulation. Alternatively, you can get into the details of any aspects of TVB simulations that interest you in TVB's hand-book, or the simulator's reference manual (which is automatically generated from DocStrings within the code)... And, of course, dig into the code itself.

In [ ]: