def update_total_results(self, player_id: Players): """FielderTotalResultsを更新する Args: player_id (Players): 対象選手 Returns: なし Notes: 対象選手のFielderResultsを集計し、FielderTotalResultsを更新する """ self.player_id = player_id fielder_results = self.tally_from_player_all_results() f = self.create_fielder_total_results(fielder_results) f.save()
def create_fielder_total_results( self, fielder_results: FielderResults) -> FielderTotalResults: """集計したFielderResultsをもとに更新用のFielderTotalResultsを作る Args: fielder_results (FielderResults):集計したFielderResults Returns: FielderTotalResults: 計算した指標を代入したFielderTotalResults Notes: セイバーメトリクスはここで計算する """ fielder_total_results = FielderTotalResults.objects.select_related( 'player').get(player=self.player_id) fielder_total_results.at_bat = fielder_results['at_bat__sum'] fielder_total_results.run = fielder_results['run__sum'] fielder_total_results.hit = fielder_results['hit__sum'] fielder_total_results.two_base = fielder_results['two_base__sum'] fielder_total_results.three_base = fielder_results['three_base__sum'] fielder_total_results.home_run = fielder_results['home_run__sum'] fielder_total_results.run_batted_in = fielder_results['run_batted_in__sum'] fielder_total_results.strike_out = fielder_results['strike_out__sum'] fielder_total_results.bb_hbp = fielder_results['bb_hbp__sum'] fielder_total_results.sacrifice_bunt = fielder_results['sacrifice_bunt__sum'] fielder_total_results.stolen_base = fielder_results['stolen_base__sum'] fielder_total_results.grounded_into_double_play = fielder_results[ 'grounded_into_double_play__sum'] fielder_total_results.error = fielder_results['error__sum'] fielder_total_results.total_bases = f.total_bases( fielder_results['hit__sum'], fielder_results['two_base__sum'], fielder_results['three_base__sum'], fielder_results['home_run__sum']) fielder_total_results.slg = f.slugging_percentage( fielder_results['at_bat__sum'], fielder_total_results.total_bases) fielder_total_results.obp = f.on_base_percentage( fielder_results['at_bat__sum'], fielder_results['bb_hbp__sum'], fielder_results['hit__sum']) fielder_total_results.ops = f.on_base_plus_slugging( fielder_total_results.obp, fielder_total_results.slg) fielder_total_results.br = f.batting_runs( fielder_results['hit__sum'], fielder_results['two_base__sum'], fielder_results['three_base__sum'], fielder_results['home_run__sum'], fielder_results['bb_hbp__sum'], fielder_results['at_bat__sum'] ) fielder_total_results.woba = f.weighted_on_base_average( fielder_results['hit__sum'], fielder_results['two_base__sum'], fielder_results['three_base__sum'], fielder_results['home_run__sum'], fielder_results['bb_hbp__sum'], fielder_results['at_bat__sum'] ) fielder_total_results.gpa = f.gross_production_average( fielder_total_results.obp, fielder_total_results.slg) fielder_total_results.batting_average = f.batting_average( fielder_results['at_bat__sum'], fielder_results['hit__sum']) fielder_total_results.bbhp_percent = f.bb_hp_percentage( fielder_results['at_bat__sum'], fielder_results['bb_hbp__sum'], fielder_results['sacrifice_bunt__sum']) fielder_total_results.isod = f.isolated_discipline( fielder_total_results.obp, fielder_total_results.batting_average) fielder_total_results.isop = f.isolated_power( fielder_total_results.slg, fielder_total_results.batting_average) fielder_total_results.bbhp_k = f.bb_hbp_per_so( fielder_results['strike_out__sum'], fielder_results['bb_hbp__sum']) fielder_total_results.p_s = f.power_speed_number( fielder_results['home_run__sum'], fielder_results['stolen_base__sum']) return fielder_total_results
def test_power_speed_number(self): self.assertAlmostEqual(cfs.power_speed_number(18, 12), 14.4000, 2) self.assertEqual(cfs.power_speed_number(0, 0), 0) self.assertEqual(cfs.power_speed_number(1, 0), 0) self.assertEqual(cfs.power_speed_number(0, 1), 0)
def test_slugging_percentage(self): self.assertAlmostEqual(cfs.slugging_percentage(384, 194), 0.505, 3) self.assertEqual(cfs.slugging_percentage(0, 1), 0) self.assertEqual(cfs.slugging_percentage(1, 0), 0)
def test_isolated_power(self): self.assertAlmostEqual(cfs.isolated_power(0.505, 0.286), 0.219000, 3)
def test_bb_hbp_per_so(self): self.assertAlmostEqual(cfs.bb_hbp_per_so(110, 33), 0.3000, 3) self.assertAlmostEqual(cfs.bb_hbp_per_so(1, 15), 15.0000, 1) self.assertEqual(cfs.bb_hbp_per_so(1, 0), 0) self.assertEqual(cfs.bb_hbp_per_so(0, 1), 0)
def test_isolated_discipline(self): self.assertAlmostEqual(cfs.isolated_discipline(0.343, 0.286), 0.05700, 3)
def test_total_bases(self): self.assertEqual(cfs.total_bases(110, 20, 5, 18), 194) self.assertEqual(cfs.total_bases(0, 0, 0, 0), 0)
def test_batting_average(self): self.assertAlmostEqual(cfs.batting_average(384, 110), 0.286, 3) self.assertEqual(cfs.batting_average(0, 1), 0) self.assertEqual(cfs.batting_average(1, 0), 0)
def test_bb_hp_percentage(self): self.assertAlmostEqual(cfs.bb_hp_percentage(384, 33, 0), 0.079136, 3) self.assertEqual(cfs.bb_hp_percentage(0, 1, 0), 1) self.assertEqual(cfs.bb_hp_percentage(0, 0, 0), 0) self.assertEqual(cfs.bb_hp_percentage(1, 0, 0), 0)
def test_gross_production_average(self): self.assertAlmostEqual(cfs.gross_production_average(0.343, 0.505), 0.2806, 3) self.assertEqual(cfs.gross_production_average(0, 0), 0)
def test_weighted_on_base_average(self): self.assertAlmostEqual( cfs.weighted_on_base_average(110, 20, 5, 18, 33, 384), 0.3678, 3) self.assertEqual(cfs.weighted_on_base_average(0, 0, 0, 0, 0, 0), 0) self.assertEqual(cfs.weighted_on_base_average(0, 0, 0, 0, 0, 1), 0)
def test_batting_runs(self): self.assertAlmostEqual(cfs.batting_runs(110, 20, 5, 18, 33, 384), 16.9299, 3)
def test_on_base_plus_slugging(self): self.assertAlmostEqual(cfs.on_base_plus_slugging(0.343, 0.505), 0.848, 3)
def test_on_base_percentage(self): self.assertAlmostEqual(cfs.on_base_percentage(384, 36, 110), 0.3476, 3) self.assertEqual(cfs.on_base_percentage(0, 0, 0), 0) self.assertEqual(cfs.on_base_percentage(0, 1, 0), 1)