156 |
adjusted = False |
adjusted = False |
157 |
if len(percents) != 0: |
if len(percents) != 0: |
158 |
|
|
|
#print "list:", list |
|
|
#print "percents:", percents |
|
|
#print "numGroups:", numGroups |
|
|
|
|
159 |
# |
# |
160 |
# find what part of the list range covers |
# find what part of the list range covers |
161 |
# |
# |
172 |
break; |
break; |
173 |
|
|
174 |
numValues = maxIndex - minIndex + 1 |
numValues = maxIndex - minIndex + 1 |
|
#print "minIndex:", minIndex, "maxIndex:", maxIndex |
|
175 |
if minIndex <= maxIndex: |
if minIndex <= maxIndex: |
176 |
|
|
177 |
# |
# |
178 |
# build a list of unique indices into list of where each |
# build a list of unique indices into list of where each |
179 |
# quantile should be |
# quantile *should* be. set adjusted if the resulting |
180 |
|
# indices are different |
181 |
# |
# |
182 |
quantiles = {} |
quantiles = {} |
183 |
for p in percents: |
for p in percents: |
185 |
|
|
186 |
adjusted = adjusted \ |
adjusted = adjusted \ |
187 |
or quantiles.has_key(index) \ |
or quantiles.has_key(index) \ |
188 |
or ((index+1) / float(numValues)) != p |
or ((index - minIndex + 1) / float(numValues)) != p |
189 |
|
|
190 |
quantiles[index] = 0 |
quantiles[index] = 0 |
191 |
|
|
192 |
quantiles = quantiles.keys() |
quantiles = quantiles.keys() |
193 |
quantiles.sort() |
quantiles.sort() |
194 |
|
|
|
#print "quantiles:", quantiles |
|
|
|
|
195 |
# |
# |
196 |
# the current quantile index must be strictly greater than |
# the current quantile index must be strictly greater than |
197 |
# the lowerBound |
# the lowerBound |
206 |
|
|
207 |
# lowerBound + 1 is always a valid index |
# lowerBound + 1 is always a valid index |
208 |
|
|
|
|
|
209 |
# |
# |
210 |
# bump up the current quantile index to be a usable index |
# bump up the current quantile index to be a usable index |
211 |
# if it currently falls below the lowerBound |
# if it currently falls below the lowerBound |
216 |
listIndex = quantiles[qindex] |
listIndex = quantiles[qindex] |
217 |
value = list[quantiles[qindex]] |
value = list[quantiles[qindex]] |
218 |
|
|
|
#print "-----------------------------------" |
|
|
#print "listIndex:", listIndex |
|
|
#print "value:", value |
|
|
|
|
219 |
# |
# |
220 |
# look for similar values around the quantile index |
# look for similar values around the quantile index |
221 |
# |
# |
|
|
|
|
#print "lowerBound:", lowerBound |
|
222 |
lindex = listIndex - 1 |
lindex = listIndex - 1 |
223 |
lcount = 0 |
lcount = 0 |
224 |
while lindex > lowerBound: |
while lindex > lowerBound: |
233 |
rcount += 1 |
rcount += 1 |
234 |
rindex += 1 |
rindex += 1 |
235 |
|
|
|
#print lcount, "(lcount)", rcount, "(rcount)" |
|
|
#print lindex, "(lindex)", rindex, "(rindex)" |
|
|
|
|
236 |
# |
# |
237 |
# adjust the current quantile index based on how many |
# adjust the current quantile index based on how many |
238 |
# numbers in the list are the same as the current value |
# numbers in the list are the same as the current value |
247 |
# |
# |
248 |
if lindex != lowerBound: |
if lindex != lowerBound: |
249 |
newIndex = lindex |
newIndex = lindex |
|
#quantiles[qindex] = lindex |
|
250 |
else: |
else: |
251 |
newIndex = rindex - 1 |
newIndex = rindex - 1 |
|
#quantiles[qindex] = rindex - 1 |
|
252 |
|
|
253 |
elif lcount < rcount: |
elif lcount < rcount: |
254 |
# there are fewer items to the left, so |
# there are fewer items to the left, so |
256 |
# doing so creates an empty quantile. |
# doing so creates an empty quantile. |
257 |
if lindex != lowerBound: |
if lindex != lowerBound: |
258 |
newIndex = lindex |
newIndex = lindex |
|
#quantiles[qindex] = lindex |
|
259 |
else: |
else: |
260 |
newIndex = rindex - 1 |
newIndex = rindex - 1 |
|
#quantiles[qindex] = rindex - 1 |
|
261 |
|
|
262 |
elif rcount < lcount: |
elif rcount < lcount: |
263 |
# there are fewer items to the right, so go to the right |
# there are fewer items to the right, so go to the right |
|
#quantiles[qindex] = rindex - 1 |
|
264 |
newIndex = rindex - 1 |
newIndex = rindex - 1 |
265 |
|
|
266 |
quantiles[qindex] = newIndex |
quantiles[qindex] = newIndex |
267 |
lowerBound = quantiles[qindex] |
lowerBound = quantiles[qindex] |
268 |
|
|
269 |
#print "quantiles:", quantiles |
# |
270 |
#print "lowerBound:", lowerBound |
# since quantiles is only set if the code is at least a little |
271 |
|
# successful, an empty list will be generated in the case that |
272 |
#print "=================================" |
# we fail to get to the real body of the algorithm |
273 |
#print "quantiles:", quantiles |
# |
274 |
#print "qindex:", qindex |
return (adjusted, |
275 |
|
[(q, (q - minIndex+1) / float(numValues)) for q in quantiles]) |
|
return (adjusted, [(q, (q+1) / float(numValues)) for q in quantiles]) |
|
276 |
|
|
277 |
CLR = 0 |
CLR = 0 |
278 |
STEP = 1 |
STEP = 1 |