Skip to main content

soma

soma is a modular framework to streamline computational pathology research.

The soma pipeline — data, a frozen encoder, a trained decoder, and evaluation.

📖 Documentation · 📦 PyPI

It provides a unified API to go from a dataset of slides and labels to a full, reproducible result report. Along the way, it makes it easy to sweep core design choices such as preprocessing (spacing, field-of-view), encoding (foundation models), and aggregation (MIL) so you can quickly find the strongest configuration for your data.

You can use it either as a full end-to-end pipeline or as a set of composable building blocks for custom experiment orchestration.

Install

pip install soma-pathology

The PyPI distribution is soma-pathology; the import package and CLI remain soma.

API Overview

The package root exports the main entry points:

  • Dataset and Splits for loading data
  • FeatureExtractor for preprocessing slides and extracting embeddings
  • train() and train_one_fold() for training directly from features
  • Pipeline for the full preprocessing + feature extraction + training workflow

Quick Start

1. Prepare dataset and splits

dataset.csv should contain one row per slide with at least sample_id, image_path, and label. sample_id must be unique, image_path should point to the slide file, and label can be either a string class name or an integer target.

splits.csv should assign each sample_id to train, tune, or a test* split for every fold. Each fold must contain at least one test split. This is what keeps evaluation reproducible and prevents leakage.

from soma import Dataset, Splits

dataset = Dataset("dataset.csv")
splits = Splits("splits.csv", dataset)

print(len(dataset.sample_ids))
print(sorted({s.label for s in dataset.samples.values()}))
print(splits.num_folds)

2. Extract once, cache, and reuse features across experiments

FeatureExtractor handles preprocessing and embedding extraction. The cache lets you reuse the same extracted features across multiple training runs, which is especially useful when comparing several MIL aggregators or heads against the same encoder output.

from soma import Dataset, Splits, FeatureExtractor, train
from soma import CacheConfig, EncoderConfig, AggregatorConfig, TaskConfig, TrainingConfig

# Extract features once

dataset = Dataset("dataset.csv")
extractor = FeatureExtractor(
    dataset=dataset,
    encoder=EncoderConfig(name="uni2"),
    output_root="output",
    cache=CacheConfig(enabled=True, root_dir="shared/feature_cache"),
)

features = extractor.extract()

# Train multiple model variants on the same features

splits = Splits("splits.csv", dataset)
task = TaskConfig(name="binary_classification")

abmil_result = train(
    feature_store=features.source,
    dataset=features.dataset,
    splits=splits.project(features.dataset),
    aggregator=AggregatorConfig(name="abmil", params={"hidden_dim": 256}),
    task=task,
    training=TrainingConfig(learning_rate=1e-4, epochs=50),
    run_dir="output/abmil/uni2",
)

clam_result = train(
    feature_store=features.source,
    dataset=features.dataset,
    splits=splits.project(features.dataset),
    aggregator=AggregatorConfig(name="clam_sb", params={"hidden_dim": 256, "attn_dim": 128}),
    task=task,
    training=TrainingConfig(learning_rate=1e-4, epochs=50),
    run_dir="output/clam_sb/uni2",
)

3. Run a full pipeline in one call

Pipeline(config).run() handles preprocessing, feature extraction, training across folds, and metric aggregation in a single call.

from soma import Pipeline, PipelineConfig
from soma import EncoderConfig, AggregatorConfig, TaskConfig, TrainingConfig

config = PipelineConfig(
    dataset_csv="dataset.csv",
    splits_csv="splits.csv",
    output_root="output",
    dataset_type="slide",
    encoder=EncoderConfig(name="uni2"),
    aggregator=AggregatorConfig(name="abmil", params={"hidden_dim": 256}),
    task=TaskConfig(name="binary_classification"),
    training=TrainingConfig(learning_rate=1e-4, epochs=50),
)

result = Pipeline(config).run()

The returned PipelineResult includes:

  • fold_results: one entry per fold, each with training, tune, and test reports
  • summary: aggregated metrics across folds
  • run_dir: the resolved run directory containing the saved artifacts

Task-free representation evaluation

Frozen tile embeddings can instead be evaluated directly with the fixed CRoMa v1 protocol. A representation config must explicitly disable the ordinary task default:

data:
  dataset_csv: dataset.csv
  splits_csv: splits.csv
  dataset_type: tile
task: null
representation:
  kind: croma
  confounder_column: medical_center
  split: test
  evaluation_design: all
  m: 5
  alpha: 0.10

Selected dataset rows must contain non-empty label, literal group_id, and the configured confounder column. Representation runs do not fit a head or write a task report. Their provenance records the installed CRoMa version and the ordinary encoder configuration (including output variant). That record is useful for comparison, but an encoder slug, variant, dimension, package version, and ordinary run metadata do not by themselves prove byte-identical weights or preprocessing; Soma does not add checkpoint hashing or a separate upstream fingerprint for this protocol.

CLI

soma ships a command-line interface that runs a full pipeline from a YAML config file:

soma /path/to/config.yaml
python -m soma /path/to/config.yaml

The YAML layout is grouped by concern: run, data, preprocessing, encoder, aggregation, task, evaluation, training, execution, cache, and reports. soma merges your file on top of the bundled soma/configs/default.yaml, so you usually only need to edit the blocks you want to change.

You can also inspect the available presets directly from the terminal:

soma list encoders --level tile
soma list aggregators
soma list decoders
soma list pixel-classifiers
soma list tasks

examples/ contains a reference.yaml documenting every available field, and focused per-task starting points (slide_binary_classification.yaml, slide_ordinal_classification.yaml, slide_regression.yaml, tile_classification.yaml).

Docs

Full documentation is hosted at https://clemsgrs.github.io/soma.

License

This repository is available under Apache-2.0.

Metadata

Release files for soma-pathology 1.12.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for soma-pathology 1.12.0
File Size Uploaded
soma_pathology-1.12.0.tar.gz 476.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for soma-pathology 1.12.0
File Interpreter ABI Platform
soma_pathology-1.12.0-py3-none-any.whl Python 3 none any Details

Total release size: 1.1 MB

Release files / soma_pathology-1.12.0.tar.gz

Download URL soma_pathology-1.12.0.tar.gz
Size 476.0 kB
Tags Source
SHA-256 checksum
How to use checksums
5f20c553d9c70785ad9a03e287097a71b4aa9e2e9e23b82b5f939debf88dee99
BLAKE2b-256 checksum
How to use checksums
b9802596e4e2ace577b51d66d156dbde00e663d9e69066282d11d0afcabfae87
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.11.16

Release files / soma_pathology-1.12.0-py3-none-any.whl

Download URL soma_pathology-1.12.0-py3-none-any.whl
Size 577.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
accd9e09f41abf5b886f0af301c165a3b6570a7ed6711365391bb918ad2a55cf
BLAKE2b-256 checksum
How to use checksums
56df31118b91d85fbb1cf1b2921e9b9817b2776937d0855bab3414209bf94d72
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.11.16

Release history Release notifications | RSS feed

2.0.0

2 release files

1.19.0

2 release files

1.16.0

2 release files

1.15.1

2 release files

1.15.0

2 release files

This release

1.12.0 This release

2 release files

1.11.3

2 release files

1.11.2

2 release files

1.11.1

2 release files

1.11.0

2 release files

1.10.1

2 release files

1.10.0

2 release files

1.9.0

2 release files

1.8.0

2 release files

1.7.0

2 release files

1.6.0

2 release files

1.5.1

2 release files

1.5.0

2 release files

1.4.0

2 release files

1.3.0

2 release files

1.2.0

2 release files

1.1.3

2 release files

1.1.2

2 release files

1.1.1

2 release files

1.1.0

2 release files

1.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page