# 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.

The main entry point will be through the ReferenceSpaceCache class. The ReferenceSpaceCache class has methods for downloading, storing, and constructing StructureTrees, annotations, and ReferenceSpaces.

## 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. The ReferenceSpaceCache takes care of that for you.

For a list of atlases and corresponding structure graph ids, see here.

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¶

You can obtain annotation volumes through the ReferenceSpaceCache which stores a nrrd file containing the Allen Common Coordinate Framework annotation on your hard drive. Above we set the resolution for annotations when we initialized the ReferenceSpaceCache to 25-micron isometric spacing. The orientation of this space is:

* Anterior -> Posterior
* Superior -> Inferior
* Left -> Right

## Constructing a ReferenceSpace¶

A reference space is built from a structure tree and an annotation volume. We can obtain our ReferenceSpace object using ReferenceSpaceCache which will load everything from the cache since we have already downloaded the files for use above.

## Using a ReferenceSpace¶

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

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

You can also make and store a number of structure_masks at once, however this is only accessible using the ReferenceSpace class directly:

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

#### Downsample the space¶

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

Now view the downsampled space:

## Exporting¶

if you want to work with our reference space data using tools made by vendors other than the Allen Institute for Brain Science, you may need to do some conversion work. The AllenSDK contains a few convenience functions which implement common conversions. If you don't see the one you need, mention it on our issues page.

#### ITKSnap¶

ITKSnap is a tool for viewing and segmenting biological volume data. Our annotation team uses it to draw CCF annotations. If you have data aligned to one of our reference spaces, you can use the AllenSDK to export an ITKSnap compatible segmentation volume and corresponding label description file.

Note: our structure ids are 32-bit unsigned integers and may exceed ITKSnap's maximum allowable label value (65535). If any of the ids in a structure tree do exceed this value, the write_itksnap_labels method will automatically remap the ids to a set of smaller numbers.