lib/PDL/Bad.pd

filter: Statementstmt 55.0% 44 / 80 Branchbran 21.4% 6 / 28 Conditioncond 38.8% 7 / 18 MC/DCmcdc 30.0% 3 / 10 Subroutinesub 62.5% 10 / 16 Podpod 0.0% 0 / 12 totaltotal 42.6% 70 / 164 CC 21 SCAR 19.5
CC
21
Cov
45%
badvalue
14.1
badvalue
14.1
check_badflag
5.9
CRAP 7.1
« lib/PDL/Ops.pm ↑ Summary blib/lib/PDL/PP/PdlParObj.pm »
1 use strict;
2 use warnings;
3  
4 use PDL::Types qw(ppdefs_all types);
5 my $A = [ppdefs_all()];
6 my $AF = [map $_->ppsym, grep !$_->integer, types()]; # all including complex
7 $AF = [(grep $_ ne 'D', @$AF), 'D']; # so defaults to D if non-float given
8  
9 #########################################################
10  
11 pp_addhdr('
12 #include <math.h>
13 ');
14  
15 pp_add_exported( '',
16 'badflag check_badflag badvalue orig_badvalue nbad nbadover ngood ngoodover ' .
17 'setbadat ' );
18  
19 ## Header
20 pp_addpm({At=>'Top'},<<'!NO!SUBS!');
21  
22 =head1 NAME
23  
24 PDL::Bad - PDL always processes bad values
25  
26 =head1 DESCRIPTION
27  
28 This module is loaded when you do C<use PDL>,
29 C<use PDL::Lite> or C<use PDL::LiteF>.
30  
31 Implementation details are given in
32 L<PDL::BadValues>.
33  
34 =head1 SYNOPSIS
35  
36 use PDL::Bad;
37 print "\nBad value per PDL support in PDL is turned " .
38 $PDL::Bad::PerPdl ? "on" : "off" . ".\n";
39  
40 =head1 VARIABLES
41  
42 =over 4
43  
44 =item $PDL::Bad::UseNaN
45  
46 Set to 0 as of PDL 2.040, as no longer available, though NaN can be used
47 as a badvalue for a given PDL object.
48  
49 =item $PDL::Bad::PerPdl
50  
51 Set to 1 as of PDL 2.040 as always available.
52  
53 =item $PDL::Bad::Status
54  
55 Set to 1 as of PDL 2.035 as always available.
56  
57 =back
58  
59 =cut
60  
61 !NO!SUBS!
62  
63 pp_addpm(<<'!NO!SUBS!');
64  
65 $PDL::Bad::Status = 1;
66 $PDL::Bad::UseNaN = 0;
67 $PDL::Bad::PerPdl = 1;
68 70 ▶ 
Subroutine BEGIN: called CC=1 SCAR=0.0
69 use strict;
70 70 ▶ 
Subroutine BEGIN: called CC=1 SCAR=0.0
71 70 ▶use PDL::Types;
Subroutine BEGIN: called CC=1 SCAR=0.0
72 use PDL::Primitive;
73  
74 ############################################################
75 ############################################################
76  
77 !NO!SUBS!
78  
79 pp_addpm(<<'!NO!SUBS!');
80 ############################################################
81 ############################################################
82  
83 *badflag = \&PDL::badflag;
84 *badvalue = \&PDL::badvalue;
85 *orig_badvalue = \&PDL::orig_badvalue;
86  
87 ############################################################
88 ############################################################
89  
90 =head2 badflag
91  
92 =for ref
93  
94 getter/setter for the bad data flag
95  
96 =for example
97  
98 if ( $x->badflag() ) {
99 print "Data may contain bad values.\n";
100 }
101 $x->badflag(1); # set bad data flag
102 $x->badflag(0); # unset bad data flag
103  
104 When called as a setter, this modifies the ndarray on which
105 it is called. This always returns a Perl scalar with the
106 final value of the bad flag.
107  
108 A return value of 1 does not guarantee the presence of
109 bad data in an ndarray; all it does is say that we need to
110 I<check> for the presence of such beasties. To actually
111 find out if there are any bad values present in an ndarray,
112 use the L</check_badflag> method.
113  
114 =for bad
115  
116 This function works with ndarrays that have bad values. It
117 always returns a Perl scalar, so it never returns bad values.
118  
119 =head2 badvalue
120  
121 =for ref
122  
123 returns (or sets) the value used to indicate a missing (or bad) element
124 for the given ndarray type. You can give it an ndarray,
125 a PDL::Type object, or one of C<$PDL_B>, C<$PDL_S>, etc.
126  
127 =for example
128  
129 $badval = badvalue( float );
130 $x = ones(ushort,10);
131 print "The bad data value for ushort is: ",
132 $x->badvalue(), "\n";
133  
134 This can act as a setter (e.g. C<< $x->badvalue(23) >>),
135 including with the value C<NaN> for floating-point types.
136 Note that this B<doesn't change the data in the ndarray> for
137 floating-point-typed ndarrays.
138 That is, if C<$x> already has bad values, they will not
139 be changed to use the given number and if any elements of
140 C<$x> have that value, they will unceremoniously be marked
141 as bad data. See L</setvaltobad>, L</setbadtoval>, and
142 L</setbadif> for ways to actually modify the data in ndarrays
143  
144 It I<does> change data for integer-typed arrays, changing values that
145 had the old bad value to have the new one.
146  
147 It is possible to change the bad value on a per-ndarray basis, so
148  
149 $x = sequence (10);
150 $x->badvalue (3); $x->badflag (1);
151 $y = sequence (10);
152 $y->badvalue (4); $y->badflag (1);
153  
154 will set $x to be C<[0 1 2 BAD 4 5 6 7 8 9]> and $y to be
155 C<[0 1 2 3 BAD 5 6 7 8 9]>.
156  
157 =for bad
158  
159 This method does not care if you call it on an input ndarray
160 that has bad values. It always returns an ndarray
161 with the current or new bad value.
162  
163 =cut
164 107 ▶ 
Subroutine badvalue: called CC=6 SCAR=26.5
Pod undocumented
165 0 ▶sub PDL::badvalue {
Subroutine badvalue: not called CC=6 SCAR=26.5
Pod undocumented
166 0 my ( $self, $val ) = @_;
167 0 ▶ my $num;
Branch: 4 branches
BranchTrueFalseTotal
if (UNIVERSAL::isa($self, 'PDL')) { } 107 0 107
elsif (UNIVERSAL::isa($self, 'PDL::Type')) { } 0 0 0
if (UNIVERSAL::isa($self, 'PDL')) { } 0 0 0
elsif (UNIVERSAL::isa($self, 'PDL::Type')) { } 0 0 0
168 0 if ( UNIVERSAL::isa($self,"PDL") ) {
169 0 ▶ $num = $self->get_datatype;
Branch: 2 branches
BranchTrueFalseTotal
if ($num < $PDL::Bad::PDL_F and defined $val and $self->badflag) 5 102 107
if ($num < $PDL::Bad::PDL_F and defined $val and $self->badflag) 0 0 0
Condition50%6/12
$num < $PDL::Bad::PDL_F and defined $val and $self->badflag
!ll&&!rl&&r
10115
$num < $PDL::Bad::PDL_F and defined $val
!ll&&!rl&&r
86156
$num < $PDL::Bad::PDL_F and defined $val and $self->badflag
!ll&&!rl&&r
000
$num < $PDL::Bad::PDL_F and defined $val
!ll&&!rl&&r
000
MC/DC100%3/3
$num < $PDL::Bad::PDL_F and defined $val and $self->badflag
$num < $PDL::Bad::PDL_F defined $val $self->badflag
MC/DC0%0/3
$num < $PDL::Bad::PDL_F and defined $val and $self->badflag
$num < $PDL::Bad::PDL_F defined $val $self->badflag
Truth table100%4/4
A and B and C
A= $num < $PDL::Bad::PDL_FB= defined $valC= $self->badflag
ABCresult
0XX0
10X0
1100
1111
Truth table0%0/4
A and B and C
A= $num < $PDL::Bad::PDL_FB= defined $valC= $self->badflag
ABCresult
0XX0
10X0
1100
1111
170 0 if ( $num < $PDL_F && defined($val) && $self->badflag ) {
171 0 $self->inplace->setbadtoval( $val );
172 $self->badflag(1);
173 0 }
174 return PDL::Bad::_badvalue_per_pdl_int($self, $val, $num);
175 0 } elsif ( UNIVERSAL::isa($self,"PDL::Type") ) {
176 $num = $self->enum;
177 } else {
178 0 # assume it's a number
179 $num = $self;
180 0 }
181 PDL::Bad::_badvalue_int( $val, $num );
182 }
183  
184 =head2 orig_badvalue
185  
186 =for ref
187  
188 returns the original value used to represent bad values for
189 a given type.
190  
191 This routine operates the same as L</badvalue>,
192 except you can not change the values.
193  
194 It also has an I<awful> name.
195  
196 =for example
197  
198 $orig_badval = orig_badvalue( float );
199 $x = ones(ushort,10);
200 print "The original bad data value for ushort is: ",
201 $x->orig_badvalue(), "\n";
202  
203 =for bad
204  
205 This method does not care if you call it on an input ndarray
206 that has bad values. It always returns an ndarray
207 with the original bad value for the associated type.
208  
209 =cut
210 28 ▶ 
Subroutine orig_badvalue: called CC=3 SCAR=15.2
Pod undocumented
211 70 ▶sub PDL::orig_badvalue {
Subroutine BEGIN: called CC=1 SCAR=0.0
212 0 ▶ no strict 'refs';
Subroutine orig_badvalue: not called CC=3 SCAR=16.7
Pod undocumented
213 0 my $self = shift;
214 0 ▶ my $num;
Branch: 4 branches
BranchTrueFalseTotal
if (UNIVERSAL::isa($self, 'PDL')) { } 28 0 28
elsif (UNIVERSAL::isa($self, 'PDL::Type')) { } 0 0 0
if (UNIVERSAL::isa($self, 'PDL')) { } 0 0 0
elsif (UNIVERSAL::isa($self, 'PDL::Type')) { } 0 0 0
215 0 if ( UNIVERSAL::isa($self,"PDL") ) {
216 $num = $self->get_datatype;
217 0 } elsif ( UNIVERSAL::isa($self,"PDL::Type") ) {
218 $num = $self->enum;
219 } else {
220 0 # assume it's a number
221 $num = $self;
222 0 }
223 PDL::Bad::_default_badvalue_int($num);
224 }
225  
226 =head2 check_badflag
227  
228 =for ref
229  
230 Clear the badflag of an ndarray if it does not
231 contain any bad values
232  
233 Given an ndarray whose bad flag is set, check whether it
234 actually contains any bad values and, if not, clear the flag.
235 It returns the final state of the badflag.
236  
237 =for example
238  
239 print "State of bad flag == ", $pdl->check_badflag;
240  
241 =for bad
242  
243 This method accepts ndarrays with or without bad values. It
244 returns an ndarray with the final badflag.
245  
246 =cut
247  
248 *check_badflag = \&PDL::check_badflag;
249 1 ▶ 
Subroutine check_badflag: called CC=3 SCAR=17.8
Pod undocumented
250 0 ▶sub PDL::check_badflag {
Subroutine check_badflag: not called CC=3 SCAR=17.8
Pod undocumented
251 0 ▶ my $pdl = shift;
Branch: 2 branches
BranchTrueFalseTotal
if $pdl->badflag and $pdl->nbad == 0 1 0 1
if $pdl->badflag and $pdl->nbad == 0 0 0 0
Condition16%1/6
$pdl->badflag and $pdl->nbad == 0
!ll&&!rl&&r
010
$pdl->badflag and $pdl->nbad == 0
!ll&&!rl&&r
000
MC/DC0%0/2
$pdl->badflag and $pdl->nbad == 0
$pdl->badflag $pdl->nbad == 0
MC/DC0%0/2
$pdl->badflag and $pdl->nbad == 0
$pdl->badflag $pdl->nbad == 0
Truth table33%1/3
A and B
A= $pdl->badflagB= $pdl->nbad == 0
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= $pdl->badflagB= $pdl->nbad == 0
ABresult
0X0
100
111
252 0 $pdl->badflag(0) if $pdl->badflag and $pdl->nbad == 0;
253 return $pdl->badflag;
254 } # sub: check_badflag()
255  
256 !NO!SUBS!
257  
258 pp_addxs(<<'EOF');
259 pdl *
260 _badvalue_int(val, type)
261 PDL_Anyval val
262 int type
263 CODE:
264 if ( val.type != -1 ) {
265 #define X_OUTER(datatype, ctype, ppsym, ...) \
266 ctype cnewval = val.value.ppsym;
267 #define X_INNER(datatype, ctype, ppsym, ...) \
268 PDL->bvals.ppsym = cnewval;
269 PDL_GENERICSWITCH2(
270 PDL_TYPELIST_ALL, val.type, X_OUTER, croak("Not a known data type code=%d", val.type),
271 PDL_TYPELIST_ALL_, type, X_INNER, croak("Not a known data type code=%d", type))
272 #undef X_OUTER
273 #undef X_INNER
274 }
275 PDL_Anyval newval = {type, {0}};
276 pdl* p = PDL->scalar(newval);
277 if (!p) PDL->pdl_barf("Error making new pdl");
278 #define X(datatype, ctype, ppsym, ...) \
279 *((ctype *)p->data) = PDL->bvals.ppsym;
280 PDL_GENERICSWITCH(PDL_TYPELIST_ALL, type, X, croak("Not a known data type code=%d", type))
281 #undef X
282 RETVAL = p;
283 OUTPUT:
284 RETVAL
285  
286 pdl *
287 _badvalue_per_pdl_int(pdl_val, val, type)
288 pdl* pdl_val
289 PDL_Anyval val
290 int type
291 CODE:
292 if ( val.type != -1) {
293 PDL_Anyval typedval;
294 ANYVAL_TO_ANYVAL_NEWTYPE(val, typedval, pdl_val->datatype);
295 if (typedval.type < 0) PDL->pdl_barf("Error making typedval");
296 #define X(datatype, ctype, ppsym, ...) \
297 pdl_val->badvalue.type = typedval.type; \
298 pdl_val->badvalue.value.ppsym = typedval.value.ppsym;
299 PDL_GENERICSWITCH(PDL_TYPELIST_ALL, pdl_val->datatype, X, croak("Not a known data type code=%d", type))
300 #undef X
301 pdl_val->has_badvalue = 1;
302 PDL->propagate_badvalue( pdl_val );
303 }
304 PDL_Anyval newval = {type, {0}};
305 pdl* p = PDL->scalar(newval);
306 if (!p) PDL->pdl_barf("Error making new pdl");
307 if (pdl_val->has_badvalue == 0) {
308 #define X(datatype, ctype, ppsym, ...) \
309 *((ctype *)p->data) = PDL->bvals.ppsym;
310 PDL_GENERICSWITCH(PDL_TYPELIST_ALL, type, X, croak("Not a known data type code=%d", type))
311 #undef X
312 } else {
313 #define X_OUTER(datatype, ctype, ...) \
314 ctype *coutvalp = p->data;
315 #define X_INNER(datatype, ctype, ppsym, ...) \
316 *coutvalp = pdl_val->badvalue.value.ppsym;
317 PDL_GENERICSWITCH2(
318 PDL_TYPELIST_ALL, type, X_OUTER, croak("Not a known data type code=%d", type),
319 PDL_TYPELIST_ALL_, pdl_val->badvalue.type, X_INNER, croak("Not a known data type code=%d", pdl_val->badvalue.type))
320 #undef X_OUTER
321 #undef X_INNER
322 }
323 RETVAL = p;
324 OUTPUT:
325 RETVAL
326  
327 pdl *
328 _default_badvalue_int(type)
329 int type
330 CODE:
331 PDL_Anyval val = {type, {0}};
332 pdl* p = PDL->scalar(val);
333 if (!p) PDL->pdl_barf("Error making new pdl");
334 #define X(datatype, ctype, ppsym, shortctype, defbval, ...) \
335 *((ctype *)p->data) = defbval;
336 PDL_GENERICSWITCH(PDL_TYPELIST_ALL, type, X, croak("Not a known data type code=%d", type))
337 #undef X
338 RETVAL = p;
339 OUTPUT:
340 RETVAL
341 EOF
342  
343 pp_def('isbad',
344 Pars => q(a(); int [o]b()),
345 Doc => <<'EOF',
346 =for ref
347  
348 Returns a binary mask indicating which values of
349 the input are bad values
350  
351 Returns a 1 if the value is bad, 0 otherwise.
352 Similar to L<isfinite|PDL::Math/isfinite>.
353  
354 =for example
355  
356 $x = pdl(1,2,3);
357 $x->badflag(1);
358 set($x,1,$x->badvalue);
359 $y = isbad($x);
360 print $y, "\n";
361 [0 1 0]
362 EOF
363 BadDoc => <<'EOF',
364 This method works with input ndarrays that are bad. The output ndarray
365 will never contain bad values, but its bad value flag will be the
366 same as the input ndarray's flag.
367 EOF
368 HandleBad => 1,
369 Code => '$b() = PDL_IF_BAD($ISBAD(a()),0);',
370 GenericTypes => $A,
371 );
372  
373 pp_def('isnan',
374 Pars => q(a(); int [o]b()),
375 Doc => <<'EOF',
376 =for ref
377  
378 Returns a binary mask indicating which values of the input are C<NaN>.
379  
380 Returns a 1 if the value is C<NaN>, 0 otherwise.
381 Similar to L<isfinite|PDL::Math/isfinite>.
382 EOF
383 Code => '$b() = PDL_ISNAN_$PPSYM()($a());',
384 GenericTypes => $A,
385 );
386  
387 pp_def('isgood',
388 Pars => q(a(); int [o]b()),
389 Doc => <<'EOF',
390 =for ref
391  
392 Is a value good?
393  
394 Returns a 1 if the value is good, 0 otherwise.
395 Also see L<isfinite|PDL::Math/isfinite>.
396  
397 =for example
398  
399 $x = pdl(1,2,3);
400 $x->badflag(1);
401 set($x,1,$x->badvalue);
402 $y = isgood($x);
403 print $y, "\n";
404 [1 0 1]
405 EOF
406 BadDoc => <<'EOF',
407 This method works with input ndarrays that are bad. The output ndarray
408 will never contain bad values, but its bad value flag will be the
409 same as the input ndarray's flag.
410 EOF
411 HandleBad => 1,
412 Code => '$b() = PDL_IF_BAD($ISGOOD(a()),1);',
413 GenericTypes => $A,
414 );
415  
416 # perhaps these should have pm code which returns the
417 # answer if the bad flag is not set
418 pp_def('nbadover',
419 Pars => q(a(n); indx [o] b()),
420 Doc => <<'EOF',
421 =for ref
422  
423 Find the number of bad elements along the 1st dimension.
424  
425 This function reduces the dimensionality of an ndarray by one by finding the
426 number of bad elements along the 1st dimension. In this sense it shares
427 much in common with the functions defined in L<PDL::Ufunc>. In particular,
428 by using L<xchg|PDL::Slices/xchg> and similar dimension rearranging methods,
429 it is possible to perform this calculation over I<any> dimension.
430 EOF
431 BadDoc => <<'EOF',
432 nbadover processes input values that are bad. The output ndarray will not have
433 any bad values, but the bad flag will be set if the input ndarray had its bad
434 flag set.
435 EOF
436 HandleBad => 1,
437 Code => q{
438 PDL_Indx cnt = 0;
439 PDL_IF_BAD(loop(n) %{ if ( $ISBAD(a()) ) { cnt++; } %},)
440 $b() = cnt;
441 },
442 GenericTypes => $A,
443 );
444  
445 pp_def('ngoodover',
446 Pars => q(a(n); indx [o] b()),
447 Doc => <<'EOF',
448 =for ref
449  
450 Find the number of good elements along the 1st dimension.
451  
452 This function reduces the dimensionality of an ndarray
453 by one by finding the number of good elements
454 along the 1st dimension.
455  
456 By using L<xchg|PDL::Slices/xchg> etc. it is possible to use
457 I<any> dimension.
458 EOF
459 BadDoc => <<'EOF',
460 ngoodover processes input values that are bad. The output ndarray will not have
461 any bad values, but the bad flag will be set if the input ndarray had its bad
462 flag set.
463 EOF
464 HandleBad => 1,
465 Code =>
466 'PDL_Indx cnt = PDL_IF_BAD(0,$SIZE(n));
467 PDL_IF_BAD(loop(n) %{ if ( $ISGOOD(a()) ) { cnt++; } %},)
468 $b() = cnt;',
469 GenericTypes => $A,
470 );
471  
472 # Generate small ops functions to do entire array
473 foreach my $op (
474 ['nbad','nbadover'],
475 ['ngood','ngoodover'],
476 ) {
477 pp_addpm(<<"EOD");
478 5 ▶ 
Subroutine ngood: called CC=1 SCAR=1.2
Pod undocumented
479 0 ▶*$op->[0] = \\&PDL::$op->[0];
Subroutine nbad: called CC=1 SCAR=1.2
Subroutine ngood: not called CC=1 SCAR=1.2
Pod undocumented
Pod undocumented
480 0 ▶sub PDL::$op->[0] {
Subroutine nbad: not called CC=1 SCAR=1.2
Pod undocumented
481 0 my(\$x) = \@_; my \$tmp;
482 0 \$x->flat->$op->[1](\$tmp=PDL->nullcreate(\$x) );
483 return \$tmp;
484 }
485 EOD
486  
487 } # for $op
488  
489 pp_addpm(<<'!NO!SUBS!');
490  
491 =head2 nbad
492  
493 =for ref
494  
495 Returns the number of bad values in an ndarray
496  
497 =for bad
498  
499 Accepts good and bad input ndarrays; output is an ndarray
500 and is always good.
501  
502 =head2 ngood
503  
504 =for ref
505  
506 Returns the number of good values in an ndarray
507  
508 =for usage
509  
510 $x = ngood($data);
511  
512 =for bad
513  
514 Accepts good and bad input ndarrays; output is an ndarray
515 and is always good.
516  
517 =head2 setbadat
518  
519 =for ref
520  
521 Set the value to bad at a given position.
522  
523 =for usage
524  
525 setbadat $ndarray, @position
526  
527 C<@position> is a coordinate list, of size equal to the
528 number of dimensions in the ndarray.
529 This is a wrapper around L<set|PDL::Core/set> and is
530 probably mainly useful in test scripts!
531  
532 =for example
533  
534 pdl> $x = sequence 3,4
535 pdl> $x->setbadat 2,1
536 pdl> p $x
537 [
538 [ 0 1 2]
539 [ 3 4 BAD]
540 [ 6 7 8]
541 [ 9 10 11]
542 ]
543  
544 =for bad
545  
546 This method can be called on ndarrays that have bad values.
547 The remainder of the arguments should be Perl scalars indicating
548 the position to set as bad. The output ndarray will have bad values
549 and will have its badflag turned on.
550  
551 =cut
552  
553 23 ▶*setbadat = \&PDL::setbadat;
Subroutine setbadat: called CC=2 SCAR=10.1
Pod undocumented
554 0 ▶sub PDL::setbadat {
Subroutine setbadat: not called CC=2 SCAR=10.1
Pod undocumented
Branch: 2 branches
BranchTrueFalseTotal
if $#_ < 1 0 23 23
if $#_ < 1 0 0 0
555 0 barf 'Usage: setbadat($pdl, $x, $y, ...)' if $#_<1;
556 0 my $self = shift;
557 0 PDL::Core::set_c ($self, [@_], $self->badvalue);
558 0 $self->badflag(1);
559 return $self;
560 }
561  
562 !NO!SUBS!
563  
564 # NOTE: the Code section uses SETBAD
565 #
566 # have removed inplace stuff because:
567 # $x->inplace->setbadif( $x % 2 )
568 # actually sets the badflag in a for ($x % 2) - this is
569 # done inplace, and the flag cleared. Hence the setbadif()
570 # call is NOT done inplace.
571 #
572 # Don't want to play around with inplace-type code to
573 # try and fix this (doubt will be easy)
574 #
575 my %setbadif_extra = ( );
576 if ( 0 ) {
577 ## ie if fix inplace issues
578 $setbadif_extra{Inplace} = [ 'a' ];
579 } else {
580 }
581 # always make sure the output is "bad"
582  
583 # note: have made the mask be an integer
584 pp_def('setbadif',
585 Pars => q(a(); int mask(); [o]b()),
586 Doc => <<'EOF',
587 =for ref
588  
589 Set elements bad based on the supplied mask, otherwise
590 copy across the data.
591  
592 =for example
593  
594 pdl> $x = sequence(5,5)
595 pdl> $x = $x->setbadif( $x % 2 )
596 pdl> p "a badflag: ", $x->badflag, "\n"
597 a badflag: 1
598 pdl> p "a is\n$x"
599 [
600 [ 0 BAD 2 BAD 4]
601 [BAD 6 BAD 8 BAD]
602 [ 10 BAD 12 BAD 14]
603 [BAD 16 BAD 18 BAD]
604 [ 20 BAD 22 BAD 24]
605 ]
606  
607 Unfortunately, this routine can I<not> be run inplace, since the
608 current implementation can not handle the same ndarray used as
609 C<a> and C<mask> (eg C<< $x->inplace->setbadif($x%2) >> fails).
610 Even more unfortunate: we can't catch this error and tell you.
611 EOF
612 BadDoc => <<'EOF',
613 The output always has its bad flag set, even if it does not contain
614 any bad values (use L</check_badflag> to check
615 whether there are any bad values in the output).
616 The input ndarray can have bad values: any bad values in the input ndarrays
617 are copied across to the output ndarray.
618  
619 Also see L</setvaltobad> and L</setnantobad>.
620 EOF
621 HandleBad => 1,
622 %setbadif_extra,
623 Code => '
624 broadcastloop %{
625 /* if the bad value == 0 then all points are going to be selected ... */
626 if ( PDL_IF_BAD($ISBAD(mask()) ||,) $mask() ) {
627 $SETBAD(b());
628 } else {
629 $b() = $a();
630 }
631 %}
632 $PDLSTATESETBAD(b);
633 ',
634 GenericTypes => $A,
635 );
636  
637 # this is useful because $x->setbadif( $x == 23 )
638 # is common and that can't be done inplace
639 pp_def('setvaltobad',
640 Pars => q(a(); [o]b()),
641 OtherPars => q(double value),
642 Doc => <<'EOF',
643 =for ref
644  
645 Set bad all those elements which equal the supplied value.
646  
647 =for example
648  
649 $x = sequence(10) % 3;
650 $x->inplace->setvaltobad( 0 );
651 print "$x\n";
652 [BAD 1 2 BAD 1 2 BAD 1 2 BAD]
653  
654 This is a simpler version of L</setbadif>, but this
655 function can be done inplace. See L</setnantobad>
656 if you want to convert NaN to the bad value.
657 EOF
658 BadDoc => <<'EOF',
659 The output always has its bad flag set, even if it does not contain
660 any bad values (use L</check_badflag> to check
661 whether there are any bad values in the output).
662 Any bad values in the input ndarrays are copied across to the output ndarray.
663 EOF
664 HandleBad => 1,
665 Inplace => 1,
666 Code => q[
667 broadcastloop %{
668 if ( $a() == ($GENERIC(a)) $COMP(value) ) {
669 $SETBAD(b());
670 } else {
671 $b() = $a();
672 }
673 %}
674 $PDLSTATESETBAD(b);
675 ],
676 GenericTypes => $A,
677 );
678  
679 pp_def('setnantobad',
680 Pars => q(a(); [o]b()),
681 Doc => <<'EOF',
682 =for ref
683  
684 Sets NaN values (for complex, where either is NaN) in the input ndarray bad
685 (only relevant for floating-point ndarrays).
686 EOF
687 BadDoc => <<'EOF',
688 This method can process ndarrays with bad values: those bad values
689 are propagated into the output ndarray. Any value that is not a number
690 (before version 2.040 the test was for "not finite")
691 is also set to bad in the output ndarray. If all values from the input
692 ndarray are good, the output ndarray will B<not> have its
693 bad flag set.
694 EOF
695 HandleBad => 1,
696 GenericTypes => $AF,
697 Inplace => 1,
698 Code => q[
699 int flag = 0;
700 broadcastloop %{
701 if ( PDL_ISNAN_$PPSYM()($a()) ) {
702 $SETBAD(b());
703 flag = 1;
704 } else {
705 $b() = $a();
706 }
707 %}
708 if ( flag ) $PDLSTATESETBAD(b);
709 ],
710 );
711  
712 pp_def('setinftobad',
713 Pars => q(a(); [o]b()),
714 Doc => <<'EOF',
715 =for ref
716  
717 Sets non-finite values (for complex, where either is non-finite) in
718 the input ndarray bad (only relevant for floating-point ndarrays).
719 EOF
720 BadDoc => <<'EOF',
721 This method can process ndarrays with bad values: those bad values
722 are propagated into the output ndarray. Any value that is not finite
723 is also set to bad in the output ndarray. If all values from the input
724 ndarray are finite, the output ndarray will B<not> have its
725 bad flag set.
726 EOF
727 HandleBad => 1,
728 GenericTypes => $AF,
729 Inplace => 1,
730 Code => q[
731 int flag = 0;
732 broadcastloop %{
733 if ( !PDL_ISFINITE_$PPSYM()($a()) && !PDL_ISNAN_$PPSYM()($a()) ) {
734 $SETBAD(b());
735 flag = 1;
736 }
737 else {
738 $b() = $a();
739 }
740 %}
741 if ( flag ) $PDLSTATESETBAD(b);
742 ],
743 );
744  
745 pp_def('setnonfinitetobad',
746 Pars => q(a(); [o]b()),
747 Doc => <<'EOF',
748 =for ref
749  
750 Sets non-finite values (for complex, where either is non-finite) in
751 the input ndarray bad (only relevant for floating-point ndarrays).
752 EOF
753 BadDoc => <<'EOF',
754 This method can process ndarrays with bad values: those bad values
755 are propagated into the output ndarray. Any value that is not finite
756 is also set to bad in the output ndarray. If all values from the input
757 ndarray are finite, the output ndarray will B<not> have its
758 bad flag set.
759 EOF
760 HandleBad => 1,
761 GenericTypes => $AF,
762 Inplace => 1,
763 Code => q[
764 int flag = 0;
765 broadcastloop %{
766 if ( !PDL_ISFINITE_$PPSYM()($a()) ) {
767 $SETBAD(b());
768 flag = 1;
769 } else {
770 $b() = $a();
771 }
772 %}
773 if ( flag ) $PDLSTATESETBAD(b);
774 ],
775 );
776  
777 pp_def('setbadtonan',
778 Pars => q(a(); [o] b();),
779 Doc => <<'EOF',
780 =for ref
781  
782 Sets Bad values to NaN
783  
784 This is only relevant for floating-point ndarrays. The input ndarray can be
785 of any type, but if done inplace, the input must be floating point.
786 EOF
787 BadDoc => <<'EOF',
788 This method processes input ndarrays with bad values. The output ndarrays will
789 not contain bad values (insofar as NaN is not Bad as far as PDL is concerned)
790 and the output ndarray does not have its bad flag set. As an inplace
791 operation, it clears the bad flag.
792 EOF
793 HandleBad => 1,
794 GenericTypes => $AF,
795 Inplace => 1,
796 Code => q{
797 broadcastloop %{
798 if ( $ISBAD(a()) ) {
799 $b() = $TFDEGCH(NAN,NAN,NAN,NAN+I*NAN,NAN+I*NAN,NAN+I*NAN);
800 } else {
801 $b() = $a();
802 }
803 %}
804 $PDLSTATESETGOOD(b);
805 },
806 );
807  
808 pp_def('setbadtoval',
809 Pars => q(a(); [o]b()),
810 OtherPars => q(double newval),
811 Doc => <<'EOF',
812 =for ref
813  
814 Replace any bad values by a (non-bad) value.
815  
816 Also see L</badmask>.
817  
818 =for example
819  
820 $x->inplace->setbadtoval(23);
821 print "a badflag: ", $x->badflag, "\n";
822 a badflag: 0
823 EOF
824 BadDoc => <<'EOF',
825 The output always has its bad flag cleared.
826 If the input ndarray does not have its bad flag set, then
827 values are copied with no replacement.
828 EOF
829 HandleBad => 1,
830 Inplace => 1,
831 Code => q{
832 PDL_IF_BAD($GENERIC(b) replace = ($GENERIC(b)) $COMP(newval);,)
833 broadcastloop %{
834 $GENERIC(b) a_val = $a();
835 $b() = PDL_IF_BAD($ISBADVAR(a_val,a) ? replace : ,) a_val;
836 %}
837 $PDLSTATESETGOOD(b); /* always make sure the output is "good" */
838 },
839 GenericTypes => $A,
840 );
841  
842 pp_def('badmask',
843 Pars => 'a(); b(); [o]c();',
844 Inplace => [ 'a' ],
845 HandleBad => 1,
846 Code => '
847 broadcastloop %{
848 $c() = ( isfinite((double) $a()) PDL_IF_BAD(&& $ISGOOD(a()),) ) ? $a() : $b();
849 %}
850 $PDLSTATESETGOOD(c);
851 ',
852 Doc => <<'EOF',
853 =for ref
854  
855 Clears all C<infs> and C<nans> in C<$a> to the corresponding value in C<$b>.
856 EOF
857 BadDoc => <<'EOF',
858 If bad values are present, these are also cleared.
859 EOF
860 );
861  
862 pp_def('copybad',
863 Pars => q(a(); mask(); [o]b()),
864 Doc => <<'EOF',
865 =for ref
866  
867 Copies values from one ndarray to another, setting them
868 bad if they are bad in the supplied mask.
869  
870 =for example
871  
872 $x = byte( [0,1,3] );
873 $mask = byte( [0,0,0] );
874 $mask->badflag(1);
875 set($mask,1,$mask->badvalue);
876 $x->inplace->copybad( $mask );
877 p $x;
878 [0 BAD 3]
879  
880 It is equivalent to:
881  
882 $c = $x + $mask * 0
883 EOF
884 BadDoc => <<'EOF',
885 This handles input ndarrays that are bad. If either C<$x>
886 or C<$mask> have bad values, those values will be marked
887 as bad in the output ndarray and the output ndarray will have
888 its bad value flag set to true.
889 EOF
890 HandleBad => 1,
891 Inplace => [ 'a' ],
892 Code => q{
893 char anybad = 0;
894 broadcastloop %{
895 PDL_IF_BAD(if ( $ISBAD(mask()) ) {
896 $SETBAD(b());
897 anybad = 1;
898 } else,) {
899 $b() = $a();
900 }
901 %}
902 if (anybad) $PDLSTATESETBAD(b);
903 },
904 GenericTypes => $A,
905 );
906  
907 pp_def('locf',
908 Pars => 'a(n); [o]b(n);',
909 HandleBad => 1,
910 GenericTypes => $A,
911 Doc => <<'EOF',
912 =for ref
913  
914 Last Observation Carried Forward - replace
915 every BAD value with the most recent non-BAD value prior to it.
916 Any leading BADs will be set to 0.
917 EOF
918 Code => q{
919 $GENERIC() tmp = 0;
920 loop(n) %{
921 if ( $ISGOOD(a()) ) tmp = $a();
922 $b() = tmp;
923 %}
924 },
925 );
926  
927 #########################################################
928  
929 pp_addpm({At=>'Bot'},<<'!WITHOUT!SUBS!');
930  
931 =head1 AUTHOR
932  
933 Doug Burke (djburke@cpan.org), 2000, 2001, 2003, 2006.
934  
935 The per-ndarray bad value support is by Heiko Klein (2006).
936  
937 CPAN documentation fixes by David Mertens (2010, 2013).
938  
939 All rights reserved. There is no warranty. You are allowed to
940 redistribute this software / documentation under certain conditions. For
941 details, see the file COPYING in the PDL distribution. If this file is
942 separated from the PDL distribution, the copyright notice should be
943 included in the file.
944  
945 =cut
946  
947 !WITHOUT!SUBS!
948  
949 ## End
950 pp_done();
« lib/PDL/Ops.pm ↑ Summary blib/lib/PDL/PP/PdlParObj.pm »