galactic.algebras.convex.descriptions.core

Contents

galactic.algebras.convex.descriptions.core#

galactic.algebras.convex.descriptions.core module

Defines core components for descriptions in convex algebras.

Description

Galois connection

  • Concept: Represents a concept in a Galois connection

  • Extent: Represents the extent of a concept in a Galois connection

  • Intent: Represents the intent of a concept in a Galois connection

  • Convex: Represents a closed set of predicates for a description space

  • ItemPolarity: Represents item polarity in a Galois connection

  • PredicatePolarity: Represents predicate polarity in a Galois connection

  • GaloisConnection: Implements the Galois connection

Context

  • Context: Represents the context of a Galois connection

  • PredicateUniverse: Represents the universe of predicates in a context


class Description(**data)#

Bases: Resource, ABC

Represents description spaces.

A description space must have a _call() method able to compute the minimal descriptors for a collection of values; i.e. the borders. It is used to compute the description for a collection of items. It is an abstract class that should be subclassed to implement specific description spaces.

Parameters:

space – A tuple of characteristics

Raises:

TypeError – If any element in the space is not an instance of Characteristic

Notes

  • The __call__ method is designed to be used as a callable description, allowing instances of this class to be used directly in contexts where a description is required. It takes a collection of items and computes the description based on the characteristics defined in the space.

  • The __call__ method iterates over the items, applying the characteristics to each item’s value. If any characteristic returns None, the method returns an empty iterator, indicating that no valid descriptions can be computed. Otherwise, it calls the _call method to compute the actual descriptions based on the values returned by the characteristics.

  • The space property returns the collection of characteristics that define the description space. This allows access to the characteristics used in the description, which can be useful for understanding or modifying the description’s behavior.

  • When subclassing this class, ensure to implement the _call method to provide the specific logic for computing descriptions based on the values of the characteristics.

__call__(items)#

Compute the description of the items.

Parameters:

items (Iterable[Item]) – A collection of items

Returns:

An iterator over a set of descriptors representing the borders of the items based on the characteristics defined in the description space

Return type:

Iterator[Predicate]

__contains__(attr)#

Check if the predicate is in the description space.

Parameters:

attr (object) – The predicate to check for membership in the description space

Returns:

True if the predicate is in the description space, False otherwise

Return type:

bool

Notes

  • The attr parameter is expected to be an instance of Predicate.

  • The method should return True if the predicate is part of the description space defined by the characteristics in the description, and False otherwise.

  • The method checks if the predicate’s characteristics match the description space and if the predicate is contained within the description space using the _contains method. The _contains method should return True when the predicate is an instance of yielded predicates of the _call method.

model_config: ClassVar[ConfigDict] = {'validate_assignment': True}#

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

class BooleanDescription(**data)#

Bases: Description

A description of a boolean characteristic.

This class represents a description of a boolean characteristic. It validates that the space contains exactly one boolean characteristic and describes a collection of values by yielding boolean predicates.

Parameters:

space (tuple[Boolean]) – A tuple containing a single boolean characteristic.

ALIAS#

The alias of the type in the resource registry.

Type:

ClassVar[str] = “core.description.Boolean”

Examples

>>> from galactic.algebras.concept.core import ItemUniverse
>>> from galactic.algebras.convex.characteristics.core import Boolean, Component
>>> from galactic.algebras.convex.descriptions.core import BooleanDescription
>>> data = [
...     [1, 0, 0, 0],
...     [0, 1, 0, 0],
...     [0, 1, 1, 0],
...     [0, 0, 1, 0],
...     [0, 0, 1, 1],
... ]
>>> universe = ItemUniverse(data)
>>> component = Component(expr="[2]")
>>> characteristic = Boolean(components=(component,), name="attribute")
>>> description = BooleanDescription(space=(characteristic,))
>>> [str(attr) for attr in description(universe)]
[]
>>> [str(attr) for attr in description(universe[2:])]
['attribute']

Configuration

If you want to specify a boolean desription in a TOML file, you can use the following syntax:

[components.my_component]
"$type" = "core.Component"
expr = "value"

