def test_fielding_independent_pitching(self):
     self.assertAlmostEqual(
         cps.fielding_independent_pitching(
             155,
             6,
             23,
             63,
         ), 3.406, 2)
     self.assertEqual(cps.fielding_independent_pitching(0, 6, 23, 63), 0.0)
Esempio n. 2
0
    def create_previous_game_pitched(self) -> float:
        """1試合前に投げたイニングを取得する

        Returns:
            float: 1試合前に投げたイニングを返す ex)7.2

        Notes:
            練習試合、または登板していない場合は0を返す
        """
        competition_choices = c.competition_choices_to_dict()
        # GamesとPitcherResultsは必ず存在するのでチェックしない
        if Games.objects.latest(
                'pk').competition_type == competition_choices['練習試合']:
            return 0

        previous_game = Games.objects.filter(
            competition_type__gt=competition_choices['練習試合']).latest('pk')
        pr = PitcherResults.objects.select_related(
            'player_id', 'game_id').filter(game_id=previous_game,
                                           player_id=self.player_id)

        if pr.exists():
            pr_latest = pr.latest('pk')
            return p.innings_conversion_for_display(
                pr_latest.innings_pitched, pr_latest.innings_pitched_fraction)
        else:
            return 0
Esempio n. 3
0
    def update_total_results(self, player_id: Players):
        """PitcherTotalResults更新用メソッド

        Args:
            player_id (Players): 対象選手

        Returns:
            なし

        Notes:
            対象選手のPitcherResultsを集計し、PitcherTotalResultsを更新する
        """
        self.player_id = player_id
        pitcher_results = self.tally_from_player_all_results()
        p = self.create_pitcher_total_results(pitcher_results)
        p.save()
 def test_innings_conversion_for_display(self):
     self.assertEqual(cps.innings_conversion_for_display(50, 0), 50.0)
     self.assertEqual(cps.innings_conversion_for_display(10, 1), 10.1)
     self.assertEqual(cps.innings_conversion_for_display(0, 10), 3.1)
     self.assertEqual(cps.innings_conversion_for_display(0, 9), 3.0)
     self.assertEqual(cps.innings_conversion_for_display(5, 6), 7.0)
     self.assertEqual(cps.innings_conversion_for_display(5, 7), 7.1)
 def test_bbhp_per_game(self):
     self.assertAlmostEqual(cps.bbhp_per_game(536.0, 56), 2.82, 2)
     self.assertEqual(cps.bbhp_per_game(1, 0), 0)
     self.assertEqual(cps.bbhp_per_game(0, 1), 0)
 def test_hit_percentage(self):
     self.assertAlmostEqual(cps.hit_percentage(731, 140), 0.1915, 3)
     self.assertEqual(cps.hit_percentage(0, 140), 0.0)
 def test_hit_per_game(self):
     self.assertAlmostEqual(cps.hit_per_game(536.0, 140), 7.052, 2)
     self.assertEqual(cps.hit_per_game(0, 140), 0.0)
 def test_pitch_per_inning(self):
     self.assertAlmostEqual(cps.pitch_per_inning(536.0, 2848), 15.940, 2)
 def test_left_on_base_percentage(self):
     self.assertAlmostEqual(cps.left_on_base_percentage(140, 67, 33, 82),
                            0.777, 2)
     self.assertEqual(cps.left_on_base_percentage(1, 0, 1, 1), 0.0)
     self.assertEqual(cps.left_on_base_percentage(0, 1, 0, 0), 1.0)
     self.assertAlmostEqual(cps.left_on_base_percentage(0, 0, 0, 0), 0.0)
 def test_home_run_percentage(self):
     self.assertAlmostEqual(cps.home_run_percentage(731, 33), 0.0451, 2)
     self.assertEqual(cps.home_run_percentage(1, 0), 0)
     self.assertEqual(cps.home_run_percentage(0, 1), 0)
 def test_earned_runs_average(self):
     self.assertAlmostEqual(cps.earned_runs_average(536.0, 79), 3.979, 3)
     self.assertEqual(cps.earned_runs_average(0, 0), 0)
     self.assertEqual(cps.earned_runs_average(0, 3), 0)
 def test_fip_subtracting_era(self):
     self.assertAlmostEqual(cps.fip_subtracting_era(2.37, 2.01), 0.36)
     self.assertAlmostEqual(cps.fip_subtracting_era(3.00, 3.34), -0.34)
 def test_strike_out_percentage(self):
     self.assertAlmostEqual(cps.strike_out_percentage(731, 229), 0.31, 2)
     self.assertEqual(cps.strike_out_percentage(1, 0), 0)
     self.assertEqual(cps.strike_out_percentage(0, 1), 0)
 def test_strike_out_per_game(self):
     self.assertAlmostEqual(cps.strike_out_per_game(536.0, 229), 11.54, 2)
     self.assertEqual(cps.strike_out_per_bbhp(1, 0), 0)
     self.assertEqual(cps.strike_out_per_bbhp(0, 1), 0)
 def test_strike_out_per_bbhp(self):
     self.assertAlmostEqual(cps.strike_out_per_bbhp(56, 229), 4.09, 2)
     self.assertEqual(cps.strike_out_per_bbhp(1, 0), 0)
     self.assertEqual(cps.strike_out_per_bbhp(0, 1), 0)
 def test_walks_plus_hits_per_inning_pitched(self):
     self.assertAlmostEqual(
         cps.walks_plus_hits_per_inning_pitched(536.0, 140, 56), 1.097, 3)
     self.assertEqual(cps.walks_plus_hits_per_inning_pitched(0, 0, 0), 0)
     self.assertEqual(cps.walks_plus_hits_per_inning_pitched(0, 1, 1), 0)
 def test_runs_average(self):
     self.assertAlmostEqual(cps.runs_average(536.0, 82), 4.131, 3)
     self.assertEqual(cps.runs_average(0, 0), 0)
     self.assertEqual(cps.runs_average(0, 3), 0)
