code-switch-mtp / tests /test_position_strategy.py
Anupam-Rawat-IITB's picture
Add code, result files and documentation
3bbd5dd verified
Raw History Blame Contribute Delete
2.56 kB
"""Tests for entropy-profile position selection.
Regression guard for the byte-level sampling bias: capping to the leading
positions left byte-level conditions with zero word-boundary positions, so
entropy_trough_depth (a ratio whose denominator is the boundary mean) came
out nan or was computed from a handful of unrepresentative sentences.
"""
from src.measure.entropy import (
POSITION_STRATEGY_FIRST,
POSITION_STRATEGY_SPREAD,
EntropyProfilePoint,
_select_positions,
entropy_trough_depth,
)
def test_spread_covers_the_full_range_while_first_does_not():
positions = list(range(1, 101))
first = _select_positions(positions, 5, POSITION_STRATEGY_FIRST)
spread = _select_positions(positions, 5, POSITION_STRATEGY_SPREAD)
assert first == [1, 2, 3, 4, 5]
assert spread[0] == 1 and spread[-1] == 100
assert len(spread) == 5
def test_no_subsampling_when_under_the_cap():
positions = [1, 2, 3]
assert _select_positions(positions, 5, POSITION_STRATEGY_SPREAD) == positions
assert _select_positions(positions, None, POSITION_STRATEGY_SPREAD) == positions
def test_spread_reaches_a_late_boundary_that_first_misses():
"""Mirrors the real failure: with 3-byte characters the first word boundary
sits past the leading-positions cap, so only spread sampling ever tags a
boundary position.
"""
positions = list(range(1, 40))
boundary_positions = {11, 12, 18, 19, 34}
first = _select_positions(positions, 5, POSITION_STRATEGY_FIRST)
spread = _select_positions(positions, 5, POSITION_STRATEGY_SPREAD)
assert not (set(first) & boundary_positions)
assert set(spread) & boundary_positions
def test_trough_depth_is_nan_without_boundary_points():
"""The observable symptom of the bug: a ratio with an empty denominator."""
points = [EntropyProfilePoint(position=i, horizon=1, entropy=2.0, is_boundary=False) for i in range(5)]
assert entropy_trough_depth(points) != entropy_trough_depth(points) # nan
def test_trough_depth_is_the_intraword_over_boundary_ratio():
points = [
EntropyProfilePoint(position=0, horizon=1, entropy=1.0, is_boundary=False),
EntropyProfilePoint(position=1, horizon=1, entropy=3.0, is_boundary=False),
EntropyProfilePoint(position=2, horizon=1, entropy=4.0, is_boundary=True),
# a higher-horizon point must not leak into a horizon-1-only statistic
EntropyProfilePoint(position=3, horizon=2, entropy=99.0, is_boundary=True),
]
assert entropy_trough_depth(points) == 2.0 / 4.0