[characteristics.my_characteristic]
"$type" = "core.characteristic.Boolean"
components = [
  {"$ref" = "components.my_component"}
]
name = "my_name"

[descriptions.my_description]
"$type" = "core.description.Boolean"
space = [
  {"$ref" = "characteristics.my_characteristic"}
]
model_config: ClassVar[ConfigDict] = {'validate_assignment': True}#

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

class Concept(connection, items=None, attrs=None)#

Bases: Concept

Represents a convex concept in Formal Concept Analysis (FCA).

Parameters:

Notes

  • The Concept class represents a convex concept in Formal Concept Analysis (FCA), which is a mathematical framework for analyzing the relationships between objects and their attributes.

  • It is built upon a Galois connection, which is a pair of monotone functions that establish a relationship between two partially ordered sets.

  • The class allows for the creation of concepts based on a set of items and predicates, where items represent objects and predicates represent properties of those objects.

  • The concept can be constructed either by providing a set of items or by specifying a set of predicates that define the concept.

  • Providing both items and attrs is ambiguous and raises a ValueError.

  • The extent property returns the set of items that belong to the concept, while the intent property returns the set of predicates that define the concept.

  • The borders property provides a collection of predicates that describe the borders of the concept in the description space.

  • The __lt__, __le__, __gt__, and __ge__ methods allow comparison between concepts based on their extents, enabling the establishment of a partial order among concepts.

  • The __and__ and __or__ methods allow computing the meet and join of concepts, respectively, enabling the creation of new concepts from existing ones.

Examples

>>> from galactic.algebras.concept.core import ItemUniverse
>>> from galactic.algebras.convex.characteristics.core import Component, Integer
>>> from galactic.algebras.convex.descriptions.core import (
...     Context,
...     GaloisConnection,
...     PredicateUniverse,
... )
>>> from galactic.algebras.convex.descriptions.core import Concept
>>> from galactic.algebras.convex.predicates.examples.arithmetic.core import (
...     Multiple,
... )
>>> from galactic.algebras.convex.descriptions.examples.arithmetic.core import (
...     MultipleDescription
... )
>>> characteristic = Integer(components=(Component(),))
>>> description = MultipleDescription(space=(characteristic,))
>>> dataset = [36, 48, 16, 32]
>>> context = Context(
...     ItemUniverse(dataset),
...     PredicateUniverse(descriptions=(description,)),
... )
>>> connection = GaloisConnection(context)
>>> top = Concept(connection)
>>> top.extent
<galactic.algebras.convex.descriptions.core.Extent object at 0x...>
>>> [item.value for item in top.extent]
[36, 48, 16, 32]
>>> top.intent
<galactic.algebras.convex.descriptions.core.Intent object at 0x...>
>>> [str(attr) for attr in top.intent]
['M(int(@),4)']
>>> concept1 = Concept(
...     connection,
...     items=[context.domain[1], context.domain[2]],
... )
>>> [item.value for item in concept1.extent]
[48, 16, 32]
>>> [str(attr) for attr in concept1.intent]
['M(int(@),16)']
>>> multiple_12 = Multiple(space=description.space, number=12)
>>> concept2 = Concept(connection, attrs=[multiple_12])
>>> [item.value for item in concept2.extent]
[36, 48]
>>> [str(attr) for attr in concept2.intent]
['M(int(@),4)', 'M(int(@),3)']
>>> meet = concept1 & concept2
>>> [item.value for item in meet.extent]
[48]
>>> [str(attr) for attr in meet.intent]
['M(int(@),16)', 'M(int(@),3)']
>>> meet <= concept1
True
>>> meet <= concept2
True
>>> join = concept1 | concept2
>>> [item.value for item in join.extent]
[36, 48, 16, 32]
>>> [str(attr) for attr in join.intent]
['M(int(@),4)']
>>> join >= concept1
True
>>> join >= concept2
True
__hash__()#

Compute the hash value of the concept.

Returns:

The hash value of the concept

Return type:

int

__eq__(other)#

Check whether this concept is equal to another object.

Parameters:

other (object) – The other object with which to compare

Return type:

bool

__lt__(other)#

Check if this concept is less than another concept.

