<commit_before>#!/usr/bin/env python

# -*- coding: utf-8 -*-

# std import
import sys
import argparse
import os.path
from collections import defaultdict
import csv
import re
import math


class Variant(object):

    def __init__(self, type, comment):
        self.type = type
        self.comment = comment

    def __eq__(self, other):
        if self.type != other.type:
            return False

        if self.comment != other.comment:
            return False

        return True

    def __str__(self):
        return "%s_%s" % (str(self.type), str(self.comment))

    def __repr__(self):
        return "<%s - %s>" % (self.type, self.comment)

    def __hash__(self):
        return hash(self.type + self.comment)


def main():
    """ The main function of vde no argument """

    enable_input = [name.split("2")[0] for name in globals().keys()
                    if name.endswith("2eva")]

    parser = argparse.ArgumentParser(
        prog="vde",
        formatter_class=argparse.ArgumentDefaultsHelpFormatter)

    parser.add_argument("-e", "--experiment",
                        type=str,
                        help="File of experimente result.",
                        required=True)
    parser.add_argument("-t", "--truth",
                        type=str,
                        help="File of truth result.",
                        required=True)
    parser.add_argument("-d", "--delta",
                        type=int,
                        help="Acceptable diff betwen truth and experimente.",
                        default=5)
    parser.add_argument("-ef", "--experiment-format",
                        type=str,
                        help="Format of experiment file",
                        choices=enable_input,
                        default="eva")
    parser.add_argument("-tf", "--truth-format",
                        type=str,
                        help="Format of truth file",
                        choices=enable_input,
                        default="eva")

    # parsing cli argument
    argument = vars(parser.parse_args())

    expfunc = globals()[argument["experiment_format"]+"2eva"]
    truthfunc = globals()[argument["truth_format"]+"2eva"]

    experiment, count = expfunc(argument["experiment"])
    truth, count = truthfunc(argument["truth"])

    result = compare(experiment, truth, argument["delta"])

    result_printing(result, count)


def result_printing(result, count):
    """ Printing the result in csv format """

    head = ",".join(("type", "TP", "FP", "recall", "precision"))
    print(head)
    for gap in result.keys(): 
        total = result[gap]["TP"] + result[gap]["FP"]

        prec = 1 if total == 0 else result[gap]["TP"]/float(total)
        recall = 1 if count[gap] == 0 else result[gap]["TP"]/float(count[gap])

        print(",".join((str(gap),
                        str(result[gap]["TP"]),
                        str(result[gap]["FP"]),
                        str(recall),
                        str(prec))))


def compare(exp, truth, delta):
    """ Compare experimente and truth return TP FP precision and recall
    for each type """

    result = defaultdict(lambda: defaultdict(int))
    
    exp_pos = set(exp.keys())
    tru_pos = set(truth.keys())

    for exact_pos in set(exp_pos & tru_pos):
        for variant in exp[exact_pos]:
            if variant in truth[exact_pos]:
                result[variant.type]["TP"] += 1
            else: 
                result[variant.type]["FP"] += 1

    not_found = set(exp_pos - (exp_pos & tru_pos))
    for fuzzy_pos in set(exp_pos - (exp_pos & tru_pos)):
	for pos in range(fuzzy_pos - delta, fuzzy_pos + delta + 1):
            for variant in exp[fuzzy_pos]:
                if variant.type in ("snp", "multi_snp"): 
		    result[variant.type]["FP"] += 1
                    try:
                        not_found.remove(fuzzy_pos)
                    except KeyError:
                        pass
                if variant in truth[pos]:
                    result[variant.type]["TP"] += 1
                    try:
                        not_found.remove(fuzzy_pos)
                    except KeyError:
                        pass

    for pos in not_found:
        for variant in set(exp[pos]):
            result[variant.type]["FP"] += 1

    return result


def eva2eva(filename):
    """ Read eva file and return value in dict
    position is key and type is value """

    __check_file_exist(filename)

    data = defaultdict(list)
    count = defaultdict(int)

    with open(filename) as csvfile:
        linereader = csv.reader(csvfile)
        for val in linereader:
            data[int(val[0])].append(Variant(val[1], val[2]))
            count[val[1]] += 1

    return data, count
<commit_msg>MTG : vde not generate some FP multi_snp<commit_after>#!/usr/bin/env python

# -*- coding: utf-8 -*-

# std import
import sys
import argparse
import os.path
from collections import defaultdict
import csv
import re
import math


