Skip to content

Commit

Permalink
refactor: remove praatio_scripts.alignBoundariesAcrossTiers
Browse files Browse the repository at this point in the history
  • Loading branch information
timmahrt committed Nov 4, 2023
1 parent 17f813c commit d7dd754
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 135 deletions.
5 changes: 4 additions & 1 deletion UPGRADING.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ splitAudioOnTier has a slightly new signature
- "noPartialIntervals" -> "allowPartialIntervals" to avoid a double negative

tgBoundariesToZeroCrossings was removed
- use the new TextgridTier.toZeroCrossings() instead
- use the new TextgridTier.toZeroCrossings() method instead

alignBoundariesAcrossTiers was removed
- use the new TextgridTier.dejitter() method instead

### interval_tier.py

Expand Down
11 changes: 8 additions & 3 deletions examples/correct_misaligned_tiers.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from os.path import join

from praatio import textgrid
from praatio import praatio_scripts

path = join(".", "files")
outputPath = join(path, "aligned-tier_textgrids")
Expand All @@ -21,5 +20,11 @@
os.mkdir(outputPath)

originalTg = textgrid.openTextgrid(inputFN, False)
tg = praatio_scripts.alignBoundariesAcrossTiers(originalTg, "word", maxDifference)
tg.save(outputFN, "short_textgrid", True)
newTg = textgrid.Textgrid()
referenceTier = originalTg.getTier("word")
for tier in originalTg.tiers:
if tier == referenceTier:
newTg.addTier(tier)
continue
newTg.addTier(tier.dejitter(referenceTier, maxDifference))
newTg.save(outputFN, "short_textgrid", True)
1 change: 0 additions & 1 deletion examples/delete_vowels.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import wave

from praatio import textgrid
from praatio import praatio_scripts
from praatio import audio
from praatio.utilities import utils

Expand Down
54 changes: 0 additions & 54 deletions praatio/praatio_scripts.py
Original file line number Diff line number Diff line change
Expand Up @@ -392,57 +392,3 @@ def _validateEntriesForWriting(nameStyle, entries, logger, outputPath):
f"Overwriting wave files in: {outputPath}\n"
f"Intervals exist with the same name:\n{instListTxt}"
)


# TODO: Remove this method in the next major version
# Migrate to using the new Textgridtier.dejitter()
def alignBoundariesAcrossTiers(
tg: textgrid.Textgrid, tierName: str, maxDifference: float = 0.005
) -> textgrid.Textgrid:
"""Aligns boundaries or points in a textgrid that suffer from 'jitter'
Often times, boundaries in different tiers are meant to line up.
For example the boundary of the first phone in a word and the start
of the word. If manually annotated, however, those values might
not be the same, even if they were intended to be the same.
This script will force all boundaries within /maxDifference/ amount
to be the same value. The replacement value is either the majority
value found within /maxDifference/ or, if no majority exists, than
the value used in the search query.
Args:
tg: the textgrid to operate on
tierName: the name of the reference tier to compare other tiers against
maxDifference: any boundaries that differ less this amount compared
to boundaries in the reference tier will be adjusted
Returns:
the provided textgrid with aligned boundaries
Raises:
ArgumentError: The provided maxDifference is larger than the smallest difference in
the tier to be used for comparisons, which could lead to strange results.
In such a case, choose a smaller maxDifference.
"""
referenceTier = tg.getTier(tierName)
times = referenceTier.timestamps

for time, nextTime in zip(times[1::], times[2::]):
if nextTime - time < maxDifference:
raise errors.ArgumentError(
"The provided maxDifference is larger than the smallest difference in"
"the tier used for comparison, which could lead to strange results."
"Please choose a smaller maxDifference.\n"
f"Max difference: {maxDifference}\n"
f"found difference {nextTime - time} for times {time} and {nextTime}"
)

for tier in tg.tiers:
if tier.name == tierName:
continue

tier = tier.dejitter(referenceTier, maxDifference)
tg.replaceTier(tier.name, tier)

return tg
76 changes: 0 additions & 76 deletions tests/test_praatio_scripts.py
Original file line number Diff line number Diff line change
Expand Up @@ -476,82 +476,6 @@ def test_split_tier_entries_raises_collision_error_when_source_and_target_tier_a
with self.assertRaises(errors.CollisionError) as _:
praatio_scripts.splitTierEntries(tg, "foo", "foo")

def test_align_boundaries_across_tiers_works_for_interval_tiers(self):
refTier = textgrid.IntervalTier("foo", [(0, 1, "hello"), (1, 2, "world")])

targetIntervalTier = textgrid.IntervalTier(
"bar", [(0, 0.5, "unchanged"), (1.001, 1.995, "changed")]
)
targetPointTier = textgrid.PointTier(
"bizz", [(0.7, "unchanged"), (1.001, "changed 1"), (1.995, "changed 2")]
)
tg = textgrid.Textgrid()
for tier in [refTier, targetIntervalTier, targetPointTier]:
tg.addTier(tier)

sut = praatio_scripts.alignBoundariesAcrossTiers(tg, "foo", maxDifference=0.01)

self.assertEqual(
[Interval(0, 1, "hello"), Interval(1, 2, "world")],
sut.getTier("foo")._entries,
)
self.assertEqual(
[Interval(0, 0.5, "unchanged"), Interval(1, 2, "changed")],
sut.getTier("bar")._entries,
)
self.assertEqual(
[Point(0.7, "unchanged"), Point(1, "changed 1"), Point(2, "changed 2")],
sut.getTier("bizz")._entries,
)

def test_align_boundaries_across_tiers_works_for_point_tiers(self):
refTier = textgrid.PointTier(
"foo", [(0, "hello"), (1, "world"), (2, "goodbye")]
)

targetIntervalTier = textgrid.IntervalTier(
"bar", [(0, 0.5, "unchanged"), (1.001, 1.995, "changed")]
)
targetPointTier = textgrid.PointTier(
"bizz", [(0.7, "unchanged"), (1.001, "changed 1"), (1.995, "changed 2")]
)
tg = textgrid.Textgrid()
for tier in [refTier, targetIntervalTier, targetPointTier]:
tg.addTier(tier)

sut = praatio_scripts.alignBoundariesAcrossTiers(tg, "foo", maxDifference=0.01)

self.assertEqual(
[Point(0, "hello"), Point(1, "world"), Point(2, "goodbye")],
sut.getTier("foo")._entries,
)
self.assertEqual(
[Interval(0, 0.5, "unchanged"), Interval(1, 2, "changed")],
sut.getTier("bar")._entries,
)
self.assertEqual(
[Point(0.7, "unchanged"), Point(1, "changed 1"), Point(2, "changed 2")],
sut.getTier("bizz")._entries,
)

def test_align_boundaries_across_tiers_raises_error_if_max_difference_is_too_small(
self,
):
# In the reference tier we have a boundary at 1 and 1.0001, which is much smaller
# than the maxDifference in this example so an exception will be thrown.
refTier = textgrid.IntervalTier("foo", [(0, 1, "hello"), (1.0001, 2, "world")])

targetTier = textgrid.IntervalTier(
"bar", [(0, 0.5, "unchanged"), (1.001, 1.995, "changed")]
)

tg = textgrid.Textgrid()
for tier in [refTier, targetTier]:
tg.addTier(tier)

with self.assertRaises(errors.ArgumentError) as _:
praatio_scripts.alignBoundariesAcrossTiers(tg, "foo", maxDifference=0.01)


if __name__ == "__main__":
unittest.main()

0 comments on commit d7dd754

Please sign in to comment.