Skip to content

Commit 43380ac

Browse files
authored
fix(checks): withhold state measurements for empty vectors (#735)
1 parent 6cf6e98 commit 43380ac

2 files changed

Lines changed: 62 additions & 10 deletions

File tree

‎src/hflow/checks.py‎

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -105,13 +105,15 @@ class _JointMotionProfile:
105105
def _joint_motion_profile(
106106
episode: Episode, topic: str, field: str | None
107107
) -> _JointMotionProfile | None:
108-
"""``None`` when the channel has fewer than two messages (no motion to
109-
profile); callers record the message count and stop."""
108+
"""``None`` when there are fewer than two messages or no selected
109+
components; callers record the message count and stop."""
110110
channel = episode.channel(topic)
111111
stamps_ns = channel.timestamps
112112
if len(stamps_ns) < 2:
113113
return None
114114
positions = channel.to_numpy(field)
115+
if positions.size == 0:
116+
return None
115117
if positions.ndim == 1:
116118
positions = positions[:, np.newaxis]
117119
deltas_s = np.diff(stamps_ns) / 1e9
@@ -437,7 +439,8 @@ async def joint_discontinuity(
437439
them dilute the percentage denominator) fails the gate open (#546).
438440
Every percentage and maximum here covers measurable steps only, and a
439441
stream with nothing measurable reports its counts and withholds the
440-
verdict-shaped keys rather than emitting 0% or NaN.
442+
verdict-shaped keys rather than emitting 0% or NaN. An empty selected
443+
field records only ``velocity_sample_count``, without motion evidence.
441444
"""
442445
return await run_blocking(
443446
_measure_joint_discontinuity,
@@ -812,7 +815,8 @@ async def idle_fraction(
812815
numerator nor the denominator: they had no velocity to be idle under, and
813816
counting their time as "moving" understates the fraction (#546). A stream
814817
with nothing measurable reports its counts and withholds
815-
``idle_fraction`` rather than storing 0.0 for a dead channel.
818+
``idle_fraction`` rather than storing 0.0 for a dead channel. An empty
819+
selected field records only ``idle_sample_count``.
816820
"""
817821
return await run_blocking(
818822
_measure_idle_fraction,
@@ -1373,7 +1377,8 @@ async def trajectory_metrics(
13731377
ratio is undefined (zero denominator), so only ``{topic}/final_pose_speed``
13741378
appears. Curation queries joining on ``final_pose_unsettled_ratio`` will
13751379
silently exclude motionless episodes; use ``final_pose_speed`` when you need
1376-
all episodes regardless of mean motion.
1380+
all episodes regardless of mean motion. An empty selected field records
1381+
only ``trajectory_sample_count``; it is not a stationary trajectory.
13771382
13781383
Evidence only, and this one ships no recommended gate at all: these are
13791384
motion-smoothness metrics, which are known to invert on real defects
@@ -1485,7 +1490,8 @@ async def trajectory_segments(
14851490
episode compare", this one answers "when, inside it, did things happen".
14861491
Emits ``motionless:<topic>``, ``trajectory_change:<topic>``, and
14871492
``peak_velocity:<topic>`` intervals in log time, which is what a timeline
1488-
view renders and what an operator scrubs to.
1493+
view renders and what an operator scrubs to. An empty selected field
1494+
records only ``segment_sample_count`` and produces no intervals.
14891495
14901496
The change threshold is derived from the episode's own curvature
14911497
distribution -- a duration-weighted median plus two robust sigmas, sigma
@@ -1779,7 +1785,8 @@ async def action_integrity(
17791785
runs this looks for, and NaN != NaN correctly breaks a run rather than
17801786
extending it. Run this on raw channels only -- a resampled channel carrying
17811787
a hold policy manufactures exactly-repeated samples, which is a fabricated
1782-
frozen run rather than a recorded one.
1788+
frozen run rather than a recorded one. An empty selected field records
1789+
only ``integrity_sample_count``; absent components are not frozen values.
17831790
"""
17841791
return await run_blocking(
17851792
_measure_action_integrity,
@@ -1805,6 +1812,8 @@ def _measure_action_integrity(
18051812
return CheckResult(measurements={f"{topic}/integrity_sample_count": len(stamps_ns)})
18061813

18071814
samples = channel.to_numpy(field)
1815+
if samples.size == 0:
1816+
return CheckResult(measurements={f"{topic}/integrity_sample_count": len(stamps_ns)})
18081817
if samples.ndim == 1:
18091818
samples = samples[:, np.newaxis]
18101819
sample_count, dimension_count = samples.shape
@@ -1895,6 +1904,8 @@ def _trajectory_profile(
18951904
if len(stamps_ns) < 2:
18961905
return None
18971906
samples = channel.to_numpy(field)
1907+
if samples.size == 0:
1908+
return None
18981909
if samples.ndim == 1:
18991910
samples = samples[:, np.newaxis]
19001911

‎tests/test_checks_nan_failopen.py‎

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,21 @@
1414

1515
import asyncio
1616
import math
17+
from collections.abc import Callable, Coroutine
1718
from pathlib import Path
19+
from typing import Any
1820

1921
import pytest
2022
from mcap.writer import Writer
2123

2224
from hflow import Episode
23-
from hflow.checks import idle_fraction, joint_discontinuity
25+
from hflow.checks import (
26+
action_integrity,
27+
idle_fraction,
28+
joint_discontinuity,
29+
trajectory_metrics,
30+
trajectory_segments,
31+
)
2432
from hflow.format import METADATA_RECORD_EPISODE
2533
from hflow.steps import CheckResult
2634
from hflow.testing import (
@@ -33,7 +41,13 @@
3341
NS_PER_S = 1_000_000_000
3442

3543

36-
def _joint_states_episode(path: Path, stamps_ns: list[int], positions: list[float]) -> Episode:
44+
def _joint_states_episode(
45+
path: Path,
46+
stamps_ns: list[int],
47+
positions: list[float],
48+
*,
49+
omit_positions: bool = False,
50+
) -> Episode:
3751
with path.open("wb") as stream:
3852
writer = Writer(stream)
3953
writer.start(profile="ros2", library="test-546")
@@ -53,7 +67,7 @@ def _joint_states_episode(path: Path, stamps_ns: list[int], positions: list[floa
5367
stamp_ns,
5468
"probe",
5569
["j0"],
56-
[position],
70+
[] if omit_positions else [position],
5771
[],
5872
[],
5973
)
@@ -167,3 +181,30 @@ def test_every_percentage_names_the_time_it_could_not_measure(
167181
assert idle.measurements[f"{TOPIC}/idle_nonpositive_dt_count"] == 1
168182
assert f"{TOPIC}/idle_fraction" in idle.measurements
169183
_assert_all_measurements_finite(velocity, idle)
184+
185+
186+
@pytest.mark.parametrize(
187+
("check", "sample_count_key"),
188+
[
189+
(action_integrity, "integrity_sample_count"),
190+
(joint_discontinuity, "velocity_sample_count"),
191+
(idle_fraction, "idle_sample_count"),
192+
(trajectory_metrics, "trajectory_sample_count"),
193+
(trajectory_segments, "segment_sample_count"),
194+
],
195+
)
196+
def test_empty_position_vectors_withhold_motion_measurements(
197+
tmp_path: Path,
198+
check: Callable[..., Coroutine[Any, Any, CheckResult]],
199+
sample_count_key: str,
200+
) -> None:
201+
with _joint_states_episode(
202+
tmp_path / "empty-positions.mcap",
203+
[i * NS_PER_S for i in range(4)],
204+
[0.0] * 4,
205+
omit_positions=True,
206+
) as episode:
207+
result = asyncio.run(check(episode, field="position"))
208+
209+
assert result.measurements == {f"{TOPIC}/{sample_count_key}": 4}
210+
assert result.intervals == []

0 commit comments

Comments
 (0)