Skip to main content

Margin-aware robustness metrics for pathology foundation models.

Project description

croma

PyPI version Code style: Black License

croma is a Python package for evaluating pathology foundation model robustness to non-biological confounders.

It implements three complementary metrics:

  • RI: Robustness Index
  • MaRI: Margin-aware Robustness Index
  • CCMR: Cross-Confounder Margin Ratio

croma provides a clean implementation of RI together with MaRI -- its margin-aware extension -- as well as a new robustness metric, CCMR -- which overcomes some of the limitations of RI/MaRI and enables tail-aware analysis for more detailed robustness characterization. RI was originally introduced in the PathoROB study.

The package also comes with optional benchmarking utilities for multi-model evaluation.

Install

Core package:

pip install cross-margin

With benchmark utilities:

pip install "cross-margin[bench]"

Python Quickstart

import numpy as np
import pandas as pd
from croma import CCMR, MaRI, RI

manifest = pd.read_csv("manifest.csv")
features = np.load("embeddings.npy")

ri = RI.compute(
    features,
    manifest,
    confounder_column="confounder",
    evaluation_design="paired_2x2",
    k_candidates=[5, 11, 21],
)

mari = MaRI.compute(
    features,
    manifest,
    confounder_column="confounder",
    evaluation_design="paired_2x2",
    k_candidates=[5, 11, 21],
    tau=0.2,
)

ccmr = CCMR.compute(
    features,
    manifest,
    confounder_column="confounder",
    evaluation_design="paired_2x2",
    m=1,
)

CLI Quickstart

Compute RI:

croma ri \
  --manifest /path/to/manifest.csv \
  --embeddings /path/to/embeddings.npy \
  --confounder-column confounder \
  --evaluation-design paired_2x2 \
  --k-candidates 5,11,21

Compute MaRI:

croma mari \
  --manifest /path/to/manifest.csv \
  --embeddings /path/to/embeddings.npy \
  --confounder-column confounder \
  --evaluation-design paired_2x2 \
  --k-candidates 5,11,21 \
  --tau 0.2

Compute CCMR:

croma ccmr \
  --manifest /path/to/manifest.csv \
  --embeddings /path/to/embeddings.npy \
  --confounder-column confounder \
  --evaluation-design paired_2x2 \
  --m 1

Benchmarking

cross-margin also includes an optional benchmarking pipeline for comparing multiple foundation models on the same manifest. The benchmark handles embedding extraction, metric computation, cached re-runs, and summary artifact generation in a single workflow.

Run it with:

python scripts/benchmark.py \
  --manifest /path/to/manifest.csv \
  --confounder-column confounder \
  --output-dir /path/to/benchmark

For benchmark options, outputs, caching behavior, and downstream analysis, see docs/benchmarking.md.

Manifest Contract

Required columns:

  • sample_id
  • image_path
  • label
  • slide_id
  • the user-selected confounder column passed as confounder_column= or --confounder-column

Default contract:

  • row i in embeddings.npy should match row i in the manifest

Optional:

  • subset: required for evaluation_design="paired_2x2"

Evaluation Designs

  • paired_2x2: uses explicit manifest-defined subsets and reports occurrence-level outputs
  • dataset_wide: evaluates the retained dataset once and reports sample-level outputs

paired_2x2 is strict by design: the manifest must define valid 2x2 (label x confounder) subsets via the subset column.

Documentation

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cross_margin-1.0.0.tar.gz (97.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cross_margin-1.0.0-py3-none-any.whl (32.4 kB view details)

Uploaded Python 3

File details

Details for the file cross_margin-1.0.0.tar.gz.

File metadata

  • Download URL: cross_margin-1.0.0.tar.gz
  • Upload date:
  • Size: 97.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.14

File hashes

Hashes for cross_margin-1.0.0.tar.gz
Algorithm Hash digest
SHA256 de35d9bc75a063075757f592adfc37bb902b7b6f8e600b07ef8a883c68c82eff
MD5 0117f2862e83a5d72bece26ec9bdef35
BLAKE2b-256 7a57820411e7513db79a347b5cc62a726164d0523d0c3c5691bf539fce5050f9

See more details on using hashes here.

File details

Details for the file cross_margin-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: cross_margin-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 32.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.14

File hashes

Hashes for cross_margin-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 418b42db2d2988fb572da845c6eb43bb0a96c9f9201211c7ec270f1d818e4c95
MD5 6e03e5a4183f100af6d02333bdaca19a
BLAKE2b-256 0456acf03bd8fd206a9ece294573cc22a16c6466846c72437da65b0ec61cd143

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page