Skip to content

Commit

Permalink
Special-casing empty intervals in trim
Browse files Browse the repository at this point in the history
  • Loading branch information
bmcfee committed Aug 27, 2019
1 parent 99b49d3 commit 7df0ec8
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
3 changes: 2 additions & 1 deletion jams/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -916,7 +916,8 @@ def trim(self, start_time, end_time, strict=False):
obs_start = obs.time
obs_end = obs_start + obs.duration

if obs_start < trim_end and obs_end >= trim_start:
# Special-case here handles duration=0 as a closed interval
if obs_start < trim_end and (obs_end > trim_start or obs_start == obs_end >= trim_start):

new_start = max(obs_start, trim_start)
new_end = min(obs_end, trim_end)
Expand Down
16 changes: 8 additions & 8 deletions tests/test_jams.py
Original file line number Diff line number Diff line change
Expand Up @@ -801,17 +801,17 @@ def test_annotation_trim_partial_overlap_beginning():
# range: at the beginning
# strict=False
namespace = 'tag_open'
data = dict(time=[5.0, 5.0, 5.0, 10.0],
duration=[0.0, 2.0, 4.0, 4.0],
value=['zero', 'one', 'two', 'three'],
confidence=[0.1, 0.9, 0.9, 0.9])
data = dict(time=[4.0, 5.0, 5.0, 5.0, 10.0],
duration=[1.0, 0.0, 2.0, 4.0, 4.0],
value=['none', 'zero', 'one', 'two', 'three'],
confidence=[1, 0.1, 0.9, 0.9, 0.9])
ann = jams.Annotation(namespace, data=data, time=5.0, duration=10.0)

ann_trim = ann.trim(0, 8, strict=False)
ann_trim = ann.trim(1, 8, strict=False)

assert ann_trim.time == 5
assert ann_trim.duration == 3
assert ann_trim.sandbox.trim == [{'start_time': 0, 'end_time': 8,
assert ann_trim.sandbox.trim == [{'start_time': 1, 'end_time': 8,
'trim_start': 5, 'trim_end': 8}]
assert ann_trim.namespace == ann.namespace
assert ann_trim.annotation_metadata == ann.annotation_metadata
Expand All @@ -826,11 +826,11 @@ def test_annotation_trim_partial_overlap_beginning():
assert ann_trim.data == expected_ann.data

# strict=True
ann_trim = ann.trim(0, 8, strict=True)
ann_trim = ann.trim(1, 8, strict=True)

assert ann_trim.time == 5
assert ann_trim.duration == 3
assert ann_trim.sandbox.trim == [{'start_time': 0, 'end_time': 8,
assert ann_trim.sandbox.trim == [{'start_time': 1, 'end_time': 8,
'trim_start': 5, 'trim_end': 8}]
assert ann_trim.namespace == ann.namespace
assert ann_trim.annotation_metadata == ann.annotation_metadata
Expand Down

0 comments on commit 7df0ec8

Please sign in to comment.