blib/lib/Stats/LikeR.pm

filter: Statementstmt 98.4% 3553 / 3609 Branchbran 88.5% 2269 / 2562 Conditioncond 80.1% 951 / 1186 MC/DCmcdc 65.7% 450 / 684 Subroutinesub 99.5% 202 / 203 Podpod 97.3% 36 / 37 totaltotal 90.1% 7461 / 8281 CC 1960 SCAR 38.5
CC
1960
Cov
92%
read_table
162.0
__ANON__
86.6
assign
86.1
CRAP 47.0
↑ Summary LikeR.xs »
1 #!/usr/bin/env perl
2 # ABSTRACT: Get basic statistical functions, like in R, but with Perl using XS for performance
3 497 require 5.010001;
4 497 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 package Stats::LikeR;
6 497 our $VERSION = '0.3423'; # quoted: a bare version ending in 0, such as 0.320, is the number 0.32, which the dist would be named
7 497 require XSLoader;
8 497 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 497 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 497 ▶use Scalar::Util qw(reftype looks_like_number);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 497 XSLoader::load('Stats::LikeR', $VERSION);
12 497 our @EXPORT_OK = qw(h add_data age_standardize agg anova aoh2h aoh2hoa aoh2hoh aov assign auc auroc avals bedroc bfill binom_test cfilter chisq_test chunk col col2col colnames concat cmh_test cor cor_test cov csort density df2h dftype bw_nrd0 bw_nrd bw_ucv bw_bcv bw_sj dnorm cohen_d cramers_v eta_squared drop_cols drop_duplicates dropna epi_2x2 ffill fillna filter fisher_test get_union glm group_by h2aoh hoa2aoh hoa2hoh hoh2hoa hist interpolate intersection is_equivalent kruskal_test ks_test kurtosis Lonly ljoin lm map_cell matrix max mean median melt merge min mode ncol nrow oneway_test p_adjust pivot_table pnorm pt qt pchisq qchisq pf qf pbinom qnorm power_t_test predict prop_test mcnemar_test friedman_test dunn_test prcomp ptukey qcut qtukey quantile rank roc Ronly rbind rbinom read_table rename_cols rnorm rownames runif sample scale sd select_cols seq shapiro_test skew smd sum summary survfit logrank_test coxph table_one t_test transpose TukeyHSD uniq vals value_counts var var_test vif hosmer_lemeshow view wilcox_test write_table zerotrunc hurdle svyglm ivreg lmer);
13 497 our @EXPORT = @EXPORT_OK;
14  
15 # File operations: failure reporting
16 #
17 # Before 0.316 this file ran under `use autodie ':default';', which replaced
18 # open() and close() with versions that threw an autodie::exception the moment
19 # either failed. 0.316 drops the dependency; _open_read() and _close() take its
20 # place, raising the same failure at the same points with the same text, so a
21 # caller's eval sees the message it has always seen. The one visible difference
22 # is that $@ is now a plain string rather than an autodie::exception object,
23 # which nothing in the module, the tests or the documentation ever inspected.
24 #
25 # The wording is autodie::exception 2.37's: _format_open() (through
26 # _FORMAT_OPEN and _format_open_with_mode()) for the open, _format_close() for
27 # the close, each followed by add_file_and_line(). That last method is where the
28 # trailing newline comes from, and it is also what stops perl appending a second,
29 # differently punctuated " at ... line ..." of its own.
30  
31 # autodie reported the caller's file and line, not its own, so build the message
32 # one frame up.
33 0 ▶sub _io_die {
Subroutine _io_die: not called CC=1 SCAR=6.9
34 0 my ($msg) = @_;
35 0 my (undef, $file, $line) = caller(1);
36 0 die sprintf("%s at %s line %d\n", $msg, $file, $line);
37 }
38  
39 13934 ▶sub _open_read {
Subroutine _open_read: called CC=2 SCAR=7.0
40 13934 my ($file) = @_;
41 13934 my $fh;
42 13934 ▶ open $fh, '<', $file
Branch: 1 branch
BranchTrueFalseTotal
open $fh, "<", $file or _io_die("Can't open '${file}' for reading: '$!'") 13934 0 13934
43 or _io_die("Can't open '$file' for reading: '$!'");
44 13934 return $fh;
45 }
46  
47 14232 ▶sub _close {
Subroutine _close: called CC=2 SCAR=7.1
48 14232 my ($fh) = @_;
49 14232 ▶ close $fh or _io_die("Can't close($fh) filehandle: '$!'");
Branch: 1 branch
BranchTrueFalseTotal
close $fh or _io_die("Can't close($fh) filehandle: '$!'") 14232 0 14232
50 14232 return;
51 }
52  
53 # Help
54 #
55 # h() is the way in, and it works for every function in the distribution, XS
56 # and pure Perl alike, because it looks the name up rather than watching an
57 # argument list:
58 #
59 # h('agg'); h(*agg); h(\&agg); h();
60 #
61 # It prints that function's own section of the documentation -- the same text
62 # as the matching heading in README.md -- to STDOUT, and returns.
63 #
64 # h() is the only way in. No function reads its own arguments for a help flag:
65 # a bare 'h' or '?' cannot be told apart from a column, file or option value
66 # that really is that string, and one help route that works everywhere beats
67 # two that behave differently depending on whether the callee is XS or Perl.
68 #
69 # The help text is not duplicated in the source. It is rendered from this
70 # file's own POD at run time (that POD is generated from README.md by
71 # md2pod.pl), so the help can never drift out of sync with the shipped
72 # documentation. Functions without a documentation section of their own fall
73 # back to listing every topic that does have one.
74  
75 # Functions that share another function's section, either because they are the
76 # very same subroutine under a second name (rbind/concat) or because they are
77 # the internal engine behind a documented front end.
78 497 my %HELP_ALIAS = (
79 rbind => 'concat',
80 bw_nrd0 => 'density',
81 bw_nrd => 'density',
82 bw_ucv => 'density',
83 bw_bcv => 'density',
84 bw_sj => 'density',
85 map_cell => 'assign',
86 _hoa_assign => 'assign',
87 col => 'filter',
88 _rename_inplace => 'rename_cols',
89 aoh2h => 'df2h',
90 _df2h => 'df2h',
91 _cols_select => 'select_cols',
92 _cols_drop => 'drop_cols',
93 _cols_rename => 'rename_cols',
94 _drop_dups_core => 'drop_duplicates',
95 _aoh_key_union => 'drop_duplicates',
96 _qcut_core => 'qcut',
97 _interp_column_xs => 'interpolate',
98 _parse_csv_file => 'read_table',
99 _impute_prop => 'fillna',
100 _fill_seq => 'fillna',
101 _render_grid => 'view',
102 _view_size => 'view',
103 _view_size_fmt => 'view',
104 _df_shape => 'agg',
105 _xtab => 'cramers_v',
106 # The eight R distribution functions share one section, since what is
107 # worth saying about tails, log_p and accuracy is the same for all of
108 # them; pnorm and dnorm keep their own, older sections.
109 qnorm => 'Distribution functions',
110 pt => 'Distribution functions',
111 qt => 'Distribution functions',
112 pchisq => 'Distribution functions',
113 qchisq => 'Distribution functions',
114 pf => 'Distribution functions',
115 qf => 'Distribution functions',
116 pbinom => 'Distribution functions',
117 );
118  
119 # _help_show($name) -- print $name's documentation to STDOUT. Returns the
120 # name it showed.
121 148 ▶sub _help_show {
Subroutine _help_show: called CC=2 SCAR=7.0
122 148 my ($name) = @_;
123 148 ▶ $name = defined($name) ? $name : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $name ? : 148 0 148
124 148 $name =~ s/\A.*:://; # accept Stats::LikeR::agg
125 148 my $old = select STDOUT; local $| = 1; select $old;
126 148 print STDOUT _help_text($name);
127 148 return $name;
128 }
129  
130 # h() -- ask for documentation by name. It never dies over the name it is
131 # given (an undocumented one lists the documented ones instead), and it never
132 # stands in the way of a call: no function here reads its arguments for a help
133 # flag, so a column, file or option value really named 'h' is just data.
134 #
135 # h('bedroc'); # by name
136 # h(*bedroc); # by name, unquoted
137 # h(\&bedroc); # by reference
138 # h(); # the general help, and the list of topics
139 #
140 # h(bedroc), with no quotes and no sigil, cannot be made to work: every
141 # function here is exported, so Perl parses the bareword as a call to bedroc()
142 # before h() is ever reached. Use one of the four forms above.
143 #
144 # Returns the name whose documentation was shown.
145 150 ▶sub h {
Subroutine h: called CC=10 SCAR=23.0
Pod documented
146 150 my ($what) = @_;
147 150 my $name;
148  
149 150 ▶ if (!@_ || !defined $what) { # h() -- the general help
Branch: 4 branches
BranchTrueFalseTotal
if (not @_ && defined $what) { } 2 148 150
elsif (ref \$what eq 'GLOB') { } 4 144 148
elsif (ref $what eq 'CODE') { } 5 139 144
elsif (ref $what) { } 1 138 139
Condition100%3/3
@_ && defined $what
!ll&&!rl&&r
11148
MC/DC100%2/2
@_ && defined $what
@_ defined $what
Truth table100%3/3
A && B
A= @_B= defined $what
ABresult
0X0
100
111
150 2 $name = '';
151 }
152 elsif (ref \$what eq 'GLOB') { # h(*bedroc)
153 4 ($name = "$what") =~ s/\A\*//;
154 }
155 elsif (ref $what eq 'CODE') { # h(\&bedroc)
156 497 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
157 5 for my $cand (@EXPORT_OK) {
158 400 my $slot = *{"Stats::LikeR::$cand"}{CODE};
159 400 ▶ next unless $slot && $slot == $what;
Branch: 1 branch
BranchTrueFalseTotal
unless $slot and $slot == $what 396 4 400
Condition66%2/3
$slot and $slot == $what
!ll&&!rl&&r
03964
MC/DC50%1/2
$slot and $slot == $what
$slot $slot == $what
Truth table66%2/3
A and B
A= $slotB= $slot == $what
ABresult
0X0
100
111
160 4 $name = $cand;
161 4 last;
162 }
163 5 ▶ die "h: that code reference is not a Stats::LikeR function\n"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 1 4 5
164 unless defined $name;
165 }
166 elsif (ref $what) {
167 1 die 'h: expected a function name, a glob or a code reference, not a '
168 . ref($what) . " reference\n";
169 }
170 else { # h('bedroc'), h($name)
171 138 $name = $what;
172 138 $name =~ s/\A&//; # h('&bedroc')
173 }
174  
175 148 $name =~ s/\A.*:://; # h('Stats::LikeR::bedroc')
176 148 $name =~ s/\A\s+//; $name =~ s/\s+\z//;
177 148 return _help_show($name);
178 }
179  
180 # terminal width to wrap to
181 148 ▶sub _help_width {
Subroutine _help_width: called CC=5 SCAR=16.6
182 148 my $w = $ENV{COLUMNS};
183 148 ▶ $w = 80 unless defined($w) && $w =~ /\A[0-9]+\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $w and $w =~ /\A[0-9]+\z/ 146 2 148
Condition66%2/3
defined $w and $w =~ /\A[0-9]+\z/
!ll&&!rl&&r
14602
MC/DC50%1/2
defined $w and $w =~ /\A[0-9]+\z/
defined $w $w =~ /\A[0-9]+\z/
Truth table66%2/3
A and B
A= defined $wB= $w =~ /\A[0-9]+\z/
ABresult
0X0
100
111
184 148 ▶ $w = 40 if $w < 40;
Branch: 1 branch
BranchTrueFalseTotal
if $w < 40 0 148 148
185 148 ▶ $w = 100 if $w > 100;
Branch: 1 branch
BranchTrueFalseTotal
if $w > 100 0 148 148
186 148 return $w;
187 }
188  
189 # display length of a UTF-8 byte string: count everything that is not a
190 # continuation byte. Keeps wrapping honest without pulling in Encode.
191 208708 ▶sub _help_len {
Subroutine _help_len: called CC=2 SCAR=6.9
192 208708 my $n = 0;
193 208708 ▶ $n++ while $_[0] =~ /[^\x80-\xbf]/g;
Branch: 1 branch
BranchTrueFalseTotal
$_[0] =~ /[^\x80-\xbf]/g and do { ++$n } 4099911 208708 4308619
194 208708 return $n;
195 }
196  
197 148 ▶sub _help_text {
Subroutine _help_text: called CC=10 SCAR=23.0
198 148 my ($name) = @_;
199 148 my $width = _help_width();
200 148 ▶ my $topic = exists $HELP_ALIAS{$name} ? $HELP_ALIAS{$name} : $name;
Branch: 1 branch
BranchTrueFalseTotal
exists $HELP_ALIAS{$name} ? : 12 136 148
201 148 ▶ my @sect = length($name) ? _pod_section($topic) : ();
Branch: 1 branch
BranchTrueFalseTotal
length $name ? : 146 2 148
202 148 ▶ my $body = @sect ? _pod_render(\@sect, $width)
Branch: 2 branches
BranchTrueFalseTotal
@sect ? : 145 3 148
length $name ? : 1 2 3
203 : length($name) ? _help_fallback($name, $width)
204 : _help_general($width);
205  
206 148 ▶ my $title = length($name) ? "Stats::LikeR::$name" : 'Stats::LikeR';
Branch: 1 branch
BranchTrueFalseTotal
length $name ? : 146 2 148
207 148 ▶ $title .= " (documented under \`$topic')" if $topic ne $name && @sect;
Branch: 1 branch
BranchTrueFalseTotal
if $topic ne $name and @sect 12 136 148
Condition66%2/3
$topic ne $name and @sect
!ll&&!rl&&r
136012
MC/DC50%1/2
$topic ne $name and @sect
$topic ne $name @sect
Truth table66%2/3
A and B
A= $topic ne $nameB= @sect
ABresult
0X0
100
111
208 148 my $rule = '=' x $width;
209  
210 148 ▶ return join('',
Branch: 2 branches
BranchTrueFalseTotal
$body =~ /\n\n\z/ ? : 3 145 148
length $name ? : 146 2 148
211 "\n", $rule, "\n", $title, "\n", $rule, "\n\n",
212 $body,
213 ($body =~ /\n\n\z/ ? '' : "\n"),
214 '-' x $width, "\n",
215 _pod_wrap(length($name)
216 ? "Call h('$name') for this page at any time; h(*$name) and "
217 . "h(\\&$name) are the same call, and h() lists every documented "
218 . 'function.'
219 : "Call h('name') for any one function's documentation -- "
220 . "h('agg'), h(*agg) and h(\\&agg) are the same call.",
221 $width, 1),
222 _pod_wrap("The full manual is `perldoc Stats::LikeR'.", $width, 1),
223 $rule, "\n\n",
224 );
225 }
226  
227 # Recognise a heading. Returns (level, text), or the empty list when the line
228 # is not one. Text carrying braces or a fat comma is a code comment that the
229 # markdown-to-POD generator mistook for a heading (README.md has a few inside
230 # indented examples); treating those as sections would cut a function's
231 # documentation short, so they are ignored here.
232 1985277 ▶sub _pod_heading {
Subroutine _pod_heading: called CC=4 SCAR=14.0
233 1985277 my ($line) = @_;
234 1985277 ▶ return () unless defined $line && $line =~ /^=head([1-6])[ \t]+(\S.*?)\s*\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line and $line =~ /^=head([1-6])[ \t]+(\S.*?)\s*\z/ 1949885 35392 1985277
Condition66%2/3
defined $line and $line =~ /^=head([1-6])[ \t]+(\S.*?)\s*\z/
!ll&&!rl&&r
0194988535392
MC/DC50%1/2
defined $line and $line =~ /^=head([1-6])[ \t]+(\S.*?)\s*\z/
defined $line $line =~ /^=head([1-6])[ \t]+(\S.*?)\s*\z/
Truth table66%2/3
A and B
A= defined $lineB= $line =~ /^=head([1-6])[ \t]+(\S.*?)\s*\z/
ABresult
0X0
100
111
235 35392 my ($level, $text) = ($1, $2);
236 35392 ▶ return () if $text =~ /[{};]|=>/;
Branch: 1 branch
BranchTrueFalseTotal
if $text =~ /[{};]|=>/ 0 35392 35392
237 35392 return ($level, $text);
238 }
239  
240 # every =head2 in the Functions section, for the "nothing documented" path
241 3 ▶sub _pod_topics {
Subroutine _pod_topics: called CC=10 SCAR=23.0
242 3 my @t;
243 3 ▶ my $fh = _pod_open() or return @t;
Branch: 1 branch
BranchTrueFalseTotal
my $fh = _pod_open() or return @t 3 0 3
244 3 my $in = 0;
245 3 ▶ while (my $line = <$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 56922 3 56925
246 56922 my ($level, $text) = _pod_heading($line);
247 56922 ▶ next unless defined $level;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $level 55596 1326 56922
248 1326 ▶ if ($level == 1) { $in = ($text =~ /Functions/i) ? 1 : 0; next }
Branch: 2 branches
BranchTrueFalseTotal
if ($level == 1) 33 1293 1326
$text =~ /Functions/i ? : 3 30 33
249 1293 ▶ next unless $in && $level == 2;
Branch: 1 branch
BranchTrueFalseTotal
unless $in and $level == 2 927 366 1293
Condition100%3/3
$in and $level == 2
!ll&&!rl&&r
6921366
MC/DC100%2/2
$in and $level == 2
$in $level == 2
Truth table100%3/3
A and B
A= $inB= $level == 2
ABresult
0X0
100
111
250 366 my $n = _pod_plain($text);
251 366 $n =~ s/\s*\(.*\z//s;
252 366 $n =~ s/\A\s+//; $n =~ s/\s+\z//;
253 # only names a caller can actually use; this also drops the one
254 # heading the POD generator mangled (_rename_inplace, a private
255 # helper, comes out as I<rename>inplace)
256 366 ▶ push @t, $n if length($n) && grep { $_ eq $n } @EXPORT_OK;
Branch: 1 branch
BranchTrueFalseTotal
if length $n and grep {$_ eq $n;} @Stats::LikeR::EXPORT_OK 360 6 366
Condition66%2/3
length $n and grep {$_ eq $n;} @Stats::LikeR::EXPORT_OK
!ll&&!rl&&r
06360
MC/DC50%1/2
length $n and grep {$_ eq $n;} @Stats::LikeR::EXPORT_OK
length $n grep {$_ eq $n;} @Stats::LikeR::EXPORT_OK
Truth table66%2/3
A and B
A= length $nB= grep {$_ eq $n;} @Stats::LikeR::EXPORT_OK
ABresult
0X0
100
111
257 }
258 3 _close($fh);
259 3 return @t;
260 }
261  
262 # the documented function names, in aligned columns
263 3 ▶sub _help_topic_block {
Subroutine _help_topic_block: called CC=6 SCAR=17.9
264 3 my ($width) = @_;
265 3 my @topics = _pod_topics();
266 3 ▶ return '' unless @topics;
Branch: 1 branch
BranchTrueFalseTotal
unless @topics 0 3 3
267  
268 3 my $col = 0;
269 3 ▶ $col = _help_len($_) > $col ? _help_len($_) : $col for @topics;
Branch: 1 branch
BranchTrueFalseTotal
_help_len($_) > $col ? : 6 354 360
270 3 $col += 2;
271 3 ▶ my $per = int(($width - 2) / $col) || 1;
Condition50%1/2
int(($width - 2) / $col) || 1
l!l
30
MC/DC0%0/1
int(($width - 2) / $col) || 1
int(($width - 2) / $col)
Truth table50%1/2
A
A= int(($width - 2) / $col)
Aresult
11
00
272 3 my $out = '';
273 3 my $i = 0;
274 3 ▶ while ($i < @topics) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @topics) 90 3 93
275 90 my @row = grep { defined } @topics[$i .. $i + $per - 1];
276 90 my $line = ' ' . join('', map { $_ . ' ' x ($col - _help_len($_)) } @row);
277 90 $line =~ s/\s+\z//;
278 90 $out .= $line . "\n";
279 90 $i += $per;
280 }
281 3 return $out;
282 }
283  
284 1 ▶sub _help_fallback {
Subroutine _help_fallback: called CC=4 SCAR=14.9
285 1 my ($name, $width) = @_;
286 1 my $list = _help_topic_block($width);
287 1 ▶ my $out = _pod_wrap("There is no documentation section for "
Branch: 2 branches
BranchTrueFalseTotal
length $name ? : 1 0 1
length $list ? : 1 0 1
288 . (length($name) ? "`$name'" : 'that name')
289 . '.'
290 . (length($list) ? ' These functions have one, and h()'
291 . ' will show any of them:' : ''),
292 $width, 1);
293 1 ▶ return length($list) ? $out . "\n" . $list . "\n" : $out;
Branch: 1 branch
BranchTrueFalseTotal
length $list ? : 1 0 1
294 }
295  
296 # h() with nothing to look up: how to ask, then every topic.
297 #
298 # README.md's own "Getting help" section is used when the POD has been
299 # regenerated from it (md2pod.pl); until then the short version below stands in,
300 # so h() is never empty.
301 2 ▶sub _help_general {
Subroutine _help_general: called CC=3 SCAR=11.3
302 2 my ($width) = @_;
303 2 my @sect = _pod_section('Getting help', 1);
304 2 my $out;
305 2 ▶ if (@sect) {
Branch: 1 branch
BranchTrueFalseTotal
if (@sect) { } 2 0 2
306 2 $out = _pod_render(\@sect, $width);
307 }
308 else {
309 0 $out = _pod_wrap('Ask for a function by name, and its section of the '
310 . 'documentation is printed here:', $width, 1)
311 . "\n"
312 . " h('quantile'); # by name\n"
313 . " h(*quantile); # by name, unquoted\n"
314 . " h(\\&quantile); # by reference\n"
315 . "\n"
316 . _pod_wrap('h() is the only way to ask: no function reads its own '
317 . 'arguments for a help flag, so a column, file or option '
318 . "value really named 'h' is just data.", $width, 1);
319 }
320 2 my $list = _help_topic_block($width);
321 2 ▶ if (length $list) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $list) 2 0 2
322 2 $out .= "\n" . _pod_wrap('Documented functions, each of which h() will '
323 . 'show in full:', $width, 1) . "\n" . $list . "\n";
324 }
325 2 return $out;
326 }
327  
328 # POD extraction
329  
330 497 our $POD_FILE; # set only by t/help.t, to read a fixture
331  
332 156 ▶sub _pod_open {
Subroutine _pod_open: called CC=6 SCAR=20.7
333 156 ▶ my $file = defined($POD_FILE) ? $POD_FILE : __FILE__;
Branch: 1 branch
BranchTrueFalseTotal
defined $Stats::LikeR::POD_FILE ? : 1 155 156
334 156 ▶ $file = $INC{'Stats/LikeR.pm'} unless defined($file) && -r $file;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file and -r $file 0 156 156
Condition33%1/3
defined $file and -r $file
!ll&&!rl&&r
00156
MC/DC0%0/2
defined $file and -r $file
defined $file -r $file
Truth table33%1/3
A and B
A= defined $fileB= -r $file
ABresult
0X0
100
111
335 156 ▶ return undef unless defined($file) && -r $file;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file and -r $file 0 156 156
Condition33%1/3
defined $file and -r $file
!ll&&!rl&&r
00156
MC/DC0%0/2
defined $file and -r $file
defined $file -r $file
Truth table33%1/3
A and B
A= defined $fileB= -r $file
ABresult
0X0
100
111
336 156 return _open_read($file);
337 }
338  
339 # Reduce a heading or a function name to a comparison key. Dropping every
340 # non-alphanumeric makes the match immune to a signature in the heading
341 # (`hoa2hoh( \%hoa, $key )'), to C<> wrapping (`C<aoh2hoh>') and to POD
342 # generated from markdown that read an underscore as italics
343 # (`I<rename>inplace' for `_rename_inplace').
344 9156 ▶sub _pod_key {
Subroutine _pod_key: called CC=2 SCAR=7.0
345 9156 my ($s) = @_;
346 9156 ▶ return '' unless defined $s;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s 0 9156 9156
347 9156 $s =~ s/\s*\(.*\z//s;
348 9156 $s = _pod_plain($s);
349 9156 $s =~ s/[^A-Za-z0-9]+//g;
350 9156 return lc $s;
351 }
352  
353 # the raw POD lines of the section headed $name, its subsections included.
354 # $want is the heading level to match, 2 (a function) unless asked otherwise.
355 153 ▶sub _pod_section {
Subroutine _pod_section: called CC=13 SCAR=25.7
356 153 my ($name, $want) = @_;
357 153 ▶ $want = 2 unless defined $want;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $want 151 2 153
358 153 my $key = _pod_key($name);
359 153 ▶ return () unless length $key;
Branch: 1 branch
BranchTrueFalseTotal
unless length $key 0 153 153
360 153 ▶ my $fh = _pod_open() or return ();
Branch: 1 branch
BranchTrueFalseTotal
my $fh = _pod_open() or return () 153 0 153
361 153 my (@out, $in);
362 153 ▶ while (my $line = <$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 1928355 2 1928357
363 1928355 my ($level, $title) = _pod_heading($line);
364 1928355 ▶ if (defined $level && $level <= $want) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $level and $level <= $want) 9917 1918438 1928355
Condition100%3/3
defined $level and $level <= $want
!ll&&!rl&&r
1894289241499917
MC/DC100%2/2
defined $level and $level <= $want
defined $level $level <= $want
Truth table100%3/3
A and B
A= defined $levelB= $level <= $want
ABresult
0X0
100
111
365 9917 ▶ if ($level == $want && _pod_key($title) eq $key) {
Branch: 1 branch
BranchTrueFalseTotal
if ($level == $want and _pod_key($title) eq $key) 151 9766 9917
Condition100%3/3
$level == $want and _pod_key($title) eq $key
!ll&&!rl&&r
9148852151
MC/DC100%2/2
$level == $want and _pod_key($title) eq $key
$level == $want _pod_key($title) eq $key
Truth table100%3/3
A and B
A= $level == $wantB= _pod_key($title) eq $key
ABresult
0X0
100
111
366 151 $in = 1;
367 151 push @out, $line;
368 151 next;
369 }
370 9766 ▶ last if $in;
Branch: 1 branch
BranchTrueFalseTotal
if $in 150 9616 9766
371 9616 next;
372 }
373 1918438 ▶ last if $in && $line =~ /^=cut\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
if $in and $line =~ /^=cut\s*$/ 1 1918437 1918438
Condition100%3/3
$in and $line =~ /^=cut\s*$/
!ll&&!rl&&r
1900921175161
MC/DC100%2/2
$in and $line =~ /^=cut\s*$/
$in $line =~ /^=cut\s*$/
Truth table100%3/3
A and B
A= $inB= $line =~ /^=cut\s*$/
ABresult
0X0
100
111
374 1918437 ▶ push @out, $line if $in;
Branch: 1 branch
BranchTrueFalseTotal
if $in 17516 1900921 1918437
375 }
376 153 _close($fh);
377 153 return @out;
378 }
379  
380 # POD -> plain text
381  
382 497 my %POD_ENTITY = (
383 lt => '<', gt => '>', 'verbar' => '|', sol => '/', amp => '&',
384 quot => '"', apos => "'", lchevron => '<<', rchevron => '>>',
385 nbsp => ' ', ndash => '-', mdash => '--', 'eacute' => 'e',
386 );
387  
388 8369 ▶sub _pod_seq {
Subroutine _pod_seq: called CC=10 SCAR=28.7
389 8369 my ($code, $text) = @_;
390 8369 ▶ if ($code eq 'E') {
Branch: 1 branch
BranchTrueFalseTotal
if ($code eq "E") 743 7626 8369
391 743 ▶ return $POD_ENTITY{$text} if exists $POD_ENTITY{$text};
Branch: 1 branch
BranchTrueFalseTotal
if exists $POD_ENTITY{$text} 743 0 743
392 0 ▶ return chr(hex $1) if $text =~ /\A0?[xX]([0-9a-fA-F]+)\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $text =~ /\A0?[xX]([0-9a-fA-F]+)\z/ 0 0 0
393 0 ▶ return chr($text) if $text =~ /\A[0-9]+\z/ && $text < 128;
Branch: 1 branch
BranchTrueFalseTotal
if $text =~ /\A[0-9]+\z/ and $text < 128 0 0 0
Condition0%0/3
$text =~ /\A[0-9]+\z/ and $text < 128
!ll&&!rl&&r
000
MC/DC0%0/2
$text =~ /\A[0-9]+\z/ and $text < 128
$text =~ /\A[0-9]+\z/ $text < 128
Truth table0%0/3
A and B
A= $text =~ /\A[0-9]+\z/B= $text < 128
ABresult
0X0
100
111
394 0 return $text;
395 }
396 7626 ▶ if ($code eq 'L') { # L<text|target>, L<target>, L<#anchor>
Branch: 1 branch
BranchTrueFalseTotal
if ($code eq "L") 116 7510 7626
397 116 ▶ $text =~ s/\|.*\z//s if $text =~ /\|/;
Branch: 1 branch
BranchTrueFalseTotal
if $text =~ /\|/ 84 32 116
398 116 $text =~ s{\A/?#}{};
399 116 $text =~ s{\A/}{};
400 116 return $text;
401 }
402 7510 ▶ return '' if $code eq 'X' || $code eq 'Z';
Branch: 1 branch
BranchTrueFalseTotal
if $code eq "X" or $code eq "Z" 0 7510 7510
Condition33%1/3
$code eq "X" or $code eq "Z"
l!l&&r!l&&!r
007510
MC/DC0%0/2
$code eq "X" or $code eq "Z"
$code eq "X" $code eq "Z"
Truth table33%1/3
A or B
A= $code eq "X"B= $code eq "Z"
ABresult
1X1
011
000
403 7510 return $text; # B, C, I, F, S and anything else
404 }
405  
406 # strip POD formatting codes, innermost first, doubled delimiters included
407 14884 ▶sub _pod_plain {
Subroutine _pod_plain: called CC=4 SCAR=13.9
408 14884 my ($s) = @_;
409 14884 ▶ return '' unless defined $s;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s 0 14884 14884
410 14884 for (1 .. 24) {
411 16881 my $before = $s;
412 16881 $s =~ s/([A-Z])<<+[ \t\n]+(.*?)[ \t\n]+>>+/_pod_seq($1, $2)/ges;
413 16881 $s =~ s/([A-Z])<([^<>]*)>/_pod_seq($1, $2)/ge;
414 16881 ▶ last if $s eq $before;
Branch: 1 branch
BranchTrueFalseTotal
if $s eq $before 14884 1997 16881
415 }
416 14884 return $s;
417 }
418  
419 2307 ▶sub _pod_wrap {
Subroutine _pod_wrap: called CC=6 SCAR=17.9
420 2307 my ($text, $width, $indent, $lead) = @_;
421 2307 ▶ $lead = '' unless defined $lead;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $lead 1697 610 2307
422 2307 my $pad = ' ' x $indent;
423 2307 my $first = $pad . $lead;
424 2307 my $cont = $pad . (' ' x _help_len($lead));
425 2307 my $limit = $width;
426 2307 my $floor = _help_len($first) + 20;
427 2307 ▶ $limit = $floor if $limit < $floor;
Branch: 1 branch
BranchTrueFalseTotal
if $limit < $floor 0 2307 2307
428  
429 2307 my @words = grep { length } split /\s+/, $text;
430 2307 ▶ return "" unless @words;
Branch: 1 branch
BranchTrueFalseTotal
unless @words 0 2307 2307
431 2307 my $out = '';
432 2307 my $line = $first . shift @words;
433 2307 for my $w (@words) {
434 81125 ▶ if (_help_len($line) + 1 + _help_len($w) > $limit) {
Branch: 1 branch
BranchTrueFalseTotal
if (_help_len($line) + 1 + _help_len($w) > $limit) { } 4895 76230 81125
435 4895 $out .= $line . "\n";
436 4895 $line = $cont . $w;
437 } else {
438 76230 $line .= ' ' . $w;
439 }
440 }
441 2307 return $out . $line . "\n";
442 }
443  
444 # Split POD lines into typed blocks: command paragraphs, verbatim paragraphs,
445 # ordinary paragraphs and =begin/=end data blocks.
446 147 ▶sub _pod_blocks {
Subroutine _pod_blocks: called CC=29 SCAR=36.1
447 147 my ($lines) = @_;
448 147 my (@blocks, $i);
449 147 my $n = scalar @$lines;
450 147 ▶ for ($i = 0; $i < $n; ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 7271 147 7418
451 7271 my $line = $lines->[$i];
452 7271 ▶ if ($line =~ /^\s*$/) { $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\s*$/) 3635 3636 7271
453 3636 ▶ if ($line =~ /^=begin\s+(\S+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^=begin\s+(\S+)/) 0 3636 3636
454 0 my $fmt = lc $1;
455 0 $i++;
456 0 my @buf;
457 0 ▶ push @buf, $lines->[$i++] while $i < $n && $lines->[$i] !~ /^=end\b/;
Branch: 1 branch
BranchTrueFalseTotal
$i < $n and not $lines->[$i] =~ /^=end\b/ and do { push @buf, $$lines[$i++] } 0 0 0
Condition0%0/3
$i < $n and not $lines->[$i] =~ /^=end\b/
!ll&&!rl&&r
000
MC/DC0%0/2
$i < $n and not $lines->[$i] =~ /^=end\b/
$i < $n not $lines->[$i] =~ /^=end\b/
Truth table0%0/3
A and not(B)
A= $i < $nB= not $lines->[$i] =~ /^=end\b/
ABresult
0X0
100
111
458 0 ▶ $i++ if $i < $n;
Branch: 1 branch
BranchTrueFalseTotal
if $i < $n 0 0 0
459 0 push @blocks, { type => 'data', fmt => $fmt, lines => \@buf };
460 0 next;
461 }
462 # Pod::Weaver rewrites every "=begin FMT ... =end FMT" into a single
463 # "=for FMT ..." paragraph when the distribution is built, so the
464 # shipped copy of this file carries the tables in that spelling. Both
465 # mean the same thing -- the rest of the paragraph is data for FMT --
466 # and the help has to read the tables either way.
467 3636 ▶ if ($line =~ /^=for[ \t]+(\S+)[ \t]*(.*)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^=for[ \t]+(\S+)[ \t]*(.*)$/) 142 3494 3636
468 142 my ($fmt, $first) = (lc $1, $2);
469 142 $i++;
470 142 ▶ my @buf = length($first) ? ($first . "\n") : ();
Branch: 1 branch
BranchTrueFalseTotal
length $first ? : 142 0 142
471 142 ▶ push @buf, $lines->[$i++]
Branch: 1 branch
BranchTrueFalseTotal
$i < $n and not $lines->[$i] =~ /^\s*$/ and not $lines->[$i] =~ /^=/ and do { push @buf, $$lines[$i++] } 4145 142 4287
Condition66%4/6
$i < $n and not $lines->[$i] =~ /^\s*$/ and not $lines->[$i] =~ /^=/
!ll&&!rl&&r
14204145
$i < $n and not $lines->[$i] =~ /^\s*$/
!ll&&!rl&&r
01424145
MC/DC33%1/3
$i < $n and not $lines->[$i] =~ /^\s*$/ and not $lines->[$i] =~ /^=/
$i < $n not $lines->[$i] =~ /^\s*$/ not $lines->[$i] =~ /^=/
Truth table50%2/4
A and not(B) and not(C)
A= $i < $nB= not $lines->[$i] =~ /^\s*$/C= not $lines->[$i] =~ /^=/
ABCresult
0XX0
10X0
1100
1111
472 while $i < $n && $lines->[$i] !~ /^\s*$/ && $lines->[$i] !~ /^=/;
473 142 push @blocks, { type => 'data', fmt => $fmt, lines => \@buf };
474 142 next;
475 }
476 3494 ▶ if ($line =~ /^=/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^=/) 1486 2008 3494
477 1486 my @buf = ($line);
478 1486 $i++;
479 1486 ▶ push @buf, $lines->[$i++]
Branch: 1 branch
BranchTrueFalseTotal
$i < $n and not $lines->[$i] =~ /^\s*$/ and not $lines->[$i] =~ /^=/ and do { push @buf, $$lines[$i++] } 870 1486 2356
Condition66%4/6
$i < $n and not $lines->[$i] =~ /^\s*$/ and not $lines->[$i] =~ /^=/
!ll&&!rl&&r
14860870
$i < $n and not $lines->[$i] =~ /^\s*$/
!ll&&!rl&&r
01486870
MC/DC33%1/3
$i < $n and not $lines->[$i] =~ /^\s*$/ and not $lines->[$i] =~ /^=/
$i < $n not $lines->[$i] =~ /^\s*$/ not $lines->[$i] =~ /^=/
Truth table50%2/4
A and not(B) and not(C)
A= $i < $nB= not $lines->[$i] =~ /^\s*$/C= not $lines->[$i] =~ /^=/
ABCresult
0XX0
10X0
1100
1111
480 while $i < $n && $lines->[$i] !~ /^\s*$/ && $lines->[$i] !~ /^=/;
481 1486 push @blocks, { type => 'cmd', lines => \@buf };
482 1486 next;
483 }
484 2008 ▶ if ($line =~ /^[ \t]/) { # verbatim
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^[ \t]/) 616 1392 2008
485 616 my @buf;
486 616 ▶ while ($i < $n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 3388 0 3388
487 3388 my $s = $lines->[$i];
488 3388 ▶ if ($s =~ /^\s*$/) { # keep interior blank lines
Branch: 1 branch
BranchTrueFalseTotal
if ($s =~ /^\s*$/) 810 2578 3388
489 810 my $j = $i;
490 810 ▶ $j++ while $j < $n && $lines->[$j] =~ /^\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
$j < $n and $lines->[$j] =~ /^\s*$/ and do { ++$j } 810 810 1620
Condition100%3/3
$j < $n and $lines->[$j] =~ /^\s*$/
!ll&&!rl&&r
13797810
MC/DC100%2/2
$j < $n and $lines->[$j] =~ /^\s*$/
$j < $n $lines->[$j] =~ /^\s*$/
Truth table100%3/3
A and B
A= $j < $nB= $lines->[$j] =~ /^\s*$/
ABresult
0X0
100
111
491 810 ▶ last if $j >= $n || $lines->[$j] !~ /^[ \t]/;
Branch: 1 branch
BranchTrueFalseTotal
if $j >= $n or not $lines->[$j] =~ /^[ \t]/ 615 195 810
Condition100%3/3
$j >= $n or not $lines->[$j] =~ /^[ \t]/
l!l&&r!l&&!r
13602195
MC/DC100%2/2
$j >= $n or not $lines->[$j] =~ /^[ \t]/
$j >= $n not $lines->[$j] =~ /^[ \t]/
Truth table100%3/3
A or not(B)
A= $j >= $nB= not $lines->[$j] =~ /^[ \t]/
ABresult
1X1
011
000
492 195 push @buf, "\n" for $i .. $j - 1;
493 195 $i = $j;
494 195 next;
495 }
496 2578 ▶ last if $s =~ /^=/ || $s !~ /^[ \t]/;
Branch: 1 branch
BranchTrueFalseTotal
if $s =~ /^=/ or not $s =~ /^[ \t]/ 1 2577 2578
Condition66%2/3
$s =~ /^=/ or not $s =~ /^[ \t]/
l!l&&r!l&&!r
012577
MC/DC50%1/2
$s =~ /^=/ or not $s =~ /^[ \t]/
$s =~ /^=/ not $s =~ /^[ \t]/
Truth table66%2/3
A or not(B)
A= $s =~ /^=/B= not $s =~ /^[ \t]/
ABresult
1X1
011
000
497 2577 push @buf, $s;
498 2577 $i++;
499 }
500 616 push @blocks, { type => 'verb', lines => \@buf };
501 616 next;
502 }
503 1392 my @buf; # ordinary paragraph
504 1392 ▶ push @buf, $lines->[$i++]
Branch: 1 branch
BranchTrueFalseTotal
$i < $n and not $lines->[$i] =~ /^\s*$/ and not $lines->[$i] =~ /^=/ and do { push @buf, $$lines[$i++] } 4177 1392 5569
Condition66%4/6
$i < $n and not $lines->[$i] =~ /^\s*$/ and not $lines->[$i] =~ /^=/
!ll&&!rl&&r
139204177
$i < $n and not $lines->[$i] =~ /^\s*$/
!ll&&!rl&&r
013924177
MC/DC33%1/3
$i < $n and not $lines->[$i] =~ /^\s*$/ and not $lines->[$i] =~ /^=/
$i < $n not $lines->[$i] =~ /^\s*$/ not $lines->[$i] =~ /^=/
Truth table50%2/4
A and not(B) and not(C)
A= $i < $nB= not $lines->[$i] =~ /^\s*$/C= not $lines->[$i] =~ /^=/
ABCresult
0XX0
10X0
1100
1111
505 while $i < $n && $lines->[$i] !~ /^\s*$/ && $lines->[$i] !~ /^=/;
506 1392 push @blocks, { type => 'text', lines => \@buf };
507 }
508 147 return @blocks;
509 }
510  
511 147 ▶sub _pod_render {
Subroutine _pod_render: called CC=27 SCAR=33.3
512 147 my ($lines, $width) = @_;
513 147 my @out;
514 147 my @indent = (1); # =over stack; 1 = one space of margin
515  
516 147 for my $b (_pod_blocks($lines)) {
517 3636 ▶ if ($b->{type} eq 'verb') {
Branch: 1 branch
BranchTrueFalseTotal
if ($b->{"type"} eq "verb") 616 3020 3636
518 # A markdown blockquote arrives here as verbatim text, but it is
519 # prose and reads far better wrapped than left at its source
520 # width, so pull those out and treat them as a paragraph.
521 616 my @body = grep { /\S/ } @{ $b->{lines} };
522 616 ▶ if (@body && !grep { !/^\s*>/ } @body) {
Branch: 1 branch
BranchTrueFalseTotal
if (@body and not grep({not /^\s*>/;} @body)) 6 610 616
Condition66%2/3
@body and not grep({not /^\s*>/;} @body)
!ll&&!rl&&r
06106
MC/DC50%1/2
@body and not grep({not /^\s*>/;} @body)
@body not grep({not /^\s*>/;} @body)
Truth table66%2/3
A and not(B)
A= @bodyB= not grep({not /^\s*>/;} @body)
ABresult
0X0
100
111
523 6 s/^\s*>\s?// for @body;
524 6 push @out, "\n", _pod_wrap(_pod_plain(join ' ', @body),
525 $width, $indent[-1] + 2);
526 6 next;
527 }
528 610 push @out, "\n";
529 610 my $pad = ' ' x $indent[-1];
530 # Formatting codes are not supposed to appear in a verbatim
531 # block, but the generated POD does leave a few behind; expand
532 # them rather than showing C<...> to the reader.
533 610 for my $v (@{ $b->{lines} }) {
534 2742 my $s = _pod_plain($v);
535 2742 $s =~ s/\s+\z//;
536 2742 ▶ push @out, length($s) ? $pad . $s . "\n" : "\n";
Branch: 1 branch
BranchTrueFalseTotal
length $s ? : 2557 185 2742
537 }
538 610 next;
539 }
540 3020 ▶ if ($b->{type} eq 'text') {
Branch: 1 branch
BranchTrueFalseTotal
if ($b->{"type"} eq "text") 1392 1628 3020
541 1392 push @out, "\n", _pod_wrap(_pod_plain(join ' ', @{ $b->{lines} }),
542 $width, $indent[-1]);
543 1392 next;
544 }
545 1628 ▶ if ($b->{type} eq 'data') {
Branch: 1 branch
BranchTrueFalseTotal
if ($b->{"type"} eq "data") 142 1486 1628
546 142 ▶ next unless $b->{fmt} =~ /^(?:html|text)$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $b->{"fmt"} =~ /^(?:html|text)$/ 0 142 142
547 142 push @out, _pod_data(join('', @{ $b->{lines} }), $b->{fmt},
548 142 $width, $indent[-1]);
549 142 next;
550 }
551  
552 # command paragraph
553 1486 my @l = @{ $b->{lines} };
554 1486 my $head = shift @l;
555 1486 ▶ $head =~ /^=(\w+)[ \t]*(.*)$/s or next;
Branch: 1 branch
BranchTrueFalseTotal
$head =~ /^=(\w+)[ \t]*(.*)$/s or next 1478 8 1486
556 1478 my ($cmd, $arg) = ($1, $2);
557 1478 $arg =~ s/\s+\z//;
558  
559 1478 ▶ if ($cmd =~ /^head([1-6])\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd =~ /^head([1-6])\z/) 612 866 1478
560 612 my $level = $1;
561 612 my $t = _pod_plain(join ' ', $arg, @l);
562 612 $t =~ s/\s+/ /g; $t =~ s/\A\s+//; $t =~ s/\s+\z//;
563 612 ▶ next unless length $t;
Branch: 1 branch
BranchTrueFalseTotal
unless length $t 0 612 612
564 612 push @out, "\n";
565 612 ▶ if ($level <= 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($level <= 2) { } 147 465 612
566 147 push @out, uc($t) . "\n";
567 } else {
568 465 push @out, $t . "\n", ('-' x _help_len($t)) . "\n";
569 }
570 612 next;
571 }
572 866 ▶ if ($cmd eq 'over') {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd eq "over") 128 738 866
573 128 ▶ my $by = ($arg =~ /([0-9]+)/) ? $1 : 4;
Branch: 1 branch
BranchTrueFalseTotal
$arg =~ /([0-9]+)/ ? : 0 128 128
574 128 ▶ $by = 2 if $by < 2;
Branch: 1 branch
BranchTrueFalseTotal
if $by < 2 0 128 128
575 128 ▶ $by = 8 if $by > 8;
Branch: 1 branch
BranchTrueFalseTotal
if $by > 8 0 128 128
576 128 push @indent, $indent[-1] + $by;
577 128 next;
578 }
579 738 ▶ if ($cmd eq 'back') {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd eq "back") 128 610 738
580 128 ▶ pop @indent if @indent > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @indent > 1 128 0 128
581 128 next;
582 }
583 610 ▶ if ($cmd eq 'item') {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd eq "item") 610 0 610
584 610 my $bullet = '*';
585 610 ▶ if ($arg =~ s/\A\*\s*//) { $bullet = '*' }
Branch: 2 branches
BranchTrueFalseTotal
if ($arg =~ s/\A\*\s*//) { } 598 12 610
elsif ($arg =~ s/\A([0-9]+\.?)(?:\s+|\z)//) { } 12 0 12
586 elsif ($arg =~ s/\A([0-9]+\.?)(?:\s+|\z)//) {
587 12 $bullet = $1; # numbered list: "1." or "1"
588 12 ▶ $bullet .= '.' unless $bullet =~ /\.\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $bullet =~ /\.\z/ 0 12 12
589 }
590 610 my $t = _pod_plain(join ' ', $arg, @l);
591 610 ▶ my $ind = $indent[-1] > 2 ? $indent[-1] - 2 : $indent[-1];
Branch: 1 branch
BranchTrueFalseTotal
$indent[-1] > 2 ? : 610 0 610
592 610 push @out, "\n";
593 610 ▶ if ($t =~ /\S/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t =~ /\S/) { } 610 0 610
594 610 push @out, _pod_wrap($t, $width, $ind, "$bullet ");
595 } else {
596 0 push @out, (' ' x $ind) . $bullet . "\n";
597 }
598 610 next;
599 }
600 # =pod, =cut, =encoding, =begin without =end: nothing to show
601 }
602  
603 147 my $text = join '', @out;
604 147 $text =~ s/\A\n+//;
605 147 $text =~ s/\n{3,}/\n\n/g;
606 147 $text =~ s/\n*\z/\n/;
607 147 return $text;
608 }
609  
610 # =begin html / =begin text data blocks (and their =for spelling, which is what
611 # Pod::Weaver leaves behind in the built distribution). The generated POD uses
612 # these for one thing only: parameter and output tables. Render them as aligned
613 # plain text so the help shows the same information the HTML documentation does.
614  
615 497 my %HTML_ENTITY = (
616 amp => '&', lt => '<', gt => '>', quot => '"', apos => "'", nbsp => ' ',
617 ndash => '-', mdash => '--', hellip => '...',
618 );
619  
620 2270 ▶sub _html_text {
Subroutine _html_text: called CC=4 SCAR=14.9
621 2270 my ($s) = @_;
622 2270 ▶ $s = '' unless defined $s;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s 0 2270 2270
623 2270 $s =~ s{<br\s*/?>}{ }gi;
624 2270 $s =~ s/<[^>]*>//g;
625 0 $s =~ s/&#x([0-9a-fA-F]+);/chr(hex $1)/ge;
626 0 ▶ $s =~ s/&#([0-9]+);/$1 < 128 ? chr($1) : '?'/ge;
Branch: 1 branch
BranchTrueFalseTotal
$1 < 128 ? : 0 0 0
627 2270 ▶ $s =~ s/&([a-zA-Z]+);/exists $HTML_ENTITY{$1} ? $HTML_ENTITY{$1} : "&$1;"/ge;
Branch: 1 branch
BranchTrueFalseTotal
exists $HTML_ENTITY{$1} ? : 193 0 193
628 2270 $s =~ s/\s+/ /g;
629 2270 $s =~ s/\A\s+//; $s =~ s/\s+\z//;
630 2270 return $s;
631 }
632  
633 142 ▶sub _pod_data {
Subroutine _pod_data: called CC=6 SCAR=21.4
634 142 my ($raw, $fmt, $width, $indent) = @_;
635 142 ▶ if ($fmt eq 'text') {
Branch: 1 branch
BranchTrueFalseTotal
if ($fmt eq "text") 0 142 142
636 0 my $pad = ' ' x $indent;
637 0 my $out = "\n";
638 0 for my $line (split /\n/, $raw, -1) {
639 0 $line =~ s/\s+\z//;
640 0 ▶ $out .= length($line) ? $pad . $line . "\n" : "\n";
Branch: 1 branch
BranchTrueFalseTotal
length $line ? : 0 0 0
641 }
642 0 return $out;
643 }
644  
645 142 my $out = '';
646 142 my $seen = 0;
647 142 ▶ while ($raw =~ m{<table[^>]*>(.*?)</table>}gis) {
Branch: 1 branch
BranchTrueFalseTotal
if ($raw =~ m[<table[^>]*>(.*?)</table>]sig) 99 142 241
648 99 $out .= _html_table($1, $width, $indent);
649 99 $seen = 1;
650 }
651 # HTML that is not a table carries nothing the plain-text help can use
652 142 ▶ return $seen ? $out : '';
Branch: 1 branch
BranchTrueFalseTotal
$seen ? : 99 43 142
653 }
654  
655 99 ▶sub _html_table {
Subroutine _html_table: called CC=32 SCAR=35.7
656 99 my ($html, $width, $indent) = @_;
657 99 my (@rows, @isheader);
658 99 ▶ while ($html =~ m{<tr[^>]*>(.*?)</tr>}gis) {
Branch: 1 branch
BranchTrueFalseTotal
if ($html =~ m[<tr[^>]*>(.*?)</tr>]sig) 690 99 789
659 690 my $tr = $1;
660 690 my (@cells, $hdr);
661 690 ▶ while ($tr =~ m{<t([dh])[^>]*>(.*?)</t\1\s*>}gis) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tr =~ m[<t([dh])[^>]*>(.*?)</t\1\s*>]sig) 2270 690 2960
662 2270 ▶ $hdr = 1 if lc($1) eq 'h';
Branch: 1 branch
BranchTrueFalseTotal
if lc $1 eq "h" 324 1946 2270
663 2270 push @cells, _html_text($2);
664 }
665 690 ▶ next unless @cells;
Branch: 1 branch
BranchTrueFalseTotal
unless @cells 0 690 690
666 690 push @rows, \@cells;
667 690 ▶ push @isheader, ($hdr ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$hdr ? : 99 591 690
668 }
669 99 ▶ return '' unless @rows;
Branch: 1 branch
BranchTrueFalseTotal
unless @rows 0 99 99
670  
671 # The header decides how many columns there are. A body row with more
672 # cells than that came from a markdown table whose source had an escaped
673 # pipe inside a cell, so fold the strays back into the last column
674 # instead of stretching the whole table to fit the accident.
675 99 my $ncol = 0;
676 99 for my $i (0 .. $#rows) {
677 99 ▶ next unless $isheader[$i];
Branch: 1 branch
BranchTrueFalseTotal
unless $isheader[$i] 0 99 99
678 99 $ncol = scalar @{ $rows[$i] };
679 99 last;
680 }
681 99 ▶ for my $r (@rows) { $ncol = @$r if !$ncol || (!grep { $_ } @isheader) && @$r > $ncol }
Branch: 1 branch
BranchTrueFalseTotal
if not $ncol or not grep({$_;} @isheader) and @$r > $ncol 0 690 690
Condition33%2/6
not $ncol or not grep({$_;} @isheader) and @$r > $ncol
l!l&&r!l&&!r
00690
not grep({$_;} @isheader) and @$r > $ncol
!ll&&!rl&&r
69000
MC/DC0%0/3
not $ncol or not grep({$_;} @isheader) and @$r > $ncol
not $ncol not grep({$_;} @isheader) @$r > $ncol
Truth table25%1/4
not(A) or not(B) and C
A= not $ncolB= not grep({$_;} @isheader)C= @$r > $ncol
ABCresult
1XX1
0111
00X0
0100
682 99 for my $r (@rows) {
683 690 ▶ if (@$r > $ncol) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$r > $ncol) 0 690 690
684 0 my @tail = splice @$r, $ncol - 1;
685 0 $r->[$ncol - 1] = join ' ', grep { length } @tail;
686 }
687 690 ▶ $r->[$ncol - 1] = '' unless defined $r->[$ncol - 1];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $$r[$ncol - 1] 0 690 690
688 }
689  
690 # natural column widths, then shrink the widest until the table fits
691 99 my @w = (0) x $ncol;
692 99 for my $r (@rows) {
693 690 for my $c (0 .. $ncol - 1) {
694 2270 ▶ my $l = _help_len(defined $r->[$c] ? $r->[$c] : '');
Branch: 1 branch
BranchTrueFalseTotal
defined $r->[$c] ? : 2270 0 2270
695 2270 ▶ $w[$c] = $l if $l > $w[$c];
Branch: 1 branch
BranchTrueFalseTotal
if $l > $w[$c] 821 1449 2270
696 }
697 }
698 99 my $gap = 2;
699 99 my $avail = $width - $indent - $gap * ($ncol - 1);
700 99 ▶ $avail = 8 * $ncol if $avail < 8 * $ncol;
Branch: 1 branch
BranchTrueFalseTotal
if $avail < 8 * $ncol 0 99 99
701 99 my $total = 0; $total += $_ for @w;
702 99 ▶ while ($total > $avail) {
Branch: 1 branch
BranchTrueFalseTotal
if ($total > $avail) 11456 99 11555
703 11456 my ($worst, $max) = (0, -1);
704 11456 ▶ for my $c (0 .. $ncol - 1) { ($worst, $max) = ($c, $w[$c]) if $w[$c] > $max }
Branch: 1 branch
BranchTrueFalseTotal
if $w[$c] > $max 31310 10993 42303
705 11456 ▶ last if $w[$worst] <= 8;
Branch: 1 branch
BranchTrueFalseTotal
if $w[$worst] <= 8 0 11456 11456
706 11456 $w[$worst]--;
707 11456 $total--;
708 }
709  
710 99 my $pad = ' ' x $indent;
711 99 my $out = "\n";
712 99 for my $i (0 .. $#rows) {
713 # wrap every cell to its column width, then print line by line
714 690 my @cell = map { [ _html_fold($rows[$i][$_], $w[$_]) ] } 0 .. $ncol - 1;
715 690 my $high = 0;
716 690 ▶ for my $c (@cell) { $high = scalar @$c if @$c > $high }
Branch: 1 branch
BranchTrueFalseTotal
if @$c > $high 1068 1202 2270
717 690 for my $line (0 .. $high - 1) {
718 my $s = $pad . join ' ' x $gap,
719 2220 ▶ map { my $t = defined $cell[$_][$line] ? $cell[$_][$line] : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $cell[$_][$line] ? : 3903 4362 8265
720 8265 $t . ' ' x ($w[$_] - _help_len($t)) } 0 .. $ncol - 1;
721 2220 $s =~ s/\s+\z//;
722 2220 $out .= $s . "\n";
723 }
724 690 ▶ if ($isheader[$i]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($isheader[$i]) 99 591 690
725 99 $out .= $pad . join(' ' x $gap, map { '-' x $w[$_] } 0 .. $ncol - 1) . "\n";
726 }
727 }
728 99 return $out . "\n";
729 }
730  
731 # greedy wrap of one table cell to $w display columns
732 2270 ▶sub _html_fold {
Subroutine _html_fold: called CC=11 SCAR=24.2
733 2270 my ($s, $w) = @_;
734 2270 ▶ $s = '' unless defined $s;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s 0 2270 2270
735 2270 ▶ return ('') unless length $s;
Branch: 1 branch
BranchTrueFalseTotal
unless length $s 8 2262 2270
736 2262 my @lines;
737 2262 my $cur = '';
738 2262 for my $word (split /\s+/, $s) {
739 11532 ▶ next unless length $word;
Branch: 1 branch
BranchTrueFalseTotal
unless length $word 0 11532 11532
740 11532 ▶ if (!length $cur) {
Branch: 2 branches
BranchTrueFalseTotal
if (not length $cur) { } 2262 9270 11532
elsif (_help_len($cur) + 1 + _help_len($word) <= $w) { } 7660 1610 9270
741 2262 $cur = $word;
742 } elsif (_help_len($cur) + 1 + _help_len($word) <= $w) {
743 7660 $cur .= ' ' . $word;
744 } else {
745 1610 push @lines, $cur;
746 1610 $cur = $word;
747 }
748 11532 ▶ while (_help_len($cur) > $w) { # a single over-long word
Branch: 1 branch
BranchTrueFalseTotal
if (_help_len($cur) > $w) 23 11532 11555
749 23 my $keep = $cur;
750 23 $keep = substr $keep, 0, $w;
751 23 ▶ $keep =~ s/[\x80-\xbf]+\z// if _help_len($keep) > $w;
Branch: 1 branch
BranchTrueFalseTotal
if _help_len($keep) > $w 0 23 23
752 23 push @lines, $keep;
753 23 $cur = substr $cur, length $keep;
754 }
755 }
756 2262 ▶ push @lines, $cur if length $cur;
Branch: 1 branch
BranchTrueFalseTotal
if length $cur 2262 0 2262
757 2262 ▶ return @lines ? @lines : ('');
Branch: 1 branch
BranchTrueFalseTotal
@lines ? : 2262 0 2262
758 }
759  
760 # colnames($df) / rownames($df)
761 #
762 # Return the column names and row names of any of the four Stats::LikeR
763 # frame shapes, as a list (R's colnames()/rownames()). In scalar context
764 # each returns the count, so `scalar colnames($df) == ncol($df)` and
765 # `scalar rownames($df) == nrow($df)` on a rectangular frame.
766 #
767 # Ordering mirrors view() exactly, so what you name is what you would see:
768 # * positional axes are 0-based integer indices --
769 # AoA columns, and the rows of AoA / AoH / HoA
770 # * key-based axes are the string-sorted union of keys --
771 # AoH / HoH columns (union across every row), HoA columns,
772 # and HoH rows (the outer keys)
773 #
774 # Shape is classified by _df_shape (the same detector agg() uses), so a
775 # ragged AoA/HoA is tolerated for enumeration: colnames() spans the widest
776 # row and rownames() the longest column. Empty frames yield an empty list.
777 # Like agg()/view(), the classifier is ref-based (not reftype), so hand it
778 # an unblessed frame -- blessed frames are the one case ncol()/nrow() take
779 # that this family does not.
780  
781 5331 ▶sub colnames {
Subroutine colnames: called CC=15 SCAR=27.1
Pod documented
782 5331 my ($df) = @_;
783 5331 ▶ _usage_error('colnames', 'undefined data in first position') unless defined $df;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $df 3 5328 5331
784 5328 my $shape = _df_shape($df, 'colnames');
785 5323 my @cols;
786 5323 ▶ if ($shape eq 'AoA') { # widest row -> 0 .. m-1
Branch: 2 branches
BranchTrueFalseTotal
if ($shape eq 'AoA') { } 14 5309 5323
elsif ($shape eq 'HoA') { } 5229 80 5309
787 14 my $m = 0;
788 14 for my $row (@$df) {
789 32 ▶ next unless ref $row eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $row eq "ARRAY" 1 31 32
790 31 ▶ $m = scalar @$row if scalar @$row > $m;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @$row > $m 14 17 31
791 }
792 14 @cols = (0 .. $m - 1);
793 } elsif ($shape eq 'HoA') { # keys ARE the columns
794 5229 @cols = sort keys %$df;
795 } else { # AoH / HoH: union of row keys
796 # `for my $row (@$df)` walks the array in place; `my @rows = @$df`
797 # flattens a copy of every row reference in the frame first, which on a
798 # million-row AoH is eight megabytes of list that is read once and
799 # thrown away. Two loops, no copy.
800 80 my %seen;
801 80 ▶ if ($shape eq 'AoH') {
Branch: 1 branch
BranchTrueFalseTotal
if ($shape eq 'AoH') { } 72 8 80
802 72 for my $row (@$df) {
803 240 ▶ next unless ref $row eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $row eq "HASH" 7 233 240
804 233 $seen{$_} = 1 for keys %$row;
805 }
806 } else {
807 8 for my $row (values %$df) {
808 21 ▶ next unless ref $row eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $row eq "HASH" 2 19 21
809 19 $seen{$_} = 1 for keys %$row;
810 }
811 }
812 80 @cols = sort keys %seen;
813 }
814 5323 ▶ return wantarray ? @cols : scalar @cols;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 5321 2 5323
815 }
816  
817 20 ▶sub rownames {
Subroutine rownames: called CC=8 SCAR=20.8
Pod documented
818 20 my ($df) = @_;
819 20 ▶ _usage_error('rownames', 'undefined data in first position') unless defined $df;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $df 3 17 20
820 17 my $shape = _df_shape($df, 'rownames');
821 12 my @rows;
822 12 ▶ if ($shape eq 'HoH') { # outer keys ARE the rows
Branch: 2 branches
BranchTrueFalseTotal
if ($shape eq 'HoH') { } 3 9 12
elsif ($shape eq 'HoA') { } 4 5 9
823 3 @rows = sort keys %$df;
824 } elsif ($shape eq 'HoA') { # longest column -> 0 .. n-1
825 4 my $n = 0;
826 4 for my $v (values %$df) {
827 6 ▶ next unless ref $v eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $v eq "ARRAY" 1 5 6
828 5 ▶ $n = scalar @$v if scalar @$v > $n;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @$v > $n 4 1 5
829 }
830 4 @rows = (0 .. $n - 1);
831 } else { # AoA / AoH: one row per element
832 5 @rows = (0 .. $#$df);
833 }
834 12 ▶ return wantarray ? @rows : scalar @rows;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 10 2 12
835 }
836  
837 # The XSUBs _cols_select / _cols_drop / _cols_rename are PRIVATE -- do NOT
838 # export them. (See select_drop_rename_cols.xs for the C side.)
839 #
840 # NAMING: `select` and `rename` are Perl core builtins, so exporting bare
841 # `select`/`rename` would shadow them in the caller. The `_cols` suffix
842 # avoids that and reads as a trio.
843  
844  
845 # select_cols($df, @cols) | select_cols($df, \@cols)
846 # drop_cols($df, @cols) | drop_cols($df, \@cols)
847 # rename_cols($df, old => new, ...) | rename_cols($df, { old => new, ... })
848 #
849 # Column subset / drop / rename over the four frame shapes -- the Stats::LikeR
850 # form of pandas df[['a','b']] / df.drop(columns=..) / df.rename(columns=..).
851 #
852 # * AoA -- identifiers are 0-based integer positions; rename_cols dies
853 # (an AoA has no labels; convert to AoH/HoA first).
854 # * AoH -- identifiers are the row-hash keys.
855 # * HoA -- identifiers are the top-level keys (the columns themselves).
856 # * HoH -- identifiers are the inner-row keys.
857 #
858 # VIEW SEMANTICS (fast + low RAM). Every result is a shallow view of the
859 # source, so huge frames cost almost nothing to slice:
860 # * the row shapes (AoH/HoH/AoA) build fresh row containers but SHARE the
861 # cell scalars by reference -- no per-cell copy, no duplicate scalar
862 # bodies (this is the XS path; see below);
863 # * HoA shares the whole column arrayrefs (a pure-Perl alias).
864 # The operation never mutates the source. But because cells/columns are
865 # shared, later IN-PLACE mutation of a result cell (e.g. $r->[0]{a}++) or a
866 # push/splice on a result HoA column reaches the source. Assigning a whole
867 # cell ($r->[0]{a} = ...) is always safe. Need an independent copy? Clone
868 # the result (e.g. Storable::dclone).
869 #
870 # IN-PLACE RENAME (rename_cols only). Called in VOID context, rename_cols
871 # mutates the source frame in place -- it renames the keys inside each AoH/HoH
872 # row, or the column keys of a HoA -- and returns nothing. In ANY other
873 # context it returns a fresh view (as above) and never touches the source:
874 #
875 # rename_cols(\%d, resolution => 'Resolution (A)'); # void -> %d in place
876 # %d = %{ rename_cols(\%d, resolution => 'Resolution (A)') }; # capture view
877 #
878 # (select_cols/drop_cols are always pure and ignore calling context -- a void
879 # call to either is a no-op.) Note: `\%d = rename_cols(...)` is not valid Perl
880 # (a reference constructor is not an lvalue before 5.22 refaliasing); use one
881 # of the two forms above.
882 #
883 # The row shapes are dispatched to XS (_cols_* ), which shares cells and
884 # hashes each column key once instead of once per row -- ~2x (select), ~3x
885 # (drop), ~4x (rename) faster than the pure-Perl rebuild at scale, and lower
886 # peak RAM (no copied cells). HoA/AoA-by-alias need no XS. All validation
887 # stays here in Perl, so the XS never has to croak mid-build.
888 #
889 # STRICT: a missing/renamed-away column, a duplicate in a select/drop list,
890 # or a rename whose targets are not distinct, all die with a labelled message.
891 # A column present in only some AoH/HoH rows is filled with undef by
892 # select_cols (rectangular); drop_cols/rename_cols leave ragged frames ragged.
893  
894 46 ▶sub _cols_arg { # normalise + validate a column list
Subroutine _cols_arg: called CC=7 SCAR=19.5
895 46 my ($fn, @a) = @_;
896 46 ▶ my @cols = (@a == 1 && ref $a[0] eq 'ARRAY') ? @{ $a[0] } : @a;
Branch: 1 branch
BranchTrueFalseTotal
@a == 1 && ref $a[0] eq 'ARRAY' ? : 1 45 46
Condition100%3/3
@a == 1 and ref $a[0] eq "ARRAY"
!ll&&!rl&&r
14311
MC/DC100%2/2
@a == 1 and ref $a[0] eq "ARRAY"
@a == 1 ref $a[0] eq "ARRAY"
Truth table100%3/3
A and B
A= @a == 1B= ref $a[0] eq "ARRAY"
ABresult
0X0
100
111
897 46 ▶ _usage_error($fn, 'at least one column is required') unless @cols;
Branch: 1 branch
BranchTrueFalseTotal
unless @cols 6 40 46
898 40 my %seen;
899 40 for my $c (@cols) {
900 49 ▶ _usage_error($fn, 'column identifier is undefined') unless defined $c;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $c 0 49 49
901 49 ▶ _usage_error($fn, "duplicate column '$c' in the list") if $seen{$c}++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$c}++ 1 48 49
902 }
903 39 return @cols;
904 }
905  
906 31 ▶sub _aoa_width { # widest row of an AoA (ragged-safe)
Subroutine _aoa_width: called CC=4 SCAR=13.9
907 31 my $df = shift;
908 31 my $w = 0;
909 31 ▶ for my $r (@$df) { $w = scalar @$r if ref $r eq 'ARRAY' && @$r > $w }
Branch: 1 branch
BranchTrueFalseTotal
if ref $r eq "ARRAY" and @$r > $w 31 82 113
Condition66%2/3
ref $r eq "ARRAY" and @$r > $w
!ll&&!rl&&r
08231
MC/DC50%1/2
ref $r eq "ARRAY" and @$r > $w
ref $r eq "ARRAY" @$r > $w
Truth table66%2/3
A and B
A= ref $r eq "ARRAY"B= @$r > $w
ABresult
0X0
100
111
910 31 return $w;
911 }
912  
913 11 ▶sub _aoa_int_cols { # validate integer positions in range
Subroutine _aoa_int_cols: called CC=4 SCAR=13.9
914 11 my ($fn, $df, @cols) = @_;
915 11 my $w = _aoa_width($df);
916 11 for my $c (@cols) {
917 14 ▶ _usage_error($fn, "AoA column '$c' is not a non-negative integer")
Branch: 1 branch
BranchTrueFalseTotal
unless $c =~ /^\d+$/ 2 12 14
918 unless $c =~ /^\d+$/;
919 12 ▶ _usage_error($fn, "AoA column index $c out of range (max index " . ($w - 1) . ")")
Branch: 1 branch
BranchTrueFalseTotal
if $c >= $w 2 10 12
920 if $c >= $w;
921 }
922 7 return $w;
923 }
924  
925 43 ▶sub _present_keys { # union of keys over AoH/HoH rows
Subroutine _present_keys: called CC=8 SCAR=20.9
926 43 my ($df, $shape) = @_;
927 43 my %seen; # no whole-frame list copy: see colnames
928 43 ▶ if ($shape eq 'AoH') {
Branch: 1 branch
BranchTrueFalseTotal
if ($shape eq 'AoH') { } 23 20 43
929 23 ▶ for my $r (@$df) { next unless ref $r eq 'HASH'; $seen{$_} = 1 for keys %$r }
Branch: 1 branch
BranchTrueFalseTotal
unless ref $r eq "HASH" 0 35 35
930 } else {
931 20 ▶ for my $r (values %$df) { next unless ref $r eq 'HASH'; $seen{$_} = 1 for keys %$r }
Branch: 1 branch
BranchTrueFalseTotal
unless ref $r eq "HASH" 0 88 88
932 }
933 43 return \%seen;
934 }
935  
936 5 ▶sub _rename_inplace { # VOID-context rename: mutate the source
Subroutine _rename_inplace: called CC=8 SCAR=20.8
937 5 my ($df, $shape, $map) = @_;
938 5 ▶ if ($shape eq 'HoA') { # rename the column keys
Branch: 1 branch
BranchTrueFalseTotal
if ($shape eq "HoA") 1 4 5
939 1 my %vals; # gather-then-set = swap-safe
940 1 for my $o (keys %$map) {
941 1 ▶ next unless exists $df->{$o};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $df->{$o} 0 1 1
942 1 $vals{ $map->{$o} } = delete $df->{$o};
943 }
944 1 $df->{$_} = $vals{$_} for keys %vals;
945 1 return;
946 }
947 7 ▶ my $one = sub { # AoH / HoH row hashes
Subroutine __ANON__: called CC=5 SCAR=16.1
948 7 my ($row) = @_;
949 7 ▶ return unless ref $row eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $row eq "HASH" 0 7 7
950 7 my %vals; # gather-then-set = swap-safe
951 7 for my $o (keys %$map) {
952 8 ▶ next unless exists $row->{$o};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $row->{$o} 1 7 8
953 7 $vals{ $map->{$o} } = delete $row->{$o};
954 }
955 7 $row->{$_} = $vals{$_} for keys %vals;
956 4 };
957 4 ▶ if ($shape eq 'AoH') { $one->($_) for @$df } # no list copy: see colnames
Branch: 1 branch
BranchTrueFalseTotal
if ($shape eq 'AoH') { } 2 2 4
958 2 else { $one->($_) for values %$df }
959 4 return;
960 }
961  
962 33 ▶sub select_cols {# shape code passed to the XS: 1 = AoH, 2 = HoH, 3 = AoA
Subroutine select_cols: called CC=10 SCAR=23.0
Pod documented
963 33 my $df = _untied(shift);
964 33 ▶ _usage_error('select_cols', 'undefined data in first position') unless defined $df;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $df 3 30 33
965 30 my @cols = _cols_arg('select_cols', @_);
966 25 my $shape = _df_shape($df, 'select_cols');
967  
968 25 ▶ if ($shape eq 'HoA') { # alias columns (pure Perl)
Branch: 1 branch
BranchTrueFalseTotal
if ($shape eq "HoA") 6 19 25
969 6 for my $c (@cols) {
970 7 ▶ die "select_cols: column '$c' not found\n" unless exists $df->{$c};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $df->{$c} 1 6 7
971 }
972 5 my %out;
973 5 $out{$_} = $df->{$_} for @cols;
974 5 return \%out;
975 }
976 19 ▶ if ($shape eq 'AoA') {
Branch: 1 branch
BranchTrueFalseTotal
if ($shape eq "AoA") 4 15 19
977 4 _aoa_int_cols('select_cols', $df, @cols);
978 2 return _cols_select($df, 3, [ @cols ]);
979 }
980 15 my $present = _present_keys($df, $shape);
981 15 for my $c (@cols) {
982 19 ▶ die "select_cols: column '$c' not found\n" unless $present->{$c};
Branch: 1 branch
BranchTrueFalseTotal
unless $present->{$c} 1 18 19
983 }
984 14 ▶ return _cols_select($df, $shape eq 'AoH' ? 1 : 2, [ @cols ]);
Branch: 1 branch
BranchTrueFalseTotal
$shape eq 'AoH' ? : 8 6 14
985 }
986  
987 18 ▶sub drop_cols {
Subroutine drop_cols: called CC=11 SCAR=24.0
Pod documented
988 18 my $df = _untied(shift);
989 18 ▶ _usage_error('drop_cols', 'undefined data in first position') unless defined $df;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $df 2 16 18
990 16 my @cols = _cols_arg('drop_cols', @_);
991 14 my %drop = map { $_ => 1 } @cols;
992 14 my $shape = _df_shape($df, 'drop_cols');
993  
994 13 ▶ if ($shape eq 'HoA') { # alias survivors (pure Perl)
Branch: 1 branch
BranchTrueFalseTotal
if ($shape eq "HoA") 2 11 13
995 2 for my $c (@cols) {
996 2 ▶ die "drop_cols: column '$c' not found\n" unless exists $df->{$c};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $df->{$c} 0 2 2
997 }
998 2 my %out;
999 2 ▶ for my $k (keys %$df) { next if $drop{$k}; $out{$k} = $df->{$k} }
Branch: 1 branch
BranchTrueFalseTotal
if $drop{$k} 2 4 6
1000 2 return \%out;
1001 }
1002 11 ▶ if ($shape eq 'AoA') {
Branch: 1 branch
BranchTrueFalseTotal
if ($shape eq "AoA") 1 10 11
1003 1 my $w = _aoa_int_cols('drop_cols', $df, @cols);
1004 1 my @keep = grep { !$drop{$_} } 0 .. $w - 1;
1005 1 return _cols_select($df, 3, [ @keep ]); # keep == select the rest
1006 }
1007 10 my $present = _present_keys($df, $shape);
1008 10 for my $c (@cols) {
1009 11 ▶ die "drop_cols: column '$c' not found\n" unless $present->{$c};
Branch: 1 branch
BranchTrueFalseTotal
unless $present->{$c} 1 10 11
1010 }
1011 9 ▶ return _cols_drop($df, $shape eq 'AoH' ? 1 : 2, \%drop);
Branch: 1 branch
BranchTrueFalseTotal
$shape eq 'AoH' ? : 4 5 9
1012 }
1013  
1014 27 ▶sub rename_cols {
Subroutine rename_cols: called CC=21 SCAR=30.4
Pod documented
1015 27 my $df = shift;
1016 27 ▶ $df = _untied($df) if defined wantarray; # void context renames the frame itself
Branch: 1 branch
BranchTrueFalseTotal
if defined wantarray 15 12 27
1017 27 ▶ _usage_error('rename_cols', 'undefined data in first position') unless defined $df;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $df 1 26 27
1018 26 my %map;
1019 26 ▶ if (@_ == 1 && ref $_[0] eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1 and ref $_[0] eq 'HASH') { } 1 25 26
Condition100%3/3
@_ == 1 and ref $_[0] eq "HASH"
!ll&&!rl&&r
2411
MC/DC100%2/2
@_ == 1 and ref $_[0] eq "HASH"
@_ == 1 ref $_[0] eq "HASH"
Truth table100%3/3
A and B
A= @_ == 1B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
1020 1 %map = %{ $_[0] };
1021 } else {
1022 25 ▶ _usage_error('rename_cols', 'arguments after the data frame must be old => new pairs (or one hashref)')
Branch: 1 branch
BranchTrueFalseTotal
if @_ % 2 1 24 25
1023 if @_ % 2;
1024 24 %map = @_;
1025 }
1026 25 ▶ _usage_error('rename_cols', 'at least one old => new mapping is required') unless %map;
Branch: 1 branch
BranchTrueFalseTotal
unless %map 3 22 25
1027 22 for my $o (keys %map) {
1028 25 ▶ _usage_error('rename_cols', "new name for '$o' is undefined") unless defined $map{$o};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $map{$o} 0 25 25
1029 }
1030 22 my $shape = _df_shape($df, 'rename_cols');
1031 21 ▶ die "rename_cols: an AoA has no column names to rename (convert to AoH/HoA first)\n"
Branch: 1 branch
BranchTrueFalseTotal
if $shape eq "AoA" 1 20 21
1032 if $shape eq 'AoA';
1033  
1034 my @present = $shape eq 'HoA' ? keys %$df
1035 20 ▶ : keys %{ _present_keys($df, $shape) };
Branch: 1 branch
BranchTrueFalseTotal
$shape eq 'HoA' ? : 2 18 20
1036 20 my %present = map { $_ => 1 } @present;
1037 20 for my $o (keys %map) {
1038 23 ▶ die "rename_cols: column '$o' not found\n" unless $present{$o};
Branch: 1 branch
BranchTrueFalseTotal
unless $present{$o} 1 22 23
1039 }
1040 19 my %final; # target names stay distinct
1041 19 for my $c (@present) {
1042 50 ▶ my $nn = exists $map{$c} ? $map{$c} : $c;
Branch: 1 branch
BranchTrueFalseTotal
exists $map{$c} ? : 22 28 50
1043 die "rename_cols: rename collides -- two columns would both become '$nn'\n"
1044 50 ▶ if $final{$nn}++;
Branch: 1 branch
BranchTrueFalseTotal
if $final{$nn}++ 2 48 50
1045 }
1046  
1047 # VOID context -> mutate the source in place and return nothing; any other
1048 # context returns a fresh shallow view exactly as before.
1049 17 ▶ unless (defined wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined wantarray) 5 12 17
1050 5 _rename_inplace($df, $shape, \%map);
1051 5 return;
1052 }
1053  
1054 12 ▶ if ($shape eq 'HoA') { # alias under new keys
Branch: 1 branch
BranchTrueFalseTotal
if ($shape eq "HoA") 1 11 12
1055 1 my %out;
1056 1 for my $k (keys %$df) {
1057 3 ▶ my $nk = exists $map{$k} ? $map{$k} : $k;
Branch: 1 branch
BranchTrueFalseTotal
exists $map{$k} ? : 1 2 3
1058 3 $out{$nk} = $df->{$k};
1059 }
1060 1 return \%out;
1061 }
1062 11 ▶ return _cols_rename($df, $shape eq 'AoH' ? 1 : 2, \%map);
Branch: 1 branch
BranchTrueFalseTotal
$shape eq 'AoH' ? : 5 6 11
1063 }
1064  
1065 24 ▶sub aoh2hoh {
Subroutine aoh2hoh: called CC=8 SCAR=20.8
Pod documented
1066 24 my ($aoh, $key) = @_;
1067 24 ▶ _usage_error('aoh2hoh', 'first argument is undefined') unless defined $aoh;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $aoh 2 22 24
1068 22 ▶ _usage_error('aoh2hoh', 'first argument must be an arrayref of hashrefs')
Branch: 1 branch
BranchTrueFalseTotal
unless ref $aoh eq "ARRAY" 4 18 22
1069 unless ref($aoh) eq 'ARRAY';
1070 18 ▶ _usage_error('aoh2hoh', 'a row key must be defined') unless defined $key;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key 2 16 18
1071 16 my %out;
1072 16 my $i = 0;
1073 16 for my $row (@$aoh) {
1074 27 ▶ die "aoh2hoh: index $i is not a hash" unless ref($row) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $row eq "HASH" 4 23 27
1075 23 ▶ die "aoh2hoh: index $i has no key \"$key\"" unless defined $row->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $row->{$key} 3 20 23
1076 20 my $rk = $row->{$key};
1077 die "aoh2hoh: duplicate key '$rk' has >= 2 occurrences"
1078 20 ▶ if exists $out{$rk};
Branch: 1 branch
BranchTrueFalseTotal
if exists $out{$rk} 2 18 20
1079 18 $out{$rk} = { %$row }; # shallow copy of the row
1080 18 $i++;
1081 }
1082 7 return \%out;
1083 }
1084  
1085 # h2aoh / df2h -- the flat hash as a two-column frame
1086 #
1087 # A plain hash is a two-column table that has been folded shut: every pair is
1088 # one row, the key in one cell and the value in the other. value_counts() and
1089 # table() hand one back, and none of the frame functions will take it, because
1090 # they all want nested data. h2aoh unfolds the hash into a real AoH; df2h
1091 # folds any frame back down -- an AoH, and also an AoA, HoA or HoH. It was
1092 # aoh2h, for an AoH alone, up to 0.3218, and aoh2h is still a name for it.
1093 # R spells this pair enframe()/deframe() (tibble);
1094 # pandas spells it pd.Series(d).rename_axis(..).reset_index(name => ..) and
1095 # Series.to_dict().
1096 #
1097 # The column names are var_name / value_name, the same two options melt() uses
1098 # to name the columns it emits, since the shape they describe is the same.
1099 #
1100 # The pair are exact inverses under their defaults, so
1101 # is_deeply( df2h( h2aoh(\%h) ), \%h )
1102 # holds for any flat hash whose keys are defined.
1103  
1104 # _kv_names($caller, \%arg) -- var_name / value_name, defaulted and checked.
1105 # Shared so the two directions cannot drift apart on the names they agree on.
1106 184 ▶sub _kv_names {
Subroutine _kv_names: called CC=4 SCAR=13.9
1107 184 my ($caller, $arg) = @_;
1108 184 ▶ my $var = defined $arg->{var_name} ? $arg->{var_name} : 'variable';
Branch: 1 branch
BranchTrueFalseTotal
defined $arg->{'var_name'} ? : 31 153 184
1109 184 ▶ my $value = defined $arg->{value_name} ? $arg->{value_name} : 'value';
Branch: 1 branch
BranchTrueFalseTotal
defined $arg->{'value_name'} ? : 22 162 184
1110 184 ▶ _usage_error($caller, 'var_name and value_name must differ') if $var eq $value;
Branch: 1 branch
BranchTrueFalseTotal
if $var eq $value 2 182 184
1111 182 return ($var, $value);
1112 }
1113  
1114 # _usage_text($fn) -- $fn's argparse-style help screen, from
1115 # Stats::LikeR::Usage, which is compiled only here, on the way to an error. A
1116 # name with no screen gets none, so that the error is still raised rather than
1117 # lost to a second one; t/usage.t keeps every exported name covered.
1118 1582 ▶sub _usage_text {
Subroutine _usage_text: called CC=3 SCAR=11.0
1119 1582 my ($fn) = @_;
1120 1582 require Stats::LikeR::Usage;
1121 1582 ▶ my $name = exists $Stats::LikeR::Usage::ALIAS{$fn} ? $Stats::LikeR::Usage::ALIAS{$fn} : $fn;
Branch: 1 branch
BranchTrueFalseTotal
exists $Stats::LikeR::Usage::ALIAS{$fn} ? : 3 1579 1582
1122 1582 my $screen = $Stats::LikeR::Usage::SCREEN{$name};
1123 1582 ▶ return defined $screen ? $screen : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $screen ? : 1581 1 1582
1124 }
1125  
1126 # _xlsx_provenance() -- ($user, $lines) for write_table's .xlsx document
1127 # properties (xlsx_written_by() in the XS). $user is the login name, undef when
1128 # none can be found; $lines is "user: ...", "host: ...", "perl: ..." and
1129 # "Stats::LikeR: ..." joined by newlines, leaving out any that is unknown.
1130 # getpwuid() dies "unimplemented" on Windows, so getlogin() and the environment
1131 # are the fallbacks there; Sys::Hostname (core) croaks when it finds no name.
1132 # $@ is local: a successful eval clears it, and this runs inside the caller's
1133 # write_table(), whose $@ up to 0.341 came back empty.
1134 87 ▶sub _xlsx_provenance {
Subroutine _xlsx_provenance: called CC=12 SCAR=29.5
1135 87 local $@;
1136 87 my $user = eval { scalar getpwuid($<) };
1137 87 ▶ $user = getlogin() unless defined $user && length $user;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $user and length $user 0 87 87
Condition33%1/3
defined $user and length $user
!ll&&!rl&&r
0087
MC/DC0%0/2
defined $user and length $user
defined $user length $user
Truth table33%1/3
A and B
A= defined $userB= length $user
ABresult
0X0
100
111
1138 87 for my $k (qw(USER USERNAME LOGNAME)) {
1139 87 ▶ last if defined $user && length $user;
Branch: 1 branch
BranchTrueFalseTotal
if defined $user and length $user 87 0 87
Condition33%1/3
defined $user and length $user
!ll&&!rl&&r
0087
MC/DC0%0/2
defined $user and length $user
defined $user length $user
Truth table33%1/3
A and B
A= defined $userB= length $user
ABresult
0X0
100
111
1140 0 $user = $ENV{$k};
1141 }
1142 87 ▶ $user = undef unless defined $user && length $user;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $user and length $user 0 87 87
Condition33%1/3
defined $user and length $user
!ll&&!rl&&r
0087
MC/DC0%0/2
defined $user and length $user
defined $user length $user
Truth table33%1/3
A and B
A= defined $userB= length $user
ABresult
0X0
100
111
1143 87 my $host = eval { require Sys::Hostname; Sys::Hostname::hostname() };
1144 87 ▶ $host = undef unless defined $host && length $host;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $host and length $host 0 87 87
Condition33%1/3
defined $host and length $host
!ll&&!rl&&r
0087
MC/DC0%0/2
defined $host and length $host
defined $host length $host
Truth table33%1/3
A and B
A= defined $hostB= length $host
ABresult
0X0
100
111
1145 87 my @lines;
1146 87 ▶ push @lines, "user: $user" if defined $user;
Branch: 1 branch
BranchTrueFalseTotal
if defined $user 87 0 87
1147 87 ▶ push @lines, "host: $host" if defined $host;
Branch: 1 branch
BranchTrueFalseTotal
if defined $host 87 0 87
1148 87 push @lines, sprintf('perl: v%vd', $^V), "Stats::LikeR: $VERSION";
1149 87 return ($user, join("\n", @lines));
1150 }
1151  
1152 # _sheet_name_clash(@names) -- the first two of a workbook's worksheet names
1153 # that are the same with case ignored, or nothing. Excel cannot open a
1154 # workbook holding both; write_table's XS (wt_sheet_names_distinct()) calls this
1155 # because lc() folds every character of a UTF-8 string, which C cannot do
1156 # portably. lc() is what Excel::Writer::XLSX 1.15 compares with too.
1157 24 ▶sub _sheet_name_clash {
Subroutine _sheet_name_clash: called CC=3 SCAR=11.0
1158 24 my %seen;
1159 24 for my $name (@_) {
1160 59 my $k = lc $name;
1161 59 ▶ return ($seen{$k}, $name) if exists $seen{$k};
Branch: 1 branch
BranchTrueFalseTotal
if exists $seen{$k} 4 55 59
1162 55 $seen{$k} = $name;
1163 }
1164 20 return;
1165 }
1166  
1167 # _usage_error($fn, $msg) -- die over an argument $fn was called with as
1168 # argparse does: the help screen, then "$fn: error: $msg" as the last line.
1169 # For the form of a call only (a missing or mistyped argument, an unknown
1170 # option, a value outside its choices); an error in the data keeps its own
1171 # one-line message. The XS raises the same through usage_croak().
1172 399 ▶sub _usage_error {
Subroutine _usage_error: called CC=1 SCAR=0.0
1173 399 my ($fn, $msg) = @_;
1174 399 die _usage_text($fn) . "$fn: error: $msg\n";
1175 }
1176  
1177 # _opt_names_defined($caller, @pairs) -- die, in the caller's name, on an undef
1178 # option name. Without it `my %arg = @_` raises perl's own "Use of
1179 # uninitialized value", which the FATAL warnings above make fatal and which
1180 # names a line of this file rather than the option. Up to 0.3217 that was what
1181 # h2aoh({}, undef, 1) and aoh2h([], undef, 1) died with.
1182 375 ▶sub _opt_names_defined {
Subroutine _opt_names_defined: called CC=3 SCAR=11.0
1183 375 my $caller = shift;
1184 375 ▶ for (my $j = 0; $j < @_; $j += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < @_) 310 372 682
1185 310 ▶ _usage_error($caller, 'an option name is undefined') unless defined $_[$j];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[$j] 3 307 310
1186 }
1187 }
1188  
1189 42 ▶sub h2aoh {
Subroutine h2aoh: called CC=18 SCAR=28.9
Pod documented
1190 42 my $h = shift;
1191 42 ▶ _usage_error('h2aoh', 'first argument is undefined') unless defined $h;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $h 2 40 42
1192 40 ▶ _usage_error('h2aoh', 'first argument must be a hashref') unless ref($h) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $h eq "HASH" 4 36 40
1193 36 ▶ _usage_error('h2aoh', 'arguments after the hash must be name => value pairs')
Branch: 1 branch
BranchTrueFalseTotal
if @_ % 2 1 35 36
1194 if @_ % 2;
1195 35 _opt_names_defined('h2aoh', @_);
1196 34 my %arg = @_;
1197 34 my %known = ( var_name => 1, value_name => 1, sort => 1 );
1198 34 my @bad = sort grep { !$known{$_} } keys %arg;
1199 34 ▶ _usage_error('h2aoh', "unknown argument(s): @bad") if @bad;
Branch: 1 branch
BranchTrueFalseTotal
if @bad 1 33 34
1200  
1201 33 my ($var_name, $value_name) = _kv_names('h2aoh', \%arg);
1202  
1203 # A reference value means the caller has a nested frame in hand, not a flat
1204 # hash, and one of the shape converters is the function they wanted. Say
1205 # which, rather than quietly stringifying the ref into a cell.
1206 32 for my $k (sort keys %$h) {
1207 78 ▶ next unless ref $h->{$k};
Branch: 1 branch
BranchTrueFalseTotal
unless ref $h->{$k} 75 3 78
1208 3 die "h2aoh: the value for key '$k' is a " . ref($h->{$k})
1209 . " reference; h2aoh takes a flat hash (hoa2aoh converts a "
1210 . "hash-of-arrays, hoh2hoa a hash-of-hashes)\n";
1211 }
1212  
1213 29 ▶ my $how = defined $arg{sort} ? lc $arg{sort} : 'key';
Branch: 1 branch
BranchTrueFalseTotal
defined $arg{'sort'} ? : 11 18 29
1214 29 my @keys = keys %$h;
1215 29 ▶ if ($how eq 'key') {
Branch: 3 branches
BranchTrueFalseTotal
if ($how eq 'key') { } 19 10 29
elsif ($how eq 'value') { } 6 4 10
elsif ($how ne 'none') { } 2 2 4
1216 # numeric when every key is a number, else string -- the rule agg()
1217 # already uses for its group keys
1218 44 @keys = ( grep { !looks_like_number($_) } @keys )
1219 ? sort @keys
1220 19 ▶ : sort { $a <=> $b } @keys;
Branch: 1 branch
BranchTrueFalseTotal
grep({not &looks_like_number($_);} @keys) ? : 17 2 19
1221 }
1222 elsif ($how eq 'value') {
1223 # biggest first for counts, which is what value_counts() output is for
1224 # and what pandas' Series.value_counts() gives. Non-numeric values have
1225 # no such convention, so they go up in string order. undef sorts last
1226 # either way, and ties break on the key so the order is total.
1227 13 my $numeric = !grep { !looks_like_number($_) }
1228 6 grep { defined } values %$h;
1229 @keys = sort {
1230 6 ( defined $h->{$a} ? 0 : 1 ) <=> ( defined $h->{$b} ? 0 : 1 )
1231 || ( !defined $h->{$a} ? 0
1232 : $numeric ? $h->{$b} <=> $h->{$a}
1233 13 ▶ : $h->{$a} cmp $h->{$b} )
Branch: 4 branches
BranchTrueFalseTotal
defined $h->{$a} ? : 13 0 13
defined $h->{$b} ? : 10 3 13
!defined($h->{$a}) ? : 0 10 10
$numeric ? : 9 1 10
Condition83%5/6
(defined $h->{$a} ? 0 : 1) <=> (defined $h->{$b} ? 0 : 1) || (!defined($h->{$a}) ? 0 : ($numeric ? $h->{$b} <=> $h->{$a} : $h->{$a} cmp $h->{$b})) || $a cmp $b
l!l&&r!l&&!r
1210
(defined $h->{$a} ? 0 : 1) <=> (defined $h->{$b} ? 0 : 1) or !defined($h->{$a}) ? 0 : ($numeric ? $h->{$b} <=> $h->{$a} : $h->{$a} cmp $h->{$b})
l!l&&r!l&&!r
391
MC/DC0%0/3
(defined $h->{$a} ? 0 : 1) <=> (defined $h->{$b} ? 0 : 1) || (!defined($h->{$a}) ? 0 : ($numeric ? $h->{$b} <=> $h->{$a} : $h->{$a} cmp $h->{$b})) || $a cmp $b
(defined $h->{$a} ? 0 : 1) <=> (defined $h->{$b} ? 0 : 1) !defined($h->{$a}) ? 0 : ($numeric ? $h->{$b} <=> $h->{$a} : $h->{$a} cmp $h->{$b}) $a cmp $b
Truth table75%3/4
A or not(B) || C
A= (defined $h->{$a} ? 0 : 1) <=> (defined $h->{$b} ? 0 : 1)B= !defined($h->{$a}) ? 0 : ($numeric ? $h->{$b} <=> $h->{$a} : $h->{$a} cmp $h->{$b})C= $a cmp $b
ABCresult
1XX1
01X1
0011
0000
1234 || $a cmp $b
1235 } @keys;
1236 }
1237 elsif ($how ne 'none') {
1238 2 _usage_error('h2aoh', "sort '$how' isn't allowed (key, value, none)");
1239 }
1240  
1241 27 return [ map { { $var_name => $_, $value_name => $h->{$_} } } @keys ];
1242 }
1243  
1244 # The rows are read by the XSUB _df2h, which also makes every check on them;
1245 # the shape and the options are read here.
1246 #
1247 # aoh2h is the same sub under the name it had up to 0.3218, when it took an AoH
1248 # alone. It is a glob alias, as rbind is of concat, so its messages say df2h.
1249 226 ▶sub df2h {
Subroutine df2h: called CC=44 SCAR=37.8
Pod documented
1250 226 my $df = shift;
1251 226 ▶ _usage_error('df2h', 'first argument is undefined') unless defined $df;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $df 5 221 226
1252 # reftype, not ref: a blessed frame or row is still one, as aoh2hoa
1253 # already took it. Up to 0.3217 aoh2h refused them.
1254 221 ▶ my $type = reftype($df) || '';
Condition100%2/2
builtin::reftype($df) || ''
l!l
2165
MC/DC100%1/1
builtin::reftype($df) || ''
builtin::reftype($df)
Truth table100%2/2
A
A= builtin::reftype($df)
Aresult
11
00
1255 221 ▶ _usage_error('df2h', 'first argument must be a data frame (AoA, AoH, HoA or HoH)')
Branch: 1 branch
BranchTrueFalseTotal
unless $type eq "ARRAY" or $type eq "HASH" 6 215 221
Condition100%3/3
$type eq "ARRAY" or $type eq "HASH"
l!l&&r!l&&!r
153626
MC/DC100%2/2
$type eq "ARRAY" or $type eq "HASH"
$type eq "ARRAY" $type eq "HASH"
Truth table100%3/3
A or B
A= $type eq "ARRAY"B= $type eq "HASH"
ABresult
1X1
011
000
1256 unless $type eq 'ARRAY' || $type eq 'HASH';
1257 215 ▶ _usage_error('df2h', 'arguments after the data frame must be name => value pairs')
Branch: 1 branch
BranchTrueFalseTotal
if @_ % 2 1 214 215
1258 if @_ % 2;
1259 214 _opt_names_defined('df2h', @_);
1260 212 my %arg = @_;
1261 212 my %known = ( var_name => 1, value_name => 1, duplicates => 1 );
1262 212 my @bad = sort grep { !$known{$_} } keys %arg;
1263 212 ▶ _usage_error('df2h', "unknown argument(s): @bad") if @bad;
Branch: 1 branch
BranchTrueFalseTotal
if @bad 1 211 212
1264  
1265 # A tied frame is copied once, as _untied() does, so that deciding its
1266 # shape here does not FETCH a row the XS then FETCHes again. _untied()
1267 # itself is not used because it asks ref(), which a blessed frame fails.
1268 211 ▶ if ($type eq 'ARRAY') { $df = [ @$df ] if tied @$df }
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'ARRAY') { } 149 62 211
if tied @$df 6 143 149
1269 62 ▶ else { $df = { %$df } if tied %$df }
Branch: 1 branch
BranchTrueFalseTotal
if tied %$df 2 60 62
1270  
1271 # The shape, by _df_shape()'s rules but with reftype(). An array is an
1272 # AoA when its first defined element is an array, and is otherwise read as
1273 # an AoH, so that the XS names the first row that is not a hash.
1274 211 my $shape; # 'AoA', 'AoH', 'HoA' or 'HoH'
1275 211 ▶ if ($type eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'ARRAY') { } 149 62 211
1276 149 $shape = 'AoH';
1277 149 for my $r (@$df) {
1278 148 ▶ next unless defined $r;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $r 3 145 148
1279 145 ▶ $shape = 'AoA' if (reftype($r) || '') eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::reftype($r) || "") eq "ARRAY" 54 91 145
Condition100%2/2
builtin::reftype($r) || ''
l!l
1432
MC/DC100%1/1
builtin::reftype($r) || ''
builtin::reftype($r)
Truth table100%2/2
A
A= builtin::reftype($r)
Aresult
11
00
1280 145 last;
1281 }
1282 } else {
1283 62 ▶ return {} unless %$df; # no rows, whichever of HoA or HoH it is
Branch: 1 branch
BranchTrueFalseTotal
unless %$df 1 61 62
1284 61 my ($saw_arr, $saw_hash) = (0, 0);
1285 61 for my $v (values %$df) {
1286 998 ▶ my $t = reftype($v) || next;
Condition100%2/2
builtin::reftype($v) || (next)
l!l
98810
MC/DC100%1/1
builtin::reftype($v) || (next)
builtin::reftype($v)
Truth table100%2/2
A
A= builtin::reftype($v)
Aresult
11
00
1287 988 ▶ $saw_arr = 1 if $t eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if $t eq "ARRAY" 66 922 988
1288 988 ▶ $saw_hash = 1 if $t eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if $t eq "HASH" 922 66 988
1289 }
1290 61 ▶ die "df2h: hashref mixes array and hash values (ambiguous HoA/HoH)\n"
Branch: 1 branch
BranchTrueFalseTotal
if $saw_arr and $saw_hash 1 60 61
Condition100%3/3
$saw_arr and $saw_hash
!ll&&!rl&&r
26341
MC/DC100%2/2
$saw_arr and $saw_hash
$saw_arr $saw_hash
Truth table100%3/3
A and B
A= $saw_arrB= $saw_hash
ABresult
0X0
100
111
1291 if $saw_arr && $saw_hash;
1292 # most likely df2h's own output, handed back to it
1293 60 ▶ die "df2h: first argument is a flat hash, not a data frame: none of "
Branch: 1 branch
BranchTrueFalseTotal
unless $saw_arr or $saw_hash 4 56 60
Condition100%3/3
$saw_arr or $saw_hash
l!l&&r!l&&!r
34224
MC/DC100%2/2
$saw_arr or $saw_hash
$saw_arr $saw_hash
Truth table100%3/3
A or B
A= $saw_arrB= $saw_hash
ABresult
1X1
011
000
1294 . "its values is an array or hash ref\n"
1295 unless $saw_arr || $saw_hash;
1296 56 ▶ $shape = $saw_hash ? 'HoH' : 'HoA';
Branch: 1 branch
BranchTrueFalseTotal
$saw_hash ? : 22 34 56
1297 }
1298  
1299 205 my ($var_name, $value_name);
1300 205 ▶ if ($shape eq 'AoA') {
Branch: 1 branch
BranchTrueFalseTotal
if ($shape eq 'AoA') { } 54 151 205
1301 # positions, Perl's own negative indexing as in agg(); the bound keeps
1302 # the XS's SvIV() from saturating, which a 32-bit IV would at 2**31
1303 ($var_name, $value_name) = map {
1304 99 my $c = $_;
1305 99 ▶ die "df2h: an AoA's columns are integer positions, not '$c'\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $c =~ /\A-?[0-9]+\z/ 9 90 99
1306 unless $c =~ /\A-?[0-9]+\z/;
1307 90 ▶ die "df2h: column position $c is past the end of any array\n"
Branch: 1 branch
BranchTrueFalseTotal
if abs $c > '9223372036854775807' 1 89 90
1308 if abs($c) > ~0 >> 1;
1309 89 ▶ 0 + $c; # '01' and '-0' are the positions 1 and 0
Branch: 2 branches
BranchTrueFalseTotal
defined $arg{'var_name'} ? : 22 32 54
defined $arg{'value_name'} ? : 12 42 54
1310 } ( defined $arg{var_name} ? $arg{var_name} : 0,
1311 54 defined $arg{value_name} ? $arg{value_name} : 1 );
1312 44 ▶ die "df2h: var_name and value_name must differ\n"
Branch: 1 branch
BranchTrueFalseTotal
if $var_name == $value_name 2 42 44
1313 if $var_name == $value_name;
1314 } else {
1315 151 ($var_name, $value_name) = _kv_names('df2h', \%arg);
1316 }
1317  
1318 192 ▶ my $dup = defined $arg{duplicates} ? lc $arg{duplicates} : 'die';
Branch: 1 branch
BranchTrueFalseTotal
defined $arg{'duplicates'} ? : 40 152 192
1319 192 my %dup_code = ( die => 0, first => 1, last => 2 ); # _df2h's `dup`
1320 die "df2h: duplicates '$dup' isn't allowed (die, first, last)\n"
1321 192 ▶ unless exists $dup_code{$dup};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $dup_code{$dup} 1 191 192
1322 # 'first' and 'last' mean an order, and a HoH has none: they would keep
1323 # whichever row hash order reached first, which differs from run to run
1324 191 ▶ die "df2h: duplicates '$dup' needs ordered rows, and a HoH's are not; "
Branch: 1 branch
BranchTrueFalseTotal
if $shape eq "HoH" and $dup ne "die" 4 187 191
Condition100%3/3
$shape eq "HoH" and $dup ne "die"
!ll&&!rl&&r
169184
MC/DC100%2/2
$shape eq "HoH" and $dup ne "die"
$shape eq "HoH" $dup ne "die"
Truth table100%3/3
A and B
A= $shape eq "HoH"B= $dup ne "die"
ABresult
0X0
100
111
1325 . "use 'die', or give an AoH, AoA or HoA\n"
1326 if $shape eq 'HoH' && $dup ne 'die';
1327  
1328 # _df2h's `shape`: 1 = AoH, 2 = HoH, 3 = AoA, 4 = HoA
1329 187 ▶ if ($shape eq 'HoA') {
Branch: 1 branch
BranchTrueFalseTotal
if ($shape eq "HoA") 34 153 187
1330 34 for my $c ($var_name, $value_name) {
1331 64 ▶ die "df2h: no '$c' column\n" unless exists $df->{$c};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $df->{$c} 4 60 64
1332 die "df2h: column '$c' is not an arrayref\n"
1333 60 ▶ unless (reftype($df->{$c}) || '') eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::reftype($df->{$c}) || "") eq "ARRAY" 2 58 60
Condition100%2/2
builtin::reftype($df->{$c}) || ''
l!l
582
MC/DC100%1/1
builtin::reftype($df->{$c}) || ''
builtin::reftype($df->{$c})
Truth table100%2/2
A
A= builtin::reftype($df->{$c})
Aresult
11
00
1334 }
1335 28 my ($nk, $nv) = map { scalar @{ $df->{$_} } } $var_name, $value_name;
1336 28 ▶ die "df2h: column '$var_name' has length $nk and '$value_name' has "
Branch: 1 branch
BranchTrueFalseTotal
if $nk != $nv 3 25 28
1337 . "length $nv; the two must be the same length\n" if $nk != $nv;
1338 return _df2h([ $df->{$var_name}, $df->{$value_name} ], 4,
1339 25 $var_name, $value_name, $dup_code{$dup}, undef);
1340 }
1341 153 ▶ if ($shape eq 'HoH') {
Branch: 1 branch
BranchTrueFalseTotal
if ($shape eq "HoH") 18 135 153
1342 # Hash order first, which costs no sort. Should that croak, the rows
1343 # are read again in sorted order, the one deterministic order a HoH has,
1344 # so that which bad row the message names is the same on every run.
1345 18 my $h;
1346 18 ▶ return $h if eval { $h = _df2h($df, 2, $var_name, $value_name, 0, [ keys %$df ]); 1 };
Branch: 1 branch
BranchTrueFalseTotal
if eval { do { $h = _df2h($df, 2, $var_name, $value_name, 0, [keys %$df]); 1 } } 8 10 18
1347 10 my $err = $@;
1348 10 _df2h($df, 2, $var_name, $value_name, 0, [ sort keys %$df ]);
1349 0 die $err; # reached only if the second reading did not croak
1350 }
1351 return _df2h($df, $shape eq 'AoA' ? 3 : 1, $var_name, $value_name,
1352 135 ▶ $dup_code{$dup}, undef);
Branch: 1 branch
BranchTrueFalseTotal
$shape eq 'AoA' ? : 42 93 135
1353 }
1354 497 ▶{ no warnings 'once'; *aoh2h = \&df2h; } # true synonym
Subroutine BEGIN: called CC=1 SCAR=0.0
1355 # agg / concat / rbind -- additions to lib/Stats/LikeR.pm
1356 # Splice these in after the dropna sub. Also add agg concat rbind to
1357 # @EXPORT_OK. rbind is a true glob-alias synonym for concat.
1358  
1359 # A tied frame, as a plain copy of it; anything else, as it is. Each pass over a
1360 # tied frame -- _df_shape, _present_keys, the XS read -- FETCHes every row or
1361 # column again, so a read-only function copies it once, here, and makes its
1362 # passes over the copy. The copy holds the same row and column references.
1363 # Only for functions that leave the frame unchanged: one that writes to it
1364 # must write to the frame it was given.
1365 473 ▶sub _untied {
Subroutine _untied: called CC=5 SCAR=16.1
1366 473 my $df = shift;
1367 473 ▶ return { %$df } if ref $df eq 'HASH' && tied %$df;
Branch: 1 branch
BranchTrueFalseTotal
if ref $df eq "HASH" and tied %$df 15 458 473
Condition100%3/3
ref $df eq "HASH" and tied %$df
!ll&&!rl&&r
29216615
MC/DC100%2/2
ref $df eq "HASH" and tied %$df
ref $df eq "HASH" tied %$df
Truth table100%3/3
A and B
A= ref $df eq "HASH"B= tied %$df
ABresult
0X0
100
111
1368 458 ▶ return [ @$df ] if ref $df eq 'ARRAY' && tied @$df;
Branch: 1 branch
BranchTrueFalseTotal
if ref $df eq "ARRAY" and tied @$df 9 449 458
Condition100%3/3
ref $df eq "ARRAY" and tied @$df
!ll&&!rl&&r
1892609
MC/DC100%2/2
ref $df eq "ARRAY" and tied @$df
ref $df eq "ARRAY" tied @$df
Truth table100%3/3
A and B
A= ref $df eq "ARRAY"B= tied @$df
ABresult
0X0
100
111
1369 449 return $df;
1370 }
1371  
1372 # _df_shape($df [, $caller]) is in LikeR.xs, where its rules are written out.
1373  
1374 # agg($df, agg => { col => 'mean' | [ 'mean', 'sd', .. ] | \&code, .. }, %opts)
1375 #
1376 # Split-apply-combine over any of the four data-frame shapes. With `by` it is
1377 # the combine half of group_by (which only splits); without `by` it collapses
1378 # the whole frame to a single row, like pandas df.agg(...).
1379 #
1380 # $df : AoA | AoH | HoA | HoH. For AoA the column identifiers in `by` and in
1381 # the `agg` spec are integer positions (negative from the end); for the
1382 # other three they are column names. A column no row has dies.
1383 #
1384 # OPTIONS
1385 # agg => { col => spec, .. } REQUIRED. spec is one aggregator name, an
1386 # arrayref of names, or a coderef. Named aggregators:
1387 # mean median sum sd var min max (numeric; call the XS functions)
1388 # count number of defined (non-undef) cells
1389 # n number of cells, undef included
1390 # nunique number of distinct defined cells
1391 # first first defined cell (undef if none)
1392 # last last defined cell (undef if none)
1393 # mode modal defined cell; ties resolved deterministically
1394 # (smallest number, else lowest string)
1395 # A coderef is called as $code->(\@cells), in scalar context, with a
1396 # copy of every cell for that column in the group (undef included).
1397 # by => $col | \@cols optional grouping column(s).
1398 # skipna => 0|1 default 1. When 0, a numeric named aggregator
1399 # (mean median sum sd var min max mode) over a group with any undef
1400 # yields undef, matching pandas skipna=False; count/n/nunique/first/
1401 # last always ignore this flag.
1402 # sort => 0|1 default 1. Sort output groups by key, each
1403 # by column numerically if all its values look like numbers, else as
1404 # strings, undef last (see _sort_group_keys); 0 keeps first-seen order.
1405 # 'output_type' => aoa|aoh|hoa|hoh default: same family as $df.
1406 #
1407 # OUTPUT COLUMN ORDER is deterministic: the `by` columns in the given order,
1408 # then the aggregated columns sorted (numerically for AoA integer columns, else
1409 # as strings), each expanded over its aggregator list in the order supplied. A
1410 # column reduced by a single aggregator keeps its own name; with two or more, or
1411 # when it is also a `by` column, it becomes "<col>_<func>" (e.g. age_mean,
1412 # age_sd), a coderef's func being fn (fn1, fn2, .. for several). A name
1413 # generated twice dies, except for positional aoa output. For hoh output the row label
1414 # is the group value (multiple `by` columns joined with '.'), 'all' when there
1415 # is no grouping, and made unique with a .N suffix on collision.
1416 #
1417 # Numeric aggregators need enough defined cells or the cell is undef: mean /
1418 # median / sum / min / max need >= 1, sd / var need >= 2. Ungrouped, a frame with
1419 # no rows is still one row. The original $df is never modified. The split itself
1420 # is the XS _agg_split(); see the comment above ag_set in LikeR.xs.
1421 {
1422 497 my %AGG_MIN = ( # minimum defined count for the XS numeric reducers
1423 mean => 1, median => 1, sum => 1, min => 1, max => 1,
1424 sd => 2, var => 2,
1425 );
1426 # the reducers an undef poisons under skipna => 0 (pandas skipna=False)
1427 497 my %AGG_NUMERIC = map { $_ => 1 } qw(mean median sum sd var min max mode);
1428 497 my %AGG_OTHER = map { $_ => 1 } qw(count n nunique first last mode);
1429 # reducers that stringify their cells: a cell they read is copied, not shared
1430 # with the caller's frame, because stringifying caches a PV on the SV
1431 497 my %AGG_STRINGIFY = ( mode => 1, nunique => 1 );
1432  
1433 72 ▶ sub _agg_reduce {
Subroutine _agg_reduce: called CC=2 SCAR=6.9
1434 72 my ($func, $raw, $def, $skipna) = @_; # $raw, $def: arrayrefs (def excl. undef)
1435 # scalar: a coderef returning a list, or nothing, would otherwise shift
1436 # every later column of the row
1437 72 ▶ return scalar $func->($raw) if ref $func eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $func eq "CODE" 4 68 72
1438 68 return _agg_reduce_named($func, $def, @$raw - @$def, $skipna);
1439 }
1440  
1441 # a named reducer over the defined cells $def, of which the group had $nna
1442 # more that were undef
1443 16005 ▶ sub _agg_reduce_named {
Subroutine _agg_reduce_named: called CC=23 SCAR=31.4
1444 16005 my ($func, $def, $nna, $skipna) = @_;
1445 # NA policy for numeric reducers when the caller asked for skipna => 0
1446 16005 ▶ return undef if !$skipna && $nna && $AGG_NUMERIC{$func};
Branch: 1 branch
BranchTrueFalseTotal
if not $skipna and $nna and $AGG_NUMERIC{$func} 101 15904 16005
Condition100%6/6
not $skipna and $nna and $AGG_NUMERIC{$func}
!ll&&!rl&&r
159022101
not $skipna and $nna
!ll&&!rl&&r
15781121103
MC/DC100%3/3
not $skipna and $nna and $AGG_NUMERIC{$func}
not $skipna $nna $AGG_NUMERIC{$func}
Truth table100%4/4
not(A) and B and C
A= not $skipnaB= $nnaC= $AGG_NUMERIC{$func}
ABCresult
0XX0
10X0
1100
1111
1447 15904 ▶ if ($func eq 'count') { return scalar @$def }
Branch: 1 branch
BranchTrueFalseTotal
if ($func eq "count") 4722 11182 15904
1448 11182 ▶ if ($func eq 'n') { return @$def + $nna }
Branch: 1 branch
BranchTrueFalseTotal
if ($func eq "n") 4748 6434 11182
1449 6434 ▶ if ($func eq 'nunique') { my %s; @s{ @$def } = (); return scalar keys %s }
Branch: 1 branch
BranchTrueFalseTotal
if ($func eq "nunique") 56 6378 6434
1450 6378 ▶ if ($func eq 'first') { return @$def ? $def->[0] : undef }
Branch: 2 branches
BranchTrueFalseTotal
if ($func eq "first") 10 6368 6378
@$def ? : 5 5 10
1451 6368 ▶ if ($func eq 'last') { return @$def ? $def->[-1] : undef }
Branch: 2 branches
BranchTrueFalseTotal
if ($func eq "last") 9 6359 6368
@$def ? : 5 4 9
1452 6359 ▶ if ($func eq 'mode') {
Branch: 1 branch
BranchTrueFalseTotal
if ($func eq "mode") 6 6353 6359
1453 6 ▶ return undef unless @$def;
Branch: 1 branch
BranchTrueFalseTotal
unless @$def 0 6 6
1454 6 my @m = mode($def);
1455 35 return (grep { !looks_like_number($_) } @m)
1456 ? (sort @m)[0]
1457 6 ▶ : (sort { $a <=> $b } @m)[0];
Branch: 1 branch
BranchTrueFalseTotal
grep({not &looks_like_number($_);} @m) ? : 1 5 6
1458 }
1459 6353 ▶ die "agg: unknown aggregator '$func'\n" unless exists $AGG_MIN{$func};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $AGG_MIN{$func} 0 6353 6353
1460 6353 ▶ return undef if @$def < $AGG_MIN{$func};
Branch: 1 branch
BranchTrueFalseTotal
if @$def < $AGG_MIN{$func} 113 6240 6353
1461 6240 ▶ return mean($def) if $func eq 'mean';
Branch: 1 branch
BranchTrueFalseTotal
if $func eq "mean" 881 5359 6240
1462 5359 ▶ return median($def) if $func eq 'median';
Branch: 1 branch
BranchTrueFalseTotal
if $func eq "median" 281 5078 5359
1463 5078 ▶ return sum($def) if $func eq 'sum';
Branch: 1 branch
BranchTrueFalseTotal
if $func eq "sum" 4859 219 5078
1464 219 ▶ return sd($def) if $func eq 'sd';
Branch: 1 branch
BranchTrueFalseTotal
if $func eq "sd" 78 141 219
1465 141 ▶ return var($def) if $func eq 'var';
Branch: 1 branch
BranchTrueFalseTotal
if $func eq "var" 37 104 141
1466 104 ▶ return min($def) if $func eq 'min';
Branch: 1 branch
BranchTrueFalseTotal
if $func eq "min" 57 47 104
1467 47 ▶ return max($def) if $func eq 'max';
Branch: 1 branch
BranchTrueFalseTotal
if $func eq "max" 47 0 47
1468 }
1469  
1470 323 ▶ sub agg {
Subroutine agg: called CC=74 SCAR=43.1
Pod documented
1471 323 my $df = _untied(shift);
1472 323 ▶ _usage_error('agg', 'undefined data in first position') unless defined $df;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $df 2 321 323
1473 321 my $shape = _df_shape($df, 'agg');
1474 318 ▶ _usage_error('agg', 'arguments after the data frame must be name => value pairs')
Branch: 1 branch
BranchTrueFalseTotal
if @_ % 2 1 317 318
1475 if @_ % 2;
1476 317 my %arg = @_;
1477 317 my %known = ( agg => 1, by => 1, skipna => 1, sort => 1, 'output_type' => 1 );
1478 317 my @bad = sort grep { !$known{$_} } keys %arg;
1479 317 ▶ _usage_error('agg', "unknown argument(s): @bad") if @bad;
Branch: 1 branch
BranchTrueFalseTotal
if @bad 2 315 317
1480  
1481 315 my $spec = $arg{agg};
1482 315 ▶ _usage_error('agg', "an 'agg' spec (hashref of column => aggregator) is required")
Branch: 1 branch
BranchTrueFalseTotal
unless ref $spec eq "HASH" and %$spec 2 313 315
Condition66%2/3
ref $spec eq "HASH" and %$spec
!ll&&!rl&&r
20313
MC/DC50%1/2
ref $spec eq "HASH" and %$spec
ref $spec eq "HASH" %$spec
Truth table66%2/3
A and B
A= ref $spec eq "HASH"B= %$spec
ABresult
0X0
100
111
1483 unless ref $spec eq 'HASH' and %$spec;
1484  
1485 my @by = !defined $arg{by} ? ()
1486 220 : ref $arg{by} eq 'ARRAY' ? @{ $arg{by} }
1487 313 ▶ : ( $arg{by} );
Branch: 2 branches
BranchTrueFalseTotal
!defined($arg{'by'}) ? : 47 266 313
ref $arg{'by'} eq 'ARRAY' ? : 220 46 266
1488 313 ▶ _usage_error('agg', "'by' contains an undefined column") if grep { !defined } @by;
Branch: 1 branch
BranchTrueFalseTotal
if grep {not defined $_;} @by 1 312 313
1489 312 ▶ my $skipna = exists $arg{skipna} ? ($arg{skipna} ? 1 : 0) : 1;
Branch: 2 branches
BranchTrueFalseTotal
exists $arg{'skipna'} ? : 216 96 312
$arg{'skipna'} ? : 158 58 216
1490 312 ▶ my $dosort = exists $arg{sort} ? ($arg{sort} ? 1 : 0) : 1;
Branch: 2 branches
BranchTrueFalseTotal
exists $arg{'sort'} ? : 2 310 312
$arg{'sort'} ? : 0 2 2
1491 312 ▶ my $otype = defined $arg{'output_type'} ? lc $arg{'output_type'}
Branch: 1 branch
BranchTrueFalseTotal
defined $arg{'output_type'} ? : 236 76 312
1492 : lc $shape;
1493 312 my %ok_otype = ( aoa => 1, aoh => 1, hoa => 1, hoh => 1 );
1494 _usage_error('agg', "output_type '$otype' isn't allowed (aoa, aoh, hoa, hoh)")
1495 312 ▶ unless $ok_otype{$otype};
Branch: 1 branch
BranchTrueFalseTotal
unless $ok_otype{$otype} 1 311 312
1496  
1497 311 my @agg_cols = keys %$spec;
1498 {
1499 311 my $all_num = !grep { !looks_like_number($_) } @agg_cols;
1500 311 ▶ @agg_cols = $all_num ? sort { $a <=> $b } @agg_cols : sort @agg_cols;
Branch: 1 branch
BranchTrueFalseTotal
$all_num ? : 61 250 311
1501 }
1502 311 ▶ if ($shape eq 'AoA') { # positions; Perl's own negative indexing
Branch: 2 branches
BranchTrueFalseTotal
if ($shape eq 'AoA') { } 62 249 311
elsif ($shape eq 'HoA') { } 74 175 249
1503 62 for my $c (@by, @agg_cols) {
1504 175 ▶ _usage_error('agg', "AoA columns are integer positions, not '$c'")
Branch: 1 branch
BranchTrueFalseTotal
unless $c =~ /\A-?[0-9]+\z/ 1 174 175
1505 unless $c =~ /\A-?[0-9]+\z/;
1506 }
1507 } elsif ($shape eq 'HoA') {
1508 74 for my $c (@by, @agg_cols) {
1509 201 ▶ die "agg: column '$c' not found\n" unless exists $df->{$c};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $df->{$c} 2 199 201
1510 die "agg: column '$c' is not an ARRAY reference\n"
1511 199 ▶ unless ref $df->{$c} eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $df->{$c} eq "ARRAY" 1 198 199
1512 }
1513 }
1514  
1515 # output plan: by-columns pass through, then each agg column with its
1516 # aggregator list contiguous. A single aggregator keeps the column name; two
1517 # or more, or a column that is also a `by` column, become "<col>_<func>". More
1518 # than one coderef on a column are numbered fn1, fn2, ..; one is just fn.
1519 307 my %is_by = map { $_ => 1 } @by;
1520 307 my (@agg_plan, @how); # [ col, [funcs], [out_names] ]; @how: _agg_split's modes
1521 307 for my $c (@agg_cols) {
1522 422 my $s = $spec->{$c};
1523 422 ▶ my @funcs = ref $s eq 'ARRAY' ? @$s : ($s);
Branch: 1 branch
BranchTrueFalseTotal
ref $s eq 'ARRAY' ? : 50 372 422
1524 422 ▶ _usage_error('agg', "empty aggregator list for column '$c'") unless @funcs;
Branch: 1 branch
BranchTrueFalseTotal
unless @funcs 2 420 422
1525 420 for my $f (@funcs) {
1526 531 ▶ next if ref $f eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $f eq "CODE" 9 522 531
1527 522 ▶ _usage_error('agg', "aggregator for column '$c' must be a name or a coderef")
Branch: 1 branch
BranchTrueFalseTotal
if not defined $f or ref $f 1 521 522
Condition66%2/3
not defined $f or ref $f
l!l&&r!l&&!r
10521
MC/DC50%1/2
not defined $f or ref $f
not defined $f ref $f
Truth table66%2/3
not(A) or B
A= not defined $fB= ref $f
ABresult
1X1
011
000
1528 if !defined $f || ref $f;
1529 _usage_error('agg', "unknown aggregator '$f'")
1530 521 ▶ unless exists $AGG_MIN{$f} || $AGG_OTHER{$f};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $AGG_MIN{$f} or $AGG_OTHER{$f} 2 519 521
Condition100%3/3
exists $AGG_MIN{$f} or $AGG_OTHER{$f}
l!l&&r!l&&!r
4111082
MC/DC100%2/2
exists $AGG_MIN{$f} or $AGG_OTHER{$f}
exists $AGG_MIN{$f} $AGG_OTHER{$f}
Truth table100%3/3
A or B
A= exists $AGG_MIN{$f}B= $AGG_OTHER{$f}
ABresult
1X1
011
000
1531 }
1532 417 my $ncode = grep { ref $_ eq 'CODE' } @funcs;
1533 417 my $ci = 0;
1534 417 ▶ my $multi = @funcs > 1 || $is_by{$c};
Condition100%3/3
@funcs > 1 || $is_by{$c}
l!l&&r!l&&!r
472368
MC/DC100%2/2
@funcs > 1 || $is_by{$c}
@funcs > 1 $is_by{$c}
Truth table100%3/3
A || B
A= @funcs > 1B= $is_by{$c}
ABresult
1X1
011
000
1535 my @names = map {
1536 417 ▶ my $l = ref $_ ne 'CODE' ? $_ : $ncode > 1 ? 'fn' . ++$ci : 'fn';
Branch: 2 branches
BranchTrueFalseTotal
ref $_ ne 'CODE' ? : 519 9 528
$ncode > 1 ? : 2 7 9
1537 528 ▶ $multi ? "${c}_${l}" : $c;
Branch: 1 branch
BranchTrueFalseTotal
$multi ? : 160 368 528
1538 } @funcs;
1539 417 push @agg_plan, [ $c, \@funcs, \@names ];
1540 528 ▶ push @how, ( grep { ref $_ eq 'CODE' } @funcs ) ? 0 # cells incl. undef, copied
Branch: 1 branch
BranchTrueFalseTotal
grep({$AGG_STRINGIFY{$_};} @funcs) ? : 23 386 409
1541 417 ▶ : ( grep { $AGG_STRINGIFY{$_} } @funcs ) ? 1 # defined cells, copied
Branch: 1 branch
BranchTrueFalseTotal
grep({ref $_ eq 'CODE';} @funcs) ? : 8 409 417
1542 : 2; # defined cells, shared
1543 }
1544 302 my @out_names = ( @by, map { @{ $_->[2] } } @agg_plan );
1545 302 ▶ if ($otype ne 'aoa') { # aoa is positional: a repeat costs nothing
Branch: 1 branch
BranchTrueFalseTotal
if ($otype ne "aoa") 285 17 302
1546 285 my (%seen, @dup);
1547 285 ▶ for my $n (@out_names) { push @dup, $n if $seen{$n}++ == 1 }
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$n}++ == 1 3 860 863
1548 285 ▶ die "agg: output column name(s) generated twice: @dup\n" if @dup;
Branch: 1 branch
BranchTrueFalseTotal
if @dup 3 282 285
1549 }
1550  
1551 # split, in XS: every row's group, and each aggregated cell dropped straight
1552 # into its group's array -- every cell for a column a coderef reads, only the
1553 # defined ones otherwise, with the undef cells counted instead
1554 my ($src, $code) = $shape eq 'AoA' ? ( $df, 3 )
1555 : $shape eq 'AoH' ? ( $df, 1 )
1556 : $shape eq 'HoA' ? ( $df, 4 )
1557 299 ▶ : ( [ @{$df}{ sort keys %$df } ], 1 ); # HoH: its rows, key order
Branch: 3 branches
BranchTrueFalseTotal
$shape eq 'AoA' ? : 61 238 299
$shape eq 'AoH' ? : 112 126 238
$shape eq 'HoA' ? : 69 57 126
1558 299 my ($groups, $seen) = @{ _agg_split($src, $code, [ @by ], [ @agg_cols ], \@how) };
1559 298 ▶ if (@$groups) { # a frame with no rows has no columns to miss
Branch: 2 branches
BranchTrueFalseTotal
if (@$groups) { } 295 3 298
elsif (not @by) { } 2 1 3
1560 295 my @all = ( @by, @agg_cols );
1561 295 for my $j (0 .. $#all) {
1562 791 ▶ die "agg: column '$all[$j]' not found\n" unless $seen->[$j];
Branch: 1 branch
BranchTrueFalseTotal
unless $seen->[$j] 5 786 791
1563 }
1564 } elsif (!@by) { # ungrouped: always one row, as pandas df.agg
1565 2 push @$groups, [ [], [ (0) x @agg_cols ], map { [] } @agg_cols ];
1566 }
1567  
1568 293 my @order = 0 .. $#$groups;
1569 293 ▶ if ($dosort && @by && @order > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dosort and @by and @order > 1) 227 66 293
Condition100%6/6
$dosort and @by and @order > 1
!ll&&!rl&&r
3531227
$dosort and @by
!ll&&!rl&&r
233258
MC/DC100%3/3
$dosort and @by and @order > 1
$dosort @by @order > 1
Truth table100%4/4
A and B and C
A= $dosortB= @byC= @order > 1
ABCresult
0XX0
10X0
1100
1111
1570 227 my %repr = map { $_ => $groups->[$_][0] } @order;
1571 227 @order = @{ _sort_group_keys(\@order, \%repr) };
1572 }
1573  
1574 # combine + materialise straight into the requested shape
1575 293 my (@aoa_rows, @aoh_rows, %hoa, %hoh, %seen_label);
1576 293 ▶ if ($otype eq 'hoa') { $hoa{$_} = [] for @out_names }
Branch: 1 branch
BranchTrueFalseTotal
if ($otype eq "hoa") 11 282 293
1577  
1578 293 for my $gi (@order) {
1579 5650 my ($repr, $nna, @cells) = @{ $groups->[$gi] };
1580 5650 $groups->[$gi] = undef; # this group's cells go once it is reduced
1581 5650 my @vals = @$repr; # by-column values, in order
1582 5650 for my $j (0 .. $#agg_plan) {
1583 6267 my ($c, $funcs) = @{ $agg_plan[$j] };
1584 6267 my $cells = $cells[$j];
1585 6267 ▶ my $def = $how[$j] || !$nna->[$j] ? $cells : [ grep { defined } @$cells ];
Branch: 1 branch
BranchTrueFalseTotal
$how[$j] || !$nna->[$j] ? : 6266 1 6267
Condition100%3/3
$how[$j] or not $nna->[$j]
l!l&&r!l&&!r
625881
MC/DC100%2/2
$how[$j] or not $nna->[$j]
$how[$j] not $nna->[$j]
Truth table100%3/3
A or not(B)
A= $how[$j]B= not $nna->[$j]
ABresult
1X1
011
000
1586 6267 for my $f (@$funcs) {
1587 15947 ▶ if (ref $f eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $f eq "CODE") 10 15937 15947
1588 10 push @vals, scalar $f->($cells); # see _agg_reduce
1589 10 next;
1590 }
1591 15937 my $v = eval { _agg_reduce_named($f, $def, $nna->[$j], $skipna) };
1592 15937 ▶ if (!defined $v && $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $v and $@) 2 15935 15937
Condition100%3/3
not defined $v and $@
!ll&&!rl&&r
157132222
MC/DC100%2/2
not defined $v and $@
not defined $v $@
Truth table100%3/3
not(A) and B
A= not defined $vB= $@
ABresult
0X0
100
111
1593 my $where = @by
1594 ? 'group (' . join(', ', map {
1595 2 ▶ "$by[$_] = " . (defined $repr->[$_] ? "'$repr->[$_]'" : 'undef')
Branch: 2 branches
BranchTrueFalseTotal
@by ? : 1 1 2
defined $repr->[$_] ? : 1 0 1
1596 } 0 .. $#by) . ')'
1597 : 'the whole frame';
1598 2 die "agg: $f of column '$c' over $where: $@";
1599 }
1600 15935 push @vals, $v;
1601 }
1602 }
1603 5648 ▶ if ($otype eq 'aoa') {
Branch: 3 branches
BranchTrueFalseTotal
if ($otype eq 'aoa') { } 4672 976 5648
elsif ($otype eq 'aoh') { } 950 26 976
elsif ($otype eq 'hoa') { } 21 5 26
1604 4672 push @aoa_rows, \@vals;
1605 } elsif ($otype eq 'aoh') {
1606 950 my %h; @h{ @out_names } = @vals; push @aoh_rows, \%h;
1607 } elsif ($otype eq 'hoa') {
1608 21 push @{ $hoa{ $out_names[$_] } }, $vals[$_] for 0 .. $#out_names;
1609 } else { # hoh
1610 my $label = @by
1611 5 ▶ ? join('.', map { defined $_ ? $_ : '' } @$repr)
Branch: 2 branches
BranchTrueFalseTotal
@by ? : 5 0 5
defined $_ ? : 8 0 8
1612 : 'all';
1613 5 my $uniq = $label; my $j = 0;
1614 0 ▶ while (exists $seen_label{$uniq}) { $uniq = $label . '.' . (++$j) }
Branch: 1 branch
BranchTrueFalseTotal
if (exists $seen_label{$uniq}) 0 5 5
1615 5 $seen_label{$uniq} = 1;
1616 5 my %h; @h{ @out_names } = @vals; $hoh{$uniq} = \%h;
1617 }
1618 }
1619 291 ▶ return \@aoa_rows if $otype eq 'aoa';
Branch: 1 branch
BranchTrueFalseTotal
if $otype eq "aoa" 16 275 291
1620 275 ▶ return \@aoh_rows if $otype eq 'aoh';
Branch: 1 branch
BranchTrueFalseTotal
if $otype eq "aoh" 262 13 275
1621 13 ▶ return \%hoa if $otype eq 'hoa';
Branch: 1 branch
BranchTrueFalseTotal
if $otype eq "hoa" 11 2 13
1622 2 return \%hoh;
1623 }
1624 }
1625  
1626  
1627 # assign($df, name => \&code, name2 => \&code2, ...)
1628 #
1629 # Add (or overwrite) columns derived from existing ones, dplyr-mutate style.
1630 # Each coderef is called once per row with the row as $_ (a hashref) and also
1631 # as $_[0]; $_[1] is the 0-based row index. For HoH inputs, $_[2] is the row key.
1632 # It is called in list context on every row and returns the new cell value; a
1633 # list of more than one value from row 0 is the whole column instead.
1634 #
1635 # Works on all three data-frame shapes:
1636 # AoH [ {weight=>70, height=>1.8}, ... ] (arrayref of row hashrefs)
1637 # HoA { weight=>[70,...], height=>[1.8,...] } (hashref of column arrayrefs)
1638 # HoH { r1 => {weight=>70}, r2 => {...} } (hashref of row hashrefs)
1639 #
1640 # Pairs are applied in order, so a later column may use an earlier new one.
1641 # Modifies $df in place (lowest RAM/CPU) and returns it for chaining.
1642 # To keep the original intact, hand it a copy: assign(clone($df), ...).
1643 #
1644 # A value may also be a map_cell { ... } block (see below) for an in-place
1645 # per-cell edit of the named column, instead of a CODE ref or ready-made ARRAY.
1646 #
1647  
1648 # map_cell { ... } -- in-place per-cell transform for assign().
1649 #
1650 # Wraps a block so assign() runs it once per row with $_ aliased to a copy of
1651 # the NAMED column's current cell; the (possibly modified) $_ is stored back and
1652 # the block's return value is ignored. This makes an in-place edit read
1653 # naturally, without the "copy, substitute, return" dance a plain sub needs
1654 # (perl 5.10 has no s///r):
1655 #
1656 # assign($tbl, 'Res.' => map_cell { s/^[A-Z]:// }); # strip a leading "X:"
1657 # assign($tbl, 'Res.' => map_cell { $_ = uc }); # upper-case in place
1658 #
1659 # The row is still available as $_[0] (and the index as $_[1]) if the transform
1660 # needs a sibling column. A plain sub { ... } keeps its existing meaning
1661 # ($_ = the whole row, the return value is stored), so map_cell is purely
1662 # additive and changes nothing for existing callers.
1663 #
1664 # An undef cell is left untouched (undef in -> undef out): the block never runs
1665 # on it, so s/// and friends don't warn on uninitialized values and a missing
1666 # cell stays missing rather than becoming ''.
1667 497 ▶sub map_cell (&) {
Subroutine map_cell: called CC=2 SCAR=6.9
Pod documented
1668 19 my ($code) = @_;
1669 19 ▶ _usage_error('map_cell', 'expects a code block')
Branch: 1 branch
BranchTrueFalseTotal
unless ref $code eq "CODE" 1 18 19
1670 unless ref $code eq 'CODE';
1671 18 return bless { code => $code }, 'Stats::LikeR::map_cell';
1672 }
1673  
1674 157 ▶sub assign {
Subroutine assign: called CC=85 SCAR=44.6
Pod documented
1675 157 my $df = shift;
1676 157 my $current_sub = (split(/::/,(caller(0))[3]))[-1];
1677 157 ▶ _usage_error($current_sub, 'first argument is undefined') unless defined $df;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $df 2 155 157
1678 155 ▶ _usage_error($current_sub, 'first argument must be a data frame (AoH arrayref or HoA/HoH hashref)')
Branch: 1 branch
BranchTrueFalseTotal
unless ref $df 6 149 155
1679 unless ref $df;
1680 149 ▶ _usage_error($current_sub, 'expected an even list of (name => value) pairs') if @_ % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ % 2 5 144 149
1681  
1682 144 my $r = ref $df;
1683 144 my $shape; # 'AoH', 'HoA' or 'HoH'
1684 144 ▶ if ($r eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if ($r eq 'ARRAY') { } 31 113 144
elsif ($r eq 'HASH') { } 113 0 113
1685 31 $shape = 'AoH';
1686 } elsif ($r eq 'HASH') {
1687 # Every value is looked at, not just the first one values() yields: a
1688 # HoA may carry scalar (or undef) entries, which the row view passes
1689 # through unchanged, and deciding on whichever value came first made the
1690 # same frame work or die from one run to the next with hash order.
1691 #
1692 # For a HoH the same pass is the row check: every value has to be a
1693 # hash, and counting them here saves a second pass over the rows, which
1694 # on 200000 rows cost 0.039s -- a fifth of the whole call.
1695 # A statement-modifier count runs in 0.013s on 200000 rows, against 0.019s
1696 # for the same test written as a loop with if/elsif.
1697 113 my %kind; # ref() of each value => how many
1698 113 $kind{ ref $_ }++ for values %$df;
1699 113 ▶ my $saw_arr = $kind{ARRAY} || 0;
Condition100%2/2
$kind{'ARRAY'} || 0
l!l
8726
MC/DC100%1/1
$kind{'ARRAY'} || 0
$kind{'ARRAY'}
Truth table100%2/2
A
A= $kind{'ARRAY'}
Aresult
11
00
1700 113 ▶ my $n_hash = $kind{HASH} || 0;
Condition100%2/2
$kind{'HASH'} || 0
l!l
2093
MC/DC100%1/1
$kind{'HASH'} || 0
$kind{'HASH'}
Truth table100%2/2
A
A= $kind{'HASH'}
Aresult
11
00
1701 113 ▶ die "$current_sub: hashref mixes array and hash values (ambiguous HoA/HoH)"
Branch: 1 branch
BranchTrueFalseTotal
if $saw_arr and $n_hash 1 112 113
Condition100%3/3
$saw_arr and $n_hash
!ll&&!rl&&r
26861
MC/DC100%2/2
$saw_arr and $n_hash
$saw_arr $n_hash
Truth table100%3/3
A and B
A= $saw_arrB= $n_hash
ABresult
0X0
100
111
1702 if $saw_arr and $n_hash;
1703 112 ▶ die "$current_sub: hashref holds no ARRAY (HoA) or HASH (HoH) values"
Branch: 1 branch
BranchTrueFalseTotal
if %$df and not $saw_arr and not $n_hash 3 109 112
Condition100%6/6
%$df and not $saw_arr and not $n_hash
!ll&&!rl&&r
90193
%$df and not $saw_arr
!ll&&!rl&&r
48622
MC/DC100%3/3
%$df and not $saw_arr and not $n_hash
%$df not $saw_arr not $n_hash
Truth table100%4/4
A and not(B) and not(C)
A= %$dfB= not $saw_arrC= not $n_hash
ABCresult
0XX0
10X0
1100
1111
1704 if %$df and not $saw_arr and not $n_hash;
1705 109 ▶ $shape = $n_hash ? 'HoH' : 'HoA'; # empty hash -> HoA
Branch: 1 branch
BranchTrueFalseTotal
$n_hash ? : 19 90 109
1706 109 ▶ if ($n_hash and @_ and $n_hash != keys %$df) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n_hash and @_ and $n_hash != keys %$df) 1 108 109
Condition83%5/6
$n_hash and @_ and $n_hash != keys %$df
!ll&&!rl&&r
90181
$n_hash and @_
!ll&&!rl&&r
90019
MC/DC66%2/3
$n_hash and @_ and $n_hash != keys %$df
$n_hash @_ $n_hash != keys %$df
Truth table75%3/4
A and B and C
A= $n_hashB= @_C= $n_hash != keys %$df
ABCresult
0XX0
10X0
1100
1111
1707 # Name the first bad row in visiting (sorted) order, as the row loops would.
1708 1 for my $k (sort keys %$df) {
1709 2 ▶ die "$current_sub: row '$k' is not a hashref" unless ref $df->{$k} eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $df->{$k} eq "HASH" 1 1 2
1710 }
1711 }
1712 } else {
1713 0 _usage_error($current_sub, 'data frame must be an arrayref (AoH) or hashref (HoA/HoH)');
1714 }
1715  
1716 # Row count. For a HoA it is the longest column; with no column at all yet
1717 # (an empty hash) it is undef until the first arrayref value fixes it.
1718 139 my (@rk, $n); # @rk: HoH row keys, in visiting order
1719 139 ▶ if ($shape eq 'AoH') {
Branch: 2 branches
BranchTrueFalseTotal
if ($shape eq 'AoH') { } 31 108 139
elsif ($shape eq 'HoH') { } 18 90 108
1720 31 $n = @$df;
1721 } elsif ($shape eq 'HoH') {
1722 18 @rk = sort keys %$df;
1723 18 $n = @rk;
1724 } else {
1725 90 for my $v (values %$df) {
1726 560 ▶ next unless ref $v eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $v eq "ARRAY" 452 108 560
1727 108 ▶ $n = @$v if not defined $n or @$v > $n;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $n or @$v > $n 88 20 108
Condition100%3/3
not defined $n or @$v > $n
l!l&&r!l&&!r
86220
MC/DC100%2/2
not defined $n or @$v > $n
not defined $n @$v > $n
Truth table100%3/3
not(A) or B
A= not defined $nB= @$v > $n
ABresult
1X1
011
000
1728 }
1729 }
1730  
1731 # Everything that can be checked before the first cell is written is checked
1732 # here, so that a bad row or a bad third pair cannot leave the first two
1733 # applied and the frame half-modified.
1734 139 my %hoa_col; # HoA: columns that exist by each pair
1735 139 ▶ if ($shape eq 'HoA') {
Branch: 1 branch
BranchTrueFalseTotal
if ($shape eq "HoA") 90 49 139
1736 90 $hoa_col{$_} = 1 for grep { ref $df->{$_} eq 'ARRAY' } keys %$df;
1737 }
1738 139 ▶ for (my $p = 0; $p < @_; $p += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p < @_) 153 123 276
1739 153 my ($name, $spec) = @_[$p, $p + 1];
1740 153 my $sref = ref $spec;
1741 153 ▶ if ($sref eq 'Stats::LikeR::map_cell') {
Branch: 1 branch
BranchTrueFalseTotal
if ($sref eq "Stats::LikeR::map_cell") 18 135 153
1742 die "$current_sub: map_cell target column '$name' must already exist as an ARRAY ref\n"
1743 18 ▶ if $shape eq 'HoA' and not $hoa_col{$name};
Branch: 1 branch
BranchTrueFalseTotal
if $shape eq "HoA" and not $hoa_col{$name} 2 16 18
Condition100%3/3
$shape eq "HoA" and not $hoa_col{$name}
!ll&&!rl&&r
792
MC/DC100%2/2
$shape eq "HoA" and not $hoa_col{$name}
$shape eq "HoA" not $hoa_col{$name}
Truth table100%3/3
A and not(B)
A= $shape eq "HoA"B= not $hoa_col{$name}
ABresult
0X0
100
111
1744 16 next;
1745 }
1746 135 ▶ _usage_error($current_sub, "value for '$name' must be a CODE or ARRAY ref")
Branch: 1 branch
BranchTrueFalseTotal
unless $sref eq "CODE" or $sref eq "ARRAY" 8 127 135
Condition100%3/3
$sref eq "CODE" or $sref eq "ARRAY"
l!l&&r!l&&!r
111168
MC/DC100%2/2
$sref eq "CODE" or $sref eq "ARRAY"
$sref eq "CODE" $sref eq "ARRAY"
Truth table100%3/3
A or B
A= $sref eq "CODE"B= $sref eq "ARRAY"
ABresult
1X1
011
000
1747 unless $sref eq 'CODE' or $sref eq 'ARRAY';
1748 127 ▶ if ($sref eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if ($sref eq "ARRAY") 16 111 127
1749 16 ▶ $n = @$spec unless defined $n;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $n 2 14 16
1750 16 ▶ die "$current_sub: column '$name' has " . @$spec . " values but data frame has $n rows"
Branch: 1 branch
BranchTrueFalseTotal
unless @$spec == $n 6 10 16
1751 unless @$spec == $n;
1752 }
1753 121 ▶ $n = 0 unless defined $n; # a coderef on a column-less HoA sees no rows
Branch: 1 branch
BranchTrueFalseTotal
unless defined $n 2 119 121
1754 121 $hoa_col{$name} = 1;
1755 }
1756 123 ▶ $n = 0 unless defined $n;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $n 0 123 123
1757 123 ▶ if ($shape ne 'HoA' and @_) {
Branch: 1 branch
BranchTrueFalseTotal
if ($shape ne "HoA" and @_) 39 84 123
Condition66%2/3
$shape ne "HoA" and @_
!ll&&!rl&&r
84039
MC/DC50%1/2
$shape ne "HoA" and @_
$shape ne "HoA" @_
Truth table66%2/3
A and B
A= $shape ne "HoA"B= @_
ABresult
0X0
100
111
1758 # A HoH's rows were checked by the shape scan above.
1759 39 ▶ if ($shape eq 'AoH') {
Branch: 1 branch
BranchTrueFalseTotal
if ($shape eq "AoH") 24 15 39
1760 24 for my $i (0 .. $n - 1) {
1761 54 ▶ die "$current_sub: row $i is not a hashref" unless ref $df->[$i] eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $df->[$i] eq "HASH" 4 50 54
1762 }
1763 }
1764 }
1765  
1766 # One save of $_ for the whole call; each row below only stores into it.
1767 # Assigning to the localized $_ copies the row reference, so a block that
1768 # writes to $_ changes nothing in $df.
1769 119 local $_;
1770  
1771 # A coderef is called in list context for every row, row 0 included. Row 0
1772 # used to be called in list context (to tell a whole-column return from a
1773 # per-row one) and every later row in scalar context, so any expression that
1774 # answers differently in the two -- a match with captures, a bare match, an
1775 # array -- gave row 0 one kind of value and the rest another:
1776 # sub { $_->{s} =~ /(\d+)/ } stored the digits in row 0 and 1 everywhere else.
1777 #
1778 # The store is a list assignment, (($cell) = $spec->(...)), whose value in
1779 # scalar context is how many values the call returned. That keeps list
1780 # context for the price of one comparison: on 200000 rows it ran 7% behind
1781 # the old scalar-context store, which hoisting `local $_` out of the row
1782 # loop (8%) more than pays for.
1783 2 ▶ my $too_many = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
1784 2 my ($name, $got, $i) = @_;
1785 2 die "$current_sub: '$name' returned $got values for row $i; a per-row coderef "
1786 . "must return one value per row (a whole-column list is told apart on row 0)";
1787 119 };
1788  
1789 119 ▶ if ($shape ne 'HoA') { # ----- AoH / HoH -----
Branch: 1 branch
BranchTrueFalseTotal
if ($shape ne "HoA") 35 84 119
1790 35 my $is_hoh = $shape eq 'HoH';
1791 35 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 40 31 71
1792 40 my ($name, $spec) = (shift, shift);
1793 40 my $sref = ref $spec;
1794 40 ▶ if ($sref eq 'Stats::LikeR::map_cell') { # in-place per-cell edit; $_ = current cell
Branch: 1 branch
BranchTrueFalseTotal
if ($sref eq "Stats::LikeR::map_cell") 6 34 40
1795 6 my $code = $spec->{code};
1796 6 for my $i (0 .. $n - 1) {
1797 15 ▶ my $row = $is_hoh ? $df->{ $rk[$i] } : $df->[$i];
Branch: 1 branch
BranchTrueFalseTotal
$is_hoh ? : 7 8 15
1798 15 $_ = $row->{$name};
1799 15 ▶ next unless defined $_; # undef cells pass through untouched (undef in -> undef out)
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_ 4 11 15
1800 11 ▶ $is_hoh ? $code->($row, $i, $rk[$i]) : $code->($row, $i);
Branch: 1 branch
BranchTrueFalseTotal
$is_hoh ? : 5 6 11
1801 11 $row->{$name} = $_;
1802 }
1803 6 next;
1804 }
1805 34 ▶ if ($sref eq 'ARRAY') { # ready-made column
Branch: 1 branch
BranchTrueFalseTotal
if ($sref eq "ARRAY") 3 31 34
1806 3 ▶ if ($is_hoh) { $df->{ $rk[$_] }{$name} = $spec->[$_] for 0 .. $n - 1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($is_hoh) { } 1 2 3
1807 2 else { $df->[$_]{$name} = $spec->[$_] for 0 .. $n - 1 }
1808 3 next;
1809 }
1810 31 ▶ next unless $n; # empty frame: nothing to compute
Branch: 1 branch
BranchTrueFalseTotal
unless $n 1 30 31
1811  
1812 30 ▶ my $row0 = $is_hoh ? $df->{ $rk[0] } : $df->[0];
Branch: 1 branch
BranchTrueFalseTotal
$is_hoh ? : 13 17 30
1813 30 $_ = $row0;
1814 30 ▶ my @out = $is_hoh ? $spec->($row0, 0, $rk[0]) : $spec->($row0, 0);
Branch: 1 branch
BranchTrueFalseTotal
$is_hoh ? : 13 17 30
1815 30 ▶ if (@out > 1) { # whole-column list (e.g. rank())
Branch: 1 branch
BranchTrueFalseTotal
if (@out > 1) 6 24 30
1816 6 ▶ die "$current_sub: column '$name' produced " . @out . " values but data frame has $n rows"
Branch: 1 branch
BranchTrueFalseTotal
unless @out == $n 2 4 6
1817 unless @out == $n;
1818 4 ▶ if ($is_hoh) { $df->{ $rk[$_] }{$name} = $out[$_] for 0 .. $n - 1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($is_hoh) { } 2 2 4
1819 2 else { $df->[$_]{$name} = $out[$_] for 0 .. $n - 1 }
1820 4 next;
1821 }
1822 24 $row0->{$name} = $out[0]; # per-row: row 0 already computed
1823 # Two copies of the loop so the hot path does not test $is_hoh per row.
1824 24 ▶ if ($is_hoh) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_hoh) { } 10 14 24
1825 10 for my $i (1 .. $n - 1) {
1826 11 my $row = $df->{ $rk[$i] };
1827 11 $_ = $row;
1828 11 my $got = (($row->{$name}) = $spec->($row, $i, $rk[$i]));
1829 11 ▶ $too_many->($name, $got, $i) if $got > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $got > 1 1 10 11
1830 }
1831 } else {
1832 14 for my $i (1 .. $n - 1) {
1833 20 my $row = $df->[$i];
1834 20 $_ = $row;
1835 20 my $got = (($row->{$name}) = $spec->($row, $i));
1836 20 ▶ $too_many->($name, $got, $i) if $got > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $got > 1 1 19 20
1837 }
1838 }
1839 }
1840 31 return $df;
1841 }
1842  
1843 # ----- HoA -----
1844 # The block sees each row as a hash built from the columns. Array columns
1845 # supply cell $i; any other value (a scalar, undef) is passed through as is.
1846 #
1847 # _hoa_assign (LikeR.xs) runs the loop with one view whose values alias the
1848 # frame's cells, so a write through $_->{col} lands in the frame, as it does
1849 # for an AoH or HoH. A tied frame or column keeps the perl loop below, which
1850 # hands the block copies: aliasing a tied element would alias the proxy SV
1851 # FETCH returns, not the element.
1852 my $tied = tied(%$df)
1853 84 ▶ || grep { ref $df->{$_} eq 'ARRAY' and tied @{ $df->{$_} } } keys %$df;
Condition83%5/6
tied %$df || grep({ref $df->{$_} eq 'ARRAY' and tied @{$$df{$_};};} keys %$df)
l!l&&r!l&&!r
0381
ref $df->{$_} eq 'ARRAY' && tied @{$$df{$_};}
!ll&&!rl&&r
4521013
MC/DC50%1/2
tied %$df || grep({ref $df->{$_} eq 'ARRAY' and tied @{$$df{$_};};} keys %$df)
tied %$df grep({ref $df->{$_} eq 'ARRAY' and tied @{$$df{$_};};} keys %$df)
MC/DC100%2/2
ref $df->{$_} eq 'ARRAY' && tied @{$$df{$_};}
ref $df->{$_} eq 'ARRAY' tied @{$$df{$_};}
Truth table66%2/3
A || B
A= tied %$dfB= grep({ref $df->{$_} eq 'ARRAY' and tied @{$$df{$_};};} keys %$df)
ABresult
1X1
011
000
Truth table100%3/3
A && B
A= ref $df->{$_} eq 'ARRAY'B= tied @{$$df{$_};}
ABresult
0X0
100
111
1854 3 ▶ my $snapshot = sub {
Subroutine __ANON__: called CC=3 SCAR=11.6
1855 3 my (@akeys, @acols, @skeys, @svals);
1856 3 for my $k (keys %$df) {
1857 5 my $c = $df->{$k};
1858 5 ▶ if (ref $c eq 'ARRAY') { push @akeys, $k; push @acols, $c }
Branch: 1 branch
BranchTrueFalseTotal
if (ref $c eq 'ARRAY') { } 5 0 5
1859 0 else { push @skeys, $k; push @svals, $c }
1860 }
1861 3 return (\@akeys, \@acols, \@skeys, \@svals);
1862 84 };
1863 84 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 88 81 169
1864 88 my ($name, $spec) = (shift, shift);
1865 88 my $sref = ref $spec;
1866 88 ▶ if ($sref eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if ($sref eq "ARRAY") 4 84 88
1867 4 $df->{$name} = [ @$spec ]; # copied, so the caller's array stays theirs
1868 4 next;
1869 }
1870 84 ▶ if (not $n and $sref eq 'CODE') { $df->{$name} = []; next }
Branch: 1 branch
BranchTrueFalseTotal
if (not $n and $sref eq "CODE") 2 82 84
Condition66%2/3
not $n and $sref eq "CODE"
!ll&&!rl&&r
8202
MC/DC50%1/2
not $n and $sref eq "CODE"
not $n $sref eq "CODE"
Truth table66%2/3
not(A) and B
A= not $nB= $sref eq "CODE"
ABresult
0X0
100
111
1871  
1872 82 ▶ if (not $tied) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($tied) 79 3 82
1873 79 ▶ if ($sref eq 'Stats::LikeR::map_cell') {
Branch: 1 branch
BranchTrueFalseTotal
if ($sref eq "Stats::LikeR::map_cell") 7 72 79
1874 7 _hoa_assign($df, $name, $spec->{code}, $n, 1);
1875 7 next;
1876 }
1877 72 my ($whole, $col) = _hoa_assign($df, $name, $spec, $n, 0);
1878 70 ▶ die "$current_sub: column '$name' produced " . @$col . " values but data frame has $n rows"
Branch: 1 branch
BranchTrueFalseTotal
if $whole and @$col != $n 1 69 70
Condition100%3/3
$whole and @$col != $n
!ll&&!rl&&r
6721
MC/DC100%2/2
$whole and @$col != $n
$whole @$col != $n
Truth table100%3/3
A and B
A= $wholeB= @$col != $n
ABresult
0X0
100
111
1879 if $whole and @$col != $n;
1880 69 $df->{$name} = $col;
1881 69 next;
1882 }
1883  
1884 # The perl loop, for a tied frame only (see $tied above).
1885 3 my ($akeys, $acols, $skeys, $svals) = $snapshot->();
1886 3 ▶ if ($sref eq 'Stats::LikeR::map_cell') { # in-place per-cell edit; $_ = current cell
Branch: 1 branch
BranchTrueFalseTotal
if ($sref eq "Stats::LikeR::map_cell") 2 1 3
1887 2 my $code = $spec->{code};
1888 2 my $tgt = $df->{$name};
1889 # One row view, refilled per row, and only for the rows the block
1890 # will actually see.
1891 #
1892 # Assembling it was the whole cost of the call: on a 64-column frame
1893 # of 20000 rows, a target column that is entirely undef -- so the
1894 # block never runs once -- still took 0.161s of the 0.165s a full
1895 # pass took, every bit of it spent building views that were then
1896 # thrown away unread, because the "is this cell undef" test sat after
1897 # the build instead of before it.
1898 #
1899 # The key set is the same for every row, so the hash is refilled
1900 # rather than reallocated. The block therefore sees one hash for the
1901 # whole pass instead of a fresh one per row, which is the shape the
1902 # other two frame layouts already have: the AoH and HoH branches above
1903 # hand the block the row hash itself, not a copy of it.
1904 2 my %view;
1905 2 for my $i (0 .. $n - 1) {
1906 6 $_ = $tgt->[$i];
1907 6 ▶ next unless defined $_; # undef cells pass through untouched (undef in -> undef out)
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_ 3 3 6
1908 3 @view{@$akeys} = map { $_->[$i] } @$acols;
1909 3 @view{@$skeys} = @$svals;
1910 3 $code->(\%view, $i);
1911 3 $tgt->[$i] = $_;
1912 }
1913 2 next;
1914 }
1915  
1916 # Unlike the map_cell view above, this one is a fresh hash every row:
1917 # a plain coderef may keep the hash it is given (return it, push it
1918 # somewhere), and a reused one would then change under it.
1919 1 my %v0;
1920 1 @v0{@$akeys} = map { $_->[0] } @$acols;
1921 1 @v0{@$skeys} = @$svals;
1922 1 $_ = \%v0;
1923 1 my @out = $spec->(\%v0, 0);
1924 1 ▶ if (@out > 1) { # whole-column list
Branch: 1 branch
BranchTrueFalseTotal
if (@out > 1) 0 1 1
1925 0 ▶ die "$current_sub: column '$name' produced " . @out . " values but data frame has $n rows"
Branch: 1 branch
BranchTrueFalseTotal
unless @out == $n 0 0 0
1926 unless @out == $n;
1927 0 $df->{$name} = \@out; # @out is this pair's own array: no second copy
1928 0 next;
1929 }
1930 1 my @new;
1931 1 $#new = $n - 1; # preallocate
1932 1 $new[0] = $out[0];
1933 1 for my $i (1 .. $n - 1) {
1934 2 my %view;
1935 2 @view{@$akeys} = map { $_->[$i] } @$acols;
1936 2 @view{@$skeys} = @$svals;
1937 2 $_ = \%view;
1938 2 my $got = (($new[$i]) = $spec->(\%view, $i));
1939 2 ▶ $too_many->($name, $got, $i) if $got > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $got > 1 0 2 2
1940 }
1941 1 $df->{$name} = \@new;
1942 }
1943 81 return $df;
1944 }
1945  
1946 22 ▶sub chunk {
Subroutine chunk: called CC=15 SCAR=27.1
Pod documented
1947 22 my ($aref, @rest) = @_;
1948 22 ▶ _usage_error('chunk', 'first argument must be an ARRAY reference')
Branch: 1 branch
BranchTrueFalseTotal
unless ref $aref eq "ARRAY" 4 18 22
1949 unless ref $aref eq 'ARRAY';
1950 # Own the message for an odd argument list. Falling straight into a hash
1951 # assignment left perl to report it, and under `warnings FATAL => 'all'`
1952 # chunk($aref, 2) died as "Odd number of elements in hash assignment at
1953 # LikeR.pm line 1686" -- naming neither chunk nor the option it wanted.
1954 18 ▶ _usage_error('chunk', 'arguments after the array reference must be name => value '
Branch: 1 branch
BranchTrueFalseTotal
if @rest % 2 2 16 18
1955 . 'pairs; pass exactly one of size => N or parts => K')
1956 if @rest % 2;
1957 16 my %opt = @rest;
1958  
1959 _usage_error('chunk', 'pass exactly one of size => N or parts => K')
1960 if (defined $opt{size} && defined $opt{parts})
1961 16 ▶ || (!defined $opt{size} && !defined $opt{parts});
Branch: 1 branch
BranchTrueFalseTotal
if defined $opt{"size"} and defined $opt{"parts"} or not defined $opt{"size"} || defined $opt{"parts"} 3 13 16
Condition100%9/9
defined $opt{"size"} and defined $opt{"parts"} or not defined $opt{"size"} || defined $opt{"parts"}
l!l&&r!l&&!r
1213
defined $opt{"size"} and defined $opt{"parts"}
!ll&&!rl&&r
871
defined $opt{'size'} || defined $opt{'parts'}
l!l&&r!l&&!r
762
MC/DC75%3/4
defined $opt{"size"} and defined $opt{"parts"} or not defined $opt{"size"} || defined $opt{"parts"}
defined $opt{"size"} defined $opt{"parts"} defined $opt{'size'} defined $opt{'parts'}
Truth table57%4/7
A and B or not(C || D)
A= defined $opt{"size"}B= defined $opt{"parts"}C= defined $opt{'size'}D= defined $opt{'parts'}
ABCDresult
11XX1
0X001
10001
0X1X0
0X010
101X0
10010
1962  
1963 13 my $n = scalar @$aref;
1964 13 my @groups;
1965 # Validated before the empty-input return, so a bad option is refused for [] too.
1966 13 for my $o (qw(size parts)) {
1967 _usage_error('chunk', "$o must be a positive integer")
1968 24 ▶ if defined $opt{$o} && $opt{$o} !~ /\A[1-9][0-9]*\z/;
Branch: 1 branch
BranchTrueFalseTotal
if defined $opt{$o} and not $opt{$o} =~ /\A[1-9][0-9]*\z/ 4 20 24
Condition100%3/3
defined $opt{$o} and not $opt{$o} =~ /\A[1-9][0-9]*\z/
!ll&&!rl&&r
1194
MC/DC100%2/2
defined $opt{$o} and not $opt{$o} =~ /\A[1-9][0-9]*\z/
defined $opt{$o} not $opt{$o} =~ /\A[1-9][0-9]*\z/
Truth table100%3/3
A and not(B)
A= defined $opt{$o}B= not $opt{$o} =~ /\A[1-9][0-9]*\z/
ABresult
0X0
100
111
1969 }
1970 # Empty input has no groups. Returning @groups rather than () keeps scalar
1971 # context a count (0), as it is for non-empty input, not undef.
1972 9 ▶ return @groups unless $n;
Branch: 1 branch
BranchTrueFalseTotal
unless $n 2 7 9
1973  
1974 7 ▶ if (defined $opt{size}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opt{'size'}) { } 4 3 7
1975 4 my $sz = $opt{size};
1976 4 ▶ for (my $i = 0; $i < $n; $i += $sz) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 15 4 19
1977 15 my $hi = $i + $sz - 1;
1978 15 ▶ $hi = $n - 1 if $hi > $n - 1;
Branch: 1 branch
BranchTrueFalseTotal
if $hi > $n - 1 3 12 15
1979 15 push @groups, [ @{$aref}[$i .. $hi] ];
1980 }
1981 } else {
1982 3 my $k = $opt{parts};
1983 3 for my $i (0 .. $k - 1) {
1984 15 my $lo = int( $i * $n / $k );
1985 15 my $hi = int( ($i + 1) * $n / $k );
1986 15 push @groups, [ @{$aref}[$lo .. $hi - 1] ];
1987 }
1988 }
1989 7 return @groups;
1990 }
1991  
1992 # A caller's qr//, as one the match operator can be handed on any perl.
1993 #
1994 # Before perl 5.18, `$x =~ $rx` with an empty $rx -- qr//, qr//i, or a qr/$s/
1995 # of an empty $s -- does not match the empty string: pp_match() swaps in the
1996 # last pattern that matched successfully, whatever it was. This prints 1 on
1997 # 5.10.1, 5.12.5 and 5.16.3 (only "bob" holds a b) and 3 on 5.18.4 and later:
1998 # perl -e 'my $rx = qr//; "abc" =~ /b/;
1999 # print scalar(grep { $_ =~ $rx } qw(NPI name bob)), "\n"'
2000 # read_table(drop_cols => qr//) failed t/read_table.drop_cols.t 923-930 on
2001 # those three perls for it, leaving out no column where it should have left
2002 # out every one. An empty pattern matches every string under any flags, so it
2003 # is exchanged for one that says so in a pattern that is not empty.
2004 #
2005 # The exchange compiles nothing: re::regexp_pattern() (in perl's core since
2006 # 5.10.0, with no `use re`) reads the pattern text, and $RX_EVERY is evaluated
2007 # once, when the module loads. Wrapping every pattern as qr/(?:$rx)/ would
2008 # evaluate a qr// on each call instead, which leaks an SV on perl 5.10.0
2009 # (pp_qr()) and inversion lists on 5.32 (regcomp), inside the callers'
2010 # no_leaks_ok checks.
2011 497 my $RX_EVERY = qr/(?:)/;
2012 1116 ▶sub _rx_for_match {
Subroutine _rx_for_match: called CC=3 SCAR=11.0
2013 1116 my ($rx) = @_;
2014 1116 my ($text) = re::regexp_pattern($rx);
2015 1116 ▶ return defined $text && $text eq '' ? $RX_EVERY : $rx;
Branch: 1 branch
BranchTrueFalseTotal
defined $text && $text eq '' ? : 142 974 1116
Condition66%2/3
defined $text and $text eq ""
!ll&&!rl&&r
0974142
MC/DC50%1/2
defined $text and $text eq ""
defined $text $text eq ""
Truth table66%2/3
A and B
A= defined $textB= $text eq ""
ABresult
0X0
100
111
2016 }
2017  
2018 # filter DSL: col() builds a predicate via overloading (pure Perl)
2019 # col('name') returns an overloaded object; comparing it (col('age') >= 18) or
2020 # combining comparisons with & | ! builds a predicate that carries its per-row
2021 # test in a {code} closure, and -- when C can reproduce it exactly -- in a
2022 # {plan} that filter() (XS) compiles and runs without calling the closure.
2023 #
2024 # Rules: numeric ops > < >= <= == != compare as numbers; string ops gt lt ge le
2025 # eq ne compare as strings; & | ! combine; operands may be in either order; a
2026 # missing/undef cell (and, for numeric ops, a non-numeric cell) never matches.
2027 #
2028 # Every predicate also carries {cols}, the distinct column names it reads, in
2029 # the order they first appear. filter() checks them against the frame before
2030 # testing a row, so a misspelt column dies instead of quietly keeping nothing
2031 # (or, under !, everything).
2032 279 ▶sub col { Stats::LikeR::col::_new(@_) }
Subroutine col: called CC=1 SCAR=0.0
Pod documented
2033 {
2034 package Stats::LikeR::col;
2035 497 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
2036 497 ▶ use Scalar::Util qw(blessed looks_like_number);
Subroutine BEGIN: called CC=1 SCAR=0.0
2037 use overload
2038 118 ▶ '>' => sub { _num($_[0], '>', $_[1], $_[2]) },
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
2039 14 ▶ '<' => sub { _num($_[0], '<', $_[1], $_[2]) },
Subroutine __ANON__: called CC=1 SCAR=0.0
2040 17 ▶ '>=' => sub { _num($_[0], '>=', $_[1], $_[2]) },
Subroutine __ANON__: called CC=1 SCAR=0.0
2041 7 ▶ '<=' => sub { _num($_[0], '<=', $_[1], $_[2]) },
Subroutine __ANON__: called CC=1 SCAR=0.0
2042 15 ▶ '==' => sub { _num($_[0], '==', $_[1], $_[2]) },
Subroutine __ANON__: called CC=1 SCAR=0.0
2043 5 ▶ '!=' => sub { _num($_[0], '!=', $_[1], $_[2]) },
Subroutine __ANON__: called CC=1 SCAR=0.0
2044 4 ▶ 'gt' => sub { _str($_[0], 'gt', $_[1], $_[2]) },
Subroutine __ANON__: called CC=1 SCAR=0.0
2045 7 ▶ 'lt' => sub { _str($_[0], 'lt', $_[1], $_[2]) },
Subroutine __ANON__: called CC=1 SCAR=0.0
2046 3 ▶ 'ge' => sub { _str($_[0], 'ge', $_[1], $_[2]) },
Subroutine __ANON__: called CC=1 SCAR=0.0
2047 3 ▶ 'le' => sub { _str($_[0], 'le', $_[1], $_[2]) },
Subroutine __ANON__: called CC=1 SCAR=0.0
2048 25 ▶ 'eq' => sub { _str($_[0], 'eq', $_[1], $_[2]) },
Subroutine __ANON__: called CC=1 SCAR=0.0
2049 5 ▶ 'ne' => sub { _str($_[0], 'ne', $_[1], $_[2]) },
Subroutine __ANON__: called CC=1 SCAR=0.0
2050 20 ▶ '&' => sub { _logic($_[0], '&', $_[1]) },
Subroutine __ANON__: called CC=1 SCAR=0.0
2051 14 ▶ '|' => sub { _logic($_[0], '|', $_[1]) },
Subroutine __ANON__: called CC=1 SCAR=0.0
2052 13 ▶ '!' => sub { _not($_[0]) },
Subroutine __ANON__: called CC=1 SCAR=0.0
2053 1 ▶ '""' => sub { 'Stats::LikeR::col predicate' },
Subroutine __ANON__: called CC=1 SCAR=0.0
2054 # A predicate has no truth value of its own. Up to 0.3216 this returned
2055 # 1, so col('a') > 1 && col('b') > 2 -- && and || test their left
2056 # operand for truth -- quietly became just col('b') > 2.
2057 5 ▶ 'bool' => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
2058 5 die "col(): a predicate has no truth value; combine predicates with & | ! "
2059 . "(not && || and or), e.g. (col('x') > 0) & (col('y') < 5)\n";
2060 },
2061 497 fallback => 0;
2062  
2063 279 ▶ sub _new {
Subroutine _new: called CC=3 SCAR=11.0
2064 279 my ($name) = @_;
2065 279 ▶ Stats::LikeR::_usage_error('col', 'expects a single column name') if !defined($name) || ref $name;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $name or ref $name 5 274 279
Condition100%3/3
not defined $name or ref $name
l!l&&r!l&&!r
41274
MC/DC100%2/2
not defined $name or ref $name
not defined $name ref $name
Truth table100%3/3
not(A) or B
A= not defined $nameB= ref $name
ABresult
1X1
011
000
2066 274 return bless { name => $name }, __PACKAGE__;
2067 }
2068  
2069 # The operand of a comparison may not be another column: up to 0.3216
2070 # col('x') > col('y') compared every cell with the col object, whose own
2071 # overloaded > returned a (true) predicate, and so kept every numeric row.
2072 223 ▶ sub _literal {
Subroutine _literal: called CC=3 SCAR=11.1
2073 223 my ($op, $other) = @_;
2074 223 ▶ die "col(): '$op' compares a column with a value, not with another column; "
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($other) and $other->isa(__PACKAGE__) 12 211 223
Condition66%2/3
builtin::blessed($other) and $other->isa(__PACKAGE__)
!ll&&!rl&&r
211012
MC/DC50%1/2
builtin::blessed($other) and $other->isa(__PACKAGE__)
builtin::blessed($other) $other->isa(__PACKAGE__)
Truth table66%2/3
A and B
A= builtin::blessed($other)B= $other->isa(__PACKAGE__)
ABresult
0X0
100
111
2075 . "use a sub, e.g. sub { \$_->{x} $op \$_->{y} }\n"
2076 if blessed($other) && $other->isa(__PACKAGE__);
2077 }
2078  
2079 # the distinct names in two {cols} lists, first appearance first
2080 44 ▶ sub _cols_union {
Subroutine _cols_union: called CC=2 SCAR=7.0
2081 44 my %seen;
2082 44 ▶ return [ grep { !$seen{$_}++ } map { @{ $_ || [] } } @_ ];
Condition50%1/2
$_ || []
l!l
760
MC/DC0%0/1
$_ || []
$_
Truth table50%1/2
A
A= $_
Aresult
11
00
2083 }
2084  
2085 my %NUM = (
2086 659 ▶ '>' => sub { $_[0] > $_[1] }, '<' => sub { $_[0] < $_[1] },
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
2087 249 ▶ '>=' => sub { $_[0] >= $_[1] }, '<=' => sub { $_[0] <= $_[1] },
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
2088 154 ▶ '==' => sub { $_[0] == $_[1] }, '!=' => sub { $_[0] != $_[1] },
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
2089 497 );
2090 my %STR = (
2091 141 ▶ 'gt' => sub { $_[0] gt $_[1] }, 'lt' => sub { $_[0] lt $_[1] },
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
2092 71 ▶ 'ge' => sub { $_[0] ge $_[1] }, 'le' => sub { $_[0] le $_[1] },
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
2093 275 ▶ 'eq' => sub { $_[0] eq $_[1] }, 'ne' => sub { $_[0] ne $_[1] },
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
2094 497 );
2095  
2096 # Besides the closure, a comparison carries a {plan}: the same test written
2097 # as plain data, which filter() (XS) compiles and runs in C, so a whole
2098 # frame can be tested without building a row hash or entering perl once per
2099 # row. The plan is [ KIND, OP, column, literal, swap ] for a comparison and
2100 # [ KIND, left, right ] / [ KIND, operand ] for & | !. KIND and OP are the
2101 # small integers LikeR.xs knows (FLTP_*/FLTC_*); the operator ids below are
2102 # positional, so the two tables must keep this order.
2103 #
2104 # A plan is only built for what C can reproduce exactly: the literal must be
2105 # a defined non-reference (and, for a numeric test, numeric), which rules
2106 # out overloaded objects and the warnings a non-numeric operand raises.
2107 # Everything else -- ->match/->nomatch, an object operand, any expression
2108 # with such a part in it -- carries no plan and takes the closure path, so
2109 # the two paths never disagree about a row.
2110 497 my $P_NUM = 0; my $P_STR = 1; my $P_AND = 2; my $P_OR = 3; my $P_NOT = 4;
2111 497 my %NUM_ID = ('>' => 0, '<' => 1, '>=' => 2, '<=' => 3, '==' => 4, '!=' => 5);
2112 497 my %STR_ID = ('gt' => 0, 'lt' => 1, 'ge' => 2, 'le' => 3, 'eq' => 4, 'ne' => 5);
2113  
2114 # numeric comparison: undef OR non-numeric cells never match
2115 176 ▶ sub _num {
Subroutine _num: called CC=8 SCAR=20.8
2116 176 my ($self, $op, $other, $swap) = @_;
2117 176 my $name = $self->{name};
2118 176 ▶ die "col(): the '$op' comparison must start from a bare column, e.g. col('x') $op ...\n"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 176 176
2119 unless defined $name;
2120 176 _literal($op, $other);
2121 170 my $f = $NUM{$op};
2122 my $code = $swap
2123 174 ▶ ? sub { my $c = $_[0]{$name}; (defined($c) && looks_like_number($c)) ? ($f->($other, $c) ? 1 : 0) : 0 }
Subroutine __ANON__: called CC=4 SCAR=13.9
Branch: 2 branches
BranchTrueFalseTotal
defined $c && &looks_like_number($c) ? : 131 43 174
&$f($other, $c) ? : 72 59 131
Condition100%3/3
defined $c and &looks_like_number($c)
!ll&&!rl&&r
1528131
MC/DC100%2/2
defined $c and &looks_like_number($c)
defined $c &looks_like_number($c)
Truth table100%3/3
A and B
A= defined $cB= &looks_like_number($c)
ABresult
0X0
100
111
2124 170 ▶ : sub { my $c = $_[0]{$name}; (defined($c) && looks_like_number($c)) ? ($f->($c, $other) ? 1 : 0) : 0 };
Subroutine __ANON__: called CC=4 SCAR=13.9
Branch: 3 branches
BranchTrueFalseTotal
$swap ? : 8 162 170
defined $c && &looks_like_number($c) ? : 1248 260 1508
&$f($c, $other) ? : 575 673 1248
Condition100%3/3
defined $c and &looks_like_number($c)
!ll&&!rl&&r
911691248
MC/DC100%2/2
defined $c and &looks_like_number($c)
defined $c &looks_like_number($c)
Truth table100%3/3
A and B
A= defined $cB= &looks_like_number($c)
ABresult
0X0
100
111
2125 my $plan = (defined($other) && !ref($other) && looks_like_number($other))
2126 170 ▶ ? [ $P_NUM, $NUM_ID{$op}, $name, $other, ($swap ? 1 : 0) ] : undef;
Branch: 2 branches
BranchTrueFalseTotal
defined $other && !ref($other) && &looks_like_number($other) ? : 167 3 170
$swap ? : 8 159 167
Condition100%6/6
defined $other and not ref $other and &looks_like_number($other)
!ll&&!rl&&r
21167
defined $other and not ref $other
!ll&&!rl&&r
11168
MC/DC100%3/3
defined $other and not ref $other and &looks_like_number($other)
defined $other not ref $other &looks_like_number($other)
Truth table100%4/4
A and not(B) and C
A= defined $otherB= not ref $otherC= &looks_like_number($other)
ABCresult
0XX0
10X0
1100
1111
2127 170 ▶ return bless { code => $code, cols => [ $name ], ($plan ? (plan => $plan) : ()) }, __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
$plan ? : 167 3 170
2128 }
2129  
2130 # string comparison: undef cells never match
2131 47 ▶ sub _str {
Subroutine _str: called CC=7 SCAR=19.7
2132 47 my ($self, $op, $other, $swap) = @_;
2133 47 my $name = $self->{name};
2134 47 ▶ die "col(): the '$op' comparison must start from a bare column, e.g. col('x') $op ...\n"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 47 47
2135 unless defined $name;
2136 47 _literal($op, $other);
2137 41 my $f = $STR{$op};
2138 my $code = $swap
2139 77 ▶ ? sub { my $c = $_[0]{$name}; defined($c) ? ($f->($other, $c) ? 1 : 0) : 0 }
Subroutine __ANON__: called CC=3 SCAR=11.0
Branch: 2 branches
BranchTrueFalseTotal
defined $c ? : 70 7 77
&$f($other, $c) ? : 49 21 70
2140 41 ▶ : sub { my $c = $_[0]{$name}; defined($c) ? ($f->($c, $other) ? 1 : 0) : 0 };
Subroutine __ANON__: called CC=3 SCAR=11.0
Branch: 3 branches
BranchTrueFalseTotal
$swap ? : 1 40 41
defined $c ? : 684 59 743
&$f($c, $other) ? : 285 399 684
2141 my $plan = (defined($other) && !ref($other))
2142 41 ▶ ? [ $P_STR, $STR_ID{$op}, $name, $other, ($swap ? 1 : 0) ] : undef;
Branch: 2 branches
BranchTrueFalseTotal
defined $other && !ref($other) ? : 41 0 41
$swap ? : 1 40 41
Condition33%1/3
defined $other and not ref $other
!ll&&!rl&&r
0041
MC/DC0%0/2
defined $other and not ref $other
defined $other not ref $other
Truth table33%1/3
A and not(B)
A= defined $otherB= not ref $other
ABresult
0X0
100
111
2143 41 ▶ return bless { code => $code, cols => [ $name ], ($plan ? (plan => $plan) : ()) }, __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
$plan ? : 41 0 41
2144 }
2145  
2146 34 ▶ sub _logic {
Subroutine _logic: called CC=10 SCAR=23.1
2147 34 my ($self, $op, $other) = @_;
2148 34 my $lc = $self->{code};
2149 34 ▶ die "col(): the left operand of '$op' is not a comparison (build it like (col('x') > 0))\n"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $lc eq "CODE" 1 33 34
2150 unless ref $lc eq 'CODE';
2151 33 ▶ my $rc = (blessed($other) && $other->isa(__PACKAGE__)) ? $other->{code} : undef;
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($other) && $other->isa(__PACKAGE__) ? : 33 0 33
Condition33%1/3
builtin::blessed($other) and $other->isa(__PACKAGE__)
!ll&&!rl&&r
0033
MC/DC0%0/2
builtin::blessed($other) and $other->isa(__PACKAGE__)
builtin::blessed($other) $other->isa(__PACKAGE__)
Truth table33%1/3
A and B
A= builtin::blessed($other)B= $other->isa(__PACKAGE__)
ABresult
0X0
100
111
2152 33 ▶ die "col(): the right operand of '$op' must be a col() comparison too\n"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $rc eq "CODE" 1 32 33
2153 unless ref $rc eq 'CODE';
2154 my $code = $op eq '&'
2155 270 ▶ ? sub { ($lc->($_[0]) && $rc->($_[0])) ? 1 : 0 }
Subroutine __ANON__: called CC=3 SCAR=11.0
Branch: 1 branch
BranchTrueFalseTotal
&$lc($_[0]) && &$rc($_[0]) ? : 82 188 270
Condition100%3/3
&$lc($_[0]) and &$rc($_[0])
!ll&&!rl&&r
1622682
MC/DC100%2/2
&$lc($_[0]) and &$rc($_[0])
&$lc($_[0]) &$rc($_[0])
Truth table100%3/3
A and B
A= &$lc($_[0])B= &$rc($_[0])
ABresult
0X0
100
111
2156 32 ▶ : sub { ($lc->($_[0]) || $rc->($_[0])) ? 1 : 0 };
Subroutine __ANON__: called CC=3 SCAR=11.0
Branch: 2 branches
BranchTrueFalseTotal
$op eq '&' ? : 18 14 32
&$lc($_[0]) || &$rc($_[0]) ? : 61 117 178
Condition100%3/3
&$lc($_[0]) or &$rc($_[0])
l!l&&r!l&&!r
4417117
MC/DC100%2/2
&$lc($_[0]) or &$rc($_[0])
&$lc($_[0]) &$rc($_[0])
Truth table100%3/3
A or B
A= &$lc($_[0])B= &$rc($_[0])
ABresult
1X1
011
000
2157 my $plan = ($self->{plan} && $other->{plan})
2158 32 ▶ ? [ ($op eq '&' ? $P_AND : $P_OR), $self->{plan}, $other->{plan} ] : undef;
Branch: 2 branches
BranchTrueFalseTotal
$self->{'plan'} && $other->{'plan'} ? : 26 6 32
$op eq '&' ? : 13 13 26
Condition100%3/3
$self->{"plan"} and $other->{"plan"}
!ll&&!rl&&r
5126
MC/DC100%2/2
$self->{"plan"} and $other->{"plan"}
$self->{"plan"} $other->{"plan"}
Truth table100%3/3
A and B
A= $self->{"plan"}B= $other->{"plan"}
ABresult
0X0
100
111
2159 32 ▶ return bless { code => $code, cols => _cols_union($self->{cols}, $other->{cols}),
Branch: 1 branch
BranchTrueFalseTotal
$plan ? : 26 6 32
2160 ($plan ? (plan => $plan) : ()) }, __PACKAGE__;
2161 }
2162  
2163 13 ▶ sub _not {
Subroutine _not: called CC=4 SCAR=13.9
2164 13 my ($self) = @_;
2165 13 my $c = $self->{code};
2166 13 ▶ die "col(): the operand of '!' is not a comparison (build it like !(col('x') > 0))\n"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $c eq "CODE" 1 12 13
2167 unless ref $c eq 'CODE';
2168 12 ▶ my $plan = $self->{plan} ? [ $P_NOT, $self->{plan} ] : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'plan'} ? : 8 4 12
2169 12 ▶ return bless { code => sub { $c->($_[0]) ? 0 : 1 }, cols => _cols_union($self->{cols}),
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 2 branches
BranchTrueFalseTotal
$plan ? : 8 4 12
&$c($_[0]) ? : 107 86 193
2170 ($plan ? (plan => $plan) : ()) }, __PACKAGE__;
2171 }
2172  
2173 # regex predicates. Perl cannot overload =~, so col('x') =~ /re/ can never
2174 # be intercepted; these methods give the same deferred, composable predicate
2175 # instead. The pattern is a qr// or a string (compiled with qr//); an undef
2176 # cell never matches (mirroring the string comparisons).
2177 # filter($df, col('id')->match(qr/^5iz/));
2178 # filter($df, col('id')->nomatch('^5iz')); # string pattern ok
2179 # filter($df, col('id')->match(qr/^5iz/) & (col('res') < 2.5));
2180 33 ▶ sub _match {
Subroutine _match: called CC=4 SCAR=13.9
2181 33 my ($self, $re, $want, $how) = @_;
2182 die "col(): ->$how must start from a bare column, e.g. col('x')->$how(qr/.../)\n"
2183 33 ▶ unless defined $self->{name};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"name"} 1 32 33
2184 32 ▶ die "col(): ->$how needs a pattern (a qr// or a string)\n" unless defined $re;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $re 2 30 32
2185 # an empty pattern (qr//, or the string '') matches every defined cell;
2186 # see _rx_for_match() for what perl before 5.18 makes of one
2187 30 ▶ my $qr = Stats::LikeR::_rx_for_match(ref $re eq 'Regexp' ? $re : qr/$re/);
Branch: 1 branch
BranchTrueFalseTotal
ref $re eq 'Regexp' ? : 21 9 30
2188 30 my $name = $self->{name};
2189 123 ▶ my $code = sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
2190 123 my $c = $_[0]{$name};
2191 123 ▶ return 0 unless defined $c;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $c 22 101 123
2192 101 ▶ return ( ($c =~ $qr) ? 1 : 0 ) == $want ? 1 : 0;
Branch: 2 branches
BranchTrueFalseTotal
($c =~ /$qr/ ? 1 : 0) == $want ? : 61 40 101
$c =~ /$qr/ ? : 74 27 101
2193 30 };
2194 30 return bless { code => $code, cols => [ $name ] }, __PACKAGE__;
2195 }
2196 25 ▶ sub match { _match($_[0], $_[1], 1, 'match') }
Subroutine match: called CC=1 SCAR=0.0
2197 8 ▶ sub nomatch { _match($_[0], $_[1], 0, 'nomatch') }
Subroutine nomatch: called CC=1 SCAR=0.0
2198 }
2199  
2200 # concat(@frames) / rbind(@frames) -- row-bind data frames (pandas concat
2201 # axis=0, R rbind). rbind is a true synonym (same subroutine).
2202 #
2203 # Every frame must be the same shape (AoA/AoH/HoA/HoH); a mix dies with a hint
2204 # to convert first (aoh2hoa, hoa2aoh, hoh2hoa, aoh2hoh). undef frames and
2205 # empty frames are skipped, and shape is taken from the first non-empty frame;
2206 # passing nothing usable dies. A NEW top-level frame of that shape is returned;
2207 # the original frames are never modified.
2208 #
2209 # AoA outer arrays concatenated in order (row arrayrefs reused by ref).
2210 # Ragged rows are kept as-is; a short row reads undef past its end.
2211 # AoH rows concatenated in order (row hashrefs reused by ref). The result is
2212 # the union of columns; a column absent from a given row reads undef,
2213 # matching this library's "missing key == undef" convention (dropna,
2214 # view, summary).
2215 # HoA union of columns (sorted for a deterministic layout). Each column is
2216 # the per-frame arrays joined in frame order; a frame lacking a column,
2217 # or a ragged short column, is padded with undef so every column ends up
2218 # the same length (= total rows).
2219 # HoH outer hashes merged in frame order (inner row hashrefs reused by ref).
2220 # Because a Perl hash cannot hold duplicate keys, a repeated row name is
2221 # made unique R-style (name, name.1, name.2, ...) and a single warning is
2222 # emitted noting that row names collided.
2223 497 ▶sub concat {
Subroutine concat: called CC=33 SCAR=35.0
Pod documented
2224 27 my @frames = grep { defined } @_;
2225 27 ▶ _usage_error('concat', 'needs at least one data frame') unless @frames;
Branch: 1 branch
BranchTrueFalseTotal
unless @frames 4 23 27
2226  
2227 23 for my $f (@frames) {
2228 44 ▶ _usage_error('concat', 'every frame must be an ARRAY or HASH ref')
Branch: 1 branch
BranchTrueFalseTotal
unless ref $f eq "ARRAY" or ref $f eq "HASH" 5 39 44
Condition100%3/3
ref $f eq "ARRAY" or ref $f eq "HASH"
l!l&&r!l&&!r
23165
MC/DC100%2/2
ref $f eq "ARRAY" or ref $f eq "HASH"
ref $f eq "ARRAY" ref $f eq "HASH"
Truth table100%3/3
A or B
A= ref $f eq "ARRAY"B= ref $f eq "HASH"
ABresult
1X1
011
000
2229 unless ref $f eq 'ARRAY' || ref $f eq 'HASH';
2230 }
2231 # An empty frame has no shape, so it is dropped here rather than skipped in
2232 # each loop below, which dereferenced a [] among HoAs as a hash and died.
2233 18 ▶ my @nonempty = grep { ref $_ eq 'ARRAY' ? scalar(@$_) : scalar(%$_) } @frames;
Branch: 1 branch
BranchTrueFalseTotal
ref $_ eq 'ARRAY' ? : 22 16 38
2234 18 ▶ unless (@nonempty) { # all frames empty
Branch: 1 branch
BranchTrueFalseTotal
unless (@nonempty) 2 16 18
2235 2 ▶ return ref $frames[0] eq 'ARRAY' ? [] : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $frames[0] eq 'ARRAY' ? : 1 1 2
2236 }
2237 16 @frames = @nonempty;
2238 # reference shape = first frame; all the others must agree
2239 16 my $ref_shape = _df_shape($frames[0], 'concat');
2240 16 for my $f (@frames) {
2241 29 my $s = _df_shape($f, 'concat');
2242 29 ▶ die "concat: cannot mix a $s frame with a $ref_shape frame; "
Branch: 1 branch
BranchTrueFalseTotal
if $s ne $ref_shape 2 27 29
2243 . "convert them to one shape first (aoh2hoa, hoa2aoh, hoh2hoa, aoh2hoh)\n"
2244 if $s ne $ref_shape;
2245 }
2246  
2247 14 ▶ if ($ref_shape eq 'AoA') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref_shape eq "AoA") 3 11 14
2248 3 my @out;
2249 3 for my $f (@frames) {
2250 6 for my $row (@$f) {
2251 8 ▶ die "concat: AoA row is not an ARRAY ref\n" unless ref $row eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $row eq "ARRAY" 0 8 8
2252 8 push @out, $row;
2253 }
2254 }
2255 3 return \@out;
2256 }
2257 11 ▶ if ($ref_shape eq 'AoH') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref_shape eq "AoH") 4 7 11
2258 4 my @out;
2259 4 for my $f (@frames) {
2260 8 for my $row (@$f) {
2261 8 ▶ die "concat: AoH row is not a HASH ref\n" unless ref $row eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $row eq "HASH" 0 8 8
2262 8 push @out, $row;
2263 }
2264 }
2265 4 return \@out;
2266 }
2267 7 ▶ if ($ref_shape eq 'HoA') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref_shape eq "HoA") 5 2 7
2268 5 my (@cols, %seen); # union of columns, sorted
2269 5 for my $f (@frames) { $seen{$_} = 1 for keys %$f }
2270 5 @cols = sort keys %seen;
2271 5 my %out = map { $_ => [] } @cols;
2272 5 for my $f (@frames) {
2273 7 my $n = 0;
2274 7 for my $c (keys %$f) {
2275 11 ▶ $n = @{ $f->{$c} } if ref $f->{$c} eq 'ARRAY' && @{ $f->{$c} } > $n;
Branch: 1 branch
BranchTrueFalseTotal
if ref $f->{$c} eq "ARRAY" and @{$f->{$c};} > $n 8 3 11
Condition66%2/3
ref $f->{$c} eq "ARRAY" and @{$f->{$c};} > $n
!ll&&!rl&&r
038
MC/DC50%1/2
ref $f->{$c} eq "ARRAY" and @{$f->{$c};} > $n
ref $f->{$c} eq "ARRAY" @{$f->{$c};} > $n
Truth table66%2/3
A and B
A= ref $f->{$c} eq "ARRAY"B= @{$f->{$c};} > $n
ABresult
0X0
100
111
2276 }
2277 7 for my $c (@cols) {
2278 13 ▶ if (ref $f->{$c} eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $f->{$c} eq 'ARRAY') { } 11 2 13
2279 11 push @{ $out{$c} }, @{ $f->{$c} };
2280 1 push @{ $out{$c} }, (undef) x ($n - @{ $f->{$c} })
2281 11 ▶ if @{ $f->{$c} } < $n; # ragged short column
Branch: 1 branch
BranchTrueFalseTotal
if @{$f->{$c};} < $n 1 10 11
2282 } else {
2283 2 push @{ $out{$c} }, (undef) x $n; # column absent in this frame
2284 }
2285 }
2286 }
2287 5 return \%out;
2288 }
2289 # HoH
2290 2 my (%out, $collided);
2291 2 for my $f (@frames) {
2292 4 for my $rk (sort keys %$f) {
2293 die "concat: HoH row '$rk' is not a HASH ref\n"
2294 5 ▶ unless ref $f->{$rk} eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $f->{$rk} eq "HASH" 0 5 5
2295 5 my $label = $rk;
2296 5 my $j = 0;
2297 5 ▶ while (exists $out{$label}) { $collided = 1; $label = $rk . '.' . (++$j) }
Branch: 1 branch
BranchTrueFalseTotal
if (exists $out{$label}) 1 5 6
2298 5 $out{$label} = $f->{$rk}; # reuse the row ref
2299 }
2300 }
2301 2 ▶ warn "concat: duplicate HoH row name(s) made unique with a .N suffix\n"
Branch: 1 branch
BranchTrueFalseTotal
if $collided 1 1 2
2302 if $collided;
2303 2 return \%out;
2304 }
2305 497 ▶{ no warnings 'once'; *rbind = \&concat; } # true synonym
Subroutine BEGIN: called CC=1 SCAR=0.0
2306 #
2307 # dropna($df, cols => \@cols, how => 'any'|'all') # NA mode
2308 # dropna($df, rows => \@rows) # literal deletion
2309 #
2310 # $df may be:
2311 # AoH [ { A=>.., B=>.. }, ... ] rows are 0-based indices
2312 # HoA { A=>[..], B=>[..] } rows are 0-based indices
2313 # HoH { r1=>{ A=>.. }, r2=>{ .. } } rows are the outer keys
2314 #
2315 # cols mode (NA): inspect the named columns and drop the rows that are undef
2316 # in them. how => 'any' (default) drops a row when any named column is undef;
2317 # how => 'all' drops it only when every named column is undef. Columns that
2318 # are not named are untouched but stay aligned (their cell at a dropped index
2319 # goes too). A missing key counts as undef.
2320 #
2321 # rows mode: delete exactly the listed rows (indices for AoH/HoA, keys for HoH);
2322 # no NA check. Indices/keys that aren't present are ignored.
2323 #
2324 # Returns a NEW top-level data frame; the original is never modified. For HoA
2325 # the column arrays are rebuilt (cell values copied); for AoH/HoH the surviving
2326 # row references are reused, not deep-copied (dropna never mutates a row).
2327 #
2328 54 ▶sub dropna {
Subroutine dropna: called CC=50 SCAR=39.2
Pod documented
2329 54 my $df = shift;
2330 54 ▶ _usage_error('dropna', 'first argument is undefined') unless defined $df;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $df 2 52 54
2331 52 ▶ _usage_error('dropna', 'first argument must be a data frame (HoA/HoH hashref or AoH arrayref)')
Branch: 1 branch
BranchTrueFalseTotal
unless ref $df 4 48 52
2332 unless ref $df;
2333 48 ▶ _usage_error('dropna', 'arguments after the data frame must be name => value pairs')
Branch: 1 branch
BranchTrueFalseTotal
if @_ % 2 1 47 48
2334 if @_ % 2;
2335 47 my %arg = @_;
2336  
2337 47 my %known = ( cols => 1, rows => 1, how => 1 );
2338 47 my @bad = sort grep { !$known{$_} } keys %arg;
2339 47 ▶ _usage_error('dropna', "unknown argument(s): @bad") if @bad;
Branch: 1 branch
BranchTrueFalseTotal
if @bad 3 44 47
2340  
2341 44 my $have_cols = exists $arg{cols};
2342 44 my $have_rows = exists $arg{rows};
2343 44 ▶ _usage_error('dropna', "pass exactly one of 'cols' or 'rows'")
Branch: 1 branch
BranchTrueFalseTotal
unless $have_cols xor $have_rows 7 37 44
Condition100%4/4
$have_cols xor $have_rows
l&&rl&&!r!l&&r!l&&!r
32894
MC/DC100%2/2
$have_cols xor $have_rows
$have_cols $have_rows
Truth table100%4/4
A xor B
A= $have_colsB= $have_rows
ABresult
110
101
011
000
2344 unless $have_cols xor $have_rows;
2345  
2346 37 ▶ my $sel = $have_cols ? $arg{cols} : $arg{rows};
Branch: 1 branch
BranchTrueFalseTotal
$have_cols ? : 28 9 37
2347 37 ▶ _usage_error('dropna', "'" . ($have_cols ? 'cols' : 'rows') . "' must be an arrayref")
Branch: 2 branches
BranchTrueFalseTotal
unless ref $sel eq "ARRAY" 2 35 37
$have_cols ? : 2 0 2
2348 unless ref $sel eq 'ARRAY';
2349  
2350 35 ▶ my $how = defined $arg{how} ? lc $arg{how} : 'any';
Branch: 1 branch
BranchTrueFalseTotal
defined $arg{'how'} ? : 8 27 35
2351 35 ▶ _usage_error('dropna', "'how' must be 'any' or 'all'")
Branch: 1 branch
BranchTrueFalseTotal
unless $how eq "any" or $how eq "all" 2 33 35
Condition100%3/3
$how eq "any" or $how eq "all"
l!l&&r!l&&!r
3032
MC/DC100%2/2
$how eq "any" or $how eq "all"
$how eq "any" $how eq "all"
Truth table100%3/3
A or B
A= $how eq "any"B= $how eq "all"
ABresult
1X1
011
000
2352 unless $how eq 'any' or $how eq 'all';
2353  
2354 33 my $r = ref $df;
2355  
2356 #AoH
2357 33 ▶ if ($r eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if ($r eq "ARRAY") 12 21 33
2358 12 ▶ if ($have_rows) { # literal index deletion
Branch: 1 branch
BranchTrueFalseTotal
if ($have_rows) 2 10 12
2359 2 my %drop = map { $_ => 1 } @$sel;
2360 2 return [ map { $df->[$_] } grep { !$drop{$_} } 0 .. $#$df ];
2361 }
2362 10 my @cols = @$sel;
2363 10 ▶ return [ @$df ] unless @cols; # nothing to check -> keep all
Branch: 1 branch
BranchTrueFalseTotal
unless @cols 1 9 10
2364 9 ▶ return [] unless @$df; # empty frame -> empty result
Branch: 1 branch
BranchTrueFalseTotal
unless @$df 2 7 9
2365 7 my %seen;
2366 7 for my $row (@$df) {
2367 18 ▶ next unless ref $row eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $row eq "HASH" 0 18 18
2368 18 $seen{$_} = 1 for keys %$row;
2369 }
2370 7 for my $c (@cols) {
2371 11 ▶ die "dropna: column '$c' not found\n" unless $seen{$c};
Branch: 1 branch
BranchTrueFalseTotal
unless $seen{$c} 1 10 11
2372 }
2373 6 my @keep;
2374 6 for my $i (0 .. $#$df) {
2375 17 my $row = $df->[$i];
2376 my $nundef = (ref $row eq 'HASH')
2377 17 ▶ ? (grep { !defined $row->{$_} } @cols)
Branch: 1 branch
BranchTrueFalseTotal
ref $row eq 'HASH' ? : 17 0 17
2378 : @cols; # malformed row counts as all-NA
2379 17 ▶ my $drop = $how eq 'any' ? $nundef > 0 : $nundef == @cols;
Branch: 1 branch
BranchTrueFalseTotal
$how eq 'any' ? : 12 5 17
2380 17 ▶ push @keep, $i unless $drop;
Branch: 1 branch
BranchTrueFalseTotal
unless $drop 10 7 17
2381 }
2382 6 return [ map { $df->[$_] } @keep ];
2383 }
2384  
2385 #HoA vs HoH
2386 21 ▶ if ($r eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if ($r eq "HASH") 21 0 21
2387 21 my ($saw_arr, $saw_hash) = (0, 0);
2388 21 for my $v (values %$df) {
2389 36 ▶ next unless ref $v;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $v 0 36 36
2390 36 ▶ $saw_arr++ if ref $v eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $v eq "ARRAY" 23 13 36
2391 36 ▶ $saw_hash++ if ref $v eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $v eq "HASH" 13 23 36
2392 }
2393 21 ▶ die "dropna: hashref mixes array and hash values (ambiguous HoA/HoH)\n"
Branch: 1 branch
BranchTrueFalseTotal
if $saw_arr and $saw_hash 1 20 21
Condition100%3/3
$saw_arr and $saw_hash
!ll&&!rl&&r
6141
MC/DC100%2/2
$saw_arr and $saw_hash
$saw_arr $saw_hash
Truth table100%3/3
A and B
A= $saw_arrB= $saw_hash
ABresult
0X0
100
111
2394 if $saw_arr and $saw_hash;
2395  
2396 #HoH
2397 20 ▶ if ($saw_hash) {
Branch: 1 branch
BranchTrueFalseTotal
if ($saw_hash) 5 15 20
2398 5 ▶ if ($have_rows) { # delete row keys
Branch: 1 branch
BranchTrueFalseTotal
if ($have_rows) 2 3 5
2399 2 my %drop = map { $_ => 1 } @$sel;
2400 2 return { map { $_ => $df->{$_} } grep { !$drop{$_} } keys %$df };
2401 }
2402 3 my @cols = @$sel;
2403 3 ▶ return { %$df } unless @cols;
Branch: 1 branch
BranchTrueFalseTotal
unless @cols 1 2 3
2404 2 my %out;
2405 2 for my $rk (keys %$df) {
2406 5 my $row = $df->{$rk};
2407 my $nundef = (ref $row eq 'HASH')
2408 5 ▶ ? (grep { !defined $row->{$_} } @cols)
Branch: 1 branch
BranchTrueFalseTotal
ref $row eq 'HASH' ? : 5 0 5
2409 : @cols;
2410 5 ▶ my $drop = $how eq 'any' ? $nundef > 0 : $nundef == @cols;
Branch: 1 branch
BranchTrueFalseTotal
$how eq 'any' ? : 5 0 5
2411 5 ▶ $out{$rk} = $row unless $drop;
Branch: 1 branch
BranchTrueFalseTotal
unless $drop 3 2 5
2412 }
2413 2 return \%out;
2414 }
2415  
2416 #HoA (also the empty-hash fallthrough)
2417 15 my $n = 0;
2418 15 for my $v (values %$df) {
2419 22 ▶ $n = @$v if ref $v eq 'ARRAY' and @$v > $n;
Branch: 1 branch
BranchTrueFalseTotal
if ref $v eq "ARRAY" and @$v > $n 14 8 22
Condition66%2/3
ref $v eq "ARRAY" and @$v > $n
!ll&&!rl&&r
0814
MC/DC50%1/2
ref $v eq "ARRAY" and @$v > $n
ref $v eq "ARRAY" @$v > $n
Truth table66%2/3
A and B
A= ref $v eq "ARRAY"B= @$v > $n
ABresult
0X0
100
111
2420 }
2421 15 ▶ if ($have_rows) { # delete indices
Branch: 1 branch
BranchTrueFalseTotal
if ($have_rows) 5 10 15
2422 5 my %drop = map { $_ => 1 } @$sel;
2423 5 my @keep = grep { !$drop{$_} } 0 .. $n - 1;
2424 5 return { map { $_ => [ @{ $df->{$_} }[@keep] ] } keys %$df };
2425 }
2426 10 my @cols = @$sel;
2427 10 ▶ return { map { $_ => [ @{ $df->{$_} } ] } keys %$df } unless @cols;
Branch: 1 branch
BranchTrueFalseTotal
unless @cols 2 8 10
2428 8 for my $c (@cols) {
2429 11 ▶ die "dropna: column '$c' not found\n" unless exists $df->{$c};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $df->{$c} 2 9 11
2430 }
2431 6 my @keep;
2432 6 for my $i (0 .. $n - 1) {
2433 17 my $nundef = grep { !defined $df->{$_}[$i] } @cols;
2434 17 ▶ my $drop = $how eq 'any' ? $nundef > 0 : $nundef == @cols;
Branch: 1 branch
BranchTrueFalseTotal
$how eq 'any' ? : 14 3 17
2435 17 ▶ push @keep, $i unless $drop;
Branch: 1 branch
BranchTrueFalseTotal
unless $drop 10 7 17
2436 }
2437 6 return { map { $_ => [ @{ $df->{$_} }[@keep] ] } keys %$df };
2438 }
2439  
2440 0 _usage_error('dropna', 'data frame must be an arrayref (AoH) or hashref (HoA/HoH)');
2441 }
2442  
2443 # drop_duplicates($df, subset => $col | \@cols, keep => 'first' | 'last' | 0)
2444 #
2445 # Remove duplicate rows, loosely modeled on pandas' DataFrame.drop_duplicates.
2446 # Works on the three positional/columnar shapes -- AoA, AoH, HoA -- but NOT
2447 # HoH (its rows are labeled, so "drop_duplicates" has no natural meaning; call
2448 # hoh2aoh/hoh2hoa first). Two rows are duplicates when their cells are equal
2449 # in every subset column; comparison is by stringified value with a distinct
2450 # undef (NA), exactly the key semantics merge() uses, so 1 and "1.0" differ.
2451 #
2452 # subset scalar or arrayref of the columns that define a row's identity;
2453 # default every column. For AoA these are 0-based integer positions
2454 # (default 0 .. widest-row-1); for AoH/HoA they are column names
2455 # (AoH default: the sorted union of row keys; HoA: the sorted keys).
2456 # keep which occurrence to keep: 'first' (default) keeps the earliest,
2457 # 'last' the latest, 0 (or 'none') drops every row that has a dup.
2458 #
2459 # Row order is preserved (first-seen positions for the survivors). Returns a
2460 # NEW top-level frame of the same family; the original is never modified. What
2461 # survives is shared, not deep-copied: AoA/AoH reuse the surviving row refs,
2462 # HoA builds new column arrays over the same cell SVs. Assigning through a
2463 # survivor therefore reaches the input's cell.
2464 84 ▶sub drop_duplicates {
Subroutine drop_duplicates: called CC=30 SCAR=34.1
Pod documented
2465 84 my $df = _untied(shift);
2466 84 ▶ _usage_error('drop_duplicates', 'undefined data in first position') unless defined $df;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $df 2 82 84
2467 82 ▶ _usage_error('drop_duplicates', 'arguments after the data frame must be name => value pairs')
Branch: 1 branch
BranchTrueFalseTotal
if @_ % 2 0 82 82
2468 if @_ % 2;
2469 82 my %arg = @_;
2470 82 my %known = ( subset => 1, keep => 1 );
2471 82 my @bad = sort grep { !$known{$_} } keys %arg;
2472 82 ▶ _usage_error('drop_duplicates', "unknown argument(s): @bad") if @bad;
Branch: 1 branch
BranchTrueFalseTotal
if @bad 1 81 82
2473  
2474 81 my $shape = _df_shape($df, 'drop_duplicates');
2475 78 ▶ _usage_error('drop_duplicates', 'an HoH data frame is not supported (convert to AoH/HoA/AoA first)')
Branch: 1 branch
BranchTrueFalseTotal
if $shape eq "HoH" 1 77 78
2476 if $shape eq 'HoH';
2477  
2478 # keep -> code: 1 = first, -1 = last, 0 = drop every duplicate
2479 77 ▶ my $keep = exists $arg{keep} ? $arg{keep} : 'first';
Branch: 1 branch
BranchTrueFalseTotal
exists $arg{'keep'} ? : 37 40 77
2480 77 ▶ _usage_error('drop_duplicates', "'keep' is undefined (use 'first', 'last', or 0)")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $keep 0 77 77
2481 unless defined $keep;
2482 77 my $kc;
2483 77 ▶ if ($keep eq 'first') { $kc = 1 }
Branch: 3 branches
BranchTrueFalseTotal
if ($keep eq 'first') { } 50 27 77
elsif ($keep eq 'last') { } 13 14 27
elsif ($keep eq 'none' or $keep eq '' or &looks_like_number($keep) and $keep == 0) { } 12 2 14
2484 13 elsif ($keep eq 'last') { $kc = -1 }
2485 12 ▶ elsif ($keep eq 'none' || $keep eq '' || (looks_like_number($keep) && $keep == 0)) { $kc = 0 }
Condition77%7/9
$keep eq "none" or $keep eq "" or &looks_like_number($keep) and $keep == 0
l!l&&r!l&&!r
1112
$keep eq "none" or $keep eq ""
l!l&&r!l&&!r
1013
&looks_like_number($keep) and $keep == 0
!ll&&!rl&&r
2011
MC/DC50%2/4
$keep eq "none" or $keep eq "" or &looks_like_number($keep) and $keep == 0
$keep eq "none" $keep eq "" &looks_like_number($keep) $keep == 0
Truth table60%3/5
A or B or C and D
A= $keep eq "none"B= $keep eq ""C= &looks_like_number($keep)D= $keep == 0
ABCDresult
1XXX1
01XX1
00111
000X0
00100
2486 2 else { _usage_error('drop_duplicates', "'keep' must be 'first', 'last', or 0 (got '$keep')") }
2487  
2488 # subset -> ordered column list
2489 75 my @sub;
2490 75 ▶ if (exists $arg{subset} && defined $arg{subset}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $arg{"subset"} and defined $arg{"subset"}) 17 58 75
Condition66%2/3
exists $arg{"subset"} and defined $arg{"subset"}
!ll&&!rl&&r
58017
MC/DC50%1/2
exists $arg{"subset"} and defined $arg{"subset"}
exists $arg{"subset"} defined $arg{"subset"}
Truth table66%2/3
A and B
A= exists $arg{"subset"}B= defined $arg{"subset"}
ABresult
0X0
100
111
2491 17 my $s = $arg{subset};
2492 17 ▶ if (ref $s eq 'ARRAY') { @sub = @$s }
Branch: 2 branches
BranchTrueFalseTotal
if (ref $s eq 'ARRAY') { } 3 14 17
elsif (not ref $s) { } 14 0 14
2493 14 elsif (!ref $s) { @sub = ($s) }
2494 0 else { _usage_error('drop_duplicates', "'subset' must be a column or an arrayref of columns") }
2495 17 ▶ _usage_error('drop_duplicates', "'subset' is empty") unless @sub;
Branch: 1 branch
BranchTrueFalseTotal
unless @sub 1 16 17
2496 16 my %seen;
2497 16 for my $c (@sub) {
2498 18 ▶ _usage_error('drop_duplicates', "undefined column in 'subset'") unless defined $c;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $c 0 18 18
2499 18 ▶ _usage_error('drop_duplicates', "duplicate column '$c' in 'subset'") if $seen{$c}++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$c}++ 1 17 18
2500 }
2501 }
2502  
2503 73 ▶ if ($shape eq 'AoA') {
Branch: 1 branch
BranchTrueFalseTotal
if ($shape eq "AoA") 26 47 73
2504 26 ▶ if (@sub) { _aoa_int_cols('drop_duplicates', $df, @sub) }
Branch: 1 branch
BranchTrueFalseTotal
if (@sub) { } 6 20 26
2505 20 else { @sub = (0 .. _aoa_width($df) - 1) }
2506 24 return _drop_dups_core($df, 3, [ @sub ], $kc);
2507 }
2508 47 ▶ if ($shape eq 'AoH') {
Branch: 1 branch
BranchTrueFalseTotal
if ($shape eq "AoH") 25 22 47
2509 # same union _present_keys builds, but scanned in C: on a large AoH the
2510 # pure-Perl walk over every key of every row cost more than the dedup
2511 25 my %present = map { $_ => 1 } @{ _aoh_key_union($df) };
2512 25 ▶ if (@sub) {
Branch: 1 branch
BranchTrueFalseTotal
if (@sub) { } 3 22 25
2513 3 for my $c (@sub) {
2514 3 ▶ die "drop_duplicates: column '$c' not found\n" unless $present{$c};
Branch: 1 branch
BranchTrueFalseTotal
unless $present{$c} 1 2 3
2515 }
2516 } else {
2517 22 @sub = sort keys %present;
2518 }
2519 24 return _drop_dups_core($df, 1, [ @sub ], $kc);
2520 }
2521 # HoA
2522 22 ▶ if (@sub) {
Branch: 1 branch
BranchTrueFalseTotal
if (@sub) { } 6 16 22
2523 6 for my $c (@sub) {
2524 6 ▶ die "drop_duplicates: column '$c' not found\n" unless exists $df->{$c};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $df->{$c} 1 5 6
2525 }
2526 } else {
2527 16 @sub = sort keys %$df;
2528 }
2529 21 return _drop_dups_core($df, 4, [ @sub ], $kc);
2530 }
2531 # Count rows across Stats::LikeR frame forms: AoH, AoA, HoA, HoH.
2532  
2533 # Count columns across Stats::LikeR frame forms: AoH, AoA, HoA, HoH
2534 # (plain vector => 1 column). Uses die, not croak. reftype => blessed frames ok.
2535 56 ▶sub ncol {
Subroutine ncol: called CC=36 SCAR=35.9
Pod documented
2536 56 my ($data) = @_;
2537 56 my $type = reftype $data;
2538 56 ▶ _usage_error('ncol', 'expected an ARRAY or HASH ref (got '
Branch: 2 branches
BranchTrueFalseTotal
unless defined $type and $type eq "ARRAY" || $type eq "HASH" 10 46 56
defined $data ? : 7 3 10
Condition100%8/8
defined $type and $type eq "ARRAY" || $type eq "HASH"
!ll&&!rl&&r
7346
$type eq "ARRAY" or $type eq "HASH"
l!l&&r!l&&!r
18283
ref $data or "non-ref scalar"
l!l
34
MC/DC100%3/3
defined $type and $type eq "ARRAY" || $type eq "HASH"
defined $type $type eq "ARRAY" $type eq "HASH"
MC/DC100%1/1
ref $data or "non-ref scalar"
ref $data
Truth table100%4/4
A and B or C
A= defined $typeB= $type eq "ARRAY"C= $type eq "HASH"
ABCresult
0XX0
1000
11X1
1011
Truth table100%2/2
A
A= ref $data
Aresult
11
00
2539 . (defined $data ? (ref($data) || 'non-ref scalar') : 'undef') . ')')
2540 unless defined $type && ($type eq 'ARRAY' || $type eq 'HASH');
2541  
2542 46 ▶ if ($type eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "ARRAY") 18 28 46
2543 18 ▶ return 0 unless @$data; # empty frame
Branch: 1 branch
BranchTrueFalseTotal
unless @$data 1 17 18
2544 17 my $r0 = reftype $data->[0]; # element 0 decides the form
2545  
2546 # AoH: columns = keys per row; every row a hash ref of equal key count
2547 17 ▶ if (defined $r0 && $r0 eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $r0 and $r0 eq "HASH") 7 10 17
Condition100%3/3
defined $r0 and $r0 eq "HASH"
!ll&&!rl&&r
287
MC/DC100%2/2
defined $r0 and $r0 eq "HASH"
defined $r0 $r0 eq "HASH"
Truth table100%3/3
A and B
A= defined $r0B= $r0 eq "HASH"
ABresult
0X0
100
111
2548 7 my $ncol = scalar keys %{ $data->[0] };
2549 7 for my $i (1 .. $#$data) {
2550 6 my $row = $data->[$i];
2551 6 ▶ die "ncol: AoH row $i is not a hash ref\n"
Branch: 1 branch
BranchTrueFalseTotal
unless defined builtin::reftype($row) and builtin::reftype($row) eq "HASH" 1 5 6
Condition66%2/3
defined builtin::reftype($row) and builtin::reftype($row) eq "HASH"
!ll&&!rl&&r
015
MC/DC50%1/2
defined builtin::reftype($row) and builtin::reftype($row) eq "HASH"
defined builtin::reftype($row) builtin::reftype($row) eq "HASH"
Truth table66%2/3
A and B
A= defined builtin::reftype($row)B= builtin::reftype($row) eq "HASH"
ABresult
0X0
100
111
2552 unless defined(reftype $row) && reftype($row) eq 'HASH';
2553 5 my $k = scalar keys %$row;
2554 5 ▶ die "ncol: ragged AoH — row $i has $k columns, but row 0 has $ncol\n"
Branch: 1 branch
BranchTrueFalseTotal
if $k != $ncol 1 4 5
2555 if $k != $ncol;
2556 }
2557 5 return $ncol;
2558 }
2559  
2560 # AoA: columns = row length; every row an array ref of equal length
2561 10 ▶ if (defined $r0 && $r0 eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $r0 and $r0 eq "ARRAY") 7 3 10
Condition100%3/3
defined $r0 and $r0 eq "ARRAY"
!ll&&!rl&&r
217
MC/DC100%2/2
defined $r0 and $r0 eq "ARRAY"
defined $r0 $r0 eq "ARRAY"
Truth table100%3/3
A and B
A= defined $r0B= $r0 eq "ARRAY"
ABresult
0X0
100
111
2562 7 my $ncol = scalar @{ $data->[0] };
2563 7 for my $i (1 .. $#$data) {
2564 5 my $row = $data->[$i];
2565 5 ▶ die "ncol: AoA row $i is not an array ref\n"
Branch: 1 branch
BranchTrueFalseTotal
unless defined builtin::reftype($row) and builtin::reftype($row) eq "ARRAY" 1 4 5
Condition66%2/3
defined builtin::reftype($row) and builtin::reftype($row) eq "ARRAY"
!ll&&!rl&&r
014
MC/DC50%1/2
defined builtin::reftype($row) and builtin::reftype($row) eq "ARRAY"
defined builtin::reftype($row) builtin::reftype($row) eq "ARRAY"
Truth table66%2/3
A and B
A= defined builtin::reftype($row)B= builtin::reftype($row) eq "ARRAY"
ABresult
0X0
100
111
2566 unless defined(reftype $row) && reftype($row) eq 'ARRAY';
2567 4 my $len = scalar @$row;
2568 4 ▶ die "ncol: ragged AoA — row $i has $len columns, but row 0 has $ncol\n"
Branch: 1 branch
BranchTrueFalseTotal
if $len != $ncol 1 3 4
2569 if $len != $ncol;
2570 }
2571 5 return $ncol;
2572 }
2573  
2574 3 ▶ return 1 unless defined $r0; # plain 1-D vector: one column
Branch: 1 branch
BranchTrueFalseTotal
unless defined $r0 2 1 3
2575  
2576 1 die "ncol: array element 0 is a $r0 ref; expected HASH (AoH), ARRAY (AoA), or plain scalars (vector)\n";
2577 }
2578  
2579 # HASH: HoA (keys are columns) or HoH (keys are rows)
2580 28 ▶ return 0 unless %$data;
Branch: 1 branch
BranchTrueFalseTotal
unless %$data 1 27 28
2581  
2582 27 my $probe; # first defined value decides the form
2583 27 foreach my $k (keys %$data) {
2584 28 ▶ next unless defined $data->{$k};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data->{$k} 2 26 28
2585 26 $probe = $data->{$k};
2586 26 last;
2587 }
2588 27 my $vtype = reftype $probe;
2589  
2590 # HoA: keys ARE the columns. Validate values are array refs so a malformed
2591 # frame dies deterministically rather than depending on which key `probe` hit.
2592 27 ▶ if (defined $vtype && $vtype eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $vtype and $vtype eq "ARRAY") 20 7 27
Condition100%3/3
defined $vtype and $vtype eq "ARRAY"
!ll&&!rl&&r
2520
MC/DC100%2/2
defined $vtype and $vtype eq "ARRAY"
defined $vtype $vtype eq "ARRAY"
Truth table100%3/3
A and B
A= defined $vtypeB= $vtype eq "ARRAY"
ABresult
0X0
100
111
2593 20 foreach my $col (keys %$data) {
2594 die "ncol: HoA column '$col' is not an array ref\n"
2595 26 ▶ unless defined(reftype $data->{$col}) && reftype($data->{$col}) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless defined builtin::reftype($data->{$col}) and builtin::reftype($data->{$col}) eq "ARRAY" 1 25 26
Condition66%2/3
defined builtin::reftype($data->{$col}) and builtin::reftype($data->{$col}) eq "ARRAY"
!ll&&!rl&&r
1025
MC/DC50%1/2
defined builtin::reftype($data->{$col}) and builtin::reftype($data->{$col}) eq "ARRAY"
defined builtin::reftype($data->{$col}) builtin::reftype($data->{$col}) eq "ARRAY"
Truth table66%2/3
A and B
A= defined builtin::reftype($data->{$col})B= builtin::reftype($data->{$col}) eq "ARRAY"
ABresult
0X0
100
111
2596 }
2597 19 return scalar keys %$data;
2598 }
2599  
2600 # HoH: keys are rows; columns = keys of a row hash, consistent across rows
2601 7 ▶ if (defined $vtype && $vtype eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $vtype and $vtype eq "HASH") 5 2 7
Condition66%2/3
defined $vtype and $vtype eq "HASH"
!ll&&!rl&&r
205
MC/DC50%1/2
defined $vtype and $vtype eq "HASH"
defined $vtype $vtype eq "HASH"
Truth table66%2/3
A and B
A= defined $vtypeB= $vtype eq "HASH"
ABresult
0X0
100
111
2602 5 my ($ncol, $ref_row);
2603 5 foreach my $row_key (keys %$data) {
2604 9 my $row = $data->{$row_key};
2605 9 ▶ die "ncol: HoH row '$row_key' is not a hash ref\n"
Branch: 1 branch
BranchTrueFalseTotal
unless defined builtin::reftype($row) and builtin::reftype($row) eq "HASH" 1 8 9
Condition66%2/3
defined builtin::reftype($row) and builtin::reftype($row) eq "HASH"
!ll&&!rl&&r
108
MC/DC50%1/2
defined builtin::reftype($row) and builtin::reftype($row) eq "HASH"
defined builtin::reftype($row) builtin::reftype($row) eq "HASH"
Truth table66%2/3
A and B
A= defined builtin::reftype($row)B= builtin::reftype($row) eq "HASH"
ABresult
0X0
100
111
2606 unless defined(reftype $row) && reftype($row) eq 'HASH';
2607 8 my $k = scalar keys %$row;
2608 8 ▶ if (not defined $ncol) { $ncol = $k; $ref_row = $row_key }
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $ncol) { } 5 3 8
elsif ($k != $ncol) { } 1 2 3
2609 elsif ($k != $ncol) {
2610 1 die "ncol: ragged HoH — row '$row_key' has $k columns, but '$ref_row' has $ncol\n";
2611 }
2612 }
2613 3 return $ncol;
2614 }
2615  
2616 2 die "ncol: HASH values are neither ARRAY refs (HoA) nor HASH refs (HoH)\n";
2617 }
2618  
2619 31 ▶sub nrow {
Subroutine nrow: called CC=17 SCAR=28.4
Pod documented
2620 31 my ($data) = @_;
2621 31 my $type = reftype $data;
2622 31 ▶ _usage_error('nrow', 'expected an ARRAY or HASH ref (got '
Branch: 2 branches
BranchTrueFalseTotal
unless defined $type 7 24 31
defined $data ? : 5 2 7
Condition50%1/2
ref $data or "non-ref scalar"
l!l
05
MC/DC0%0/1
ref $data or "non-ref scalar"
ref $data
Truth table50%1/2
A
A= ref $data
Aresult
11
00
2623 . (defined $data ? (ref($data) || 'non-ref scalar') : 'undef') . ')')
2624 unless defined $type;
2625  
2626 # AoH / AoA (and a plain vector): one top-level element per row.
2627 24 ▶ return scalar @$data if $type eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "ARRAY" 8 16 24
2628  
2629 # HASH: HoA (keys are columns) or HoH (keys are rows).
2630 16 ▶ return 0 unless %$data; # empty frame, either form
Branch: 1 branch
BranchTrueFalseTotal
unless %$data 1 12 13
2631  
2632 12 my $probe; # first defined value decides the form
2633 12 foreach my $k (keys %$data) {
2634 14 ▶ next unless defined $data->{$k};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data->{$k} 3 11 14
2635 11 $probe = $data->{$k};
2636 11 last;
2637 }
2638 12 my $vtype = reftype $probe;
2639  
2640 12 ▶ return scalar keys %$data # HoH: one key per row
Branch: 1 branch
BranchTrueFalseTotal
if defined $vtype and $vtype eq "HASH" 4 8 12
Condition100%3/3
defined $vtype and $vtype eq "HASH"
!ll&&!rl&&r
264
MC/DC100%2/2
defined $vtype and $vtype eq "HASH"
defined $vtype $vtype eq "HASH"
Truth table100%3/3
A and B
A= defined $vtypeB= $vtype eq "HASH"
ABresult
0X0
100
111
2641 if defined $vtype && $vtype eq 'HASH';
2642  
2643 8 ▶ if (defined $vtype && $vtype eq 'ARRAY') { # HoA: rows = common column length
Branch: 1 branch
BranchTrueFalseTotal
if (defined $vtype and $vtype eq "ARRAY") 6 2 8
Condition66%2/3
defined $vtype and $vtype eq "ARRAY"
!ll&&!rl&&r
206
MC/DC50%1/2
defined $vtype and $vtype eq "ARRAY"
defined $vtype $vtype eq "ARRAY"
Truth table66%2/3
A and B
A= defined $vtypeB= $vtype eq "ARRAY"
ABresult
0X0
100
111
2644 6 my ($n, $ref_col);
2645 6 foreach my $col (keys %$data) { # verify columns agree, so a ragged
2646 10 my $vec = $data->{$col}; # frame can't return a silently-wrong
2647 10 ▶ die "nrow: HoA column '$col' is not an array ref" # (and, given hash
Branch: 1 branch
BranchTrueFalseTotal
unless defined builtin::reftype($vec) and builtin::reftype($vec) eq "ARRAY" 1 9 10
Condition66%2/3
defined builtin::reftype($vec) and builtin::reftype($vec) eq "ARRAY"
!ll&&!rl&&r
109
MC/DC50%1/2
defined builtin::reftype($vec) and builtin::reftype($vec) eq "ARRAY"
defined builtin::reftype($vec) builtin::reftype($vec) eq "ARRAY"
Truth table66%2/3
A and B
A= defined builtin::reftype($vec)B= builtin::reftype($vec) eq "ARRAY"
ABresult
0X0
100
111
2648 unless defined(reftype $vec) && reftype($vec) eq 'ARRAY'; # ordering,
2649 9 my $len = scalar @$vec; # nondeterministic) count
2650 9 ▶ if (not defined $n) { $n = $len; $ref_col = $col }
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $n) { } 5 4 9
elsif ($len != $n) { } 1 3 4
2651 elsif ($len != $n) {
2652 1 die "nrow: ragged HoA — column '$col' has $len rows, but '$ref_col' has $n";
2653 }
2654 }
2655 4 return $n;
2656 }
2657 2 die 'nrow: HASH values are neither ARRAY refs (HoA) nor HASH refs (HoH)';
2658 }
2659 35 ▶sub qcut {
Subroutine qcut: called CC=33 SCAR=35.0
Pod documented
2660 35 my ($data, $q, %opt) = @_;
2661  
2662 35 ▶ _usage_error('qcut', 'first argument must be an ARRAY reference')
Branch: 1 branch
BranchTrueFalseTotal
unless ref $data eq "ARRAY" 5 30 35
2663 unless ref $data eq 'ARRAY';
2664  
2665 # probability vector: q+1 evenly spaced points, or an explicit list
2666 30 my $probs;
2667 30 ▶ if (ref $q eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $q eq 'ARRAY') { } 2 28 30
2668 2 $probs = [ sort { $a <=> $b } @$q ];
2669 } else {
2670 28 ▶ _usage_error('qcut', 'number of quantiles must be a positive integer')
Branch: 1 branch
BranchTrueFalseTotal
unless defined $q and $q =~ /\A[1-9][0-9]*\z/ 3 25 28
Condition66%2/3
defined $q and $q =~ /\A[1-9][0-9]*\z/
!ll&&!rl&&r
0325
MC/DC50%1/2
defined $q and $q =~ /\A[1-9][0-9]*\z/
defined $q $q =~ /\A[1-9][0-9]*\z/
Truth table66%2/3
A and B
A= defined $qB= $q =~ /\A[1-9][0-9]*\z/
ABresult
0X0
100
111
2671 unless defined $q && $q =~ /\A[1-9][0-9]*\z/;
2672 25 $probs = [ map { $_ / $q } 0 .. $q ];
2673 }
2674  
2675 27 ▶ my $drop = (($opt{duplicates} // 'raise') eq 'drop') ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
($opt{'duplicates'} // 'raise') eq 'drop' ? : 1 26 27
Condition100%2/2
$opt{"duplicates"} // "raise"
l!l
126
MC/DC100%1/1
$opt{"duplicates"} // "raise"
$opt{"duplicates"}
Truth table100%2/2
A
A= $opt{"duplicates"}
Aresult
11
00
2676 27 my $labels = $opt{labels};
2677  
2678 # codes are opt-in (labels imply them); edges are on unless codes asked for
2679 27 ▶ my $want_codes = ($opt{codes} || defined $labels) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$opt{'codes'} || defined $labels ? : 15 12 27
Condition100%3/3
$opt{"codes"} or defined $labels
l!l&&r!l&&!r
13212
MC/DC100%2/2
$opt{"codes"} or defined $labels
$opt{"codes"} defined $labels
Truth table100%3/3
A or B
A= $opt{"codes"}B= defined $labels
ABresult
1X1
011
000
2680 my $want_edges = exists $opt{edges}
2681 27 ▶ ? ($opt{edges} ? 1 : 0)
Branch: 3 branches
BranchTrueFalseTotal
exists $opt{'edges'} ? : 9 18 27
$opt{'edges'} ? : 9 0 9
$want_codes ? : 6 12 18
2682 : ($want_codes ? 0 : 1);
2683 27 ▶ _usage_error('qcut', 'nothing to return (set edges => 1 or codes => 1)')
Branch: 1 branch
BranchTrueFalseTotal
unless $want_edges or $want_codes 0 27 27
Condition66%2/3
$want_edges or $want_codes
l!l&&r!l&&!r
2160
MC/DC0%0/2
$want_edges or $want_codes
$want_edges $want_codes
Truth table66%2/3
A or B
A= $want_edgesB= $want_codes
ABresult
1X1
011
000
2684 unless $want_edges || $want_codes;
2685  
2686 # does the column contain any NA? undef and NaN both are, as NaN is to
2687 # pandas: up to 0.3413 a NaN was binned as a number, and every edge it
2688 # touched came back NaN (qcut([1, 2, 3, 4, NaN], 4) gave 1 2 3 NaN NaN).
2689 497 ▶ my $is_na = sub { my $x = shift; no warnings 'numeric'; return !defined($x) || $x != $x };
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=2 SCAR=6.9
Condition100%3/3
!defined($x) || $x != $x
l!l&&r!l&&!r
3503481305
MC/DC100%2/2
!defined($x) || $x != $x
!defined($x) $x != $x
Truth table100%3/3
not(A) || B
A= !defined($x)B= $x != $x
ABresult
1X1
011
000
2690 27 my $has_na = 0;
2691 27 ▶ for my $x (@$data) { if ($is_na->($x)) { $has_na = 1; last } }
Branch: 1 branch
BranchTrueFalseTotal
if (&$is_na($x)) 17 376 393
2692  
2693 27 my ($codes, $edges, @pos);
2694 27 ▶ if ($want_codes && $has_na) {
Branch: 2 branches
BranchTrueFalseTotal
if ($want_codes and $has_na) { } 9 18 27
elsif ($has_na) { } 8 10 18
Condition100%3/3
$want_codes and $has_na
!ll&&!rl&&r
1269
MC/DC100%2/2
$want_codes and $has_na
$want_codes $has_na
Truth table100%3/3
A and B
A= $want_codesB= $has_na
ABresult
0X0
100
111
2695 # strip NA, remember positions, scatter codes back afterwards
2696 9 my @vals;
2697 9 for my $i (0 .. $#$data) {
2698 810 my $x = $data->[$i];
2699 810 ▶ next if $is_na->($x);
Branch: 1 branch
BranchTrueFalseTotal
if &$is_na($x) 341 469 810
2700 469 push @vals, $x + 0;
2701 469 push @pos, $i;
2702 }
2703 9 ▶ die "qcut: no non-missing values\n" unless @vals;
Branch: 1 branch
BranchTrueFalseTotal
unless @vals 0 9 9
2704 9 ($codes, $edges) = _qcut_core(\@vals, $probs, $drop, 1);
2705 } elsif ($has_na) {
2706 # edges only: drop NA so cutpoints ignore them, positions don't matter
2707 8 my @vals = grep { !$is_na->($_) } @$data;
2708 8 ▶ die "qcut: no non-missing values\n" unless @vals;
Branch: 1 branch
BranchTrueFalseTotal
unless @vals 0 8 8
2709 8 ($codes, $edges) = _qcut_core(\@vals, $probs, $drop, 0);
2710 } else {
2711 # no NA: hand the original arrayref straight to XS (no copy)
2712 10 ($codes, $edges) = _qcut_core($data, $probs, $drop, $want_codes);
2713 }
2714  
2715 # edges-only: return the flat list
2716 26 ▶ return @$edges unless $want_codes;
Branch: 1 branch
BranchTrueFalseTotal
unless $want_codes 11 15 26
2717  
2718 # turn integer codes into requested labels, or keep the XS arrayref as-is
2719 15 my $out;
2720 15 ▶ if (defined $labels && ref $labels eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $labels and ref $labels eq 'ARRAY') { } 1 14 15
elsif (defined $labels and $labels eq 'interval') { } 1 13 14
Condition100%3/3
defined $labels and ref $labels eq "ARRAY"
!ll&&!rl&&r
1311
MC/DC100%2/2
defined $labels and ref $labels eq "ARRAY"
defined $labels ref $labels eq "ARRAY"
Truth table100%3/3
A and B
A= defined $labelsB= ref $labels eq "ARRAY"
ABresult
0X0
100
111
2721 1 my $nbin = scalar(@$edges) - 1;
2722 1 ▶ die "qcut: got $nbin bins but " . scalar(@$labels) . " labels\n"
Branch: 1 branch
BranchTrueFalseTotal
unless @$labels == $nbin 0 1 1
2723 unless @$labels == $nbin;
2724 1 $out = [ map { $labels->[$_] } @$codes ];
2725 ▶ } elsif (defined $labels && $labels eq 'interval') {
Condition66%2/3
defined $labels and $labels eq "interval"
!ll&&!rl&&r
1301
MC/DC50%1/2
defined $labels and $labels eq "interval"
defined $labels $labels eq "interval"
Truth table66%2/3
A and B
A= defined $labelsB= $labels eq "interval"
ABresult
0X0
100
111
2726 1 my @iv;
2727 1 for my $b (0 .. $#$edges - 1) {
2728 4 my $l = $edges->[$b];
2729 4 my $r = $edges->[$b + 1];
2730 4 ▶ $iv[$b] = $b == 0 ? "[$l, $r]" : "($l, $r]";
Branch: 1 branch
BranchTrueFalseTotal
$b == 0 ? : 1 3 4
2731 }
2732 1 $out = [ map { $iv[$_] } @$codes ];
2733 } else {
2734 13 $out = $codes; # reuse XS result; no copy
2735 }
2736  
2737 # scatter NA positions back in (codes path only)
2738 15 ▶ if ($has_na) {
Branch: 1 branch
BranchTrueFalseTotal
if ($has_na) 9 6 15
2739 9 my @r = (undef) x scalar(@$data);
2740 9 @r[@pos] = @$out;
2741 9 $out = \@r;
2742 }
2743  
2744 15 ▶ return $want_edges ? ($out, $edges) : $out;
Branch: 1 branch
BranchTrueFalseTotal
$want_edges ? : 9 6 15
2745 }
2746  
2747 # summary($data, %opts) -- R-style five-number-plus-mean summary.
2748 #
2749 # Accepts every shape view() does and computes one statistics row per numeric
2750 # "variable": a flat vector (one row); an AoA (one row per inner array, labelled
2751 # by Index); a HoA (one row per key); and -- like view() -- an AoH or HoH (one
2752 # row per column, gathered across the rows). Non-numeric and undefined cells are
2753 # ignored (they never count toward '# values'); an all-non-numeric variable
2754 # shows 0 values and 'na' statistics. Output, colour, and the display options
2755 # are rendered exactly like view() via the shared _render_grid().
2756 44 ▶sub summary {
Subroutine summary: called CC=45 SCAR=38.9
Pod documented
2757 44 my $current_sub = (split(/::/,(caller(0))[3]))[-1];
2758 # options view() understands, plus the row-cap synonyms
2759 44 my %opt_key = map { $_ => 1 } qw(
2760 nrows nrow n rows
2761 na color colors max_width ellipsis gap width to return_only
2762 );
2763 44 my ($data, %args);
2764 44 ▶ if (@_ && ref $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ and ref $_[0]) { } 33 11 44
Condition66%2/3
@_ and ref $_[0]
!ll&&!rl&&r
01133
MC/DC50%1/2
@_ and ref $_[0]
@_ ref $_[0]
Truth table66%2/3
A and B
A= @_B= ref $_[0]
ABresult
0X0
100
111
2765 # summary(\@arr, ...) / summary(\%h, ...)
2766 33 $data = shift;
2767 33 ▶ _usage_error($current_sub, 'arguments after the data must be name => value pairs')
Branch: 1 branch
BranchTrueFalseTotal
if @_ % 2 1 32 33
2768 if @_ % 2;
2769 32 %args = @_;
2770 } else {
2771 # summary(@vector) / summary(@vector, nrows => N): peel recognised
2772 # trailing key/value option pairs off the flat list; the rest is data.
2773 11 ▶ while (@_ >= 2 && defined $_[-2] && !ref($_[-2]) && $opt_key{ $_[-2] }) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ >= 2 and defined $_[-2] and not ref $_[-2] and $opt_key{$_[-2]}) 22 11 33
Condition77%7/9
@_ >= 2 and defined $_[-2] and not ref $_[-2] and $opt_key{$_[-2]}
!ll&&!rl&&r
2922
@_ >= 2 and defined $_[-2] and not ref $_[-2]
!ll&&!rl&&r
2031
@_ >= 2 and defined $_[-2]
!ll&&!rl&&r
2031
MC/DC50%2/4
@_ >= 2 and defined $_[-2] and not ref $_[-2] and $opt_key{$_[-2]}
@_ >= 2 defined $_[-2] not ref $_[-2] $opt_key{$_[-2]}
Truth table60%3/5
A and B and not(C) and D
A= @_ >= 2B= defined $_[-2]C= not ref $_[-2]D= $opt_key{$_[-2]}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
2774 22 my $val = pop @_;
2775 22 my $key = pop @_;
2776 22 $args{$key} = $val;
2777 }
2778 11 my @list = @_;
2779 11 $data = \@list;
2780 }
2781 43 my @bad = sort grep { !$opt_key{$_} } keys %args;
2782 43 ▶ _usage_error($current_sub, "unknown argument(s): @bad") if @bad;
Branch: 1 branch
BranchTrueFalseTotal
if @bad 3 40 43
2783 # row cap: nrows / nrow / n / rows are synonyms (default 10)
2784 my $nrows = exists $args{nrows} ? $args{nrows} : exists $args{nrow} ? $args{nrow}
2785 : exists $args{n} ? $args{n} : exists $args{rows} ? $args{rows}
2786 40 ▶ : 10;
Branch: 4 branches
BranchTrueFalseTotal
exists $args{'nrows'} ? : 6 34 40
exists $args{'nrow'} ? : 1 33 34
exists $args{'n'} ? : 1 32 33
exists $args{'rows'} ? : 0 32 32
2787 40 ▶ _usage_error($current_sub, "'nrows' must be a non-negative integer")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $nrows and $nrows =~ /^\d+$/ 0 40 40
Condition33%1/3
defined $nrows and $nrows =~ /^\d+$/
!ll&&!rl&&r
0040
MC/DC0%0/2
defined $nrows and $nrows =~ /^\d+$/
defined $nrows $nrows =~ /^\d+$/
Truth table33%1/3
A and B
A= defined $nrowsB= $nrows =~ /^\d+$/
ABresult
0X0
100
111
2788 unless defined $nrows && $nrows =~ /^\d+$/;
2789  
2790 40 my $rt = ref $data;
2791 40 ▶ _usage_error($current_sub, "data must either be a hash or an array, not \"$rt\"")
Branch: 1 branch
BranchTrueFalseTotal
unless $rt eq "ARRAY" or $rt eq "HASH" 2 38 40
Condition100%3/3
$rt eq "ARRAY" or $rt eq "HASH"
l!l&&r!l&&!r
3082
MC/DC100%2/2
$rt eq "ARRAY" or $rt eq "HASH"
$rt eq "ARRAY" $rt eq "HASH"
Truth table100%3/3
A or B
A= $rt eq "ARRAY"B= $rt eq "HASH"
ABresult
1X1
011
000
2792 unless $rt eq 'ARRAY' or $rt eq 'HASH';
2793  
2794 # resolve the data shape into (label, numeric-vector) series
2795 38 my (@labels, @vecs, $lab_header);
2796 38 ▶ if ($rt eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if ($rt eq 'ARRAY') { } 30 8 38
2797 30 my $first;
2798 30 ▶ for my $e (@$data) { if (defined $e) { $first = $e; last } }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $e) 30 0 30
2799 30 my $ft = ref $first;
2800 30 ▶ if ($ft eq 'HASH') { # AoH: one series per column
Branch: 2 branches
BranchTrueFalseTotal
if ($ft eq 'HASH') { } 1 29 30
elsif ($ft eq 'ARRAY') { } 3 26 29
2801 1 $lab_header = 'Column';
2802 1 my %seen;
2803 1 ▶ for my $row (@$data) { next unless ref $row eq 'HASH'; $seen{$_} = 1 for keys %$row }
Branch: 1 branch
BranchTrueFalseTotal
unless ref $row eq "HASH" 0 3 3
2804 1 for my $col (sort keys %seen) {
2805 2 push @labels, $col;
2806 2 ▶ push @vecs, [ map { ref $_ eq 'HASH' ? $_->{$col} : undef } @$data ];
Branch: 1 branch
BranchTrueFalseTotal
ref $_ eq 'HASH' ? : 6 0 6
2807 }
2808 } elsif ($ft eq 'ARRAY') { # AoA: one series per inner array
2809 3 $lab_header = 'Index';
2810 3 for my $i (0 .. $#$data) {
2811 6 push @labels, $i;
2812 6 ▶ push @vecs, (ref $data->[$i] eq 'ARRAY' ? [ @{ $data->[$i] } ] : []);
Branch: 1 branch
BranchTrueFalseTotal
ref $data->[$i] eq 'ARRAY' ? : 6 0 6
2813 }
2814 } else { # flat vector: a single series
2815 26 $lab_header = '';
2816 26 push @labels, '';
2817 26 push @vecs, [ @$data ];
2818 }
2819 } else { # HASH
2820 8 my @keys = keys %$data;
2821 8 my $sample;
2822 8 ▶ for my $k (@keys) { $sample = $data->{$k}; last if defined $sample }
Branch: 1 branch
BranchTrueFalseTotal
if defined $sample 8 0 8
2823 8 my $vt = ref $sample;
2824 8 ▶ if ($vt eq 'ARRAY') { # HoA: one series per key
Branch: 2 branches
BranchTrueFalseTotal
if ($vt eq 'ARRAY') { } 5 3 8
elsif ($vt eq 'HASH') { } 3 0 3
2825 5 $lab_header = 'Key';
2826 5 for my $k (sort { lc $a cmp lc $b } @keys) {
2827 14 push @labels, $k;
2828 14 ▶ push @vecs, (ref $data->{$k} eq 'ARRAY' ? [ @{ $data->{$k} } ] : []);
Branch: 1 branch
BranchTrueFalseTotal
ref $data->{$k} eq 'ARRAY' ? : 14 0 14
2829 }
2830 } elsif ($vt eq 'HASH') { # HoH: one series per column (inner key)
2831 3 $lab_header = 'Column';
2832 3 my %seen;
2833 3 ▶ for my $k (@keys) { next unless ref $data->{$k} eq 'HASH'; $seen{$_} = 1 for keys %{ $data->{$k} } }
Branch: 1 branch
BranchTrueFalseTotal
unless ref $data->{$k} eq "HASH" 0 7 7
2834 3 for my $col (sort keys %seen) {
2835 6 push @labels, $col;
2836 6 ▶ push @vecs, [ map { ref $data->{$_} eq 'HASH' ? $data->{$_}{$col} : undef } @keys ];
Branch: 1 branch
BranchTrueFalseTotal
ref $data->{$_} eq 'HASH' ? : 14 0 14
2837 }
2838 } else { # flat hash: its values as one series
2839 0 $lab_header = '';
2840 0 push @labels, '';
2841 0 push @vecs, [ map { $data->{$_} } @keys ];
2842 }
2843 }
2844  
2845 # compute the statistics grid
2846 38 my @colnames = ('# values', 'Min.', '1st Qu.', 'Median', 'Mean', '3rd Qu.', 'Max.');
2847 38 my @raw;
2848 38 for my $vec (@vecs) {
2849 54 ▶ my @numeric = grep { defined $_ && looks_like_number($_) } @$vec;
Condition100%3/3
defined $_ && &looks_like_number($_)
!ll&&!rl&&r
23403
MC/DC100%2/2
defined $_ && &looks_like_number($_)
defined $_ &looks_like_number($_)
Truth table100%3/3
A && B
A= defined $_B= &looks_like_number($_)
ABresult
0X0
100
111
2850 54 ▶ if (!@numeric) { push @raw, [ 0, (undef) x 6 ]; next } # empty -> na stats
Branch: 1 branch
BranchTrueFalseTotal
unless (@numeric) 3 51 54
2851 51 my $q = quantile(\@numeric, probs => [0.25, 0.75]);
2852 # format as %.4g strings; they still look numeric, so _render_grid
2853 # right-aligns and colours them as numbers.
2854 push @raw, [
2855 scalar @numeric,
2856 sprintf('%.4g', min(\@numeric)), sprintf('%.4g', $q->{'25%'}),
2857 sprintf('%.4g', median(\@numeric)), sprintf('%.4g', mean(\@numeric)),
2858 51 sprintf('%.4g', $q->{'75%'}), sprintf('%.4g', max(\@numeric)),
2859 ];
2860 }
2861  
2862 # cap the number of series shown (keep the true total for the "... more" note)
2863 38 my $total = scalar @labels;
2864 38 ▶ if ($nrows < $total) { $#labels = $nrows - 1; $#raw = $nrows - 1; }
Branch: 1 branch
BranchTrueFalseTotal
if ($nrows < $total) 2 36 38
2865  
2866 # The display options go on as they were given; _render_grid() checks them.
2867 return _render_grid('summary', $total, \@colnames, \@labels, \@raw, $lab_header,
2868 38 ▶ map { exists $args{$_} ? ($_ => $args{$_}) : () }
Branch: 1 branch
BranchTrueFalseTotal
exists $args{$_} ? : 76 266 342
2869 qw(na max_width ellipsis gap width to return_only color colors));
2870 }
2871  
2872 # .xlsx support (pure Perl, no CPAN deps)
2873 # An .xlsx file is a ZIP archive of XML parts. IO::Uncompress::Unzip is a core
2874 # module, so we can pull the parts out and parse the (very regular) XML with
2875 # regexes, then feed rows to read_table's callback exactly like _parse_csv_file
2876 # does. A workbook with more than one worksheet is read into a hash keyed by
2877 # sheet name (see read_table). Limitations: dates/times come back as their raw
2878 # serial numbers (no style-based formatting); shared-string rich-text runs are
2879 # concatenated.
2880  
2881 # The decompressed bytes of a named archive member as a SCALAR REFERENCE, or
2882 # undef if the member is absent.
2883 #
2884 # The reference is the point. Returning the string itself costs a full copy of
2885 # it -- perl cannot hand back the pad slot of a lexical, so `return $content'
2886 # copies, and on the 36 MB worksheet part of a 21,845 x 50 workbook that was
2887 # 34 MB of peak RSS for nothing: 82.4 MB against 48.5 MB for the reference, and
2888 # 81 MB still resident afterwards against 13 MB, the difference being heap the
2889 # allocator never gave back. Every caller dereferences; `$$ws' on an argument
2890 # list pushes the SV itself, so the part reaches the XS parser without a copy
2891 # either.
2892 #
2893 # _unzip_member_fast() handles the archives Excel, LibreOffice and openpyxl
2894 # actually write; the loop below is the fallback for everything else, and is
2895 # what every member went through up to 0.316.
2896 #
2897 # The read appends onto the end of $content rather than going through a second
2898 # scalar: IO::Uncompress::Base::read() turns its truncating substr() into a
2899 # no-op when the offset is already the buffer's length, so the string is
2900 # extended in place instead of being copied by a concatenation. On the 36 MB
2901 # worksheet part of a 21,845 x 50 workbook that is 0.171 s against 0.243 s for
2902 # `$content .= $buf`, over three runs each in a fresh process, and it leaves
2903 # behind none of the ~25 MB of realloc slack the concatenation did.
2904 # Do NOT reach for BlockSize => 1<<20 instead: measured at 0.262 s on the same
2905 # part against 0.174 s for the default. (The 2.08 s this comment used to quote
2906 # does not reproduce here, but its conclusion does.)
2907 1378 ▶sub _unzip_member {
Subroutine _unzip_member: called CC=4 SCAR=13.9
2908 1378 my ($file, $member) = @_;
2909 1378 my ($handled, $ref) = _unzip_member_fast($file, $member);
2910 1378 ▶ return $ref if $handled;
Branch: 1 branch
BranchTrueFalseTotal
if $handled 1122 256 1378
2911 256 require IO::Uncompress::Unzip;
2912 256 ▶ my $z = IO::Uncompress::Unzip->new($file, Name => $member)
Branch: 1 branch
BranchTrueFalseTotal
my $z = "IO::Uncompress::Unzip"->new($file, "Name", $member) or return undef 232 24 256
2913 or return undef;
2914 232 my $content = '';
2915 232 my ($off, $n) = (0, 0);
2916 232 ▶ while (($n = $z->read($content, 1 << 20, $off)) > 0) { $off += $n }
Branch: 1 branch
BranchTrueFalseTotal
if (($n = $z->read($content, 1048576, $off)) > 0) 231 232 463
2917 232 $z->close;
2918 232 return \$content;
2919 }
2920  
2921 # The same member, read straight out of the archive with Compress::Raw::Zlib
2922 # instead of through IO::Uncompress::Unzip.
2923 #
2924 # Returns a two-element list: (1, \$bytes) for a member it read, (1, undef) for
2925 # an archive it understood that does not hold the member, and (0, undef) for
2926 # anything it declines -- at which point _unzip_member falls back and the old
2927 # path decides. Declining is never an error: this reads the central directory
2928 # and one deflate stream and nothing else, and hands everything beyond that to
2929 # the module that has been ported to all of it.
2930 #
2931 # Why bother: with the worksheet parser in XS since 0.316, decompression is what
2932 # a read of an .xlsx now spends its time on -- 0.239 s of the 0.413 s a
2933 # 21,845 x 50 workbook takes, against 0.015 s to parse its 117,870 shared
2934 # strings. IO::Uncompress::Unzip needs 0.174 s for the 36 MB worksheet part
2935 # where inflating it directly needs 0.052 s, plus 0.004 s to read the 5.5 MB of
2936 # compressed bytes.
2937 #
2938 # Most of that gap is one thing. Unzip.pm's ckParams() sets `crc32 => 1'
2939 # unconditionally ("unzip always needs crc32"), so every byte is run through
2940 # Compress::Raw::Zlib::crc32() -- 0.076 s on this part, more than the inflate
2941 # itself -- and then the comparison against the stored CRC happens only under
2942 # `Strict', which defaults to 0. The check is paid for and not made. This path
2943 # does not compute it either, which is the same answer for the same money;
2944 # asking Inflate for -CRC32 => 1 costs the identical 0.076 s (it is the same
2945 # per-chunk call), so turning it on here would want `Strict'-like behaviour --
2946 # a croak on mismatch -- to be worth anything, and that is a change in what
2947 # read_table does with a damaged file rather than a speed decision.
2948 #
2949 # Bufsize 1<<16: 0.052 s against 0.063 s at the 4 KB default on that part, and
2950 # flat from there (1<<18 through 1<<22 all measure 0.052 s).
2951 #
2952 # Layout constants are ECMA-376's container, i.e. PKWARE's APPNOTE.TXT 6.3.10:
2953 # section 4.3.16 for the end-of-central-directory record, 4.3.12 for a central
2954 # directory entry, 4.3.7 for a local file header.
2955 1404 ▶sub _unzip_member_fast {
Subroutine _unzip_member_fast: called CC=46 SCAR=43.1
2956 1404 my ($file, $member) = @_;
2957 1404 require Compress::Raw::Zlib;
2958 1404 my $fh;
2959 1404 ▶ open $fh, '<', $file or return (0, undef);
Branch: 1 branch
BranchTrueFalseTotal
open $fh, "<", $file or return 0, undef 1402 2 1404
2960 1402 binmode $fh;
2961 1402 my $size = -s $fh;
2962 1402 ▶ return (0, undef) unless defined $size && $size >= 22;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $size and $size >= 22 2 1400 1402
Condition66%2/3
defined $size and $size >= 22
!ll&&!rl&&r
021400
MC/DC50%1/2
defined $size and $size >= 22
defined $size $size >= 22
Truth table66%2/3
A and B
A= defined $sizeB= $size >= 22
ABresult
0X0
100
111
2963  
2964 # The EOCD is 22 bytes plus a comment of up to 65535, so it begins no
2965 # earlier than 65557 from the end. Its signature can also occur inside the
2966 # comment (or inside compressed data, for an archive with no comment), so
2967 # a candidate counts only when the record it starts ends exactly at the end
2968 # of the file.
2969 1400 ▶ my $want = $size < 65557 ? $size : 65557;
Branch: 1 branch
BranchTrueFalseTotal
$size < 65557 ? : 1396 4 1400
2970 1400 ▶ seek $fh, $size - $want, 0 or return (0, undef);
Branch: 1 branch
BranchTrueFalseTotal
seek $fh, $size - $want, 0 or return 0, undef 1400 0 1400
2971 1400 my $tail = '';
2972 1400 ▶ return (0, undef) unless read($fh, $tail, $want) == $want;
Branch: 1 branch
BranchTrueFalseTotal
unless read($fh, $tail, $want) == $want 0 1400 1400
2973 1400 my ($ncd, $cdsz, $cdoff);
2974 1400 my $at = length($tail) - 22;
2975 1400 ▶ while ($at >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($at >= 0) 1402 0 1402
2976 1402 $at = rindex($tail, "PK\5\6", $at);
2977 1402 ▶ last if $at < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $at < 0 4 1398 1402
2978 1398 my ($d1, $d2, $nd, $nt, $sz, $off, $cmt)
2979 = unpack('x' . ($at + 4) . ' v v v v V V v', $tail);
2980 1398 ▶ if ($at + 22 + $cmt == length $tail) {
Branch: 1 branch
BranchTrueFalseTotal
if ($at + 22 + $cmt == length $tail) 1396 2 1398
2981 # a split archive has its directory somewhere this cannot reach
2982 1396 ▶ return (0, undef) if $d1 || $d2 || $nd != $nt;
Branch: 1 branch
BranchTrueFalseTotal
if $d1 or $d2 or $nd != $nt 0 1396 1396
Condition33%2/6
$d1 or $d2 or $nd != $nt
l!l&&r!l&&!r
001396
$d1 or $d2
l!l&&r!l&&!r
001396
MC/DC0%0/3
$d1 or $d2 or $nd != $nt
$d1 $d2 $nd != $nt
Truth table25%1/4
A or B or C
A= $d1B= $d2C= $nd != $nt
ABCresult
1XX1
01X1
0011
0000
2983 # the zip64 sentinels: the real values are in a record this does
2984 # not read, so hand the whole archive back
2985 1396 ▶ return (0, undef)
Branch: 1 branch
BranchTrueFalseTotal
if $nt == 65535 or $sz == 4294967295 or $off == 4294967295 2 1394 1396
Condition50%3/6
$nt == 65535 or $sz == 4294967295 or $off == 4294967295
l!l&&r!l&&!r
021394
$nt == 65535 or $sz == 4294967295
l!l&&r!l&&!r
001396
MC/DC33%1/3
$nt == 65535 or $sz == 4294967295 or $off == 4294967295
$nt == 65535 $sz == 4294967295 $off == 4294967295
Truth table50%2/4
A or B or C
A= $nt == 65535B= $sz == 4294967295C= $off == 4294967295
ABCresult
1XX1
01X1
0011
0000
2986 if $nt == 0xFFFF || $sz == 0xFFFFFFFF || $off == 0xFFFFFFFF;
2987 1394 ($ncd, $cdsz, $cdoff) = ($nt, $sz, $off);
2988 1394 last;
2989 }
2990 2 $at--;
2991 }
2992 1398 ▶ return (0, undef) unless defined $cdoff;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cdoff 4 1394 1398
2993 1394 ▶ return (0, undef) if $cdoff + $cdsz > $size;
Branch: 1 branch
BranchTrueFalseTotal
if $cdoff + $cdsz > $size 0 1394 1394
2994 1394 ▶ seek $fh, $cdoff, 0 or return (0, undef);
Branch: 1 branch
BranchTrueFalseTotal
seek $fh, $cdoff, 0 or return 0, undef 1394 0 1394
2995 1394 my $cd = '';
2996 1394 ▶ return (0, undef) unless read($fh, $cd, $cdsz) == $cdsz;
Branch: 1 branch
BranchTrueFalseTotal
unless read($fh, $cd, $cdsz) == $cdsz 0 1394 1394
2997  
2998 # Walk the directory to the member. Entries are in the order the local
2999 # headers are, so stopping at the first match is what Unzip.pm's own
3000 # sequential scan would have found.
3001 1394 my ($gp, $method, $csz, $usz, $lho);
3002 1394 my $p = 0;
3003 1394 ▶ for (my $i = 0; $i < $ncd; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $ncd) 5568 239 5807
3004 5568 ▶ return (0, undef) if $p + 46 > $cdsz;
Branch: 1 branch
BranchTrueFalseTotal
if $p + 46 > $cdsz 0 5568 5568
3005 5568 ▶ return (0, undef) unless substr($cd, $p, 4) eq "PK\1\2";
Branch: 1 branch
BranchTrueFalseTotal
unless substr($cd, $p, 4) eq "PK\1\2" 253 5315 5568
3006 5315 my ($g, $m, $cs, $us, $nl, $el, $cl, $lo)
3007 = unpack('x' . ($p + 8) . ' v v x8 V V v v v x8 V', $cd);
3008 5315 ▶ return (0, undef) if $p + 46 + $nl + $el + $cl > $cdsz;
Branch: 1 branch
BranchTrueFalseTotal
if $p + 46 + $nl + $el + $cl > $cdsz 0 5315 5315
3009 5315 ▶ if (substr($cd, $p + 46, $nl) eq $member) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($cd, $p + 46, $nl) eq $member) 902 4413 5315
3010 902 ($gp, $method, $csz, $usz, $lho) = ($g, $m, $cs, $us, $lo);
3011 902 last;
3012 }
3013 4413 $p += 46 + $nl + $el + $cl;
3014 }
3015 1141 ▶ return (1, undef) unless defined $lho; # the archive has no such member
Branch: 1 branch
BranchTrueFalseTotal
unless defined $lho 239 902 1141
3016  
3017 # bit 0 is encryption and bit 6 strong encryption; bit 3 only moves the
3018 # sizes into a trailing descriptor, and the directory's copies (which is
3019 # what is read here) are authoritative either way.
3020 902 ▶ return (0, undef) if $gp & 0x41;
Branch: 1 branch
BranchTrueFalseTotal
if $gp & 65 0 902 902
3021 902 ▶ return (0, undef) if $method != 0 && $method != 8; # 0 stored, 8 deflate
Branch: 1 branch
BranchTrueFalseTotal
if $method != 0 and $method != 8 0 902 902
Condition66%2/3
$method != 0 and $method != 8
!ll&&!rl&&r
6482540
MC/DC0%0/2
$method != 0 and $method != 8
$method != 0 $method != 8
Truth table66%2/3
A and B
A= $method != 0B= $method != 8
ABresult
0X0
100
111
3022 902 ▶ return (0, undef) if $csz == 0xFFFFFFFF || $usz == 0xFFFFFFFF
Branch: 1 branch
BranchTrueFalseTotal
if $csz == 4294967295 or $usz == 4294967295 or $lho == 4294967295 0 902 902
Condition33%2/6
$csz == 4294967295 or $usz == 4294967295 or $lho == 4294967295
l!l&&r!l&&!r
00902
$csz == 4294967295 or $usz == 4294967295
l!l&&r!l&&!r
00902
MC/DC0%0/3
$csz == 4294967295 or $usz == 4294967295 or $lho == 4294967295
$csz == 4294967295 $usz == 4294967295 $lho == 4294967295
Truth table25%1/4
A or B or C
A= $csz == 4294967295B= $usz == 4294967295C= $lho == 4294967295
ABCresult
1XX1
01X1
0011
0000
3023 || $lho == 0xFFFFFFFF; # zip64 again
3024 902 ▶ return (0, undef) if $lho + 30 > $size;
Branch: 1 branch
BranchTrueFalseTotal
if $lho + 30 > $size 0 902 902
3025  
3026 # The local header repeats the name and carries its own extra field, which
3027 # need not be the directory's: only its two lengths are read here, to step
3028 # over them to the data.
3029 902 ▶ seek $fh, $lho, 0 or return (0, undef);
Branch: 1 branch
BranchTrueFalseTotal
seek $fh, $lho, 0 or return 0, undef 902 0 902
3030 902 my $lh = '';
3031 902 ▶ return (0, undef) unless read($fh, $lh, 30) == 30;
Branch: 1 branch
BranchTrueFalseTotal
unless read($fh, $lh, 30) == 30 0 902 902
3032 902 ▶ return (0, undef) unless substr($lh, 0, 4) eq "PK\3\4";
Branch: 1 branch
BranchTrueFalseTotal
unless substr($lh, 0, 4) eq "PK\3\4" 0 902 902
3033 902 my ($lnl, $lel) = unpack('x26 v v', $lh);
3034 902 my $data = $lho + 30 + $lnl + $lel;
3035 902 ▶ return (0, undef) if $data + $csz > $size;
Branch: 1 branch
BranchTrueFalseTotal
if $data + $csz > $size 0 902 902
3036 902 ▶ seek $fh, $data, 0 or return (0, undef);
Branch: 1 branch
BranchTrueFalseTotal
seek $fh, $data, 0 or return 0, undef 902 0 902
3037 902 my $comp = '';
3038 902 ▶ return (0, undef) unless read($fh, $comp, $csz) == $csz;
Branch: 1 branch
BranchTrueFalseTotal
unless read($fh, $comp, $csz) == $csz 0 902 902
3039 902 ▶ close $fh or return (0, undef);
Branch: 1 branch
BranchTrueFalseTotal
close $fh or return 0, undef 902 0 902
3040  
3041 902 ▶ if ($method == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method == 0) 648 254 902
3042 648 ▶ return (0, undef) unless length($comp) == $usz;
Branch: 1 branch
BranchTrueFalseTotal
unless length $comp == $usz 0 648 648
3043 648 return (1, \$comp);
3044 }
3045 254 my ($inf, $st) = Compress::Raw::Zlib::Inflate->new(
3046 -WindowBits => -Compress::Raw::Zlib::MAX_WBITS(),
3047 -Bufsize => 1 << 16);
3048 254 ▶ return (0, undef) unless $inf;
Branch: 1 branch
BranchTrueFalseTotal
unless $inf 0 254 254
3049 254 my $out = '';
3050 # inflate() eats $comp as it goes, so the two are never both whole
3051 254 $st = $inf->inflate($comp, $out);
3052 254 ▶ return (0, undef) unless $st == Compress::Raw::Zlib::Z_STREAM_END()
Branch: 1 branch
BranchTrueFalseTotal
unless $st == Compress::Raw::Zlib::Z_STREAM_END() or $st == Compress::Raw::Zlib::Z_OK() 0 254 254
Condition33%1/3
$st == Compress::Raw::Zlib::Z_STREAM_END() or $st == Compress::Raw::Zlib::Z_OK()
l!l&&r!l&&!r
25400
MC/DC0%0/2
$st == Compress::Raw::Zlib::Z_STREAM_END() or $st == Compress::Raw::Zlib::Z_OK()
$st == Compress::Raw::Zlib::Z_STREAM_END() $st == Compress::Raw::Zlib::Z_OK()
Truth table33%1/3
A or B
A= $st == Compress::Raw::Zlib::Z_STREAM_END()B= $st == Compress::Raw::Zlib::Z_OK()
ABresult
1X1
011
000
3053 || $st == Compress::Raw::Zlib::Z_OK();
3054 254 ▶ return (0, undef) unless length($out) == $usz;
Branch: 1 branch
BranchTrueFalseTotal
unless length $out == $usz 0 254 254
3055 254 return (1, \$out);
3056 }
3057  
3058 # Decode the five predefined XML entities plus numeric character references,
3059 # the latter as UTF-8 bytes so the result stays byte-consistent with the rest of
3060 # the file (which we read, and return, as raw UTF-8 bytes). The decoding is
3061 # xlsx_xml_uncat() in LikeR.xs, the one the cells go through: up to 0.320 this
3062 # was five substitutions in a row, and a reference the first one produced was
3063 # decoded again by a later one, so "&#38;lt;" came back as "<".
3064 822 ▶sub _xml_unescape {
Subroutine _xml_unescape: called CC=3 SCAR=11.0
3065 822 my ($s) = @_;
3066 822 ▶ return $s unless defined $s && index($s, '&') >= 0;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s and index($s, "&") != -1 816 6 822
Condition100%3/3
defined $s and index($s, "&") != -1
!ll&&!rl&&r
18156
MC/DC100%2/2
defined $s and index($s, "&") != -1
defined $s index($s, "&") != -1
Truth table100%3/3
A and B
A= defined $sB= index($s, "&") != -1
ABresult
0X0
100
111
3067 6 return _xml_unescape_xs($s);
3068 }
3069  
3070 # The start tags of every element called $local in $xml, under any namespace
3071 # prefix, as their attribute runs. Comments are taken out first, so that a
3072 # commented-out <sheet> or <Relationship> is not read as one, and a tag is
3073 # matched quote by quote, so that a '>' inside an attribute value -- which XML
3074 # allows unescaped -- does not end it.
3075 #
3076 # Both this and _xml_attr() compile their pattern once per name and keep it.
3077 # Interpolated into the match, it was recompiled on every call (the callers
3078 # alternate names), and perl 5.32's regcomp leaks the inversion lists it builds
3079 # for \s and \w on each compile: 0.3214 failed 14 leak subtests on a 5.32.1
3080 # smoker that no perl in the local matrix could reproduce. The keys are the
3081 # literal names the xlsx reader passes, so the caches stay a few entries long,
3082 # and the qr// is evaluated only on a miss, never inside a warmed leak check
3083 # (5.10.0's pp_qr() leaks an SV each time one is).
3084 497 my (%xml_tag_re, %xml_attr_re);
3085 661 ▶sub _xml_start_tags {
Subroutine _xml_start_tags: called CC=4 SCAR=13.9
3086 661 my ($xml, $local) = @_;
3087 661 ▶ $xml =~ s/<!--.*?-->//gs if index($xml, '<!--') >= 0;
Branch: 1 branch
BranchTrueFalseTotal
if index($xml, "<!--") != -1 2 659 661
3088 661 ▶ my $re = $xml_tag_re{$local}
Condition100%2/2
$xml_tag_re{$local} ||= qr/<(?:[\w.-]+:)?\Q$local\E\b((?:[^>"']|"[^"]*"|'[^']*')*)>/
l!l
62140
MC/DC100%1/1
$xml_tag_re{$local} ||= qr/<(?:[\w.-]+:)?\Q$local\E\b((?:[^>"']|"[^"]*"|'[^']*')*)>/
$xml_tag_re{$local}
Truth table100%2/2
A
A= $xml_tag_re{$local}
Aresult
11
00
3089 ||= qr{<(?:[\w.-]+:)?\Q$local\E\b((?:[^>"']|"[^"]*"|'[^']*')*)>};
3090 661 my @tags;
3091 661 ▶ while ($xml =~ /$re/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($xml =~ /$re/g) 820 661 1481
3092 820 (my $at = $1) =~ s{/\z}{}; # the "/" of a self-closing tag
3093 820 push @tags, $at;
3094 }
3095 661 return @tags;
3096 }
3097  
3098 # The value of the attribute whose name matches $name (a pattern, so that the
3099 # r:id of a <sheet> can be found under whatever prefix its writer bound) in an
3100 # attribute run from _xml_start_tags, or undef. XML lets a value be quoted with
3101 # '"' or with "'", and up to 0.3213 only the first was read: a workbook written
3102 # with single quotes lost every sheet name, and its sheets were then read by
3103 # file position rather than through their relationships.
3104 2061 ▶sub _xml_attr {
Subroutine _xml_attr: called CC=4 SCAR=13.9
3105 2061 my ($attrs, $name) = @_;
3106 2061 ▶ my $re = $xml_attr_re{$name}
Condition100%2/2
$xml_attr_re{$name} ||= qr/(?:\A|\s)$name\s*=\s*(?:"([^"]*)"|'([^']*)')/
l!l
1961100
MC/DC100%1/1
$xml_attr_re{$name} ||= qr/(?:\A|\s)$name\s*=\s*(?:"([^"]*)"|'([^']*)')/
$xml_attr_re{$name}
Truth table100%2/2
A
A= $xml_attr_re{$name}
Aresult
11
00
3107 ||= qr/(?:\A|\s)$name\s*=\s*(?:"([^"]*)"|'([^']*)')/;
3108 2061 ▶ return undef unless $attrs =~ $re;
Branch: 1 branch
BranchTrueFalseTotal
unless $attrs =~ /$re/ 2 2059 2061
3109 2059 ▶ return defined $1 ? $1 : $2;
Branch: 1 branch
BranchTrueFalseTotal
defined $1 ? : 2049 10 2059
3110 }
3111  
3112 # A relationship's Target as the name of the archive member it points at. A
3113 # target is a URI reference resolved against the part that holds the
3114 # relationship (ECMA-376 Part 2, Open Packaging Conventions), so it is relative
3115 # to xl/, where workbook.xml lives, unless it starts with "/", which makes it
3116 # relative to the package root; "." and ".." segments are resolved. A
3117 # target that already starts with "xl/" is taken as rooted, which is how this
3118 # has always read one.
3119 426 ▶sub _xlsx_part_path {
Subroutine _xlsx_part_path: called CC=7 SCAR=19.5
3120 426 my ($tg) = @_;
3121 426 ▶ my $path = $tg =~ s{\A/+}{} ? $tg : $tg =~ m{\Axl/} ? $tg : "xl/$tg";
Branch: 2 branches
BranchTrueFalseTotal
$tg =~ s[\A/+][] ? : 2 424 426
$tg =~ m[\Axl/] ? : 1 423 424
3122 426 my @seg;
3123 426 for my $s (split m{/}, $path) {
3124 1270 ▶ next if $s eq '' || $s eq '.';
Branch: 1 branch
BranchTrueFalseTotal
if $s eq "" or $s eq "." 4 1266 1270
Condition100%3/3
$s eq "" or $s eq "."
l!l&&r!l&&!r
131266
MC/DC100%2/2
$s eq "" or $s eq "."
$s eq "" $s eq "."
Truth table100%3/3
A or B
A= $s eq ""B= $s eq "."
ABresult
1X1
011
000
3125 1266 ▶ if ($s eq '..') { pop @seg; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($s eq "..") 2 1264 1266
3126 1264 push @seg, $s;
3127 }
3128 426 return join '/', @seg;
3129 }
3130  
3131 # The relationships of xl/workbook.xml, from xl/_rels/workbook.xml.rels:
3132 # { target => { $id => $member, ... }, sst => $member or undef }. 'sst' is the
3133 # shared-string part, found by its relationship Type -- the transitional
3134 # .../officeDocument/2006/relationships/sharedStrings or the strict
3135 # .../officeDocument/relationships/sharedStrings, both ending in
3136 # "/sharedStrings" -- rather than by its usual name: the name is the writer's
3137 # choice, and up to 0.3213 a workbook that stored the part as
3138 # xl/SharedStrings.xml read every string cell as empty.
3139 332 ▶sub _xlsx_rels {
Subroutine _xlsx_rels: called CC=8 SCAR=21.6
3140 332 my ($file) = @_;
3141 332 my (%target, $sst);
3142 332 my $rels = _unzip_member($file, 'xl/_rels/workbook.xml.rels');
3143 332 ▶ if (defined $rels) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rels) 332 0 332
3144 332 for my $at (_xml_start_tags($$rels, 'Relationship')) {
3145 421 my $tg = _xml_attr($at, 'Target');
3146 421 ▶ next unless defined $tg;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $tg 0 421 421
3147 421 $tg = _xlsx_part_path(_xml_unescape($tg));
3148 421 my $id = _xml_attr($at, 'Id');
3149 421 my $type = _xml_attr($at, 'Type');
3150 421 ▶ $target{$id} = $tg if defined $id;
Branch: 1 branch
BranchTrueFalseTotal
if defined $id 421 0 421
3151 421 ▶ $sst = $tg if !defined $sst && defined $type && $type =~ m{/sharedStrings\z};
Branch: 1 branch
BranchTrueFalseTotal
if not defined $sst and defined $type and $type =~ m[/sharedStrings\z] 18 403 421
Condition50%3/6
not defined $sst and defined $type and $type =~ m[/sharedStrings\z]
!ll&&!rl&&r
040318
not defined $sst and defined $type
!ll&&!rl&&r
00421
MC/DC33%1/3
not defined $sst and defined $type and $type =~ m[/sharedStrings\z]
not defined $sst defined $type $type =~ m[/sharedStrings\z]
Truth table50%2/4
not(A) and B and C
A= not defined $sstB= defined $typeC= $type =~ m[/sharedStrings\z]
ABCresult
0XX0
10X0
1100
1111
3152 }
3153 }
3154 332 return { target => \%target, sst => $sst };
3155 }
3156  
3157 # Shared strings (optional part): each <si> may hold several <t> runs, which
3158 # are concatenated. Returns an arrayref indexed by shared-string id. The parse
3159 # is xlsx_sst_parse() in LikeR.xs; the two nested regexes it replaces cost
3160 # 0.205 s on an 8 MB table of 117,870 strings. $rels, from _xlsx_rels(), says
3161 # where the part is; a workbook whose relationships do not name one is tried at
3162 # xl/sharedStrings.xml, the name Excel gives it, as it always was.
3163 326 ▶sub _xlsx_shared_strings {
Subroutine _xlsx_shared_strings: called CC=4 SCAR=13.9
3164 326 my ($file, $rels) = @_;
3165 326 ▶ $rels ||= _xlsx_rels($file);
Condition66%2/3
$rels ||= _xlsx_rels($file)
l!l&&r!l&&!r
32420
MC/DC0%0/2
$rels ||= _xlsx_rels($file)
$rels _xlsx_rels($file)
Truth table66%2/3
A ||= B
A= $relsB= _xlsx_rels($file)
ABresult
1X1
011
000
3166 326 ▶ my $ss = _unzip_member($file, $rels->{sst} // 'xl/sharedStrings.xml');
Condition100%2/2
$rels->{"sst"} // "xl/sharedStrings.xml"
l!l
17309
MC/DC100%1/1
$rels->{"sst"} // "xl/sharedStrings.xml"
$rels->{"sst"}
Truth table100%2/2
A
A= $rels->{"sst"}
Aresult
11
00
3167 326 ▶ return defined $ss ? _xlsx_sst_xs($$ss) : [];
Branch: 1 branch
BranchTrueFalseTotal
defined $ss ? : 66 260 326
3168 }
3169  
3170 # The workbook's worksheets, in document order, as a list of
3171 # { name => $sheet_name, path => 'xl/worksheets/sheetN.xml' } hashrefs. The path
3172 # is resolved through workbook.xml.rels ($rels, from _xlsx_rels(), read here
3173 # when it is not given); a sheet with no resolvable relationship (or a workbook
3174 # with no metadata at all) falls back to a positional sheetN.xml.
3175 #
3176 # An element may carry a namespace prefix -- <x:sheet>, as the Open XML SDK
3177 # writes them -- and the relationship id is matched by its local name, since
3178 # the prefix bound to the relationships namespace is the writer's choice. See
3179 # xlsx_ns_prefix() in LikeR.xs for the worksheet side of the same thing.
3180 329 ▶sub _xlsx_sheets {
Subroutine _xlsx_sheets: called CC=9 SCAR=22.7
3181 329 my ($file, $rels) = @_;
3182 329 ▶ $rels ||= _xlsx_rels($file);
Condition66%2/3
$rels ||= _xlsx_rels($file)
l!l&&r!l&&!r
32810
MC/DC0%0/2
$rels ||= _xlsx_rels($file)
$rels _xlsx_rels($file)
Truth table66%2/3
A ||= B
A= $relsB= _xlsx_rels($file)
ABresult
1X1
011
000
3183 329 my $target = $rels->{target};
3184 329 my @sheets;
3185 329 ▶ if (defined(my $wb = _unzip_member($file, 'xl/workbook.xml'))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $wb = _unzip_member($file, "xl/workbook.xml"))) 329 0 329
3186 329 for my $at (_xml_start_tags($$wb, 'sheet')) {
3187 399 my $name = _xml_attr($at, 'name');
3188 399 my $rid = _xml_attr($at, '[\w.-]+:id');
3189 push @sheets, {
3190 name => defined $name ? _xml_unescape($name) : undef,
3191 399 ▶ path => defined $rid ? $target->{$rid} : undef,
Branch: 2 branches
BranchTrueFalseTotal
defined $name ? : 397 2 399
defined $rid ? : 399 0 399
3192 };
3193 }
3194 }
3195 329 ▶ @sheets = ({ name => undef, path => undef }) unless @sheets; # no metadata
Branch: 1 branch
BranchTrueFalseTotal
unless @sheets 0 329 329
3196 # any sheet still lacking a path falls back to its positional worksheet file
3197 $sheets[$_]{path} //= 'xl/worksheets/sheet' . ($_ + 1) . '.xml'
3198 329 ▶ for 0 .. $#sheets;
Condition50%1/2
$sheets[$_]{"path"} //= 'xl/worksheets/sheet' . ($_ + 1) . '.xml'
l!l
3990
MC/DC0%0/1
$sheets[$_]{"path"} //= 'xl/worksheets/sheet' . ($_ + 1) . '.xml'
$sheets[$_]{"path"}
Truth table50%1/2
A
A= $sheets[$_]{"path"}
Aresult
11
00
3199 329 return \@sheets;
3200 }
3201  
3202 # Resolve a 'sheet' argument (undef -> first; a name; or a 1-based index) to one
3203 # of the hashrefs from _xlsx_sheets, dying with a clear message on a bad request.
3204 #
3205 # A name is tried first. A workbook read whole comes back keyed by sheet name,
3206 # and up to 0.3213 any all-digit 'sheet' was an index, so of sheets "2024" and
3207 # "2023" neither key could be asked for again ("sheet index 2023 is out of
3208 # range"), and with sheets "2" and "1", sheet => '1' quietly returned "2". The
3209 # name is compared as bytes (see _as_bytes), which is how it was read.
3210 288 ▶sub _xlsx_choose_sheet {
Subroutine _xlsx_choose_sheet: called CC=8 SCAR=20.9
3211 288 my ($file, $sheets, $sheet) = @_;
3212 288 ▶ return $sheets->[0] unless defined $sheet;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sheet 260 28 288
3213 28 my $want = _as_bytes($sheet);
3214 28 ▶ my ($chosen) = grep { defined $_->{name} && $_->{name} eq $want } @$sheets;
Condition66%2/3
defined $_->{'name'} && $_->{'name'} eq $want
!ll&&!rl&&r
02922
MC/DC50%1/2
defined $_->{'name'} && $_->{'name'} eq $want
defined $_->{'name'} $_->{'name'} eq $want
Truth table66%2/3
A && B
A= defined $_->{'name'}B= $_->{'name'} eq $want
ABresult
0X0
100
111
3215 28 ▶ return $chosen if $chosen;
Branch: 1 branch
BranchTrueFalseTotal
if $chosen 22 6 28
3216 6 ▶ if ($want =~ /\A[0-9]+\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($want =~ /\A[0-9]+\z/) 4 2 6
3217 4 ▶ die "read_table: sheet index $want is out of range (1..${\ scalar @$sheets}), "
Branch: 1 branch
BranchTrueFalseTotal
if $want < 1 or $want > @$sheets 2 2 4
Condition66%2/3
$want < 1 or $want > @$sheets
l!l&&r!l&&!r
022
MC/DC50%1/2
$want < 1 or $want > @$sheets
$want < 1 $want > @$sheets
Truth table66%2/3
A or B
A= $want < 1B= $want > @$sheets
ABresult
1X1
011
000
3218 . "and no sheet is named '$want', in $file\n"
3219 if $want < 1 || $want > @$sheets;
3220 2 return $sheets->[$want - 1];
3221 }
3222 die "read_table: sheet '$want' not found in $file (have: "
3223 2 ▶ . join(', ', map { defined $_->{name} ? "'$_->{name}'" : '?' } @$sheets)
Branch: 1 branch
BranchTrueFalseTotal
defined $_->{'name'} ? : 4 0 4
3224 . ")\n";
3225 }
3226  
3227 # Parse one worksheet, invoking $callback->(\@fields) once per non-empty row
3228 # (header row included) with all rows padded to the same width -- the same
3229 # contract _parse_csv_file offers read_table's callback, and the same $plan
3230 # fast path: once read_table has filled the plan in, the rows are assembled in
3231 # XS and the callback is not called again. $sst is the shared strings arrayref
3232 # from _xlsx_shared_strings.
3233 #
3234 # The part is decompressed here and parsed by xlsx_ws_scan() in LikeR.xs, which
3235 # is also where the reason it is not done in perl any more is written down.
3236 369 ▶sub _parse_xlsx_sheet {
Subroutine _parse_xlsx_sheet: called CC=2 SCAR=7.0
3237 369 my ($file, $sst, $path, $callback, $plan) = @_;
3238 369 my $ws = _unzip_member($file, $path);
3239 369 ▶ die "read_table: could not read worksheet '$path' in $file\n"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ws 0 369 369
3240 unless defined $ws;
3241 369 _parse_xlsx_sheet_xs($$ws, $sst, $callback, $plan);
3242 301 return;
3243 }
3244  
3245 # True when a sep regex is pandas' whitespace-delimited spelling. pandas reads
3246 # sep=r"\s+" as delim_whitespace, not as a plain re.split(): leading and
3247 # trailing whitespace on a line make no empty field (pandas 3.0.4,
3248 # tests/io/parser/common/test_common_basic.py, test_ignore_leading_whitespace
3249 # and test_whitespace_regex_separator). R's read.table(sep = "") and Perl's own
3250 # split ' ' treat whitespace the same way. Only the pattern text counts, so
3251 # qr/\s+/x qualifies and qr/[ \t]+/ does not.
3252 829 ▶sub _sep_re_is_ws {
Subroutine _sep_re_is_ws: called CC=2 SCAR=7.0
3253 829 my ($re) = @_;
3254 829 my ($pat) = re::regexp_pattern($re);
3255 829 ▶ return defined $pat && $pat eq '\s+';
Condition66%2/3
defined $pat && $pat eq '\\s+'
!ll&&!rl&&r
0659170
MC/DC50%1/2
defined $pat && $pat eq '\\s+'
defined $pat $pat eq '\\s+'
Truth table66%2/3
A && B
A= defined $patB= $pat eq '\\s+'
ABresult
0X0
100
111
3256 }
3257  
3258 # Compressed input for read_table
3259 #
3260 # A gzip or bzip2 file is recognised by its first bytes, not its name, the way
3261 # R's file() recognises one for read.table: do_url() in src/main/connections.c
3262 # calls comp_type_from_memory() on the first bytes of a file opened "r" or
3263 # "rt" (R-devel 4.7.0, r-source commit b3c233dd). pandas' compression='infer'
3264 # goes by the extension instead; sniffing reads every file pandas would, and a
3265 # compressed one that has lost its suffix too.
3266 #
3267 # The magic numbers are that function's:
3268 # gzip 1f 8b, RFC 1952 section 2.3.1's ID1 and ID2. A method other than
3269 # deflate after them is reported by zlib as corrupt data, as R's
3270 # gzio.h check_header() refuses it.
3271 # bzip2 "BZh" and a block size of '1'..'9', then the 48-bit magic of either
3272 # the first block (0x314159265359, BCD pi) or the end-of-stream
3273 # marker (0x177245385090, BCD sqrt(pi)), which is all an empty stream
3274 # holds -- bzip2 1.0.8 compress.c, BZ_HDR_h/BZ_HDR_0 and the
3275 # bsPutUChar() runs. R added the last six bytes in PR#18768, after a
3276 # text file that began "BZh" was taken for bzip2.
3277 # Neither is how text begins: 0x1f is a control character, 0x8b is not ASCII
3278 # and cannot start a UTF-8 sequence, and the bzip2 test runs to ten bytes.
3279 #
3280 # The same function also recognises four formats this module cannot inflate,
3281 # and they are sniffed only so that read_table can say so: up to 0.3213 an
3282 # .xz file was parsed as text and reported as "Alignment error on x.csv.xz data
3283 # row 2", which sent the reader looking for a ragged row. Their magic numbers
3284 # are comp_type_from_memory()'s as well:
3285 # xz fd "7zXZ" (the .xz container)
3286 # lzma ff "LZMA", or 5d 00 00 80 00 (the two legacy LZMA headers)
3287 # zstd 28 b5 2f fd (RFC 8878 section 3.1.1's Magic_Number)
3288 # lzop 89 "LZO"
3289 # Each begins with a byte that no text file does, or, for the 5d header, holds
3290 # NUL bytes, which no text file does either.
3291 8280 ▶sub _sniff_compression {
Subroutine _sniff_compression: called CC=9 SCAR=22.0
3292 8280 my ($file) = @_;
3293 8280 ▶ open my $fh, '<', $file or return ''; # read_table's own open reports it
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $file or return "" 8280 0 8280
3294 8280 binmode $fh;
3295 8280 my $got = read $fh, my $head, 10;
3296 8280 close $fh;
3297 8280 ▶ return '' unless $got;
Branch: 1 branch
BranchTrueFalseTotal
unless $got 2 8278 8280
3298 8278 ▶ return 'gzip' if $head =~ /\A\x1f\x8b/;
Branch: 1 branch
BranchTrueFalseTotal
if $head =~ /\A\x1f\x8b/ 673 7605 8278
3299 7605 ▶ return 'bzip2'
Branch: 1 branch
BranchTrueFalseTotal
if $head =~ /\ABZh[1-9](?:\x31\x41\x59\x26\x53\x59|\x17\x72\x45\x38\x50\x90)/ 496 7109 7605
3300 if $head =~ /\ABZh[1-9](?:\x31\x41\x59\x26\x53\x59|\x17\x72\x45\x38\x50\x90)/;
3301 7109 ▶ return 'xz' if $head =~ /\A\xFD7zXZ/;
Branch: 1 branch
BranchTrueFalseTotal
if $head =~ /\A\xFD7zXZ/ 1 7108 7109
3302 7108 ▶ return 'lzma' if $head =~ /\A(?:\xFFLZMA|\x5D\x00\x00\x80\x00)/;
Branch: 1 branch
BranchTrueFalseTotal
if $head =~ /\A(?:\xFFLZMA|\x5D\x00\x00\x80\x00)/ 2 7106 7108
3303 7106 ▶ return 'zstd' if $head =~ /\A\x28\xB5\x2F\xFD/;
Branch: 1 branch
BranchTrueFalseTotal
if $head =~ /\A\x28\xB5\x2F\xFD/ 1 7105 7106
3304 7105 ▶ return 'lzop' if $head =~ /\A\x89LZO/;
Branch: 1 branch
BranchTrueFalseTotal
if $head =~ /\A\x89LZO/ 1 7104 7105
3305 7104 return '';
3306 }
3307  
3308 # True when $file's lines end in a bare CR, as classic Mac OS wrote them.
3309 # R's scan() and pandas' C tokenizer both take a lone CR as a line end (R
3310 # 4.6.1 tests/reg-tests-1a.R, PR#2469; pandas 3.0.4
3311 # tests/io/parser/test_textreader.py, test_cr_delimited); _parse_csv_file()
3312 # reads LF and CRLF only, and handed such a file back as one long header.
3313 #
3314 # The file is taken for one only when what is read of it holds a CR and no LF
3315 # outside a quoted field, so an LF or CRLF file with a stray CR in its first
3316 # line still reads as it always has. A line break inside quotes is part of a
3317 # cell, not a line end: up to 0.33 any LF counted, so a CR file with one quoted
3318 # cell holding an LF was split on LF, and 'a,b\r1,"x\ny"\r2,z\r' came back as
3319 # the one row a, b1, "x\ny2", z where R 4.6.1's read.csv() and pandas 3.0.4's
3320 # read_csv() both read a, b over (1, "x\ny") and (2, z). Quotes are counted
3321 # as _parse_csv_file() counts them -- every '"' opens or closes one, a doubled
3322 # '""' doing both -- unless $quote is false (quote => ''). A CR that ends the
3323 # block may be the first half of a CRLF, so the block is extended past it. 64
3324 # KB is one read; reading stops at 1 MB, which bounds the cost for a file with
3325 # no line end in its first megabyte, and reads that one as LF, as it was read
3326 # before.
3327 8275 ▶sub _eol_is_cr {
Subroutine _eol_is_cr: called CC=18 SCAR=29.0
3328 8275 my ($file, $codec, $quote) = @_;
3329 # not on a compressed handle: it is opened :raw already, and a bare binmode
3330 # is :raw, which can pop the via layer that is doing the decompressing
3331 8275 ▶ my $fh = $codec ? _open_decompressed($file, $codec) : _open_read($file);
Branch: 1 branch
BranchTrueFalseTotal
$codec ? : 1169 7106 8275
3332 8274 ▶ binmode $fh unless $codec;
Branch: 1 branch
BranchTrueFalseTotal
unless $codec 7106 1168 8274
3333 8274 my $buf = '';
3334 8274 my $eof = 0;
3335 8274 my $in_q = 0; # the scan is inside a quoted field
3336 8274 my $cr = 0; # a CR outside quotes has been seen
3337 8274 my $lf = 0; # an LF outside quotes has been seen
3338 8274 ▶ my $class = $quote ? qr/["\r\n]/ : qr/[\r\n]/;
Branch: 1 branch
BranchTrueFalseTotal
$quote ? : 8096 178 8274
3339 8274 pos($buf) = 0;
3340 8274 ▶ while (length $buf < 1 << 20) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $buf < 1048576) 8318 0 8318
3341 8318 ▶ my $at = pos($buf) // 0; # where the scan resumes in the grown block
Condition50%1/2
pos $buf // 0
l!l
83180
MC/DC0%0/1
pos $buf // 0
pos $buf
Truth table50%1/2
A
A= pos $buf
Aresult
11
00
3342 8318 my $got = read $fh, $buf, 1 << 16, length $buf;
3343 7256 ▶ if (!$got) { $eof = 1; last }
Branch: 1 branch
BranchTrueFalseTotal
unless ($got) 46 7210 7256
3344 7210 pos($buf) = $at;
3345 7210 ▶ while ($buf =~ /$class/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($buf =~ /$class/g) 16156 54 16210
3346 16156 my $c = substr($buf, pos($buf) - 1, 1);
3347 16156 ▶ if ($c eq '"') { $in_q = !$in_q }
Branch: 3 branches
BranchTrueFalseTotal
if ($c eq '"') { } 8755 7401 16156
elsif ($in_q) { } 9 7392 7401
elsif ($c eq "\n") { } 7156 236 7392
3348 9 elsif ($in_q) { next }
3349 7156 elsif ($c eq "\n") { $lf = 1; last }
3350 236 else { $cr = 1 }
3351 }
3352 7210 ▶ last if $lf;
Branch: 1 branch
BranchTrueFalseTotal
if $lf 7156 54 7210
3353 54 ▶ last if $cr && substr($buf, -1) ne "\r";
Branch: 1 branch
BranchTrueFalseTotal
if $cr and substr($buf, -1) ne "\r" 10 44 54
Condition100%3/3
$cr and substr($buf, -1) ne "\r"
!ll&&!rl&&r
53910
MC/DC100%2/2
$cr and substr($buf, -1) ne "\r"
$cr substr($buf, -1) ne "\r"
Truth table100%3/3
A and B
A= $crB= substr($buf, -1) ne "\r"
ABresult
0X0
100
111
3354 44 pos($buf) = length $buf;
3355 }
3356 7212 _close($fh);
3357 7212 ▶ return 0 if $lf || !$cr;
Branch: 1 branch
BranchTrueFalseTotal
if $lf or not $cr 7164 48 7212
Condition100%3/3
$lf or not $cr
l!l&&r!l&&!r
7156848
MC/DC100%2/2
$lf or not $cr
$lf not $cr
Truth table100%3/3
A or not(B)
A= $lfB= not $cr
ABresult
1X1
011
000
3358 48 ▶ return 1 if $eof || substr($buf, -1) ne "\r";
Branch: 1 branch
BranchTrueFalseTotal
if $eof or substr($buf, -1) ne "\r" 48 0 48
Condition66%2/3
$eof or substr($buf, -1) ne "\r"
l!l&&r!l&&!r
38100
MC/DC0%0/2
$eof or substr($buf, -1) ne "\r"
$eof substr($buf, -1) ne "\r"
Truth table66%2/3
A or B
A= $eofB= substr($buf, -1) ne "\r"
ABresult
1X1
011
000
3359 0 return 0; # 1 MB ending in a CR whose partner was not read: as before
3360 }
3361  
3362 # An input handle on the decompressed text of $file, streamed through the
3363 # PerlIO::via layer below, so a multi-gigabyte .tsv.gz is inflated a buffer
3364 # at a time as the parser reads it rather than into memory first. Both halves
3365 # are core: PerlIO::via since 5.8, Compress::Raw::Zlib since 5.9.4 and
3366 # Compress::Raw::Bzip2 since 5.10.1, the oldest perl this supports. The bzip2
3367 # one is still loaded only when a bzip2 file is read, and its absence reported
3368 # as what it is: some vendors' perls ship core modules as separate packages.
3369 1367 ▶sub _open_decompressed {
Subroutine _open_decompressed: called CC=4 SCAR=13.9
3370 1367 my ($file, $codec) = @_;
3371 1367 require PerlIO::via;
3372 1367 my $layer;
3373 1367 ▶ if ($codec eq 'gzip') {
Branch: 1 branch
BranchTrueFalseTotal
if ($codec eq 'gzip') { } 832 535 1367
3374 832 require Compress::Raw::Zlib;
3375 832 $layer = 'Stats::LikeR::_Gunzip';
3376 } else {
3377 535 ▶ eval { require Compress::Raw::Bzip2; 1 }
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require Compress::Raw::Bzip2; 1 } } or die qq[read_table: "$file" is bzip2-compressed, and reading it ] . "needs Compress::Raw::Bzip2, which is not installed\n" 534 1 535
3378 or die "read_table: \"$file\" is bzip2-compressed, and reading it "
3379 . "needs Compress::Raw::Bzip2, which is not installed\n";
3380 534 $layer = 'Stats::LikeR::_Bunzip2';
3381 }
3382 # PUSHED is given no file name, so it reads this one while the open runs.
3383 1366 local $Stats::LikeR::_Decompress::name = $file;
3384 1366 my $fh;
3385 1366 ▶ open $fh, "<:raw:via($layer)", $file
Branch: 1 branch
BranchTrueFalseTotal
open $fh, "<:raw:via($layer)", $file or _io_die("Can't open '${file}' for reading: '$!'") 1366 0 1366
3386 or _io_die("Can't open '$file' for reading: '$!'");
3387 1366 return $fh;
3388 }
3389  
3390 # A copy of $s as the bytes read_table compares it with. Every field comes
3391 # back from the file as the bytes it holds -- UTF-8 ones for an .xlsx and for
3392 # any CSV written as UTF-8 -- and a literal sep or comment marker already meets
3393 # them that way, since the parser takes its bytes from SvPV. A name or token
3394 # the caller holds as characters (under `use utf8`, or decoded from anywhere)
3395 # is encoded the same way here before it is compared with a field. Up to
3396 # 0.3213 na_strings => "\x{2014}", sheet => "Donn\x{e9}es" and a filter or
3397 # row_names key of the same kind never matched what the file plainly held. A
3398 # string perl holds as bytes is returned as it is.
3399 1956 ▶sub _as_bytes {
Subroutine _as_bytes: called CC=3 SCAR=11.0
3400 1956 my ($s) = @_;
3401 1956 ▶ utf8::encode($s) if defined $s && utf8::is_utf8($s);
Branch: 1 branch
BranchTrueFalseTotal
if defined $s and utf8::is_utf8($s) 17 1939 1956
Condition66%2/3
defined $s and utf8::is_utf8($s)
!ll&&!rl&&r
0193917
MC/DC50%1/2
defined $s and utf8::is_utf8($s)
defined $s utf8::is_utf8($s)
Truth table66%2/3
A and B
A= defined $sB= utf8::is_utf8($s)
ABresult
0X0
100
111
3402 1956 return $s;
3403 }
3404  
3405 8709 ▶sub read_table {
Subroutine read_table: called CC=160 SCAR=50.9
Pod documented
3406 8709 my $file = shift;
3407 # without a name, -f would die on the fatal "uninitialized" warning instead
3408 8709 ▶ _usage_error('read_table', 'file name missing') unless defined $file;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file 2 8707 8709
3409 8707 ▶ die "read_table: \"$file\" is not a file\n" unless -f $file;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $file 4 8703 8707
3410 8703 ▶ die "read_table: \"$file\" is not readable\n" unless -r $file;
Branch: 1 branch
BranchTrueFalseTotal
unless -r $file 0 8703 8703
3411  
3412 8703 my %input_args = @_;
3413 8703 ▶ if (exists $input_args{delim}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $input_args{"delim"}) 7 8696 8703
3414 # FIX: sep + delim together used to silently prefer delim
3415 _usage_error('read_table', "pass either 'sep' or 'delim', not both")
3416 7 ▶ if exists $input_args{sep};
Branch: 1 branch
BranchTrueFalseTotal
if exists $input_args{"sep"} 3 4 7
3417 4 $input_args{sep} = delete $input_args{delim};
3418 }
3419 # One option, three names: 'na_strings' is R's na.strings, 'na_values' is
3420 # pandas', and 'undef_val' is what write_table calls the same token on the
3421 # way out, so a round trip can be written with one spelling on both halves.
3422 # Only one may be given, for the same reason 'sep' and 'delim' may not both
3423 # be. The names are checked in a fixed order so the message is
3424 # deterministic. The dotted 'na.strings' and 'undef.val' are refused as
3425 # unknown arguments.
3426 {
3427 8700 my @given = grep { exists $input_args{$_} }
3428 qw(na_strings na_values undef_val);
3429 _usage_error('read_table', "pass only one of 'na_strings', 'na_values' or "
3430 8700 ▶ . "'undef_val'; got " . join(', ', map { "'$_'" } @given))
Branch: 1 branch
BranchTrueFalseTotal
if @given > 1 7 8693 8700
3431 if @given > 1;
3432 8693 ▶ $input_args{'na_strings'} = delete $input_args{ $given[0] } if @given;
Branch: 1 branch
BranchTrueFalseTotal
if @given 272 8421 8693
3433 }
3434  
3435 # An Excel workbook by its name: .xlsx, and the macro-enabled .xlsm and the
3436 # two template forms .xltx and .xltm, which hold their sheets in the same
3437 # parts (ECMA-376 differs only in the main part's content type). Up to
3438 # 0.3213 only .xlsx was recognised, and an .xlsm was read as text and
3439 # reported as an alignment error in a ragged row.
3440 8693 my $is_xlsx = $file =~ /\.xl(?:sx|sm|tx|tm)\z/i;
3441 8693 ▶ my $codec = $is_xlsx ? '' : _sniff_compression($file);
Branch: 1 branch
BranchTrueFalseTotal
$is_xlsx ? : 413 8280 8693
3442 8693 ▶ die "read_table: \"$file\" is $codec-compressed, which read_table cannot "
Branch: 1 branch
BranchTrueFalseTotal
if $codec =~ /\A(?:xz|lzma|zstd|lzop)\z/ 5 8688 8693
3443 . "decompress; decompress it first, or recompress it with gzip or bzip2\n"
3444 if $codec =~ /\A(?:xz|lzma|zstd|lzop)\z/;
3445 # quote => '' makes '"' text, so it opens no quoted field for the line-end
3446 # scan either; the value itself is checked further down
3447 my $cr_eol = $is_xlsx ? 0 : _eol_is_cr($file, $codec,
3448 8688 ▶ !(defined $input_args{quote} && !ref $input_args{quote} && $input_args{quote} eq '')); # lines end in a bare CR
Branch: 1 branch
BranchTrueFalseTotal
$is_xlsx ? : 413 8275 8688
Condition100%6/6
defined $input_args{'quote'} && !ref($input_args{'quote'}) && $input_args{'quote'} eq ''
!ll&&!rl&&r
80934178
defined $input_args{"quote"} and not ref $input_args{"quote"}
!ll&&!rl&&r
80921182
MC/DC100%3/3
defined $input_args{'quote'} && !ref($input_args{'quote'}) && $input_args{'quote'} eq ''
defined $input_args{"quote"} not ref $input_args{"quote"} $input_args{'quote'} eq ''
Truth table100%4/4
A and not(B) && C
A= defined $input_args{"quote"}B= not ref $input_args{"quote"}C= $input_args{'quote'} eq ''
ABCresult
0XX0
10X0
1100
1111
3449 7625 ▶ my $eol = $cr_eol ? "\r" : "\n";
Branch: 1 branch
BranchTrueFalseTotal
$cr_eol ? : 48 7577 7625
3450 # The extension only picks the default sep, and a compressed file's is
3451 # that of the text inside it: x.tsv.gz is tab-separated.
3452 7625 ▶ (my $sep_name = $file) =~ s/\.(?:gz|bgz|bz2)\z//i if $codec;
Branch: 1 branch
BranchTrueFalseTotal
if $codec 106 7519 7625
3453 7625 ▶ $sep_name = $file unless $codec;
Branch: 1 branch
BranchTrueFalseTotal
unless $codec 7519 106 7625
3454 # A VCF (VCF 4.2/4.3 spec, section 1: "##" meta-information lines, then
3455 # one "#CHROM POS ID REF ALT QUAL FILTER INFO [FORMAT sample...]" header
3456 # line, then tab-separated data) is read with its own defaults: sep is a
3457 # tab, the comment marker is "##" so that only the meta lines are
3458 # comments, and the "#" in front of CHROM comes off (see $finalize_header).
3459 7625 my $is_vcf = $sep_name =~ /\.vcf\z/i;
3460 7625 ▶ my $default_sep = $is_vcf || $sep_name =~ /\.tsv\z/i ? "\t" : ',';
Branch: 1 branch
BranchTrueFalseTotal
$is_vcf || $sep_name =~ /\.tsv\z/i ? : 885 6740 7625
Condition100%3/3
$is_vcf or $sep_name =~ /\.tsv\z/i
l!l&&r!l&&!r
2466396740
MC/DC100%2/2
$is_vcf or $sep_name =~ /\.tsv\z/i
$is_vcf $sep_name =~ /\.tsv\z/i
Truth table100%3/3
A or B
A= $is_vcfB= $sep_name =~ /\.tsv\z/i
ABresult
1X1
011
000
3461 7625 ▶ my %args = (
Branch: 1 branch
BranchTrueFalseTotal
$is_vcf ? : 246 7379 7625
3462 sep => $default_sep, comment => $is_vcf ? '##' : '#', %input_args,
3463 );
3464  
3465 7625 my %allowed_args = map { $_ => 1 } (
3466 'comment', 'output_type', 'filter', 'row_names', 'sep',
3467 'auto_row_names', 'sheet', 'na_strings', 'header', 'col_names', 'quote',
3468 'explode', 'colClasses', 'drop_cols', 'keep_cols',
3469 # private, undocumented: the multi-sheet expansion passes an already
3470 # parsed worksheet list / shared-string table to each per-sheet recursion
3471 # so a big sharedStrings.xml is not re-decompressed once per worksheet.
3472 '_xlsx_sheets', '_sst', '_sheet_ix',
3473 # private, for t/read_table.filter.t: keep every row in the perl
3474 # closure, which is the reference the parser's filter path is held to
3475 '_closure',
3476 );
3477 7625 my @undef_args = sort grep { !$allowed_args{$_} } keys %args;
3478 7625 ▶ _usage_error('read_table', "unknown argument(s): @undef_args") if @undef_args;
Branch: 1 branch
BranchTrueFalseTotal
if @undef_args 5 7620 7625
3479 # explode => 1, the default for a VCF read with its header: every sample
3480 # column is split on ':' into one column per FORMAT key, and the table is
3481 # a hoh keyed by CHROM:POS:REF:ALT (see _vcf_explode). Without a header
3482 # there is no FORMAT column to split by, so header => 0 reads it plain.
3483 7620 ▶ if (exists $args{explode}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"explode"}) 171 7449 7620
3484 171 ▶ _usage_error('read_table', "'explode' applies only to a VCF (.vcf, .vcf.gz or "
Branch: 1 branch
BranchTrueFalseTotal
unless $is_vcf 1 170 171
3485 . ".vcf.bgz), and \"$file\" is not named as one")
3486 unless $is_vcf;
3487 _usage_error('read_table', "'explode' must be 0 or 1")
3488 unless defined $args{explode} && !ref $args{explode}
3489 170 ▶ && $args{explode} =~ /\A[01]?\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"explode"} and not ref $args{"explode"} and $args{"explode"} =~ /\A[01]?\z/ 2 168 170
Condition83%5/6
defined $args{"explode"} and not ref $args{"explode"} and $args{"explode"} =~ /\A[01]?\z/
!ll&&!rl&&r
11168
defined $args{"explode"} and not ref $args{"explode"}
!ll&&!rl&&r
10169
MC/DC66%2/3
defined $args{"explode"} and not ref $args{"explode"} and $args{"explode"} =~ /\A[01]?\z/
defined $args{"explode"} not ref $args{"explode"} $args{"explode"} =~ /\A[01]?\z/
Truth table75%3/4
A and not(B) and C
A= defined $args{"explode"}B= not ref $args{"explode"}C= $args{"explode"} =~ /\A[01]?\z/
ABCresult
0XX0
10X0
1100
1111
3490 }
3491 my $explode = $is_vcf && ($args{explode} // 1)
3492 7617 ▶ && !(exists $args{header} && defined $args{header} && !$args{header});
Condition85%12/14
$is_vcf && ($args{'explode'} // 1) && !(exists $args{'header'} && defined $args{'header'} && !$args{'header'})
!ll&&!rl&&r
7534578
$is_vcf and $args{"explode"} // 1
!ll&&!rl&&r
737316183
$args{"explode"} // 1
l!l
16876
exists $args{'header'} && defined $args{'header'} && !$args{'header'}
!ll&&!rl&&r
7805
exists $args{"header"} and defined $args{"header"}
!ll&&!rl&&r
7805
MC/DC20%1/5
$is_vcf && ($args{'explode'} // 1) && !(exists $args{'header'} && defined $args{'header'} && !$args{'header'})
$is_vcf $args{"explode"} exists $args{"header"} defined $args{"header"} !$args{'header'}
Truth table33%2/6
A and B && not(C and D && not(E))
A= $is_vcfB= $args{"explode"}C= exists $args{"header"}D= defined $args{"header"}E= !$args{'header'}
ABCDEresult
0XXXX0
10XXX0
111110
110XX1
1110X1
111101
3493 # keep_cols and drop_cols: the columns kept, and the columns left out, by
3494 # name. Each is a column name, a qr//, or a list of either, tried against
3495 # each column's name once the header is fixed (see $finalize_header); undef
3496 # is no selection at all. A plain string is a whole name, compared with eq,
3497 # and a qr// is a pattern. A column is stored when keep_cols, if given,
3498 # names or matches it and drop_cols does not. It is what keeps a wide
3499 # file's unwanted columns from ever being stored, so the entries are checked
3500 # here, before the file is opened.
3501 7617 my %sel; # keep / drop => { names => [...], name => {...}, rx => [...] }; absent when not given
3502 7617 for my $opt ('keep_cols', 'drop_cols') {
3503 15225 ▶ next unless defined $args{$opt};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{$opt} 13097 2128 15225
3504 2128 ▶ my @ent = ref $args{$opt} eq 'ARRAY' ? @{ $args{$opt} } : ($args{$opt});
Branch: 1 branch
BranchTrueFalseTotal
ref $args{$opt} eq 'ARRAY' ? : 1091 1037 2128
3505 _usage_error('read_table', "'$opt' must be a column name, a qr// regex, or an "
3506 . "ARRAY reference of them")
3507 2128 ▶ if !@ent || grep { !defined($_) || (ref($_) && ref($_) ne 'Regexp') } @ent;
Branch: 1 branch
BranchTrueFalseTotal
if not @ent or grep {not defined $_ or ref $_ and ref $_ ne "Regexp";} @ent 20 2108 2128
Condition100%9/9
not @ent or grep {not defined $_ or ref $_ and ref $_ ne "Regexp";} @ent
l!l&&r!l&&!r
4162108
!defined($_) || ref $_ && ref $_ ne 'Regexp'
l!l&&r!l&&!r
5114401
ref $_ and ref $_ ne "Regexp"
!ll&&!rl&&r
3313108811
MC/DC100%2/2
not @ent or grep {not defined $_ or ref $_ and ref $_ ne "Regexp";} @ent
not @ent grep {not defined $_ or ref $_ and ref $_ ne "Regexp";} @ent
MC/DC100%3/3
!defined($_) || ref $_ && ref $_ ne 'Regexp'
!defined($_) ref $_ ref $_ ne "Regexp"
Truth table100%3/3
not(A) or B
A= not @entB= grep {not defined $_ or ref $_ and ref $_ ne "Regexp";} @ent
ABresult
1X1
011
000
Truth table100%4/4
not(A) || B and C
A= !defined($_)B= ref $_C= ref $_ ne "Regexp"
ABCresult
1XX1
0111
00X0
0100
3508 # The names, once each and in the order given, which is the order a
3509 # name that is no column is reported in. {name} is what a header name
3510 # is looked up in: a hash key is compared as the characters it spells,
3511 # so a name held as characters and the same name held as Latin-1 bytes
3512 # are one key.
3513 2108 my (@names, %name);
3514 2108 for my $n (grep { !ref $_ } @ent) {
3515 3309 ▶ push @names, $n unless $name{$n}++;
Branch: 1 branch
BranchTrueFalseTotal
unless $name{$n}++ 3277 32 3309
3516 }
3517 $sel{ $opt eq 'keep_cols' ? 'keep' : 'drop' } = {
3518 names => \@names, name => \%name,
3519 # qr// is every column, on every perl: see _rx_for_match()
3520 2108 ▶ rx => [ map { _rx_for_match($_) } grep { ref $_ } @ent ],
Branch: 1 branch
BranchTrueFalseTotal
$opt eq 'keep_cols' ? : 931 1177 2108
3521 };
3522 }
3523 # An exploded VCF is built from the file's own columns by position (FORMAT
3524 # and each sample), so there is no column to choose until it has been
3525 # exploded, by which time it has been stored.
3526 7597 for my $opt (grep { defined $args{$_} } 'keep_cols', 'drop_cols') {
3527 2107 ▶ _usage_error('read_table', "'$opt' cannot be used with an exploded VCF; pass "
Branch: 1 branch
BranchTrueFalseTotal
if $explode 6 2101 2107
3528 . "explode => 0 to read the file's own columns")
3529 if $explode;
3530 }
3531 7591 ▶ my $otype = $args{'output_type'} // ($explode ? 'hoh' : 'aoh');
Branch: 1 branch
BranchTrueFalseTotal
$explode ? : 22 1594 1616
Condition100%2/2
$args{"output_type"} // ($explode ? "hoh" : "aoh")
l!l
59751616
MC/DC100%1/1
$args{"output_type"} // ($explode ? "hoh" : "aoh")
$args{"output_type"}
Truth table100%2/2
A
A= $args{"output_type"}
Aresult
11
00
3532 7591 ▶ _usage_error('read_table', "output_type \"$otype\" isn't allowed (aoa, aoh, hoa, hoh)")
Branch: 1 branch
BranchTrueFalseTotal
unless $otype =~ /^(?:aoa|aoh|hoa|hoh)$/ 4 7587 7591
3533 unless $otype =~ m/^(?:aoa|aoh|hoa|hoh)$/;
3534 # Only a hoh is keyed by a column. An aoa is positional, and an aoh or hoa
3535 # keeps every column as data, so a row_names column would only be a column
3536 # like any other. Up to 0.3213 an aoh or hoa checked the name against the
3537 # header and then ignored it, which read as though it had done something.
3538 _usage_error('read_table', "'row_names' has no meaning for output_type \"$otype\"; "
3539 . "the row names column is read as an ordinary column")
3540 7587 ▶ if $otype ne 'hoh' && defined $args{'row_names'};
Branch: 1 branch
BranchTrueFalseTotal
if $otype ne "hoh" and defined $args{"row_names"} 18 7569 7587
Condition100%3/3
$otype ne "hoh" and defined $args{"row_names"}
!ll&&!rl&&r
992657718
MC/DC100%2/2
$otype ne "hoh" and defined $args{"row_names"}
$otype ne "hoh" defined $args{"row_names"}
Truth table100%3/3
A and B
A= $otype ne "hoh"B= defined $args{"row_names"}
ABresult
0X0
100
111
3541 # compared with the header's bytes, both here and in _vcf_explode()
3542 7569 ▶ $args{'row_names'} = _as_bytes($args{'row_names'}) if defined $args{'row_names'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $args{"row_names"} 394 7175 7569
3543 # 'sheet' picks a worksheet, which only an .xlsx has; it was ignored on
3544 # any other file up to 0.3213, as 'explode' is refused on a non-VCF.
3545 _usage_error('read_table', "'sheet' applies only to an .xlsx, and \"$file\" is not "
3546 . "named as one")
3547 7569 ▶ if defined $args{sheet} && !$is_xlsx;
Branch: 1 branch
BranchTrueFalseTotal
if defined $args{"sheet"} and not $is_xlsx 1 7568 7569
Condition100%3/3
defined $args{"sheet"} and not $is_xlsx
!ll&&!rl&&r
7540281
MC/DC100%2/2
defined $args{"sheet"} and not $is_xlsx
defined $args{"sheet"} not $is_xlsx
Truth table100%3/3
A and not(B)
A= defined $args{"sheet"}B= not $is_xlsx
ABresult
0X0
100
111
3548 # A qr// separator is found by perl's regex engine; any other sep is
3549 # a literal string, as it has always been. A regex is not looked at for an
3550 # .xlsx, where a literal sep is not either.
3551 7568 ▶ if (ref $args{sep} && ref $args{sep} ne 'Regexp') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args{"sep"} and ref $args{"sep"} ne "Regexp") 2 7566 7568
Condition100%3/3
ref $args{"sep"} and ref $args{"sep"} ne "Regexp"
!ll&&!rl&&r
67448222
MC/DC100%2/2
ref $args{"sep"} and ref $args{"sep"} ne "Regexp"
ref $args{"sep"} ref $args{"sep"} ne "Regexp"
Truth table100%3/3
A and B
A= ref $args{"sep"}B= ref $args{"sep"} ne "Regexp"
ABresult
0X0
100
111
3552 _usage_error('read_table', "'sep' must be a string or a qr// regex, not a "
3553 2 . ref($args{sep}) . " reference");
3554 }
3555 7566 ▶ my $sep_re = (ref $args{sep} && !$is_xlsx) ? $args{sep} : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $args{'sep'} && !$is_xlsx ? : 821 6745 7566
Condition100%3/3
ref $args{"sep"} and not $is_xlsx
!ll&&!rl&&r
67441821
MC/DC100%2/2
ref $args{"sep"} and not $is_xlsx
ref $args{"sep"} not $is_xlsx
Truth table100%3/3
A and not(B)
A= ref $args{"sep"}B= not $is_xlsx
ABresult
0X0
100
111
3556 # Refused before the file is read, so that qr/\s*/ fails the same way
3557 # whatever the file holds. A pattern that matches nothing but an empty
3558 # string -- a bare lookahead, say -- passes this, and is refused where it
3559 # first matches one anywhere but at the start of a line.
3560 7566 ▶ _usage_error('read_table', "the sep regex $sep_re matches an empty string; it must "
Branch: 1 branch
BranchTrueFalseTotal
if $sep_re and "" =~ /\A(?:$sep_re)\z/ 1 7565 7566
Condition100%3/3
$sep_re and "" =~ /\A(?:$sep_re)\z/
!ll&&!rl&&r
67458201
MC/DC100%2/2
$sep_re and "" =~ /\A(?:$sep_re)\z/
$sep_re "" =~ /\A(?:$sep_re)\z/
Truth table100%3/3
A and B
A= $sep_reB= "" =~ /\A(?:$sep_re)\z/
ABresult
0X0
100
111
3561 . "match at least one character")
3562 if $sep_re && '' =~ /\A(?:$sep_re)\z/;
3563 # header => 0 is R's header = FALSE and pandas' header=None: the first line
3564 # is data, and the columns are named by 'col_names' or, as R names them,
3565 # V1, V2, ... 'col_names' with a header renames its columns, as in R.
3566 # '' is allowed as well as 0, since that is what perl's own false is:
3567 # header => ($n > 0) must not be refused when it is false.
3568 7565 my $want_header = 1;
3569 7565 ▶ if (exists $args{header}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"header"}) 295 7270 7565
3570 _usage_error('read_table', "'header' must be 0 or 1")
3571 unless defined $args{header} && !ref $args{header}
3572 295 ▶ && $args{header} =~ /\A[01]?\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"header"} and not ref $args{"header"} and $args{"header"} =~ /\A[01]?\z/ 4 291 295
Condition100%6/6
defined $args{"header"} and not ref $args{"header"} and $args{"header"} =~ /\A[01]?\z/
!ll&&!rl&&r
22291
defined $args{"header"} and not ref $args{"header"}
!ll&&!rl&&r
11293
MC/DC100%3/3
defined $args{"header"} and not ref $args{"header"} and $args{"header"} =~ /\A[01]?\z/
defined $args{"header"} not ref $args{"header"} $args{"header"} =~ /\A[01]?\z/
Truth table100%4/4
A and not(B) and C
A= defined $args{"header"}B= not ref $args{"header"}C= $args{"header"} =~ /\A[01]?\z/
ABCresult
0XX0
10X0
1100
1111
3573 291 ▶ $want_header = $args{header} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$args{'header'} ? : 1 290 291
3574 }
3575 7561 my $col_names = $args{'col_names'};
3576 7561 ▶ if (defined $col_names) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $col_names) 110 7451 7561
3577 110 ▶ _usage_error('read_table', "'col_names' must be an ARRAY reference of names")
Branch: 1 branch
BranchTrueFalseTotal
unless ref $col_names eq "ARRAY" and @$col_names 3 107 110
Condition100%3/3
ref $col_names eq "ARRAY" and @$col_names
!ll&&!rl&&r
21107
MC/DC100%2/2
ref $col_names eq "ARRAY" and @$col_names
ref $col_names eq "ARRAY" @$col_names
Truth table100%3/3
A and B
A= ref $col_names eq "ARRAY"B= @$col_names
ABresult
0X0
100
111
3578 unless ref $col_names eq 'ARRAY' && @$col_names;
3579 107 for my $n (@$col_names) {
3580 556 ▶ _usage_error('read_table', "'col_names' may only hold defined, plain strings")
Branch: 1 branch
BranchTrueFalseTotal
if not defined $n or ref $n 2 554 556
Condition100%3/3
not defined $n or ref $n
l!l&&r!l&&!r
11554
MC/DC100%2/2
not defined $n or ref $n
not defined $n ref $n
Truth table100%3/3
not(A) or B
A= not defined $nB= ref $n
ABresult
1X1
011
000
3581 if !defined $n || ref $n;
3582 }
3583 }
3584 # quote => '' is R's quote = "" and pandas' quoting=QUOTE_NONE: '"' is
3585 # ordinary text. Only '"' can quote, so it is the only other value; R's
3586 # read.table would also take "'", which neither parser here knows.
3587 7556 my $quote = 1;
3588 7556 ▶ if (exists $args{quote}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"quote"}) 185 7371 7556
3589 _usage_error('read_table', "'quote' must be '\"' (the default) or '' (no quoting)")
3590 unless defined $args{quote} && !ref $args{quote}
3591 185 ▶ && ($args{quote} eq '' || $args{quote} eq '"');
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"quote"} and not ref $args{"quote"} and $args{"quote"} eq "" || $args{"quote"} eq "\"" 4 181 185
Condition100%9/9
defined $args{"quote"} and not ref $args{"quote"} and $args{"quote"} eq "" || $args{"quote"} eq "\""
!ll&&!rl&&r
22181
defined $args{"quote"} and not ref $args{"quote"}
!ll&&!rl&&r
11183
$args{"quote"} eq "" or $args{"quote"} eq "\""
l!l&&r!l&&!r
17922
MC/DC100%4/4
defined $args{"quote"} and not ref $args{"quote"} and $args{"quote"} eq "" || $args{"quote"} eq "\""
defined $args{"quote"} not ref $args{"quote"} $args{"quote"} eq "" $args{"quote"} eq "\""
Truth table100%5/5
A and not(B) and C or D
A= defined $args{"quote"}B= not ref $args{"quote"}C= $args{"quote"} eq ""D= $args{"quote"} eq "\""
ABCDresult
0XXX0
10XX0
11000
111X1
11011
3592 181 ▶ $quote = $args{quote} eq '"' ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$args{'quote'} eq '"' ? : 2 179 181
3593 }
3594  
3595 # The file is read plain, as an aoa, with every option but the output
3596 # type and row_names, so 'filter' sees the file's own columns (FORMAT and
3597 # each sample's unsplit text), and is then exploded into the shape asked
3598 # for. Each raw row is released as it is exploded.
3599 7552 ▶ if ($explode) {
Branch: 1 branch
BranchTrueFalseTotal
if ($explode) 70 7482 7552
3600 70 my %raw_args = (%input_args, explode => 0, 'output_type' => 'aoa');
3601 70 delete $raw_args{'row_names'};
3602 70 my $raw = read_table($file, %raw_args);
3603 5 ▶ my %na = map { _as_bytes($_) => 1 } !defined $args{'na_strings'} ? ()
Branch: 2 branches
BranchTrueFalseTotal
!defined($args{'na_strings'}) ? : 64 5 69
ref $args{'na_strings'} ? : 0 5 5
3604 0 : ref $args{'na_strings'} ? @{ $args{'na_strings'} }
3605 69 : ($args{'na_strings'});
3606 # _vcf_explode() in LikeR.xs; its comment says what the columns are
3607 69 my %mode = (aoh => 0, hoa => 1, hoh => 2, aoa => 3);
3608 # A sample's values have no column of their own to give a class to, so
3609 # they follow the one spelling that speaks for every column:
3610 # colClasses => 'character' keeps them text, and anything else stores
3611 # an integer's text as its number, as the file's own columns are.
3612 my $all_text = defined $args{colClasses} && !ref $args{colClasses}
3613 69 ▶ && $args{colClasses} eq 'character';
Condition66%4/6
defined $args{'colClasses'} && !ref($args{'colClasses'}) && $args{'colClasses'} eq 'character'
!ll&&!rl&&r
6801
defined $args{"colClasses"} and not ref $args{"colClasses"}
!ll&&!rl&&r
6801
MC/DC33%1/3
defined $args{'colClasses'} && !ref($args{'colClasses'}) && $args{'colClasses'} eq 'character'
defined $args{"colClasses"} not ref $args{"colClasses"} $args{'colClasses'} eq 'character'
Truth table50%2/4
A and not(B) && C
A= defined $args{"colClasses"}B= not ref $args{"colClasses"}C= $args{'colClasses'} eq 'character'
ABCresult
0XX0
10X0
1100
1111
3614 69 ▶ return _vcf_explode($raw, $mode{$otype}, $args{'row_names'}, $file,
Branch: 2 branches
BranchTrueFalseTotal
%na ? : 5 64 69
$all_text ? : 1 68 69
3615 %na ? \%na : undef, $all_text ? 0 : 1);
3616 }
3617  
3618 # A multi-worksheet .xlsx with no explicit 'sheet' is returned as a hash
3619 # keyed by worksheet name, each value being that sheet parsed with the same
3620 # options (recursing one sheet at a time keeps every table's state, and any
3621 # hoh row_names default, independent). A single-worksheet workbook, or an
3622 # explicit 'sheet', still returns that one table directly.
3623 # Resolve the worksheet list once and reuse it below (it decompresses and
3624 # parses workbook.xml + its rels, so recomputing it in the main xlsx branch
3625 # would double that work for every single-sheet / explicit-sheet read).
3626 7482 my ($xlsx_sheets, $xlsx_rels);
3627 7482 ▶ if ($is_xlsx) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_xlsx) 413 7069 7482
3628 # Reuse a caller-supplied worksheet list (from the multi-sheet expansion
3629 # below) rather than re-parsing workbook.xml + its rels for every sheet.
3630 413 ▶ if ($args{_xlsx_sheets}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'_xlsx_sheets'}) { } 85 328 413
3631 85 $xlsx_sheets = $args{_xlsx_sheets};
3632 } else {
3633 328 $xlsx_rels = _xlsx_rels($file);
3634 328 $xlsx_sheets = _xlsx_sheets($file, $xlsx_rels);
3635 }
3636 413 ▶ if (!defined $args{sheet} && !defined $args{_sheet_ix}
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $args{"sheet"} || defined $args{"_sheet_ix"} and @$xlsx_sheets > 1) 40 373 413
Condition100%6/6
not defined $args{"sheet"} || defined $args{"_sheet_ix"} and @$xlsx_sheets > 1
!ll&&!rl&&r
11326040
defined $args{'sheet'} || defined $args{'_sheet_ix'}
l!l&&r!l&&!r
2885300
MC/DC100%3/3
not defined $args{"sheet"} || defined $args{"_sheet_ix"} and @$xlsx_sheets > 1
defined $args{'sheet'} defined $args{'_sheet_ix'} @$xlsx_sheets > 1
Truth table100%4/4
not(A || B) and C
A= defined $args{'sheet'}B= defined $args{'_sheet_ix'}C= @$xlsx_sheets > 1
ABCresult
1XX0
01X0
0000
0011
3637 && @$xlsx_sheets > 1) {
3638 # Decompress + parse the shared-string table once for the whole
3639 # workbook and hand it to each per-sheet read, instead of every
3640 # recursion re-reading (a potentially large) sharedStrings.xml.
3641 40 my $sst = _xlsx_shared_strings($file, $xlsx_rels);
3642 # A sheet with no name is keyed "SheetN", N its position -- or the
3643 # next N no other sheet is named, since up to 0.3213 an unnamed
3644 # first sheet and a sheet called "Sheet1" were both keyed "Sheet1",
3645 # and one of the two tables was silently lost.
3646 40 ▶ my %taken = map { defined $_->{name} ? ($_->{name} => 1) : () }
Branch: 1 branch
BranchTrueFalseTotal
defined $_->{'name'} ? : 85 2 87
3647 @$xlsx_sheets;
3648 40 my %book;
3649 40 for my $i (0 .. $#$xlsx_sheets) {
3650 85 my $name = $xlsx_sheets->[$i]{name};
3651 85 ▶ if (!defined $name) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $name) 2 83 85
3652 2 my $n = $i + 1;
3653 2 ▶ $n++ while $taken{"Sheet$n"};
Branch: 1 branch
BranchTrueFalseTotal
$taken{"Sheet$n"} and do { ++$n } 1 2 3
3654 2 $name = "Sheet$n";
3655 2 $taken{$name} = 1;
3656 }
3657 # by position, privately: 'sheet' takes a name before a
3658 # number, so sheets named "2" and "1" would swap places
3659 85 $book{$name} = read_table($file, %input_args,
3660 _sheet_ix => $i,
3661 _xlsx_sheets => $xlsx_sheets,
3662 _sst => $sst);
3663 }
3664 26 return \%book;
3665 }
3666 }
3667  
3668 # R's write.table(col.names=TRUE) default omits the header label for the
3669 # row-names column, so a header comes out one field short of every data
3670 # row. With 'auto_row_names' set, mirror read.table's rule: when (and only
3671 # when) the header is exactly one field short, treat the first data field
3672 # as an (otherwise unlabelled) row-names column. Any truthy value enables
3673 # it; a non-1 string is used as the synthesized column name.
3674 7442 ▶ my $want_auto_rn = $args{'auto_row_names'} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$args{'auto_row_names'} ? : 562 6880 7442
3675 7442 my $auto_rn_added = 0; # the row-names column was put in front of @header
3676 my $auto_rn_name =
3677 ($want_auto_rn && "$args{'auto_row_names'}" ne '1')
3678 7442 ▶ ? $args{'auto_row_names'} : 'row_name';
Branch: 1 branch
BranchTrueFalseTotal
$want_auto_rn && "$args{'auto_row_names'}" ne '1' ? : 20 7422 7442
Condition100%3/3
$want_auto_rn and qq[$args{"auto_row_names"}] ne 1
!ll&&!rl&&r
688054220
MC/DC100%2/2
$want_auto_rn and qq[$args{"auto_row_names"}] ne 1
$want_auto_rn qq[$args{"auto_row_names"}] ne 1
Truth table100%3/3
A and B
A= $want_auto_rnB= qq[$args{"auto_row_names"}] ne 1
ABresult
0X0
100
111
3679  
3680 7442 my $filter = $args{filter};
3681 7442 ▶ if (defined $filter && ref($filter) eq 'CODE') {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $filter and ref $filter eq 'CODE') { } 52 7390 7442
elsif (defined $filter and ref $filter ne 'HASH') { } 2 14342 14344
Condition100%3/3
defined $filter and ref $filter eq "CODE"
!ll&&!rl&&r
695443652
MC/DC100%2/2
defined $filter and ref $filter eq "CODE"
defined $filter ref $filter eq "CODE"
Truth table100%3/3
A and B
A= defined $filterB= ref $filter eq "CODE"
ABresult
0X0
100
111
3682 52 $filter = { 0 => $filter };
3683 ▶ } elsif (defined $filter && ref($filter) ne 'HASH') {
Condition100%3/3
defined $filter and ref $filter ne "HASH"
!ll&&!rl&&r
69544342
MC/DC100%2/2
defined $filter and ref $filter ne "HASH"
defined $filter ref $filter ne "HASH"
Truth table100%3/3
A and B
A= defined $filterB= ref $filter ne "HASH"
ABresult
0X0
100
111
3684 2 _usage_error('read_table', "'filter' must be a CODE or HASH reference");
3685 }
3686 # The parser calls the subs itself (see $plan below), and it takes a code
3687 # reference only. Asked up front, so a bad one is not first noticed on
3688 # whichever row reaches it.
3689 7440 ▶ for my $k (sort keys %{ $filter || {} }) {
Condition100%2/2
$filter || {}
l!l
4866954
MC/DC100%1/1
$filter || {}
$filter
Truth table100%2/2
A
A= $filter
Aresult
11
00
3690 _usage_error('read_table', "filter '$k' must be a CODE reference")
3691 557 ▶ unless (reftype($filter->{$k}) // '') eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::reftype($filter->{$k}) // "") eq "CODE" 1 556 557
Condition100%2/2
builtin::reftype($filter->{$k}) // ""
l!l
5561
MC/DC100%1/1
builtin::reftype($filter->{$k}) // ""
builtin::reftype($filter->{$k})
Truth table100%2/2
A
A= builtin::reftype($filter->{$k})
Aresult
11
00
3692 }
3693  
3694 # colClasses, R's: a column declared numeric or integer is stored as an NV or
3695 # an IV rather than as its text. On a 300,000 x 5 CSV with three of its five
3696 # columns declared, a hoa took 74 MB instead of 116 MB on perl 5.44.0, about
3697 # 47 bytes less for each of the 900,000 cells converted. R's spellings:
3698 # one class for every column, a list by position (recycled when short, as R
3699 # recycles it), or a hash by name, where a name that is no column is warned
3700 # about, in R's words, and otherwise ignored. 'character' keeps a column as
3701 # the text the file holds. undef (R's NA), and any column with no class at
3702 # all, is code 3: a cell whose text is an integer exactly as perl would print
3703 # it ("42", "-7", not "007" or "4.0") is stored as that IV, 24 bytes where the
3704 # text took 72, and every other cell as its text; S_text_is_iv() in LikeR.xs
3705 # has the rule and what it measured. Each name is checked here, before the
3706 # file is opened; which field gets which class waits for the header.
3707 # %cls_code: 0 = character (its text), 1 = numeric (an NV), 2 = integer (an
3708 # IV), 3 = no class given
3709 7439 my %cls_code = (numeric => 1, double => 1, real => 1, integer => 2, character => 0);
3710 7439 my $cls_arg = $args{colClasses};
3711 1017 ▶ my $cls_of = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
3712 1017 my ($c) = @_;
3713 1017 ▶ return 3 unless defined $c;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $c 51 966 1017
3714 _usage_error('read_table', "colClasses '$c' is not one read_table reads: it takes "
3715 . "'numeric' (or 'double' or 'real'), 'integer', 'character' or undef")
3716 966 ▶ unless exists $cls_code{$c};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $cls_code{$c} 3 963 966
3717 963 return $cls_code{$c};
3718 7439 };
3719 7439 ▶ if (defined $cls_arg) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cls_arg) 281 7158 7439
3720 281 my $r = ref $cls_arg;
3721 281 ▶ _usage_error('read_table', "'colClasses' must be a string, an ARRAY reference or a "
Branch: 1 branch
BranchTrueFalseTotal
if $r and $r ne "ARRAY" and $r ne "HASH" 1 280 281
Condition100%6/6
$r and $r ne "ARRAY" and $r ne "HASH"
!ll&&!rl&&r
1411391
$r and $r ne "ARRAY"
!ll&&!rl&&r
8655140
MC/DC100%3/3
$r and $r ne "ARRAY" and $r ne "HASH"
$r $r ne "ARRAY" $r ne "HASH"
Truth table100%4/4
A and B and C
A= $rB= $r ne "ARRAY"C= $r ne "HASH"
ABCresult
0XX0
10X0
1100
1111
3722 . "HASH reference") if $r && $r ne 'ARRAY' && $r ne 'HASH';
3723 280 ▶ $cls_of->($_) for $r eq 'ARRAY' ? @$cls_arg : $r eq 'HASH' ? values %$cls_arg : $cls_arg;
Branch: 2 branches
BranchTrueFalseTotal
$r eq 'ARRAY' ? : 55 225 280
$r eq 'HASH' ? : 139 86 225
3724 277 ▶ _usage_error('read_table', "'colClasses' is an empty list") if $r eq 'ARRAY' && !@$cls_arg;
Branch: 1 branch
BranchTrueFalseTotal
if $r eq "ARRAY" and not @$cls_arg 1 276 277
Condition100%3/3
$r eq "ARRAY" and not @$cls_arg
!ll&&!rl&&r
223531
MC/DC100%2/2
$r eq "ARRAY" and not @$cls_arg
$r eq "ARRAY" not @$cls_arg
Truth table100%3/3
A and not(B)
A= $r eq "ARRAY"B= not @$cls_arg
ABresult
0X0
100
111
3725 }
3726 7434 my @cls; # per field of @header, once it is fixed: a %cls_code value
3727  
3728 # na_strings / na_values / undef_val -- field texts that mean "missing", mapped
3729 # to undef exactly as an empty field already is. Every spelling has been folded
3730 # into 'na_strings' above, so only that one is read from here on.
3731 #
3732 # Off by default. R's read.table defaults to na.strings = "NA" and pandas
3733 # recognises a whole list, but this module has always handed a literal "NA"
3734 # back as real data, and t/read_table.t pins that, so turning it on for
3735 # everyone is a maintainer's call rather than a bug fix. What was a bug is that
3736 # there was no way to ask for it at all: write_table renders undef with
3737 # 'undef_val' (documented under write_table as not round-tripping back to
3738 # undef), so read_table could not read back what its own sibling had written.
3739 #
3740 # The match is on the exact field text, case-sensitively and without trimming
3741 # whitespace, so " NA" is not "NA"; list both spellings if a file has both.
3742 my %na_string;
3743 {
3744 7434 my $ns = $args{'na_strings'};
3745 7434 ▶ my @ns = !defined($ns) ? ()
Branch: 3 branches
BranchTrueFalseTotal
!defined($ns) ? : 7171 263 7434
ref $ns eq 'ARRAY' ? : 62 201 263
ref $ns ? : 3 198 201
3746 : ref($ns) eq 'ARRAY' ? @$ns
3747 : ref($ns) ? _usage_error('read_table', "'na_strings' must be a "
3748 . 'string or an ARRAY reference, not a '
3749 . ref($ns) . ' reference')
3750 : ($ns);
3751 7431 for my $s (@ns) {
3752 464 ▶ _usage_error('read_table', "'na_strings' may not contain an undefined value")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s 1 463 464
3753 unless defined $s;
3754 463 $na_string{ _as_bytes($s) } = 1;
3755 }
3756 }
3757 7430 ▶ my $has_na = %na_string ? 1 : 0; # skip the hash lookup in the common case
Branch: 1 branch
BranchTrueFalseTotal
%na_string ? : 254 7176 7430
3758  
3759 # @field_wins: per field, TRUE when it is the last to carry its name and
3760 # the name is not excluded, which is when %line_hash holds its value
3761 # @excl: per field, TRUE when 'drop_cols' leaves its column out; empty when
3762 # nothing is excluded. @kept_flds: the indices of the other fields
3763 7430 my (@data, %data, @header, @uniq_header, @hoa_cols, @field_wins,
3764 %mapped_filters, @sorted_filter_flds, %seen_rownames, @excl, @kept_flds);
3765 7430 my ($data_row, $header_seen, $header_done, $provisional_hdr) = (0, 0, 0, 0);
3766 # hoh: rows whose name an earlier row already had, and the first of them as
3767 # [name, data row]. Counted and warned about once, after the parse.
3768 7430 my ($n_dup_rn, $first_dup_rn) = (0, undef);
3769 # The alignment message's parts, set by $finalize_header: what a ragged
3770 # row's field count is compared with, and a note for a width of one whose
3771 # line held no separator (undef when there is none). The parser's
3772 # S_fast_row() gets both through the plan.
3773 7430 my ($width_txt, $sep_note);
3774  
3775 # Once the header is fixed there is nothing left for the row closure below to
3776 # decide, so the parser is given a plan and builds the rest of the table
3777 # itself. That closure, called once per row, used to be about four fifths of
3778 # read_table's wall clock -- on a 300,000 x 5 CSV, 0.42 s of 0.53 s -- doing in
3779 # perl what C can do from the fields it has already cut.
3780 #
3781 # A 'filter' goes this way too: its subs are handed over in the plan and the
3782 # parser calls them, with the same $_, %_ and arguments the closure below
3783 # gives them (S_filter_row() in LikeR.xs). Up to 0.3212 a filter kept every row
3784 # in the closure, and a filtered read of a 300,000 x 5 CSV took 0.58 s to
3785 # 1.07 s against 0.08 s to 0.18 s unfiltered. The closure still applies them to
3786 # any data row it handles itself, before the plan is installed. 'hoh' went
3787 # through the closure too until 0.319, which on a 300,000 x 5 CSV cost 0.91 s;
3788 # it now takes 0.20 s. An .xlsx goes through a different parser (_parse_xlsx_sheet_xs)
3789 # but the same plan: both hand a finished row to the same S_fast_row().
3790 # A qr// sep goes this way too: _parse_csv_file() matches it with perl's regex
3791 # engine, from C, so only how a separator is found changes.
3792 #
3793 # See csv_plan in LikeR.xs for what each key means. install_plan() runs exactly
3794 # once, from wherever $header_done is first set, and writes 'out' last because
3795 # that is the key the parser tests for.
3796 7430 ▶ my $plan = $args{_closure} ? undef : {};
Branch: 1 branch
BranchTrueFalseTotal
$args{'_closure'} ? : 1124 6306 7430
3797 7149 ▶ my $install_plan = sub {
Subroutine __ANON__: called CC=12 SCAR=24.8
3798 7149 ▶ return if !$plan || %$plan;
Branch: 1 branch
BranchTrueFalseTotal
if not $plan or %$plan 995 6154 7149
Condition66%2/3
not $plan or %$plan
l!l&&r!l&&!r
99506154
MC/DC50%1/2
not $plan or %$plan
not $plan %$plan
Truth table66%2/3
not(A) or B
A= not $planB= %$plan
ABresult
1X1
011
000
3799 # A repeated column name resolves to its LAST field, which is what
3800 # "later values win" means when %line_hash is built below.
3801 6154 my %last;
3802 6154 $last{ $header[$_] } = $_ for 0 .. $#header;
3803 6154 $plan->{keys} = \@uniq_header;
3804 6154 $plan->{idx} = [ map { $last{$_} } @uniq_header ];
3805 6154 $plan->{ncol} = scalar @header;
3806 6154 $plan->{file} = $file;
3807 6154 $plan->{width} = $width_txt;
3808 6154 $plan->{sep_note} = $sep_note;
3809 6154 ▶ $plan->{na} = $has_na ? \%na_string : undef;
Branch: 1 branch
BranchTrueFalseTotal
$has_na ? : 204 5950 6154
3810 6154 ▶ if (@cls) {
Branch: 1 branch
BranchTrueFalseTotal
if (@cls) 6140 14 6154
3811 6140 $plan->{cls} = [ @cls ];
3812 6140 $plan->{hdr} = [ @header ];
3813 }
3814 # 'drop_cols': @uniq_header already lacks the names, so no key reads an
3815 # excluded field; this tells the parser not to make its cells
3816 6154 ▶ $plan->{skip} = [ @excl ] if @excl;
Branch: 1 branch
BranchTrueFalseTotal
if @excl 877 5277 6154
3817 # in the order the closure below runs them
3818 6154 for my $fld (@sorted_filter_flds) {
3819 390 for my $sub (@{ $mapped_filters{$fld} }) {
3820 412 push @{ $plan->{flt_fld} }, $fld;
3821 412 push @{ $plan->{flt_sub} }, $sub;
3822 }
3823 }
3824 # a reference, not a copy: the parser reads it when it picks the plan
3825 # up, by which time $on_line may have emitted a data row of its own.
3826 6154 $plan->{row} = \$data_row;
3827 6154 ▶ if ($otype eq 'aoh') {
Branch: 3 branches
BranchTrueFalseTotal
if ($otype eq 'aoh') { } 2070 4084 6154
elsif ($otype eq 'aoa') { } 2733 1351 4084
elsif ($otype eq 'hoa') { } 686 665 1351
3828 2070 $plan->{mode} = 0;
3829 2070 $plan->{out} = \@data;
3830 } elsif ($otype eq 'aoa') {
3831 2733 $plan->{mode} = 3;
3832 2733 $plan->{out} = \@data;
3833 } elsif ($otype eq 'hoa') {
3834 686 $plan->{mode} = 1;
3835 686 $plan->{out} = [ @hoa_cols ];
3836 } else {
3837 # $finalize_header has already checked that row_names is a column
3838 665 my ($rn) = grep { $uniq_header[$_] eq $args{'row_names'} }
3839 0 .. $#uniq_header;
3840 665 $plan->{mode} = 2;
3841 665 $plan->{rn} = $rn;
3842 665 $plan->{out} = \%data;
3843 }
3844 7430 };
3845  
3846 # Everything that depends on the (possibly augmented) @header lives here so
3847 # it can run either right after the header line (strict mode) or deferred
3848 # to the first data row (auto_row_names mode, once the width is known).
3849 7417 ▶ my $finalize_header = sub {
Subroutine __ANON__: called CC=75 SCAR=43.2
3850 # A VCF header line is "#CHROM\tPOS...": under the default "##" marker it
3851 # is not a comment, so it arrives with its "#" on. Read with
3852 # comment => '#' instead, the marker is already off and this is a no-op.
3853 # Its first column, not $header[0], since auto_row_names may have put a
3854 # row-names column in front of it.
3855 7417 ▶ $header[$auto_rn_added] =~ s/\A#//
Branch: 1 branch
BranchTrueFalseTotal
if $is_vcf and $want_header and defined $header[$auto_rn_added] 156 7261 7417
Condition83%5/6
$is_vcf and $want_header and defined $header[$auto_rn_added]
!ll&&!rl&&r
72610156
$is_vcf and $want_header
!ll&&!rl&&r
725110156
MC/DC66%2/3
$is_vcf and $want_header and defined $header[$auto_rn_added]
$is_vcf $want_header defined $header[$auto_rn_added]
Truth table75%3/4
A and B and C
A= $is_vcfB= $want_headerC= defined $header[$auto_rn_added]
ABCresult
0XX0
10X0
1100
1111
3856 if $is_vcf && $want_header && defined $header[$auto_rn_added];
3857 # R's read.table: 'col.names' replaces a header's names, and a header
3858 # of another length is warned about ("header and 'col.names' are of
3859 # different lengths") and then the names are used all the same.
3860 7417 ▶ if ($col_names && $want_header) {
Branch: 1 branch
BranchTrueFalseTotal
if ($col_names and $want_header) 67 7350 7417
Condition100%3/3
$col_names and $want_header
!ll&&!rl&&r
73153567
MC/DC100%2/2
$col_names and $want_header
$col_names $want_header
Truth table100%3/3
A and B
A= $col_namesB= $want_header
ABresult
0X0
100
111
3861 67 my $n = @header - $auto_rn_added;
3862 67 ▶ warn "read_table: header and 'col_names' are of different lengths "
Branch: 1 branch
BranchTrueFalseTotal
if $n != @$col_names 1 66 67
3863 . "($n and " . scalar(@$col_names) . ") in $file\n"
3864 if $n != @$col_names;
3865 67 splice @header, $auto_rn_added, $n, @$col_names;
3866 }
3867 7417 ▶ if (@header && $header[0] eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (@header and $header[0] eq "") 80 7337 7417
Condition66%2/3
@header and $header[0] eq ""
!ll&&!rl&&r
0733780
MC/DC50%1/2
@header and $header[0] eq ""
@header $header[0] eq ""
Truth table66%2/3
A and B
A= @headerB= $header[0] eq ""
ABresult
0X0
100
111
3868 80 $header[0] = 'row_name';
3869 }
3870 7417 my %seen_h;
3871 7417 @uniq_header = grep { !$seen_h{$_}++ } @header;
3872 # 'keep_cols' and 'drop_cols', against the names as they now stand:
3873 # after 'col_names', the VCF "#" and the row_name default, so a name or
3874 # pattern is written for the name the table would have had. A name is
3875 # the bytes the file holds, and is tried as those and, when they are
3876 # UTF-8, as the characters they spell, so that a pattern compiled under
3877 # `use utf8` and one compiled without it both match "Größe", and so do
3878 # a column name written under it and one written without. Each name is
3879 # tried once, and every field carrying an excluded name goes.
3880 7417 @excl = ();
3881 7417 @kept_flds = (0 .. $#header);
3882 7417 ▶ if (%sel) {
Branch: 1 branch
BranchTrueFalseTotal
if (%sel) 2010 5407 7417
3883 # A hoh is keyed by its row_names column, which is never a cell of
3884 # a row, so keep_cols keeps it whether or not it names it: what
3885 # keep_cols chooses is the cells. drop_cols naming it is refused
3886 # below. Its default, the first column, is set further down.
3887 my $rn = $otype ne 'hoh' ? undef
3888 2010 ▶ : defined $args{'row_names'} ? $args{'row_names'}
Branch: 2 branches
BranchTrueFalseTotal
$otype ne 'hoh' ? : 1504 506 2010
defined $args{'row_names'} ? : 346 160 506
3889 : $header[0];
3890 2010 my %named; # keep / drop => { name => 1 }: the entries that are a column
3891 # the file, and the worksheet when a workbook is read one sheet at a time
3892 2010 my $in = $file;
3893 2010 ▶ if (defined $args{_sheet_ix}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"_sheet_ix"}) 46 1964 2010
3894 46 my $sn = $xlsx_sheets->[ $args{_sheet_ix} ]{name};
3895 46 ▶ $in .= defined $sn ? " (sheet \"$sn\")" : ' (sheet ' . ($args{_sheet_ix} + 1) . ')';
Branch: 1 branch
BranchTrueFalseTotal
defined $sn ? : 46 0 46
3896 }
3897 14265 ▶ my $hit = sub { # does $sel{$k} name or match this header name?
Subroutine __ANON__: called CC=7 SCAR=19.5
3898 14265 my ($k, $name, $chars, $wide) = @_;
3899 14265 my $s = $sel{$k};
3900 14265 my $found = 0;
3901 14265 ▶ for my $as ($wide ? ($name, $chars) : ($name)) {
Branch: 1 branch
BranchTrueFalseTotal
$wide ? : 362 13903 14265
3902 14627 ▶ next unless $s->{name}{$as};
Branch: 1 branch
BranchTrueFalseTotal
unless $s->{"name"}{$as} 11640 2987 14627
3903 2987 $named{$k}{$as} = 1;
3904 2987 $found = 1;
3905 }
3906 return $found
3907 14265 ▶ || grep { $name =~ $_ || ($wide && $chars =~ $_) } @{ $s->{rx} };
Condition100%9/9
$found || grep({$name =~ /$_/ or $wide and $chars =~ /$_/;} @{$$s{'rx'};})
l!l&&r!l&&!r
297126728622
$name =~ /$_/ || $wide && $chars =~ /$_/
l!l&&r!l&&!r
2659244578
$wide and $chars =~ /$_/
!ll&&!rl&&r
45403824
MC/DC100%2/2
$found || grep({$name =~ /$_/ or $wide and $chars =~ /$_/;} @{$$s{'rx'};})
$found grep({$name =~ /$_/ or $wide and $chars =~ /$_/;} @{$$s{'rx'};})
MC/DC100%3/3
$name =~ /$_/ || $wide && $chars =~ /$_/
$name =~ /$_/ $wide $chars =~ /$_/
Truth table100%3/3
A || B
A= $foundB= grep({$name =~ /$_/ or $wide and $chars =~ /$_/;} @{$$s{'rx'};})
ABresult
1X1
011
000
Truth table100%4/4
A || B and C
A= $name =~ /$_/B= $wideC= $chars =~ /$_/
ABCresult
1XX1
0111
00X0
0100
3908 2010 };
3909 2010 my %out;
3910 2010 for my $name (@uniq_header) {
3911 13888 my $chars = $name;
3912 13888 ▶ my $wide = utf8::decode($chars) && $chars ne $name;
Condition100%3/3
utf8::decode($chars) && $chars ne $name
!ll&&!rl&&r
2413502362
MC/DC100%2/2
utf8::decode($chars) && $chars ne $name
utf8::decode($chars) $chars ne $name
Truth table100%3/3
A && B
A= utf8::decode($chars)B= $chars ne $name
ABresult
0X0
100
111
3913 # both are tried, whatever the first says, so that each one's
3914 # names are marked as found
3915 13888 ▶ my $kept = !$sel{keep} || $hit->('keep', $name, $chars, $wide)
Condition100%9/9
!$sel{'keep'} || &$hit('keep', $name, $chars, $wide) || defined $rn && $name eq $rn
l!l&&r!l&&!r
9691344163
not $sel{"keep"} or &$hit("keep", $name, $chars, $wide)
l!l&&r!l&&!r
720624854197
defined $rn and $name eq $rn
!ll&&!rl&&r
329187234
MC/DC100%4/4
!$sel{'keep'} || &$hit('keep', $name, $chars, $wide) || defined $rn && $name eq $rn
not $sel{"keep"} &$hit("keep", $name, $chars, $wide) defined $rn $name eq $rn
Truth table100%5/5
not(A) or B || C and D
A= not $sel{"keep"}B= &$hit("keep", $name, $chars, $wide)C= defined $rnD= $name eq $rn
ABCDresult
1XXX1
01XX1
00111
000X0
00100
3916 || (defined $rn && $name eq $rn);
3917 13888 ▶ my $dropped = $sel{drop} && $hit->('drop', $name, $chars, $wide);
Condition100%3/3
$sel{'drop'} && &$hit('drop', $name, $chars, $wide)
!ll&&!rl&&r
630544253158
MC/DC100%2/2
$sel{'drop'} && &$hit('drop', $name, $chars, $wide)
$sel{'drop'} &$hit('drop', $name, $chars, $wide)
Truth table100%3/3
A && B
A= $sel{'drop'}B= &$hit('drop', $name, $chars, $wide)
ABresult
0X0
100
111
3918 13888 ▶ $out{$name} = 1 if !$kept || $dropped;
Branch: 1 branch
BranchTrueFalseTotal
if not $kept or $dropped 7302 6586 13888
Condition100%3/3
not $kept or $dropped
l!l&&r!l&&!r
416331396586
MC/DC100%2/2
not $kept or $dropped
not $kept $dropped
Truth table100%3/3
not(A) or B
A= not $keptB= $dropped
ABresult
1X1
011
000
3919 }
3920 # A name that is no column is most often a misspelled one, and the
3921 # column it meant is then stored after all, or not stored, so it is
3922 # said -- as R's read.table() says "not all columns named in
3923 # 'colClasses' exist" of colClasses = c(nope = "NULL"), its way of
3924 # skipping a column by name (R 4.6.1 src/library/utils/R/readtable.R).
3925 # A pattern that matches nothing stays silent: it never claimed a
3926 # column was there. Said before the die below, so that a read
3927 # refused for keeping no column says which name was no column; and
3928 # of a workbook read sheet by sheet, with the sheet, since the name
3929 # may be a column of another one.
3930 #
3931 # A workbook read sheet by sheet is refused instead: the caller
3932 # chose columns for every sheet at once, and a sheet without one of
3933 # them would come back whole, or cut down to other columns, under
3934 # a warning that is easy to miss among the sheets that were right.
3935 # Both options' names are said in the one error.
3936 2010 my @absent_msg;
3937 2010 for my $k ('keep', 'drop') {
3938 4020 ▶ next unless $sel{$k};
Branch: 1 branch
BranchTrueFalseTotal
unless $sel{$k} 1945 2075 4020
3939 2075 my @absent = grep { !$named{$k}{$_} } @{ $sel{$k}{names} };
3940 push @absent_msg, "read_table: not all columns named in '${k}_cols' exist in $in: "
3941 2075 ▶ . join(', ', map { "'" . _as_bytes($_) . "'" } @absent) . "\n"
Branch: 1 branch
BranchTrueFalseTotal
if @absent 227 1848 2075
3942 if @absent;
3943 }
3944 2010 ▶ if (@absent_msg && defined $args{_sheet_ix}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@absent_msg and defined $args{"_sheet_ix"}) 14 1996 2010
Condition100%3/3
@absent_msg and defined $args{"_sheet_ix"}
!ll&&!rl&&r
179320314
MC/DC100%2/2
@absent_msg and defined $args{"_sheet_ix"}
@absent_msg defined $args{"_sheet_ix"}
Truth table100%3/3
A and B
A= @absent_msgB= defined $args{"_sheet_ix"}
ABresult
0X0
100
111
3945 14 die join('', @absent_msg) . "read_table: pass 'sheet' to read one worksheet, "
3946 . "or a qr// regex, which may match no column of a sheet\n";
3947 }
3948 1996 warn $_ for @absent_msg;
3949 1996 ▶ if (%out) {
Branch: 1 branch
BranchTrueFalseTotal
if (%out) 1902 94 1996
3950 1902 ▶ @excl = map { $out{$_} ? 1 : 0 } @header;
Branch: 1 branch
BranchTrueFalseTotal
$out{$_} ? : 7328 6026 13354
3951 1902 @kept_flds = grep { !$excl[$_] } 0 .. $#header;
3952 1902 @uniq_header = grep { !$out{$_} } @uniq_header;
3953 die "read_table: " . (!$sel{keep} ? "'drop_cols' leaves out every column"
3954 1902 ▶ : !$sel{drop} ? "'keep_cols' keeps no column"
Branch: 3 branches
BranchTrueFalseTotal
unless @kept_flds 216 1686 1902
!$sel{'keep'} ? : 168 48 216
!$sel{'drop'} ? : 36 12 48
3955 : "'keep_cols' and 'drop_cols' leave no column")
3956 . " of $in\n"
3957 unless @kept_flds;
3958 }
3959 }
3960 # Name each repeated column, with how many fields carry it and which
3961 # ones (1-based, as a spreadsheet or `cut -f` counts them). A bare
3962 # list of the names is not enough to find them: a merged banner row
3963 # repeats the *empty* name, which prints as nothing at all, and a name
3964 # repeated three times reads the same as one repeated twice.
3965 7187 my %at;
3966 7187 push @{ $at{ $header[$_] } }, $_ + 1 for 0 .. $#header;
3967 # 12 positions is what fits on one terminal line beside the name and
3968 # the count; a merged banner row can repeat the empty name across
3969 # every field of the sheet (66 of them in the file this was written
3970 # for), and the count still says how many there are in total.
3971 my @dup_cols = map {
3972 100 my @f = @{ $at{$_} };
3973 100 ▶ my $more = @f > 12 ? ', ...' : '';
Branch: 1 branch
BranchTrueFalseTotal
@f > 12 ? : 1 99 100
3974 100 ▶ splice @f, 12 if @f > 12;
Branch: 1 branch
BranchTrueFalseTotal
if @f > 12 1 99 100
3975 100 "'$_' x $seen_h{$_} (fields " . join(', ', @f) . "$more)"
3976 7187 } grep { $seen_h{$_} > 1 } @uniq_header;
3977 # an aoa keeps every field, so a repeated name loses nothing there
3978 7187 ▶ warn "read_table: duplicate column name(s) in $file (later values win): "
Branch: 1 branch
BranchTrueFalseTotal
if @dup_cols and $otype ne "aoa" 72 7115 7187
Condition100%3/3
@dup_cols and $otype ne "aoa"
!ll&&!rl&&r
70882772
MC/DC100%2/2
@dup_cols and $otype ne "aoa"
@dup_cols $otype ne "aoa"
Truth table100%3/3
A and B
A= @dup_colsB= $otype ne "aoa"
ABresult
0X0
100
111
3979 . join('; ', @dup_cols) . "\n"
3980 if @dup_cols && $otype ne 'aoa';
3981 # An aoa's first row is the header -- the shape write_table reads an
3982 # AoA as -- in file order and with any repeated name kept.
3983 7187 ▶ @data = ([ @header[@kept_flds] ]) if $otype eq 'aoa';
Branch: 1 branch
BranchTrueFalseTotal
if $otype eq "aoa" 2985 4202 7187
3984 7187 ▶ if ($otype eq 'hoh' && !defined $args{'row_names'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($otype eq "hoh" and not defined $args{"row_names"}) 532 6655 7187
Condition100%3/3
$otype eq "hoh" and not defined $args{"row_names"}
!ll&&!rl&&r
6269386532
MC/DC100%2/2
$otype eq "hoh" and not defined $args{"row_names"}
$otype eq "hoh" not defined $args{"row_names"}
Truth table100%3/3
A and not(B)
A= $otype eq "hoh"B= not defined $args{"row_names"}
ABresult
0X0
100
111
3985 532 $args{'row_names'} = $header[0];
3986 }
3987 7187 ▶ if (defined $args{'row_names'}
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"row_names"} and not grep({$_ eq $args{"row_names"};} @header)) 3 7184 7187
Condition100%3/3
defined $args{"row_names"} and not grep({$_ eq $args{"row_names"};} @header)
!ll&&!rl&&r
62699153
MC/DC100%2/2
defined $args{"row_names"} and not grep({$_ eq $args{"row_names"};} @header)
defined $args{"row_names"} not grep({$_ eq $args{"row_names"};} @header)
Truth table100%3/3
A and not(B)
A= defined $args{"row_names"}B= not grep({$_ eq $args{"row_names"};} @header)
ABresult
0X0
100
111
3988 5439 && !grep { $_ eq $args{'row_names'} } @header) {
3989 3 die "\"$args{'row_names'}\" isn't in the header of $file\n";
3990 }
3991 # A hoh is keyed by this column, so it cannot be left out; said here
3992 # rather than picking another column to key the rows by.
3993 die "read_table: 'drop_cols' leaves out \"$args{'row_names'}\", the row_names "
3994 . "column of $file\n"
3995 if defined $args{'row_names'} && @excl
3996 7184 ▶ && !grep { $_ eq $args{'row_names'} } @uniq_header;
Branch: 1 branch
BranchTrueFalseTotal
if defined $args{"row_names"} and @excl and not grep({$_ eq $args{"row_names"};} @uniq_header) 10 7174 7184
Condition100%6/6
defined $args{"row_names"} and @excl and not grep({$_ eq $args{"row_names"};} @uniq_header)
!ll&&!rl&&r
674443010
defined $args{"row_names"} and @excl
!ll&&!rl&&r
6269475440
MC/DC100%3/3
defined $args{"row_names"} and @excl and not grep({$_ eq $args{"row_names"};} @uniq_header)
defined $args{"row_names"} @excl not grep({$_ eq $args{"row_names"};} @uniq_header)
Truth table100%4/4
A and B and not(C)
A= defined $args{"row_names"}B= @exclC= not grep({$_ eq $args{"row_names"};} @uniq_header)
ABCresult
0XX0
10X0
1100
1111
3997 # A repeated name resolves to its last field, which is the one
3998 # %line_hash keeps, so only that field's filter writes $_ back into it.
3999 7174 my %last_field;
4000 7174 $last_field{ $header[$_] } = $_ for 0 .. $#header;
4001 # An excluded field is not in %line_hash, so a filter keyed on it is
4002 # given the field's own value, as one whose name a later field repeats
4003 # is, and nothing it writes to $_ reaches %line_hash.
4004 7174 ▶ @field_wins = map { $last_field{ $header[$_] } == $_ && !$excl[$_] } 0 .. $#header;
Condition100%3/3
$last_field{$header[$_]} == $_ && !$excl[$_]
!ll&&!rl&&r
162644226596
MC/DC100%2/2
$last_field{$header[$_]} == $_ && !$excl[$_]
$last_field{$header[$_]} == $_ !$excl[$_]
Truth table100%3/3
A && not(B)
A= $last_field{$header[$_]} == $_B= !$excl[$_]
ABresult
0X0
100
111
4005 7174 ▶ if ($filter) {
Branch: 1 branch
BranchTrueFalseTotal
if ($filter) 485 6689 7174
4006 # Each key is resolved to a field before any filter runs: '0' to
4007 # the whole row; a column's name to the last field carrying it;
4008 # and any other number to that field, counting from 1. A name
4009 # comes before a number, so a column named "2021" can be filtered
4010 # on -- up to 0.3213 that key was a field number and died as out
4011 # of range. A field named by two keys (1 and its name, say) runs
4012 # both, in key order; one used to replace the other, and which
4013 # depended on hash order, so the rows kept changed between runs.
4014 485 %mapped_filters = ();
4015 485 for my $k (sort keys %$filter) {
4016 556 my $fld;
4017 556 ▶ if ($k eq '0') {
Branch: 1 branch
BranchTrueFalseTotal
if ($k eq '0') { } 238 318 556
4018 238 $fld = 0;
4019 } else {
4020 318 my $kb = _as_bytes($k);
4021 318 my $idx = $last_field{$kb};
4022 318 ▶ if (!defined $idx && length( $args{comment} // '' )) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $idx and length($args{"comment"} // "")) 103 215 318
Condition60%3/5
not defined $idx and length($args{"comment"} // "")
!ll&&!rl&&r
2150103
$args{"comment"} // ""
l!l
1030
MC/DC50%1/2
not defined $idx and length($args{"comment"} // "")
not defined $idx $args{"comment"}
Truth table66%2/3
not(A) and B
A= not defined $idxB= $args{"comment"}
ABresult
0X0
100
111
4023 # A commented-out header has its marker (and any
4024 # following whitespace) stripped from the first
4025 # column, so a key written as it appears in the file
4026 # (e.g. "# PDB") won't match the clean name ("PDB").
4027 # Normalize the key the same way and retry.
4028 103 (my $nk = $kb) =~ s/^\s*\Q$args{comment}\E\s*//;
4029 103 $idx = $last_field{$nk};
4030 }
4031 318 ▶ if (defined $idx) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $idx) { } 216 102 318
elsif ($k =~ /\A[0-9]+\z/) { } 82 20 102
4032 216 $fld = $idx + 1;
4033 } elsif ($k =~ /\A[0-9]+\z/) {
4034 82 ▶ die "read_table: numeric filter key $k exceeds the "
Branch: 1 branch
BranchTrueFalseTotal
if $k > @header 2 80 82
4035 . scalar(@header) . " columns of $file, and no column "
4036 . "is named '$k'\n"
4037 if $k > @header;
4038 80 $fld = $k + 0;
4039 } else {
4040 die "read_table: Filter column '$k' not found in the "
4041 . "header of $file; header is: "
4042 20 . join( ', ', map { "'$_'" } @header ) . "\n";
4043 }
4044 }
4045 534 push @{ $mapped_filters{$fld} }, $filter->{$k};
4046 }
4047 463 @sorted_filter_flds = sort { $a <=> $b } keys %mapped_filters;
4048 }
4049 7152 ▶ if (defined $cls_arg) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cls_arg) { } 276 6876 7152
4050 276 my $r = ref $cls_arg;
4051 276 ▶ if ($r eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if ($r eq 'HASH') { } 137 139 276
elsif ($r eq 'ARRAY') { } 53 86 139
4052 137 my %by = map { _as_bytes($_) => $cls_of->( $cls_arg->{$_} ) } keys %$cls_arg;
4053 137 ▶ @cls = map { $by{$_} // 3 } @header;
Condition100%2/2
$by{$_} // 3
l!l
205490
MC/DC100%1/1
$by{$_} // 3
$by{$_}
Truth table100%2/2
A
A= $by{$_}
Aresult
11
00
4054 137 my %have = map { $_ => 1 } @header;
4055 warn "read_table: not all columns named in 'colClasses' exist\n"
4056 137 ▶ if grep { !$have{$_} } keys %by;
Branch: 1 branch
BranchTrueFalseTotal
if grep {not $have{$_};} keys %by 9 128 137
4057 } elsif ($r eq 'ARRAY') {
4058 53 ▶ die "read_table: 'colClasses' has " . scalar(@$cls_arg) . " entries "
Branch: 1 branch
BranchTrueFalseTotal
if @$cls_arg > @header 1 52 53
4059 . "for the " . scalar(@header) . " columns of $file\n"
4060 if @$cls_arg > @header;
4061 52 @cls = map { $cls_of->( $cls_arg->[ $_ % @$cls_arg ] ) } 0 .. $#header;
4062 } else {
4063 86 @cls = ( $cls_of->($cls_arg) ) x @header;
4064 }
4065 } else {
4066 6876 @cls = (3) x @header;
4067 }
4068 # all 'character': nothing to convert, and the parser is told nothing
4069 7151 ▶ @cls = () unless grep { $_ } @cls;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_;} @cls 19 7132 7151
4070 # The column arrays are made once and held, so neither the fast path
4071 # nor the row loop below has to fetch them out of %data (and
4072 # autovivify them) once per column per row.
4073 7151 ▶ @hoa_cols = map { $data{$_} ||= [] } @uniq_header if $otype eq 'hoa';
Branch: 1 branch
BranchTrueFalseTotal
if $otype eq "hoa" 896 6255 7151
Condition50%1/2
$data{$_} ||= []
l!l
03988
MC/DC0%0/1
$data{$_} ||= []
$data{$_}
Truth table50%1/2
A
A= $data{$_}
Aresult
11
00
4074 # What the alignment message compares a ragged row with. With
4075 # header => 0 there is no header to count, and up to 0.3217 the message
4076 # said "N headers" all the same, which read as though header => 0 had
4077 # been ignored; it now says where the width came from.
4078 7151 my $w = @header;
4079 7151 ▶ $width_txt = $want_header ? "$w headers"
Branch: 2 branches
BranchTrueFalseTotal
$want_header ? : 6862 289 7151
$col_names ? : 35 254 289
4080 : $col_names ? "$w 'col_names'"
4081 : "$w in data row 1";
4082 # A width of one, read from the file, means that the line that set it
4083 # held no separator, and a later row with more fields did: the shape of
4084 # a file read with the wrong sep. A ';'-separated .csv read with the
4085 # default ',' fails on the first row with a comma in its text. Not for
4086 # an .xlsx, whose cells are not split, nor where 'col_names' set the
4087 # width.
4088 7151 $sep_note = undef;
4089 7151 ▶ if ($w == 1 && !$is_xlsx && !$col_names) {
Branch: 1 branch
BranchTrueFalseTotal
if ($w == 1 and not $is_xlsx and not $col_names) 275 6876 7151
Condition100%6/6
$w == 1 and not $is_xlsx and not $col_names
!ll&&!rl&&r
68742275
$w == 1 and not $is_xlsx
!ll&&!rl&&r
680668277
MC/DC100%3/3
$w == 1 and not $is_xlsx and not $col_names
$w == 1 not $is_xlsx not $col_names
Truth table100%4/4
A and not(B) and not(C)
A= $w == 1B= not $is_xlsxC= not $col_names
ABCresult
0XX0
10X0
1100
1111
4090 275 ▶ my $where = $want_header ? 'the header' : 'data row 1';
Branch: 1 branch
BranchTrueFalseTotal
$want_header ? : 244 31 275
4091 275 ▶ if ($sep_re) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sep_re) { } 72 203 275
4092 72 $sep_note = "$where has no match for the sep regex $sep_re, so "
4093 . "it may not be what separates this file's fields";
4094 } else {
4095 203 ▶ my $s = $args{sep} eq "\t" ? '\t' : $args{sep};
Branch: 1 branch
BranchTrueFalseTotal
$args{'sep'} eq "\t" ? : 29 174 203
4096 $sep_note = exists $input_args{sep}
4097 203 ▶ ? "$where has no '$s' in it, so '$s' may not be what "
Branch: 1 branch
BranchTrueFalseTotal
exists $input_args{'sep'} ? : 53 150 203
4098 . "separates this file's fields"
4099 : "$where has no '$s' in it, and '$s' is only the default "
4100 . "sep for this file's name: pass 'sep' if its fields are "
4101 . "separated by something else";
4102 }
4103 275 $sep_note = _as_bytes($sep_note);
4104 }
4105 7430 };
4106  
4107 # _parse_csv_file() treats a line whose comment marker is followed by
4108 # whitespace (e.g. "# PDB\tscore") as a comment and drops it, so a header
4109 # written that way never reaches the callback and the first data row would
4110 # be mistaken for the header. Recover it: read the first physical line, and
4111 # if it is marker + whitespace and splits into >=2 fields, hold it as a
4112 # CANDIDATE header. It is confirmed (in the callback) only if its field
4113 # count matches the first line the parser delivers and that line looks
4114 # like data rather than a header (see $is_data below); otherwise it was an
4115 # ordinary leading comment and is discarded. A marker hugging its text ("#id,val") is
4116 # delivered by the parser and held as a candidate in the same way by the
4117 # callback, so it never reaches this branch.
4118 7430 ▶ if ($want_header && !$is_xlsx && length( $args{comment} // '' )
Branch: 1 branch
BranchTrueFalseTotal
if ($want_header and not $is_xlsx and length($args{"comment"} // "") and length($args{"sep"} // "")) 6768 662 7430
Condition69%9/13
$want_header and not $is_xlsx and length($args{"comment"} // "") and length($args{"sep"} // "")
!ll&&!rl&&r
66206768
$want_header and not $is_xlsx and length($args{"comment"} // "")
!ll&&!rl&&r
66206768
$want_header and not $is_xlsx
!ll&&!rl&&r
2903726768
$args{"comment"} // ""
l!l
67680
$args{"sep"} // ""
l!l
67680
MC/DC50%2/4
$want_header and not $is_xlsx and length($args{"comment"} // "") and length($args{"sep"} // "")
$want_header not $is_xlsx $args{"comment"} $args{"sep"}
Truth table60%3/5
A and not(B) and C and D
A= $want_headerB= not $is_xlsxC= $args{"comment"}D= $args{"sep"}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
4119 && length( $args{sep} // '' )) {
4120 # $/ is the caller's, and under a `local $/;` this read the whole
4121 # file; _parse_csv_file() splits on "\n" (or "\r", see _eol_is_cr)
4122 # whatever $/ is, and so does this. A UTF-8 byte-order mark is dropped
4123 # here as the parser drops it.
4124 6768 ▶ my $fh = $codec ? _open_decompressed($file, $codec)
Branch: 1 branch
BranchTrueFalseTotal
$codec ? : 96 6672 6768
4125 : _open_read($file);
4126 6768 ▶ binmode $fh unless $codec; # the parser's bytes; see _eol_is_cr for the codec case
Branch: 1 branch
BranchTrueFalseTotal
unless $codec 6672 96 6768
4127 6768 my $first = do { local $/ = $eol; <$fh> };
4128 6768 _close($fh);
4129 6768 ▶ $first =~ s/\A\xEF\xBB\xBF// if defined $first;
Branch: 1 branch
BranchTrueFalseTotal
if defined $first 6765 3 6768
4130 6768 ▶ if (defined $first && $first =~ /^\Q$args{comment}\E\s/) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $first and $first =~ /^\Q$args{"comment"}\E\s/) 112 6656 6768
Condition100%3/3
defined $first and $first =~ /^\Q$args{"comment"}\E\s/
!ll&&!rl&&r
36653112
MC/DC100%2/2
defined $first and $first =~ /^\Q$args{"comment"}\E\s/
defined $first $first =~ /^\Q$args{"comment"}\E\s/
Truth table100%3/3
A and B
A= defined $firstB= $first =~ /^\Q$args{"comment"}\E\s/
ABresult
0X0
100
111
4131 112 ▶ if ($cr_eol) { $first =~ s/\r\z// } else { $first =~ s/\r?\n\z// }
Branch: 1 branch
BranchTrueFalseTotal
if ($cr_eol) { } 2 110 112
4132 # The marker and the blanks after it come off before the cut, so
4133 # that neither can become a field: a tab after "#" in a
4134 # tab-separated file made an empty first column of "#\tid\tval",
4135 # whose three names then failed to match two-field data rows.
4136 112 (my $body = $first) =~ s/^\Q$args{comment}\E\s*//;
4137 # The rest is cut by _parse_csv_file() itself, through an in-memory
4138 # handle, so it is split by exactly the rules the data will be:
4139 # up to 0.3213 a perl split() ignored quoting, and # "x,y",z came
4140 # out as three names. Both mismatches promoted the first data row
4141 # to the header without a word. Quoting is left off for a comment
4142 # with an odd number of '"', which no one-line row of quoted
4143 # fields has, so that prose like 5'10" is not taken for a quoted
4144 # field running to the end of the file; and a sep regex that
4145 # croaks on this line leaves it as no candidate at all, since the
4146 # parser drops it as a comment and would never have seen it.
4147 112 my @cols;
4148 112 ▶ if (length $body) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $body) 112 0 112
4149 112 ▶ my $q = $quote && !(($body =~ tr/"//) % 2);
Condition66%2/3
$quote && !($body =~ tr/"// % 2)
!ll&&!rl&&r
01111
MC/DC50%1/2
$quote && !($body =~ tr/"// % 2)
$quote !($body =~ tr/"// % 2)
Truth table66%2/3
A && not(B)
A= $quoteB= !($body =~ tr/"// % 2)
ABresult
0X0
100
111
4150 112 my $rows = eval {
4151 112 ▶ open my $mfh, '<', \$body
Branch: 1 branch
BranchTrueFalseTotal
open my $mfh, "<", \$body or die "read_table: could not read the commented-out header\n" 112 0 112
4152 or die "read_table: could not read the commented-out header\n";
4153 112 ▶ _parse_csv_file($file, $sep_re ? '' : $args{sep}, '', undef,
Branch: 2 branches
BranchTrueFalseTotal
$sep_re ? : 9 103 112
$sep_re && _sep_re_is_ws($sep_re) ? : 7 105 112
Condition100%3/3
$sep_re and _sep_re_is_ws($sep_re)
!ll&&!rl&&r
10327
MC/DC100%2/2
$sep_re and _sep_re_is_ws($sep_re)
$sep_re _sep_re_is_ws($sep_re)
Truth table100%3/3
A and B
A= $sep_reB= _sep_re_is_ws($sep_re)
ABresult
0X0
100
111
4154 undef, $q, 0, $sep_re,
4155 $sep_re && _sep_re_is_ws($sep_re) ? 1 : 0, $mfh, 0);
4156 };
4157 112 ▶ @cols = @{ $rows->[0] } if $rows && @$rows;
Branch: 1 branch
BranchTrueFalseTotal
if $rows and @$rows 112 0 112
Condition33%1/3
$rows and @$rows
!ll&&!rl&&r
00112
MC/DC0%0/2
$rows and @$rows
$rows @$rows
Truth table33%1/3
A and B
A= $rowsB= @$rows
ABresult
0X0
100
111
4158 }
4159 112 ▶ if (@cols >= 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@cols >= 2) 83 29 112
4160 83 @header = @cols;
4161 83 $header_seen = 1;
4162 83 $provisional_hdr = 1; # confirm against the first data row
4163 }
4164 }
4165 }
4166  
4167 # $quote_note is the parser's account of a quoted field that ran this row
4168 # across lines (S_quote_note() in LikeR.xs), undef for a one-line row; it
4169 # only ever goes into the alignment message. $first_quoted is true when the
4170 # row's first field was written in '"' (S_emit_row()), so that a comment
4171 # marker at its start is text, never a commented-out header's.
4172 32859 ▶ my $on_line = sub {
Subroutine __ANON__: called CC=84 SCAR=44.6
4173 32859 my ($line_ref, $quote_note, $first_quoted) = @_;
4174  
4175 # quote => '' on a file whose first line has every field in '"' keeps
4176 # the quote marks in every name or value: R's write.csv() writes
4177 # exactly that header, and it is read as intended only with the
4178 # default quote. 'quote' means nothing to an .xlsx, so it is not checked.
4179 32859 ▶ if (!$header_seen && !$quote && !$is_xlsx && @$line_ref
Branch: 1 branch
BranchTrueFalseTotal
if (not $header_seen || $quote || $is_xlsx and @$line_ref and not grep({not defined $_ && /\A".*"\z/s;} @$line_ref)) 82 32777 32859
Condition91%11/12
not $header_seen || $quote || $is_xlsx and @$line_ref and not grep({not defined $_ && /\A".*"\z/s;} @$line_ref)
!ll&&!rl&&r
326819682
not $header_seen || $quote || $is_xlsx and @$line_ref
!ll&&!rl&&r
326810178
$header_seen || $quote || $is_xlsx
l!l&&r!l&&!r
326801178
$header_seen or $quote
l!l&&r!l&&!r
255257155179
MC/DC80%4/5
not $header_seen || $quote || $is_xlsx and @$line_ref and not grep({not defined $_ && /\A".*"\z/s;} @$line_ref)
$header_seen $quote $is_xlsx @$line_ref not grep({not defined $_ && /\A".*"\z/s;} @$line_ref)
Truth table83%5/6
not(A or B || C) and D and not(E)
A= $header_seenB= $quoteC= $is_xlsxD= @$line_refE= not grep({not defined $_ && /\A".*"\z/s;} @$line_ref)
ABCDEresult
1XXXX0
01XXX0
001XX0
0000X0
000100
000111
4180 1020 ▶ && !grep { !defined || !/\A".*"\z/s } @$line_ref) {
Condition66%2/3
defined $_ && /\A".*"\z/s
!ll&&!rl&&r
0417603
MC/DC50%1/2
defined $_ && /\A".*"\z/s
defined $_ /\A".*"\z/s
Truth table66%2/3
A && B
A= defined $_B= /\A".*"\z/s
ABresult
0X0
100
111
4181 82 warn "read_table: every field on the first line of $file is "
4182 . "wrapped in '\"', and quote => '' keeps the quote marks as "
4183 . "part of the text; leave 'quote' out to read them as quoting\n";
4184 }
4185  
4186 32859 ▶ if (!$header_seen && !$want_header) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($header_seen or $want_header) 289 32570 32859
Condition100%3/3
$header_seen or $want_header
l!l&&r!l&&!r
255257045289
MC/DC100%2/2
$header_seen or $want_header
$header_seen $want_header
Truth table100%3/3
A or B
A= $header_seenB= $want_header
ABresult
1X1
011
000
4187 # header => 0: this line is the first data row, so it only sets the
4188 # width; the names come from 'col_names' or are V1, V2, ... It falls
4189 # through to be finalized and read as data below.
4190 289 ▶ @header = $col_names ? @$col_names : map { "V$_" } 1 .. @$line_ref;
Branch: 1 branch
BranchTrueFalseTotal
$col_names ? : 35 254 289
4191 289 $header_seen = 1;
4192 }
4193 # A line whose marker hugs its text ("#id,val") is not dropped by the
4194 # parser, since it may be a commented-out header. It is held as a
4195 # CANDIDATE, as the "# id,val" form read above is, and a later one
4196 # replaces it, so in a run of them it is the last -- the one next to
4197 # the data -- that is tried. Up to 0.3213 the first was taken for the
4198 # header on the spot, so a VCF's 140 "##" meta lines became one
4199 # column called "fileformat=VCFv4.2" and the "#CHROM" line an
4200 # alignment error. Only while no real header has been taken: after
4201 # that, such a line is data, as it has always been.
4202 # Not in an .xlsx, where 'comment' has never applied: a first cell
4203 # such as "#id" or "# of items" is a cell, and treating it as a
4204 # commented-out header lost "#a1"-style data rows wholesale up to 0.3213.
4205 # Nor in a quoted first field: R's read.table() reads a comment
4206 # character inside quotes as text, and up to 0.3213 "#a",b came back
4207 # as a column named a -- or, where the next row was text as well, not
4208 # at all, that row being taken for the header in its place.
4209 32859 ▶ if ((!$header_seen || $provisional_hdr) && !$is_xlsx && !$first_quoted
Branch: 1 branch
BranchTrueFalseTotal
if (!$header_seen || $provisional_hdr and not $is_xlsx and not $first_quoted and length($args{"comment"} // "") and @$line_ref and defined $line_ref->[0] and index($line_ref->[0], $args{"comment"}) == 0) 1178 31681 32859
Condition82%19/23
!$header_seen || $provisional_hdr and not $is_xlsx and not $first_quoted and length($args{"comment"} // "") and @$line_ref and defined $line_ref->[0] and index($line_ref->[0], $args{"comment"}) == 0
!ll&&!rl&&r
2548461971178
!$header_seen || $provisional_hdr and not $is_xlsx and not $first_quoted and length($args{"comment"} // "") and @$line_ref and defined $line_ref->[0]
!ll&&!rl&&r
2548407375
!$header_seen || $provisional_hdr and not $is_xlsx and not $first_quoted and length($args{"comment"} // "") and @$line_ref
!ll&&!rl&&r
2548407375
!$header_seen || $provisional_hdr and not $is_xlsx and not $first_quoted and length($args{"comment"} // "")
!ll&&!rl&&r
2548407375
!$header_seen || $provisional_hdr and not $is_xlsx and not $first_quoted
!ll&&!rl&&r
249205647375
!$header_seen || $provisional_hdr and not $is_xlsx
!ll&&!rl&&r
245553657939
not $header_seen or $provisional_hdr
l!l&&r!l&&!r
7045125924555
$args{"comment"} // ""
l!l
73750
MC/DC62%5/8
!$header_seen || $provisional_hdr and not $is_xlsx and not $first_quoted and length($args{"comment"} // "") and @$line_ref and defined $line_ref->[0] and index($line_ref->[0], $args{"comment"}) == 0
not $header_seen $provisional_hdr not $is_xlsx not $first_quoted $args{"comment"} @$line_ref defined $line_ref->[0] index($line_ref->[0], $args{"comment"}) == 0
Truth table53%8/15
not(A) or B and not(C) and not(D) and E and F and G and H
A= not $header_seenB= $provisional_hdrC= not $is_xlsxD= not $first_quotedE= $args{"comment"}F= @$line_refG= defined $line_ref->[0]H= index($line_ref->[0], $args{"comment"}) == 0
ABCDEFGHresult
00XXXXXX0
1X0XXXXX0
010XXXXX0
1X10XXXX0
0110XXXX0
1X110XXX0
01110XXX0
1X1110XX0
011110XX0
1X11110X0
0111110X0
1X1111100
011111100
1X1111111
011111111
4210 && length( $args{comment} // '' )
4211 && @$line_ref && defined $line_ref->[0]
4212 && index($line_ref->[0], $args{comment}) == 0) {
4213 1178 @header = @$line_ref;
4214 1178 $header[0] =~ s/^\Q$args{comment}\E\s*//;
4215 1178 $header_seen = 1;
4216 1178 $provisional_hdr = 1; # confirm against the first data row
4217 1178 return;
4218 }
4219  
4220 31681 ▶ if (!$header_seen) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($header_seen) 6849 24832 31681
4221 # HEADER CAPTURE (copy made only here; runs once). A commented
4222 # line never gets here: it was taken as a candidate just above.
4223 6849 @header = @$line_ref;
4224 6849 $header_seen = 1;
4225 6849 ▶ unless ($want_auto_rn) { # strict: finalize immediately
Branch: 1 branch
BranchTrueFalseTotal
unless ($want_auto_rn) 6302 547 6849
4226 6302 $finalize_header->();
4227 6038 $header_done = 1;
4228 6038 $install_plan->();
4229 }
4230 6585 return;
4231 }
4232  
4233 24832 ▶ if (!$header_done) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($header_done) 1115 23717 24832
4234 # Confirm or reject a provisionally-captured commented-out header:
4235 # it is a real header only if its field count matches the first
4236 # data row. If not, the candidate was an ordinary leading comment;
4237 # discard it and treat THIS delivered line as the header instead.
4238 1115 ▶ if ($provisional_hdr) {
Branch: 1 branch
BranchTrueFalseTotal
if ($provisional_hdr) 277 838 1115
4239 277 $provisional_hdr = 0;
4240 # Under auto_row_names a header one field short of the data is
4241 # the shape being looked for, not a mismatch; refusing it here
4242 # made the first data row the header (0.320 and before).
4243 277 ▶ my $one_short = $want_auto_rn && @$line_ref == @header + 1;
Condition100%3/3
$want_auto_rn && @$line_ref == @header + 1
!ll&&!rl&&r
263212
MC/DC100%2/2
$want_auto_rn && @$line_ref == @header + 1
$want_auto_rn @$line_ref == @header + 1
Truth table100%3/3
A && B
A= $want_auto_rnB= @$line_ref == @header + 1
ABresult
0X0
100
111
4244 # The same width is not enough on its own. "# written by foo,
4245 # v2" before "id,val" has two fields too, and up to 0.3213 that
4246 # comment became the header and "id,val" the first data row,
4247 # without a word. A row the same width is taken for data only
4248 # when one of its fields is a number or an na_strings token, or
4249 # every field is empty, which no header is; otherwise it is the
4250 # file's own header, as R and pandas read it, and the comment
4251 # is a comment.
4252 2066 ▶ my $is_data = (!grep { defined && length } @$line_ref)
Condition66%2/3
defined $_ && length $_
!ll&&!rl&&r
022064
MC/DC50%1/2
defined $_ && length $_
defined $_ length $_
Truth table66%2/3
A && B
A= defined $_B= length $_
ABresult
0X0
100
111
4253 || grep {
4254 277 ▶ defined && length
Condition100%3/3
!grep({defined $_ and length $_;} @$line_ref) || grep({defined $_ and length $_ and &looks_like_number($_) || $has_na && $na_string{$_};} @$line_ref)
l!l&&r!l&&!r
1104172
MC/DC100%2/2
!grep({defined $_ and length $_;} @$line_ref) || grep({defined $_ and length $_ and &looks_like_number($_) || $has_na && $na_string{$_};} @$line_ref)
!grep({defined $_ and length $_;} @$line_ref) grep({defined $_ and length $_ and &looks_like_number($_) || $has_na && $na_string{$_};} @$line_ref)
Truth table100%3/3
not(A) || B
A= !grep({defined $_ and length $_;} @$line_ref)B= grep({defined $_ and length $_ and &looks_like_number($_) || $has_na && $na_string{$_};} @$line_ref)
ABresult
1X1
011
000
4255 2064 ▶ && (looks_like_number($_) || ($has_na && $na_string{$_}))
Condition75%9/12
defined $_ && length $_ && (&looks_like_number($_) || $has_na && $na_string{$_})
!ll&&!rl&&r
01863201
defined $_ and length $_
!ll&&!rl&&r
002064
&looks_like_number($_) or $has_na and $na_string{$_}
l!l&&r!l&&!r
19831863
$has_na and $na_string{$_}
!ll&&!rl&&r
1799643
MC/DC60%3/5
defined $_ && length $_ && (&looks_like_number($_) || $has_na && $na_string{$_})
defined $_ length $_ &looks_like_number($_) $has_na $na_string{$_}
Truth table66%4/6
A and B && C or D and E
A= defined $_B= length $_C= &looks_like_number($_)D= $has_naE= $na_string{$_}
ABCDEresult
0XXXX0
10XXX0
1100X0
110100
111XX1
110111
4256 } @$line_ref;
4257 277 ▶ if (!$one_short && (@$line_ref != @header || !$is_data)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $one_short and @$line_ref != @header || !$is_data) 172 105 277
Condition100%6/6
not $one_short and @$line_ref != @header || !$is_data
!ll&&!rl&&r
1293172
@$line_ref != @header or not $is_data
l!l&&r!l&&!r
1581493
MC/DC100%3/3
not $one_short and @$line_ref != @header || !$is_data
not $one_short @$line_ref != @header not $is_data
Truth table100%4/4
not(A) and B or not(C)
A= not $one_shortB= @$line_ref != @headerC= not $is_data
ABCresult
0XX0
1000
11X1
1011
4258 # not commented: a commented line is a candidate above
4259 172 @header = @$line_ref;
4260 172 ▶ unless ($want_auto_rn) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($want_auto_rn) 170 2 172
4261 170 $finalize_header->();
4262 169 $header_done = 1;
4263 169 $install_plan->();
4264 }
4265 171 return; # this line WAS the header, not data
4266 }
4267 # widths match and the row is data: accept the commented
4268 # header, and let the auto_row_names / finalize logic below
4269 # run on THIS data row.
4270 }
4271  
4272 # First data row in auto_row_names mode: now the data width is
4273 # known, so decide whether the file carries an unlabelled leading
4274 # row-names column (header exactly one field short).
4275 943 ▶ if ($want_auto_rn && @$line_ref == @header + 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($want_auto_rn and @$line_ref == @header + 1) 115 828 943
Condition100%3/3
$want_auto_rn and @$line_ref == @header + 1
!ll&&!rl&&r
381447115
MC/DC100%2/2
$want_auto_rn and @$line_ref == @header + 1
$want_auto_rn @$line_ref == @header + 1
Truth table100%3/3
A and B
A= $want_auto_rnB= @$line_ref == @header + 1
ABresult
0X0
100
111
4276 115 unshift @header, $auto_rn_name;
4277 115 $auto_rn_added = 1;
4278 }
4279 943 $finalize_header->();
4280 942 $header_done = 1;
4281 942 $install_plan->();
4282 # fall through and process THIS line as data
4283 }
4284  
4285 # --- DATA PROCESSING (operate on $line_ref directly; no row copy)
4286 24659 $data_row++;
4287 24659 ▶ if (@$line_ref != @header) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$line_ref != @header) 40 24619 24659
4288 # FIX: alignment errors now say WHICH row is ragged. The words
4289 # are S_fast_row()'s in LikeR.xs, and must stay the same.
4290 40 ▶ die sprintf "Alignment error on %s data row %d (%d fields vs %s)%s%s.\n",
Branch: 2 branches
BranchTrueFalseTotal
defined $quote_note ? : 5 35 40
defined $sep_note && @$line_ref > @header ? : 9 31 40
Condition66%2/3
defined $sep_note and @$line_ref > @header
!ll&&!rl&&r
3109
MC/DC50%1/2
defined $sep_note and @$line_ref > @header
defined $sep_note @$line_ref > @header
Truth table66%2/3
A and B
A= defined $sep_noteB= @$line_ref > @header
ABresult
0X0
100
111
4291 $file, $data_row, scalar @$line_ref, $width_txt,
4292 defined $quote_note ? "; $quote_note" : '',
4293 defined $sep_note && @$line_ref > @header ? "; $sep_note" : '';
4294 }
4295 24619 my %line_hash;
4296 24619 for my $i (@kept_flds) { # every field, unless 'drop_cols' left some out
4297 373051 my $v = $line_ref->[$i];
4298 $line_hash{ $header[$i] }
4299 = ( !defined($v) || $v eq ''
4300 373051 ▶ || ( $has_na && $na_string{$v} ) ) ? undef : $v;
Branch: 1 branch
BranchTrueFalseTotal
!defined($v) || $v eq '' || $has_na && $na_string{$v} ? : 40261 332790 373051
Condition88%8/9
not defined $v or $v eq "" or $has_na and $na_string{$v}
l!l&&r!l&&!r
318888373332790
not defined $v or $v eq ""
l!l&&r!l&&!r
031888341163
$has_na and $na_string{$v}
!ll&&!rl&&r
105903222008373
MC/DC75%3/4
not defined $v or $v eq "" or $has_na and $na_string{$v}
not defined $v $v eq "" $has_na $na_string{$v}
Truth table80%4/5
not(A) or B or C and D
A= not defined $vB= $v eq ""C= $has_naD= $na_string{$v}
ABCDresult
1XXX1
01XX1
00111
000X0
00100
4301 }
4302 # APPLY FILTERS
4303 24619 ▶ if (@sorted_filter_flds) {
Branch: 1 branch
BranchTrueFalseTotal
if (@sorted_filter_flds) 328 24291 24619
4304 328 local *_ = \%line_hash;
4305 328 foreach my $fld (@sorted_filter_flds) {
4306 400 for my $sub (@{ $mapped_filters{$fld} }) {
4307 # The field's value as %line_hash holds it, which is what
4308 # an earlier filter may have changed -- except for a field
4309 # whose name a later field repeats, whose own value is not
4310 # in %line_hash at all; that one is read from the row by
4311 # the rule %line_hash is built by.
4312 local $_ = $fld == 0 ? $line_ref
4313 : $field_wins[ $fld - 1 ] ? $line_hash{ $header[ $fld - 1 ] }
4314 404 ▶ : do {
Branch: 2 branches
BranchTrueFalseTotal
$fld == 0 ? : 79 325 404
$field_wins[$fld - 1] ? : 196 129 325
4315 129 my $v = $line_ref->[ $fld - 1 ];
4316 ( !defined($v) || $v eq ''
4317 129 ▶ || ( $has_na && $na_string{$v} ) ) ? undef : $v
Branch: 1 branch
BranchTrueFalseTotal
!defined($v) || $v eq '' || $has_na && $na_string{$v} ? : 18 111 129
Condition55%5/9
not defined $v or $v eq "" or $has_na and $na_string{$v}
l!l&&r!l&&!r
180111
not defined $v or $v eq ""
l!l&&r!l&&!r
018111
$has_na and $na_string{$v}
!ll&&!rl&&r
11100
MC/DC25%1/4
not defined $v or $v eq "" or $has_na and $na_string{$v}
not defined $v $v eq "" $has_na $na_string{$v}
Truth table40%2/5
not(A) or B or C and D
A= not defined $vB= $v eq ""C= $has_naD= $na_string{$v}
ABCDresult
1XXX1
01XX1
00111
000X0
00100
4318 };
4319 404 ▶ return if !$sub->( $line_ref, \%line_hash );
Branch: 1 branch
BranchTrueFalseTotal
unless &$sub($line_ref, \%line_hash) 86 317 403
4320 317 ▶ next if $fld == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $fld == 0 74 243 317
4321 # write back any mutation made to $_
4322 243 $line_ref->[ $fld - 1 ] = $_;
4323 # na_strings applies to a written-back value too, so the
4324 # empty-string rule and the NA rule stay the same rule.
4325 # A field whose name a later field repeats is not the one
4326 # %line_hash holds, so it does not overwrite it.
4327 $line_hash{ $header[ $fld - 1 ] }
4328 = ( !defined($_) || $_ eq ''
4329 243 ▶ || ( $has_na && $na_string{$_} ) ) ? undef : $_
Branch: 2 branches
BranchTrueFalseTotal
if $field_wins[$fld - 1] 150 93 243
!defined($_) || $_ eq '' || $has_na && $na_string{$_} ? : 13 137 150
Condition77%7/9
not defined $_ or $_ eq "" or $has_na and $na_string{$_}
l!l&&r!l&&!r
130137
not defined $_ or $_ eq ""
l!l&&r!l&&!r
94137
$has_na and $na_string{$_}
!ll&&!rl&&r
111260
MC/DC0%0/4
not defined $_ or $_ eq "" or $has_na and $na_string{$_}
not defined $_ $_ eq "" $has_na $na_string{$_}
Truth table0%0/5
not(A) or B or C and D
A= not defined $_B= $_ eq ""C= $has_naD= $na_string{$_}
ABCDresult
1XXX1
01XX1
00111
000X0
00100
4330 if $field_wins[ $fld - 1 ];
4331 }
4332 }
4333 }
4334 # colClasses, after the filters and on %line_hash itself, as the parser does it
4335 # (S_filter_row() in LikeR.xs); a key a filter deleted stays deleted. An aoa's
4336 # fields are converted as they are copied, below.
4337 24532 ▶ if (@cls && $otype ne 'aoa') {
Branch: 1 branch
BranchTrueFalseTotal
if (@cls and $otype ne "aoa") 18530 6002 24532
Condition100%3/3
@cls and $otype ne "aoa"
!ll&&!rl&&r
15598718530
MC/DC100%2/2
@cls and $otype ne "aoa"
@cls $otype ne "aoa"
Truth table100%3/3
A and B
A= @clsB= $otype ne "aoa"
ABresult
0X0
100
111
4338 18530 ▶ for my $f (grep { $cls[$_] && $field_wins[$_] } 0 .. $#header) {
Condition100%3/3
$cls[$_] && $field_wins[$_]
!ll&&!rl&&r
38379358280640
MC/DC100%2/2
$cls[$_] && $field_wins[$_]
$cls[$_] $field_wins[$_]
Truth table100%3/3
A && B
A= $cls[$_]B= $field_wins[$_]
ABresult
0X0
100
111
4339 280622 my $name = $header[$f];
4340 $line_hash{$name} = _cell_class($line_hash{$name}, $cls[$f],
4341 $file, $data_row, $name, $f + 1)
4342 280622 ▶ if exists $line_hash{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $line_hash{$name} 280622 0 280622
4343 }
4344 }
4345 # Populate requested data structure
4346 24493 ▶ if ($otype eq 'aoh') {
Branch: 4 branches
BranchTrueFalseTotal
if ($otype eq 'aoh') { } 6365 18128 24493
elsif ($otype eq 'aoa') { } 5990 12138 18128
elsif ($otype eq 'hoa') { } 6040 6098 12138
elsif ($otype eq 'hoh') { } 6098 0 6098
4347 6365 push @data, \%line_hash;
4348 } elsif ($otype eq 'aoa') {
4349 # from the fields rather than %line_hash, so a repeated name keeps
4350 # every one of its fields, as the fast path's mode 3 does
4351 push @data, [ map {
4352 91898 my $v = $line_ref->[$_];
4353 91898 ▶ $v = undef if !defined($v) || $v eq '' || ( $has_na && $na_string{$v} );
Branch: 1 branch
BranchTrueFalseTotal
if not defined $v or $v eq "" or $has_na and $na_string{$v} 10043 81855 91898
Condition100%9/9
not defined $v or $v eq "" or $has_na and $na_string{$v}
l!l&&r!l&&!r
7953209081855
not defined $v or $v eq ""
l!l&&r!l&&!r
2795183945
$has_na and $na_string{$v}
!ll&&!rl&&r
1760800952090
MC/DC100%4/4
not defined $v or $v eq "" or $has_na and $na_string{$v}
not defined $v $v eq "" $has_na $na_string{$v}
Truth table100%5/5
not(A) or B or C and D
A= not defined $vB= $v eq ""C= $has_naD= $na_string{$v}
ABCDresult
1XXX1
01XX1
00111
000X0
00100
4354 91898 ▶ ( defined $v && @cls && $cls[$_] )
Branch: 1 branch
BranchTrueFalseTotal
defined $v && @cls && $cls[$_] ? : 81820 10078 91898
Condition100%6/6
defined $v and @cls and $cls[$_]
!ll&&!rl&&r
100572181820
defined $v and @cls
!ll&&!rl&&r
100431481841
MC/DC100%3/3
defined $v and @cls and $cls[$_]
defined $v @cls $cls[$_]
Truth table100%4/4
A and B and C
A= defined $vB= @clsC= $cls[$_]
ABCresult
0XX0
10X0
1100
1111
4355 ? _cell_class($v, $cls[$_], $file, $data_row, $header[$_], $_ + 1)
4356 : $v
4357 5990 } grep { !$excl[$_] } 0 .. $#$line_ref ]; # a field past the header is one a filter added
4358 } elsif ($otype eq 'hoa') {
4359 6040 my $c = 0;
4360 6040 push @{ $hoa_cols[ $c++ ] }, $line_hash{$_} for @uniq_header;
4361 } elsif ($otype eq 'hoh') {
4362 6098 my $row_name = $line_hash{ $args{'row_names'} };
4363 die sprintf "read_table: undefined row name (column '%s') in %s data row %d\n",
4364 6098 ▶ $args{'row_names'}, $file, $data_row
Branch: 1 branch
BranchTrueFalseTotal
unless defined $row_name 8 6090 6098
4365 unless defined $row_name;
4366 6090 ▶ if ($seen_rownames{$row_name}++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seen_rownames{$row_name}++) 5 6085 6090
4367 5 $n_dup_rn++;
4368 5 ▶ $first_dup_rn ||= [ $row_name, $data_row ];
Condition100%2/2
$first_dup_rn ||= [$row_name, $data_row]
l!l
23
MC/DC100%1/1
$first_dup_rn ||= [$row_name, $data_row]
$first_dup_rn
Truth table100%2/2
A
A= $first_dup_rn
Aresult
11
00
4369 }
4370 # made up front, so that a file whose only column is the row name
4371 # still has its rows -- as empty hashes, the way R's read.table
4372 # gives it a data frame of n rows and 0 columns -- rather than none
4373 6090 ▶ my $row = $data{$row_name} ||= {};
Condition100%2/2
$data{$row_name} ||= {}
l!l
56085
MC/DC100%1/1
$data{$row_name} ||= {}
$data{$row_name}
Truth table100%2/2
A
A= $data{$row_name}
Aresult
11
00
4374 6090 foreach my $col (@uniq_header) {
4375 94350 ▶ next if $col eq $args{'row_names'};
Branch: 1 branch
BranchTrueFalseTotal
if $col eq $args{"row_names"} 6090 88260 94350
4376 88260 $row->{$col} = $line_hash{$col};
4377 }
4378 }
4379 7430 };
4380 7430 ▶ if ($is_xlsx) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_xlsx) { } 373 7057 7430
4381 # the sheet first, so a bad 'sheet' is refused before the whole
4382 # shared-string table has been inflated and parsed for nothing
4383 my $chosen = defined $args{_sheet_ix} ? $xlsx_sheets->[ $args{_sheet_ix} ]
4384 373 ▶ : _xlsx_choose_sheet($file, $xlsx_sheets, $args{sheet});
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'_sheet_ix'} ? : 85 288 373
4385 369 ▶ my $sst = $args{_sst} // _xlsx_shared_strings($file, $xlsx_rels);
Condition66%2/3
$args{"_sst"} // _xlsx_shared_strings($file, $xlsx_rels)
l!l&&r!l&&!r
852840
MC/DC0%0/2
$args{"_sst"} // _xlsx_shared_strings($file, $xlsx_rels)
$args{"_sst"} _xlsx_shared_strings($file, $xlsx_rels)
Truth table66%2/3
A // B
A= $args{"_sst"}B= _xlsx_shared_strings($file, $xlsx_rels)
ABresult
1X1
011
000
4386 369 _parse_xlsx_sheet($file, $sst, $chosen->{path}, $on_line, $plan);
4387 } else {
4388 7057 ▶ my $fh = $codec ? _open_decompressed($file, $codec) : undef;
Branch: 1 branch
BranchTrueFalseTotal
$codec ? : 102 6955 7057
4389 _parse_csv_file($file, $sep_re ? '' : $args{sep} // '',
4390 7057 ▶ $args{comment} // '', $on_line, $plan, $quote,
Branch: 3 branches
BranchTrueFalseTotal
$sep_re ? : 820 6237 7057
$want_header ? : 6768 289 7057
$sep_re && _sep_re_is_ws($sep_re) ? : 163 6894 7057
Condition71%5/7
$args{"sep"} // ""
l!l
62370
$args{"comment"} // ""
l!l
70570
$sep_re and _sep_re_is_ws($sep_re)
!ll&&!rl&&r
6237657163
MC/DC0%0/1
$args{"sep"} // ""
$args{"sep"}
MC/DC0%0/1
$args{"comment"} // ""
$args{"comment"}
MC/DC100%2/2
$sep_re and _sep_re_is_ws($sep_re)
$sep_re _sep_re_is_ws($sep_re)
Truth table50%1/2
A
A= $args{"sep"}
Aresult
11
00
Truth table50%1/2
A
A= $args{"comment"}
Aresult
11
00
Truth table100%3/3
A and B
A= $sep_reB= _sep_re_is_ws($sep_re)
ABresult
0X0
100
111
4391 $want_header ? 0 : 1, $sep_re,
4392 $sep_re && _sep_re_is_ws($sep_re) ? 1 : 0, $fh, $cr_eol);
4393 6576 ▶ _close($fh) if $fh;
Branch: 1 branch
BranchTrueFalseTotal
if $fh 96 6480 6576
4394 }
4395 # A hoh's repeated row names, from this closure and from the fast path
4396 # (S_plan_report() in LikeR.xs) together. One warning for the file: up to
4397 # 0.3213 there was one per repeated row, 40,820 of them for a 300,000-row
4398 # file. A single repeat keeps the words it has always had.
4399 6877 ▶ if ($plan && $plan->{dup_n}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($plan and $plan->{"dup_n"}) 222 6655 6877
Condition100%3/3
$plan and $plan->{"dup_n"}
!ll&&!rl&&r
9015754222
MC/DC100%2/2
$plan and $plan->{"dup_n"}
$plan $plan->{"dup_n"}
Truth table100%3/3
A and B
A= $planB= $plan->{"dup_n"}
ABresult
0X0
100
111
4400 222 $n_dup_rn += $plan->{dup_n};
4401 222 ▶ $first_dup_rn ||= [ $plan->{dup_first}, $plan->{dup_row} ];
Condition50%1/2
$first_dup_rn ||= [$plan->{"dup_first"}, $plan->{"dup_row"}]
l!l
0222
MC/DC0%0/1
$first_dup_rn ||= [$plan->{"dup_first"}, $plan->{"dup_row"}]
$first_dup_rn
Truth table50%1/2
A
A= $first_dup_rn
Aresult
11
00
4402 }
4403 6877 ▶ if ($n_dup_rn == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if ($n_dup_rn == 1) { } 9 6868 6877
elsif ($n_dup_rn) { } 216 6652 6868
4404 9 warn "read_table: duplicate row name '$first_dup_rn->[0]' in $file "
4405 . "(later values win)\n";
4406 } elsif ($n_dup_rn) {
4407 216 warn "read_table: $n_dup_rn rows of $file repeat an earlier row's name "
4408 . "(later values win); the first is '$first_dup_rn->[0]', on data "
4409 . "row $first_dup_rn->[1]\n";
4410 }
4411 # header-only files never hit a data row. A provisional (commented-out)
4412 # header was never confirmed against a data row, but with no data to
4413 # contradict it we accept it; either way still validate.
4414 6877 ▶ $finalize_header->() if $header_seen && !$header_done;
Branch: 1 branch
BranchTrueFalseTotal
if $header_seen and not $header_done 2 6875 6877
Condition100%3/3
$header_seen and not $header_done
!ll&&!rl&&r
768682
MC/DC100%2/2
$header_seen and not $header_done
$header_seen not $header_done
Truth table100%3/3
A and not(B)
A= $header_seenB= not $header_done
ABresult
0X0
100
111
4415 # A hoa's column arrays are now made when the header is finalized rather
4416 # than by the first push into them, so a file with a header and no data
4417 # rows would come back as one empty array per column. It has always come
4418 # back as {}, the way an aoh comes back as [], so keep it that way.
4419 6877 ▶ %data = () if $otype eq 'hoa' && @hoa_cols && !@{ $hoa_cols[0] };
Branch: 1 branch
BranchTrueFalseTotal
if $otype eq "hoa" and @hoa_cols and not @{$hoa_cols[0];} 9 6868 6877
Condition83%5/6
$otype eq "hoa" and @hoa_cols and not @{$hoa_cols[0];}
!ll&&!rl&&r
60088609
$otype eq "hoa" and @hoa_cols
!ll&&!rl&&r
60080869
MC/DC66%2/3
$otype eq "hoa" and @hoa_cols and not @{$hoa_cols[0];}
$otype eq "hoa" @hoa_cols not @{$hoa_cols[0];}
Truth table75%3/4
A and B and not(C)
A= $otype eq "hoa"B= @hoa_colsC= not @{$hoa_cols[0];}
ABCresult
0XX0
10X0
1100
1111
4420 6877 ▶ if ($otype eq 'aoh' || $otype eq 'aoa') {
Branch: 1 branch
BranchTrueFalseTotal
if ($otype eq 'aoh' or $otype eq 'aoa') { } 5181 1696 6877
Condition100%3/3
$otype eq "aoh" or $otype eq "aoa"
l!l&&r!l&&!r
225429271696
MC/DC100%2/2
$otype eq "aoh" or $otype eq "aoa"
$otype eq "aoh" $otype eq "aoa"
Truth table100%3/3
A or B
A= $otype eq "aoh"B= $otype eq "aoa"
ABresult
1X1
011
000
4421 5181 return \@data;
4422 } else { # hoa or hoh
4423 1696 return \%data;
4424 }
4425 }
4426 # view() and _render_grid(), the renderer it shares with summary(), are in
4427 # LikeR.xs, under "view() and summary(): the table renderer".
4428  
4429 # TukeyHSD($fit, %opts) -- Tukey Honest Significant Differences.
4430 #
4431 # Mirrors R's stats::TukeyHSD for the fitted objects produced by this
4432 # module's aov(), lm() and glm(). Base R only defines TukeyHSD.aov; this
4433 # extends the same all-pairwise studentized-range comparison to lm and glm
4434 # outputs as well.
4435 #
4436 # The fitted objects here do not retain the model frame, so unlike R the
4437 # response values and per-level replication counts are recomputed from the
4438 # data. Therefore the caller supplies the data frame and the response name:
4439 #
4440 # my $fit = aov({ weight => \@w, group => \@g }, 'weight ~ group');
4441 # my $hsd = TukeyHSD($fit, data => $df, formula => 'weight ~ group');
4442 # # or: TukeyHSD($fit, data => $df, response => 'weight');
4443 #
4444 # Options:
4445 # data (required) the AoH / HoA / HoH used to fit the model
4446 # response response column name; or give formula => 'y ~ ...'
4447 # formula alternative to response: LHS is parsed for the response
4448 # which factor name or arrayref of names (default: all factors)
4449 # conf_level confidence level, default 0.95 (the dotted conf.level is
4450 # refused as an unknown option)
4451 # ordered if true, order each factor's levels by increasing mean
4452 #
4453 # Returns a hashref: one entry per factor mapping to an arrayref of
4454 # comparison hashes { comparison, diff, lwr, upr, 'p adj' } in R's
4455 # lower-triangle order, plus the attributes 'conf_level' and 'ordered'.
4456 #
4457 # Scope: main-effect factors (those present in $fit->{xlevels}); a grouping
4458 # variable must be categorical (string levels) to be treated as a factor,
4459 # exactly as R requires factor(). MSE is the residual mean square (for glm
4460 # this is deviance/df_residual: exact for the gaussian family, a Wald-type
4461 # scale otherwise). Per-level means are observed marginal means, which
4462 # match R's model.tables means for one-way (and balanced) designs.
4463 30 ▶sub TukeyHSD {
Subroutine TukeyHSD: called CC=53 SCAR=43.3
Pod undocumented
4464 30 ▶ _usage_error('TukeyHSD', 'first argument must be a fitted-model hashref (from aov/lm/glm)')
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_[0] eq "HASH" 3 27 30
4465 unless ref($_[0]) eq 'HASH';
4466 # checked before %opt is filled, which an odd list would make perl's own
4467 # "Odd number of elements" die under FATAL warnings
4468 27 ▶ _usage_error('TukeyHSD', 'arguments after the fit must be name => value pairs')
Branch: 1 branch
BranchTrueFalseTotal
unless @_ % 2 1 26 27
4469 unless @_ % 2;
4470 26 my ($fit, %opt) = @_;
4471 26 my %known = map { $_ => 1 } qw(conf_level data formula ordered response which);
4472 26 my @bad = sort grep { !$known{$_} } keys %opt;
4473 26 ▶ _usage_error('TukeyHSD', "unknown argument(s): @bad") if @bad;
Branch: 1 branch
BranchTrueFalseTotal
if @bad 0 26 26
4474  
4475 26 ▶ my $conf = exists $opt{conf_level} ? $opt{conf_level} : 0.95;
Branch: 1 branch
BranchTrueFalseTotal
exists $opt{'conf_level'} ? : 3 23 26
4476 26 ▶ _usage_error('TukeyHSD', 'conf_level must be between 0 and 1')
Branch: 1 branch
BranchTrueFalseTotal
unless $conf > 0 and $conf < 1 0 26 26
Condition33%1/3
$conf > 0 and $conf < 1
!ll&&!rl&&r
0026
MC/DC0%0/2
$conf > 0 and $conf < 1
$conf > 0 $conf < 1
Truth table33%1/3
A and B
A= $conf > 0B= $conf < 1
ABresult
0X0
100
111
4477 unless $conf > 0 && $conf < 1;
4478 26 ▶ my $ordered = $opt{ordered} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$opt{'ordered'} ? : 8 18 26
4479  
4480 my $data = $opt{data}
4481 26 ▶ or _usage_error('TukeyHSD', "'data' (the data frame used to fit the model) is required");
Branch: 1 branch
BranchTrueFalseTotal
my $data = $opt{"data"} or _usage_error("TukeyHSD", "'data' (the data frame used to fit the model) is required") 25 1 26
4482  
4483 # residual mean square (MSE) and residual d.f., per model type
4484 25 my ($mse, $df);
4485 25 ▶ if (ref($fit->{Residuals}) eq 'HASH') { # aov
Branch: 3 branches
BranchTrueFalseTotal
if (ref $fit->{'Residuals'} eq 'HASH') { } 23 2 25
elsif (exists $fit->{'rss'} and exists $fit->{'df_residual'}) { } 1 1 2
elsif (exists $fit->{'deviance'} and exists $fit->{'df_residual'}) { } 1 0 1
4486 23 $df = $fit->{Residuals}{Df};
4487 23 $mse = $fit->{Residuals}{'Mean Sq'};
4488 ▶ } elsif (exists($fit->{rss}) && exists($fit->{'df_residual'})) { # lm
Condition66%2/3
exists $fit->{"rss"} and exists $fit->{"df_residual"}
!ll&&!rl&&r
101
MC/DC50%1/2
exists $fit->{"rss"} and exists $fit->{"df_residual"}
exists $fit->{"rss"} exists $fit->{"df_residual"}
Truth table66%2/3
A and B
A= exists $fit->{"rss"}B= exists $fit->{"df_residual"}
ABresult
0X0
100
111
4489 1 $df = $fit->{'df_residual'};
4490 1 ▶ $mse = ($df > 0) ? $fit->{rss} / $df : undef;
Branch: 1 branch
BranchTrueFalseTotal
$df > 0 ? : 1 0 1
4491 ▶ } elsif (exists($fit->{deviance}) && exists($fit->{'df_residual'})) { # glm
Condition33%1/3
exists $fit->{"deviance"} and exists $fit->{"df_residual"}
!ll&&!rl&&r
001
MC/DC0%0/2
exists $fit->{"deviance"} and exists $fit->{"df_residual"}
exists $fit->{"deviance"} exists $fit->{"df_residual"}
Truth table33%1/3
A and B
A= exists $fit->{"deviance"}B= exists $fit->{"df_residual"}
ABresult
0X0
100
111
4492 1 $df = $fit->{'df_residual'};
4493 1 ▶ $mse = ($df > 0) ? $fit->{deviance} / $df : undef;
Branch: 1 branch
BranchTrueFalseTotal
$df > 0 ? : 1 0 1
4494 } else {
4495 0 die 'TukeyHSD: could not find residual MSE/df in the fit (expected aov/lm/glm output)';
4496 }
4497 25 ▶ die 'TukeyHSD: residual degrees of freedom must be >= 2 (got '
Branch: 2 branches
BranchTrueFalseTotal
unless defined $df and $df >= 2 0 25 25
defined $df ? : 0 0 0
Condition33%1/3
defined $df and $df >= 2
!ll&&!rl&&r
0025
MC/DC0%0/2
defined $df and $df >= 2
defined $df $df >= 2
Truth table33%1/3
A and B
A= defined $dfB= $df >= 2
ABresult
0X0
100
111
4498 . (defined($df) ? $df : 'undef') . ')'
4499 unless defined($df) && $df >= 2;
4500 25 ▶ die 'TukeyHSD: could not determine a positive residual mean square'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $mse and $mse > 0 0 25 25
Condition33%1/3
defined $mse and $mse > 0
!ll&&!rl&&r
0025
MC/DC0%0/2
defined $mse and $mse > 0
defined $mse $mse > 0
Truth table33%1/3
A and B
A= defined $mseB= $mse > 0
ABresult
0X0
100
111
4501 unless defined($mse) && $mse > 0;
4502  
4503 # WIDE one-way layout
4504 # data = { level => [observations], ... } with no response/formula: each
4505 # key is a group and its arrayref holds that group's values -- the same
4506 # shape aov() auto-stacks when the formula is omitted. Simpler than R's
4507 # long format: no stacked response column, no separate factor column.
4508 25 ▶ if ( !defined($opt{response}) && !defined($opt{formula})
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $opt{"response"} || defined $opt{"formula"} and ref $data eq "HASH" and keys %$data >= 2 and not grep({ref $_ ne "ARRAY";} values %$data) and not grep({not grep({defined $_ and &looks_like_number($_);} @$_);} values %$data)) 2 23 25
Condition80%12/15
not defined $opt{"response"} || defined $opt{"formula"} and ref $data eq "HASH" and keys %$data >= 2 and not grep({ref $_ ne "ARRAY";} values %$data) and not grep({not grep({defined $_ and &looks_like_number($_);} @$_);} values %$data)
!ll&&!rl&&r
2212
not defined $opt{"response"} || defined $opt{"formula"} and ref $data eq "HASH" and keys %$data >= 2 and not grep({ref $_ ne "ARRAY";} values %$data)
!ll&&!rl&&r
2203
not defined $opt{"response"} || defined $opt{"formula"} and ref $data eq "HASH" and keys %$data >= 2
!ll&&!rl&&r
2203
not defined $opt{"response"} || defined $opt{"formula"} and ref $data eq "HASH"
!ll&&!rl&&r
2203
defined $opt{'response'} || defined $opt{'formula'}
l!l&&r!l&&!r
1663
MC/DC50%3/6
not defined $opt{"response"} || defined $opt{"formula"} and ref $data eq "HASH" and keys %$data >= 2 and not grep({ref $_ ne "ARRAY";} values %$data) and not grep({not grep({defined $_ and &looks_like_number($_);} @$_);} values %$data)
defined $opt{'response'} defined $opt{'formula'} ref $data eq "HASH" keys %$data >= 2 not grep({ref $_ ne "ARRAY";} values %$data) not grep({not grep({defined $_ and &looks_like_number($_);} @$_);} values %$data)
Truth table57%4/7
not(A || B) and C and D and not(E) and not(F)
A= defined $opt{'response'}B= defined $opt{'formula'}C= ref $data eq "HASH"D= keys %$data >= 2E= not grep({ref $_ ne "ARRAY";} values %$data)F= not grep({not grep({defined $_ and &looks_like_number($_);} @$_);} values %$data)
ABCDEFresult
1XXXXX0
01XXXX0
000XXX0
0010XX0
00110X0
0011100
0011111
4509 && ref($data) eq 'HASH' && keys(%$data) >= 2
4510 8 && (!grep { ref($_) ne 'ARRAY' } values %$data)
4511 8 ▶ && (!grep { !grep { defined && looks_like_number($_) } @$_ } values %$data)) {
Condition66%2/3
defined $_ && &looks_like_number($_)
!ll&&!rl&&r
03090
MC/DC50%1/2
defined $_ && &looks_like_number($_)
defined $_ &looks_like_number($_)
Truth table66%2/3
A && B
A= defined $_B= &looks_like_number($_)
ABresult
0X0
100
111
4512 2 ▶ my $label = (defined $opt{which} && !ref $opt{which}) ? $opt{which} : 'group';
Branch: 1 branch
BranchTrueFalseTotal
defined $opt{'which'} && !ref($opt{'which'}) ? : 1 1 2
Condition66%2/3
defined $opt{"which"} and not ref $opt{"which"}
!ll&&!rl&&r
101
MC/DC50%1/2
defined $opt{"which"} and not ref $opt{"which"}
defined $opt{"which"} not ref $opt{"which"}
Truth table66%2/3
A and not(B)
A= defined $opt{"which"}B= not ref $opt{"which"}
ABresult
0X0
100
111
4513 2 my (%sum, %cnt);
4514 2 for my $lev (keys %$data) {
4515 6 for my $yv (@{ $data->{$lev} }) {
4516 60 ▶ next unless defined($yv) && looks_like_number($yv);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $yv and &looks_like_number($yv) 0 60 60
Condition33%1/3
defined $yv and &looks_like_number($yv)
!ll&&!rl&&r
0060
MC/DC0%0/2
defined $yv and &looks_like_number($yv)
defined $yv &looks_like_number($yv)
Truth table33%1/3
A and B
A= defined $yvB= &looks_like_number($yv)
ABresult
0X0
100
111
4517 60 $sum{$lev} += $yv;
4518 60 $cnt{$lev}++;
4519 }
4520 }
4521 2 my @levels = grep { $cnt{$_} } sort keys %$data; # R orders levels alphabetically
4522 2 ▶ die 'TukeyHSD: need at least 2 non-empty groups' unless @levels >= 2;
Branch: 1 branch
BranchTrueFalseTotal
unless @levels >= 2 0 2 2
4523 2 my @means = map { $sum{$_} / $cnt{$_} } @levels;
4524 2 my @n = map { $cnt{$_} } @levels;
4525 return {
4526 2 $label => _tukey_compare(\@levels, \@means, \@n, $mse, $df, $conf, $ordered),
4527 'conf_level' => $conf,
4528 ordered => $ordered,
4529 };
4530 }
4531  
4532 # --- LONG layout (R-style): a response column + one or more factor columns
4533 23 my $resp = $opt{response};
4534 23 ▶ if (!defined($resp) && defined $opt{formula}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $resp and defined $opt{"formula"}) 6 17 23
Condition100%3/3
not defined $resp and defined $opt{"formula"}
!ll&&!rl&&r
1616
MC/DC100%2/2
not defined $resp and defined $opt{"formula"}
not defined $resp defined $opt{"formula"}
Truth table100%3/3
not(A) and B
A= not defined $respB= defined $opt{"formula"}
ABresult
0X0
100
111
4535 6 ($resp) = $opt{formula} =~ /\A\s*([^~]+?)\s*~/;
4536 }
4537 23 ▶ _usage_error('TukeyHSD', "need the response variable; pass response => 'name' or formula => 'y ~ ...'")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $resp and length $resp 1 22 23
Condition66%2/3
defined $resp and length $resp
!ll&&!rl&&r
1022
MC/DC50%1/2
defined $resp and length $resp
defined $resp length $resp
Truth table66%2/3
A and B
A= defined $respB= length $resp
ABresult
0X0
100
111
4538 unless defined($resp) && length $resp;
4539  
4540 # factors present in the model (idx 0 of each xlevels entry = reference)
4541 22 my $xl = $fit->{xlevels};
4542 22 ▶ die 'TukeyHSD: no factors in the fitted model (nothing to compare)'
Branch: 1 branch
BranchTrueFalseTotal
unless ref $xl eq "HASH" and keys %$xl 0 22 22
Condition33%1/3
ref $xl eq "HASH" and keys %$xl
!ll&&!rl&&r
0022
MC/DC0%0/2
ref $xl eq "HASH" and keys %$xl
ref $xl eq "HASH" keys %$xl
Truth table33%1/3
A and B
A= ref $xl eq "HASH"B= keys %$xl
ABresult
0X0
100
111
4543 unless ref($xl) eq 'HASH' && keys %$xl;
4544  
4545 my @which = defined($opt{which})
4546 22 ▶ ? (ref($opt{which}) eq 'ARRAY' ? @{ $opt{which} } : ($opt{which}))
Branch: 2 branches
BranchTrueFalseTotal
defined $opt{'which'} ? : 2 20 22
ref $opt{'which'} eq 'ARRAY' ? : 1 1 2
4547 : (sort keys %$xl);
4548  
4549 22 my @factors;
4550 22 for my $f (@which) {
4551 35 ▶ if (exists $xl->{$f}) { push @factors, $f }
Branch: 1 branch
BranchTrueFalseTotal
if (exists $xl->{$f}) { } 34 1 35
4552 1 else { warn "TukeyHSD: '$f' is not a factor in the model and will be dropped\n" }
4553 }
4554 22 ▶ _usage_error('TukeyHSD', "'which' specified no factors") unless @factors;
Branch: 1 branch
BranchTrueFalseTotal
unless @factors 0 22 22
4555  
4556 22 my $y = _tukey_col($data, $resp);
4557  
4558 22 my %out;
4559 22 for my $f (@factors) {
4560 34 my $g = _tukey_col($data, $f);
4561 34 ▶ die "TukeyHSD: response '$resp' and factor '$f' differ in length"
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @$y == scalar @$g 0 34 34
4562 unless scalar(@$y) == scalar(@$g);
4563  
4564 34 my (%sum, %cnt);
4565 34 for my $i (0 .. $#$g) {
4566 1589 my $gv = $g->[$i];
4567 1589 my $yv = $y->[$i];
4568 1589 ▶ next unless defined($gv) && defined($yv) && looks_like_number($yv);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $gv and defined $yv and &looks_like_number($yv) 0 1589 1589
Condition33%2/6
defined $gv and defined $yv and &looks_like_number($yv)
!ll&&!rl&&r
001589
defined $gv and defined $yv
!ll&&!rl&&r
001589
MC/DC0%0/3
defined $gv and defined $yv and &looks_like_number($yv)
defined $gv defined $yv &looks_like_number($yv)
Truth table25%1/4
A and B and C
A= defined $gvB= defined $yvC= &looks_like_number($yv)
ABCresult
0XX0
10X0
1100
1111
4569 1589 $sum{$gv} += $yv;
4570 1589 $cnt{$gv}++;
4571 }
4572  
4573 # canonical level order from xlevels, then any extra observed levels
4574 34 my @levels = @{ $xl->{$f} };
4575 34 my %seen = map { $_ => 1 } @levels;
4576 34 push @levels, sort grep { !$seen{$_} } keys %cnt;
4577 34 @levels = grep { $cnt{$_} } @levels; # only levels that carry data
4578  
4579 34 ▶ die "TukeyHSD: factor '$f' needs at least 2 non-empty levels"
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @levels >= 2 0 34 34
4580 unless scalar(@levels) >= 2;
4581  
4582 34 my @means = map { $sum{$_} / $cnt{$_} } @levels;
4583 34 my @n = map { $cnt{$_} } @levels;
4584  
4585 34 $out{$f} = _tukey_compare(\@levels, \@means, \@n, $mse, $df, $conf, $ordered);
4586 }
4587  
4588 22 $out{'conf_level'} = $conf; # attributes, R-style
4589 22 $out{ordered} = $ordered;
4590 22 return \%out;
4591 }
4592  
4593 # _tukey_compare(\@levels, \@means, \@n, $mse, $df, $conf, $ordered)
4594 # Shared HSD math for one factor: builds the pairwise-comparison rows in R's
4595 # lower-triangle, column-major order. Used by both the wide and long paths.
4596 36 ▶sub _tukey_compare {
Subroutine _tukey_compare: called CC=6 SCAR=18.0
4597 36 my ($levels, $means, $n, $mse, $df, $conf, $ordered) = @_;
4598 36 my @levels = @$levels;
4599 36 my @means = @$means;
4600 36 my @n = @$n;
4601 36 ▶ if ($ordered) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ordered) 14 22 36
4602 14 my @ord = sort { $means[$a] <=> $means[$b] } 0 .. $#means;
4603 14 @levels = @levels[@ord];
4604 14 @means = @means[@ord];
4605 14 @n = @n[@ord];
4606 }
4607 36 my $k = scalar @means;
4608 36 my $crit = Stats::LikeR::qtukey($conf, $k, $df); # nranges = 1
4609 36 my @rows;
4610 36 for my $j (0 .. $k - 1) { # column-major lower triangle
4611 99 for my $i ($j + 1 .. $k - 1) {
4612 96 my $diff = $means[$i] - $means[$j];
4613 96 my $se = sqrt( ($mse / 2) * (1 / $n[$i] + 1 / $n[$j]) );
4614 96 my $width = $crit * $se;
4615 96 ▶ my $est = ($se > 0)
Branch: 2 branches
BranchTrueFalseTotal
$se > 0 ? : 96 0 96
$diff >= 0 ? : 0 0 0
4616 ? $diff / $se
4617 : ($diff >= 0 ? 9**9**9 : -(9**9**9));
4618 96 my $padj = Stats::LikeR::ptukey(abs($est), $k, $df, 'lower_tail' => 0);
4619 96 push @rows, {
4620 comparison => "$levels[$i]-$levels[$j]",
4621 diff => $diff,
4622 lwr => $diff - $width,
4623 upr => $diff + $width,
4624 'p adj' => $padj,
4625 };
4626 }
4627 }
4628 36 return \@rows;
4629 }
4630  
4631 # melt / pivot_table / fillna / ffill / bfill
4632 # pure-Perl additions to lib/Stats/LikeR.pm
4633 #
4634 # Placement: splice the reshape pair (melt, pivot_table) in after concat/
4635 # rbind, and the impute trio (fillna, ffill, bfill) in after dropna. Add
4636 # melt pivot_table fillna ffill bfill
4637 # to @EXPORT_OK (== @EXPORT). All five reshape/impute at the Perl level
4638 # only; the sole numeric work is in pivot_table, which reuses the XS
4639 # reducers through _agg_reduce, so there is no XS/ABI surface added here.
4640 #
4641 # NA is undef throughout, exactly as dropna() treats it (a missing hash key
4642 # counts as NA). Every function returns a NEW top-level frame and never
4643 # mutates its input. Shape is classified by _df_shape (the agg()/view()
4644 # detector); 'output_type' defaults to the input family, like agg().
4645  
4646 # _frame_cols($df, $shape, \@need, \@rows) -> (\%col, $R, \@rows)
4647 #
4648 # Extract the named columns once, aligned to row positions 0 .. R-1, using
4649 # the same per-shape access agg() uses. For HoA the column arrays are
4650 # aliased (read-only callers), not rebuilt; every other shape materialises
4651 # a fresh per-column slice. HoH rows are visited in string-sorted key
4652 # order so a positional axis exists. \@rows is those rows in that order,
4653 # undef for HoA, which needs none; passing back the one an earlier call
4654 # returned skips finding them again -- for HoH, a sort of every key -- so a
4655 # caller can fetch one column at a time. Not exported.
4656 79 ▶sub _frame_cols {
Subroutine _frame_cols: called CC=12 SCAR=24.9
4657 79 my ($df, $shape, $need, $rows) = @_;
4658 79 my %col;
4659 79 ▶ if ($shape eq 'HoA') {
Branch: 1 branch
BranchTrueFalseTotal
if ($shape eq "HoA") 33 46 79
4660 33 my $R = 0;
4661 33 ▶ for my $v (values %$df) { $R = @$v if ref $v eq 'ARRAY' && @$v > $R }
Branch: 1 branch
BranchTrueFalseTotal
if ref $v eq "ARRAY" and @$v > $R 37 52 89
Condition66%2/3
ref $v eq "ARRAY" and @$v > $R
!ll&&!rl&&r
05237
MC/DC50%1/2
ref $v eq "ARRAY" and @$v > $R
ref $v eq "ARRAY" @$v > $R
Truth table66%2/3
A and B
A= ref $v eq "ARRAY"B= @$v > $R
ABresult
0X0
100
111
4662 33 ▶ for my $c (@$need) { $col{$c} = ref $df->{$c} eq 'ARRAY' ? $df->{$c} : [] }
Branch: 1 branch
BranchTrueFalseTotal
ref $df->{$c} eq 'ARRAY' ? : 34 0 34
4663 33 return (\%col, $R, undef);
4664 }
4665 2 $rows ||= $shape eq 'HoH' ? [ map { $df->{$_} } sort keys %$df ]
4666 46 ▶ : [ grep { defined } @$df ];
Branch: 1 branch
BranchTrueFalseTotal
$shape eq 'HoH' ? : 1 37 38
Condition66%2/3
$rows ||= $shape eq "HoH" ? [map({$df->{$_};} sort(keys %$df))] : [grep({defined $_;} @$df)]
l!l&&r!l&&!r
8380
MC/DC0%0/2
$rows ||= $shape eq "HoH" ? [map({$df->{$_};} sort(keys %$df))] : [grep({defined $_;} @$df)]
$rows $shape eq "HoH" ? [map({$df->{$_};} sort(keys %$df))] : [grep({defined $_;} @$df)]
Truth table66%2/3
A ||= B
A= $rowsB= $shape eq "HoH" ? [map({$df->{$_};} sort(keys %$df))] : [grep({defined $_;} @$df)]
ABresult
1X1
011
000
4667 46 ▶ if ($shape eq 'AoA') {
Branch: 1 branch
BranchTrueFalseTotal
if ($shape eq 'AoA') { } 1 45 46
4668 1 for my $c (@$need) { $col{$c} = [ map { $_->[$c] } @$rows ] }
4669 } else { # AoH, HoH
4670 45 for my $c (@$need) { $col{$c} = [ map { $_->{$c} } @$rows ] }
4671 }
4672 46 return (\%col, scalar @$rows, $rows);
4673 }
4674  
4675 # _sort_group_keys(\@order, \%repr) -> \@sorted
4676 #
4677 # Order group keys by their representative value tuple. Each tuple position
4678 # is compared on its own terms: numerically when every defined value in that
4679 # position (across every group) looks like a number, else as strings. Within a
4680 # position undef sorts last, as pandas puts its NaN group, and a NaN sorts after
4681 # every number and before undef -- `<=>` has no answer for it, and under this
4682 # module's FATAL warnings a sort comparator that returns undef dies.
4683 #
4684 # Up to 0.3212 one numeric-or-string decision covered every position and any
4685 # undef made it "string", so by => ['sex', 'age'], or a single missing key,
4686 # sorted the numbers 10, 2, 9. Used by agg() and pivot_table(). Not exported.
4687 270 ▶sub _sort_group_keys {
Subroutine _sort_group_keys: called CC=15 SCAR=27.1
4688 270 my ($order, $repr) = @_;
4689 270 ▶ return [ @$order ] unless @$order;
Branch: 1 branch
BranchTrueFalseTotal
unless @$order 0 270 270
4690 270 my $width = @{ $repr->{ $order->[0] } };
4691 270 my @num = (1) x $width;
4692 270 for my $k (@$order) {
4693 5654 my $t = $repr->{$k};
4694 5654 for my $j (0 .. $width - 1) {
4695 10830 ▶ $num[$j] = 0 if $num[$j] && defined $t->[$j] && !looks_like_number($t->[$j]);
Branch: 1 branch
BranchTrueFalseTotal
if $num[$j] and defined $t->[$j] and not &looks_like_number($t->[$j]) 248 10582 10830
Condition100%6/6
$num[$j] and defined $t->[$j] and not &looks_like_number($t->[$j])
!ll&&!rl&&r
53515231248
$num[$j] and defined $t->[$j]
!ll&&!rl&&r
5338135479
MC/DC100%3/3
$num[$j] and defined $t->[$j] and not &looks_like_number($t->[$j])
$num[$j] defined $t->[$j] not &looks_like_number($t->[$j])
Truth table100%4/4
A and B and not(C)
A= $num[$j]B= defined $t->[$j]C= not &looks_like_number($t->[$j])
ABCresult
0XX0
10X0
1100
1111
4696 }
4697 }
4698 # per position: [ class, value ]; class 0 = value, 1 = NaN, 2 = undef
4699 270 my %key;
4700 270 for my $k (@$order) {
4701 5654 my $t = $repr->{$k};
4702 $key{$k} = [ map {
4703 5654 my $v = $t->[$_];
4704 10830 ▶ !defined $v ? ( 2, 0 )
Branch: 3 branches
BranchTrueFalseTotal
!defined($v) ? : 54 10776 10830
!$num[$_] ? : 5545 5231 10776
$v != $v ? : 1 5230 5231
4705 : !$num[$_] ? ( 0, $v )
4706 : $v != $v ? ( 1, 0 )
4707 : ( 0, 0 + $v )
4708 } 0 .. $width - 1 ];
4709 }
4710 return [ sort {
4711 270 my ($ka, $kb) = ($key{$a}, $key{$b});
4712 43445 my $c = 0;
4713 43445 for my $j (0 .. $width - 1) {
4714 47571 my $i = 2 * $j;
4715 47571 ▶ last if $c = $ka->[$i] <=> $kb->[$i]
Branch: 2 branches
BranchTrueFalseTotal
if $c = $ka->[$i] <=> $kb->[$i] || ($num[$j] ? $$ka[$i + 1] <=> $$kb[$i + 1] : $$ka[$i + 1] cmp $$kb[$i + 1]) 43444 4127 47571
$num[$j] ? : 42575 4912 47487
Condition100%3/3
$ka->[$i] <=> $kb->[$i] || ($num[$j] ? $$ka[$i + 1] <=> $$kb[$i + 1] : $$ka[$i + 1] cmp $$kb[$i + 1])
l!l&&r!l&&!r
84433604127
MC/DC100%2/2
$ka->[$i] <=> $kb->[$i] || ($num[$j] ? $$ka[$i + 1] <=> $$kb[$i + 1] : $$ka[$i + 1] cmp $$kb[$i + 1])
$ka->[$i] <=> $kb->[$i] ($num[$j] ? $$ka[$i + 1] <=> $$kb[$i + 1] : $$ka[$i + 1] cmp $$kb[$i + 1])
Truth table100%3/3
A || B
A= $ka->[$i] <=> $kb->[$i]B= ($num[$j] ? $$ka[$i + 1] <=> $$kb[$i + 1] : $$ka[$i + 1] cmp $$kb[$i + 1])
ABresult
1X1
011
000
4716 || ( $num[$j] ? $ka->[$i + 1] <=> $kb->[$i + 1]
4717 : $ka->[$i + 1] cmp $kb->[$i + 1] );
4718 }
4719 $c;
4720 } @$order ];
4721 }
4722  
4723 # melt($df, id_vars => $col|\@cols, value_vars => $col|\@cols,
4724 # var_name => 'variable', value_name => 'value', 'output_type' => aoa|aoh|hoa|hoh)
4725 #
4726 # Wide -> long, like pandas DataFrame.melt. Each cell of the value_vars
4727 # columns becomes its own output row: the id_vars are copied across, the
4728 # var_name column holds the source column identifier and the value_name
4729 # column holds the cell. Column identifiers are names for AoH/HoA/HoH and
4730 # 0-based integer positions for AoA.
4731 #
4732 # id_vars scalar or arrayref; default none.
4733 # value_vars scalar or arrayref; default every column not in id_vars
4734 # (column universe and order come from colnames()).
4735 # var_name name of the variable column; default 'variable'.
4736 # value_name name of the value column; default 'value'.
4737 # 'output_type' aoa|aoh|hoa|hoh; default the input family. For aoa output
4738 # the columns are positional (id_vars.., variable, value) so
4739 # var_name/value_name are not used. For hoh output the row
4740 # labels are reset to 0 .. N-1 (like pandas' RangeIndex).
4741 #
4742 # Row order is column-major, matching pandas: all rows for value_vars[0],
4743 # then all rows for value_vars[1], and so on, preserving input row order
4744 # within each block. The original frame is never modified.
4745 22 ▶sub melt {
Subroutine melt: called CC=29 SCAR=33.7
Pod documented
4746 22 my $df = shift;
4747 22 ▶ _usage_error('melt', 'undefined data in first position') unless defined $df;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $df 2 20 22
4748 20 my $shape = _df_shape($df, 'melt');
4749 18 ▶ _usage_error('melt', 'arguments after the data frame must be name => value pairs')
Branch: 1 branch
BranchTrueFalseTotal
if @_ % 2 2 16 18
4750 if @_ % 2;
4751 16 my %arg = @_;
4752 16 my %known = ( id_vars => 1, value_vars => 1, var_name => 1,
4753 value_name => 1, 'output_type' => 1 );
4754 16 my @bad = sort grep { !$known{$_} } keys %arg;
4755 16 ▶ _usage_error('melt', "unknown argument(s): @bad") if @bad;
Branch: 1 branch
BranchTrueFalseTotal
if @bad 1 15 16
4756  
4757 my @id = !defined $arg{id_vars} ? ()
4758 1 : ref $arg{id_vars} eq 'ARRAY' ? @{ $arg{id_vars} }
4759 15 ▶ : ( $arg{id_vars} );
Branch: 2 branches
BranchTrueFalseTotal
!defined($arg{'id_vars'}) ? : 1 14 15
ref $arg{'id_vars'} eq 'ARRAY' ? : 1 13 14
4760 # A name repeated in id_vars is one output column, so it is kept once:
4761 # pushed twice per row, it made that column of a hoa twice as long as the rest.
4762 15 { my %s; @id = grep { !$s{$_}++ } @id }
4763 15 ▶ my $var_name = defined $arg{var_name} ? $arg{var_name} : 'variable';
Branch: 1 branch
BranchTrueFalseTotal
defined $arg{'var_name'} ? : 3 12 15
4764 15 ▶ my $value_name = defined $arg{value_name} ? $arg{value_name} : 'value';
Branch: 1 branch
BranchTrueFalseTotal
defined $arg{'value_name'} ? : 2 13 15
4765 15 ▶ my $otype = defined $arg{'output_type'} ? lc $arg{'output_type'} : lc $shape;
Branch: 1 branch
BranchTrueFalseTotal
defined $arg{'output_type'} ? : 4 11 15
4766 15 my %ok_otype = ( aoa => 1, aoh => 1, hoa => 1, hoh => 1 );
4767 _usage_error('melt', "output_type '$otype' isn't allowed (aoa, aoh, hoa, hoh)")
4768 15 ▶ unless $ok_otype{$otype};
Branch: 1 branch
BranchTrueFalseTotal
unless $ok_otype{$otype} 1 14 15
4769  
4770 14 my @universe = colnames($df);
4771 14 my %uni = map { $_ => 1 } @universe;
4772 14 my %is_id = map { $_ => 1 } @id;
4773 16 ▶ my @val = !defined $arg{value_vars} ? ( grep { !$is_id{$_} } @universe )
Branch: 1 branch
BranchTrueFalseTotal
ref $arg{'value_vars'} eq 'ARRAY' ? : 3 4 7
4774 3 : ref $arg{value_vars} eq 'ARRAY' ? @{ $arg{value_vars} }
4775 14 ▶ : ( $arg{value_vars} );
Branch: 1 branch
BranchTrueFalseTotal
!defined($arg{'value_vars'}) ? : 7 7 14
4776 14 for my $c (@id, @val) {
4777 33 ▶ die "melt: column '$c' not found\n" unless $uni{$c};
Branch: 1 branch
BranchTrueFalseTotal
unless $uni{$c} 1 32 33
4778 }
4779 # name hygiene: the emitted variable/value columns must not collide
4780 13 ▶ _usage_error('melt', 'var_name and value_name must differ')
Branch: 1 branch
BranchTrueFalseTotal
if $var_name eq $value_name 1 12 13
4781 if $var_name eq $value_name;
4782 12 ▶ if ($otype ne 'aoa') {
Branch: 1 branch
BranchTrueFalseTotal
if ($otype ne "aoa") 10 2 12
4783 10 for my $c (@id) {
4784 11 ▶ die "melt: var_name '$var_name' collides with an id_vars column\n"
Branch: 1 branch
BranchTrueFalseTotal
if $c eq $var_name 1 10 11
4785 if $c eq $var_name;
4786 10 ▶ die "melt: value_name '$value_name' collides with an id_vars column\n"
Branch: 1 branch
BranchTrueFalseTotal
if $c eq $value_name 0 10 10
4787 if $c eq $value_name;
4788 }
4789 }
4790  
4791 11 my %need; $need{$_} = 1 for @id, @val;
4792 11 my ($col, $R) = _frame_cols($df, $shape, [ keys %need ]);
4793  
4794 # Column-major, straight into the requested shape. This used to build the
4795 # whole long frame first, as one arrayref-of-arrayrefs record per output
4796 # row, and then walk it again to materialise -- so a melt of R rows over V
4797 # value columns held R*V records, each with a nested arrayref of the id
4798 # values, alive at the same time as the result it was about to become. On
4799 # a million-row frame with ten value columns that is ten million throwaway
4800 # containers, and none of them is anything the output needs. Emitting as
4801 # the loops go keeps only the result. The output type is branched on once,
4802 # outside the loops, and a hoa is built a whole column at a time: per cell,
4803 # the branch and the pushes made a hoa melt about 4 times slower.
4804 11 my @idcol = map { $col->{$_} } @id;
4805 11 ▶ if ($otype eq 'hoa') {
Branch: 1 branch
BranchTrueFalseTotal
if ($otype eq "hoa") 2 9 11
4806 2 my %hoa = map { $_ => [] } @id, $var_name, $value_name;
4807 2 for my $v (@val) {
4808 3 my $vcol = $col->{$v};
4809 # _frame_cols() columns can be short (ragged AoA rows); a slice
4810 # reads undef past the end, as $vcol->[$i] did, and keeps R cells.
4811 3 push @{ $hoa{ $id[$_] } }, @{ $idcol[$_] }[0 .. $R - 1] for 0 .. $#id;
4812 3 push @{ $hoa{$var_name} }, ($v) x $R;
4813 3 push @{ $hoa{$value_name} }, @{$vcol}[0 .. $R - 1];
4814 }
4815 2 return \%hoa;
4816 }
4817 9 ▶ if ($otype eq 'aoa') {
Branch: 1 branch
BranchTrueFalseTotal
if ($otype eq "aoa") 2 7 9
4818 2 my @aoa;
4819 2 for my $v (@val) {
4820 3 my $vcol = $col->{$v};
4821 3 for my $i (0 .. $R - 1) {
4822 6 push @aoa, [ (map { $_->[$i] } @idcol), $v, $vcol->[$i] ];
4823 }
4824 }
4825 2 return \@aoa;
4826 }
4827 7 my @rows; # aoh and hoh share the row
4828 7 for my $v (@val) {
4829 10 my $vcol = $col->{$v};
4830 10 for my $i (0 .. $R - 1) {
4831 17 my %h;
4832 17 @h{ @id } = map { $_->[$i] } @idcol;
4833 17 $h{$var_name} = $v;
4834 17 $h{$value_name} = $vcol->[$i];
4835 17 push @rows, \%h;
4836 }
4837 }
4838 7 ▶ return \@rows if $otype eq 'aoh';
Branch: 1 branch
BranchTrueFalseTotal
if $otype eq "aoh" 5 2 7
4839 2 return { map { $_ => $rows[$_] } 0 .. $#rows }; # hoh: RangeIndex
4840 }
4841  
4842 # pivot_table($df, index => $col|\@cols, columns => $col|\@cols,
4843 # values => $col|\@cols, aggfunc => 'mean', fill_value => undef,
4844 # skipna => 1, sort => 1, sep => '.', 'output_type' => ...)
4845 #
4846 # Long -> wide with aggregation, like pandas DataFrame.pivot_table. Rows are
4847 # the distinct `index` tuples; the distinct `columns` tuples spread into new
4848 # output columns; each cell is `aggfunc` applied to the `values` that fall in
4849 # that (index, columns) bucket. This is the combine half of agg() with the
4850 # group value spread across columns instead of down rows, and it reuses the
4851 # same aggregator vocabulary.
4852 #
4853 # index scalar/arrayref of columns that become output rows; default
4854 # none (a single aggregated row).
4855 # columns scalar/arrayref whose distinct value tuples become output
4856 # columns. REQUIRED. A row whose `columns` tuple has any NA
4857 # is skipped (no column can be named from it).
4858 # values scalar/arrayref of columns to aggregate; default every column
4859 # not used by index/columns.
4860 # aggfunc one aggregator name, an arrayref of names, or a coderef;
4861 # default 'mean'. Named: mean median sum sd var min max count
4862 # n nunique first last mode (the agg() set). A coderef is
4863 # called as $code->(\@cells) with every cell (NA included).
4864 # fill_value substituted for any NA result cell (missing bucket, or an
4865 # aggregate that came back undef); default leaves undef.
4866 # skipna 0|1, default 1; forwarded to the numeric reducers as in agg.
4867 # sort 0|1, default 1; sort output rows and columns by their key
4868 # (numeric if every key looks numeric, else string).
4869 # sep separator for generated column names; default '.'.
4870 # 'output_type' aoa|aoh|hoa|hoh; default input family. For hoh the row
4871 # label is the index tuple joined with '.', uniquified with .N.
4872 #
4873 # Generated column names join, with `sep` and in this order, only the pieces
4874 # that vary: the aggregator name (only when >1 aggregator), the value column
4875 # (only when >1 value), and always the `columns` tuple. With a single value
4876 # and single aggregator the name is exactly the `columns` tuple, matching
4877 # pandas' flat output. A duplicate generated name is an error (raise `sep`).
4878 # The original frame is never modified.
4879 36 ▶sub pivot_table {
Subroutine pivot_table: called CC=65 SCAR=41.8
Pod documented
4880 36 my $df = shift;
4881 36 ▶ _usage_error('pivot_table', 'undefined data in first position') unless defined $df;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $df 2 34 36
4882 34 my $shape = _df_shape($df, 'pivot_table');
4883 32 ▶ _usage_error('pivot_table', 'arguments after the data frame must be name => value pairs')
Branch: 1 branch
BranchTrueFalseTotal
if @_ % 2 1 31 32
4884 if @_ % 2;
4885 31 my %arg = @_;
4886 31 my %known = ( index => 1, columns => 1, values => 1, aggfunc => 1,
4887 fill_value => 1, skipna => 1, sort => 1, sep => 1,
4888 'output_type' => 1 );
4889 31 my @bad = sort grep { !$known{$_} } keys %arg;
4890 31 ▶ _usage_error('pivot_table', "unknown argument(s): @bad") if @bad;
Branch: 1 branch
BranchTrueFalseTotal
if @bad 1 30 31
4891 30 ▶ _usage_error('pivot_table', "'columns' is required") unless defined $arg{columns};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $arg{"columns"} 2 28 30
4892  
4893 my @index = !defined $arg{index} ? ()
4894 2 : ref $arg{index} eq 'ARRAY' ? @{ $arg{index} }
4895 28 ▶ : ( $arg{index} );
Branch: 2 branches
BranchTrueFalseTotal
!defined($arg{'index'}) ? : 8 20 28
ref $arg{'index'} eq 'ARRAY' ? : 2 18 20
4896 28 ▶ my @columns = ref $arg{columns} eq 'ARRAY' ? @{ $arg{columns} } : ( $arg{columns} );
Branch: 1 branch
BranchTrueFalseTotal
ref $arg{'columns'} eq 'ARRAY' ? : 3 25 28
4897 28 ▶ _usage_error('pivot_table', "'columns' must name at least one column") unless @columns;
Branch: 1 branch
BranchTrueFalseTotal
unless @columns 0 28 28
4898  
4899 28 ▶ my $sep = defined $arg{sep} ? $arg{sep} : '.';
Branch: 1 branch
BranchTrueFalseTotal
defined $arg{'sep'} ? : 2 26 28
4900 28 ▶ my $skipna = exists $arg{skipna} ? ($arg{skipna} ? 1 : 0) : 1;
Branch: 2 branches
BranchTrueFalseTotal
exists $arg{'skipna'} ? : 1 27 28
$arg{'skipna'} ? : 0 1 1
4901 28 ▶ my $dosort = exists $arg{sort} ? ($arg{sort} ? 1 : 0) : 1;
Branch: 2 branches
BranchTrueFalseTotal
exists $arg{'sort'} ? : 1 27 28
$arg{'sort'} ? : 0 1 1
4902 28 my $has_fill = exists $arg{fill_value};
4903 28 my $fill = $arg{fill_value};
4904 28 ▶ my $otype = defined $arg{'output_type'} ? lc $arg{'output_type'} : lc $shape;
Branch: 1 branch
BranchTrueFalseTotal
defined $arg{'output_type'} ? : 10 18 28
4905 28 my %ok_otype = ( aoa => 1, aoh => 1, hoa => 1, hoh => 1 );
4906 _usage_error('pivot_table', "output_type '$otype' isn't allowed (aoa, aoh, hoa, hoh)")
4907 28 ▶ unless $ok_otype{$otype};
Branch: 1 branch
BranchTrueFalseTotal
unless $ok_otype{$otype} 1 27 28
4908  
4909 27 ▶ my $af = exists $arg{aggfunc} ? $arg{aggfunc} : 'mean';
Branch: 1 branch
BranchTrueFalseTotal
exists $arg{'aggfunc'} ? : 22 5 27
4910 27 ▶ my @funcs = ref $af eq 'ARRAY' ? @$af : ( $af );
Branch: 1 branch
BranchTrueFalseTotal
ref $af eq 'ARRAY' ? : 3 24 27
4911 27 ▶ _usage_error('pivot_table', 'empty aggfunc list') unless @funcs;
Branch: 1 branch
BranchTrueFalseTotal
unless @funcs 1 26 27
4912 26 my %known_agg = map { $_ => 1 }
4913 qw(mean median sum sd var min max count n nunique first last mode);
4914 26 for my $f (@funcs) {
4915 28 ▶ next if ref $f eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $f eq "CODE" 1 27 28
4916 27 ▶ _usage_error('pivot_table', "unknown aggfunc '$f'") unless $known_agg{$f};
Branch: 1 branch
BranchTrueFalseTotal
unless $known_agg{$f} 1 26 27
4917 }
4918  
4919 25 my @universe = colnames($df);
4920 25 my %uni = map { $_ => 1 } @universe;
4921 25 my %reserved = map { $_ => 1 } @index, @columns;
4922 4 ▶ my @values = !defined $arg{values} ? ( grep { !$reserved{$_} } @universe )
Branch: 1 branch
BranchTrueFalseTotal
ref $arg{'values'} eq 'ARRAY' ? : 1 22 23
4923 1 : ref $arg{values} eq 'ARRAY' ? @{ $arg{values} }
4924 25 ▶ : ( $arg{values} );
Branch: 1 branch
BranchTrueFalseTotal
!defined($arg{'values'}) ? : 2 23 25
4925 25 ▶ die "pivot_table: no value columns to aggregate\n" unless @values;
Branch: 1 branch
BranchTrueFalseTotal
unless @values 0 25 25
4926 25 for my $c (@index, @columns, @values) {
4927 75 ▶ die "pivot_table: column '$c' not found\n" unless $uni{$c};
Branch: 1 branch
BranchTrueFalseTotal
unless $uni{$c} 1 74 75
4928 }
4929  
4930 24 my %need; $need{$_} = 1 for @index, @columns, @values;
4931 24 my ($col, $R) = _frame_cols($df, $shape, [ keys %need ]);
4932  
4933 # bucket every row under (index tuple, columns tuple), first-seen order
4934 24 my (%rrepr, @rorder, %rseen, %crepr, @corder, %cseen, %bucket);
4935 24 ▶ for (my $i = 0; $i < $R; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $R) 88 24 112
4936 88 my @cv = map { $col->{$_}[$i] } @columns;
4937 88 ▶ next if grep { !defined } @cv; # NA columns tuple -> unnameable
Branch: 1 branch
BranchTrueFalseTotal
if grep {not defined $_;} @cv 1 87 88
4938 87 my $ck = join "\x1e", map { "v$_" } @cv;
4939 87 ▶ unless ($cseen{$ck}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cseen{$ck}) 40 47 87
4940 40 $cseen{$ck} = 1; push @corder, $ck; $crepr{$ck} = [ @cv ];
4941 }
4942 87 my @iv = map { $col->{$_}[$i] } @index;
4943 my $rk = @index
4944 87 ▶ ? join("\x1e", map { defined $_ ? "v$_" : "\0" } @iv)
Branch: 2 branches
BranchTrueFalseTotal
@index ? : 76 11 87
defined $_ ? : 80 2 82
4945 : "\0all";
4946 87 ▶ unless ($rseen{$rk}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($rseen{$rk}) 42 45 87
4947 42 $rseen{$rk} = 1; push @rorder, $rk; $rrepr{$rk} = [ @iv ];
4948 }
4949 87 push @{ $bucket{$rk}{$ck}{$_} }, $col->{$_}[$i] for @values;
4950 }
4951 24 ▶ if ($dosort) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dosort) 23 1 24
4952 23 ▶ @rorder = @{ _sort_group_keys(\@rorder, \%rrepr) } if @index;
Branch: 1 branch
BranchTrueFalseTotal
if @index 20 3 23
4953 23 @corder = @{ _sort_group_keys(\@corder, \%crepr) };
4954 }
4955  
4956 # output column plan: aggfunc-major, then value, then columns tuple
4957 24 my $multi_f = @funcs > 1;
4958 24 my $multi_v = @values > 1;
4959 24 my @colplan; # [ ck, value, func, outname ]
4960 24 for my $f (@funcs) {
4961 26 ▶ my $fl = ref $f eq 'CODE' ? 'fn' : $f;
Branch: 1 branch
BranchTrueFalseTotal
ref $f eq 'CODE' ? : 1 25 26
4962 26 for my $vv (@values) {
4963 27 for my $ck (@corder) {
4964 45 ▶ my $cstr = join $sep, map { defined $_ ? $_ : '' } @{ $crepr{$ck} };
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 51 0 51
4965 45 my @pieces;
4966 45 ▶ push @pieces, $fl if $multi_f;
Branch: 1 branch
BranchTrueFalseTotal
if $multi_f 6 39 45
4967 45 ▶ push @pieces, $vv if $multi_v;
Branch: 1 branch
BranchTrueFalseTotal
if $multi_v 4 41 45
4968 45 push @pieces, $cstr;
4969 45 push @colplan, [ $ck, $vv, $f, join($sep, @pieces) ];
4970 }
4971 }
4972 }
4973 24 my @out_names = ( @index, map { $_->[3] } @colplan );
4974 {
4975 24 my (%seen, @dup);
4976 24 ▶ for my $n (@out_names) { push @dup, $n if $seen{$n}++ == 1 }
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$n}++ == 1 1 66 67
4977 24 ▶ die "pivot_table: generated duplicate column name(s): @dup; "
Branch: 1 branch
BranchTrueFalseTotal
if @dup 1 23 24
4978 . "pass a different 'sep' or rename inputs\n" if @dup;
4979 }
4980  
4981 # materialise straight into the requested shape
4982 23 my (@aoa, @aoh, %hoa, %hoh, %lseen);
4983 23 ▶ if ($otype eq 'hoa') { $hoa{$_} = [] for @out_names }
Branch: 1 branch
BranchTrueFalseTotal
if ($otype eq "hoa") 1 22 23
4984 23 for my $rk (@rorder) {
4985 41 my @vals = @{ $rrepr{$rk} }; # index values
4986 41 for my $cp (@colplan) {
4987 74 my ($ck, $vv, $f) = @$cp;
4988 74 my $raw = $bucket{$rk}{$ck}{$vv};
4989 74 my $cell;
4990 74 ▶ if (defined $raw && @$raw) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $raw and @$raw) 72 2 74
Condition66%2/3
defined $raw and @$raw
!ll&&!rl&&r
2072
MC/DC50%1/2
defined $raw and @$raw
defined $raw @$raw
Truth table66%2/3
A and B
A= defined $rawB= @$raw
ABresult
0X0
100
111
4991 72 my @def = grep { defined } @$raw;
4992 72 $cell = _agg_reduce($f, $raw, \@def, $skipna);
4993 }
4994 74 ▶ $cell = $fill if !defined $cell && $has_fill;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $cell and $has_fill 2 72 74
Condition100%3/3
not defined $cell and $has_fill
!ll&&!rl&&r
7112
MC/DC100%2/2
not defined $cell and $has_fill
not defined $cell $has_fill
Truth table100%3/3
not(A) and B
A= not defined $cellB= $has_fill
ABresult
0X0
100
111
4995 74 push @vals, $cell;
4996 }
4997 41 ▶ if ($otype eq 'aoa') {
Branch: 3 branches
BranchTrueFalseTotal
if ($otype eq 'aoa') { } 12 29 41
elsif ($otype eq 'aoh') { } 22 7 29
elsif ($otype eq 'hoa') { } 2 5 7
4998 12 push @aoa, \@vals;
4999 } elsif ($otype eq 'aoh') {
5000 22 my %h; @h{ @out_names } = @vals; push @aoh, \%h;
5001 } elsif ($otype eq 'hoa') {
5002 2 push @{ $hoa{ $out_names[$_] } }, $vals[$_] for 0 .. $#out_names;
5003 } else { # hoh
5004 my $label = @index
5005 5 ▶ ? join('.', map { defined $_ ? $_ : '' } @{ $rrepr{$rk} })
Branch: 2 branches
BranchTrueFalseTotal
@index ? : 4 1 5
defined $_ ? : 6 0 6
5006 : 'all';
5007 5 my $uniq = $label; my $j = 0;
5008 5 ▶ while (exists $lseen{$uniq}) { $uniq = $label . '.' . (++$j) }
Branch: 1 branch
BranchTrueFalseTotal
if (exists $lseen{$uniq}) 1 5 6
5009 5 $lseen{$uniq} = 1;
5010 5 my %h; @h{ @out_names } = @vals; $hoh{$uniq} = \%h;
5011 }
5012 }
5013 23 ▶ return \@aoa if $otype eq 'aoa';
Branch: 1 branch
BranchTrueFalseTotal
if $otype eq "aoa" 5 18 23
5014 18 ▶ return \@aoh if $otype eq 'aoh';
Branch: 1 branch
BranchTrueFalseTotal
if $otype eq "aoh" 14 4 18
5015 4 ▶ return \%hoa if $otype eq 'hoa';
Branch: 1 branch
BranchTrueFalseTotal
if $otype eq "hoa" 1 3 4
5016 3 return \%hoh;
5017 }
5018  
5019 # fillna($df, value => $scalar | { col => val, ... }, cols => \@cols)
5020 #
5021 # Replace NA (undef) cells with a constant, like pandas DataFrame.fillna with
5022 # a scalar or a dict. `value` is REQUIRED and is either a single scalar (fill
5023 # every NA in the frame, or only within `cols` when given) or a hashref mapping
5024 # column => fill value (only those columns are touched; a dict key that names
5025 # no existing column is ignored, matching pandas, and `cols` is then forbidden).
5026 # For a scalar `value`, an explicit `cols` that names a missing column dies,
5027 # like dropna(). Column identifiers are names for AoH/HoA/HoH and 0-based
5028 # integer positions for AoA.
5029 #
5030 # A targeted column's missing hash key counts as NA and is materialised on
5031 # fill (as in dropna's NA view). AoA rows are not extended past their own
5032 # length. A structurally-undef (non-ref) row is passed through unchanged, not
5033 # fabricated into a data row, matching ffill()/bfill(). For propagation instead
5034 # of a constant use ffill()/bfill(). Returns
5035 # a NEW frame (rows/columns rebuilt as needed); the original is never modified.
5036 22 ▶sub fillna {
Subroutine fillna: called CC=35 SCAR=35.6
Pod documented
5037 22 my $df = shift;
5038 22 ▶ _usage_error('fillna', 'undefined data in first position') unless defined $df;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $df 2 20 22
5039 20 my $shape = _df_shape($df, 'fillna');
5040 18 ▶ _usage_error('fillna', 'arguments after the data frame must be name => value pairs')
Branch: 1 branch
BranchTrueFalseTotal
if @_ % 2 1 17 18
5041 if @_ % 2;
5042 17 my %arg = @_;
5043 17 my %known = ( value => 1, cols => 1 );
5044 17 my @bad = sort grep { !$known{$_} } keys %arg;
5045 17 ▶ _usage_error('fillna', "unknown argument(s): @bad") if @bad;
Branch: 1 branch
BranchTrueFalseTotal
if @bad 1 16 17
5046 16 ▶ _usage_error('fillna', "a 'value' is required") unless exists $arg{value};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $arg{"value"} 2 14 16
5047  
5048 14 my $value = $arg{value};
5049 14 my $per_col = ref $value eq 'HASH';
5050 _usage_error('fillna', "'cols' cannot be combined with a per-column 'value' hashref")
5051 14 ▶ if $per_col && exists $arg{cols};
Branch: 1 branch
BranchTrueFalseTotal
if $per_col and exists $arg{"cols"} 1 13 14
Condition100%3/3
$per_col and exists $arg{"cols"}
!ll&&!rl&&r
1211
MC/DC100%2/2
$per_col and exists $arg{"cols"}
$per_col exists $arg{"cols"}
Truth table100%3/3
A and B
A= $per_colB= exists $arg{"cols"}
ABresult
0X0
100
111
5052  
5053 13 my @universe = colnames($df);
5054 13 my %uni = map { $_ => 1 } @universe;
5055  
5056 13 my (%fillmap, $scalar_fill, %tset, @targets);
5057 13 ▶ if ($per_col) {
Branch: 1 branch
BranchTrueFalseTotal
if ($per_col) { } 1 12 13
5058 1 %fillmap = %$value;
5059 1 @targets = grep { $uni{$_} } keys %fillmap; # ignore unknown dict keys
5060 } else {
5061 12 $scalar_fill = $value;
5062 12 ▶ if (exists $arg{cols}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $arg{'cols'}) { } 3 9 12
5063 _usage_error('fillna', "'cols' must be an arrayref")
5064 3 ▶ unless ref $arg{cols} eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $arg{"cols"} eq "ARRAY" 0 3 3
5065 3 for my $c (@{ $arg{cols} }) {
5066 3 ▶ die "fillna: column '$c' not found\n" unless $uni{$c};
Branch: 1 branch
BranchTrueFalseTotal
unless $uni{$c} 1 2 3
5067 }
5068 2 @targets = @{ $arg{cols} };
5069 } else {
5070 9 @targets = @universe;
5071 }
5072 }
5073 12 %tset = map { $_ => 1 } @targets;
5074 12 ▶ my $tv = sub { $per_col ? $fillmap{ $_[0] } : $scalar_fill };
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$per_col ? : 1 18 19
5075  
5076 12 ▶ if ($shape eq 'AoH') {
Branch: 1 branch
BranchTrueFalseTotal
if ($shape eq "AoH") 6 6 12
5077 6 my @out;
5078 6 for my $row (@$df) {
5079 10 ▶ unless (ref $row eq 'HASH') { push @out, $row; next }
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $row eq "HASH") 1 9 10
5080 9 my %h = %$row;
5081 9 ▶ for my $c (@targets) { $h{$c} = $tv->($c) unless defined $h{$c} }
Branch: 1 branch
BranchTrueFalseTotal
unless defined $h{$c} 8 6 14
5082 9 push @out, \%h;
5083 }
5084 6 return \@out;
5085 }
5086 6 ▶ if ($shape eq 'HoH') {
Branch: 1 branch
BranchTrueFalseTotal
if ($shape eq "HoH") 2 4 6
5087 2 my %out;
5088 2 for my $rk (keys %$df) {
5089 5 my $row = $df->{$rk};
5090 5 ▶ unless (ref $row eq 'HASH') { $out{$rk} = $row; next }
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $row eq "HASH") 1 4 5
5091 4 my %h = %$row;
5092 4 ▶ for my $c (@targets) { $h{$c} = $tv->($c) unless defined $h{$c} }
Branch: 1 branch
BranchTrueFalseTotal
unless defined $h{$c} 3 3 6
5093 4 $out{$rk} = \%h;
5094 }
5095 2 return \%out;
5096 }
5097 4 ▶ if ($shape eq 'HoA') {
Branch: 1 branch
BranchTrueFalseTotal
if ($shape eq "HoA") 1 3 4
5098 1 my $R = 0;
5099 1 ▶ for my $v (values %$df) { $R = @$v if ref $v eq 'ARRAY' && @$v > $R }
Branch: 1 branch
BranchTrueFalseTotal
if ref $v eq "ARRAY" and @$v > $R 1 1 2
Condition66%2/3
ref $v eq "ARRAY" and @$v > $R
!ll&&!rl&&r
011
MC/DC50%1/2
ref $v eq "ARRAY" and @$v > $R
ref $v eq "ARRAY" @$v > $R
Truth table66%2/3
A and B
A= ref $v eq "ARRAY"B= @$v > $R
ABresult
0X0
100
111
5100 1 my %out;
5101 1 for my $c (keys %$df) {
5102 2 ▶ my $arr = ref $df->{$c} eq 'ARRAY' ? $df->{$c} : [];
Branch: 1 branch
BranchTrueFalseTotal
ref $df->{$c} eq 'ARRAY' ? : 2 0 2
5103 2 ▶ if ($tset{$c}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tset{$c}) { } 2 0 2
5104 2 my $fv = $tv->($c);
5105 2 ▶ $out{$c} = [ map { defined $arr->[$_] ? $arr->[$_] : $fv } 0 .. $R - 1 ];
Branch: 1 branch
BranchTrueFalseTotal
defined $arr->[$_] ? : 2 4 6
5106 } else {
5107 0 $out{$c} = [ @$arr ];
5108 }
5109 }
5110 1 return \%out;
5111 }
5112 # AoA
5113 3 my @out;
5114 3 for my $row (@$df) {
5115 7 ▶ unless (ref $row eq 'ARRAY') { push @out, $row; next }
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $row eq "ARRAY") 1 6 7
5116 6 my @r = @$row;
5117 6 for my $c (@targets) {
5118 12 ▶ $r[$c] = $tv->($c) if $c <= $#r && !defined $r[$c];
Branch: 1 branch
BranchTrueFalseTotal
if $c <= $#r and not defined $r[$c] 6 6 12
Condition100%3/3
$c <= $#r and not defined $r[$c]
!ll&&!rl&&r
156
MC/DC100%2/2
$c <= $#r and not defined $r[$c]
$c <= $#r not defined $r[$c]
Truth table100%3/3
A and not(B)
A= $c <= $#rB= not defined $r[$c]
ABresult
0X0
100
111
5119 }
5120 6 push @out, \@r;
5121 }
5122 3 return \@out;
5123 }
5124  
5125 # _fill_seq(\@vals, $dir, $limit) -> \@vals (modifies in place)
5126 #
5127 # Propagate the last (dir=1, forward) or next (dir=-1, backward) defined value
5128 # over runs of undef. With a defined `limit`, at most `limit` consecutive
5129 # undefs are filled per gap; the rest stay undef. Not exported.
5130 17 ▶sub _fill_seq {
Subroutine _fill_seq: called CC=7 SCAR=19.5
5131 17 my ($vals, $dir, $limit) = @_;
5132 17 my $n = scalar @$vals;
5133 17 ▶ my @idx = $dir > 0 ? ( 0 .. $n - 1 ) : reverse( 0 .. $n - 1 );
Branch: 1 branch
BranchTrueFalseTotal
$dir > 0 ? : 12 5 17
5134 17 my ($last, $have, $run) = (undef, 0, 0);
5135 17 for my $i (@idx) {
5136 47 ▶ if (defined $vals->[$i]) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $vals->[$i]) { } 20 27 47
elsif ($have) { } 23 4 27
5137 20 $last = $vals->[$i]; $have = 1; $run = 0;
5138 } elsif ($have) {
5139 23 ▶ next if defined $limit && $run >= $limit;
Branch: 1 branch
BranchTrueFalseTotal
if defined $limit and $run >= $limit 2 21 23
Condition100%3/3
defined $limit and $run >= $limit
!ll&&!rl&&r
1382
MC/DC100%2/2
defined $limit and $run >= $limit
defined $limit $run >= $limit
Truth table100%3/3
A and B
A= defined $limitB= $run >= $limit
ABresult
0X0
100
111
5140 21 $vals->[$i] = $last; $run++;
5141 }
5142 }
5143 17 return $vals;
5144 }
5145  
5146 # _impute_prop($df, $name, $dir, %opts) -- shared core of ffill/bfill.
5147 #
5148 # Propagate defined values along the row axis within each targeted column.
5149 # Row order is positional for AoA/AoH/HoA and string-sorted key order for HoH
5150 # (the only deterministic order a HoH has). Options: cols => \@cols (default
5151 # every column; an unknown column dies), limit => positive int (max fills per
5152 # gap). Fills within each column's existing length only (ragged HoA columns
5153 # are not extended); AoA rows are not extended past their own length. Returns
5154 # a NEW frame; the original is never modified. Not exported.
5155 29 ▶sub _impute_prop {
Subroutine _impute_prop: called CC=31 SCAR=34.4
5156 29 my $df = shift;
5157 29 my $name = shift;
5158 29 my $dir = shift;
5159 29 ▶ _usage_error($name, 'undefined data in first position') unless defined $df;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $df 3 26 29
5160 26 my $shape = _df_shape($df, $name);
5161 22 ▶ _usage_error($name, 'arguments after the data frame must be name => value pairs')
Branch: 1 branch
BranchTrueFalseTotal
if @_ % 2 1 21 22
5162 if @_ % 2;
5163 21 my %arg = @_;
5164 21 my %known = ( cols => 1, limit => 1 );
5165 21 my @bad = sort grep { !$known{$_} } keys %arg;
5166 21 ▶ _usage_error($name, "unknown argument(s): @bad") if @bad;
Branch: 1 branch
BranchTrueFalseTotal
if @bad 2 19 21
5167  
5168 19 my $limit = $arg{limit};
5169 19 ▶ _usage_error($name, "'limit' must be a positive integer")
Branch: 1 branch
BranchTrueFalseTotal
if defined $limit and !&looks_like_number($limit) || $limit < 1 || $limit != int $limit 3 16 19
Condition88%8/9
defined $limit and !&looks_like_number($limit) || $limit < 1 || $limit != int $limit
!ll&&!rl&&r
1063
not &looks_like_number($limit) or $limit < 1 or $limit != int $limit
l!l&&r!l&&!r
216
not &looks_like_number($limit) or $limit < 1
l!l&&r!l&&!r
027
MC/DC75%3/4
defined $limit and !&looks_like_number($limit) || $limit < 1 || $limit != int $limit
defined $limit not &looks_like_number($limit) $limit < 1 $limit != int $limit
Truth table80%4/5
A and not(B) or C or D
A= defined $limitB= not &looks_like_number($limit)C= $limit < 1D= $limit != int $limit
ABCDresult
0XXX0
10000
11XX1
101X1
10011
5170 if defined $limit
5171 && ( !looks_like_number($limit) || $limit < 1 || $limit != int $limit );
5172  
5173 16 my @universe = colnames($df);
5174 16 my %uni = map { $_ => 1 } @universe;
5175 16 my @targets;
5176 16 ▶ if (exists $arg{cols}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $arg{'cols'}) { } 8 8 16
5177 8 ▶ _usage_error($name, "'cols' must be an arrayref") unless ref $arg{cols} eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $arg{"cols"} eq "ARRAY" 0 8 8
5178 8 for my $c (@{ $arg{cols} }) {
5179 8 ▶ die "$name: column '$c' not found\n" unless $uni{$c};
Branch: 1 branch
BranchTrueFalseTotal
unless $uni{$c} 1 7 8
5180 }
5181 7 @targets = @{ $arg{cols} };
5182 } else {
5183 8 @targets = @universe;
5184 }
5185 15 my %tset = map { $_ => 1 } @targets;
5186  
5187 15 ▶ if ($shape eq 'AoH') {
Branch: 1 branch
BranchTrueFalseTotal
if ($shape eq "AoH") 9 6 15
5188 9 ▶ my @out = map { ref $_ eq 'HASH' ? { %$_ } : $_ } @$df;
Branch: 1 branch
BranchTrueFalseTotal
ref $_ eq 'HASH' ? : 26 3 29
5189 # Only the rows that are hashes take part, so a row that cannot be
5190 # written neither breaks a gap nor counts against `limit`.
5191 9 my @rows = grep { ref eq 'HASH' } @out;
5192 9 for my $c (@targets) {
5193 10 my @vals = map { $_->{$c} } @rows;
5194 10 _fill_seq(\@vals, $dir, $limit);
5195 10 for my $i (0 .. $#rows) {
5196 28 ▶ $rows[$i]{$c} = $vals[$i] if defined $vals[$i];
Branch: 1 branch
BranchTrueFalseTotal
if defined $vals[$i] 25 3 28
5197 }
5198 }
5199 9 return \@out;
5200 }
5201 6 ▶ if ($shape eq 'HoH') {
Branch: 1 branch
BranchTrueFalseTotal
if ($shape eq "HoH") 3 3 6
5202 3 my @keys = sort keys %$df;
5203 my %out = map {
5204 3 ▶ $_ => ( ref $df->{$_} eq 'HASH' ? { %{ $df->{$_} } } : $df->{$_} )
Branch: 1 branch
BranchTrueFalseTotal
ref $df->{$_} eq 'HASH' ? : 8 1 9
5205 } keys %$df;
5206 3 my @rows = grep { ref eq 'HASH' } map { $out{$_} } @keys; # as for AoH
5207 3 for my $c (@targets) {
5208 3 my @vals = map { $_->{$c} } @rows;
5209 3 _fill_seq(\@vals, $dir, $limit);
5210 3 for my $j (0 .. $#rows) {
5211 8 ▶ $rows[$j]{$c} = $vals[$j] if defined $vals[$j];
Branch: 1 branch
BranchTrueFalseTotal
if defined $vals[$j] 6 2 8
5212 }
5213 }
5214 3 return \%out;
5215 }
5216 3 ▶ if ($shape eq 'HoA') {
Branch: 1 branch
BranchTrueFalseTotal
if ($shape eq "HoA") 1 2 3
5217 1 my %out;
5218 1 for my $c (keys %$df) {
5219 1 ▶ my $arr = ref $df->{$c} eq 'ARRAY' ? [ @{ $df->{$c} } ] : [];
Branch: 1 branch
BranchTrueFalseTotal
ref $df->{$c} eq 'ARRAY' ? : 1 0 1
5220 1 ▶ _fill_seq($arr, $dir, $limit) if $tset{$c};
Branch: 1 branch
BranchTrueFalseTotal
if $tset{$c} 1 0 1
5221 1 $out{$c} = $arr;
5222 }
5223 1 return \%out;
5224 }
5225 # AoA
5226 2 ▶ my @out = map { ref $_ eq 'ARRAY' ? [ @$_ ] : $_ } @$df;
Branch: 1 branch
BranchTrueFalseTotal
ref $_ eq 'ARRAY' ? : 5 0 5
5227 2 for my $c (@targets) {
5228 # Only rows long enough to hold column $c take part (rows are not
5229 # extended), so a short row neither breaks a gap nor counts against `limit`.
5230 3 ▶ my @rows = grep { ref eq 'ARRAY' && $c <= $#$_ } @out;
Condition66%2/3
ref $_ eq 'ARRAY' && $c <= $_->$#*
!ll&&!rl&&r
017
MC/DC50%1/2
ref $_ eq 'ARRAY' && $c <= $_->$#*
ref $_ eq 'ARRAY' $c <= $_->$#*
Truth table66%2/3
A && B
A= ref $_ eq 'ARRAY'B= $c <= $_->$#*
ABresult
0X0
100
111
5231 3 my @vals = map { $_->[$c] } @rows;
5232 3 _fill_seq(\@vals, $dir, $limit);
5233 3 for my $i (0 .. $#rows) {
5234 7 ▶ $rows[$i][$c] = $vals[$i] if defined $vals[$i];
Branch: 1 branch
BranchTrueFalseTotal
if defined $vals[$i] 6 1 7
5235 }
5236 }
5237 2 return \@out;
5238 }
5239  
5240 # ffill($df, cols => \@cols, limit => $n) -- forward-fill NA (last valid obs).
5241 # bfill($df, cols => \@cols, limit => $n) -- back-fill NA (next valid obs).
5242 # See _impute_prop for the row-axis and shape semantics.
5243 20 ▶sub ffill { _impute_prop( shift, 'ffill', 1, @_ ) }
Subroutine ffill: called CC=1 SCAR=0.0
Pod documented
5244 9 ▶sub bfill { _impute_prop( shift, 'bfill', -1, @_ ) }
Subroutine bfill: called CC=1 SCAR=0.0
Pod documented
5245  
5246 # The interpolate() numeric kernels now live in XS (see ip_fill_column and the
5247 # _interp_column_xs XSUB in LikeR.xs); it is called once per target column below.
5248  
5249 # interpolate($df, method => 'linear', cols => \@cols, x => ...,
5250 # order => $k, limit => $n,
5251 # limit_direction => 'forward'|'backward'|'both',
5252 # limit_area => 'inside'|'outside')
5253 #
5254 # Fill NA (undef) cells along the row axis, like pandas DataFrame.interpolate.
5255 # It is the numeric sibling of ffill/bfill. Row order and the four shapes match
5256 # ffill/bfill (positional for AoA/AoH/HoA, sorted-key order for HoH), and it
5257 # returns a NEW frame; the original is never modified.
5258 #
5259 # method the interpolant. All of pandas' methods are supported:
5260 # linear (default), index, values, time -- straight line; the
5261 # first three use `x`, 'linear' uses equal spacing.
5262 # slinear, nearest, zero -- piecewise, interior only.
5263 # pad/ffill, bfill/backfill -- hold the last/next value.
5264 # quadratic, cubic -- interp1d B-splines.
5265 # cubicspline, pchip, akima, spline -- SciPy-named splines.
5266 # polynomial, spline (need `order`) -- degree-k spline.
5267 # barycentric, krogh -- global polynomial.
5268 # order required for method 'polynomial' / 'spline' (degree 1, 2 or 3).
5269 # x abscissae: an arrayref (one per row) or a column name/index whose
5270 # numeric values are the coordinates (default: equal spacing 0,1,2..).
5271 # Must be strictly increasing. Used by every method except 'linear'.
5272 # limit_direction 'forward' (default), 'backward', or 'both'.
5273 # limit_area 'inside' fills only interior gaps, 'outside' only leading/
5274 # trailing gaps, undef (default) fills both.
5275 # limit max cells filled per undef run.
5276 # cols columns to interpolate; default every column.
5277 #
5278 # The pipeline follows pandas exactly: fill every gap with the method, then
5279 # blank the cells limit/direction/area forbid. Only 'linear' and the hold/
5280 # global methods reach leading/trailing gaps (the interp1d and akima methods
5281 # are interior-only, matching SciPy). Only numeric cells anchor a fill; a
5282 # defined non-numeric cell is preserved (and, for the piecewise-local methods,
5283 # blocks interpolation across it). Interpolated cells are floats. Fills within
5284 # each column's existing length only; a non-ref row is passed through untouched.
5285 5232 ▶sub interpolate {
Subroutine interpolate: called CC=65 SCAR=43.0
Pod documented
5286 5232 my $df = shift;
5287 5232 ▶ _usage_error('interpolate', 'undefined data in first position') unless defined $df;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $df 2 5230 5232
5288 5230 my $shape = _df_shape($df, 'interpolate');
5289 5228 ▶ _usage_error('interpolate', 'arguments after the data frame must be name => value pairs')
Branch: 1 branch
BranchTrueFalseTotal
if @_ % 2 1 5227 5228
5290 if @_ % 2;
5291 5227 my %arg = @_;
5292 5227 my %known = ( cols => 1, limit => 1, limit_direction => 1,
5293 limit_area => 1, method => 1, order => 1, x => 1 );
5294 5227 my @bad = sort grep { !$known{$_} } keys %arg;
5295 5227 ▶ _usage_error('interpolate', "unknown argument(s): @bad") if @bad;
Branch: 1 branch
BranchTrueFalseTotal
if @bad 1 5226 5227
5296  
5297 5226 my %known_method = map { $_ => 1 } qw(
5298 linear index values time slinear nearest zero pad ffill bfill backfill
5299 quadratic cubic cubicspline pchip akima barycentric krogh polynomial spline );
5300 5226 ▶ my $method = defined $arg{method} ? lc $arg{method} : 'linear';
Branch: 1 branch
BranchTrueFalseTotal
defined $arg{'method'} ? : 5194 32 5226
5301 5226 ▶ _usage_error('interpolate', "unknown method '$method'") unless $known_method{$method};
Branch: 1 branch
BranchTrueFalseTotal
unless $known_method{$method} 2 5224 5226
5302  
5303 5224 my $order = $arg{order};
5304 5224 ▶ if ($method eq 'polynomial' || $method eq 'spline') {
Branch: 1 branch
BranchTrueFalseTotal
if ($method eq "polynomial" or $method eq "spline") 81 5143 5224
Condition100%3/3
$method eq "polynomial" or $method eq "spline"
l!l&&r!l&&!r
7745143
MC/DC100%2/2
$method eq "polynomial" or $method eq "spline"
$method eq "polynomial" $method eq "spline"
Truth table100%3/3
A or B
A= $method eq "polynomial"B= $method eq "spline"
ABresult
1X1
011
000
5305 81 ▶ _usage_error('interpolate', "method '$method' requires an integer 'order' >= 1")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $order and &looks_like_number($order) and $order >= 1 and $order == int $order 2 79 81
Condition66%6/9
defined $order and &looks_like_number($order) and $order >= 1 and $order == int $order
!ll&&!rl&&r
2079
defined $order and &looks_like_number($order) and $order >= 1
!ll&&!rl&&r
2079
defined $order and &looks_like_number($order)
!ll&&!rl&&r
2079
MC/DC25%1/4
defined $order and &looks_like_number($order) and $order >= 1 and $order == int $order
defined $order &looks_like_number($order) $order >= 1 $order == int $order
Truth table40%2/5
A and B and C and D
A= defined $orderB= &looks_like_number($order)C= $order >= 1D= $order == int $order
ABCDresult
0XXX0
10XX0
110X0
11100
11111
5306 unless defined $order && looks_like_number($order)
5307 && $order >= 1 && $order == int $order;
5308 }
5309  
5310 5222 my $limit = $arg{limit};
5311 5222 ▶ _usage_error('interpolate', "'limit' must be a positive integer")
Branch: 1 branch
BranchTrueFalseTotal
if defined $limit and !&looks_like_number($limit) || $limit < 1 || $limit != int $limit 2 5220 5222
Condition88%8/9
defined $limit and !&looks_like_number($limit) || $limit < 1 || $limit != int $limit
!ll&&!rl&&r
218730332
not &looks_like_number($limit) or $limit < 1 or $limit != int $limit
l!l&&r!l&&!r
113033
not &looks_like_number($limit) or $limit < 1
l!l&&r!l&&!r
013034
MC/DC75%3/4
defined $limit and !&looks_like_number($limit) || $limit < 1 || $limit != int $limit
defined $limit not &looks_like_number($limit) $limit < 1 $limit != int $limit
Truth table80%4/5
A and not(B) or C or D
A= defined $limitB= not &looks_like_number($limit)C= $limit < 1D= $limit != int $limit
ABCDresult
0XXX0
10000
11XX1
101X1
10011
5312 if defined $limit
5313 && ( !looks_like_number($limit) || $limit < 1 || $limit != int $limit );
5314  
5315 5220 ▶ my $dir = defined $arg{limit_direction} ? lc $arg{limit_direction} : 'forward';
Branch: 1 branch
BranchTrueFalseTotal
defined $arg{'limit_direction'} ? : 4217 1003 5220
5316 5220 my %okdir = ( forward => 1, backward => 1, both => 1 );
5317 _usage_error('interpolate', "'limit_direction' must be 'forward', 'backward', or 'both'")
5318 5220 ▶ unless $okdir{$dir};
Branch: 1 branch
BranchTrueFalseTotal
unless $okdir{$dir} 1 5219 5220
5319 # the directional-hold methods pin their own direction
5320 5219 ▶ $dir = 'forward' if $method eq 'pad' || $method eq 'ffill';
Branch: 1 branch
BranchTrueFalseTotal
if $method eq "pad" or $method eq "ffill" 325 4894 5219
Condition100%3/3
$method eq "pad" or $method eq "ffill"
l!l&&r!l&&!r
32414894
MC/DC100%2/2
$method eq "pad" or $method eq "ffill"
$method eq "pad" $method eq "ffill"
Truth table100%3/3
A or B
A= $method eq "pad"B= $method eq "ffill"
ABresult
1X1
011
000
5321 5219 ▶ $dir = 'backward' if $method eq 'bfill' || $method eq 'backfill';
Branch: 1 branch
BranchTrueFalseTotal
if $method eq "bfill" or $method eq "backfill" 325 4894 5219
Condition66%2/3
$method eq "bfill" or $method eq "backfill"
l!l&&r!l&&!r
32504894
MC/DC50%1/2
$method eq "bfill" or $method eq "backfill"
$method eq "bfill" $method eq "backfill"
Truth table66%2/3
A or B
A= $method eq "bfill"B= $method eq "backfill"
ABresult
1X1
011
000
5322  
5323 5219 my $area;
5324 5219 ▶ if (defined $arg{limit_area}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $arg{"limit_area"}) 3317 1902 5219
5325 3317 $area = lc $arg{limit_area};
5326 3317 ▶ _usage_error('interpolate', "'limit_area' must be 'inside' or 'outside'")
Branch: 1 branch
BranchTrueFalseTotal
unless $area eq "inside" or $area eq "outside" 1 3316 3317
Condition100%3/3
$area eq "inside" or $area eq "outside"
l!l&&r!l&&!r
180215141
MC/DC100%2/2
$area eq "inside" or $area eq "outside"
$area eq "inside" $area eq "outside"
Truth table100%3/3
A or B
A= $area eq "inside"B= $area eq "outside"
ABresult
1X1
011
000
5327 unless $area eq 'inside' || $area eq 'outside';
5328 }
5329  
5330 5218 my @universe = colnames($df);
5331 5218 my %uni = map { $_ => 1 } @universe;
5332 5218 my @targets;
5333 5218 ▶ if (exists $arg{cols}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $arg{'cols'}) { } 7 5211 5218
5334 7 ▶ _usage_error('interpolate', "'cols' must be an arrayref") unless ref $arg{cols} eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $arg{"cols"} eq "ARRAY" 1 6 7
5335 6 for my $c (@{ $arg{cols} }) {
5336 7 ▶ die "interpolate: column '$c' not found\n" unless $uni{$c};
Branch: 1 branch
BranchTrueFalseTotal
unless $uni{$c} 1 6 7
5337 }
5338 5 @targets = @{ $arg{cols} };
5339 } else {
5340 5211 @targets = @universe;
5341 }
5342 5216 my %tset = map { $_ => 1 } @targets;
5343  
5344 # x coordinate: arrayref, or a column key whose values are the coordinates
5345 5216 my ($x_is_col, $x_col);
5346 5216 ▶ if (defined $arg{x} && ref $arg{x} ne 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $arg{"x"} and ref $arg{"x"} ne "ARRAY") 2 5214 5216
Condition100%3/3
defined $arg{"x"} and ref $arg{"x"} ne "ARRAY"
!ll&&!rl&&r
520862
MC/DC100%2/2
defined $arg{"x"} and ref $arg{"x"} ne "ARRAY"
defined $arg{"x"} ref $arg{"x"} ne "ARRAY"
Truth table100%3/3
A and B
A= defined $arg{"x"}B= ref $arg{"x"} ne "ARRAY"
ABresult
0X0
100
111
5347 2 $x_is_col = 1;
5348 2 $x_col = $arg{x};
5349 2 ▶ die "interpolate: x column '$x_col' not found\n" unless $uni{$x_col};
Branch: 1 branch
BranchTrueFalseTotal
unless $uni{$x_col} 0 2 2
5350 }
5351 # validated coordinates of length $len; $xcolseq is the x column pulled in
5352 # the same row order (only consulted when x is a column key).
5353 5216 ▶ my $mkcoords = sub {
Subroutine __ANON__: called CC=12 SCAR=25.1
5354 5216 my ($len, $xcolseq) = @_;
5355 5216 my @x;
5356 5216 ▶ if ($x_is_col) {
Branch: 2 branches
BranchTrueFalseTotal
if ($x_is_col) { } 2 5214 5216
elsif (ref $arg{'x'} eq 'ARRAY') { } 6 5208 5214
5357 0 ▶ die "interpolate: x column '$x_col' length (${\ scalar @$xcolseq}) != column length ($len)\n"
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @$xcolseq == $len 0 2 2
5358 unless scalar @$xcolseq == $len;
5359 2 @x = @$xcolseq;
5360 }
5361 elsif (ref $arg{x} eq 'ARRAY') {
5362 1 die "interpolate: 'x' arrayref length (${\ scalar @{$arg{x}}}) != column length ($len)\n"
5363 6 ▶ unless scalar @{ $arg{x} } == $len;
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @{$arg{"x"};} == $len 1 5 6
5364 5 @x = @{ $arg{x} };
5365 5208 } else { @x = (0 .. $len - 1); }
5366 5215 for my $v (@x) {
5367 882480 ▶ die "interpolate: 'x' coordinates must all be defined and numeric\n"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v and &looks_like_number($v) 0 882480 882480
Condition33%1/3
defined $v and &looks_like_number($v)
!ll&&!rl&&r
00882480
MC/DC0%0/2
defined $v and &looks_like_number($v)
defined $v &looks_like_number($v)
Truth table33%1/3
A and B
A= defined $vB= &looks_like_number($v)
ABresult
0X0
100
111
5368 unless defined $v && looks_like_number($v);
5369 # pandas raises NotImplementedError for a NaN in the index ("Interpolation
5370 # with NaNs in the index has not been implemented"); a NaN here would
5371 # slip past the increasing test below, since every comparison with it is false
5372 882480 ▶ die "interpolate: 'x' coordinates must not be NaN\n" if $v != $v;
Branch: 1 branch
BranchTrueFalseTotal
if $v != $v 4 882476 882480
5373 }
5374 5211 ▶ if (defined $arg{x}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $arg{"x"}) 3 5208 5211
5375 3 for my $i (1 .. $#x) {
5376 7 ▶ die "interpolate: 'x' coordinates must be strictly increasing\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $x[$i] > $x[$i - 1] 1 6 7
5377 unless $x[$i] > $x[$i - 1];
5378 }
5379 }
5380 5210 return \@x;
5381 5216 };
5382 # shape dispatch mirrors _impute_prop (ffill/bfill). The per-column numeric
5383 # fill (all methods, the dense solve, the preserve mask) is done in XS by
5384 # _interp_column_xs, which modifies the extracted @vals in place.
5385 # shape dispatch mirrors _impute_prop (ffill/bfill)
5386 5216 ▶ if ($shape eq 'AoH') {
Branch: 1 branch
BranchTrueFalseTotal
if ($shape eq "AoH") 5 5211 5216
5387 5 ▶ my @out = map { ref $_ eq 'HASH' ? { %$_ } : $_ } @$df;
Branch: 1 branch
BranchTrueFalseTotal
ref $_ eq 'HASH' ? : 14 1 15
5388 0 ▶ my $xcolseq = $x_is_col ? [ map { ref $_ eq 'HASH' ? $_->{$x_col} : undef } @out ] : undef;
Branch: 2 branches
BranchTrueFalseTotal
$x_is_col ? : 0 5 5
ref $_ eq 'HASH' ? : 0 0 0
5389 5 my $coords = $mkcoords->(scalar @out, $xcolseq);
5390 5 for my $c (@targets) {
5391 5 ▶ my @vals = map { ref $_ eq 'HASH' ? $_->{$c} : undef } @out;
Branch: 1 branch
BranchTrueFalseTotal
ref $_ eq 'HASH' ? : 14 1 15
5392 5 _interp_column_xs(\@vals, $coords, $method, $order, $dir, $limit, $area);
5393 5 for my $i (0 .. $#out) {
5394 15 ▶ next unless ref $out[$i] eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $out[$i] eq "HASH" 1 14 15
5395 14 ▶ $out[$i]{$c} = $vals[$i] if defined $vals[$i];
Branch: 1 branch
BranchTrueFalseTotal
if defined $vals[$i] 14 0 14
5396 }
5397 }
5398 5 return \@out;
5399 }
5400 5211 ▶ if ($shape eq 'HoH') {
Branch: 1 branch
BranchTrueFalseTotal
if ($shape eq "HoH") 1 5210 5211
5401 1 my @keys = sort keys %$df;
5402 my %out = map {
5403 1 ▶ $_ => ( ref $df->{$_} eq 'HASH' ? { %{ $df->{$_} } } : $df->{$_} )
Branch: 1 branch
BranchTrueFalseTotal
ref $df->{$_} eq 'HASH' ? : 3 0 3
5404 } keys %$df;
5405 0 ▶ my $xcolseq = $x_is_col ? [ map { ref $out{$_} eq 'HASH' ? $out{$_}{$x_col} : undef } @keys ] : undef;
Branch: 2 branches
BranchTrueFalseTotal
$x_is_col ? : 0 1 1
ref $out{$_} eq 'HASH' ? : 0 0 0
5406 1 my $coords = $mkcoords->(scalar @keys, $xcolseq);
5407 1 for my $c (@targets) {
5408 1 ▶ my @vals = map { ref $out{$_} eq 'HASH' ? $out{$_}{$c} : undef } @keys;
Branch: 1 branch
BranchTrueFalseTotal
ref $out{$_} eq 'HASH' ? : 3 0 3
5409 1 _interp_column_xs(\@vals, $coords, $method, $order, $dir, $limit, $area);
5410 1 for my $j (0 .. $#keys) {
5411 3 my $rk = $keys[$j];
5412 3 ▶ next unless ref $out{$rk} eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $out{$rk} eq "HASH" 0 3 3
5413 3 ▶ $out{$rk}{$c} = $vals[$j] if defined $vals[$j];
Branch: 1 branch
BranchTrueFalseTotal
if defined $vals[$j] 3 0 3
5414 }
5415 }
5416 1 return \%out;
5417 }
5418 5210 ▶ if ($shape eq 'HoA') {
Branch: 1 branch
BranchTrueFalseTotal
if ($shape eq "HoA") 5206 4 5210
5419 5206 my %out;
5420 5206 for my $c (keys %$df) {
5421 5208 ▶ $out{$c} = ref $df->{$c} eq 'ARRAY' ? [ @{ $df->{$c} } ] : [];
Branch: 1 branch
BranchTrueFalseTotal
ref $df->{$c} eq 'ARRAY' ? : 5208 0 5208
5422 }
5423 5206 for my $c (@targets) {
5424 5206 my $arr = $out{$c};
5425 my $xcolseq = $x_is_col
5426 5206 ▶ ? [ @{ ref $df->{$x_col} eq 'ARRAY' ? $df->{$x_col} : [] } ]
Branch: 2 branches
BranchTrueFalseTotal
$x_is_col ? : 2 5204 5206
ref $df->{$x_col} eq 'ARRAY' ? : 2 0 2
5427 : undef;
5428 5206 my $coords = $mkcoords->(scalar @$arr, $xcolseq);
5429 5200 _interp_column_xs($arr, $coords, $method, $order, $dir, $limit, $area);
5430 }
5431 5198 return \%out;
5432 }
5433 # AoA
5434 4 ▶ my @out = map { ref $_ eq 'ARRAY' ? [ @$_ ] : $_ } @$df;
Branch: 1 branch
BranchTrueFalseTotal
ref $_ eq 'ARRAY' ? : 12 0 12
5435 0 ▶ my $xcolseq = $x_is_col ? [ map { ref $_ eq 'ARRAY' ? $_->[$x_col] : undef } @out ] : undef;
Branch: 2 branches
BranchTrueFalseTotal
$x_is_col ? : 0 4 4
ref $_ eq 'ARRAY' ? : 0 0 0
5436 4 my $coords = $mkcoords->(scalar @out, $xcolseq);
5437 4 for my $c (@targets) {
5438 5 ▶ my @vals = map { ref $_ eq 'ARRAY' ? $_->[$c] : undef } @out;
Branch: 1 branch
BranchTrueFalseTotal
ref $_ eq 'ARRAY' ? : 15 0 15
5439 5 _interp_column_xs(\@vals, $coords, $method, $order, $dir, $limit, $area);
5440 5 for my $i (0 .. $#out) {
5441 15 ▶ next unless ref $out[$i] eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $out[$i] eq "ARRAY" 0 15 15
5442 15 ▶ $out[$i][$c] = $vals[$i] if $c <= $#{ $out[$i] } && defined $vals[$i];
Branch: 1 branch
BranchTrueFalseTotal
if $c <= $#{$out[$i];} and defined $vals[$i] 14 1 15
Condition66%2/3
$c <= $#{$out[$i];} and defined $vals[$i]
!ll&&!rl&&r
1014
MC/DC50%1/2
$c <= $#{$out[$i];} and defined $vals[$i]
$c <= $#{$out[$i];} defined $vals[$i]
Truth table66%2/3
A and B
A= $c <= $#{$out[$i];}B= defined $vals[$i]
ABresult
0X0
100
111
5443 }
5444 }
5445 4 return \@out;
5446 }
5447  
5448 # _tukey_col($data, $col) -- pull one column's cells, in row order, from any
5449 # of the three data-frame shapes (AoH arrayref, HoA/HoH hashref).
5450 56 ▶sub _tukey_col {
Subroutine _tukey_col: called CC=6 SCAR=18.2
5451 56 my ($data, $col) = @_;
5452 56 ▶ die 'TukeyHSD: data must be a reference (AoH / HoA / HoH)' unless ref $data;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $data 0 56 56
5453 56 my $r = ref $data;
5454 56 ▶ if ($r eq 'ARRAY') { # AoH
Branch: 2 branches
BranchTrueFalseTotal
if ($r eq 'ARRAY') { } 12 44 56
elsif ($r eq 'HASH') { } 44 0 44
5455 12 return [ map { $_->{$col} } @$data ];
5456 } elsif ($r eq 'HASH') {
5457 44 my ($first) = values %$data;
5458 44 ▶ if (ref($first) eq 'ARRAY') { # HoA
Branch: 1 branch
BranchTrueFalseTotal
if (ref $first eq 'ARRAY') { } 32 12 44
5459 die "TukeyHSD: column '$col' not found in data"
5460 32 ▶ unless exists $data->{$col};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $data->{$col} 0 32 32
5461 32 return [ @{ $data->{$col} } ];
5462 } else { # HoH (row-name keyed)
5463 12 return [ map { $data->{$_}{$col} } sort keys %$data ];
5464 }
5465 }
5466 0 die 'TukeyHSD: unsupported data shape';
5467 }
5468  
5469 # table_one: a stratified descriptive "Table 1"
5470 # Classify a column's non-missing values: 'continuous' if every one looks
5471 # numeric, else 'categorical'.
5472 21 ▶sub _t1_classify {
Subroutine _t1_classify: called CC=5 SCAR=16.1
5473 21 my ($vals) = @_;
5474 21 my $any = 0; # 0 = no defined value seen yet
5475 21 for (@$vals) {
5476 121 ▶ next unless defined;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_ 6 115 121
5477 115 ▶ return 'categorical' unless looks_like_number($_);
Branch: 1 branch
BranchTrueFalseTotal
unless &looks_like_number($_) 5 110 115
5478 110 $any = 1;
5479 }
5480 16 ▶ return $any ? 'continuous' : 'categorical';
Branch: 1 branch
BranchTrueFalseTotal
$any ? : 16 0 16
5481 }
5482  
5483 # p-value + test label for a continuous variable across >=2 groups.
5484 # @$byg is one arrayref of (numeric, defined) values per group. Returns
5485 # (undef, undef) when the test cannot run, as _t1_cat_p() does: t_test() dies
5486 # on a group of one and on data that are constant, and up to 0.3217 that took
5487 # the whole table down for the sake of one row.
5488 15 ▶sub _t1_cont_p {
Subroutine _t1_cont_p: called CC=9 SCAR=22.0
5489 15 my ($byg, $nonpar) = @_;
5490 15 my @g = grep { @$_ >= 1 } @$byg;
5491 15 ▶ return (undef, undef) if @g < 2;
Branch: 1 branch
BranchTrueFalseTotal
if @g < 2 0 15 15
5492 15 my @r = eval {
5493 15 ▶ if (@g == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@g == 2) 11 4 15
5494 11 ▶ my $r = $nonpar ? wilcox_test($g[0], $g[1]) : t_test($g[0], $g[1]);
Branch: 1 branch
BranchTrueFalseTotal
$nonpar ? : 3 8 11
5495 7 ▶ return ($r->{'p_value'}, $nonpar ? 'wilcoxon' : 't-test');
Branch: 1 branch
BranchTrueFalseTotal
$nonpar ? : 3 4 7
5496 }
5497 # >2 groups: Kruskal-Wallis (nonparametric) or one-way ANOVA
5498 4 my %by; @by{ map { "g$_" } 0 .. $#g } = @g;
5499 4 ▶ return (kruskal_test(\%by)->{'p_value'}, 'kruskal-wallis') if $nonpar;
Branch: 1 branch
BranchTrueFalseTotal
if $nonpar 1 3 4
5500 # oneway_test(var_equal => 1) is the F test aov() computes for one
5501 # factor -- R's oneway.test(var.equal = TRUE) -- from each group's sum
5502 # and sum of squares, where aov() builds and rotates a design: on 1e5
5503 # rows in three groups, 8 ms against aov()'s 54. It refuses a group of
5504 # one observation, which aov() takes, so that case still goes to aov().
5505 return (oneway_test(\@g, var_equal => 1)->{Group}{'Pr(>F)'}, 'anova')
5506 3 ▶ unless grep { @$_ < 2 } @g;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {@$_ < 2;} @g 1 2 3
5507 2 my (@x, @lab);
5508 2 for my $i (0 .. $#g) { push @x, @{ $g[$i] }; push @lab, ("g$i") x scalar @{ $g[$i] } }
5509 2 my $aov = aov({ value => \@x, grp => \@lab }, 'value ~ grp');
5510 2 return ($aov->{grp}{'Pr(>F)'}, 'anova');
5511 };
5512 15 ▶ return $@ ? (undef, undef) : @r;
Branch: 1 branch
BranchTrueFalseTotal
$@ ? : 4 11 15
5513 }
5514  
5515 # p-value + test label for a categorical variable: chi-squared on the
5516 # level-by-group contingency table. Returns undef if the test cannot run.
5517 5 ▶sub _t1_cat_p {
Subroutine _t1_cat_p: called CC=5 SCAR=17.8
5518 5 my ($table) = @_;
5519 # A variable with a single level gives a 1 x k table, which chisq_test
5520 # (like R) collapses to a goodness-of-fit test on the group sizes -- a
5521 # different question from the one this column asks. There is no
5522 # association to test, so report none.
5523 5 ▶ return (undef, undef) if @$table < 2 || @{ $table->[0] } < 2;
Branch: 1 branch
BranchTrueFalseTotal
if @$table < 2 or @{$table->[0];} < 2 0 5 5
Condition33%1/3
@$table < 2 or @{$table->[0];} < 2
l!l&&r!l&&!r
005
MC/DC0%0/2
@$table < 2 or @{$table->[0];} < 2
@$table < 2 @{$table->[0];} < 2
Truth table33%1/3
A or B
A= @$table < 2B= @{$table->[0];} < 2
ABresult
1X1
011
000
5524 # small expected counts are worth knowing about at the call site, but
5525 # table_one summarises dozens of variables at once and the warning would
5526 # say nothing about which one
5527 5 my $r = eval {
5528 5 ▶ local $SIG{__WARN__} = sub {
Subroutine __ANON__: called CC=2 SCAR=7.6
5529 5 ▶ warn @_ unless $_[0] =~ /Chi-squared approximation may be incorrect/;
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0] =~ /Chi-squared approximation may be incorrect/ 0 5 5
5530 5 };
5531 5 chisq_test($table);
5532 };
5533 5 ▶ return (undef, undef) if $@ || !$r;
Branch: 1 branch
BranchTrueFalseTotal
if $@ or not $r 0 5 5
Condition33%1/3
$@ or not $r
l!l&&r!l&&!r
005
MC/DC0%0/2
$@ or not $r
$@ not $r
Truth table33%1/3
A or not(B)
A= $@B= not $r
ABresult
1X1
011
000
5534 5 return ($r->{'p_value'}, 'chi-squared');
5535 }
5536  
5537 29 ▶sub table_one {
Subroutine table_one: called CC=55 SCAR=40.3
Pod documented
5538 29 my ($df, %opt) = @_;
5539 29 my %known = map { $_ => 1 } qw(by vars types nonparametric digits pct_digits);
5540 29 my @bad = sort grep { !$known{$_} } keys %opt;
5541 29 ▶ _usage_error('table_one', "unknown argument(s): @bad") if @bad;
Branch: 1 branch
BranchTrueFalseTotal
if @bad 2 27 29
5542  
5543 27 my $by = $opt{by};
5544 27 ▶ my $digits = defined $opt{digits} ? $opt{digits} : 2;
Branch: 1 branch
BranchTrueFalseTotal
defined $opt{'digits'} ? : 1 26 27
5545 27 ▶ my $pdig = defined $opt{pct_digits} ? $opt{pct_digits} : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $opt{'pct_digits'} ? : 0 27 27
5546 27 ▶ my $nonpar = $opt{nonparametric} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$opt{'nonparametric'} ? : 2 25 27
5547 27 ▶ my %types = $opt{types} ? %{ $opt{types} } : ();
Branch: 1 branch
BranchTrueFalseTotal
$opt{'types'} ? : 5 22 27
5548 # anything but 'continuous' took the categorical branch, so a misspelling
5549 # or types => { age => 'numeric' } made a row of every distinct age
5550 27 for my $v (sort keys %types) {
5551 5 my $t = $types{$v};
5552 5 ▶ _usage_error('table_one', "the type of '$v' must be 'continuous' or 'categorical', not '"
Branch: 2 branches
BranchTrueFalseTotal
unless defined $t and $t eq "continuous" || $t eq "categorical" 2 3 5
defined $t ? : 1 1 2
Condition83%5/6
defined $t and $t eq "continuous" || $t eq "categorical"
!ll&&!rl&&r
113
$t eq "continuous" or $t eq "categorical"
l!l&&r!l&&!r
031
MC/DC66%2/3
defined $t and $t eq "continuous" || $t eq "categorical"
defined $t $t eq "continuous" $t eq "categorical"
Truth table75%3/4
A and B or C
A= defined $tB= $t eq "continuous"C= $t eq "categorical"
ABCresult
0XX0
1000
11X1
1011
5553 . (defined $t ? $t : 'undef') . "'")
5554 unless defined $t && ($t eq 'continuous' || $t eq 'categorical');
5555 }
5556  
5557 25 my $shape = _df_shape($df, 'table_one');
5558 22 my @allcols = colnames($df);
5559 22 my %colset = map { $_ => 1 } @allcols;
5560 22 ▶ die "table_one: 'by' column '$by' not found\n" if defined $by && !$colset{$by};
Branch: 1 branch
BranchTrueFalseTotal
if defined $by and not $colset{$by} 1 21 22
Condition100%3/3
defined $by and not $colset{$by}
!ll&&!rl&&r
3181
MC/DC100%2/2
defined $by and not $colset{$by}
defined $by not $colset{$by}
Truth table100%3/3
A and not(B)
A= defined $byB= not $colset{$by}
ABresult
0X0
100
111
5561 3 my @vars = $opt{vars} ? @{ $opt{vars} }
5562 21 ▶ : grep { !defined $by || $_ ne $by } @allcols;
Branch: 1 branch
BranchTrueFalseTotal
$opt{'vars'} ? : 3 18 21
Condition100%3/3
!defined($by) || $_ ne $by
l!l&&r!l&&!r
42416
MC/DC100%2/2
!defined($by) || $_ ne $by
!defined($by) $_ ne $by
Truth table100%3/3
not(A) || B
A= !defined($by)B= $_ ne $by
ABresult
1X1
011
000
5563 21 ▶ for my $v (@vars) { die "table_one: column '$v' not found\n" unless $colset{$v} }
Branch: 1 branch
BranchTrueFalseTotal
unless $colset{$v} 1 30 31
5564  
5565 # Each column is fetched as it is summarised rather than all at once: an
5566 # AoA, AoH or HoH frame's columns are copies, and holding every one of them
5567 # for the whole call was most of the memory this used.
5568 20 ▶ my ($bc, $R, $rows) = _frame_cols($df, $shape, defined $by ? [$by] : []);
Branch: 1 branch
BranchTrueFalseTotal
defined $by ? : 18 2 20
5569  
5570 # $gidx[$r] is row $r's group, an index into @groups. The labels are read
5571 # over 0 .. $R-1, as every column below is, so that a `by` column shorter
5572 # than the frame reads as 'NA' past its end; mapping over the column itself
5573 # left those rows with no group, and the warning that raised was fatal.
5574 20 my (@groups, @gidx, @gsize);
5575 20 ▶ if (defined $by) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $by) { } 18 2 20
5576 18 my $bycol = $bc->{$by};
5577 18 my %size; # label => rows in that group
5578 18 for my $r (0 .. $R - 1) {
5579 128 my $g = $bycol->[$r];
5580 128 ▶ $size{ defined $g ? $g : 'NA' }++;
Branch: 1 branch
BranchTrueFalseTotal
defined $g ? : 127 1 128
5581 }
5582 18 @groups = sort keys %size;
5583 18 @gsize = @size{@groups};
5584 # Each group is a key of every output row, beside these: a group named
5585 # 'test' was overwritten by the label 't-test', and one named 'Overall'
5586 # overwrote the column summarising every row.
5587 18 my %fixed = map { $_ => 1 } qw(variable level type p_value test Overall);
5588 18 my @clash = grep { $fixed{$_} } @groups;
5589 18 ▶ die "table_one: the '$by' column has a group named '"
Branch: 1 branch
BranchTrueFalseTotal
if @clash 6 12 18
5590 . join("', '", @clash) . "', which is also the name of an output column\n"
5591 if @clash;
5592 12 my %gpos; @gpos{@groups} = 0 .. $#groups;
5593 12 ▶ @gidx = map { my $g = $bycol->[$_]; $gpos{ defined $g ? $g : 'NA' } } 0 .. $R - 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $g ? : 91 1 92
5594 } else {
5595 2 @groups = ('Overall');
5596 2 @gsize = ($R);
5597 2 @gidx = (0) x $R;
5598 }
5599  
5600 14 my @out;
5601 14 for my $v (@vars) {
5602 24 my ($vc) = _frame_cols($df, $shape, [$v], $rows);
5603 24 my $vals = $vc->{$v};
5604 24 ▶ my $type = $types{$v} || _t1_classify($vals);
Condition66%2/3
$types{$v} || _t1_classify($vals)
l!l&&r!l&&!r
3210
MC/DC0%0/2
$types{$v} || _t1_classify($vals)
$types{$v} _t1_classify($vals)
Truth table66%2/3
A || B
A= $types{$v}B= _t1_classify($vals)
ABresult
1X1
011
000
5605  
5606 24 ▶ if ($type eq 'continuous') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'continuous') { } 16 8 24
5607 16 my %row = (variable => $v, level => '', type => 'continuous');
5608 # One pass in row order, so each group's values and the Overall
5609 # ones keep the order they had, and with it mean()'s and sd()'s
5610 # last bit. Each value is made a number once, here: mean(), sd()
5611 # and the test used to read copies of the cell, and each copy of a
5612 # numeric string was parsed again.
5613 16 my @byg = map { [] } @groups;
5614 16 my @allv;
5615 16 for my $r (0 .. $R - 1) {
5616 116 my $x = $vals->[$r];
5617 116 ▶ next unless defined $x && looks_like_number($x);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $x and &looks_like_number($x) 6 110 116
Condition66%2/3
defined $x and &looks_like_number($x)
!ll&&!rl&&r
60110
MC/DC50%1/2
defined $x and &looks_like_number($x)
defined $x &looks_like_number($x)
Truth table66%2/3
A and B
A= defined $xB= &looks_like_number($x)
ABresult
0X0
100
111
5618 110 my $num = 0 + $x;
5619 110 push @{ $byg[ $gidx[$r] ] }, $num;
5620 110 push @allv, $num;
5621 }
5622 16 for my $gi (0 .. $#groups) {
5623 35 my $gv = $byg[$gi];
5624 35 ▶ $row{ $groups[$gi] } = @$gv
Branch: 2 branches
BranchTrueFalseTotal
@$gv ? : 35 0 35
@$gv > 1 ? : 29 6 35
5625 ? sprintf('%.*f (%.*f)', $digits, mean($gv), $digits, @$gv > 1 ? sd($gv) : 0)
5626 : '';
5627 }
5628 $row{Overall} = @allv
5629 16 ▶ ? sprintf('%.*f (%.*f)', $digits, mean(\@allv), $digits, @allv > 1 ? sd(\@allv) : 0)
Branch: 2 branches
BranchTrueFalseTotal
@allv ? : 16 0 16
@allv > 1 ? : 16 0 16
5630 : '';
5631 16 ▶ if (defined $by && @groups >= 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $by and @groups >= 2) 15 1 16
Condition66%2/3
defined $by and @groups >= 2
!ll&&!rl&&r
1015
MC/DC50%1/2
defined $by and @groups >= 2
defined $by @groups >= 2
Truth table66%2/3
A and B
A= defined $byB= @groups >= 2
ABresult
0X0
100
111
5632 15 ($row{'p_value'}, $row{test}) = _t1_cont_p(\@byg, $nonpar);
5633 }
5634 16 push @out, \%row;
5635 }
5636 else {
5637 # One pass over the rows counts every (group, level) pair; the
5638 # Overall count of a level is the sum over the groups, since every
5639 # row is in exactly one. Up to 0.3217 a string was built for every
5640 # row and counted twice, and the levels listed came from the cells
5641 # themselves, so a column shorter than the frame counted its rows
5642 # past the end as NA without listing an NA row; they are listed now.
5643 #
5644 # Before that the counts came from a `grep` over a group's rows for
5645 # each (level, group) pair -- run twice -- plus one more scan of
5646 # every row per level for the Overall column: O(levels x groups x
5647 # rows), which on 20000 rows cost 0.021s at 5 levels against 0.120s
5648 # at 40.
5649 8 my @gcnt = map { {} } @groups; # $gcnt[$gi]{$level} = rows in that group
5650 8 for my $r (0 .. $R - 1) {
5651 71 my $x = $vals->[$r];
5652 71 ▶ $gcnt[ $gidx[$r] ]{ defined $x ? $x : 'NA' }++;
Branch: 1 branch
BranchTrueFalseTotal
defined $x ? : 69 2 71
5653 }
5654 8 my %allcnt;
5655 8 for my $c (@gcnt) { $allcnt{$_} += $c->{$_} for keys %$c }
5656 8 my @levels = keys %allcnt;
5657 # Numeric codes in numeric order -- a string sort put stage 10
5658 # between 1 and 2 -- with NA last. A NaN level has no numeric
5659 # order, and <=> returning undef is fatal here, so it keeps the
5660 # string sort.
5661 8 my @known = grep { $_ ne 'NA' } @levels;
5662 8 ▶ if (@known && !grep { !looks_like_number($_) || $_ != $_ } @known) {
Branch: 1 branch
BranchTrueFalseTotal
if (@known and not grep({not &looks_like_number($_) or $_ != $_;} @known)) { } 2 6 8
Condition66%4/6
@known and not grep({not &looks_like_number($_) or $_ != $_;} @known)
!ll&&!rl&&r
062
!&looks_like_number($_) || $_ != $_
l!l&&r!l&&!r
13015
MC/DC50%1/2
@known and not grep({not &looks_like_number($_) or $_ != $_;} @known)
@known not grep({not &looks_like_number($_) or $_ != $_;} @known)
MC/DC50%1/2
!&looks_like_number($_) || $_ != $_
!&looks_like_number($_) $_ != $_
Truth table66%2/3
A and not(B)
A= @knownB= not grep({not &looks_like_number($_) or $_ != $_;} @known)
ABresult
0X0
100
111
Truth table66%2/3
not(A) || B
A= !&looks_like_number($_)B= $_ != $_
ABresult
1X1
011
000
5663 2 ▶ @levels = ( (sort { $a <=> $b || $a cmp $b } @known),
Branch: 1 branch
BranchTrueFalseTotal
@known < @levels ? : 1 1 2
Condition33%1/3
$a <=> $b || $a cmp $b
l!l&&r!l&&!r
3100
MC/DC0%0/2
$a <=> $b || $a cmp $b
$a <=> $b $a cmp $b
Truth table33%1/3
A || B
A= $a <=> $bB= $a cmp $b
ABresult
1X1
011
000
5664 (@known < @levels ? 'NA' : ()) );
5665 } else {
5666 6 @levels = sort @levels;
5667 }
5668 8 my %hdr = (variable => $v, level => '', type => 'categorical');
5669 8 ▶ if (defined $by && @groups >= 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $by and @groups >= 2) 5 3 8
Condition66%2/3
defined $by and @groups >= 2
!ll&&!rl&&r
305
MC/DC50%1/2
defined $by and @groups >= 2
defined $by @groups >= 2
Truth table66%2/3
A and B
A= defined $byB= @groups >= 2
ABresult
0X0
100
111
5670 5 my @table;
5671 5 for my $lv (@levels) {
5672 23 ▶ push @table, [ map { $gcnt[$_]{$lv} || 0 } 0 .. $#groups ];
Condition100%2/2
$gcnt[$_]{$lv} || 0
l!l
3115
MC/DC100%1/1
$gcnt[$_]{$lv} || 0
$gcnt[$_]{$lv}
Truth table100%2/2
A
A= $gcnt[$_]{$lv}
Aresult
11
00
5673 }
5674 5 ($hdr{'p_value'}, $hdr{test}) = _t1_cat_p(\@table);
5675 }
5676 8 push @out, \%hdr;
5677 8 for my $lv (@levels) {
5678 31 my %row = (variable => $v, level => $lv, type => 'categorical');
5679 31 for my $gi (0 .. $#groups) {
5680 54 ▶ my $cnt = $gcnt[$gi]{$lv} || 0;
Condition100%2/2
$gcnt[$gi]{$lv} || 0
l!l
3915
MC/DC100%1/1
$gcnt[$gi]{$lv} || 0
$gcnt[$gi]{$lv}
Truth table100%2/2
A
A= $gcnt[$gi]{$lv}
Aresult
11
00
5681 54 my $tot = $gsize[$gi];
5682 54 ▶ $row{ $groups[$gi] } = $tot ? sprintf('%d (%.*f%%)', $cnt, $pdig, 100 * $cnt / $tot) : '0';
Branch: 1 branch
BranchTrueFalseTotal
$tot ? : 54 0 54
5683 }
5684 31 ▶ my $cntall = $allcnt{$lv} || 0;
Condition50%1/2
$allcnt{$lv} || 0
l!l
310
MC/DC0%0/1
$allcnt{$lv} || 0
$allcnt{$lv}
Truth table50%1/2
A
A= $allcnt{$lv}
Aresult
11
00
5685 31 ▶ $row{Overall} = $R ? sprintf('%d (%.*f%%)', $cntall, $pdig, 100 * $cntall / $R) : '0';
Branch: 1 branch
BranchTrueFalseTotal
$R ? : 31 0 31
5686 31 push @out, \%row;
5687 }
5688 }
5689 }
5690 14 return \@out;
5691 }
5692  
5693 # Effect sizes (Perl level; compose the XS primitives mean/var/aov). All
5694 # validated numerically against R. Added to @EXPORT_OK (== @EXPORT).
5695  
5696 # _num_pair(\@x, \@y, $who) -> (\@xn, \@yn): defined, numeric values only.
5697 142 ▶sub _num_pair {
Subroutine _num_pair: called CC=7 SCAR=19.5
5698 142 my ($x, $y, $who) = @_;
5699 142 ▶ _usage_error($who, 'first two arguments must be array references')
Branch: 1 branch
BranchTrueFalseTotal
unless ref $x eq "ARRAY" and ref $y eq "ARRAY" 7 135 142
Condition100%3/3
ref $x eq "ARRAY" and ref $y eq "ARRAY"
!ll&&!rl&&r
61135
MC/DC100%2/2
ref $x eq "ARRAY" and ref $y eq "ARRAY"
ref $x eq "ARRAY" ref $y eq "ARRAY"
Truth table100%3/3
A and B
A= ref $x eq "ARRAY"B= ref $y eq "ARRAY"
ABresult
0X0
100
111
5700 unless ref $x eq 'ARRAY' && ref $y eq 'ARRAY';
5701 135 ▶ my @xn = grep { defined && looks_like_number($_) } @$x;
Condition100%3/3
defined $_ && &looks_like_number($_)
!ll&&!rl&&r
58111416325893
MC/DC100%2/2
defined $_ && &looks_like_number($_)
defined $_ &looks_like_number($_)
Truth table100%3/3
A && B
A= defined $_B= &looks_like_number($_)
ABresult
0X0
100
111
5702 135 ▶ my @yn = grep { defined && looks_like_number($_) } @$y;
Condition100%3/3
defined $_ && &looks_like_number($_)
!ll&&!rl&&r
59081416317117
MC/DC100%2/2
defined $_ && &looks_like_number($_)
defined $_ &looks_like_number($_)
Truth table100%3/3
A && B
A= defined $_B= &looks_like_number($_)
ABresult
0X0
100
111
5703 135 ▶ die "$who: each group needs at least two numeric observations\n"
Branch: 1 branch
BranchTrueFalseTotal
if @xn < 2 or @yn < 2 4 131 135
Condition100%3/3
@xn < 2 or @yn < 2
l!l&&r!l&&!r
31131
MC/DC100%2/2
@xn < 2 or @yn < 2
@xn < 2 @yn < 2
Truth table100%3/3
A or B
A= @xn < 2B= @yn < 2
ABresult
1X1
011
000
5704 if @xn < 2 || @yn < 2;
5705 131 return (\@xn, \@yn);
5706 }
5707  
5708 # cohen_d(\@x, \@y, conf_level => 0.95, ci_method => 'nct')
5709 #
5710 # Cohen's d for two independent samples using the pooled standard deviation,
5711 # with the Hedges' g small-sample bias correction.
5712 #
5713 # The interval is effectsize 1.0.3's cohens_d() interval by default
5714 # (R/cohens_d.R): t = d / sqrt(1/n1 + 1/n2) on n1 + n2 - 2 df, the limits of
5715 # its noncentrality parameter (_nct_ncp_ci(), in the XS), each scaled back by
5716 # sqrt(1/n1 + 1/n2). Up to 0.3217 it was d +- z * se, which effectsize's own
5717 # test of mtcars wt ~ am puts at 1.048 to 2.737 for its 1.030 to 2.732; that
5718 # interval is still there as ci_method => 'normal'. se is Hedges and Olkin's
5719 # large-sample standard error, which effsize 0.8.1's cohen.d() also reports
5720 # (as sqrt of var); the 'nct' interval does not use it.
5721 #
5722 # J is the approximation 1 - 3/(4 df - 1) that effsize 0.8.1 uses, written
5723 # with df = n1 + n2 - 2. effectsize 1.0.3 uses the exact gamma ratio; the two
5724 # differ by 4e-5 relative at df = 30 and by 3e-6 at df = 100.
5725 110 ▶sub cohen_d {
Subroutine cohen_d: called CC=13 SCAR=25.6
Pod documented
5726 110 my ($x, $y, @rest) = @_;
5727 110 ▶ _usage_error('cohen_d', 'arguments after the two groups must be name => value pairs')
Branch: 1 branch
BranchTrueFalseTotal
if @rest % 2 1 109 110
5728 if @rest % 2;
5729 109 _opt_names_defined('cohen_d', @rest);
5730 109 my %opt = @rest;
5731 109 ▶ my @bad = sort grep { $_ ne 'conf_level' && $_ ne 'ci_method' } keys %opt;
Condition100%3/3
$_ ne 'conf_level' && $_ ne 'ci_method'
!ll&&!rl&&r
98471
MC/DC100%2/2
$_ ne 'conf_level' && $_ ne 'ci_method'
$_ ne 'conf_level' $_ ne 'ci_method'
Truth table100%3/3
A && B
A= $_ ne 'conf_level'B= $_ ne 'ci_method'
ABresult
0X0
100
111
5732 109 ▶ _usage_error('cohen_d', "unknown argument(s): @bad") if @bad;
Branch: 1 branch
BranchTrueFalseTotal
if @bad 1 108 109
5733 108 ▶ my $cl = defined $opt{conf_level} ? $opt{conf_level} : 0.95;
Branch: 1 branch
BranchTrueFalseTotal
defined $opt{'conf_level'} ? : 98 10 108
5734 108 ▶ _usage_error('cohen_d', 'conf_level must be between 0 and 1')
Branch: 1 branch
BranchTrueFalseTotal
unless &looks_like_number($cl) and $cl > 0 and $cl < 1 5 103 108
Condition100%6/6
&looks_like_number($cl) and $cl > 0 and $cl < 1
!ll&&!rl&&r
32103
&looks_like_number($cl) and $cl > 0
!ll&&!rl&&r
12105
MC/DC100%3/3
&looks_like_number($cl) and $cl > 0 and $cl < 1
&looks_like_number($cl) $cl > 0 $cl < 1
Truth table100%4/4
A and B and C
A= &looks_like_number($cl)B= $cl > 0C= $cl < 1
ABCresult
0XX0
10X0
1100
1111
5735 unless looks_like_number($cl) && $cl > 0 && $cl < 1;
5736 103 ▶ my $method = defined $opt{ci_method} ? $opt{ci_method} : 'nct';
Branch: 1 branch
BranchTrueFalseTotal
defined $opt{'ci_method'} ? : 47 56 103
5737 103 ▶ _usage_error('cohen_d', "ci_method must be 'nct' or 'normal'")
Branch: 1 branch
BranchTrueFalseTotal
unless $method eq "nct" or $method eq "normal" 1 102 103
Condition100%3/3
$method eq "nct" or $method eq "normal"
l!l&&r!l&&!r
56461
MC/DC100%2/2
$method eq "nct" or $method eq "normal"
$method eq "nct" $method eq "normal"
Truth table100%3/3
A or B
A= $method eq "nct"B= $method eq "normal"
ABresult
1X1
011
000
5738 unless $method eq 'nct' || $method eq 'normal';
5739 102 my ($xn, $yn) = _num_pair($x, $y, 'cohen_d');
5740 97 my ($n1, $n2) = (scalar @$xn, scalar @$yn);
5741 97 my ($m1, $m2) = (mean($xn), mean($yn));
5742 97 my ($v1, $v2) = (var($xn), var($yn));
5743 97 my $sp = sqrt((($n1 - 1) * $v1 + ($n2 - 1) * $v2) / ($n1 + $n2 - 2));
5744 97 ▶ die "cohen_d: pooled standard deviation is zero\n" if $sp == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $sp == 0 1 96 97
5745 96 my $d = ($m1 - $m2) / $sp;
5746 96 my $J = 1 - 3 / (4 * ($n1 + $n2) - 9); # Hedges' correction factor
5747 96 my $se = sqrt(($n1 + $n2) / ($n1 * $n2) + $d * $d / (2 * ($n1 + $n2)));
5748 96 my @ci;
5749 96 ▶ if ($method eq 'nct') {
Branch: 1 branch
BranchTrueFalseTotal
if ($method eq 'nct') { } 50 46 96
5750 50 my $hn = sqrt(1 / $n1 + 1 / $n2);
5751 50 @ci = map { $_ * $hn } _nct_ncp_ci($d / $hn, $n1 + $n2 - 2, $cl);
5752 } else {
5753 46 my $z = qnorm((1 + $cl) / 2);
5754 46 @ci = ($d - $z * $se, $d + $z * $se);
5755 }
5756 return {
5757 96 estimate => $d,
5758 hedges_g => $d * $J,
5759 pooled_sd => $sp,
5760 se => $se,
5761 'conf_int' => \@ci,
5762 'conf_level' => $cl,
5763 ci_method => $method,
5764 n1 => $n1,
5765 n2 => $n2,
5766 };
5767 }
5768  
5769 # smd(\@x, \@y)
5770 #
5771 # Standardized mean difference for two continuous groups using the simple
5772 # (unweighted) average of the group variances in the denominator -- the
5773 # convention used for covariate-balance "Table 1" diagnostics. It is
5774 # tableone 0.13.2's StdDiff() (R/modules-smd.R) for one pair of groups, signed
5775 # where StdDiff() returns abs().
5776 #
5777 # A zero denominator is answered as StdDiff() answers it. A column that is
5778 # one constant in both groups is 0, which StdDiff() substitutes for 0/0 and
5779 # smd 0.8.0's tests also expect; a column constant within each group but not
5780 # between them is +-Inf, StdDiff()'s mean difference over sqrt(0). Up to
5781 # 0.3217 both died with "pooled standard deviation is zero", which took a
5782 # balance table down over one constant covariate.
5783 40 ▶sub smd {
Subroutine smd: called CC=4 SCAR=13.9
Pod documented
5784 40 my ($x, $y) = @_;
5785 40 my ($xn, $yn) = _num_pair($x, $y, 'smd');
5786 34 my $diff = mean($xn) - mean($yn);
5787 34 my $denom = sqrt((var($xn) + var($yn)) / 2);
5788 34 ▶ if ($denom == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($denom == 0) 10 24 34
5789 10 ▶ return 0 if $diff == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $diff == 0 8 2 10
5790 2 ▶ return $diff > 0 ? 9**9**9 : -9**9**9;
Branch: 1 branch
BranchTrueFalseTotal
$diff > 0 ? : 1 1 2
5791 }
5792 24 return $diff / $denom;
5793 }
5794  
5795 # _xtab(\@a, \@b) -> (\@table, \@rowlevels, \@collevels): contingency table
5796 # from two parallel categorical vectors (rows = levels of a, cols = levels of b).
5797 1 ▶sub _xtab {
Subroutine _xtab: called CC=6 SCAR=18.4
5798 1 my ($a, $b) = @_;
5799 1 ▶ die "cramers_v: the two vectors must have the same length\n"
Branch: 1 branch
BranchTrueFalseTotal
unless @$a == @$b 0 1 1
5800 unless @$a == @$b;
5801 1 my (%rseen, %cseen, %cell);
5802 1 for my $i (0 .. $#$a) {
5803 12 ▶ next unless defined $a->[$i] && defined $b->[$i];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $a->[$i] and defined $b->[$i] 0 12 12
Condition33%1/3
defined $a->[$i] and defined $b->[$i]
!ll&&!rl&&r
0012
MC/DC0%0/2
defined $a->[$i] and defined $b->[$i]
defined $a->[$i] defined $b->[$i]
Truth table33%1/3
A and B
A= defined $a->[$i]B= defined $b->[$i]
ABresult
0X0
100
111
5804 12 my ($r, $c) = ("$a->[$i]", "$b->[$i]");
5805 12 $rseen{$r}++; $cseen{$c}++; $cell{$r}{$c}++;
5806 }
5807 1 my @rl = sort keys %rseen;
5808 1 my @cl = sort keys %cseen;
5809 1 ▶ my @tab = map { my $r = $_; [ map { $cell{$r}{$_} // 0 } @cl ] } @rl;
Condition50%1/2
$cell{$r}{$_} // 0
l!l
60
MC/DC0%0/1
$cell{$r}{$_} // 0
$cell{$r}{$_}
Truth table50%1/2
A
A= $cell{$r}{$_}
Aresult
11
00
5810 1 return (\@tab, \@rl, \@cl);
5811 }
5812  
5813 # cramers_v(\@table) or cramers_v(\@x, \@y)
5814 #
5815 # Cramer's V for an r x c contingency table (uncorrected Pearson chi-square),
5816 # with the Bergsma (2013) bias-corrected variant. Accepts either a table
5817 # (array of array refs of counts) or two parallel categorical vectors.
5818 8 ▶sub cramers_v {
Subroutine cramers_v: called CC=22 SCAR=31.5
Pod documented
5819 8 my @args = @_;
5820 8 my $tab;
5821 8 ▶ if (ref $args[0] eq 'ARRAY' && ref $args[0][0] eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $args[0] eq 'ARRAY' and ref $args[0][0] eq 'ARRAY') { } 3 5 8
elsif (ref $args[0] eq 'ARRAY' and ref $args[1] eq 'ARRAY') { } 1 4 5
Condition100%3/3
ref $args[0] eq "ARRAY" and ref $args[0][0] eq "ARRAY"
!ll&&!rl&&r
413
MC/DC100%2/2
ref $args[0] eq "ARRAY" and ref $args[0][0] eq "ARRAY"
ref $args[0] eq "ARRAY" ref $args[0][0] eq "ARRAY"
Truth table100%3/3
A and B
A= ref $args[0] eq "ARRAY"B= ref $args[0][0] eq "ARRAY"
ABresult
0X0
100
111
5822 3 $tab = $args[0];
5823 ▶ } elsif (ref $args[0] eq 'ARRAY' && ref $args[1] eq 'ARRAY') {
Condition66%2/3
ref $args[0] eq "ARRAY" and ref $args[1] eq "ARRAY"
!ll&&!rl&&r
401
MC/DC50%1/2
ref $args[0] eq "ARRAY" and ref $args[1] eq "ARRAY"
ref $args[0] eq "ARRAY" ref $args[1] eq "ARRAY"
Truth table66%2/3
A and B
A= ref $args[0] eq "ARRAY"B= ref $args[1] eq "ARRAY"
ABresult
0X0
100
111
5824 1 ($tab) = _xtab($args[0], $args[1]);
5825 } else {
5826 4 _usage_error('cramers_v', 'expected a count table or two parallel vectors');
5827 }
5828 4 my $r = scalar @$tab;
5829 4 ▶ die "cramers_v: table needs at least two rows and columns\n" if $r < 2;
Branch: 1 branch
BranchTrueFalseTotal
if $r < 2 1 3 4
5830 3 my $c = scalar @{ $tab->[0] };
5831 3 ▶ die "cramers_v: table needs at least two rows and columns\n" if $c < 2;
Branch: 1 branch
BranchTrueFalseTotal
if $c < 2 0 3 3
5832 3 my (@rsum, @csum, $N);
5833 3 for my $i (0 .. $r - 1) {
5834 7 ▶ die "cramers_v: ragged table\n" unless @{ $tab->[$i] } == $c;
Branch: 1 branch
BranchTrueFalseTotal
unless @{$tab->[$i];} == $c 0 7 7
5835 7 for my $j (0 .. $c - 1) {
5836 16 my $v = $tab->[$i][$j];
5837 16 ▶ die "cramers_v: counts must be non-negative numbers\n"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v and &looks_like_number($v) and $v >= 0 0 16 16
Condition33%2/6
defined $v and &looks_like_number($v) and $v >= 0
!ll&&!rl&&r
0016
defined $v and &looks_like_number($v)
!ll&&!rl&&r
0016
MC/DC0%0/3
defined $v and &looks_like_number($v) and $v >= 0
defined $v &looks_like_number($v) $v >= 0
Truth table25%1/4
A and B and C
A= defined $vB= &looks_like_number($v)C= $v >= 0
ABCresult
0XX0
10X0
1100
1111
5838 unless defined $v && looks_like_number($v) && $v >= 0;
5839 16 $rsum[$i] += $v; $csum[$j] += $v; $N += $v;
5840 }
5841 }
5842 3 ▶ die "cramers_v: table total is zero\n" unless $N;
Branch: 1 branch
BranchTrueFalseTotal
unless $N 0 3 3
5843 # the bias correction divides by N - 1: up to 0.3217 a total of 1 died on
5844 # perl's own "Illegal division by zero", naming a line of this file
5845 3 ▶ die "cramers_v: the table total must be more than 1\n" unless $N > 1;
Branch: 1 branch
BranchTrueFalseTotal
unless $N > 1 1 2 3
5846 2 my $chi = 0;
5847 2 for my $i (0 .. $r - 1) {
5848 5 for my $j (0 .. $c - 1) {
5849 12 my $e = $rsum[$i] * $csum[$j] / $N;
5850 12 ▶ next unless $e > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $e > 0 0 12 12
5851 12 my $diff = $tab->[$i][$j] - $e;
5852 12 $chi += $diff * $diff / $e;
5853 }
5854 }
5855 2 ▶ my $mindim = ($r < $c ? $r : $c) - 1;
Branch: 1 branch
BranchTrueFalseTotal
$r < $c ? : 1 1 2
5856 2 my $v = sqrt($chi / ($N * $mindim));
5857 # Bergsma bias-corrected V
5858 2 my $phi2 = $chi / $N;
5859 2 my $phi2c = $phi2 - ($c - 1) * ($r - 1) / ($N - 1);
5860 2 ▶ $phi2c = 0 if $phi2c < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $phi2c < 0 1 1 2
5861 2 my $rc = $r - ($r - 1) ** 2 / ($N - 1);
5862 2 my $cc = $c - ($c - 1) ** 2 / ($N - 1);
5863 2 ▶ my $mc = ($rc < $cc ? $rc : $cc) - 1;
Branch: 1 branch
BranchTrueFalseTotal
$rc < $cc ? : 1 1 2
5864 2 ▶ my $vc = $mc > 0 ? sqrt($phi2c / $mc) : 0;
Branch: 1 branch
BranchTrueFalseTotal
$mc > 0 ? : 2 0 2
5865 return {
5866 2 estimate => $v,
5867 bias_corrected => $vc,
5868 chisq => $chi,
5869 df => ($r - 1) * ($c - 1),
5870 n => $N,
5871 };
5872 }
5873  
5874 # eta_squared($aov_result) or eta_squared(\@values, \@groups)
5875 #
5876 # Eta-squared, partial eta-squared and omega-squared for a one-way design,
5877 # from the ANOVA sums of squares. Accepts an aov() result hash (single factor)
5878 # or raw values + group labels.
5879 #
5880 # The raw form fits Group as a factor whatever its labels look like, through
5881 # oneway_test() or aov()'s stacked form; see below for which. Up to 0.3217 it
5882 # passed aov() a formula, which fits a column whose values all look like
5883 # numbers as one slope: groups coded 1, 2, 3 were a linear trend with 1 df,
5884 # and the airquality Ozone ~ Month example gave eta_sq 0.0271 for R's 0.2352.
5885 32 ▶sub eta_squared {
Subroutine eta_squared: called CC=17 SCAR=28.3
Pod documented
5886 32 my @args = @_;
5887 32 my $aov_res;
5888 32 ▶ if (ref $args[0] eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $args[0] eq 'HASH') { } 11 21 32
elsif (ref $args[0] eq 'ARRAY' and ref $args[1] eq 'ARRAY') { } 16 5 21
5889 11 $aov_res = $args[0];
5890 ▶ } elsif (ref $args[0] eq 'ARRAY' && ref $args[1] eq 'ARRAY') {
Condition66%2/3
ref $args[0] eq "ARRAY" and ref $args[1] eq "ARRAY"
!ll&&!rl&&r
5016
MC/DC50%1/2
ref $args[0] eq "ARRAY" and ref $args[1] eq "ARRAY"
ref $args[0] eq "ARRAY" ref $args[1] eq "ARRAY"
Truth table66%2/3
A and B
A= ref $args[0] eq "ARRAY"B= ref $args[1] eq "ARRAY"
ABresult
0X0
100
111
5891 16 my ($v, $g) = @args;
5892 16 ▶ die "eta_squared: values and groups must have the same length\n"
Branch: 1 branch
BranchTrueFalseTotal
unless @$v == @$g 1 15 16
5893 unless @$v == @$g;
5894 15 my %by; # group label => its values; aov() drops an undef value itself
5895 15 my $plain = 1; # 0 = some value is undef or not a number
5896 15 for my $i (0 .. $#$v) {
5897 739 ▶ next unless defined $g->[$i];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $g->[$i] 1 738 739
5898 738 my $x = $v->[$i];
5899 738 ▶ $plain = 0 unless defined $x && looks_like_number($x);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $x and &looks_like_number($x) 76 662 738
Condition100%3/3
defined $x and &looks_like_number($x)
!ll&&!rl&&r
751662
MC/DC100%2/2
defined $x and &looks_like_number($x)
defined $x &looks_like_number($x)
Truth table100%3/3
A and B
A= defined $xB= &looks_like_number($x)
ABresult
0X0
100
111
5900 738 push @{ $by{ $g->[$i] } }, $x;
5901 }
5902 # oneway_test(var_equal => 1) gives the table aov()'s stacked form does,
5903 # from each group's sum and sum of squares rather than a design matrix:
5904 # on 1e6 values in 5 groups, 0.10 s and 47 MB against 0.73 s and 326 MB.
5905 # It refuses what aov() takes -- one group, a group of one value, and an
5906 # undef or non-numeric value, which aov() drops -- so those go to aov().
5907 15 my $small = grep { @$_ < 2 } values %by; # groups of fewer than two values
5908 15 ▶ $aov_res = $plain && keys %by >= 2 && !$small
Branch: 1 branch
BranchTrueFalseTotal
$plain && keys %by >= 2 && !$small ? : 9 6 15
Condition100%6/6
$plain and keys %by >= 2 and not $small
!ll&&!rl&&r
519
$plain and keys %by >= 2
!ll&&!rl&&r
4110
MC/DC100%3/3
$plain and keys %by >= 2 and not $small
$plain keys %by >= 2 not $small
Truth table100%4/4
A and B and not(C)
A= $plainB= keys %by >= 2C= not $small
ABCresult
0XX0
10X0
1100
1111
5909 ? oneway_test(\%by, var_equal => 1)
5910 : aov(\%by);
5911 } else {
5912 5 _usage_error('eta_squared', 'expected an aov() result or (\@values, \@groups)');
5913 }
5914 my $resid = $aov_res->{Residuals}
5915 26 ▶ or die "eta_squared: not an ANOVA result (no Residuals term)\n";
Branch: 1 branch
BranchTrueFalseTotal
my $resid = $aov_res->{"Residuals"} or die "eta_squared: not an ANOVA result (no Residuals term)\n" 25 1 26
5916 25 my $ss_resid = $resid->{'Sum Sq'};
5917 25 my $ms_resid = $resid->{'Mean Sq'};
5918 # SS_total below is SS_effect + SS_resid, which is the total only when there
5919 # is one effect term. Up to 0.3217 a two-factor result was answered for
5920 # whichever term sorted first, with partial eta-squared reported as eta_sq.
5921 25 my @terms = grep { $_ ne 'Residuals' && ref $aov_res->{$_} eq 'HASH'
5922 140 ▶ && exists $aov_res->{$_}{'Sum Sq'} } sort keys %$aov_res;
Condition100%6/6
$_ ne 'Residuals' && ref $aov_res->{$_} eq 'HASH' && exists $aov_res->{$_}{'Sum Sq'}
!ll&&!rl&&r
417326
$_ ne "Residuals" and ref $aov_res->{$_} eq "HASH"
!ll&&!rl&&r
251699
MC/DC100%3/3
$_ ne 'Residuals' && ref $aov_res->{$_} eq 'HASH' && exists $aov_res->{$_}{'Sum Sq'}
$_ ne "Residuals" ref $aov_res->{$_} eq "HASH" exists $aov_res->{$_}{'Sum Sq'}
Truth table100%4/4
A and B && C
A= $_ ne "Residuals"B= ref $aov_res->{$_} eq "HASH"C= exists $aov_res->{$_}{'Sum Sq'}
ABCresult
0XX0
10X0
1100
1111
5923 25 ▶ die "eta_squared: could not find an effect term\n" unless @terms;
Branch: 1 branch
BranchTrueFalseTotal
unless @terms 0 25 25
5924 25 ▶ die 'eta_squared: the aov() result has ' . @terms . " effect terms (@terms); "
Branch: 1 branch
BranchTrueFalseTotal
if @terms > 1 1 24 25
5925 . "eta_squared() measures a one-way design, which has one\n" if @terms > 1;
5926 24 my $term = $terms[0];
5927 24 my $ss_eff = $aov_res->{$term}{'Sum Sq'};
5928 24 my $df_eff = $aov_res->{$term}{'Df'};
5929 24 my $ss_tot = $ss_eff + $ss_resid;
5930 return {
5931 24 term => $term,
5932 eta_sq => $ss_eff / $ss_tot,
5933 partial_eta_sq => $ss_eff / ($ss_eff + $ss_resid),
5934 omega_sq => ($ss_eff - $df_eff * $ms_resid) / ($ss_tot + $ms_resid),
5935 };
5936 }
5937  
5938 # Regression diagnostics (Perl level). Validated numerically against R.
5939 #
5940 # _lgamma/_igamc/_pchisq_upper used to be pure-Perl ports of the XS igamc()
5941 # living here. They are now XS (see LikeR.xs), so there is one implementation
5942 # instead of two that could disagree; _lgamma went away entirely because only
5943 # _igamc ever called it. _qnorm went the same way in 0.303: it was Acklam's
5944 # rational approximation, good to about 1e-9, and cohen_d's normal interval
5945 # is now built from the XS qnorm() that every other confidence limit in the
5946 # distribution uses.
5947  
5948 # _quantile7(\@sorted_ascending, $p): R's default (type 7) sample quantile,
5949 # in R 4.6.1 quantile.default()'s own arithmetic -- the 1-based index, and
5950 # (1 - h) * x[lo] + h * x[hi] rather than x[lo] + h * (x[hi] - x[lo]) -- so that
5951 # a break hosmer_lemeshow() compares a prediction against is R's to the bit.
5952 493 ▶sub _quantile7 {
Subroutine _quantile7: called CC=3 SCAR=11.0
5953 493 my ($s, $p) = @_;
5954 493 my $index = 1 + (@$s - 1) * $p;
5955 493 my $lo = int $index; # floor: $index >= 1
5956 493 my $qs = $s->[$lo - 1];
5957 493 ▶ return $qs unless $index > $lo && $s->[$lo] != $qs;
Branch: 1 branch
BranchTrueFalseTotal
unless $index > $lo and $s->[$lo] != $qs 147 346 493
Condition100%3/3
$index > $lo and $s->[$lo] != $qs
!ll&&!rl&&r
4998346
MC/DC100%2/2
$index > $lo and $s->[$lo] != $qs
$index > $lo $s->[$lo] != $qs
Truth table100%3/3
A and B
A= $index > $loB= $s->[$lo] != $qs
ABresult
0X0
100
111
5958 346 my $h = $index - $lo;
5959 346 return (1 - $h) * $qs + $h * $s->[$lo];
5960 }
5961  
5962 # _chol(\@A) -> \@L: the lower Cholesky factor of a symmetric positive definite
5963 # matrix given as an array of rows, or undef when it is not positive definite.
5964 177 ▶sub _chol {
Subroutine _chol: called CC=6 SCAR=17.9
5965 177 my ($A) = @_;
5966 177 my $n = @$A;
5967 177 my @L = map { [ (0) x $n ] } 1 .. $n;
5968 177 for my $j (0 .. $n - 1) {
5969 386 my $s = $A->[$j][$j];
5970 386 $s -= $L[$j][$_] ** 2 for 0 .. $j - 1;
5971 386 ▶ return undef unless $s > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $s > 0 0 386 386
5972 386 my $d = sqrt $s;
5973 386 $L[$j][$j] = $d;
5974 386 for my $i ($j + 1 .. $n - 1) {
5975 273 my $t = $A->[$i][$j];
5976 273 $t -= $L[$i][$_] * $L[$j][$_] for 0 .. $j - 1;
5977 273 $L[$i][$j] = $t / $d;
5978 }
5979 }
5980 177 return \@L;
5981 }
5982  
5983 # _chol_det(\@A): the determinant of a symmetric positive definite matrix.
5984 14 ▶sub _chol_det {
Subroutine _chol_det: called CC=3 SCAR=11.1
5985 14 ▶ my $L = _chol($_[0]) or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my $L = _chol($_[0]) or return 0 14 0 14
5986 14 my $det = 1;
5987 14 $det *= $L->[$_][$_] ** 2 for 0 .. $#$L;
5988 14 return $det;
5989 }
5990  
5991 # vif($data, $formula_or_predictors)
5992 #
5993 # Variance inflation factors, ported from car 3.1-5's vif.default()
5994 # (R/vif.R), which vif.lm() hands its default type = "terms" to:
5995 #
5996 # R <- cov2cor(vcov(mod))[-1, -1] # less the intercept
5997 # GVIF_term = det(R[subs, subs]) * det(R[-subs, -subs]) / det(R)
5998 #
5999 # with subs the columns of one term, its result a plain VIF per term when
6000 # every term has 1 df and a GVIF, Df and GVIF^(1/(2*Df)) table when one has
6001 # more. The design is the one lm() builds from the formula, read by the XS
6002 # _vif_design(); only its columns' correlation matters, so R here is the
6003 # correlation of the design columns rather than of the coefficients. The two
6004 # give the same determinant ratio: by Jacobi's identity, det((C^-1)_SS) =
6005 # det(C_-S-S) / det(C), and the diagonal scaling of cov2cor() cancels. So
6006 #
6007 # GVIF_term = det(C[S, S]) * det(Cinv[S, S]), C = cor(X), Cinv = C^-1
6008 #
6009 # which for a 1-df term is Cinv's diagonal, the classical 1 / (1 - R^2_j).
6010 # Without an intercept car uses the uncentred vcov, and C here is the
6011 # uncentred cross-product matrix scaled to a unit diagonal, by the same
6012 # identity.
6013 #
6014 # Through 0.3217 vif() split the formula's right-hand side on '+' and
6015 # regressed each piece on the others, so only additive numeric terms worked:
6016 # y ~ x1*x2 + x3, y ~ x1 + x2 - x3 and y ~ . all died, the first two with
6017 # lm()'s "0 degrees of freedom" and the last with "need at least two
6018 # predictors"; a factor was never handled; and a response's missing values
6019 # did not take its rows out, where car's model has none of them.
6020 181 ▶sub vif {
Subroutine vif: called CC=23 SCAR=31.4
Pod documented
6021 181 my ($data, $spec) = @_;
6022 181 ▶ _usage_error('vif', 'first argument must be a data reference') unless ref $data;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $data 4 177 181
6023 177 my $formula;
6024 177 ▶ if (ref $spec eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $spec eq 'ARRAY') { } 8 169 177
elsif (defined $spec and not ref $spec and $spec =~ /~/) { } 166 3 169
6025 8 $formula = '~ ' . join(' + ', @$spec);
6026 ▶ } elsif (defined $spec && !ref $spec && $spec =~ /~/) {
Condition100%6/6
defined $spec and not ref $spec and $spec =~ /~/
!ll&&!rl&&r
21166
defined $spec and not ref $spec
!ll&&!rl&&r
11167
MC/DC100%3/3
defined $spec and not ref $spec and $spec =~ /~/
defined $spec not ref $spec $spec =~ /~/
Truth table100%4/4
A and not(B) and C
A= defined $specB= not ref $specC= $spec =~ /~/
ABCresult
0XX0
10X0
1100
1111
6027 166 $formula = $spec;
6028 } else {
6029 3 _usage_error('vif', 'expected a formula string or an array ref of predictors');
6030 }
6031 # the XS croaks from this line, so its messages would name a line of this
6032 # file; vif()'s own messages carry no location, and neither do these
6033 174 my $ds = eval { _vif_design($data, $formula) };
6034 174 ▶ if (!$ds) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ds) 5 169 174
6035 5 (my $e = $@) =~ s/ at \S+ line \d+\.\n\z/\n/;
6036 5 die $e;
6037 }
6038 1 die 'vif: there are aliased coefficients in the model (' . join(', ', @{ $ds->{aliased} }) . ")\n"
6039 169 ▶ if @{ $ds->{aliased} };
Branch: 1 branch
BranchTrueFalseTotal
if @{$ds->{"aliased"};} 1 168 169
6040 # car counts the formula's terms; a factor with one level has no column
6041 # here, and lm() leaves it out of the fit, so it is not one of them
6042 168 my @terms = grep { $ds->{order}[$_] } 0 .. $#{ $ds->{term_labels} };
6043 168 ▶ die "vif: model contains fewer than 2 terms\n" if @terms < 2;
Branch: 1 branch
BranchTrueFalseTotal
if @terms < 2 5 163 168
6044 163 ▶ warn "vif: No intercept: vifs may not be sensible.\n" unless $ds->{intercept};
Branch: 1 branch
BranchTrueFalseTotal
unless $ds->{"intercept"} 2 161 163
6045 warn "vif: there are higher-order terms (interactions) in this model\n"
6046 163 ▶ if grep { $ds->{order}[$_] > 1 } @terms;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$ds->{"order"}[$_] > 1;} @terms 3 160 163
6047 163 my $q = @{ $ds->{names} };
6048 163 my $x = $ds->{cross};
6049 163 my @sd = map { sqrt $x->[$_ * $q + $_] } 0 .. $q - 1;
6050 163 my @C;
6051 163 for my $i (0 .. $q - 1) {
6052 358 $C[$i][$_] = $x->[$i * $q + $_] / ($sd[$i] * $sd[$_]) for 0 .. $q - 1;
6053 }
6054 163 ▶ my $L = _chol(\@C) or die "vif: there are aliased coefficients in the model\n";
Branch: 1 branch
BranchTrueFalseTotal
my $L = _chol(\@C) or die "vif: there are aliased coefficients in the model\n" 163 0 163
6055 # Cinv = L'^-1 L^-1, from M = L^-1
6056 163 my @M = map { [ (0) x $q ] } 1 .. $q;
6057 163 for my $j (0 .. $q - 1) {
6058 358 $M[$j][$j] = 1 / $L->[$j][$j];
6059 358 for my $i ($j + 1 .. $q - 1) {
6060 259 my $s = 0;
6061 259 $s += $L->[$i][$_] * $M[$_][$j] for $j .. $i - 1;
6062 259 $M[$i][$j] = -$s / $L->[$i][$i];
6063 }
6064 }
6065 163 my @Cinv;
6066 163 for my $i (0 .. $q - 1) {
6067 358 for my $j ($i .. $q - 1) {
6068 617 my $s = 0;
6069 617 $s += $M[$_][$i] * $M[$_][$j] for $j .. $q - 1;
6070 617 $Cinv[$i][$j] = $Cinv[$j][$i] = $s;
6071 }
6072 }
6073 163 my (%gvif, %df);
6074 163 for my $t (@terms) {
6075 351 my @S = grep { $ds->{assign}[$_] == $t + 1 } 0 .. $q - 1;
6076 351 my $label = $ds->{term_labels}[$t];
6077 351 $df{$label} = @S;
6078 $gvif{$label} = @S == 1
6079 ? $Cinv[ $S[0] ][ $S[0] ]
6080 14 : _chol_det([ map { my $i = $_; [ @{ $C[$i] }[@S] ] } @S ])
6081 351 ▶ * _chol_det([ map { my $i = $_; [ @{ $Cinv[$i] }[@S] ] } @S ]);
Branch: 1 branch
BranchTrueFalseTotal
@S == 1 ? : 344 7 351
6082 }
6083 163 ▶ return \%gvif unless grep { $_ > 1 } values %df;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ > 1;} values %df 159 4 163
6084 4 return { map { $_ => {
6085 GVIF => $gvif{$_},
6086 Df => $df{$_},
6087 14 'GVIF^(1/(2*Df))' => $gvif{$_} ** (1 / (2 * $df{$_})),
6088 } } keys %gvif };
6089 }
6090  
6091 # hosmer_lemeshow(\@observed, \@predicted, g => 10)
6092 #
6093 # Hosmer-Lemeshow goodness-of-fit test for a logistic model, ported from
6094 # ResourceSelection 0.3-6's hoslem.test() (R/hoslem.test.R):
6095 #
6096 # qq <- unique(quantile(yhat, probs = seq(0, 1, 1/g)))
6097 # cutyhat <- cut(yhat, breaks = qq, include.lowest = TRUE)
6098 # chisq <- sum((observed - expected)^2/expected)
6099 # G <- length(unique(cutyhat)); df = G - 2
6100 #
6101 # Up to 0.3217 the breaks were not made unique, so a prediction tied at the
6102 # lowest break -- every prediction from a model with only categorical
6103 # predictors -- was a group of its own rather than part of the first one; and
6104 # df was g - 2 where 0.3-6 has G - 2, the number of groups that hold a row.
6105 #
6106 # Two departures from 0.3-6, both where it has no usable answer:
6107 # * an empty bin, which cut() makes when two breaks fall in the gap between
6108 # the same two sorted predictions, is left out rather than added as 0/0,
6109 # which made R's statistic NaN; a bin whose expected count is 0 and whose
6110 # observed count is also 0 adds 0 for the same reason.
6111 # * the last break is the largest prediction. seq(0, 1, 1/g) ends at
6112 # 1 - 2^-53 rather than 1 for some g (49 is the first), and the last
6113 # quantile then falls below the largest prediction, which cut() leaves out of
6114 # every bin: at g = 49 the package's own example bins 499 of 500 rows and,
6115 # counting the NA as a group, reports df 48.
6116 62 ▶sub hosmer_lemeshow {
Subroutine hosmer_lemeshow: called CC=29 SCAR=33.7
Pod documented
6117 62 my ($obs, $pred, @opt) = @_;
6118 62 ▶ _usage_error('hosmer_lemeshow', 'observed and predicted must be array references')
Branch: 1 branch
BranchTrueFalseTotal
unless ref $obs eq "ARRAY" and ref $pred eq "ARRAY" 6 56 62
Condition100%3/3
ref $obs eq "ARRAY" and ref $pred eq "ARRAY"
!ll&&!rl&&r
4256
MC/DC100%2/2
ref $obs eq "ARRAY" and ref $pred eq "ARRAY"
ref $obs eq "ARRAY" ref $pred eq "ARRAY"
Truth table100%3/3
A and B
A= ref $obs eq "ARRAY"B= ref $pred eq "ARRAY"
ABresult
0X0
100
111
6119 unless ref $obs eq 'ARRAY' && ref $pred eq 'ARRAY';
6120 # checked before the hash is built, which an odd list makes die with perl's
6121 # own "Odd number of elements" instead
6122 56 ▶ _usage_error('hosmer_lemeshow', 'options after the two arrays must be name => value pairs')
Branch: 1 branch
BranchTrueFalseTotal
if @opt % 2 1 55 56
6123 if @opt % 2;
6124 55 my %opt = @opt;
6125 55 ▶ die "hosmer_lemeshow: observed and predicted must have the same length\n"
Branch: 1 branch
BranchTrueFalseTotal
unless @$obs == @$pred 2 53 55
6126 unless @$obs == @$pred;
6127 53 ▶ my $g = defined $opt{g} ? $opt{g} : 10;
Branch: 1 branch
BranchTrueFalseTotal
defined $opt{'g'} ? : 52 1 53
6128 53 ▶ _usage_error('hosmer_lemeshow', 'g must be an integer of at least 3')
Branch: 1 branch
BranchTrueFalseTotal
unless &looks_like_number($g) and $g == int $g and $g >= 3 5 48 53
Condition100%6/6
&looks_like_number($g) and $g == int $g and $g >= 3
!ll&&!rl&&r
2348
&looks_like_number($g) and $g == int $g
!ll&&!rl&&r
1151
MC/DC100%3/3
&looks_like_number($g) and $g == int $g and $g >= 3
&looks_like_number($g) $g == int $g $g >= 3
Truth table100%4/4
A and B and C
A= &looks_like_number($g)B= $g == int $gC= $g >= 3
ABCresult
0XX0
10X0
1100
1111
6129 unless looks_like_number($g) && $g == int $g && $g >= 3;
6130  
6131 48 my (@y, @p);
6132 48 for my $i (0 .. $#$obs) {
6133 9954 ▶ next unless defined $obs->[$i] && defined $pred->[$i]
Branch: 1 branch
BranchTrueFalseTotal
unless defined $obs->[$i] and defined $pred->[$i] and &looks_like_number($obs->[$i]) and &looks_like_number($pred->[$i]) 4 9950 9954
Condition100%9/9
defined $obs->[$i] and defined $pred->[$i] and &looks_like_number($obs->[$i]) and &looks_like_number($pred->[$i])
!ll&&!rl&&r
319950
defined $obs->[$i] and defined $pred->[$i] and &looks_like_number($obs->[$i])
!ll&&!rl&&r
219951
defined $obs->[$i] and defined $pred->[$i]
!ll&&!rl&&r
119952
MC/DC100%4/4
defined $obs->[$i] and defined $pred->[$i] and &looks_like_number($obs->[$i]) and &looks_like_number($pred->[$i])
defined $obs->[$i] defined $pred->[$i] &looks_like_number($obs->[$i]) &looks_like_number($pred->[$i])
Truth table100%5/5
A and B and C and D
A= defined $obs->[$i]B= defined $pred->[$i]C= &looks_like_number($obs->[$i])D= &looks_like_number($pred->[$i])
ABCDresult
0XXX0
10XX0
110X0
11100
11111
6134 && looks_like_number($obs->[$i]) && looks_like_number($pred->[$i]);
6135 9950 push @y, $obs->[$i] + 0;
6136 9950 push @p, $pred->[$i] + 0;
6137 }
6138 48 my $n = scalar @y;
6139 48 ▶ die "hosmer_lemeshow: not enough complete observations for g=$g groups\n" if $n < $g;
Branch: 1 branch
BranchTrueFalseTotal
if $n < $g 1 47 48
6140  
6141 47 my @sorted = sort { $a <=> $b } @p;
6142 # seq(0, 1, 1/g) is (0:g) * (1/g) in R, not (0:g) / g; the two differ in
6143 # the last bit for some k, and with it which side of a tie a break lands on.
6144 47 my $step = 1 / $g;
6145 780 my @br = sort { $a <=> $b }
6146 47 ( map( { _quantile7(\@sorted, $_ * $step) } 0 .. $g - 1 ), $sorted[-1] );
6147 47 my @qq = ($br[0]);
6148 47 ▶ for my $b (@br[1 .. $#br]) { push @qq, $b if $b != $qq[-1] }
Branch: 1 branch
BranchTrueFalseTotal
if $b != $qq[-1] 408 85 493
6149 47 ▶ die "hosmer_lemeshow: every prediction is $qq[0], so there is only one group\n"
Branch: 1 branch
BranchTrueFalseTotal
if @qq < 2 1 46 47
6150 if @qq < 2;
6151  
6152 46 my $nbin = $#qq;
6153 46 my (@O1, @O0, @E1, @E0, @ng);
6154 46 $O1[$_] = $O0[$_] = $E1[$_] = $E0[$_] = $ng[$_] = 0 for 0 .. $nbin - 1;
6155 46 for my $i (0 .. $n - 1) {
6156 # cut(..., include.lowest = TRUE): bin 0 is [qq0, qq1], bin k is
6157 # (qq_k, qq_k+1]. A binary search for the first break >= the
6158 # prediction, as R's .bincode() does.
6159 9926 my ($lo, $hi) = (0, $nbin);
6160 9926 ▶ while ($hi - $lo > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hi - $lo > 1) 34827 9926 44753
6161 34827 my $mid = ($lo + $hi) >> 1;
6162 34827 ▶ if ($p[$i] > $qq[$mid]) { $lo = $mid } else { $hi = $mid }
Branch: 1 branch
BranchTrueFalseTotal
if ($p[$i] > $qq[$mid]) { } 18716 16111 34827
6163 }
6164 9926 $O1[$lo] += $y[$i];
6165 9926 $O0[$lo] += 1 - $y[$i];
6166 9926 $E1[$lo] += $p[$i];
6167 9926 $E0[$lo] += 1 - $p[$i];
6168 9926 $ng[$lo]++;
6169 }
6170  
6171 46 my ($chi, $used) = (0, 0);
6172 46 my @groups;
6173 46 for my $j (0 .. $nbin - 1) {
6174 408 ▶ next unless $ng[$j];
Branch: 1 branch
BranchTrueFalseTotal
unless $ng[$j] 1 407 408
6175 407 $used++;
6176 407 for my $oe ([ $O1[$j], $E1[$j] ], [ $O0[$j], $E0[$j] ]) {
6177 814 my ($o, $e) = @$oe;
6178 814 ▶ $chi += $e ? ($o - $e) ** 2 / $e : $o ? 9**9**9 : 0;
Branch: 2 branches
BranchTrueFalseTotal
$e ? : 810 4 814
$o ? : 1 3 4
6179 }
6180 407 push @groups, { n => $ng[$j], observed => $O1[$j], expected => $E1[$j] };
6181 }
6182 46 ▶ die "hosmer_lemeshow: the predictions allow only one group\n" if $used < 2;
Branch: 1 branch
BranchTrueFalseTotal
if $used < 2 3 43 46
6183 43 ▶ warn "hosmer_lemeshow: the data allowed $used of the $g groups requested\n"
Branch: 1 branch
BranchTrueFalseTotal
if $used < $g 21 22 43
6184 if $used < $g;
6185 43 my $df = $used - 2;
6186 return {
6187 43 statistic => $chi,
6188 parameter => $df,
6189 # not _pchisq_upper(), which is NaN at an infinite statistic and 1 at
6190 # df 0, where R's 1 - pchisq(chisq, 0) is 0 for any chisq > 0
6191 'p_value' => pchisq($chi, $df, lower => 0),
6192 groups => $used,
6193 table => \@groups,
6194 };
6195 }
6196  
6197 # _qgamma($p, $shape, $scale): quantile of the gamma distribution, found by
6198 # inverting the regularized lower incomplete gamma P(shape, x) = p (bisection).
6199 #
6200 # The bisection compares against _pgamma_lower, which is the XS igam() and
6201 # computes the lower tail directly. It used to form that tail as
6202 # `1 - _igamc($shape, $x)`, and subtracting from 1 is precisely the
6203 # cancellation igam() was added to avoid: below a lower tail of about
6204 # NV_EPSILON the difference can only be a multiple of NV_EPSILON, so every
6205 # candidate the bisection tried compared equal and the search converged on
6206 # noise. age_standardize() reaches this with p = alpha/2, so it is the lower
6207 # confidence limit at a high conf_level that was affected -- conf_level =>
6208 # 0.9999 asks for the 5e-5 quantile, and 1 - _igamc could not resolve one.
6209 24 ▶sub _qgamma {
Subroutine _qgamma: called CC=12 SCAR=26.4
6210 24 my ($p, $shape, $scale) = @_;
6211 24 ▶ $scale = 1 unless defined $scale;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $scale 0 24 24
6212 24 ▶ return 0 if $p <= 0 || $shape <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $p <= 0 or $shape <= 0 0 24 24
Condition33%1/3
$p <= 0 or $shape <= 0
l!l&&r!l&&!r
0024
MC/DC0%0/2
$p <= 0 or $shape <= 0
$p <= 0 $shape <= 0
Truth table33%1/3
A or B
A= $p <= 0B= $shape <= 0
ABresult
1X1
011
000
6213 24 ▶ return 9**9**9 if $p >= 1;
Branch: 1 branch
BranchTrueFalseTotal
if $p >= 1 0 24 24
6214 24 my ($lo, $hi) = (0, 1);
6215 24 ▶ $hi *= 2 while _pgamma_lower($shape, $hi) < $p && $hi < 1e15;
Branch: 1 branch
BranchTrueFalseTotal
_pgamma_lower($shape, $hi) < $p and $hi < "1e+15" and do { $hi *= 2 } 180 24 204
Condition66%2/3
_pgamma_lower($shape, $hi) < $p and $hi < "1e+15"
!ll&&!rl&&r
240180
MC/DC50%1/2
_pgamma_lower($shape, $hi) < $p and $hi < "1e+15"
_pgamma_lower($shape, $hi) < $p $hi < "1e+15"
Truth table66%2/3
A and B
A= _pgamma_lower($shape, $hi) < $pB= $hi < "1e+15"
ABresult
0X0
100
111
6216 24 for (1 .. 300) {
6217 984 my $mid = ($lo + $hi) / 2;
6218 984 ▶ last if $mid <= $lo || $mid >= $hi; # adjacent NVs: nothing left
Branch: 1 branch
BranchTrueFalseTotal
if $mid <= $lo or $mid >= $hi 0 984 984
Condition33%1/3
$mid <= $lo or $mid >= $hi
l!l&&r!l&&!r
00984
MC/DC0%0/2
$mid <= $lo or $mid >= $hi
$mid <= $lo $mid >= $hi
Truth table33%1/3
A or B
A= $mid <= $loB= $mid >= $hi
ABresult
1X1
011
000
6219 984 ▶ if (_pgamma_lower($shape, $mid) < $p) { $lo = $mid } else { $hi = $mid }
Branch: 1 branch
BranchTrueFalseTotal
if (_pgamma_lower($shape, $mid) < $p) { } 478 506 984
6220 984 ▶ last if ($hi - $lo) <= 1e-12 * ($hi + 1e-300);
Branch: 1 branch
BranchTrueFalseTotal
if $hi - $lo <= "1e-12" * ($hi + "1e-300") 24 960 984
6221 }
6222 24 return $scale * ($lo + $hi) / 2;
6223 }
6224  
6225 # age_standardize(\@count, \@pop, \@stdpop, conf_level => 0.95, per => 1)
6226 # or age_standardize(count => \@c, pop => \@n, stdpop => \@w, ...)
6227 # (supply rate => \@r instead of count if you have stratum-specific rates)
6228 #
6229 # Directly standardized rate: reweights stratum-specific rates to a standard
6230 # population. The confidence interval uses the Fay-Feuer gamma method (as in
6231 # R's epitools::ageadjust.direct), which is accurate even for rare events.
6232 # `per` scales every reported rate (e.g. per => 100_000). Validated against R.
6233 20 ▶sub age_standardize {
Subroutine age_standardize: called CC=29 SCAR=35.0
Pod documented
6234 20 my @a = @_;
6235 20 my (%opt, $count, $pop, $stdpop, $rate);
6236 20 my $positional = ref $a[0] eq 'ARRAY'; # else every argument is named
6237 20 ▶ ($count, $pop, $stdpop) = splice(@a, 0, 3) if $positional;
Branch: 1 branch
BranchTrueFalseTotal
if $positional 14 6 20
6238 20 ▶ _usage_error('age_standardize', 'arguments must be name => value pairs, after any '
Branch: 1 branch
BranchTrueFalseTotal
if @a % 2 3 17 20
6239 . 'positional count, pop and stdpop') if @a % 2;
6240 17 _opt_names_defined('age_standardize', @a);
6241 17 %opt = @a;
6242 17 ▶ ($count, $pop, $stdpop, $rate) = @opt{qw(count pop stdpop rate)} unless $positional;
Branch: 1 branch
BranchTrueFalseTotal
unless $positional 4 13 17
6243 17 my %known = map { $_ => 1 } qw(count pop stdpop rate per conf_level);
6244 17 my @bad = sort grep { !$known{$_} } keys %opt;
6245 17 ▶ _usage_error('age_standardize', "unknown argument(s): @bad") if @bad;
Branch: 1 branch
BranchTrueFalseTotal
if @bad 1 16 17
6246 16 ▶ $rate ||= $opt{rate};
Condition66%2/3
$rate ||= $opt{"rate"}
l!l&&r!l&&!r
1015
MC/DC50%1/2
$rate ||= $opt{"rate"}
$rate $opt{"rate"}
Truth table66%2/3
A ||= B
A= $rateB= $opt{"rate"}
ABresult
1X1
011
000
6247 16 ▶ my $cl = defined $opt{conf_level} ? $opt{conf_level} : 0.95;
Branch: 1 branch
BranchTrueFalseTotal
defined $opt{'conf_level'} ? : 9 7 16
6248 16 ▶ my $per = defined $opt{per} ? $opt{per} : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $opt{'per'} ? : 1 15 16
6249 16 ▶ _usage_error('age_standardize', 'conf_level must be between 0 and 1') unless $cl > 0 && $cl < 1;
Branch: 1 branch
BranchTrueFalseTotal
unless $cl > 0 and $cl < 1 1 15 16
Condition66%2/3
$cl > 0 and $cl < 1
!ll&&!rl&&r
0115
MC/DC50%1/2
$cl > 0 and $cl < 1
$cl > 0 $cl < 1
Truth table66%2/3
A and B
A= $cl > 0B= $cl < 1
ABresult
0X0
100
111
6250 15 ▶ _usage_error('age_standardize', "'pop' and 'stdpop' array refs are required")
Branch: 1 branch
BranchTrueFalseTotal
unless ref $pop eq "ARRAY" and ref $stdpop eq "ARRAY" 1 14 15
Condition66%2/3
ref $pop eq "ARRAY" and ref $stdpop eq "ARRAY"
!ll&&!rl&&r
1014
MC/DC50%1/2
ref $pop eq "ARRAY" and ref $stdpop eq "ARRAY"
ref $pop eq "ARRAY" ref $stdpop eq "ARRAY"
Truth table66%2/3
A and B
A= ref $pop eq "ARRAY"B= ref $stdpop eq "ARRAY"
ABresult
0X0
100
111
6251 unless ref $pop eq 'ARRAY' && ref $stdpop eq 'ARRAY';
6252 14 ▶ _usage_error('age_standardize', "supply either 'count' or 'rate'")
Branch: 1 branch
BranchTrueFalseTotal
unless ref $count eq "ARRAY" or ref $rate eq "ARRAY" 1 13 14
Condition100%3/3
ref $count eq "ARRAY" or ref $rate eq "ARRAY"
l!l&&r!l&&!r
1211
MC/DC100%2/2
ref $count eq "ARRAY" or ref $rate eq "ARRAY"
ref $count eq "ARRAY" ref $rate eq "ARRAY"
Truth table100%3/3
A or B
A= ref $count eq "ARRAY"B= ref $rate eq "ARRAY"
ABresult
1X1
011
000
6253 unless ref $count eq 'ARRAY' || ref $rate eq 'ARRAY';
6254  
6255 13 my $k = scalar @$pop;
6256 13 ▶ die "age_standardize: pop and stdpop must have the same length\n" unless @$stdpop == $k;
Branch: 1 branch
BranchTrueFalseTotal
unless @$stdpop == $k 1 12 13
6257 12 ▶ if (ref $count eq 'ARRAY') { die "age_standardize: count and pop length mismatch\n" unless @$count == $k; }
Branch: 2 branches
BranchTrueFalseTotal
if (ref $count eq 'ARRAY') { } 11 1 12
unless @$count == $k 0 11 11
6258 1 ▶ else { die "age_standardize: rate and pop length mismatch\n" unless @$rate == $k; }
Branch: 1 branch
BranchTrueFalseTotal
unless @$rate == $k 0 1 1
6259  
6260 12 ▶ my @cnt = ref $count eq 'ARRAY' ? @$count : map { $rate->[$_] * $pop->[$_] } 0 .. $k - 1;
Branch: 1 branch
BranchTrueFalseTotal
ref $count eq 'ARRAY' ? : 11 1 12
6261 12 my ($sum_c, $sum_n, $sum_w) = (0, 0, 0);
6262 12 $sum_c += $cnt[$_], $sum_n += $pop->[$_], $sum_w += $stdpop->[$_] for 0 .. $k - 1;
6263 12 ▶ die "age_standardize: total population and standard population must be positive\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $sum_n > 0 and $sum_w > 0 0 12 12
Condition33%1/3
$sum_n > 0 and $sum_w > 0
!ll&&!rl&&r
0012
MC/DC0%0/2
$sum_n > 0 and $sum_w > 0
$sum_n > 0 $sum_w > 0
Truth table33%1/3
A and B
A= $sum_n > 0B= $sum_w > 0
ABresult
0X0
100
111
6264 unless $sum_n > 0 && $sum_w > 0;
6265  
6266 12 my ($dsr, $var, $wmax) = (0, 0, 0);
6267 12 for my $i (0 .. $k - 1) {
6268 48 ▶ die "age_standardize: stratum $i has non-positive population\n" if $pop->[$i] <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $pop->[$i] <= 0 0 48 48
6269 48 my $r = $cnt[$i] / $pop->[$i];
6270 48 my $wt = $stdpop->[$i] / $sum_w; # normalized weight
6271 48 $dsr += $wt * $r;
6272 48 $var += $wt * $wt * $cnt[$i] / ($pop->[$i] ** 2);
6273 48 my $w_over_n = $wt / $pop->[$i];
6274 48 ▶ $wmax = $w_over_n if $w_over_n > $wmax;
Branch: 1 branch
BranchTrueFalseTotal
if $w_over_n > $wmax 12 36 48
6275 }
6276 12 my $crude = $sum_c / $sum_n;
6277  
6278 12 my $alpha = 1 - $cl;
6279 12 my ($lci, $uci);
6280 12 ▶ if ($dsr > 0 && $var > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dsr > 0 and $var > 0) { } 12 0 12
Condition33%1/3
$dsr > 0 and $var > 0
!ll&&!rl&&r
0012
MC/DC0%0/2
$dsr > 0 and $var > 0
$dsr > 0 $var > 0
Truth table33%1/3
A and B
A= $dsr > 0B= $var > 0
ABresult
0X0
100
111
6281 12 $lci = _qgamma($alpha / 2, ($dsr ** 2) / $var, $var / $dsr);
6282 12 $uci = _qgamma(1 - $alpha / 2, (($dsr + $wmax) ** 2) / ($var + $wmax ** 2),
6283 ($var + $wmax ** 2) / ($dsr + $wmax));
6284 } else {
6285 0 $lci = 0;
6286 0 ▶ $uci = ($dsr == 0) ? _qgamma(1 - $alpha / 2, 1, $wmax > 0 ? $wmax : 0) : $dsr;
Branch: 2 branches
BranchTrueFalseTotal
$dsr == 0 ? : 0 0 0
$wmax > 0 ? : 0 0 0
6287 }
6288  
6289 return {
6290 12 crude_rate => $crude * $per,
6291 adj_rate => $dsr * $per,
6292 'conf_int' => [ $lci * $per, $uci * $per ],
6293 se => sqrt($var) * $per,
6294 'conf_level' => $cl,
6295 per => $per,
6296 };
6297 }
6298  
6299  
6300 # anova($fit0, $fit1, ..., test => 'Chisq' | 'LRT' | 'F', dispersion => $phi)
6301 #
6302 # Nested-model comparison on fits that already exist -- R's anova(m0, m1) --
6303 # for lm() and glm() results, and MASS's likelihood-ratio table for
6304 # negative-binomial fits whose theta was estimated. The XS anova() compares
6305 # models it fits itself from a data set and formulas; this is the same
6306 # question asked of fits the caller already has, which is what a first-stage
6307 # F on excluded instruments or a joint test of a block of terms needs. Any
6308 # call whose first argument is not a fitted model goes to the XS function
6309 # unchanged.
6310 #
6311 # The arithmetic is R's: anova.lmlist() for lm, anova.glmlist() and
6312 # stat.anova() for glm, anova.negbin() for glm.nb. Rows come back in the
6313 # order given (MASS's negbin table is sorted by residual df, as it sorts it),
6314 # each a hash; every row after the first carries the comparison with the row
6315 # before it.
6316 {
6317 497 my $xs_anova = \&anova;
6318 # The XS anova() is prototyped ($@), which would put anova(@fits) in
6319 # scalar context and hand it the number of fits; the replacement has no
6320 # prototype, which changes nothing for the XS call forms.
6321 497 ▶ no warnings qw(redefine prototype);
Subroutine BEGIN: called CC=1 SCAR=0.0
6322 249 ▶ *anova = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
6323 249 ▶ return _anova_fits(@_) if @_ && _is_fit($_[0]);
Branch: 1 branch
BranchTrueFalseTotal
if @_ and _is_fit($_[0]) 21 228 249
Condition100%3/3
@_ and _is_fit($_[0])
!ll&&!rl&&r
122721
MC/DC100%2/2
@_ and _is_fit($_[0])
@_ _is_fit($_[0])
Truth table100%3/3
A and B
A= @_B= _is_fit($_[0])
ABresult
0X0
100
111
6324 228 goto &$xs_anova;
6325 497 };
6326 }
6327  
6328 313 ▶sub _is_fit {
Subroutine _is_fit: called CC=6 SCAR=18.7
6329 313 my $f = shift;
6330 return ref $f eq 'HASH' && exists $f->{coefficients} && exists $f->{'df_residual'}
6331 313 ▶ && !ref $f->{'df_residual'} && (exists $f->{rss} || exists $f->{deviance});
Condition73%11/15
ref $f eq 'HASH' && exists $f->{'coefficients'} && exists $f->{'df_residual'} && !ref($f->{'df_residual'}) && (exists $f->{'rss'} || exists $f->{'deviance'})
!ll&&!rl&&r
239074
ref $f eq "HASH" and exists $f->{"coefficients"} and exists $f->{"df_residual"} and not ref $f->{"df_residual"}
!ll&&!rl&&r
239074
ref $f eq "HASH" and exists $f->{"coefficients"} and exists $f->{"df_residual"}
!ll&&!rl&&r
239074
ref $f eq "HASH" and exists $f->{"coefficients"}
!ll&&!rl&&r
1822174
exists $f->{"rss"} or exists $f->{"deviance"}
l!l&&r!l&&!r
30440
MC/DC33%2/6
ref $f eq 'HASH' && exists $f->{'coefficients'} && exists $f->{'df_residual'} && !ref($f->{'df_residual'}) && (exists $f->{'rss'} || exists $f->{'deviance'})
ref $f eq "HASH" exists $f->{"coefficients"} exists $f->{"df_residual"} not ref $f->{"df_residual"} exists $f->{"rss"} exists $f->{"deviance"}
Truth table57%4/7
A and B and C and not(D) && E or F
A= ref $f eq "HASH"B= exists $f->{"coefficients"}C= exists $f->{"df_residual"}D= not ref $f->{"df_residual"}E= exists $f->{"rss"}F= exists $f->{"deviance"}
ABCDEFresult
0XXXXX0
10XXXX0
110XXX0
1110XX0
1111000
11111X1
1111011
6332 }
6333  
6334 21 ▶sub _anova_fits {
Subroutine _anova_fits: called CC=52 SCAR=40.7
6335 21 my @m;
6336 21 ▶ push @m, shift while @_ && _is_fit($_[0]);
Branch: 1 branch
BranchTrueFalseTotal
@_ and _is_fit($_[0]) and do { push @m, shift() } 53 21 74
Condition100%3/3
@_ and _is_fit($_[0])
!ll&&!rl&&r
91253
MC/DC100%2/2
@_ and _is_fit($_[0])
@_ _is_fit($_[0])
Truth table100%3/3
A and B
A= @_B= _is_fit($_[0])
ABresult
0X0
100
111
6337 21 ▶ _usage_error('anova', 'options after the models must be name => value pairs') if @_ % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ % 2 0 21 21
6338 21 my %opt = @_;
6339 21 for (sort keys %opt) {
6340 12 ▶ _usage_error('anova', "unknown argument '$_'") unless /^(?:test|dispersion)$/;
Branch: 1 branch
BranchTrueFalseTotal
unless /^(?:test|dispersion)$/ 1 11 12
6341 }
6342 20 ▶ _usage_error('anova', 'give at least two fitted models to compare') if @m < 2;
Branch: 1 branch
BranchTrueFalseTotal
if @m < 2 1 19 20
6343 19 my $is_glm = exists $m[0]{family};
6344 19 for (@m) {
6345 die "anova: cannot compare lm() and glm() fits in one table\n"
6346 50 ▶ if (exists $_->{family}) != $is_glm;
Branch: 1 branch
BranchTrueFalseTotal
if exists $_->{"family"} != $is_glm 1 49 50
6347 die "anova: models are not all of the same family\n"
6348 49 ▶ if $is_glm && $_->{family} ne $m[0]{family};
Branch: 1 branch
BranchTrueFalseTotal
if $is_glm and $_->{"family"} ne $m[0]{"family"} 1 48 49
Condition100%3/3
$is_glm and $_->{"family"} ne $m[0]{"family"}
!ll&&!rl&&r
23251
MC/DC100%2/2
$is_glm and $_->{"family"} ne $m[0]{"family"}
$is_glm $_->{"family"} ne $m[0]{"family"}
Truth table100%3/3
A and B
A= $is_glmB= $_->{"family"} ne $m[0]{"family"}
ABresult
0X0
100
111
6349 }
6350 # anova.lmlist / anova.glmlist: "models were not all fitted to the same
6351 # size of dataset"
6352 17 ▶ my @n = map { $is_glm ? $_->{nobs} : $_->{'df_residual'} + $_->{rank} } @m;
Branch: 1 branch
BranchTrueFalseTotal
$is_glm ? : 23 23 46
6353 17 for (@n) {
6354 46 ▶ die "anova: models were not all fitted to the same size of dataset\n"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_ and $_ == $n[0] 1 45 46
Condition66%2/3
defined $_ and $_ == $n[0]
!ll&&!rl&&r
0145
MC/DC50%1/2
defined $_ and $_ == $n[0]
defined $_ $_ == $n[0]
Truth table66%2/3
A and B
A= defined $_B= $_ == $n[0]
ABresult
0X0
100
111
6355 unless defined $_ && $_ == $n[0];
6356 }
6357 16 ▶ my $fam = $is_glm ? $m[0]{family} : 'lm';
Branch: 1 branch
BranchTrueFalseTotal
$is_glm ? : 10 6 16
6358 16 ▶ $fam = 'negbin' if $fam eq 'negative.binomial' || $fam eq 'nb';
Branch: 1 branch
BranchTrueFalseTotal
if $fam eq "negative.binomial" or $fam eq "nb" 0 16 16
Condition33%1/3
$fam eq "negative.binomial" or $fam eq "nb"
l!l&&r!l&&!r
0016
MC/DC0%0/2
$fam eq "negative.binomial" or $fam eq "nb"
$fam eq "negative.binomial" $fam eq "nb"
Truth table33%1/3
A or B
A= $fam eq "negative.binomial"B= $fam eq "nb"
ABresult
1X1
011
000
6359 16 ▶ if ($fam eq 'negbin' && !grep { !exists $_->{'SE_theta'} } @m) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fam eq "negbin" and not grep({not exists $_->{"SE_theta"};} @m)) 3 13 16
Condition66%2/3
$fam eq "negbin" and not grep({not exists $_->{"SE_theta"};} @m)
!ll&&!rl&&r
1303
MC/DC50%1/2
$fam eq "negbin" and not grep({not exists $_->{"SE_theta"};} @m)
$fam eq "negbin" not grep({not exists $_->{"SE_theta"};} @m)
Truth table66%2/3
A and not(B)
A= $fam eq "negbin"B= not grep({not exists $_->{"SE_theta"};} @m)
ABresult
0X0
100
111
6360 # MASS anova.negbin(): theta was estimated in every model, so the
6361 # models differ in theta too and only the likelihood-ratio test is
6362 # valid. MASS sorts by residual df, largest first.
6363 die "anova: negbin models are compared by the likelihood-ratio test only\n"
6364 3 ▶ if defined $opt{test} && $opt{test} !~ /^(?:Chisq|LRT)$/;
Branch: 1 branch
BranchTrueFalseTotal
if defined $opt{"test"} and not $opt{"test"} =~ /^(?:Chisq|LRT)$/ 1 2 3
Condition66%2/3
defined $opt{"test"} and not $opt{"test"} =~ /^(?:Chisq|LRT)$/
!ll&&!rl&&r
201
MC/DC50%1/2
defined $opt{"test"} and not $opt{"test"} =~ /^(?:Chisq|LRT)$/
defined $opt{"test"} not $opt{"test"} =~ /^(?:Chisq|LRT)$/
Truth table66%2/3
A and not(B)
A= defined $opt{"test"}B= not $opt{"test"} =~ /^(?:Chisq|LRT)$/
ABresult
0X0
100
111
6365 2 my @o = sort { $b->{'df_residual'} <=> $a->{'df_residual'} } @m;
6366 2 my @rows;
6367 2 for my $i (0 .. $#o) {
6368 my %r = (theta => $o[$i]{theta}, 'Resid. df' => $o[$i]{'df_residual'},
6369 4 '2 x log-lik.' => $o[$i]{twologlik});
6370 4 ▶ if ($i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i) 2 2 4
6371 2 my $df = $o[$i - 1]{'df_residual'} - $o[$i]{'df_residual'};
6372 2 my $lr = $o[$i]{twologlik} - $o[$i - 1]{twologlik};
6373 2 $r{df} = $df;
6374 2 $r{'LR stat.'} = $lr;
6375 # MASS: 1 - pchisq(x2, df); the upper tail directly here
6376 2 $r{'Pr(Chi)'} = pchisq($lr, $df, lower => 0);
6377 }
6378 4 push @rows, \%r;
6379 }
6380 2 return \@rows;
6381 }
6382 13 my @resdf = map { $_->{'df_residual'} } @m;
6383 13 ▶ my @resdev = map { $is_glm ? $_->{deviance} : $_->{rss} } @m;
Branch: 1 branch
BranchTrueFalseTotal
$is_glm ? : 15 23 38
6384 13 my ($big) = sort { $resdf[$a] <=> $resdf[$b] } 0 .. $#m; # order(resdf)[1]
6385 13 my ($test, $scale, $df_scale);
6386 13 ▶ if (!$is_glm) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $is_glm) { } 6 7 13
6387 # anova.lmlist: an F test on the largest model's residual mean square
6388 6 ▶ $test = defined $opt{test} ? $opt{test} : 'F';
Branch: 1 branch
BranchTrueFalseTotal
defined $opt{'test'} ? : 5 1 6
6389 6 ▶ $scale = defined $opt{dispersion} ? $opt{dispersion} : $resdev[$big] / $resdf[$big];
Branch: 1 branch
BranchTrueFalseTotal
defined $opt{'dispersion'} ? : 0 6 6
6390 6 $df_scale = $resdf[$big];
6391 } else {
6392 # anova.glmlist: family$dispersion is 1 for binomial and poisson and
6393 # NA for gaussian; a negbin at a fixed theta has its variance fully
6394 # specified, and is treated like poisson (see glm's dispersion).
6395 7 my $known = $fam ne 'gaussian';
6396 7 ▶ $test = defined $opt{test} ? $opt{test} : ($known ? 'Chisq' : 'F');
Branch: 2 branches
BranchTrueFalseTotal
defined $opt{'test'} ? : 5 2 7
$known ? : 1 1 2
6397 7 ▶ $scale = defined $opt{dispersion} ? $opt{dispersion} : $m[$big]{dispersion};
Branch: 1 branch
BranchTrueFalseTotal
defined $opt{'dispersion'} ? : 0 7 7
6398 7 ▶ $df_scale = (defined $opt{dispersion} ? $opt{dispersion} == 1 : $known)
Branch: 2 branches
BranchTrueFalseTotal
(defined $opt{'dispersion'} ? $opt{'dispersion'} == 1 : $known) ? : 5 2 7
defined $opt{'dispersion'} ? : 0 7 7
6399 ? 9**9**9 : $resdf[$big];
6400 7 ▶ if ($test eq 'F' && $df_scale == 9**9**9) {
Branch: 1 branch
BranchTrueFalseTotal
if ($test eq "F" and $df_scale == "Inf") 1 6 7
Condition100%3/3
$test eq "F" and $df_scale == "Inf"
!ll&&!rl&&r
511
MC/DC100%2/2
$test eq "F" and $df_scale == "Inf"
$test eq "F" $df_scale == "Inf"
Truth table100%3/3
A and B
A= $test eq "F"B= $df_scale == "Inf"
ABresult
0X0
100
111
6401 1 ▶ warn(($fam eq 'binomial' || $fam eq 'poisson')
Branch: 1 branch
BranchTrueFalseTotal
$fam eq 'binomial' || $fam eq 'poisson' ? : 1 0 1
Condition33%1/3
$fam eq "binomial" or $fam eq "poisson"
l!l&&r!l&&!r
010
MC/DC0%0/2
$fam eq "binomial" or $fam eq "poisson"
$fam eq "binomial" $fam eq "poisson"
Truth table33%1/3
A or B
A= $fam eq "binomial"B= $fam eq "poisson"
ABresult
1X1
011
000
6402 ? "anova: using F test with a '$fam' family is inappropriate\n"
6403 : "anova: using F test with a fixed dispersion is inappropriate\n");
6404 }
6405 }
6406 13 ▶ _usage_error('anova', "test must be 'F', 'Chisq' or 'LRT'") unless $test =~ /^(?:F|Chisq|LRT)$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $test =~ /^(?:F|Chisq|LRT)$/ 1 12 13
6407 12 my @rows;
6408 12 for my $i (0 .. $#m) {
6409 36 ▶ my %r = $is_glm ? ('Resid. Df' => $resdf[$i], 'Resid. Dev' => $resdev[$i])
Branch: 1 branch
BranchTrueFalseTotal
$is_glm ? : 13 23 36
6410 : ('Res_Df' => $resdf[$i], 'RSS' => $resdev[$i]);
6411 36 ▶ if ($i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i) 24 12 36
6412 24 my $df = $resdf[$i - 1] - $resdf[$i];
6413 24 my $dv = $resdev[$i - 1] - $resdev[$i];
6414 24 $r{Df} = $df;
6415 24 ▶ $r{ $is_glm ? 'Deviance' : 'Sum of Sq' } = $dv;
Branch: 1 branch
BranchTrueFalseTotal
$is_glm ? : 7 17 24
6416 # stat.anova(): a zero or negative statistic has no p-value (NA)
6417 24 ▶ if ($test eq 'F') {
Branch: 1 branch
BranchTrueFalseTotal
if ($test eq 'F') { } 17 7 24
6418 17 ▶ my $F = ($df != 0) ? ($dv / $df) / $scale : undef;
Branch: 1 branch
BranchTrueFalseTotal
$df != 0 ? : 17 0 17
6419 17 ▶ $F = undef if defined $F && $F < 0;
Branch: 1 branch
BranchTrueFalseTotal
if defined $F and $F < 0 0 17 17
Condition33%1/3
defined $F and $F < 0
!ll&&!rl&&r
0170
MC/DC0%0/2
defined $F and $F < 0
defined $F $F < 0
Truth table33%1/3
A and B
A= defined $FB= $F < 0
ABresult
0X0
100
111
6420 17 $r{F} = $F;
6421 17 ▶ $r{'Pr(>F)'} = defined $F
Branch: 2 branches
BranchTrueFalseTotal
defined $F ? : 17 0 17
$df_scale == 9**9**9 ? : 1 16 17
6422 ? ($df_scale == 9**9**9 ? pchisq($F * abs($df), abs($df), lower => 0)
6423 : pf($F, abs($df), $df_scale, lower => 0))
6424 : undef;
6425 } else {
6426 7 ▶ my $v = ($df != 0) ? ($dv / $scale) * ($df <=> 0) : undef;
Branch: 1 branch
BranchTrueFalseTotal
$df != 0 ? : 7 0 7
6427 7 ▶ $v = undef if defined $v && $v < 0;
Branch: 1 branch
BranchTrueFalseTotal
if defined $v and $v < 0 0 7 7
Condition33%1/3
defined $v and $v < 0
!ll&&!rl&&r
070
MC/DC0%0/2
defined $v and $v < 0
defined $v $v < 0
Truth table33%1/3
A and B
A= defined $vB= $v < 0
ABresult
0X0
100
111
6428 7 ▶ $r{'Pr(>Chi)'} = defined $v ? pchisq($v, abs($df), lower => 0) : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $v ? : 7 0 7
6429 }
6430 }
6431 36 push @rows, \%r;
6432 }
6433 12 return \@rows;
6434 }
6435  
6436 # The PerlIO::via layers _open_decompressed() reads a compressed file through.
6437 # PerlIO::via calls FILL whenever the parser's sv_gets() has used up the
6438 # buffer, and FILL returns the next piece of decompressed text, or undef at
6439 # the end.
6440 #
6441 # A file may hold several compressed members one after another, and every one
6442 # of them is read: `cat a.gz b.gz' is a valid gzip file (RFC 1952 section 2.2)
6443 # holding both, bgzip -- the BGZF format of tabix and every .vcf.gz -- writes
6444 # nothing else, and pbzip2 does the same with bzip2 streams. Stopping at the
6445 # first would drop all but the first 64 KB of a bgzip file without a word. NUL
6446 # bytes between or after members are skipped, as Python's gzip module skips
6447 # them (Lib/gzip.py, _GzipReader._read_eof(): "Gzip files can be padded with
6448 # zeroes and still have archives"); anything else there is an error, as it is
6449 # in Python, rather than being passed on as text, as R's gzio.h does
6450 # ("transparent" mode).
6451 #
6452 # Each member's own check is made: zlib compares a gzip member's CRC-32 and
6453 # length against its trailer, bzip2 its block and stream CRCs, and a mismatch
6454 # comes back as corrupt data.
6455 package Stats::LikeR::_Decompress;
6456  
6457 497 our $name; # the file being opened; see _open_decompressed()
6458  
6459 # 1<<16, as a read of both the compressed bytes and the text made per call.
6460 # read_table of a 2,000,000 x 5 CSV (110 MB, 47 MB gzipped) took 1.50 s from
6461 # the .gz against 0.82 s from the plain file, three runs each; 1<<12 took
6462 # 1.66 s, 1<<14 1.60 s, and 1<<18 and 1<<20 both 1.45 s. `zcat' alone takes
6463 # 0.69 s, so the layer adds almost nothing to the inflate itself, and the last
6464 # 3% is not worth four times the buffer. The .bz2 took 4.47 s, against 3.69 s
6465 # for `bzip2 -dc' alone.
6466 497 my $BUFSIZE = 1 << 16;
6467  
6468 1366 ▶sub PUSHED {
Subroutine PUSHED: called CC=1 SCAR=0.0
6469 1366 my ($class) = @_;
6470 1366 return bless { name => $name, in => '', eof => 0, z => undef,
6471 members => 0 }, $class;
6472 }
6473  
6474 4634 ▶sub FILL {
Subroutine FILL: called CC=14 SCAR=26.4
6475 4634 my ($self, $fh) = @_;
6476 4634 my $magic = $self->MAGIC;
6477 4634 for (;;) {
6478 7902 ▶ if (!$self->{z}) { # at the start of a member, or past the last
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"z"}) 5547 2355 7902
6479 5547 ▶ $self->{in} =~ s/\A\0+// if $self->{members};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"members"} 2815 2732 5547
6480 5547 ▶ if (length $self->{in} < length($magic) && !$self->{eof}) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $self->{"in"} < length $magic and not $self->{"eof"}) 1559 3988 5547
Condition100%3/3
length $self->{"in"} < length $magic and not $self->{"eof"}
!ll&&!rl&&r
37941941559
MC/DC100%2/2
length $self->{"in"} < length $magic and not $self->{"eof"}
length $self->{"in"} < length $magic not $self->{"eof"}
Truth table100%3/3
A and not(B)
A= length $self->{"in"} < length $magicB= not $self->{"eof"}
ABresult
0X0
100
111
6481 1559 $self->_more($fh);
6482 1559 next;
6483 }
6484 3988 ▶ return undef if !length $self->{in} && $self->{members};
Branch: 1 branch
BranchTrueFalseTotal
if not length $self->{"in"} and $self->{"members"} 191 3797 3988
Condition66%2/3
not length $self->{"in"} and $self->{"members"}
!ll&&!rl&&r
37970191
MC/DC50%1/2
not length $self->{"in"} and $self->{"members"}
not length $self->{"in"} $self->{"members"}
Truth table66%2/3
not(A) and B
A= not length $self->{"in"}B= $self->{"members"}
ABresult
0X0
100
111
6485 die "read_table: \"$self->{name}\" has data after its last "
6486 . $self->CODEC . " member that is not " . $self->CODEC . "\n"
6487 3797 ▶ unless substr($self->{in}, 0, length $magic) eq $magic;
Branch: 1 branch
BranchTrueFalseTotal
unless substr($self->{"in"}, 0, length $magic) eq $magic 4 3793 3797
6488 3793 $self->{z} = $self->NEW;
6489 }
6490 6148 my $before = length $self->{in};
6491 6148 my $out = '';
6492 6148 my ($status, $ended) = $self->INFLATE($out);
6493 6148 ▶ die "read_table: \"$self->{name}\" is not valid " . $self->CODEC
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ended 3 6145 6148
6494 . " data ($status)\n" unless defined $ended;
6495 6145 ▶ if ($ended) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ended) 2704 3441 6145
6496 2704 $self->{z} = undef;
6497 2704 $self->{members}++;
6498 }
6499 6145 ▶ return $out if length $out;
Branch: 1 branch
BranchTrueFalseTotal
if length $out 3380 2765 6145
6500 2765 ▶ next if $ended || length $self->{in} != $before;
Branch: 1 branch
BranchTrueFalseTotal
if $ended or length $self->{"in"} != $before 586 2179 2765
Condition100%3/3
$ended or length $self->{"in"} != $before
l!l&&r!l&&!r
275592179
MC/DC100%2/2
$ended or length $self->{"in"} != $before
$ended length $self->{"in"} != $before
Truth table100%3/3
A or B
A= $endedB= length $self->{"in"} != $before
ABresult
1X1
011
000
6501 # no progress: the member wants more input than has been read
6502 die "read_table: \"$self->{name}\" ends in the middle of its "
6503 2179 ▶ . $self->CODEC . " data; it is truncated\n" if $self->{eof};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"eof"} 1056 1123 2179
6504 1123 $self->_more($fh);
6505 }
6506 }
6507  
6508 2682 ▶sub _more {
Subroutine _more: called CC=3 SCAR=11.0
6509 2682 my ($self, $fh) = @_;
6510 2682 my $n = read $fh, $self->{in}, $BUFSIZE, length $self->{in};
6511 2682 ▶ die "read_table: could not read \"$self->{name}\": $!\n" unless defined $n;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $n 0 2682 2682
6512 2682 ▶ $self->{eof} = 1 unless $n;
Branch: 1 branch
BranchTrueFalseTotal
unless $n 1249 1433 2682
6513 2682 return;
6514 }
6515  
6516 package Stats::LikeR::_Gunzip;
6517 497 our @ISA = ('Stats::LikeR::_Decompress');
6518  
6519 591 ▶sub CODEC { 'gzip' }
Subroutine CODEC: called CC=1 SCAR=0.0
6520 3597 ▶sub MAGIC { "\x1f\x8b" }
Subroutine MAGIC: called CC=1 SCAR=0.0
6521  
6522 2857 ▶sub NEW {
Subroutine NEW: called CC=2 SCAR=7.1
6523 # WANT_GZIP: a gzip wrapper, header and trailer, around each deflate
6524 # stream. LimitOutput caps what one call makes at about Bufsize, so one
6525 # 64 KB read of a highly compressible member cannot inflate into hundreds
6526 # of megabytes at once.
6527 2857 my ($z, $err) = Compress::Raw::Zlib::Inflate->new(
6528 -WindowBits => Compress::Raw::Zlib::WANT_GZIP(),
6529 -Bufsize => $BUFSIZE,
6530 -ConsumeInput => 1,
6531 -LimitOutput => 1);
6532 2857 ▶ die "read_table: could not start inflating: $err\n" unless $z;
Branch: 1 branch
BranchTrueFalseTotal
unless $z 0 2857 2857
6533 2857 return $z;
6534 }
6535  
6536 # (status, 1) at the end of a member, (status, 0) to go on, (status, undef) on
6537 # corrupt data. Z_BUF_ERROR is "no progress possible", which with LimitOutput
6538 # and ConsumeInput is how the stream asks for more input, not an error.
6539 4115 ▶sub INFLATE {
Subroutine INFLATE: called CC=4 SCAR=13.9
6540 4115 my ($self) = @_;
6541 4115 my $status = $self->{z}->inflate($self->{in}, $_[1]);
6542 4115 ▶ return ($status, 1) if $status == Compress::Raw::Zlib::Z_STREAM_END();
Branch: 1 branch
BranchTrueFalseTotal
if $status == Compress::Raw::Zlib::Z_STREAM_END() 2255 1860 4115
6543 1860 ▶ return ($status, 0) if $status == Compress::Raw::Zlib::Z_OK()
Branch: 1 branch
BranchTrueFalseTotal
if $status == Compress::Raw::Zlib::Z_OK() or $status == Compress::Raw::Zlib::Z_BUF_ERROR() 1857 3 1860
Condition100%3/3
$status == Compress::Raw::Zlib::Z_OK() or $status == Compress::Raw::Zlib::Z_BUF_ERROR()
l!l&&r!l&&!r
60412533
MC/DC100%2/2
$status == Compress::Raw::Zlib::Z_OK() or $status == Compress::Raw::Zlib::Z_BUF_ERROR()
$status == Compress::Raw::Zlib::Z_OK() $status == Compress::Raw::Zlib::Z_BUF_ERROR()
Truth table100%3/3
A or B
A= $status == Compress::Raw::Zlib::Z_OK()B= $status == Compress::Raw::Zlib::Z_BUF_ERROR()
ABresult
1X1
011
000
6544 || $status == Compress::Raw::Zlib::Z_BUF_ERROR();
6545 3 return ("$status", undef);
6546 }
6547  
6548 package Stats::LikeR::_Bunzip2;
6549 497 our @ISA = ('Stats::LikeR::_Decompress');
6550  
6551 476 ▶sub CODEC { 'bzip2' }
Subroutine CODEC: called CC=1 SCAR=0.0
6552 1037 ▶sub MAGIC { 'BZh' }
Subroutine MAGIC: called CC=1 SCAR=0.0
6553  
6554 936 ▶sub NEW {
Subroutine NEW: called CC=2 SCAR=7.1
6555 # appendOutput 0, consumeInput 1, small 0, verbosity 0, limitOutput 1:
6556 # the same streaming contract as _Gunzip's.
6557 936 my ($z, $err) = Compress::Raw::Bunzip2->new(0, 1, 0, 0, 1);
6558 936 ▶ die "read_table: could not start bunzipping: $err\n" unless $z;
Branch: 1 branch
BranchTrueFalseTotal
unless $z 0 936 936
6559 936 return $z;
6560 }
6561  
6562 2033 ▶sub INFLATE {
Subroutine INFLATE: called CC=3 SCAR=11.3
6563 2033 my ($self) = @_;
6564 2033 my $status = $self->{z}->bzinflate($self->{in}, $_[1]);
6565 2033 ▶ return ($status, 1) if $status == Compress::Raw::Bzip2::BZ_STREAM_END();
Branch: 1 branch
BranchTrueFalseTotal
if $status == Compress::Raw::Bzip2::BZ_STREAM_END() 449 1584 2033
6566 1584 ▶ return ($status, 0) if $status == Compress::Raw::Bzip2::BZ_OK();
Branch: 1 branch
BranchTrueFalseTotal
if $status == Compress::Raw::Bzip2::BZ_OK() 1584 0 1584
6567 0 return ("$status", undef);
6568 }
6569  
6570 # The PerlIO::via layers write_table writes a .gz or .bz2 file through. The XS
6571 # opens the file as for plain text and pushes :raw:via(<class>):perlio onto it
6572 # (:crlf in place of :perlio where perl is a CRLF shop, so that Windows gets
6573 # the CRLF a plain file would), and the rows reach WRITE 8 KB at a time from
6574 # that buffer above rather than a field or a character at a time.
6575 #
6576 # The end of the stream -- zlib's final block and the gzip trailer, bzip2's
6577 # end-of-stream marker -- cannot be written from FLUSH or CLOSE. The :perlio
6578 # buffer above calls FLUSH after every 8 KB it hands down, so FLUSH does not
6579 # mean "done", and PerlIO::via calls CLOSE only once the layer below has been
6580 # closed (PerlIOVia_close() runs PerlIOBase_close() first; perl 5.10.1 and
6581 # 5.42.0 ext/PerlIO-via/via.xs). So once the last row is written, the XS pops
6582 # the :perlio layer and then this one, and POPPED finishes the stream while
6583 # the file is still open.
6584 #
6585 # A write that croaks partway closes the handle without the pops, and then
6586 # CLOSE runs before POPPED: the stream is left unfinished, on purpose, so that
6587 # the file is a truncated .gz that read_table refuses rather than a complete
6588 # one holding half the table.
6589 #
6590 # A failure goes back the PerlIO way, as -1 from PUSHED or WRITE; the XS turns
6591 # it into a croak naming the file. PUSHED leaves its reason in $error.
6592 package Stats::LikeR::_Compress;
6593  
6594 497 our $error;
6595  
6596 34 ▶sub PUSHED {
Subroutine PUSHED: called CC=3 SCAR=14.2
6597 34 my ($class) = @_;
6598 34 my $z = eval { $class->NEW };
6599 34 ▶ if (!$z) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($z) 0 34 34
6600 0 ▶ $error = $@ || 'could not start compressing';
Condition0%0/2
$@ || 'could not start compressing'
l!l
00
MC/DC0%0/1
$@ || 'could not start compressing'
$@
Truth table0%0/2
A
A= $@
Aresult
11
00
6601 0 $error =~ s/\n\z//;
6602 0 return -1;
6603 }
6604 34 return bless { z => $z, closed => 0 }, $class;
6605 }
6606  
6607 326 ▶sub WRITE {
Subroutine WRITE: called CC=4 SCAR=13.9
6608 326 my ($self, $buf, $fh) = @_;
6609 326 my $out = '';
6610 326 ▶ return -1 unless $self->DEFLATE($buf, $out);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->DEFLATE($buf, $out) 0 326 326
6611 326 ▶ return -1 if length $out && !print {$fh} $out;
Branch: 1 branch
BranchTrueFalseTotal
if length $out and not print({$fh;} $out) 3 323 326
Condition100%3/3
length $out and not print({$fh;} $out)
!ll&&!rl&&r
280433
MC/DC100%2/2
length $out and not print({$fh;} $out)
length $out not print({$fh;} $out)
Truth table100%3/3
A and not(B)
A= length $outB= not print({$fh;} $out)
ABresult
0X0
100
111
6612 323 return length $buf;
6613 }
6614  
6615 390 ▶sub FLUSH { 0 }
Subroutine FLUSH: called CC=1 SCAR=0.0
6616  
6617 4 ▶sub CLOSE {
Subroutine CLOSE: called CC=1 SCAR=0.0
6618 4 $_[0]{closed} = 1;
6619 4 return 0;
6620 }
6621  
6622 34 ▶sub POPPED {
Subroutine POPPED: called CC=5 SCAR=16.7
6623 34 my ($self, $fh) = @_;
6624 34 ▶ return if $self->{closed} || !$self->{z};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"closed"} or not $self->{"z"} 4 30 34
Condition66%2/3
$self->{"closed"} or not $self->{"z"}
l!l&&r!l&&!r
4030
MC/DC50%1/2
$self->{"closed"} or not $self->{"z"}
$self->{"closed"} not $self->{"z"}
Truth table66%2/3
A or not(B)
A= $self->{"closed"}B= not $self->{"z"}
ABresult
1X1
011
000
6625 30 my $out = '';
6626 # a failure here shows up as the error flag or the close of the layer
6627 # below, which is what the XS checks next
6628 30 ▶ print {$fh} $out if $self->FINISH($out) && length $out;
Branch: 1 branch
BranchTrueFalseTotal
if $self->FINISH($out) and length $out 30 0 30
Condition33%1/3
$self->FINISH($out) and length $out
!ll&&!rl&&r
0030
MC/DC0%0/2
$self->FINISH($out) and length $out
$self->FINISH($out) length $out
Truth table33%1/3
A and B
A= $self->FINISH($out)B= length $out
ABresult
0X0
100
111
6629 30 $self->{z} = undef;
6630 30 return;
6631 }
6632  
6633 package Stats::LikeR::_Gzip;
6634 497 our @ISA = ('Stats::LikeR::_Compress');
6635  
6636 # Level 6 is Z_DEFAULT_COMPRESSION, the level of gzip(1) and of R's
6637 # gzfile(compression = 6). zlib's gzip wrapper writes a header with no name
6638 # and an mtime of 0, so the same table always makes the same bytes.
6639 19 ▶sub NEW {
Subroutine NEW: called CC=2 SCAR=7.0
6640 19 require Compress::Raw::Zlib;
6641 19 my ($z, $err) = Compress::Raw::Zlib::Deflate->new(
6642 -WindowBits => Compress::Raw::Zlib::WANT_GZIP(),
6643 -Level => Compress::Raw::Zlib::Z_DEFAULT_COMPRESSION());
6644 19 ▶ die "could not start gzip compression: $err\n" unless $z;
Branch: 1 branch
BranchTrueFalseTotal
unless $z 0 19 19
6645 19 return $z;
6646 }
6647  
6648 165 ▶sub DEFLATE {
Subroutine DEFLATE: called CC=1 SCAR=0.0
6649 165 $_[0]{z}->deflate($_[1], $_[2]) == Compress::Raw::Zlib::Z_OK();
6650 }
6651  
6652 17 ▶sub FINISH {
Subroutine FINISH: called CC=1 SCAR=0.0
6653 17 $_[0]{z}->flush($_[1]) == Compress::Raw::Zlib::Z_OK();
6654 }
6655  
6656 package Stats::LikeR::_Bzip2;
6657 497 our @ISA = ('Stats::LikeR::_Compress');
6658  
6659 # appendOutput 1, blockSize100k 9, workfactor 0 (the default, 30), verbosity
6660 # 0. 9 is bzip2(1)'s default and R's bzfile(compression = 9). Output is
6661 # appended because DEFLATE may make it in two calls; WRITE and POPPED each pass
6662 # an empty buffer.
6663 15 ▶sub NEW {
Subroutine NEW: called CC=2 SCAR=7.0
6664 15 require Compress::Raw::Bzip2;
6665 15 my ($z, $err) = Compress::Raw::Bzip2->new(1, 9, 0, 0);
6666 15 ▶ die "could not start bzip2 compression: $err\n" unless $z;
Branch: 1 branch
BranchTrueFalseTotal
unless $z 0 15 15
6667 15 return $z;
6668 }
6669  
6670 # bzip2 writes nothing until it has a whole 900 KB block, so a write that
6671 # croaked before then would leave an empty file, and an empty file reads as an
6672 # empty table rather than a broken one. The first WRITE therefore ends its
6673 # block at once, which puts the stream header and that block on disk, and a
6674 # file that is never finished is then a truncated bzip2 file, as a gzip one
6675 # already is from its first write. It costs one short block at the start.
6676 161 ▶sub DEFLATE {
Subroutine DEFLATE: called CC=3 SCAR=11.0
6677 161 my ($self) = @_;
6678 161 ▶ return 0 unless $self->{z}->bzdeflate($_[1], $_[2])
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"z"}->bzdeflate($_[1], $_[2]) == Compress::Raw::Bzip2::BZ_RUN_OK() 0 161 161
6679 == Compress::Raw::Bzip2::BZ_RUN_OK();
6680 161 ▶ return 1 if $self->{started}++;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"started"}++ 146 15 161
6681 15 return $self->{z}->bzflush($_[2]) == Compress::Raw::Bzip2::BZ_RUN_OK();
6682 }
6683  
6684 13 ▶sub FINISH {
Subroutine FINISH: called CC=1 SCAR=0.0
6685 13 $_[0]{z}->bzclose($_[1]) == Compress::Raw::Bzip2::BZ_STREAM_END();
6686 }
6687  
6688 package Stats::LikeR;
6689  
6690 497 1;
6691  
6692 __END__
↑ Summary LikeR.xs »