annotate Process/Voting.py @ 2:46fb79167a61 tip

Main Code
author Victor Padilla <victor.padilla.mc@gmail.com>
date Mon, 04 May 2015 22:56:18 +0200
parents
children
rev   line source
victor@2 1 '''
victor@2 2 Created on 12/12/2014
victor@2 3
victor@2 4 @author: victor
victor@2 5 '''
victor@2 6 class Voting:
victor@2 7
victor@2 8 def __getIndicesFromValue(self,value, qlist):
victor@2 9 '''
victor@2 10 Return the index in the matrix with that value
victor@2 11 '''
victor@2 12 indices = []
victor@2 13 idx = -1
victor@2 14 while True:
victor@2 15 try:
victor@2 16 idx = qlist.index(value, idx+1)
victor@2 17 indices.append(idx)
victor@2 18 except ValueError:
victor@2 19 break
victor@2 20 return indices
victor@2 21
victor@2 22 def vote(self,omr_symbolsAlign):
victor@2 23 '''
victor@2 24 main function for voting the omr symbol array
victor@2 25
victor@2 26 '''
victor@2 27 voteArr=self.getArrayVotation(omr_symbolsAlign)
victor@2 28 outArr=self.getVoteResult(omr_symbolsAlign,voteArr)
victor@2 29 return outArr
victor@2 30
victor@2 31 def getArrayVotation(self,omr_symbolsAlign):
victor@2 32 '''
victor@2 33 Returns a matrix with the number of votes of each symbol.
victor@2 34 Each symbol is voted individually
victor@2 35
victor@2 36 '''
victor@2 37 voteArr=[]
victor@2 38 for i in range(len(omr_symbolsAlign[0])):
victor@2 39 voteArr.append([])
victor@2 40
victor@2 41 for j in range(len(omr_symbolsAlign)):
victor@2 42 vote=[]
victor@2 43 for n in range(5):
victor@2 44 vote.append(0)
victor@2 45 voteArr[i].append([])
victor@2 46
victor@2 47 for k in range(len(omr_symbolsAlign)):
victor@2 48 symbol1=omr_symbolsAlign[j][i]
victor@2 49 symbol2=omr_symbolsAlign[k][i]
victor@2 50 for n in range(5):
victor@2 51 if isinstance(symbol1,list) and isinstance(symbol2,list):
victor@2 52 if(symbol1[n]==symbol2[n]):
victor@2 53 vote[n]+=1.0
victor@2 54
victor@2 55 voteArr[i][j].append(vote)
victor@2 56 return voteArr
victor@2 57 #
victor@2 58
victor@2 59
victor@2 60 def _getNValueArray(self,array,n):
victor@2 61 '''
victor@2 62 Takes one slide in the aligned array
victor@2 63 '''
victor@2 64 values=[]
victor@2 65 for o in array:
victor@2 66 value=o[0][n]
victor@2 67 values.append(value)
victor@2 68 return values
victor@2 69
victor@2 70 def __getBetterIndexSymbol(self,a0,a1,a2):
victor@2 71 '''
victor@2 72 returns the best symbol based on the first three parameters
victor@2 73 [[D5_0.25,0.33,'start'....]
victor@2 74 note,realDuration, tie
victor@2 75 '''
victor@2 76 symbol_count=[]
victor@2 77 for i0 in a0:
victor@2 78 s=0
victor@2 79 if i0 in a1:
victor@2 80 s=s+10
victor@2 81 if i0 in a2:
victor@2 82 s=s+1
victor@2 83 symbol_count.append(s)
victor@2 84 better=symbol_count.index(max(symbol_count))
victor@2 85 index=a0[better]
victor@2 86 return index
victor@2 87
victor@2 88 def getBetterGeneralSymbol(self,omr_symbolsAlign,indexPosition,a):
victor@2 89 '''
victor@2 90 reconstruct the symbol using the best 5 parameters
victor@2 91 '''
victor@2 92 s=omr_symbolsAlign[a[0][0]][indexPosition]
victor@2 93 if isinstance(s,list):
victor@2 94 try:
victor@2 95 for n in range(5):
victor@2 96 s[n]=omr_symbolsAlign[a[n][0]][indexPosition][n]
victor@2 97 return s
victor@2 98 except:
victor@2 99 return s
victor@2 100 else:
victor@2 101 return s
victor@2 102
victor@2 103 def getVoteResult(self,omr_symbolsAlign,voteArr):#voteArr[i][j].append([vote,vote_1,vote_2])
victor@2 104 '''
victor@2 105 Using the symbols aligned and the matrix with the votes.
victor@2 106
victor@2 107 One symbol in three omr has this structure
victor@2 108 [[[3.0, 2.0, 3.0, 2.0, 2.0]],
victor@2 109 [[3.0, 1.0, 3.0, 1.0, 1.0]],
victor@2 110 [[3.0, 2.0, 3.0, 2.0, 2.0]]]
victor@2 111
victor@2 112 returns the better output
victor@2 113 '''
victor@2 114 outArr=[]
victor@2 115 for i in range(len(voteArr)):
victor@2 116 vote=[]
victor@2 117 indexMax_array=[]
victor@2 118 for n in range(5):
victor@2 119 vote.append([])
victor@2 120 vote[n]=self._getNValueArray(voteArr[i],n)
victor@2 121 indexMax_array.append([])
victor@2 122 indexMax_array[n]=self.__getIndicesFromValue(max(vote[n]),vote[n])
victor@2 123
victor@2 124 betterIndex=self.__getBetterIndexSymbol(indexMax_array[0],indexMax_array[1],indexMax_array[2])
victor@2 125 sBetterGeneral=self.getBetterGeneralSymbol(omr_symbolsAlign,i,indexMax_array)
victor@2 126 perc=vote[0][betterIndex]/len(vote[0])
victor@2 127
victor@2 128 if perc>=0.5:
victor@2 129 outArr.append(sBetterGeneral)
victor@2 130 return outArr
victor@2 131
victor@2 132