Concepts¶
The galactic.concepts
module contains classes for exploring concept lattices.
Concept
class is used to represent collections of objects that share a set of predicates;ConceptLattice
class is used to represent concept lattices;ConceptClosure
class is used to represent closure operators used inConceptLattice
instances.
Two classes allow to visualize concept lattices:
ConceptRenderer
class is used to draw concept nodes into graphviz graphs.ConceptTable
class is used to display concepts into jupyter notebooks;
And two functions:
-
class
Concept
(closure: galactic.concepts.ConceptClosure, keys: Optional[Iterable[str]] = None)¶ The
Concept
class represents a formal concept.-
__and__
(other: Any) → galactic.concepts.Concept¶ Return the join of this concept and the other.
- Parameters
other (
Concept
) – the other concept- Returns
Concept
– the join of this concept and the otherNotImplementedType
–NotImplemented
if the other concept is either not a concept or either not constructed with the same population.
-
__init__
(closure: galactic.concepts.ConceptClosure, keys: Optional[Iterable[str]] = None)¶ Initialise a
Concept
instance.- Parameters
closure (
ConceptClosure
) – The concept closure operator.keys (
Iterable[str]
) – An iterable of identifiers.
-
__or__
(other: Any) → galactic.concepts.Concept¶ Return the join of this concept and the other.
- Parameters
other (
Concept
) – the other concept- Returns
Concept
– the join of this concept and the otherNotImplementedType
–NotImplemented
if the other concept is either not a concept or either not constructed with the same population.
-
property
closure
¶ Get the closure operator.
- Returns
The closure operator.
- Return type
-
property
descriptors
¶ Get the descriptors.
- Returns
The descriptors.
- Return type
-
enlarge
(keys: Iterable[str]) → galactic.concepts.Concept¶ Enlarge the concept by adding objects.
- Parameters
keys (
Iterable[Predicate]
) – An iterable of identifiers.- Returns
A new concept.
- Return type
-
equivalence
(element: _T) → Iterator[_T]¶ Compute equivalences of the element.
- Parameters
element (
_T
) – The element whose equivalences are requested- Returns
An iterator over the equivalence elements of element
- Return type
-
intersection
(*args: Any) → galactic.algebras.lattice.Meetable¶ Return the intersection of this element and the others.
-
predicates
() → Iterator[galactic.descriptions.Predicate]¶ Get an iterator over the predicates.
- Returns
- Return type
-
restrict
(predicates: Iterable[galactic.descriptions.Predicate]) → galactic.concepts.Concept¶ Restrict the concept by adding predicates.
- Parameters
predicates (
Iterable[Predicate]
) – An iterable of predicates.- Returns
A new concept.
- Return type
-
subsumption
(element: _T) → Iterator[_T]¶ Compute subsumed elements of the element.
- Parameters
element (
_T
) – The element whose subsumed elements are requested- Returns
An iterator over the subsumed elements of element
- Return type
-
supsumption
(element: _T) → Iterator[_T]¶ Compute supsumed elements of the element.
Notes
supsumption is a neologism to designate the inverse relation of subsumption.
- Parameters
element (
_T
) – The element whose supsumed elements are requested- Returns
An iterator over the supsumed elements of element
- Return type
-
union
(*args: Any) → galactic.algebras.lattice.Joinable¶ Return the union of this element and the others.
-
-
class
ConceptClosure
(population: galactic.population.Population, descriptions: Iterable[galactic.descriptions.Description])¶ The
ConceptClosure
class.It allows to compute concepts using a closure operator.
-
__call__
(keys: Optional[Iterable[str]] = None) → galactic.concepts.Concept¶ Initialise a concept using an iterable of identifiers.
- Parameters
keys (
Iterable[str]
) – An iterable of identifiers.- Returns
A new concept equal to the closure of the keys.
- Return type
-
__init__
(population: galactic.population.Population, descriptions: Iterable[galactic.descriptions.Description])¶ Initialise a
ConceptClosure
instance.- Parameters
population (
Population
) – A population of individuals.descriptions (
Iterable[Description]
) – An iterable of descriptions.
-
property
descriptions
¶ Get the descriptions.
- Returns
The descriptions.
- Return type
-
property
population
¶ Get the population.
- Returns
The population of individuals.
- Return type
-
-
class
ConceptLattice
(population: galactic.population.Population, descriptions: Iterable[galactic.descriptions.Description])¶ The
ConceptLattice
class represents concept lattices.-
__copy__
() → galactic.algebras.lattice.Lattice[_E]¶ Get a copy of the lattice.
The operation is in \(O(j+m)\).
- Returns
The copy of this lattice.
- Return type
-
__init__
(population: galactic.population.Population, descriptions: Iterable[galactic.descriptions.Description]) → None¶ Initialise a
ConceptLattice
instance.- Parameters
population (
Population
) – The population.descriptions (
Iterable[Description]
) – An iterable of descriptions.
-
__iter__
() → Iterator[Tuple[_P, _P]]¶ Get an iterator over the elements.
- Returns
An iterator over the elements
- Return type
-
apply
(strategies: Iterable[galactic.strategies.Strategy]) → None¶ Apply an iterable of strategies on a concept lattice.
- Parameters
lattice (
ConceptLattice
) – The concept latticestrategies (
Iterable[Strategy]
) – The iterable of strategies
-
property
atoms
¶ Get the atoms of this lattice.
- Returns
The atoms
- Return type
-
property
bottom
¶ Get the bottom elements.
The collection contains only one element, the minimum element.
- Returns
The bottom elements.
- Return type
-
property
closure
¶ Get the underlying closure operator.
- Returns
The closure operator.
- Return type
-
property
co_atoms
¶ Get the co-atoms of this lattice.
- Returns
The co-atoms.
- Return type
-
property
co_domain
¶ Get the co-domain of this lattice.
- Returns
The co-domain of this lattice.
- Return type
-
property
cover
¶ Get the covering relation of this lattice.
- Returns
The covering relation.
- Return type
-
classmethod
create
(population: galactic.population.Population, descriptions: Iterable[galactic.descriptions.Description], strategies: Iterable[galactic.strategies.Strategy]) → galactic.concepts.ConceptLattice¶ Create a
ConceptLattice
instance.- Parameters
cls (
Type[ConceptLattice]
) – TheConceptLattice
class.population (
Population
) – The population.descriptions (
Iterable[Description]
) – An iterable of descriptions.strategies (
Iterable[Strategy]
) – An iterable of strategies.
- Returns
A concept lattice.
- Return type
-
property
descriptions
¶ Get the underlying descriptions.
- Returns
The descriptions. It’s a proxy to
self.closure.descriptions
.- Return type
-
property
domain
¶ Get the domain of this lattice.
- Returns
The domain of this lattice.
- Return type
-
extend
(iterable: Iterable[_E]) → None¶ In-place enlarge the lattice with an iterable of elements.
- Parameters
iterable (
Iterable[_E]
) – An iterable of elements
-
filter
(element: _E) → galactic.algebras.lattice.AbstractLattice[_E]¶ Get a filter of a lattice.
- Parameters
element (
_E
) – The lower limit- Returns
The lower bounded lattice.
- Return type
AbstractLattice[_E]
- Raises
ValueError – If the element does not belong to the poset.
-
property
global_entropy
¶ Compute the global entropy of the Moore family.
\[H(L)=\sum_{A\in L}p(A)I(A)=n-\frac{\sum_{A\in L}\kappa(( A,B))\log_2(\kappa(A))}{2^n}\]Notes
See Information .
- Returns
The global entropy.
- Return type
Examples
>>> from galactic.algebras.closure import MooreFamily >>> from galactic.examples.closure import NumericalClosure, NumericalClosed >>> elements = {0, 1, 2, 3, 4, 5} >>> closure = NumericalClosure(elements) >>> family = MooreFamily[NumericalClosed, int](closure) >>> family.extend([closure([1, 2, 3]), closure([2, 3, 4])]) >>> round(family.global_entropy, 3) 1.311
-
property
global_information
¶ Compute the global information of the Moore family.
It is equal to the global entropy multiplied by the number of element defined by the closure operator.
\[I(L)=n H(L)\]- Returns
The global information.
- Return type
Examples
>>> from galactic.algebras.closure import MooreFamily >>> from galactic.examples.closure import NumericalClosure, NumericalClosed >>> elements = {0, 1, 2, 3, 4, 5} >>> closure = NumericalClosure(elements) >>> family = MooreFamily[NumericalClosed, int](closure) >>> family.extend([closure([1, 2, 3]), closure([2, 3, 4])]) >>> round(family.global_information, 3) 7.868
-
property
global_logarithmic_stability
¶ Compute the global normalized logarithmic stability of the Moore family.
\[\lambda(L)=\sum_{A\in L}p(A)\lambda(A)\]- Returns
The global normalized logarithmic stability.
- Return type
Examples
>>> from galactic.algebras.closure import MooreFamily >>> from galactic.examples.closure import NumericalClosure, NumericalClosed >>> elements = {0, 1, 2, 3, 4, 5} >>> closure = NumericalClosure(elements) >>> family = MooreFamily[NumericalClosed, int](closure) >>> family.extend([closure([1, 2, 3]), closure([2, 3, 4])]) >>> round(family.global_logarithmic_stability, 3) 0.335
-
property
global_stability
¶ Compute the global stability of the Moore family.
\[\sigma(L)=\sum_{A\in L}p(A)\sigma(A) =\sum_{A\in L}\frac{\kappa(A)^2}{2^{|A|+n}}\]- Returns
The global stability.
- Return type
Examples
>>> from galactic.algebras.closure import MooreFamily >>> from galactic.examples.closure import NumericalClosure, NumericalClosed >>> elements = {0, 1, 2, 3, 4, 5} >>> closure = NumericalClosure(elements) >>> family = MooreFamily[NumericalClosed, int](closure) >>> family.extend([closure([1, 2, 3]), closure([2, 3, 4])]) >>> round(family.global_stability, 3) 0.703
-
greatest_join_irreducible
(element: _E) → AbstractSet[_E]¶ Get the greatest join irreducible smaller than an element.
- Parameters
element (
_E
) – The element whose greatest join irreducible are requested- Returns
The greatest join irreducible smaller than the element.
- Return type
- Raises
ValueError – If the element does not belong to the lattice.
-
ideal
(element: _E) → galactic.algebras.lattice.AbstractLattice[_E]¶ Get an ideal of a lattice.
- Parameters
element (
_E
) – The upper limit- Returns
The upper bounded lattice.
- Return type
AbstractLattice[_E]
- Raises
ValueError – If the element does not belong to the poset.
-
information
() → Iterator[Tuple[_E, float]]¶ Get an iterator on elements (\(A\), \(I(A)\)).
\(I(A)\) is the information of \(A\).
\[I(A)=-\log_2(p(A)\]- Returns
An iterator on elements and their information.
- Return type
Notes
See Information .
Examples
>>> from galactic.algebras.closure import MooreFamily >>> from galactic.examples.closure import NumericalClosure, NumericalClosed >>> elements = {0, 1, 2, 3, 4, 5} >>> closure = NumericalClosure(elements) >>> family = MooreFamily[NumericalClosed, int](closure) >>> family.extend([closure([1, 2, 3]), closure([2, 3, 4])]) >>> for element, information in family.information(): ... print(list(element), information) [2, 3] 4.0 [2, 3, 4] 4.0 [1, 2, 3] 4.0 [1, 2, 3, 4] 4.0 [0, 1, 2, 3, 4, 5] 0.4150374992788439
-
isdisjoint
(other)¶ Return True if two sets have a null intersection.
-
property
join_irreducible
¶ Get the join irreducible elements.
- Returns
The join irreducible elements
- Return type
-
logarithmic_stabilities
() → Iterator[Tuple[_E, float]]¶ Get an iterator on (\(A\), \(\lambda(A)\)).
\(\lambda(A)\) is the normalized logarithmic stabilities of \(A\) (the normalized logarithmic stability is always between 0 and 1).
if \(\kappa(A)\) is equal to \(2^{|A|}\), the normalized logarithmic stability is equal to 1.
if \(\kappa(A)\) is equal to \(1\), the normalized logarithmic stability is equal to 0
the special case \(A=\emptyset\) (\(2^{|A|}=\kappa(A)=1\)) gives \(\lambda(\emptyset)=1\) by convention.
\[\begin{split}\begin{eqnarray} \lambda(A) &=&\frac{-\log_2\left(1-\sigma(A)+\frac{1}{2^{|A|}}\right)}{|A|}\\ &=&\frac{-\log_2\left(\frac{2^{|A|}-\kappa(A)+1}{2^{|A|}}\right)}{|A|}\\ &=&\frac{|A|-\log_2\left(2^{|A|}-\kappa(A)+1\right)}{|A|}\\ &=&1-\frac{\log_2\left(2^{|A|}-\kappa(A)+1\right)}{|A|}\\ \lambda(\emptyset) &=&1\\ \end{eqnarray}\end{split}\]See also
Notes
Originally, the logarithmic stability was defined using
\[\begin{split}\begin{eqnarray} -\log_2\left(1-\sigma(A)\right) &=&-\log_2\left(1-\frac{\kappa(A)}{2^{|A|}}\right)\\ &=&|A|-\log_2\left(2^{|A|}-\kappa(A)\right)\\ \end{eqnarray}\end{split}\]but this formulae can give an infinity value and it is not normalized between elements.
- Returns
An iterator on elements and their normalized logarithmic stability.
- Return type
Examples
>>> from galactic.algebras.closure import MooreFamily >>> from galactic.examples.closure import NumericalClosure, NumericalClosed >>> elements = {0, 1, 2, 3, 4, 5} >>> closure = NumericalClosure(elements) >>> family = MooreFamily[NumericalClosed, int](closure) >>> family.extend([closure([1, 2, 3]), closure([2, 3, 4])]) >>> for element, stability in family.logarithmic_stabilities(): ... print(list(element), round(stability, 3)) [2, 3] 1.0 [2, 3, 4] 0.226 [1, 2, 3] 0.226 [1, 2, 3, 4] 0.075 [0, 1, 2, 3, 4, 5] 0.319
-
lower_limit
(limit: _E, strict: bool = Ellipsis) → AbstractSet[_E]¶ Get the elements greater than a
limit
.- Parameters
- Returns
The selected elements.
- Return type
-
property
maximum
¶ Get the maximum element of this lattice.
- Returns
the maximum element
- Return type
-
property
meet_irreducible
¶ Get the meet irreducible elements.
- Returns
The meet irreducible elements
- Return type
-
property
minimum
¶ Get the minimum element of this lattice.
- Returns
The minimum element
- Return type
-
parts
() → Iterator[Tuple[_E, int]]¶ Get an iterator on (\(A\), \(\kappa(A)\)).
\(\kappa(A)\) is the number of parts of \(A\) whose closure \(\phi\) equal to \(A\).
\[\kappa(A)= \left|\left\{ \tilde A\subseteq A: \phi\left(\tilde A\right)=A \right\}\right|\]- Returns
An iterator on elements and the number of parts \(\tilde A\subseteq A\subseteq X\) whose closure is equal to the element.
- Return type
Examples
>>> from galactic.algebras.closure import MooreFamily >>> from galactic.examples.closure import NumericalClosure, NumericalClosed >>> elements = {0, 1, 2, 3, 4, 5} >>> closure = NumericalClosure(elements) >>> family = MooreFamily[NumericalClosed, int](closure) >>> family.extend([closure([1, 2, 3]), closure([2, 3, 4])]) >>> for element, count in family.parts(): ... print(list(element), count) [2, 3] 4 [2, 3, 4] 4 [1, 2, 3] 4 [1, 2, 3, 4] 4 [0, 1, 2, 3, 4, 5] 48
-
property
population
¶ Get the underlying population.
- Returns
The population. It’s a proxy to
self.closure.population
.- Return type
-
predecessors
(element: _E) → AbstractSet[_E]¶ Get the predecessors of an
element
.- Parameters
element (
_E
) – The element whose predecessors are requested- Returns
The predecessors.
- Return type
- Raises
ValueError – If the element does not belong to the poset.
-
probabilities
() → Iterator[Tuple[_E, float]]¶ Get an iterator on (\(A\), \(p(A)\)).
\(p(A)\) is the probability of \(A\).
\[p(A)=\frac{\kappa(A)}{2^n}\]See also
- Returns
An iterator on elements and their probability.
- Return type
Examples
>>> from galactic.algebras.closure import MooreFamily >>> from galactic.examples.closure import NumericalClosure, NumericalClosed >>> elements = {0, 1, 2, 3, 4, 5} >>> closure = NumericalClosure(elements) >>> family = MooreFamily[NumericalClosed, int](closure) >>> family.extend([closure([1, 2, 3]), closure([2, 3, 4])]) >>> for element, probability in family.probabilities(): ... print(list(element), probability) [2, 3] 0.0625 [2, 3, 4] 0.0625 [1, 2, 3] 0.0625 [1, 2, 3, 4] 0.0625 [0, 1, 2, 3, 4, 5] 0.75
-
property
reduced_context
¶ Get the reduced context from this lattice.
- Returns
The reduced context.
- Return type
-
property
sinks
¶ Get the sink elements.
The collection contains only one element, the maximum element.
- Returns
The sink elements.
- Return type
-
smallest_meet_irreducible
(element: _E) → AbstractSet[_E]¶ Get the smallest meet irreducible greater than an element.
- Parameters
element (
_E
) – The element whose greatest join irreducible are requested- Returns
The smallest meet irreducible smaller than the element.
- Return type
- Raises
ValueError – If the element does not belong to the lattice.
-
property
sources
¶ Get the source elements.
The collection contains only one element, the minimum element.
- Returns
The source elements.
- Return type
-
stabilities
() → Iterator[Tuple[_E, float]]¶ Get an iterator on (\(A\), \(\sigma(A)\)).
\(\sigma(A)\) is the stability of \(A\).
\[\sigma(A)=\frac{\kappa(A)}{2^{|A|}}\]See also
- Returns
An iterator on elements and their stability.
- Return type
Examples
>>> from galactic.algebras.closure import MooreFamily >>> from galactic.examples.closure import NumericalClosure, NumericalClosed >>> elements = {0, 1, 2, 3, 4, 5} >>> closure = NumericalClosure(elements) >>> family = MooreFamily[NumericalClosed, int](closure) >>> family.extend([closure([1, 2, 3]), closure([2, 3, 4])]) >>> for element, stability in family.stabilities(): ... print(list(element), round(stability, 3)) [2, 3] 1.0 [2, 3, 4] 0.5 [1, 2, 3] 0.5 [1, 2, 3, 4] 0.25 [0, 1, 2, 3, 4, 5] 0.75
-
successors
(element: _E) → AbstractSet[_E]¶ Get the successors of an
element
.- Parameters
element (
_E
) – The element whose successors are requested- Returns
The successors.
- Return type
- Raises
ValueError – If the element does not belong to the poset.
-
property
top
¶ Get the top elements.
The collection contains only one element, the maximum element.
- Returns
The top elements.
- Return type
-
property
universes
¶ Get the universes of this lattice.
- Returns
The universes.
- Return type
-
-
class
ConceptRenderer
(fill_color: str = '#ffffff', current_color: str = '#ad7fa8', anonymous_color: str = '#d3d3d3', generator_color: str = '#fcaf3e', pseudo_color: str = '#729fcf', prototype_color: str = '#8ae234', show_predicates: bool = False, show_individuals: bool = False, compact: bool = True, join_irreducible: bool = False, meet_irreducible: bool = False, width: int = 40)¶ The
ConceptRenderer
class renders graphviz node and edge attributes.It is used for rendering
Concept
instances.-
__init__
(fill_color: str = '#ffffff', current_color: str = '#ad7fa8', anonymous_color: str = '#d3d3d3', generator_color: str = '#fcaf3e', pseudo_color: str = '#729fcf', prototype_color: str = '#8ae234', show_predicates: bool = False, show_individuals: bool = False, compact: bool = True, join_irreducible: bool = False, meet_irreducible: bool = False, width: int = 40)¶ Initialise a
ConceptRenderer
instance.- Keyword Arguments
fill_color (str) – The fill color.
anonymous_color (str) – The anonymous color.
generator_color (str) – The generator color.
pseudo_color (str) – The pseudo-generator color.
prototype_color (str) – The prototype color.
show_predicates (bool) – Display predicates.
show_individuals (bool) – Display individuals.
compact (bool) – Compact mode.
join_irreducible (bool) – Display elements as join irreducible.
meet_irreducible (bool) – Display elements as meet irreducible.
width (int) – The node width.
-
attributes
(element: galactic.concepts.Concept, index: Optional[int] = None, current: bool = False, successors: Optional[AbstractSet[galactic.concepts.Concept]] = None, predecessors: Optional[AbstractSet[galactic.concepts.Concept]] = None) → Dict[str, str]¶ Produce a dictionary of graphviz attributes for a node.
- Parameters
element (
Concept
) – The concept to render.index (int) – The element index.
successors (
AbstractSet[Concept]
) – The successors ofelement
.predecessors (
AbstractSet[Concept]
) – The predecessors ofelement
.
- Returns
A dictionary of graphviz attributes.
- Return type
Notes
-
property
compact
¶ Get the compact mode.
-
property
join_irreducible
¶ Get the join irreducible mode.
-
property
meet_irreducible
¶ Get the meet irreducible mode.
-
render
(element: galactic.concepts.Concept, index: int) → str¶ Render a concept for a cell element.
-
property
show_individuals
¶ Get the show individuals mode.
-
property
show_predicates
¶ Get the show predicates mode.
-
-
class
ConceptTable
(lattice: galactic.concepts.ConceptLattice, all_individuals: bool = False, all_predicates: bool = False, concept_width: int = 1, individual_width: int = 1, predicate_width: int = 1)¶ The
Table
class is used to represent lattice in jupyter notebooks.-
__init__
(lattice: galactic.concepts.ConceptLattice, all_individuals: bool = False, all_predicates: bool = False, concept_width: int = 1, individual_width: int = 1, predicate_width: int = 1)¶ Initialise a
Table
class.- Parameters
lattice (
ConceptLattice
) – The lattice whose table representation is requested- Keyword Arguments
all_individuals (bool) – A boolean flag for displaying all individuals for each concept
all_predicates (bool) – A boolean flag for displaying all predicates for each concept
concept_width (int) – The concept width
individual_width (int) – The individual width.
predicate_width (int) – The predicate width.
By default only new predicates and new individuals are displayed. The values of the given widths are compared with each other to allocate relative column sizes.
-
property
all_individuals
¶ Get the all individuals flag.
- Returns
The all individuals flag.
- Return type
-
property
all_predicates
¶ Get the all predicates flag.
- Returns
The all predicates flag.
- Return type
-
property
lattice
¶ Get the underlying lattice.
- Returns
The concept lattice.
- Return type
-
-
new_predicates
(concept: galactic.concepts.Concept, successors: AbstractSet[galactic.concepts.Concept]) → Iterator[galactic.descriptions.Predicate]¶ Get the new predicates given by the concept.
- Parameters
concept (
Concept
) – The concept whose new predicates are requested.successors (
AbstractSet[Concept]
) – The successors of the concept.
- Returns
An iterator over the new predicates.
- Return type
-
new_individuals
(concept: galactic.concepts.Concept, predecessors: AbstractSet[galactic.concepts.Concept]) → Iterator[str]¶ Get the new individuals given by the concept.
- Parameters
concept (
Concept
) – The concept whose new individuals are requested.predecessors (
AbstractSet[Concept]
) – The predecessors of the concept.
- Returns
An iterator over the individual keys.
- Return type