fcapy.lattice.ConceptLattice
- class fcapy.lattice.ConceptLattice(concepts: List[Union[FormalConcept, PatternConcept]], **kwargs)
A class used to represent Concept Lattice object from FCA theory
- from_context(context, algo, ...):
Construct a ConceptLattice from the given
contextby specifiedalgo(‘CbO’,’Sofia’, ‘RandomForest’)
- calc_concepts_measures(measure, ...):
Calculate interestingness
measureof concepts in the ConceptLattice (like ‘stability’ or ‘stability_bounds’)
- trace_context(context, ...):
Get the set of concepts from the ConceptLattice which describe objects from the given
context
- add_concept(new_concept):
Add
new_conceptto the ConceptLattice
- remove_concept(concept_i):
Remove a concept with
concept_iindex from the ConceptLattice
Notes
A ConceptLattice L = {(A,B) | A subseteq G, B subseteq M, A’=B, B’=A} is a set of Formal Concepts (A,B) contained in a Formal Context K = (G, M, I)
A Formal Concept (A,B) denotes the pair of subset of objects A and subset of attributes B, s.t. objects A are all the objects described by attributes B and attributes B are all the attributes which describe objects A.
The notion of Formal Context is described in the class fcapy.context.formal_context.FormalContext
A ConceptLattice idea may be applied to Many Valued Context too (described in the class fcapy.mvcontext.mvcontext.MVContext) resolving a set of Pattern Concepts (A, d) where A is a subset of objects, d is a description from the ManyValuedContext s.t. objects A are all the objects described by desciption d and description d is the biggest (most precise) description of objects A
- __init__(concepts: List[Union[FormalConcept, PatternConcept]], **kwargs)
Construct a ConceptLattice based on a set of
conceptsand**kwargsvalues- Parameters
concepts (list`[`FormalConcept or PatternConcept]) –
kwargs –
- subconcepts_dict: dict`{`int, list`[`int]}
A dictionary with subconcept (order) relation on the
concepts- superconcepts_dict: dict`{`int, list`[`int]}
A dictionary with superconcept (inverse order) relation on the
concepts
Methods
__init__(concepts, **kwargs)Construct a ConceptLattice based on a set of
conceptsand**kwargsvaluesadd(element[, fill_up_cache])Add an
elementto semilattice.add_concept(new_concept)Add concept
new_conceptinto the latticecalc_concepts_measures(measure[, context])Calculate the values of
measurefor each concept in a latticeconcepts_leq_func(a, b)A function to compare two formal (or pattern) concepts
direct_sub_elements(element_index)Return a set of indexes of closest elements of POSet smaller than element #``element_index``
direct_super_elements(element_index)Return a set of indexes of closest elements of POSet bigger than element #``element_index``
Fill up each cache of POSet
Compute direct subelements of each element in a POSet
Compute direct superelements of each element in a POSet
Compare all the elements of POSet at once
Compute all subelements of each element in a POSet
Compute all superelements of each element in a POSet
from_context(context[, algo])Return a ConceptLattice constructed on the
contextby algorithmalgoget_all_subconcepts_dict(concepts, ...)Return the transitively closed superconcept relation of
conceptfromsubconcepts_dictget_all_superconcepts_dict(concepts, ...)Return the transitively closed superconcept relation of
conceptfromsuperconcepts_dictReturn a list of chains of concept indexes from the ConceptLattice
get_concept_new_extent(concept_i)Return the subset of objects which are contained in
concept_ibut not its children conceptsget_concept_new_extent_i(concept_i)Return the subset of objects indexes which are contained in
concept_ibut not its children conceptsget_concept_new_intent(concept_i)Return the subset of objects which are contained in
concept_ibut not its parent conceptsget_concept_new_intent_i(concept_i)Return the subset of attributes indexes which are contained in
concept_ibut not its parent conceptsget_conditional_generators_dict(context[, ...])Return the conditional generators of concepts from the Concept Lattice
get_top_bottom_concepts_i(concepts[, ...])Return the indexes of top and bottom concept from the list of
conceptsindex(element)Returns an index of the
elementin the list ofPOSet.elementsinfimum([element_indexes])Alias for self.meet_elements(element_indexes)
join_elements([element_indexes])Return the smallest element from POSet bigger than all elements from
element_indexesleq_elements(a_index, b_index)Compare two elements of POSet by their indexes
meet_elements([element_indexes])Return the biggest element from POSet smaller than all elements from
element_indexesread_json([path, json_data, pattern_types])Read ConceptLattice from .json file .json formatted string data
remove(element)Remove and
elementfrom the semilatticeremove_concept(concept_i)Remove concept
concept_iinto the latticesort_concepts(concepts)Return the topologically sorted set of concepts
sub_elements(element_index)Return a set of indexes of elements of POSet smaller than element #``element_index``
super_elements(element_index)Return a set of indexes of elements of POSet bigger than element #``element_index``
supremum([element_indexes])Alias for self.join_elements(element_indexes)
to_networkx([direction])Generate Networkx graph from the concept lattice
trace_context(context[, use_object_indices, ...])Return the dictionaries which map an object from
contextto a set of bottom/all the concepts which cover ittrace_element(element, direction)Get the sets of all and direct superelements (or subelements) of an
elementin the POSetwrite_json([path])Convert (and possible save) a ConceptLattice in .json format
Attributes
CLASS_NAMElist of indexes of all concepts smaller than concept concept_index}
list of indexes of all concepts bigger than concept concept_index}
The bottom (the smallest) concept
An index of the bottom (the smallest) concept
An index of the bottom (the smallest) element of a semilattice
A list of indexes of the bottom (the smallest) elements of a semilattice
A list of concepts of the lattice
list`[indexes of closest elements smaller than `element_index]
list`[indexes of closest elements bigger than `element_index]
A set of elements of the POSet
A function to compare whether element
a` from the POSet is smaller than ``bor notlist`[indexes of all elements smaller than `element_index]
list of indexes of closest concepts smaller than concept concept_index}
list`[indexes of all elements bigger than `element_index]
list of indexes of closest concepts bigger than concept concept_index}
The top (biggest) concept
An index of the top (the biggest) concept
An index of the single top (the biggest) element of the semilattice
The set of indexes of the top (the biggest) elements of the semilattice
- add(element, fill_up_cache=True)
Add an
elementto semilattice. Automatically fill up the comparison caches if needed
- add_concept(new_concept: Union[FormalConcept, PatternConcept])
Add concept
new_conceptinto the lattice
- property all_subconcepts_dict: Dict[int, List[int]]
list of indexes of all concepts smaller than concept concept_index}
- Type
A dictionary {concept_index
- property all_superconcepts_dict: Dict[int, List[int]]
list of indexes of all concepts bigger than concept concept_index}
- Type
A dictionary {concept_index
- property bottom_concept: Union[FormalConcept, PatternConcept]
The bottom (the smallest) concept
- property bottom_concept_i: int
An index of the bottom (the smallest) concept
- property bottom_element
An index of the bottom (the smallest) element of a semilattice
- property bottom_elements
A list of indexes of the bottom (the smallest) elements of a semilattice
- calc_concepts_measures(measure: str, context: Optional[Union[FormalContext, MVContext]] = None)
Calculate the values of
measurefor each concept in a latticeThe calculated measure values are stored in
measuresproperty of eachconceptfromConceptLattice.concepts- Parameters
measure (str in (‘LStab’, ‘UStab’, ‘stability_bounds’, ‘stability’)) – The name of the measure to compute
context (FormalContext or MVContext) – The context is used when calculating ‘stability’ measure
- Return type
None
- property concepts: List[Union[FormalConcept, PatternConcept]]
A list of concepts of the lattice
- static concepts_leq_func(a: Union[FormalConcept, PatternConcept], b: Union[FormalConcept, PatternConcept])
A function to compare two formal (or pattern) concepts
- direct_sub_elements(element_index: int)
Return a set of indexes of closest elements of POSet smaller than element #``element_index``
Element
ais a direct sub element of elementbifa``<``band there is no elementcsuch thata``<``c``<``b
- property direct_sub_elements_dict
list`[indexes of closest elements smaller than `element_index]
- Type
A dictionary of kind {element_index
- direct_super_elements(element_index: int)
Return a set of indexes of closest elements of POSet bigger than element #``element_index``
Element
ais a direct super element of elementbifa>``b`` and there is no elementcsuch thata>``c``>``b``
- property direct_super_elements_dict
list`[indexes of closest elements bigger than `element_index]
- Type
A dictionary of kind {element_index
- property elements
A set of elements of the POSet
- fill_up_caches()
Fill up each cache of POSet
- fill_up_direct_subelements_cache()
Compute direct subelements of each element in a POSet
- fill_up_direct_superelements_cache()
Compute direct superelements of each element in a POSet
- fill_up_leq_cache()
Compare all the elements of POSet at once
- fill_up_subelements_cache()
Compute all subelements of each element in a POSet
- fill_up_superelements_cache()
Compute all superelements of each element in a POSet
- classmethod from_context(context: Union[FormalContext, MVContext], algo: Optional[str] = None, **kwargs)
Return a ConceptLattice constructed on the
contextby algorithmalgo- Parameters
context ('FormalContext` or 'MVContext`) –
algo (str in {‘CbO’, ‘Sofia’, ‘RandomForest’}) –
kwargs – Parameters used in CbO, Sofia and RandomForest algorithms from fcapy.algorithms.concept_construction module
- Returns
ltc – A concept lattice constructed on the
contextby algorithmalgo- Return type
ConceptLattice
- classmethod get_all_subconcepts_dict(concepts: List[Union[FormalConcept, PatternConcept]], subconcepts_dict: Dict[int, List[int]]) Dict[int, List[int]]
Return the transitively closed superconcept relation of
conceptfromsubconcepts_dictThe transitively closed subconcept relation of
conceptfromsubconcepts_dictmeans the dict of type: {parent_concept_index: list of indexes of all concepts smaller than the parent}- Parameters
concepts (list of FormalConcept or PatternConcept) – A list of concepts to compute relation on
subconcepts_dict (dict of type {int: list of int}) – The subconcept relation of the
concepts(i.e. {parent_concept_index: list of children_concept_index})
- Returns
all_subconcepts – The transitively closed subconcept relation of
conceptfromsubconcepts_dict- Return type
dict of type {int: list of int}
- classmethod get_all_superconcepts_dict(concepts: List[Union[FormalConcept, PatternConcept]], superconcepts_dict: Dict[int, List[int]]) Dict[int, List[int]]
Return the transitively closed superconcept relation of
conceptfromsuperconcepts_dictThe transitively closed superconcept relation of
conceptfromsuperconceptsmeans the dict of type: {child_concept_index: list of indexes of all concepts bigger than the child}- Parameters
concepts (list of FormalConcept or PatternConcept) – A list of concepts to compute relation on
superconcepts_dict (dict of type {int: list of int}) – The superconcept relation of the concepts (i.e. {`child_concept_index: list of parent_concept_index})
- Returns
all_superconcepts – The transitively closed superconcept relation of
conceptfromsuperconcepts_dict- Return type
dict of type {int: list of int}
- get_chains() List[List[int]]
Return a list of chains of concept indexes from the ConceptLattice
A chain of concept indexes is the list of concept indexes s.t. the first element of the chain is the index of top (biggest) concept each next element is a child of the previous one
A list of chains covers covers all the concepts in the lattice
- Returns
chain – A list of chains of concept indexes from the ConceptLattice
- Return type
list of list of int
- get_concept_new_extent(concept_i: int) Set[str]
Return the subset of objects which are contained in
concept_ibut not its children concepts
- get_concept_new_extent_i(concept_i: int) Set[int]
Return the subset of objects indexes which are contained in
concept_ibut not its children concepts
- get_concept_new_intent(concept_i: int) Set[str]
Return the subset of objects which are contained in
concept_ibut not its parent concepts
- get_concept_new_intent_i(concept_i: int) Set[int]
Return the subset of attributes indexes which are contained in
concept_ibut not its parent concepts
- get_conditional_generators_dict(context: MVContext, use_tqdm: bool = False, algo: str = 'exact') Dict[int, Dict[int, List[frozendict]]]
Return the conditional generators of concepts from the Concept Lattice
WARNING: No comments for now. The notion of conditional generators is under construction
- static get_top_bottom_concepts_i(concepts: List[Union[FormalConcept, PatternConcept]], is_concepts_sorted: bool = False) Tuple[Optional[int], Optional[int]]
Return the indexes of top and bottom concept from the list of
concepts- Parameters
concepts (list of FormalConcept or PatternConcept) – A list of concepts to look for top (biggest) and bottom (smallest) concepts
is_concepts_sorted (bool) – A flag whether the
conceptsare topologically sorted or they should be sorted inside the function
- Returns
top_concept_i (int) – An index of the top (biggest) concept from the list of
conceptsbottom_concept_i (int) – An index of the bottom (smallest) concept from the list of
concepts
- index(element)
Returns an index of the
elementin the list ofPOSet.elements
- infimum(element_indexes: Optional[Collection] = None)
Alias for self.meet_elements(element_indexes)
- join_elements(element_indexes: Optional[Collection] = None)
Return the smallest element from POSet bigger than all elements from
element_indexes
- leq_elements(a_index: int, b_index: int)
Compare two elements of POSet by their indexes
- property leq_func
A function to compare whether element
a` from the POSet is smaller than ``bor not
- meet_elements(element_indexes: Optional[Collection] = None)
Return the biggest element from POSet smaller than all elements from
element_indexes
- static read_json(path: Optional[str] = None, json_data: Optional[str] = None, pattern_types: Optional[Tuple[AbstractPS]] = None)
Read ConceptLattice from .json file .json formatted string data
- Parameters
path (str) – A path to .json file
json_data (str) – A json encoded data
pattern_types (tuple of pattern structure types) – A set of pattern types to decode the values in concept intents (if reading pattern concept lattice)
- Returns
ltc
- Return type
ConceptLattice
- remove(element)
Remove and
elementfrom the semilattice
- remove_concept(concept_i: int)
Remove concept
concept_iinto the lattice
- static sort_concepts(concepts: List[Union[FormalConcept, PatternConcept]]) List[Union[FormalConcept, PatternConcept]]
Return the topologically sorted set of concepts
(ordered by descending of support, lexicographical order of extent indexes)
- sub_elements(element_index: int)
Return a set of indexes of elements of POSet smaller than element #``element_index``
- property sub_elements_dict
list`[indexes of all elements smaller than `element_index]
- Type
A dictionary of kind {element_index
- property subconcepts_dict: Dict[int, List[int]]
list of indexes of closest concepts smaller than concept concept_index}
- Type
A dictionary {concept_index
- super_elements(element_index: int)
Return a set of indexes of elements of POSet bigger than element #``element_index``
- property super_elements_dict
list`[indexes of all elements bigger than `element_index]
- Type
A dictionary of kind {element_index
- property superconcepts_dict: Dict[int, List[int]]
list of indexes of closest concepts bigger than concept concept_index}
- Type
A dictionary {concept_index
- supremum(element_indexes: Optional[Collection] = None)
Alias for self.join_elements(element_indexes)
- to_networkx(direction: str = 'down')
Generate Networkx graph from the concept lattice
- Parameters
direction ({up, down}) – up if the graph should be directed from the lowest concepts to the greatest. down otherwise
- Return type
nx.DiGraph
- property top_concept: Union[FormalConcept, PatternConcept]
The top (biggest) concept
- property top_concept_i: int
An index of the top (the biggest) concept
- property top_element
An index of the single top (the biggest) element of the semilattice
- property top_elements
The set of indexes of the top (the biggest) elements of the semilattice
- trace_context(context: Union[FormalContext, MVContext], use_object_indices: bool = False, use_generators: bool = False, use_tqdm: bool = False, return_generators_extents: bool = False) Tuple[Dict[int, List[int]], Dict[int, List[int]], List[frozendict]]
Return the dictionaries which map an object from
contextto a set of bottom/all the concepts which cover it- Parameters
context (FormalContext or PatternContext) – A Formal (or Pattern) Context to trace
use_object_indices (bool) – A flag whether to return a dict with keys as object indices (if True) or object names (if False)
use_generators (bool) – A flag whether to describe object of context by closed concept intents (if False) or their generators (o/w)
use_tqdm (bool) – A flag whether to visualize the progress of the algorithm with tqdm bar
return_generators_extents (bool) – A flag whether to add generators extents statistics in the output
- Returns
object_bottom_concepts (dict of type {int: list of int}) – Dictionary which maps each object from the
contextto a subset of the smallest concepts from the ConceptLattice which describe this objectobject_traced_concepts (dict of type {int: list of int}) – Dictionary which maps each object from the
contextto a subset of all the concepts from the ConceptLattice which describe this objectgenerators_extents (list of dict) – A list of dictionaries containing information about generators ran while tracing.
- trace_element(element, direction: str)
Get the sets of all and direct superelements (or subelements) of an
elementin the POSet- Parameters
element – An element to compare POSet elements with (not necessary from the POSet itself)
direction ({'up', 'down'}) – If set ‘up’ then compute all (and direct) subelements of an
element, if set ‘down’ then compute all (and direct) superelements of anelement
- Returns
final_elements (set) – A set of direct super (or sub) elements of
elementin the POSettraced_elements (set) – A set of all super (or sub) elements of
elementin the POSet
- write_json(path: Optional[str] = None) Optional[str]
Convert (and possible save) a ConceptLattice in .json format
- Parameters
path (str) – A path to .json file
- Returns
json_data (str) – ConceptLattice decoded in .json format (if
pathis None)None – (if
pathis not None)