Skip to content

Commit

Permalink
Empty query results typing (#101)
Browse files Browse the repository at this point in the history
* change default empty matrix dtype in fast csr builder

* ensure correct dtype on empty axis dataframe

* set correct shape on empty result
  • Loading branch information
Bruce Martin authored Jan 25, 2023
1 parent 1785e9c commit 357c903
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
8 changes: 5 additions & 3 deletions python-spec/src/somacore/query/_fast_csr.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,15 @@ def finalize(self) -> _CSRAccumulatorFinalResult:
nnz = sum(len(chunk[2]) for chunk in self.coo_chunks)
index_dtype = _select_dtype(nnz)
if nnz == 0:
# no way to infer matrix dtype, so use default and return empty matrix
empty = sparse.csr_matrix((0, 0))
# There is no way to infer matrix dtype, so use a default and return
# an empty matrix. Float32 is used as a default type, as it is most
# compatible with AnnData expectations.
empty = sparse.csr_matrix((0, 0), dtype=np.float32)
return _CSRAccumulatorFinalResult(
data=empty.data,
indptr=empty.indptr,
indices=empty.indices,
shape=(0, 0),
shape=self.shape,
)

# cumsum row lengths to get indptr
Expand Down
4 changes: 2 additions & 2 deletions python-spec/src/somacore/query/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -398,10 +398,10 @@ class _AxisQueryResult:
def to_anndata(self) -> anndata.AnnData:
"""Convert to AnnData"""
obs = self.obs.to_pandas()
obs.index = obs.index.map(str)
obs.index = obs.index.astype(str)

var = self.var.to_pandas()
var.index = var.index.map(str)
var.index = var.index.astype(str)

return anndata.AnnData(
X=self.X, obs=obs, var=var, layers=(self.X_layers or None)
Expand Down

0 comments on commit 357c903

Please sign in to comment.