owlapy.utils

Owlapy utils.

Attributes

measurer

transformer

Classes

OWLClassExpressionLengthMetric

Length calculation of OWLClassExpression

EvaluatedDescriptionSet

Abstract base class for generic types.

ConceptOperandSorter

CESimplifier

Simplifies OWLClassExpression by removing redundant operands and normalizing the structure.

HasIndex

Interface for types with an index; this is used to group objects by type when sorting.

OrderedOWLObject

Holder of OWL Objects that can be used for Python sorted.

NNF

This class contains functions to transform a Class Expression into Negation Normal Form.

LRUCache

Constants shares by all lru cache instances.

Functions

jaccard_similarity(→ float)

Calculate the Jaccard similarity between two sets.

f1_set_similarity(→ float)

Calculate the F1 score between two sets.

run_with_timeout(func, timeout[, args])

concept_reducer(concepts, opt)

Reduces a set of concepts by applying a binary operation to each pair of concepts.

concept_reducer_properties(...)

Map a set of owl concepts and a set of properties into OWL Restrictions

get_expression_length(→ int)

get_top_level_cnf(...)

Convert a class expression into Top-Level Conjunctive Normal Form. Operands will be sorted.

get_top_level_dnf(...)

Convert a class expression into Top-Level Disjunctive Normal Form. Operands will be sorted.

get_remaining(original_set, common_part, type_b)

Used in factorization function 'factor_nary_expression'.

factor_nary_expression(expr[, ...])

Factor a common operand from a top-level Union (⊔) or Intersection (⊓) if possible. This

combine_nary_expressions(…)

Shortens an OWLClassExpression or OWLDataRange by combining all nested nary expressions of the same type.

iter_count(→ int)

Count the number of elements in an iterable.

as_index(→ HasIndex)

Cast OWL Object to HasIndex.

simplify_class_expression(...)

Simplify a class expression by removing redundant expressions and sorting operands.

Module Contents

owlapy.utils.jaccard_similarity(set1, set2) float[source]

Calculate the Jaccard similarity between two sets.

Parameters:
  • set1 – First set

  • set2 – Second set

Returns:

intersection(set1, set2) / union(set1, set2)

Return type:

Jaccard similarity

owlapy.utils.f1_set_similarity(set1, set2) float[source]

Calculate the F1 score between two sets.

Parameters:
  • set1 – First set (treated as ground truth)

  • set2 – Second set (treated as prediction)

Returns:

F1 score

owlapy.utils.run_with_timeout(func, timeout, args=(), **kwargs)[source]
owlapy.utils.concept_reducer(concepts: Iterable, opt: Callable)[source]

Reduces a set of concepts by applying a binary operation to each pair of concepts.

Parameters:
  • concepts (set) – A set of concepts to be reduced.

  • opt (function) – A binary function that takes a pair of concepts and returns a single concept.

Returns:

A set containing the results of applying the binary operation to each pair of concepts.

Return type:

set

Example

>>> concepts = {1, 2, 3}
>>> opt = lambda x: x[0] + x[1]
>>> concept_reducer(concepts, opt)
{2, 3, 4, 5, 6}

Note

The operation opt should be commutative and associative to ensure meaningful reduction in the context of set operations.

owlapy.utils.concept_reducer_properties(concepts: Iterable, properties, cls: Callable = None, cardinality: int = 2) Iterable[owlapy.class_expression.OWLQuantifiedObjectRestriction | owlapy.class_expression.OWLObjectCardinalityRestriction][source]

Map a set of owl concepts and a set of properties into OWL Restrictions

Parameters:
  • concepts

  • properties

  • cls (Callable) – An owl Restriction class

  • cardinality – A positive Integer

Returns: List of OWL Restrictions

class owlapy.utils.OWLClassExpressionLengthMetric(*, class_length: int, object_intersection_length: int, object_union_length: int, object_complement_length: int, object_some_values_length: int, object_all_values_length: int, object_has_value_length: int, object_cardinality_length: int, object_has_self_length: int, object_one_of_length: int, data_some_values_length: int, data_all_values_length: int, data_has_value_length: int, data_cardinality_length: int, object_property_length: int, object_inverse_length: int, data_property_length: int, datatype_length: int, data_one_of_length: int, data_complement_length: int, data_intersection_length: int, data_union_length: int)[source]

