71 lines
2.2 KiB
Python
71 lines
2.2 KiB
Python
|
|
"""撕段门控 + 段合并的纯逻辑单测(不加载 YOLO)。"""
|
|||
|
|
|
|||
|
|
from __future__ import annotations
|
|||
|
|
|
|||
|
|
import numpy as np
|
|||
|
|
import pytest
|
|||
|
|
|
|||
|
|
from app.services.tear_gated_segment_consumption.product_map import (
|
|||
|
|
load_tear_segment_name_to_id,
|
|||
|
|
resolve_tear_segment_labels_yaml_path,
|
|||
|
|
)
|
|||
|
|
from app.services.tear_gated_segment_consumption.runner import haocai_mean_topk
|
|||
|
|
from app.services.tear_gated_segment_consumption.segments import merge_tear_segments
|
|||
|
|
|
|||
|
|
|
|||
|
|
def test_merge_tear_segments_one_valid() -> None:
|
|||
|
|
dt = 0.04
|
|||
|
|
rows: list[tuple[int, float, bool, str]] = [
|
|||
|
|
(i, i * dt, True, "A") for i in range(40)
|
|||
|
|
]
|
|||
|
|
segs = merge_tear_segments(
|
|||
|
|
rows,
|
|||
|
|
min_tear_sec=1.2,
|
|||
|
|
min_gap_sec=1.0,
|
|||
|
|
)
|
|||
|
|
assert len(segs) == 1
|
|||
|
|
assert segs[0]["start_frame"] == 0
|
|||
|
|
assert segs[0]["end_frame"] == 39
|
|||
|
|
|
|||
|
|
|
|||
|
|
def test_haocai_mean_topk() -> None:
|
|||
|
|
names = {0: "A", 1: "B"}
|
|||
|
|
a = np.array([0.2, 0.8, 0.0], dtype=np.float32)
|
|||
|
|
b = np.array([0.2, 0.8, 0.0], dtype=np.float32)
|
|||
|
|
t1, c1, t2, c2, t3, c3 = haocai_mean_topk([a, b], names)
|
|||
|
|
assert t1 == "B"
|
|||
|
|
assert abs(c1 - 0.8) < 1e-5
|
|||
|
|
|
|||
|
|
|
|||
|
|
def test_load_tear_segment_name_to_id_uses_package_yaml() -> None:
|
|||
|
|
p = resolve_tear_segment_labels_yaml_path()
|
|||
|
|
assert p.name == "consumable_classifier_labels.yaml"
|
|||
|
|
m = load_tear_segment_name_to_id()
|
|||
|
|
assert "一次性使用灭菌橡胶外科手套" in m or len(m) >= 1
|
|||
|
|
|
|||
|
|
|
|||
|
|
@pytest.mark.asyncio
|
|||
|
|
async def test_append_confirmed_detail_tear_cooldown_keys() -> None:
|
|||
|
|
"""同 item_id 多段在独立 cooldown_key 下应都能写入。"""
|
|||
|
|
from app.services.video.session_registry import SurgerySessionRegistry, SurgerySessionState
|
|||
|
|
|
|||
|
|
reg = SurgerySessionRegistry()
|
|||
|
|
st = SurgerySessionState(candidate_consumables=["X"], name_to_code={"X": "id1"})
|
|||
|
|
await reg.append_confirmed_detail(
|
|||
|
|
state=st,
|
|||
|
|
item_id="SAME",
|
|||
|
|
item_name="A",
|
|||
|
|
doctor_id="d",
|
|||
|
|
source="tear_segment",
|
|||
|
|
cooldown_key="s1:seg:1",
|
|||
|
|
)
|
|||
|
|
await reg.append_confirmed_detail(
|
|||
|
|
state=st,
|
|||
|
|
item_id="SAME",
|
|||
|
|
item_name="A",
|
|||
|
|
doctor_id="d",
|
|||
|
|
source="tear_segment",
|
|||
|
|
cooldown_key="s1:seg:2",
|
|||
|
|
)
|
|||
|
|
assert len(st.details) == 2
|