Пример #1
0
    def __init__(self, args, command):
        """
        :type args: any
        :type command: str
        """
        super(TestConfig, self).__init__(args, command)

        self.coverage = args.coverage  # type: bool
        self.coverage_label = args.coverage_label  # type: str
        self.include = args.include  # type: list [str]
        self.exclude = args.exclude  # type: list [str]
        self.require = args.require  # type: list [str]

        self.changed = args.changed  # type: bool
        self.tracked = args.tracked  # type: bool
        self.untracked = args.untracked  # type: bool
        self.committed = args.committed  # type: bool
        self.staged = args.staged  # type: bool
        self.unstaged = args.unstaged  # type: bool
        self.changed_from = args.changed_from  # type: str
        self.changed_path = args.changed_path  # type: list [str]

        self.lint = args.lint if 'lint' in args else False  # type: bool
        self.junit = args.junit if 'junit' in args else False  # type: bool
        self.failure_ok = args.failure_ok if 'failure_ok' in args else False  # type: bool

        self.metadata = Metadata.from_file(
            args.metadata) if args.metadata else Metadata()
        self.metadata_path = None
Пример #2
0
    def _get_pluginname_and_metadata(self, plg_section, plg_conf):
        """
        Return the actual plugin name and the metadata instance

        :param plg_conf: loaded section of the plugin.yaml for the actual plugin
        :type plg_conf: dict

        :return: plugin_name and metadata_instance
        :rtype: string, object
        """
        plugin_name = plg_conf.get('plugin_name', '').lower()
        plugin_version = plg_conf.get('plugin_version', '').lower()
        if plugin_version != '':
            plugin_version = '._pv_' + plugin_version.replace('.', '_')
        if plugin_name != '':
            meta = Metadata(self._sh, (plugin_name + plugin_version).replace('.', os.sep), 'plugin')
        else:
            classpath = plg_conf.get(KEY_CLASS_PATH, '')
            if classpath != '':
                plugin_name = classpath.split('.')[len(classpath.split('.')) - 1].lower()
                if plugin_name.startswith('_pv'):
                    plugin_name = classpath.split('.')[len(classpath.split('.')) - 2].lower()
                self.logger.debug("Plugins __init__: pluginname = '{}', classpath '{}'".format(plugin_name, classpath))
                meta = Metadata(self._sh, plugin_name, 'plugin', (classpath + plugin_version).replace('.', os.sep))
            else:
                self.logger.error(
                    "Plugin configuration section '{}': Neither 'plugin_name' nor '{}' are defined.".format(plg_section,
                                                                                                            KEY_CLASS_PATH))
                meta = Metadata(self._sh, plugin_name, 'plugin', classpath)
        return (plugin_name + plugin_version, meta)
Пример #3
0
 def _get_modulename_and_metadata(self, module, mod_conf):
     """
     Return the actual module name and the metadata instance
     
     :param mod_conf: loaded section of the module.yaml for the actual module
     :type mod_conf: dict
     
     :return: module_name and metadata_instance
     :rtype: string, object
     """
     module_name = mod_conf.get('module_name', '').lower()
     if module_name != '':
         meta = Metadata(self._sh, module_name, 'module')
     else:
         classpath = mod_conf.get(KEY_CLASS_PATH, '')
         if classpath != '':
             module_name = classpath.split('.')[len(classpath.split('.')) -
                                                1].lower()
             logger.info(
                 "Module '{}': module_name '{}' was extracted from classpath '{}'"
                 .format(module, module_name, classpath))
         else:
             logger.error(
                 "Module '{}': No module_name found in configuration".
                 format(module))
         meta = Metadata(self._sh, module_name, 'module', classpath)
     return (module_name, meta)
