コード例 #1
0
    def __init__(self,
                 resolution=None,
                 cache=True,
                 manifest_file=None,
                 ccf_version=None,
                 base_uri=None,
                 version=None):

        if manifest_file is None:
            manifest_file = get_default_manifest_file('mouse_connectivity')

        if version is None:
            version = self.MANIFEST_VERSION

        if resolution is None:
            resolution = MouseConnectivityApi.VOXEL_RESOLUTION_25_MICRONS

        if ccf_version is None:
            ccf_version = MouseConnectivityApi.CCF_VERSION_DEFAULT

        super(MouseConnectivityCache,
              self).__init__(resolution,
                             reference_space_key=ccf_version,
                             cache=cache,
                             manifest=manifest_file,
                             version=version)

        self.api = MouseConnectivityApi(base_uri=base_uri)
コード例 #2
0
    def __init__(self):
        # get mouse connectivity cache and structure tree
        self.mcc = MouseConnectivityCache(manifest_file=os.path.join(
            self.mouse_connectivity_cache, "manifest.json"))
        self.structure_tree = self.mcc.get_structure_tree()

        # get ontologies API and brain structures sets
        self.oapi = OntologiesApi()

        # get reference space
        self.space = ReferenceSpaceApi()
        self.spacecache = ReferenceSpaceCache(
            manifest=os.path.join(
                self.annotated_volume_fld, "manifest.json"
            ),  # downloaded files are stored relative to here
            resolution=int(self.resolution[0]),
            reference_space_key=
            "annotation/ccf_2017",  # use the latest version of the CCF
        )
        self.annotated_volume, _ = self.spacecache.get_annotation_volume()

        # mouse connectivity API [used for tractography]
        self.mca = MouseConnectivityApi()

        # Get tree search api
        self.tree_search = TreeSearchApi()
コード例 #3
0
    def __init__(self,
                 resolution=25,
                 cache=True,
                 manifest_file='mouse_connectivity_manifest.json',
                 base_uri=None):
        super(MouseConnectivityCache, self).__init__(manifest=manifest_file,
                                                     cache=cache)

        self.resolution = resolution
        self.api = MouseConnectivityApi(base_uri=base_uri)
コード例 #4
0
    def __init__(self,
                 projection_metric="projection_energy",
                 base_dir=None,
                 **kwargs):
        """ 
		Set up file paths and Allen SDKs
		
		:param base_dir: path to directory to use for saving data (default value None)
		:param path_fiprojection_metricle: - str, metric to quantify the strength of projections from the Allen Connectome. (default value 'projection_energy')
		:param kwargs: can be used to pass path to individual data folders. See brainrender/Utils/paths_manager.py

		"""

        Paths.__init__(self, base_dir=base_dir, **kwargs)

        self.projection_metric = projection_metric

        # get mouse connectivity cache and structure tree
        self.mcc = MouseConnectivityCache(manifest_file=os.path.join(
            self.mouse_connectivity_cache, "manifest.json"))
        self.structure_tree = self.mcc.get_structure_tree()

        # get ontologies API and brain structures sets
        self.oapi = OntologiesApi()
        self.get_structures_sets()

        # get reference space
        self.space = ReferenceSpaceApi()
        self.spacecache = ReferenceSpaceCache(
            manifest=os.path.join(
                self.annotated_volume, "manifest.json"
            ),  # downloaded files are stored relative to here
            resolution=self.resolution,
            reference_space_key=
            "annotation/ccf_2017"  # use the latest version of the CCF
        )
        self.annotated_volume, _ = self.spacecache.get_annotation_volume()

        # mouse connectivity API [used for tractography]
        self.mca = MouseConnectivityApi()

        # Get tree search api
        self.tree_search = TreeSearchApi()

        # Get some metadata about experiments
        self.all_experiments = self.mcc.get_experiments(dataframe=True)
        self.strains = sorted(
            [x for x in set(self.all_experiments.strain) if x is not None])
        self.transgenic_lines = sorted(
            set([
                x for x in set(self.all_experiments.transgenic_line)
                if x is not None
            ]))
コード例 #5
0
def connectivity():
    nrrd.read = MagicMock(name='nrrd_read_file',
                          return_value=('mock_annotation_data',
                                        'mock_annotation_image'))
    conn_api = MouseConnectivityApi()
    download_link = '/path/to/link'
    conn_api.do_query = MagicMock(return_value=download_link)

    conn_api.json_msg_query = MagicMock(name='json_msg_query')
    conn_api.retrieve_file_over_http = \
        MagicMock(name='retrieve_file_over_http')

    return conn_api
