blib/lib/PDL/Demos/BAD_demo.pm untested

filter: Statementstmt 0.0% 0 / 15 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 5 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 24 CC 1 SCAR 6.9
CC
1
Cov
0%
run
2.0
BEGIN
2.0
CRAP 2.0
1 #
2 # Created by BAD_demo.pm.PL
3 # ** DO NOT EDIT THIS FILE **
4 #
5 package PDL::Demos::BAD_demo;
6 use PDL;
7  
8 PDL::Demos::Routines->import();
9 sub comment($);
10 sub act($);
11 sub output;
12  
13 sub run {
14  
15  
16 comment q|
17 Welcome to this tour of the bad value support in PDL
18  
19 Each piddle contains a flag - accessible via the badflag() method -
20 which indicates whether:
21  
22 the piddle contains no bad values (flag equals 0)
23 the piddle *MAY* contain bad values (flag equals 1)
24  
25 If the flag is set, then the routines (well, those that have been
26 converted) will process these bad values correctly, otherwise they
27 are ignored.
28  
29 The code has been written so as to provide as little overhead as
30 possible; therefore there should be almost no difference in the
31 time it takes to process piddles which do not have their bad flag
32 set.
33  
34 |;
35  
36 act q|
37  
38 # There are 2 ways to see whether bad-value support has been
39 # compiled into your perldl or pdl2 shell:
40 print("You can use bad values.\n") if $PDL::Bad::Status;
41  
42 # or
43 use PDL::Config;
44 print("You can stil use bad values.\n") if $PDL::Config{WITH_BADVAL};
45  
46 # note that PDL::Bad is included by default when you use
47 # 'use PDL', 'use PDL::Lite', or 'use PDL::LiteF'
48  
49 |;
50  
51 act q|
52  
53 # create a piddle
54 $a = byte(1,2,3);
55 print( "Bad flag (a) == ", $a->badflag(), "\n" );
56  
57 # set bad flag, even though all the data is good
58 $a->badflag(1);
59 print( "Bad flag (a) == ", $a->badflag(), "\n" );
60  
61 # note the bad flag is infectious
62 $b = 2 * $a;
63 print( "Bad flag (b) == ", $b->badflag(), "\n\n" );
64  
65 |;
66  
67 act q|
68  
69 # the badflag is also included in the state info of
70 # piddle
71 #
72 $c = pdl(2,3); # just a piddle without the badflag set
73  
74 print " Type Dimension State Mem\n";
75 print "-------------------------------------------------\n";
76 print "a ", $a->info("%-6T %-15D %-5S %12M"), "\n";
77 print "b ", $b->info("%-6T %-15D %-5S %12M"), "\n";
78 print "c ", $c->info("%-6T %-15D %-5S %12M"), "\n\n";
79 |;
80  
81 act q|
82  
83 print "No bad values: $a\n";
84 # set the middle value bad
85 $a->setbadat(1);
86  
87 # now print out
88 print "Some bad values: $a\n";
89 print "b contains: $b\n";
90 $c = $a + $b;
91 print "so a + b = $c\n\n";
92  
93 |;
94  
95 act q|
96  
97 # The module PDL::Bad contains a number of routines designed
98 # to make using bad values easy.
99 print "a contains ", $a->nbad, " bad elements.\n";
100 print "The bad value for type #",$a->get_datatype," is ",$a->badvalue,"\n";
101 print "It is easy to find whether a value is good: ", isgood($a), "\n\n";
102  
103 print "or to remove the bad values\n";
104 $a->inplace->setbadtoval(23);
105 print "a = $a and \$a->badflag == ", $a->badflag, "\n\n";
106  
107 |;
108  
109 act q|
110  
111 print "We can even label certain values as bad!\n";
112 $a = sequence(3,3);
113 $a = $a->setbadif( $a % 2 ); # unfortunately can not be done inplace
114 print $a;
115  
116 |;
117  
118 act q|
119  
120 # the issue of how to cope with dataflow is not fully resolved. At
121 # present, if you change the badflag of a piddle, all its children
122 # are also changed:
123 $a = sequence( byte, 2, 3 );
124 $a = $a->setbadif( $a == 3 );
125 $b = $a->slice("(1),:");
126 print "b = $b\tbadflag = ", $b->badflag, "\n";
127  
128 $a->inplace->setbadtoval(3);
129 print "b = $b\tbadflag = ", $b->badflag, "\n\n";
130  
131 |;
132  
133 act q|
134  
135 # Note that "boolean" operators return a bad value if either of the
136 # operands are bad: one way around this is to replace all bad values
137 # by 0 or 1.
138  
139 $a = sequence(3,3); $a = $a->setbadif( $a % 2 );
140 print $a > 5;
141 print setbadtoval($a > 5,0); # set all bad values to false
142  
143 |;
144  
145 act q|
146 # One area that is likely to cause confusion is the return value from
147 # comparison operators (e.g. all and any) when ALL elements are bad.
148  
149 # Currently, the bad value is returned; however most code will not
150 # be aware of this and just see it as a true or false value (depending
151 # on the numerical value used to store bad values).
152  
153 # There is also the fact that the bad value need not relate to the
154 # type of the input piddle (due to internal conversion to an 'int +').
155
156 $a = ones(3); $a = $a->setbadif( $a == 1 );
157 print "Any returns: ", any( $a > 2 ), "\n";
158 print "which is the bad value of 'long' (", long->badvalue, ").\n";
159  
160 print "Whereas the bad value for \$a is: ", $a->badvalue, "\n";
161  
162 |;
163  
164 comment q|
165 Many of the 'core' routines have been converted to handle bad values.
166 However, some (including most of the additional modules) have not,
167 either because it does not make sense or its too much work to do!
168  
169 To find out the status of a particular routine, use the 'badinfo'
170 command in perldl or pdl2 shell (this information is also included
171 when you do 'help'), or the '-b' switch of pdldoc.
172  
173 |;
174  
175  
176 }
177  
178 1;
179