2021-11-02 05:37:27 +00:00
import abc
2021-10-21 20:05:07 +00:00
import data as d
2021-10-21 21:27:35 +00:00
import signal
import numpy as np
2022-01-10 20:23:14 +00:00
from tra_analysis import Analysis as an
2022-03-28 19:43:24 +00:00
from tqdm import tqdm
2021-10-21 20:42:47 +00:00
2021-11-02 05:37:27 +00:00
class Module ( metaclass = abc . ABCMeta ) :
@classmethod
def __subclasshook__ ( cls , subclass ) :
2021-11-23 22:23:59 +00:00
return ( hasattr ( subclass , ' __init__ ' ) and
callable ( subclass . __init__ ) and
hasattr ( subclass , ' validate_config ' ) and
callable ( subclass . validate_config ) and
hasattr ( subclass , ' run ' ) and
callable ( subclass . run )
2021-11-02 05:37:27 +00:00
)
@abc.abstractmethod
2021-11-23 22:23:59 +00:00
def __init__ ( self , config , apikey , tbakey , timestamp , competition , * args , * * kwargs ) :
raise NotImplementedError
@abc.abstractmethod
def validate_config ( self , * args , * * kwargs ) :
raise NotImplementedError
@abc.abstractmethod
2022-02-19 06:19:13 +00:00
def run ( self , * args , * * kwargs ) :
2021-11-02 05:37:27 +00:00
raise NotImplementedError
2021-10-21 20:05:07 +00:00
2021-11-02 05:37:27 +00:00
class Match ( Module ) :
2021-10-28 20:13:57 +00:00
2021-10-21 20:05:07 +00:00
config = None
apikey = None
tbakey = None
timestamp = None
2021-10-22 01:40:56 +00:00
competition = None
2021-10-21 20:05:07 +00:00
2021-10-28 20:13:57 +00:00
data = None
results = None
2021-10-21 20:05:07 +00:00
2021-10-22 01:40:56 +00:00
def __init__ ( self , config , apikey , tbakey , timestamp , competition ) :
2021-10-21 20:05:07 +00:00
self . config = config
self . apikey = apikey
self . tbakey = tbakey
self . timestamp = timestamp
2021-10-22 01:40:56 +00:00
self . competition = competition
2021-10-21 20:05:07 +00:00
def validate_config ( self ) :
2021-10-21 21:58:02 +00:00
return True , " "
2021-10-21 20:05:07 +00:00
2022-02-19 06:19:13 +00:00
def run ( self ) :
2021-11-23 22:23:59 +00:00
self . _load_data ( )
2022-02-19 06:19:13 +00:00
self . _process_data ( )
2021-11-23 22:23:59 +00:00
self . _push_results ( )
def _load_data ( self ) :
2021-10-22 01:40:56 +00:00
self . data = d . load_match ( self . apikey , self . competition )
2022-01-10 20:23:14 +00:00
def _simplestats ( self , data_test ) :
2021-10-21 21:27:35 +00:00
2021-10-22 01:40:56 +00:00
signal . signal ( signal . SIGINT , signal . SIG_IGN )
2021-10-21 21:27:35 +00:00
2021-10-22 01:40:56 +00:00
data = np . array ( data_test [ 3 ] )
2021-10-21 21:27:35 +00:00
data = data [ np . isfinite ( data ) ]
ranges = list ( range ( len ( data ) ) )
2021-10-22 01:40:56 +00:00
test = data_test [ 2 ]
if test == " basic_stats " :
return an . basic_stats ( data )
if test == " historical_analysis " :
return an . histo_analysis ( [ ranges , data ] )
if test == " regression_linear " :
return an . regression ( ranges , data , [ ' lin ' ] )
if test == " regression_logarithmic " :
return an . regression ( ranges , data , [ ' log ' ] )
2021-10-21 21:27:35 +00:00
2021-10-22 01:40:56 +00:00
if test == " regression_exponential " :
return an . regression ( ranges , data , [ ' exp ' ] )
2021-10-21 21:27:35 +00:00
2021-10-22 01:40:56 +00:00
if test == " regression_polynomial " :
return an . regression ( ranges , data , [ ' ply ' ] )
2021-10-21 21:27:35 +00:00
2021-10-22 01:40:56 +00:00
if test == " regression_sigmoidal " :
return an . regression ( ranges , data , [ ' sig ' ] )
2021-10-21 20:05:07 +00:00
2022-02-19 06:19:13 +00:00
def _process_data ( self ) :
2021-10-22 01:40:56 +00:00
tests = self . config [ " tests " ]
data = self . data
input_vector = [ ]
for team in data :
for variable in data [ team ] :
if variable in tests :
for test in tests [ variable ] :
input_vector . append ( ( team , variable , test , data [ team ] [ variable ] ) )
self . data = input_vector
2022-01-10 20:23:14 +00:00
self . results = [ ]
for test_var_data in self . data :
self . results . append ( self . _simplestats ( test_var_data ) )
2021-10-21 20:05:07 +00:00
2021-11-23 22:23:59 +00:00
def _push_results ( self ) :
2021-10-22 01:40:56 +00:00
2021-10-21 21:27:35 +00:00
short_mapping = { " regression_linear " : " lin " , " regression_logarithmic " : " log " , " regression_exponential " : " exp " , " regression_polynomial " : " ply " , " regression_sigmoidal " : " sig " }
2021-10-22 01:40:56 +00:00
class AutoVivification ( dict ) :
def __getitem__ ( self , item ) :
try :
return dict . __getitem__ ( self , item )
except KeyError :
value = self [ item ] = type ( self ) ( )
return value
result_filtered = self . results
input_vector = self . data
return_vector = AutoVivification ( )
2021-10-21 21:27:35 +00:00
i = 0
2021-10-22 01:40:56 +00:00
for result in result_filtered :
filtered = input_vector [ i ] [ 2 ]
try :
short = short_mapping [ filtered ]
return_vector [ input_vector [ i ] [ 0 ] ] [ input_vector [ i ] [ 1 ] ] [ input_vector [ i ] [ 2 ] ] = result [ short ]
except KeyError : # not in mapping
return_vector [ input_vector [ i ] [ 0 ] ] [ input_vector [ i ] [ 1 ] ] [ input_vector [ i ] [ 2 ] ] = result
i + = 1
self . results = return_vector
2021-10-28 20:13:57 +00:00
d . push_match ( self . apikey , self . competition , self . results )
2021-11-02 05:37:27 +00:00
class Metric ( Module ) :
2021-10-28 20:13:57 +00:00
config = None
apikey = None
tbakey = None
timestamp = None
competition = None
data = None
results = None
def __init__ ( self , config , apikey , tbakey , timestamp , competition ) :
self . config = config
self . apikey = apikey
self . tbakey = tbakey
self . timestamp = timestamp
self . competition = competition
def validate_config ( self ) :
return True , " "
2022-02-19 06:19:13 +00:00
def run ( self ) :
2021-11-23 22:23:59 +00:00
self . _load_data ( )
2022-02-19 06:19:13 +00:00
self . _process_data ( )
2021-11-23 22:23:59 +00:00
self . _push_results ( )
def _load_data ( self ) :
2021-10-28 20:40:56 +00:00
self . data = d . pull_new_tba_matches ( self . tbakey , self . competition , self . timestamp )
2021-10-28 20:13:57 +00:00
2022-02-19 06:19:13 +00:00
def _process_data ( self ) :
2021-10-28 20:13:57 +00:00
2022-03-29 21:17:58 +00:00
self . results = { }
2021-10-28 20:13:57 +00:00
matches = self . data
red = { }
blu = { }
2022-03-28 22:02:39 +00:00
for match in tqdm ( matches , desc = " Metrics " ) : # grab matches and loop through each one
red = d . load_metric ( self . apikey , self . competition , match , " red " , self . config [ " tests " ] ) # get the current ratings for red
blu = d . load_metric ( self . apikey , self . competition , match , " blue " , self . config [ " tests " ] ) # get the current ratings for blue
2021-10-28 20:13:57 +00:00
gl2_red_score_total = 0
gl2_blu_score_total = 0
gl2_red_rd_total = 0
gl2_blu_rd_total = 0
gl2_red_vol_total = 0
gl2_blu_vol_total = 0
2022-03-29 04:17:25 +00:00
for team in red : # for each team in red, add up gl2 score components
2021-10-28 20:13:57 +00:00
gl2_red_score_total + = red [ team ] [ " gl2 " ] [ " score " ]
gl2_red_rd_total + = red [ team ] [ " gl2 " ] [ " rd " ]
gl2_red_vol_total + = red [ team ] [ " gl2 " ] [ " vol " ]
2022-03-29 04:17:25 +00:00
for team in blu : # for each team in blue, add up gl2 score components
2021-10-28 20:13:57 +00:00
gl2_blu_score_total + = blu [ team ] [ " gl2 " ] [ " score " ]
gl2_blu_rd_total + = blu [ team ] [ " gl2 " ] [ " rd " ]
gl2_blu_vol_total + = blu [ team ] [ " gl2 " ] [ " vol " ]
2022-03-28 22:02:39 +00:00
red_gl2 = { " score " : gl2_red_score_total / len ( red ) , " rd " : gl2_red_rd_total / len ( red ) , " vol " : gl2_red_vol_total / len ( red ) } # average the scores by dividing by 3
blu_gl2 = { " score " : gl2_blu_score_total / len ( blu ) , " rd " : gl2_blu_rd_total / len ( blu ) , " vol " : gl2_blu_vol_total / len ( blu ) } # average the scores by dividing by 3
2022-03-28 19:21:23 +00:00
2021-10-28 20:13:57 +00:00
2022-03-28 22:02:39 +00:00
if match [ " winner " ] == " red " : # if red won, set observations to {"red": 1, "blu": 0}
2021-10-28 20:13:57 +00:00
observations = { " red " : 1 , " blu " : 0 }
2022-03-28 22:02:39 +00:00
elif match [ " winner " ] == " blue " : # if blue won, set observations to {"red": 0, "blu": 1}
2021-10-28 20:13:57 +00:00
observations = { " red " : 0 , " blu " : 1 }
2022-03-28 22:02:39 +00:00
else : # otherwise it was a tie and observations is {"red": 0.5, "blu": 0.5}
2021-10-28 20:13:57 +00:00
observations = { " red " : 0.5 , " blu " : 0.5 }
2022-03-28 19:21:23 +00:00
2021-10-28 20:13:57 +00:00
2022-03-28 22:02:39 +00:00
new_red_gl2_score , new_red_gl2_rd , new_red_gl2_vol = an . Metric ( ) . glicko2 ( red_gl2 [ " score " ] , red_gl2 [ " rd " ] , red_gl2 [ " vol " ] , [ blu_gl2 [ " score " ] ] , [ blu_gl2 [ " rd " ] ] , [ observations [ " red " ] , observations [ " blu " ] ] ) # calculate new scores for gl2 for red
new_blu_gl2_score , new_blu_gl2_rd , new_blu_gl2_vol = an . Metric ( ) . glicko2 ( blu_gl2 [ " score " ] , blu_gl2 [ " rd " ] , blu_gl2 [ " vol " ] , [ red_gl2 [ " score " ] ] , [ red_gl2 [ " rd " ] ] , [ observations [ " blu " ] , observations [ " red " ] ] ) # calculate new scores for gl2 for blue
2021-10-28 20:13:57 +00:00
2022-03-28 22:02:39 +00:00
red_gl2_delta = { " score " : new_red_gl2_score - red_gl2 [ " score " ] , " rd " : new_red_gl2_rd - red_gl2 [ " rd " ] , " vol " : new_red_gl2_vol - red_gl2 [ " vol " ] } # calculate gl2 deltas for red
blu_gl2_delta = { " score " : new_blu_gl2_score - blu_gl2 [ " score " ] , " rd " : new_blu_gl2_rd - blu_gl2 [ " rd " ] , " vol " : new_blu_gl2_vol - blu_gl2 [ " vol " ] } # calculate gl2 deltas for blue
2021-10-28 20:13:57 +00:00
2022-03-28 22:02:39 +00:00
for team in red : # for each team on red, add the previous score with the delta to find the new score
2021-10-28 20:13:57 +00:00
red [ team ] [ " gl2 " ] [ " score " ] = red [ team ] [ " gl2 " ] [ " score " ] + red_gl2_delta [ " score " ]
red [ team ] [ " gl2 " ] [ " rd " ] = red [ team ] [ " gl2 " ] [ " rd " ] + red_gl2_delta [ " rd " ]
red [ team ] [ " gl2 " ] [ " vol " ] = red [ team ] [ " gl2 " ] [ " vol " ] + red_gl2_delta [ " vol " ]
2022-03-28 22:02:39 +00:00
for team in blu : # for each team on blue, add the previous score with the delta to find the new score
2021-10-28 20:13:57 +00:00
blu [ team ] [ " gl2 " ] [ " score " ] = blu [ team ] [ " gl2 " ] [ " score " ] + blu_gl2_delta [ " score " ]
blu [ team ] [ " gl2 " ] [ " rd " ] = blu [ team ] [ " gl2 " ] [ " rd " ] + blu_gl2_delta [ " rd " ]
blu [ team ] [ " gl2 " ] [ " vol " ] = blu [ team ] [ " gl2 " ] [ " vol " ] + blu_gl2_delta [ " vol " ]
temp_vector = { }
2022-03-28 22:02:39 +00:00
temp_vector . update ( red ) # update the team's score with the temporay vector
2021-10-28 20:13:57 +00:00
temp_vector . update ( blu )
2022-03-29 21:17:58 +00:00
self . results [ match [ ' match ' ] ] = temp_vector
2022-03-28 22:02:39 +00:00
d . push_metric ( self . apikey , self . competition , temp_vector ) # push new scores to db
2021-10-28 20:13:57 +00:00
2021-11-23 22:23:59 +00:00
def _push_results ( self ) :
2021-10-28 20:13:57 +00:00
pass
2021-11-02 05:37:27 +00:00
class Pit ( Module ) :
2021-10-28 20:13:57 +00:00
config = None
apikey = None
tbakey = None
timestamp = None
competition = None
data = None
results = None
def __init__ ( self , config , apikey , tbakey , timestamp , competition ) :
self . config = config
self . apikey = apikey
self . tbakey = tbakey
self . timestamp = timestamp
self . competition = competition
def validate_config ( self ) :
return True , " "
2022-02-19 06:19:13 +00:00
def run ( self ) :
2021-11-23 22:23:59 +00:00
self . _load_data ( )
2022-02-19 06:19:13 +00:00
self . _process_data ( )
2021-11-23 22:23:59 +00:00
self . _push_results ( )
def _load_data ( self ) :
2021-10-28 20:13:57 +00:00
self . data = d . load_pit ( self . apikey , self . competition )
2022-02-19 06:19:13 +00:00
def _process_data ( self ) :
2022-02-04 03:28:08 +00:00
tests = self . config [ " tests " ]
2021-10-28 20:13:57 +00:00
return_vector = { }
for team in self . data :
for variable in self . data [ team ] :
2022-02-04 03:28:08 +00:00
if variable in tests :
2021-10-28 20:13:57 +00:00
if not variable in return_vector :
return_vector [ variable ] = [ ]
return_vector [ variable ] . append ( self . data [ team ] [ variable ] )
self . results = return_vector
2021-11-23 22:23:59 +00:00
def _push_results ( self ) :
2021-11-05 23:17:42 +00:00
d . push_pit ( self . apikey , self . competition , self . results )
class Rating ( Module ) :
pass
class Heatmap ( Module ) :
pass
class Sentiment ( Module ) :
pass