コード例 #1
0
 def test_games_before_january_not_2012(self):
     """Can I fetch games for home teams on the months October-December of a season?"""
     for _ in range(20):  # number of time to do the test
         year_begin = choice(list(set(range(2005, 2014)) - {2012}))
         # year_begin = randint(2005, 2014)
         season = Season(global_logger,
                         db_root=self.db_root,
                         repo_model=self.repoModel,
                         year_begin=year_begin)
         home_team = choice(list(season.get_teams()))
         the_month = randint(10, 12)
         the_day = randint(15, 30)
         base_date = datetime.date(year=season.year_begin,
                                   month=the_month,
                                   day=the_day)
         delta_in_days = randint(
             20,
             40)  # let's make this large enough for a game to always exist.
         # print("[base: %s] Trying to fetch games for '%s', up until %d days before" % (base_date, home_team, delta_in_days))
         result = season.get_game_at_or_just_before(
             game_date=base_date,
             home_team_abbr=home_team,
             delta_in_days=delta_in_days)
         if result is None:
             season.get_game_at_or_just_before(game_date=base_date,
                                               home_team_abbr=home_team,
                                               delta_in_days=delta_in_days)
         self.assertIsNotNone(
             result,
             "[%s] Impossible to find a game for '%s' up to %d days before %s"
             % (season, home_team, delta_in_days, base_date))
コード例 #2
0
ファイル: test_game.py プロジェクト: YounesZ/NHL_stats
class TestGame(unittest.TestCase):
    """Testing definitions of Game's."""
    def setUp(self):
        """Initialization"""
        self.db_root = Config(
        ).data_dir  # This is the location of the Hockey database
        self.repoCode = get_git_root()

        self.repoModel = path.join(
            self.repoCode,
            'ReinforcementLearning/NHL/playerstats/offVSdef/Automatic_classification/MODEL_perceptron_1layer_10units_relu'
        )

        # Now lets get game data
        self.season = Season(
            db_root=self.db_root, repo_model=self.repoModel,
            year_begin=2012)  # Season.from_year_begin(2012) # '20122013'
        # Montreal received Ottawa on march 13, 2013, let's convert game date to game code
        gameId = self.season.get_game_id(home_team_abbr='MTL',
                                         game_date=datetime.date(year=2013,
                                                                 month=3,
                                                                 day=13))
        self.a_game = Game(self.season, gameId=gameId)

    def test_shifts_differential(self):
        """Are the differential for lines properly calculated?"""

        days_before = 20
        all_teams = self.season.get_teams()
        for _ in range(10):  # repeat this test 10 times
            random_date = datetime.date(year=2013,
                                        month=random.randint(1, 4),
                                        day=random.randint(1, 28))
            random_team = random.sample(all_teams, 1)[0]
            result = self.season.get_game_at_or_just_before(
                random_date,
                home_team_abbr=random_team,
                delta_in_days=days_before)
            if result is None:
                print(
                    "WARNING => No home game for '%s' up to %d days before %s"
                    % (random_team, days_before, random_date))
            else:
                random_game_id, game_date = result
                print("[home team: '%s'] Examining game %d (from %s)" %
                      (random_team, random_game_id, game_date))
                random_game = Game(self.season, gameId=random_game_id)
                df_differential = random_game.lineShifts.shifts[
                    'differential'].reset_index()  # ?
                idxs_change_differential = df_differential.diff()[
                    df_differential.diff().differential != 0].index.values
                for idx in idxs_change_differential[
                        1:]:  # skip fist one, because it's a NaN
                    self.assertNotEqual(
                        random_game.lineShifts.shifts.iloc[idx]['GOAL'], 0,
                        "Differential is %d but there was no goal scored" %
                        (random_game.lineShifts.shifts.iloc[idx]
                         ['differential']))

    def test_shifts_goals_generate_differential(self):
        """Are the differential for lines properly calculated?"""

        days_before = 20
        all_teams = self.season.get_teams()
        for _ in range(10):  # repeat this test 10 times
            random_date = datetime.date(year=2013,
                                        month=random.randint(1, 4),
                                        day=random.randint(1, 28))
            random_team = random.sample(all_teams, 1)[0]
            result = self.season.get_game_at_or_just_before(
                random_date,
                home_team_abbr=random_team,
                delta_in_days=days_before)
            if result is None:
                print(
                    "WARNING => No home game for '%s' up to %d days before %s"
                    % (random_team, days_before, random_date))
            else:
                random_game_id, game_date = result
                print("[home team: '%s'] Examining game %d (from %s)" %
                      (random_team, random_game_id, game_date))
                random_game = Game(self.season, gameId=random_game_id)
                df_goals = random_game.lineShifts.shifts['GOAL'].reset_index()
                idxs_goals = [
                    a_val
                    for a_val in df_goals[df_goals.GOAL != 0].index.values
                    if a_val > 0
                ]
                for idx in idxs_goals:
                    diff = random_game.lineShifts.shifts.iloc[idx][
                        'differential']
                    diff_before = random_game.lineShifts.shifts.iloc[
                        idx - 1]['differential']
                    goals_now = random_game.lineShifts.shifts.iloc[idx]['GOAL']
                    goals_before = random_game.lineShifts.shifts.iloc[
                        idx - 1]['GOAL']
                    expected_result = diff_before + goals_now
                    self.assertEqual(
                        diff, expected_result,
                        "\n%s\n [index: %d] Differential is %d but it should be => %d (== (diff before) %d + %d (goals now))"
                        %
                        (random_game.lineShifts.shifts.iloc[idx - 2:idx + 2][[
                            'GOAL', 'differential'
                        ]], idx, diff, expected_result, diff_before,
                         goals_now))
