Tony S Yu | 26 Sep 19:38
Favicon

spy: ignore zero values in sparse matrix

When sparse matrices have explicit zero values, `axes.spy` plots those  
zero values. This behavior seems unintentional. For example, the  
following code should have a main diagonal with markers missing in the  
middle, but `spy` currently plots a full main diagonal.

#~~~~~~~~~~~
import scipy.sparse as sparse
import matplotlib.pyplot as plt

sp = sparse.spdiags([[1,1,1,0,0,0,1,1,1]], [0], 9, 9)
plt.spy(sp, marker='.')
#~~~~~~~~~~~

Below is a patch which only plots the nonzero entries in a sparse  
matrix. Note, sparse matrices with all zero entries raises an error;  
this behavior differs from dense matrices. I could change this  
behavior, but I wanted to minimize the code changed.

Cheers,

-Tony

PS: this patch also includes two trivial changes to some examples.

Index: lib/matplotlib/axes.py
===================================================================
--- lib/matplotlib/axes.py	(revision 6122)
+++ lib/matplotlib/axes.py	(working copy)
@@ -6723,9 +6723,11 @@
          else:
(Continue reading)

John Hunter | 26 Sep 20:28
Gravatar

Re: spy: ignore zero values in sparse matrix

On Fri, Sep 26, 2008 at 12:39 PM, Tony S Yu <tonyyu@...> wrote:

