dice-group / owlapy

OWLAPY is a Python Framework for creating and manipulating OWL Ontologies.
MIT License
21 stars 2 forks source link

Computing Confusion matrix via SPARQL #82

Closed Demirrr closed 1 month ago

Demirrr commented 1 month ago

The following functions from owlapy import owl_expression_to_sparql, owl_expression_to_dl work like a charm to convert an instance of OWL Class Expressions Class into string.

It would be great if we could implement owl_expression_to_sparq_confusion_matrix and owl_expression_to_sparql_exist function

def owl_expression_to_sparql_confusion_matrix(expression: OWLClassExpression = None,
                             root_variable: str = "?x",
                             values: Optional[Iterable[OWLNamedIndividual]] = None,
                             for_all_de_morgan: bool = True,
                             named_individuals: bool = False, positive_examples:Set[OWLNamedIndividual],
negative_examples:Set[OWLNamedIndividual]) -> Tuple[float,float,float,float]:
    return tp, fp, fn, tn
  1. If you have any better nameing, the name of the function can be changed.
  2. Some of the input arguments (E.g. values, for_all_de_morgan,named_individuals can be ignred for the time being) can be ignored for the time being

The goal is to omit return the result of the SPARQL query but only returning the 4 scalar values representing the quality of an OWL Class expression given positive and negative examples. Below, we have the computation of tp, fp, fn, and tn

def f1(*, individuals: Set, pos: Set, neg: Set):
    assert isinstance(individuals, set)
    assert isinstance(pos, set)
    assert isinstance(neg, set)

    tp = len(pos.intersection(individuals))
    tn = len(neg.difference(individuals))

    fp = len(neg.intersection(individuals))
    fn = len(pos.difference(individuals))

    try:
        recall = tp / (tp + fn)
    except ZeroDivisionError:
        return 0

    try:
        precision = tp / (tp + fp)
    except ZeroDivisionError:
        return 0

    if precision == 0 or recall == 0:
        return 0

    f_1 = 2 * ((precision * recall) / (precision + recall))
    return f_1

def owl_expression_to_sparql_exist()->Boolean

This function returns True of False depending on whether a single OWLNamedIndividual occurs in the result set

Demirrr commented 1 month ago

Completed with https://github.com/dice-group/owlapy/pull/85