Пример #4
0
    def __init__(self, args, command):
        """
        :type args: any
        :type command: str
        """
        super(TestConfig, self).__init__(args, command)

        self.coverage = args.coverage  # type: bool
        self.coverage_label = args.coverage_label  # type: str
        self.coverage_check = args.coverage_check  # type: bool
        self.coverage_config_base_path = None  # type: t.Optional[str]
        self.include = args.include or []  # type: t.List[str]
        self.exclude = args.exclude or []  # type: t.List[str]
        self.require = args.require or []  # type: t.List[str]

        self.changed = args.changed  # type: bool
        self.tracked = args.tracked  # type: bool
        self.untracked = args.untracked  # type: bool
        self.committed = args.committed  # type: bool
        self.staged = args.staged  # type: bool
        self.unstaged = args.unstaged  # type: bool
        self.changed_from = args.changed_from  # type: str
        self.changed_path = args.changed_path  # type: t.List[str]

        self.lint = args.lint if 'lint' in args else False  # type: bool
        self.junit = args.junit if 'junit' in args else False  # type: bool
        self.failure_ok = args.failure_ok if 'failure_ok' in args else False  # type: bool

        self.metadata = Metadata.from_file(
            args.metadata) if args.metadata else Metadata()
        self.metadata_path = None

        if self.coverage_check:
            self.coverage = True

        def metadata_callback(
                files):  # type: (t.List[t.Tuple[str, str]]) -> None
            """Add the metadata file to the payload file list."""
            config = self

            if data_context().content.collection:
                working_path = data_context().content.collection.directory
            else:
                working_path = ''

            if self.metadata_path:
                files.append((os.path.abspath(config.metadata_path),
                              os.path.join(working_path,
                                           config.metadata_path)))

        data_context().register_payload_callback(metadata_callback)
Пример #5
0
    def __init__(self, args, command):
        """
        :type args: any
        :type command: str
        """
        super(TestConfig, self).__init__(args, command)

        self.coverage = args.coverage  # type: bool
        self.coverage_label = args.coverage_label  # type: str
        self.include = args.include  # type: list [str]
        self.exclude = args.exclude  # type: list [str]
        self.require = args.require  # type: list [str]

        self.changed = args.changed  # type: bool
        self.tracked = args.tracked  # type: bool
        self.untracked = args.untracked  # type: bool
        self.committed = args.committed  # type: bool
        self.staged = args.staged  # type: bool
        self.unstaged = args.unstaged  # type: bool
        self.changed_from = args.changed_from  # type: str
        self.changed_path = args.changed_path  # type: list [str]

        self.lint = args.lint if 'lint' in args else False  # type: bool
        self.junit = args.junit if 'junit' in args else False  # type: bool
        self.failure_ok = args.failure_ok if 'failure_ok' in args else False  # type: bool

        self.metadata = Metadata.from_file(args.metadata) if args.metadata else Metadata()
        self.metadata_path = None
