| 1 | package Math::SimpleHisto::XS::CLI; | ||||||||||||||||||||||||||||||||
| 2 | 1 | ▶ | use 5.008001; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||
| 6 | 1 | our $VERSION = '1.07'; | |||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use constant BATCHSIZE => 1000; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use Carp 'croak'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use Exporter; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||
| 12 | 1 | our @ISA = qw(Exporter); | |||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||
| 14 | 1 | our @EXPORT_OK = qw( | |||||||||||||||||||||||||||||||
| 15 | histogram_from_dumps_fh | ||||||||||||||||||||||||||||||||
| 16 | histogram_from_random_data | ||||||||||||||||||||||||||||||||
| 17 | histogram_from_fh | ||||||||||||||||||||||||||||||||
| 18 | histogram_slurp_from_fh | ||||||||||||||||||||||||||||||||
| 19 | minmax | ||||||||||||||||||||||||||||||||
| 20 | display_histogram_using_soot | ||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||
| 22 | intuit_ascii_style | ||||||||||||||||||||||||||||||||
| 23 | intuit_output_size | ||||||||||||||||||||||||||||||||
| 24 | draw_ascii_histogram | ||||||||||||||||||||||||||||||||
| 25 | print_hist_stats | ||||||||||||||||||||||||||||||||
| 26 | ); | ||||||||||||||||||||||||||||||||
| 27 | 1 | our %EXPORT_TAGS = ( | |||||||||||||||||||||||||||||||
| 28 | 'all' => \@EXPORT_OK, | ||||||||||||||||||||||||||||||||
| 29 | ); | ||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||
| 31 | 1 | ▶ | use Math::SimpleHisto::XS; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||
| 33 | 0 | ▶ | sub histogram_from_dumps_fh { | ||||||||||||||||||||||||||||||
|
Subroutine
histogram_from_dumps_fh: not called CC=8 SCAR=42.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 34 | 0 | my ($fh) = @_; | |||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||
| 36 | 0 | my $hist; | |||||||||||||||||||||||||||||||
| 37 | my $tmphist; | ||||||||||||||||||||||||||||||||
| 38 | #require Math::SimpleHisto::XS::Named; # TODO implement & test using this | ||||||||||||||||||||||||||||||||
| 39 | 0 | ▶ | while (my $dump = <$fh>) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 40 | 0 | ▶ | next if not $dump =~ /\S/; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 41 | 0 | foreach my $type (qw(json yaml simple)) { | |||||||||||||||||||||||||||||||
| 42 | 0 | eval {$tmphist = Math::SimpleHisto::XS->new_from_dump($type, $dump);}; | |||||||||||||||||||||||||||||||
| 43 | 0 | ▶ | last if defined $tmphist; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||||||
| 45 | 0 | ▶ | if (defined $tmphist) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 46 | 0 | ▶ | if ($hist) { $hist->add_histogram($tmphist) } | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 47 | 0 | else { $hist = $tmphist } | |||||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||||||
| 50 | 0 | ▶ | Carp::croak("Could not recreate histogram from input histogram dump string") | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 51 | if not defined $hist; | ||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||
| 53 | 0 | return $hist; | |||||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||
| 56 | 0 | ▶ | sub histogram_from_random_data { | ||||||||||||||||||||||||||||||
|
Subroutine
histogram_from_random_data: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 57 | 0 | my ($histopt, $random_samples) = @_; | |||||||||||||||||||||||||||||||
| 58 | 0 | my %opt = %$histopt; | |||||||||||||||||||||||||||||||
| 59 | 0 | ▶ | $opt{min} ||= 0; | ||||||||||||||||||||||||||||||
|
Condition0%0/2
$opt{"min"} ||= 0
MC/DC0%0/1
$opt{"min"} ||= 0
$opt{"min"}
Truth table0%0/2
A
A= $opt{"min"}
| |||||||||||||||||||||||||||||||||
| 60 | 0 | ▶ | $opt{max} ||= 1; | ||||||||||||||||||||||||||||||
|
Condition0%0/2
$opt{"max"} ||= 1
MC/DC0%0/1
$opt{"max"} ||= 1
$opt{"max"}
Truth table0%0/2
A
A= $opt{"max"}
| |||||||||||||||||||||||||||||||||
| 61 | 0 | ▶ | $random_samples = 1000 if not $random_samples; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||
| 63 | my $hist = Math::SimpleHisto::XS->new( | ||||||||||||||||||||||||||||||||
| 64 | min => $opt{min}, | ||||||||||||||||||||||||||||||||
| 65 | max => $opt{max}, | ||||||||||||||||||||||||||||||||
| 66 | nbins => $opt{nbins}, | ||||||||||||||||||||||||||||||||
| 67 | 0 | ); | |||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||
| 69 | 0 | my $min = $hist->min; | |||||||||||||||||||||||||||||||
| 70 | 0 | my $width = $hist->width; | |||||||||||||||||||||||||||||||
| 71 | 0 | $hist->fill($min + rand($width)) for 1..$random_samples; | |||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||
| 73 | 0 | return $hist; | |||||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||
| 76 | 0 | ▶ | sub histogram_from_fh { | ||||||||||||||||||||||||||||||
|
Subroutine
histogram_from_fh: not called CC=18 SCAR=58.3
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 77 | 0 | my ($histopt, $fh, $hist) = @_; | |||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||
| 79 | 0 | ▶ | $hist ||= Math::SimpleHisto::XS->new(map {$_ => $histopt->{$_}} qw(nbins min max)); | ||||||||||||||||||||||||||||||
|
Condition0%0/3
$hist ||= "Math::SimpleHisto::XS"->new(map({$_, $histopt->{$_};} "nbins", "min", "max"))
MC/DC0%0/2
$hist ||= "Math::SimpleHisto::XS"->new(map({$_, $histopt->{$_};} "nbins", "min", "max"))
$hist
"Math::SimpleHisto::XS"->new(map({$_, $histopt->{$_};} "nbins", "min", "max"))
Truth table0%0/3
A ||= B
A= $histB= "Math::SimpleHisto::XS"->new(map({$_, $histopt->{$_};} "nbins", "min", "max"))
| |||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||
| 81 | 0 | my $pos_weight = $histopt->{xw}; | |||||||||||||||||||||||||||||||
| 82 | 0 | my (@coords, @weights); | |||||||||||||||||||||||||||||||
| 83 | 0 | my $i = 0; | |||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||
| 85 | 0 | my ($rbits); | |||||||||||||||||||||||||||||||
| 86 | 0 | my $step_size = $histopt->{stepsize}; | |||||||||||||||||||||||||||||||
| 87 | 0 | ▶ | if ($step_size) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 88 | 0 | $rbits = ''; | |||||||||||||||||||||||||||||||
| 89 | 0 | vec($rbits, fileno($fh), 1) = 1; | |||||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||
| 92 | 0 | while (1) { | |||||||||||||||||||||||||||||||
| 93 | 0 | ▶ | if ($step_size) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 94 | 0 | my ($havedata, undef) = select($rbits, undef, undef, 0.1); | |||||||||||||||||||||||||||||||
| 95 | 0 | ▶ | if (not $havedata) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 96 | 0 | ▶ | last if $i >= 1; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 97 | 0 | redo; | |||||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||||||
| 99 | 0 | $_ = <$fh>; | |||||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||||||
| 101 | else { | ||||||||||||||||||||||||||||||||
| 102 | 0 | $_ = <$fh>; | |||||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||||||
| 104 | 0 | ▶ | last if not defined $_; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 105 | 0 | chomp; | |||||||||||||||||||||||||||||||
| 106 | 0 | my @row = split " ", $_; | |||||||||||||||||||||||||||||||
| 107 | 0 | ++$i; | |||||||||||||||||||||||||||||||
| 108 | 0 | ▶ | if ($pos_weight) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 109 | 0 | ▶ | push @{ ($i % 2) ? \@coords : \@weights }, $_ for split " ", $_; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||||||
| 111 | else { | ||||||||||||||||||||||||||||||||
| 112 | 0 | push @coords, split " ", $_; | |||||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||||||
| 114 | 0 | ▶ | if (@coords >= BATCHSIZE) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 115 | 0 | my $tmp; | |||||||||||||||||||||||||||||||
| 116 | 0 | ▶ | $tmp = pop(@weights) if @coords != @weights; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 117 | 0 | ▶ | $hist->fill($pos_weight ? (\@coords, \@weights) : (\@coords)); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||
| 119 | 0 | @coords = (); | |||||||||||||||||||||||||||||||
| 120 | 0 | ▶ | @weights = (defined($tmp) ? ($tmp) : ()); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||
| 123 | 0 | ▶ | last if $step_size and $i >= $step_size; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$step_size and $i >= $step_size
MC/DC0%0/2
$step_size and $i >= $step_size
$step_size
$i >= $step_size
Truth table0%0/3
A and B
A= $step_sizeB= $i >= $step_size
| |||||||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||
| 126 | 0 | ▶ | $hist->fill($pos_weight ? (\@coords, \@weights) : (\@coords)) | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 127 | if @coords; | ||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||
| 129 | 0 | return $hist; | |||||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||
| 132 | # modifies input options | ||||||||||||||||||||||||||||||||
| 133 | 0 | ▶ | sub histogram_slurp_from_fh { | ||||||||||||||||||||||||||||||
|
Subroutine
histogram_slurp_from_fh: not called CC=9 SCAR=45.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 134 | 0 | my ($histopt, $fh) = @_; | |||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||
| 136 | 0 | my $pos_weight = $histopt->{xw}; | |||||||||||||||||||||||||||||||
| 137 | 0 | my $hist; | |||||||||||||||||||||||||||||||
| 138 | 0 | my (@coords, @weights); | |||||||||||||||||||||||||||||||
| 139 | 0 | my $i = 0; | |||||||||||||||||||||||||||||||
| 140 | 0 | ▶ | while (<STDIN>) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 141 | 0 | chomp; | |||||||||||||||||||||||||||||||
| 142 | 0 | s/^\s+//; s/\s+$//; | |||||||||||||||||||||||||||||||
| 143 | 0 | ▶ | if ($pos_weight) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 144 | 0 | ▶ | push @{ (++$i % 2) ? \@coords : \@weights }, $_ for split " ", $_; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||||||
| 146 | else { | ||||||||||||||||||||||||||||||||
| 147 | 0 | push @coords, split " ", $_; | |||||||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||
| 151 | # Without input and configured histogram boundaries, we can't make one | ||||||||||||||||||||||||||||||||
| 152 | # TODO: should this be silent "success" or an empty histogram (for dump | ||||||||||||||||||||||||||||||||
| 153 | # output mode) or an exception? | ||||||||||||||||||||||||||||||||
| 154 | 0 | ▶ | exit(0) if not @coords; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 155 | 0 | my ($min, $max) = minmax(@coords); | |||||||||||||||||||||||||||||||
| 156 | 0 | ▶ | $histopt->{min} = $min if not defined $histopt->{min}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 157 | 0 | ▶ | $histopt->{max} = $max if not defined $histopt->{max}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||
| 159 | 0 | $hist = Math::SimpleHisto::XS->new(map {$_ => $histopt->{$_}} qw(nbins min max)); | |||||||||||||||||||||||||||||||
| 160 | 0 | ▶ | $hist->fill($pos_weight ? (\@coords, \@weights) : (\@coords)); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||
| 162 | 0 | return $hist; | |||||||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||
| 165 | 0 | ▶ | sub minmax { | ||||||||||||||||||||||||||||||
|
Subroutine
minmax: not called CC=6 SCAR=37.4
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 166 | 0 | my ($min, $max); | |||||||||||||||||||||||||||||||
| 167 | 0 | for (@_) { | |||||||||||||||||||||||||||||||
| 168 | 0 | ▶ | $min = $_ if not defined $min or $min > $_; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not defined $min or $min > $_
MC/DC0%0/2
not defined $min or $min > $_
not defined $min
$min > $_
Truth table0%0/3
not(A) or B
A= not defined $minB= $min > $_
| |||||||||||||||||||||||||||||||||
| 169 | 0 | ▶ | $max = $_ if not defined $max or $max < $_; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not defined $max or $max < $_
MC/DC0%0/2
not defined $max or $max < $_
not defined $max
$max < $_
Truth table0%0/3
not(A) or B
A= not defined $maxB= $max < $_
| |||||||||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||||||
| 171 | 0 | return($min, $max); | |||||||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||||||
| 174 | 0 | ▶ | sub display_histogram_using_soot { | ||||||||||||||||||||||||||||||
|
Subroutine
display_histogram_using_soot: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 175 | 0 | my ($hist) = @_; | |||||||||||||||||||||||||||||||
| 176 | 0 | my $h = $hist->to_soot; | |||||||||||||||||||||||||||||||
| 177 | 0 | my $cv = TCanvas->new; | |||||||||||||||||||||||||||||||
| 178 | 0 | $h->Draw(); | |||||||||||||||||||||||||||||||
| 179 | 0 | my $app = $SOOT::gApplication = $SOOT::gApplication; # silence warnings | |||||||||||||||||||||||||||||||
| 180 | 0 | $app->Run(); | |||||||||||||||||||||||||||||||
| 181 | 0 | exit; | |||||||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||||||
| 185 | 1 | our %AsciiStyles = ( | |||||||||||||||||||||||||||||||
| 186 | '-' => {character => '-', end_character => '>'}, | ||||||||||||||||||||||||||||||||
| 187 | '=' => {character => '=', end_character => '>'}, | ||||||||||||||||||||||||||||||||
| 188 | '~' => {character => '~', end_character => '>'}, | ||||||||||||||||||||||||||||||||
| 189 | ); | ||||||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||||||
| 191 | # Determine the style to use for drawing the histogram | ||||||||||||||||||||||||||||||||
| 192 | 0 | ▶ | sub intuit_ascii_style { | ||||||||||||||||||||||||||||||
|
Subroutine
intuit_ascii_style: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 193 | 0 | my ($style_option) = @_; | |||||||||||||||||||||||||||||||
| 194 | 0 | ▶ | $style_option = '~' if not defined $style_option; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 195 | 0 | ▶ | if (not exists $AsciiStyles{$style_option}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 196 | 0 | ▶ | if (length($style_option) == 1) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 197 | 0 | $AsciiStyles{$style_option} = {character => $style_option, end_character => $style_option}; | |||||||||||||||||||||||||||||||
| 198 | } | ||||||||||||||||||||||||||||||||
| 199 | else { | ||||||||||||||||||||||||||||||||
| 200 | 0 | die "Invalid histogram style '$style_option'. Valid styles: '" | |||||||||||||||||||||||||||||||
| 201 | . join("', '", keys %AsciiStyles), "' and any single character.\n"; | ||||||||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||||||
| 203 | } | ||||||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||||||
| 205 | 0 | my $styledef = $AsciiStyles{$style_option}; | |||||||||||||||||||||||||||||||
| 206 | 0 | return $styledef; | |||||||||||||||||||||||||||||||
| 207 | } | ||||||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||||||
| 210 | 0 | ▶ | sub intuit_output_size { | ||||||||||||||||||||||||||||||
|
Subroutine
intuit_output_size: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 211 | 0 | my ($ofh) = @_; | |||||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||||||
| 213 | 0 | ▶ | $ofh ||= \*STDOUT; | ||||||||||||||||||||||||||||||
|
Condition0%0/2
$ofh ||= \*STDOUT
MC/DC0%0/1
$ofh ||= \*STDOUT
$ofh
Truth table0%0/2
A
A= $ofh
| |||||||||||||||||||||||||||||||||
| 214 | # figure out output width | ||||||||||||||||||||||||||||||||
| 215 | 0 | my ($terminal_columns, $terminal_rows); | |||||||||||||||||||||||||||||||
| 216 | 0 | ▶ | if (-t $ofh) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 217 | 0 | ($terminal_columns, $terminal_rows) = Term::Size::chars($ofh); | |||||||||||||||||||||||||||||||
| 218 | } | ||||||||||||||||||||||||||||||||
| 219 | else { | ||||||||||||||||||||||||||||||||
| 220 | 0 | $terminal_columns = 80; | |||||||||||||||||||||||||||||||
| 221 | 0 | $terminal_rows = 10; | |||||||||||||||||||||||||||||||
| 222 | } | ||||||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||||||
| 224 | 0 | return ($terminal_columns, $terminal_rows); | |||||||||||||||||||||||||||||||
| 225 | } | ||||||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||||||
| 227 | 0 | ▶ | sub print_hist_stats { | ||||||||||||||||||||||||||||||
|
Subroutine
print_hist_stats: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 228 | 0 | my ($ofh, $hist, $histopt) = @_; | |||||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||||||
| 230 | 0 | ▶ | my $v_total_width = $histopt->{width} || (intuit_output_size($ofh))[0] - 2; | ||||||||||||||||||||||||||||||
|
Condition0%0/3
$histopt->{'width'} || (intuit_output_size($ofh))[0] - 2
MC/DC0%0/2
$histopt->{'width'} || (intuit_output_size($ofh))[0] - 2
$histopt->{'width'}
(intuit_output_size($ofh))[0] - 2
Truth table0%0/3
A || B
A= $histopt->{'width'}B= (intuit_output_size($ofh))[0] - 2
| |||||||||||||||||||||||||||||||||
| 231 | # Total: X Fills: X Mean: X Median: X | ||||||||||||||||||||||||||||||||
| 232 | 0 | my ($tot, $nfills, $mean, $median) = map $hist->$_, qw(total nfills mean median); | |||||||||||||||||||||||||||||||
| 233 | 0 | my $str = sprintf("Total: %f NFills: %u Mean: %f Median %f\n", $tot, $nfills, $mean, $median); | |||||||||||||||||||||||||||||||
| 234 | 0 | $str = substr($str, 0, $v_total_width); | |||||||||||||||||||||||||||||||
| 235 | 0 | print $ofh $str; | |||||||||||||||||||||||||||||||
| 236 | } | ||||||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||||||
| 238 | # relevant options: | ||||||||||||||||||||||||||||||||
| 239 | # - sort | ||||||||||||||||||||||||||||||||
| 240 | # - width | ||||||||||||||||||||||||||||||||
| 241 | # - min | ||||||||||||||||||||||||||||||||
| 242 | # - max | ||||||||||||||||||||||||||||||||
| 243 | # - numeric-format | ||||||||||||||||||||||||||||||||
| 244 | # - show-numeric | ||||||||||||||||||||||||||||||||
| 245 | # - timestamp | ||||||||||||||||||||||||||||||||
| 246 | # - log | ||||||||||||||||||||||||||||||||
| 247 | # - style | ||||||||||||||||||||||||||||||||
| 248 | 0 | ▶ | sub draw_ascii_histogram { | ||||||||||||||||||||||||||||||
|
Subroutine
draw_ascii_histogram: not called CC=29 SCAR=67.7
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 249 | 0 | my ($ofh, $rows, $histopt) = @_; | |||||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||||||
| 251 | 0 | my $convert_timestamps = $histopt->{timestamp}; | |||||||||||||||||||||||||||||||
| 252 | 0 | my $show_numeric = $histopt->{"show-numeric"}; | |||||||||||||||||||||||||||||||
| 253 | 0 | my $numeric_format = $histopt->{"numeric-format"}; | |||||||||||||||||||||||||||||||
| 254 | 0 | my $logscale = $histopt->{log}; | |||||||||||||||||||||||||||||||
| 255 | 0 | my $styledef = $histopt->{style}; | |||||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||||||
| 257 | # extract min/max/width info from input data | ||||||||||||||||||||||||||||||||
| 258 | # The $v_ prefixed variables below refer to "visible" widths in columns. | ||||||||||||||||||||||||||||||||
| 259 | 0 | my $v_desc_width = 0; | |||||||||||||||||||||||||||||||
| 260 | 0 | my $v_numeric_value_width = 0; | |||||||||||||||||||||||||||||||
| 261 | 0 | my $hist_total = 0; | |||||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||||||
| 263 | 0 | my ($hist_max, $hist_min); | |||||||||||||||||||||||||||||||
| 264 | 0 | foreach my $row (@$rows) { | |||||||||||||||||||||||||||||||
| 265 | 0 | my ($description, $value) = @$row; | |||||||||||||||||||||||||||||||
| 266 | 0 | ▶ | $row->[0] = $description = localtime(int($description)) if $convert_timestamps; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||||||
| 268 | 0 | my $formatted_value = sprintf($numeric_format, $value); | |||||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||||||
| 270 | 0 | ▶ | $v_desc_width = length($description) if length($description) > $v_desc_width; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 271 | 0 | ▶ | $v_numeric_value_width = length($formatted_value) if length($formatted_value) > $v_numeric_value_width; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 272 | 0 | ▶ | $hist_min = $value if !defined $hist_min or $value < $hist_min; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not defined $hist_min or $value < $hist_min
MC/DC0%0/2
not defined $hist_min or $value < $hist_min
not defined $hist_min
$value < $hist_min
Truth table0%0/3
not(A) or B
A= not defined $hist_minB= $value < $hist_min
| |||||||||||||||||||||||||||||||||
| 273 | 0 | ▶ | $hist_max = $value if !defined $hist_max or $value > $hist_max; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not defined $hist_max or $value > $hist_max
MC/DC0%0/2
not defined $hist_max or $value > $hist_max
not defined $hist_max
$value > $hist_max
Truth table0%0/3
not(A) or B
A= not defined $hist_maxB= $value > $hist_max
| |||||||||||||||||||||||||||||||||
| 274 | 0 | $hist_total += $value; | |||||||||||||||||||||||||||||||
| 275 | # extend each row by the formatted numeric value -- just in case. | ||||||||||||||||||||||||||||||||
| 276 | 0 | ▶ | push @$row, $show_numeric ? $formatted_value : ''; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 277 | } | ||||||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||||||
| 279 | # sort by value if desired | ||||||||||||||||||||||||||||||||
| 280 | 0 | ▶ | @$rows = sort {$a->[1] <=> $b->[1]} @$rows if $histopt->{sort}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||||||
| 282 | 0 | ▶ | my $v_total_width = $histopt->{width} || (intuit_output_size($ofh))[0] - 2; | ||||||||||||||||||||||||||||||
|
Condition0%0/3
$histopt->{'width'} || (intuit_output_size($ofh))[0] - 2
MC/DC0%0/2
$histopt->{'width'} || (intuit_output_size($ofh))[0] - 2
$histopt->{'width'}
(intuit_output_size($ofh))[0] - 2
Truth table0%0/3
A || B
A= $histopt->{'width'}B= (intuit_output_size($ofh))[0] - 2
| |||||||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||||||
| 284 | 0 | ▶ | if ($v_total_width < $v_desc_width + 3) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 285 | 0 | warn "Terminal or desired width is insufficient.\n"; | |||||||||||||||||||||||||||||||
| 286 | 0 | $v_total_width = $v_desc_width + 3; | |||||||||||||||||||||||||||||||
| 287 | } | ||||||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||||||
| 289 | 0 | ▶ | $v_numeric_value_width = $show_numeric ? $v_numeric_value_width+2 : 0; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 290 | # The total output width is comprised of the bin description, possibly | ||||||||||||||||||||||||||||||||
| 291 | # the width of the numeric bin content, and the width of the actual | ||||||||||||||||||||||||||||||||
| 292 | # histogram. | ||||||||||||||||||||||||||||||||
| 293 | 0 | my $v_hist_width = $v_total_width - $v_desc_width - $v_numeric_value_width - 3; | |||||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||||||
| 295 | # figure out the range of values in the visible part of the histogram | ||||||||||||||||||||||||||||||||
| 296 | 0 | ▶ | my $min_display_value = $histopt->{min} || 0; | ||||||||||||||||||||||||||||||
|
Condition0%0/2
$histopt->{'min'} || 0
MC/DC0%0/1
$histopt->{'min'} || 0
$histopt->{'min'}
Truth table0%0/2
A
A= $histopt->{'min'}
| |||||||||||||||||||||||||||||||||
| 297 | 0 | ▶ | if ($min_display_value =~ /^auto$/i) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 298 | 0 | $min_display_value = $hist_min; | |||||||||||||||||||||||||||||||
| 299 | } | ||||||||||||||||||||||||||||||||
| 300 | 0 | ▶ | $min_display_value = log($min_display_value||$hist_min*0.99||1e-9) if $logscale; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/5
$min_display_value || $hist_min * 0.99 || 1e-09
$min_display_value or $hist_min * "0.99"
MC/DC0%0/2
$min_display_value || $hist_min * 0.99 || 1e-09
$min_display_value
$hist_min * "0.99"
Truth table0%0/3
A or B
A= $min_display_valueB= $hist_min * "0.99"
| |||||||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||||||
| 302 | 0 | my $max_display_value = $histopt->{max}; | |||||||||||||||||||||||||||||||
| 303 | 0 | ▶ | if (not defined $max_display_value or $max_display_value =~ /^auto$/) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition0%0/3
not defined $max_display_value or $max_display_value =~ /^auto$/
MC/DC0%0/2
not defined $max_display_value or $max_display_value =~ /^auto$/
not defined $max_display_value
$max_display_value =~ /^auto$/
Truth table0%0/3
not(A) or B
A= not defined $max_display_valueB= $max_display_value =~ /^auto$/
| |||||||||||||||||||||||||||||||||
| 304 | 0 | $max_display_value = $hist_max; | |||||||||||||||||||||||||||||||
| 305 | } | ||||||||||||||||||||||||||||||||
| 306 | elsif ($max_display_value =~ /^total$/i) { | ||||||||||||||||||||||||||||||||
| 307 | 0 | $max_display_value = $hist_total; | |||||||||||||||||||||||||||||||
| 308 | } | ||||||||||||||||||||||||||||||||
| 309 | 0 | ▶ | $max_display_value = log($max_display_value) if $logscale; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||||||
| 311 | 0 | my $display_value_range = $max_display_value - $min_display_value; | |||||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||||||
| 313 | # format the output | ||||||||||||||||||||||||||||||||
| 314 | 0 | my $format = "%${v_desc_width}s: %${v_numeric_value_width}s|%-${v_hist_width}s|\n"; | |||||||||||||||||||||||||||||||
| 315 | 0 | my $hchar_body = $styledef->{character}; | |||||||||||||||||||||||||||||||
| 316 | 0 | my $hchar_end = $styledef->{end_character}; | |||||||||||||||||||||||||||||||
| 317 | 0 | my $hchar_end_len = length($hchar_end); | |||||||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||||||
| 319 | # The actual output loop | ||||||||||||||||||||||||||||||||
| 320 | 0 | foreach my $row (@$rows) { | |||||||||||||||||||||||||||||||
| 321 | 0 | my ($desc, $value, $formatted_value) = @$row; | |||||||||||||||||||||||||||||||
| 322 | 0 | ▶ | $value = log($value||1e-15) if $logscale; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$value || 1e-15
MC/DC0%0/1
$value || 1e-15
$value
Truth table0%0/2
A
A= $value
| |||||||||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||||||
| 324 | 0 | my $hlen = int(($value-$min_display_value) / $display_value_range * $v_hist_width); | |||||||||||||||||||||||||||||||
| 325 | 0 | ▶ | $hlen = 0 if $hlen < 0; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 326 | 0 | ▶ | $hlen = $v_hist_width if $hlen > $v_hist_width; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||||||
| 328 | 0 | ▶ | if ($hlen >= $hchar_end_len) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 329 | 0 | printf($format, $desc, $formatted_value, ($hchar_body x ($hlen-$hchar_end_len)) . $hchar_end); | |||||||||||||||||||||||||||||||
| 330 | } | ||||||||||||||||||||||||||||||||
| 331 | else { | ||||||||||||||||||||||||||||||||
| 332 | 0 | printf($format, $desc, $formatted_value, ($hchar_body x $hlen)); | |||||||||||||||||||||||||||||||
| 333 | } | ||||||||||||||||||||||||||||||||
| 334 | } | ||||||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||||||
| 336 | } | ||||||||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||||||
| 339 | 1 | 1; | |||||||||||||||||||||||||||||||
| 340 | __END__ | ||||||||||||||||||||||||||||||||