Skip to main content

eWHT: Evolutionary Walsh-Hadamard Transform for Fitness Landscapes

PyPI version PyPI - License PyPI Status PyPI Version Code Style Last Commit

ewht is a Python package for analyzing combinatorial fitness landscapes using the evolutionary Walsh-Hadamard transform (eWHT). It provides:

  • Fast O(N log N) forward and inverse eWHT transforms
  • Evolutionary mutation probabilities ps, estimated from a landscape scored by ESM2 or AntiBERTy
  • Data preprocessing helpers (genotype encoding, train/test splits)
  • Compressed sensing with Lasso on eWHT/WHT bases

Installation

ewht supports Python 3.9 and above. Install from PyPI:

pip install ewht

Optional extras:

pip install ewht[esm]         # score with ESM2-650M (torch + transformers)
pip install ewht[antiberty]   # score with AntiBERTy (torch + antiberty)
pip install ewht[plm]         # both

Quickstart

The package bundles the CR6261-H1 dataset from the paper, including a precomputed plm_score column, so the whole pipeline runs without downloading a model. The full script is in example_ewht.py:

import ewht

# Load data and encode binary genotypes
raw = ewht.load_example()
df = ewht.genotypes_from_dataframe(raw, POSITIONS, wt_sequence=WT, mutants=MUTANTS)

# ps from the pLM-scored library: median split, then per-site mutation frequency
ps, scored = ewht.get_ps_from_library(df, scores=df["plm_score"], return_frame=True)
print(ps.round(3))
[0.677 0.627 0.517 0.597 0.688 0.545 0.644 0.651 0.604 0.563 0.59 ]

# Forward eWHT of the centered landscape
coeffs, center = ewht.efwht_from_dataframe(df, ps)

# Compressed sensing: 100 variants from the pLM bin, tested on the rest of the bin
measured = (scored["estimated_fitness"] == 0).to_numpy()
train, test = ewht.split_bin_train_test(
    scored[measured].reset_index(drop=True), 100, in_bin="plm_score_bin", random_state=0
)
for basis in ("eWHT", "WHT"):
    r = ewht.run_cs_experiment(train, test, ps, basis=basis, random_state=0)
    print(basis, round(r.test_metrics["r2"], 4))
eWHT 0.5992
WHT 0.4593

Run the full example:

python example_ewht.py

Evolutionary mutation probabilities

The paper estimates ps from the measured library, not from the wild-type sequence alone. Every variant is scored with a protein language model, the library is split at the median score, and p_i is the fraction of variants in the upper half that carry a mutation at site i (clipped to [0.05, 0.95]):

Give it a landscape CSV (or DataFrame) and pick a model. Either way you get back the landscape plus plm_score and plm_score_bin (1 at or above the median, else 0), and ps is read off the plm_score_bin == 1 rows.

# ESM2-650M, wildtype-marginal LLRs (default)
ps, scored = ewht.get_ps_from_library(
    "landscape.csv", "esm2",
    wt_sequence=WT, positions=POSITIONS, mutants=MUTANTS, return_frame=True,
)

# AntiBERTy, pseudo-perplexity per variant sequence. Antibodies only.
ps, scored = ewht.get_ps_from_library(
    "landscape.csv", "antiberty",
    sequence_col="mutated_sequence", return_frame=True,
)

scored[["genotype", "fitness", "plm_score", "plm_score_bin"]].head()

Scoring uses the model score as returned (LLR or pseudo-perplexity), and is also able to cache the scores and feed them back rather than rescoring. The same hook takes scores from any other source (another model, a second assay):

ps = ewht.get_ps_from_library(df, scores=my_scores)
scored = ewht.score_landscape(df, scores=my_scores)  # just the frame
ps = ewht.ps_from_scored_landscape(scored)           # just the ps

Per-site mutation probabilities from the pLM bin

eWHT spectrum

The forward transform decomposes the centered landscape into Walsh coefficients grouped by interaction order:

eWHT coefficient spectrum orders 1-5

