Add from_dense and to_dense methods#382
Conversation
|
Compare: def from_dense(cls, value, missing_value=None, dtype=None, *, nrows=None, ncols=None, name=None):
# handle scalars and arraysvs def from_dense_scalar(cls, value, nrows, ncols, dtype=None, *, name=None):
def from_dense_array(cls, values, missing_value=None, dtype=None, *, name=None):I prefer two methods. But, what should we name them? |
Perhaps:
|
… `io.from_numpy`
|
Splitting I still need to update documentation. Do we like |
Yes, it is more readable now.
Looks good, I'm not sure how frequently this specific function is used (e.g. in algorithms). |
|
Updated and ready for review. |
|
We discussed this in our meeting today, and we settled on We could consider adding a |
|
@jim22k, what note about masks did you want to add? I think this PR is ready to merge, but I'll wait for an approval (for a couple days). |
|
The note could say that if you want to create an iso-valued Matrix or Vector with the same structure as an existing object, use |
…calar A more flexible way with any mask is e.g.: ```python w = Vector(v.dtype, size=v.size) w(~v.S) << value ```
|
Thanks @jim22k and @SultanOrazbayev for your input on this PR! Even though I'm getting credit for commits, I know y'all deserve credit for helping too, because your engagement does make things better ❤️ This is going in! 🚀 |
Closes #378.
@jim22k, I think we discussed to/from dense years ago, and I think we punted on methods like in this PR b/c it's surprisingly awkward to do in GraphBLAS. This PR shows that, indeed, this is difficult to do well, so I think they're good methods to add.
I also added
sort=Trueto e.g.to_csr, because the C spec says column indices within a row may be unsorted, but sometimes we want them to be sorted.to_cooalready hassort=True, so adding sorting more places seems natural.Currently,
from_denseis also used to create a fully dense Vector/Matrix from a scalar, which requires the shape to be provided. I think I like this better than a separate method such asMatrix.from_scalar(1, nrows=3, ncols=4).This also improves some handling of sub-array dtypes.
I still need to add documentation. I think the APIs and behaviors need the most careful review.