Repository navigation
Add fixed Gradient XGBoost label-projection ensemble #39
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
Open
edmundmiller
wants to merge
1
commit into
openproblems-bio:main
Choose a base branch
from
edmundmiller:codex/gradient-label-projection-ensemble
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.
Open
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| # Gradient XGBoost ensemble | ||
|
|
||
| This adapts ten frozen parameter recipes recovered from the Gradient kidney | ||
| label-projection campaign: top 3,000 supplied HVGs plus AQP3, normalized sparse | ||
| expression, and majority voting. Only training-cell labels are used at inference | ||
| fit time. It accepts the OpenProblems training/test API and emits predictions as | ||
| AnnData. AQP3 is resolved through feature names as well as the gene index. | ||
|
|
||
| The recipes were selected after repeated feedback on the original test set | ||
| (263 attempts); they were subsequently reused without retuning in donor-fold | ||
| validation. The exact original ensemble membership is a source-supported | ||
| reconstruction and its original tie rule is unverified. This implementation | ||
| uses lexicographically smallest labels on tied votes, as in the frozen donor | ||
| validation. Dependency versions match the recovered producer Dockerfile. | ||
| Supplied cohort-wide HVG scores and normalization are reused; this component | ||
| does not fit preprocessing separately within folds. | ||
|
|
||
| This contribution does not claim that the campaign score is an independently | ||
| validated OpenProblems leaderboard result. OpenProblems must evaluate this | ||
| adaptation on its own datasets. | ||
|
|
||
| ```sh | ||
| viash test src/methods/gradient_xgboost_ensemble/config.vsh.yaml | ||
| ``` |
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 |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| __merge__: /src/api/comp_method.yaml | ||
| name: gradient_xgboost_ensemble | ||
| label: Gradient XGBoost ensemble | ||
| summary: Majority vote of ten fixed XGBoost classifiers on highly variable genes. | ||
| description: | | ||
| Train ten fixed XGBoost classifiers on normalized expression of the top | ||
| 3,000 highly variable genes, adding AQP3 when present. Predict by majority | ||
| vote, breaking ties by lexicographic label order. Parameters were selected | ||
| during a kidney label-projection campaign with repeated test feedback; | ||
| this contribution requires independent evaluation on OpenProblems datasets. | ||
| The tie rule is from a subsequent frozen donor validation, because the | ||
| original campaign's tie rule could not be verified. This is an adaptation | ||
| rather than an exact replay of the original score. | ||
| references: | ||
| doi: '10.1145/2939672.2939785' | ||
| links: | ||
| repository: https://github.com/dmlc/xgboost | ||
| documentation: https://xgboost.readthedocs.io/en/stable/index.html | ||
| info: | ||
| preferred_normalization: log_cp10k | ||
| authors: | ||
| - name: Edmund Miller | ||
| roles: [author] | ||
| info: | ||
| github: edmundmiller | ||
| resources: | ||
| - type: python_script | ||
| path: script.py | ||
| - path: recipes.json | ||
| engines: | ||
| - type: docker | ||
| image: openproblems/base_python:1 | ||
| setup: | ||
| - type: python | ||
| packages: [xgboost==3.4.1, scikit-learn==1.9.1, anndata==0.13.4, numpy==2.5.3, scipy==1.18.1, pandas==3.0.6] | ||
| runners: | ||
| - type: executable | ||
| - type: nextflow | ||
| directives: | ||
| label: [midtime, highmem, midcpu] | ||
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 |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| [ | ||
| { | ||
| "n_hvg": 3000, | ||
| "n_estimators": 850, | ||
| "learning_rate": 0.05572301764105427, | ||
| "max_depth": 10, | ||
| "subsample": 0.8, | ||
| "colsample_bytree": 0.18, | ||
| "seed": 17 | ||
| }, | ||
| { | ||
| "n_hvg": 3000, | ||
| "n_estimators": 850, | ||
| "learning_rate": 0.0523475679682032, | ||
| "max_depth": 10, | ||
| "subsample": 0.8, | ||
| "colsample_bytree": 0.18, | ||
| "seed": 17 | ||
| }, | ||
| { | ||
| "n_hvg": 3000, | ||
| "n_estimators": 851, | ||
| "learning_rate": 0.08057190998396768, | ||
| "max_depth": 10, | ||
| "subsample": 0.8, | ||
| "colsample_bytree": 0.18, | ||
| "seed": 17 | ||
| }, | ||
| { | ||
| "n_hvg": 3000, | ||
| "n_estimators": 610, | ||
| "learning_rate": 0.0914207870888236, | ||
| "max_depth": 10, | ||
| "subsample": 0.8, | ||
| "colsample_bytree": 0.18, | ||
| "seed": 17 | ||
| }, | ||
| { | ||
| "n_hvg": 3000, | ||
| "n_estimators": 745, | ||
| "learning_rate": 0.062154911560308904, | ||
| "max_depth": 10, | ||
| "subsample": 0.8, | ||
| "colsample_bytree": 0.18, | ||
| "seed": 17 | ||
| }, | ||
| { | ||
| "n_hvg": 3000, | ||
| "n_estimators": 1100, | ||
| "learning_rate": 0.045, | ||
| "max_depth": 10, | ||
| "subsample": 0.8, | ||
| "colsample_bytree": 0.18, | ||
| "seed": 17 | ||
| }, | ||
| { | ||
| "n_hvg": 3000, | ||
| "n_estimators": 950, | ||
| "learning_rate": 0.061, | ||
| "max_depth": 10, | ||
| "subsample": 0.8, | ||
| "colsample_bytree": 0.18, | ||
| "seed": 17 | ||
| }, | ||
| { | ||
| "n_hvg": 3000, | ||
| "n_estimators": 1033, | ||
| "learning_rate": 0.06218229787146111, | ||
| "max_depth": 10, | ||
| "subsample": 0.8, | ||
| "colsample_bytree": 0.36045527470080035, | ||
| "seed": 17 | ||
| }, | ||
| { | ||
| "n_hvg": 3000, | ||
| "n_estimators": 950, | ||
| "learning_rate": 0.061, | ||
| "max_depth": 10, | ||
| "subsample": 0.8, | ||
| "colsample_bytree": 0.25, | ||
| "seed": 17 | ||
| }, | ||
| { | ||
| "n_hvg": 3000, | ||
| "n_estimators": 950, | ||
| "learning_rate": 0.061, | ||
| "max_depth": 10, | ||
| "subsample": 0.8, | ||
| "colsample_bytree": 0.2, | ||
| "seed": 17 | ||
| } | ||
| ] |
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 |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| import json | ||
| from pathlib import Path | ||
|
|
||
| import anndata as ad | ||
| import numpy as np | ||
| import pandas as pd | ||
| from scipy import sparse | ||
| from sklearn.preprocessing import LabelEncoder | ||
| from xgboost import XGBClassifier | ||
|
|
||
| ## VIASH START | ||
| par = { | ||
| "input_train": "resources_test/task_label_projection/cxg_immune_cell_atlas/train.h5ad", | ||
| "input_test": "resources_test/task_label_projection/cxg_immune_cell_atlas/test.h5ad", | ||
| "output": "output.h5ad", | ||
| } | ||
| meta = {"name": "gradient_xgboost_ensemble", "resources_dir": "."} | ||
| ## VIASH END | ||
|
|
||
| train = ad.read_h5ad(par["input_train"]) | ||
| test = ad.read_h5ad(par["input_test"]) | ||
| if not train.var_names.equals(test.var_names): | ||
| raise ValueError("Training and test genes must have the same order") | ||
| if not train.obs_names.is_unique or not test.obs_names.is_unique: | ||
| raise ValueError("Cell identifiers must be unique") | ||
| if "label" not in train.obs or train.obs["label"].isna().any(): | ||
| raise ValueError("Every training cell needs a label") | ||
| if not np.isfinite(train.var["hvg_score"]).all(): | ||
| raise ValueError("HVG scores must be finite") | ||
|
|
||
| recipes = json.loads((Path(meta["resources_dir"]) / "recipes.json").read_text()) | ||
| labels = LabelEncoder() | ||
| y_train = labels.fit_transform(train.obs["label"].astype(str)) | ||
| votes = np.zeros((test.n_obs, len(labels.classes_)), dtype=np.uint8) | ||
| threads = meta.get("cpus") or 1 | ||
| for recipe in recipes: | ||
| mask = np.zeros(train.n_vars, dtype=bool) | ||
| mask[np.argsort(train.var["hvg_score"].to_numpy())[-recipe["n_hvg"]:]] = True | ||
| names = train.var.get("feature_name", pd.Series(train.var_names, index=train.var_names)) | ||
| mask |= (names == "AQP3").to_numpy() | (train.var_names == "AQP3") | ||
| if not mask.any(): | ||
| raise ValueError("No expression features available") | ||
| x_train = train.layers["normalized"][:, mask].astype(np.float32) | ||
| x_test = test.layers["normalized"][:, mask].astype(np.float32) | ||
| if sparse.issparse(x_train): | ||
| x_train, x_test = sparse.csr_matrix(x_train), sparse.csr_matrix(x_test) | ||
| if len(labels.classes_) == 1: | ||
| predicted = np.zeros(test.n_obs, dtype=int) | ||
| else: | ||
| params = {k: v for k, v in recipe.items() if k not in ("n_hvg", "seed")} | ||
| model = XGBClassifier(**params, random_state=recipe["seed"], | ||
| tree_method="hist", n_jobs=threads, eval_metric="mlogloss") | ||
| model.fit(x_train, y_train) | ||
| predicted = model.predict(x_test).astype(int) | ||
| votes[np.arange(test.n_obs), predicted] += 1 | ||
|
|
||
| # LabelEncoder sorts labels, so argmax resolves tied votes lexicographically. | ||
| output = ad.AnnData( | ||
| obs=pd.DataFrame({"label_pred": labels.inverse_transform(votes.argmax(axis=1))}, | ||
| index=test.obs_names), | ||
| uns={"method_id": meta["name"], "dataset_id": test.uns["dataset_id"], | ||
| "normalization_id": test.uns["normalization_id"]}, | ||
| ) | ||
| output.write_h5ad(par["output"], compression="gzip") |
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.
When using the repository's
run_benchmarkworkflow, this component is never available:src/workflows/run_benchmark/config.vsh.yaml:97-120explicitly enumerates every method dependency, andsrc/workflows/run_benchmark/main.nf:9-43separately enumerates the methods passed torunEach, but neither list includes this name. Consequently default benchmarks, and even a run with--methods_include gradient_xgboost_ensemble, cannot invoke the new method, preventing the independent evaluation described here; add it to both lists.Useful? React with 👍 / 👎.