SPPAS 4.20

Module sppas.src.annotations

Class sppasMomel

Description

SPPAS integration of Momel.

Constructor

Create a new sppasMomel instance.

Log is used for a better communication of the annotation process and its

results. If None, logs are redirected to the default logging system.

Parameters
  • log: (sppasLog) Human-readable logs.
View Source
def __init__(self, log=None):
    """Create a new sppasMomel instance.

    Log is used for a better communication of the annotation process and its
    results. If None, logs are redirected to the default logging system.

    :param log: (sppasLog) Human-readable logs.

    """
    super(sppasMomel, self).__init__('momel.json', log)
    self.__momel = Momel()
    self._options['elim_glitch'] = True

Public functions

fix_options

Fix all options.

Available options are:

  • lfen1
  • hzinf
  • hzsup
  • maxec
  • lfen2
  • seuildiff_x
  • seuildiff_y
  • glitch
Parameters
  • options: (sppasOption)
View Source
def fix_options(self, options):
    """Fix all options.

        Available options are:

            - lfen1
            - hzinf
            - hzsup
            - maxec
            - lfen2
            - seuildiff_x
            - seuildiff_y
            - glitch

        :param options: (sppasOption)

        """
    for opt in options:
        if isinstance(opt, sppasOption):
            key = opt.get_key()
            value = opt.get_value()
        else:
            key = opt
            value = options[key]
        if 'win1' == key:
            self.set_option_win1(value)
        elif 'lo' == key:
            self.set_option_lo(value)
        elif 'hi' == key:
            self.set_option_hi(value)
        elif 'maxerr' == key:
            self.set_option_maxerr(value)
        elif 'win2' == key:
            self.set_option_win2(value)
        elif 'mind' == key:
            self.set_option_mind(value)
        elif 'minr' == key:
            self.set_option_minr(value)
        elif 'elim_glitch' == key:
            self.set_option_elim_glitch(value)
        elif 'pattern' in key:
            self._options[key] = opt.get_value()
        else:
            raise AnnotationOptionError(key)
set_option_win1
View Source
def set_option_win1(self, value):
    self._options['win1'] = value
set_option_lo
View Source
def set_option_lo(self, value):
    self._options['lo'] = value
set_option_hi
View Source
def set_option_hi(self, value):
    self._options['hi'] = value
set_option_maxerr
View Source
def set_option_maxerr(self, value):
    self._options['maxerr'] = value
set_option_win2
View Source
def set_option_win2(self, value):
    self._options['win2'] = value
set_option_mind
View Source
def set_option_mind(self, value):
    self._options['mind'] = value
set_option_minr
View Source
def set_option_minr(self, value):
    self._options['minr'] = value
set_option_elim_glitch
View Source
def set_option_elim_glitch(self, value):
    self._options['elim_glitch'] = value
fix_pitch

Load pitch values from a file.

It is supposed that the given file contains a tier with name "Pitch"

with a pitch value every 10ms, or a tier with name "PitchTier".

Returns
  • A list of pitch values(one value each 10 ms).
Parameters
  • input_filename
View Source
@staticmethod
def fix_pitch(input_filename):
    """Load pitch values from a file.

        It is supposed that the given file contains a tier with name "Pitch"
        with a pitch value every 10ms, or a tier with name "PitchTier".

        :returns: A list of pitch values (one value each 10 ms).

        """
    parser = sppasTrsRW(input_filename)
    trs = parser.read()
    pitch_tier = sppasFindTier.pitch(trs)
    if pitch_tier is None:
        raise NoTierInputError
    if isinstance(trs, sppasPitchTier) is True:
        pitch_list = trs.to_pitch()
    else:
        pitch_list = [round(a.get_best_tag().get_typed_content(), 6) for a in pitch_tier]
    if len(pitch_list) == 0:
        raise EmptyInputError(name='Pitch')
    return pitch_list
estimate_momel

Estimate momel on an IPU.

Parameters
  • ipu_pitch: (list of float) Pitch values of an IPU.
  • current_time: (float) Time value of the last pitch value
Returns
  • (list of Anchor)
