📘 Characteristics#

The galactic.algebras.convex.characteristics.core module defines the foundational building blocks of characteristics.

It provides the Characteristic protocol, JSONPath-based Component extraction, and concrete value types like:

Characteristics compute lists of possible values (imprecision) from item data and combine component outputs through an abstract _call() method. The module also supplies an identity component and a cache() decorator to memoize characteristic evaluations.

Creating components#

Components are the atomic elements constituting a characteristic. They are defined using a JSONPath-based expression and are able to give each datum an iterator of possible values representing imprecision using the find() method.

from frozendict import frozendict
from galactic.algebras.convex.characteristics.core import Component
component = Component("$.value")
display(component)
print(component)
dataset = [
    frozendict({"value": 5}),
    frozendict(),
]
display(list(component.find(dataset[0])))
display(list(component.find(dataset[1])))
Component('$.value')
$.value
[5]
[]

Creating characteristics#

A characteristic is created from a sequence of components and is determined by its optional name and other parameters depending on the characteristic type. Its string representation is done using a template with the mechanism of the format() method.

from frozendict import frozendict
from galactic.algebras.convex.characteristics.core import (
    Component,
    Integer,
)
component = Component("$.value")
characteristic = Integer(component)
display(str(characteristic))
dataset = [
    frozendict({"value": 1}),
    frozendict({"value": "a"}),
    frozendict({}),
]
display(
    characteristic(dataset[0]),
    characteristic(dataset[1]),
    characteristic(dataset[2]),
)
characteristic = Integer(Component("$.value[:]"))
display(str(characteristic))
dataset = [
     frozendict({"value": (1.2, 2.3)}),
     frozendict({"value": (3,)}),
]
display(
    characteristic(dataset[0]),
    characteristic(dataset[1]),
)
'int($.value)'
[1]
[]
[]
'int($.value.[*])'
[1, 2]
[3]

Caching characteristics#

Calls to characteristics can be cached using the cache() decorator:

from frozendict import frozendict
from galactic.algebras.convex.characteristics.core import (
    Component,
    Integer,
    cache,
)
component = Component("$.value")
characteristic = cache()(Integer(component))
display(characteristic.cache_info())
dataset = [
    frozendict({"value": 1}),
    frozendict({"value": "a"}),
    frozendict({}),
]
display(
    characteristic(dataset[0]),
    characteristic(dataset[1]),
    characteristic(dataset[2]),
)
display(characteristic.cache_info())
display(
    characteristic(dataset[0]),
    characteristic(dataset[1]),
    characteristic(dataset[2]),
)
display(characteristic.cache_info())
characteristic.cache_clear()
display(characteristic.cache_info())
CacheInfo(hits=0, misses=0, maxsize=None, currsize=0)
[1]
[]
[]
CacheInfo(hits=0, misses=3, maxsize=None, currsize=3)
[1]
[]
[]
CacheInfo(hits=3, misses=3, maxsize=None, currsize=3)
CacheInfo(hits=0, misses=0, maxsize=None, currsize=0)