diff --git a/src/aligned_textgrid/aligned_textgrid.py b/src/aligned_textgrid/aligned_textgrid.py index ea8ad1a..8b2c7c4 100644 --- a/src/aligned_textgrid/aligned_textgrid.py +++ b/src/aligned_textgrid/aligned_textgrid.py @@ -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): @@ -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, @@ -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( diff --git a/tests/test_aligned_textgrid.py b/tests/test_aligned_textgrid.py index 14c19fd..fc4d3f7 100644 --- a/tests/test_aligned_textgrid.py +++ b/tests/test_aligned_textgrid.py @@ -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 @@ -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(