-
Notifications
You must be signed in to change notification settings - Fork 0
/
getLogDistributions.py
459 lines (437 loc) · 11.7 KB
/
getLogDistributions.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
"""
This is the getLogDistributions module
providing a few utilities to manipulate data
for distributions in log scale
"""
import scipy
import scipy.stats
import numpy as np
import itertools
def checkIfVoid(listValues):
# Check the list of Values
if not len(listValues):
raise "Please pass a list of values"
else:
return True
def getLogBins(first_point, last_point, log_step):
"""
get the bin in log scale and the center bin value
Parameters:
----------------
first_point, last_point : number
First and last point of the x-axis
log_step : number
Required log-distance between x-points
Returns:
-----------
xbins : array of the x values at the center (in log-scale) of the bin
bins : array of the x values of the bins
"""
log_first_point = scipy.log10(first_point)
log_last_point = scipy.log10(last_point)
# Calculate the bins as required by the histogram function, i.e. the bins edges including the rightmost one
N_log_steps = scipy.floor((log_last_point-log_first_point)/log_step) + 1.
llp = N_log_steps * log_step + log_first_point
bins_in_log_scale = np.linspace(log_first_point, llp, N_log_steps+1)
bins = 10**bins_in_log_scale
center_of_bins_log_scale = bins_in_log_scale[:-1] + log_step/2.
xbins = 10**center_of_bins_log_scale
return xbins, bins
def logDistribution(listValues, log_step=0.2, first_point=None,
last_point=None, normed=True):
"""
Calculate the distribution in log scale from a list of values
Returns:
--------------
X : array of the x values at the center (in log-scale) of the bin
Y : array of the y values at the center (in log-scale) of the bin
Yerr : array of the y errors, calculated by a binomial distribution
"""
# Check the list of Values
if not checkIfVoid(listValues):
print("Error")
if not first_point:
first_point = scipy.amin(listValues)
if not last_point:
last_point = scipy.amax(listValues)
X, Xbins = getLogBins(first_point, last_point, log_step)
Y, bin_edges = np.histogram(listValues, Xbins, density=False)
deltas = Xbins[1:]-Xbins[:-1]
# Check if zeros occurs
bool0 = Y!=0
X, Y, deltas = X[bool0], Y[bool0], deltas[bool0]
Y = Y/deltas
Yerr = (Y * (1. - Y/sum(Y)))**0.5 / (deltas*sum(Y))
if normed:
Y = Y/scipy.sum(Y)
return X, Y, Yerr
def averageLogDistribution(values, log_step=0.2, density=False,
first_point=None, last_point=None):
"""
calculates the <values> vs. xVariable in log scale
Parameters:
---------------
values : dict
A dictionary where the keys are the xValues
and each element contains an array-like sequence of data
Example:
[1: array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]),
2: array([1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 2]),
values : ndarray
Two columns array of xValues and yValues,
to be rearranged as above
density: bool
Calculate the distribution as a density function
dividing by the width of the bin
Returns:
center point of the bin, average value within the bin
"""
# Check the list of Values
if not checkIfVoid(values):
print("Error")
if isinstance(values, dict):
xValues = np.asarray(values.keys())
yValues = np.asarray(values.values())
elif isinstance(values, np.ndarray):
xValues = np.unique(values[:, 0])
yValues = []
for xVal in xValues:
index = values[:,0] == xVal
yValues.append(values[index, 1])
yValues = scipy.array(yValues)
else:
print("Values shape not recognized")
return
if not first_point:
first_point = scipy.amin(xValues)*0.99
if not last_point:
last_point = scipy.amax(xValues)*1.01
xbins, bins = getLogBins(first_point, last_point, log_step)
yAverage = []
for i, j in zip(bins[:-1],bins[1:]):
q1, q2 = np.greater_equal(xValues, i), np.less(xValues, j)
q = np.logical_and(q1, q2)
if sum(q) == 0:
averageValue = np.NaN
else:
allElements = [val for val in itertools.chain(*yValues[q])]
averageValue = sum(allElements)/float(len(allElements))
#print averageValue, allElements
if density:
averageValue = averageValue/(j-i)
yAverage.append(averageValue)
yAverage = np.asanyarray(yAverage)
# Check if there are NaN values
iNan = np.isnan(yAverage)
x = xbins[~iNan]
y = yAverage[~iNan]
return x, y
if __name__ == "__main__":
#listValues = scipy.rand(100)*230
#listValues = scipy.loadtxt("/home/gf/Python/Moke/wtm_sizes.dat")
#xbins, yhist = logDistribution(listValues)
#loglog(xbins,yhist,'o')
#show()
#x,y = averageLogDistribution(N_cluster)
#loglog(x,y, 'bo')
#show()
q = np.array([[ 1, 1],
[ 1, 1],
[ 2, 2],
[ 5, 1],
[ 1, 1],
[ 6, 3],
[32258, 84],
[22689, 458],
[ 520, 25],
[ 510, 16],
[ 1215, 6],
[ 20, 10],
[ 38, 12],
[ 1324, 9],
[ 104, 14],
[ 44, 12],
[ 512, 8],
[ 56, 15],
[ 4989, 3],
[ 17, 9],
[ 11, 5],
[ 19, 6],
[ 271, 1],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 1],
[ 2, 2],
[ 7, 3],
[ 279, 6],
[ 131, 6],
[ 5, 1],
[ 1565, 2],
[ 12, 1],
[ 296, 1],
[ 21, 3],
[ 118, 3],
[ 13, 1],
[ 53, 3],
[ 57, 11],
[ 75, 8],
[ 2, 1],
[ 89, 1],
[ 1, 1],
[ 57, 6],
[ 96, 4],
[ 23, 10],
[ 181, 3],
[ 2, 1],
[ 1, 1],
[ 10, 1],
[ 2, 1],
[ 26, 4],
[ 4, 2],
[ 41, 1],
[ 463, 4],
[ 1, 1],
[ 134, 1],
[ 15, 6],
[ 4452, 6],
[ 564, 35],
[ 2090, 18],
[13804, 10],
[ 1050, 4],
[ 71, 4],
[ 964, 11],
[ 7471, 5],
[ 216, 6],
[ 103, 13],
[ 41, 13],
[ 793, 8],
[ 764, 6],
[ 463, 6],
[ 10, 5],
[ 467, 12],
[ 904, 6],
[ 77, 7],
[ 3, 1],
[ 169, 11],
[ 234, 8],
[ 16, 2],
[ 4, 2],
[ 1432, 3],
[ 1, 1],
[ 141, 2],
[ 64, 17],
[ 203, 4],
[ 102, 7],
[ 51, 7],
[ 2, 1],
[ 2, 1],
[ 3445, 2],
[ 52, 2],
[ 2, 1],
[ 180, 1],
[ 6, 1],
[ 1017, 2],
[ 1, 1],
[ 904, 4],
[ 4740, 4],
[ 482, 24],
[ 165, 1],
[ 1, 1],
[ 228, 1],
[ 7, 1],
[ 11, 3],
[ 807, 2],
[ 1, 1],
[ 1, 1],
[ 34, 2],
[ 41, 1],
[ 1, 1],
[ 2, 1],
[ 2, 1],
[ 2, 1],
[ 16, 5],
[ 30, 2],
[ 1, 1],
[ 2, 2],
[37088, 3],
[ 80, 16],
[ 17, 7],
[ 35, 9],
[ 235, 3],
[ 82, 3],
[ 2, 1],
[ 660, 8],
[ 131, 17],
[ 4, 1],
[ 303, 3],
[ 16, 6],
[ 340, 5],
[ 201, 13],
[25939, 14],
[17963, 7],
[ 3, 3],
[ 7, 4],
[ 8, 4],
[ 159, 7],
[ 1810, 2],
[ 4, 1],
[ 1, 1],
[ 57, 3],
[ 954, 2],
[ 18, 4],
[ 6, 5],
[ 100, 2],
[ 1, 1],
[ 2, 1],
[ 1, 1],
[ 287, 1],
[ 142, 1],
[ 8, 3],
[ 9, 6],
[ 124, 5],
[ 2, 1],
[ 629, 2],
[ 3, 1],
[ 4, 1],
[ 2, 1],
[ 9, 1],
[ 2, 1],
[ 2, 1],
[ 1378, 2],
[ 9836, 1],
[ 29, 5],
[ 38, 1],
[ 6, 2],
[ 170, 1],
[ 38, 4],
[ 21, 7],
[ 7, 4],
[ 2, 2],
[ 5825, 1],
[ 2, 1],
[ 5, 1],
[ 1, 1],
[ 3, 1],
[ 1, 1],
[ 2, 1]])
import random
import matplotlib.pylab as plt
imageSize = 500**2
N = 100
sizes = np.random.uniform(1, imageSize, N)
nCluster = scipy.array([int(s**0.5)*(1+random.randint(-5,5)/100.) for s in sizes])
#q = np.array(zip(sizes, nCluster))
x,y = averageLogDistribution(q)
plt.loglog(x,y, 'bo')
plt.grid()
plt.show()