fcapy.lattice.ConceptLattice

class fcapy.lattice.ConceptLattice(concepts: List[FormalConcept], **kwargs)

A class used to represent Concept Lattice object from FCA theory

__init__(concepts: List[FormalConcept], **kwargs)

Construct a ConceptLattice based on a set of concepts and **kwargs values

Parameters
  • concepts (list`[`FormalConcept or PatternConcept]) –

  • kwargs

    children_dict: dict`{`int, list`[`int]}

    A dictionary with children relation (preceding concepts) on concepts

    parents_dict: dict`{`int, list`[`int]}

    A dictionary with parents relation (succeeding concepts) on concepts

    is_monotone: bool

    A flag whether lattice contains antimonotone (default) or monotone concepts

Methods

__init__(concepts, **kwargs)

Construct a ConceptLattice based on a set of concepts and **kwargs values

add(element[, fill_up_cache])

Add an element to semilattice.

ancestors(element_index)

Return a set of indexes of elements of POSet bigger than element #``element_index``

calc_concepts_measures(measure[, context])

Calculate the values of measure for each concept in a lattice

children(element_index)

Return a set of indexes of the biggest elements smaller than #``element_index``

descendants(element_index)

Return a set of indexes of elements of POSet smaller than element #``element_index``

fill_up_ancestors_cache()

Compute all ancestors of each element in a POSet

fill_up_caches()

Fill up each cache of POSet

fill_up_children_cache()

Compute children of each element in a POSet

fill_up_descendants_cache()

Compute all descendants of each element in a POSet

fill_up_leq_cache()

Compare all the elements of POSet at once

fill_up_parents_cache()

Compute parents of each element in a POSet

from_context(context[, algo, is_monotone])

Return a ConceptLattice constructed on the context by algorithm algo

get_all_subconcepts_dict(concepts, children_dict)

Return the transitively closed children relation of concept from children_dict

get_all_superconcepts_dict(concepts, ...)

Return the transitively closed superconcept relation of concept from parents_dict

get_chains()

Return 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_i but not its children concepts

get_concept_new_extent_i(concept_i)

Return the subset of objects indexes which are contained in concept_i but not its children concepts

get_concept_new_intent(concept_i)

Return the subset of objects which are contained in concept_i but not its parent concepts

get_concept_new_intent_i(concept_i)

Return the subset of attributes indexes which are contained in concept_i but not its parent concepts

get_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 concepts

index(element)

Returns an index of the element in the list of POSet.elements

infimum([element_indexes])

Alias for self.meet(element_indexes)

join([element_indexes])

Return the smallest element from POSet bigger than all elements from element_indexes

leq_elements(a_index, b_index)

Compare two elements of POSet by their indexes

meet([element_indexes])

Return the biggest element from POSet smaller than all elements from element_indexes

parents(element_index)

Return a set of indexes of the smallest elements bigger than #``element_index``

read_json([path, json_data, pattern_types])

Read ConceptLattice from .json file .json formatted string data

remove(element)

Remove and element from the semilattice

sort_concepts(concepts)

Return the topologically sorted set of concepts

supremum([element_indexes])

Alias for self.join(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 context to a set of bottom/all the concepts which cover it

trace_element(element, direction)

Get the sets of descendants and children (or ancestors and parents) of an element in the POSet

write_json(objs_order, attrs_order[, path])

Convert (and possible save) a ConceptLattice in .json format

Attributes

CLASS_NAME

T

ancestors_dict

list`[indexes of all elements bigger than `element_idx]

bottom

An index of the bottom (the smallest) element of a semilattice

bottoms

A list of indexes of the bottom (the smallest) elements of a semilattice

children_dict

list`[indexes of the biggest elements smaller than `element_idx]

descendants_dict

list`[indexes of all elements smaller than `element_idx]

elements

A list of elements of the POSet

is_monotone

leq_func

