The ImageDownloadApi
class in the AllenSDK provides a Python interface for accessing image data produced by the Allen Institute for Brain Science.
Contents:
Reference Documentation:
Download this file in .ipynb format here.
from allensdk.api.queries.image_download_api import ImageDownloadApi
from allensdk.api.queries.svg_api import SvgApi
from allensdk.config.manifest import Manifest
import matplotlib.pyplot as plt
from skimage.io import imread
import pandas as pd
import logging
import os
from base64 import b64encode
from IPython.display import HTML, display
%matplotlib inline
We will want to look at the images we download in this notebook. Here are a couple of functions that do this:
def verify_image(file_path, figsize=(18, 22)):
image = imread(file_path)
fig, ax = plt.subplots(figsize=figsize)
ax.imshow(image)
def verify_svg(file_path, width_scale, height_scale):
# we're using this function to display scaled svg in the rendered notebook.
# we suggest that in your own work you use a tool such as inkscape or illustrator to view svg
with open(file_path, 'rb') as svg_file:
svg = svg_file.read()
encoded_svg = b64encode(svg)
decoded_svg = encoded_svg.decode('ascii')
st = r'<img class="figure" src="data:image/svg+xml;base64,{}" width={}% height={}%></img>'.format(decoded_svg, width_scale, height_scale)
display(HTML(st))
Finally, we will need an instance of ImageDownloadApi
and an instance of SvgApi
:
image_api = ImageDownloadApi()
svg_api = SvgApi()
In this example, we will download a single section image from Mouse Brain Atlas experiment 71210895.
For now, we'll assume that we already know the id (unique integer identifier) of the image that we wish to download. In a later section, we'll use our ImageDownloadApi
instance to find the ids of all the images from a particular experiment.
section_image_id = 70945123
file_path = '70945123.jpg'
The raw images can be quite large, so we will download downsampled versions. The downsample
keyword argument reduces the size of the obtained image by a factor of $2^{\text{downsample}}$ along each axis.
downsample = 3
Now we download the image using our api object and view it in the notebook.
image_api.download_section_image(section_image_id, file_path, downsample=downsample)
verify_image(file_path)
In this example, we will download a single section image from Mouse Brain Connectivity experiment 297225422.
section_image_id = 297225716
file_path = '297225716_connectivity.jpg'
downsample = 3
In order to clearly distinguish projection signal from background structure, it helps to window and rescale each channel. We have precomputed channelwise windows for each section data set in the connectivity atlas. You can obtain these window parameters using the ImageDownloadApi
ranges = image_api.get_section_image_ranges([section_image_id])[0]
Then simply pass them in via the range
keyword argument.
image_api.download_projection_image(section_image_id, file_path, downsample=downsample, range=ranges)
verify_image(file_path)
If we set the projection
keyword argument to True
, we can download a mask which labels important features of the image, such as the injection site, detected projection signal, and the boundary of detectable tissue.
section_image_id = 297225716
file_path = '297225716_projection.jpg'
downsample = 3
projection=True
image_api.download_projection_image(section_image_id, file_path, downsample=downsample,
projection=projection)
verify_image(file_path)
If we know the id of the atlas image we want, we can download it directly. We'll use an image from the Adult Human Brain Reference Atlas for this example.
atlas_image_id = 112282603
downsample = 6
file_path = '112282603_nissl.jpg'
Atlas images consist of an annotation image layered over a biological specimen image. You can control which one you download via the annotation
keyword argument.
Setting annotation to False
will download the biological specimen (in this case, a Nissl section).
annotation = False
image_api.download_atlas_image(atlas_image_id, file_path, annotation=annotation, downsample=downsample)
verify_image(file_path)
We can download the structure drawings and labels (instead of the nissl) by setting annotation=True
atlas_image_id = 112282603
annotation = True
downsample = 6
file_path = '112282603_annotation.jpg'
image_api.download_atlas_image(atlas_image_id, file_path, annotation=annotation, downsample=downsample)
verify_image(file_path)
We make the atlas drawings available (sans textual labels) as SVG. We'll download and display the SVG for the above atlas image:
from allensdk.api.queries.svg_api import SvgApi
svg_api = SvgApi()
svg_api.download_svg(atlas_image_id, file_path='112282603.svg')
verify_svg('112282603.svg', 35, 35)
The ImageDownloadApi
class can list the images in an atlas or section dataset. For an atlas (the same Human Brain reference atlas as above), this looks like:
atlas_id = 138322605
# image_api.section_image_query(section_data_set_id) is the analogous method for section data sets
atlas_image_records = image_api.atlas_image_query(atlas_id)
# this returns a list of dictionaries. Let's convert it to a pandas dataframe
atlas_image_dataframe = pd.DataFrame(atlas_image_records)
# and use the .head() method to display the first few rows
atlas_image_dataframe.head()
The id
column of this dataframe contains the ids of the images in this atlas - these are the same integer identifiers that we have been using to download single images.
atlas_image_dataframe['id'].head()
Specifically, Brainspan ISH experiment 100147602.
We'll start with some parameters:
section_data_set_id = 100147602
downsample=6
section_image_directory = '75214738_section_images'
format_str = '.jpg'
Now we need to get the image ids for all of the images in this data set:
section_images = image_api.section_image_query(section_data_set_id)
section_image_ids = [si['id'] for si in section_images]
print(len(section_image_ids))
and then iterate:
# You have probably noticed that the AllenSDK has a logger which notifies you of file downloads.
# Since we are downloading ~300 images, we don't want to see messages for each one.
# The following line will temporarily disable the download logger.
logging.getLogger('allensdk.api.api.retrieve_file_over_http').disabled = True
for section_image_id in section_image_ids:
file_name = str(section_image_id) + format_str
file_path = os.path.join(section_image_directory, file_name)
Manifest.safe_make_parent_dirs(file_path)
image_api.download_section_image(section_image_id, file_path=file_path, downsample=downsample)
# re-enable the logger
logging.getLogger('allensdk.api.api.retrieve_file_over_http').disabled = False
We should now have a directory containing the requested images.
file_names = os.listdir(section_image_directory)
print(len(file_names))
display_file_paths = [os.path.join(section_image_directory, dfn) for dfn in file_names[::50]]
fig, ax = plt.subplots(ncols=len(display_file_paths), figsize=(18, 22))
for ii, dfp in enumerate(display_file_paths):
image = imread(dfp)
ax[ii].imshow(image)
ax[ii].get_xaxis().set_visible(False)
ax[ii].get_yaxis().set_visible(False)
Related experiments are grouped into products. The ImageDownloadApi
has a method for listing the section data sets in a particular product.
Here we will use this method to list section data sets from the Human Brain ISH SubCortex Study (id=10 per the above link).
human_subcortex_datasets = image_api.get_section_data_sets_by_product([10])
print('there are {} section data sets in the Human Brain ISH SubCortex Study'.format(len(human_subcortex_datasets)))
pd.DataFrame(human_subcortex_datasets).head()