def pack_model_cvs(packaging_parameters):

    successful_classifiers = packaging_parameters[0]
    classifiers, cbin_train, ux_train, cbin_test, ux_test, t_norm, freq_classes = packaging_parameters[
        1]

    num_classes = cbin_train.shape[1]
    indexes_premises_byclass = []
    unique_indexes_byclass = []
    for i in range(num_classes):
        container_aux = []
        for j in classifiers:
            container_aux.append(j[i][0])

        list_premises_container = list(chain(*container_aux))
        unique_indexes = list(set(list_premises_container))
        unique_indexes_byclass.append(unique_indexes)
        unique_premises = toolfis.calculation_premises(unique_indexes,
                                                       ux_train, t_norm)
        indexes_premises_byclass.append([unique_indexes, unique_premises])

    exito, output_collective = toolfis.classifiers_aggregation(
        indexes_premises_byclass, cbin_train, 'MQR', freq_classes)

    if not exito:
        report, metrics = ["Final aggregation was fail"], [0]
    else:
        premises_weights_names, estimation_classes = output_collective

        final_premises_classes = []
        for i in range(len(premises_weights_names)):  # x cada clase
            final_premises_classes.append(premises_weights_names[i][0])

        f6 = Evaluation(premises_weights_names, final_premises_classes,
                        freq_classes)
        metrics_train = f6.eval_train(cbin_train, estimation_classes)
        metrics_test = f6.eval_test(cbin_test, ux_test, t_norm)

        results_cv = summary_cv_bagfis(successful_classifiers, metrics_train,
                                       metrics_test)
        report = toolfis.template_results(results_cv)

        metrics = [
            1,
            [
                metrics_train[0], metrics_test[0], metrics_train[1],
                metrics_test[1], metrics_test[4]
            ]
        ]
    return report, metrics
Example #2
0
def main():

    directory = os.getcwd()
    os.chdir(f'{directory}/2019-01-31_06-42-36/15')
    for filename in glob.glob('Report*.txt'):

        with open(filename, 'r') as file_tmp:

            suc_classifiers, ac_train, auc_train, ac_test, auc_test, num_rules, rule_len = [], [], [], [], [], [], []
            for line in file_tmp.read().splitlines():

                if "Successful classifiers: " in line:
                    suc_classifiers.append(
                        re.search(r'\[(.*?)\]', line).group(1))
                elif "Accuracy training: " in line:
                    ac_train.append(float(re.findall("\d+\.\d+", line)[0]))
                elif "AUC Training: " in line:
                    if len(re.findall("\d+\.\d+", line)) == 0:
                        auc_train.append(0)
                    else:
                        auc_train.append(float(
                            re.findall("\d+\.\d+", line)[0]))
                elif "Accuracy Testing: " in line:
                    ac_test.append(float(re.findall("\d+\.\d+", line)[0]))
                elif "AUC Testing: " in line:
                    if len(re.findall("\d+\.\d+", line)) == 0:
                        auc_test.append(0)
                    else:
                        auc_test.append(float(re.findall("\d+\.\d+", line)[0]))
                elif "Number of Rules: " in line:
                    num_rules.append(int(re.findall("\d+", line)[0]))
                elif "Total Rule Length: " in line:
                    rule_len.append(int(re.findall("\d+", line)[0]))

            results = toolfis.summary_multiple(suc_classifiers[1],
                                               [ac_train, auc_train],
                                               [ac_test, auc_test],
                                               [num_rules, rule_len])

        abstract = toolfis.template_results('Summary_CVs\n    -----------',
                                            results)
        underline = "\n%s\n" % (110 * "=")

        file = open(filename, 'a+')
        file.write(underline)
        file.write(abstract)

        file.close()
