Skip to content

Commit 79c7b52

Browse files
committed
Fix sparse matrix output
1 parent bd16795 commit 79c7b52

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

src/squidpy/gr/_niche.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import pandas as pd
1010
import scanpy as sc
1111
from anndata import AnnData
12-
from scipy.sparse import csr_matrix, vstack
12+
from scipy.sparse import csr_matrix, hstack
1313
from scipy.stats import ranksums
1414
from sklearn import metrics
1515
from sklearn.metrics import adjusted_rand_score, fowlkes_mallows_score, normalized_mutual_info_score
@@ -139,14 +139,24 @@ def calculate_niche(
139139
raise ValueError(
140140
f"Invalid aggregation method '{aggregation}'. Please choose either 'mean' or 'variance'."
141141
)
142-
concatenated_matrix = vstack(inner_products)
142+
concatenated_matrix = hstack(inner_products)
143+
144+
# create df from sparse matrix
145+
arr = concatenated_matrix.toarray()
146+
df = pd.DataFrame(arr, index=adata.obs.index)
147+
col_names = []
148+
for A_i in adj_subsets:
149+
for var in adata.var_names:
150+
col_names.append(f"{var}_Adj_{A_i}")
151+
df.columns = col_names
152+
143153
if copy:
144154
return concatenated_matrix
145155
else:
146156
if is_sdata:
147157
sdata.tables[f"{flavor}_niche"] = ad.AnnData(concatenated_matrix)
148158
else:
149-
adata.obsm[f"{flavor}_niche"] = concatenated_matrix
159+
adata.obsm[f"{flavor}_niche"] = df
150160
else:
151161
raise ValueError(
152162
"Flavor 'cellcharter' requires list of neighbors to build adjacency matrices. Please provide a list of k_neighbors for 'adj_subsets'."
@@ -225,7 +235,6 @@ def _get_adj_matrix_subsets(connectivities: csr_matrix, distances: csr_matrix, k
225235

226236
# Create the new sparse matrix with the reduced neighbors
227237
new_adj_matrix = csr_matrix((data, (rows, cols)), shape=connectivities.shape)
228-
print(new_adj_matrix.shape)
229238
return new_adj_matrix
230239

231240

0 commit comments

Comments
 (0)