Skip to content

Let callers choose the jitter and exclusion rules, defaulting to upstream cherimoya main — Closes #21 - #22

Merged
conradbzura merged 3 commits into
mainfrom
21-jitter-and-exclusion-rules
Nov 17, 2026
Merged

conradbzura merged 3 commits into
mainfrom
21-jitter-and-exclusion-rules

Conversation

@conradbzura

@conradbzura conradbzura commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Make the two rules a training set is drawn under values the caller states, and default them to upstream cherimoya main. Jitter(low, high) is the shifts a peak's draw may take from its locus's centre, both bounds inclusive; Exclusion(regions, bin_width=100, end_padding=0) is the bin grid an exclusion is tested on and how an interval's end is snapped to it. The sampler, the window loader, the locus filters, the loader and the window-cache key take them through the existing max_jitter and exclusion_lists arguments, where an integer m still works and means Jitter(-m, m), and a bare region set means Exclusion(regions).

The js031 run measured quadrat byte-identical to upstream abb4d7b and divergent from upstream main (5729b57) in the jitter draw: jmschrei/cherimoya#92 widened it to include both ends of the widened peak. The exclusion rule differs too, found in upstream's code rather than measured, since the run's exclusions never reach the difference: tangermeme 1.6 snaps [start, end) to the bins it overlaps and skips empty regions. Both now default to main's, apart from an empty window, which covers no bin where tangermeme 1.6 gives it one, and both old rules stay one argument away, so a run seeded before #92 reproduces without pinning an old quadrat.

Each rule is expressed by its input rather than a flag, following the API guide's "compose, don't configure": the pre-#92 jitter is Jitter(-m, m - 1), and tangermeme 1.5's exclusion rule is end_padding=1, which reproduces its closed end and its marking of an empty region for every region whose end is not before its start.

Closes #21

Usage

from quadrat.loader import build_training_loader
from quadrat.rules import Exclusion, Jitter

# Upstream cherimoya main: shifts uniform over [-500, 500], tangermeme 1.6 bins.
build_training_loader(peaks, negatives, fasta, signals, max_jitter=500,
                      exclusion_lists=["ENCFF356LFX.bed.gz"])

# The same run as cherimoya drew it before #92, byte for byte (Jitter(-m, m - 1) for m > 0; Jitter(0, 0) at m = 0).
build_training_loader(peaks, negatives, fasta, signals,
                      max_jitter=Jitter(-500, 499),
                      exclusion_lists=Exclusion("ENCFF356LFX.bed.gz", end_padding=1))

# Exclude by base overlap, as bedtools intersect does, and keep a second list on 100-base bins.
build_training_loader(peaks, negatives, fasta, signals,
                      exclusion_lists=[Exclusion("blacklist.bed", bin_width=1),
                                       Exclusion("repeats.bed")])

Proposed changes

quadrat.rules (new)

A data-only module holding Jitter, Exclusion, as_jitter, as_exclusions and EXCLUSION_BIN_WIDTH. It imports neither torch nor the pileup extra, which is why the values live here and not in quadrat.sampling: quadrat.loci must not import torch, and both modules read them.

  • Jitter is a frozen dataclass of two ints, compared, hashed and pickled by value. Its margin, max(-low, high, 0), is what a window is widened by on each side; a draw's offset into the widened window runs from low + margin through high + margin. A lopsided rule pays for its longer side on both, which its docstring states.
  • Exclusion is frozen and compared by identity, since its regions may be frames. An interval, region or window alike, covers bins start // bin_width through (end + end_padding - 1) // bin_width, and none when end + end_padding <= start.
  • Neither can be subclassed, as Aggregate cannot: every reader trusts their fields as validated.
  • as_exclusions gathers every bare locus set in a list into one default Exclusion, after the explicit ones, so exclusion_lists=[a, b] still means one set of regions.

The sampler

PeakNegativeMixSampler holds a Jitter as jitter and draws randint(low + margin, high + margin + 1), made exactly when the margin is positive, the branch condition it already had. The sequence of generator calls is unchanged, so for Jitter(-m, m) the draw is upstream's randint(0, 2m + 1) and for Jitter(-m, m - 1) it is the previous randint(0, 2m), bit for bit. LocusDraw.jitter keeps meaning the offset into the widened window, so LocusDataset is untouched. max_jitter as an attribute is gone; jitter replaces it.

Windows and exclusions

load_windows widens by as_jitter(max_jitter).margin. exclusion_mask resolves its argument with as_exclusions and runs the existing bin-table interval join once per rule, binning both sides by that rule and ORing the results. Every rule's regions are read before the first join, so a bad one is refused whatever an earlier rule flagged. Intervals that cover no bin are left out of the join on both sides.

