Skip to content
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

len() function for COO objects #68

Merged
merged 3 commits into from Jan 10, 2018
Merged

len() function for COO objects #68

merged 3 commits into from Jan 10, 2018

Conversation

@nils-werner
Copy link
Contributor

@nils-werner nils-werner commented Jan 10, 2018

Quite trivial really, implements the len() operation following NumPy's convention of

len(array) == array.shape[0]
@hameerabbasi
Copy link
Collaborator

@hameerabbasi hameerabbasi commented Jan 10, 2018

Seems fine, however it would be really nice to have docstrings for these, and if possible, tests.

@nils-werner
Copy link
Contributor Author

@nils-werner nils-werner commented Jan 10, 2018

Added them

@hameerabbasi
Copy link
Collaborator

@hameerabbasi hameerabbasi commented Jan 10, 2018

Well, this looks good. Let me know if you're finished working on it, and I'll review and merge (don't want to be too fast this time).

@nils-werner
Copy link
Contributor Author

@nils-werner nils-werner commented Jan 10, 2018

This is ready to be merged.

@mrocklin
Copy link
Collaborator

@mrocklin mrocklin commented Jan 10, 2018

Agreed.

@mrocklin mrocklin merged commit 988c6a3 into pydata:master Jan 10, 2018
1 check failed
1 check failed
continuous-integration/travis-ci/pr The Travis CI build failed
Details
@mrocklin
Copy link
Collaborator

@mrocklin mrocklin commented Jan 10, 2018

Thanks @nils-werner !

@nils-werner
Copy link
Contributor Author

@nils-werner nils-werner commented Jan 10, 2018

One unexpected sideeffect of this:

numpy.array(sparse_arr)

now magically works!

x = sparse.random((20, 30))
np.allclose(x.todense(), np.array(x))    # True
@hameerabbasi
Copy link
Collaborator

@hameerabbasi hameerabbasi commented Jan 10, 2018

I'm going to assume it's really slow. Do we have any benchmarks for this?

@nils-werner
Copy link
Contributor Author

@nils-werner nils-werner commented Jan 10, 2018

Yes, it's very very slow.

If I understand it correctly, numpy.array(x) looks for buffer and __array_interface__ when creating an array from an object. If it doesn't find one it simply iterates over it.

What if we implement __array_interface__ that calls self.todense()?

@property
def __array_interface__(self):
    return {
        'shape': self.shape,
        'data': self.todense(),
        'typestr': self.dtype.str,
    }

Before

x = sparse.random((20, 30, 20))

%timeit numpy.array(x)
# 1.48 s ± 25.5 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
%timeit x.todense()
# 11.6 µs ± 40.3 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
numpy.allclose(x, x.todense)
# True

After

%timeit numpy.array(x)
# 23 µs ± 392 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)
numpy.allclose(x, x.todense)
# True

I can't explain where the 2x slowdown (11 vs 23 us) comes from though...

@hameerabbasi
Copy link
Collaborator

@hameerabbasi hameerabbasi commented Jan 10, 2018

A pull request would be welcome. Also, your data is too small. Maybe it's just the overhead.

@nils-werner nils-werner deleted the nils-werner:len branch Jan 15, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

3 participants