Reference Space

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.

Constructing a StructureTree

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.

In [1]:
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)
In [2]:
# now let's take a look at a structure
tree.get_structures_by_name(['Dorsal auditory area'])
Out[2]:
[{'acronym': u'AUDd',
  'graph_id': 1,
  'graph_order': 122,
  'id': 1011,
  'name': u'Dorsal auditory area',
  'rgb_triplet': [1, 147, 153],
  'structure_id_path': [997, 8, 567, 688, 695, 315, 247, 1011],
  'structure_set_ids': [112905828, 184527634, 114512891, 12, 167587189]}]

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

Using a StructureTree

In [3]:
# get a structure's parent
tree.parents([1011])
Out[3]:
[{'acronym': u'AUD',
  'graph_id': 1,
  'graph_order': 121,
  'id': 247,
  'name': u'Auditory areas',
  'rgb_triplet': [1, 147, 153],
  'structure_id_path': [997, 8, 567, 688, 695, 315, 247],
  'structure_set_ids': [112905828, 184527634, 3, 12, 114512891]}]
In [4]:
# get a dictionary mapping structure ids to names

name_map = tree.get_name_map()
name_map[247]
Out[4]:
u'Auditory areas'
In [5]:
# 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]) )
Primary visual area is not in Auditory areas
In [6]:
# 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] )
VISp

Downloading an annotation volume

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.

In [7]:
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)
2017-10-18 10:31:52,237 allensdk.api.api.retrieve_file_over_http INFO     Downloading URL: http://download.alleninstitute.org/informatics-archive/current-release/mouse_ccf/annotation/ccf_2017/annotation_25.nrrd

Constructing a ReferenceSpace

In [8]:
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])

Using a ReferenceSpace

making structure masks

The simplest use of a Reference space is to build binary indicator masks for structures or groups of structures.

In [9]:
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)
Out[9]:
<matplotlib.image.AxesImage at 0x7f8aa098c2d0>

What if you want a mask for a whole collection of ontologically disparate structures? Just pass more structure ids to make_structure_masks:

In [10]:
# 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)
Out[10]:
<matplotlib.image.AxesImage at 0x7f8a9a4e1b50>

You can also make and store a number of structure_masks at once:

In [11]:
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)
made mask for structure 385.
made mask for structure 1097.
Out[11]:
['annotation.nrrd', 'structure_385.nrrd', 'structure_1097.nrrd']

Removing unassigned structures

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").

In [12]:
# 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()
voxel count for structure 12997: 0
Out[12]:
False

View a slice from the annotation

In [13]:
import numpy as np

fig, ax = plt.subplots(figsize=(10, 10))
plt.imshow(rsp.get_slice_image(1, 5000), interpolation='none')
Out[13]:
<matplotlib.image.AxesImage at 0x7f8a9016cf10>

Downsample the space

If you want an annotation at a resolution we don't provide, you can make one with the downsample method.

In [14]:
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 )
(528, 320, 456)
(176, 107, 152)

Now view the downsampled space:

In [15]:
fig, ax = plt.subplots(figsize=(10, 10))
plt.imshow(sf_rsp.get_slice_image(1, 5000), interpolation='none')
Out[15]:
<matplotlib.image.AxesImage at 0x7f8a900a9ad0>