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