Skip to content

add full function to simplify COO creation #150

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

Closed
wants to merge 4 commits into from

Conversation

ahwillia
Copy link
Contributor

@ahwillia ahwillia commented May 5, 2018

This package may end up being very important to me, so I wanted to dip my toes in and try adding a simple extension in case I want to add something more substantial later.

The basic idea is to make array creation easier when all the data values are the same (I think this is incredibly common, e.g. if you are representing sparse graph structure)

sparse.full(np.array([[0,1,2],[0,1,2]]), 1).todense()

array([[1, 0, 0],
       [0, 1, 0],
       [0, 0, 1]])

A few things I wasn't sure on...

  • how to document **kwargs
  • Should I be accepting non-array objects for coords? In particular should I add something to the effect of coords = np.asarray(coords) at the top of my function

Hope that this is welcome and within scope. Thanks!

@codecov-io
Copy link

codecov-io commented May 5, 2018

Codecov Report

Merging #150 into master will increase coverage by 0.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #150      +/-   ##
==========================================
+ Coverage   95.77%   95.78%   +0.01%     
==========================================
  Files          10       10              
  Lines        1183     1187       +4     
==========================================
+ Hits         1133     1137       +4     
  Misses         50       50
Impacted Files Coverage Δ
sparse/coo/__init__.py 100% <ø> (ø) ⬆️
sparse/coo/core.py 93.71% <100%> (ø) ⬆️
sparse/coo/common.py 96.19% <100%> (+0.07%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 444655c...2070086. Read the comment docs.

Copy link
Collaborator

@hameerabbasi hameerabbasi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello, @ahwillia! A big thanks for adding to the project. Every contribution is welcome. As for your questions:

  • You can accept non-array values for coords, the COO constructor fixes those for you. See
    self.coords = np.asarray(coords)
  • I would recommend mirroring the signature of np.full rather than adding **kwargs, and then documenting those keywords as usual. If you prefer to keep **kwargs, though, just document it like this:
kwargs : dict, optional
    Additional arguments to pass to ``np.full``.


a = sparse.full(coords, 1)
e = np.diag(np.ones(5, dtype=int))
assert_eq(e, a, compare_dtype=True)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

compare_dtype is on by default, no need to add it here.


a = sparse.full(coords, 1.0)
e = np.diag(np.ones(5))
assert_eq(e, a, compare_dtype=True)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See comment to above.

@mrocklin
Copy link
Contributor

mrocklin commented May 5, 2018

I wonder if it would make sense to have the COO constructor do this automatically

x = COO(coords=..., data=1)

To my knowledge this doesn't conflict with any other potential input.

@hameerabbasi
Copy link
Collaborator

Yes, we can. The question is whether we should add this to the constuctor, which is already quite bloated (both in terms of using and coding).

@mrocklin
Copy link
Contributor

mrocklin commented May 5, 2018 via email

@hameerabbasi
Copy link
Collaborator

Fair enough. We can just replace self.data = ... with self.data = np.broadcast_to(..., coords.shape[1]).

@ahwillia
Copy link
Contributor Author

ahwillia commented May 5, 2018

Thanks for the feedback.

I would recommend mirroring the signature of np.full rather than adding **kwargs, and then documenting those keywords as usual.

I am actually passing kwargs to the COO constructor not to np.full. My reasoning here is that the constructor signature may change? This might be an argument for instead adding this functionality to the constructor as @mrocklin suggested.

As someone who just started playing around with the package I actually find the COO constructor a bit confusing. I would be in favor of keeping sparse.full(...) because it matches a common function in numpy and I think familiar users will know how to use it immediately.

I don't mind also adding this to the COO constructor as well -- having multiple ways to do something isn't necessarily a bad thing. On the other hand, I kind of wish that the COO constructor didn't accept a dense numpy array and instead threw an error pointing to COO.from_numpy(x). I managed to confuse myself quite a bit while initially playing around with this package.

Would it make sense to hold off on this PR and open an issue/discussion on the COO constructor behavior?

@hameerabbasi
Copy link
Collaborator

Sure. I'm +0.5 on changing constructor behaviour. @ahwillia would you mind opening that issue?

@ahwillia
Copy link
Contributor Author

Closing this in favor of #152

@ahwillia ahwillia closed this May 14, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants