Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/torchcodec/samplers/_index_based.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ def _validate_sampling_range_index_based(
sampling_range_end,
num_frames_in_video,
):
if sampling_range_start < -num_frames_in_video:
raise ValueError(
f"sampling_range_start ({sampling_range_start}) must be greater than "
f"or equal to {-num_frames_in_video}."
)

if sampling_range_start < 0:
sampling_range_start = num_frames_in_video + sampling_range_start

Expand Down
14 changes: 14 additions & 0 deletions test/test_samplers.py
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,20 @@ def test_index_based_samplers_errors(sampler):
):
sampler(decoder, sampling_range_start=-100, sampling_range_end=-100)

sampling_range_start = -len(decoder) - 1
with pytest.raises(
ValueError,
match=re.escape(
f"sampling_range_start ({sampling_range_start}) must be greater than "
f"or equal to {-len(decoder)}"
),
):
sampler(
decoder,
sampling_range_start=sampling_range_start,
sampling_range_end=-len(decoder),
)

with pytest.raises(
ValueError, match="We determined that sampling_range_end should"
):
Expand Down