Parameters:

other (object) – The other concept to compare with

Returns:

True if this concept is less than the other concept, False otherwise

Return type:

bool

__le__(other)#

Check if this concept is less than or equal to another concept.

Parameters:

other (object) – The other concept to compare with

Returns:

True if this concept is less than or equal to the other concept, False otherwise

Return type:

bool

__gt__(other)#

Check if this concept is greater than another concept.

Parameters:

other (object) – The other concept to compare with

Returns:

True if this concept is greater than the other concept, False otherwise

Return type:

bool

__ge__(other)#

Check if this concept is greater than or equal to another concept.

Parameters:

other (object) – The other concept to compare with

Returns:

True if this concept is greater than or equal to the other concept, False otherwise

Return type:

bool

__or__(other)#

Compute the join of two concepts.

Parameters:

other (object) – Another concept to join with

Returns:

The join of the two concepts

Return type:

Self

join(*others)#

Compute the join of multiple concepts.

Parameters:

*others (Self) – Other concepts to join with

Returns:

The join of the concepts

Return type:

Self

Raises:
  • TypeError – If any of the other concepts is not of the same type as self

  • ValueError – If any of the other concepts does not share the same connection as self

__and__(other)#

Compute the meet of two concepts.

Parameters:

other (object) – Another concept to meet with

Returns:

The meet of the two concepts

Return type:

Self

meet(*others)#

Compute the meet of multiple concepts.

Parameters:

*others (Self) – Other concepts to meet with

Returns:

The meet of the concepts

Return type:

Self

Raises:
  • TypeError – If any of the other concepts is not of the same type as self

  • ValueError – If any of the other concepts does not share the same connection as self

property connection: GaloisConnection#

Get the underlying antitone Galois connection.

Returns:

The antitone Galois connection

Return type:

GaloisConnection

property intent: Intent#

Get the intent of the concept.

Returns:

The intent of the concept

Return type:

Intent

property convexes: tuple[Convex, ...]#

Get the convexes of the concept.

It’s a proxy to self.intent.convexes.

Returns:

The convexes of the concept

Return type:

tuple[Convex, …]

property extent: Extent#

Get the extent of the concept.

Returns:

The extent of the concept

Return type:

Extent

class Extent(connection, attrs=None)#

Bases: Extent

Represents a finite closed set of items.

Parameters:

Examples

>>> from galactic.algebras.concept.core import ItemUniverse
>>> from galactic.algebras.convex.characteristics.core import Component, Integer
>>> from galactic.algebras.convex.descriptions.core import (
...     Context,
...     GaloisConnection,
...     PredicateUniverse,
... )
>>> from galactic.algebras.convex.descriptions.examples.arithmetic.core import (
...     MultipleDescription,
... )
>>> from galactic.algebras.convex.predicates.examples.arithmetic.core import (
...     Multiple,
... )
>>> characteristic = Integer(components=(Component(),))
>>> description = MultipleDescription(space=(characteristic,))
>>> dataset = [36, 48, 16, 32]
>>> context = Context(
...     ItemUniverse(dataset),
...     PredicateUniverse(descriptions=(description,)),
... )
>>> connection = GaloisConnection(context)
>>> extent = Extent(
...     connection,
...     [
...         Multiple(space=description.space, number=4),
...         Multiple(space=description.space, number=3),
...     ],
... )
>>> extent
<galactic.algebras.convex.descriptions.core.Extent object at 0x...>
>>> [item.value for item in extent]
[36, 48]
>>> len(extent)
2

Notes

  • The Extent class represents a finite closed set of items that share a common set of predicates.

  • The attrs parameter is used to define the common predicates that characterize the items in the extent. The extent will include all items that satisfy all the specified predicates.

  • The connection parameter is used to link the extent to the Galois connection, which allows for the computation of the corresponding intent and for checking membership of items in the extent.

Hint

The Extent class is typically used in conjunction with the Intent class to define the Galois connection between items and predicates, where the extent represents the set of items that share a common set of predicates, and the intent represents the set of predicates that are shared by a common set of items.

static __new__(cls, connection=None, attrs=None)#

Create a new extent object.