Length calculation of OWLClassExpression

Parameters:
  • class_length – Class: “C”

  • object_intersection_length – Intersection: A ⨅ B

  • object_union_length – Union: A ⨆ B

  • object_complement_length – Complement: ¬ C

  • object_some_values_length – Obj. Some Values: ∃ r.C

  • object_all_values_length – Obj. All Values: ∀ r.C

  • object_has_value_length – Obj. Has Value: ∃ r.{I}

  • object_cardinality_length – Obj. Cardinality restriction: ≤n r.C

  • object_has_self_length – Obj. Self restriction: ∃ r.Self

  • object_one_of_length – Obj. One of: ∃ r.{X,Y,Z}

  • data_some_values_length – Data Some Values: ∃ p.t

  • data_all_values_length – Data All Values: ∀ p.t

  • data_has_value_length – Data Has Value: ∃ p.{V}

  • data_cardinality_length – Data Cardinality restriction: ≤n r.t

  • object_property_length – Obj. Property: ∃ r.C

  • object_inverse_length – Inverse property: ∃ r⁻.C

  • data_property_length – Data Property: ∃ p.t

  • datatype_length – Datatype: ^^datatype

  • data_one_of_length – Data One of: ∃ p.{U,V,W}

  • data_complement_length – Data Complement: ¬datatype

  • data_intersection_length – Data Intersection: datatype ⨅ datatype

  • data_union_length – Data Union: datatype ⨆ datatype

