|
9 | 9 | import pandas as pd
|
10 | 10 | import scanpy as sc
|
11 | 11 | from anndata import AnnData
|
12 |
| -from scipy.sparse import csr_matrix, vstack |
| 12 | +from scipy.sparse import csr_matrix, hstack |
13 | 13 | from scipy.stats import ranksums
|
14 | 14 | from sklearn import metrics
|
15 | 15 | from sklearn.metrics import adjusted_rand_score, fowlkes_mallows_score, normalized_mutual_info_score
|
@@ -139,14 +139,24 @@ def calculate_niche(
|
139 | 139 | raise ValueError(
|
140 | 140 | f"Invalid aggregation method '{aggregation}'. Please choose either 'mean' or 'variance'."
|
141 | 141 | )
|
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 | + |
143 | 153 | if copy:
|
144 | 154 | return concatenated_matrix
|
145 | 155 | else:
|
146 | 156 | if is_sdata:
|
147 | 157 | sdata.tables[f"{flavor}_niche"] = ad.AnnData(concatenated_matrix)
|
148 | 158 | else:
|
149 |
| - adata.obsm[f"{flavor}_niche"] = concatenated_matrix |
| 159 | + adata.obsm[f"{flavor}_niche"] = df |
150 | 160 | else:
|
151 | 161 | raise ValueError(
|
152 | 162 | "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
|
225 | 235 |
|
226 | 236 | # Create the new sparse matrix with the reduced neighbors
|
227 | 237 | new_adj_matrix = csr_matrix((data, (rows, cols)), shape=connectivities.shape)
|
228 |
| - print(new_adj_matrix.shape) |
229 | 238 | return new_adj_matrix
|
230 | 239 |
|
231 | 240 |
|
|
0 commit comments