Example #3
0
def BagFISonecv(zipFilePath, file_train, file_test, parameters,
                num_classifiers):
    # General parameters
    t_norm = parameters[3]
    max_size_of_premise = parameters[5]
    association_method = parameters[11]
    aggregation_method = parameters[12]

    # Gathering parameters
    # Formulation parameters:
    par_area, par_over, par_pcd = toolfis.get_formulation_parameters(
        parameters)

    # 1. Lecture & Fuzzification
    out1 = toolfis.lecture_fuz_one_cv(zipFilePath, file_train, file_test,
                                      parameters)
    ux_train, cbin_train = out1[0]
    ux_test, cbin_test = out1[1]
    sizes_attributes, premises_by_attribute, ref_attributes, premises_contain_negation = out1[
        2]
    freq_classes = out1[3]

    outputs_trees = []
    pars = [
        max_size_of_premise, t_norm, par_area, par_over, par_pcd,
        association_method, aggregation_method, freq_classes
    ]
    classifiers = []
    successful_classifiers = 0

    # ================================= GENERATION CLASSIFIERS =========================================================

    for i in range(num_classifiers):
        new_data, genesis_data = toolfis.create_data(
            ref_attributes, sizes_attributes, premises_by_attribute,
            premises_contain_negation, ux_train, cbin_train)
        exit_flag, salida = toolfis.inference_fuzzy(new_data, pars)
        # salida: [[premisesFinales, weights, method], [Train_prediction]]

        if exit_flag:
            successful_classifiers += 1
            outputs_trees.append(salida[1])  # Output Train

            # Transformation premises relative2absolute:
            converter = dict(zip(range(len(genesis_data[0])), genesis_data[0]))
            absolute_models = []
            for j in salida[0]:
                relative_premises = j[0]
                absolute_premises = toolfis.relative2absolute(
                    relative_premises, converter)
                absolute_models.append(
                    [absolute_premises, j[1],
                     j[2]])  # premises absolutes, Weights, name_method
            classifiers.append(absolute_models)

    # ======================================= PACKING IN ONE MODEL =================================================

    if not classifiers:
        return ["Any of %i classifiers was successful" % num_classifiers], [0]

    num_classes = cbin_train.shape[1]
    indexes_premises_byclass = []
    unique_indexes_byclass = []
    for i in range(num_classes):
        container_aux = []
        for j in classifiers:
            container_aux.append(j[i][0])

        list_premises_container = list(chain(*container_aux))
        unique_indexes = list(set(list_premises_container))
        unique_indexes_byclass.append(unique_indexes)
        unique_premises = toolfis.calculation_premises(unique_indexes,
                                                       ux_train, t_norm)
        indexes_premises_byclass.append([unique_indexes, unique_premises])

    exito, output_collective = toolfis.classifiers_aggregation(
        indexes_premises_byclass, cbin_train, 'MQR', freq_classes)

    if not exito:
        report, metrics = ["Final aggregation was fail"], [0]
    else:
        premises_weights_names, estimation_classes = output_collective

        final_premises_classes = []
        for i in range(len(premises_weights_names)):  # x cada clase
            final_premises_classes.append(premises_weights_names[i][0])

        f6 = Evaluation(premises_weights_names, final_premises_classes,
                        freq_classes)
        metrics_train = f6.eval_train(cbin_train, estimation_classes)
        metrics_test = f6.eval_test(cbin_test, ux_test, t_norm)

        results_cv = summary_cv_bagfis(successful_classifiers, metrics_train,
                                       metrics_test)
        report = toolfis.template_results(results_cv)

        metrics = [
            1,
            [
                metrics_train[0], metrics_test[0], metrics_train[1],
                metrics_test[1], metrics_test[4]
            ], successful_classifiers
        ]

    return report, metrics
Example #4
0
def BagFISonezip(root, zip_file_name, parameters, num_classifiers,
                 report_folder):
    zipFilePath = os.path.join(root, zip_file_name)

    try:
        with zipfile.ZipFile(zipFilePath, 'r') as z:
            files_cv = z.namelist()

        number_files_zip = len(files_cv)
        if not (number_files_zip == 20 or number_files_zip == 10
                or number_files_zip == 2):
            raise ValueError(
                "This module works with a zip file to 10cv or 5cv. "
                "For this reason, it is expected 20 or 10 files inside the zip file"
            )
        number_cv_pairs = number_files_zip / 2
        list_train, list_test = packing_train_test_files(files_cv)

        msg = []
        ac_train, ac_test, auc_train, auc_test, num_rules, rule_len, suc_classifiers = container_results(
            number_cv_pairs)

        for i in range(number_cv_pairs):
            train_file = list_train[i]
            test_file = list_test[i]

            message, indicators = BagFISonecv(zipFilePath, train_file,
                                              test_file, parameters,
                                              num_classifiers)
            msg.append(message)

            if indicators[0] == 0:
                toolfis.create_error_file(root, zip_file_name, message, i)
                raise ValueError("Problem detected in CV " + str(i + 1))

            ac_train[i], ac_test[i] = indicators[1][0], indicators[1][1]
            auc_train[i], auc_test[i] = indicators[1][2], indicators[1][3]
            num_rules[i] = indicators[1][4][0]
            rule_len[i] = indicators[1][4][1]
            suc_classifiers[i] = indicators[2]

        filename_report = os.path.join(report_folder,
                                       'Report of ' + zip_file_name[:-8])
        target = open(filename_report, 'w')
        summary_parameters = 'Parameters: %s' % str(parameters)
        underline = "\n%s\n" % (len(summary_parameters) * "=")

        cvs_info = [summary_parameters]

        for i in range(number_cv_pairs):
            cv_summary = """    CV-{indice}:
            {cvv}""".format(indice=i + 1, cvv=msg[i])
            cvs_info.append(cv_summary)

        results = toolfis.summary_multiple(mean(suc_classifiers),
                                           [ac_train, auc_train],
                                           [ac_test, auc_test],
                                           [num_rules, rule_len])
        abstract = toolfis.template_results(results)
        cvs_info.append(abstract)
        target.write(underline.join(cvs_info))
        target.close()

        achievement = 1
        print "win ", zip_file_name

    except ValueError as e:
        print e
        achievement, ac_test, num_rules, suc_classifiers = 0, 0, 0, 0

    return achievement, (100 * mean(ac_test), 100 * std(ac_test),
                         mean(num_rules), mean(suc_classifiers))
Example #5
0
def random_fis_one_cv(zipFilePath, file_train, file_test,
                      parameters_classifiers, cv_i, clf_n, folder__tree_output,
                      time_ini):

    # General parameters
    p_method_agg = GlobalParameter()
    method_aggreg_classf = p_method_agg.method_aggregation
    cv_i += 1
    # print ('Clf = %i, CV = %i' % (clf_n, cv_i))
    successful_classifiers = 0
    classifiers = []
    outputs_tree_train_bin = []
    outputs_tree_test_bin = []
    outputs_tree_train = []
    outputs_tree_test = []
    partial_metrics_rules = []
    container_ac_train = []
    container_train = []
    container_ac_test = []
    container_test = []
    parameters = [parameters_classifiers[0]] + parameters_classifiers[1]
    t_norm = parameters[3]
    max_size_of_premise = parameters[5]
    association_method = parameters[11]
    aggregation_method = parameters[12]
    aux_blb = []

    # Gathering parameters
    # Formulation parameters:
    par_area, par_over, par_pcd = toolfis.get_formulation_parameters(
        parameters)
    p_blb = GlobalParameter()

    # 0. Divide data
    train_clt, test_clt = toolfis.divide_data(zipFilePath, file_train,
                                              file_test)
    matrix_train_clt = np.array(train_clt)
    matrix_test_clt = np.array(test_clt)

    for nc in range(matrix_train_clt.shape[0]):

        # 1. Lecture & Fuzzification
        out1 = toolfis.lecture_fuz_one_cv_v2(matrix_train_clt[nc],
                                             matrix_test_clt[nc], parameters)
        ux_train, cbin_train = out1[0]
        ux_test, cbin_test = out1[1]
        sizes_attributes, premises_by_attribute, ref_attributes, premises_contain_negation = out1[
            2]
        freq_classes = out1[3]

        pars = [
            max_size_of_premise, t_norm, par_area, par_over, par_pcd,
            association_method, aggregation_method, freq_classes
        ]

        # ===== Classifiers =====
        i_blb = 0
        classifiers_blb = []
        n_blb = list(range(p_blb.blb))
        for blb_i in n_blb:
            i_blb += 1

            new_data_blb, genesis_data_blb = toolfis.create_data_v2(
                ref_attributes, sizes_attributes, premises_by_attribute,
                premises_contain_negation, ux_train, cbin_train)

            exit_flag_blb, out_model_blb = toolfis.inference_fuzzy\
                (new_data_blb, pars, info=(str(i_blb), str(nc)), ensemble='RandomFIS')
            #  out_model = [premises_weights_names, train_bin_prediction, estimation_classes(u_estimation)]

            if exit_flag_blb:
                # successful_classifiers += 1
                #  Transformation premises relative2absolute:
                converter_blb = dict(
                    zip(range(len(genesis_data_blb[0])), genesis_data_blb[0]))
                absolute_model_blb = []
                end_premises_classes_blb = []
                for j in out_model_blb[0]:  # by Class
                    relative_premises_blb = j[0]
                    absolute_premises_blb = toolfis.relative2absolute(
                        relative_premises_blb, converter_blb)
                    end_premises_classes_blb.append(
                        absolute_premises_blb
                    )  # For estimation metrics rules and premises
                    absolute_model_blb.append(
                        [absolute_premises_blb, j[1],
                         j[2]])  # premises absolutes, Weights, name_method
                classifiers_blb.append(absolute_model_blb)

        num_classes = cbin_train.shape[1]
        indexes_premises_byclass = []
        for ci in range(
                num_classes):  # debo de colocar aqui el numero de clases
            container_aux = []
            for j in classifiers_blb:
                container_aux.append(j[ci][0])

            list_premises_container = list(chain(*container_aux))
            unique_indexes = list(set(list_premises_container))
            unique_premises = toolfis.calculation_premises(
                unique_indexes, ux_train, t_norm)
            indexes_premises_byclass.append([unique_indexes, unique_premises])

        success, out_model = toolfis.classifiers_aggregation(
            indexes_premises_byclass,
            cbin_train,
            'MQR',
            freq_classes,
            info=('All models', str(nc)))

        if success:
            successful_classifiers += 1
            absolute_model = []
            end_premises_classes = []
            for j in out_model[0]:  # by Class
                absolute_premises = j[0]
                end_premises_classes.append(
                    absolute_premises
                )  # For estimation metrics rules and premises
                absolute_model.append(
                    [absolute_premises, j[1],
                     j[2]])  # premises absolutes, Weights, name_method
            classifiers.append(absolute_model)

            # Metrics Train
            outputs_tree_train.append(
                eval_classifier_one(absolute_model, ux_train,
                                    t_norm))  # Out U by class train
            outputs_tree_train_bin.append(
                decision_class(outputs_tree_train[-1], freq_classes))
            container_ac_train.append(
                eval_metrics(cbin_train, out_model[1])[0])
            container_train.append(cbin_train)

            # Metrics Test
            outputs_tree_test.append(
                eval_classifier_one(absolute_model, ux_test,
                                    t_norm))  # Out U by class test
            outputs_tree_test_bin.append(
                decision_class(outputs_tree_test[-1], freq_classes))
            container_ac_test.append(
                round(
                    eval_metrics(cbin_test, outputs_tree_test_bin[-1])[0], 2))
            container_test.append(cbin_test)

            aux_metrics = metricas_rules_premises(end_premises_classes)
            partial_metrics_rules.append(hstack(aux_metrics))

        if not classifiers:
            return [
                "Any of %i classifiers was successful" % successful_classifiers
            ], [0], cv_i

    if method_aggreg_classf == 0:

        #  ============================== SPECIALIST EVALUATION ================================================

        metrics_train = eval_metrics(np.vstack(container_train),
                                     np.vstack(outputs_tree_train_bin))
        metrics_test = eval_metrics(np.vstack(container_test),
                                    np.vstack(outputs_tree_test_bin))

        #  Evaluation rules
        aux_metrics_rules = sum(partial_metrics_rules, 0)
        metrics_rules = aux_metrics_rules

        metrics = [
            1,
            [
                metrics_train[0], metrics_test[0], metrics_train[1],
                metrics_test[1], metrics_rules
            ], successful_classifiers
        ]
        results = [
            successful_classifiers, metrics_train[0], metrics_test[0],
            metrics_train[1], metrics_test[1], metrics_rules[0],
            metrics_rules[1], metrics_rules[2]
        ]

        report = toolfis.template_results('', results)

        return report, metrics, cv_i

    elif method_aggreg_classf == 1:

        #  ============================== AVERAGE AGGREGATION ================================================

        #  Evaluation Train
        u_by_class = estimation_average(outputs_tree_train)
        train_decision = decision_class(u_by_class, freq_classes)
        metrics_train = eval_metrics(cbin_train, train_decision)

        #  Evaluation Test_average
        u_by_class = estimation_average(outputs_tree_test)
        test_decision = decision_class(u_by_class, freq_classes)
        metrics_test = eval_metrics(cbin_test, test_decision)

        #  Evaluation rules
        aux_metrics_rules = sum(partial_metrics_rules, 0)
        metrics_rules = aux_metrics_rules

        metrics = [
            1,
            [
                metrics_train[0], metrics_test[0], metrics_train[1],
                metrics_test[1], metrics_rules
            ], successful_classifiers
        ]
        results = [
            successful_classifiers, metrics_train[0], metrics_test[0],
            metrics_train[1], metrics_test[1], metrics_rules[0],
            metrics_rules[1]
        ]

        report = toolfis.template_results('', results)

        return report, metrics, cv_i

    elif method_aggreg_classf == 2:

        #  ============================== WEIGHT AGGREGATION ================================================

        weight_by_classifier = estimation_weight(
            outputs_tree_train, cbin_train)  # Class train binario

        # Evaluation Train
        output_tree_train_aggregated = aggregation_classifiers_by_weight(
            weight_by_classifier, outputs_tree_train)
        train_decision = decision_class(output_tree_train_aggregated,
                                        freq_classes)
        metrics_train = eval_metrics(cbin_train, train_decision)

        # Evaluation Test
        output_tree_test_aggregated = aggregation_classifiers_by_weight(
            weight_by_classifier, outputs_tree_test)
        test_decision = decision_class(output_tree_test_aggregated,
                                       freq_classes)
        metrics_test = eval_metrics(cbin_test, test_decision)

        #  Evaluation rules
        aux_metrics_rules = sum(partial_metrics_rules, 0)
        constant = len(partial_metrics_rules)
        metrics_rules = aux_metrics_rules / constant

        metrics = [
            1,
            [
                metrics_train[0], metrics_test[0], metrics_train[1],
                metrics_test[1], metrics_rules
            ], successful_classifiers
        ]
        results = [
            successful_classifiers, metrics_train[0], metrics_test[0],
            metrics_train[1], metrics_test[1], metrics_rules[0],
            metrics_rules[1]
        ]

        report = toolfis.template_results('', results)
        return report, metrics, cv_i

    elif method_aggreg_classf == 3:

        #  ============================== WEIGHT_BY_CLASS AGGREGATION ================================================

        weight_class_by_classifier = estimation_weight_by_class(
            outputs_tree_train, cbin_train)  # Class train binario

        # Evaluation Train
        output_tree_train_aggregated = aggregation_classifiers_by_weight_class(
            weight_class_by_classifier, outputs_tree_train)
        train_decision = decision_class(output_tree_train_aggregated,
                                        freq_classes)
        metrics_train = eval_metrics(cbin_train, train_decision)

        # Evaluation Test
        output_tree_test_aggregated = aggregation_classifiers_by_weight_class(
            weight_class_by_classifier, outputs_tree_test)
        test_decision = decision_class(output_tree_test_aggregated,
                                       freq_classes)
        metrics_test = eval_metrics(cbin_test, test_decision)

        #  Evaluation rules
        aux_metrics_rules = sum(partial_metrics_rules, 0)
        constant = len(partial_metrics_rules)
        metrics_rules = aux_metrics_rules / constant

        metrics = [
            1,
            [
                metrics_train[0], metrics_test[0], metrics_train[1],
                metrics_test[1], metrics_rules
            ], successful_classifiers
        ]
        results = [
            successful_classifiers, metrics_train[0], metrics_test[0],
            metrics_train[1], metrics_test[1], metrics_rules[0],
            metrics_rules[1]
        ]

        report = toolfis.template_results('', results)
        return report, metrics, cv_i

    elif method_aggreg_classf == 4:

        #  ============================== PACKING ONE MODEL V.1 ================================================

        #  Evaluation Test_average
        u_by_class_ave = estimation_average(outputs_tree_test)
        test_decision_ave = decision_class(u_by_class_ave, freq_classes)
        metrics_test_ave = round(
            eval_metrics(cbin_test, test_decision_ave)[0], 2)

        voting_bin = voting(outputs_tree_test_bin)
        vot_bin = decision_class(voting_bin, freq_classes)
        metrics_test_vot = round(eval_metrics(cbin_test, vot_bin)[0], 2)

        # vec_dng = np.array(i_dng)
        # redu_trn = vec_dng.shape[0] / ux_train.shape[0]

        #  Evaluation rules
        aux_metrics_rules_ave = sum(partial_metrics_rules, 0)
        metrics_rules_ave = aux_metrics_rules_ave[0]
        aux_t0 = timeit.default_timer()
        time_finished = aux_t0 - time_ini

        folder__tree_output_test = os.path.join(folder__tree_output,
                                                'ac_teste_av.txt')
        with open(folder__tree_output_test, 'a') as f:
            f.write(str(metrics_test_ave) + '\n')

        folder__tree_output_test = os.path.join(folder__tree_output,
                                                'ac_teste_vot.txt')
        with open(folder__tree_output_test, 'a') as f:
            f.write(str(metrics_test_vot) + '\n')

        # folder__tree_output_test = os.path.join(folder__tree_output, 'reducao_trn.txt')
        # with open(folder__tree_output_test, 'a') as f:
        #     f.write(str(redu_trn) + '\n')

        folder__tree_output_test = os.path.join(folder__tree_output,
                                                'regras_teste_10CV.txt')
        with open(folder__tree_output_test, 'a') as f:
            f.write(str(metrics_rules_ave) + '\n')

        if cv_i == 10:
            folder__tree_output_test = os.path.join(folder__tree_output,
                                                    'time_teste_10CV.txt')
            with open(folder__tree_output_test, 'a') as f:
                f.write(str(time_finished) + '\n')

        num_classes = cbin_train.shape[1]
        indexes_premises_byclass = []
        for i in range(
                num_classes):  # debo de colocar aqui el numero de clases
            container_aux = []
            for j in classifiers:
                container_aux.append(j[i][0])

            list_premises_container = list(chain(*container_aux))
            unique_indexes = list(set(list_premises_container))
            unique_premises = toolfis.calculation_premises(
                unique_indexes, ux_train, t_norm)
            indexes_premises_byclass.append([unique_indexes, unique_premises])

        exito, output_collective = toolfis.classifiers_aggregation(
            indexes_premises_byclass,
            cbin_train,
            'MQR',
            freq_classes,
            info=('batches', 'All'))

        if exito:
            premises_weights_names = output_collective[0]
            estimation_classes = output_collective[1]

            final_premises_classes = []
            for i in range(len(premises_weights_names)):  # x cada clase
                final_premises_classes.append(premises_weights_names[i][0])

            F6 = Evaluation(premises_weights_names, final_premises_classes,
                            freq_classes)
            metrics_train = F6.eval_train(cbin_train, estimation_classes)
            metrics_test = F6.eval_test(cbin_test, ux_test, t_norm)
            metrics_rules = metrics_test[4]

            metrics = [
                1,
                [
                    metrics_train[0] * 100, metrics_test[0] * 100,
                    metrics_train[1], metrics_test[1], metrics_rules
                ], successful_classifiers
            ]
            results = [
                successful_classifiers, metrics_train[0] * 100,
                metrics_test[0] * 100, metrics_train[1], metrics_test[1],
                metrics_rules[0], metrics_rules[1], metrics_rules[2]
            ]

            report = toolfis.template_results('', results)
            return report, metrics, cv_i

    elif method_aggreg_classf == 5:

        #  ============================== PACKING MODEL v.2 ================================================

        num_classes = cbin_train.shape[1]
        indexes_premises_byclass = []
        for i in range(
                num_classes):  # debo de colocar aqui el numero de clases
            container_aux = []
            container_w = []
            for j in classifiers:
                container_aux.append(j[i][0])
                container_w.append(j[i][1].tolist())

            list_premises_container = list(chain(*container_aux))
            list_w_container = sum(list(chain(*container_w)), [])
            container_dic = dict(zip(list_premises_container,
                                     list_w_container))
            rule_duplicate = list(list_duplicates(list_premises_container))
            for rule in rule_duplicate:
                aver_w = sum(array(list_w_container)[rule[1]]) / len(rule[1])
                container_dic[rule[0]] = aver_w
            unique_indexes = list(container_dic.keys())
            aux_unique_w = array(container_dic.values()) / sum(
                container_dic.values())
            unique_w = aux_unique_w.reshape(aux_unique_w.shape[0], 1)
            indexes_premises_byclass.append([unique_indexes, unique_w, 'MQR'])

        output_classifier = eval_classifier_one(indexes_premises_byclass,
                                                ux_train,
                                                t_norm)  # Out U by class train
        output_bin = decision_class(output_classifier, freq_classes)

        final_premises_classes = []
        for i in range(len(indexes_premises_byclass)):  # x cada clase
            final_premises_classes.append(indexes_premises_byclass[i][0])

        F6 = Evaluation(indexes_premises_byclass, final_premises_classes,
                        freq_classes)
        metrics_train = F6.eval_train(cbin_train, output_bin)
        metrics_test = F6.eval_test(cbin_test, ux_test, t_norm)
        metrics_rules = metrics_test[4]

        metrics = [
            1,
            [
                metrics_train[0] * 100, metrics_test[0] * 100,
                metrics_train[1], metrics_test[1], metrics_rules
            ], successful_classifiers
        ]
        results = [
            successful_classifiers, metrics_train[0] * 100,
            metrics_test[0] * 100, metrics_train[1], metrics_test[1],
            metrics_rules[0], metrics_rules[1]
        ]

        report = toolfis.template_results('', results)
        return report, metrics, cv_i
