TVB Fish

Overview

We are going to set up a TVB model using coarse-scale zebrafish connectivity data.

Notebook Setup

Define some variables

data_dir = '../data/tvb_fish'

tvb_folder = '~/Code/libraries_of_mine/github'

Importage

import os,sys,glob,numpy as np,pandas as pd
from copy import deepcopy

%matplotlib inline
from matplotlib import pyplot as plt
import seaborn as sns

sys.path += [tvb_folder + '/' + t for t in ['tvb-library', 'tvb-data']]
from tvb.simulator.lab import *

Ok, let’s get cracking.

Load the data

weights = np.loadtxt(data_dir + '/weights_orig.txt')
centres = np.loadtxt(data_dir + '/centres_orig.txt')
tract_lengths = np.loadtxt(data_dir + '/tract_lengths.txt')

centres_orig.txt needs modifying; roi names need to be added as a first column

names = np.array(['roi%s' %(r+1) for r in range(centres.shape[0])])
df_nc = pd.DataFrame(centres, columns=['x', 'y', 'z']).astype(str)
df_nc['names'] = names
df_nc[['names', 'x', 'y', 'z']].to_csv(data_dir + '/centres.txt', 
                                       header=False, sep=' ', index=False)
df_nc[['names', 'x', 'y', 'z']].head()
names x y z
0 roi1 279.0 162.0 40.0
1 roi2 284.0 214.0 40.0
2 roi3 296.0 251.0 40.0
3 roi4 347.0 274.0 40.0
4 roi5 280.0 146.0 50.0
f = open(data_dir + '/centres.txt', 'w+')
for l in df_nc[['names', 'x', 'y', 'z']].values:
  f.writelines(' '.join(l) + '\n')
