lib/Math/Histo/PDL.pm

filter: Statementstmt 93.5% 306 / 327 Branchbran 72.7% 99 / 136 Conditioncond 52.1% 48 / 92 MC/DCmcdc 30.6% 15 / 49 Subroutinesub 100% 28 / 28 Podpod 94.4% 17 / 18 totaltotal 78.9% 513 / 650 CC 110 SCAR 33.4
CC
110
Cov
82%
hist2d
70.6
hist1d
36.2
fill2d_pdl
15.5
CRAP 28.2
↑ Summary
1 package Math::Histo::PDL;
2  
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 4 our $VERSION = '0.4.1';
8  
9 4 ▶use Math::Histo 0.4.1;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use Math::Histo::2D;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use PDL 2.000 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 4 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 4 our @EXPORT_OK = qw(
16 hist1d
17 hist2d
18 fill_pdl
19 fill2d_pdl
20 to_pdl
21 to_pdl2d
22 pdl_to_histo
23 histo_to_pdl
24 counts_pdl
25 edges_pdl
26 centers_pdl
27 errors_pdl
28 matrix_pdl
29 x_edges_pdl
30 y_edges_pdl
31 x_centers_pdl
32 y_centers_pdl
33 );
34  
35 4 our %EXPORT_TAGS = (
36 all => \@EXPORT_OK,
37 );
38  
39 # Internal helper: extract packed byte buffer from a PDL object for zero-copy ingestion
40 51 ▶sub _ensure_packed_double {
Subroutine _ensure_packed_double: called CC=7 SCAR=20.9
41 51 my ($pdl) = @_;
42 51 ▶ if (!eval { $pdl->isa('PDL') }) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { $pdl->isa("PDL") } }) 0 51 51
43 0 ▶ if (ref($pdl) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $pdl eq 'ARRAY') { } 0 0 0
44 0 $pdl = PDL::pdl(PDL::double(), $pdl);
45 } else {
46 0 croak "Math::Histo::PDL: input must be a PDL piddle or arrayref";
47 }
48 }
49 51 ▶ return ('', 0) if $pdl->isempty || $pdl->nelem == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $pdl->isempty or $pdl->nelem == 0 3 48 51
Condition66%2/3
$pdl->isempty or $pdl->nelem == 0
l!l&&r!l&&!r
3048
MC/DC50%1/2
$pdl->isempty or $pdl->nelem == 0
$pdl->isempty $pdl->nelem == 0
Truth table66%2/3
A or B
A= $pdl->isemptyB= $pdl->nelem == 0
ABresult
1X1
011
000
50  
51 48 ▶ my $d = ($pdl->type eq 'double') ? $pdl : $pdl->double;
Branch: 1 branch
BranchTrueFalseTotal
$pdl->type eq 'double' ? : 41 7 48
52 48 ▶ $d = $d->flat unless $d->ndims == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless $d->ndims == 1 1 47 48
53 48 $d = $d->make_physical;
54 48 my $dataref = $d->get_dataref;
55 48 return ($$dataref, $d->nelem);
56 }
57  
58 # 1D Ingestion
59 17 ▶sub fill_pdl {
Subroutine fill_pdl: called CC=6 SCAR=18.0
Pod documented
60 17 my ($self, $data, $weights) = @_;
61 croak "Math::Histo::PDL: first argument must be a Math::Histo object"
62 17 ▶ unless eval { $self->isa('Math::Histo') };
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { $self->isa("Math::Histo") } } 0 17 17
63 17 ▶ croak "Math::Histo::PDL: data piddle is required" unless defined $data;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data 0 17 17
64  
65 17 my ($x_bytes, $n_x) = _ensure_packed_double($data);
66 17 ▶ return $self if $n_x == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $n_x == 0 1 16 17
67  
68 16 ▶ if (defined $weights) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $weights) { } 3 13 16
69 3 my ($w_bytes, $n_w) = _ensure_packed_double($weights);
70 3 ▶ if ($n_w != $n_x) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n_w != $n_x) 1 2 3
71 1 croak "Math::Histo::PDL::fill_pdl: weights length ($n_w) must match data length ($n_x)";
72 }
73 2 $self->fill_packed_f64($x_bytes, $w_bytes);
74 } else {
75 13 $self->fill_packed_f64($x_bytes);
76 }
77 15 return $self;
78 }
79  
80 # 2D Ingestion
81 15 ▶sub fill2d_pdl {
Subroutine fill2d_pdl: called CC=15 SCAR=27.4
Pod documented
82 15 my ($self, @args) = @_;
83 croak "Math::Histo::PDL: first argument must be a Math::Histo::2D object"
84 15 ▶ unless eval { $self->isa('Math::Histo::2D') };
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { $self->isa("Math::Histo::2D") } } 0 15 15
85  
86 15 my ($x_pdl, $y_pdl, $weights);
87  
88 15 ▶ if (@args == 1 || (@args == 2 && eval { $args[0]->isa('PDL') } && $args[0]->ndims == 2)) {
Branch: 2 branches
BranchTrueFalseTotal
if (@args == 1 or @args == 2 and eval { do { $args[0]->isa('PDL') } } and $args[0]->ndims == 2) { } 2 13 15
elsif (@args == 2 or @args == 3) { } 13 0 13
Condition88%8/9
@args == 1 or @args == 2 and eval { do { $args[0]->isa("PDL") } } and $args[0]->ndims == 2
l!l&&r!l&&!r
1113
@args == 2 and eval { do { $args[0]->isa("PDL") } } and $args[0]->ndims == 2
!ll&&!rl&&r
1031
@args == 2 and eval { do { $args[0]->isa("PDL") } }
!ll&&!rl&&r
1004
MC/DC75%3/4
@args == 1 or @args == 2 and eval { do { $args[0]->isa("PDL") } } and $args[0]->ndims == 2
@args == 1 @args == 2 eval { do { $args[0]->isa("PDL") } } $args[0]->ndims == 2
Truth table80%4/5
A or B and C and D
A= @args == 1B= @args == 2C= eval { do { $args[0]->isa("PDL") } }D= $args[0]->ndims == 2
ABCDresult
1XXX1
01111
00XX0
010X0
01100
89 # Single 2D coordinate matrix: (2, N) or (N, 2), plus optional weights
90 2 my $coords = $args[0];
91 2 ▶ $weights = $args[1] if @args == 2;
Branch: 1 branch
BranchTrueFalseTotal
if @args == 2 1 1 2
92  
93 2 ▶ if ($coords->dim(0) == 2) {
Branch: 2 branches
BranchTrueFalseTotal
if ($coords->dim(0) == 2) { } 1 1 2
elsif ($coords->dim(1) == 2) { } 0 1 1
94 1 $x_pdl = $coords->slice('(0),:')->flat;
95 1 $y_pdl = $coords->slice('(1),:')->flat;
96 } elsif ($coords->dim(1) == 2) {
97 0 $x_pdl = $coords->slice(',(0)')->flat;
98 0 $y_pdl = $coords->slice(',(1)')->flat;
99 } else {
100 1 croak "Math::Histo::PDL::fill2d_pdl: coordinate matrix must have dimension 2 along axis 0 or 1 (got " . join('x', $coords->dims) . ")";
101 }
102 ▶ } elsif (@args == 2 || @args == 3) {
Condition66%2/3
@args == 2 or @args == 3
l!l&&r!l&&!r
3100
MC/DC0%0/2
@args == 2 or @args == 3
@args == 2 @args == 3
Truth table66%2/3
A or B
A= @args == 2B= @args == 3
ABresult
1X1
011
000
103 13 ($x_pdl, $y_pdl, $weights) = @args;
104 } else {
105 0 croak "Math::Histo::PDL::fill2d_pdl: invalid arguments (expected (\$h2d, \$x, \$y, [\$weights]) or (\$h2d, \$coords, [\$weights]))";
106 }
107  
108 14 my ($x_bytes, $n_x) = _ensure_packed_double($x_pdl);
109 14 my ($y_bytes, $n_y) = _ensure_packed_double($y_pdl);
110  
111 14 ▶ if ($n_x != $n_y) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n_x != $n_y) 1 13 14
112 1 croak "Math::Histo::PDL::fill2d_pdl: X length ($n_x) and Y length ($n_y) must match";
113 }
114 13 ▶ return $self if $n_x == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $n_x == 0 1 12 13
115  
116 12 ▶ if (defined $weights) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $weights) { } 3 9 12
117 3 my ($w_bytes, $n_w) = _ensure_packed_double($weights);
118 3 ▶ if ($n_w != $n_x) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n_w != $n_x) 1 2 3
119 1 croak "Math::Histo::PDL::fill2d_pdl: weights length ($n_w) must match data length ($n_x)";
120 }
121 2 $self->fill_packed_f64($x_bytes, $y_bytes, $w_bytes);
122 } else {
123 9 $self->fill_packed_f64($x_bytes, $y_bytes);
124 }
125 11 return $self;
126 }
127  
128 # 1D Export to PDL
129 5 ▶sub counts_pdl {
Subroutine counts_pdl: called CC=2 SCAR=6.9
Pod documented
130 5 my ($self) = @_;
131 croak "Math::Histo::PDL: argument must be a Math::Histo object"
132 5 ▶ unless eval { $self->isa('Math::Histo') };
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { $self->isa("Math::Histo") } } 0 5 5
133 5 my $contents = $self->bin_contents;
134 5 my $packed = pack('d*', @$contents);
135 5 my $p = PDL->new_from_specification(PDL::double(), scalar(@$contents));
136 5 ${$p->get_dataref} = $packed;
137 5 $p->upd_data;
138 5 return $p;
139 }
140  
141 5 ▶sub edges_pdl {
Subroutine edges_pdl: called CC=2 SCAR=6.9
Pod documented
142 5 my ($self) = @_;
143 croak "Math::Histo::PDL: argument must be a Math::Histo object"
144 5 ▶ unless eval { $self->isa('Math::Histo') };
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { $self->isa("Math::Histo") } } 0 5 5
145 5 my $edges = $self->bin_edges;
146 5 my $packed = pack('d*', @$edges);
147 5 my $p = PDL->new_from_specification(PDL::double(), scalar(@$edges));
148 5 ${$p->get_dataref} = $packed;
149 5 $p->upd_data;
150 5 return $p;
151 }
152  
153 2 ▶sub centers_pdl {
Subroutine centers_pdl: called CC=1 SCAR=0.0
Pod documented
154 2 my ($self) = @_;
155 2 my $edges = edges_pdl($self);
156 2 return ($edges->slice('0:-2') + $edges->slice('1:-1')) / 2.0;
157 }
158  
159 3 ▶sub errors_pdl {
Subroutine errors_pdl: called CC=3 SCAR=11.0
Pod documented
160 3 my ($self) = @_;
161 croak "Math::Histo::PDL: argument must be a Math::Histo object"
162 3 ▶ unless eval { $self->isa('Math::Histo') };
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { $self->isa("Math::Histo") } } 0 3 3
163 3 my $n = $self->nbins;
164 3 ▶ my @errors = map { $self->bin_error($_) // 0.0 } 0 .. $n - 1;
Condition50%1/2
$self->bin_error($_) // 0
l!l
120
MC/DC0%0/1
$self->bin_error($_) // 0
$self->bin_error($_)
Truth table50%1/2
A
A= $self->bin_error($_)
Aresult
11
00
165 3 my $packed = pack('d*', @errors);
166 3 my $p = PDL->new_from_specification(PDL::double(), $n);
167 3 ${$p->get_dataref} = $packed;
168 3 $p->upd_data;
169 3 return $p;
170 }
171  
172 4 ▶sub to_pdl {
Subroutine to_pdl: called CC=4 SCAR=13.9
Pod documented
173 4 my ($self, %opts) = @_;
174 4 ▶ if (wantarray && !%opts) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray and not %opts) 1 3 4
Condition66%2/3
wantarray and not %opts
!ll&&!rl&&r
301
MC/DC50%1/2
wantarray and not %opts
wantarray not %opts
Truth table66%2/3
A and not(B)
A= wantarrayB= not %opts
ABresult
0X0
100
111
175 1 return (counts_pdl($self), edges_pdl($self), errors_pdl($self));
176 }
177 3 ▶ if ($opts{all}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"all"}) 1 2 3
178 return {
179 1 counts => counts_pdl($self),
180 edges => edges_pdl($self),
181 centers => centers_pdl($self),
182 errors => errors_pdl($self),
183 };
184 }
185 2 return counts_pdl($self);
186 }
187  
188 # 2D Export to PDL
189 5 ▶sub matrix_pdl {
Subroutine matrix_pdl: called CC=4 SCAR=13.9
Pod documented
190 5 my ($self) = @_;
191 croak "Math::Histo::PDL: argument must be a Math::Histo::2D object"
192 5 ▶ unless eval { $self->isa('Math::Histo::2D') };
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { $self->isa("Math::Histo::2D") } } 0 5 5
193 5 my $nx = $self->nx;
194 5 my $ny = $self->ny;
195 5 my $packed = '';
196 5 for my $iy (0 .. $ny - 1) {
197 13 for my $ix (0 .. $nx - 1) {
198 49 $packed .= pack('d', $self->bin_content($ix, $iy));
199 }
200 }
201 5 my $p = PDL->new_from_specification(PDL::double(), $nx, $ny);
202 5 ${$p->get_dataref} = $packed;
203 5 $p->upd_data;
204 5 return $p;
205 }
206  
207 5 ▶sub x_edges_pdl {
Subroutine x_edges_pdl: called CC=4 SCAR=13.9
Pod documented
208 5 my ($self) = @_;
209 croak "Math::Histo::PDL: argument must be a Math::Histo::2D object"
210 5 ▶ unless eval { $self->isa('Math::Histo::2D') };
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { $self->isa("Math::Histo::2D") } } 0 5 5
211 5 my $nx = $self->nx;
212 5 my @edges;
213 5 for my $ix (0 .. $nx - 1) {
214 15 my ($x0, $x1) = $self->bin_bounds($ix, 0);
215 15 push @edges, $x0;
216 15 ▶ push @edges, $x1 if $ix == $nx - 1;
Branch: 1 branch
BranchTrueFalseTotal
if $ix == $nx - 1 5 10 15
217 }
218 5 my $packed = pack('d*', @edges);
219 5 my $p = PDL->new_from_specification(PDL::double(), scalar(@edges));
220 5 ${$p->get_dataref} = $packed;
221 5 $p->upd_data;
222 5 return $p;
223 }
224  
225 5 ▶sub y_edges_pdl {
Subroutine y_edges_pdl: called CC=4 SCAR=13.9
Pod documented
226 5 my ($self) = @_;
227 croak "Math::Histo::PDL: argument must be a Math::Histo::2D object"
228 5 ▶ unless eval { $self->isa('Math::Histo::2D') };
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { $self->isa("Math::Histo::2D") } } 0 5 5
229 5 my $ny = $self->ny;
230 5 my @edges;
231 5 for my $iy (0 .. $ny - 1) {
232 10 my (undef, undef, $y0, $y1) = $self->bin_bounds(0, $iy);
233 10 push @edges, $y0;
234 10 ▶ push @edges, $y1 if $iy == $ny - 1;
Branch: 1 branch
BranchTrueFalseTotal
if $iy == $ny - 1 5 5 10
235 }
236 5 my $packed = pack('d*', @edges);
237 5 my $p = PDL->new_from_specification(PDL::double(), scalar(@edges));
238 5 ${$p->get_dataref} = $packed;
239 5 $p->upd_data;
240 5 return $p;
241 }
242  
243 2 ▶sub x_centers_pdl {
Subroutine x_centers_pdl: called CC=1 SCAR=0.0
Pod documented
244 2 my ($self) = @_;
245 2 my $edges = x_edges_pdl($self);
246 2 return ($edges->slice('0:-2') + $edges->slice('1:-1')) / 2.0;
247 }
248  
249 2 ▶sub y_centers_pdl {
Subroutine y_centers_pdl: called CC=1 SCAR=0.0
Pod documented
250 2 my ($self) = @_;
251 2 my $edges = y_edges_pdl($self);
252 2 return ($edges->slice('0:-2') + $edges->slice('1:-1')) / 2.0;
253 }
254  
255 2 ▶sub errors2d_pdl {
Subroutine errors2d_pdl: called CC=5 SCAR=16.2
Pod documented
256 2 my ($self) = @_;
257 croak "Math::Histo::PDL: argument must be a Math::Histo::2D object"
258 2 ▶ unless eval { $self->isa('Math::Histo::2D') };
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { $self->isa("Math::Histo::2D") } } 0 2 2
259 2 my $nx = $self->nx;
260 2 my $ny = $self->ny;
261 2 my $packed = '';
262 2 for my $iy (0 .. $ny - 1) {
263 4 for my $ix (0 .. $nx - 1) {
264 12 ▶ $packed .= pack('d', $self->bin_error($ix, $iy) // 0.0);
Condition50%1/2
$self->bin_error($ix, $iy) // 0
l!l
120
MC/DC0%0/1
$self->bin_error($ix, $iy) // 0
$self->bin_error($ix, $iy)
Truth table50%1/2
A
A= $self->bin_error($ix, $iy)
Aresult
11
00
265 }
266 }
267 2 my $p = PDL->new_from_specification(PDL::double(), $nx, $ny);
268 2 ${$p->get_dataref} = $packed;
269 2 $p->upd_data;
270 2 return $p;
271 }
272  
273 4 ▶sub to_pdl2d {
Subroutine to_pdl2d: called CC=4 SCAR=13.9
Pod documented
274 4 my ($self, %opts) = @_;
275 4 ▶ if (wantarray && !%opts) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray and not %opts) 1 3 4
Condition66%2/3
wantarray and not %opts
!ll&&!rl&&r
301
MC/DC50%1/2
wantarray and not %opts
wantarray not %opts
Truth table66%2/3
A and not(B)
A= wantarrayB= not %opts
ABresult
0X0
100
111
276 1 return (matrix_pdl($self), x_edges_pdl($self), y_edges_pdl($self));
277 }
278 3 ▶ if ($opts{all}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"all"}) 1 2 3
279 return {
280 1 matrix => matrix_pdl($self),
281 x_edges => x_edges_pdl($self),
282 y_edges => y_edges_pdl($self),
283 x_centers => x_centers_pdl($self),
284 y_centers => y_centers_pdl($self),
285 errors => errors2d_pdl($self),
286 };
287 }
288 2 return matrix_pdl($self);
289 }
290  
291 # High-level functional builders
292 9 ▶sub hist1d {
Subroutine hist1d: called CC=21 SCAR=35.9
Pod documented
293 9 my ($data, %opts) = @_;
294 9 ▶ my $pdl = eval { $data->isa('PDL') } ? $data : PDL::pdl($data);
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $data->isa('PDL') } } ? : 9 0 9
295 9 my $weights = delete $opts{weights};
296  
297 9 my $h;
298 9 ▶ if (exists $opts{edges}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $opts{'edges'}) { } 1 8 9
elsif (exists $opts{'rule'} or exists $opts{'auto'}) { } 1 7 8
299 1 my $edges = $opts{edges};
300 1 ▶ my @edges_arr = eval { $edges->isa('PDL') } ? $edges->list : @$edges;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $edges->isa('PDL') } } ? : 1 0 1
301 1 delete $opts{edges};
302 1 $h = Math::Histo->new(edges => \@edges_arr, %opts);
303 ▶ } elsif (exists $opts{rule} || exists $opts{auto}) {
Condition66%2/3
exists $opts{"rule"} or exists $opts{"auto"}
l!l&&r!l&&!r
107
MC/DC50%1/2
exists $opts{"rule"} or exists $opts{"auto"}
exists $opts{"rule"} exists $opts{"auto"}
Truth table66%2/3
A or B
A= exists $opts{"rule"}B= exists $opts{"auto"}
ABresult
1X1
011
000
304 1 ▶ my $rule = delete $opts{rule} // delete $opts{auto};
Condition33%1/3
delete $opts{"rule"} // delete $opts{"auto"}
l!l&&r!l&&!r
100
MC/DC0%0/2
delete $opts{"rule"} // delete $opts{"auto"}
delete $opts{"rule"} delete $opts{"auto"}
Truth table33%1/3
A // B
A= delete $opts{"rule"}B= delete $opts{"auto"}
ABresult
1X1
011
000
305 1 my @samples = $pdl->list;
306 1 my ($nbins, $min, $max) = Math::Histo->estimate_bins(\@samples, $rule);
307 1 ▶ if ($min == $max) {
Branch: 1 branch
BranchTrueFalseTotal
if ($min == $max) { } 0 1 1
308 0 $min -= 0.5;
309 0 $max += 0.5;
310 } else {
311 1 my $margin = ($max - $min) * 1e-6;
312 1 ▶ $max += ($margin > 0 ? $margin : 1e-6);
Branch: 1 branch
BranchTrueFalseTotal
$margin > 0 ? : 1 0 1
313 }
314 1 $h = Math::Histo->new(bins => $nbins, min => $min, max => $max, %opts);
315 } else {
316 7 ▶ my $nbins = delete $opts{bins} // delete $opts{nbins} // 50;
Condition40%2/5
(delete $opts{"bins"} // delete $opts{"nbins"}) // 50
l!l
70
delete $opts{"bins"} // delete $opts{"nbins"}
l!l&&r!l&&!r
700
MC/DC0%0/2
(delete $opts{"bins"} // delete $opts{"nbins"}) // 50
delete $opts{"bins"} delete $opts{"nbins"}
Truth table33%1/3
A // B
A= delete $opts{"bins"}B= delete $opts{"nbins"}
ABresult
1X1
011
000
317 7 my $min = delete $opts{min};
318 7 my $max = delete $opts{max};
319  
320 7 ▶ if (!defined $min || !defined $max) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $min and defined $max) 1 6 7
Condition66%2/3
defined $min and defined $max
!ll&&!rl&&r
106
MC/DC50%1/2
defined $min and defined $max
defined $min defined $max
Truth table66%2/3
A and B
A= defined $minB= defined $max
ABresult
0X0
100
111
321 1 ▶ if ($pdl->nelem > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pdl->nelem > 0) { } 1 0 1
322 1 my ($pmin, $pmax) = $pdl->minmax;
323 1 ▶ $min //= $pmin;
Condition33%1/3
$min //= $pmin
l!l&&r!l&&!r
010
MC/DC0%0/2
$min //= $pmin
$min $pmin
Truth table33%1/3
A //= B
A= $minB= $pmin
ABresult
1X1
011
000
324 1 ▶ if (!defined $max) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $max) 1 0 1
325 1 ▶ if ($pmin == $pmax) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pmin == $pmax) { } 0 1 1
326 0 $min -= 0.5;
327 0 $max = $pmax + 0.5;
328 } else {
329 1 my $margin = ($pmax - $pmin) * 1e-6;
330 1 ▶ $max = $pmax + ($margin > 0 ? $margin : 1e-6);
Branch: 1 branch
BranchTrueFalseTotal
$margin > 0 ? : 1 0 1
331 }
332 }
333 } else {
334 0 ▶ $min //= 0.0;
Condition0%0/2
$min //= 0
l!l
00
MC/DC0%0/1
$min //= 0
$min
Truth table0%0/2
A
A= $min
Aresult
11
00
335 0 ▶ $max //= 1.0;
Condition0%0/2
$max //= 1
l!l
00
MC/DC0%0/1
$max //= 1
$max
Truth table0%0/2
A
A= $max
Aresult
11
00
336 }
337 }
338 7 $h = Math::Histo->new(bins => $nbins, min => $min, max => $max, %opts);
339 }
340  
341 9 ▶ fill_pdl($h, $pdl, $weights) if $pdl->nelem > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $pdl->nelem > 0 8 1 9
342 9 return $h;
343 }
344  
345 9 ▶sub hist2d {
Subroutine hist2d: called CC=37 SCAR=42.6
Pod documented
346 9 my (@args) = @_;
347 9 my ($x, $y, $coords, %opts);
348  
349 9 ▶ if (@args >= 1 && eval { $args[0]->isa('PDL') } && $args[0]->ndims == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args >= 1 and eval { do { $args[0]->isa('PDL') } } and $args[0]->ndims == 2) { } 5 4 9
Condition50%3/6
@args >= 1 and eval { do { $args[0]->isa("PDL") } } and $args[0]->ndims == 2
!ll&&!rl&&r
045
@args >= 1 and eval { do { $args[0]->isa("PDL") } }
!ll&&!rl&&r
009
MC/DC33%1/3
@args >= 1 and eval { do { $args[0]->isa("PDL") } } and $args[0]->ndims == 2
@args >= 1 eval { do { $args[0]->isa("PDL") } } $args[0]->ndims == 2
Truth table50%2/4
A and B and C
A= @args >= 1B= eval { do { $args[0]->isa("PDL") } }C= $args[0]->ndims == 2
ABCresult
0XX0
10X0
1100
1111
350 5 $coords = shift @args;
351 5 %opts = @args;
352 5 ▶ if ($coords->dim(0) == 2) {
Branch: 2 branches
BranchTrueFalseTotal
if ($coords->dim(0) == 2) { } 3 2 5
elsif ($coords->dim(1) == 2) { } 1 1 2
353 3 $x = $coords->slice('(0),:')->flat;
354 3 $y = $coords->slice('(1),:')->flat;
355 } elsif ($coords->dim(1) == 2) {
356 1 $x = $coords->slice(',(0)')->flat;
357 1 $y = $coords->slice(',(1)')->flat;
358 } else {
359 1 croak "Math::Histo::PDL::hist2d: coordinate matrix must have 2 columns or 2 rows (got " . join('x', $coords->dims) . ")";
360 }
361 } else {
362 4 $x = shift @args;
363 4 $y = shift @args;
364 4 %opts = @args;
365 4 ▶ $x = PDL::pdl($x) unless eval { $x->isa('PDL') };
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { $x->isa("PDL") } } 0 4 4
366 4 ▶ $y = PDL::pdl($y) unless eval { $y->isa('PDL') };
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { $y->isa("PDL") } } 0 4 4
367 }
368  
369 8 my $weights = delete $opts{weights};
370 8 my $h2d;
371  
372 8 ▶ if (exists $opts{xedges} && exists $opts{yedges}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts{'xedges'} and exists $opts{'yedges'}) { } 1 7 8
Condition66%2/3
exists $opts{"xedges"} and exists $opts{"yedges"}
!ll&&!rl&&r
701
MC/DC50%1/2
exists $opts{"xedges"} and exists $opts{"yedges"}
exists $opts{"xedges"} exists $opts{"yedges"}
Truth table66%2/3
A and B
A= exists $opts{"xedges"}B= exists $opts{"yedges"}
ABresult
0X0
100
111
373 1 my $xe = delete $opts{xedges};
374 1 my $ye = delete $opts{yedges};
375 1 ▶ my @x_arr = eval { $xe->isa('PDL') } ? $xe->list : @$xe;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $xe->isa('PDL') } } ? : 1 0 1
376 1 ▶ my @y_arr = eval { $ye->isa('PDL') } ? $ye->list : @$ye;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $ye->isa('PDL') } } ? : 1 0 1
377 1 $h2d = Math::Histo::2D->new(xedges => \@x_arr, yedges => \@y_arr, %opts);
378 } else {
379 7 my ($xbins, $ybins);
380 7 ▶ if (exists $opts{bins}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts{"bins"}) 1 6 7
381 1 my $b = delete $opts{bins};
382 1 ▶ if (ref($b) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $b eq 'ARRAY') { } 0 1 1
383 0 ($xbins, $ybins) = @$b;
384 } else {
385 1 $xbins = $ybins = $b;
386 }
387 }
388 7 ▶ $xbins //= delete $opts{xbins} // 50;
Condition75%3/4
$xbins //= delete $opts{"xbins"} // 50
l!l
16
delete $opts{"xbins"} // 50
l!l
60
MC/DC100%1/1
$xbins //= delete $opts{"xbins"} // 50
$xbins
Truth table100%2/2
A
A= $xbins
Aresult
11
00
389 7 ▶ $ybins //= delete $opts{ybins} // 50;
Condition75%3/4
$ybins //= delete $opts{"ybins"} // 50
l!l
16
delete $opts{"ybins"} // 50
l!l
60
MC/DC100%1/1
$ybins //= delete $opts{"ybins"} // 50
$ybins
Truth table100%2/2
A
A= $ybins
Aresult
11
00
390  
391 7 my $xmin = delete $opts{xmin};
392 7 my $xmax = delete $opts{xmax};
393 7 my $ymin = delete $opts{ymin};
394 7 my $ymax = delete $opts{ymax};
395  
396 7 ▶ if (!defined $xmin || !defined $xmax) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $xmin and defined $xmax) 1 6 7
Condition66%2/3
defined $xmin and defined $xmax
!ll&&!rl&&r
106
MC/DC50%1/2
defined $xmin and defined $xmax
defined $xmin defined $xmax
Truth table66%2/3
A and B
A= defined $xminB= defined $xmax
ABresult
0X0
100
111
397 1 ▶ if ($x->nelem > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->nelem > 0) { } 1 0 1
398 1 my ($min, $max) = $x->minmax;
399 1 ▶ $xmin //= $min;
Condition33%1/3
$xmin //= $min
l!l&&r!l&&!r
010
MC/DC0%0/2
$xmin //= $min
$xmin $min
Truth table33%1/3
A //= B
A= $xminB= $min
ABresult
1X1
011
000
400 1 ▶ if (!defined $xmax) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $xmax) 1 0 1
401 1 ▶ if ($min == $max) {
Branch: 1 branch
BranchTrueFalseTotal
if ($min == $max) { } 0 1 1
402 0 $xmin -= 0.5;
403 0 $xmax = $max + 0.5;
404 } else {
405 1 my $margin = ($max - $min) * 1e-6;
406 1 ▶ $xmax = $max + ($margin > 0 ? $margin : 1e-6);
Branch: 1 branch
BranchTrueFalseTotal
$margin > 0 ? : 1 0 1
407 }
408 }
409 } else {
410 0 ▶ $xmin //= 0.0;
Condition0%0/2
$xmin //= 0
l!l
00
MC/DC0%0/1
$xmin //= 0
$xmin
Truth table0%0/2
A
A= $xmin
Aresult
11
00
411 0 ▶ $xmax //= 1.0;
Condition0%0/2
$xmax //= 1
l!l
00
MC/DC0%0/1
$xmax //= 1
$xmax
Truth table0%0/2
A
A= $xmax
Aresult
11
00
412 }
413 }
414  
415 7 ▶ if (!defined $ymin || !defined $ymax) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ymin and defined $ymax) 1 6 7
Condition66%2/3
defined $ymin and defined $ymax
!ll&&!rl&&r
106
MC/DC50%1/2
defined $ymin and defined $ymax
defined $ymin defined $ymax
Truth table66%2/3
A and B
A= defined $yminB= defined $ymax
ABresult
0X0
100
111
416 1 ▶ if ($y->nelem > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y->nelem > 0) { } 1 0 1
417 1 my ($min, $max) = $y->minmax;
418 1 ▶ $ymin //= $min;
Condition33%1/3
$ymin //= $min
l!l&&r!l&&!r
010
MC/DC0%0/2
$ymin //= $min
$ymin $min
Truth table33%1/3
A //= B
A= $yminB= $min
ABresult
1X1
011
000
419 1 ▶ if (!defined $ymax) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ymax) 1 0 1
420 1 ▶ if ($min == $max) {
Branch: 1 branch
BranchTrueFalseTotal
if ($min == $max) { } 0 1 1
421 0 $ymin -= 0.5;
422 0 $ymax = $max + 0.5;
423 } else {
424 1 my $margin = ($max - $min) * 1e-6;
425 1 ▶ $ymax = $max + ($margin > 0 ? $margin : 1e-6);
Branch: 1 branch
BranchTrueFalseTotal
$margin > 0 ? : 1 0 1
426 }
427 }
428 } else {
429 0 ▶ $ymin //= 0.0;
Condition0%0/2
$ymin //= 0
l!l
00
MC/DC0%0/1
$ymin //= 0
$ymin
Truth table0%0/2
A
A= $ymin
Aresult
11
00
430 0 ▶ $ymax //= 1.0;
Condition0%0/2
$ymax //= 1
l!l
00
MC/DC0%0/1
$ymax //= 1
$ymax
Truth table0%0/2
A
A= $ymax
Aresult
11
00
431 }
432 }
433  
434 7 $h2d = Math::Histo::2D->new(
435 xbins => $xbins, xmin => $xmin, xmax => $xmax,
436 ybins => $ybins, ymin => $ymin, ymax => $ymax,
437 %opts
438 );
439 }
440  
441 8 ▶ if ($x->nelem > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->nelem > 0) 8 0 8
442 8 fill2d_pdl($h2d, $x, $y, $weights);
443 }
444 8 return $h2d;
445 }
446  
447 2 ▶sub pdl_to_histo {
Subroutine pdl_to_histo: called CC=5 SCAR=16.8
Pod documented
448 2 my ($pdl, %opts) = @_;
449 2 ▶ if (eval { $pdl->isa('PDL') } && $pdl->ndims == 2 && ($pdl->dim(0) == 2 || $pdl->dim(1) == 2)) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { $pdl->isa("PDL") } } and $pdl->ndims == 2 and $pdl->dim(0) == 2 || $pdl->dim(1) == 2) 1 1 2
Condition55%5/9
eval { do { $pdl->isa("PDL") } } and $pdl->ndims == 2 and $pdl->dim(0) == 2 || $pdl->dim(1) == 2
!ll&&!rl&&r
101
eval { do { $pdl->isa("PDL") } } and $pdl->ndims == 2
!ll&&!rl&&r
011
$pdl->dim(0) == 2 or $pdl->dim(1) == 2
l!l&&r!l&&!r
100
MC/DC25%1/4
eval { do { $pdl->isa("PDL") } } and $pdl->ndims == 2 and $pdl->dim(0) == 2 || $pdl->dim(1) == 2
eval { do { $pdl->isa("PDL") } } $pdl->ndims == 2 $pdl->dim(0) == 2 $pdl->dim(1) == 2
Truth table40%2/5
A and B and C or D
A= eval { do { $pdl->isa("PDL") } }B= $pdl->ndims == 2C= $pdl->dim(0) == 2D= $pdl->dim(1) == 2
ABCDresult
0XXX0
10XX0
11000
111X1
11011
450 1 return hist2d($pdl, %opts);
451 }
452 1 return hist1d($pdl, %opts);
453 }
454  
455 2 ▶sub histo_to_pdl {
Subroutine histo_to_pdl: called CC=2 SCAR=6.9
Pod undocumented
456 2 my ($h, %opts) = @_;
457 2 ▶ if (eval { $h->isa('Math::Histo::2D') }) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { $h->isa("Math::Histo::2D") } }) 1 1 2
458 1 return to_pdl2d($h, %opts);
459 }
460 1 return to_pdl($h, %opts);
461 }
462  
463 # Inject OO extension methods into Math::Histo and Math::Histo::2D upon module load
464 {
465 4 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
466 4 *Math::Histo::fill_pdl = \&fill_pdl;
467 4 *Math::Histo::to_pdl = \&to_pdl;
468 4 *Math::Histo::counts_pdl = \&counts_pdl;
469 4 *Math::Histo::edges_pdl = \&edges_pdl;
470 4 *Math::Histo::centers_pdl = \&centers_pdl;
471 4 *Math::Histo::errors_pdl = \&errors_pdl;
472  
473 4 *Math::Histo::2D::fill_pdl = \&fill2d_pdl;
474 4 *Math::Histo::2D::to_pdl = \&to_pdl2d;
475 4 *Math::Histo::2D::matrix_pdl = \&matrix_pdl;
476 4 *Math::Histo::2D::counts_pdl = \&matrix_pdl;
477 4 *Math::Histo::2D::x_edges_pdl = \&x_edges_pdl;
478 4 *Math::Histo::2D::y_edges_pdl = \&y_edges_pdl;
479 4 *Math::Histo::2D::x_centers_pdl = \&x_centers_pdl;
480 4 *Math::Histo::2D::y_centers_pdl = \&y_centers_pdl;
481 4 *Math::Histo::2D::errors_pdl = \&errors2d_pdl;
482 }
483  
484 4 1;
485  
486 __END__
↑ Summary