3
# - needed since we allow bad pixel handling to be switched off
9
use File::Basename qw(&basename &dirname);
11
# check for bad value support
13
my $bvalflag = $PDL::Config{WITH_BADVAL} || 0;
15
# This forces PL files to create target in same directory as PL file.
16
# This is so that make depend always knows where to find PL derivatives.
19
($file = basename($0)) =~ s/\.PL$//;
21
if ($Config{'osname'} eq 'VMS' or
22
$Config{'osname'} eq 'OS2'); # "case-forgiving"
23
open OUT,">$file" or die "Can't create $file: $!";
26
print "Extracting $file (WITH bad value support)\n";
28
print "Extracting $file (NO bad value support)\n";
32
print OUT <<'!NO!SUBS!';
34
# Created by BAD_demo.pm.PL
35
# ** DO NOT EDIT THIS FILE **
37
package PDL::Demos::BAD_demo;
40
PDL::Demos::Routines->import();
50
print OUT <<'!NO!SUBS!';
54
Your version of PDL has been compiled without support for bad
55
values, hence this demo doesn't do anything.
64
print OUT <<'!NO!SUBS!';
67
Welcome to this tour of the bad value support in PDL
69
Each piddle contains a flag - accessible via the badflag() method -
70
which indicates whether:
72
the piddle contains no bad values (flag equals 0)
73
the piddle *MAY* contain bad values (flag equals 1)
75
If the flag is set, then the routines (well, those that have been
76
converted) will process these bad values correctly, otherwise they
79
The code has been written so as to provide as little overhead as
80
possible; therefore there should be almost no difference in the
81
time it takes to process piddles which do not have their bad flag
88
# There are 2 ways to see whether bad-value support has been
89
# compiled into your perldl:
90
print("You can use bad values.\n") if $PDL::Bad::Status;
94
print("You can stil use bad values.\n") if $PDL::Config{WITH_BADVAL};
96
# note that PDL::Bad is included by default when you use
97
# 'use PDL', 'use PDL::Lite', or 'use PDL::LiteF'
105
print( "Bad flag (a) == ", $a->badflag(), "\n" );
107
# set bad flag, even though all the data is good
109
print( "Bad flag (a) == ", $a->badflag(), "\n" );
111
# note the bad flag is infectious
113
print( "Bad flag (b) == ", $b->badflag(), "\n\n" );
119
# the badflag is also included in the state info of
122
$c = pdl(2,3); # just a piddle without the badflag set
124
print " Type Dimension State Mem\n";
125
print "-------------------------------------------------\n";
126
print "a ", $a->info("%-6T %-15D %-5S %12M"), "\n";
127
print "b ", $b->info("%-6T %-15D %-5S %12M"), "\n";
128
print "c ", $c->info("%-6T %-15D %-5S %12M"), "\n\n";
133
print "No bad values: $a\n";
134
# set the middle value bad
138
print "Some bad values: $a\n";
139
print "b contains: $b\n";
141
print "so a + b = $c\n\n";
147
# The module PDL::Bad contains a number of routines designed
148
# to make using bad values easy.
149
print "a contains ", $a->nbad, " bad elements.\n";
150
print "The bad value for type #",$a->get_datatype," is ",$a->badvalue,"\n";
151
print "It is easy to find whether a value is good: ", isgood($a), "\n\n";
153
print "or to remove the bad values\n";
154
$a->inplace->setbadtoval(23);
155
print "a = $a and \$a->badflag == ", $a->badflag, "\n\n";
161
print "We can even label certain values as bad!\n";
163
$a = $a->setbadif( $a % 2 ); # unfortunately can not be done inplace
170
# the issue of how to cope with dataflow is not fully resolved. At
171
# present, if you change the badflag of a piddle, all its children
173
$a = sequence( byte, 2, 3 );
174
$a = $a->setbadif( $a == 3 );
175
$b = $a->slice("(1),:");
176
print "b = $b\tbadflag = ", $b->badflag, "\n";
178
$a->inplace->setbadtoval(3);
179
print "b = $b\tbadflag = ", $b->badflag, "\n\n";
185
# Note that "boolean" operators return a bad value if either of the
186
# operands are bad: one way around this is to replace all bad values
189
$a = sequence(3,3); $a = $a->setbadif( $a % 2 );
191
print setbadtoval($a > 5,0); # set all bad values to false
196
# One area that is likely to cause confusion is the return value from
197
# comparison operators (e.g. all and any) when ALL elements are bad.
199
# Currently, the bad value is returned; however most code will not
200
# be aware of this and just see it as a true or false value (depending
201
# on the numerical value used to store bad values).
203
# There is also the fact that the bad value need not relate to the
204
# type of the input piddle (due to internal conversion to an 'int +').
206
$a = ones(3); $a = $a->setbadif( $a == 1 );
207
print "Any returns: ", any( $a > 2 ), "\n";
208
print "which is the bad value of 'long' (", long->badvalue, ").\n";
210
print "Whereas the bad value for \$a is: ", $a->badvalue, "\n";
215
Many of the 'core' routines have been converted to handle bad values.
216
However, some (including most of the additional modules) have not,
217
either because it does not make sense or its too much work to do!
219
To find out the status of a particular routine, use the 'badinfo'
220
command in perldl (this information is also included when you do
221
'help'), or the '-b' switch of pdldoc.
229
print OUT <<'!NO!SUBS!';