def allen_volume_from_structures(structures=selection_dorsal_cortex, resolution=10, version='annotation/ccf_2017'): ''' Gets specific regions from an annotation volume from the allen atlas ''' from allensdk.api.queries.mouse_connectivity_api import MouseConnectivityApi from allensdk.api.queries.ontologies_api import OntologiesApi from allensdk.core.structure_tree import StructureTree from allensdk.core.reference_space import ReferenceSpace # the annotation download writes a file, so we will need somwhere to put it # the annotation download writes a file, so we will need somwhere to put it annotation, meta = allen_get_raw_annotation(annotation_dir, version=version, resolution=10) 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) rsp = ReferenceSpace(tree, annotation, [resolution] * 3) areas = rsp.structure_tree.get_structures_by_acronym(structures) ids = [st['id'] for st in areas] mask_volume = np.zeros_like(annotation, dtype='int16') for i, sid in tqdm(enumerate(ids)): masks = rsp.make_structure_mask([sid]) mask_volume[masks == 1] = i + 1 areas[i]['mask_volume_id'] = i + 1 return mask_volume, areas
def Ref(): oapi = OntologiesApi() structure_graph = oapi.get_structures_with_sets([1]) structure_graph = StructureTree.clean_structures(structure_graph) tree = StructureTree(structure_graph) # "need to download annotation the first time" # annotation_dir = 'annotation' # Manifest.safe_mkdir(annotation_dir) # annotation_path = os.path.join(annotation_dir, 'annotation.nrrd') annotation_path = 'annotation_2017_25.nrrd' # mcapi = MouseConnectivityApi() # mcapi.download_annotation_volume('annotation/ccf_2016', 25, annotation_path) annotation, meta = nrrd.read(annotation_path) rsp = ReferenceSpace(tree, annotation, [25, 25, 25]) import pandas as pd df = pd.read_csv('ontology_170731.csv') idList = df['id'] acronymList = df['acronym'] nameList = df['name'] voxel_count = [rsp.total_voxel_map[int(stid)] for stid in df['id']] df = pd.DataFrame(np.column_stack( [idList, acronymList, nameList, voxel_count]), columns=['ID', 'Acronym', 'Name', 'Total Voxel']) df.to_csv('s2_{}.csv'.format(time.strftime('%y%m%d')))
def rsp(): tree = [{ 'id': 1, 'structure_id_path': [1] }, { 'id': 2, 'structure_id_path': [1, 2] }, { 'id': 3, 'structure_id_path': [1, 3] }, { 'id': 4, 'structure_id_path': [1, 2, 4] }, { 'id': 5, 'structure_id_path': [1, 2, 5] }, { 'id': 6, 'structure_id_path': [1, 2, 5, 6] }, { 'id': 7, 'structure_id_path': [1, 7] }] # leaves are 6, 4, 3 # additionally annotate 2, 5 for realism :) annotation = np.zeros((10, 10, 10)) annotation[4:8, 4:8, 4:8] = 2 annotation[5:7, 5:7, 5:7] = 5 annotation[5:7, 5:7, 5] = 6 annotation[7, 7, 7] = 4 annotation[8:10, 8:10, 8:10] = 3 return ReferenceSpace(StructureTree(tree), annotation, [10, 10, 10])
def mcc(tree, annotation): # data shape = (10, 10, 10) data_mask = np.ones(shape) injection_density = np.ones(shape) injection_fraction = np.ones(shape) projection_density = np.ones(shape) # mock mcc = mock.Mock(manifest_file="manifest_file") mcc.get_data_mask.return_value = (data_mask, ) mcc.get_injection_density.return_value = (injection_density, ) mcc.get_injection_fraction.return_value = (injection_fraction, ) mcc.get_projection_density.return_value = (projection_density, ) mcc.get_experiments.return_value = [{"id": 456}, {"id": 12}, {"id": 315}] # reference space rsp = ReferenceSpace(StructureTree(tree), annotation, [10, 10, 10]) mcc.get_reference_space.return_value = rsp mcc.get_structure_tree.return_value = StructureTree(tree) return mcc
def get_reference_space(resolution=100): if isinstance(resolution, int): resolution = [resolution, resolution, resolution] for r in resolution: if r not in [10, 25, 50, 100]: raise ValueError( f'Axis resolution must in 10, 25, 50, 100 micron, got {r}.') resolution_file = min(resolution) annotation_grid_path = PACKAGE_DIR / f'data/ccf2017_annotation_{resolution_file}.nrrd' structure_graph_path = PACKAGE_DIR / f'data/StructureGraph_Set1_Adult_Mouse_Brain.json' import nrrd from allensdk.core.structure_tree import StructureTree from allensdk.core.reference_space import ReferenceSpace annotation, meta = nrrd.read(str(annotation_grid_path)) with open(structure_graph_path) as f: # This removes some unused fields returned by the query structure_graph = json.load(f) structure_graph = StructureTree.clean_structures(structure_graph) tree = StructureTree(structure_graph) rsp = ReferenceSpace(tree, annotation, resolution) return rsp, tree
def itksnap_rsp(): tree = [ { 'id': 1, 'rgb_triplet': [1, 2, 3], 'acronym': 'b', 'structure_id_path': [1] }, { 'id': 5000, 'rgb_triplet': [4, 5, 6], 'acronym': 'a', 'structure_id_path': [1, 5000] }, ] annotation = np.zeros((10, 10, 10)) annotation[:, :, :5] = 1 annotation[:, :, 7:] = 5000 return ReferenceSpace(StructureTree(tree), annotation, [10, 10, 10])
Manifest.safe_mkdir(annotation_dir) annotation_path = os.path.join(annotation_dir, 'annotation.nrrd') #------------------------------------------------------------------------------- # Use the connectivity API: mcapi = MouseConnectivityApi() # The name of the latest ccf version (a string): annotation_version = mcapi.CCF_VERSION_DEFAULT if not os.path.exists(annotation_path): mcapi.download_annotation_volume(annotation_version, resolution, annotation_path) annotation, meta = nrrd.read(annotation_path) # 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, [resolution, resolution, resolution]) #------------------------------------------------------------------------------- #------------------------------------------------------------------------------- # So now we're ready to go through structures, and extract their coordinates structureID_df = pd.read_csv(structIDSource) structureIDs = structureID_df['ids'].to_numpy() print("Retrieved %u structures from %s..." % (len(structureIDs), structIDSource)) # A complete mask for one structure # midPoint = 227 # Middle of brain (z-coordinate) # coOrds = np.zeros((len(structureIDs),3)) # Assign labels to each voxel according to the list of structureIDs for ind, sID in enumerate(structureIDs): structure_mask = rsp.make_structure_mask([sID])
def test_notebook(fn_temp_dir): # coding: utf-8 # # 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](https://alleninstitute.github.io/AllenSDK/_static/examples/nb/mouse_connectivity.html) 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](http://help.brain-map.org/display/api/Atlas+Drawings+and+Ontologies). # 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']) # 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.parent([1011]) # In[4]: # get a dictionary mapping structure ids to names name_map = tree.get_name_map() name_map[247] # 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])) # 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]) # ## 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 <a href='_static/examples/nb/mouse_connectivity.html#Manipulating-Grid-Data'>mouse connectivity</a> 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') mcapi = MouseConnectivityApi() mcapi.download_annotation_volume('annotation/ccf_2016', 25, annotation_path) annotation, meta = nrrd.read(annotation_path) # ## 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]: # A complete mask for one structure whole_cortex_mask = rsp.make_structure_mask([315]) # view in coronal section # 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 # 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) # #### 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() # #### View a slice from the annotation # In[13]: import numpy as np # #### 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)
annotation_path = os.path.join(annotation_dir, 'annotation_10.nrrd') # this is a string which contains the name of the latest ccf version annotation_version = MouseConnectivityApi.CCF_VERSION_DEFAULT mcapi = MouseConnectivityApi() #Next line commented because the annotation volume is already downloaded mcapi.download_annotation_volume(annotation_version, 10, annotation_path) annotation, meta = nrrd.read(annotation_path) swapped_ann = np.swapaxes(annotation, 1, 2) swapped_ann = swapped_ann[:, :, :: -1] #Revert the z axis so the 0 is the ventral part rsp = ReferenceSpace(tree, swapped_ann, [10, 10, 10]) root_path = "E:\\Histology\\brain_structures_half_not_close_10\\" ##Here comes the obj creation for struct in structure_graph[:1]: path_parent = "" for parent_id in struct["structure_id_path"][:-1]: name_parent = tree.get_structures_by_id([parent_id])[0]["acronym"] name_parent = name_parent.replace("/", "-") path_parent = path_parent + name_parent + "\\" struct_id = struct["id"] name = tree.get_structures_by_id( [struct_id])[0]["name"] + " (" + tree.get_structures_by_id( [struct_id])[0]["acronym"] + ")" name = name.replace("/", "-") export_obj(struct_id, name, root_path + path_parent)