Interacting with cloud atlas data through Python#

This example demonstrates how to calculate the volume of the retrosplenial area in the Allen mouse brain atlas at 25um resolution. It does so avoiding use of BrainGlobe tools, and using minimal other dependencies. It is intended to showcase the use of atlas data directly.

This tutorial will guide through the steps needed to extract the volume of the retrosplenial area (RSP) in the Allen Mouse Brain Atlas, programmatically - but without using BrainGlobe. To do this, we need some preliminary knowledge about how BrainGlobe atlases such as this are structured under the hood:

  • the annotation image is stored in a zipped zarr array.

  • the region metadata (e.g. each region’s id, name, acronym and parent) are stored in a comma separated (csv) file

  • these files (and other atlas files, like the template) are stored on AWS

To compute the volume of RSP we will therefore

  • download the structures file

  • determine the id of the RSP region and all its children from the structures file

  • download the annotation file

  • count how many pixels in the annotation file correspond to any of the ids

  • convert the pixels to cubic millimeters

Now the conceptual part is covered, let’s dive into the code:

We start by importing some Python libraries that we will need

import pooch
import numpy as np
import zarr
from pathlib import Path
from matplotlib import pyplot as plt
from matplotlib import colormaps as cm

Next, we download the structures file using pooch

atlas_url = "https://gin.g-node.org/BrainGlobe/atlases-v2/raw/master/annotations/allen_mouse_v1/"
structures_file = "structures.csv"

csv_path = pooch.retrieve(
    url=atlas_url+"/"+structures_file,
    known_hash="a04e9f6656c98bcd6d9d4c0ba2bd0ef927cf5cb97658ded9cda8237ad4575ff2",
    path=pooch.os_cache("brainglobe_atlases")
)
Downloading data from 'https://gin.g-node.org/BrainGlobe/atlases-v2/raw/master/annotations/allen_mouse_v1//structures.csv' to file '/home/runner/.cache/brainglobe_atlases/8bdfd39b6d25a0c1d1f84bfb5e051b9b-structures.csv'.

By printing the rows, we can observe that the structures file contains one row per region, with the first column (index 0) containing the region acronym, and the second column (index 1) containing the region ID:

with open(csv_path) as file:
    for i, row in enumerate(file):
        if i >= 10:
            break
        print(row)
acronym,id,name,structure_id_path,parent_structure_id

VI,653,Abducens nucleus,/997/8/343/1065/354/370/653/,370.0

ACVII,576,Accessory facial motor nucleus,/997/8/343/1065/354/370/576/,370.0

AOB,151,Accessory olfactory bulb,/997/8/567/688/695/698/151/,698.0

AOBgl,188,"Accessory olfactory bulb, glomerular layer",/997/8/567/688/695/698/151/188/,151.0

AOBgr,196,"Accessory olfactory bulb, granular layer",/997/8/567/688/695/698/151/196/,151.0

AOBmi,204,"Accessory olfactory bulb, mitral layer",/997/8/567/688/695/698/151/204/,151.0

ASO,332,Accessory supraoptic group,/997/8/343/1129/1097/157/332/,157.0

Acs5,549009219,Accessory trigeminal nucleus,/997/8/343/1065/771/987/549009219/,987.0

AI,95,Agranular insular area,/997/8/567/688/695/315/95/,315.0

We know that all child regions of RSP have a name starting with “RSP”, which we can use to identify our IDs of interest and store them in a list called rsp_ids.

rsp_ids = []

with open(csv_path) as file:
    for row in file:
        entries = row.split(",")
        if entries[0].startswith("RSP"):
            rsp_ids.append(int(entries[1]))

Equipped with this information, we can now download the annotations file for the atlas. Annotation files are stored in zipped zarr files in the cloud.

ZARR_ANNOTATIONS_FILENAME = "25um.zarr.zip"
zarr_path = pooch.retrieve(
    url=atlas_url+"/"+ZARR_ANNOTATIONS_FILENAME,
    known_hash="4a28ba2b3f25cea9c7d2c944ab42014c9f09aaef3bb32f14b9fcccc536a6140f",
    processor=pooch.Unzip(),
    path=pooch.os_cache("brainglobe_atlases")
)

