1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
|
# -*-perl-*-
#
# Test ->slice(). This is not yet good enough: we need
# nasty test cases
#
# Okay -- here're a couple (CED 3-apr-2002).
# Added permissive-slicing tests
#
use strict;
use Test::More;
plan tests => 66;
use PDL::LiteF;
# PDL::Core::set_debugging(1);
# Useful for debugging. Removed by DJB whilst cleaning up the
# tests
#
#sub kill_if_debug () {
# kill INT,$$ if $ENV{UNDER_DEBUGGER};
#}
sub tapprox ($$) {
my $a = shift;
my $b = shift;
my $maxdiff = abs($a-$b)->max;
return $maxdiff < 0.01;
}
my ($a, $b, $c, $d, $e, $f);
$a = (1+(xvals zeroes 4,5) + 10*(yvals zeroes 4,5));
print "FOO\n";
print $a;
print "BAR\n";
is($a->at(2,2), 23);
$b = $a->slice('1:3:2,2:4:2');
# print $a; print $b;
is($b->at(0,0), 22);
is($b->at(1,0), 24);
is($b->at(0,1), 42);
is($b->at(1,1), 44);
#$b .= 0.5 * double ones(2,2);
$b .= 0.5 * ones(2,2);
is($b->at(1,0), 0.5);
is($b->at(0,1), 0.5);
print $a;
is($a->at(1,2), 0.5);
# Check that nothing happened to other elems
is($a->at(2,2), 23);
$a = pdl (1,2);
$b = pdl [[1,2],[1,2],[1,2]];
$c = $a->slice(',*3');
print $a,$b,$c;
# check dimensions, sum of elements and correct order of els (using tapprox)
my $sum;
# $c = $a->dummy(1,3);
sumover($c->clump(-1),($sum=null));
ok(tapprox($b,$c));
is($sum->at, 9);
is(join(',',$c->dims), "2,3");
$b = pdl [[1,1,1],[2,2,2]];
$c = $a->slice('*3,');
sumover($c->clump(-1),($sum=null));
ok(tapprox($b,$c));
is($sum->at, 9);
is(join(',',$c->dims), "3,2");
# test stringify
$a = zeroes(3,3);
my $line = $a->slice(':,(0)');
$a++;
# $line += 0; # that's how to force an update before interpolation
my $linepr = "$line";
is($linepr, '[1 1 1]');
# Test whether error is properly returned:
$b = zeroes(5,3,3);
$c = $b->slice(":,:,1");
is(join(',',$c->dims), "5,3,1");
eval { my $d = $c->slice(":,:,2"); print $d; };
print "ERROR WAS: '$@'\n";
like($@, qr/Slice cannot start or end/);
$a = zeroes 3,3;
print $a;
$b = $a->slice("1,1:2");
# print $b;
$b .= 1;
print $b;
print $a;
$a = xvals zeroes 20,20;
print $a;
$b = $a->slice("1:18:2,:");
$c = $b->slice(":,1:18:2");
$d = $c->slice("3:5,:");
$e = $d->slice(":,(0)");
$f = $d->slice(":,(1)");
print "TOPRINT\n";
# print $b;
print $e,$f;
print $d,$c,$b,$a;
is("$e", "[7 9 11]");
is("$f", "[7 9 11]");
# Make sure that vaffining is properly working:
$a = zeroes 5,6,2;
$b = (xvals $a) + 0.1 * (yvals $a) + 0.01 * (zvals $a);
$b = $b->copy;
print $b;
$c = $b->slice("2:3");
$d = $c->copy;
# $c->dump;
# $d->dump;
$e = $c-$d;
print $e;
print $c;
print $d;
# $c->dump; $d->dump;
is(max(abs($e)), 0);
print "OUTOUTOUT!\n";
use PDL::Dbg;
my ($im, $im1, $im2, $lut, $in);
$im = byte [[0,1,255],[0,0,0],[1,1,1]];
($im1 = null) .= $im->dummy(0,3);
# print("1..2\n");
print $im1;
print ($im2 = $im1->clump(2)->slice(':,0:2')->px);
ok(!tapprox(ones(byte,9,3),$im2));
# here we encounter the problem
print ($im2 = $im1->clump(2)->slice(':,-1:0')->px);
ok(!tapprox(ones(byte,9,3),$im2));
$a = xvals( zeroes 10,10) + 0.1*yvals(zeroes 10,10);
ok(tapprox($a->mslice('X',[6,7]),
pdl([
[0.6, 1.6, 2.6, 3.6, 4.6, 5.6, 6.6, 7.6, 8.6, 9.6],
[0.7, 1.7, 2.7, 3.7, 4.7, 5.7, 6.7, 7.7, 8.7, 9.7]
])));
$lut = pdl [[1,0],[0,1]];
$im = pdl [1];
$in = $lut->xchg(0,1)->index($im->dummy(0));
is("$in", "
[
[0 1]
]
");
$in .= pdl 1;
is("$in", "
[
[1 1]
]
");
ok(tapprox($lut,pdl([[1,0],[1,1]])));
# can we catch indices which are too negative?
$a = PDL->sequence(10);
$b = $a->slice('0:-10');
is("$b", "[0]", "slice 0:-n picks first element");
$b = $a->slice('0:-14');
eval 'print $b';
like($@, qr/Negative slice cannot start or end above limit/);
# Test of dice and dice_axis
$a = sequence(10,4);
is($a->dice([1,2],[0,3])->sum, 66, "dice");
is($a->dice([0,1],'X')->sum, 124, "dice 'X'");
# Test of Reorder:
$a = sequence(5,3,2);
my @newDimOrder = (2,1,0);
$b = $a->reorder(@newDimOrder);
# since doing floating-point arithmetic here, should probably
# use a better test than "eq" here
#
is($b->average->average->sum , 72.5, "Test of reorder");
$a = zeroes(3,4);
$b = $a->dummy(-1,2);
is(join(',',$b->dims), '3,4,2');
$a = pdl(2);
print "a\n";
$b = $a->slice('');
ok(tapprox($a, $b), "Empty slice");
$a = pdl [1,1,1,3,3,4,4,1,1,2];
$b = null;
$c = null;
rle($a,$b,$c);
ok(tapprox($a, rld($b,$c)));
$b = $a->mslice(0.5);
ok(tapprox($b, 1), "mslice 1");
$b = $a->mslice([0.5,2.11]);
is("$b", "[1 1 1]", "mslice 2");
$a = zeroes(3,3);
$b = $a->splitdim(3,3);
eval '$b->make_physdims';
like($@, qr/^Splitdim: nthdim/, "make_physdim: Splitdim");
$a = sequence 5,5;
$b = $a->diagonal(0,1);
is("$b", "[0 6 12 18 24]", "diagonal");
$a = sequence 10;
eval '$b = $a->lags(1,1,1)->make_physdims';
like($@, qr/lags: dim out of range/, "make_physdim: out of range");
eval '$b = $a->lags(0,-1,1)->make_physdims';
like($@, qr/lags: step must be positive/, "make_physdim: negative step");
eval '$b = $a->lags(0,1,11)->make_physdims';
like($@, qr/too large/, "make_pyhsdim: too large");
##############################
# Tests of permissive slicing and dummying
$a = xvals(5,5)+10*yvals(5,5);
eval '$b = $a->slice("1,2,(0)")->make_physical';
ok(!$@);
is($b->ndims, 2, "slice->make_physical: ndims");
is(pdl($b->dims)->sumover, 2, "slice->make_physical: dims");
eval '$c = $a->slice("1,2,(1)")->make_physical';
like($@, qr/too many dims/i, "slice->make_physical: too many dims");
# Hmmm, think these could be split up but not sure exactly what is being
# tested so leave as is (ish)
#
eval '$d = $a->slice("0:1,2:3,0")->make_physical';
ok(!$@);
ok(eval '$d->ndims == 3 && ((pdl($d->dims) == pdl(2,2,1))->sumover == 3)' && !$@);
eval '$d = $a->slice("0:1,2:3,0")->xchg(0,2)';
ok(!$@, "slice->xchg");
ok(eval '$d->ndims == 3 && ((pdl($d->dims) == pdl(1,2,2))->sumover == 3)' && !$@);
eval '$e = $a->dummy(6,2)';
ok(!$@, "dummy");
ok(eval '$e->ndims == 7 && ((pdl($e->dims) == pdl(5,5,1,1,1,1,2))->sumover==7)' && !$@);
##############################
# Tests of indexND (Nowadays this is just another call to range)
my ($source, $index, $dest, $z);
# Basic indexND operation
$source = 10*xvals(10,10) + yvals(10,10);
$index = pdl([[2,3],[4,5]],[[6,7],[8,9]]);
eval '$a = $source->indexND( $index )';
ok(!$@);
ok(eval 'zcheck($a != pdl([23,45],[67,89]))', "eval of zcheck 1");
# Threaded indexND operation
$source = 100*xvals(10,10,2)+10*yvals(10,10,2)+zvals(10,10,2);
$index = pdl([[2,3],[4,5]],[[6,7],[8,9]]);
eval '$a = $source->indexND($index)';
ok(!$@);
ok(eval 'zcheck($a != pdl([[230,450],[670,890]],[[231,451],[671,891]]))', "eval of zcheck 2");
##############################
# Tests of range operator
# Basic range operation
$source = 10*xvals(10,10) + yvals(10,10);
$index = pdl([[2,3],[4,5]],[[6,7],[8,9]]);
eval '$dest = $source->range($index);';
ok(!$@);
ok(eval 'zcheck($dest != pdl([23,45],[67,89]));', "eval of zcheck 3");
# Make a 3x3 range at each index
eval '$dest = $source->range($index,3);';
ok(!$@);
# Check that the range has the correct size
is($dest->ndims, 4, "ndims after range");
ok(zcheck(pdl($dest->dims) != pdl(2,2,3,3)), "zcheck after range");
#### Check boundary conditions
eval '$z = $dest->copy;'; # Should throw range-out-of-bounds error
ok($@); # should check actual error message here
## Truncation
eval '$z = $source->range($index,3,"t")->copy;';
ok(!$@); # Should NOT throw range-out-of-bounds error.
ok(zcheck($z->slice("(1),(1)") != pdl([[89,99,0],[0,0,0],[0,0,0]])));
## Truncation on one axis, periodic on another; string syntax
eval '$z = $source->range($index,3,"tp")';
ok(zcheck($z->slice("(1),(1)") != pdl([[89,99,0],[80,90,0],[81,91,0]])));
## Periodic on first axis, extension on another; list syntax
eval '$z = $source->range($index,3,["e","p"]);';
ok(zcheck($z->slice("(1),(1)") != pdl([[89,99,99],[80,90,90],[81,91,91]])));
|