"**EBRAINS & IBRO First Virtual Master Class on Brain Atlasing and Simulation Services, July 29, 2021**"
"**EBRAINS & IBRO Second Virtual Master Class on Brain Atlasing and Simulation Services, September 3, 2021**"
]
},
{
...
...
@@ -50,7 +50,7 @@
"name": "stderr",
"output_type": "stream",
"text": [
"Matplotlib created a temporary config/cache directory at /tmp/matplotlib-079i6xhd because the default path (/tmp/cache/matplotlib) is not a writable directory; it is highly recommended to set the MPLCONFIGDIR environment variable to a writable directory, in particular to speed up the import of Matplotlib and to better support multiprocessing.\n"
"Matplotlib created a temporary config/cache directory at /tmp/matplotlib-dze_bazl because the default path (/tmp/cache/matplotlib) is not a writable directory; it is highly recommended to set the MPLCONFIGDIR environment variable to a writable directory, in particular to speed up the import of Matplotlib and to better support multiprocessing.\n"
]
},
{
...
...
@@ -103,7 +103,7 @@
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "f3e84e2d564543b19098d287a75562fc",
"model_id": "fda29b02dd764d289887c014db5ae0a7",
"version_major": 2,
"version_minor": 0
},
...
...
@@ -146,7 +146,7 @@
"<ul>\n",
"<li>We have here the <b>first population (variables $\\pmb{x_{1}}$ in abscissa and $\\pmb{y_{1}}$ in ordinate)</b>.\n",
"\n",
"The left most intersection of the two nullclines defines a <b>stable fixed point</b>, representing the <b>interictal state (i.e., between seizures)</b>, whereas the right most intersection is the center of a <b>limit cycle</b>, being the <b>ictal state (i.e., during seizures)</b>. Both states are separated by a separatrix, as you can se by drawing different trajectories in this phase space <i>(left click on the figure)</i>.</li><br>\n",
"The left most intersection of the two nullclines defines a <b>stable fixed point</b>, representing the <b>interictal state (i.e., between seizures)</b>, whereas the right most intersection is the center of a <b>limit cycle</b>, being the <b>ictal state (i.e., during seizures)</b>. Both states are separated by a separatrix, as you can se by drawing different trajectories in this phase space.</li><br>\n",
"\n",
"<li>You can also have a look at other variables in the phase space, such as the <b>second population (variables $\\pmb{x_{2}}$ in abscissa and $\\pmb{y_{2}}$ in ordinate)</b>, responsible for the <b>interictal spikes</b> in the Epileptor model. Change the lower and upper bound of the axis to see correctly the phase space.</li>\n",
"<tr><td>monitors</td><td style=\"text-align:left;\"><pre>(<tvb.simulator.monitors.TemporalAverage object at 0x7f39a837df98>, <tvb.simulator.monitors.iEEG object at 0x7f39a837de48>)</pre></td>\n",
"<tr><td>monitors</td><td style=\"text-align:left;\"><pre>(<tvb.simulator.monitors.TemporalAverage object at 0x7f4b83037160>, <tvb.simulator.monitors.iEEG object at 0x7f4b82d6af60>)</pre></td>\n",
"<tvb.simulator.simulator.Simulator at 0x7f39a7bbf278>"
"<tvb.simulator.simulator.Simulator at 0x7f4b827f2390>"
]
},
"execution_count": 29,
...
...
@@ -1034,7 +1034,7 @@
"text": [
"Starting simulation...\n",
"Finished simulation.\n",
"execute for82.03099298477173\n"
"execute for82.26916146278381\n"
]
}
],
...
...
%% Cell type:markdown id: tags:
# Modeling Epileptic seizures using TVB
%% Cell type:markdown id: tags:
**EBRAINS & IBRO First Virtual Master Class on Brain Atlasing and Simulation Services, July 29, 2021**
**EBRAINS & IBRO Second Virtual Master Class on Brain Atlasing and Simulation Services, September 3, 2021**
%% Cell type:markdown id: tags:
## Context
<p><div style="text-align: justify"><font size="4.5" face="time roman">TVB can be used to model <b>large-scale epileptic seizure dynamics</b>. Using relevant neural mass models, TVB allows to ask multiple questions such as the <b>localisation of the Epileptogenic Zone</b> or the <b>validity of different neuroimaging modalities</b> to assess the epileptogenicity of a brain structure. Here, we will present an example of such a modelisation.</font></div></p>
%% Cell type:markdown id: tags:
## Objectives
<p><div style="text-align: justify"><font size="4.5" face="time roman">The main goal of this tutorial is to provide a clear understanding of <b>how we can reproduce clinically relevant senarios</b> such as the modelisation of <b>propagation of an epileptic seizure</b> in the human brain, <b>surgical resection</b> of brain regions, and <b>electrical stimulation</b> of a brain region that can trigger a seizure.</font></div></p>
%% Cell type:markdown id: tags:
## How to do it with TVB?
<p><div style="text-align: justify"><font size="4.5" face="time roman">The first thing we want to do is to import the modules we will need for a simulation.</font></div></p>
%% Cell type:code id: tags:
``` python
%pylab inline
```
%% Output
Matplotlib created a temporary config/cache directory at /tmp/matplotlib-079i6xhd because the default path (/tmp/cache/matplotlib) is not a writable directory; it is highly recommended to set the MPLCONFIGDIR environment variable to a writable directory, in particular to speed up the import of Matplotlib and to better support multiprocessing.
Matplotlib created a temporary config/cache directory at /tmp/matplotlib-dze_bazl because the default path (/tmp/cache/matplotlib) is not a writable directory; it is highly recommended to set the MPLCONFIGDIR environment variable to a writable directory, in particular to speed up the import of Matplotlib and to better support multiprocessing.
Populating the interactive namespace from numpy and matplotlib
%% Cell type:code id: tags:
``` python
%%capture
# Import a bunch of stuff to ease command line usage.
from tvb.simulator.lab import *
from tvb.datatypes.time_series import TimeSeriesRegion
import tvb.datatypes.projections as projections
from utils import plot_connectivity
from phase_plane import phase_plane_interactive
import numpy as np
import os
import time as tm
```
%% Cell type:markdown id: tags:
<h1><font size="6" face="arial" color="black"> 1. Exploring the Epileptor model</font></h1>
<br>
<p><div style="text-align: justify"><font size="4.5" face="time roman">In order to model epilepsy, we choose the <b>Epileptor</b> which is a phenomenological neural mass model able to realistically reproduce <b>epileptic seizure dynamics</b> such as recorded with intracranial EEG electrodes (see <b>Jirsa et al., Brain 2014</b>).</font></div></p>
<p><div style="text-align: justify"><font size="4.5" face="time roman">Before launching any simulations, we will have a look at the <b>phase space</b> of the model in order to better understand its dynamics. We will use the phase plane interactive tool.</font></div></p>
%% Cell type:code id: tags:
``` python
# Create an Epileptor model instance.
epi = models.Epileptor()
# Initialise an Integrator scheme.
dt = 0.075 #integration steps [ms]
heunint = integrators.HeunDeterministic(dt=dt)
# Open the phase plane tool with the Epileptor model.
<p><div style="text-align: justify"><font size="4.5" face="time roman">Look at the phase space:
<br>
<ul>
<li>We have here the <b>first population (variables $\pmb{x_{1}}$ in abscissa and $\pmb{y_{1}}$ in ordinate)</b>.
The left most intersection of the two nullclines defines a <b>stable fixed point</b>, representing the <b>interictal state (i.e., between seizures)</b>, whereas the right most intersection is the center of a <b>limit cycle</b>, being the <b>ictal state (i.e., during seizures)</b>. Both states are separated by a separatrix, as you can se by drawing different trajectories in this phase space <i>(left click on the figure)</i>.</li><br>
The left most intersection of the two nullclines defines a <b>stable fixed point</b>, representing the <b>interictal state (i.e., between seizures)</b>, whereas the right most intersection is the center of a <b>limit cycle</b>, being the <b>ictal state (i.e., during seizures)</b>. Both states are separated by a separatrix, as you can se by drawing different trajectories in this phase space.</li><br>
<li>You can also have a look at other variables in the phase space, such as the <b>second population (variables $\pmb{x_{2}}$ in abscissa and $\pmb{y_{2}}$ in ordinate)</b>, responsible for the <b>interictal spikes</b> in the Epileptor model. Change the lower and upper bound of the axis to see correctly the phase space.</li>
<br><li>You can continue to play along to explore the dynamics of this model. For instance, try changing the number of integration steps, or choosing a HeunStochastic integrator and varying the scaling of the noise:</li>
</ul></font></div></p>
%% Cell type:code id: tags:
``` python
epi = models.Epileptor()
# Initialise a stochastic Integrator scheme.
dt = 0.01 #integration steps [ms]
heunstochint = integrators.HeunStochastic(dt=dt)
heunstochint.noise.nsig = np.r_[0.1] #standard deviation of the noise
# Open the phase plane tool with the Epileptor Model and stochastic Integrator.
<p><div style="text-align: justify"><font size="4.5" face="time roman">We will use the <b>default dataset</b> that is supplied with The Virtual Brain. To make things fully transparent, we will load the files from our local copy. To follow, change the path to the location of the dataset on your machine:</font></div></p>
%% Cell type:code id: tags:
``` python
datadir = os.path.abspath("./dataset")
```
%% Cell type:markdown id: tags:
<h1><font size="6" face="arial" color="black">3. Region-based modeling of an epileptic patient</font></h1>
<br>
<p><div style="text-align: justify"><font size="4.5" face="time roman">In the following, we will model a patient with a <b>bilateral temporal lobe epilepsy (TLE)</b> using the Default TVB Subject.</font></div></p> <br>
<p><div style="text-align: justify"><font size="4.5" face="time roman">The basic simulation of TVB consists of 5 main components. Each of these components is an object within TVB:</font></div></p>
<p><div style="text-align: justify"><font size="4.5" face="time roman">We start with loading and visualizing the structural connectivity matrix.</font></div></p>
%% Cell type:code id: tags:
``` python
# Initialise a Connectivity.
con = connectivity.Connectivity.from_file(os.path.join(datadir, "connectivity_76.zip"))
<p><div style="text-align: justify"><font size="4.5" face="time roman">A set of differential equations describing the local dynamics. Here, we use the <b>Epileptor</b>.</font></div></p>
<p><div style="text-align: justify"><font size="4.5" face="time roman">We can refine the network pathology of the patient by including the hypothesis about the Epileptogenic Network. To this end, we define a <b>spatial map of epileptogenicity</b> where each network's node <i>i</i> is characterized by an excitability value $\pmb{x_{0,i}}$, which quantifies its ability to trigger a seizure.
<br>We set:
<ul>
<li>the hippocampus (rHC, region 9 and lHC, region 47) as well as the amydala (rAMYG, region 2 and lAMYG, region 40)
as <b>Epileptogenic Zone (EZ)</b>, with an epileptogenicity parameter value $\pmb{x_{0,i}}$ equal to <b>-0.75</b> for the rHC and <b>-1.65</b> for the lHC and l-rAMYG,</li>
<li>we also add two lesser epileptogenic regions: the parahippocampal (lPHC, region 62) and the temporal pole (lTPOL, region 70) as <b>Propagation Zone (PZ)</b>, with an epileptogenicity parameter value $\pmb{x_{0,i}}$ equal to <b>-1.78</b> and <b>-1.81</b>, respectively,</li>
<li>and all the other regions (or <b>Non-Epileptogenic Zone, NEZ</b>) are set to <b>-2.6</b>.</li>
<p><div style="text-align: justify"><font size="4.5" face="time roman">It is a function that is used to join the local model dynamics at distinct spatial locations.</font></div></p>
<p><div style="text-align: justify"><font size="4.5" face="time roman">We couple the Epileptors by permittivity coupling, which quantifies the influence of neuronal fast discharges $x_{1,j}$ of a remote region $j$ on the local slow permittivity variable $z_{i}$ of region $i$ via a linear <b>difference coupling</b> function.</font></div></p>
%% Cell type:code id: tags:
``` python
# Initialise a Coupling function.
G = np.array(-0.25) #global (long-range) coupling strength
<p><div style="text-align: justify"><font size="4.5" face="time roman">We use a stochastic integration scheme, and the noise is added. The shape of the noise must correspond to the number of state-variables in the neural mass model, here equal to 6. </font></div></p>
<p><div style="text-align: justify"><font size="4.5" face="time roman">We employ a standard <b>Heun method</b> with an integration time step of <b>0.05</b>. <b>Additive white Gaussian noise</b> is introduced in the state-variables $\pmb{x_{2,i}}$ and $\pmb{y_{2,i}}$ with 0 mean and variance <b>0.00025</b>. Other variables experience only little or no noise due to their high density.</font></div></p>
%% Cell type:code id: tags:
``` python
# Initialise an Integrator scheme.
dt = 0.05 #integration steps [ms]
nsigma = 0.0001 #standard deviation of the noise
hiss = noise.Additive(nsig=np.array([0., 0., 0., nsigma, nsigma, 0.]))
<p><div style="text-align: justify"><font size="4.5" face="time roman">The last component we need to define are some Monitors. Monitors are measurement processes applied to the output data from the simulation.
<pre></pre>
Here, we will select (1) a simple data reduction and (2) a biophysical measurement process to relate the simulated neural activity to recent epilepsy studies:
<ol type="1">
<li>the <b>TemporalAverage</b> monitor averages over a time window of length <i>period</i> returning one time point every <i>period</i> ms,</li>
<p><div style="text-align: justify"><font size="4.5" face="time roman">And finally, we can look at the results of our simulation in terms of time series:</font></div></p>
<p><div style="text-align: justify"><font size="4.5" face="time roman">Surgical resection is used for around 20% of epileptic patient whose seizures are drug- resistant. We will simulate the hypothetic case of a surgical resection of the Epileptogenic Zone.</font></div></p>
<p><div style="text-align: justify"><font size="4.5" face="time roman">We set all the connections to the Epileptogenic Zone to 0 in the connectivity matrix. The resection of the Epileptogenic Network is not complete, will it be enough to prevent seizures?</font></div></p>
%% Cell type:code id: tags:
``` python
# All the incoming and outcoming connections of the resected regions are removed:
<p><div style="text-align: justify"><font size="4.5" face="time roman">We can now relaunch our first simulation, taking care of replacing the dynamic of the EZ by a stable node, as if the region was resected. The rest of the model is set as before, but we just use a time average monitor.</font></div></p>