-
Notifications
You must be signed in to change notification settings - Fork 48
[draft][do not merge] Add top-k accuracy metric for inverse evaluation #215
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
fctb12
wants to merge
9
commits into
main
Choose a base branch
from
francis/inverse-top-k
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
52fa4ff
Add top-k accuracy metric and inverse evaluation
fctb12 5fb113a
reset src/cell_eval/utils.py
fctb12 0d8fe48
reset src/cell_eval/metrics/_de.py
fctb12 3e42603
reset src/cell_eval/_types/_de.py and src/cell_eval/_pipeline/_runner.py
fctb12 be1b978
reset /src/cell_eval/_cli/_prep.py and src/cell_eval/_pipeline/_runne…
fctb12 dfce6ae
reset src/cell_eval/metrics/_anndata.py
fctb12 2ad897a
reset src/cell_eval/metrics/_anndata.py
fctb12 2a2434e
reset src/cell_eval/_evaluator.py
fctb12 8354ed4
clean up src/cell_eval/_cli/_run.py
fctb12 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -197,6 +197,64 @@ def discrimination_score( | |
|
|
||
| return norm_ranks | ||
|
|
||
| def top_k_accuracy( | ||
| data, | ||
| k: int = 10, | ||
| metric: str = "l2", | ||
| embed_key: str | None = None, | ||
| ) -> dict[str, float]: | ||
| """ | ||
| Top-k accuracy over pseudo-bulked perturbation profiles. | ||
| For each perturbation, we compute one vector for real and one for predicted | ||
| (pseudobulk/mean per perturbation). We then compare each predicted | ||
| perturbation vector against all real perturbation vectors and mark a hit if | ||
| the correct real perturbation is within the top-k closest. | ||
| Args: | ||
| data: PerturbationAnndataPair | ||
| k: number of nearest neighbors to consider per perturbation | ||
| metric: one of {"l2", "euclidean", "cosine"} | ||
| embed_key: optional key for .obsm | ||
| """ | ||
|
|
||
| if k <= 0: | ||
| raise ValueError("Parameter `k` must be positive.") | ||
|
|
||
| metric = metric.lower() | ||
| if metric in {"l2", "euclidean"}: | ||
| dist_metric = "euclidean" | ||
| elif metric == "cosine": | ||
| dist_metric = "cosine" | ||
| else: | ||
| raise ValueError(f"Unsupported metric: {metric}") | ||
|
|
||
| # Build one vector per perturbation (exclude control) in a consistent order | ||
| real_vectors: list[np.ndarray] = [] | ||
| pred_vectors: list[np.ndarray] = [] | ||
| perts_order: list[str] = [] | ||
| for bulk in data.iter_bulk_arrays(embed_key=embed_key): | ||
| perts_order.append(bulk.key) | ||
| real_vectors.append(bulk.pert_real) | ||
| pred_vectors.append(bulk.pert_pred) | ||
|
|
||
| if not real_vectors: | ||
| return {} | ||
|
|
||
| real_mat = np.vstack(real_vectors) | ||
| pred_mat = np.vstack(pred_vectors) | ||
|
|
||
| # Compute distance matrix between predicted and real pseudo-bulks | ||
| D = skm.pairwise_distances(pred_mat, real_mat, metric=dist_metric) | ||
|
|
||
| n_real = D.shape[1] | ||
| k_eff = int(min(max(1, k), n_real)) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
|
||
| scores: dict[str, float] = {} | ||
| for i, pert in enumerate(perts_order): | ||
| # indices of k smallest distances | ||
| idx = np.argpartition(D[i], k_eff - 1)[:k_eff] | ||
| scores[str(pert)] = 1.0 if i in idx else 0.0 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
|
||
| return scores | ||
|
|
||
| def _generic_evaluation( | ||
| data: PerturbationAnndataPair, | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
dataparameter is missing a type hint. For consistency with other metric functions in this file and for better code clarity, please add the type hintPerturbationAnndataPair.