f.close()
cat $data_dir/centres.txt
roi1 279.0 162.0 40.0
roi2 284.0 214.0 40.0
roi3 296.0 251.0 40.0
roi4 347.0 274.0 40.0
roi5 280.0 146.0 50.0
roi6 282.0 203.0 50.0
roi7 295.0 247.0 50.0
roi8 332.0 266.0 50.0
roi9 375.0 283.0 50.0
roi10 280.0 127.0 60.0
roi11 281.0 182.0 60.0
roi12 287.0 228.0 60.0
roi13 317.0 256.0 60.0
roi14 372.0 283.0 60.0
roi15 283.0 125.0 70.0
roi16 282.0 190.0 70.0
roi17 290.0 234.0 70.0
roi18 332.0 262.0 70.0
roi19 391.0 286.0 70.0
roi20 287.0 108.0 80.0
roi21 283.0 168.0 80.0
roi22 287.0 213.0 80.0
roi23 310.0 242.0 80.0
roi24 349.0 268.0 80.0
roi25 406.0 288.0 80.0
roi26 287.0 110.0 90.0
roi27 282.0 174.0 90.0
roi28 288.0 216.0 90.0
roi29 317.0 241.0 90.0
roi30 354.0 268.0 90.0
roi31 411.0 288.0 90.0
roi32 289.0 112.0 100.0
roi33 286.0 174.0 100.0
roi34 293.0 212.0 100.0
roi35 322.0 234.0 100.0
roi36 357.0 257.0 100.0
roi37 414.0 292.0 100.0
roi38 310.0 134.0 110.0
roi39 314.0 193.0 110.0
roi40 336.0 231.0 110.0
roi41 372.0 264.0 110.0
roi42 427.0 288.0 110.0
roi43 320.0 141.0 120.0
roi44 341.0 196.0 120.0
roi45 366.0 234.0 120.0
roi46 423.0 281.0 120.0
roi47 327.0 159.0 130.0
roi48 352.0 204.0 130.0
roi49 379.0 243.0 130.0
roi50 427.0 286.0 130.0
roi51 330.0 166.0 140.0
roi52 366.0 216.0 140.0
roi53 393.0 257.0 140.0
roi54 446.0 287.0 140.0
roi55 343.0 171.0 150.0
roi56 376.0 214.0 150.0
roi57 408.0 251.0 150.0
roi58 451.0 283.0 150.0
roi59 370.0 193.0 160.0
roi60 419.0 242.0 160.0
roi61 462.0 276.0 160.0
roi62 228.0 158.0 40.0
roi63 226.0 217.0 40.0
roi64 208.0 254.0 40.0
roi65 158.0 276.0 40.0
roi66 231.0 147.0 50.0
roi67 228.0 200.0 50.0
roi68 213.0 241.0 50.0
roi69 180.0 265.0 50.0
roi70 133.0 283.0 50.0
roi71 228.0 120.0 60.0
roi72 229.0 186.0 60.0
roi73 219.0 231.0 60.0
roi74 187.0 262.0 60.0
roi75 129.0 288.0 60.0
roi76 226.0 119.0 70.0
roi77 224.0 192.0 70.0
roi78 214.0 234.0 70.0
roi79 175.0 261.0 70.0
roi80 117.0 290.0 70.0
roi81 223.0 106.0 80.0
roi82 226.0 171.0 80.0
roi83 220.0 216.0 80.0
roi84 192.0 248.0 80.0
roi85 155.0 272.0 80.0
roi86 103.0 292.0 80.0
roi87 221.0 108.0 90.0
roi88 223.0 175.0 90.0
roi89 216.0 220.0 90.0
roi90 184.0 247.0 90.0
roi91 148.0 274.0 90.0
roi92 90.0 294.0 90.0
roi93 219.0 115.0 100.0
roi94 217.0 178.0 100.0
roi95 207.0 222.0 100.0
roi96 174.0 249.0 100.0
roi97 137.0 273.0 100.0
roi98 82.0 294.0 100.0
roi99 199.0 124.0 110.0
roi100 195.0 189.0 110.0
roi101 163.0 234.0 110.0
roi102 127.0 271.0 110.0
roi103 70.0 291.0 110.0
roi104 192.0 138.0 120.0
roi105 176.0 199.0 120.0
roi106 145.0 238.0 120.0
roi107 78.0 283.0 120.0
roi108 180.0 149.0 130.0
roi109 156.0 196.0 130.0
roi110 132.0 233.0 130.0
roi111 74.0 280.0 130.0
roi112 170.0 169.0 140.0
roi113 140.0 212.0 140.0
roi114 111.0 256.0 140.0
roi115 62.0 286.0 140.0
roi116 167.0 180.0 150.0
roi117 130.0 207.0 150.0
roi118 100.0 240.0 150.0
roi119 56.0 283.0 150.0
roi120 133.0 199.0 160.0
roi121 91.0 238.0 160.0
roi122 46.0 268.0 160.0
roi123 336.0 330.0 40.0
roi124 289.0 325.0 40.0
roi125 343.0 330.0 50.0
roi126 294.0 326.0 50.0
roi127 356.0 333.0 60.0
roi128 303.0 327.0 60.0
roi129 373.0 333.0 70.0
roi130 303.0 323.0 70.0
roi131 382.0 335.0 80.0
roi132 306.0 327.0 80.0
roi133 402.0 337.0 90.0
roi134 347.0 331.0 90.0
roi135 292.0 333.0 90.0
roi136 405.0 339.0 100.0
roi137 351.0 331.0 100.0
roi138 294.0 326.0 100.0
roi139 407.0 338.0 110.0
roi140 353.0 333.0 110.0
roi141 300.0 333.0 110.0
roi142 279.0 290.0 110.0
roi143 405.0 339.0 120.0
roi144 346.0 330.0 120.0
roi145 289.0 321.0 120.0
roi146 302.0 272.0 120.0
roi147 405.0 338.0 130.0
roi148 350.0 335.0 130.0
roi149 294.0 330.0 130.0
roi150 290.0 278.0 130.0
roi151 406.0 336.0 140.0
roi152 343.0 331.0 140.0
roi153 287.0 325.0 140.0
roi154 288.0 277.0 140.0
roi155 407.0 335.0 150.0
roi156 355.0 327.0 150.0
roi157 305.0 331.0 150.0
roi158 271.0 303.0 150.0
roi159 403.0 333.0 160.0
roi160 347.0 316.0 160.0
roi161 304.0 315.0 160.0
roi162 262.0 300.0 160.0
roi163 170.0 336.0 40.0
roi164 223.0 330.0 40.0
roi165 165.0 337.0 50.0
roi166 222.0 328.0 50.0
roi167 158.0 335.0 60.0
roi168 218.0 326.0 60.0
roi169 135.0 337.0 70.0
roi170 209.0 323.0 70.0
roi171 142.0 333.0 80.0
roi172 215.0 323.0 80.0
roi173 115.0 338.0 90.0
roi174 171.0 330.0 90.0
roi175 220.0 325.0 90.0
roi176 112.0 337.0 100.0
roi177 171.0 328.0 100.0
roi178 220.0 325.0 100.0
roi179 100.0 340.0 110.0
roi180 157.0 335.0 110.0
roi181 208.0 328.0 110.0
roi182 225.0 288.0 110.0
roi183 101.0 339.0 120.0
roi184 163.0 329.0 120.0
roi185 215.0 322.0 120.0
roi186 219.0 271.0 120.0
roi187 97.0 340.0 130.0
roi188 159.0 322.0 130.0
roi189 211.0 323.0 130.0
roi190 221.0 280.0 130.0
roi191 105.0 336.0 140.0
roi192 166.0 325.0 140.0
roi193 217.0 330.0 140.0
roi194 213.0 277.0 140.0
roi195 106.0 335.0 150.0
roi196 156.0 318.0 150.0
roi197 200.0 312.0 150.0
roi198 240.0 301.0 150.0
roi199 97.0 339.0 160.0
roi200 143.0 319.0 160.0
roi201 185.0 309.0 160.0
roi202 241.0 301.0 160.0
roi203 284.0 30.0 90.0
roi204 323.0 33.0 90.0
roi205 287.0 27.0 100.0
roi206 332.0 31.0 100.0
roi207 298.0 32.0 110.0
roi208 340.0 32.0 110.0
roi209 304.0 25.0 120.0
roi210 342.0 29.0 120.0
roi211 220.0 31.0 90.0
roi212 185.0 31.0 90.0
roi213 222.0 29.0 100.0
roi214 180.0 32.0 100.0
roi215 214.0 30.0 110.0
roi216 173.0 32.0 110.0
roi217 207.0 27.0 120.0
roi218 167.0 29.0 120.0
roi219 283.0 93.0 130.0
roi220 277.0 157.0 130.0
roi221 272.0 209.0 130.0
roi222 284.0 80.0 140.0
roi223 275.0 143.0 140.0
roi224 271.0 211.0 140.0
roi225 287.0 84.0 150.0
roi226 280.0 134.0 150.0
roi227 275.0 188.0 150.0
roi228 294.0 58.0 160.0
roi229 288.0 112.0 160.0
roi230 284.0 155.0 160.0
roi231 270.0 203.0 160.0
roi232 225.0 85.0 130.0
roi233 231.0 157.0 130.0
roi234 231.0 217.0 130.0
roi235 221.0 81.0 140.0
roi236 230.0 144.0 140.0
roi237 233.0 210.0 140.0
roi238 220.0 82.0 150.0
roi239 226.0 130.0 150.0
roi240 232.0 184.0 150.0
roi241 210.0 58.0 160.0
roi242 218.0 105.0 160.0
roi243 223.0 142.0 160.0
roi244 230.0 184.0 160.0

