PULSE-code / pulse /models /f5 /subset.py
velvet-pine-22's picture
PULSE-code: benchmark code for the PULSE dataset (camera-ready release)
dffbc1f verified
Raw History Blame Contribute Delete
3.53 kB
"""F5 label budget: nested random subsets of the training volunteers' labelled segments.
The pool of a fold is every kept T2 segment (frozen taxonomy, ``segment.min_sec``)
of the training-partition recordings that overlaps the recording's evaluation
span (``pulse.tasks.t2_data.Timeline``). The pool does not depend
on the protocol, so tables ``5`` (sliding) and ``5seg`` (segment-aligned) draw
the same segments for the same (seed, fold, fraction).
``subset_segments`` draws one permutation from (seed, fold) and keeps its first
``round(fraction * N)`` segments, so the subsets are nested across fractions
and identical for the scratch and the S9-initialised condition. A training
sample (window or segment) is kept iff the segment that labels it -- the one
containing its centre frame -- is in the subset. Inner-validation and test
samples are never subsampled.
"""
from __future__ import annotations
import math
from typing import Dict, List, Sequence, Tuple
import numpy as np
from ...paths import DataRoot
from ...taxonomy import Taxonomy
from ...tasks import t2_data as D
Key = Tuple[str, int] # (recording id, segment start frame on the alignment timeline)
def segment_pool(dr: DataRoot, tax: Taxonomy, recs: Sequence["D.RecordingSamples"],
min_sec: float) -> Tuple[List[Key], List[Key]]:
"""(pool of segment keys, key of every sample in ``D.SampleSet`` order)."""
pool: List[Key] = []
sample_keys: List[Key] = []
for r in recs:
tl = r.timeline
spans = D.kept_segments(dr, r.rid, tax, min_sec, {})
# every kept segment that overlaps the evaluation span (it may label a sample)
pool += [(r.rid, sp.start) for sp in spans if sp.start < tl.end and sp.end > tl.start]
starts = np.asarray([sp.start for sp in spans], dtype=np.int64)
ends = np.asarray([sp.end for sp in spans], dtype=np.int64)
for a, b in zip(r.starts, r.ends):
centre = tl.start + tl.ds * ((int(a) + int(b)) // 2)
i = int(np.searchsorted(starts, centre, side="right")) - 1
if i < 0 or centre >= ends[i]:
raise ValueError(f"{r.rid}: sample [{a}, {b}) is not inside a kept segment")
sample_keys.append((r.rid, int(starts[i])))
missing = set(sample_keys) - set(pool)
if missing:
raise ValueError(f"samples labelled by segments outside the pool: {sorted(missing)[:3]}")
return pool, sample_keys
def subset_segments(keys: Sequence[Key], fraction: float, seed: int, fold: int) -> List[Key]:
if not 0 < fraction <= 1:
raise ValueError(f"fraction must be in (0, 1], got {fraction}")
keys = list(keys)
if fraction >= 1:
return keys
rng = np.random.default_rng(np.random.SeedSequence([int(seed), int(fold), 5]))
perm = rng.permutation(len(keys))
n = max(1, int(math.floor(fraction * len(keys) + 0.5)))
return [keys[i] for i in sorted(perm[:n])]
def subset_index(sample_keys: Sequence[Key], keep: Sequence[Key]) -> np.ndarray:
ks = set(keep)
return np.asarray([i for i, k in enumerate(sample_keys) if k in ks], dtype=np.int64)
def subset_summary(pool: Sequence[Key], keep: Sequence[Key], n_samples_all: int,
n_samples_kept: int) -> Dict[str, object]:
import hashlib
return {"segments_total": len(pool), "segments_used": len(keep),
"samples_total": int(n_samples_all), "samples_used": int(n_samples_kept),
"subset_md5": hashlib.md5(repr(sorted(keep)).encode()).hexdigest()}