Skip to content

Commit 2351714

Browse files
committed
Validate negative sampling range starts
1 parent 1f36e84 commit 2351714

2 files changed

Lines changed: 20 additions & 0 deletions

File tree

src/torchcodec/samplers/_index_based.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ def _validate_sampling_range_index_based(
3131
sampling_range_end,
3232
num_frames_in_video,
3333
):
34+
if sampling_range_start < -num_frames_in_video:
35+
raise ValueError(
36+
f"sampling_range_start ({sampling_range_start}) must be greater than "
37+
f"or equal to {-num_frames_in_video}."
38+
)
39+
3440
if sampling_range_start < 0:
3541
sampling_range_start = num_frames_in_video + sampling_range_start
3642

test/test_samplers.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,20 @@ def test_index_based_samplers_errors(sampler):
536536
):
537537
sampler(decoder, sampling_range_start=-100, sampling_range_end=-100)
538538

539+
sampling_range_start = -len(decoder) - 1
540+
with pytest.raises(
541+
ValueError,
542+
match=re.escape(
543+
f"sampling_range_start ({sampling_range_start}) must be greater than "
544+
f"or equal to {-len(decoder)}"
545+
),
546+
):
547+
sampler(
548+
decoder,
549+
sampling_range_start=sampling_range_start,
550+
sampling_range_end=-len(decoder),
551+
)
552+
539553
with pytest.raises(
540554
ValueError, match="We determined that sampling_range_end should"
541555
):

0 commit comments

Comments
 (0)