Skip to content

Commit b8f5aa3

Browse files
Enhance cell index retrieval in get_cell_indices_by_name function
- Improved handling of cell indices in the get_cell_indices_by_name function to accommodate cases where cell names may match multiple entries or be represented as slices or arrays. - Updated test cases to utilize variables for initial and target cell names, enhancing code readability and maintainability.
1 parent a80fd23 commit b8f5aa3

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

dynamo/prediction/least_action_path.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1751,8 +1751,20 @@ def get_cell_indices_by_name(adata, cell_names):
17511751
indices = []
17521752
for name in cell_names:
17531753
if name in adata.obs_names:
1754+
# get_loc with string is safe
17541755
idx = adata.obs_names.get_loc(name)
1755-
indices.append(idx)
1756+
if isinstance(idx, slice):
1757+
# if name matches multiple cells (rare but possible with non-unique index)
1758+
# we'll take the first one to match original logic which appended single idx
1759+
start = idx.start if idx.start is not None else 0
1760+
indices.append(start)
1761+
elif isinstance(idx, np.ndarray):
1762+
# mask
1763+
where = np.where(idx)[0]
1764+
if len(where) > 0:
1765+
indices.append(where[0])
1766+
else:
1767+
indices.append(idx)
17561768
else:
17571769
print(f"Warning: Cell name '{name}' not found in adata.obs_names")
17581770

tests/test_pl.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,10 +185,12 @@ def test_lap_plots():
185185

186186
HSC_cells_indices = nearest_neighbors(fixed_points[0], adata.obsm["X_umap"])
187187
Meg_cells_indices = nearest_neighbors(fixed_points[1], adata.obsm["X_umap"])
188+
init_cells = [adata.obs_names[HSC_cells_indices[0]][0]]
189+
target_cells = [adata.obs_names[Meg_cells_indices[0]][0]]
188190
dyn.pd.least_action(
189191
adata,
190-
[adata.obs_names[HSC_cells_indices[0]][0]],
191-
[adata.obs_names[Meg_cells_indices[0]][0]],
192+
init_cells,
193+
target_cells,
192194
basis="umap",
193195
adj_key="X_umap_distances",
194196
min_lap_t=True,

0 commit comments

Comments
 (0)