lib/CXC/PDL/Bin1D.pd

filter: Statementstmt 100% 20 / 20 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 7 / 7 Podpod n/a totaltotal 100% 27 / 27 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« Bin1D.xs ↑ Summary
1 #!perl
2  
3 # --8<--8<--8<--8<--
4 #
5 # Copyright (C) 2014 Smithsonian Astrophysical Observatory
6 #
7 # This file is part of CXC::PDL::Bin1D
8 #
9 # CXC::PDL::Bin1D is free software: you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation, either version 3 of the License, or
12 # (at your option) any later version.
13 #
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License
20 # along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #
22 # -->8-->8-->8-->8--
23  
24 use v5.10;
25  
26 use strict;
27 use warnings;
28 use version;
29  
30 use IO::File;
31 use File::Spec::Functions qw[ catfile ];
32  
33 use lib 'lib';
34  
35 use CXC::PDL::Bin1D::Utils '_bitflags';
36 use PDL::Bad;
37  
38 use File::Basename qw[ fileparse ];
39  
40 # this file is executed directly by Perl, so $0 will be our filename,
41 # which allows us to find the parent directory.
42 use constant LIBDIR => ( fileparse( $0 ) )[1];
43  
44 use PDL::Types qw[ typefld ];
45  
46  
47 # later versions of PDL define index types:
48 # 'PDL_Indx' (as a C typedef) and 'indx' for signatures
49 # for backwards compatibility, support long as well as the others
50  
51 my ( $IndexParsType, $IndexCType ) = do {
52  
53 my $type = eval { typefld( 'PDL_IND', 'ctype' ); 1 } ? 'PDL_IND' : 'PDL_L';
54  
55 map { typefld( $type, $_ ) } qw( ppforcetype ctype );
56 };
57  
58 our $VERSION = '0.27';
59 pp_setversion( $VERSION );
60  
61 {
62 package CXC::PDL::Bin1D;
63  
64 use Text::Template::LocalVars;
65 use File::Spec::Functions qw[ catfile ];
66  
67 use Exporter 'import';
68 ## no critic (Modules::ProhibitAutomaticExportation)
69 our @EXPORT = qw< fill_in fill_in_string >;
70  
71 # this code must be compiled in the template variable package
72 sub init_fill_package {
73  
74 Text::Template::LocalVars->new(
75 type => 'string',
76 delimiters => [qw( <% %> )],
77 source => <<'EOS',
78 *fill_in = \&<% $PACKAGE %>::fill_in;
79 *fill_in_string = \&<% $PACKAGE %>::fill_in_string;
80 EOS
81 )->fill_in( hash => { PACKAGE => __PACKAGE__ } );
82  
83 }
84  
85 # fill in templates using Text::Template::LocalVars. This routine
86 # is also imported into all of the template fragments.
87 sub fill_in {
88  
89 my ( $type, $src ) = ( shift, shift );
90  
91 $src = catfile( ::LIBDIR, $src )
92 if lc $type eq 'file' && $src !~ m{^[./]}s;
93  
94 my $tpl = Text::Template::LocalVars->new(
95 type => $type,
96 source => $src,
97 delimiters => [qw( <% %> )],
98 broken => sub { my %args = @_; die $args{error}; },
99 prepend => init_fill_package(),
100 );
101  
102 die $Text::Template::ERROR unless defined $tpl;
103  
104 # by default, localize the variable packages, and insert the last one
105 # in the call chain if not explicitly specified.
106 my %args = ( localize => 1, trackpkgvars => 1, @_ );
107  
108 my $hash = $args{hash} ||= {};
109 $hash->{PDL_Indx} = $IndexCType;
110  
111 my $txt;
112 eval {
113 $txt = $tpl->fill_in( %args );
114 die unless defined $txt;
115 1;
116 }
117 or die defined $Text::Template::LocalVars::ERROR
118 ? $Text::Template::LocalVars::ERROR
119 : $@;
120  
121 return $txt;
122 }
123  
124 sub fill_in_string {
125  
126 unshift @_, 'string';
127 goto \&fill_in;
128  
129 }
130  
131 }
132  
133 CXC::PDL::Bin1D->import;
134  
135  
136 my %CONSTANTS = (
137 BIN_ARG => {
138 _bitflags( qw[
139 BIN_ARG_HAVE_ERROR
140 BIN_ARG_HAVE_WEIGHT
141 BIN_ARG_SET_BAD
142 BIN_ARG_FOLD
143 BIN_ARG_HAVE_WIDTH
144 BIN_ARG_ERROR_SDEV
145 BIN_ARG_ERROR_POISSON
146 BIN_ARG_ERROR_RSS
147 BIN_ARG_HAVE_SIGNAL
148 BIN_ARG_SAVE_OOB_START_END
149 BIN_ARG_SAVE_OOB_START_NBINS
150 BIN_ARG_SAVE_OOB_END
151 BIN_ARG_SAVE_OOB_NBINS
152 BIN_ARG_WANT_EXTREMA
153 BIN_ARG_WANT_SUM_WEIGHT
154 BIN_ARG_WANT_SUM_WEIGHT2
155 ],
156 ),
157 },
158 BIN_RC => {
159 _bitflags( qw[
160 BIN_RC_OK
161 BIN_RC_GEWMAX
162 BIN_RC_GENMAX
163 BIN_RC_FOLDED
164 BIN_RC_GTMINSN
165 ],
166 ),
167  
168 },
169 );
170  
171 {
172 $CONSTANTS{BIN_ARG}{BIN_ARG_SAVE_OOB} = 0;
173 $CONSTANTS{BIN_ARG}{BIN_ARG_SAVE_OOB} |= $CONSTANTS{BIN_ARG}{$_}
174 for grep { /BIN_ARG_SAVE_OOB/ } keys %{ $CONSTANTS{BIN_ARG} };
175  
176 $CONSTANTS{BIN_ARG}{BIN_ARG_SHIFT_IMIN} = $CONSTANTS{BIN_ARG}{BIN_ARG_SAVE_OOB_START_END}
177 | $CONSTANTS{BIN_ARG}{BIN_ARG_SAVE_OOB_START_NBINS};
178 }
179  
180  
181 # convert hash into [key => value] tuples, sorted by group, then value in group
182 ## no critic (BuiltinFunctions::ProhibitComplexMappings)
183 my @CONSTANTS = map {
184 my $h = $_;
185 map { [ $_, $h->{$_} ] } sort { $h->{$a} <=> $h->{$b} } keys %$h;
186 } values %CONSTANTS;
187  
188 my @CONSTANT_NAMES = ( map { keys %$_ } values %CONSTANTS );
189 my @EXPORT_OK;
190  
191 sub slurp {
192 my $file = catfile( LIBDIR, shift );
193 local $/ = undef;
194 ( IO::File->new( $file, 'r' ) or die( "can't slurp $file" ) )->getline;
195 }
196  
197  
198 pp_core_importList( '()' );
199  
200 pp_bless( 'CXC::PDL::Bin1D' );
201  
202 pp_addpm( { At => 'Top' }, <<'EOD' );
203 4 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
204 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
205 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
206 4 ▶no namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
207 4 ▶use CXC::PDL::Bin1D::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
208 4 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
209  
210 EOD
211  
212 if ( version->parse( $PDL::VERSION ) < version->parse( '2.030' ) ) {
213 pp_addpm( { At => 'Top' }, <<'EOD' );
214 our @EXPORT_OK;
215 our %EXPORT_TAGS;
216 EOD
217 }
218  
219 pp_addpm(
220 { At => 'Top' },
221 join( "\n", 'use constant {', ( map { "$_->[0] => $_->[1]," } @CONSTANTS ), '};', q{} ),
222 4 );
223  
224 pp_addpm(
225 { At => 'Top' },
226 join( "\n", '=for Pod::Coverage', ( map { "$_->[0]" } @CONSTANTS ), q{}, '=cut', q{} ),
227 );
228  
229 pp_addpm( { At => 'Top' }, <<'EOD' );
230 my %MapErrorAlgo = (
231  
232 sdev => BIN_ARG_ERROR_SDEV,
233 rss => BIN_ARG_ERROR_RSS,
234 poisson => BIN_ARG_ERROR_POISSON,
235 );
236 EOD
237  
238 pp_addpm( { At => 'Top' }, <<'EOD' );
239 =for Pod::Coverage
240 set_boundscheck
241 set_debugging
242  
243 =for stopwords
244 merchantability
245 4 ▶ 
Subroutine BEGIN: called CC=1 SCAR=0.0
246 =cut
247  
248 =head1 NAME
249  
250 CXC::PDL::Bin1D - one dimensional binning functions
251  
252 =head1 SYNOPSIS
253  
254 use PDL;
255 use CXC::PDL::Bin1D;
256  
257 =head1 DESCRIPTION
258  
259 One dimensional binning functions,
260  
261 =over
262  
263 =item *
264  
265 binning up to a given S/N
266  
267 =item *
268  
269 optimized one-pass robust statistics
270  
271 =back
272  
273 All functions are made available in the B<CXC::PDL::Bin1D> namespace.
274  
275 =head1 SUBROUTINES
276  
277 =cut
278 EOD
279  
280 pp_addhdr(
281 join( "\n",
282 '#include <float.h>',
283 '#include <limits.h>',
284 map { "#define $_->[0] $_->[1]" } @CONSTANTS )
285 . "\n",
286 );
287  
288  
289 push @EXPORT_OK, 'bin_adaptive_snr';
290 pp_def(
291 'bin_adaptive_snr',
292 Pars => join(
293 q{;},
294 'signal(n)', # piddle containing signal to bin
295 'error(n)', # error piddle if flags && HAVE_ERROR or HAVE_ERROR2
296 'width(n)', # optional width for each signal datum
297 "$IndexParsType [o] index(n)", # output index
298 "$IndexParsType [o] nbins()",
299 "$IndexParsType [o] nelem(n)",
300 'double [o] b_signal(n)',
301 'double [o] b_error(n)',
302 'double [o] b_mean(n)',
303 'double [o] b_snr(n)',
304 'double [o] b_width(nwidth)',
305 "$IndexParsType [o] ifirst(n)",
306 "$IndexParsType [o] ilast(n)",
307 'int [o] rc(n)',
308 'double [t] b_error2(nrss)',
309 'double [t] b_signal2(nsdev)',
310 'double [t] b_m2(nsdev)',
311 'double [t] b_weight(nweight)', # use only for SDEV (with or without errors) & RSS
312 'double [t] b_weight_sig(nwsdev)',
313 'double [t] b_weight_sig2(nwsdev)',
314 ),
315 RedoDimsCode => fill_in_string(
316 <<'EOS'
317 PDL_Indx n = $PDL(signal)->dims[0];
318 $SIZE(nrss) = $COMP(optflags) & BIN_ARG_ERROR_RSS ? n : 0 ;
319 $SIZE(nwidth) = $COMP(optflags) & BIN_ARG_HAVE_WIDTH ? n : 0 ;
320 $SIZE(nsdev) = $COMP(optflags) & BIN_ARG_ERROR_SDEV ? n : 0 ;
321 $SIZE(nwsdev) = $COMP(optflags) & (BIN_ARG_ERROR_SDEV | BIN_ARG_HAVE_ERROR )
322 == (BIN_ARG_ERROR_SDEV | BIN_ARG_HAVE_ERROR ) ? n : 0 ;
323 $SIZE(nweight) = $COMP(optflags) & (BIN_ARG_ERROR_SDEV | BIN_ARG_ERROR_RSS) ? n : 0 ;
324 EOS
325 ),
326 OtherPars => fill_in_string(
327 join(
328 q{;},
329 'unsigned long optflags'
330 , # can't call it flags; clashes with PDL internals
331 'double min_snr',
332 'PDL_Indx min_nelem',
333 'PDL_Indx max_nelem',
334 'double min_width',
335 'double max_width',
336 ),
337 ),
338 Code => fill_in(
339 file => 'bin_adaptive_snr.c',
340 package => 'bin_adaptive_snr',
341 ),
342 HandleBad => 1,
343 BadCode => fill_in(
344 file => 'bin_adaptive_snr.c',
345 package => 'bin_adaptive_snr_bad',
346 hash => { PDL_BAD_CODE => 1 },
347 ),
348 PMCode => slurp( 'bin_adaptive_snr.pl' ),
349 PMFunc => q{},
350 Doc => undef,
351 );
352  
353 push @EXPORT_OK, 'bin_on_index';
354 pp_def(
355 'bin_on_index',
356 Pars => join(
357 q{;},
358  
359 # inputs
360 'data(n)', # piddle containing data to bin
361 "$IndexParsType index(n)", # input index
362 'weight(n)', # populated if flags && HAVE_WEIGHT
363 "$IndexParsType imin(nt)", # minimum index value to consider
364 "$IndexParsType nbins(nt)", # number of bins
365  
366 # outputs
367 "$IndexParsType [o] b_count(nb)", # number of data values
368 'double [o] b_data(nb)', # sum of the data
369 'double [o] b_weight(nb)',
370 'double [o] b_weight2(nb)',
371 'double [o] b_mean(nb)',
372 'double [o] b_dmin(nb)',
373 'double [o] b_dmax(nb)',
374 'double [o] b_dev2(nb)',
375  
376 # temporaries
377 'double [t] b_data_error(nb)',
378 'double [t] b_weight_error(nb)',
379 'double [t] b_weight2_error(nb)',
380 ),
381 OtherPars => fill_in_string(
382 join(
383 q{;},
384 'unsigned long optflags', # can't call it flags; clashes with PDL internals
385 'PDL_Indx nbins_max', # maximum number of bins
386 ),
387 ),
388 RedoDimsCode =>
389 q/ $SIZE(nb) = $COMP(nbins_max) + ($COMP(optflags) & BIN_ARG_SAVE_OOB ? 2 : 0 ); /,
390 Code => fill_in(
391 file => 'bin_on_index.c',
392 package => 'bin_on_index',
393 ),
394 HandleBad => 1,
395 BadCode => fill_in(
396 file => 'bin_on_index.c',
397 package => 'bin_on_index_bad',
398 hash => { PDL_BAD_CODE => 1 },
399 ),
400 PMCode => slurp( 'bin_on_index.pl' ),
401 PMFunc => q{},
402 Doc => undef,
403 );
404  
405  
406  
407 pp_addpm( { At => 'Bot' }, <<'EOD' );
408  
409 =head1 BUGS AND LIMITATIONS
410  
411 No bugs have been reported.
412  
413 =head1 AUTHOR
414  
415 Diab Jerius, E<lt>djerius@cpan.orgE<gt>
416  
417 =head1 COPYRIGHT AND LICENSE
418  
419 Copyright 2008 Smithsonian Astrophysical Observatory
420  
421 CXC::PDL::Bin1D is free software: you can redistribute it and/or modify
422 it under the terms of the GNU General Public License as published by
423 the Free Software Foundation, either version 3 of the License, or (at
424 your option) any later version.
425  
426 This program is distributed in the hope that it will be useful,
427 but without any warranty; without even the implied warranty of
428 merchantability or fitness for a particular purpose. See the
429 GNU General Public License for more details.
430  
431 You should have received a copy of the GNU General Public License
432 along with this program. If not, see L<http://www.gnu.org/licenses/>.
433  
434 =cut
435 EOD
436  
437  
438 pp_export_nothing();
439  
440 pp_addpm( { At => 'Top' },
441 qq[\$EXPORT_TAGS{constants} = [ qw(\n@{[ join "\n", @CONSTANT_NAMES ]}\n)];\n] );
442 pp_addpm( { At => 'Top' }, qq[\$EXPORT_TAGS{Func} = [ qw(\n@{[ join "\n", @EXPORT_OK ]}\n)];\n] );
443 pp_addpm( { At => 'Top' }, q[@EXPORT_OK = map { @{$_} } values %EXPORT_TAGS] . ";\n" );
444  
445 pp_done();
446  
447 1;
« Bin1D.xs ↑ Summary