<commit_before>#! /usr/bin/env python

# Author : <NAME> <<EMAIL>>
from unit_timeside import unittest, TestRunner
import numpy as np

import timeside.core
from timeside.plugins.decoder.file import FileDecoder
from timeside.core.tools.test_samples import samples
from jsonschema import Draft4Validator


class TestAnalyzers_parameters(unittest.TestCase):
    """Test analyzer parameters and schema specification"""

    def _perform_test(self, analyzer_cls):
        """Internal function that test analyzer schema and default parameters"""

        analyzer = analyzer_cls()

        self.assertEqual(analyzer.get_parameters(),
                         analyzer.get_parameters_default_from_argspec())

        analyzer.check_schema()


class TestAnalyzers_with_default(unittest.TestCase):
    """Test analyzer with default parameters"""

    def setUp(self):
        source = samples["C4_scale.wav"]

        self.decoder = FileDecoder(source)

    def _perform_test(self, analyzer_cls):
        """Internal function that test if there is NaN in the results
        of a given analyzer"""

        analyzer = analyzer_cls()
        pipe = (self.decoder | analyzer)
        pipe.run()
        for key, result in analyzer.results.items():
            if 'value' in result.data_object.keys():
                # Test for NaN
                self.assertFalse(np.any(np.isnan(result.data)),
                                 'NaN in %s data value' % result.name)
                # Test for Inf
                self.assertFalse(np.any(np.isinf(result.data)),
                                 'Inf in %s data value' % result.name)


def _tests_factory(test_class, test_doc, list_analyzers, skip_reasons={}):
    """Define a test for each analyzer provided in the list"""
    for analyzer in list_analyzers:

        def test_func_factory(analyzer):
            test_func = lambda self: self._perform_test(analyzer)
            test_func.__doc__ = test_doc % analyzer.__name__
            return test_func

        test_func_name = "test_%s" % analyzer.__name__
        test_func = test_func_factory(analyzer)

        if analyzer.__name__ not in skip_reasons:<commit_msg>Test HDF5 serialization for all analyzers<commit_after>#! /usr/bin/env python

# Author : <NAME> <<EMAIL>>
from unit_timeside import unittest, TestRunner
import numpy as np

import timeside.core
from timeside.plugins.decoder.file import FileDecoder
from timeside.core.tools.test_samples import samples
from jsonschema import Draft4Validator

import tempfile

class TestAnalyzers_parameters(unittest.TestCase):
    """Test analyzer parameters and schema specification"""

    def _perform_test(self, analyzer_cls):
        """Internal function that test analyzer schema and default parameters"""

        analyzer = analyzer_cls()

        self.assertEqual(analyzer.get_parameters(),
                         analyzer.get_parameters_default_from_argspec())

        analyzer.check_schema()


class TestAnalyzers_with_default(unittest.TestCase):
    """Test analyzer with default parameters"""

    def setUp(self):
        source = samples["C4_scale.wav"]

        self.decoder = FileDecoder(source)

    def _perform_test(self, analyzer_cls):
        """Internal function that test if there is NaN in the results
        of a given analyzer"""

        analyzer = analyzer_cls()
        pipe = (self.decoder | analyzer)
        pipe.run()
        for key, result in analyzer.results.items():
            if 'value' in result.data_object.keys():
                # Test for NaN
                self.assertFalse(np.any(np.isnan(result.data)),
                                 'NaN in %s data value' % result.name)
                # Test for Inf
                self.assertFalse(np.any(np.isinf(result.data)),
                                 'Inf in %s data value' % result.name)
        # Try to serialize as hdf5
        h5_file = tempfile.NamedTemporaryFile(suffix='.h5', delete=True)
        analyzer.results.to_hdf5(h5_file.name)

def _tests_factory(test_class, test_doc, list_analyzers, skip_reasons={}):
    """Define a test for each analyzer provided in the list"""
    for analyzer in list_analyzers:

        def test_func_factory(analyzer):
            test_func = lambda self: self._perform_test(analyzer)
            test_func.__doc__ = test_doc % analyzer.__name__
            return test_func

        test_func_name = "test_%s" % analyzer.__name__
        test_func = test_func_factory(analyzer)

        if analyzer.__name__ not in skip_reasons:
{'fraction_non_alphanumeric': 0.08761061946902655, 'fraction_numerical': 0.0022123893805309734, 'mean_word_length': 2.6607287449392714, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2953169', 'n_tokens_mistral': 1305, 'n_tokens_neox': 1221, 'n_words': 335}