<commit_before># -*- coding: utf-8 -*-
#
# Copyright (c) 2013 <NAME> <<EMAIL>>

# This file is part of TimeSide.

# TimeSide is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.

# TimeSide is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with TimeSide.  If not, see <http://www.gnu.org/licenses/>.

# Author: <NAME> <<EMAIL>>
from __future__ import absolute_import
from timeside.analyzer.utils import segmentFromValues
from timeside.core import implements, interfacedoc
from timeside.analyzer.core import Analyzer
from timeside.api import IAnalyzer
from aubio import pitch
import numpy
from timeside.analyzer.preprocessors import frames_adapter


class IRITMonopoly(Analyzer):
    """
    Segmentor Monophony/Polyphony based on the analysis of yin confidence.

    Properties:
    """
    implements(IAnalyzer)

    @interfacedoc
    def setup(self, channels=None, samplerate=None,
              blocksize=None, totalframes=None):
        super(IRITMonopoly, self).setup(channels,
                                        samplerate,
                                        blocksize,
                                        totalframes)
        self.aubio_pitch = pitch(
            "default", self.input_blocksize, self.input_stepsize,
            samplerate)
        self.aubio_pitch.set_unit("freq")
        self.block_read = 0
        self.pitches = []
        self.pitch_confidences = []
        self.decisionLen = 1.0

        self.wLen = 0.1
        self.wStep = 0.05
        self.input_blocksize = int(self.wLen * samplerate)
        self.input_stepsize = int(self.wStep * samplerate)

    @staticmethod
    @interfacedoc
    def id():
        return "irit_monopoly"

    @staticmethod
    @interfacedoc
    def name():
        return "IRIT Monophony / Polyphony classification"

    @staticmethod
    @interfacedoc
    def unit():
        return ""

    def __str__(self):
        return "Labeled Monophonic/Polyphonic segments"

    @frames_adapter
    def process(self, frames, eod=False):
        # in seconds
        pf = self.aubio_pitch(frames[0])
        self.pitches += [pf[0]]
        self.pitch_confidences += [self.aubio_pitch.get_confidence()]
        self.block_read += 1
        return frames, eod

    def post_process(self):
        '''

        '''
        nb_frameDecision = int(self.decisionLen / self.wStep)
        epsilon = numpy.spacing(self.pitch_confidences[0])
        w = int(nb_frameDecision/2)

        is_mono = []
        for i in range(w, len(self.pitch_confidences) - w, nb_frameDecision):
            d = self.pitch_confidences[i - w:i + w]
            conf_mean = numpy.mean(d)
            conf_var = numpy.var(d + epsilon)
            if self.monoLikelihood(conf_mean, conf_var) > self.polyLikelihood(conf_mean, conf_var):
                is_mono += [True]
            else:
                is_mono += [False]

        conf = self.new_result(data_mode='value', time_mode='framewise')
        conf = self.new_result(data_mode='value', time_mode='framewise')
        conf.id_metadata.id += '.' + 'yin_confidence'
        conf.id_metadata.name += ' ' + 'Yin Confidence'
        conf.data_object.value = self.pitch_confidences
<commit_msg>Refactor : In Irit Monopoly, Implement aubio_pitch as a parent analyzer<commit_after># -*- coding: utf-8 -*-
#
# Copyright (c) 2013 <NAME> <<EMAIL>>

# This file is part of TimeSide.

# TimeSide is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.

# TimeSide is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with TimeSide.  If not, see <http://www.gnu.org/licenses/>.

# Author: <NAME> <<EMAIL>>
from __future__ import absolute_import
from timeside.analyzer.utils import segmentFromValues
from timeside.core import implements, interfacedoc, get_processor
from timeside.analyzer.core import Analyzer
from timeside.api import IAnalyzer
import numpy
from timeside.analyzer.preprocessors import frames_adapter


class IRITMonopoly(Analyzer):
    """
    Segmentor Monophony/Polyphony based on the analysis of yin confidence.

    Properties:
    """
    implements(IAnalyzer)

    @interfacedoc
    def __init__(self):
        super(IRITMonopoly, self).__init__()

        self.parents.append(get_processor('aubio_pitch')())

        # Irit Monopoly parameters
        self.decisionLen = 1.0
        self.wLen = 0.1
        self.wStep = 0.05

    @interfacedoc
    def setup(self, channels=None, samplerate=None,
              blocksize=None, totalframes=None):
        super(IRITMonopoly, self).setup(channels,
                                        samplerate,
                                        blocksize,
                                        totalframes)

        self.input_blocksize = int(self.wLen * samplerate)
        self.input_stepsize = int(self.wStep * samplerate)

    @staticmethod
    @interfacedoc
    def id():
        return "irit_monopoly"

    @staticmethod
    @interfacedoc
    def name():
        return "IRIT Monophony / Polyphony classification"

    @staticmethod
    @interfacedoc
    def unit():
        return ""

    def __str__(self):
        return "Labeled Monophonic/Polyphonic segments"

    @frames_adapter
    def process(self, frames, eod=False):
        return frames, eod

    def post_process(self):
        '''

        '''
        aubio_res_id = 'aubio_pitch.pitch_confidence'
        pitch_confidences = self.process_pipe.results[aubio_res_id].data

        nb_frameDecision = int(self.decisionLen / self.wStep)
        epsilon = numpy.spacing(pitch_confidences[0])
        w = int(nb_frameDecision/2)

        is_mono = []
        for i in range(w, len(pitch_confidences) - w, nb_frameDecision):
            d = pitch_confidences[i - w:i + w]
            conf_mean = numpy.mean(d)
            conf_var = numpy.var(d + epsilon)
            if self.monoLikelihood(conf_mean, conf_var) > self.polyLikelihood(conf_mean, conf_var):
                is_mono += [True]
            else:
                is_mono += [False]

        conf = self.new_result(data_mode='value', time_mode='framewise')
        conf = self.new_result(data_mode='value', time_mode='framewise')
        conf.id_metadata.id += '.' + 'yin_confidence'
        conf.id_metadata.name += ' ' + 'Yin Confidence'
        conf.data_object.value = pitch_confidences

{'fraction_non_alphanumeric': 0.09136842105263158, 'fraction_numerical': 0.004771929824561404, 'mean_word_length': 2.529470034670629, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6325897', 'n_tokens_mistral': 2163, 'n_tokens_neox': 2050, 'n_words': 568}