-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstatistics.py
463 lines (411 loc) · 17.2 KB
/
statistics.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
460
461
462
463
import json
from itertools import product, combinations
from argparse import ArgumentParser
from selae.selae20 import Euromillions, EuromillionsDraw, EuroPrize
class Number:
def __init__(self):
self.number = 0
self.last_draw_id = 0
self.last_draw_datetime = None
self.last_seen = 0
self.frequence = 0.0
self.max = 0
self.all_appearances = 0
self.last_5 = 0
self.last_10 = 0
self.last_20 = 0
self.last_50 = 0
self.last_100 = 0
self.last_200 = 0
self.last_500 = 0
self.draw_list = []
class Ball(Number):
def __init__(self):
Number.__init__(self)
class Statistics(EuromillionsDraw):
def __init__(self):
EuromillionsDraw.__init__(self)
euromilions = Euromillions()
euromilions.load()
self.data = euromilions.data
self.check_data = euromilions.check_data
self.balls = {}
self.save = euromilions.save
self.skip = 0
def flash_back(self, count=1):
self.skip = count
def get_keys(self):
if not self.skip:
for draw in self.data:
yield draw.id
else:
next = 0
for draw in self.data:
key = draw.id
if next < self.skip:
next += 1
continue
yield key
def run(self):
"""
Primer actualitzem els repetits del darrer sorteig amb l'anterior.
Obtenim les estadístiques dels 50 números
"""
if args.alreadyseen:
self.set_repeated_balls()
for i in range(1, 51):
ball = Ball()
ball.number = i
ball.all_appearances = self.count(i)
last_seen = self.last_seen(i)
ball.last_draw_id = last_seen['draw_id']
ball.last_draw_datetime = last_seen['date']
ball.last_seen = last_seen['draws']
freq = self.number_frequence(i)
ball.frequence = freq['freq']
ball.max = freq['max']
ball.draw_list = freq['list']
ball.last_5 = self.count(i, 5)
ball.last_10 = self.count(i, 10)
ball.last_20 = self.count(i, 20)
ball.last_50 = self.count(i, 50)
ball.last_100 = self.count(i, 100)
ball.last_200 = self.count(i, 200)
ball.last_500 = self.count(i, 500)
self.balls[i] = ball
def fix_alreadyseen(self):
for draw in self.data:
id = draw.id - 1
last_draw = self.data[id-1]
cur_draw = self.data[id]
already_seen = [n for n in cur_draw.balls if n in last_draw.balls]
already_seen.sort()
print (id, last_draw.balls, cur_draw.balls, already_seen, cur_draw.alreadyseen)
cur_draw.alreadyseen = already_seen
self.save()
def count(self, number, limit=0):
"""
Contem el nombre de vegades que ha eixit un número durant els x sortejos.
"""
counter = 0
loop = 1
for key in self.get_keys():
if number in self.data[key-1].balls:
counter += 1
if limit and loop > limit - 1:
break
loop += 1
return counter
def last_seen(self, number):
"""
Obtenim l'última vegada que s'ha vist un número.
"""
i = 0
for key in self.get_keys():
draw = self.data[key-1]
if number in draw.balls:
return {'draws': i, 'date': draw.date.to_date(), 'draw_id': key}
i += 1
def get_frequence(self, item_list):
"""
Obtenim la freqüència de les aparicions d'un element.
Es passa una llista que conté l'id dels sortejos on ha eixit l'element.
"""
rslt = {"freq": 0.0, "max": 0, "min": 0, 'list': []}
frequences = []
for i in range(len(item_list) - 1):
frequences.append(item_list[i] - item_list[i + 1])
if frequences:
rslt["freq"] = sum(frequences) / len(frequences)
rslt['max'] = max(frequences)
rslt["min"] = min(frequences)
rslt['list'] = item_list
return rslt
def number_frequence(self, number):
last_seen_list = []
for key in self.get_keys():
draw = self.data[key-1]
if number in draw.balls:
last_seen_list.append(key)
return self.get_frequence(last_seen_list)
def specnumb_frequence(self, stars):
last_seen_list = []
for key in self.get_keys():
special_numbers = sorted(self.data[key-1].stars)
if stars == f"{special_numbers[0]:02}{special_numbers[1]:02}":
last_seen_list.append(key)
return self.get_frequence(last_seen_list)
def dozen_groups(self):
"""
Obtenim un diccionari de les desenes que han aparegut.
Es registra cada sorteig on apareix.
Es semblant al que es fa a self.number_frequence(), però
aplicat a les desenes.
"""
dozens_dict = {}
for draw_id in self.get_keys():
key = draw_id - 1
dozens = self.data[key].get_tens()
if dozens_dict.get(dozens):
dozens_dict[dozens].append(draw_id)
else:
dozens_dict.update({dozens: [draw_id]})
return dozens_dict
def __sum_last_draws(self, property):
last_draws = []
for i in self.balls:
last_draws.append(getattr(self.balls[i], property))
return sum(last_draws)
def balls_in_last_5(self):
ball_list = []
for i in self.balls:
if self.balls[i].last_5 > 0:
ball_list.append(i)
return ball_list
def balls_not_in_last_5(self):
ball_list = []
for i in self.balls:
if self.balls[i].last_5 == 0:
ball_list.append(i)
return ball_list
def set_repeated_balls(self):
self.check_data()
print("LEN DATA:", len(self.data))
cur_draw_id = self.data[-1].id
prev_draw = self.data[cur_draw_id-1]
cur_draw: EuromillionsDraw = self.data[cur_draw_id]
cur_draw.alreadyseen = [n for n in prev_draw.numbers if n in cur_draw.numbers]
self.save()
def check_issues(self):
for i in range(1, 51):
print(f"{i: >5} {self.balls[i].all_appearances: >7} {self.balls[i].last_5: >5} "
f"{self.balls[i].last_10: >5} {self.balls[i].last_20: >5} {self.balls[i].last_50: >5} "
f"{self.balls[i].last_100: >5} {self.balls[i].last_200: >5} {self.balls[i].last_500: >5}")
print(
f"{len(self.data): >5} {self.__sum_last_draws('all_appearances'): >7} {self.__sum_last_draws('last_5'): >5} "
f"{self.__sum_last_draws('last_10'): >5} {self.__sum_last_draws('last_20'): >5} "
f"{self.__sum_last_draws('last_50'): >5} {self.__sum_last_draws('last_100'): >5} "
f"{self.__sum_last_draws('last_200'): >5} {self.__sum_last_draws('last_500'): >5}"
)
def to_cvs(self):
"""
bola | aparicions | freqüència | max | delta | sortejos des darrera ap |
darrers 5 | d 10 | d 20 | d 50 | d 100 | d 200 | d 500 | llista id sortejos
"""
self.header("NUMBERS", True)
for i in range(1, 51):
draws = "\t".join("%i" % d for d in self.balls[i].draw_list)
diff = self.balls[i].frequence - self.balls[i].last_seen
print(
f"{i}\t{self.balls[i].all_appearances}\t{self.balls[i].frequence:.3f}\t"
f"{self.balls[i].max}\t{diff:.3f}\t{self.balls[i].last_seen}\t{self.balls[i].last_5}\t"
f"{self.balls[i].last_10}\t{self.balls[i].last_20}\t{self.balls[i].last_50}\t{self.balls[i].last_100}\t"
f"{self.balls[i].last_200}\t{self.balls[i].last_500}\t{draws}".replace('.', ',')
)
#self.csv_summary()
def csv_summary(self):
print(
f"\n{len(self.data)}\t{self.__sum_last_draws('all_appearances')}\t{self.__sum_last_draws('frequence')/50:.3f}"
f"\t\t\t{self.__sum_last_draws('last_5')}\t{self.__sum_last_draws('last_10')}\t{self.__sum_last_draws('last_20')}\t"
f"{self.__sum_last_draws('last_50')}\t{self.__sum_last_draws('last_100')}\t"
f"{self.__sum_last_draws('last_200')}\t{self.__sum_last_draws('last_500')}"
)
def to_cvs_short_fmt(self):
for i in range(1, 51):
draws = "\t".join("%i" % d for d in self.balls[i].draw_list)
print(f"{i}\t{self.balls[i].all_appearances}\t{self.balls[i].last_seen}\t"
f"{self.balls[i].frequence:.3f}\t{self.balls[i].max}\t{draws}".replace('.', ','))
def dozen_permutations(self):
for permutation in product([0, 1, 2, 3, 4, 5], repeat=5):
if sum(permutation) == 5:
yield "".join(str(i) for i in permutation)
def _get_consecutives(self, draw_id):
draw: EuromillionsDraw = self.data[draw_id]
numbers = sorted(draw.balls)
return self.get_consecutives(numbers)
def _get_repeated_unities(self, draw):
numbers = sorted(self.data[draw].balls)
return self.get_repeated_unities(numbers)
def _get_repeated_tens(self, draw):
numbers = sorted(self.data[draw].numbers)
return self.get_repeated_tens(numbers)
def _get_dozens(self, draw):
numbers = sorted(self.data[draw].numbers)
return self.get_dozens(numbers)
def last_draw(self):
print(self.data[-1])
def soupify(self, soup):
string = "\n"
for key in soup:
string += f"\t[:{key}:] ->\n"
for subkey in reversed(list(soup[key].keys())):
string += f"\t\t[:{subkey}:] -> ({', '.join(str(x) for x in soup[key][subkey])})\n"
return string
def show_last_draws(self, limit=5):
i = 1
for draw_id in self.get_keys():
key = draw_id - 1
if i > limit:
break
draw = self.data[key]
print(draw)
i += 1
last_5 = self.balls_in_last_5()
more = [b for b in range(1,51) if b not in last_5] #self.balls_not_in_last_5()
print(f"\n\n{len(last_5)}: {last_5}\n{len(more)}: {more}".replace(", ", "\t"))
last_5_soup = self.show_balls_by_appearance_order(last_5)
print (f"bab: {self.soupify(last_5_soup)}".replace(", ", "\t"))
def show_balls_by_appearance_order(self, expected_balls=[]):
"""
Obtenim dos diccionaris que ens indiquen les boles que han eixit en els darrers
cinc sortejos.
S'ordenen per ordre d'aparició, els de més a l'esquerra són els més antics. El
diccionari els tria per desenes.
Li passem la llista de les boles dels cinc darrers sortejos.
"""
by_appearance_order_balls = []
draw_id = (self.data[-1].id -1) - self.skip
while len(by_appearance_order_balls)<50:
draw = self.data[draw_id]
for ball in draw.balls:
if ball in by_appearance_order_balls:
by_appearance_order_balls.remove(ball)
by_appearance_order_balls.insert(0, ball)
draw_id -= 1
last_5_dict = {4:[], 3: [], 2: [], 1: [], 0: []}
remainder_dict = {4: [], 3: [], 2: [], 1: [], 0: []}
for k in by_appearance_order_balls:
if k in expected_balls:
last_5_dict[self.get_dozen(k)].append(k)
else:
remainder_dict[self.get_dozen(k)].append(k)
return {'last 5': last_5_dict, 'remainder balls': remainder_dict}
def ball_friends(self, number):
"""
Extraem les boles amb les que eix més la bola que li passem.
"""
friends = {}
for draw_id in self.get_keys():
key = draw_id - 1
if number in self.data[key].balls:
numbers = list(self.data[key].balls)
numbers.remove(number)
for n in numbers:
if friends.get(n):
friends[n] += 1
else:
friends[n] = 1
return friends
def dozens_tables(self):
"""
grup | aparicions | freqüència | max | min | delta | sortejos des darrera ap | llista id sortejos
"""
self.header("DOZENS")
dozens_groups = self.dozen_groups()
for c in self.dozen_permutations():
d = dozens_groups.get(c, [])
last_seen = len(self.data) - d[0] if d else len(self.data)
f = self.get_frequence(d)
freq = f"{f['freq']:.3f}\t{f['max']}\t{f['min']}"
strd = '\t'.join([str(s) for s in d])
delta = f['freq'] - last_seen
print(f"{c}\t{len(d)}\t{freq}\t{delta:.3f}\t{last_seen}\t{strd}".replace('.', ','))
def header(self, title: str, first=False):
head = ""
if not first:
head += "\n\n"
print (f"{head}## -- {title.upper()} -- ##\n")
def special_number_table(self):
""""
parella | aparicions | freqüència | max | min | delta
"""
sdict = {}
for sn in combinations(range(1,13), 2):
key = f"{sn[0]:02}{sn[1]:02}"
sdict[key] = self.specnumb_frequence(key)
self.header('STARS')
for key in sorted(sdict):
sn = sdict[key]
last_seen = len(self.data) - sn['list'][0]
freq = f"{sn['freq']:.3f}\t{sn['max']}\t{sn['min']}"
draws = '\t'.join(str(s) for s in sn['list'])
delta = sn['freq'] - last_seen
print(f"{key}\t{len(sn['list'])}\t{freq}\t{delta:.3f}\t{last_seen}\t{draws}".replace('.', ','))
self.show_last_draws()
self.header("BALL FRIENDS")
for i in range(1, 51):
friends = self.ball_friends(i)
print(f"{i}:")
inv_friends = {}
for k, v in friends.items():
inv_friends.setdefault(v, []).append(k)
best_friends = list(reversed(sorted(inv_friends.keys())))[:5]
for k in reversed(sorted(inv_friends.keys())):
if k in best_friends:
print("\t", k, tuple(sorted(inv_friends[k])))
def crosses(self, draw_id=0, hide_header=False):
"""
Mostra les creus per a la fulla de càlcul.
"""
if not draw_id:
key = self.data[-1].id - 1
else:
key = draw_id - 1
cross = '×'
header = "%s%s\t%s\n" % (
'\n\n== # ×+× CROSSES ×+× # ==\n',
' ' * 10,
'\t'.join(str(i) for i in range(1,51))
)
rtn = header if not hide_header else ''
draw = self.data[key]
rtn += f'{draw.date.to_date():%Y/%m/%d}\t'
for i in range(1, 51):
if i in draw.balls:
rtn += f'{cross}'
rtn += '\t'
print (rtn)
if __name__ == '__main__':
"""-rtCdR 0"""
"""-rtCduR 0"""
stats = Statistics()
arg_parser = ArgumentParser()
arg_parser.add_argument('-C', dest='csv', action='store_true')
arg_parser.add_argument('-c', dest='checkissues', action='store_true')
arg_parser.add_argument('-F', dest='fixalreadyseen', action='store_true', default=False)
arg_parser.add_argument('-i', dest='increase', action='store_true', default=False)
arg_parser.add_argument('-k', dest='combinatories', action='store_true')
arg_parser.add_argument('-R', '--crosses', dest='crosses', action='store', type=int, default=0)
arg_parser.add_argument('-d', dest='dozenstables', action='store_true')
arg_parser.add_argument('-f', dest='flashback', action='store', type=int)
arg_parser.add_argument('-r', dest='run', action='store_true')
arg_parser.add_argument('-s', dest='spectrum', action='store_true')
arg_parser.add_argument('-t', dest='specnumtable', action='store_true')
arg_parser.add_argument('-u', '--updatealreadyseen', dest='alreadyseen', action='store_true')
arg_parser.add_argument('-w', dest='watch', action='store_true')
args = arg_parser.parse_args()
if args.checkissues:
stats.check_issues()
elif args.flashback:
stats.flash_back(args.flashback)
if args.run:
stats.run()
if args.fixalreadyseen:
stats.fix_alreadyseen()
if args.csv:
stats.to_cvs()
if args.dozenstables:
stats.dozens_tables()
if args.specnumtable:
stats.special_number_table()
if args.crosses is 0 or args.crosses:
stats.crosses(args.crosses)
if args.increase:
hide=False
while True:
try:
args.crosses += 1
stats.crosses(args.crosses, hide_header=hide)
hide=True
except:
break