__slots__ = ('class_length', 'object_intersection_length', 'object_union_length',...
class_length: int
object_intersection_length: int
object_union_length: int
object_complement_length: int
object_some_values_length: int
object_all_values_length: int
object_has_value_length: int
object_cardinality_length: int
object_has_self_length: int
object_one_of_length: int
data_some_values_length: int
data_all_values_length: int
data_has_value_length: int
data_cardinality_length: int
object_property_length: int
object_inverse_length: int
data_property_length: int
datatype_length: int
data_one_of_length: int
data_complement_length: int
data_intersection_length: int
data_union_length: int
static get_default() OWLClassExpressionLengthMetric[source]
abstractmethod length(o: owlapy.owl_object.OWLObject) int[source]
owlapy.utils.measurer
owlapy.utils.get_expression_length(ce: owlapy.class_expression.OWLClassExpression) int[source]
class owlapy.utils.EvaluatedDescriptionSet(ordering: Callable[[_N], _O], max_size: int = 10)[source]

Bases: Generic[_N, _O]

Abstract base class for generic types.

A generic type is typically declared by inheriting from this class parameterized with one or more type variables. For example, a generic mapping type might be defined as:

class Mapping(Generic[KT, VT]):
    def __getitem__(self, key: KT) -> VT:
        ...
    # Etc.

This class can then be used as follows:

def lookup_name(mapping: Mapping[KT, VT], key: KT, default: VT) -> VT:
    try:
        return mapping[key]
    except KeyError:
        return default
__slots__ = ('items', '_max_size', '_Ordering')
items: SortedSet[_N]
maybe_add(node: _N)[source]
clean()[source]
worst()[source]
best()[source]
best_quality_value() float[source]
__iter__() Iterable[_N][source]
class owlapy.utils.ConceptOperandSorter[source]
abstractmethod sort(o: _O) _O[source]
owlapy.utils.get_top_level_cnf(ce: owlapy.class_expression.OWLClassExpression) owlapy.class_expression.OWLClassExpression[source]

Convert a class expression into Top-Level Conjunctive Normal Form. Operands will be sorted.

Parameters:

ce – Class Expression.

Returns:

Class Expression in Top-Level Conjunctive Normal Form.

owlapy.utils.get_top_level_dnf(ce: owlapy.class_expression.OWLClassExpression) owlapy.class_expression.OWLClassExpression[source]

Convert a class expression into Top-Level Disjunctive Normal Form. Operands will be sorted.

Parameters:

ce – Class Expression.

Returns:

Class Expression in Top-Level Disjunctive Normal Form.

owlapy.utils.get_remaining(original_set, common_part, type_b)[source]

Used in factorization function ‘factor_nary_expression’.

owlapy.utils.factor_nary_expression(expr: owlapy.class_expression.OWLObjectIntersectionOf | owlapy.class_expression.OWLObjectUnionOf, transform_to_dnf_on_first_iteration=False)[source]

Factor a common operand from a top-level Union (⊔) or Intersection (⊓) if possible. This factorization takes into consideration only boolean construction. Restrictions are not considered (use CESimplifier) for that.

class owlapy.utils.CESimplifier[source]

Simplifies OWLClassExpression by removing redundant operands and normalizing the structure. Simplifications include: - Merging redundant cardinality restrictions - Merging redundant existential restrictions - Normalizing union and intersection of class expressions - Absorption of redundant class expressions - Converting to negation normal form (NNF) - and more…

Note: This simplifier follows Unique Name Assumption (UNA) meaning that every class/individual/property is unique as long as its name is unique.

sorter
simplify(o: owlapy.class_expression.OWLClassExpression) owlapy.class_expression.OWLClassExpression[source]
datatype_restriction_inwards_simplification(n)[source]
class owlapy.utils.HasIndex[source]

Bases: Protocol

Interface for types with an index; this is used to group objects by type when sorting.

type_index: ClassVar[int]
__eq__(other)[source]
class owlapy.utils.OrderedOWLObject(o: _HasIndex)[source]

Holder of OWL Objects that can be used for Python sorted.

The Ordering is dependent on the type_index of the impl. classes recursively followed by all components of the OWL Object.

o

OWL object.

__slots__ = ('o', '_chain')
o: _HasIndex
__lt__(other)[source]
__eq__(other)[source]
class owlapy.utils.NNF[source]

This class contains functions to transform a Class Expression into Negation Normal Form.

abstractmethod get_class_nnf(ce: owlapy.class_expression.OWLClassExpression, negated: bool = False) owlapy.class_expression.OWLClassExpression[source]

Convert a Class Expression to Negation Normal Form. Operands will be sorted.

Parameters:
  • ce – Class Expression.

  • negated – Whether the result should be negated.

Returns:

Class Expression in Negation Normal Form.

owlapy.utils.combine_nary_expressions(ce: owlapy.class_expression.OWLClassExpression) owlapy.class_expression.OWLClassExpression[source]
owlapy.utils.combine_nary_expressions(ce: owlapy.owl_data_ranges.OWLDataRange) owlapy.owl_data_ranges.OWLDataRange

Shortens an OWLClassExpression or OWLDataRange by combining all nested nary expressions of the same type. Operands will be sorted.

E.g. OWLObjectUnionOf(A, OWLObjectUnionOf(C, B)) -> OWLObjectUnionOf(A, B, C).

owlapy.utils.iter_count(i: Iterable) int[source]

Count the number of elements in an iterable.

owlapy.utils.as_index(o: owlapy.owl_object.OWLObject) HasIndex[source]

Cast OWL Object to HasIndex.

class owlapy.utils.LRUCache(maxsize: int | None = None)[source]

Bases: Generic[_K, _V]

Constants shares by all lru cache instances.

Adapted from functools.lru_cache.

sentinel

Unique object used to signal cache misses.

PREV

Name for the link field 0.

NEXT

Name for the link field 1.

KEY

Name for the link field 2.

RESULT

Name for the link field 3.

sentinel
cache
full = False
cache_get
cache_len
lock
root = []
maxsize = None
__contains__(item: _K) bool[source]
__getitem__(item: _K) _V[source]
__setitem__(key: _K, value: _V)[source]
cache_info()[source]

Report cache statistics.

cache_clear()[source]

Clear the cache and cache statistics.

owlapy.utils.transformer
owlapy.utils.simplify_class_expression(ce: owlapy.class_expression.OWLClassExpression) owlapy.class_expression.OWLClassExpression[source]

Simplify a class expression by removing redundant expressions and sorting operands.