Parameters:
Return type:

Self

Notes

If the extent is created with only one intent with known extent, it returns that extent, otherwise it returns a new one.

__or__(other)#

Compute the join with another extent.

Parameters:

other (object) – The other extent

Returns:

The join of the two extents

Return type:

Self

Raises:

ValueError – If the extents belong to different connections

join(*others)#

Compute the join with multiple extents.

Parameters:

*others (Self) – The other extents

Returns:

The join of the extents

Return type:

Self

Raises:
  • TypeError – If any of the others is not an Extent

  • ValueError – If any of the extents belong to different connections

__and__(other)#

Compute the meet with another extent.

Parameters:

other (object) – The other extent

Returns:

The meet of the two extents

Return type:

Self

Raises:

ValueError – If the extents belong to different connections

meet(*others)#

Compute the meet with multiple extents.

Parameters:

*others (Extent) – The other extents

Returns:

The meet of the extents

Return type:

Self

Raises:
  • TypeError – If any of the others is not an Extent

  • ValueError – If any of the extents belong to different connections

property connection: GaloisConnection#

Get the connection.

Returns:

The connection

Return type:

GaloisConnection

class Intent(connection, items=None)#

Bases: Closed[Predicate]

Represents a closed set of attributes.

Parameters:
  • connection (GaloisConnection) – The Galois connection

  • items (Iterable[Item] | None, default: None) – The items sharing the attributes in the closed set

Examples

>>> from galactic.algebras.concept.core import ItemUniverse
>>> from galactic.algebras.convex.characteristics.core import Component, Integer
>>> from galactic.algebras.convex.descriptions.core import (
...     Context,
...     GaloisConnection,
...     PredicateUniverse,
... )
>>> from galactic.algebras.convex.descriptions.examples.arithmetic.core import (
...     DivisorDescription,
...     MultipleDescription,
... )
>>> characteristic = Integer(components=(Component(),))
>>> dataset = [36, 48, 16, 32]
>>> context = Context(
...     ItemUniverse(dataset),
...     PredicateUniverse(
...         descriptions=(
...             DivisorDescription(space=(characteristic,)),
...             MultipleDescription(space=(characteristic,)),
...         ),
...     )
... )
>>> connection = GaloisConnection(context)
>>> intent = Intent(connection, [context.domain[0], context.domain[1]])
>>> intent
<galactic.algebras.convex.descriptions.core.Intent object at 0x...>
>>> [str(attr) for attr in intent]
['D(int(@),144)', 'M(int(@),4)', 'M(int(@),3)']
>>> # Display the predicates for the description space #0
>>> [str(attr) for attr in intent.convexes[0]]
['D(int(@),144)']
>>> # Display the predicates for the description space #1
>>> [str(attr) for attr in intent.convexes[1]]
['M(int(@),4)', 'M(int(@),3)']
static __new__(cls, connection=None, items=None)#

Create a new intent object.

Notes

If the intent is created with only one extent with known intent, it returns that intent, otherwise it returns a new one.

Return type:

Self

__hash__()#

Compute the hash value of the intent.

Returns:

The hash value of the intent

Return type:

int

__eq__(other)#

Check if self is equal to other.

Parameters:

other (object) – The other intent to compare with

Returns:

True if self is equal to other.

Return type:

bool

Notes

Equality is determined by comparing the bitmaps of the corresponding extents, i.e., two intents are equal if their corresponding extents contain the same elements.

__lt__(other)#

Check if self is less than other.

Parameters:

other (object) – The other intent to compare with

Returns:

True if self is less than other.

Return type:

bool

Notes

An intent is considered less than another if it is a proper subset of the other.

__le__(other)#

Check if self is less than or equal to other.

Parameters:

other (object) – The other intent to compare with

Returns:

True if self is less than or equal to other.

Return type:

bool

Notes

An intent is considered less than or equal to another if it is a subset of the other.

__gt__(other)#

Check if self is greater than other.

Parameters:

other (object) – The other intent to compare with

Returns:

True if self is greater than other.

Return type:

bool

Notes

An intent is considered greater than another if it is a proper superset of the other.

__ge__(other)#

