-
Notifications
You must be signed in to change notification settings - Fork 49
Allow ceiling-only evaluation without a prediction #245
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -71,8 +71,10 @@ class MetricsEvaluator: | |
| Arguments | ||
| ========= | ||
|
|
||
| adata_pred: ad.AnnData | str | ||
| Predicted anndata object or path to anndata object. | ||
| adata_pred: ad.AnnData | str | None | ||
| Predicted anndata object or path to anndata object. May be ``None`` to run | ||
| in ceiling-only mode (the data ceiling is estimated from the real data | ||
| alone); in that case only :meth:`compute_ceiling` is available. | ||
| adata_real: ad.AnnData | str | ||
| Real anndata object or path to anndata object. | ||
| de_pred: pl.DataFrame | str | None = None | ||
|
|
@@ -100,7 +102,7 @@ class MetricsEvaluator: | |
|
|
||
| def __init__( | ||
| self, | ||
| adata_pred: ad.AnnData | str, | ||
| adata_pred: ad.AnnData | str | None, | ||
| adata_real: ad.AnnData | str, | ||
| de_pred: pl.DataFrame | str | None = None, | ||
| de_real: pl.DataFrame | str | None = None, | ||
|
|
@@ -132,6 +134,25 @@ def __init__( | |
| self._skip_de = skip_de | ||
| self._pdex_kwargs = pdex_kwargs or {} | ||
|
|
||
| # Ceiling-only mode: the data ceiling is estimated from the real data | ||
| # alone, so no prediction is required. When adata_pred is None we skip | ||
| # building the main de_comparison (compute() is unavailable) - only | ||
| # compute_ceiling() may be called. | ||
| self.ceiling_only = adata_pred is None | ||
|
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. When self.ceiling_only = adata_pred is None
if self.ceiling_only and (de_pred is not None or de_real is not None):
raise ValueError(
"de_pred and de_real cannot be provided in ceiling-only mode (adata_pred=None)."
) |
||
|
|
||
| # Precomputed DE cannot be reused in ceiling-only mode: the main comparison | ||
| # is skipped, and the ceiling computes DE on its own disjoint halves. Warn | ||
| # (rather than fail) so a stray argument does not break an otherwise valid | ||
| # run, but the user is not left believing their table was used. | ||
| if self.ceiling_only: | ||
| for name, value in (("de_pred", de_pred), ("de_real", de_real)): | ||
| if value is not None: | ||
| logger.warning( | ||
| f"{name} is ignored in ceiling-only mode (adata_pred=None): " | ||
| f"the ceiling computes differential expression on its own " | ||
| f"disjoint halves of the real data." | ||
| ) | ||
|
|
||
| self.anndata_pair = _build_anndata_pair( | ||
| real=adata_real, | ||
| pred=adata_pred, | ||
|
|
@@ -140,7 +161,7 @@ def __init__( | |
| allow_discrete=allow_discrete, | ||
| ) | ||
|
|
||
| if skip_de: | ||
| if skip_de or self.ceiling_only: | ||
| self.de_comparison = None | ||
| else: | ||
| self.de_comparison = _build_de_comparison( | ||
|
|
@@ -166,6 +187,11 @@ def compute( | |
| write_csv: bool = True, | ||
| break_on_error: bool = False, | ||
| ) -> tuple[pl.DataFrame, pl.DataFrame]: | ||
| if self.ceiling_only: | ||
| raise ValueError( | ||
| "compute() requires a prediction (adata_pred). This evaluator was " | ||
| "created without one (ceiling-only mode); call compute_ceiling() instead." | ||
| ) | ||
| pipeline = MetricPipeline( | ||
| profile=profile, | ||
| metric_configs=metric_configs, | ||
|
|
@@ -399,7 +425,7 @@ def _spearman_brown_correct(results: pl.DataFrame) -> pl.DataFrame: | |
|
|
||
| def _build_anndata_pair( | ||
| real: ad.AnnData | str, | ||
| pred: ad.AnnData | str, | ||
| pred: ad.AnnData | str | None, | ||
| control_pert: str, | ||
| pert_col: str, | ||
| allow_discrete: bool = False, | ||
|
|
@@ -413,11 +439,24 @@ def _build_anndata_pair( | |
|
|
||
| # Cast float16 to float32 since NUMBA (used by pdex) does not support float16 | ||
| _cast_float16_to_float32(real, which="real") | ||
| _cast_float16_to_float32(pred, which="pred") | ||
|
|
||
| # Validate that the input is normalized and log-transformed | ||
| _convert_to_normlog(real, which="real", allow_discrete=allow_discrete) | ||
| _convert_to_normlog(pred, which="pred", allow_discrete=allow_discrete) | ||
|
|
||
| # Ceiling-only mode: no prediction supplied. The data ceiling reads only | ||
| # `.real`, so mirror real into pred to satisfy the pair (it is never scored). | ||
| # | ||
| # INVARIANT: this aliases the SAME object - `pair.real is pair.pred`. It is not | ||
| # a copy, because copying a matrix that is never scored would double peak memory | ||
| # for nothing. Safe only because ceiling-only mode blocks `compute()` and | ||
| # `compute_ceiling()` derives fresh copies of both halves from `.real`. Anything | ||
| # that mutates `.pred` in place would therefore corrupt `.real`: take a copy | ||
| # first, or gate the write on `adata_pred is not None`. | ||
| if pred is None: | ||
| pred = real | ||
| else: | ||
| _cast_float16_to_float32(pred, which="pred") | ||
| _convert_to_normlog(pred, which="pred", allow_discrete=allow_discrete) | ||
|
|
||
| # Build the anndata pair | ||
| return 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.
In ceiling-only mode, any precomputed DE results passed via
--de-predor--de-realwill be silently ignored because the main evaluation is skipped and the ceiling estimation computes its own DE on the split halves. To prevent user confusion and ensure correctness, we should raise aValueErrorif these arguments are provided in ceiling-only mode.