-
Notifications
You must be signed in to change notification settings - Fork 7
/
allen_download.py
executable file
·114 lines (87 loc) · 3.57 KB
/
allen_download.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/usr/bin/env python3
"""
Download the entire Allen Reference Human Brain Atlas (34 years, cortex-gyral) as a JPG stack
- use the brain-map.org API
Usage
----
allen_download.py
Authors
----
Mike Tyszka, Caltech, Division of Humaninities and Social Sciences
Dates
----
2017-05-24 JMT From scratch
License
----
This file is part of atlaskit.
atlaskit is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
atlaskit is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with atlaskit. If not, see <http://www.gnu.org/licenses/>.
Copyright
----
2017 California Institute of Technology.
"""
__version__ = '0.1.0'
import os
import sys
import requests
import xml.etree.ElementTree as ET
from urllib.request import urlopen
def main():
# Image type: 'vector' (SVG) or 'image' (JPEG, annotated)
imtype = 'image'
# Allen Brain Institute Human, 34 years, Cortex-Gyral atlas
# Use developing rather than reference human brain atlas
# http://help.brain-map.org/display/api/Atlas+Drawings+and+Ontologies
# API request for full list of all images associated with the Human 34y gyral atlas
api_request_url = ("http://api.brain-map.org/api/v2/data/query.xml?criteria=model::AtlasImage,"
"rma::criteria,[annotated$eqtrue],"
"atlas_data_set(atlases[id$eq138322605]),"
"alternate_images[image_type$eq'Atlas+-+Developing+Human'],"
"rma::options[order$eq'sub_images.section_number'][num_rows$eqall]")
# Base image download format string
if imtype == 'vector':
image_url_fmt = 'http://api.brain-map.org/api/v2/svg_download/%s?groups=31,113753815&downsample=8d'
out_dir = '/Users/jmt/sandbox/Allen_Vector'
else:
image_url_fmt = 'http://api.brain-map.org/api/v2/atlas_image_download/%s?groups=31,113753815&downsample=4&annotation=true'
out_dir = '/Users/jmt/sandbox/Allen_Image'
# Safely create output directory
os.makedirs(out_dir, exist_ok=True)
# Download XML for Allen human brain ontology
print('Downloading XML image list from brain-map.org')
try:
image_list_xml = urlopen(api_request_url)
except:
print('* Problem getting XML image list from server')
sys.exit(1)
# Parse XML as a Document Object Model (DOM)
print('Parsing XML image list')
tree = ET.parse(image_list_xml)
# Loop over all atlas images
for atlas_image in tree.iter(tag='atlas-image'):
id_tag = atlas_image.find('id')
sno_tag = atlas_image.find('section-number')
sno = int(sno_tag.text)
# Construct image download URL
image_url = image_url_fmt % id_tag.text
# Construct output filename
if imtype == 'vector':
image_fname = os.path.join(out_dir, 'Allen_%04d.svg' % sno)
else:
image_fname = os.path.join(out_dir, 'Allen_%04d.jpg' % sno)
print(' Writing image to %s' % image_fname)
# Grab image data and write to a files
image_data = requests.get(image_url).content
with open(image_fname, 'wb') as handler:
handler.write(image_data)
# This is the standard boilerplate that calls the main() function.
if __name__ == '__main__':
main()