Exemplo n.º 1
0
    def test_param_not_a_sklearn_model(self):
        self.beforeTest(self.model_dictionary)
        model_wrong = "Hallo"

        with self.assertRaises(ex.PersistorException):
            mp.persist_classifier(model_wrong, config,
                                  self.current_model_type_std[0])
Exemplo n.º 2
0
    def test_for_UnpicklingError(self):
        test_config = config.copy()
        test_config[
            'datasource_classifier'] = "test_models/unpicklable_model.txt"

        with self.assertRaises(ex.InvalidConfigValueException):
            mp.persist_classifier(self.model, test_config,
                                  self.current_model_type_std[0])
Exemplo n.º 3
0
    def test_param_event_no_key(self):
        self.beforeTest(self.model_dictionary)
        wrong_key_config = copy.deepcopy(config)
        del wrong_key_config['selected_event']

        with self.assertRaises(ex.InvalidConfigKeyException):
            mp.persist_classifier(self.model, wrong_key_config,
                                  self.current_model_type_std[0])
Exemplo n.º 4
0
    def test_param_datasource_classifier_key_not_there(self):
        self.beforeTest(self.model_dictionary)
        wrong_config = copy.deepcopy(config)
        del wrong_config['datasource_classifier']

        with self.assertRaises(ex.InvalidConfigKeyException):
            mp.persist_classifier(self.model, wrong_config,
                                  self.current_model_type_std[0])
Exemplo n.º 5
0
    def test_param_datasource_classifier_empty(self):
        self.beforeTest(self.model_dictionary)
        wrong_config = copy.deepcopy(config)
        wrong_config['datasource_classifier'] = ""  # <- not valid source

        with self.assertRaises(ex.InvalidConfigValueException):
            mp.persist_classifier(self.model, wrong_config,
                                  self.current_model_type_std[0])
Exemplo n.º 6
0
 def test_current_model_type_does_not_exist(self):
     test_config = copy.deepcopy(config)
     test_config[
         'datasource_classifier'] = "test_models/model_returns_code_0.txt"
     wrong_current_model_type = "Hallo"
     with self.assertRaises(ex.InvalidConfigKeyException):
         mp.persist_classifier(self.model, test_config,
                               wrong_current_model_type)
Exemplo n.º 7
0
    def test_param_event_none_value(self):
        self.beforeTest(self.model_dictionary)
        wrong_config = copy.deepcopy(config)
        wrong_config[
            'datasource_classifier'] = 'test_models/model_returns_code_0.txt'
        del wrong_config['selected_event']

        with self.assertRaises(ex.InvalidConfigKeyException):
            mp.persist_classifier(self.model, wrong_config,
                                  self.current_model_type_std[0])
    def test_for_existing_models_in_dictionary(self):
        test_config = config.copy()
        test_config[
            'datasource_classifier'] = "test_models/model_returns_code_0.txt"
        test_config['selected_event'] = "warmwasseraufbereitung"
        mp.persist_classifier(self.model, test_config,
                              self.current_model_type_std[1])

        existing_model = mp.load_classifier(test_config,
                                            self.current_model_type_std[1])
        self.assertTrue(isinstance(existing_model, type(self.model)))
Exemplo n.º 9
0
    def test_returns_statuscode_0(self):
        self.beforeTest(self.model_dictionary)
        wrong_config = copy.deepcopy(config)
        wrong_config[
            'datasource_classifier'] = 'test_models/model_returns_code_0.txt'

        self.assertEqual(
            mp.persist_classifier(self.model, wrong_config,
                                  self.current_model_type_std[0]), 0)
def train_classifier(config):
    """
    Name in documentation: klassifizierer_trainieren()

    Train a classifier to identify a specific event.
    :param classification_config: Contains parameters for training the classifier
    :param classifier:(sklearn-object) a classification algorithm form the sklearn package
    :raises InvalidConfigValueException:Raised if a value inside of the config is wrong
    :raises  PersistorException: Raised if classifier is not an instance of sklearn
    :return int: Status code that indicates whether the training was successful(0 Success, 1 Failure)"""
    try:
        selected_event, required_score, test_size, datasource_marked_data, start_time, end_time, events = get_config_parameter(
            config)
    except Exception:
        raise exce.InvalidConfigValueException
    logger.info("config parameter loaded")
    try:
        start = convert_time(start_time)
        end = convert_time(end_time)
    except Exception as e:
        raise exce.InvalidConfigValueException(str(e))
    df = read_manager.read_query(
        datasource_marked_data,
        f"SELECT * FROM {selected_event} WHERE time >= {start}ms AND time <= {end}ms"
    )
    for event in events:
        end_start = markers[event]
        start_event = list(end_start.keys())[0]
        end_event = list(end_start.values())[len(end_start) - 1]
        if (str(df.index[0]) > start_event) or (str(df.index[-1]) < end_event):
            raise exce.ConfigException(
                'time frame of trainingsdata not in selected data frame included'
            )
        df_copy = df.copy()[start_event:end_event]

        try:
            classifier = model_persistor.load_classifier(config, event, True)
        except Exception as e:
            raise exce.PersistorException(str(e))
        logger.info("model loaded")
        df_copy.dropna(inplace=True)
        y = np.array(df_copy[event])
        for drop_event in events:
            df_copy = df_copy.drop(labels=[drop_event, f"{drop_event}_marker"],
                                   axis=1)
        X = df_copy.to_numpy()
        try:
            X_train, X_test, y_train, y_test = train_test_split(
                X, y, test_size=test_size)
        except Exception as e:
            raise exce.SklearnException(str(e))
        try:
            classifier = classifier.fit(X_train, y_train)
        except Exception as e:
            raise exce.SklearnException(str(e))
        logger.info("model trained")
        if evaluate_classifier(classifier, required_score[event], X_test,
                               y_test):
            model_persistor.persist_classifier(classifier, config, event)
            logger.info('model persisted')
        else:
            logger.info('score too low, model not persisted')
    return 0
Exemplo n.º 11
0
 def test_no_model_dictionary_in_datasource_classifier(self):
     wrong_config = copy.deepcopy(config)
     wrong_config['datasource_classifier'] = 'test_models/model_empty.txt'
     with self.assertRaises(ex.InvalidConfigValueException):
         mp.persist_classifier(self.model, wrong_config,
                               self.current_model_type_std[0])
Exemplo n.º 12
0
 def test_wrong_structure_congfig(self):
     str = "Hallo"
     with self.assertRaises(ex.ConfigTypeException):
         mp.persist_classifier(self.model, str,
                               self.current_model_type_std[0])