Repository navigation
ENH: add new function replace - #1072
willow-ahrens wants to merge 4 commits into
Conversation
b8600dc to
6afe7ce
Compare
lucascolley
left a comment
There was a problem hiding this comment.
thanks @willow-ahrens ! Just one question on the docs.
| For sparse arrays, elements which are not explicitly stored are replaced too: | ||
| if `old` equals the fill value, the fill value of the result is `new`. | ||
| The result keeps the sparse format of `x` and is never densified. |
There was a problem hiding this comment.
Question: this is specific to namespaces satisfying the _compat.is_pydata_sparse_namespace predicate right now, right?
There was a problem hiding this comment.
Maybe this is part of a bigger question. It's my intention for sparse arrays to have the same semantic meaning as dense arrays (So that we don't need to store extra data that specifies stored/not stored). These three lines are meant to clarify the implication of matching dense behavior: apply all ops to the fill value too. Because this function is a bit of a signal to sparse frameworks that they should change the fill value, I meant to add this to make that clear. So in that sense, it's not pydata/sparse specific, Finch also should care about it.
There was a problem hiding this comment.
right, I think the tone of the comment should just be more 'sparse array libraries can and should implement this as a change of fill value, without densification', rather than 'as a promise to array-api-extra users, all sparse arrays will avoid densification'.
I know you didn't intend the latter, but I think it could be read that way, while there is nothing stopping a sparse library from having an implementation of xp.where that in fact densifies, right? In which case, this function would densify too (apart from the special-cased pydata sparse).
Does that make sense?
There was a problem hiding this comment.
One thing that might be helpful is to have two separate paragraphs:
- what happens for generic libraries (that might be sparse, but otherwise implemented in any way)
- what happens for known sparse libraries (i.e. what is special-cased, or what behaviour do we get given assumptions about how the sparse library implements other functions)
fixes #992
adds a function
xp.replace(array, old, new)which replaces valueoldwith valuenewinarray. This function is helpful in sparse kernels. for example, after loading a graph matrix, one may wish to replace the zero value with infinity if they wish to then implement a shortest-paths operation: https://github.com/SparseArrayProgrammingSuite/SparseArrayProgrammingSuite/blob/33b2949b4c0e960df8818e94406ed5bd970cc76c/src/saps/util/adjacency.py#L63.The operation
sparse.where(A != old, A, np.inf)needs to allocate a boolean mask to perform this operation, but a fused replace would be a good moment to signal to sparse arrays that they should change fill values explicitly, and could give an opportunity to do so in O(1) time if the explicitly stored entries are already known not to be fill.This can also be a good way to fix exceptional values, like
NaN -> 0versusNaN -> Infhttps://github.com/SparseArrayProgrammingSuite/SparseArrayProgrammingSuite/blob/33b2949b4c0e960df8818e94406ed5bd970cc76c/src/saps/benchmarks/pcg.py#L1920