> +                if all(nonzero == False):
> +                    raise ValueError('spy cannot plot sparse zeros
> matrix')

Is raising an exception the right choice here -- why can't we plot an
all zeros image?

JDH

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
Tony S Yu | 26 Sep 21:36
Favicon

Re: spy: ignore zero values in sparse matrix


On Sep 26, 2008, at 2:28 PM, John Hunter wrote:

> On Fri, Sep 26, 2008 at 12:39 PM, Tony S Yu <tonyyu@...> wrote:
>
>> +                if all(nonzero == False):
>> +                    raise ValueError('spy cannot plot sparse zeros
>> matrix')
>
> Is raising an exception the right choice here -- why can't we plot an
> all zeros image?
>
> JDH

I guess you could plot sparse all-zero matrices with image mode.  My  
only hesitation is that sparse arrays tend to be very large and (I  
imagine) this would lead to very slow performance. I assumed this was  
the reason image mode wasn't adapted to use sparse arrays.

Actually, now that I think about it: you could plot a trivially small  
image and just adjust the coordinates so that they correspond to the  
original matrix shape. Is this what you were thinking?

I should note that a dense zero array also fails to plot with spy *if  
marker mode is used*.

-T

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
(Continue reading)

Eric Firing | 26 Sep 23:01
Favicon

Re: spy: ignore zero values in sparse matrix

Tony S Yu wrote:
> On Sep 26, 2008, at 2:28 PM, John Hunter wrote:
> 
>> On Fri, Sep 26, 2008 at 12:39 PM, Tony S Yu <tonyyu@...> wrote:
>>
>>> +                if all(nonzero == False):
>>> +                    raise ValueError('spy cannot plot sparse zeros
>>> matrix')
>> Is raising an exception the right choice here -- why can't we plot an
>> all zeros image?
>>
>> JDH
> 
> I guess you could plot sparse all-zero matrices with image mode.  My  
> only hesitation is that sparse arrays tend to be very large and (I  
> imagine) this would lead to very slow performance. I assumed this was  
> the reason image mode wasn't adapted to use sparse arrays.

Also, if an image cannot be resolved by the output device, info is 
lost--one might not see anything at a location where there actually is a 
value--whereas with markers, a marker will always show up, and the only 
problem is that one can't necessarily distinguish a single point from a 
cluster.

The real problem with all-zero values is that plot can't handle 
"plot([],[])".  One can work around this by putting in bogus values to 
plot a single point, saving the line, and then setting the line data to 
empty; or, better, by not using the high-level plot command, but by 
generating the Line2D object and adding it to the axes.  The Line2D 
initializer is happy with empty x and y sequences. I think if you use 
(Continue reading)

Tony S Yu | 27 Sep 18:01
Favicon

Re: spy: ignore zero values in sparse matrix


On Sep 26, 2008, at 5:01 PM, Eric Firing wrote:
> Also, if an image cannot be resolved by the output device, info is  
> lost--one might not see anything at a location where there actually  
> is a value--whereas with markers, a marker will always show up, and  
> the only problem is that one can't necessarily distinguish a single  
> point from a cluster.
>
> The real problem with all-zero values is that plot can't handle  
> "plot([],[])".  One can work around this by putting in bogus values  
> to plot a single point, saving the line, and then setting the line  
> data to empty; or, better, by not using the high-level plot command,  
> but by generating the Line2D object and adding it to the axes.  The  
> Line2D initializer is happy with empty x and y sequences. I think if  
> you use this approach it will kill two bugs (failure on all-zeros  
> with sparse and full arrays) with one very simple stone.
>
> Eric

Thanks for the tip Eric. Below is a patch for spy that implements  
Eric's suggestion. This patch seems to work for a couple simple tests  
on my end: sparse and dense arrays with non-zero and all-zero values.

A couple of notes:

* the call to `add_artist` isn't needed to show the correct plot, but  
it may be helpful for debugging.

* the docstring for `spy` suggests that a Line2D instance is returned,  
but `spy` currently returns a list with a Line2D instance. I set all- 
(Continue reading)

Eric Firing | 28 Sep 00:22
Favicon

Re: spy: ignore zero values in sparse matrix

Tony S Yu wrote:
> 
> On Sep 26, 2008, at 5:01 PM, Eric Firing wrote:
>> Also, if an image cannot be resolved by the output device, info is 
>> lost--one might not see anything at a location where there actually is 
>> a value--whereas with markers, a marker will always show up, and the 
>> only problem is that one can't necessarily distinguish a single point 
>> from a cluster.
>>
>> The real problem with all-zero values is that plot can't handle 
>> "plot([],[])".  One can work around this by putting in bogus values to 
>> plot a single point, saving the line, and then setting the line data 
>> to empty; or, better, by not using the high-level plot command, but by 
>> generating the Line2D object and adding it to the axes.  The Line2D 
>> initializer is happy with empty x and y sequences. I think if you use 
>> this approach it will kill two bugs (failure on all-zeros with sparse 
>> and full arrays) with one very simple stone.
>>
>> Eric
> 
> 
> Thanks for the tip Eric. Below is a patch for spy that implements Eric's 
> suggestion. This patch seems to work for a couple simple tests on my 
> end: sparse and dense arrays with non-zero and all-zero values.

Tony,

Thanks.  I will take care of this shortly, along with fixing the failure 
of plot([],[]) and maybe a few other things.

(Continue reading)

Eric Firing | 28 Sep 02:56
Favicon

Re: spy: ignore zero values in sparse matrix

Tony S Yu wrote:
> 
> On Sep 26, 2008, at 5:01 PM, Eric Firing wrote:
>> Also, if an image cannot be resolved by the output device, info is 
>> lost--one might not see anything at a location where there actually is 
>> a value--whereas with markers, a marker will always show up, and the 
>> only problem is that one can't necessarily distinguish a single point 
>> from a cluster.
>>
>> The real problem with all-zero values is that plot can't handle 
>> "plot([],[])".  One can work around this by putting in bogus values to 
>> plot a single point, saving the line, and then setting the line data 
>> to empty; or, better, by not using the high-level plot command, but by 
>> generating the Line2D object and adding it to the axes.  The Line2D 
>> initializer is happy with empty x and y sequences. I think if you use 
>> this approach it will kill two bugs (failure on all-zeros with sparse 
>> and full arrays) with one very simple stone.
>>
>> Eric
> 
> 
> Thanks for the tip Eric. Below is a patch for spy that implements Eric's 
> suggestion. This patch seems to work for a couple simple tests on my 
> end: sparse and dense arrays with non-zero and all-zero values.
> 
> A couple of notes:
> 
> * the call to `add_artist` isn't needed to show the correct plot, but it 
> may be helpful for debugging.
> 
(Continue reading)

Tony S Yu | 2 Oct 03:26
Favicon

Re: spy: ignore zero values in sparse matrix

Hi Eric,

Sorry for the late reply.

On Sep 27, 2008, at 8:56 PM, Eric Firing wrote:

> Actually, I think the most logical thing would be to let the default  
> None give the old behavior, and require precision=0 to get the new  
> behavior.  What do you think?  Is it OK if I make this change?  It  
> is more consistent with the old behavior.

I'm ambivalent about this change. On one hand, I think it makes a lot  
more sense to have None give the old behavior and precision=0 to  
ignore zero values in the sparse array (then precision would be  
consistent for finite values and for zero).

On the other hand, I think ignoring zero values should be the default  
behavior for sparse arrays (although, I definitely agree there should  
be the option to plot all assigned values).

Would it be possible to make the change you suggest and also change  
the default precision value to 0? (see diff below) This change would  
also allow you to remove a lot of the special handling for  
precision=None, since precision=0 gives the same result (I didn't go  
this far in the diff below).

> I also changed the behavior so that if a sparse array is input, with  
> no marker specifications, it simply makes a default marker plot  
> instead of raising an exception.

(Continue reading)

Eric Firing | 4 Oct 09:18
Favicon

Re: spy: ignore zero values in sparse matrix

Tony S Yu wrote:
> Hi Eric,
> 
> Sorry for the late reply.
> 
> On Sep 27, 2008, at 8:56 PM, Eric Firing wrote:
> 
>> Actually, I think the most logical thing would be to let the default  
>> None give the old behavior, and require precision=0 to get the new  
>> behavior.  What do you think?  Is it OK if I make this change?  It  
>> is more consistent with the old behavior.
> 
> I'm ambivalent about this change. On one hand, I think it makes a lot  
> more sense to have None give the old behavior and precision=0 to  
> ignore zero values in the sparse array (then precision would be  
> consistent for finite values and for zero).
> 
> On the other hand, I think ignoring zero values should be the default  
> behavior for sparse arrays (although, I definitely agree there should  
> be the option to plot all assigned values).
> 
> Would it be possible to make the change you suggest and also change  
> the default precision value to 0? (see diff below) This change would  
> also allow you to remove a lot of the special handling for  
> precision=None, since precision=0 gives the same result (I didn't go  
> this far in the diff below).

Good point.  I made that change, but then made precision='present' be 
the value for sparse arrays to show all filled cells.  precision=None is 
deprecated, but converted to 0.
(Continue reading)

John Hunter | 26 Sep 21:38
Gravatar

Re: spy: ignore zero values in sparse matrix

On Fri, Sep 26, 2008 at 2:36 PM, Tony S Yu <tonyyu@...> wrote:

> I guess you could plot sparse all-zero matrices with image mode.  My only
> hesitation is that sparse arrays tend to be very large and (I imagine) this
> would lead to very slow performance. I assumed this was the reason image
> mode wasn't adapted to use sparse arrays.
>
> Actually, now that I think about it: you could plot a trivially small image
> and just adjust the coordinates so that they correspond to the original
> matrix shape. Is this what you were thinking?

This is something I considered, but I was thinking less about the
implementation and more about the functionality.  I don't want to
raise an exception unless the input doesn't make sense.  I would
rather the user start at a boring image and figure out why it is blank
that deal with an exception.

> I should note that a dense zero array also fails to plot with spy *if marker
> mode is used*.

Can you fix this along with spy2?

JDH

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
(Continue reading)

Tony S Yu | 26 Sep 22:01
Favicon

Re: spy: ignore zero values in sparse matrix


On Sep 26, 2008, at 3:38 PM, John Hunter wrote:

> On Fri, Sep 26, 2008 at 2:36 PM, Tony S Yu <tonyyu@...> wrote:
>>
>> Actually, now that I think about it: you could plot a trivially  
>> small image
>> and just adjust the coordinates so that they correspond to the  
>> original
>> matrix shape. Is this what you were thinking?
>
> This is something I considered, but I was thinking less about the
> implementation and more about the functionality.  I don't want to
> raise an exception unless the input doesn't make sense.  I would
> rather the user start at a boring image and figure out why it is blank
> that deal with an exception.

Yeah, I agree this is much friendlier.

>> I should note that a dense zero array also fails to plot with spy  
>> *if marker
>> mode is used*.
>
> Can you fix this along with spy2?

I assume you mean spy, not spy2 (I just searched through the  
matplotlib files and saw that spy2 hasn't existed since 2006). I'll  
work on a patch to return a blank plot using the method described  
above (unless someone chimes in with a better suggestion).

(Continue reading)


Gmane