def selection_criteria(ux_train, ux_test, cbin_train, cbin_test, freq_classes,
                       parameters_classifiers, classifiers, cv_i,
                       outputs_tree_train, outputs_tree_train_bin,
                       container_ac_train, outputs_tree_test,
                       outputs_tree_test_bin, partial_metrics_rules,
                       sel_method, sel_param, size_ensemble,
                       folder__tree_output, time_ini):

    param_clf = [parameters_classifiers[0]] + parameters_classifiers[1]
    t_norm = param_clf[3]

    # Objective function
    if sel_method == 0:
        sel_name = 'WAD'
        wad_values = wad_calc_v2(cbin_train, outputs_tree_train_bin,
                                 container_ac_train, [sel_param, 1])
        wad_list = enumerate(wad_values)
        clf_sorted = sorted(wad_list, key=itemgetter(1))
    else:
        sel_name = 'REG'
        reg_values = reg_calc(cbin_train, outputs_tree_train_bin,
                              container_ac_train, [sel_param, 1])
        reg_list = enumerate(reg_values)
        clf_sorted = sorted(reg_list, key=itemgetter(1))

    # Selection of N classifiers
    clf_sel_list = [int(i[0]) for i in clf_sorted[-size_ensemble:]]
    sel_classifiers = [classifiers[i] for i in clf_sel_list]

    # Selected classifiers results
    sel_outputs_tree_train = [outputs_tree_train[i] for i in clf_sel_list]
    sel_outputs_tree_train_bin = [
        outputs_tree_train_bin[i] for i in clf_sel_list
    ]

    sel_outputs_tree_test = [outputs_tree_test[i] for i in clf_sel_list]
    sel_outputs_tree_test_bin = [
        outputs_tree_test_bin[i] for i in clf_sel_list
    ]

    sel_partial_metrics_rules = [
        partial_metrics_rules[i] for i in clf_sel_list
    ]
    sel_successful_classifiers = [len(clf_sel_list), sel_name, sel_param]

    p_method_agg = GlobalParameter()
    method_aggreg_classf = p_method_agg.method_aggregation

    if method_aggreg_classf == 4:

        #  ============================== PACKING ONE MODEL V.1 ============================================

        #  Evaluation Test_average
        u_by_class_ave = estimation_average(sel_outputs_tree_test)
        test_decision_ave = decision_class(u_by_class_ave, freq_classes)
        metrics_test_ave = round(
            eval_metrics(cbin_test, test_decision_ave)[0], 2)

        voting_bin = voting(sel_outputs_tree_test_bin)
        vot_bin = decision_class(voting_bin, freq_classes)
        metrics_test_vot = round(eval_metrics(cbin_test, vot_bin)[0], 2)

        #  Evaluation rules
        aux_metrics_rules_ave = sum(sel_partial_metrics_rules, 0)
        metrics_rules_ave = aux_metrics_rules_ave[0]
        aux_t0 = timeit.default_timer()
        time_finished = aux_t0 - time_ini

        folder__tree_output_test = os.path.join(folder__tree_output,
                                                'ac_teste_av.txt')
        with open(folder__tree_output_test, 'a') as f:
            f.write(str(metrics_test_ave) + '\n')

        folder__tree_output_test = os.path.join(folder__tree_output,
                                                'ac_teste_vot.txt')
        with open(folder__tree_output_test, 'a') as f:
            f.write(str(metrics_test_vot) + '\n')

        folder__tree_output_test = os.path.join(folder__tree_output,
                                                'regras_teste_10CV.txt')
        with open(folder__tree_output_test, 'a') as f:
            f.write(str(metrics_rules_ave) + '\n')

        if cv_i == 10:
            folder__tree_output_test = os.path.join(folder__tree_output,
                                                    'time_teste_10CV.txt')
            with open(folder__tree_output_test, 'a') as f:
                f.write(str(time_finished) + '\n')

        num_classes = cbin_train.shape[1]
        indexes_premises_byclass = []
        for i in range(
                num_classes):  # debo de colocar aqui el numero de clases
            container_aux = []
            for j in sel_classifiers:
                container_aux.append(j[i][0])

            list_premises_container = list(chain(*container_aux))
            unique_indexes = list(set(list_premises_container))
            unique_premises = toolfis.calculation_premises(
                unique_indexes, ux_train, t_norm)
            indexes_premises_byclass.append([unique_indexes, unique_premises])

        exito, output_collective = toolfis.classifiers_aggregation(
            indexes_premises_byclass,
            cbin_train,
            'MQR',
            freq_classes,
            info=('', str(sel_successful_classifiers)))

        if exito:
            premises_weights_names = output_collective[0]
            estimation_classes = output_collective[1]

            final_premises_classes = []
            for i in range(len(premises_weights_names)):  # x cada clase
                final_premises_classes.append(premises_weights_names[i][0])

            F6 = Evaluation(premises_weights_names, final_premises_classes,
                            freq_classes)
            metrics_train = F6.eval_train(cbin_train, estimation_classes)
            metrics_test = F6.eval_test(cbin_test, ux_test, t_norm)
            metrics_rules = metrics_test[4]

            metrics = [
                1,
                [
                    metrics_train[0] * 100, metrics_test[0] * 100,
                    metrics_train[1], metrics_test[1], metrics_rules
                ], sel_successful_classifiers
            ]
            results = [
                sel_successful_classifiers, metrics_train[0] * 100,
                metrics_test[0] * 100, metrics_train[1], metrics_test[1],
                metrics_rules[0], metrics_rules[1], metrics_rules[2]
            ]

            report = toolfis.template_results('', results)
            return report, metrics, cv_i, sel_successful_classifiers
