def get(self, structure_graph_id):
        # from: https://allensdk.readthedocs.io/en/latest/_static/examples/nb/reference_space.html#Constructing-a-structure-tree
        # http://help.brain-map.org/display/api/Atlas+Drawings+and+Ontologies

        # ! we need to use different FOLDERS for each structure-graph. else, structures.json gets overwritten by the last retrieved species/graph-id. yikes...
        rspc = ReferenceSpaceCache(
            self.resolution,
            self.reference_space_key,
            manifest=f'cache\\reference_space\\{structure_graph_id}\\cache.json'
        )

        # doc @ http://api.brain-map.org/doc/Structure.html
        tree = rspc.get_structure_tree(structure_graph_id=structure_graph_id)
        #annotation, meta = rspc.get_annotation_volume()

        # https://allensdk.readthedocs.io/en/latest/_static/examples/nb/reference_space.html
        name_map = tree.get_name_map()
        # from the readthedocs.io-sample: create a mapping from id to acronym.
        acronym_map = tree.value_map(
            lambda x: x['id'], lambda y: y['acronym']
        )  # tree.get_id_acronym_map() # tree.value_map(lambda x: x['id'], lambda y: y['acronym'])

        # from https://datatofish.com/dictionary-to-dataframe/
        structure_map = pd.DataFrame(list(name_map.items()),
                                     columns=[
                                         'structure_id', 'structure_name'
                                     ]).set_index(['structure_id'])

        # the index (= structure-id) is mapped to an acronym:
        structure_map['acronym'] = structure_map.index.map(acronym_map)

        # this transforms the ancestor_id_map into a dataframe containing the structure_id and the respective ancestors.
        ancestor_map = pd.DataFrame([
            {
                "structure_id": k,
                # ancestors of a structure are obtained as a list, using get_ancestor_id_map()
                # but instead of the ancestor's id, we want the name. hence, we lookup using the name_map
                "ancestors": [
                    name_map[id] for id in reversed(v)
                ]  # we reverse the ancestors to always have the root in first position. 
                # from there, we go into finer structures from left to right. this way, we can have null-values for the root-nodes
            } for k, v in tree.get_ancestor_id_map().items()
        ])

        ancestor_map = ancestor_map.set_index(['structure_id'])

        # ancestors are stored as a list in one column. but we want separate columns for each ancestor-level:
        # https://stackoverflow.com/questions/35491274/pandas-split-column-of-lists-into-multiple-columns
        ancestor_map = ancestor_map.ancestors.apply(
            pd.Series).add_prefix('level_')
        ancestor_map = ancestor_map.where(pd.notnull(ancestor_map), None)

        return structure_map.merge(ancestor_map,
                                   left_index=True,
                                   right_index=True,
                                   how="left").sort_index()
Example #2
0
    reference_space_key="annotation/ccf_2017"
    # use the latest version of the CCF
)

# Download
annotated_volume, _ = spacecache.get_annotation_volume()
template_volume, _ = spacecache.get_template_volume()

# Save tiff stacks:
tifffile.imsave(str(save_dir / "reference.tiff"), template_volume)
tifffile.imsave(str(save_dir / "annotated.tiff"), annotated_volume)

# Download structures tree and meshes:
######################################
oapi = OntologiesApi()  # ontologies
struct_tree = spacecache.get_structure_tree()  # structures tree

# Find id of set of regions with mesh:
select_set = "Structures whose surfaces are represented by a precomputed mesh"

all_sets = pd.DataFrame(oapi.get_structure_sets())
mesh_set_id = all_sets[all_sets.description == select_set].id.values[0]

structs_with_mesh = struct_tree.get_structures_by_set_id([mesh_set_id])

meshes_dir = (save_dir / "meshes")  # directory to save meshes into
space = ReferenceSpaceApi()
for s in structs_with_mesh:
    name = s["id"]
    try:
        space.download_structure_mesh(structure_id=s["id"],