Example #1
0
def evaluate_feature_model():
    """
    :return: evaluate the feature entropy and accuracy metrics.
    """
    prefix = "C:\\Users\\yukimula\\git\\jcsa\\JCMuta\\results\\data"
    postfix = "C:\\Users\\yukimula\\git\\jcsa\\PyMuta\\output\\evaluate"
    print("Start testing here.")
    evaluator = FeatureEvaluation()
    for filename in os.listdir(prefix):
        directory = os.path.join(prefix, filename)
        program = cprog.CProgram(directory)
        mutant_space = mut.MutantSpace(program)
        evaluator.train(mutant_space.get_mutants())
        print("\tTraining from", filename, "for", len(mutant_space.get_mutants()), "mutants.")
    evaluator.evaluate(os.path.join(postfix, "feature.error.txt"))
    return
Example #2
0
def encoding_feature_vectors():
    """
    :return: generate .corpus, .path and .csv
    """
    prefix = "C:\\Users\\yukimula\\git\\jcsa\\JCMuta\\results\\data"
    postfix = "C:\\Users\\yukimula\\git\\jcsa\\PyMuta\\output\\encoding"
    print("Start testing here.")
    max_distance = 3
    encoder = FeatureEncoding()
    csv_files = list()
    for filename in os.listdir(prefix):
        directory = os.path.join(prefix, filename)
        program = cprog.CProgram(directory)
        mutant_space = mut.MutantSpace(program)
        encoder.reset()
        print("Testing for", filename, "starts.")
        print("\t\t1. Parsing program and", len(mutant_space.get_mutants()), "mutants from code.")
        encoder.encode_paths(mutant_space.get_mutants(), max_distance)
        encoder.print_paths(mutant_space.get_mutants(), os.path.join(postfix, filename + ".path.txt"), max_distance)
        print("\t\t2. Encode the documents in mutation with", max_distance, "length.")
        encoder.generate_doc2vec(os.path.join(postfix, filename + ".encoding.model"),
                                 os.path.join(postfix, filename + "paths.corpus"))
        print("\t\t3. Generate .corpus and .model file in", postfix)
        csv_files.append(os.path.join(postfix, filename + ".csv"))
        encoder.generate_data_file(mutant_space.get_mutants(), max_distance, os.path.join(postfix, filename + ".csv"))
        print("\t\t4. Generate .csv file in", postfix)
        print("\tTesting for", program.get_file_name(), "finished.")
    csv_total_file = os.path.join(postfix, "training_data.csv")
    with open(csv_total_file, 'w') as writer:
        first = True
        for csv_file in csv_files:
            first2 = True
            with open(csv_file, 'r') as reader:
                for line in reader:
                    line = line.strip()
                    if first2:
                        first2 = False
                        if first:
                            first = False
                            writer.write(line.strip() + "\n")
                    elif len(line) > 0:
                        writer.write(line + "\n")
    print("Testing end for all.")
    return
Example #3
0
def __output_state_error_main__():
    prefix = "C:\\Users\\yukimula\\git\\jcsa\\JCMuta\\results\\data"
    distance, sym_depth, vector_size, optimize, epochs = 6, 3, 128, True, 4
    for filename in os.listdir(prefix):
        directory = os.path.join(prefix, filename)
        program = cpro.CProgram(directory)
        mutant_space = mut.MutantSpace(program)
        common_corpus.reset()  # reset the encoding feature space
        print("Get mutation information for", program.get_file_name())
        output_file = os.path.join(
            "C:\\Users\\yukimula\\git\\jcsa\\PyMuta\\output\\feature_paths",
            filename + ".path")
        with open(output_file, 'w') as writer:
            for mutant in mutant_space.get_mutants():
                writer.write(str(mutant.get_id()) + "\t")
                writer.write(str(mutant.mutation.get_mutation_class()) + "\t")
                writer.write(
                    str(mutant.mutation.get_mutation_operator()) + "\t")
                location = mutant.mutation.get_location()
                location: astree.AstNode
                writer.write(str(location.get_beg_line()) + "\t")
                writer.write("\"" + location.get_code(True) + "\"\t")
                if mutant.mutation.has_parameter():
                    writer.write(str(mutant.mutation.get_parameter()))
                else:
                    writer.write("")
                writer.write("\t")
                labels = mutant.labels
                labels: mut.MutantLabels
                writer.write(str(labels.get_category()) + "\t")
                writer.write("\n")
                state_infection = mutant.get_features()
                state_infection: mut.StateInfection
                writer.write("coverage at:\t" +
                             str(state_infection.get_faulty_execution()) +
                             "\n")
                for state_error, constraints in state_infection.error_infections.items(
                ):
                    constraints: mut.StateConstraints
                    __output_state_error__(program, state_error, constraints,
                                           writer, distance, sym_depth)
                writer.write("\n")
    print("Testing end for all.")
