SPPAS 4.22

https://sppas.org/

Module sppas.src.analysis

Class SingleFilterTier

Description

This class applies predefined filters on a tier.

Apply defined filters, as a list of tuples with: - name of the filter: one of "tag", "loc", "dur", "nlab", "rel" - name of the function in sppasCompare (equal, lt, ...) - value of its expected type (str, float, int, bool)

Constructor

Filter process of a tier.

Parameters
  • filters: (list) List of tuples (filter, function, [typed values])
  • annot_format: (bool) The annotation result contains the name of the filter (if True) or the original label (if False)
  • match_all: (bool) The annotations must match all the filters (il set to True) or any of them (if set to False)
View Source
def __init__(self, filters, annot_format=False, match_all=True):
    """Filter process of a tier.

    :param filters: (list) List of tuples (filter, function, [typed values])
    :param annot_format: (bool) The annotation result contains the
    name of the filter (if True) or the original label (if False)
    :param match_all: (bool) The annotations must match all the filters
    (il set to True) or any of them (if set to False)

    """
    self.__filters = filters
    self.__match_all = bool(match_all)
    self.__annot_format = bool(annot_format)

Public functions

filter_tier

Apply the filters on the given tier.

Applicable functions are "tag", "loc" and "dur".

Parameters
  • tier: (sppasTier)
  • out_tiername: (str) Name or the filtered tier
Raises
  • ValueError: Invalid filter or invalid value for a filter
Returns
  • sppasTier or None if no annotation is matching
View Source
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

Protected functions

__check_filers

Check the actual filters.

View Source
def __check_filers(self):
    """Check the actual filters."""
    for f in self.__filters:
        if f[0] not in SingleFilterTier.functions:
            raise ValueError("{:s} is not a Single Filter and can't be applied".format(f[0]))