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]
2995 total experiments
Out[1]:
gender                                            M
id                                        122642490
injection_structures                     [985, 993]
injection_volume                           0.151071
injection_x                                    4300
injection_y                                    2690
injection_z                                    7050
primary_injection_structure                     985
product_id                                        5
specimen_name                          Syt6-Cre-585
strain                                     C57BL/6J
structure_abbrev                                MOs
structure_id                                    993
structure_name                 Secondary motor area
transgenic_line                      Syt6-Cre_KI148
transgenic_line_id                      1.77839e+08
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, 688152357, 691663206, 6...
1 HY 1 715 1097 Hypothalamus [230, 68, 56] [997, 8, 343, 1129, 1097] [2, 112905828, 691663206, 12, 184527634, 11290...

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 structures in Isocortex layer 5 667481446 Isocortex layer 5
1 List of structures in Isocortex layer 6b 667481450 Isocortex layer 6b
2 Summary structures of the cerebellum 688152368 Cerebellum
3 List of structures for ABA Differential Search 12 ABA - Differential Search
4 List of valid structures for projection target... 184527634 Mouse Connectivity - Target Search
5 Structures whose surfaces are represented by a... 691663206 Mouse Brain - Has Surface Mesh
6 Summary structures of the midbrain 688152365 Midbrain
7 Summary structures of the medulla 688152367 Medulla
8 Summary structures of the striatum 688152361 Striatum
9 Structures representing subdivisions of the mo... 687527945 Mouse Connectivity - Summary
10 Summary structures of the hippocampal formation 688152359 Hippocampal Formation
11 List of visual cortex structures targeted for ... 514166994 Allen Brain Observatory targeted structure set
12 Summary structures of the olfactory areas 688152358 Olfactory Areas
13 Curated list of non-overlapping substructures ... 167587189 Brain – Summary Structures
14 List of structures in Isocortex layer 4 667481445 Isocortex layer 4
15 Structures representing the major divisions of... 687527670 Brain - Major Divisions
16 Summary structures of the pallidum 688152362 Pallidum
17 List of Primary injection structures for BDA/A... 114512892 Mouse Connectivity - BDA/AAV Primary Injection...
18 List of primary AND secondary injection struct... 112905813 Mouse Connectivity - BDA/AAV All Injection Str...
19 List of structures for ABA Fine Structure Search 10 ABA - Fine Structure Search
20 List of primary AND secondary injection struct... 112905828 Mouse Connectivity - Projection All Injection ...
21 List of structures in Isocortex layer 6a 667481449 Isocortex layer 6a
22 List of structures representing a areal level ... 3 Mouse - Areas
23 List of structures in Isocortex layer 1 667481440 Isocortex layer 1
24 Summary structures of the hypothalamus 688152364 Hypothalamus
25 List of structures in Isocortex layer 2/3 667481441 Isocortex layer 2/3
26 All mouse visual areas with layers 396673091 Mouse Cell Types - Structures
27 Summary structures of the cortical subplate 688152360 Cortical Subplate
28 Summary structures of the thalamus 688152363 Thalamus
29 List of structures representing a coarse level... 2 Mouse - Coarse
30 Summary structures of the isocortex 688152357 Isocortex
31 List of Primary injection structures for Proje... 114512891 Mouse Connectivity - Projection Primary Inject...
32 Summary structures of the pons 688152366 Pons

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 FRP 1 6 184 Frontal pole, cerebral cortex [38, 143, 69] [997, 8, 567, 688, 695, 315, 184] [3, 112905828, 688152357, 691663206, 687527945...
1 MOp 1 18 985 Primary motor area [31, 157, 90] [997, 8, 567, 688, 695, 315, 500, 985] [112905828, 688152357, 691663206, 687527945, 1...
2 MOs 1 24 993 Secondary motor area [31, 157, 90] [997, 8, 567, 688, 695, 315, 500, 993] [112905828, 688152357, 691663206, 687527945, 1...
3 SSp-n 1 44 353 Primary somatosensory area, nose [24, 128, 100] [997, 8, 567, 688, 695, 315, 453, 322, 353] [112905828, 688152357, 691663206, 687527945, 1...
4 SSp-bfd 1 51 329 Primary somatosensory area, barrel field [24, 128, 100] [997, 8, 567, 688, 695, 315, 453, 322, 329] [112905828, 688152357, 691663206, 687527945, 1...
5 SSp-ll 1 65 337 Primary somatosensory area, lower limb [24, 128, 100] [997, 8, 567, 688, 695, 315, 453, 322, 337] [112905828, 688152357, 691663206, 687527945, 1...
6 SSp-m 1 72 345 Primary somatosensory area, mouth [24, 128, 100] [997, 8, 567, 688, 695, 315, 453, 322, 345] [112905828, 688152357, 691663206, 687527945, 1...
7 SSp-ul 1 79 369 Primary somatosensory area, upper limb [24, 128, 100] [997, 8, 567, 688, 695, 315, 453, 322, 369] [112905828, 688152357, 691663206, 687527945, 1...
8 SSp-tr 1 86 361 Primary somatosensory area, trunk [24, 128, 100] [997, 8, 567, 688, 695, 315, 453, 322, 361] [112905828, 688152357, 691663206, 687527945, 1...
9 SSp-un 1 93 182305689 Primary somatosensory area, unassigned [24, 128, 100] [997, 8, 567, 688, 695, 315, 453, 322, 182305689] [112905828, 688152357, 691663206, 687527945, 1...
10 SSs 1 100 378 Supplemental somatosensory area [24, 128, 100] [997, 8, 567, 688, 695, 315, 453, 378] [112905828, 688152357, 691663206, 687527945, 1...
11 GU 1 107 1057 Gustatory areas [0, 156, 117] [997, 8, 567, 688, 695, 315, 1057] [3, 112905828, 688152357, 691663206, 687527945...
12 VISC 1 114 677 Visceral area [17, 173, 131] [997, 8, 567, 688, 695, 315, 677] [3, 112905828, 688152357, 691663206, 687527945...
13 AUDd 1 122 1011 Dorsal auditory area [1, 147, 153] [997, 8, 567, 688, 695, 315, 247, 1011] [112905828, 688152357, 691663206, 687527945, 1...
14 AUDp 1 136 1002 Primary auditory area [1, 147, 153] [997, 8, 567, 688, 695, 315, 247, 1002] [112905828, 688152357, 691663206, 687527945, 1...
15 AUDpo 1 143 1027 Posterior auditory area [1, 147, 153] [997, 8, 567, 688, 695, 315, 247, 1027] [112905828, 688152357, 691663206, 687527945, 1...
16 AUDv 1 150 1018 Ventral auditory area [1, 147, 153] [997, 8, 567, 688, 695, 315, 247, 1018] [112905828, 688152357, 691663206, 687527945, 1...
17 VISal 1 164 402 Anterolateral visual area [8, 133, 140] [997, 8, 567, 688, 695, 315, 669, 402] [396673091, 112905828, 688152357, 691663206, 6...
18 VISam 1 171 394 Anteromedial visual area [8, 133, 140] [997, 8, 567, 688, 695, 315, 669, 394] [396673091, 112905828, 688152357, 691663206, 6...
19 VISl 1 178 409 Lateral visual area [8, 133, 140] [997, 8, 567, 688, 695, 315, 669, 409] [396673091, 112905828, 688152357, 691663206, 6...
20 VISp 1 185 385 Primary visual area [8, 133, 140] [997, 8, 567, 688, 695, 315, 669, 385] [396673091, 112905828, 688152357, 691663206, 6...
21 VISpl 1 192 425 Posterolateral visual area [8, 133, 140] [997, 8, 567, 688, 695, 315, 669, 425] [396673091, 112905828, 688152357, 691663206, 6...
22 VISpm 1 199 533 posteromedial visual area [8, 133, 140] [997, 8, 567, 688, 695, 315, 669, 533] [396673091, 112905828, 688152357, 691663206, 6...
23 VISli 1 206 312782574 Laterointermediate area [8, 133, 140] [997, 8, 567, 688, 695, 315, 669, 312782574] [396673091, 112905828, 688152357, 691663206, 6...
24 VISpor 1 213 312782628 Postrhinal area [8, 133, 140] [997, 8, 567, 688, 695, 315, 669, 312782628] [396673091, 112905828, 688152357, 691663206, 6...
25 ACAd 1 226 39 Anterior cingulate area, dorsal part [64, 166, 102] [997, 8, 567, 688, 695, 315, 31, 39] [112905828, 688152357, 691663206, 687527945, 1...
26 ACAv 1 232 48 Anterior cingulate area, ventral part [64, 166, 102] [997, 8, 567, 688, 695, 315, 31, 48] [112905828, 688152357, 691663206, 687527945, 1...
27 PL 1 238 972 Prelimbic area [47, 168, 80] [997, 8, 567, 688, 695, 315, 972] [3, 112905828, 688152357, 691663206, 687527945...
28 ILA 1 245 44 Infralimbic area [89, 179, 99] [997, 8, 567, 688, 695, 315, 44] [3, 112905828, 688152357, 691663206, 687527945...
29 ORBl 1 258 723 Orbital area, lateral part [36, 138, 94] [997, 8, 567, 688, 695, 315, 714, 723] [112905828, 688152357, 691663206, 687527945, 1...
... ... ... ... ... ... ... ... ...
286 PPY 1 998 1069 Parapyramidal nucleus [255, 179, 217] [997, 8, 343, 1065, 354, 370, 1069] [112905828, 691663206, 687527945, 12, 68815236...
287 LAV 1 1002 209 Lateral vestibular nucleus [255, 179, 217] [997, 8, 343, 1065, 354, 370, 701, 209] [112905828, 691663206, 687527945, 12, 68815236...
288 MV 1 1003 202 Medial vestibular nucleus [255, 179, 217] [997, 8, 343, 1065, 354, 370, 701, 202] [112905828, 691663206, 687527945, 12, 68815236...
289 SPIV 1 1004 225 Spinal vestibular nucleus [255, 179, 217] [997, 8, 343, 1065, 354, 370, 701, 225] [112905828, 691663206, 687527945, 12, 68815236...
290 SUV 1 1005 217 Superior vestibular nucleus [255, 179, 217] [997, 8, 343, 1065, 354, 370, 701, 217] [112905828, 691663206, 687527945, 12, 68815236...
291 x 1 1006 765 Nucleus x [255, 179, 217] [997, 8, 343, 1065, 354, 370, 765] [112905828, 691663206, 687527945, 12, 68815236...
292 XII 1 1007 773 Hypoglossal nucleus [255, 179, 217] [997, 8, 343, 1065, 354, 370, 773] [112905828, 691663206, 687527945, 10, 12, 6881...
293 y 1 1008 781 Nucleus y [255, 179, 217] [997, 8, 343, 1065, 354, 370, 781] [112905828, 691663206, 687527945, 12, 68815236...
294 RM 1 1011 206 Nucleus raphe magnus [255, 198, 226] [997, 8, 343, 1065, 354, 379, 206] [112905828, 691663206, 687527945, 12, 68815236...
295 RPA 1 1012 230 Nucleus raphe pallidus [255, 198, 226] [997, 8, 343, 1065, 354, 379, 230] [112905828, 691663206, 687527945, 12, 68815236...
296 RO 1 1013 222 Nucleus raphe obscurus [255, 198, 226] [997, 8, 343, 1065, 354, 379, 222] [112905828, 691663206, 687527945, 12, 68815236...
297 LING 1 1020 912 Lingula (I) [255, 252, 145] [997, 8, 512, 528, 645, 912] [112905828, 691663206, 687527945, 12, 68815236...
298 CENT 1 1024 920 Central lobule [255, 252, 145] [997, 8, 512, 528, 645, 920] [112905828, 691663206, 687527945, 12, 68815236...
299 CUL 1 1033 928 Culmen [255, 252, 145] [997, 8, 512, 528, 645, 928] [112905828, 691663206, 687527945, 12, 68815236...
300 DEC 1 1046 936 Declive (VI) [255, 252, 145] [997, 8, 512, 528, 645, 936] [112905828, 691663206, 687527945, 12, 68815236...
301 FOTU 1 1050 944 Folium-tuber vermis (VII) [255, 252, 145] [997, 8, 512, 528, 645, 944] [112905828, 691663206, 687527945, 12, 68815236...
302 PYR 1 1054 951 Pyramus (VIII) [255, 252, 145] [997, 8, 512, 528, 645, 951] [112905828, 691663206, 687527945, 12, 68815236...
303 UVU 1 1058 957 Uvula (IX) [255, 252, 145] [997, 8, 512, 528, 645, 957] [112905828, 691663206, 687527945, 12, 68815236...
304 NOD 1 1062 968 Nodulus (X) [255, 252, 145] [997, 8, 512, 528, 645, 968] [112905828, 691663206, 687527945, 12, 68815236...
305 SIM 1 1067 1007 Simple lobule [255, 252, 145] [997, 8, 512, 528, 1073, 1007] [112905828, 691663206, 687527945, 12, 68815236...
306 AN 1 1071 1017 Ansiform lobule [255, 252, 145] [997, 8, 512, 528, 1073, 1017] [112905828, 691663206, 687527945, 12, 68815236...
307 PRM 1 1080 1025 Paramedian lobule [255, 252, 145] [997, 8, 512, 528, 1073, 1025] [112905828, 691663206, 687527945, 12, 68815236...
308 COPY 1 1084 1033 Copula pyramidis [255, 252, 145] [997, 8, 512, 528, 1073, 1033] [112905828, 691663206, 687527945, 12, 68815236...
309 PFL 1 1088 1041 Paraflocculus [255, 252, 145] [997, 8, 512, 528, 1073, 1041] [112905828, 691663206, 687527945, 12, 68815236...
310 FL 1 1092 1049 Flocculus [255, 252, 145] [997, 8, 512, 528, 1073, 1049] [112905828, 691663206, 687527945, 12, 68815236...
311 FN 1 1097 989 Fastigial nucleus [255, 253, 188] [997, 8, 512, 519, 989] [112905828, 691663206, 687527945, 12, 68815236...
312 IP 1 1098 91 Interposed nucleus [255, 253, 188] [997, 8, 512, 519, 91] [112905828, 691663206, 687527945, 12, 68815236...
313 DN 1 1099 846 Dentate nucleus [255, 253, 188] [997, 8, 512, 519, 846] [112905828, 691663206, 687527945, 12, 68815236...
314 VeCB 1 1100 589508455 Vestibulocerebellar nucleus [255, 253, 188] [997, 8, 512, 519, 589508455] [112905828, 691663206, 688152368, 184527634, 1...
315 fiber tracts 1 1101 1009 fiber tracts [204, 204, 204] [997, 1009] [687527945, 184527634, 167587189, 691663206]

316 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))
1211 cre cortical experiments
105 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))
33 VISp experiments
29204 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 3 636132051 False 0.135948 5700 680 7490 0.000045 0.000090 0.011458 126.726430 0.000030 307297141 1030 5.779040e+10 269614784.0 3.089182e+06 2.437678e+04 0.330278
1 2 636125427 False 0.239197 4670 1090 6950 0.000218 0.000198 0.057760 292.118347 0.000145 307297141 320 1.609707e+11 598041920.0 3.454268e+07 1.182489e+05 0.732601
2 1 636130167 False 1.000000 9210 1990 2790 0.028052 0.205331 319.533539 1556.188721 0.018616 307297141 305 4.455195e+10 74012400.0 2.364944e+10 1.519703e+07 0.090665
3 1 636130887 False 0.000000 0 0 0 0.000000 0.000000 0.000000 0.000000 0.000000 307297141 2 6.701981e+09 51414300.0 0.000000e+00 0.000000e+00 0.062983
4 1 636128375 False 0.000000 0 0 0 0.000000 0.000000 0.000000 0.000000 0.000000 307297141 1010 2.252017e+10 81598496.0 0.000000e+00 0.000000e+00 0.099958

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               Lateral visual area
1         Rostrolateral visual area
2                   Postrhinal area
3                      Visual areas
4               Lateral visual area
5     Primary visual area, layer 6a
6               Lateral visual area
7               Primary visual area
8               Lateral visual area
9               Lateral visual area
10              Lateral visual area
11              Lateral visual area
12              Lateral visual area
13                  Postrhinal area
14              Lateral visual area
15     Primary visual area, layer 1
16              Primary visual area
17        Rostrolateral 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
229 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
1538 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
1841 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
2306 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
2476 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
2548 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
3668 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
5856 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
6054 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
6931 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
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
15081 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
17443 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
20448 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
22154 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
22471 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
27675 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

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)
{'keyvaluepairs': {}, 'type': 'double', 'dimension': 3, 'space': 'left-posterior-superior', 'sizes': [528, 320, 456], 'space directions': [['25', '0', '0'], ['0', '25', '0'], ['0', '0', '25']], 'kinds': ['domain', 'domain', 'domain'], 'endian': 'little', 'encoding': 'gzip', 'space origin': ['0', '0', '0']}
(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()