Пример #6
0
def main():
    run_id = "run_" + os.environ.get(
        'USERNAME') + "_" + datetime.datetime.now().strftime("%Y%m%d_%H%M")
    metadata = Metadata('UrbanSound8K/metadata/UrbanSound8K.csv', limit=-1)
    accuracies = []
    folder_accuracy = []
    y_all = []
    predictions_all = []
    fe_params = FeatureExtractionParameters(hop_length=1024, n_mfcc=25)
    metadata.calculate_all_features(fe_params)

    def run_folder(folder):
        #get metadata train set excluding first directory
        train_set = metadata.train_set(folder)
        #get metadata test set for the selected folder
        test_set = metadata.test_set(folder)

        #extract features for each sound that belong to class c
        def forEachClass(c: SoundClass):
            return np.array(
                list(
                    map(lambda s: s.feature_extraction(fe_params),
                        tqdm(c.positive))))

        X_train = np.array(list(map(
            forEachClass, train_set)))  #extract the training set features
        print(list(map(lambda x: len(x), X_train)))
        X_train = np.concatenate(X_train, axis=0)
        y_train = np.array(
            list(map(lambda x: [x.name] * len(x.positive),
                     train_set)))  #extract the training set targets (classes)
        y_train = np.concatenate(y_train, axis=0)

        X_test = np.array(list(
            map(forEachClass,
                test_set)))  #extract the test set features for each class
        X_test = np.concatenate(X_test, axis=0)
        y_test = np.array(
            list(map(lambda x: [x.name] * len(x.positive),
                     test_set)))  #extract the test set targets (classes)
        y_test = np.concatenate(y_test, axis=0)

        #normalize the features
        scaler = skl.preprocessing.RobustScaler()
        scaler.fit(X_train)
        X_train = scaler.transform(X_train)
        X_test = scaler.transform(X_test)

        model = skl.svm.SVC(C=1, kernel='rbf')  #define the model
        model.fit(X_train, y_train)  #train the model with the training set
        predictions = model.predict(X_test)  #make predictions on test set

        acc = skl.metrics.accuracy_score(y_test,
                                         predictions)  #compute the accuracy
        accuracies.append({"acc": acc, "folder": folder})
        folder_accuracy.append(acc)
        print(f'\nAccuracy: {acc}')

        predictions_all.extend(predictions)
        y_all.extend(y_test)

        skplt.metrics.plot_confusion_matrix(
            y_test,
            predictions,
            x_tick_rotation=90,
            text_fontsize='small',
            figsize=(10, 10))  #compute the confusion matrix
        plt.savefig('runs/' + run_id + '_folder_' + str(folder) + '.png')

    for folder in range(1, 11):
        run_folder(folder)

    acc_all = skl.metrics.accuracy_score(np.array(y_all),
                                         np.array(predictions_all))
    skplt.metrics.plot_confusion_matrix(
        np.array(y_all),
        np.array(predictions_all),
        x_tick_rotation=90,
        text_fontsize='small',
        figsize=(10, 10))  #compute the confusion matrix
    plt.savefig('runs/' + run_id + '_all.png')
    with open('runs/' + run_id + '.csv', 'w', newline='') as csvfile:
        writer = csv.writer(csvfile)
        writer.writerow(['Folder', 'Accuracy'])
        writer.writerow(['Average', "{:.2f}".format(acc_all)])
        writer.writerow(['Average', "{:.2f}".format(mean(folder_accuracy))])
        for acc in accuracies:
            writer.writerow([acc['folder'], acc['acc']])
