New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement __array__ protocol #87

Merged
merged 2 commits into from Jan 25, 2018

Conversation

Projects
None yet
2 participants
@mrocklin
Collaborator

mrocklin commented Jan 25, 2018

One approach to the problems described in #72

@mrocklin

This comment has been minimized.

Collaborator

mrocklin commented Jan 25, 2018

I haven't thought through this very deeply. Criticism welcome from @nils-werner and @hameerabbasi

@@ -894,6 +894,7 @@ def test_scipy_sparse_interface():
assert_eq(x, xx)
assert_eq(x.T.dot(x), xx.T.dot(xx))
assert isinstance(x + xx, (COO, scipy.sparse.spmatrix))

This comment has been minimized.

@hameerabbasi

hameerabbasi Jan 25, 2018

Collaborator

Perhaps test only for COO? Since we're "higher level" than scipy.sparse, it makes sense that mixed binary operations don't return scipy.sparse.spmatrix. Otherwise we have things like x + xx + x returning scipy.sparse.spmatrix.

This comment has been minimized.

@hameerabbasi

hameerabbasi Jan 25, 2018

Collaborator

In addition (just as a minor quip) I would parametrize this with format for scipy.sparse and check for (at least) csr, csc, coo and dok.

This comment has been minimized.

@mrocklin

mrocklin Jan 25, 2018

Collaborator

Done

x = self.todense()
if dtype and x.dtype != dtype:
x = x.astype(dtype)
if not isinstance(x, np.ndarray):

This comment has been minimized.

@hameerabbasi

hameerabbasi Jan 25, 2018

Collaborator

Maybe np.asarray? It passes through things that are already arrays.

But, in any case, do we really need this check, since .todense() already returns an np.ndarray.

This comment has been minimized.

@mrocklin

mrocklin Jan 25, 2018

Collaborator

Removed

@hameerabbasi

Some minor comments. It seems to work fine! spmatrix + COO returns COO as desired now! (at least with CSR, CSC and COO).

In addition, for now it's okay, but later with #10 (which I'm working on next), I would like to treat this with the auto-densification rules...

@hameerabbasi

This comment has been minimized.

Collaborator

hameerabbasi commented Jan 25, 2018

Hmm. I just tested this some more:

>>> x * xx
Traceback (most recent call last):
  File "/Users/hameerabbasi/anaconda/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 2910, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-7-d2266dae7fac>", line 1, in <module>
>>> x * xx
  File "/Users/hameerabbasi/anaconda/lib/python3.6/site-packages/scipy/sparse/base.py", line 476, in __mul__
    raise ValueError('dimension mismatch')
ValueError: dimension mismatch
>>> xx * x
Out[8]: <COO: shape=(100, 10), dtype=uint8, nnz=96, sorted=False, duplicates=False>
@mrocklin

This comment has been minimized.

Collaborator

mrocklin commented Jan 25, 2018

Can you explain more about how you're getting that error? I'm not able to reproduce.

@hameerabbasi

This comment has been minimized.

Collaborator

hameerabbasi commented Jan 25, 2018

add and sub work fine, mul gives that error.

@mrocklin

This comment has been minimized.

Collaborator

mrocklin commented Jan 25, 2018

Yeah, I mentioned this in the other PR, but just for completeness __mul__ in scipy.sparse matrices will be matrix-multiplication, which I expect would be a bit stranger. Personally I'm ok seeing this fail.

@hameerabbasi

This comment has been minimized.

Collaborator

hameerabbasi commented Jan 25, 2018

In that case, this gets a +1 from me. :)

@hameerabbasi

This comment has been minimized.

Collaborator

hameerabbasi commented Jan 25, 2018

We should merge this, I'll rebase #84 on top of it, and then I'm totally fine releasing.

@mrocklin mrocklin merged commit 5021571 into master Jan 25, 2018

2 checks passed

continuous-integration/travis-ci/pr The Travis CI build passed
Details
continuous-integration/travis-ci/push The Travis CI build passed
Details

@mrocklin mrocklin deleted the array-protocol branch Jan 25, 2018

@mrocklin

This comment has been minimized.

Collaborator

mrocklin commented Jan 25, 2018

@hameerabbasi ok to release current master as 0.2.0 ?

@hameerabbasi

This comment has been minimized.

Collaborator

hameerabbasi commented Jan 25, 2018

Of course. :)

hameerabbasi added a commit to hameerabbasi/sparse that referenced this pull request Feb 27, 2018

Implement __array__ protocol (pydata#87)
* Implement __array__ protocol

* add parametrized test for scipy sparse interactions
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment