Mouse Connectivity

This notebook demonstrates how to access and manipulate data in the Allen Mouse Brain Connectivity Atlas. The MouseConnectivityCache AllenSDK class provides methods for downloading metadata about experiments, including their viral injection site and the mouse's transgenic line. You can request information either as a Pandas DataFrame or a simple list of dictionaries.

An important feature of the MouseConnectivityCache is how it stores and retrieves data for you. By default, it will create (or read) a manifest file that keeps track of where various connectivity atlas data are stored. If you request something that has not already been downloaded, it will download it and store it in a well known location.

Download this notebook in .ipynb format here.

In [1]:
from allensdk.core.mouse_connectivity_cache import MouseConnectivityCache

# The manifest file is a simple JSON file that keeps track of all of
# the data that has already been downloaded onto the hard drives.
# If you supply a relative path, it is assumed to be relative to your
# current working directory.
mcc = MouseConnectivityCache(manifest_file='connectivity/mouse_connectivity_manifest.json')

# open up a list of all of the experiments
all_experiments = mcc.get_experiments(dataframe=True)
print "%d total experiments" % len(all_experiments)

# take a look at what we know about an experiment with a primary motor injection
all_experiments.loc[122642490]
2911 total experiments
Out[1]:
gender                                                                   M
id                                                               122642490
injection-coordinates                                   [4300, 2690, 7050]
injection-structures     [{u'abbreviation': u'MOp', u'color': u'1F9D5A'...
product-id                                                               5
strain                                                            C57BL/6J
structure-abbrev                                                       MOs
structure-color                                                     1F9D5A
structure-id                                                           993
structure-name                                        Secondary motor area
transgenic-line                                             Syt6-Cre_KI148
Name: 122642490, dtype: object

MouseConnectivityCache has a method for retrieving the adult mouse structure tree as an StructureTree class instance. This is a wrapper around a list of dictionaries, where each dictionary describes a structure. It is principally useful for looking up structures by their properties.

In [2]:
# pandas for nice tables
import pandas as pd

# grab the StructureTree instance
structure_tree = mcc.get_structure_tree()

# get info on some structures
structures = structure_tree.get_structures_by_name(['Primary visual area', 'Hypothalamus'])
pd.DataFrame(structures)
Out[2]:
acronym graph_id graph_order id name rgb_triplet structure_id_path structure_set_ids
0 VISp 1 185 385 Primary visual area [8, 133, 140] [997, 8, 567, 688, 695, 315, 669, 385] [396673091, 112905828, 12, 184527634, 51416699...
1 HY 1 715 1097 Hypothalamus [230, 68, 56] [997, 8, 343, 1129, 1097] [2, 112905828, 12, 184527634, 112905813, 11451...

As a convenience, structures are grouped in to named collections called "structure sets". These sets can be used to quickly gather a useful subset of structures from the tree. The criteria used to define structure sets are eclectic; a structure set might list:

  • structures that were used in a particular project.
  • structures that coarsely partition the brain.
  • structures that bear functional similarity.

or something else entirely. To view all of the available structure sets along with their descriptions, follow this link. To see only structure sets relevant to the adult mouse brain, use the StructureTree:

In [3]:
from allensdk.api.queries.ontologies_api import OntologiesApi

oapi = OntologiesApi()

# get the ids of all the structure sets in the tree
structure_set_ids = structure_tree.get_structure_sets()

# query the API for information on those structure sets
pd.DataFrame(oapi.get_structure_sets(structure_set_ids))
Out[3]:
description id name
0 List of Primary injection structures for BDA/A... 114512892 Mouse Connectivity - BDA/AAV Primary Injection...
1 List of primary AND secondary injection struct... 112905813 Mouse Connectivity - BDA/AAV All Injection Str...
2 List of structures for ABA Fine Structure Search 10 ABA - Fine Structure Search
3 List of structures used for the Connectivity p... 167587189 Mouse Connectivity - Summary
4 List of primary AND secondary injection struct... 112905828 Mouse Connectivity - Projection All Injection ...
5 All mouse visual areas with layers 396673091 Mouse Cell Types - Structures
6 List of visual cortex structures targeted for ... 514166994 Allen Brain Observatory targeted structure set
7 List of structures for ABA Differential Search 12 ABA - Differential Search
8 List of structures representing a coarse level... 2 Mouse - Coarse
9 List of valid structures for projection target... 184527634 Mouse Connectivity - Target Search
10 List of structures representing a areal level ... 3 Mouse - Areas
11 List of Primary injection structures for Proje... 114512891 Mouse Connectivity - Projection Primary Inject...

On the connectivity atlas web site, you'll see that we show most of our data at a fairly coarse structure level. We did this by creating a structure set of ~300 structures, which we call the "summary structures". We can use the structure tree to get all of the structures in this set:

In [4]:
# From the above table, "Mouse Connectivity - Summary" has id 167587189
summary_structures = structure_tree.get_structures_by_set_id([167587189])
pd.DataFrame(summary_structures)
Out[4]:
acronym graph_id graph_order id name rgb_triplet structure_id_path structure_set_ids
0 TMv 1 775 1 Tuberomammillary nucleus, ventral part [255, 76, 62] [997, 8, 343, 1129, 1097, 467, 331, 557, 1] [12, 184527634, 114512891, 112905828, 167587189]
1 IC 1 812 4 Inferior colliculus [255, 122, 255] [997, 8, 343, 313, 339, 4] [112905828, 10, 12, 184527634, 167587189, 1145...
2 PSV 1 889 7 Principal sensory nucleus of the trigeminal [255, 174, 111] [997, 8, 343, 1065, 771, 1132, 7] [112905828, 10, 12, 184527634, 112905813, 1145...
3 IF 1 869 12 Interfascicular nucleus raphe [255, 166, 255] [997, 8, 343, 313, 348, 165, 12] [112905828, 184527634, 114512891, 12, 167587189]
4 PT 1 693 15 Parataenial nucleus [255, 144, 159] [997, 8, 343, 1129, 549, 856, 571, 15] [12, 184527634, 114512891, 112905828, 167587189]
5 IG 1 491 19 Induseum griseum [126, 208, 75] [997, 8, 567, 688, 695, 1089, 1080, 19] [112905828, 184527634, 3, 12, 167587189]
6 AAA 1 593 23 Anterior amygdalar area [128, 192, 226] [997, 8, 567, 623, 477, 278, 23] [112905828, 184527634, 114512891, 12, 167587189]
7 IGL 1 705 27 Intergeniculate leaflet of the lateral genicul... [255, 144, 159] [997, 8, 343, 1129, 549, 856, 1014, 27] [112905828, 184527634, 12, 167587189]
8 PVa 1 731 30 Periventricular hypothalamic nucleus, anterior... [255, 93, 80] [997, 8, 343, 1129, 1097, 157, 30] [12, 184527634, 112905828, 167587189]
9 III 1 854 35 Oculomotor nucleus [255, 144, 255] [997, 8, 343, 313, 323, 35] [112905828, 10, 12, 184527634, 167587189, 1145...
10 PVH 1 720 38 Paraventricular hypothalamic nucleus [255, 93, 80] [997, 8, 343, 1129, 1097, 157, 38] [112905828, 10, 12, 184527634, 167587189, 1145...
11 ACAd 1 226 39 Anterior cingulate area, dorsal part [64, 166, 102] [997, 8, 567, 688, 695, 315, 31, 39] [112905828, 184527634, 114512891, 12, 167587189]
12 ILA 1 245 44 Infralimbic area [89, 179, 99] [997, 8, 567, 688, 695, 315, 44] [3, 112905828, 12, 184527634, 167587189, 11451...
13 ACAv 1 232 48 Anterior cingulate area, ventral part [64, 166, 102] [997, 8, 567, 688, 695, 315, 31, 48] [112905828, 184527634, 114512891, 12, 167587189]
14 ACB 1 575 56 Nucleus accumbens [128, 205, 248] [997, 8, 567, 623, 477, 493, 56] [112905828, 10, 12, 184527634, 167587189, 1145...
15 IMD 1 684 59 Intermediodorsal nucleus of the thalamus [255, 144, 159] [997, 8, 343, 1129, 549, 856, 444, 59] [112905828, 184527634, 114512891, 12, 167587189]
16 PVHd 1 782 63 Paraventricular hypothalamic nucleus, descendi... [255, 76, 62] [997, 8, 343, 1129, 1097, 467, 63] [12, 184527634, 114512891, 112905828, 167587189]
17 AD 1 679 64 Anterodorsal nucleus [255, 144, 159] [997, 8, 343, 1129, 549, 856, 239, 64] [112905828, 10, 12, 184527634, 167587189, 1145...
18 LT 1 861 66 Lateral terminal nucleus of the accessory opti... [255, 144, 255] [997, 8, 343, 313, 323, 66] [12, 184527634, 112905828, 167587189]
19 ADP 1 735 72 Anterodorsal preoptic nucleus [255, 85, 71] [997, 8, 343, 1129, 1097, 141, 72] [12, 184527634, 114512891, 112905828, 167587189]
20 IO 1 977 83 Inferior olivary complex [255, 179, 217] [997, 8, 343, 1065, 354, 370, 83] [112905828, 10, 12, 184527634, 167587189, 1145...
21 AHN 1 757 88 Anterior hypothalamic nucleus [255, 76, 62] [997, 8, 343, 1129, 1097, 467, 88] [112905828, 184527634, 114512891, 12, 167587189]
22 IP 1 1098 91 Interposed nucleus [255, 253, 188] [997, 8, 512, 519, 91] [112905828, 184527634, 114512891, 12, 167587189]
23 DCO 1 941 96 Dorsal cochlear nucleus [255, 165, 210] [997, 8, 343, 1065, 354, 386, 607, 96] [112905828, 184527634, 114512891, 12, 167587189]
24 IPN 1 870 100 Interpeduncular nucleus [255, 166, 255] [997, 8, 343, 313, 348, 165, 100] [112905828, 10, 12, 184527634, 167587189, 1145...
25 VCO 1 942 101 Ventral cochlear nucleus [255, 165, 210] [997, 8, 343, 1065, 354, 386, 607, 101] [12, 184527634, 114512891, 112905828, 167587189]
26 AId 1 279 104 Agranular insular area, dorsal part [33, 152, 102] [997, 8, 567, 688, 695, 315, 95, 104] [112905828, 184527634, 114512891, 12, 167587189]
27 ISN 1 979 106 Inferior salivatory nucleus [255, 179, 217] [997, 8, 343, 1065, 354, 370, 106] [112905828, 184527634, 12, 167587189]
28 AIp 1 285 111 Agranular insular area, posterior part [33, 152, 102] [997, 8, 567, 688, 695, 315, 95, 111] [112905828, 184527634, 114512891, 12, 167587189]
29 IV 1 857 115 Trochlear nucleus [255, 144, 255] [997, 8, 343, 313, 323, 115] [112905828, 184527634, 12, 167587189]
... ... ... ... ... ... ... ... ...
263 PPN 1 867 1052 Pedunculopontine nucleus [255, 166, 255] [997, 8, 343, 313, 348, 1052] [112905828, 10, 12, 184527634, 167587189, 1145...
264 GU 1 107 1057 Gustatory areas [0, 156, 117] [997, 8, 567, 688, 695, 315, 1057] [3, 112905828, 12, 184527634, 167587189, 11451...
265 PPT 1 849 1061 Posterior pretectal nucleus [255, 144, 255] [997, 8, 343, 313, 323, 1100, 1061] [112905828, 184527634, 114512891, 12, 167587189]
266 PPY 1 998 1069 Parapyramidal nucleus [255, 179, 217] [997, 8, 343, 1065, 354, 370, 1069] [12, 184527634, 112905828, 167587189]
267 PR 1 690 1077 Perireunensis nucleus [255, 144, 159] [997, 8, 343, 1129, 549, 856, 444, 1077] [112905828, 184527634, 12, 167587189]
268 PRE 1 531 1084 Presubiculum [89, 185, 71] [997, 8, 567, 688, 695, 1089, 822, 1084] [112905828, 184527634, 114512891, 12, 167587189]
269 PRNc 1 913 1093 Pontine reticular nucleus, caudal part [255, 186, 134] [997, 8, 343, 1065, 771, 987, 1093] [112905828, 12, 184527634, 112905813, 11451289...
270 MDRNd 1 986 1098 Medullary reticular nucleus, dorsal part [255, 179, 217] [997, 8, 343, 1065, 354, 370, 395, 1098] [112905828, 184527634, 114512891, 12, 167587189]
271 SUM 1 770 525 Supramammillary nucleus [255, 76, 62] [997, 8, 343, 1129, 1097, 467, 331, 525] [112905828, 184527634, 114512891, 12, 167587189]
272 MDRNv 1 987 1107 Medullary reticular nucleus, ventral part [255, 179, 217] [997, 8, 343, 1065, 354, 370, 395, 1107] [112905828, 12, 184527634, 112905813, 11451289...
273 PS 1 747 1109 Parastrial nucleus [255, 85, 71] [997, 8, 343, 1129, 1097, 141, 1109] [112905828, 184527634, 12, 167587189]
274 IAD 1 681 1113 Interanterodorsal nucleus of the thalamus [255, 144, 159] [997, 8, 343, 1129, 549, 856, 239, 1113] [112905828, 184527634, 114512891, 12, 167587189]
275 IAM 1 680 1120 Interanteromedial nucleus of the thalamus [255, 144, 159] [997, 8, 343, 1129, 549, 856, 239, 1120] [112905828, 184527634, 12, 167587189]
276 TMd 1 774 1126 Tuberomammillary nucleus, dorsal part [255, 76, 62] [997, 8, 343, 1129, 1097, 467, 331, 557, 1126] [12, 184527634, 112905828, 167587189]
277 SNr 1 822 381 Substantia nigra, reticular part [255, 144, 255] [997, 8, 343, 313, 323, 381] [112905828, 12, 184527634, 112905813, 11451289...
278 MV 1 1003 202 Medial vestibular nucleus [255, 179, 217] [997, 8, 343, 1065, 354, 370, 701, 202] [112905828, 184527634, 114512891, 12, 167587189]
279 RM 1 1011 206 Nucleus raphe magnus [255, 198, 226] [997, 8, 343, 1065, 354, 379, 206] [112905828, 184527634, 12, 167587189]
280 SUV 1 1005 217 Superior vestibular nucleus [255, 179, 217] [997, 8, 343, 1065, 354, 370, 701, 217] [12, 184527634, 112905828, 167587189]
281 RO 1 1013 222 Nucleus raphe obscurus [255, 198, 226] [997, 8, 343, 1065, 354, 379, 222] [12, 184527634, 114512891, 112905828, 167587189]
282 ACVII 1 968 576 Accessory facial motor nucleus [255, 179, 217] [997, 8, 343, 1065, 354, 370, 576] [112905828, 184527634, 12, 167587189]
283 PFL 1 1088 1041 Paraflocculus [255, 252, 145] [997, 8, 512, 528, 1073, 1041] [12, 184527634, 114512891, 112905828, 167587189]
284 SPVC 1 954 429 Spinal nucleus of the trigeminal, caudal part [255, 165, 210] [997, 8, 343, 1065, 354, 386, 429] [112905828, 184527634, 114512891, 12, 167587189]
285 NB 1 816 580 Nucleus of the brachium of the inferior collic... [255, 122, 255] [997, 8, 343, 313, 339, 580] [112905828, 184527634, 12, 167587189]
286 PG 1 912 931 Pontine gray [255, 186, 134] [997, 8, 343, 1065, 771, 987, 931] [112905828, 10, 12, 184527634, 167587189, 1145...
287 PYR 1 1054 951 Pyramus (VIII) [255, 252, 145] [997, 8, 512, 528, 645, 951] [12, 184527634, 114512891, 112905828, 167587189]
288 LC 1 928 147 Locus ceruleus [255, 195, 149] [997, 8, 343, 1065, 771, 1117, 147] [112905828, 10, 12, 167587189, 184527634]
289 VAL 1 644 629 Ventral anterior-lateral complex of the thalamus [255, 128, 132] [997, 8, 343, 1129, 549, 864, 637, 629] [112905828, 12, 184527634, 112905813, 11451289...
290 VISpor 1 213 312782628 Postrhinal area [8, 133, 140] [997, 8, 567, 688, 695, 315, 669, 312782628] [184527634, 396673091, 112905828, 167587189, 1...
291 FS 1 576 998 Fundus of striatum [128, 205, 248] [997, 8, 567, 623, 477, 493, 998] [112905828, 184527634, 12, 167587189]
292 SH 1 591 333 Septohippocampal nucleus [144, 203, 237] [997, 8, 567, 623, 477, 275, 333] [12, 184527634, 112905828, 167587189]

293 rows × 8 columns

This is how you can filter experiments by transgenic line:

In [5]:
# fetch the experiments that have injections in the isocortex of cre-positive mice
isocortex = structure_tree.get_structures_by_name(['Isocortex'])[0]
cre_cortical_experiments = mcc.get_experiments(cre=True, 
                                                injection_structure_ids=[isocortex['id']])

print "%d cre cortical experiments" % len(cre_cortical_experiments)

# same as before, but restrict the cre line
rbp4_cortical_experiments = mcc.get_experiments(cre=[ 'Rbp4-Cre_KL100' ], 
                                                injection_structure_ids=[isocortex['id']])


print "%d Rbp4 cortical experiments" % len(rbp4_cortical_experiments)
1141 cre cortical experiments
94 Rbp4 cortical experiments

Structure Signal Unionization

The ProjectionStructureUnionizes API data tells you how much signal there was in a given structure and experiment. It contains the density of projecting signal, volume of projecting signal, and other information. MouseConnectivityCache provides methods for querying and storing this data.

In [6]:
# find wild-type injections into primary visual area
visp = structure_tree.get_structures_by_acronym(['VISp'])[0]
visp_experiments = mcc.get_experiments(cre=False, 
                                       injection_structure_ids=[visp['id']])

print "%d VISp experiments" % len(visp_experiments)

structure_unionizes = mcc.get_structure_unionizes([ e['id'] for e in visp_experiments ], 
                                                  is_injection=False,
                                                  structure_ids=[isocortex['id']],
                                                  include_descendants=True)

print "%d VISp non-injection, cortical structure unionizes" % len(structure_unionizes)
32 VISp experiments
28319 VISp non-injection, cortical structure unionizes
In [7]:
structure_unionizes.head()
Out[7]:
hemisphere_id id is_injection max_voxel_density max_voxel_x max_voxel_y max_voxel_z normalized_projection_volume projection_density projection_energy projection_intensity projection_volume experiment_id structure_id sum_pixel_intensity sum_pixels sum_projection_pixel_intensity sum_projection_pixels volume
0 1 630240332 False 0.275452 6500 1820 3330 0.000041 0.000205 0.123399 603.286011 0.000033 180296424 182305709 2.758447e+10 1.323639e+08 1.633355e+07 2.707431e+04 0.162146
1 2 630239860 False 0.494745 5800 3040 9380 0.000160 0.000294 0.114581 390.172089 0.000130 180296424 889 7.897317e+10 3.621636e+08 4.149712e+07 1.063560e+05 0.443650
2 2 630239504 False 0.586985 6920 1140 7950 0.020999 0.022122 12.038620 544.199328 0.017093 180296424 361 1.552042e+11 6.307641e+08 7.593530e+09 1.395358e+07 0.772686
3 1 630240910 False 0.285612 4710 4860 2650 0.000121 0.000048 0.016843 351.533434 0.000098 180296424 104 3.509343e+11 1.676093e+09 2.823080e+07 8.030757e+04 2.053213
4 2 630239786 False 0.615703 4670 4620 8800 0.003526 0.006660 3.059985 459.459167 0.002870 180296424 783 6.654045e+10 3.517758e+08 1.076429e+09 2.342816e+06 0.430925

This is a rather large table, even for a relatively small number of experiments. You can filter it down to a smaller list of structures like this.

In [8]:
dense_unionizes = structure_unionizes[ structure_unionizes.projection_density > .5 ]
large_unionizes = dense_unionizes[ dense_unionizes.volume > .5 ]
large_structures = pd.DataFrame(structure_tree.nodes(large_unionizes.structure_id))

print "%d large, dense, cortical, non-injection unionizes, %d structures" % ( len(large_unionizes), len(large_structures) )

print large_structures.name

large_unionizes
18 large, dense, cortical, non-injection unionizes, 18 structures
0                   Postrhinal area
1                      Visual areas
2               Lateral visual area
3     Primary visual area, layer 6a
4                   Postrhinal area
5               Primary visual area
6               Lateral visual area
7         Rostrolateral visual area
8         Rostrolateral visual area
9               Lateral visual area
10              Lateral visual area
11              Lateral visual area
12              Lateral visual area
13              Lateral visual area
14              Lateral visual area
15     Primary visual area, layer 1
16              Primary visual area
17              Lateral visual area
Name: name, dtype: object
Out[8]:
hemisphere_id id is_injection max_voxel_density max_voxel_x max_voxel_y max_voxel_z normalized_projection_volume projection_density projection_energy projection_intensity projection_volume experiment_id structure_id sum_pixel_intensity sum_pixels sum_projection_pixel_intensity sum_projection_pixels volume
71 2 630240820 False 1.0 9310 3180 9690 0.626614 0.744479 4059.921430 5453.373345 0.510067 180296424 312782628 2.350927e+12 5.592923e+08 2.270683e+12 4.163813e+08 0.685133
536 2 630239708 False 1.0 9170 2300 9690 4.241160 0.522139 2128.869392 4077.206061 3.452329 180296424 669 1.279820e+13 5.397464e+09 1.149050e+13 2.818228e+09 6.611893
706 3 630241279 False 1.0 9440 2350 2440 0.712232 0.532626 2363.397654 4437.253859 0.579761 180296424 409 2.262464e+12 8.885676e+08 2.100039e+12 4.732744e+08 1.088495
778 2 630239270 False 1.0 9200 1740 8390 0.411229 0.636154 2327.454346 3658.632568 0.334743 180296424 33 1.103850e+12 4.295492e+08 9.997563e+11 2.732596e+08 0.526198
2399 2 636156255 False 1.0 9210 2680 9390 0.399948 0.612732 4104.780068 6699.147569 0.433155 309004492 312782628 2.587937e+12 5.770818e+08 2.368794e+12 3.535963e+08 0.706925
3201 2 636151215 False 1.0 9500 1260 8460 1.379541 0.508111 2117.836336 4168.060426 1.460212 307558646 385 5.716203e+12 2.345965e+09 4.968369e+12 1.192010e+09 2.873807
3399 2 636151229 False 1.0 8720 1520 9270 0.375017 0.593851 2496.282540 4203.549890 0.396947 307558646 409 1.524682e+12 5.456559e+08 1.362111e+12 3.240383e+08 0.668428
4193 2 633272766 False 1.0 7830 1500 8340 0.836458 0.627888 1960.408377 3122.223785 0.341144 113887162 417 9.620248e+11 4.435262e+08 8.694925e+11 2.784850e+08 0.543320
4666 2 634218630 False 1.0 8250 1770 8770 0.303568 0.500234 1285.347746 2569.495412 0.275880 309372716 417 6.793426e+11 4.502058e+08 5.786710e+11 2.252080e+08 0.551502
5347 2 636088739 False 1.0 9090 1440 9290 0.925616 0.669305 1829.851643 2733.958375 0.452182 307137980 409 1.121455e+12 5.515096e+08 1.009181e+12 3.691281e+08 0.675599
7333 3 636150146 False 1.0 8780 1980 2480 1.059370 0.506598 2611.704169 5155.378858 0.602429 307743253 409 2.724830e+12 9.707481e+08 2.535307e+12 4.917789e+08 1.189166
7808 2 636148419 False 1.0 9360 1730 9430 0.872533 0.966195 5588.182354 5783.703259 0.496181 307743253 409 2.355657e+12 4.192173e+08 2.342663e+12 4.050454e+08 0.513541
8093 2 636166751 False 1.0 9590 2050 9530 0.619244 0.817509 4431.448065 5420.674606 0.498723 307321674 409 2.314397e+12 4.980019e+08 2.206870e+12 4.071209e+08 0.610052
9079 2 636125869 False 1.0 9650 1820 9220 0.582058 0.598548 1902.673072 3178.812192 0.386275 307297141 409 1.172856e+12 5.268182e+08 1.002363e+12 3.153262e+08 0.645352
11356 2 636132495 False 1.0 9330 1630 9420 0.658085 0.500901 1653.509511 3301.070831 0.338421 307320960 409 1.054259e+12 5.515297e+08 9.119596e+11 2.762617e+08 0.675624
11535 2 636098918 False 1.0 9680 680 7570 0.679637 0.511493 2051.580811 4010.968262 0.352412 307593747 593 1.278747e+12 5.624387e+08 1.153888e+12 2.876833e+08 0.688987
11852 2 636098800 False 1.0 9720 1100 8280 3.377159 0.516129 1825.202443 3536.327715 1.751159 307593747 385 5.794928e+12 2.769688e+09 5.055242e+12 1.429517e+09 3.392868
12484 2 634237507 False 1.0 9440 1690 9230 0.605485 0.731085 4958.194804 6781.965420 0.479664 307296433 409 2.804687e+12 5.355905e+08 2.655562e+12 3.915623e+08 0.656098

Generating a Projection Matrix

The MouseConnectivityCache class provides a helper method for converting ProjectionStructureUnionize records for a set of experiments and structures into a matrix. This code snippet demonstrates how to make a matrix of projection density values in auditory sub-structures for cre-negative VISp experiments.

In [9]:
import numpy as np
import matplotlib.pyplot as plt
import warnings
warnings.filterwarnings('ignore')
%matplotlib inline

visp_experiment_ids = [ e['id'] for e in visp_experiments ]
ctx_children = structure_tree.child_ids( [isocortex['id']] )[0]

pm = mcc.get_projection_matrix(experiment_ids = visp_experiment_ids, 
                               projection_structure_ids = ctx_children,
                               hemisphere_ids= [2], # right hemisphere, ipsilateral
                               parameter = 'projection_density')

row_labels = pm['rows'] # these are just experiment ids
column_labels = [ c['label'] for c in pm['columns'] ] 
matrix = pm['matrix']

fig, ax = plt.subplots(figsize=(15,15))
heatmap = ax.pcolor(matrix, cmap=plt.cm.afmhot)

# put the major ticks at the middle of each cell
ax.set_xticks(np.arange(matrix.shape[1])+0.5, minor=False)
ax.set_yticks(np.arange(matrix.shape[0])+0.5, minor=False)

ax.set_xlim([0, matrix.shape[1]])
ax.set_ylim([0, matrix.shape[0]])          

# want a more natural, table-like display
ax.invert_yaxis()
ax.xaxis.tick_top()

ax.set_xticklabels(column_labels, minor=False)
ax.set_yticklabels(row_labels, minor=False)
plt.show()

Manipulating Grid Data

The MouseConnectivityCache class also helps you download and open every experiment's projection grid data volume. By default it will download 25um volumes, but you could also download data at other resolutions if you prefer (10um, 50um, 100um).

This demonstrates how you can load the projection density for a particular experiment. It also shows how to download the template volume to which all grid data is registered. Voxels in that template have been structurally annotated by neuroanatomists and the resulting labels stored in a separate annotation volume image.

In [10]:
# we'll take this experiment - an injection into the primary somatosensory - as an example
experiment_id = 181599674
In [11]:
# projection density: number of projecting pixels / voxel volume
pd, pd_info = mcc.get_projection_density(experiment_id)

# injection density: number of projecting pixels in injection site / voxel volume
ind, ind_info = mcc.get_injection_density(experiment_id)

# injection fraction: number of pixels in injection site / voxel volume
inf, inf_info = mcc.get_injection_fraction(experiment_id)

# data mask:
# binary mask indicating which voxels contain valid data
dm, dm_info = mcc.get_data_mask(experiment_id)

template, template_info = mcc.get_template_volume()
annot, annot_info = mcc.get_annotation_volume()

# in addition to the annotation volume, you can get binary masks for individual structures
# in this case, we'll get one for the isocortex
cortex_mask, cm_info = mcc.get_structure_mask(315)

print(pd_info)
print(pd.shape, template.shape, annot.shape)
{u'space origin': ['0', '0', '0'], u'space directions': [['25', '0', '0'], ['0', '25', '0'], ['0', '0', '25']], u'sizes': [528, 320, 456], u'space': 'left-posterior-superior', u'encoding': 'gzip', u'keyvaluepairs': {}, u'endian': 'little', u'kinds': ['domain', 'domain', 'domain'], u'type': 'double', u'dimension': 3}
((528, 320, 456), (528, 320, 456), (528, 320, 456))

Once you have these loaded, you can use matplotlib see what they look like.

In [12]:
# compute the maximum intensity projection (along the anterior-posterior axis) of the projection data
pd_mip = pd.max(axis=0)
ind_mip = ind.max(axis=0)
inf_mip = inf.max(axis=0)

# show that slice of all volumes side-by-side
f, pr_axes = plt.subplots(1, 3, figsize=(15, 6))

pr_axes[0].imshow(pd_mip, cmap='hot', aspect='equal')
pr_axes[0].set_title("projection density MaxIP")

pr_axes[1].imshow(ind_mip, cmap='hot', aspect='equal')
pr_axes[1].set_title("injection density MaxIP")

pr_axes[2].imshow(inf_mip, cmap='hot', aspect='equal')
pr_axes[2].set_title("injection fraction MaxIP")

plt.show()
In [13]:
# Look at a slice from the average template and annotation volumes

# pick a slice to show
slice_idx = 264

f, ccf_axes = plt.subplots(1, 3, figsize=(15, 6))

ccf_axes[0].imshow(template[slice_idx,:,:], cmap='gray', aspect='equal', vmin=template.min(), vmax=template.max())
ccf_axes[0].set_title("registration template")

ccf_axes[1].imshow(annot[slice_idx,:,:], cmap='gray', aspect='equal', vmin=0, vmax=2000)
ccf_axes[1].set_title("annotation volume")

ccf_axes[2].imshow(cortex_mask[slice_idx,:,:], cmap='gray', aspect='equal', vmin=0, vmax=1)
ccf_axes[2].set_title("isocortex mask")

plt.show()

On occasion the TissueCyte microscope fails to acquire a tile. In this case the data from that tile should not be used for analysis. The data mask associated with each experiment can be used to determine which portions of the grid data came from correctly acquired tiles.

In this experiment, a missed tile can be seen in the data mask as a dark warped square. The values in the mask exist within [0, 1], describing the fraction of each voxel that was correctly acquired

In [14]:
f, data_mask_axis = plt.subplots(figsize=(5, 6))

data_mask_axis.imshow(dm[81, :, :], cmap='hot', aspect='equal', vmin=0, vmax=1)
data_mask_axis.set_title('data mask')

plt.show()