Skip to content

CellFilter transform to eliminate cells during training#413

Open
sjfleming wants to merge 4 commits into
mainfrom
sf-scvi-remove-lowcount-cells
Open

CellFilter transform to eliminate cells during training#413
sjfleming wants to merge 4 commits into
mainfrom
sf-scvi-remove-lowcount-cells

Conversation

@sjfleming

@sjfleming sjfleming commented Jul 16, 2026

Copy link
Copy Markdown
Contributor

There is a possibility that after applying some arbitrary gene filtering, scVI would encounter cells with only a few counts... or even zero counts! The upfront cell filtering we do in Nexus looks for cells with some minimal number of UMIs or nonzero genes. But there is no guarantee this doesn't change after Filtering to just 4k or 8k genes. If we are encountering cells with extremely low UMIs during training, we should probably skip them.

OLD:
This PR introduces min_count_per_cell_threshold to allow us to skip such cells during training (on the fly).

EDIT:
This PR introduces the CellFilter Transform which can eliminate cells from a minibatch on the fly. It can be used with any model. It cannot be used with the contrastive learning model (due to shape expectations).

@sjfleming
sjfleming requested a review from fedorgrab July 16, 2026 19:15
@sjfleming

Copy link
Copy Markdown
Contributor Author

Hmmm... you know... this could alternatively be implemented more broadly than in scVI by making it a cellarium-ml Transform... like a CellFilter

@sjfleming

Copy link
Copy Markdown
Contributor Author

Actually... eliminating user cells is a potential problem here in this PR as written. If a user approaches a trained model with 10k cells, this model could return < 10k embeddings, which is not good.

@sjfleming

Copy link
Copy Markdown
Contributor Author

It might be easier / conceptually clearer at inference time to modify a Transform than to reach into the saved scVI model and start changing attributes.

@fedorgrab

Copy link
Copy Markdown
Contributor

Hmmm... you know... this could alternatively be implemented more broadly than in scVI by making it a cellarium-ml Transform... like a CellFilter

I like that idea. Filtering on the model side seems a little more disorganized. Keeping the filtering on the transform side preserves the original separation of responsibilities: transforms handle data preprocessing, while the model does what a model is supposed to do.

@sjfleming
sjfleming marked this pull request as draft July 16, 2026 19:59
@sjfleming sjfleming changed the title scvi: optionally eliminate low-count cells from minibatch on the fly CellFilter transform to eliminate cells during training Jul 16, 2026
@sjfleming

sjfleming commented Jul 16, 2026

Copy link
Copy Markdown
Contributor Author

Okay so the intention here is also to allow for different CellFilter behavior automatically at prediction time. If prediction is run via a CellariumModule.forward (which calls CellariumPipeline.predict), then a marker is set in batch which tells CellFilter (and any other method that may care eventually) that we are in predict mode. In that case, CellFilter is a no-op. The idea is that when we ask for output for a set of cells during prediction, we want them all.

Alternatively, one could reach into the transform's attributes and change the filters to zeros to get a no-op, but this is annoying (and specific to CellFilter). The different behavior on predict seems to make better sense (if it works well).

@sjfleming
sjfleming marked this pull request as ready for review July 16, 2026 20:29
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.

2 participants