コード例 #6
0
def allen_get_raw_annotation(annotation_dir,
                             version='annotation/ccf_2017',
                             resolution=10):
    import nrrd
    from allensdk.api.queries.mouse_connectivity_api import MouseConnectivityApi
    annotation_path = pjoin(
        annotation_dir, 'annotation_{0}_{1}.nrrd'.format(version, resolution))
    if not os.path.isdir(annotation_dir):
        os.makedirs(annotation_dir)
    if not os.path.isfile(annotation_path):
        mcapi = MouseConnectivityApi()
        mcapi.download_annotation_volume(version, resolution, annotation_path)
    annotation, meta = nrrd.read(annotation_path)
    return annotation, meta
コード例 #7
0
def test_compute_centroid():
    # ------------------------------------------------------------------------
    # tests computed centroid is same as MouseConnApi with 'true' inj density
    # pull 'data' from mcc fixture
    a = np.random.rand(4, 4, 4)
    b = np.random.rand(4, 4, 4)

    # compute allensdk centroid
    api = MouseConnectivityApi()
    mcc_centroid = api.calculate_injection_centroid(a, b, 1)

    # 'true' injection density
    _compute_true_injection_density(a, b, inplace=True)

    assert_array_almost_equal(compute_centroid(a), mcc_centroid)
コード例 #8
0
    def __init__(self,
                 resolution=None,
                 cache=True,
                 manifest_file='mouse_connectivity_manifest.json',
                 ccf_version=None,
                 base_uri=None):
        super(MouseConnectivityCache, self).__init__(manifest=manifest_file,
                                                     cache=cache)

        if resolution is None:
            self.resolution = MouseConnectivityApi.VOXEL_RESOLUTION_25_MICRONS
        else:
            self.resolution = resolution
        self.api = MouseConnectivityApi(base_uri=base_uri)

        if ccf_version is None:
            ccf_version = MouseConnectivityApi.CCF_VERSION_DEFAULT
        self.ccf_version = ccf_version
コード例 #9
0
    def __init__(self, projection_metric="projection_energy", paths_file=None):
        """ path_file {[str]} -- [Path to a YAML file specifying paths to data folders, to replace default paths] (default: {None}) """

        Paths.__init__(self, paths_file=paths_file)

        self.projection_metric = projection_metric

        # get mouse connectivity cache and structure tree
        self.mcc = MouseConnectivityCache(manifest_file=os.path.join(
            self.mouse_connectivity_cache, "manifest.json"))
        self.structure_tree = self.mcc.get_structure_tree()

        # get ontologies API and brain structures sets
        self.oapi = OntologiesApi()
        self.get_structures_sets()

        # get reference space
        self.space = ReferenceSpaceApi()
        self.spacecache = ReferenceSpaceCache(
            manifest=os.path.join(
                "Data/ABA", "manifest.json"
            ),  # downloaded files are stored relative to here
            resolution=self.resolution,
            reference_space_key=
            "annotation/ccf_2017"  # use the latest version of the CCF
        )
        self.annotated_volume, _ = self.spacecache.get_annotation_volume()

        # mouse connectivity API [used for tractography]
        self.mca = MouseConnectivityApi()

        # Get tree search api
        self.tree_search = TreeSearchApi()

        # Get some metadata about experiments
        self.all_experiments = self.mcc.get_experiments(dataframe=True)
        self.strains = sorted(
            [x for x in set(self.all_experiments.strain) if x is not None])
        self.transgenic_lines = sorted(
            set([
                x for x in set(self.all_experiments.transgenic_line)
                if x is not None
            ]))
コード例 #10
0
    def __init__(self,  base_dir=None, **kwargs):
        """ 
        Set up file paths and Allen SDKs
        
        :param base_dir: path to directory to use for saving data (default value None)
        :param kwargs: can be used to pass path to individual data folders. See brainrender/Utils/paths_manager.py

        """

        Atlas.__init__(self, base_dir=base_dir, **kwargs)
        self.meshes_folder = self.mouse_meshes # where the .obj mesh for each region is saved

        # get mouse connectivity cache and structure tree
        self.mcc = MouseConnectivityCache(manifest_file=os.path.join(self.mouse_connectivity_cache, "manifest.json"))
        self.structure_tree = self.mcc.get_structure_tree()
        
        # get ontologies API and brain structures sets
        self.oapi = OntologiesApi()
        self.get_structures_sets()

        # get reference space
        self.space = ReferenceSpaceApi()
        self.spacecache = ReferenceSpaceCache(
            manifest=os.path.join(self.annotated_volume_fld, "manifest.json"),  # downloaded files are stored relative to here
            resolution=self.resolution,
            reference_space_key="annotation/ccf_2017"  # use the latest version of the CCF
            )
        self.annotated_volume, _ = self.spacecache.get_annotation_volume()

        # mouse connectivity API [used for tractography]
        self.mca = MouseConnectivityApi()

        # Get tree search api
        self.tree_search = TreeSearchApi()

        # Store all regions metadata [If there's internet connection]
        if self.other_sets is not None: 
            self.regions = self.other_sets["Structures whose surfaces are represented by a precomputed mesh"].sort_values('acronym')
            self.region_acronyms = list(self.other_sets["Structures whose surfaces are represented by a precomputed mesh"].sort_values(
                                                'acronym').acronym.values)
コード例 #11
0
 def __init__(self,
              mcc,
              experiment_id,
              output_dir,
              anno,
              meta,
              rsp,
              logger,
              zoom=8,
              remove_transform_data=True):
     self.remove_transform_data = remove_transform_data
     self.output_dir = output_dir
     os.makedirs(output_dir, exist_ok=True)
     self.mcc = mcc
     self.mapi = MouseConnectivityApi()
     self.anno, self.meta = anno, meta
     self.rsp = rsp
     self.zoom = 8 - zoom
     self.id = experiment_id
     assert zoom >= 0
     self.details = self.mapi.get_experiment_detail(self.id)
     image_resolution = self.details[0]['sub_images'][0]['resolution']
     self.two_d = 1.0 / image_resolution
     self.size = self.mcc.resolution * self.two_d / (2**self.zoom)
     self.dims = (self.details[0]['sub_images'][0]['height'] //
                  (2**self.zoom),
                  self.details[0]['sub_images'][0]['width'] //
                  (2**self.zoom))
     self.root_points = np.array(np.where(self.anno != 0)).T
     self.logger = logger
     self.logger.info(
         f"Initializing displacement transform data for {self.id}...")
     self.__init_transform__()
     self.logger.info(
         f"Performing displacement transformation for {self.id}...")
     self.__init_transformed_points__()
コード例 #12
0
 def __init__(self, mcc, experiment_id, directory, brain_seg_data_dir, parent_struct_id,
              experiment_fields_to_save, details, logger, default_struct_id=997):
     self.experiment_fields_to_save = experiment_fields_to_save
     self.default_struct_id = default_struct_id
     self.parent_struct_id = parent_struct_id
     self.brain_seg_data_dir = brain_seg_data_dir
     self.directory = directory
     self.mcc = mcc
     self.id = experiment_id
     mapi = MouseConnectivityApi()
     while True:
         try:
             self.details = {**details, **(mapi.get_experiment_detail(self.id)[0])}
             break
         except simplejson.errors.JSONDecodeError or urllib.error.URLError or urllib.error.URLError:
             time.sleep(1.0)
     self.logger = logger
     self.subimages = {i['section_number']: i for i in self.details['sub_images']}
     self.seg_data = np.load(f'{self.brain_seg_data_dir}/{self.id}/{self.id}-sections.npz')['arr_0']
     self.structure_tree = self.mcc.get_structure_tree()
     self.structure_ids = self.get_requested_structure_children()
     with open(f'{self.directory}/bboxes.pickle', "rb") as f:
         bboxes = pickle.load(f)
     self.bboxes = {k: v for k, v in bboxes.items() if v}
コード例 #13
0
ファイル: MakeCCFMasks.py プロジェクト: ejcorn/tau-spread
oapi = OntologiesApi()
structure_graph = oapi.get_structures_with_sets([adultMouseStructureGraphID])
# Removes some unused fields returned by the query:
structure_graph = StructureTree.clean_structures(structure_graph)
tree = StructureTree(structure_graph)

# Example:
# tree.get_structures_by_name(['Dorsal auditory area'])
# The annotation download writes a file, so we will need somwhere to put it
annotation_dir = os.path.dirname(structIDSource)
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)
コード例 #14
0
import pandas as pd
from rich.progress import track
from rich import print
from loguru import logger
from myterial import orange

try:
    from allensdk.api.queries.mouse_connectivity_api import (
        MouseConnectivityApi, )

    mca = MouseConnectivityApi()
    allen_sdk_installed = True
except ModuleNotFoundError:  # pragma: no cover
    allen_sdk_installed = False  # pragma: no cover

from brainrender._utils import listify
from brainrender._io import request
from brainrender import base_dir

streamlines_folder = base_dir / "streamlines"
streamlines_folder.mkdir(exist_ok=True)


def experiments_source_search(SOI):
    """
    Returns data about experiments whose injection was in the SOI, structure of interest
    :param SOI: str, structure of interest. Acronym of structure to use as seed for teh search
    :param source:  (Default value = True)
    """

    transgenic_id = 0  # id = 0 means use only wild type
コード例 #15
0
ファイル: aba.py プロジェクト: yooerzf/brainrender
 def __init__(self):
     # mouse connectivity API [used for tractography]
     if allen_sdk_installed:
         self.mca = MouseConnectivityApi()
     else:
         self.mca = None
コード例 #16
0
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)