Пример #1
0
def inference(
        input_params: List[int],
        code_lines_dict,
        args):
    """
    Find a pattern which has the largest impact on target

    :param input_params: list if calculated patterns/metrics
    :param code_lines_dict: list with found code lines of patterns/metrics
    :param file: filename

    :return: list of results with code_lies for each pattern and its name
    """
    model_path = args.model
    do_full_report = args.full
    results = []
    if input_params:
        if not model_path:
            model_path = Config.folder_model_data()
        with open(model_path, 'rb') as fid:
            model = pickle.load(fid)
        sorted_result, importances = model.predict(input_params)
        patterns_list = model.features_conf['features_order']
        for iter, (key, val) in enumerate(sorted_result.items()):
            if key in patterns_list:
                pattern_code = key
                code_lines = code_lines_dict.get('lines_' + key)
                importance = importances[iter] * input_params[pattern_code]

                # We show only patterns with positive importance
                if code_lines and importance > 0:
                    if code_lines:
                        pattern_name = \
                            [x['name'] for x in Config.get_patterns_config()['patterns']
                             if x['code'] == pattern_code][0]
                        results.append(
                            {'code_lines': code_lines,
                             'pattern_code': pattern_code,
                             'pattern_name': pattern_name,
                             'importance': importance
                             })
                    if not do_full_report:
                        break
    else:
        return results

    return results
Пример #2
0
    def aibolit_stat(test_csv: pd.DataFrame, model=None) -> pd.DataFrame:
        if not model:
            load_model_file = Config.folder_model_data()
            print('Loading model from file {}:'.format(load_model_file))
            with open(load_model_file, 'rb') as fid:
                model = pickle.load(fid)
                print('Model has been loaded successfully')

        scaled_dataset = scale_dataset(test_csv, model.features_conf)
        cleaned_dataset = scaled_dataset[model.features_conf['features_order']
                                         + ['M2']]
        ranked, _, acts_complexity, acts = Stats.check_impact(
            cleaned_dataset.values, model)

        m, p = Stats.count_acts(acts, ranked)
        return Stats.get_table(model.features_conf['features_order'], m, p,
                               acts_complexity)
Пример #3
0
def inference(input_params: List[int], code_lines_dict, args):
    """
    Find a pattern which has the largest impact on target

    :param input_params: list if calculated patterns/metrics
    :param code_lines_dict: list with found code lines of patterns/metrics
    :return:
    """
    model_path = args.model_file
    if input_params:
        if not model_path:
            model_path = Config.folder_model_data()
        with open(model_path, 'rb') as fid:
            model = pickle.load(fid)
        sorted_result = predict(input_params, model, args)
        code_lines = None
        patterns_list = model.features_conf['patterns_only']
        pattern_code = None  # type: ignore
        for iter, (key, val) in enumerate(sorted_result.items()):
            if key in patterns_list:
                pattern_code = key
                code_lines = code_lines_dict.get('lines_' + key)
                # We show only positive gradient, we won't add patterns
                if code_lines and val > 1.00000e-20:
                    break

        if code_lines:
            pattern_name = [
                x['name'] for x in Config.get_patterns_config()['patterns']
                if x['code'] == pattern_code
            ][0]
        else:
            pattern_name = None
            pattern_code = None
            code_lines = []
    else:
        code_lines = []
        pattern_code = None  # type: ignore
        pattern_name = None

    return code_lines, pattern_code, pattern_name