Skip to content

Commit

Permalink
(feat): add cache workaround
Browse files Browse the repository at this point in the history
  • Loading branch information
ilan-gold committed Mar 16, 2023
1 parent 974293e commit 4c7f57d
Showing 1 changed file with 9 additions and 12 deletions.
21 changes: 9 additions & 12 deletions anndata/experimental/read_remote/read_remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@


class LazyCategoricalArray(ExplicitlyIndexedNDArrayMixin):
__slots__ = ("codes", "categories", "attrs")
__slots__ = ("codes", "attrs", "_categories", "_categories_cache")

def __init__(self, group, *args, **kwargs):
"""Class for lazily reading categorical data from formatted zarr group
Expand All @@ -33,11 +33,16 @@ def __init__(self, group, *args, **kwargs):
group (zarr.Group): group containing "codes" and "categories" key as well as "ordered" attr
"""
self.codes = group["codes"]
self.categories = group["categories"][
...
] # slots don't mix with cached_property, ExpicitlyIndexedArray uses slots
self._categories = group["categories"]
self._categories_cache = None
self.attrs = dict(group.attrs)

@property
def categories(self): # __slots__ and cached_property are incompatible
if self._categories_cache is None:
self._categories_cache = self._categories[...]
return self._categories_cache

@property
def dtype(self) -> pd.CategoricalDtype:
return pd.CategoricalDtype(self.categories, self.ordered)
Expand All @@ -50,14 +55,6 @@ def shape(self) -> Tuple[int, ...]:
def ordered(self):
return bool(self.attrs["ordered"])

def __array__(
self, *args
) -> np.ndarray: # may need to override this, copied for now
a = self[...]
if args:
a = a.astype(args[0])
return a

def __getitem__(self, selection) -> pd.Categorical:
codes = self.codes.oindex[selection]
if codes.shape == (): # handle 0d case
Expand Down

0 comments on commit 4c7f57d

Please sign in to comment.