Core API

Function Description
efwht(y, ps) Forward eWHT on a length-2^L landscape vector
iefwht(coeffs, ps) Inverse eWHT (exact round-trip with matching norm)
efwht_from_dataframe(df, ps) Forward eWHT from a preprocessed DataFrame
get_ps_from_library(data, model) Score a landscape and estimate ps (the paper's method)
score_landscape(data, model) Landscape + plm_score + plm_score_bin
ps_from_scored_landscape(df) ps from an already scored and binned frame
get_ps_from_scores(df, scores) ps from a library scored by any model
genotypes_from_dataframe(df, positions, wt_sequence, mutants) Build a binary genotype column from sequences
split_bin_train_test(df, n_train, in_bin=...) Uniform train/test split inside a bin (the paper's protocol)
sample_evolutionary_sequences(df, ps, ...) ps-weighted subsampling, with an optional MSA mask
run_cs_experiment(train, test, ps) Lasso compressed sensing with CV on train
get_basis(bin_seqs, ps) eWHT / WHT design matrix

Compressed sensing

run_cs_experiment follows the paper: an expectation-norm design matrix (the only normalization it uses), an unpenalized intercept, and the Lasso alpha chosen by LassoCV on a 30-point, data-dependent path over the training rows.

r = ewht.run_cs_experiment(train, test, ps, basis="eWHT")
r.best_lambda, r.test_metrics, r.coeffs   # coeffs in expectation norm, lexicographic order

To compare bases fairly across n, draw one pool per seed and slice it, so the training sets are nested and every basis sees identical data:

for n in range(20, 201, 20):
    train, test = ewht.split_bin_train_test(df, n, in_bin=in_bin, pool_size=200, random_state=seed)

Conventions

  • Genotypes are 0/1 strings, 0 = wild-type and 1 = mutant. Pseudoboolean strings (1 = wild-type, -1 = mutant) are accepted too. For a custom mapping, add a genotype column directly instead of using genotypes_from_dataframe.
  • Site 0 is the most significant bit of the genotype index, for both the transform and the design matrix, so get_basis(...) @ coeffs reproduces the landscape that efwht decomposed. Coefficients come back in lexicographic order unless degree_order=True.

Optional dependencies

Extra Packages Use case
(default) numpy, pandas, scipy, scikit-learn transforms, ps from scores, MSA ps, CS
ewht[esm] torch, transformers scoring a landscape with ESM2-650M
ewht[antiberty] torch, antiberty scoring an antibody landscape with AntiBERTy
ewht[plm] both of the above either model

Metadata

Release files for ewht 0.2.1

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

Source distribution (sdist)

Source distribution for ewht 0.2.1
File Size Uploaded Provenance Actions
ewht-0.2.1.tar.gz 3.2 MB Without provenance Details for ewht-0.2.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for ewht 0.2.1
File Interpreter ABI Platform Actions
ewht-0.2.1-py3-none-any.whl Python 3 none any Details for ewht-0.2.1-py3-none-any.whl

Total release size: 6.4 MB

Release files / ewht-0.2.1.tar.gz

Download URL ewht-0.2.1.tar.gz
Size 3.2 MB
Tags Source
SHA-256 checksum
How to use checksums
4967c083f11a943009964839973c1655345443d936bb4b5c95be3ab811763352
BLAKE2b-256 checksum
How to use checksums
9f5a69746da2fe255b0a93504dd63031e9c15818080a436a09b67e0977872fa7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.10.14

Release files / ewht-0.2.1-py3-none-any.whl

Download URL ewht-0.2.1-py3-none-any.whl
Size 3.2 MB
Tags Python 3
SHA-256 checksum
How to use checksums
faf87b02f322b8d03909bed666ada6a5e981f1c949e2acd7c37608a326c577d9
BLAKE2b-256 checksum
How to use checksums
c507745b56603219f23b5f563ebd44e39dc57cb79b715be7ffac571199f043d7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.10.14

Security for ewht 0.2.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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