πŸ“˜ Contexts

πŸ“˜ Contexts#

In formal concept analysis, a context is a mathematical structure that represents the relationships between a set of objects and a set of attributes. It is typically represented as a binary relation between the objects and attributes, indicating which objects possess which attributes.

Attributes are considered as special kinds of callable objects which may return True (when the object owns the attribute), False (when the object does not own the attribute), or None (when it is unknown whether the object owns the attribute or not). To avoid confusion with the term object widely used in programming, we will use the term item to refer to the objects of a formal context.

In the galactic.algebras.concept.core module, an item is represented as an instance of the Item class with two properties (key and value ) and an attribute as an instance of the Attr class.

The galactic.algebras.concept.core module provides the Context class to represent contexts and several utility functions to create contexts from:

<galactic.algebras.concept.core.Context object at 0x75ce467120c0>

A context is a binary relation between a collection of items (the domain) and a collection of attributes (the co-domain) and therefore is also an instance of the BinaryRelation protocol from the galactic.algebras.relational.core module.

from galactic.algebras.relational.core import BinaryRelation
isinstance(context, BinaryRelation)
True

Each item in the context’s domain (ItemUniverse class) and co-domain (AttrUniverse class) can be accessed using indexing and iterating over them.

display(context.domain[0])
display(context.co_domain[0])
display(context.domain[:3])
Item(key='Dove', value=<list object at 0x75ce3446c3c0>)
<function small at 0x75ce34476660>
(Item(key='Dove', value=<list object at 0x75ce3446c3c0>),
 Item(key='Hen', value=<list object at 0x75ce3448c180>),
 Item(key='Duck', value=<list object at 0x75ce3448c1c0>))
dove = context.domain.item(key="Dove")
small = context.co_domain.attr(name="small")
display(dove, dove.value, small, small(dove.value))
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[4], line 3
      1 dove = context.domain.item(key="Dove")
      2 small = context.co_domain.attr(name="small")
----> 3 display(dove, dove.value, small, small(dove.value))

File /builds/galactic/public/src/python/algebras/galactic-algebra-concept/src/galactic/algebras/concept/core/_attribute.py:77, in member.<locals>.create_attr.<locals>.AttrWrapper.__call__(self, item)
     67 def __call__(self, item: Item) -> bool | None:
     68     """
     69     Call the wrapped function.
     70 
   (...)     75 
     76     """
---> 77     return self._func(item)

File /builds/galactic/public/src/python/algebras/galactic-algebra-concept/src/galactic/algebras/concept/core/_attribute.py:115, in member.<locals>.attr(item)
    100 """
    101 Compute the membership of the attribute.
    102 
   (...)    112 
    113 """
    114 try:
--> 115     return key in item.value  # type: ignore[operator]
    116 except (ValueError, TypeError):
    117     return None

AttributeError: 'list' object has no attribute 'value'

Contexts are binary relations, so we can get its length, iterate over its pairs, and check for membership of a pair.

display(len(context))
for item, attr in context:
    display(f"{item} has attribute {attr}")
display((dove, small) in context)

The GALACTIC framework provides methods to access to the items that possess a given attribute and the attributes owned by a given item.

attributes_of_dove = context.successors(dove)
items_with_small = context.predecessors(small)
display(attributes_of_dove, len(attributes_of_dove), list(attributes_of_dove))
display(items_with_small, len(items_with_small), list(items_with_small))