Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I propose to include CSR/CSC sparse arrays wrapped as Img.
Motivation
The need for supporting traditional sparse arrays arose while working on a spatial transcriptomics visualization library using ImgLib2. This library needs to read and write files stored in the (Python based) AnnData format, which stores certain data as scipy CSR/CSC arrays on disk. While an on-the-fly conversion to a dense array would be possible in most cases, it is more convenient having the sparse data represented as proper ImgLib2 Imgs.
Also, considering the popularity of CSR/CSC arrays in scientific computing in general, it's maybe of independent interest to represent and visualize them in ImgLib2.
Changes
I introduced the following without changing existing code:
SparseImg
class from which CSR and CSC style Imgs are derived. These basically wrap three 1DArrayImg
s (data, indices on contiguous slices of the matrix, and pointers to those slices in the data / indices array). This abstract class also features some static utility methods.SparseRandomAcess
that, given an index, finds the corresponding element inO(log(n))
, wheren
is the size of the contiguous dimension.SparseLocalizingCursor
that traverses the whole sparse array in linear complexity and (trivially) without cache misses on the underlyingArrayImg
s. Since any cursor needs to track the current position internally to check if the next element is zero, alsoImg::cursor
returns the localizing cursor.SparseImgFactory
that creates an empty CSR/CSC array.Possible problems
O(number of current non-zero elements)
steps, this operation is usually not supported by sparse arrays. Thus, populating a new sparse matrix created by a factory is not possible, severely limiting the usefulness ofSparseImgFactory
.