<commit_before>import fnmatch
import os
import csv
from time import time
from pygraphc.preprocess.PreprocessLog import PreprocessLog
from pygraphc.preprocess.CreateGraph import CreateGraph
from pygraphc.clustering.MajorClust import MajorClust, ImprovedMajorClust
from pygraphc.clustering.GraphEntropy import GraphEntropy
from pygraphc.evaluation.ExternalEvaluation import ExternalEvaluation
from pygraphc.evaluation.InternalEvaluation import InternalEvaluation
from pygraphc.anomaly.AnomalyScore import AnomalyScore
from pygraphc.output.Output import Output


def get_dataset(dataset, dataset_path, file_extension, method):
    # get all log files under dataset directory
    matches = []
    for root, dirnames, filenames in os.walk(dataset_path):
        for filename in fnmatch.filter(filenames, file_extension):
            matches.append(os.path.join(root, filename))

    # get file name to save all of the results
    files = {}
    result_path = '/home/hudan/Git/pygraphc/result/' + method + '/'
    for match in matches:
        identifier = match.split(dataset)
        index = dataset + identifier[1]
        files[index] = {'log_path': match, 'labeled_path': str(match) + '.labeled',
                        'result_percluster': result_path + index + '.percluster',
                        'result_perline': result_path + index + '.perline',
                        'anomaly_report': result_path + index + '.anomaly.csv'}

    # file to save evaluation performance per method
    evaluation_file = result_path + dataset + '.evaluation.csv'

    return files, evaluation_file


def get_evaluation(evaluated_graph, clusters, logs, properties, year, edges_dict):
    # get prediction file
    ExternalEvaluation.set_cluster_label_id(evaluated_graph, clusters, logs, properties['result_perline'])

    # get anomaly score
    anomaly_score = AnomalyScore(evaluated_graph, clusters, properties['anomaly_report'], year, edges_dict)
    anomaly_score.write_property()

    # get output
    output_txt = Output(evaluated_graph, clusters, logs, properties['result_percluster'])
    output_txt.to_txt()

    # get evaluation of clustering performance
    ar = ExternalEvaluation.get_adjusted_rand(properties['labeled_path'], properties['result_perline'])
    ami = ExternalEvaluation.get_adjusted_mutual_info(properties['labeled_path'], properties['result_perline'])
    nmi = ExternalEvaluation.get_normalized_mutual_info(properties['labeled_path'], properties['result_perline'])
    h = ExternalEvaluation.get_homogeneity(properties['labeled_path'], properties['result_perline'])
    c = ExternalEvaluation.get_completeness(properties['labeled_path'], properties['result_perline'])
    v = ExternalEvaluation.get_vmeasure(properties['labeled_path'], properties['result_perline'])
    silhoutte_index = InternalEvaluation.get_silhoutte_index(evaluated_graph, clusters)

    return ar, ami, nmi, h, c, v, silhoutte_index


def main(dataset, year, method):
    # get dataset files
    files = {}
    evaluation_file = ''
    if dataset == 'Hofstede2014':
        files, evaluation_file = get_dataset(dataset, '/home/hudan/Git/labeled-authlog/dataset/Hofstede2014',
                                             '*.anon', method)
    elif dataset == 'SecRepo':
        files, evaluation_file = get_dataset(dataset, '/home/hudan/Git/labeled-authlog/dataset/SecRepo',
                                             '*.log', method)

    # open evaluation file<commit_msg>Edit path and extension for Hofstede2014 dataset<commit_after>import fnmatch
import os
import csv
from time import time
from pygraphc.preprocess.PreprocessLog import PreprocessLog
from pygraphc.preprocess.CreateGraph import CreateGraph
from pygraphc.clustering.MajorClust import MajorClust, ImprovedMajorClust
from pygraphc.clustering.GraphEntropy import GraphEntropy
from pygraphc.evaluation.ExternalEvaluation import ExternalEvaluation
from pygraphc.evaluation.InternalEvaluation import InternalEvaluation
from pygraphc.anomaly.AnomalyScore import AnomalyScore
from pygraphc.output.Output import Output


def get_dataset(dataset, dataset_path, file_extension, method):
    # get all log files under dataset directory
    matches = []
    for root, dirnames, filenames in os.walk(dataset_path):
        for filename in fnmatch.filter(filenames, file_extension):
            matches.append(os.path.join(root, filename))

    # get file name to save all of the results
    files = {}
    result_path = '/home/hudan/Git/pygraphc/result/' + method + '/'
    for match in matches:
        identifier = match.split(dataset)
        index = dataset + identifier[1]
        files[index] = {'log_path': match, 'labeled_path': str(match) + '.labeled',
                        'result_percluster': result_path + index + '.percluster',
                        'result_perline': result_path + index + '.perline',
                        'anomaly_report': result_path + index + '.anomaly.csv'}

    # file to save evaluation performance per method
    evaluation_file = result_path + dataset + '.evaluation.csv'

    return files, evaluation_file


def get_evaluation(evaluated_graph, clusters, logs, properties, year, edges_dict):
    # get prediction file
    ExternalEvaluation.set_cluster_label_id(evaluated_graph, clusters, logs, properties['result_perline'])

    # get anomaly score
    anomaly_score = AnomalyScore(evaluated_graph, clusters, properties['anomaly_report'], year, edges_dict)
    anomaly_score.write_property()

    # get output
    output_txt = Output(evaluated_graph, clusters, logs, properties['result_percluster'])
    output_txt.to_txt()

    # get evaluation of clustering performance
    ar = ExternalEvaluation.get_adjusted_rand(properties['labeled_path'], properties['result_perline'])
    ami = ExternalEvaluation.get_adjusted_mutual_info(properties['labeled_path'], properties['result_perline'])
    nmi = ExternalEvaluation.get_normalized_mutual_info(properties['labeled_path'], properties['result_perline'])
    h = ExternalEvaluation.get_homogeneity(properties['labeled_path'], properties['result_perline'])
    c = ExternalEvaluation.get_completeness(properties['labeled_path'], properties['result_perline'])
    v = ExternalEvaluation.get_vmeasure(properties['labeled_path'], properties['result_perline'])
    silhoutte_index = InternalEvaluation.get_silhoutte_index(evaluated_graph, clusters)

    return ar, ami, nmi, h, c, v, silhoutte_index


def main(dataset, year, method):
    # get dataset files
    files = {}
    evaluation_file = ''
    if dataset == 'Hofstede2014':
        files, evaluation_file = get_dataset(dataset,
                                             '/home/hudan/Git/labeled-authlog/dataset/Hofstede2014/dataset1_perday',
                                             '*.log', method)
    elif dataset == 'SecRepo':
        files, evaluation_file = get_dataset(dataset, '/home/hudan/Git/labeled-authlog/dataset/SecRepo',
                                             '*.log', method)

    # open evaluation file
{'fraction_non_alphanumeric': 0.09463497936530525, 'fraction_numerical': 0.003273089511882738, 'mean_word_length': 3.863667820069204, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1298922', 'n_tokens_mistral': 2024, 'n_tokens_neox': 1886, 'n_words': 443}