This notebook contains example code demonstrating the use of the StructureTree and ReferenceSpace classes. These classes provide methods for interacting with the 3d spaces to which Allen Institute data and atlases are registered.
Unlike the AllenSDK cache classes, StructureTree and ReferenceSpace operate entirely in memory. We recommend using json files to store text and nrrd files to store volumetric images.
The MouseConnectivityCache class has methods for downloading, storing, and constructing StructureTrees and ReferenceSpaces. Please see here for examples.
A StructureTree object is a wrapper around a structure graph - a list of dictionaries documenting brain structures and their containment relationships. To build a structure tree, you will first need to obtain a structure graph.
For a list of atlases and corresponding structure graph ids, see here.
from allensdk.api.queries.ontologies_api import OntologiesApi
from allensdk.core.structure_tree import StructureTree
oapi = OntologiesApi()
structure_graph = oapi.get_structures_with_sets([1]) # 1 is the id of the adult mouse structure graph
# This removes some unused fields returned by the query
structure_graph = StructureTree.clean_structures(structure_graph)
tree = StructureTree(structure_graph)
# now let's take a look at a structure
tree.get_structures_by_name(['Dorsal auditory area'])
The fields are:
* acronym: a shortened name for the structure
* rgb_triplet: each structure is assigned a consistent color for visualizations
* graph_id: the structure graph to which this structure belongs
* graph_order: each structure is assigned a consistent position in the flattened graph
* id: a unique integer identifier
* name: the full name of the structure
* structure_id_path: traces a path from the root node of the tree to this structure
* structure_set_ids: the structure belongs to these predefined groups
# get a structure's parent
tree.parents([1011])
# get a dictionary mapping structure ids to names
name_map = tree.get_name_map()
name_map[247]
# ask whether one structure is contained within another
strida = 385
stridb = 247
is_desc = '' if tree.structure_descends_from(385, 247) else ' not'
print( '{0} is{1} in {2}'.format(name_map[strida], is_desc, name_map[stridb]) )
# build a custom map that looks up acronyms by ids
# the syntax here is just a pair of node-wise functions.
# The first one returns keys while the second one returns values
acronym_map = tree.value_map(lambda x: x['id'], lambda y: y['acronym'])
print( acronym_map[385] )
This code snippet will download and store a nrrd file containing the Allen Common Coordinate Framework annotation. We have requested an annotation with 25-micron isometric spacing. The orientation of this space is:
* Anterior -> Posterior
* Superior -> Inferior
* Left -> Right
This is the no-frills way to download an annotation volume. See the mouse connectivity examples if you want to properly cache the downloaded data.
import os
import nrrd
from allensdk.api.queries.mouse_connectivity_api import MouseConnectivityApi
from allensdk.config.manifest import Manifest
# the annotation download writes a file, so we will need somwhere to put it
annotation_dir = 'annotation'
Manifest.safe_mkdir(annotation_dir)
annotation_path = os.path.join(annotation_dir, 'annotation.nrrd')
# this is a string which contains the name of the latest ccf version
annotation_version = MouseConnectivityApi.CCF_VERSION_DEFAULT
mcapi = MouseConnectivityApi()
mcapi.download_annotation_volume(annotation_version, 25, annotation_path)
annotation, meta = nrrd.read(annotation_path)
from allensdk.core.reference_space import ReferenceSpace
# build a reference space from a StructureTree and annotation volume, the third argument is
# the resolution of the space in microns
rsp = ReferenceSpace(tree, annotation, [25, 25, 25])
The simplest use of a Reference space is to build binary indicator masks for structures or groups of structures.
import matplotlib.pyplot as plt
%matplotlib inline
# A complete mask for one structure
whole_cortex_mask = rsp.make_structure_mask([315])
# view in coronal section
fig, ax = plt.subplots(figsize=(10, 10))
plt.imshow(whole_cortex_mask[150, :], interpolation='none', cmap=plt.cm.afmhot)
What if you want a mask for a whole collection of ontologically disparate structures? Just pass more structure ids to make_structure_masks:
# This gets all of the structures targeted by the Allen Brain Observatory project
brain_observatory_structures = rsp.structure_tree.get_structures_by_set_id([514166994])
brain_observatory_ids = [st['id'] for st in brain_observatory_structures]
brain_observatory_mask = rsp.make_structure_mask(brain_observatory_ids)
# view in horizontal section
fig, ax = plt.subplots(figsize=(10, 10))
plt.imshow(brain_observatory_mask[:, 40, :], interpolation='none', cmap=plt.cm.afmhot)
You can also make and store a number of structure_masks at once:
import functools
# Define a wrapper function that will control the mask generation.
# This one checks for a nrrd file in the specified base directory
# and builds/writes the mask only if one does not exist
mask_writer = functools.partial(ReferenceSpace.check_and_write, annotation_dir)
# many_structure_masks is a generator - nothing has actrually been run yet
mask_generator = rsp.many_structure_masks([385, 1097], mask_writer)
# consume the resulting iterator to make and write the masks
for structure_id in mask_generator:
print( 'made mask for structure {0}.'.format(structure_id) )
os.listdir(annotation_dir)
A structure graph may contain structures that are not used in a particular reference space. Having these around can complicate use of the reference space, so we generally want to remove them.
We'll try this using "Somatosensory areas, layer 6a" as a test case. In the 2016 ccf space, this structure is unused in favor of finer distinctions (e.g. "Primary somatosensory area, barrel field, layer 6a").
# Double-check the voxel counts
no_voxel_id = rsp.structure_tree.get_structures_by_name(['Somatosensory areas, layer 6a'])[0]['id']
print( 'voxel count for structure {0}: {1}'.format(no_voxel_id, rsp.total_voxel_map[no_voxel_id]) )
# remove unassigned structures from the ReferenceSpace's StructureTree
rsp.remove_unassigned()
# check the structure tree
no_voxel_id in rsp.structure_tree.node_ids()
import numpy as np
fig, ax = plt.subplots(figsize=(10, 10))
plt.imshow(rsp.get_slice_image(1, 5000), interpolation='none')
If you want an annotation at a resolution we don't provide, you can make one with the downsample method.
import warnings
target_resolution = [75, 75, 75]
# in some versions of scipy, scipy.ndimage.zoom raises a helpful but distracting
# warning about the method used to truncate integers.
warnings.simplefilter('ignore')
sf_rsp = rsp.downsample(target_resolution)
# re-enable warnings
warnings.simplefilter('default')
print( rsp.annotation.shape )
print( sf_rsp.annotation.shape )
Now view the downsampled space:
fig, ax = plt.subplots(figsize=(10, 10))
plt.imshow(sf_rsp.get_slice_image(1, 5000), interpolation='none')