This result seems to violate the principle of least surprise.
On the LHS we have a PDL with badvalue(0) and on the RHS we
have a perl scalar with value 0 (which happens to be the bad
value for the PDL).

It seems surprising that $pdl > 0 is actually $pdl > pdl(0)->badvalue(0)

Thoughts?
Chris


-------- Forwarded Message --------
Subject:        Re: [Pdl-general] Weird behaviour of stats with bad values
Date:   Tue, 16 Jun 2015 09:18:29 -0400
From:   Chris Marshall <devel.chm...@gmail.com>
To: Derek Lamb <de...@boulder.swri.edu>, Marek Gierliński <marek.gierlin...@gmail.com>
CC:     pdl-gene...@lists.sourceforge.net



I think the problem is that if $s is a badvalue piddle with the badvalue=0
then $s>0 is $s>BAD which is BAD.  If you use a value for badvalue that is
not in the comparison, you should be good.

--Chris

On 6/16/2015 09:11, Derek Lamb wrote:
Thanks for the simple example.  That helps a lot.

The actual problem is that piddles with the badflags set always evaluate to BAD when element-wise logical operations are performed on them.

"print $s<0" prints BAD instead of 0 or 1. "print $x>0" also prints [BAD BAD BAD BAD].

$m and $s have their badflags set (as they should, since they were made from partially-bad data). You can check if they can be safely cleared, and clear them if so, with $s->check_badflag, etc. Since $m and $s are 1-element piddles, you can also just set them directly to their scalar values, for example

map{$_=$_->sclr} ($m,$s)=stats($x); #if you know they are only going to be 1 element piddles
OR
map{$_=$_->((0))} ($m,$s)=stats($x); #if you just want the first value

The fact that $m and $s are 1-element piddles is likely what is causing your problem. My guess is that this behavior has changed (perhaps intentionally, perhaps not) between your old version and more recent versions, and if you were getting $m and $s as simple perl scalars before you would not have seen this problem. I can verify that stats() has returned 1-piddles and not perl scalars at least as far back as PDL-2.4.11.

cheers,
Derek

On Jun 16, 2015, at 6:29 AM, Marek Gierliński <marek.gierlin...@gmail.com <mailto:marek.gierlin...@gmail.com>> wrote:

Oops, sorry, I missed a line when copying the text. Here is the code again.

use PDL;
use PDL::NiceSlice;


my $x = pdl(1, 2, 3, 0);
$x->badflag(1);
$x->badvalue(0);

print "x = $x\n";

my ($m, $s) = stats($x);

print "m = $m, s = $s\n";
print "s greater than zero\n" if $s > 0;
print "s less than zero\n" if $s < 0;
print "s equals zero\n" if $s == 0;


On Tue, 16 Jun 2015 at 12:20 Chris Marshall <devel.chm...@gmail.com <mailto:devel.chm...@gmail.com>> wrote:

    Hi Marek-

    Do you have a complete example showing the problem?
    You don't show where $m and $s are being set below.

    Thanks,
    Chris


    On 6/16/2015 07:04, Marek Gierliński wrote:
    I have recently encountered a weird problem with stats (or
    statsover) using bad values. The resulting numbers have a
    strange property, they always return true in comparisons. This
    code:

    use PDL;
    use PDL::NiceSlice;

    my $x = pdl(1, 2, 3, 0);
    $x->badflag(1); $x->badvalue(0);

    print "m = $m, s = $s\n";
    print "s greater than zero\n" if $s > 0;
    print "s less than zero\n" if $s < 0;
    print "s equals zero\n" if $s == 0;

    returns the following output:

    x = [1 2 3 BAD]
    m = 2, s = 1
    s greater than zero
    s less than zero
    s equals zero

    which doesn't make any sense. Can anyone explain this? I always
    assumed that 'stats' returned ordinary Perl scalars, but this
    must be something different.

    I'm using PDL version 2.007. We had an older version installed
    for a long time and I think this problem appeared when we
    upgraded to 2.007.

    Marek

------------------------------------------------------------------------------
_______________________________________________
pdl-devel mailing list
pdl-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/pdl-devel

Reply via email to