Last active
October 31, 2016 16:06
-
-
Save lydiang/12d04cd4ed750f4b24cfc72d67d14387 to your computer and use it in GitHub Desktop.
Example Python script to download atlas SVG 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 svg files | |
# | |
# 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" and "GraphicGroupLabels" | |
# | |
# Specify output directory for files | |
output_directory = "temp/svg" | |
# Specify downsample factor | |
downsample = 6 | |
# Copy "Atlas ID" here | |
atlas_id = 265297126 | |
# Copy "GraphicGroupLabels" here | |
group_labels = [31,113753816,141667008,265297118] | |
# 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'] | |
# download svg | |
svg_url = "http://api.brain-map.org/api/v2/svg_download/%d?" % (i['id']) | |
svg_url += "groups=%s" % ( ",".join([str(g) for g in group_labels])) | |
svg_url += "&downsample=%d" % (downsample) | |
svg_path = os.path.join( output_directory, '%04d_%d.svg' % (i['section_number'],i['id']) ) | |
urllib.urlretrieve(svg_url, svg_path) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment