{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Examples"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Installation\n",
"\n",
"To install dmd, simply run the following command (we advise you to do so in your favorite virtual environment)\n",
"```\n",
"pip install nidmd\n",
"```\n",
"\n",
"### Importing the adequate modules\n",
"\n",
"For this tutorial, we will load both the `Decomposition` module as well as the different plotting classes."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import plotly.io as pio\n",
"\n",
"pio.renderers.default = \"notebook\""
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import pandas as pd\n",
"\n",
"from nidmd import Decomposition, TimeSeries, Radar, TimePlot, Brain, Spectre, Atlas"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Loading data into a `Decomposition` instance"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"# Supported filetypes: `.csv` and `.mat`\n",
"\n",
"# Load from MATLAB file\n",
"dcp = Decomposition(filenames='../nidmd/tests/data/schaefer_test.mat')"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"# You can also do the extraction yourself if your filetype is not supported\n",
"# Here is an example with a `.csv` file (even though they are supported)\n",
"\n",
"matrix = np.genfromtxt('../nidmd/tests/data/glasser.csv', delimiter=',')\n",
"\n",
"dcp_ = Decomposition(data=matrix, sampling_time=0.72)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### `Decomposition` is a child of `TimeSeries`"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(dict_keys(['values', 'vectors', 'indices', 'A', 'activity']),\n",
" array([ 0.19149021+0.j , -0.13420224+0.j ,\n",
" -0.08280084+0.08482337j, -0.08280084-0.08482337j,\n",
" 0.1291952 +0.j ]))"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# The input files must follow certain restrictions to generate the wanted output\n",
"# One of them is that they must adhere to one of the supported cortical parcellation atlasses\n",
"# If this is not the case, a ImportError will be raised\n",
"\n",
"# You can still perform the DMD, using Decomposition's parent `TimeSeries`\n",
"\n",
"# Some fake data\n",
"data = np.random.rand(10, 500)\n",
"\n",
"ts = TimeSeries(data=data, sampling_time=1.5)\n",
"\n",
"dmd = ts.dmd()\n",
"\n",
"dmd.keys(), dmd['values'][:5]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Fetching the `Atlas`"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"schaefer Atlas of size 400\n"
]
},
{
"data": {
"text/plain": [
"(None, ['DA', 'VA', 'SM', 'VIS', 'LIM', 'FP', 'DMN'])"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# If the atlas is identified, you can read corresponding data as well\n",
"print(dcp.atlas), list(dcp.atlas.networks.keys())"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"
\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" atlas | \n",
" hemi | \n",
" side | \n",
" .long | \n",
" .lat | \n",
" .id | \n",
" .subid | \n",
" .roi | \n",
" .L1 | \n",
" .L3 | \n",
" .order | \n",
" region | \n",
" label | \n",
"
\n",
" \n",
" \n",
" \n",
" | 0 | \n",
" schaefer7 | \n",
" left | \n",
" lateral | \n",
" 77.9824 | \n",
" 37.6632 | \n",
" 1 | \n",
" 1 | \n",
" 1 | \n",
" 1 | \n",
" 1 | \n",
" 1 | \n",
" NaN | \n",
" NaN | \n",
"
\n",
" \n",
" | 1 | \n",
" schaefer7 | \n",
" left | \n",
" lateral | \n",
" 77.9378 | \n",
" 37.1861 | \n",
" 1 | \n",
" 1 | \n",
" 1 | \n",
" 1 | \n",
" 1 | \n",
" 2 | \n",
" NaN | \n",
" NaN | \n",
"
\n",
" \n",
" | 2 | \n",
" schaefer7 | \n",
" left | \n",
" lateral | \n",
" 78.4289 | \n",
" 36.3179 | \n",
" 1 | \n",
" 1 | \n",
" 1 | \n",
" 1 | \n",
" 1 | \n",
" 3 | \n",
" NaN | \n",
" NaN | \n",
"
\n",
" \n",
" | 3 | \n",
" schaefer7 | \n",
" left | \n",
" lateral | \n",
" 79.4202 | \n",
" 34.9920 | \n",
" 1 | \n",
" 1 | \n",
" 1 | \n",
" 1 | \n",
" 1 | \n",
" 4 | \n",
" NaN | \n",
" NaN | \n",
"
\n",
" \n",
" | 4 | \n",
" schaefer7 | \n",
" left | \n",
" lateral | \n",
" 80.3509 | \n",
" 34.1146 | \n",
" 1 | \n",
" 1 | \n",
" 1 | \n",
" 1 | \n",
" 1 | \n",
" 5 | \n",
" NaN | \n",
" NaN | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" atlas hemi side .long .lat .id .subid .roi .L1 .L3 \\\n",
"0 schaefer7 left lateral 77.9824 37.6632 1 1 1 1 1 \n",
"1 schaefer7 left lateral 77.9378 37.1861 1 1 1 1 1 \n",
"2 schaefer7 left lateral 78.4289 36.3179 1 1 1 1 1 \n",
"3 schaefer7 left lateral 79.4202 34.9920 1 1 1 1 1 \n",
"4 schaefer7 left lateral 80.3509 34.1146 1 1 1 1 1 \n",
"\n",
" .order region label \n",
"0 1 NaN NaN \n",
"1 2 NaN NaN \n",
"2 3 NaN NaN \n",
"3 4 NaN NaN \n",
"4 5 NaN NaN "
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# You can even access the 3D annot file projected into 2D coordinates used for plotting the cortical surfaces\n",
"dcp.atlas.coords_2d.head()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Retrieving information from a `Decomposition` instance"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ 0.56413247+0.3190115j, 0.56413247-0.3190115j,\n",
" -0.03441946+0.6527684j, -0.03441946-0.6527684j,\n",
" -0.50794325+0.4097298j])"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Let's say you are interested in the eigen-values of the eigen-decomposition of the auto-regressive model's matrix\n",
"# You can fetch the np.array directly\n",
"# All available attributes are defined in the documentation in the constructor 'Yields'\n",
"dcp.eig_val[:5]\n",
"\n",
"# Feel free to hack in!"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" mode | \n",
" value | \n",
" intensity | \n",
" damping_time | \n",
" period | \n",
" conjugate | \n",
" strength_real | \n",
" strength_imag | \n",
" activity | \n",
"
\n",
" \n",
" \n",
" \n",
" | 0 | \n",
" 1 | \n",
" -0.034419+0.652768j | \n",
" [(-0.012655198391238085-0.03277613608089217j),... | \n",
" 2.352141 | \n",
" 3.870205 | \n",
" True | \n",
" [0.026587127219590664, 0.02569015367883381, 0.... | \n",
" [0.028033854040225306, 0.030361969400318163, 0... | \n",
" [-3.964237936448096, -0.251766507342133, 2.547... | \n",
"
\n",
" \n",
" | 1 | \n",
" 2 | \n",
" -0.507943+0.409730j | \n",
" [(-0.021107785329405007-0.0540421133813644j), ... | \n",
" 2.343056 | \n",
" 2.551223 | \n",
" True | \n",
" [0.029381687031751896, 0.027642749359050808, 0... | \n",
" [0.028208494658742225, 0.025607545866732896, 0... | \n",
" [-1.4151377929253224, 1.6512486466854643, -0.7... | \n",
"
\n",
" \n",
" | 2 | \n",
" 3 | \n",
" 0.564132+0.319012j | \n",
" [(0.013203698555521668+0.039968652566025095j),... | \n",
" 2.305561 | \n",
" 12.208462 | \n",
" True | \n",
" [0.03006469800851769, 0.03480270558702691, 0.0... | \n",
" [0.02730288016966112, 0.025318438695252375, 0.... | \n",
" [-3.178872648091539, -3.579534391558363, -1.19... | \n",
"
\n",
" \n",
" | 3 | \n",
" 4 | \n",
" -0.605041+0.209460j | \n",
" [(0.02940859182897382+0.03819766844681426j), (... | \n",
" 2.242842 | \n",
" 2.237351 | \n",
" True | \n",
" [0.027911581293748263, 0.03075186173081999, 0.... | \n",
" [0.03114029955310492, 0.032727595208755296, 0.... | \n",
" [-3.6512764245617757, 3.709499255149587, -2.58... | \n",
"
\n",
" \n",
" | 4 | \n",
" 5 | \n",
" -0.268493-0.575713j | \n",
" [(0.008964801301731842-0.00868364630630098j), ... | \n",
" 2.203870 | \n",
" 3.130361 | \n",
" True | \n",
" [0.02603063026483108, 0.0297353883053019, 0.02... | \n",
" [0.02936413970518554, 0.0317265066676885, 0.02... | \n",
" [-0.43901115692259696, 4.161516719307512, -0.2... | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" mode value \\\n",
"0 1 -0.034419+0.652768j \n",
"1 2 -0.507943+0.409730j \n",
"2 3 0.564132+0.319012j \n",
"3 4 -0.605041+0.209460j \n",
"4 5 -0.268493-0.575713j \n",
"\n",
" intensity damping_time period \\\n",
"0 [(-0.012655198391238085-0.03277613608089217j),... 2.352141 3.870205 \n",
"1 [(-0.021107785329405007-0.0540421133813644j), ... 2.343056 2.551223 \n",
"2 [(0.013203698555521668+0.039968652566025095j),... 2.305561 12.208462 \n",
"3 [(0.02940859182897382+0.03819766844681426j), (... 2.242842 2.237351 \n",
"4 [(0.008964801301731842-0.00868364630630098j), ... 2.203870 3.130361 \n",
"\n",
" conjugate strength_real \\\n",
"0 True [0.026587127219590664, 0.02569015367883381, 0.... \n",
"1 True [0.029381687031751896, 0.027642749359050808, 0... \n",
"2 True [0.03006469800851769, 0.03480270558702691, 0.0... \n",
"3 True [0.027911581293748263, 0.03075186173081999, 0.... \n",
"4 True [0.02603063026483108, 0.0297353883053019, 0.02... \n",
"\n",
" strength_imag \\\n",
"0 [0.028033854040225306, 0.030361969400318163, 0... \n",
"1 [0.028208494658742225, 0.025607545866732896, 0... \n",
"2 [0.02730288016966112, 0.025318438695252375, 0.... \n",
"3 [0.03114029955310492, 0.032727595208755296, 0.... \n",
"4 [0.02936413970518554, 0.0317265066676885, 0.02... \n",
"\n",
" activity \n",
"0 [-3.964237936448096, -0.251766507342133, 2.547... \n",
"1 [-1.4151377929253224, 1.6512486466854643, -0.7... \n",
"2 [-3.178872648091539, -3.579534391558363, -1.19... \n",
"3 [-3.6512764245617757, 3.709499255149587, -2.58... \n",
"4 [-0.43901115692259696, 4.161516719307512, -0.2... "
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# The most important is probably the Pandas DataFrame that is computed\n",
"# It contains all modes and their relevant information\n",
"dcp.df.head()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Plotting"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
" \n",
" "
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"\n",
" \n",
" \n",
"
\n",
" \n",
"
"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# By following the documentation and providing `dcp.df`, the calls to the plotting classes are straightforward\n",
"Radar(dcp.df, dcp.atlas).figure(imag=True, amount=6)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
" \n",
" \n",
"
\n",
" \n",
"
"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# Let us plot Mode no. 2 with an specific matplotlib colormap\n",
"# I urge you to use Diverging colormaps for an accurate representation of region activity correlation\n",
"Brain(dcp_.df, 2, dcp_.atlas.coords_2d).figure(colormap='PiYG')"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
" \n",
" \n",
"
\n",
" \n",
"
"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"Spectre(dcp_.df, ['Group 1']).figure()"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [
{
"data": {
"text/html": [
"\n",
" \n",
" \n",
"
\n",
" \n",
"
"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"TimePlot(dcp_.df).figure(amount=3)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.0"
}
},
"nbformat": 4,
"nbformat_minor": 4
}