Пример #7
0
    def test_meta_is_registered(self):
        logger.warning('')
        logger.warning('=== Begin Metadata Tests:')

        self.sh = MockSmartHome()
        self.meta = Metadata(self.sh, 'test_resources', 'module', 'tests.resources.test_metadata')
        print()
        print('self.meta: {}'.format(self.meta))
        print()

        args = {}
        # (processed_args, allparams_ok) = self.meta.check_parameters(args)
        (processed_args, allparams_ok, hide_params) = self.meta.check_parameters(args)

        # Test default values for datatypes
        self.assertIsNone(processed_args.get('notype_nodefault', None))
        self.assertEqual(False, processed_args['bool_nodefault'])
        self.assertEqual(0, processed_args['int_nodefault'])
        self.assertEqual(2, processed_args['int_validmin_nodefault'])
        self.assertEqual(-42, processed_args['int_validmax_nodefault'])
        self.assertEqual(0, processed_args['pint_nodefault'])
        self.assertEqual(0, processed_args['float_nodefault'])
        self.assertEqual(0, processed_args['pfloat_nodefault'])
        self.assertEqual('', processed_args['str_nodefault'])
        self.assertEqual('', processed_args['str_validlist_nodefault'])
        self.assertEqual([], processed_args['list_nodefault'])
        self.assertEqual({}, processed_args['dict_nodefault'])
        self.assertEqual('0.0.0.0', processed_args['ip_nodefault'])
        self.assertEqual('00:00:00:00:00:00', processed_args['mac_nodefault'])
        self.assertIsNone(processed_args['foo_nodefault'])

        # Test set default values for parameters
        self.assertEqual(42, processed_args['notype_default1'])
        self.assertEqual('42', processed_args['notype_default2'])
        self.assertEqual(True, processed_args['bool_default'])
        self.assertEqual(42, processed_args['int_default'])
        self.assertEqual(42, processed_args['pint_default'])
        self.assertEqual(4.2, processed_args['float_default'])
        self.assertEqual(4.2, processed_args['pfloat_default'])
        self.assertEqual('42', processed_args['str_default'])
        self.assertEqual('string2', processed_args['str_validlist_default'])
        self.assertEqual('string1', processed_args['str_validlist_invalid_default'])
        self.assertEqual([4,2], processed_args['list_default'])
        self.assertEqual({'answer': 42}, processed_args['dict_default'])
        self.assertEqual('127.0.0.1', processed_args['ip_default'])
        self.assertEqual('01:23:45:67:89:ab', processed_args['mac_default'])
        self.assertEqual(42, processed_args['foo_default'])

        args = {
            'notype_nodefault': True, 'bool_nodefault': '42', 'int_nodefault': -24, 'pint_nodefault': 24,
            'float_nodefault': -24.2, 'pfloat_nodefault': 24.3, 'str_nodefault': 'answer', 'str_validlist_nodefault': 'string2',
            'str_validlist_default': 'x', 'str_validlist_invalid_default': 'string2',
            'list_nodefault': [24,42], 'dict_nodefault': {'question': 24, 'answer': '42'},
            'ip_nodefault': '1.2.3.4', 'mac_nodefault': 'aa:ab:ac:ad:ae:af',
            'foo_nodefault': [4, 2],
            'notype_default1': '24', 'notype_default2': 24, 'bool_default': True, 'int_default': '-x', 'pint_default': -24,
            'float_default': '-x', 'pfloat_default': -24.2, 'str_default': 25,
            'list_default': "[24,'42', 4.2, '4.2']", 'dict_default': {24, '42'},
            'ip_default': '1.2.3.256', 'mac_default': 'aa:ab:ac:ad:ae:ag',
            'foo_default': ['4', 2, [4, '2']]
        }
        # (processed_args, allparams_ok) = self.meta.check_parameters(args)
        (processed_args, allparams_ok, hide_params) = self.meta.check_parameters(args)
        logger.warning("\n=== processed_args: {}".format(processed_args))
        logger.warning("\n=== allparams_ok: {}".format(allparams_ok))
        logger.warning("\n=== hide_params: {}\n".format(hide_params))

        # Test valid parameter configurations
        #self.assertEqual(True, processed_args.get('notype_nodefault', None))
        self.assertEqual(False, processed_args['bool_nodefault'])
        self.assertEqual(-24, processed_args['int_nodefault'])
        self.assertEqual(24, processed_args['pint_nodefault'])
        self.assertEqual(-24.2, processed_args['float_nodefault'])
        self.assertEqual(24.3, processed_args['pfloat_nodefault'])
        self.assertEqual('answer', processed_args['str_nodefault'])
        self.assertEqual('string2', processed_args['str_validlist_nodefault'])
        self.assertEqual([24,42], processed_args['list_nodefault'])
        self.assertEqual({'question': 24, 'answer': '42'}, processed_args['dict_nodefault'])
        self.assertEqual('1.2.3.4', processed_args['ip_nodefault'])
        self.assertEqual('aa:ab:ac:ad:ae:af', processed_args['mac_nodefault'])
        self.assertEqual([4, 2], processed_args['foo_nodefault'])


        # Test invalid parameter configurations
        self.assertEqual('24', processed_args['notype_default1'])
        self.assertEqual(24, processed_args['notype_default2'])
        self.assertTrue(processed_args['bool_default'])
        self.assertEqual(42, processed_args['int_default'])               # default value taken (42 instead of '-x')
        self.assertEqual(0, processed_args['pint_default'])               # valid_min value taken, not default value (0 instead of -24)
        self.assertEqual(4.2, processed_args['float_default'])            # default value taken (42 instead of '-x')
        self.assertEqual(0, processed_args['pfloat_default'])             # valid_min value taken, not default value (0 instead of -24)
        self.assertEqual('25', processed_args['str_default'])             # default value not taken, because 25 can be converted to '25'
        self.assertEqual('string1', processed_args['str_validlist_default'])  # default value taken ('string1' instead of 'x')
        self.assertEqual('string2', processed_args['str_validlist_invalid_default'])
        self.assertEqual([24,'42', 4.2, '4.2'], processed_args['list_default'])       # Test list with mixed datatypes
        self.assertEqual({'answer': 42}, processed_args['dict_default'])  # default value taken ({'answer': 42} instead of invalid dict {24, '42'})
        self.assertEqual('127.0.0.1', processed_args['ip_default'])       # default value taken ('127.0.0.1' instead of invalid ip '1.2.3.256')
        self.assertEqual('01:23:45:67:89:ab', processed_args['mac_default'])  # default value taken (instead of invalid mac 'aa:ab:ac:ad:ae:ag')
        self.assertEqual(['4', 2, [4, '2']], processed_args['foo_default'])   # accepts any data (no default is taken, if a value is specified)

        logger.warning('=== End metadata Tests')