Currently when using the GUI there is an (unnecessary) error check which prevents the use of negative weights. There is a hack to fix this; but for the moment also make a new weights matrix with all positive values.

weights_allpos = deepcopy(weights)
weights_allpos[weights_allpos==1] = 1
weights_allpos[weights_allpos==-1] = 2
np.savetxt(data_dir + '/weights_allpos.txt', weights_allpos)

Make a zip folder

os.chdir(data_dir)
!rm -r fish_connectivity

!mkdir fish_connectivity
rm: cannot remove 'fish_connectivity': No such file or directory
cp weights.txt fish_connectivity/
cp tract_lengths.txt fish_connectivity/
cp centres.txt fish_connectivity/
!rm fish_connectivity.zip
rm: cannot remove 'fish_connectivity.zip': No such file or directory
!zip -r fish_connectivity.zip fish_connectivity
  adding: fish_connectivity/ (stored 0%)
  adding: fish_connectivity/centres.txt (deflated 72%)
  adding: fish_connectivity/tract_lengths.txt (deflated 61%)
  adding: fish_connectivity/weights.txt (deflated 99%)

Repeat for the all positive weights matrix

!rm -r fish_connectivity_allpos
rm: cannot remove 'fish_connectivity_allpos': No such file or directory
mkdir fish_connectivity_allpos
cp weights_allpos.txt fish_connectivity_allpos/weights.txt
cp tract_lengths.txt fish_connectivity_allpos/
cp centres.txt fish_connectivity_allpos/
!rm fish_connectivity_allpos.zip
rm: cannot remove 'fish_connectivity_allpos.zip': No such file or directory
!zip -r fish_connectivity_allpos.zip fish_connectivity_allpos
  adding: fish_connectivity_allpos/ (stored 0%)
  adding: fish_connectivity_allpos/centres.txt (deflated 72%)
  adding: fish_connectivity_allpos/tract_lengths.txt (deflated 61%)
  adding: fish_connectivity_allpos/weights.txt (deflated 100%)

Import to TVB

conn = connectivity.Connectivity.from_file(os.path.abspath('fish_connectivity.zip'))
WARNING  File 'average_orientations' not found in ZIP.
WARNING  File 'cortical' not found in ZIP.
WARNING  File 'hemispheres' not found in ZIP.
WARNING  File 'areas' not found in ZIP.
sns.heatmap(conn.weights, linecolor=None)
<matplotlib.axes._subplots.AxesSubplot at 0x7f2350f9a080>

png

Plot 2D

df_centres = pd.DataFrame(conn.centres, columns=['x', 'y', 'z'])
fig, ax = plt.subplots(ncols=3, figsize=(12,3))

df_centres.plot(kind='scatter', x='x', y='y', ax=ax[0])
df_centres.plot(kind='scatter', x='x', y='z', ax=ax[1])
df_centres.plot(kind='scatter', x='y', y='z', ax=ax[2])
<matplotlib.axes._subplots.AxesSubplot at 0x7f23426caf60>

png

Plot 3D

fig, ax = plt.subplots()
plot_3d_centres(conn.centres)#,axes=ax)

png