def test_clean(self): with self.subTest( "when predicted margin and win probability are None"): prediction = Prediction( match=self.match, ml_model=self.ml_model, predicted_winner=self.away_team, predicted_margin=None, predicted_win_probability=None, ) with self.assertRaisesMessage( ValidationError, "Prediction must have a predicted_margin or predicted_win_probability.", ): prediction.clean() with self.subTest( "when predicted margin and win probability are both numbers"): prediction = Prediction( match=self.match, ml_model=self.ml_model, predicted_winner=self.away_team, predicted_margin=23, predicted_win_probability=0.23, ) with self.assertRaisesMessage( ValidationError, "Prediction cannot have both a predicted_margin and " "predicted_win_probability.", ): prediction.clean()
def __build_match_prediction( ml_model_record: MLModel, prediction_data: pd.DataFrame, match: Match ) -> Prediction: home_team = match.teammatch_set.get(at_home=True).team away_team = match.teammatch_set.get(at_home=False).team match_prediction = prediction_data.loc[ ([home_team.name, away_team.name], match.year, match.round_number), "predicted_margin", ] predicted_home_margin = match_prediction.loc[home_team.name].iloc[0] predicted_away_margin = match_prediction.loc[away_team.name].iloc[0] # predicted_margin is always positive as its always associated with predicted_winner predicted_margin = match_prediction.abs().mean() if predicted_home_margin > predicted_away_margin: predicted_winner = home_team elif predicted_away_margin > predicted_home_margin: predicted_winner = away_team else: raise ValueError( "Predicted home and away margins are equal, which is basically impossible, " "so figure out what's going on:\n" f"home_team = {home_team.name}\n" f"away_team = {away_team.name}\n" "data =" f"{match_prediction}" ) prediction = Prediction( match=match, ml_model=ml_model_record, predicted_margin=predicted_margin, predicted_winner=predicted_winner, ) prediction.clean_fields() prediction.clean() return prediction
def test_clean(self): with self.subTest("when predicted margin rounds to 0"): prediction = Prediction( match=self.match, ml_model=self.ml_model, predicted_winner=self.away_team, predicted_margin=0.2, ) prediction.clean() self.assertEqual(1, prediction.predicted_margin) with self.subTest("when predicted margin is a float"): prediction = Prediction( match=self.match, ml_model=self.ml_model, predicted_winner=self.away_team, predicted_margin=65.7, ) prediction.clean() self.assertEqual(66, prediction.predicted_margin)