-
Notifications
You must be signed in to change notification settings - Fork 0
/
statistics.py
120 lines (111 loc) · 4.74 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
from predictions import Prediction
from scrapper import Scrapper
### Statistics class ###
class Statistics:
def __init__(self):
self.has_scrapper_links = False
self.has_valid_predictions = False
self.driver_standings = []
self.team_standings = []
self.predictions = {}
def check_stats(self):
try:
open("links.txt")
open("predictions.txt")
self.has_scrapper_links = True
self.has_valid_predictions = True
except FileNotFoundError:
print("Statistics cannot be initialised, predictions and links not properly configured")
return self
def initialise_stats(self):
if self.has_scrapper_links and self.has_valid_predictions:
self.predictions = Prediction().initialise_prediction().get_all_prediction()
self.driver_standings = Scrapper().initialise_links().scrape_driver()
self.team_standings = Scrapper().initialise_links().scrape_constructor()
return self
else:
print("Links and predictions not initialised properly")
return self
def count_driver_zeros(self):
counter = 0
for info in self.driver_standings:
formatted_info = info.split(" ")
if formatted_info[3] == "0":
counter += 1
return counter
def min_driver_zero(self):
counter = 1
for info in self.driver_standings:
formatted_info = info.split(" ")
if formatted_info[3] == "0":
break
counter += 1
return counter
def count_team_zeros(self):
counter = 0
for info in self.team_standings:
formatted_info = info.split(" ")
if formatted_info[-1] == "0":
counter += 1
return counter
def min_team_zero(self):
counter = 1
for info in self.team_standings:
formatted_info = info.split(" ")
if formatted_info[-1] == "0":
break
counter += 1
return counter
def calculate_deviation(self, name, who):
if who == "driver":
driver_prediction = self.predictions[name]["drivers"]
inaccuracy_score = 0
current_predicted_position = 1
current_actual_position = 1
for cd in driver_prediction:
for ranking in self.driver_standings:
if cd.lower() in ranking.lower():
if ranking.split(" ")[3] == "0":
if current_predicted_position < self.min_driver_zero():
score = abs(self.min_driver_zero() - current_predicted_position)
inaccuracy_score += score
current_actual_position = 1
else:
inaccuracy_score += 0
current_actual_position = 1
else:
score = abs(current_actual_position - current_predicted_position)
inaccuracy_score += score
current_actual_position = 1
break
current_actual_position += 1
current_predicted_position += 1
return inaccuracy_score
elif who == "team":
driver_prediction = self.predictions[name]["teams"]
inaccuracy_score = 0
current_predicted_position = 1
current_actual_position = 1
for cd in driver_prediction:
for ranking in self.team_standings:
if cd.lower() in ranking.lower():
if ranking.split(" ")[2] == "0":
if current_predicted_position < self.min_team_zero():
score = abs(self.min_team_zero() - current_predicted_position)
inaccuracy_score += score
current_actual_position = 1
else:
inaccuracy_score += 0
current_actual_position = 1
else:
score = abs(current_actual_position - current_predicted_position)
inaccuracy_score += score
current_actual_position = 1
break
current_actual_position += 1
current_predicted_position += 1
return inaccuracy_score
def calculate_all_deviation(self, who):
for person in self.predictions.keys():
print(person)
print(self.calculate_deviation(person, who))