def test_or(self): """Test logical "or" between two data sets.""" d1 = sppasAnnSet() d2 = sppasAnnSet() d1.append(self.a1, ['contains = t']) d2.append(self.a1, ['contains = t']) res = d1 | d2 self.assertEqual(1, len(res)) self.assertEqual(1, len(res.get_value(self.a1))) d2.append(self.a1, ['contains = o']) res = d1 | d2 self.assertEqual(1, len(res)) self.assertEqual(2, len(res.get_value(self.a1))) d2.append(self.a2, ['exact = toto']) res = d1 | d2 self.assertEqual(2, len(res)) self.assertEqual(1, len(res.get_value(self.a2))) d2.append(self.a2, ['exact = toto', 'istartswith = T']) res = d1 | d2 self.assertEqual(2, len(res)) self.assertEqual(2, len(res.get_value(self.a2))) d1.append(self.a3, ['istartswith = t']) res = d1 | d2 self.assertEqual(3, len(res)) self.assertEqual(2, len(res.get_value(self.a1))) self.assertEqual(2, len(res.get_value(self.a2))) self.assertEqual(1, len(res.get_value(self.a3)))
def test_and(self): """Test logical "and" between two data sets.""" d1 = sppasAnnSet() d2 = sppasAnnSet() d1.append(self.a1, ['contains = t']) d2.append(self.a1, ['contains = o']) res = d1 & d2 self.assertEqual(1, len(res)) self.assertEqual(2, len(res.get_value(self.a1))) # nothing changed. a2 is only in d1. d1.append(self.a2, ['exact = toto']) res = d1 & d2 self.assertEqual(1, len(res)) self.assertEqual(2, len(res.get_value(self.a1))) # ok. add a2 in d2 too... d2.append(self.a2, ['exact = toto']) res = d1 & d2 self.assertEqual(2, len(res)) self.assertEqual(2, len(res.get_value(self.a1))) self.assertEqual(1, len(res.get_value(self.a2)))
def rel(self, other_tier, *args, **kwargs): """Apply functions of relations between localizations of annotations. :param other_tier: the tier to be in relation with. :param args: any sppasIntervalCompare() method. :param kwargs: any option of the methods. :returns: (sppasAnnSet) :Example: >>> f.rel(other_tier, "equals", >>> "overlaps", >>> "overlappedby", min_overlap=0.04) """ comparator = sppasIntervalCompare() # extract the information from the arguments rel_functions = sppasTierFilters.__fix_relation_functions( comparator, *args) data = sppasAnnSet() # search the annotations to be returned: for annotation in self.tier: location = annotation.get_location() match_values = sppasTierFilters.__connect(location, other_tier, rel_functions, **kwargs) if len(match_values) > 0: data.append(annotation, list(set(match_values))) return data
def loc(self, **kwargs): """Apply functions on localizations of annotations. :param kwargs: logic_bool/any sppasLocalizationCompare() method. :returns: (sppasAnnSet) :Example: >>> f.loc(rangefrom=3.01) & f.loc(rangeto=10.07) >>> f.loc(rangefrom=3.01, rangeto=10.07, logic_bool="and") """ comparator = sppasLocalizationCompare() # extract the information from the arguments sppasTierFilters.__test_args(comparator, **kwargs) logic_bool = sppasTierFilters.__fix_logic_bool(**kwargs) loc_fct_values = sppasTierFilters.__fix_function_values( comparator, **kwargs) loc_functions = sppasTierFilters.__fix_functions(comparator, **kwargs) data = sppasAnnSet() # search the annotations to be returned: for annotation in self.tier: location = annotation.get_location() is_matching = location.match_localization(loc_functions, logic_bool) if is_matching is True: data.append(annotation, loc_fct_values) return data
def dur(self, **kwargs): """Apply functions on durations of the location of annotations. :param kwargs: logic_bool/any sppasDurationCompare() method. :returns: (sppasAnnSet) Examples: >>> f.dur(ge=0.03) & f.dur(le=0.07) >>> f.dur(ge=0.03, le=0.07, logic_bool="and") """ comparator = sppasDurationCompare() # extract the information from the arguments sppasTierFilters.__test_args(comparator, **kwargs) logic_bool = sppasTierFilters.__fix_logic_bool(**kwargs) dur_fct_values = sppasTierFilters.__fix_function_values( comparator, **kwargs) dur_functions = sppasTierFilters.__fix_functions(comparator, **kwargs) data = sppasAnnSet() # search the annotations to be returned: for annotation in self.tier: location = annotation.get_location() is_matching = location.match_duration(dur_functions, logic_bool) if is_matching is True: data.append(annotation, dur_fct_values) return data
def test_copy(self): """Test the copy of a data set.""" d = sppasAnnSet() d.append(self.a1, ['contains = t', 'contains = o']) d.append(self.a2, ['exact = titi']) dc = d.copy() self.assertEqual(len(d), len(dc)) for ann in d: self.assertEqual(d.get_value(ann), dc.get_value(ann))
def test_append(self): """Append an annotation and values.""" d = sppasAnnSet() self.assertEqual(0, len(d)) d.append(self.a1, ['contains = t']) self.assertEqual(1, len(d)) self.assertEqual(1, len(d.get_value(self.a1))) # do not append the same value d.append(self.a1, ['contains = t']) self.assertEqual(1, len(d)) self.assertEqual(1, len(d.get_value(self.a1))) d.append(self.a1, ['contains = o']) self.assertEqual(1, len(d)) self.assertEqual(2, len(d.get_value(self.a1)))
def tag(self, **kwargs): """Apply functions on all tags of all labels of annotations. Each argument is made of a function name and its expected value. Each function can be prefixed with 'not_', like in the next example. :Example: >>> f.tag(startswith="pa", not_endswith='a', logic_bool="and") >>> f.tag(startswith="pa") & f.tag(not_endswith='a') >>> f.tag(startswith="pa") | f.tag(startswith="ta") :param kwargs: logic_bool/any sppasTagCompare() method. :returns: (sppasAnnSet) """ comparator = sppasTagCompare() # extract the information from the arguments sppasTierFilters.__test_args(comparator, **kwargs) logic_bool = sppasTierFilters.__fix_logic_bool(**kwargs) tag_fct_values = sppasTierFilters.__fix_function_values( comparator, **kwargs) tag_functions = sppasTierFilters.__fix_functions(comparator, **kwargs) data = sppasAnnSet() # search the annotations to be returned: for annotation in self.tier: # any label can match for label in annotation.get_labels(): is_matching = label.match(tag_functions, logic_bool) # no need to test the next labels if the current one is matching. if is_matching is True: data.append(annotation, tag_fct_values) break return data
def test_to_tier(self): """Create a tier from the data set.""" d = sppasAnnSet() d.append(self.a3, ['contains = t']) d.append(self.a1, ['contains = t']) d.append(self.a2, [ 'contains = t', 'exact = toto', ]) # with default parameters t = d.to_tier() self.assertEqual("AnnSet", t.get_name()) self.assertEqual(3, len(t)) self.assertEqual(self.a1, t[0]) self.assertEqual(self.a2, t[1]) self.assertEqual(self.a3, t[2]) # with parameters t = d.to_tier(name="Filter", annot_value=True) self.assertEqual("Filter", t.get_name()) self.assertEqual(3, len(t)) self.assertEqual(self.a1.get_location(), t[0].get_location()) self.assertEqual(self.a2.get_location(), t[1].get_location()) self.assertEqual(self.a3.get_location(), t[2].get_location()) self.assertEqual(sppasTag('contains = t'), t[0].get_labels()[0].get_best()) self.assertEqual(sppasTag('contains = t'), t[2].get_labels()[0].get_best()) self.assertEqual(sppasTag('contains = t'), t[1].get_labels()[0].get_best()) self.assertEqual(sppasTag('exact = toto'), t[1].get_labels()[1].get_best())