Skip to content

Commit 5219a30

Browse files
committed
Fix neighborhood profile calculation
1 parent 79c7b52 commit 5219a30

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

src/squidpy/gr/_niche.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ def calculate_niche(
2727
flavor: str = "neighborhood",
2828
library_key: str | None = None,
2929
table_key: str | None = None,
30-
spatial_key: str = "spatial",
3130
adj_subsets: list[int] | None = None,
3231
aggregation: str = "mean",
32+
spatial_key: str = "spatial",
3333
spatial_connectivities_key: str = "spatial_connectivities",
3434
spatial_distances_key: str = "spatial_distances",
3535
copy: bool = False,
@@ -174,11 +174,14 @@ def _calculate_neighborhood_profile(
174174
neighbor_matrix = pd.DataFrame(nonzero_indices)
175175

176176
# get unique categories
177-
category_arr = adata.obs[groups].values
178-
unique_categories = np.unique(category_arr)
177+
unique_categories = np.unique(adata.obs[groups].values)
179178

180179
# get obs x k matrix where each column is the category of the k-th neighbor
181-
cat_by_id = np.take(category_arr, neighbor_matrix)
180+
indices_with_nan = neighbor_matrix.to_numpy()
181+
valid_indices = neighbor_matrix.fillna(-1).astype(int).to_numpy()
182+
cat_by_id = adata.obs[groups].values[valid_indices]
183+
cat_by_id[indices_with_nan == -1] = np.nan
184+
# cat_by_id = np.take(category_arr, neighbor_matrix)
182185

183186
# in obs x k matrix convert categorical values to numerical values
184187
cat_indices = {category: index for index, category in enumerate(unique_categories)}

0 commit comments

Comments
 (0)