blib/lib/CXC/PDL/Bin1D.pm

filter: Statementstmt 46.6% 76 / 163 Branchbran 0.0% 0 / 44 Conditioncond 0.0% 0 / 74 MC/DCmcdc 0.0% 0 / 31 Subroutinesub 89.2% 25 / 28 Podpod n/a totaltotal 29.7% 101 / 340 CC 60 SCAR 58.1
CC
60
Cov
27%
_bin_on_index_parse_range_opts
552.0
bin_adaptive_snr
506.0
bin_on_index
306.0
CRAP 334.0
↑ Summary blib/lib/CXC/PDL/Bin1D/Utils.pm »
1 #
2 # GENERATED WITH PDL::PP from lib/CXC/PDL/Bin1D.pd! Don't modify!
3 #
4 package CXC::PDL::Bin1D;
5  
6 our @EXPORT_OK = qw( );
7 our %EXPORT_TAGS = (Func=>\@EXPORT_OK);
8  
9 4 ▶use PDL::Core();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use PDL::Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use DynaLoader;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13  
14 our $VERSION = '0.27';
15 our @ISA = ( 'PDL::Exporter','DynaLoader' );
16 push @PDL::Core::PP, __PACKAGE__;
17 bootstrap CXC::PDL::Bin1D $VERSION;
18  
19  
20  
21  
22  
23  
24  
25  
26 #line 202 "lib/CXC/PDL/Bin1D.pd"
27  
28 use v5.10;
29 use strict;
30 use warnings;
31 no namespace::clean;
32 use CXC::PDL::Bin1D::Utils;
33 use namespace::clean;
34  
35 #line 212 "lib/CXC/PDL/Bin1D.pd"
36 our @EXPORT_OK;
37 our %EXPORT_TAGS;
38  
39 #line 221 "lib/CXC/PDL/Bin1D.pd"
40 use constant {
41 BIN_RC_OK => 1,
42 BIN_RC_GEWMAX => 2,
43 BIN_RC_GENMAX => 4,
44 BIN_RC_FOLDED => 8,
45 BIN_RC_GTMINSN => 16,
46 BIN_ARG_HAVE_ERROR => 1,
47 BIN_ARG_HAVE_WEIGHT => 2,
48 BIN_ARG_SET_BAD => 4,
49 BIN_ARG_FOLD => 8,
50 BIN_ARG_HAVE_WIDTH => 16,
51 BIN_ARG_ERROR_SDEV => 32,
52 BIN_ARG_ERROR_POISSON => 64,
53 BIN_ARG_ERROR_RSS => 128,
54 BIN_ARG_HAVE_SIGNAL => 256,
55 BIN_ARG_SAVE_OOB_START_END => 512,
56 BIN_ARG_SAVE_OOB_START_NBINS => 1024,
57 BIN_ARG_SHIFT_IMIN => 1536,
58 BIN_ARG_SAVE_OOB_END => 2048,
59 BIN_ARG_SAVE_OOB_NBINS => 4096,
60 BIN_ARG_SAVE_OOB => 7680,
61 BIN_ARG_WANT_EXTREMA => 8192,
62 BIN_ARG_WANT_SUM_WEIGHT => 16384,
63 BIN_ARG_WANT_SUM_WEIGHT2 => 32768,
64 };
65  
66 #line 226 "lib/CXC/PDL/Bin1D.pd"
67  
68 =for Pod::Coverage
69 BIN_RC_OK
70 BIN_RC_GEWMAX
71 BIN_RC_GENMAX
72 BIN_RC_FOLDED
73 BIN_RC_GTMINSN
74 BIN_ARG_HAVE_ERROR
75 BIN_ARG_HAVE_WEIGHT
76 BIN_ARG_SET_BAD
77 BIN_ARG_FOLD
78 BIN_ARG_HAVE_WIDTH
79 BIN_ARG_ERROR_SDEV
80 BIN_ARG_ERROR_POISSON
81 BIN_ARG_ERROR_RSS
82 BIN_ARG_HAVE_SIGNAL
83 BIN_ARG_SAVE_OOB_START_END
84 BIN_ARG_SAVE_OOB_START_NBINS
85 BIN_ARG_SHIFT_IMIN
86 BIN_ARG_SAVE_OOB_END
87 BIN_ARG_SAVE_OOB_NBINS
88 BIN_ARG_SAVE_OOB
89 BIN_ARG_WANT_EXTREMA
90 BIN_ARG_WANT_SUM_WEIGHT
91 BIN_ARG_WANT_SUM_WEIGHT2
92  
93 =cut
94  
95 #line 229 "lib/CXC/PDL/Bin1D.pd"
96 my %MapErrorAlgo = (
97  
98 sdev => BIN_ARG_ERROR_SDEV,
99 rss => BIN_ARG_ERROR_RSS,
100 poisson => BIN_ARG_ERROR_POISSON,
101 );
102  
103 #line 238 "lib/CXC/PDL/Bin1D.pd"
104  
105 =for Pod::Coverage
106 set_boundscheck
107 set_debugging
108  
109 =for stopwords
110 merchantability
111  
112 =cut
113  
114 =head1 NAME
115  
116 CXC::PDL::Bin1D - one dimensional binning functions
117  
118 =head1 SYNOPSIS
119  
120 use PDL;
121 use CXC::PDL::Bin1D;
122  
123 =head1 DESCRIPTION
124  
125 One dimensional binning functions,
126  
127 =over
128  
129 =item *
130  
131 binning up to a given S/N
132  
133 =item *
134  
135 optimized one-pass robust statistics
136  
137 =back
138  
139 All functions are made available in the B<CXC::PDL::Bin1D> namespace.
140  
141 =head1 SUBROUTINES
142  
143 =cut
144  
145 #line 440 "lib/CXC/PDL/Bin1D.pd"
146 $EXPORT_TAGS{constants} = [ qw(
147 BIN_RC_GTMINSN
148 BIN_RC_GEWMAX
149 BIN_RC_GENMAX
150 BIN_RC_OK
151 BIN_RC_FOLDED
152 BIN_ARG_WANT_EXTREMA
153 BIN_ARG_WANT_SUM_WEIGHT
154 BIN_ARG_SAVE_OOB_NBINS
155 BIN_ARG_ERROR_RSS
156 BIN_ARG_SHIFT_IMIN
157 BIN_ARG_SET_BAD
158 BIN_ARG_FOLD
159 BIN_ARG_HAVE_SIGNAL
160 BIN_ARG_ERROR_POISSON
161 BIN_ARG_SAVE_OOB
162 BIN_ARG_SAVE_OOB_END
163 BIN_ARG_HAVE_WEIGHT
164 BIN_ARG_WANT_SUM_WEIGHT2
165 BIN_ARG_SAVE_OOB_START_END
166 BIN_ARG_HAVE_WIDTH
167 BIN_ARG_ERROR_SDEV
168 BIN_ARG_SAVE_OOB_START_NBINS
169 BIN_ARG_HAVE_ERROR
170 )];
171  
172 #line 442 "lib/CXC/PDL/Bin1D.pd"
173 $EXPORT_TAGS{Func} = [ qw(
174 bin_adaptive_snr
175 bin_on_index
176 )];
177  
178 #line 443 "lib/CXC/PDL/Bin1D.pd"
179 @EXPORT_OK = map { @{$_} } values %EXPORT_TAGS;
180 #line 181 "Bin1D.pm"
181  
182 4 ▶no namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
183  
184 4 ▶use Types::Common::Numeric qw[ PositiveNum PositiveInt PositiveOrZeroInt ];
Subroutine BEGIN: called CC=1 SCAR=0.0
185 4 ▶use Types::Standard qw[ Optional InstanceOf slurpy Dict Bool Enum ];
Subroutine BEGIN: called CC=1 SCAR=0.0
186 4 ▶use Type::Params qw[ compile ];
Subroutine BEGIN: called CC=1 SCAR=0.0
187  
188 4 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
189 4 ▶use PDL::Lite ();
Subroutine BEGIN: called CC=1 SCAR=0.0
190  
191 4 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
192  
193 4 ▶use constant PP_PASS_TMPS => do { require version; PDL->VERSION < version->parse( '2.073' ) };
Subroutine BEGIN: called CC=1 SCAR=0.0
194  
195 my $bin_adaptive_snr_check;
196  
197 BEGIN {
198  
199 4 ▶ $bin_adaptive_snr_check = compile(
Subroutine BEGIN: called CC=1 SCAR=0.0
200 slurpy Dict [
201 signal => InstanceOf ['PDL'],
202 error => Optional [ InstanceOf ['PDL'] ],
203 width => Optional [ InstanceOf ['PDL'] ],
204 min_snr => PositiveNum,
205 min_nelem => Optional [PositiveInt],
206 max_nelem => Optional [PositiveInt],
207 min_width => Optional [PositiveNum],
208 max_width => Optional [PositiveNum],
209 fold => Optional [Bool],
210 error_algo => Optional [ Enum [ keys %MapErrorAlgo ] ],
211 set_bad => Optional [Bool],
212 ] );
213 }
214  
215 ## no critic (Subroutines::ProhibitExcessComplexity)
216 sub bin_adaptive_snr {
217  
218 0 ▶ my ( $opts ) = $bin_adaptive_snr_check->( @_ );
Subroutine bin_adaptive_snr: not called CC=22 SCAR=62.3
219  
220 # specify defaults
221 0 my %opt = (
222 error_algo => 'sdev',
223 min_nelem => 1,
224 %$opts,
225 );
226  
227 Carp::croak( "width must be specified if either of min_width or max_width is specified\n" )
228 if ( defined $opt{min_width} || defined $opt{max_width} )
229 0 ▶ && !defined $opt{width};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opt{"min_width"} || defined $opt{"max_width"} and not defined $opt{"width"} 0 0 0
Condition0%0/6
defined $opt{"min_width"} || defined $opt{"max_width"} and not defined $opt{"width"}
!ll&&!rl&&r
000
defined $opt{"min_width"} or defined $opt{"max_width"}
l!l&&r!l&&!r
000
MC/DC0%0/3
defined $opt{"min_width"} || defined $opt{"max_width"} and not defined $opt{"width"}
defined $opt{"min_width"} defined $opt{"max_width"} not defined $opt{"width"}
Truth table0%0/5
A or B and not(C)
A= defined $opt{"min_width"}B= defined $opt{"max_width"}C= not defined $opt{"width"}
ABCresult
00X0
1X00
0100
1X11
0111
230  
231 Carp::croak( "must specify error attribute if 'rss' errors selected\n" )
232 0 ▶ if !defined $opt{error} && $opt{error_algo} eq 'rss';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $opt{"error"} and $opt{"error_algo"} eq "rss" 0 0 0
Condition0%0/3
not defined $opt{"error"} and $opt{"error_algo"} eq "rss"
!ll&&!rl&&r
000
MC/DC0%0/2
not defined $opt{"error"} and $opt{"error_algo"} eq "rss"
not defined $opt{"error"} $opt{"error_algo"} eq "rss"
Truth table0%0/3
not(A) and B
A= not defined $opt{"error"}B= $opt{"error_algo"} eq "rss"
ABresult
0X0
100
111
233  
234 0 ▶ $opt{min_width} ||= 0;
Condition0%0/2
$opt{"min_width"} ||= 0
l!l
00
MC/DC0%0/1
$opt{"min_width"} ||= 0
$opt{"min_width"}
Truth table0%0/2
A
A= $opt{"min_width"}
Aresult
11
00
235 0 ▶ $opt{max_width} ||= 0;
Condition0%0/2
$opt{"max_width"} ||= 0
l!l
00
MC/DC0%0/1
$opt{"max_width"} ||= 0
$opt{"max_width"}
Truth table0%0/2
A
A= $opt{"max_width"}
Aresult
11
00
236 0 ▶ $opt{max_nelem} ||= 0;
Condition0%0/2
$opt{"max_nelem"} ||= 0
l!l
00
MC/DC0%0/1
$opt{"max_nelem"} ||= 0
$opt{"max_nelem"}
Truth table0%0/2
A
A= $opt{"max_nelem"}
Aresult
11
00
237  
238 # if the user hasn't specified whether to fold the last bin,
239 # turn it on if there aren't *maximum* constraints
240 $opt{fold} = !defined $opt{max_width} || !defined $opt{max_nelem}
241 0 ▶ unless defined $opt{fold};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opt{"fold"} 0 0 0
Condition0%0/3
defined $opt{'max_width'} && defined $opt{'max_nelem'}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $opt{'max_width'} && defined $opt{'max_nelem'}
defined $opt{'max_width'} defined $opt{'max_nelem'}
Truth table0%0/3
A && B
A= defined $opt{'max_width'}B= defined $opt{'max_nelem'}
ABresult
0X0
100
111
242  
243 $opt{flags}
244 = ( ( defined $opt{error} && BIN_ARG_HAVE_ERROR ) || 0 )
245 | ( ( defined $opt{width} && BIN_ARG_HAVE_WIDTH ) || 0 )
246 | ( ( $opt{fold} && BIN_ARG_FOLD ) || 0 ) | ( ( $opt{set_bad} && BIN_ARG_SET_BAD ) || 0 )
247 0 ▶ | $MapErrorAlgo{ $opt{error_algo} };
Condition0%0/16
defined $opt{'error'} && 1 || 0
l!l
00
defined $opt{"error"} and 1
!ll
00
defined $opt{'width'} && 16 || 0
l!l
00
defined $opt{"width"} and 16
!ll
00
$opt{'fold'} && 8 || 0
l!l
00
$opt{"fold"} and 8
!ll
00
$opt{'set_bad'} && 4 || 0
l!l
00
$opt{"set_bad"} and 4
!ll
00
MC/DC0%0/1
defined $opt{'error'} && 1 || 0
defined $opt{"error"}
MC/DC0%0/1
defined $opt{'width'} && 16 || 0
defined $opt{"width"}
MC/DC0%0/1
$opt{'fold'} && 8 || 0
$opt{"fold"}
MC/DC0%0/1
$opt{'set_bad'} && 4 || 0
$opt{"set_bad"}
Truth table0%0/2
A
A= defined $opt{"error"}
Aresult
11
00
Truth table0%0/2
A
A= defined $opt{"width"}
Aresult
11
00
Truth table0%0/2
A
A= $opt{"fold"}
Aresult
11
00
Truth table0%0/2
A
A= $opt{"set_bad"}
Aresult
11
00
248  
249 0 my @pin = qw[ signal error width ];
250 0 my @pout = qw[ index nbins nelem b_signal b_error b_mean b_snr
251 b_width ifirst ilast rc ];
252 0 my @oargs = qw[ flags min_snr min_nelem max_nelem min_width max_width ];
253  
254 # several of the input piddles are optional. the PP routine
255 # doesn't know that and will complain about the wrong number of
256 # dimensions if we pass a null piddle. A 1D zero element piddle
257 # will have its dimensions auto-expanded without much
258 # wasted memory.
259 0 $opt{$_} = PDL->new( 0 ) for grep { !defined $opt{$_} } @pin;
260 0 $opt{$_} = PDL->null for grep { !defined $opt{$_} } @pout;
261  
262 0 my @pars;
263 0 if ( PP_PASS_TMPS ) {
264 my @ptmp = qw[ berror2 bsignal2 b_m2 b_weight b_weight_sig b_weight_sig2 ];
265 $opt{$_} = PDL->null for grep { !defined $opt{$_} } @ptmp;
266 @pars = ( @pin, @pout, @ptmp, @oargs );
267 }
268 else {
269 0 @pars = ( @pin, @pout, @oargs );
270 }
271  
272 0 _bin_adaptive_snr_int( @opt{@pars} );
273  
274 my %results = map { ## no critic (BuiltinFunctions::ProhibitComplexMappings)
275 0 ( my $nkey = $_ ) =~ s/^b_//;
276 0 ( $nkey, $opt{$_} )
277 } @pout;
278  
279 0 ▶ return wantarray ? %results : \%results; ## no critic(Community::Wantarray)
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
280 }
281  
282 =pod
283  
284 =head2 bin_adaptive_snr
285  
286 =for usage
287  
288 %hash = bin_adaptive_snr( %options );
289  
290 =for stopwords
291 Adaptively
292  
293 =for ref
294  
295 Adaptively bin a data set to achieve a minimum signal to noise ratio
296 in each bin.
297  
298 This routine ignores data with bad values or with errors that have
299 bad values.
300  
301 B<bin_adaptive_snr> groups data into bins such that each bin meets
302 one or more conditions:
303  
304 =over
305  
306 =item *
307  
308 a minimum signal to noise ratio (S/N).
309  
310 =item *
311  
312 A minimum number of data elements (optional).
313  
314 =item *
315  
316 A maximum number of data elements (optional).
317  
318 =item *
319  
320 A maximum data I<width> (see below) (optional).
321  
322 =item *
323  
324 A minimum data I<width> (see below) (optional).
325  
326 =back
327  
328 The data are typically dependent values (e.g. flux as a function of
329 energy or counts as a function of radius). The data should be sorted
330 by the independent variable (e.g. energy or radius).
331  
332 Calculation of the S/N requires an estimate of the error associated
333 with each datum. The error may be provided or may be estimated from
334 the population using either the number of data elements in a bin
335 (e.g. Poisson errors) or the standard deviation of the signal in a bin.
336 If errors are provided, they may be used to weight the population standard
337 deviation or may be added in quadrature.
338  
339 Binning begins at the start of the signal vector. Data are accumulated
340 into a bin until one or more of the possible criteria is met. If the
341 final bin does not meet the required criteria, it may optionally be
342 successively folded into preceding bins until the final bin passes the
343 criteria or there are no bins left.
344  
345 Each datum may be assigned an extra parameter, its I<width>,
346 which is summed for each bin, and can be used as an additional constraint
347 on bin membership.
348  
349 =head3 Parameters
350  
351 B<bin_adaptive_snr> is passed a hash or a reference to a hash containing
352 its parameters. The available parameters are:
353  
354 =over
355  
356 =item C<signal>
357  
358 A piddle containing the signal data. This is required.
359  
360 =item C<error>
361  
362 A piddle with the error for signal datum. Optional.
363  
364 =item C<width>
365  
366 A piddle with the I<width> of each element of the signal. Optional.
367  
368 =item C<error_algo>
369  
370 A string indicating how the error is to be handled or calculated. It
371 may be have one of the following values:
372  
373 =over
374  
375 =item * C<poisson>
376  
377 Poisson errors will be calculated based upon the number of elements in a bin,
378  
379 error**2 = N
380  
381 Any input errors are ignored.
382  
383 =item * C<sdev>
384  
385 The error is the population standard deviation of the signal in a bin.
386  
387 error**2 = Sum [ ( signal - mean ) **2 ] / ( N - 1 )
388  
389 If errors are provided, they are used to calculated the weighted population
390 standard deviation.
391  
392 error**2 = ( Sum [ (signal/error)**2 ] / Sum [ 1/error**2 ] - mean**2 )
393 * N / ( N - 1 )
394  
395 =item * C<rss>
396  
397 Errors must be provided; the errors of elements in a bin are added in
398 quadrature.
399  
400 =back
401  
402 =item C<min_snr>
403  
404 The minimum signal to noise ratio to be achieved in each bin. Required.
405  
406 =item C<min_nelem>
407  
408 =item C<max_nelem>
409  
410 The minimum and/or maximum number of elements to be achieved in each bin. Optional
411  
412 =item C<min_width>
413  
414 =item C<max_width>
415  
416 The minimum and/or maximum width of the elements to be achieved in each bin. Optional.
417  
418 =item C<fold> I<boolean>
419  
420 If true, the last bin may be folded into the preceding bin in order to
421 ensure that the last bin meets one or more of the criteria. It defaults to false.
422  
423 =back
424  
425 =head3 Results
426  
427 B<bin_adaptive_snr> returns a hashref with the following entries:
428  
429 =over
430  
431 =item C<index>
432  
433 A piddle containing the bin indices for the elements in the input
434 data piddle. Data which were skipped because of bad values will have
435 their index set to the bad value.
436  
437 =item C<nbins>
438  
439 A piddle containing the number of bins which spanned the range of the
440 input data.
441  
442 =item C<signal>
443  
444 A piddle containing the sum of the data values in each bin. Only
445 indices C<0> through C<nbins -1> are valid.
446  
447 =item C<nelem>
448  
449 A piddle containing the number of data elements in each bin. Only
450 indices C<0> through C<nbins -1> are valid.
451  
452 =item C<error>
453  
454 A piddle containing the errors in each bin, calculated using the
455 algorithm specified via C<error_algo>. Only indices C<0> through
456 C<nbins -1> are valid.
457  
458 =item C<mean>
459  
460 A piddle containing the weighted mean of the signal in each bin. Only
461 indices C<0> through C<nbins -1 > are valid.
462  
463 =item C<ifirst>
464  
465 A piddle containing the index into the input data piddle of the first
466 data value in a bin. Only indices C<0> through C<nbins -1 > are valid.
467  
468 =item C<ilast>
469  
470 A piddle containing the index into the input data piddle of the last
471 data value in a bin. Only indices C<0> through C<nbins -1 > are valid.
472  
473 =item C<rc>
474  
475 A piddle containing a results code for each output bin. Only indices
476 C<0> through C<nbins -1 > are valid. The code is the bitwise "or" of
477 the following constants (available in the C<CXC::PDL::Bin1D>
478 namespace)
479  
480 =over
481  
482 =item BIN_RC_OK
483  
484 The bin met the minimum S/N, data element count and weight requirements
485  
486 =item BIN_RC_GEWMAX
487  
488 The bin weight was greater or equal to that requested.
489  
490 =item BIN_RC_GENMAX
491  
492 The number of data elements was greater or equal to that requested.
493  
494 =item BIN_RC_FOLDED
495  
496 The bin is the result of folding bins at the end of the bin vector to
497 achieve a minimum S/N.
498  
499 =item BIN_RC_GTMINSN
500  
501 The bin accumulated more data elements than was necessary to meet the
502 S/N requirements. This results from constraints on the minimum number
503 of data elements or bin weight.
504  
505 =back
506  
507 =back
508  
509 =cut
510  
511  
512  
513  
514  
515  
516  
517  
518 4 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
519 4 ▶no namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
520  
521 4 ▶use Types::Common::Numeric qw[ PositiveOrZeroInt ];
Subroutine BEGIN: called CC=1 SCAR=0.0
522 4 ▶use Types::Standard qw[ Optional Bool StrMatch Undef Int ];
Subroutine BEGIN: called CC=1 SCAR=0.0
523 4 ▶use Types::PDL -types;
Subroutine BEGIN: called CC=1 SCAR=0.0
524 4 ▶use Type::Params qw[ compile_named ];
Subroutine BEGIN: called CC=1 SCAR=0.0
525  
526 4 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
527 4 ▶use PDL::Lite ();
Subroutine BEGIN: called CC=1 SCAR=0.0
528  
529 4 ▶use Hash::Wrap ( { -class => __PACKAGE__ . '::bin_on_index' } );
Subroutine BEGIN: called CC=1 SCAR=0.0
530  
531 4 ▶use constant PP_PASS_TMPS => do { require version; PDL->VERSION < version->parse( '2.073' ) };
Subroutine BEGIN: called CC=1 SCAR=0.0
532  
533 # this is ugly
534 sub CoercedPiddle {
535  
536 16 ▶ my $to_type = shift;
Subroutine CoercedPiddle: called CC=1 SCAR=0.0
537  
538 16 my $convert = sprintf(
539 <<'EOS',
540 do { local $@;
541 require PDL::Core;
542 my $new = eval { PDL::Core::convert( $_, PDL::%s ) };
543 length($@) ? $_ : $new
544 }
545 EOS
546 $to_type,
547 );
548 16 return Piddle( [ type => $to_type ] )->plus_coercions( map { $_ => $convert } Piddle, @_ );
549 }
550  
551 4 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
552  
553 {
554  
555 my $check;
556 my $range_RE;
557  
558 BEGIN {
559 4 ▶ $range_RE = qr/(flat|slice)(?:,(minmax|min|max))?/;
Subroutine BEGIN: called CC=1 SCAR=0.0
560  
561 4 $check = compile_named(
562 index => CoercedPiddle( 'indx' ),
563 data => Piddle,
564 nbins => Optional [ CoercedPiddle( indx => PositiveOrZeroInt ) ],
565 weight => Optional [ Piddle | Undef ],
566 # Enum is preferred for the following, but see https://rt.cpan.org/Ticket/Display.html?id=129729
567 oob => Optional [ StrMatch( [qr/^start-end|start-nbins|end|nbins$/] ) | Bool ] => { default => 0 },
568 want_sum_weight => Optional [Bool],
569 want_sum_weight2 => Optional [Bool],
570 imin => Optional [ CoercedPiddle( indx => Int ) ],
571 imax => Optional [ CoercedPiddle( indx => Int ) ],
572 range => Optional [ StrMatch( [$range_RE] ) ] );
573 }
574  
575 my %Map_OOB = (
576 'start-end' => BIN_ARG_SAVE_OOB_START_END,
577 'start-nbins' => BIN_ARG_SAVE_OOB_START_NBINS,
578 'end' => BIN_ARG_SAVE_OOB_END,
579 'nbins' => BIN_ARG_SAVE_OOB_NBINS,
580 );
581  
582 sub bin_on_index {
583  
584 0 ▶ my $opt = $check->( @_ );
Subroutine bin_on_index: not called CC=17 SCAR=57.2
585  
586 0 _bin_on_index_parse_range_opts( $opt );
587  
588 $opt->{flags}
589 = ( ( defined $opt->{weight} && BIN_ARG_HAVE_WEIGHT ) || 0 )
590 | ( ( $opt->{want_sum_weight} && BIN_ARG_WANT_SUM_WEIGHT ) || 0 )
591 | ( ( $opt->{want_sum_weight2} && BIN_ARG_WANT_SUM_WEIGHT2 ) || 0 )
592 0 ▶ | ( ( $opt->{want_extrema} && BIN_ARG_WANT_EXTREMA ) || 0 );
Condition0%0/16
defined $opt->{'weight'} && 2 || 0
l!l
00
defined $opt->{"weight"} and 2
!ll
00
$opt->{'want_sum_weight'} && 16384 || 0
l!l
00
$opt->{"want_sum_weight"} and 16384
!ll
00
$opt->{'want_sum_weight2'} && 32768 || 0
l!l
00
$opt->{"want_sum_weight2"} and 32768
!ll
00
$opt->{'want_extrema'} && 8192 || 0
l!l
00
$opt->{"want_extrema"} and 8192
!ll
00
MC/DC0%0/1
defined $opt->{'weight'} && 2 || 0
defined $opt->{"weight"}
MC/DC0%0/1
$opt->{'want_sum_weight'} && 16384 || 0
$opt->{"want_sum_weight"}
MC/DC0%0/1
$opt->{'want_sum_weight2'} && 32768 || 0
$opt->{"want_sum_weight2"}
MC/DC0%0/1
$opt->{'want_extrema'} && 8192 || 0
$opt->{"want_extrema"}
Truth table0%0/2
A
A= defined $opt->{"weight"}
Aresult
11
00
Truth table0%0/2
A
A= $opt->{"want_sum_weight"}
Aresult
11
00
Truth table0%0/2
A
A= $opt->{"want_sum_weight2"}
Aresult
11
00
Truth table0%0/2
A
A= $opt->{"want_extrema"}
Aresult
11
00
593  
594 $opt->{flags}
595 |= $Map_OOB{ lc( $opt->{oob} ) } # prefer fc, but requires 5.16
596 0 ▶ // ( $opt->{oob} && BIN_ARG_SAVE_OOB_START_END )
Condition0%0/7
($Map_OOB{lc $opt->{'oob'}} // $opt->{'oob'} && 512) || 0
l!l
00
$Map_OOB{lc $opt->{"oob"}} // $opt->{"oob"} && 512
l!l&&r!l&&!r
000
$opt->{"oob"} and 512
!ll
00
MC/DC0%0/2
($Map_OOB{lc $opt->{'oob'}} // $opt->{'oob'} && 512) || 0
$Map_OOB{lc $opt->{"oob"}} $opt->{"oob"}
Truth table0%0/3
A // B
A= $Map_OOB{lc $opt->{"oob"}}B= $opt->{"oob"}
ABresult
1X1
011
000
597 || 0;
598  
599 0 my @pin = qw[ data index weight imin nbins ];
600 0 my @pout = qw[ b_count b_data b_weight b_weight2 b_mean b_dmin b_dmax b_dev2 ];
601 0 my @oargs = qw[ flags nbins_max ];
602  
603 # several of the input piddles are optional. the PP routine
604 # doesn't know that and will complain about the wrong number of
605 # dimensions if we pass a null piddle. A 1D one-element piddle
606 # will have its dimensions auto-expanded without much
607 # wasted memory.
608  
609 0 $opt->{$_} = $opt->{data}->zeroes( 1 ) for grep { !defined $opt->{$_} } @pin;
610  
611 # output and temp piddles will be auto-inflated, so we can use
612 # null piddles. In a perfect world PDL::PP would have a mechanism
613 # to avoid inflating the optional ones.
614 # TODO: use nullcreate here to get correct piddle type?
615  
616 0 $opt->{$_} = PDL->null for grep { !defined $opt->{$_} } @pout;
617  
618 0 my @pars;
619 0 if ( PP_PASS_TMPS ) {
620 my @ptmp = qw[ b_data_error b_weight_error b_weight2_error ];
621 $opt->{$_} = PDL->null for grep { !defined $opt->{$_} } @ptmp;
622 @pars = ( @pin, @pout, @ptmp, @oargs );
623 }
624 else {
625 0 @pars = ( @pin, @pout, @oargs );
626 }
627  
628 0 _bin_on_index_int( @{$opt}{@pars} );
629 my %results = map { ## no critic (BuiltinFunctions::ProhibitComplexMappings)
630 0 ( my $nkey = $_ ) =~ s/^b_//;
631 0 ( $nkey, $opt->{$_} )
632 } @pout;
633  
634 0 ▶ if ( $opt->{flags} & BIN_ARG_HAVE_WEIGHT ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{'flags'} & 2) { } 0 0 0
635 delete $results{weight}
636 0 ▶ unless $opt->{flags} & BIN_ARG_WANT_SUM_WEIGHT;
Branch: 1 branch
BranchTrueFalseTotal
unless $opt->{"flags"} & 16384 0 0 0
637 delete $results{weight2}
638 0 ▶ unless $opt->{flags} & BIN_ARG_WANT_SUM_WEIGHT2;
Branch: 1 branch
BranchTrueFalseTotal
unless $opt->{"flags"} & 32768 0 0 0
639 }
640 else {
641 0 delete @results{qw( weight weight2)};
642 }
643  
644 0 $results{imin} = $opt->{imin};
645 0 $results{nbins} = $opt->{nbins};
646  
647 0 return wrap_hash( \%results );
648 }
649  
650 sub _bin_on_index_parse_range_opts {
651  
652 0 ▶ my $opt = shift;
Subroutine _bin_on_index_parse_range_opts: not called CC=23 SCAR=63.1
653  
654 0 ▶ if ( $opt->{range} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{'range'}) { } 0 0 0
655  
656 0 my ( $dims, $range ) = $opt->{range} =~ $range_RE;
657 0 ▶ $range //= 'minmax';
Condition0%0/2
$range //= "minmax"
l!l
00
MC/DC0%0/1
$range //= "minmax"
$range
Truth table0%0/2
A
A= $range
Aresult
11
00
658  
659 eval {
660  
661 # treat the index as a one big pool of numbers, or
662 # as threaded piddles.
663 my ( $imin, $imax )
664 = $dims eq 'slice'
665 0 ? map { $_->dummy( 0 ) } $opt->{index}->minmaximum
666 0 ▶ : map { PDL::indx( [$_] ) } $opt->{index}->minmax;
Branch: 1 branch
BranchTrueFalseTotal
$dims eq 'slice' ? : 0 0 0
667  
668 0 ▶ if ( $range eq 'minmax' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($range eq 'minmax') { } 0 0 0
elsif ($range eq 'min') { } 0 0 0
elsif ($range eq 'max') { } 0 0 0
669  
670 die( "do not specify $_ when specifying 'minmax'\range with " )
671 0 for grep { defined $opt->{$_} } qw[ imin imax nbins ];
672  
673 0 $opt->{imin} = $imin;
674 0 $opt->{imax} = $imax;
675 }
676  
677 elsif ( $range eq 'min' ) {
678  
679 die( "do not specify imin when specifying 'min'\n" )
680 0 ▶ if defined $opt->{imin};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opt->{"imin"} 0 0 0
681  
682 die( "specify exactly one of 'imax' or 'nbins'\n" )
683 0 ▶ if defined $opt->{imax} && defined $opt->{nbins};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opt->{"imax"} and defined $opt->{"nbins"} 0 0 0
Condition0%0/3
defined $opt->{"imax"} and defined $opt->{"nbins"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $opt->{"imax"} and defined $opt->{"nbins"}
defined $opt->{"imax"} defined $opt->{"nbins"}
Truth table0%0/3
A and B
A= defined $opt->{"imax"}B= defined $opt->{"nbins"}
ABresult
0X0
100
111
684  
685 0 $opt->{imin} = $imin;
686 }
687  
688 elsif ( $range eq 'max' ) {
689 die( "do not specify imax\n" )
690 0 ▶ if defined $opt->{imax};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opt->{"imax"} 0 0 0
691  
692 die( "specify exactly one of 'imin' or 'nbins'\n" )
693 0 ▶ if defined $opt->{imin} && defined $opt->{nbins};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opt->{"imin"} and defined $opt->{"nbins"} 0 0 0
Condition0%0/3
defined $opt->{"imin"} and defined $opt->{"nbins"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $opt->{"imin"} and defined $opt->{"nbins"}
defined $opt->{"imin"} defined $opt->{"nbins"}
Truth table0%0/3
A and B
A= defined $opt->{"imin"}B= defined $opt->{"nbins"}
ABresult
0X0
100
111
694  
695 0 $opt->{imax} = $imax;
696 }
697 0 1;
698 0 ▶ } // do {
Condition0%0/3
eval { do { () = $dims eq "slice" ? map({$_->dummy(0);} $opt->{"index"}->minmaximum) : map({PDL::indx([$_]);} $opt->{"index"}->minmax); if ($range eq "minmax") { die "do not specify $_ when specifying 'minmax'\range with " foreach (grep {defined $opt->{$_};} "imin", "imax", "nbins"); $opt->{"imin"} = $imin; $opt->{"imax"} = $imax; } elsif ($range eq "min") { die "do not specify imin when specifying 'min'\n" if defined $opt->{"imin"}; die "specify exactly one of 'imax' or 'nbins'\n" if defined $opt->{"imax"} and defined $opt->{"nbins"}; $opt->{"imin"} = $imin; } elsif ($range eq "max") { die "do not specify imax\n" if defined $opt->{"imax"}; die "specify exactly one of 'imin' or 'nbins'\n" if defined $opt->{"imin"} and defined $opt->{"nbins"}; $opt->{"imax"} = $imax; } ; 1 } } // do { my $error = $@; chomp $error; Carp::croak($opt->{"range"}, ": error: $error\n") }
l!l&&r!l&&!r
000
MC/DC0%0/2
eval { do { () = $dims eq "slice" ? map({$_->dummy(0);} $opt->{"index"}->minmaximum) : map({PDL::indx([$_]);} $opt->{"index"}->minmax); if ($range eq "minmax") { die "do not specify $_ when specifying 'minmax'\range with " foreach (grep {defined $opt->{$_};} "imin", "imax", "nbins"); $opt->{"imin"} = $imin; $opt->{"imax"} = $imax; } elsif ($range eq "min") { die "do not specify imin when specifying 'min'\n" if defined $opt->{"imin"}; die "specify exactly one of 'imax' or 'nbins'\n" if defined $opt->{"imax"} and defined $opt->{"nbins"}; $opt->{"imin"} = $imin; } elsif ($range eq "max") { die "do not specify imax\n" if defined $opt->{"imax"}; die "specify exactly one of 'imin' or 'nbins'\n" if defined $opt->{"imin"} and defined $opt->{"nbins"}; $opt->{"imax"} = $imax; } ; 1 } } // do { my $error = $@; chomp $error; Carp::croak($opt->{"range"}, ": error: $error\n") }
eval { do { () = $dims eq "slice" ? map({$_->dummy(0);} $opt->{"index"}->minmaximum) : map({PDL::indx([$_]);} $opt->{"index"}->minmax); if ($range eq "minmax") { die "do not specify $_ when specifying 'minmax'\range with " foreach (grep {defined $opt->{$_};} "imin", "imax", "nbins"); $opt->{"imin"} = $imin; $opt->{"imax"} = $imax; } elsif ($range eq "min") { die "do not specify imin when specifying 'min'\n" if defined $opt->{"imin"}; die "specify exactly one of 'imax' or 'nbins'\n" if defined $opt->{"imax"} and defined $opt->{"nbins"}; $opt->{"imin"} = $imin; } elsif ($range eq "max") { die "do not specify imax\n" if defined $opt->{"imax"}; die "specify exactly one of 'imin' or 'nbins'\n" if defined $opt->{"imin"} and defined $opt->{"nbins"}; $opt->{"imax"} = $imax; } ; 1 } } do { my $error = $@; chomp $error; Carp::croak($opt->{"range"}, ": error: $error\n") }
Truth table0%0/3
A // B
A= eval { do { () = $dims eq "slice" ? map({$_->dummy(0);} $opt->{"index"}->minmaximum) : map({PDL::indx([$_]);} $opt->{"index"}->minmax); if ($range eq "minmax") { die "do not specify $_ when specifying 'minmax'\range with " foreach (grep {defined $opt->{$_};} "imin", "imax", "nbins"); $opt->{"imin"} = $imin; $opt->{"imax"} = $imax; } elsif ($range eq "min") { die "do not specify imin when specifying 'min'\n" if defined $opt->{"imin"}; die "specify exactly one of 'imax' or 'nbins'\n" if defined $opt->{"imax"} and defined $opt->{"nbins"}; $opt->{"imin"} = $imin; } elsif ($range eq "max") { die "do not specify imax\n" if defined $opt->{"imax"}; die "specify exactly one of 'imin' or 'nbins'\n" if defined $opt->{"imin"} and defined $opt->{"nbins"}; $opt->{"imax"} = $imax; } ; 1 } }B= do { my $error = $@; chomp $error; Carp::croak($opt->{"range"}, ": error: $error\n") }
ABresult
1X1
011
000
699 0 my $error = $@;
700 0 chomp $error;
701 0 Carp::croak( $opt->{range}, ": error: $error\n" );
702 };
703  
704 }
705 else {
706  
707 0 my $nrange_opts = grep { defined $opt->{$_} } qw[ imin imax nbins ];
708  
709 0 ▶ if ( $nrange_opts == 0 ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($nrange_opts == 0) { } 0 0 0
elsif ($nrange_opts == 1) { } 0 0 0
elsif ($nrange_opts == 3) { } 0 0 0
710  
711 0 @{$opt}{ 'imin', 'imax' }
712 0 = map { PDL::indx( $_ ) } $opt->{index}->minmax;
713 }
714  
715 elsif ( $nrange_opts == 1 ) {
716  
717 0 ▶ if ( defined $opt->{nbins} ) {
Branch: 3 branches
BranchTrueFalseTotal
if (defined $opt->{'nbins'}) { } 0 0 0
elsif (defined $opt->{'imax'}) { } 0 0 0
elsif (defined $opt->{'imin'}) { } 0 0 0
718 0 $opt->{imin} = PDL::indx( 0 );
719 }
720 elsif ( defined $opt->{imax} ) {
721 0 $opt->{imin} = PDL::indx( 0 );
722 }
723 elsif ( defined $opt->{imin} ) {
724 0 $opt->{imax} = PDL::indx( $opt->{index}->max );
725 }
726 }
727 elsif ( $nrange_opts == 3 ) {
728 0 Carp::croak( "error: too many options. specify only two of imin, imax, nbins\n" );
729 }
730 }
731  
732 # don't care about imax.
733  
734 0 ▶ $opt->{imin} //= $opt->{imax} - $opt->{nbins} + 1;
Condition0%0/3
$opt->{"imin"} //= $opt->{"imax"} - $opt->{"nbins"} + 1
l!l&&r!l&&!r
000
MC/DC0%0/2
$opt->{"imin"} //= $opt->{"imax"} - $opt->{"nbins"} + 1
$opt->{"imin"} $opt->{"imax"} - $opt->{"nbins"} + 1
Truth table0%0/3
A //= B
A= $opt->{"imin"}B= $opt->{"imax"} - $opt->{"nbins"} + 1
ABresult
1X1
011
000
735 0 ▶ $opt->{nbins} //= $opt->{imax} - $opt->{imin} + 1;
Condition0%0/3
$opt->{"nbins"} //= $opt->{"imax"} - $opt->{"imin"} + 1
l!l&&r!l&&!r
000
MC/DC0%0/2
$opt->{"nbins"} //= $opt->{"imax"} - $opt->{"imin"} + 1
$opt->{"nbins"} $opt->{"imax"} - $opt->{"imin"} + 1
Truth table0%0/3
A //= B
A= $opt->{"nbins"}B= $opt->{"imax"} - $opt->{"imin"} + 1
ABresult
1X1
011
000
736 0 $opt->{nbins_max} = $opt->{nbins}->max;
737 }
738  
739 }
740  
741 =pod
742  
743 =for stopwords
744 ACM
745 Bevington
746 Commun
747 Golub
748 Higham
749 Kahan
750 LeVeque
751 McGraw
752 superset
753  
754 =head2 bin_on_index
755  
756 =for usage
757  
758 $hashref = bin_on_index( %pars );
759  
760 =for ref
761  
762 Calculate basic statistics on optionally weighted, binned data using a
763 provided index. The input data are assumed to be one-dimensional; extra
764 dimensions are threaded over.
765  
766 This routine ignores data with bad values or with weights that have bad values.
767  
768 =head3 Description
769  
770 When generating statistics for multiple-component data binned on a
771 common component, it's more efficient to calculate a bin index for the
772 common component and then use it to generate statistics for each component.
773  
774 For example, if a time dependent stream of events is binned into time
775 intervals, statistics of the events' properties (such as position or
776 energy) must be evaluated on data binned on the intervals.
777  
778 Some statistics (such as the summed squared deviation from the mean)
779 may be calculated in two passes over the data, but may suffer from
780 numerical inaccuracies depending upon the magnitude of the
781 intermediate values.
782  
783 C<bin_on_index> uses numerically stable algorithms to calculate
784 various statistics on binned data in a single pass. It uses an index
785 piddle to assign data to bins and calculates basic statistics on the
786 data in those bins. Data may have associated weights, and the index
787 piddle need not be sorted. It by default ignores out-of-bounds data, but
788 can be directed to operate on them.
789  
790 The statistics which are returned for each bin are:
791  
792 =over
793  
794 =item *
795  
796 The number of elements, I<N>
797  
798 =item *
799  
800 The (weighted) sum of the data,
801 I<< Sum(x_i) >>,
802 I<< Sum(w_i * x_i) >>.
803  
804 =item *
805  
806 The (weighted) mean of the data,
807 I<< Sum(x_i) / N >>,
808 I<< Sum(w_i * x_i) / Sum(w_i) >>
809  
810 =item *
811  
812 The sum of the weights, I<< Sum(w_i) >>
813  
814 =item *
815  
816 The sum of the square of the weights, I<< Sum(w_i^2) >>
817  
818 =item *
819  
820 The sum of the (weighted) squared deviation of the data from mean,
821 I<< Sum( (x_i-u)^2 ) >>,
822 I<< Sum( w_i(x_i-u)^2 ) >>. These are I<not> normalized!.
823  
824 =item *
825  
826 The minimum and maximum data values
827  
828 =back
829  
830 The sum of the squared deviations are I<not> normalized, to allow the
831 user to handle it according to their needs.
832  
833 For unweighted data, the typical normalization factor is I<N-1>,
834 while for weighted data, the normalization factor varies depending
835 upon whether the weights represent errors or quality weights. In the
836 former case, where I<< w_i = 1 / sigma^2 >>, the normalization factor
837 is typically I<< N / (N - 1) / Sum(w_i) >>, while for the latter the
838 normalization is typically I<< Sum(w_i) / ( Sum(w_i)^2 - Sum(w_i^2) ) >>.
839 See L</[Robinson]> and L</[Bevington]>.
840  
841 The algorithms used are chosen for their numerical stability. Sums
842 are computed using Kahan summation and the mean and squared deviations are
843 calculated with stable updating algorithms. See L</[Chan83]>, L</[West79]>, L</[Higham]>.
844  
845 =head4 Threading
846  
847 The parameters L</data>, L</index>, L</weight>, L</imin>, L</imax>, and
848 L</nbins> are threaded over. This keeps things quite flexible, as one
849 can specialize things for complex datasets or keep them simple by
850 specifying the minimum information for a given parameter to have it
851 remain constant over the threaded dimensions. (Note that C<data>
852 must have the same shape or be a superset of the other parameters).
853  
854 Unless otherwise specified, the term I<extents> refers to those of the
855 core (non-threaded) one-dimensional slices of the input and output
856 piddles.
857  
858 For some parameters there is value in applying an algorithm to
859 either the entire dataset (including threaded dimensions) or just
860 the core one-dimensional data. For example, the L</range>
861 option can indicate that the in-bounds bins are defined by the range
862 in each one-dimensional slice of L</index> or in L</index> as a whole.
863  
864 =head4 Minimum Bin Index, Number of Bins, Out-Of-Bounds Data
865  
866 By default, the minimum bin index and the number of bins are
867 determined from the values in the L</index> piddle, treating it as if
868 it were one-dimensional. Statistics for data with the minimum index
869 are stored in the results piddles at index I<0>.
870  
871 The caller may use the options L</imin>, L</imax>, and
872 L</nbins>, and L</range> to change how the index range is mapped onto
873 the results piddles, and whether the range should be specific to each
874 one-dimensional slice of L</index>. If none of these are specified,
875 the default is equivalent to setting L</range> to C<flat,minmax>.
876 To most efficiently store the statistics, set L</range> to C<slice,minmax>.
877  
878 Data with indices outside of the range C<[$imin, $imin + $nbins - 1]>
879 are treated as I<out-of-bounds> data, and by default are ignored. If the
880 user wishes to accumulate statistics on these data, the L</oob> option
881 may be used to indicate where in the results piddles the statistics
882 should be stored.
883  
884 =head3 Parameters
885  
886 B<bin_on_index> is passed a hash or a reference to a hash containing
887 its parameters. The possible parameters are:
888  
889 =over
890  
891 =item C<data> I<piddle>
892  
893 The data.
894 I<Required>
895  
896 =item C<index> I<piddle>
897  
898 The bin index. It should be of type C<indx>, but will be converted to that if not.
899 It must be thread compatible with L</data>.
900 I<Required>
901  
902 =item C<weight> I<piddle>
903  
904 Data weight.
905 It must be thread compatible with L</data>.
906 I<Optional>
907  
908 =item C<nbins> I<integer> | I<piddle>
909  
910 The number of bins.
911 It must be thread compatible with L</data>.
912  
913 If C<nbins> is set and neither of C<imin> or C<imax> are set, C<imin> is set to C<0>.
914  
915 Use L</range> for more control over automatic determination of the range.
916  
917 =item C<imin> I<integer> | I<piddle>
918  
919 The index value associated with the first in-bounds index in the
920 result piddle.
921 It must be thread compatible with L</data>.
922  
923 If C<imin> is set and neither of C<nbins> or C<imax> are set, C<imax> is set to C<< $index->max >>.
924  
925 Use L</range> for more control over automatic determination of the range.
926  
927 =item C<imax> I<integer> | I<piddle>
928  
929 The index value associated with the last in-bounds index in the
930 result piddle. It must be thread compatible with L</data>.
931  
932 If C<imax> is set and neither of C<nbins> or C<imin> are set, C<imin> is set to C<< 0 >>.
933  
934 Use L</range> for more control over automatic determination of the range.
935  
936 =item C<range> I<"spec,spec,...">
937  
938 Determine the in-bounds range of indices from L</index>. The value is
939 a string containing a list of comma separated specifications.
940  
941 The first element must be one of the following values:
942  
943 =over
944  
945 =item C<flat>
946  
947 Treat L</index> as one-dimensional, and determine a single range which
948 covers it.
949  
950 =item C<slice>
951  
952 Determine a separate range for each one-dimensional slice of L</index>.
953  
954 =back
955  
956 It may optionally be followed by one of the following
957  
958 =over
959  
960 =item C<minmax>
961  
962 Determine the full range (i.e. both minimum and maximum) from
963 L</index>. This is the default. Do not specify L</nbins>,
964 L</imax>, or L</imin>.
965  
966 =item C<min>
967  
968 Determine the minimum of the range from L</index>. Specify only one of L</nbins>
969 or L</imax>.
970  
971 =item C<max>
972  
973 Determine the maximum of the range from L</index>. Specify only one of L</nbins>
974 or L</imin>.
975  
976 =back
977  
978 =item C<oob>
979  
980 An index is out-of-bounds if it is not within the range
981  
982 [ $imin, $imin + $nbins )
983  
984 By default, it will be ignored.
985  
986 This option specifies where in the results piddles the out-of-bound
987 data statistics should be stored. It may be one of:
988  
989 =over
990  
991 =item C<start-end>
992  
993 The extent of the results piddle is increased by two, and out-of-bound
994 statistics are written as follows:
995  
996 $index - $imin < 0 ==> $result->at(0)
997 $index - $imin >= $nbins ==> $result->at(-1)
998  
999 =item C<start-nbins>
1000  
1001 The extent of the results piddle is increased by two, and out-of-bound
1002 statistics are written as follows:
1003  
1004 $index - $imin < 0 ==> $result->at(0)
1005 $index - $imin >= $nbins ==> $result->at($nbins)
1006  
1007 This differs from C<start-end> if C<$nbins> is different for each
1008 one-dimensional slice.
1009  
1010 =item C<end>
1011  
1012 The extent of the results piddle is increased by two, and out-of-bound
1013 statistics are written as follows:
1014  
1015 $index - $imin < 0 ==> $result->at(-2)
1016 $index - $imin >= $nbins ==> $result->at(-1)
1017  
1018 =item C<nbins>
1019  
1020 The extent of the results piddle is increased by two, and out-of-bound
1021 statistics are written as follows:
1022  
1023 $index - $imin < 0 ==> $result->at($nbins-1)
1024 $index - $imin >= $nbins ==> $result->at($nbins)
1025  
1026 This differs from C<end> if C<$nbins> is different for each
1027 one-dimensional slice.
1028  
1029 =item I<boolean>
1030  
1031 If false (the default) out-of-bounds data are ignored. If true, it is
1032 equivalent to specifying L</start-end>
1033  
1034 =back
1035  
1036 =item C<want_sum_weight> I<boolean> I<[false]>
1037  
1038 if true, the sum of the bins' weights are calculated.
1039  
1040 =item C<want_sum_weight2> I<boolean> I<[false]>
1041  
1042 if true, the sum of square of the bins' weights are calculated.
1043  
1044 =back
1045  
1046 =head3 Results
1047  
1048 B<bin_on_index> returns a reference which may be used either a hash
1049 or object reference.
1050  
1051 The keys (or methods) and their values are as follows:
1052  
1053 =over
1054  
1055 =item C<data>
1056  
1057 A piddle containing the (possibly weighted) sum of the data in each bin.
1058  
1059 =item C<count>
1060  
1061 A piddle containing the number of data elements in each bin.
1062  
1063 =item C<weight>
1064  
1065 A piddle containing the sum of the weights in each bin (only if
1066 L</weight> was provided and L</want_sum_weight> specified).
1067  
1068 =item C<weight2>
1069  
1070 A piddle containing the sum of the square of the weights in each bin
1071 (only if L</weight> was provided and L</want_sum_weight2> specified).
1072  
1073 =item C<mean>
1074  
1075 A piddle containing the (possibly weighted) mean of the data in each bin.
1076  
1077 =item C<dmin>
1078  
1079 =item C<dmax>
1080  
1081 Piddles containing the minimum and maximum values of the data in each bin.
1082  
1083 =item C<imin>
1084  
1085 =item C<nbins>
1086  
1087 The index value associated with the first in-bounds index
1088 in the statistics piddles, and the number of in-bounds bins.
1089  
1090 =back
1091  
1092 =head3 References
1093  
1094 =over
1095  
1096 =item [Chan83]
1097  
1098 Chan, Tony F., Golub, Gene H., and LeVeque, Randall J., Algorithms for
1099 Computing the Sample Variance: Analysis and Recommendations, The
1100 American Statistician, August 1983, Vol. 37, No.3, p 242.
1101  
1102 =item [West79]
1103  
1104 D. H. D. West. 1979. Updating mean and variance estimates: an improved
1105 method. Commun. ACM 22, 9 (September 1979), 532-535.
1106  
1107 =item [Higham]
1108  
1109 Higham, N. (1996). Accuracy and stability of numerical
1110 algorithms. Philadelphia: Society for Industrial and Applied
1111 Mathematics.
1112  
1113 =item [Robinson]
1114  
1115 Robinson, E.L., Data Analysis for Scientists and Engineers, 2016,
1116 Princeton University Press. ISBN 978-0-691-16992-7.
1117  
1118 =item [Bevington]
1119  
1120 Bevington, P.R., Robinson, D.K., Data Reduction and Error Analysis for
1121 the Physical Sciences, Second Edition, 1992, McGraw-Hill, ISBN
1122 0-07-91243-9.
1123  
1124 =back
1125  
1126 =cut
1127  
1128  
1129  
1130  
1131  
1132  
1133  
1134  
1135  
1136  
1137 #line 407 "lib/CXC/PDL/Bin1D.pd"
1138  
1139 =head1 BUGS AND LIMITATIONS
1140  
1141 No bugs have been reported.
1142  
1143 =head1 AUTHOR
1144  
1145 Diab Jerius, E<lt>djerius@cpan.orgE<gt>
1146  
1147 =head1 COPYRIGHT AND LICENSE
1148  
1149 Copyright 2008 Smithsonian Astrophysical Observatory
1150  
1151 CXC::PDL::Bin1D is free software: you can redistribute it and/or modify
1152 it under the terms of the GNU General Public License as published by
1153 the Free Software Foundation, either version 3 of the License, or (at
1154 your option) any later version.
1155  
1156 This program is distributed in the hope that it will be useful,
1157 but without any warranty; without even the implied warranty of
1158 merchantability or fitness for a particular purpose. See the
1159 GNU General Public License for more details.
1160  
1161 You should have received a copy of the GNU General Public License
1162 along with this program. If not, see L<http://www.gnu.org/licenses/>.
1163  
1164 =cut
1165 #line 1166 "Bin1D.pm"
1166  
1167 # Exit with OK status
1168  
1169 1;
↑ Summary blib/lib/CXC/PDL/Bin1D/Utils.pm »