Check if self is greater than or equal to other.

Parameters:

other (object) – The other intent to compare with

Returns:

True if self is greater than or equal to other.

Return type:

bool

Notes

An intent is considered greater than or equal to another if it is a superset of the other.

__contains__(attr)#

Check if the predicate is in the intent.

Parameters:

attr (object) – The predicate to check for membership

Returns:

True if the predicate is in the intent, False otherwise

Return type:

bool

__iter__()#

Iterate over the borders in the intent.

Returns:

An iterator over the borders in the intent

Return type:

Iterator[Predicate]

__len__()#

Get the number of borders in the intent.

Returns:

The number of borders in the intent

Return type:

int

__or__(other)#

Compute the join with another intent.

Parameters:

other (object) – The other intent

Returns:

The join of the two intents

Return type:

Self

join(*others)#

Compute the join with multiple intents.

Parameters:

*others (Self) – The other intents

Returns:

The join of the intents

Return type:

Self

Raises:
  • TypeError – If any of the others is not an Intent

  • ValueError – If any of the intents belong to different connections

__and__(other)#

Compute the meet with another intent.

Parameters:

other (object) – The other intent

Returns:

The meet of the two intents

Return type:

Self

meet(*others)#

Compute the meet with multiple intents.

Parameters:

*others (Self) – The other intents

Returns:

The meet of the intents

Return type:

Self

Raises:
  • TypeError – If any of the others is not an Intent

  • ValueError – If any of the intents belong to different connections

property connection: GaloisConnection#

Get the connection.

Returns:

The connection

Return type:

GaloisConnection

property convexes: tuple[Convex, ...]#

Get the sequence of convexes.

Returns:

The sequence of convexes

Return type:

tuple[Convex, …]

class Convex(connection, index, *items)#

Bases: Closed[Predicate]

A convex is a closed set of predicates in a description space.

Parameters:
  • connection (GaloisConnection) – The Galois connection

  • index (int) – The index of the description space

  • items (Iterable[Item]) – The items sharing the predicates in the closed set

__eq__(other)#

Check if self is equal to other.

Parameters:

other (object) – The other convex to compare with

Returns:

True if self is equal to other.

Return type:

bool

Notes

Equality is determined by comparing the sets of items in both convexes, i.e., two convexes are equal if their corresponding item sets are identical.

__lt__(other)#

Check if self is less than other.

Parameters:

other (object) – The other convex to compare with

Returns:

True if self is less than other.

Return type:

bool

Notes

A convex is considered less than another if it is a proper subset of the other.

__le__(other)#

Check if self is less than or equal to other.

Parameters:

other (object) – The other convex to compare with

Returns:

True if self is less than or equal to other.

Return type:

bool

Notes

A convex is considered less than or equal to another if it is a subset of the other.

__gt__(other)#

Check if self is greater than other.

Parameters:

other (object) – The other convex to compare with

Returns:

True if self is greater than other.

Return type:

bool

Notes

A convex is considered greater than another if it is a proper superset of the other.

__ge__(other)#

Check if self is greater than or equal to other.

Parameters:

other (object) – The other convex to compare with

Returns:

True if self is greater than or equal to other.

Return type:

bool

Notes

A convex is considered greater than or equal to another if it is a superset of the other.

__or__(other)#

Compute the join with another convex.

Parameters:

other (object) – The other convex

Returns:

The join of the two convexes

Return type:

Self

join(*others)#

Compute the join with multiple convexes.

Parameters:

*others (Self) – The other convexes

Returns:

The join of the convexes

Return type:

Self

Raises:
  • TypeError – If any of the others is not a Convex

  • ValueError – If any of the extents belong to different connections

__and__(other)#

Compute the meet with another convex.

Parameters:

other (object) – The other convex

Returns:

The meet of the two convexes

Return type:

Self

meet(*others)#

Compute the meet with multiple convexes.

Parameters:

*others (Self) – The other convexes

Returns:

The meet of the convexes

Return type:

Self

Raises:
  • TypeError – If any of the others is not a Convex

  • ValueError – If any of the extents belong to different connections

__iter__()#

Iterate over the borders in the convex.