The loader and the cache key

build_training_loader resolves max_jitter once and passes the Jitter to the peak windows and the sampler; negatives are still not jittered. window_cache_key keys the jitter by its margin alone, so the two jitter rules share an entry, and keys each Exclusion by its bin_width, its end_padding and its files. Keys without exclusions are unchanged; keys with them all change, which they must, since an entry built under the old default rule must not be hit under the new one.

Golden digests

The plan and batch digests are re-recorded from upstream's own code: PeakNegativeSampler._prepare_epoch and PeakGenerator at 5729b57, over the same arithmetic corpus and the same six sampler configurations, by a recorder that first reproduced every existing literal from abb4d7b. The existing literals stay, as GOLDEN_PLANS_BEFORE_92 and GOLDEN_BATCHES_BEFORE_92, pinned under Jitter(-m, m - 1). no_jitter is the same in both, as it should be.

Breaking

Every seeded stream with a positive max_jitter draws differently. Under the default exclusion rule, an interval ending exactly on a 100-base edge no longer reaches the bin beginning there, whether it is a region or a window, and an empty region or window no longer covers the bin holding it. Jitter(-m, m - 1) for m > 0 and Exclusion(regions, end_padding=1) reproduce earlier runs for every region whose end is not before its start. The tangermeme floor rises to 1.6.0, whose exclusion rule is now the default and which the parity test against tangermeme's own extract_loci assumes.

Decisions for the owner

  • No flags. The issue's provisional inclusive= and closed_end= became inputs: an inclusive bound pair and an end padding. The padding also expresses a base-overlap exclusion (bin_width=1) at no extra surface.
  • Argument names kept. max_jitter takes a Jitter and exclusion_lists takes an Exclusion, as the issue specified, though a range is not a maximum and one rule is not a list. Rename to jitter= and exclusions=, with the old names as shorthand, when the loader is published; ledgered with renaming LocusDraw.jitter to offset.
  • A new module. quadrat.rules rather than the sampler, for the import constraint above.
  • Package-internal. Like the loader, neither value is exported from quadrat yet.
  • Malformed regions. Under the old code a region whose end precedes its start flagged its own bin when it sat inside one bin, and any window spanning it otherwise; under end_padding=1 it flags nothing. No well-formed input changes.
  • Out of scope. Jitter on negatives, stranded and control tracks, sharding.

Review

Four passes, one reviewer per role, each pass's findings remediated:

Pass Roles Blocking Advisory Headline
Core 1 api, python, perf 0 14 One-sided jitter widened both sides for nothing; exclusion_mask rebuilt its window frame per rule through Python strings (1.6× at a million windows); the tangermeme floor admitted 1.5
Core 2 api, python, perf 3 9 The empty-window docstring fix overreached under padding; the tangermeme parity test still skipped the bin-edge peak; a copied loader test; refusing one-sided jitter drew an arbitrary line, so lopsided rules are accepted and their cost stated
Docs/qa 1 docs, qa 5 9 LocusDraw.jitter misdescribed a negative's offset; TypeError undocumented; the PR body cited a deleted test and overstated the js031 run; the empty-interval filter and bin_width were pinned only by Hypothesis draws
Docs/qa 2 docs, qa 2 8 Two PR-body claims (reversed regions, empty windows) narrower than the code; all 28 non-equivalent mutants of the rule paths killed deterministically

Two independent checks ran alongside: a reimplementation of upstream's _prepare_epoch reproduced both golden plan sets, and a 400-trial differential against tangermeme 1.6's own binning found no mismatch over non-empty windows. The last pass's fixes were verified by the suite and the mutation harness, not another review pass. Deferred and incidental findings are ledgered.

Test cases

