Last active
October 31, 2016 16:06
-
-
Save lydiang/24e64f97b29bed6c7f1efe689a51b80f to your computer and use it in GitHub Desktop.
Example Python script to download atlas background images from #AllenBrainAtlasAPI #AtlasDownload
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import urllib, json | |
import os | |
# | |
# Example python code to download atlas background images | |
# | |
# Read the Allen Brain Atlas API documentation: | |
# http://help.brain-map.org/display/api/Atlas+Drawings+and+Ontologies | |
# | |
# In the table find the atlas that you looking for. | |
# Take note of the "Atlas ID" | |
# | |
# Specify output directory for files | |
output_directory = "temp/background_image" | |
# Specify downsample factor | |
downsample = 6 | |
# Copy "Atlas ID" here | |
atlas_id = 265297126 | |
# RMA query to find images for atlas | |
query_url = "http://api.brain-map.org/api/v2/data/query.json?criteria=model::AtlasImage" | |
query_url += ",rma::criteria,[annotated$eqtrue]" | |
query_url += ",atlas_data_set(atlases[id$eq%d])" % (atlas_id) | |
query_url += ",rma::options[order$eq'sub_images.section_number'][num_rows$eqall]" | |
response = urllib.urlopen(query_url) | |
images = json.loads(response.read())['msg'] | |
# make output directory | |
if not os.path.exists( output_directory ) : | |
os.makedirs( output_directory ) | |
# loop through each image | |
for i in images : | |
print i['section_number'] | |
# downsampled images | |
image_url = "http://api.brain-map.org/api/v2/section_image_download/%d?" % (i['id']) | |
image_url += "downsample=%d" % (downsample) | |
image_path = os.path.join( output_directory, '%04d_%d.jpg' % (i['section_number'],i['id']) ) | |
urllib.urlretrieve(image_url, image_path) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment