-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
executable file
·214 lines (152 loc) · 5.12 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# Misc. stats and plotting utils.
# Miguel Matos - [email protected]
# (c) 2012-2017
import math
def computeCDF(data,precision=1000):
import numpypy
import numpy
from scipy.stats import cumfreq, scoreatpercentile
maxVal = max(data) + 0.
freqs, _ , _, _ = cumfreq(data,precision)
freqsNormalized = map(lambda x : x / maxVal,freqs)
values = []
step = 100. / precision
scores = numpy.arange(0,100+step,step)
for s in scores:
values.append(scoreatpercentile(data,s))
return values,freqs,freqsNormalized
def dumpAsGnuplot(data,path,caption,pad=True):
if pad:
merge = zip(range(len(data[0])),*data)
else:
merge = zip(*data)
writer = open(path,'w')
print>>writer, caption
for pack in merge:
strData = str(pack) #to avoid commas and brackets
strData = strData[1:-1].replace(',','\t').replace(' ','')
print>>writer, strData
writer.close()
def paddListsToSameSize(data,defaultElement=0):
maxSize = 0
for lst in data:
lgt = len(lst)
if lgt > maxSize:
maxSize = lgt
return map( lambda x: [defaultElement for n in range(maxSize-len(x))] + x,data)
def computeAverage(data):
"""
Computes the average of a sequence of lists.
"""
itens = len(data)
if itens> 1:
#print 'Averaging...'
data = paddListsToSameSize(data)
dataAverage = map(lambda x,y: x +y, data[0],data[1])
for i in range(2,itens):
dataAverage = map(lambda x,y: x +y , dataAverage, data[i])
dataAverage = map(lambda x : x / (itens + 0.), dataAverage)
else:
print 'Single Run.'
dataAverage = data[0]
return dataAverage
def mean(data):
try:
if len(data) > 0:
return sum(data) / (len(data) + 0.)
else:
return 0
except: #we may receive a int or float, return it directly if that's the case
return data
def getClosest(data,value):
"""
Finds the index of the closest element to value in data. Data should be ordered.
"""
data.sort()
i = 0
lgt = len(data)
while i < lgt and value > data[i]:
i+=1
return i if i <lgt else lgt-1
#original forom scipy, adjusted to run on pypy
def scoreatpercentile(a, per, limit=(),isSorted=False):
try:
import numpypy
except:
#print 'unable to use numpypy'
pass
import numpy
#values = np.sort(a,axis=0) #not yet implemented in pypy
if not isSorted:
values = sorted(a) #np.sort(a,axis=0)
else:
values = a
if limit:
values = values[(limit[0] <= values) & (values <= limit[1])]
#idx = per /100. * (values.shape[0] - 1)
idx = per /100. * (len(values) - 1)
if (idx % 1 == 0):
return values[int(idx)]
else:
return _interpolate(values[int(idx)], values[int(idx) + 1], idx % 1)
def _interpolate(a, b, fraction):
"""Returns the point at the given fraction between a and b, where
'fraction' must be between 0 and 1.
"""
return a + (b - a)*fraction;
def percentiles(data,percs=[0,1,5,25,50,75,95,99,100],paired=True,roundPlaces=None):
"""
Returns the values at the given percentiles.
Inf percs is null gives the 5,25,50,75,95,99,100 percentiles.
"""
data = sorted(data)
#data migth be an iterator so we need to do this check after sorting
if len(data) == 0:
return []
result = []
for p in percs:
score = scoreatpercentile(data,p,isSorted=True)
if roundPlaces:
score = round(score,roundPlaces)
if paired:
result.append( (p, score))
else:
result.append( score)
return result
def checkLatencyNodes(latencyTable, nbNodes, defaultLatency=None):
global latencyValue
if latencyTable == None and defaultLatency != None:
print 'WARNING: using constant latency'
latencyTable = {n: {m: defaultLatency for m in range(nbNodes)} for n in range(nbNodes)}
# latencyTable = {n : {m: random.randint(0,defaultLatency)for m in range(nbNodes)} for n in range(nbNodes) }
return latencyTable
nbNodesAvailable = len(latencyTable)
latencyList = [l for tmp in latencyTable.itervalues() for l in tmp.values()]
latencyValue = math.ceil(percentiles(latencyList, percs=[50], paired=False)[0])
if nbNodes > nbNodesAvailable:
nodesToPopulate = nbNodes - nbNodesAvailable
nodeIds = range(nbNodes)
logger.warning('Need to add nodes to latencyTable')
for node in range(nbNodesAvailable):
latencyTable[node].update({target: random.choice(latencyList) for target in nodeIds[nbNodesAvailable:]})
for node in range(nbNodesAvailable, nbNodes):
latencyTable[node] = {target: random.choice(latencyList) for target in nodeIds}
latencyTable[node].pop(node) # remove itself
# FIXME: we should also remove some other nodes to be more faithful to the original distribution
with open('/tmp/latencyTable.obj', 'w') as f:
cPickle.dump(latencyTable, f)
return latencyTable
def copy(org):
'''
much, much faster than deepcopy, for a dict of the simple python types.
'''
out = dict().fromkeys(org)
for k,v in org.iteritems():
try:
out[k] = v.copy() # dicts, sets
except AttributeError:
try:
out[k] = v[:] # lists, tuples, strings, unicode
except TypeError:
out[k] = v # ints
return out