# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
# vi: set ft=python sts=4 ts=4 sw=4 et:

import os
import xml.dom.minidom
import nibabel as nib

def xmlread_label(prefix, xmlname, tagname, index):
    """
    read specific label from xml file according to tag and index.

    """
    dom=xml.dom.minidom.parse(prefix + xmlname)
    root = dom.documentElement
    tags = root.getElementsByTagName(tagname)
    tag = tags[index]
    return tag.firstChild.data

def xmlread_labellist(prefix, xmlname, tagname):
    """
    read specific label list from xml file according to tag.

    """
    labellist = []
    dom=xml.dom.minidom.parse(prefix + xmlname)
    root = dom.documentElement
    tags = root.getElementsByTagName(tagname)
    for i in range(len(tags)):
        labellist.append(tags[i].firstChild.data)
    return labellist

def extract_atlasprob(prefix, image, x, y, z):
    """
    extract atlas probability values according to coordinate.

    """
    atlas = nib.load(prefix + image)
    atlasdata = atlas.get_data()
    problist = atlasdata[x, y, z, :]/100.0
    return problist

def sorting(labellist, problist):
    """
    sorting the label according to probability value.

    """
    if (len(labellist)!=len(problist)):
        raise 'the length of label and value can not match'
    result = ''
    plist, llist = [], []
    for i in range(len(problist)):
        if (problist[i] != 0):
            plist.append(problist[i])
            llist.append(labellist[i])
    if (len(plist)==0):
        result = 'No Labels\n'
        return result
    for i in range(len(plist)):
        for j in range(i+1,len(plist)):
            if (plist[i]<plist[j]):
                plist[i], plist[j] = plist[j], plist[i]
                llist[i], llist[j] = llist[j], llist[i]
    for i in range(len(plist)):
        result += (str(plist[i])+' '+llist[i]+'\n')
    return result
@@ -118,16 +118,32 @@
 port os%0A
+import operator%0A
 import x
@@ -1357,26 +1357,20 @@
     
-plist, l
+output
 list =
- %5B%5D,
  %5B%5D%0A
@@ -1443,25 +1443,30 @@
             
-p
+output
 list.append(
@@ -1465,16 +1465,17 @@
 .append(
+%5B
 problist
@@ -1481,35 +1481,9 @@
 t%5Bi%5D
-)%0A            llist.append(
+,
 labe
@@ -1490,16 +1490,17 @@
 llist%5Bi%5D
+%5D
 )%0A    if
@@ -1501,25 +1501,30 @@
     if (len(
-p
+output
 list)==0):%0A 
@@ -1583,223 +1583,93 @@
     
-for i in range(len(plist)):%0A        for j in range(i+1,len(plist)):%0A            if (plist%5Bi%5D%3Cplist%5Bj%5D):%0A                plist%5Bi%5D, plist%5Bj%5D = plist%5Bj%5D, plist%5Bi%5D%0A                llist%5Bi%5D, llist%5Bj%5D = llist%5Bj%5D, llist%5Bi%5D
+outputlist = sorted(outputlist, key= operator.itemgetter(0))%0A    outputlist.reverse()
 %0A   
@@ -1688,17 +1688,22 @@
 nge(len(
-p
+output
 list)):%0A
@@ -1729,30 +1729,46 @@
 str(
-p
+output
 list%5Bi%5D
+%5B0%5D
 )+' '+
-l
+output
 list%5Bi%5D
+%5B1%5D
 +'%5Cn
@@ -1771,24 +1771,25 @@
 +'%5Cn')%0A    return result
+%0A

improve the sorting efficiency