Integration with Other Tools

Throught the utils package, Maybrain provides some support to interact with other tools.

Let's start by importing it and initialising a Brain (we are not including some nodes just for the sake of some later examples):


In [1]:
from maybrain import utils
from maybrain import resources as rr
from maybrain import brain as mbt
import bct

a = mbt.Brain()
a.import_adj_file(rr.DUMMY_ADJ_FILE_500, nodes_to_exclude=[0,1,2,3,4,5])
a.import_spatial_info(rr.MNI_SPACE_COORDINATES_500)
a.apply_threshold()

Integration with bctpy

Maybrain can be used with the pythonic version of the Brain Connectivity Toolbox which can be found here.

For this, the G networkx object needs to be saved in a specific format. We also indicate that when a connection is not present, it will be filled with the value zero:


In [2]:
bct_mat = utils.makebctmat(a, nonedge=0)
print("Shape of bct_mat: " + str(bct_mat.shape))
print("Number of nodes in a.G: " + str(a.G.number_of_nodes()))
print("Shape of the original a.adjMat: " + str(a.adjMat.shape))
print(bct_mat)


Shape of bct_mat: (494, 494)
Number of nodes in a.G: 494
Shape of the original a.adjMat: (500, 500)
[[0.         0.06617782 0.40941173 ... 0.08898691 0.07360708 0.03935779]
 [0.06617782 0.         0.1360191  ... 0.30046154 0.24445986 0.20757509]
 [0.40941173 0.1360191  0.         ... 0.10033294 0.17673282 0.18465023]
 ...
 [0.08898691 0.30046154 0.10033294 ... 0.         0.27570423 0.20343263]
 [0.07360708 0.24445986 0.17673282 ... 0.27570423 0.         0.37814492]
 [0.03935779 0.20757509 0.18465023 ... 0.20343263 0.37814492 0.        ]]

Functions can then be called on the resulting matrix:


In [3]:
bct_res = bct.modularity_louvain_und_sign(bct_mat) # calculate the Louvain modularity
print(bct_res[0])
print(bct_res[0].shape)


[3 2 3 2 3 2 2 3 1 3 3 5 5 2 3 3 3 5 2 2 3 2 3 3 2 5 1 5 3 3 5 2 3 5 5 2 3
 5 5 2 3 3 2 2 3 3 2 3 3 2 1 3 1 3 5 2 1 1 1 3 3 5 2 2 2 5 1 3 3 3 3 3 3 5
 3 2 3 2 1 3 3 1 1 1 1 5 1 2 2 3 2 1 1 3 1 3 2 1 2 5 2 3 2 2 2 5 1 3 1 1 5
 2 2 3 2 3 2 5 5 1 2 1 1 3 5 3 3 3 5 3 2 1 3 2 3 2 3 2 1 5 3 3 2 2 5 1 3 5
 3 2 2 1 3 3 3 2 2 3 3 3 2 3 3 3 1 1 2 3 3 3 1 3 3 5 2 2 5 2 3 1 2 5 2 3 1
 2 2 2 2 5 2 1 1 3 5 3 5 5 2 1 3 3 2 5 5 2 5 3 3 3 1 1 2 5 1 5 3 1 3 3 2 2
 5 2 2 1 3 3 3 3 3 3 1 2 3 1 5 3 3 3 1 2 3 5 3 2 2 3 5 1 2 1 3 2 2 1 2 1 5
 2 1 1 3 5 1 1 2 2 1 3 1 2 3 2 1 5 2 3 5 2 1 3 1 5 1 2 2 2 3 3 3 1 3 2 2 1
 1 3 3 1 5 3 2 3 1 2 2 1 5 3 1 1 2 1 3 3 3 5 5 3 3 5 2 2 3 5 3 2 1 3 2 2 5
 3 5 1 3 5 1 2 2 3 2 5 5 3 2 2 3 3 1 5 2 1 1 1 2 3 3 1 1 1 2 1 3 2 3 5 1 2
 2 2 2 3 1 1 2 2 1 2 3 5 3 3 2 5 5 3 3 3 3 3 3 2 2 5 3 3 3 2 3 5 3 1 3 2 3
 2 1 3 2 5 3 3 5 5 4 3 3 1 2 2 2 3 2 1 3 5 1 1 2 3 2 2 5 5 1 3 2 5 2 1 1 2
 5 3 2 5 3 1 1 1 3 5 5 2 2 1 2 2 2 3 3 1 2 2 1 1 3 2 3 3 3 3 2 3 5 2 1 5 2
 5 5 5 3 3 2 3 5 5 5 2 1 1]