View Source
def estimate_momel(self, ipu_pitch, current_time):
    """Estimate momel on an IPU.

        :param ipu_pitch: (list of float) Pitch values of an IPU.
        :param current_time: (float) Time value of the last pitch value
        :returns: (list of Anchor)

        """
    self.__momel.set_option_win1(self._options['win1'])
    self.__momel.set_option_lo(self._options['lo'])
    self.__momel.set_option_hi(self._options['hi'])
    self.__momel.set_option_maxerr(self._options['maxerr'])
    self.__momel.set_option_win2(self._options['win2'])
    self.__momel.set_option_mind(self._options['mind'])
    self.__momel.set_option_minr(self._options['minr'])
    self.__momel.set_option_elim_glitch(self._options['elim_glitch'])
    ipu_start_time = current_time - len(ipu_pitch) + 1
    try:
        anchors = self.__momel.annotate(ipu_pitch)
    except Exception as e:
        self.logfile.print_message('No anchors found between time ' + str(ipu_start_time * 0.01) + ' and time ' + str(current_time * 0.01) + ': ' + str(e), indent=2, status=annots.warning)
        anchors = list()
        pass
    for i in range(len(anchors)):
        anchors[i].x += ipu_start_time
    return anchors
anchors_to_tier

Transform anchors to a sppasTier.

Anchors are stored in frames. It is converted to seconds (a frame is

during 10ms).

Parameters
  • anchors: (List of Anchor)
Returns
  • (sppasTier)
View Source
@staticmethod
def anchors_to_tier(anchors):
    """Transform anchors to a sppasTier.

        Anchors are stored in frames. It is converted to seconds (a frame is
        during 10ms).

        :param anchors: (List of Anchor)
        :returns: (sppasTier)

        """
    tier = sppasTier('Momel')
    for anchor in anchors:
        tier.create_annotation(sppasLocation(sppasPoint(anchor.x * 0.01, 0.005)), sppasLabel(sppasTag(anchor.y, 'float')))
    return tier
convert

Search for momel anchors.

Parameters
  • pitch: (list of float) pitch values samples at 10ms
Returns
  • sppasTier
View Source
def convert(self, pitch):
    """Search for momel anchors.

        :param pitch: (list of float) pitch values samples at 10ms
        :returns: sppasTier

        """
    targets = list()
    ipu_pitch = []
    nbzero = 0
    curtime = 0
    for p in pitch:
        if p == 0.0:
            nbzero += 1
        else:
            nbzero = 0
        ipu_pitch.append(p)
        if nbzero * 10 > 249:
            if len(ipu_pitch) > 0 and len(ipu_pitch) > nbzero:
                ipu_anchors = self.estimate_momel(ipu_pitch, curtime)
                targets.extend(ipu_anchors)
                ipu_pitch = list()
        curtime += 1
    ipu_anchors = self.estimate_momel(ipu_pitch, curtime)
    targets.extend(ipu_anchors)
    return sppasMomel.anchors_to_tier(targets)
run

Run the automatic annotation process on an input.

Parameters
  • input_files: (list of str) Pitch values
  • output: (str) the output name
Returns
  • (sppasTranscription)
View Source
def run(self, input_files, output=None):
    """Run the automatic annotation process on an input.

        :param input_files: (list of str) Pitch values
        :param output: (str) the output name
        :returns: (sppasTranscription)

        """
    pitch = self.fix_pitch(input_files[0])
    anchors_tier = self.convert(pitch)
    self.logfile.print_message(str(len(anchors_tier)) + ' anchors found.', indent=2, status=annots.info)
    trs_output = sppasTranscription(self.name)
    trs_output.append(anchors_tier)
    trs_output.set_meta('annotation_result_of', input_files[0])
    if output is not None:
        output_file = self.fix_out_file_ext(output)
        parser = sppasTrsRW(output_file)
        parser.write(trs_output)
        return [output_file]
    return trs_output
get_output_pattern

Pattern this annotation uses in an output filename.

View Source
def get_output_pattern(self):
    """Pattern this annotation uses in an output filename."""
    return self._options.get('outputpattern', '-momel')
get_input_extensions

Extensions that the annotation expects for its input filename.

View Source
@staticmethod
def get_input_extensions():
    """Extensions that the annotation expects for its input filename."""
    return [sppasFiles.get_informat_extensions('ANNOT_MEASURE')]