π 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:
create_context_from_dataset(): Creates a context from a dataset represented as a iterable or a mapping and an iterable of attributes.create_context_from_table(): Creates a context from a binary table.create_context_from_integers(): Creates a context from a table of integers.
from galactic.algebras.concept.core import create_context_from_dataset
from galactic.algebras.concept.examples.animals.core import ANIMAL_ATTRS, ANIMAL_DATA
context = create_context_from_dataset(ANIMAL_DATA, ANIMAL_ATTRS)
context
<galactic.algebras.concept.core.Context object at 0x77a324641a80>
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=<tuple object at 0x77a324641ad0>)
<function small at 0x77a3243204a0>
(Item(key='Dove', value=<tuple object at 0x77a324641ad0>),
Item(key='Hen', value=<tuple object at 0x77a324660580>),
Item(key='Duck', value=<tuple object at 0x77a324513ba0>))
dove = context.domain.item(key="Dove")
small = context.co_domain.attr(name="small")
display(dove, dove.value, small, small(dove.value))
Item(key='Dove', value=<tuple object at 0x77a324641ad0>)
('small', 'twolegs', 'feathers', 'fly')
<function small at 0x77a3243204a0>
True
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)
79
'Dove has attribute small'
'Dove has attribute twolegs'
'Dove has attribute feathers'
'Dove has attribute fly'
'Hen has attribute small'
'Hen has attribute twolegs'
'Hen has attribute feathers'
'Duck has attribute small'
'Duck has attribute twolegs'
'Duck has attribute feathers'
'Duck has attribute fly'
'Duck has attribute swim'
'Goose has attribute small'
'Goose has attribute twolegs'
'Goose has attribute feathers'
'Goose has attribute fly'
'Goose has attribute swim'
'Owl has attribute small'
'Owl has attribute twolegs'
'Owl has attribute feathers'
'Owl has attribute fly'
'Owl has attribute hunt'
'Hawk has attribute small'
'Hawk has attribute twolegs'
'Hawk has attribute feathers'
'Hawk has attribute fly'
'Hawk has attribute hunt'
'Eagle has attribute medium'
'Eagle has attribute twolegs'
'Eagle has attribute feathers'
'Eagle has attribute fly'
'Eagle has attribute hunt'
'Fox has attribute medium'
'Fox has attribute fourlegs'
'Fox has attribute hair'
'Fox has attribute run'
'Fox has attribute hunt'
'Dog has attribute medium'
'Dog has attribute fourlegs'
'Dog has attribute hair'
'Dog has attribute run'
'Wolf has attribute medium'
'Wolf has attribute fourlegs'
'Wolf has attribute hair'
'Wolf has attribute run'
'Wolf has attribute hunt'
'Wolf has attribute mane'
'Cat has attribute small'
'Cat has attribute fourlegs'
'Cat has attribute hair'
'Cat has attribute run'
'Cat has attribute hunt'
'Tiger has attribute big'
'Tiger has attribute fourlegs'
'Tiger has attribute hair'
'Tiger has attribute run'
'Tiger has attribute hunt'
'Lion has attribute big'
'Lion has attribute fourlegs'
'Lion has attribute hair'
'Lion has attribute run'
'Lion has attribute hunt'
'Lion has attribute mane'
'Horse has attribute big'
'Horse has attribute fourlegs'
'Horse has attribute hair'
'Horse has attribute run'
'Horse has attribute mane'
'Horse has attribute hooves'
'Zebra has attribute big'
'Zebra has attribute fourlegs'
'Zebra has attribute hair'
'Zebra has attribute run'
'Zebra has attribute mane'
'Zebra has attribute hooves'
'Cow has attribute big'
'Cow has attribute fourlegs'
'Cow has attribute hair'
'Cow has attribute hooves'
True
The GALACTIC framework provides methods to access to the items that possess a given attribute and the attributes owned by a given item.
<galactic...Successors object at 0x77a324342000>
4
[<function small at 0x77a3243204a0>,
<function twolegs at 0x77a324321f80>,
<function feathers at 0x77a324321760>,
<function fly at 0x77a324322de0>]
<galactic...Predecessors object at 0x77a324342210>
7
[Item(key='Dove', value=<tuple object at 0x77a324641ad0>),
Item(key='Hen', value=<tuple object at 0x77a324660580>),
Item(key='Duck', value=<tuple object at 0x77a324513ba0>),
Item(key='Goose', value=<tuple object at 0x77a324513ba0>),
Item(key='Owl', value=<tuple object at 0x77a324513bf0>),
Item(key='Hawk', value=<tuple object at 0x77a324513bf0>),
Item(key='Cat', value=<tuple object at 0x77a324513ce0>)]