コード例 #3
0
    def evaluate_all_coaches(self, season_year_begin: int,
                             teams_opt: Optional[List[str]], n_games: int):
        from ReinforcementLearning.NHL.playbyplay.state_space_data import HockeySS
        """Initialization"""
        os.makedirs(self.base_dir, exist_ok=True)
        my_config = Config()
        self.alogger.debug("Data configured to be in '%s'" %
                           (my_config.data_dir))

        db_root = my_config.data_dir
        repoCode = get_git_root()

        repoModel = path.join(
            repoCode,
            'ReinforcementLearning/NHL/playerstats/offVSdef/Automatic_classification/MODEL_perceptron_1layer_10units_relu'
        )

        season = Season(self.alogger,
                        db_root=db_root,
                        year_begin=season_year_begin,
                        repo_model=repoModel)

        # Line translation table
        linedict = HockeySS(db_root)
        linedict.make_line_dictionary()
        linedict = linedict.line_dictionary

        # Load the Qvalues table
        Qvalues = \
        pickle.load(open(path.join(repoCode, 'ReinforcementLearning/NHL/playbyplay/data/stable/RL_action_values.p'), 'rb'))[
            'action_values']

        # Visualize it dimensions (period x differential x away line's code x home line's code)
        print('Q-table dimensions: ', Qvalues.shape)

        # for what teams will we run this calculation?
        calc_teams = season.get_teams() if teams_opt is None else teams_opt
        for a_team in calc_teams:
            season.alogger.debug("=============> calculating %s" % (a_team))
            seconds = get_teams_coach_performance(season,
                                                  team_abbr=a_team,
                                                  maybe_a_starting_date=None,
                                                  line_dict=linedict,
                                                  Qvalues=Qvalues,
                                                  how_many_games=n_games)
            season.alogger.debug(seconds)

            if seconds["does_not_match_optimal"] == seconds[
                    "matches_optimal"] == 0:
                season.alogger.info(
                    "[team: '%s'] No evidence for coach to be evaluated on." %
                    (a_team))
            else:
                total_secs = seconds['matches_optimal'] + seconds[
                    'does_not_match_optimal']
                season.alogger.info(
                    "['%s'] Home coach's score is %d (secs. optimal) / %d (secs. total) = %.2f (in [0,1])"
                    % (a_team, seconds['matches_optimal'], total_secs,
                       seconds['matches_optimal'] / total_secs))
            file_to_save = os.path.join(self.base_dir, a_team + ".pkl")
            self.alogger.debug("Saving data for '%s' in file %s" %
                               (a_team, file_to_save))
            with open(file_to_save, 'wb') as dict_file:
                pickle.dump(seconds, dict_file)
            self.alogger.debug("DONE")