def filter_tier(self, tier, out_tiername='Filtered'):
"""Apply the filters on the given tier.
Applicable functions are "tag", "loc" and "dur".
:param tier: (sppasTier)
:param out_tiername: (str) Name or the filtered tier
:raises: ValueError: Invalid filter or invalid value for a filter
:return: sppasTier or None if no annotation is matching
"""
self.__check_filers()
logging.info('Apply sppasTiersFilter() on tier: {:s}'.format(tier.get_name()))
ann_sets = list()
sfilter = sppasTierFilters(tier)
for f in self.__filters:
if len(f[2]) == 0:
raise ValueError('No value defined for filter {:s}'.format(f[0]))
value = sppasTierFilters.cast_data(tier, f[0], f[2][0])
logging.info(' >>> filter.{:s}({:s}={!s:s})'.format(f[0], f[1], value))
ann_set = getattr(sfilter, f[0])(**{f[1]: value})
for i in range(1, len(f[2])):
value = sppasTierFilters.cast_data(tier, f[0], f[2][i])
logging.info(' >>> | filter.{:s}({:s}={!s:s})'.format(f[0], f[1], value))
ann_set = ann_set | getattr(sfilter, f[0])(**{f[1]: value})
ann_sets.append(ann_set)
if len(ann_sets) == 0:
return None
ann_set = ann_sets[0]
if self.__match_all:
for i in range(1, len(ann_sets)):
ann_set = ann_set & ann_sets[i]
if len(ann_set) == 0:
return None
else:
for i in range(1, len(ann_sets)):
ann_set = ann_set | ann_sets[i]
filtered_tier = ann_set.to_tier(name=out_tiername, annot_value=self.__annot_format)
return filtered_tier