Esempio n. 18
0
    def create_pitcher_total_results(
            self, pitcher_results: PitcherResults) -> PitcherTotalResults:
        """集計したPitcherResultsをもとに更新用のPitcherTotalResultsを作る

        Args:
            pitcher_results (PitcherResults): 集計したPitcherResults

        Returns:
            PitcherTotalResults: 計算した指標を代入したPitcherTotalResults

        Notes:
            セイバーメトリクスはここで計算する
        """
        pitcher_total_results = PitcherTotalResults.objects.select_related(
            'player').get(player_id=self.player_id)
        pitcher_total_results.games = pitcher_results['pk__count']
        pitcher_total_results.games_started = pitcher_results['games_started']
        pitcher_total_results.number_of_pitch = pitcher_results[
            'number_of_pitch__sum']
        pitcher_total_results.total_batters_faced = pitcher_results[
            'total_batters_faced__sum']
        pitcher_total_results.hit = pitcher_results['hit__sum']
        pitcher_total_results.strike_out = pitcher_results['strike_out__sum']
        pitcher_total_results.bb_hbp = pitcher_results['bb_hbp__sum']
        pitcher_total_results.run = pitcher_results['run__sum']
        pitcher_total_results.earned_run = pitcher_results['earned_run__sum']
        pitcher_total_results.wild_pitch = pitcher_results['wild_pitch__sum']
        pitcher_total_results.home_run = pitcher_results['home_run__sum']
        pitcher_total_results.previous_game_pitched = pitcher_results[
            'previous_game_pitched']

        sum_innings_pitched = p.innings_conversion_for_calculate(
            pitcher_results['innings_pitched__sum'],
            pitcher_results['innings_pitched_fraction__sum'])
        pitcher_total_results.innings_pitched = p.innings_conversion_for_display(
            pitcher_results['innings_pitched__sum'],
            pitcher_results['innings_pitched_fraction__sum'])
        pitcher_total_results.fip = p.fielding_independent_pitching(
            sum_innings_pitched, pitcher_results['home_run__sum'],
            pitcher_results['bb_hbp__sum'], pitcher_results['strike_out__sum'])
        pitcher_total_results.era = p.earned_runs_average(
            sum_innings_pitched, pitcher_results['earned_run__sum'])
        pitcher_total_results.ura = p.runs_average(sum_innings_pitched,
                                                   pitcher_results['run__sum'])
        pitcher_total_results.whip = p.walks_plus_hits_per_inning_pitched(
            sum_innings_pitched, pitcher_results['hit__sum'],
            pitcher_results['bb_hbp__sum'])
        pitcher_total_results.k_bbhp = p.strike_out_per_bbhp(
            pitcher_results['bb_hbp__sum'], pitcher_results['strike_out__sum'])
        pitcher_total_results.k_9 = p.strike_out_per_game(
            sum_innings_pitched, pitcher_results['strike_out__sum'])
        pitcher_total_results.k_percent = p.strike_out_percentage(
            pitcher_results['total_batters_faced__sum'],
            pitcher_results['strike_out__sum'])
        pitcher_total_results.bbhp_9 = p.bbhp_per_game(
            sum_innings_pitched, pitcher_results['bb_hbp__sum'])
        pitcher_total_results.p_bbhp_percent = p.bbhp_percentage(
            pitcher_results['total_batters_faced__sum'],
            pitcher_results['bb_hbp__sum'])
        pitcher_total_results.h_9 = p.hit_per_game(sum_innings_pitched,
                                                   pitcher_results['hit__sum'])
        pitcher_total_results.h_percent = p.hit_percentage(
            pitcher_results['total_batters_faced__sum'],
            pitcher_results['hit__sum'])
        pitcher_total_results.hr_9 = p.home_run_per_game(
            sum_innings_pitched, pitcher_results['home_run__sum'])
        pitcher_total_results.hr_percent = p.home_run_percentage(
            pitcher_results['total_batters_faced__sum'],
            pitcher_results['home_run__sum'])
        pitcher_total_results.lob_percent = p.left_on_base_percentage(
            pitcher_results['hit__sum'], pitcher_results['bb_hbp__sum'],
            pitcher_results['home_run__sum'], pitcher_results['run__sum'])
        pitcher_total_results.p_ip = p.pitch_per_inning(
            sum_innings_pitched, pitcher_results['number_of_pitch__sum'])

        return pitcher_total_results
 def test_bbhp_percentage(self):
     self.assertAlmostEqual(cps.bbhp_percentage(731, 56), 0.0766, 3)
     self.assertEqual(cps.bbhp_percentage(1, 0), 0)
     self.assertEqual(cps.bbhp_percentage(0, 1), 0)
 def test_home_run_per_game(self):
     self.assertAlmostEqual(cps.home_run_per_game(536.0, 33), 1.66, 2)
     self.assertEqual(cps.home_run_per_game(1, 0), 0)
     self.assertEqual(cps.home_run_per_game(0, 1), 0)
 def test_innings_conversion_for_calculate(self):
     self.assertEqual(cps.innings_conversion_for_calculate(10, 0), 30.0)
     self.assertEqual(cps.innings_conversion_for_calculate(178, 2), 536.0)
     self.assertEqual(cps.innings_conversion_for_calculate(10, 6), 36.0)
     self.assertEqual(cps.innings_conversion_for_calculate(10, 7), 37.0)