victor@2: ''' victor@2: Created on 12/12/2014 victor@2: victor@2: @author: victor victor@2: ''' victor@2: class Voting: victor@2: victor@2: def __getIndicesFromValue(self,value, qlist): victor@2: ''' victor@2: Return the index in the matrix with that value victor@2: ''' victor@2: indices = [] victor@2: idx = -1 victor@2: while True: victor@2: try: victor@2: idx = qlist.index(value, idx+1) victor@2: indices.append(idx) victor@2: except ValueError: victor@2: break victor@2: return indices victor@2: victor@2: def vote(self,omr_symbolsAlign): victor@2: ''' victor@2: main function for voting the omr symbol array victor@2: victor@2: ''' victor@2: voteArr=self.getArrayVotation(omr_symbolsAlign) victor@2: outArr=self.getVoteResult(omr_symbolsAlign,voteArr) victor@2: return outArr victor@2: victor@2: def getArrayVotation(self,omr_symbolsAlign): victor@2: ''' victor@2: Returns a matrix with the number of votes of each symbol. victor@2: Each symbol is voted individually victor@2: victor@2: ''' victor@2: voteArr=[] victor@2: for i in range(len(omr_symbolsAlign[0])): victor@2: voteArr.append([]) victor@2: victor@2: for j in range(len(omr_symbolsAlign)): victor@2: vote=[] victor@2: for n in range(5): victor@2: vote.append(0) victor@2: voteArr[i].append([]) victor@2: victor@2: for k in range(len(omr_symbolsAlign)): victor@2: symbol1=omr_symbolsAlign[j][i] victor@2: symbol2=omr_symbolsAlign[k][i] victor@2: for n in range(5): victor@2: if isinstance(symbol1,list) and isinstance(symbol2,list): victor@2: if(symbol1[n]==symbol2[n]): victor@2: vote[n]+=1.0 victor@2: victor@2: voteArr[i][j].append(vote) victor@2: return voteArr victor@2: # victor@2: victor@2: victor@2: def _getNValueArray(self,array,n): victor@2: ''' victor@2: Takes one slide in the aligned array victor@2: ''' victor@2: values=[] victor@2: for o in array: victor@2: value=o[0][n] victor@2: values.append(value) victor@2: return values victor@2: victor@2: def __getBetterIndexSymbol(self,a0,a1,a2): victor@2: ''' victor@2: returns the best symbol based on the first three parameters victor@2: [[D5_0.25,0.33,'start'....] victor@2: note,realDuration, tie victor@2: ''' victor@2: symbol_count=[] victor@2: for i0 in a0: victor@2: s=0 victor@2: if i0 in a1: victor@2: s=s+10 victor@2: if i0 in a2: victor@2: s=s+1 victor@2: symbol_count.append(s) victor@2: better=symbol_count.index(max(symbol_count)) victor@2: index=a0[better] victor@2: return index victor@2: victor@2: def getBetterGeneralSymbol(self,omr_symbolsAlign,indexPosition,a): victor@2: ''' victor@2: reconstruct the symbol using the best 5 parameters victor@2: ''' victor@2: s=omr_symbolsAlign[a[0][0]][indexPosition] victor@2: if isinstance(s,list): victor@2: try: victor@2: for n in range(5): victor@2: s[n]=omr_symbolsAlign[a[n][0]][indexPosition][n] victor@2: return s victor@2: except: victor@2: return s victor@2: else: victor@2: return s victor@2: victor@2: def getVoteResult(self,omr_symbolsAlign,voteArr):#voteArr[i][j].append([vote,vote_1,vote_2]) victor@2: ''' victor@2: Using the symbols aligned and the matrix with the votes. victor@2: victor@2: One symbol in three omr has this structure victor@2: [[[3.0, 2.0, 3.0, 2.0, 2.0]], victor@2: [[3.0, 1.0, 3.0, 1.0, 1.0]], victor@2: [[3.0, 2.0, 3.0, 2.0, 2.0]]] victor@2: victor@2: returns the better output victor@2: ''' victor@2: outArr=[] victor@2: for i in range(len(voteArr)): victor@2: vote=[] victor@2: indexMax_array=[] victor@2: for n in range(5): victor@2: vote.append([]) victor@2: vote[n]=self._getNValueArray(voteArr[i],n) victor@2: indexMax_array.append([]) victor@2: indexMax_array[n]=self.__getIndicesFromValue(max(vote[n]),vote[n]) victor@2: victor@2: betterIndex=self.__getBetterIndexSymbol(indexMax_array[0],indexMax_array[1],indexMax_array[2]) victor@2: sBetterGeneral=self.getBetterGeneralSymbol(omr_symbolsAlign,i,indexMax_array) victor@2: perc=vote[0][betterIndex]/len(vote[0]) victor@2: victor@2: if perc>=0.5: victor@2: outArr.append(sBetterGeneral) victor@2: return outArr victor@2: victor@2: