Download tests/test_segmentation.py from k9cli/video-vec2wav2-tokenizer: direct link, hf CLI and curl.
- Browser
- Download file 1.99 kB
-
https://huggingface.co/datasets/k9cli/video-vec2wav2-tokenizer/resolve/main/tests/test_segmentation.py
- Command line
-
hf download hf://datasets/k9cli/video-vec2wav2-tokenizer/tests/test_segmentation.py
-
curl -L -o test_segmentation.py https://huggingface.co/datasets/k9cli/video-vec2wav2-tokenizer/resolve/main/tests/test_segmentation.py
1.99 kB
| """Tests for segmentation.""" | |
| from __future__ import annotations | |
| from pathlib import Path | |
| from video_vec2wav2_tokenizer.audio.extractor import load_wav | |
| from video_vec2wav2_tokenizer.segmentation.segmenter import Segmenter, normalize_segments | |
| from video_vec2wav2_tokenizer.utils.io import Segment | |
| def test_normalize_drops_short_segments() -> None: | |
| segs = [Segment(0.0, 0.4, "tiny"), Segment(1.0, 3.0, "ok")] | |
| out = normalize_segments(segs, min_length=1.0, max_length=20.0) | |
| assert len(out) == 1 | |
| assert out[0].text == "ok" | |
| def test_normalize_splits_long_segments() -> None: | |
| segs = [Segment(0.0, 50.0, "long")] | |
| out = normalize_segments(segs, min_length=1.0, max_length=20.0) | |
| assert len(out) == 3 # ceil(50/20) | |
| assert out[0].text == "long" | |
| assert out[1].text == "" # text not duplicated | |
| assert all(s.duration <= 20.0 + 1e-6 for s in out) | |
| def test_normalize_padding_clamped() -> None: | |
| segs = [Segment(0.0, 2.0, "x")] | |
| out = normalize_segments(segs, min_length=0.5, max_length=20.0, padding=1.0, total_duration=2.5) | |
| assert out[0].start == 0.0 # clamped at 0 | |
| assert out[0].end == 2.5 # clamped at total_duration | |
| def test_segment_file_writes_clips(sine_wav: Path, tmp_path: Path) -> None: | |
| segs = [Segment(0.0, 1.0, "a"), Segment(1.0, 2.0, "b")] | |
| segmenter = Segmenter(sample_rate=16000) | |
| out_dir = tmp_path / "clips" | |
| results = segmenter.segment_file(sine_wav, segs, out_dir, start_index=1) | |
| assert [p.name for p, _ in results] == ["000001.wav", "000002.wav"] | |
| samples, sr = load_wav(results[0][0]) | |
| assert sr == 16000 | |
| assert abs(samples.shape[0] - 16000) < 10 # ~1 second | |
| def test_segment_index_continues(sine_wav: Path, tmp_path: Path) -> None: | |
| segs = [Segment(0.0, 1.0, "a")] | |
| segmenter = Segmenter(sample_rate=16000) | |
| out_dir = tmp_path / "clips" | |
| r1 = segmenter.segment_file(sine_wav, segs, out_dir, start_index=5) | |
| assert r1[0][0].name == "000005.wav" | |