def create(self): # TODO: there is no test for this function # get schedule for season # using self.season_stats_file create all examples for the season for # as much data as we have available. schedule = nfl_local_data_handler() season_matchups = schedule.get_schedule(self.season) for matchup in season_matchups: if (self.week != "ALL" and str(self.week) == matchup['Week']) or self.week == "ALL": ex = nfl_example_maker(matchup['HomeTeam'], matchup['AwayTeam'], self.season, int(matchup['Week'])) print(matchup) self.season_examples.append(ex) # examples created for the first week do not make sense since there are no # previous weeks in order to make average stat calcs from. Because of that # the ordered_example_keys of a first week example is not right. we need # to use an example from week 2+ key_order = [] for i in range(len(self.season_examples)): if self.season_examples[i].week >= 2: key_order = self.season_examples[i].ordered_example_keys break key_order = self.rearrange_keys(key_order) if self.write_to_file: self.writer.create_header(key_order, self.season_examples_file) for i in range(len(self.season_examples)): self.writer.write(self.season_examples[i].example_data_dict, key_order, self.season_examples_file, 'a')
def test_one_off_for_specific_game(self): example = nfl_example_maker('ATL', 'WAS', 2009, 9) key_order = example.ordered_example_keys self.writer.create_header(key_order, self.csv_file) self.writer.write(example.example_data_dict, key_order, self.csv_file, 'a') self.assertDictEqual(example.example_data_dict, self.writer.read(key_order, self.csv_file)[1])
def test_write_multiple_examples_as_csv(self): examples = [nfl_example_maker('NYJ', 'BAL', 2004, 10), nfl_example_maker('PHI', 'DAL', 2001, 3), nfl_example_maker('NYG', 'ATL', 1998, 6), nfl_example_maker('DET', 'KC', 2007, 16)] key_order = examples[0].ordered_example_keys self.writer.create_header(key_order, self.csv_file) for i in range(len(examples)): self.writer.write(examples[i].example_data_dict, key_order, self.csv_file, 'a') dict_as_read_from_csv_file = \ self.writer.read(key_order, self.csv_file) for i in range(len(examples)): # have to add one to the csv_file_dict because the first line is hdr self.assertDictEqual(examples[i].example_data_dict, dict_as_read_from_csv_file[i + 1])
def test_time_of_possession(self): example = nfl_example_maker('NYG', 'ATL', 1998, 6) expected_home_top = '1660.0' expected_home_opp_top = '1940.0' expected_away_top = '1976.25' expected_away_opp_top = '1623.75' actual_home_top = example.example_data_dict['HOMETimeOfPossession'] self.assertEquals(expected_home_top, actual_home_top) actual_home_opp_top = example.example_data_dict['HOMEOpponentTimeOfPossession'] self.assertEquals(expected_home_opp_top, actual_home_opp_top) actual_away_opp_top = example.example_data_dict['AWAYOpponentTimeOfPossession'] self.assertEquals(expected_away_opp_top, actual_away_opp_top) actual_away_top = example.example_data_dict['AWAYTimeOfPossession'] self.assertEquals(expected_away_top, actual_away_top)
def test_verify_dvoa_stats_in_example(self): example = nfl_example_maker('CHI', 'BUF', 2006, 5) h_def = example.example_data_dict['HOMEdef_dvoa'] h_st = example.example_data_dict['HOMEst_dvoa'] h_off = example.example_data_dict['HOMEoff_dvoa'] a_def = example.example_data_dict['AWAYdef_dvoa'] a_st = example.example_data_dict['AWAYst_dvoa'] a_off = example.example_data_dict['AWAYoff_dvoa'] self.assertEqual(h_def, '-30.2') self.assertEqual(h_st, '10.8') self.assertEqual(h_off, '15.3') self.assertEqual(a_def, '6.2') self.assertEqual(a_st, '0.0') self.assertEqual(a_off, '-9.0')
def test_examples_for_one_week_in_one_season(self): # loop through each game a of particular week and # create example for each game and write to csv for game in self.schedule: if game["Week"] == str(self.week_num): home_team = game["HomeTeam"] away_team = game["AwayTeam"] if home_team != "BYE" and away_team != "BYE": example = nfl_example_maker(home_team, away_team, self.season, self.week_num) self.examples.append(example) key_order = self.examples[0].ordered_example_keys writer = nfl_example_io() writer.create_header(key_order, self.csv_output_file) for i in range(len(self.examples)): writer.write(self.examples[i].example_data_dict, key_order, self.csv_output_file, "a")
def test_example_has_right_data_size(self): """ example should be the size of both teams history + two for the score + any more for other items (e.g. DVOA, line, OU, weather... """ stats_for_example = nfl_team_example_stats() number_of_dvoa_stats = nfl_dvoa_stats.num_dvoa_stats number_of_stats_expected = \ len(stats_for_example.stats_to_average) * 2 number_of_stats_expected += number_of_dvoa_stats * 2 number_of_stats_expected += 2 # for expected score number_of_stats_expected += 2 # for home and away team number_of_stats_expected += 2 # for season and week example = nfl_example_maker('SF', 'SEA', 2014, 13) # print example.example number_of_stats_found = len(example.example_data_dict) self.assertEqual(number_of_stats_expected, number_of_stats_found)
def test_final_score_in_example(self): example = nfl_example_maker('OAK', 'DEN', 1985, 12) self.assertEqual('31', example.example_data_dict['HOMEscore']) self.assertEqual('28', example.example_data_dict['AWAYscore'])
def test_team_names_in_csv_line(self): example = nfl_example_maker('TAM', 'MIA', 1988, 9) self.assertEqual('TAM', example.example_data_dict['HOMEteam']) self.assertEqual('MIA', example.example_data_dict['AWAYteam'])
def test_examples_with_missing_stats(self): example = nfl_example_maker('NYG', 'ATL', 1998, 6) if 'None' in example.example_data_dict.values() or None in example.example_data_dict.values(): self.fail('found a none in here man!')