Пример #8
0
    def test_meta_is_registered(self):
        logger.warning('')
        logger.warning('=== Begin Metadata Tests:')
        
        self.sh = MockSmartHome()
        self.meta = Metadata(self.sh, 'test_resources', 'module', 'tests.resources.test_metadata')
        args = {}
        (processed_args, allparams_ok) = self.meta.check_parameters(args)
        
        # Test default values for datatypes
        self.assertIsNone(processed_args['notype_nodefault'])
        self.assertEqual(False, processed_args['bool_nodefault'])
        self.assertEqual(0, processed_args['int_nodefault'])
        self.assertEqual(2, processed_args['int_validmin_nodefault'])
        self.assertEqual(-42, processed_args['int_validmax_nodefault'])
        self.assertEqual(0, processed_args['pint_nodefault'])
        self.assertEqual(0, processed_args['float_nodefault'])
        self.assertEqual(0, processed_args['pfloat_nodefault'])
        self.assertEqual('', processed_args['str_nodefault'])
        self.assertEqual('', processed_args['str_validlist_nodefault'])
        self.assertEqual([], processed_args['list_nodefault'])
        self.assertEqual({}, processed_args['dict_nodefault'])
        self.assertEqual('0.0.0.0', processed_args['ip_nodefault'])
        self.assertEqual('00:00:00:00:00:00', processed_args['mac_nodefault'])
        self.assertIsNone(processed_args['foo_nodefault'])

        # Test set default values for parameters
        self.assertEqual(42, processed_args['notype_default1'])
        self.assertEqual('42', processed_args['notype_default2'])
        self.assertEqual(True, processed_args['bool_default'])
        self.assertEqual(42, processed_args['int_default'])
        self.assertEqual(42, processed_args['pint_default'])
        self.assertEqual(4.2, processed_args['float_default'])
        self.assertEqual(4.2, processed_args['pfloat_default'])
        self.assertEqual('42', processed_args['str_default'])
        self.assertEqual('string2', processed_args['str_validlist_default'])
        self.assertEqual('string1', processed_args['str_validlist_invalid_default'])
        self.assertEqual([4,2], processed_args['list_default'])
        self.assertEqual({'answer': 42}, processed_args['dict_default'])
        self.assertEqual('127.0.0.1', processed_args['ip_default'])
        self.assertEqual('01:23:45:67:89:ab', processed_args['mac_default'])
        self.assertEqual(42, processed_args['foo_default'])

        args = {
            'notype_nodefault': True, 'bool_nodefault': '42', 'int_nodefault': -24, 'pint_nodefault': 24, 
            'float_nodefault': -24.2, 'pfloat_nodefault': 24.3, 'str_nodefault': 'answer', 'str_validlist_nodefault': 'string2',
            'str_validlist_default': 'x', 'str_validlist_invalid_default': 'string2',
            'list_nodefault': [24,42], 'dict_nodefault': {'question': 24, 'answer': '42'}, 
            'ip_nodefault': '1.2.3.4', 'mac_nodefault': 'aa:ab:ac:ad:ae:af',
            'foo_nodefault': [4, 2], 
            'notype_default1': '24', 'notype_default2': 24, 'bool_default': True, 'int_default': '-x', 'pint_default': -24, 
            'float_default': '-x', 'pfloat_default': -24.2, 'str_default': 25,
            'list_default': "[24,'42', 4.2, '4.2']", 'dict_default': {24, '42'}, 
            'ip_default': '1.2.3.256', 'mac_default': 'aa:ab:ac:ad:ae:ag',
            'foo_default': ['4', 2, [4, '2']] 
        }
        (processed_args, allparams_ok) = self.meta.check_parameters(args)
        
        # Test valid parameter configurations
        self.assertEqual(True, processed_args['notype_nodefault'])
        self.assertEqual(False, processed_args['bool_nodefault'])
        self.assertEqual(-24, processed_args['int_nodefault'])
        self.assertEqual(24, processed_args['pint_nodefault'])
        self.assertEqual(-24.2, processed_args['float_nodefault'])
        self.assertEqual(24.3, processed_args['pfloat_nodefault'])
        self.assertEqual('answer', processed_args['str_nodefault'])
        self.assertEqual('string2', processed_args['str_validlist_nodefault'])
        self.assertEqual([24,42], processed_args['list_nodefault'])
        self.assertEqual({'question': 24, 'answer': '42'}, processed_args['dict_nodefault'])
        self.assertEqual('1.2.3.4', processed_args['ip_nodefault'])
        self.assertEqual('aa:ab:ac:ad:ae:af', processed_args['mac_nodefault'])
        self.assertEqual([4, 2], processed_args['foo_nodefault'])


        # Test invalid parameter configurations
        self.assertEqual('24', processed_args['notype_default1'])
        self.assertEqual(24, processed_args['notype_default2'])
        self.assertTrue(processed_args['bool_default'])
        self.assertEqual(42, processed_args['int_default'])               # default value taken (42 instead of '-x')
        self.assertEqual(0, processed_args['pint_default'])               # valid_min value taken, not default value (0 instead of -24)
        self.assertEqual(4.2, processed_args['float_default'])            # default value taken (42 instead of '-x')
        self.assertEqual(0, processed_args['pfloat_default'])             # valid_min value taken, not default value (0 instead of -24)
        self.assertEqual('25', processed_args['str_default'])             # default value not taken, because 25 can be converted to '25'
        self.assertEqual('string1', processed_args['str_validlist_default'])  # default value taken ('string1' instead of 'x')
        self.assertEqual('string2', processed_args['str_validlist_invalid_default'])
        self.assertEqual([24,'42', 4.2, '4.2'], processed_args['list_default'])       # Test list with mixed datatypes
        self.assertEqual({'answer': 42}, processed_args['dict_default'])  # default value taken ({'answer': 42} instead of invalid dict {24, '42'})
        self.assertEqual('127.0.0.1', processed_args['ip_default'])       # default value taken ('127.0.0.1' instead of invalid ip '1.2.3.256')
        self.assertEqual('01:23:45:67:89:ab', processed_args['mac_default'])  # default value taken (instead of invalid mac 'aa:ab:ac:ad:ae:ag')
        self.assertEqual(['4', 2, [4, '2']], processed_args['foo_default'])   # accepts any data (no default is taken, if a value is specified)

        logger.warning('=== End metadata Tests')