Skip to content

FilteredGenesCounts: use the detected gene-name column for the mapping - #283

Open
rpinho wants to merge 1 commit into
ArcInstitute:mainfrom
rpinho:fix-gene-name-column
Open

rpinho wants to merge 1 commit into
ArcInstitute:mainfrom
rpinho:fix-gene-name-column

Conversation

@rpinho

@rpinho rpinho commented Nov 16, 2026

Copy link
Copy Markdown

There's a bug in FilteredGenesCounts.__init__: when loading picks a gene-name column (because var_names are Ensembl IDs), the gene mapping still uses var_names. So genes that are only named in that column are dropped.

With index [TP53, ENSG2, ENSG3, ENSG4] and gene_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_names when there's no selected column.

Test: test_encode_adata_uses_detected_gene_names in tests/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 build passes.

🤖 Generated with Claude Code

https://claude.ai/code/session_01LKxc9GKLwnyeV2jaZBxH9N

@rpinho
rpinho requested a review from a team as a code owner November 16, 2026 17:30

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +225 to +228
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)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

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

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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.

Suggested change
assert adata is not None
if adata is None:
raise ValueError("adata must be provided when adata_name is specified")

@rpinho

rpinho commented Nov 24, 2026

Copy link
Copy Markdown
Author

Let me know if you have any thoughts on this one.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant