list_plot: "The truth value of an array with more than one element is ambiguous"

879 views
Skip to first unread message

Renan Birck Pinheiro

unread,
Sep 9, 2011, 8:05:33 PM9/9/11
to sage-s...@googlegroups.com
Hi,

I have the following Sage code (it comes from a larger Python program):

from scipy.stats import norm, randint

signalD = randint(0,2);
signal = signalD.rvs(10);

It successfully generates the array "signal" with 10 random elements.

However, this fails:

list_plot(signal)
        Traceback (click to the left of this block for traceback)
        ...
        ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

Any ideas? Using a syntax like list_plot([i for i in signal]) does work, however.

I'm using Sage 4.7.1 on x86_64 Arch Linux.

Thanks.
--
Renan Birck Pinheiro - Grupo de Microeletrônica - Engenharia Elétrica/UFSM

http://renanbirck.blogspot.com / skype: renan.ee.ufsm

Justin C. Walker

unread,
Sep 9, 2011, 8:32:55 PM9/9/11
to sage-s...@googlegroups.com
Hi,

On Sep 9, 2011, at 17:05 , Renan Birck Pinheiro wrote:

> I have the following Sage code (it comes from a larger Python program):
>
> from scipy.stats import norm, randint
>
> signalD = randint(0,2);
> signal = signalD.rvs(10);
>
> It successfully generates the array "signal" with 10 random elements.
>
> However, this fails:
>
> list_plot(signal)
> Traceback (click to the left of this block for traceback)
> ...
> ValueError: The truth value of an array with more than one element
> is ambiguous. Use a.any() or a.all()

I *think* that the problem is the type of 'signal': it's a "numpy.ndarray". Evidently, such a gizmo can be coerced to a list or tuple, but not a dictionary.

Your "work-around" (or "list(signal)") should make it work, as you've noticed.

I don't know whether this can be called a bug, or that other thing. Anyone have a thought?

HTH

Justin

--
Justin C. Walker, Curmudgeon at Large
Institute for the Absorption of Federal Funds
--
Democracy is two wolves and a lamb
voting on what to have for lunch.
Liberty is a well-armed lamb contesting
the vote.

Jason Grout

unread,
Sep 10, 2011, 11:07:04 PM9/10/11
to sage-s...@googlegroups.com
On 9/9/11 7:05 PM, Renan Birck Pinheiro wrote:
> Hi,
>
> I have the following Sage code (it comes from a larger Python program):
>
> from scipy.stats import norm, randint
>
> signalD = randint(0,2);
> signal = signalD.rvs(10);
>
> It successfully generates the array "signal" with 10 random elements.
>
> However, this fails:
>
> list_plot(signal)
> Traceback (click to the left of this block for traceback)
> ...
> ValueError: The truth value of an array with more than one
> element is ambiguous. Use a.any() or a.all()
>
> Any ideas? Using a syntax like list_plot([i for i in signal]) does work,
> however.
>


It's a bug in how list_plot checks for an empty list. We can see in the
error message (below):

sage: import numpy as np
sage: x=np.array(range(10))
sage: x
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
sage: list_plot(x)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)

/Users/grout/<ipython console> in <module>()

/Users/grout/sage/local/lib/python2.6/site-packages/sage/plot/plot.pyc
in list_plot(data, plotjoined, **kwargs)
3561 """
3562 from sage.plot.all import line, point
-> 3563 if data == {} or data == () or data == []:
3564 return Graphics()


list_plot is trying to check to see if there is an empty
list/tuple/dict. I think it should just do len(data). The real root of
the error we are seeing is that numpy arrays compare element-by-element
(IIRC) for the dictionary comparison:

sage: x=={}
array([False, False, False, False, False, False, False, False, False,
False], dtype=bool)

Then trying to do bool(resulting array) throws the error.

Anyways, your workaround works great for now. I've opened
http://trac.sagemath.org/sage_trac/ticket/11787 to track this issue.

Thanks,

Jason

Reply all
Reply to author
Forward
0 new messages