Sabermetrics.pm

filter: Statementstmt 81.6% 80 / 98 Branchbran 35.0% 7 / 20 Conditioncond 11.1% 1 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 92.8% 13 / 14 Podpod 37.5% 3 / 8 totaltotal 67.5% 104 / 154 CC 30 SCAR 26.5
CC
30
Cov
69%
setup_common_info
26.2
new
11.4
report_teams
6.0
CRAP 14.1
« Sabermetrics/League/CPBL.pm ↑ Summary Sabermetrics/League/CPB2.pm »
1 package Baseball::Sabermetrics;
2  
3 2 ▶use Baseball::Sabermetrics::Team;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use Baseball::Sabermetrics::Player;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use base qw/ Baseball::Sabermetrics::Team /;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 =head1 NAME
10  
11 Baseball::Sabermetrics - A Baseball Statistics Module
12  
13 =cut
14  
15 2 our $VERSION = '0.03';
16  
17 =head1 SYNOPSIS
18  
19 Baseball::Sabermetrics provides an easy interface for calculating baseball statistics, given a data importer. In this package, I've written CPBL.pm for (I<Chinese Professional Baseball League>, L<http://www.cpbl.com.tw>).
20  
21 use Baseball::Sabermetrics;
22 use Baseball::Sabermetrics::CPBL;
23  
24 my $league = Baseball::Sabermetrics->new(league => 'CPBL');
25  
26 # Actually these are predefined.
27 # Those data with 'p_' or '_allowed' here are for seperating pitchers
28 # and batters.
29  
30 $league->define(
31 rc => 'ab * obp',
32 babip => '(h_allowed - hr_allowed) / (p_pa - h_allowed - p_so - p_bb - hr_allowed',
33 # what started with '$' will be reserved.
34 # Players have team and league predefined, and team has league.
35 formula1 => 'hr / $_->team->hr';
36 formula2 => 'hr / $_->league->hr';
37 complex => sub {
38 print "You can write a sub directly\n";
39 $_->slg - $_->ba;
40 },
41 ...
42 );
43  
44 # Some formulas can be applied to players, teams, and league, depend on what
45 # columns are used in the formula. For example, ab and obp are defined for
46 # players, teams, and league, so that rc is available for all of them.
47  
48 # top 5 obp of teams
49 $_->print qw/ team name ba obp slg isop / for $league->top('teams', 5, 'obp');
50  
51 # top 10 obp of players
52 $_->print qw/ team name ba obp slg isop / for $league->top('players', 10, 'obp');
53  
54 # show a player's information
55 $league->players('Chien-Ming Wang')->print qw/ win lose ip so bb whip go_ao /;
56 $league->teams('Yankees')->players('Chien-Ming Wang')->print qw/ win lose ip so bb whip go_ao /;
57  
58 # show team statistics data (accumulated from players')
59 $league->{Yankees}->print qw/ win lose ip so bb whip go_ao /;
60  
61 # give a brief report for pitchers/batters of the team
62 $league->{Yankees}->report_pitchers qw/ name ip p_so p_bb whip go_ab /;
63 $league->{Yankees}->report_batters qw/ name ba obp slg isop /;
64  
65 $league->report_teams qw/ name win lose era obp /;
66  
67 # show all available formula
68 print join ' ', $league->formula_list;
69  
70 =head1 Data Structure
71  
72 Baseball::Sabermetrics is aimed for providing a base class of your interested teams (a league, for example). You'll need to provide a data retriever to pull data out. The following example shows how you have to fill data into this structure.
73  
74 $league = {
75 teams => {
76 Yankees => {
77 players => {
78 "Chien-Ming Wang" => {
79 bio => {
80 bats => 'right', # coule be left, switch
81 throws => 'right',
82 },
83 ip => 57.33333333333,
84 game => 9,
85 ...
86 fielding => {
87 p => {
88 tc => 43,
89 pop => 4,
90 ...
91 },
92 # b1 => { }, b2 => { }, b3 => { },
93 # first, second and thrid baseman should be
94 # b1, b2, and b3 respectively for convenient in
95 # fielding context. Because the initial of the
96 # name of subroutine can't be a number in perl.
97 },
98 };
99 ...
100 }
101 },
102 Athletics => {
103 ...
104 },
105 },
106 };
107  
108 =head1 TERMS
109  
110 Available terms of players (including teams and league, which are accumulated from players and could be treated as an abstract player) are:
111  
112 # pitching
113 p_game win lose tie gs sv bs hld cg sho ip p_pa np h_allowed
114 hr_allowed sh_allowed sf_allowed p_bb p_ibb hb p_so wp bk ra er
115  
116 # batting
117 pa ab rbi r h 1b 2b 3b hr tb dp sh sf ibb bb hbp so sb cs
118 tc po a e f_dp ppo tp pb c_cs c_sb
119  
120 # fielding
121 pos fgame tc po a e f_dp tp pb c_cs c_sb
122  
123 And there are additional terms for team:
124  
125 game win lose tie
126  
127  
128 =head1 FUNCTIONS
129  
130 =over 4
131  
132 =item new([I<%hash>])
133  
134 Create sabermetric data set of a group of teams. The following keys are supported:
135  
136 =over 8
137  
138 league:
139 a string like 'CPBL', which is a module and has to be defined in Baseball::Sabermetrics::League::CPBL.
140  
141 data:
142 If your league is not exists there, you can feed in a structure mentioned above.
143  
144 Accumulate:
145 If $hash{Accumulate} is false, players data will not be accumulated to their teams and the league (and therefore team-wise and league-wise statistics are not allowed). Default is to accumulate stats.
146  
147 =back
148  
149 =cut
150  
151 sub new
152 1 ▶{
Subroutine new: called CC=8 SCAR=24.3
Pod documented
153 1 my ($class, %config) = @_;
154 1 my $self;
155
156 1 ▶ if (exists $config{data}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $config{'data'}) { } 1 0 1
elsif (exists $config{'league'}) { } 0 0 0
157 1 $self = $config{data};
158 }
159 elsif (exists $config{league}) {
160 0 eval "require Baseball::Sabermetrics::League::$config{league}; \$self = Baseball::Sabermetrics::League::$config{league}->new(\%config);";
161 0 ▶ die unless $self;
Branch: 1 branch
BranchTrueFalseTotal
unless $self 0 0 0
162 }
163 else {
164 0 die "You have to provide statistic data";
165 }
166  
167 1 bless $self, $class;
168  
169 1 ▶ if (exists $config{Accumulate} && !$config{Accumulate}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $config{"Accumulate"} and not $config{"Accumulate"}) 0 1 1
Condition33%1/3
exists $config{"Accumulate"} and not $config{"Accumulate"}
!ll&&!rl&&r
100
MC/DC0%0/2
exists $config{"Accumulate"} and not $config{"Accumulate"}
exists $config{"Accumulate"} not $config{"Accumulate"}
Truth table33%1/3
A and not(B)
A= exists $config{"Accumulate"}B= not $config{"Accumulate"}
ABresult
0X0
100
111
170 0 $self->{_DontAccumulate} = 1;
171 }
172  
173 1 for my $team (values %{$self->{teams}}) {
174 3 $team = Baseball::Sabermetrics::Team->new($team);
175 3 $team->{league} = $self;
176 3 for my $p (values %{$team->{players}}) {
177 7 $p = Baseball::Sabermetrics::Player->new($p);
178 7 $p->{team} = $team;
179 7 $p->{league} = $self;
180 }
181 }
182  
183 1 setup_common_info($self);
184  
185 1 return $self;
186 }
187  
188 sub player_accumulate_term
189 7 ▶{
Subroutine player_accumulate_term: called CC=1 SCAR=0.0
Pod undocumented
190 # picher and batter's game is the same here, could be a problem later?
191 7 return qw/ p_game win lose tie gs sv bs hld cg sho ip p_pa np h_allowed
192 hr_allowed sh_allowed sf_allowed p_bb p_ibb hb p_so wp bk ra er
193 pa ab rbi r h 1b 2b 3b hr tb dp sh sf ibb bb hbp so sb cs
194 tc po a e f_dp ppo tp pb c_cs c_sb /;
195 }
196  
197 sub fielding_accumulate_term
198 9 ▶{
Subroutine fielding_accumulate_term: called CC=1 SCAR=0.0
Pod undocumented
199 9 return qw/ pos fgame tc po a e f_dp tp pb c_cs c_sb /;
200 }
201  
202 sub team_accumulate_term
203 3 ▶{
Subroutine team_accumulate_term: called CC=1 SCAR=0.0
Pod undocumented
204 3 return qw/ game win lose tie /;
205 }
206  
207 sub setup_common_info
208 1 ▶{
Subroutine setup_common_info: called CC=18 SCAR=32.7
Pod undocumented
209 1 my $league = shift;
210 1 $league->{fielding} = Baseball::Sabermetrics::Team->new();
211 1 for (qw/ p c b1 b2 b3 ss lf cf rf of /) {
212 10 $league->{fielding}->{$_} = Baseball::Sabermetrics::Player->new();
213 }
214  
215 1 for my $tname (keys %{$league->{teams}}) {
216 3 my $team = $league->{teams}->{$tname};
217 3 $team->{name} = $tname;
218 3 ▶ unless (exists $league->{_DontAccumulate}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $league->{"_DontAccumulate"}) 3 0 3
219 3 $team->{fielding} = Baseball::Sabermetrics::Team->new();
220 3 for (qw/ p c b1 b2 b3 ss lf cf rf of /) {
221 30 $team->{fielding}->{$_} = Baseball::Sabermetrics::Player->new();
222 }
223  
224 2 ▶ no warnings; # FIXME
Subroutine BEGIN: called CC=1 SCAR=0.0
225 3 for my $name (keys %{$team->{players}}) {
226 7 my $p = $team->{players}->{$name};
227 7 $p->{name} = $name;
228 7 for (player_accumulate_term()) {
229 378 $league->{$_} += $p->{$_};
230 378 $team->{$_} += $p->{$_};
231 }
232 }
233 3 for my $p (values %{$team->{players}}) {
234 7 for my $pos (keys %{$p->{fielding}}) {
235 0 for (fielding_accumulate_term()) {
236 0 $team->{fielding}->{$pos}->{$_} += $p->{fielding}->{$pos}->{$_};
237 0 $league->{fielding}->{$pos}->{$_} += $p->{fielding}->{$pos}->{$_};
238 }
239  
240 0 ▶ if ($pos eq 'lf' or $pos eq 'cf' or $pos eq 'rf') {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos eq "lf" or $pos eq "cf" or $pos eq "rf") 0 0 0
Condition0%0/6
$pos eq "lf" or $pos eq "cf" or $pos eq "rf"
l!l&&r!l&&!r
000
$pos eq "lf" or $pos eq "cf"
l!l&&r!l&&!r
000
MC/DC0%0/3
$pos eq "lf" or $pos eq "cf" or $pos eq "rf"
$pos eq "lf" $pos eq "cf" $pos eq "rf"
Truth table0%0/4
A or B or C
A= $pos eq "lf"B= $pos eq "cf"C= $pos eq "rf"
ABCresult
1XX1
01X1
0011
0000
241 0 for (fielding_accumulate_term()) {
242 0 $p->{fielding}->{of}->{$_} += $p->{fielding}->{$pos}->{$_};
243 }
244 }
245 }
246 }
247  
248 3 for my $pos (qw/ lf cf rf /) {
249 9 for (fielding_accumulate_term()) {
250 99 $league->{fielding}->{of}->{$_} += $team->{fielding}->{$pos}->{$_};
251 99 $team->{fielding}->{of}->{$_} += $team->{fielding}->{$pos}->{$_};
252 }
253 }
254  
255 3 for (team_accumulate_term()) {
256 12 $league->{$_} += $team->{$_};
257 }
258 }
259 3 $league->{players}->{$_} = $team->{players}->{$_} for (keys %{$team->{players}});
260 }
261 1 delete $league->{_DontAccumulate};
262 }
263  
264 =item players([$name])
265  
266 for ($league->players) { ... }
267 # or specify the name for the player
268 print $league->players('Someone')->obp;
269  
270 =cut
271  
272 sub players
273 2 ▶{
Subroutine players: called CC=3 SCAR=12.7
Pod documented
274 2 my ($self, $name) = @_;
275 2 ▶ if ($name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name) 2 0 2
276 2 ▶ die "Player not found: $name\n" unless exists $self->{players}->{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"players"}{$name} 0 2 2
277 2 return $self->{players}->{$name};
278 }
279 0 return values %{$self->{players}};
280 }
281  
282 =item teams([$name])
283  
284 for ($league->teams) { ... }
285 # or specify the name for the team
286 print $league->teams('Someone')->win;
287  
288 =cut
289  
290 sub teams
291 1 ▶{
Subroutine teams: called CC=3 SCAR=12.7
Pod documented
292 1 my ($self, $name) = @_;
293 1 ▶ if ($name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name) 1 0 1
294 1 ▶ die "Team not found: $name\n" unless exists $self->{teams}->{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"teams"}{$name} 0 1 1
295 1 return $self->{teams}->{$name};
296 }
297 0 return values %{$self->{teams}};
298 }
299  
300 sub report_teams
301 0 ▶{
Subroutine report_teams: not called CC=2 SCAR=17.9
Pod undocumented
302 0 my ($self, @cols) = @_;
303 0 print join("\t", @cols), "\n";
304 0 for ($self->teams) {
305 0 $_->print(@cols);
306 }
307 }
308  
309 =item pitchers
310  
311 Return all pitchers, i.e., NP (Number of Pitches) > 0.
312  
313 =item batters
314  
315 Return all batters, i.e., PA (Plate Appearances) > 0.
316  
317 =back
318  
319 =cut
320  
321  
322 =head1 AUTHOR
323  
324 Victor Hsieh, C<< <victor at cpan.org> >>
325  
326 =head1 BUGS
327  
328 Please report any bugs or feature requests to
329 C<bug-sabermetrics at rt.cpan.org>, or through the web interface at
330 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=BaseBall-Sabermetrics>.
331 I will be notified, and then you'll automatically be notified of progress on
332 your bug as I make changes.
333  
334 =head1 SUPPORT
335  
336 You can find documentation for this module with the perldoc command.
337  
338 perldoc Baseball::Sabermetrics
339  
340 You can also look for information at:
341  
342 =over 4
343  
344 =item * AnnoCPAN: Annotated CPAN documentation
345  
346 L<http://annocpan.org/dist/Baseball-Sabermetrics>
347  
348 =item * CPAN Ratings
349  
350 L<http://cpanratings.perl.org/d/Baseball-Sabermetrics>
351  
352 =item * RT: CPAN's request tracker
353  
354 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Baseball-Sabermetrics>
355  
356 =item * Search CPAN
357  
358 L<http://search.cpan.org/dist/Baseball-Sabermetrics>
359  
360 =back
361  
362 =head1 COPYRIGHT & LICENSE
363  
364 Copyright 2006 Victor Hsieh, all rights reserved.
365  
366 This program is free software; you can redistribute it and/or modify it
367 under the same terms as Perl itself.
368  
369 =cut
370  
371 2 1; # End of Baseball::Sabermetrics
« Sabermetrics/League/CPBL.pm ↑ Summary Sabermetrics/League/CPB2.pm »