class Variant(object):

    def __init__(self, type, comment):
        self.type = type
        self.comment = comment

    def __eq__(self, other):
        if self.type != other.type:
            return False

        if self.comment != other.comment:
            return False

        return True

    def __str__(self):
        return "%s_%s" % (str(self.type), str(self.comment))

    def __repr__(self):
        return "<%s - %s>" % (self.type, self.comment)

    def __hash__(self):
        return hash(self.type + self.comment)


def main():
    """ The main function of vde no argument """

    enable_input = [name.split("2")[0] for name in globals().keys()
                    if name.endswith("2eva")]

    parser = argparse.ArgumentParser(
        prog="vde",
        formatter_class=argparse.ArgumentDefaultsHelpFormatter)

    parser.add_argument("-e", "--experiment",
                        type=str,
                        help="File of experimente result.",
                        required=True)
    parser.add_argument("-t", "--truth",
                        type=str,
                        help="File of truth result.",
                        required=True)
    parser.add_argument("-d", "--delta",
                        type=int,
                        help="Acceptable diff betwen truth and experimente.",
                        default=5)
    parser.add_argument("-ef", "--experiment-format",
                        type=str,
                        help="Format of experiment file",
                        choices=enable_input,
                        default="eva")
    parser.add_argument("-tf", "--truth-format",
                        type=str,
                        help="Format of truth file",
                        choices=enable_input,
                        default="eva")

    # parsing cli argument
    argument = vars(parser.parse_args())

    expfunc = globals()[argument["experiment_format"]+"2eva"]
    truthfunc = globals()[argument["truth_format"]+"2eva"]

    experiment, count = expfunc(argument["experiment"])
    truth, count = truthfunc(argument["truth"])

    result = compare(experiment, truth, argument["delta"])

    result_printing(result, count)


def result_printing(result, count):
    """ Printing the result in csv format """

    head = ",".join(("type", "TP", "FP", "recall", "precision"))
    print(head)
    for gap in result.keys(): 
        total = result[gap]["TP"] + result[gap]["FP"]

        prec = 1 if total == 0 else result[gap]["TP"]/float(total)
        recall = 1 if count[gap] == 0 else result[gap]["TP"]/float(count[gap])

        print(",".join((str(gap),
                        str(result[gap]["TP"]),
                        str(result[gap]["FP"]),
                        str(recall),
                        str(prec))))


def compare(exp, truth, delta):
    """ Compare experimente and truth return TP FP precision and recall
    for each type """

    result = defaultdict(lambda: defaultdict(int))
    
    exp_pos = set(exp.keys())
    tru_pos = set(truth.keys())

    for exact_pos in set(exp_pos & tru_pos):
        for variant in exp[exact_pos]:
            if variant in truth[exact_pos]:
                result[variant.type]["TP"] += 1
            else: 
                result[variant.type]["FP"] += 1

    not_found = set(exp_pos - (exp_pos & tru_pos))
    for fuzzy_pos in set(exp_pos - (exp_pos & tru_pos)):
        end = False
	for pos in range(fuzzy_pos - delta, fuzzy_pos + delta + 1):
            for variant in exp[fuzzy_pos]:
                if variant.type in ("snp", "multi_snp"):
		    result[variant.type]["FP"] += 1
                    try:
                        not_found.remove(fuzzy_pos)
                    except KeyError:
                        pass
                    end = True
                    break

                if variant in truth[pos]:
                    result[variant.type]["TP"] += 1
                    try:
                        not_found.remove(fuzzy_pos)
                    except KeyError:
                        pass
                    end = True
                    break
            if end :
                break

    for pos in not_found:
        for variant in set(exp[pos]):
            result[variant.type]["FP"] += 1

    return result


def eva2eva(filename):
    """ Read eva file and return value in dict
    position is key and type is value """

    __check_file_exist(filename)

    data = defaultdict(list)
    count = defaultdict(int)

    with open(filename) as csvfile:
        linereader = csv.reader(csvfile)
        for val in linereader:
            data[int(val[0])].append(Variant(val[1], val[2]))
            count[val[1]] += 1

    return data, count

{'fraction_non_alphanumeric': 0.12266090890396875, 'fraction_numerical': 0.004729590787579683, 'mean_word_length': 1.7376864621446664, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1006987', 'n_tokens_mistral': 2854, 'n_tokens_neox': 2622, 'n_words': 679}