Spyder: Add Sparse Matrix support in the Variable Explorer

Created on 20 Aug 2017  路  4Comments  路  Source: spyder-ide/spyder

Hi,

I think it might be very helpful if the variable explorer can show the sparse matrix. Right now only ndarray are supported. Along with it there could be a spy option in the menu.

Optional: maybe a convert to ndarray and convert to sparse can be added in the corresponding variable menu.

Thanks for the consideration.

Help wanted Variable Explorer Enhancement

Most helpful comment

I would like to share my temporary solution. But this should be considered as a "solution" to it.(my spyder version 3.2.1)

  1. I cheated spyder by adding a faked supported type in the function is_supported of file widgets/variableexplorer/utils.py
def is_supported(value, check_all=False, filters=None, iterate=True):
    """Return True if the value is supported, False otherwise"""
    import scipy.sparse as sps
    if isinstance(value, sps.spmatrix):
        return True
   # ...
  1. add two lines to value_to_display function in the same file
elif isinstance(value, scipy.sparse.spmatrix):
            display = 'nnz: %i, Min: %r\n Max: %r' %(value.nnz, value.min(), value.max())

and in get_size

elif isinstance(item, (ndarray, MaskedArray, scipy.sparse.spmatrix)):
        return item.shape
  1. modify imshow method in namespacebrowser.py
def imshow(self, name):
        sw = self.shellwidget
        import scipy
        try:
            sw.execute("%%varexp --spy %s" % name)
        except:
            if sw._reading:
                sw.dbg_exec_magic('varexp', '--imshow %s' % name)
            else:
                sw.execute("%%varexp --imshow %s" % name)
  1. Finally, I add a silly line of code into refresh_plot_entries functions in the collections editor
else:
            is_array = condition_plot = condition_imshow = is_list \
                     = condition_hist = False
condition_imshow = True # my code here
self.plot_action.setVisible(condition_plot or is_list)

This gives me the desired feature I want.

screen shot 2017-08-20 at 12 08 22 am
screen shot 2017-08-20 at 12 08 43 am

All 4 comments

I would like to share my temporary solution. But this should be considered as a "solution" to it.(my spyder version 3.2.1)

  1. I cheated spyder by adding a faked supported type in the function is_supported of file widgets/variableexplorer/utils.py
def is_supported(value, check_all=False, filters=None, iterate=True):
    """Return True if the value is supported, False otherwise"""
    import scipy.sparse as sps
    if isinstance(value, sps.spmatrix):
        return True
   # ...
  1. add two lines to value_to_display function in the same file
elif isinstance(value, scipy.sparse.spmatrix):
            display = 'nnz: %i, Min: %r\n Max: %r' %(value.nnz, value.min(), value.max())

and in get_size

elif isinstance(item, (ndarray, MaskedArray, scipy.sparse.spmatrix)):
        return item.shape
  1. modify imshow method in namespacebrowser.py
def imshow(self, name):
        sw = self.shellwidget
        import scipy
        try:
            sw.execute("%%varexp --spy %s" % name)
        except:
            if sw._reading:
                sw.dbg_exec_magic('varexp', '--imshow %s' % name)
            else:
                sw.execute("%%varexp --imshow %s" % name)
  1. Finally, I add a silly line of code into refresh_plot_entries functions in the collections editor
else:
            is_array = condition_plot = condition_imshow = is_list \
                     = condition_hist = False
condition_imshow = True # my code here
self.plot_action.setVisible(condition_plot or is_list)

This gives me the desired feature I want.

screen shot 2017-08-20 at 12 08 22 am
screen shot 2017-08-20 at 12 08 43 am

Hi @kailaix thanks for sharing your solution!, if you want maybe you could create a PR with this changes and then we could give you some comments on the code. @ccordoba12 @goanpeca @jitseniesen , what do you guys think?

Sure, go for it :-)

+1 from me too. @kailaix, please go for it!

Our only requirement is to make this against our master branch because Spyder 3.2 is now in maintenance, so we don't accept new features for it.

Was this page helpful?
0 / 5 - 0 ratings