Skip to content

Commit

Permalink
Raise error when accessing attributes of empty ATG
Browse files Browse the repository at this point in the history
When accessing attributes like xmin of an empty ATG, the exception
raised should inform the user that they are trying to do something
meaningless rather than a cryptic numpy error.

Adds testing to this effect.
  • Loading branch information
chrisbrickhouse committed Feb 27, 2024
1 parent 3151f94 commit 1cc3365
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/aligned_textgrid/aligned_textgrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ class for each tier within each tier group. Say, if only the first speaker
The entry classes for each tier within a tier group.
tier_groups (list[TierGroup] | list[]):
A list of `TierGroup` or an empty list.
tier_names (list[str]):
A list of names for tiers in tier_groups.
xmax (float):
Maximum time
xmin (float):
Expand Down Expand Up @@ -178,8 +180,6 @@ def _init_empty(self):
self.contains = self.tier_groups
self.entry_classes = []
self.tg_tiers = None
self.xmax = 0.0
self.xmin = 0.0

def _nestify_tiers(
self,
Expand Down Expand Up @@ -276,14 +276,20 @@ def _relate_tiers(self):

@property
def tier_names(self):
if len(self) == 0:
raise ValueError('No tier names in an empty TextGrid.')
return [x.tier_names for x in self.tier_groups]

@property
def xmin(self):
if len(self) == 0:
raise ValueError('No minimum time for empty TextGrid.')
return np.array([tgroup.xmin for tgroup in self.tier_groups]).min()

@property
def xmax(self):
if len(self) == 0:
raise ValueError('No maximum time for empty TextGrid.')
return np.array([tgroup.xmax for tgroup in self.tier_groups]).max()

def interleave_class(
Expand Down
11 changes: 11 additions & 0 deletions tests/test_aligned_textgrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,10 @@ def test_get_class_by_name(self):

missing_class1 = self.atg1.get_class_by_name("Foo")
missing_class2 = self.atg2.get_class_by_name("Foo")
missing_class4 = self.atg4.get_class_by_name("Foo")
assert missing_class1 is None
assert missing_class2 is None
assert missing_class4 is None

target_classes = self.atg3.get_class_by_name("MyWord")
assert len(target_classes) > 1
Expand All @@ -158,6 +160,15 @@ def test_empty_class_indexing(self):
empty_idx_list = []
self.atg4[empty_idx_list]

def test_empty_class_attributes(self):
with pytest.raises(ValueError):
self.atg4.xmin

with pytest.raises(ValueError):
self.atg4.xmax

with pytest.raises(ValueError):
self.atg4.tier_names

class TestInGetLen:
atg = AlignedTextGrid(
Expand Down

0 comments on commit 1cc3365

Please sign in to comment.