# Test Suite Given When Then Coverage Target
1 TestPeakNegativeMixSampler Each golden configuration Two epochs are planned and hashed The digest equals upstream 5729b57's Default draw is upstream main's
2 TestPeakNegativeMixSampler Each golden configuration with Jitter(-m, m - 1) Two epochs are planned and hashed The digest equals upstream abb4d7b's Pre-#92 stream still reachable
3 build_training_loader The arithmetic corpus and each golden configuration Two epochs are drained and hashed, in process and under persistent and fresh workers The digest equals upstream 5729b57's PeakGenerator Default batches are upstream main's
4 build_training_loader The same, with Jitter(-m, m - 1) Two epochs are drained and hashed The digest equals upstream abb4d7b's Pre-#92 batches still reachable
5 TestPeakNegativeMixSampler Any arguments and any Jitter in a small range An epoch is planned Every peak offset lies in [low + margin, high + margin] and every negative's is 0 Draw range for any rule (property)
6 TestPeakNegativeMixSampler Jitter(-2, 1), Jitter(-2, 2) and the lopsided Jitter(0, 3) Several epochs are planned Peak offsets fill [0, 3], [0, 4] and [3, 6] Every offset of each rule drawn, by peaks alone
7 exclusion_mask Any windows and regions on two contigs, any of three bin widths and an end padding up to two The mask is taken It equals a base-by-base oracle of the documented rule Snapping rule (property)
8 exclusion_mask A region and a window each ending on a bin edge The mask is taken with and without padding The neighbouring bin is reached only when padded tangermeme 1.5 and 1.6 rules
9 exclusion_mask An empty region beside a window in its bin The mask is taken under each padding The window is flagged only when padded Empty-region rule
10 exclusion_mask Two Exclusions each flagging one window; a list of two bare frames The mask is taken Both windows are flagged in each case Rules ORed; bare sets grouped
11 exclusion_mask A valid rule flagging every window, then a rule over a non-locus object The mask is taken TypeError Every rule read before any join
12 window_cache_key max_jitter=500, Jitter(-500, 499), Jitter(-500, 500) Each is keyed The keys are equal; a different margin moves the key Jitter keyed by margin alone
13 window_cache_key One exclusion file under other bin widths and paddings Each is keyed Each pair of keys differs; a bare path keys as Exclusion(path) Exclusion rule in the key
14 build_training_loader max_jitter=True with missing inputs The loader is built TypeError, nothing indexed and nothing cached Refused before anything is read
15 TestJitter Bounds of every type and order A rule is constructed and its margin read Integers are normalised, low > high and non-integers are refused, and the margin contains every shift Construction and margin
16 TestExclusion Defaults, floors, non-integers and a nested Exclusion A rule is constructed Defaults are 100 and 0, a list is held as a tuple, and each bad field is refused by name Construction
17 as_jitter and as_exclusions Integers, rules, None, paths, and lists mixing rules and bare sets Each is resolved The shorthand and grouping documented, in order Shorthand resolution

@conradbzura conradbzura self-assigned this Nov 17, 2026
A training set is drawn under two rules that were hardcoded: how far a
peak's draw may shift from its locus's centre, and which windows an
excluded region removes. Each becomes a frozen value a caller can
state: Jitter(low, high) as inclusive shift bounds with the margin a
window needs, and Exclusion(regions, bin_width, end_padding) as the bin
grid and how an interval's end is snapped to it. as_jitter and
as_exclusions resolve the shorthand the loader already accepts.

The module imports neither torch nor the pileup extra, so the locus
filters and the sampler can both read it.
The default exclusion rule is tangermeme 1.6's, and the parity test of
extract_loci against tangermeme's own reader holds only from 1.6.
Upstream cherimoya changed both rules after the commit quadrat was
ported from. jmschrei/cherimoya#92 widened the jitter draw so a window
may start at either end of the widened peak, and tangermeme 1.6 snaps
a half-open interval to the bins it overlaps rather than also reaching
the bin its exclusive end falls in, and skips empty regions.

The sampler, the window loader, the locus filters, the loader and the
cache key now take a Jitter for max_jitter and Exclusion values for
exclusion_lists, and their defaults are upstream main's rules: an
integer m means Jitter(-m, m), and a bare region set means
Exclusion(regions). The jitter rule never moves the window cache key,
since the margin is unchanged; an exclusion's rule does.

The golden digests are re-recorded from upstream's own sampler and
PeakGenerator at 5729b57, and the previous ones, recorded at abb4d7b,
stay pinned under the pre-#92 rule.

BREAKING CHANGE: every seeded stream with a positive max_jitter draws
differently. Under the default exclusion rule an interval ending on a
100-base edge no longer reaches the bin beginning there, and an empty
region or window no longer covers the bin holding it. Pass
Jitter(-m, m - 1) for m > 0 and Exclusion(regions, end_padding=1) to
reproduce earlier runs for every region whose end is not before its
start.
@conradbzura
conradbzura force-pushed the 21-jitter-and-exclusion-rules branch from db60e69 to 776f4bd Compare November 17, 2026 16:38
@conradbzura
conradbzura marked this pull request as ready for review November 17, 2026 17:43
@conradbzura
conradbzura merged commit 005e0f5 into main Nov 17, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Let callers choose the jitter and exclusion rules, defaulting to upstream cherimoya main

1 participant