-
Notifications
You must be signed in to change notification settings - Fork 157
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
(fix): cache indptr
for backed sparse matrices
#1266
Changes from 7 commits
d7097df
d78e623
f9b9b51
6622ba5
9b48a9d
b921596
32e8230
508a248
25ad928
eed21ab
fd814f9
45abc11
c222dd8
93a7ae9
1dcd763
7b1c66c
badc6ad
376872d
730b62f
fc593cc
b448e88
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,7 @@ | |
import collections.abc as cabc | ||
import warnings | ||
from abc import ABC | ||
from functools import cached_property | ||
from itertools import accumulate, chain | ||
from typing import TYPE_CHECKING, Literal, NamedTuple | ||
|
||
|
@@ -282,11 +283,21 @@ class BaseCompressedSparseDataset(ABC): | |
|
||
def __init__(self, group: h5py.Group | ZarrGroup): | ||
type(self)._check_group_format(group) | ||
self.group = group | ||
self._group = group | ||
|
||
shape: tuple[int, int] | ||
"""Shape of the matrix.""" | ||
|
||
@property | ||
def group(self): | ||
return self._group | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would be okay with this being made public, but could you add a docstring + type hint for this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So the type hint appears to be unnecessary. You'll notice that some of the builds failed because of documentation issues - I honestly don't know why. The other references to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry I lied. The docs don't work but the type hints do work e.g., in VSCode. I was looking at the wrong There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I have no idea why this is breaking. Extremely strange stuff. I will look again tomorrow. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This looks like it's working. Can we resolve? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well I did not push the change, so the docs are building because of that, unless this works for you locally. |
||
|
||
@group.setter | ||
def group(self, val): | ||
raise TypeError( | ||
f"Do not reset group on a {type(self)}. Instead use `sparse_dataset` to make a new class." | ||
) | ||
ilan-gold marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
ilan-gold marked this conversation as resolved.
Show resolved
Hide resolved
|
||
@property | ||
def backend(self) -> Literal["zarr", "hdf5"]: | ||
if isinstance(self.group, ZarrGroup): | ||
|
@@ -431,20 +442,24 @@ def append(self, sparse_matrix: ss.spmatrix): | |
indices.resize((orig_data_size + sparse_matrix.indices.shape[0],)) | ||
indices[orig_data_size:] = sparse_matrix.indices | ||
|
||
@cached_property | ||
def indptr(self): | ||
ilan-gold marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return self.group["indptr"][...] | ||
|
||
ilan-gold marked this conversation as resolved.
Show resolved
Hide resolved
|
||
def _to_backed(self) -> BackedSparseMatrix: | ||
format_class = get_backed_class(self.format) | ||
mtx = format_class(self.shape, dtype=self.dtype) | ||
mtx.data = self.group["data"] | ||
mtx.indices = self.group["indices"] | ||
mtx.indptr = self.group["indptr"][:] | ||
mtx.indptr = self.indptr | ||
return mtx | ||
|
||
def to_memory(self) -> ss.spmatrix: | ||
format_class = get_memory_class(self.format) | ||
mtx = format_class(self.shape, dtype=self.dtype) | ||
mtx.data = self.group["data"][...] | ||
mtx.indices = self.group["indices"][...] | ||
mtx.indptr = self.group["indptr"][...] | ||
mtx.indptr = self.indptr | ||
return mtx | ||
|
||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this the change that was giving you problems with the docs?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes!