Repository navigation
Conversation
There was a problem hiding this comment.
Code Review
This pull request updates the gene mapping logic in loader.py to honor the gene column selected by inference's overlap detection, falling back to adata.var_names if not specified, and simplifies the valid gene mask derivation. It also adds a comprehensive test suite in test_emb_gene_mapping.py. Feedback suggests removing a now-redundant fallback block (if (new_mapping == -1).all():) that could cause runtime crashes, replacing the runtime assert with a proper ValueError exception, and addressing dead code and architectural inconsistencies regarding how self.datasets is cleared and validated.
| if self.gene_column is not None and self.gene_column in adata.var: | ||
| gene_names = np.asarray(adata.var[self.gene_column]) | ||
| else: | ||
| gene_names = np.array(adata.var_names) |
There was a problem hiding this comment.
With the new logic, the best gene column is selected upfront (either self.gene_column if available, or falling back to adata.var_names).
Because of this, the subsequent fallback block at lines 232-238:
if (new_mapping == -1).all():
# probably it contains ensembl id's instead
assert self.gene_column in adata.var.keys(), (
f"Column '{self.gene_column}' not found in adata.var. Available columns: {list(adata.var.keys())}"
)
gene_names = adata.var[self.gene_column].values
new_mapping = np.array([global_pos.get(g, -1) for g in gene_names])is now either completely redundant (if self.gene_column was already used and yielded all -1s) or guaranteed to raise an AssertionError (if self.gene_column is None or not in adata.var).
Please remove this if (new_mapping == -1).all(): block entirely to avoid redundant computations and potential runtime crashes.
| emb_cfg = utils.get_embedding_cfg(self.cfg) | ||
| # for inference, let's make sure this dataset's valid mask is available | ||
| if adata_name is not None: | ||
| assert adata is not None |
There was a problem hiding this comment.
Using assert for runtime validation of arguments (such as assert adata is not None) is discouraged because assertions can be globally disabled in Python when run with optimization flags (e.g., python -O). If adata is required, it should be validated using an explicit if adata is None: raise ValueError(...) check.
Additionally, there is a larger architectural issue here: self.datasets and self.shapes_dict are cleared unconditionally at the beginning of FilteredGenesCounts.__init__ (lines 205-206). This breaks the datasets and shape_dict support of create_dataloader (which is explicitly allowed by its signature and validation checks). Furthermore, because self.datasets is only populated when adata_name is not None (which asserts adata is not None), adata is guaranteed to never be None for any dataset in self.datasets. This makes the if adata is None: check at line 250 (now line 255) completely dead code.
If FilteredGenesCounts is indeed only meant to be used with adata, we should clean up the dead code and remove the unused datasets parameter and the dead if adata is None: branch. Otherwise, we should fix the unconditional clearing of self.datasets and self.shapes_dict so that they are preserved when adata is not provided.
| assert adata is not None | |
| if adata is None: | |
| raise ValueError("adata must be provided when adata_name is specified") |
|
Let me know if you have any thoughts on this one. |
There's a bug in
FilteredGenesCounts.__init__: when loading picks a gene-name column (becausevar_namesare Ensembl IDs), the gene mapping still usesvar_names. So genes that are only named in that column are dropped.With index
[TP53, ENSG2, ENSG3, ENSG4]andgene_name[TP53, ACTB, GAPDH, MALAT1], the mapping is[0, -1, -1, -1]: three valid genes are dropped from the embedding input. It should be[0, 1, 2, 3].This builds the mapping and the validity mask from the selected column, and falls back to
var_nameswhen there's no selected column.Test:
test_encode_adata_uses_detected_gene_namesintests/test_emb_gene_mapping.py(5 cases) fails on main and passes here.pytest tests: 42 passed (37 on main). Ruff and pyright report no new diagnostics;uv buildpasses.🤖 Generated with Claude Code
https://claude.ai/code/session_01LKxc9GKLwnyeV2jaZBxH9N