Returns:

An iterator over the borders in the convex

Return type:

Iterator[Predicate]

__contains__(attr)#

Check if the predicate is in the convex.

Parameters:

attr (object) – The predicate to check for membership

Returns:

True if the predicate is in the convex, False otherwise

Return type:

bool

property connection: GaloisConnection#

Get the connection.

Returns:

The connection

Return type:

GaloisConnection

property index: int#

Get the description index.

Returns:

The description index

Return type:

int

class ItemPolarity(connection)#

Bases: Polarity[Predicate, Item]

Represents the polarity of items.

Parameters:

connection (GaloisConnection) – A Galois connection

Examples

>>> from galactic.algebras.concept.core import ItemUniverse
>>> from galactic.algebras.convex.characteristics.core import Component, Integer
>>> from galactic.algebras.convex.descriptions.core import (
...     Context,
...     GaloisConnection,
...     PredicateUniverse,
... )
>>> from galactic.algebras.convex.descriptions.examples.arithmetic.core import (
...     MultipleDescription
... )
>>> from galactic.algebras.convex.predicates.examples.arithmetic.core import (
...     Multiple,
... )
>>> characteristic = Integer(components=(Component(),))
>>> description = MultipleDescription(space=(characteristic,))
>>> dataset = [36, 48, 16, 32]
>>> context = Context(
...     ItemUniverse(dataset),
...     PredicateUniverse(descriptions=(description,))
... )
>>> connection = GaloisConnection(context)
>>> extent = connection.polarities[1](
...     [
...         Multiple(space=description.space,  number=4),
...         Multiple(space=description.space, number=3),
...     ],
... )
>>> extent
<galactic.algebras.convex.descriptions.core.Extent object at 0x...>
>>> [item.value for item in extent]
[36, 48]
__call__(*elements)#

Compute the correspondence of elements.

Parameters:

*elements (Iterable[Predicate]) – Iterable of predicates

Returns:

The correspondence of the elements

Return type:

Extent

Raises:

ValueError – If an element is not in the context co-domain

property connection: GaloisConnection#

Get the connection.

Returns:

The connection

Return type:

GaloisConnection

class PredicatePolarity(connection)#

Bases: object

Represents the polarity of predicates.

Parameters:

connection (GaloisConnection) – A Galois connection

Examples

>>> from galactic.algebras.concept.core import ItemUniverse
>>> from galactic.algebras.convex.characteristics.core import Component, Integer
>>> from galactic.algebras.convex.descriptions.core import (
...     Context,
...     GaloisConnection,
...     PredicateUniverse,
... )
>>> from galactic.algebras.convex.descriptions.examples.arithmetic.core import (
...     MultipleDescription
... )
>>> characteristic = Integer(components=(Component(),))
>>> description = MultipleDescription(space=(characteristic,))
>>> dataset = [36, 48, 16, 32]
>>> context = Context(
...     ItemUniverse(dataset),
...     PredicateUniverse(descriptions=(description,)),
... )
>>> connection = GaloisConnection(context)
>>> intent = connection.polarities[0](
...     [
...         context.domain[0],
...         context.domain[1],
...     ],
... )
>>> [str(border) for border in intent]
['M(int(@),4)', 'M(int(@),3)']
__call__(*elements)#

Compute the correspondence of elements.

Parameters:

*elements (Iterable[Item]) – Iterable of items

Returns:

The correspondence of the elements

Return type:

Intent

Raises:

ValueError – If an element is not in the context domain

property connection: GaloisConnection#

Get the connection.

Returns:

The connection

Return type:

GaloisConnection

class GaloisConnection(context)#

Bases: GaloisConnection[Item, Predicate]

Represents an antitone Galois connection.

It is obtained using an item universe and a description.

Parameters:

context (Context) – The context of the Galois connection, which includes the item universe and the predicate universe, as well as the binary relation between them.

Examples

