Example #1
0
    def __init__(self, recipe=None, hop=1, **kwargs):
        """Constructor

        Parameters
        ----------
        recipe : dict or str
            Stacking recipe
            Default value None

        hop : int, optional
            Data item hopping
            Default value 1

        """

        # Run super init to call init of mixins too
        super(Stacker, self).__init__(**kwargs)

        if isinstance(recipe, str):
            from dcase_util.utils import VectorRecipeParser
            self.recipe = VectorRecipeParser().parse(recipe=recipe)

        elif isinstance(recipe, list):
            self.recipe = recipe

        else:
            message = '{name}: No recipe set'.format(
                name=self.__class__.__name__)
            self.logger.exception(message)
            raise ValueError(message)

        self.hop = hop
Example #2
0
    def __init__(self,
                 win_length_frames=10,
                 hop_length_frames=1,
                 recipe=None,
                 center=True,
                 padding=True,
                 **kwargs):
        """Constructor

        Parameters
        ----------
        recipe : list of dict or list of str
            Aggregation recipe, supported methods [mean, std, cov, kurtosis, skew, flatten].
            Default value None

        win_length_frames : int
            Window length in data frames
            Default value 10

        hop_length_frames : int
            Hop length in data frames
            Default value 1

        center : bool
            Centering of the window
            Default value True

        padding : bool
            Padding of the first window with the first frame and last window with last frame to have equal
            length data in the windows.
            Default value True

        """

        # Run super init to call init of mixins too
        super(Aggregator, self).__init__(**kwargs)

        self.win_length_frames = win_length_frames
        self.hop_length_frames = hop_length_frames
        self.center = center
        self.padding = padding

        if recipe is None and kwargs.get('aggregation_recipe',
                                         None) is not None:
            recipe = kwargs.get('aggregation_recipe', None)

        if isinstance(recipe, dict):
            self.recipe = [d['label'] for d in recipe]

        elif isinstance(recipe, list):
            recipe = recipe

            if isinstance(recipe[0], dict):
                self.recipe = [d['label'] for d in recipe]

            else:
                self.recipe = recipe

        elif isinstance(recipe, six.string_types):
            recipe = VectorRecipeParser().parse(recipe=recipe)
            self.recipe = [d['label'] for d in recipe]

        else:
            message = '{name}: No valid recipe set'.format(
                name=self.__class__.__name__)
            self.logger.exception(message)
            raise ValueError(message)
def test_parse():
    parser = VectorRecipeParser()

    # Test #1
    test_recipe = 'mel'

    parsed_recipe = parser.parse(recipe=test_recipe)

    # correct amount of items
    nose.tools.eq_(len(parsed_recipe), 1)

    # method is correct
    nose.tools.eq_(parsed_recipe[0]['label'], 'mel')

    # Test #2
    test_recipe = 'mel=0;mfcc=1'
    parsed_recipe = parser.parse(recipe=test_recipe)

    # correct amount of items
    nose.tools.eq_(len(parsed_recipe), 2)

    # methods are correct
    nose.tools.eq_(parsed_recipe[0]['label'], 'mel')
    nose.tools.eq_(parsed_recipe[1]['label'], 'mfcc')

    # vector-index is correct / channel
    nose.tools.eq_(parsed_recipe[0]['vector-index']['stream'], 0)
    nose.tools.eq_(parsed_recipe[1]['vector-index']['stream'], 1)
    nose.tools.eq_(parsed_recipe[0]['vector-index']['full'], True)
    nose.tools.eq_(parsed_recipe[1]['vector-index']['full'], True)
    nose.tools.eq_(parsed_recipe[0]['vector-index']['selection'], False)
    nose.tools.eq_(parsed_recipe[1]['vector-index']['selection'], False)

    # Test #3
    test_recipe = 'mel=1-20'
    parsed_recipe = parser.parse(recipe=test_recipe)

    # correct amount of items
    nose.tools.eq_(len(parsed_recipe), 1)

    # method is correct
    nose.tools.eq_(parsed_recipe[0]['label'], 'mel')

    # vector-index is correct / channel
    nose.tools.eq_(parsed_recipe[0]['vector-index']['stream'], 0)
    nose.tools.eq_(parsed_recipe[0]['vector-index']['full'], False)
    nose.tools.eq_(parsed_recipe[0]['vector-index']['selection'], False)
    nose.tools.eq_(parsed_recipe[0]['vector-index']['start'], 1)
    nose.tools.eq_(parsed_recipe[0]['vector-index']['stop'], 21)

    # Test #4
    test_recipe = 'mel=1,2,4,5'
    parsed_recipe = parser.parse(recipe=test_recipe)

    # correct amount of items
    nose.tools.eq_(len(parsed_recipe), 1)

    # extractor is correct
    nose.tools.eq_(parsed_recipe[0]['label'], 'mel')

    # vector-index is correct / channel
    nose.tools.eq_(parsed_recipe[0]['vector-index']['stream'], 0)
    nose.tools.eq_(parsed_recipe[0]['vector-index']['full'], False)
    nose.tools.eq_(parsed_recipe[0]['vector-index']['selection'], True)
    nose.tools.assert_list_equal(parsed_recipe[0]['vector-index']['vector'],
                                 [1, 2, 4, 5])

    # Test #5
    test_recipe = 'mel=1:1-20'
    parsed_recipe = parser.parse(recipe=test_recipe)

    # correct amount of items
    nose.tools.eq_(len(parsed_recipe), 1)

    # method is correct
    nose.tools.eq_(parsed_recipe[0]['label'], 'mel')

    # vector-index is correct / channel
    nose.tools.eq_(parsed_recipe[0]['vector-index']['stream'], 1)
    nose.tools.eq_(parsed_recipe[0]['vector-index']['full'], False)
    nose.tools.eq_(parsed_recipe[0]['vector-index']['selection'], False)
    nose.tools.eq_(parsed_recipe[0]['vector-index']['start'], 1)
    nose.tools.eq_(parsed_recipe[0]['vector-index']['stop'], 21)