Example #4
0
def generate_predict_csv_files():
    prefix = "C:\\Users\\yukimula\\git\\jcsa\\JCMuta\\results\\pdata"
    distance, sym_depth, vector_size, optimize, epochs = 6, 3, 128, True, 4
    encoder = PathFeatureEncoding(path_distance=distance,
                                  sym_depth=sym_depth,
                                  optimize=optimize,
                                  vector_size=vector_size)
    encoder.doc2vec = Doc2Vec.load(
        os.path.join(
            "C:\\Users\\yukimula\\git\\jcsa\\PyMuta\\output\\doc2vec.model"))
    for filename in os.listdir(prefix):
        directory = os.path.join(prefix, filename)
        program = cpro.CProgram(directory)
        mutant_space = mut.MutantSpace(program)
        output_file = os.path.join(
            "C:\\Users\\yukimula\\git\\jcsa\\PyMuta\\output\\predict_files",
            filename + ".csv")
        encoder.write_csv_file(mutant_space, output_file)
        print("\tOutput csv file for", filename, "with",
              len(mutant_space.mutants), "mutants.")
    print("Testing end for all.")
Example #5
0
def read_fault_detection_contexts():
    """
    :return: Generate the k-degree of fault detection context for reachability, infection and propagation of each mutant
    """
    prefix = "C:\\Users\\yukimula\\git\\jcsa\\JCMuta\\results\\data"
    postfix = "C:\\Users\\yukimula\\git\\jcsa\\PyMuta\\output\\features"
    max_distance = 3
    print("Start testing here.")
    for filename in os.listdir(prefix):
        directory = os.path.join(prefix, filename)
        program = cprog.CProgram(directory)
        mutant_space = mut.MutantSpace(program)
        output_file = os.path.join(postfix, filename)
        mutant = mutant_space.get_mutant(random.randint(0, len(mutant_space.mutants) - 1))
        print_reach_context(mutant, output_file, False, max_distance)
        mutant = mutant_space.get_mutant(random.randint(0, len(mutant_space.mutants) - 1))
        print_infect_context(mutant, output_file, False, max_distance)
        mutant = mutant_space.get_mutant(random.randint(0, len(mutant_space.mutants) - 1))
        print_propagate_context(mutant, output_file, False, max_distance)
        mutant = mutant_space.get_mutant(random.randint(0, len(mutant_space.mutants) - 1))
        print("Testing on", program.filename, "for mutant", mutant.id)
    print("Testing end for all.")
    return
Example #6
0
def training_doc2vec_encoding():
    prefix = "C:\\Users\\yukimula\\git\\jcsa\\JCMuta\\results\\data"
    distance, sym_depth, vector_size, optimize, epochs = 6, 3, 128, True, 4
    encoder = PathFeatureEncoding(path_distance=distance,
                                  sym_depth=sym_depth,
                                  optimize=optimize,
                                  vector_size=vector_size)
    print("Testing start for files.")
    for filename in os.listdir(prefix):
        directory = os.path.join(prefix, filename)
        program = cpro.CProgram(directory)
        mutant_space = mut.MutantSpace(program)
        for mutant in mutant_space.get_mutants():
            encoder.encoding_word_vectors(mutant)
        print("\tComplete encoding", len(mutant_space.get_mutants()),
              "mutants for", filename)
    print("Start training doc2vec with", len(encoder.tagged_sentences),
          "sentences.")
    encoder.train_doc2vec(
        4,
        os.path.join(
            "C:\\Users\\yukimula\\git\\jcsa\\PyMuta\\output\\doc2vec.model"))
    print("Complete doc2vec training")
    return
Example #7
0
    data_frame = MutantDataFrame()
    prefix = "C:\\Users\\yukimula\\git\\jcsa\\JCMuta\\results\\data"
    p_prefix = "C:\\Users\\yukimula\\git\\jcsa\\JCMuta\\results\\pdata"
    postfix = "C:\\Users\\yukimula\\git\\jcsa\\PyMuta\\output\\training_files"
    p_postfix = "C:\\Users\\yukimula\\git\\jcsa\\PyMuta\\output\\predict_files"
    for filename in os.listdir(prefix):
        csv_file = os.path.join(postfix, filename + ".csv")
        data_frame.append(csv_file)
        print("\tProcessing samples for", filename)
    print("Load", len(data_frame), "mutant samples for training.")
    classifier = data_frame.train_for(nnet.MLPClassifier())

    for filename in os.listdir(p_prefix):
        directory = os.path.join(p_prefix, filename)
        program = cpro.CProgram(directory)
        mutant_space = mut.MutantSpace(program)
        new_data_frame = MutantDataFrame()
        csv_file = os.path.join(p_postfix, filename + ".csv")
        new_data_frame.append(csv_file)
        stubborn_data_frame = new_data_frame.generate_predict_table(classifier)
        print("\tIdentify", len(stubborn_data_frame.mutant_id_list), "stubborn mutants in",
              len(new_data_frame.mutant_id_list), "for", filename)
        output_directory = os.path.join(p_postfix, filename)
        if not os.path.exists(output_directory):
            os.mkdir(output_directory)
        for mutant_key in stubborn_data_frame.mutant_id_list:
            mutant_key: MutantKey
            output_file = os.path.join(output_directory, filename + "." + str(mutant_key.get_mid()) + ".mc")
            mutant_key.interpret(mutant_space, output_file)
    print("Prediction ends for all.")