>>> from galactic.algebras.concept.core import ItemUniverse
>>> from galactic.algebras.convex.characteristics.core import Component, Integer
>>> from galactic.algebras.convex.descriptions.core import (
...     Context,
...     GaloisConnection,
...     PredicateUniverse,
... )
>>> from galactic.algebras.convex.descriptions.examples.arithmetic.core import (
...     MultipleDescription,
... )
>>> characteristic = Integer(components=(Component(),))
>>> description = MultipleDescription(space=(characteristic,))
>>> dataset = [36, 48, 16, 32]
>>> context = Context(
...     ItemUniverse(dataset),
...     PredicateUniverse(descriptions=(description,)),
... )
>>> connection = GaloisConnection(context)
>>> connection
<galactic.algebras.convex.descriptions.core.GaloisConnection object at 0x...>
property polarities: tuple[PredicatePolarity, ItemPolarity]#

Get the polarities.

Returns:

The polarities

Return type:

tuple[PredicatePolarity, ItemPolarity]

property context: Context#

Get the context.

Returns:

The context

Return type:

Context

class Context(domain, co_domain)#

Bases: BinaryRelation[Item, Predicate]

Represents contexts in generalized convex hull FCA.

Parameters:
  • domain (ItemUniverse) – The item universe

  • co_domain (PredicateUniverse) – The predicate universe

Examples

>>> from galactic.algebras.convex.characteristics.core import Component, Integer
>>> from galactic.algebras.convex.descriptions.core import Context
>>> from galactic.algebras.convex.descriptions.examples.arithmetic.core import (
...     DivisorDescription,
...     MultipleDescription,
... )
>>> from galactic.algebras.convex.predicates.examples.arithmetic.core import (
...     Multiple,
... )
>>> characteristic = Integer(components=(Component(),))
>>> descriptions = (
...     DivisorDescription(space=(characteristic,)),
...     MultipleDescription(space=(characteristic,)),
... )
>>> dataset = [36, 48, 16, 32]
>>> context = Context(
...     ItemUniverse(dataset),
...     PredicateUniverse(descriptions=descriptions),
... )
>>> context
<galactic.algebras.convex.descriptions.core.Context object at 0x...>
>>> [item.value for item in context.domain]
[36, 48, 16, 32]
>>> multiple_3 = Multiple(space=descriptions[1].space, number=3)
>>> successors = context.successors(context.domain[0])  # 36
>>> successors
<galactic...Successors object at 0x...>
>>> multiple_3 in successors
True
>>> [item.value for item in context.predecessors(multiple_3)]
[36, 48]
>>> (context.domain[0], multiple_3) in context
True
>>> (context.domain[1], multiple_3) in context
True
>>> (context.domain[2], None) in context
False
__bool__()#

Check if the context is non-empty.

Returns:

True if the context has elements, False otherwise.

Return type:

bool

__contains__(pair)#

Check if an (item, pred) pair is in the context.

Parameters:

pair (object) – A pair of (item, pred)

Returns:

True if the pair is in the context, False otherwise.

Return type:

bool

__iter__()#

Iterate over the (item, pred) pairs in the context.

Returns:

The (item, pred) pairs in the context.

Return type:

Iterator[tuple[Item, Predicate]]

__len__()#

Get the number of (item, pred) pairs in the context.

Returns:

The number of pairs in the context.

Return type:

int

property universes: tuple[ItemUniverse, Sequence[Predicate]]#

Get the universes of this context.

This property returns the collection of universes associated with this context. It includes the item universe and the predicate universe, which are used to represent the items and predicates in the context, respectively.

Returns:

The universes

Return type:

tuple[ItemUniverse, Sequence[Predicate]]

property domain: ItemUniverse#

Get the domain of this context.

This property returns the item universe associated with this context.

Returns:

The domain

Return type:

ItemUniverse

property co_domain: PredicateUniverse#

Get the co-domain of this context.

This property returns the predicate universe associated with this context.

Returns:

The co-domain

Return type:

PredicateUniverse

successors(item)#

Compute the successors of item.

Parameters:

item (Item) – The item

Returns:

The successors of item

Return type:

Collection[Predicate]

Raises:

ValueError – If item is not in the domain

predecessors(attr)#

Compute the predecessors of a predicate.

Parameters:

attr (Predicate) – The predicate

Returns:

The predecessors of attr

Return type:

Collection[Item]