(494,)

As you can see from the above output, the result is a simple list of numbers, one for each node of our originally a.G. We need to assign them back for each a.G node, which you can do by having a dictionary with that information (you can check from the output that our nodes start with the label 6 because when importing our adjacency matrix we didn't import the first 6 nodes):


In [4]:
dict_info = utils.assignbctresult(a, bct_res[0])
print(dict_info)


{6: 3, 7: 2, 8: 3, 9: 2, 10: 3, 11: 2, 12: 2, 13: 3, 14: 1, 15: 3, 16: 3, 17: 5, 18: 5, 19: 2, 20: 3, 21: 3, 22: 3, 23: 5, 24: 2, 25: 2, 26: 3, 27: 2, 28: 3, 29: 3, 30: 2, 31: 5, 32: 1, 33: 5, 34: 3, 35: 3, 36: 5, 37: 2, 38: 3, 39: 5, 40: 5, 41: 2, 42: 3, 43: 5, 44: 5, 45: 2, 46: 3, 47: 3, 48: 2, 49: 2, 50: 3, 51: 3, 52: 2, 53: 3, 54: 3, 55: 2, 56: 1, 57: 3, 58: 1, 59: 3, 60: 5, 61: 2, 62: 1, 63: 1, 64: 1, 65: 3, 66: 3, 67: 5, 68: 2, 69: 2, 70: 2, 71: 5, 72: 1, 73: 3, 74: 3, 75: 3, 76: 3, 77: 3, 78: 3, 79: 5, 80: 3, 81: 2, 82: 3, 83: 2, 84: 1, 85: 3, 86: 3, 87: 1, 88: 1, 89: 1, 90: 1, 91: 5, 92: 1, 93: 2, 94: 2, 95: 3, 96: 2, 97: 1, 98: 1, 99: 3, 100: 1, 101: 3, 102: 2, 103: 1, 104: 2, 105: 5, 106: 2, 107: 3, 108: 2, 109: 2, 110: 2, 111: 5, 112: 1, 113: 3, 114: 1, 115: 1, 116: 5, 117: 2, 118: 2, 119: 3, 120: 2, 121: 3, 122: 2, 123: 5, 124: 5, 125: 1, 126: 2, 127: 1, 128: 1, 129: 3, 130: 5, 131: 3, 132: 3, 133: 3, 134: 5, 135: 3, 136: 2, 137: 1, 138: 3, 139: 2, 140: 3, 141: 2, 142: 3, 143: 2, 144: 1, 145: 5, 146: 3, 147: 3, 148: 2, 149: 2, 150: 5, 151: 1, 152: 3, 153: 5, 154: 3, 155: 2, 156: 2, 157: 1, 158: 3, 159: 3, 160: 3, 161: 2, 162: 2, 163: 3, 164: 3, 165: 3, 166: 2, 167: 3, 168: 3, 169: 3, 170: 1, 171: 1, 172: 2, 173: 3, 174: 3, 175: 3, 176: 1, 177: 3, 178: 3, 179: 5, 180: 2, 181: 2, 182: 5, 183: 2, 184: 3, 185: 1, 186: 2, 187: 5, 188: 2, 189: 3, 190: 1, 191: 2, 192: 2, 193: 2, 194: 2, 195: 5, 196: 2, 197: 1, 198: 1, 199: 3, 200: 5, 201: 3, 202: 5, 203: 5, 204: 2, 205: 1, 206: 3, 207: 3, 208: 2, 209: 5, 210: 5, 211: 2, 212: 5, 213: 3, 214: 3, 215: 3, 216: 1, 217: 1, 218: 2, 219: 5, 220: 1, 221: 5, 222: 3, 223: 1, 224: 3, 225: 3, 226: 2, 227: 2, 228: 5, 229: 2, 230: 2, 231: 1, 232: 3, 233: 3, 234: 3, 235: 3, 236: 3, 237: 3, 238: 1, 239: 2, 240: 3, 241: 1, 242: 5, 243: 3, 244: 3, 245: 3, 246: 1, 247: 2, 248: 3, 249: 5, 250: 3, 251: 2, 252: 2, 253: 3, 254: 5, 255: 1, 256: 2, 257: 1, 258: 3, 259: 2, 260: 2, 261: 1, 262: 2, 263: 1, 264: 5, 265: 2, 266: 1, 267: 1, 268: 3, 269: 5, 270: 1, 271: 1, 272: 2, 273: 2, 274: 1, 275: 3, 276: 1, 277: 2, 278: 3, 279: 2, 280: 1, 281: 5, 282: 2, 283: 3, 284: 5, 285: 2, 286: 1, 287: 3, 288: 1, 289: 5, 290: 1, 291: 2, 292: 2, 293: 2, 294: 3, 295: 3, 296: 3, 297: 1, 298: 3, 299: 2, 300: 2, 301: 1, 302: 1, 303: 3, 304: 3, 305: 1, 306: 5, 307: 3, 308: 2, 309: 3, 310: 1, 311: 2, 312: 2, 313: 1, 314: 5, 315: 3, 316: 1, 317: 1, 318: 2, 319: 1, 320: 3, 321: 3, 322: 3, 323: 5, 324: 5, 325: 3, 326: 3, 327: 5, 328: 2, 329: 2, 330: 3, 331: 5, 332: 3, 333: 2, 334: 1, 335: 3, 336: 2, 337: 2, 338: 5, 339: 3, 340: 5, 341: 1, 342: 3, 343: 5, 344: 1, 345: 2, 346: 2, 347: 3, 348: 2, 349: 5, 350: 5, 351: 3, 352: 2, 353: 2, 354: 3, 355: 3, 356: 1, 357: 5, 358: 2, 359: 1, 360: 1, 361: 1, 362: 2, 363: 3, 364: 3, 365: 1, 366: 1, 367: 1, 368: 2, 369: 1, 370: 3, 371: 2, 372: 3, 373: 5, 374: 1, 375: 2, 376: 2, 377: 2, 378: 2, 379: 3, 380: 1, 381: 1, 382: 2, 383: 2, 384: 1, 385: 2, 386: 3, 387: 5, 388: 3, 389: 3, 390: 2, 391: 5, 392: 5, 393: 3, 394: 3, 395: 3, 396: 3, 397: 3, 398: 3, 399: 2, 400: 2, 401: 5, 402: 3, 403: 3, 404: 3, 405: 2, 406: 3, 407: 5, 408: 3, 409: 1, 410: 3, 411: 2, 412: 3, 413: 2, 414: 1, 415: 3, 416: 2, 417: 5, 418: 3, 419: 3, 420: 5, 421: 5, 422: 4, 423: 3, 424: 3, 425: 1, 426: 2, 427: 2, 428: 2, 429: 3, 430: 2, 431: 1, 432: 3, 433: 5, 434: 1, 435: 1, 436: 2, 437: 3, 438: 2, 439: 2, 440: 5, 441: 5, 442: 1, 443: 3, 444: 2, 445: 5, 446: 2, 447: 1, 448: 1, 449: 2, 450: 5, 451: 3, 452: 2, 453: 5, 454: 3, 455: 1, 456: 1, 457: 1, 458: 3, 459: 5, 460: 5, 461: 2, 462: 2, 463: 1, 464: 2, 465: 2, 466: 2, 467: 3, 468: 3, 469: 1, 470: 2, 471: 2, 472: 1, 473: 1, 474: 3, 475: 2, 476: 3, 477: 3, 478: 3, 479: 3, 480: 2, 481: 3, 482: 5, 483: 2, 484: 1, 485: 5, 486: 2, 487: 5, 488: 5, 489: 5, 490: 3, 491: 3, 492: 2, 493: 3, 494: 5, 495: 5, 496: 5, 497: 2, 498: 1, 499: 1}