print(zarr_path)
Downloading data from 'https://gin.g-node.org/BrainGlobe/atlases-v2/raw/master/annotations/allen_mouse_v1//25um.zarr.zip' to file '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip'.
Unzipping contents of '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip' to '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip'
['/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/zarr.json', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/6/6/1', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/6/6/2', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/6/6/0', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/6/6/3', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/6/5/1', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/6/5/2', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/6/5/0', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/6/5/3', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/6/7/1', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/6/7/2', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/6/1/1', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/6/1/2', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/6/1/0', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/6/1/3', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/6/2/1', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/6/2/2', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/6/2/0', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/6/2/3', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/6/4/1', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/6/4/2', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/6/4/0', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/6/4/3', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/6/0/1', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/6/0/2', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/6/3/1', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/6/3/2', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/6/3/0', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/6/3/3', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/5/6/1', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/5/6/2', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/5/6/0', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/5/6/3', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/5/5/1', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/5/5/2', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/5/5/0', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/5/5/3', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/5/7/1', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/5/7/2', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/5/7/0', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/5/7/3', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/5/1/1', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/5/1/2', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/5/1/0', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/5/1/3', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/5/2/1', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/5/2/2', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/5/2/0', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/5/2/3', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/5/4/1', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/5/4/2', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/5/4/0', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/5/4/3', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/5/0/1', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/5/0/2', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/5/0/0', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/5/0/3', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/5/3/1', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/5/3/2', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/5/3/0', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/5/3/3', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/7/6/1', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/7/6/2', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/7/6/0', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/7/6/3', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/7/5/1', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/7/5/2', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/7/5/0', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/7/5/3', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/7/7/1', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/7/7/2', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/7/1/1', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/7/1/2', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/7/1/0', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/7/1/3', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/7/2/1', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/7/2/2', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/7/2/0', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/7/2/3', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/7/4/1', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/7/4/2', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/7/4/0', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/7/4/3', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/7/0/1', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/7/0/2', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/7/3/1', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/7/3/2', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/7/3/0', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/7/3/3', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/1/6/1', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/1/6/2', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/1/5/1', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/1/5/2', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/1/5/0', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/1/5/3', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/1/1/1', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/1/1/2', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/1/2/1', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/1/2/2', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/1/2/0', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/1/2/3', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/1/4/1', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/1/4/2', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/1/4/0', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/1/4/3', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/1/3/1', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/1/3/2', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/1/3/0', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/1/3/3', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/2/6/1', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/2/6/2', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/2/6/0', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/2/6/3', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/2/5/1', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/2/5/2', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/2/5/0', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/2/5/3', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/2/7/1', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/2/7/2', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/2/1/1', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/2/1/2', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/2/1/0', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/2/1/3', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/2/2/1', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/2/2/2', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/2/2/0', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/2/2/3', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/2/4/1', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/2/4/2', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/2/4/0', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/2/4/3', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/2/0/1', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/2/0/2', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/2/3/1', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/2/3/2', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/2/3/0', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/2/3/3', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/4/6/1', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/4/6/2', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/4/6/0', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/4/6/3', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/4/5/1', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/4/5/2', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/4/5/0', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/4/5/3', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/4/7/1', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/4/7/2', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/4/7/0', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/4/7/3', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/4/1/1', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/4/1/2', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/4/1/0', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/4/1/3', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/4/2/1', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/4/2/2', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/4/2/0', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/4/2/3', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/4/4/1', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/4/4/2', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/4/4/0', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/4/4/3', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/4/0/1', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/4/0/2', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/4/0/0', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/4/0/3', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/4/3/1', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/4/3/2', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/4/3/0', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/4/3/3', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/0/6/1', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/0/6/2', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/0/5/1', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/0/5/2', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/0/2/1', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/0/2/2', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/0/4/1', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/0/4/2', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/0/3/1', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/0/3/2', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/3/6/1', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/3/6/2', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/3/6/0', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/3/6/3', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/3/5/1', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/3/5/2', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/3/5/0', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/3/5/3', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/3/7/1', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/3/7/2', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/3/7/0', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/3/7/3', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/3/1/1', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/3/1/2', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/3/1/0', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/3/1/3', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/3/2/1', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/3/2/2', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/3/2/0', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/3/2/3', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/3/4/1', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/3/4/2', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/3/4/0', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/3/4/3', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/3/0/1', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/3/0/2', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/3/0/0', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/3/0/3', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/3/3/1', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/3/3/2', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/3/3/0', '/home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip/c/3/3/3']

After downloading and unzipping, we can open the zarr folder, which contains a single zarr array.

zarr_array = zarr.open(Path(zarr_path[0]).parent, mode="r")

print(zarr_array)
<Array file:///home/runner/.cache/brainglobe_atlases/59ad86bcafd1ed211b31620b50e2b3d8-25um.zarr.zip.unzip shape=(528, 320, 456) dtype=uint32>

By plotting a slice of the array contents, we can see the various regions encoded by integer values:

# Get the middle section and plot
middle_section = zarr_array.shape[0] // 2

# Create a cyclic colormap due to the high values in the Allen atlas
N = 512
colors = cm.get_cmap('tab20').resampled(N)
lut = colors(np.arange(N))

# Map label image to lookup table and plot
plt.imshow(lut[zarr_array[middle_section,:,:] % N])
explore atlas
<matplotlib.image.AxesImage object at 0x7f62d219d880>

Combining the annotation data with our RSP IDs allows us to calculate the volume of the RSP, which we finally print. This reaches the goal of this tutorial.

num_pixels = np.isin(zarr_array[:], rsp_ids).sum()
pixel_volume = 25*25*25
cubic_microns_to_cubic_millimeters = 1.0/1000**3

print(f"RSP has volume of around {np.round(num_pixels*pixel_volume*cubic_microns_to_cubic_millimeters)} cubic millimeters")
RSP has volume of around 11.0 cubic millimeters

In conclusion, this tutorial shows how to programmatically access and process atlas data for the specific purpose of calculating a region volume. More generally, it also constitutes an example for BrainGlobe atlas data being used independently of the BrainGlobe software tools.

Total running time of the script: (0 minutes 16.719 seconds)

Gallery generated by Sphinx-Gallery