Raises:

ValueError – If attr is not in the co-domain

property descriptions: tuple[Description, ...]#

Get the descriptions.

Returns:

The descriptions

Return type:

tuple[Description, …]

class PredicateUniverse(**data)#

Bases: Resource, Sequence[Predicate]

Represents a universe of predicates.

This class is used to represent a collection of predicates that can be evaluated against items in a context. It is designed to be used as a container for predicates, allowing for operations such as checking membership and representation.

Parameters:

descriptions (tuple[Description, ...]) – The description spaces to include in the global universe

ALIAS#

The alias of the type in the resource registry.

Type:

ClassVar[str] = “core.universe.PredicateUniverse”

Examples

>>> from galactic.algebras.convex.descriptions.examples.arithmetic.core import (
...     MultipleDescription,
... )
>>> from galactic.algebras.convex.characteristics.core import Component, Integer
>>> from galactic.algebras.convex.predicates.examples.arithmetic.core import (
...     Multiple,
... )
>>> characteristic = Integer(components=(Component(),))
>>> description = MultipleDescription(space=(characteristic,))
>>> predicate_universe = PredicateUniverse(descriptions=(description,))
>>> predicate_universe
<galactic.algebras.convex.descriptions.core.PredicateUniverse object at 0x...>
>>> multiple_3 = Multiple(space=description.space, number=3)
>>> multiple_3 in predicate_universe
True
>>> len(predicate_universe)
0
>>> list(predicate_universe)
[]
>>> predicate_universe.descriptions == (description,)
True

Configuration

If you want to specify an predicate universe in a TOML file, you can use the following syntax for a boolean description:

[components.my_component]
"$type" = "core.Component"
expr = "value"

[characteristics.my_characteristic]
"$type" = "core.characteristic.Boolean"
components = [
  {"$ref" = "components.my_component"}
]
name = "my_name"

[descriptions.my_description]
"$type" = "core.description.Boolean"
space = [
  {"$ref" = "characteristics.my_characteristic"}
]

[universes.my_predicate_universe]
"$type" = "core.universe.PredicateUniverse"
descriptions = [
  {"$ref" = "descriptions.my_description"}
]
property universes: tuple[Sequence[Predicate], ...]#

Get the universes.

Returns:

A tuple containing the predicate universes for each description space

Return type:

tuple[Sequence[Predicate], …]

__bool__()#

Check if the global universe is non-empty.

Returns:

True if the global universe has elements, False otherwise

Return type:

bool

__contains__(attr)#

Check if the predicate is in the global universe.

Parameters:

attr (object) – The predicate to check for membership in the global universe

Returns:

True if attr is in the global universe, False otherwise

Return type:

bool

__iter__()#

Iterate over the predicates in the global universe.

Returns:

An iterator over the predicates in the global universe

Return type:

Iterator[Predicate]

__len__()#

Get the number of predicates in the global universe.

Returns:

The number of predicates in the global universe

Return type:

int

__getitem__(index)#

Get an element or a slice from the universe.

Overloads:
  • self, index (int) → Predicate

  • self, index (slice) → Sequence[Predicate]

Parameters:

index (int | slice) – The index or slice to get

Returns:

The element or slice requested

Return type:

Predicate | Sequence[Predicate]

index(elem, start=None, stop=None)#

Get the index of an element.

Parameters:
  • elem (Predicate) – The element whose index is requested

  • start (int | None, default: None) – Where to start

  • stop (int | None, default: None) – Where to stop

Returns:

The index requested

Return type:

int

Raises:

ValueError – If the element is not in the universe or not found in the interval

count(elem)#

Get the total number of occurrences of the element.

Parameters:

elem (Predicate) – The element whose count is requested

Returns:

1 if the element is in the universe, else 0

Return type:

int

model_config: ClassVar[ConfigDict] = {'validate_assignment': True}#

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

model_post_init(context, /)#

This function is meant to behave like a BaseModel method to initialize private attributes.

It takes context as an argument since that’s what pydantic-core passes when calling it.

Parameters:
  • self (BaseModel) – The BaseModel instance.

  • context (Any) – The context.

Return type:

None