A function to compare whether element a` from the POSet is smaller than ``b or not

measures

The dictionary containing all precomputed interestingness measures of concepts

parents_dict

list`[indexes of the smallest elements bigger than `element_idx]

top

An index of the single top (the biggest) element of the semilattice

tops

The set of indexes of the top (the biggest) elements of the semilattice

add(element: Any, fill_up_cache: bool = True)

Add an element to semilattice. Automatically fill up the comparison caches if needed

ancestors(element_index: int) FrozenSet[int]

Return a set of indexes of elements of POSet bigger than element #``element_index``

property ancestors_dict: Dict[int, FrozenSet[int]]

list`[indexes of all elements bigger than `element_idx]

Type

A dictionary of kind {element_idx

property bottom: int

An index of the bottom (the smallest) element of a semilattice

property bottoms: List[int]

A list of indexes of the bottom (the smallest) elements of a semilattice

calc_concepts_measures(measure: str, context: Optional[FormalContext] = None)

Calculate the values of measure for each concept in a lattice

The calculated measure values are stored in measures property of each concept from ConceptLattice.elements

Parameters
  • measure (str in {‘stability_bounds’, ‘LStab’, ‘UStab’, ‘stability’, ‘target_entropy’, ‘mean_information_gain’}) – The name of the measure to compute

  • context (FormalContext or MVContext) – The context is used when calculating ‘stability’ measure

Return type

None

children(element_index: int) FrozenSet[int]

Return a set of indexes of the biggest elements smaller than #``element_index``

Element a is a direct sub element of element b if a``<``b and there is no element c such that a``<``c``<``b

property children_dict: Dict[int, FrozenSet[int]]

list`[indexes of the biggest elements smaller than `element_idx]

Type

A dictionary of kind {element_idx

descendants(element_index: int) FrozenSet[int]

Return a set of indexes of elements of POSet smaller than element #``element_index``

property descendants_dict: Dict[int, FrozenSet[int]]

list`[indexes of all elements smaller than `element_idx]

Type

A dictionary of kind {element_idx

property elements: List[Any]

A list of elements of the POSet

fill_up_ancestors_cache()

Compute all ancestors of each element in a POSet

fill_up_caches()

Fill up each cache of POSet

fill_up_children_cache()

Compute children of each element in a POSet

fill_up_descendants_cache()

Compute all descendants of each element in a POSet

fill_up_leq_cache()

Compare all the elements of POSet at once

fill_up_parents_cache()

Compute parents of each element in a POSet

classmethod from_context(context: Union[FormalContext, MVContext], algo: Optional[str] = None, is_monotone: bool = False, **kwargs) ConceptLattice

Return a ConceptLattice constructed on the context by algorithm algo

Parameters
  • context ('FormalContext` or 'MVContext`) –

  • algo (str in {‘CbO’, ‘Sofia’, ‘RandomForest’}) –

  • is_monotone (bool) – Whether to build antimonotone lattice (if False, default) or monotone concept lattice

  • kwargs – Parameters used in CbO, Sofia and RandomForest algorithms from fcapy.algorithms.concept_construction module

Returns

ltc – A concept lattice constructed on the context by algorithm algo

Return type

ConceptLattice

classmethod get_all_subconcepts_dict(concepts: List[Union[FormalConcept, PatternConcept]], children_dict: Dict[int, Collection[int]]) Dict[int, List[int]]

Return the transitively closed children relation of concept from children_dict

The transitively closed children relation of concept from children_dict means 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

  • children_dict (dict of type {int: list of int}) – The children relation of the concepts (i.e. {parent_concept_index: list of children_concept_index})

Returns

descendants – The transitively closed children relation of concept from children_dict

Return type

dict of type {int: list of int}

classmethod get_all_superconcepts_dict(concepts: List[FormalConcept], parents_dict: Dict[int, Collection[int]]) Dict[int, List[int]]

Return the transitively closed superconcept relation of concept from parents_dict

The transitively closed superconcept relation of concept from superconcepts means 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

  • parents_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

ancestors – The transitively closed parents relation of concept from parents_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 concept of the chain is the index of top (biggest) concept each next concept 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_i but 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_i but not its children concepts

get_concept_new_intent(concept_i: int) Set[str]

Return the subset of objects which are contained in concept_i but 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_i but 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[FormalConcept], 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 concepts are topologically sorted or they should be sorted inside the function

Returns

  • top_concept (int) – An index of the top (biggest) concept from the list of concepts

  • bottom_concept (int) – An index of the bottom (smallest) concept from the list of concepts

index(element: Any) int

Returns an index of the element in the list of POSet.elements

infimum(element_indexes: Optional[Collection[int]] = None) Optional[int]

Alias for self.meet(element_indexes)

join(element_indexes: Optional[Collection[int]] = None) Optional[int]

Return the smallest element from POSet bigger than all elements from element_indexes

leq_elements(a_index: int, b_index: int) bool

Compare two elements of POSet by their indexes

property leq_func: Callable[[Any, Any], bool]

A function to compare whether element a` from the POSet is smaller than ``b or not

property measures: Dict[str, ndarray[Any, dtype[ScalarType]]]

The dictionary containing all precomputed interestingness measures of concepts

meet(element_indexes: Optional[Collection[int]] = None) Optional[int]

Return the biggest element from POSet smaller than all elements from element_indexes

parents(element_index: int) FrozenSet[int]

Return a set of indexes of the smallest elements bigger than #``element_index``

Element a is a direct super element of element b if a>``b`` and there is no element c such that a>``c``>``b``

property parents_dict: Dict[int, FrozenSet[int]]

list`[indexes of the smallest elements bigger than `element_idx]

Type

A dictionary of kind {element_idx

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: Any)

Remove and element from the semilattice

static sort_concepts(concepts: List[FormalConcept]) Optional[List[FormalConcept]]

Return the topologically sorted set of concepts

(ordered by descending of support, lexicographical order of extent indexes)

supremum(element_indexes: Optional[Collection[int]] = None) Optional[int]

Alias for self.join(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: int

An index of the single top (the biggest) element of the semilattice

property tops: List[int]

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 context to 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 context to a subset of the smallest concepts from the ConceptLattice which describe this object

  • object_traced_concepts (dict of type {int: list of int}) – Dictionary which maps each object from the context to a subset of all the concepts from the ConceptLattice which describe this object

  • generators_extents (list of dict) – A list of dictionaries containing information about generators ran while tracing.

trace_element(element: Any, direction: str) Tuple[Set[int], Set[int]]

Get the sets of descendants and children (or ancestors and parents) of an element in 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) descendants of an element, if set ‘down’ then compute all (and direct) ancestors of an element

Returns

  • final_elements (set) – A set of children (or parents) of element in the POSet

  • traced_elements (set) – A set of descendants (or ancestors) of element in the POSet

write_json(objs_order: List[str], attrs_order: List[str], path: Optional[str] = None) Optional[str]

Convert (and possible save) a ConceptLattice in .json format

Parameters
  • objs_order (List[str]) – Names of objects put into list (so that name of object i is objs_order[i])

  • attrs_order (List[str]) – Names of attributes put into list (so that name of attribute i is attrs_order[i])

  • path (str) – A path to .json file

Returns

  • json_data (str) – ConceptLattice decoded in .json format (if path is None)

  • None – (if path is not None)