Skip to main content

pyhighlights

Tests Documentation PyPI

Select-then-predict models for highlight-based explainable AI research.

Most explainability methods answer why did the model say that? after the fact, and nothing forces the answer to be true. A select-then-predict model rearranges the pipeline so the question does not arise: a selector picks a subset of the input, and a predictor sees only that subset. The selected subset — the highlight — is not a story about the prediction, it is the input to it. A highlight that omits what mattered produces a worse prediction, which is measurable rather than arguable.

Documentation · Tutorial · Contributing

Installation

pip install pyhighlights

Transformer backbones are an extra, so a GRU run does not pull in transformers:

pip install "pyhighlights[transformers]"

One key, one experiment

A task is one experiment start to finish — a corpus, its preprocessing, a model, its metrics, and a list of seeds:

from pathlib import Path

import pyhighlights
from cinnamon.registry import Registry
from pyhighlights.components.analyzers import MetricsAnalyzer, PredictionAnalyzer
from pyhighlights.configurations.keys import TOY_TASK

Registry.build(directory=Path(pyhighlights.__file__).parent)

task = Registry.from_key(
    TOY_TASK,
    save_path="results",
    seeds=[0, 1],
    store_predictions=True,
)
task.run()

Each seed trains from scratch, restores the checkpoint that scored best on validation, and is scored on validation and test. Seeds are a list rather than a number because one run of a select-then-predict model says very little: the selector is trained through a discrete choice, and the spread across seeds is part of the result.

What lands on disk:

results/toy/2026-09-09T17-55-22/
├── results.json               # every seed's metrics, and their summary
├── manifest.json              # the key, the overrides, the whole
│                              #   configuration tree, and the versions
├── predictions-seed=0.pkl
├── predictions-seed=1.pkl
└── seed=0/epoch=0-step=8.ckpt

A run never overwrites an earlier one: two runs of a task are two results to compare. The manifest names the key and the arguments the run was launched with, and resolves every nested key into the numbers behind it — so the file states the hidden size and the learning rate rather than the name of the place they came from.

Read the results back as frames, so the same analyzer serves a notebook, a test and a LaTeX table:

MetricsAnalyzer(directory="results", metrics=["accuracy", "highlight_f1"]).run()
task                 run  seeds          accuracy      highlight_f1
 toy 2026-09-09T17-55-22      2 0.4062 +/- 0.0312 0.0000 +/- 0.0000

A stored prediction is token ids and masks. PredictionAnalyzer rebuilds the corpus from the key in the manifest and joins on sample_id, so a row says which words the model kept:

PredictionAnalyzer(directory="results").analyze()[
    ["seed", "sample_id", "label", "predicted", "rationale"]
]

What is in it

Architectures, each a different answer to selection being a discrete choice inside a differentiable model. All five are registered for both a GRU and a Transformer backbone; the algorithms never mention either, because a backbone is anything implementing encode / pool / output_size.

FR Selector and predictor fold onto one encoder, so they cannot drift apart. Liu et al., NeurIPS 2022
MGR Several generators, one shared predictor, so no single degenerate generator sets the equilibrium. Liu et al., ACL 2023
MCD Trained against selected-input and full-input predictions; agreement means the highlight d-separates the label. Liu et al., NeurIPS 2023
G-RAT A pretrained attention classifier guides the selection and matches its distribution. Hu and Yu, AAAI 2024
GenSPP No gradient reaches the generator — a genetic search scores each candidate by training a fresh predictor. Ruggeri and Signorelli, ACL 2025

Corpora, each loaded as its authors distributed it: R2A beer and hotel (three aspects each), ERASER movies, hatexplain, and a synthetic toy for smoke tests. Cleaning is a preprocessor a study names, not something a loader does, so two studies over one corpus can prepare it differently.

Metrics per corpus, by output classes and by highlights, plus faithfulness — sufficiency and comprehensiveness — over the test split.

Reproductions live in pyhighlights_benchmarks, beside the library rather than inside it, so nothing here carries one paper's values. GenSPP (ACL 2025) is the first: two corpora against all five architectures.

Where it is going

Roadmap — Zenodo dataset artifacts so a reproduction does not depend on a URL somebody else controls, a study on unannotated legal text, per-corpus sparsity targets, and the rest of the highlight-metric suite.

pyhighlights ships tools, not an experiment. Which metrics to log, which aspect of Beer to train on, which sparsity target to aim at — all of that depends on a study, so all of it stays a configuration the study writes.

Metadata

Release files for pyhighlights 0.5.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 pyhighlights 0.5.0
File Size Uploaded
pyhighlights-0.5.0.tar.gz 119.3 kB Details

Built distribution (wheel)

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

Total release size: 224.3 kB

Release files / pyhighlights-0.5.0.tar.gz

Download URL pyhighlights-0.5.0.tar.gz
Size 119.3 kB
Tags Source
SHA-256 checksum
How to use checksums
55581b2931aa83f224f56f6832d3dbc3b6bf624ec229850b7e547c842bf791d9
BLAKE2b-256 checksum
How to use checksums
2a1139a73855f2cad0c7dde25d50bed00bb1aaf7ce88e99ac93ca74c02d11ce5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 10, 2026.

Transparency log

Release files / pyhighlights-0.5.0-py3-none-any.whl

Download URL pyhighlights-0.5.0-py3-none-any.whl
Size 105.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8785dcfdf0b3431ecd6d7a2c387113b02062ccfec24aa74a576aabf85426ab19
BLAKE2b-256 checksum
How to use checksums
af08813655ef302d1c05d079ad89bd24ac61fdbccd2cccfbde9d3809b01ef027
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 10, 2026.

Transparency log

Release history Release notifications | RSS feed

0.13.0

2 release files

0.12.0

2 release files

0.11.0

2 release files

0.9.0

2 release files

0.8.2

2 release files

0.8.1

2 release files

0.8.0

2 release files

0.7.3

2 release files

0.7.2

2 release files

0.7.1

2 release files

0.7.0

2 release files

0.6.0

2 release files

This release

0.5.0 This release

2 release files

0.4.0

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.0

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