def selection_criteria(ux_train, ux_test, cbin_train, cbin_test, freq_classes, parameters_classifiers, classifiers,
                       cv_i, outputs_tree_train, outputs_tree_train_bin, container_ac_train,
                       outputs_tree_test, outputs_tree_test_bin, partial_metrics_rules,
                       sel_method, sel_param, size_ensemble, folder__tree_output, time_ini):

    aux_outputs_tree_train_bin = outputs_tree_train_bin[:]
    aux_container_ac_train = container_ac_train[:]

    param_clf = [parameters_classifiers[0]] + parameters_classifiers[1]
    t_norm = param_clf[3]

    acc_list = enumerate(container_ac_train)
    acc_sorted = sorted(acc_list, key=itemgetter(1))
    aux_container_ac_train[acc_sorted[-1][0]] = 1

    for se in range(2, size_ensemble+1):
        sel_name = 'WAD'

        if se == 2:
            wad_values = wad_calc_v2_inc(cbin_train, aux_outputs_tree_train_bin, aux_container_ac_train, [sel_param, 1],
                                         acc_sorted[-1][0])
            wad_list = enumerate(wad_values)
            clf_sorted = sorted(wad_list, key=itemgetter(1))
            # clf_sel_list = [int(i[0]) for i in [acc_sorted[-1], clf_sorted[-1]]]
            list_sel = [acc_sorted[-1][0], clf_sorted[-1][0]]
            clf_sel_list = [i for i in list_sel]
            aux_container_ac_train[clf_sorted[-1][0]] = 1
        else:
            aux_outputs_tree_train_bin[acc_sorted[-1][0]] = output_collective[1]
            aux_outputs_tree_train_bin[clf_sorted[-1][0]] = 0

            wad_values = wad_calc_v2_inc(cbin_train, aux_outputs_tree_train_bin, aux_container_ac_train, [sel_param, 1],
                                         acc_sorted[-1][0])
            wad_list = enumerate(wad_values)
            clf_sorted = sorted(wad_list, key=itemgetter(1))
            # clf_sel_list = [int(i[0]) for i in [acc_sorted[-1], clf_sorted[-1]]]
            list_sel.append(clf_sorted[-1][0])
            clf_sel_list = [i for i in list_sel]
            aux_container_ac_train[clf_sorted[-1][0]] = 1

        # Selection of N classifiers
        sel_classifiers = [classifiers[i] for i in clf_sel_list]

        sel_successful_classifiers = [se, sel_name, sel_param]

        #  ============================== PACKING ONE MODEL V.1 ============================================
        #  Evaluation rules
        aux_t0 = timeit.default_timer()
        time_finished = aux_t0 - time_ini

        if cv_i == 10:
            folder__tree_output_test = os.path.join(folder__tree_output, 'time_teste_10CV.txt')
            with open(folder__tree_output_test, 'a') as f:
                f.write(str(time_finished) + '\n')

        num_classes = cbin_train.shape[1]
        indexes_premises_byclass = []
        for i in range(num_classes):  # debo de colocar aqui el numero de clases
            container_aux = []
            for j in sel_classifiers:
                container_aux.append(j[i][0])

            list_premises_container = list(chain(*container_aux))
            unique_indexes = list(set(list_premises_container))
            unique_premises = toolfis.calculation_premises(unique_indexes, ux_train, t_norm)
            indexes_premises_byclass.append([unique_indexes, unique_premises])

        if se == size_ensemble:
            exito, output_collective = toolfis.classifiers_aggregation(indexes_premises_byclass, cbin_train,
                                                                       'MQR', freq_classes,
                                                                       info=('', str(sel_successful_classifiers)))
        else:
            with HiddenPrints():
                exito, output_collective = toolfis.classifiers_aggregation(indexes_premises_byclass, cbin_train,
                                                                           'MQR', freq_classes,
                                                                           info=('', str(sel_successful_classifiers)))

        if exito:
            premises_weights_names = output_collective[0]
            estimation_classes = output_collective[1]

            final_premises_classes = []
            for i in range(len(premises_weights_names)):  # x cada clase
                final_premises_classes.append(premises_weights_names[i][0])

            F6 = Evaluation(premises_weights_names, final_premises_classes, freq_classes)
            metrics_train = F6.eval_train(cbin_train, estimation_classes)
            metrics_test = F6.eval_test(cbin_test, ux_test, t_norm)
            metrics_rules = metrics_test[4]

            metrics = [1, [metrics_train[0]*100, metrics_test[0]*100, metrics_train[1], metrics_test[1],
                           metrics_rules], sel_successful_classifiers]
            results = [sel_successful_classifiers, metrics_train[0]*100, metrics_test[0]*100, metrics_train[1],
                       metrics_test[1], metrics_rules[0], metrics_rules[1], metrics_rules[2]]

            report = toolfis.template_results('', results)

    return report, metrics, cv_i, sel_successful_classifiers