Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions spateo/preprocessing/aggregate.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,20 @@ def bin_adata(
adata = adata.copy()
adata.obsm[coords_key] = (adata.obsm[coords_key] // bin_size).astype(np.int32)

if scipy.issparse(adata.X):
if scipy.sparse.issparse(adata.X):
df = pd.DataFrame(adata.X.toarray(), columns=adata.var_names)
Comment on lines -35 to 36
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you please confirm the version requirement for scipy? does it match up with the one stated in the requirements.txt file?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have confirmed that the requirements are met. scipy.sparse.issparse is supported in v1.0.0(https://github.com/scipy/scipy/blob/11509c4a98edded6c59423ac44ca1b7f28fba1fd/scipy/sparse/base.py#L1231)

else:
df = pd.DataFrame(adata.X, columns=adata.var_names)

df[["x", "y"]] = adata.obsm[coords_key]
df2 = df.groupby(by=["x", "y"]).sum()
_spatial = np.array(df2.index.to_list())
_idx = [str(i[0]) + "_" + str(i[1]) for i in df2.index.to_list()]
df2.index = _idx

Comment on lines +42 to 45
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note that f2.index.to_list() may be a tuple (for x/y dimensions). what you have assumes the index is just a numeric value

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The index is come from the spatial information so it's the numeric value

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, it should not be a couple of x, y coordinates?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I misunderstood. It is a list composed of tuples (x, y), but the code here can convert it into an array with shape (n, 2).

adata_binned = AnnData(df2)
adata_binned.uns["__type"] = "UMI"
adata_binned.obs_names = [str(i[0]) + "_" + str(i[1]) for i in df2.index.to_list()]
adata_binned.obsm[coords_key] = np.array([list(i) for i in df2.index.to_list()], dtype=np.float64)
adata_binned.obs_names = _idx
adata_binned.obsm[coords_key] = _spatial

return adata_binned
Loading