Spaces:
Sleeping
Sleeping
Upload main.py with huggingface_hub
Browse files
main.py
CHANGED
|
@@ -5,16 +5,19 @@ FastAPI + DuckDB backend serving benchmark results.
|
|
| 5 |
Loads Parquet files at startup and queries them via DuckDB in-memory.
|
| 6 |
|
| 7 |
Endpoints:
|
| 8 |
-
GET /api/v1/registry
|
| 9 |
-
GET /api/v1/maxn/heatmap
|
| 10 |
-
GET /api/v1/maxn/detail
|
| 11 |
-
GET /api/v1/
|
| 12 |
-
GET /api/v1/
|
| 13 |
-
GET /api/v1/
|
| 14 |
-
GET /api/v1/h2h/
|
| 15 |
-
GET /api/v1/
|
| 16 |
-
GET /api/v1/
|
| 17 |
-
GET /api/v1/
|
|
|
|
|
|
|
|
|
|
| 18 |
"""
|
| 19 |
|
| 20 |
import json
|
|
@@ -504,6 +507,137 @@ def feature_cross_target(
|
|
| 504 |
}
|
| 505 |
|
| 506 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 507 |
@app.get("/api/v1/health")
|
| 508 |
def health():
|
| 509 |
"""Health check."""
|
|
|
|
| 5 |
Loads Parquet files at startup and queries them via DuckDB in-memory.
|
| 6 |
|
| 7 |
Endpoints:
|
| 8 |
+
GET /api/v1/registry - Full registry metadata
|
| 9 |
+
GET /api/v1/maxn/heatmap - Blood x brain heatmap data
|
| 10 |
+
GET /api/v1/maxn/detail - Per-module results for one combination
|
| 11 |
+
GET /api/v1/maxn/panel_summary - One panel across all outcomes (mean/min/max)
|
| 12 |
+
GET /api/v1/maxn/outcome_summary - One outcome across all panels (mean/min/max)
|
| 13 |
+
GET /api/v1/maxn/target_comparison - Per-panel performance for a specific target
|
| 14 |
+
GET /api/v1/h2h/blood - Blood H2H comparison
|
| 15 |
+
GET /api/v1/h2h/blood/summary - H2H win counts
|
| 16 |
+
GET /api/v1/h2h/brain - Brain H2H comparison
|
| 17 |
+
GET /api/v1/h2h/models - Model H2H comparison (future)
|
| 18 |
+
GET /api/v1/temporal - Temporal decay results
|
| 19 |
+
GET /api/v1/features - Feature importance for a target
|
| 20 |
+
GET /api/v1/features/cross - Cross-target feature importance
|
| 21 |
"""
|
| 22 |
|
| 23 |
import json
|
|
|
|
| 507 |
}
|
| 508 |
|
| 509 |
|
| 510 |
+
@app.get("/api/v1/maxn/panel_summary")
|
| 511 |
+
def maxn_panel_summary(
|
| 512 |
+
blood: str = Query(..., description="Blood platform name"),
|
| 513 |
+
metric: str = Query("r2"),
|
| 514 |
+
covariates: str = Query("none", enum=["none", "only", "included"]),
|
| 515 |
+
model: str = Query("TabPFN"),
|
| 516 |
+
):
|
| 517 |
+
"""
|
| 518 |
+
Summary of one predictor panel across all outcome panels.
|
| 519 |
+
Returns mean, min, max, n_targets, and median n_samples per outcome.
|
| 520 |
+
Replaces N calls to /maxn/detail with a single query.
|
| 521 |
+
"""
|
| 522 |
+
validate_metric(metric)
|
| 523 |
+
phase = covariate_phase("maxn", covariates)
|
| 524 |
+
|
| 525 |
+
rows = db.execute(f"""
|
| 526 |
+
SELECT brain_target,
|
| 527 |
+
AVG({metric}) as mean_val,
|
| 528 |
+
MIN({metric}) as min_val,
|
| 529 |
+
MAX({metric}) as max_val,
|
| 530 |
+
COUNT(*) as n_targets,
|
| 531 |
+
MEDIAN(n_samples) as n_samples
|
| 532 |
+
FROM results
|
| 533 |
+
WHERE phase = ? AND blood_platform = ? AND model = ?
|
| 534 |
+
GROUP BY brain_target
|
| 535 |
+
ORDER BY mean_val DESC
|
| 536 |
+
""", [phase, blood, model]).fetchall()
|
| 537 |
+
|
| 538 |
+
return {
|
| 539 |
+
"blood": blood,
|
| 540 |
+
"metric": metric,
|
| 541 |
+
"covariates": covariates,
|
| 542 |
+
"outcomes": [
|
| 543 |
+
{
|
| 544 |
+
"brain_target": r[0],
|
| 545 |
+
"mean": round(r[1], 4) if r[1] is not None else None,
|
| 546 |
+
"min": round(r[2], 4) if r[2] is not None else None,
|
| 547 |
+
"max": round(r[3], 4) if r[3] is not None else None,
|
| 548 |
+
"n_targets": r[4],
|
| 549 |
+
"n_samples": int(r[5]) if r[5] is not None else None,
|
| 550 |
+
}
|
| 551 |
+
for r in rows
|
| 552 |
+
],
|
| 553 |
+
}
|
| 554 |
+
|
| 555 |
+
|
| 556 |
+
@app.get("/api/v1/maxn/outcome_summary")
|
| 557 |
+
def maxn_outcome_summary(
|
| 558 |
+
brain: str = Query(..., description="Brain target name"),
|
| 559 |
+
metric: str = Query("r2"),
|
| 560 |
+
covariates: str = Query("none", enum=["none", "only", "included"]),
|
| 561 |
+
model: str = Query("TabPFN"),
|
| 562 |
+
):
|
| 563 |
+
"""
|
| 564 |
+
Summary of one outcome panel across all predictor panels.
|
| 565 |
+
Returns mean, min, max, n_targets, and median n_samples per predictor.
|
| 566 |
+
Replaces N calls to /maxn/detail with a single query.
|
| 567 |
+
"""
|
| 568 |
+
validate_metric(metric)
|
| 569 |
+
phase = covariate_phase("maxn", covariates)
|
| 570 |
+
|
| 571 |
+
rows = db.execute(f"""
|
| 572 |
+
SELECT blood_platform,
|
| 573 |
+
AVG({metric}) as mean_val,
|
| 574 |
+
MIN({metric}) as min_val,
|
| 575 |
+
MAX({metric}) as max_val,
|
| 576 |
+
COUNT(*) as n_targets,
|
| 577 |
+
MEDIAN(n_samples) as n_samples
|
| 578 |
+
FROM results
|
| 579 |
+
WHERE phase = ? AND brain_target = ? AND model = ?
|
| 580 |
+
GROUP BY blood_platform
|
| 581 |
+
ORDER BY mean_val DESC
|
| 582 |
+
""", [phase, brain, model]).fetchall()
|
| 583 |
+
|
| 584 |
+
return {
|
| 585 |
+
"brain": brain,
|
| 586 |
+
"metric": metric,
|
| 587 |
+
"covariates": covariates,
|
| 588 |
+
"predictors": [
|
| 589 |
+
{
|
| 590 |
+
"blood_platform": r[0],
|
| 591 |
+
"mean": round(r[1], 4) if r[1] is not None else None,
|
| 592 |
+
"min": round(r[2], 4) if r[2] is not None else None,
|
| 593 |
+
"max": round(r[3], 4) if r[3] is not None else None,
|
| 594 |
+
"n_targets": r[4],
|
| 595 |
+
"n_samples": int(r[5]) if r[5] is not None else None,
|
| 596 |
+
}
|
| 597 |
+
for r in rows
|
| 598 |
+
],
|
| 599 |
+
}
|
| 600 |
+
|
| 601 |
+
|
| 602 |
+
@app.get("/api/v1/maxn/target_comparison")
|
| 603 |
+
def maxn_target_comparison(
|
| 604 |
+
brain: str = Query(..., description="Brain target name"),
|
| 605 |
+
target: str = Query(..., description="Specific target/module name"),
|
| 606 |
+
metric: str = Query("r2"),
|
| 607 |
+
covariates: str = Query("none", enum=["none", "only", "included"]),
|
| 608 |
+
model: str = Query("TabPFN"),
|
| 609 |
+
):
|
| 610 |
+
"""
|
| 611 |
+
Per-predictor performance for a specific target within an outcome panel.
|
| 612 |
+
E.g., how each blood platform predicts module m4 of bulkrnaseq_dlpfc.
|
| 613 |
+
Replaces N calls to /maxn/detail with a single query.
|
| 614 |
+
"""
|
| 615 |
+
validate_metric(metric)
|
| 616 |
+
phase = covariate_phase("maxn", covariates)
|
| 617 |
+
|
| 618 |
+
rows = db.execute(f"""
|
| 619 |
+
SELECT blood_platform, {metric}, n_samples
|
| 620 |
+
FROM results
|
| 621 |
+
WHERE phase = ? AND brain_target = ? AND target = ? AND model = ?
|
| 622 |
+
ORDER BY {metric} DESC
|
| 623 |
+
""", [phase, brain, target, model]).fetchall()
|
| 624 |
+
|
| 625 |
+
return {
|
| 626 |
+
"brain": brain,
|
| 627 |
+
"target": target,
|
| 628 |
+
"metric": metric,
|
| 629 |
+
"covariates": covariates,
|
| 630 |
+
"predictors": [
|
| 631 |
+
{
|
| 632 |
+
"blood_platform": r[0],
|
| 633 |
+
"value": round(r[1], 4) if r[1] is not None else None,
|
| 634 |
+
"n_samples": r[2],
|
| 635 |
+
}
|
| 636 |
+
for r in rows
|
| 637 |
+
],
|
| 638 |
+
}
|
| 639 |
+
|
| 640 |
+
|
| 641 |
@app.get("/api/v1/health")
|
| 642 |
def health():
|
| 643 |
"""Health check."""
|