Talk:Automated Anatomical Labeling

From Brede Wiki
Jump to: navigation, search

[edit] Check labeling

import re
import requests
from urllib import quote
 
url_rbb = "http://neuro.compute.dtu.dk/w/api.php?action=query&format=json&list=categorymembers&cmtitle=Category:Brain regions in AAL"
url_pages = "http://neuro.compute.dtu.dk/w/index.php?action=raw&title="
 
pattern_fields = re.compile(r'\s*\|\s*(\w+)\s*=\s*([^\|]*\w)', re.DOTALL | re.UNICODE)
pattern_brainregion = re.compile('{{brain region(\s*?\|.*?)}}', 
                                      re.DOTALL | re.IGNORECASE | re.UNICODE)
 
 
response = requests.get(url_rbb).json()
pagetitles = [page['title'] for page in response['query']['categorymembers']]
while 'query-continue' in response:
    url_continue = url_rbb + '&cmcontinue=' + response['query-continue']['categorymembers']['cmcontinue'].encode('utf-8')
    response = requests.get(url_continue).json()
    print(url_continue)
    pagetitles.extend([page['title'] for page in response['query']['categorymembers']])
 
brainregions = []
for pagetitle in pagetitles:
    raw = unicode(requests.get(url_pages + quote(pagetitle.encode('utf-8'))).content, 'utf-8')
    print pagetitle 
    brainregion = pattern_brainregion.findall(raw)
    if brainregion:
        brainregions.append(dict(pattern_fields.findall(brainregion[0])))
 
 
# Show old AAL IDs (if any):
for brainregion in brainregions:
    if int(brainregion['aal']) < 1000:
        print(brainregion['name'])
 
 
# List the brain regions
for n, brainregion in enumerate(sorted(brainregions, key=lambda br: br['aal']), start=1):
    print("{:3} {:50} {}".format(n, brainregion['name'], brainregion['aal']))
 
 
# Are there any duplicates?
import collections
collections.Counter([br['aal'] for br in brainregions])
 
 
# Compare with text file in AAL distribution
from os.path import expanduser, join
filename = join(expanduser('~'), 'matlab', 'aal_for_SPM8', 'ROI_MNI_V4.txt')
aal = dict([line.split()[3:0:-1] for line in open(filename).readlines()])
for n, brainregion in enumerate(brainregions, start=1):
    print("{} {:22} {}".format(n, aal[brainregion['aal']], brainregion['name']))
Personal tools