-
Notifications
You must be signed in to change notification settings - Fork 0
/
histograma_bines_gral.py
268 lines (153 loc) · 9.05 KB
/
histograma_bines_gral.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
#! /usr/bin/env python
import numpy
def histograma_bins(lista,Nbins, name_h):
minimum=float(min(lista))
maximum=float(max(lista)) # fixed range so i can compare histograms from diff. models
hist, bin_edges= numpy.histogram(lista, bins=Nbins, range=(minimum,maximum)) # optional: range=( , ) The lower and upper range of the bins.
#if i wanna compare several distrib. i MUST give same Nbins and (max,min) range too!!!
#print "\n",name_h,"max:",max(lista),"min:",min(lista)
Cumul_prob=[0]*500000
norm=0.
for item in lista:
for b in range (len(bin_edges)):
value_bin=bin_edges[b]
if value_bin<= item:
Cumul_prob[b]+=1.
norm+=1.
# print "bin size:", bin_edges[1]-bin_edges[0], bin_edges[2]-bin_edges[1], bin_edges[3]-bin_edges[2]
bin_size=float(bin_edges[1]-bin_edges[0])
# ojoooooo! normalizar tb por bin size, ademas de N puntos
lista_tuplas=[]
origin=float(bin_edges[0])
file = open(name_h,'wt')
for b in range (len(bin_edges)-1):
#if hist[b] !=0:
print >> file,origin+(bin_edges[b+1]-bin_edges[b])/2.0, float(hist[b])/(float(len(lista))*bin_size), float(hist[b]),float(Cumul_prob[b])/(float(len(lista))*bin_size),float(Cumul_prob[b]), float(hist[b])/float(len(lista))
tupla=(origin+(bin_edges[b+1]-bin_edges[b])/2.0, float(hist[b])/(float(len(lista))*bin_size))
lista_tuplas.append(tupla)
origin=origin+(bin_edges[b+1]-bin_edges[b])
file.close()
print "written histogram:",name_h
# print "written:", name_h, " colum names: norm_prob, raw_count, cumul_prob, raw_cumul_count (normalization by N events times bin_size), cumul_prob (normalization by N events only)"
return lista_tuplas
def histograma_bins_zero(lista,Nbins, name_h):# bins centered on the left corner of the bin (not the center)
print min(lista),max(lista)
minimum=0#float(min(lista))
maximum=20000#float(max(lista)) # fixed range so i can compare histograms from diff. models
hist, bin_edges= numpy.histogram(lista, bins=Nbins, range=(minimum,maximum)) # optional: range=( , ) The lower and upper range of the bins.
#if i wanna compare several distrib. i MUST give same Nbins and (max,min) range too!!!
#print "\n",name_h,"max:",max(lista),"min:",min(lista)
Cumul_prob=[0]*500000
norm=0.
for item in lista:
for b in range (len(bin_edges)):
value_bin=bin_edges[b]
if value_bin<= item:
Cumul_prob[b]+=1.
norm+=1.
# print "bin size:", bin_edges[1]-bin_edges[0], bin_edges[2]-bin_edges[1], bin_edges[3]-bin_edges[2]
bin_size=float(bin_edges[1]-bin_edges[0])
# ojoooooo! normalizar tb por bin size, ademas de N puntos
lista_tuplas=[]
origin=float(bin_edges[0])
file = open(name_h,'wt')
for b in range (len(bin_edges)-1):
#if hist[b] !=0:
print >> file,bin_edges[b], float(hist[b])/(float(len(lista))*bin_size), float(hist[b]),float(Cumul_prob[b])/(float(len(lista))*bin_size),float(Cumul_prob[b]), float(hist[b])/float(len(lista))
tupla=(bin_edges[b], float(hist[b])/(float(len(lista))*bin_size))
lista_tuplas.append(tupla)
origin=origin+(bin_edges[b+1]-bin_edges[b])
file.close()
print "written histogram:",name_h
# print "written:", name_h, " colum names: norm_prob, raw_count, cumul_prob, raw_cumul_count (normalization by N events times bin_size), cumul_prob (normalization by N events only)"
return lista_tuplas
def histograma_bins_zero_small_bins_at_start(lista,Nbins, name_h):# bins centered on the left corner of the bin (not the center)
minimum=0#float(min(lista))
maximum=60000#float(max(lista)) # fixed range so i can compare histograms from diff. models
num_small_bins=10 # the rest of the bins, will be collapsed to a big one
hist, bin_edges= numpy.histogram(lista, bins=Nbins, range=(minimum,maximum)) # optional: range=( , ) The lower and upper range of the bins.
#if i wanna compare several distrib. i MUST give same Nbins and (max,min) range too!!!
#print "\n",name_h,"max:",max(lista),"min:",min(lista)
Cumul_prob=[0]*500000
norm=0.
for item in lista:
for b in range (len(bin_edges)):
value_bin=bin_edges[b]
if value_bin<= item:
Cumul_prob[b]+=1.
norm+=1.
# print "bin size:", bin_edges[1]-bin_edges[0], bin_edges[2]-bin_edges[1], bin_edges[3]-bin_edges[2]
bin_size=float(bin_edges[1]-bin_edges[0])
# ojoooooo! normalizar tb por bin size, ademas de N puntos
lista_tuplas=[]
origin=float(bin_edges[0])
file = open(name_h,'wt')
for b in range (num_small_bins -1):
#if hist[b] !=0:
print >> file,bin_edges[b], float(hist[b])/(float(len(lista))*bin_size), float(hist[b]),float(Cumul_prob[b])/(float(len(lista))*bin_size),float(Cumul_prob[b]), float(hist[b])/float(len(lista))
tupla=(bin_edges[b], float(hist[b])/(float(len(lista))*bin_size))
lista_tuplas.append(tupla)
origin=origin+(bin_edges[b+1]-bin_edges[b])
rest_prob=0.
for element in hist[num_small_bins:]:
rest_prob+=element
bin_size=float(bin_edges[-1]-bin_edges[num_small_bins])
print >> file,bin_edges[num_small_bins],rest_prob/(float(len(lista))*bin_size)
file.close()
print "written histogram:",name_h
# print "written:", name_h, " colum names: norm_prob, raw_count, cumul_prob, raw_cumul_count (normalization by N events times bin_size), cumul_prob (normalization by N events only)"
return lista_tuplas
def histograma_bins_return_only_freq(lista,Nbins, name_h):
hist, bin_edges= numpy.histogram(lista, bins=Nbins, range=(float(min(lista)),float(max(lista)))) # optional: range=( , ) The lower and upper range of the bins.
#if i wanna compare several distrib. i MUST give same Nbins and (max,min) range too!!!
# print "\n",name_h,"max:",max(lista),"min:",min(lista)
Cumul_prob=[0]*50000
norm=0.
for item in lista:
for b in range (len(bin_edges)):
value_bin=bin_edges[b]
if value_bin<= item:
Cumul_prob[b]+=1.
norm+=1.
# print "bin size:", bin_edges[1]-bin_edges[0], bin_edges[2]-bin_edges[1], bin_edges[3]-bin_edges[2]
bin_size=float(bin_edges[1]-bin_edges[0])
# ojoooooo! normalizar tb por bin size, ademas de N puntos
origin=float(bin_edges[0])
file = open(name_h,'wt')
for b in range (len(bin_edges)-1):
if hist[b] !=0:
print >> file,origin+(bin_edges[b+1]-bin_edges[b])/2.0, float(hist[b])/(float(len(lista))*bin_size), float(hist[b]),float(Cumul_prob[b])/(float(len(lista))*bin_size),float(Cumul_prob[b])/float(len(lista)),float(Cumul_prob[b])
origin=origin+(bin_edges[b+1]-bin_edges[b])
file.close()
print "written:", name_h, " colum names: norm_prob, raw_count, cumul_prob, raw_cumul_count (normalization by N events times bin_size)"
return hist
def histograma_bins_return_prob_and_cumul(lista,Nbins, name_h):
hist, bin_edges= numpy.histogram(lista, bins=Nbins, range=(float(min(lista)),float(max(lista)))) # optional: range=( , ) The lower and upper range of the bins.
#if i wanna compare several distrib. i MUST give same Nbins and (max,min) range too!!!
# print "\n",name_h,"max:",max(lista),"min:",min(lista)
Cumul_prob=[0]*50000
norm=0.
for item in lista:
for b in range (len(bin_edges)):
value_bin=bin_edges[b]
if value_bin<= item:
Cumul_prob[b]+=1.
norm+=1.
# print "bin size:", bin_edges[1]-bin_edges[0], bin_edges[2]-bin_edges[1], bin_edges[3]-bin_edges[2]
bin_size=float(bin_edges[1]-bin_edges[0])
# ojoooooo! normalizar tb por bin size, ademas de N puntos
lista_tuplas_prob=[]
lista_tuplas_cumulat_prob=[]
origin=float(bin_edges[0])
file = open(name_h,'wt')
for b in range (len(bin_edges)-1):
if hist[b] !=0:
print >> file,origin+(bin_edges[b+1]-bin_edges[b])/2.0, float(hist[b])/(float(len(lista))*bin_size), float(hist[b]),float(Cumul_prob[b])/(float(len(lista))*bin_size),float(Cumul_prob[b])/float(len(lista)),float(Cumul_prob[b])
tupla_prob=(origin+(bin_edges[b+1]-bin_edges[b])/2.0, float(hist[b])/(float(len(lista))*bin_size))
tupla_cumulat=(origin+(bin_edges[b+1]-bin_edges[b])/2.0, float(Cumul_prob[b])/(float(len(lista))*bin_size))
lista_tuplas_prob.append(tupla_prob)
lista_tuplas_cumulat_prob.append(tupla_cumulat)
origin=origin+(bin_edges[b+1]-bin_edges[b])
file.close()
print "written:", name_h, " colum names: norm_prob, raw_count, cumul_prob, raw_cumul_count (normalization by N events times bin_size)"
return lista_tuplas_prob,lista_tuplas_cumulat_prob