fcapy.lattice.ConceptLattice

class fcapy.lattice.ConceptLattice(concepts, **kwargs)

A class used to represent Concept Lattice object from FCA theory

from_context(context, algo, ...):

Construct a ConceptLattice from the given context by specified algo (‘CbO’,’Sofia’, ‘RandomForest’)

calc_concepts_measures(measure, ...):

Calculate interestingness measure of 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_concept to the ConceptLattice

remove_concept(concept_i):

Remove a concept with concept_i index 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, **kwargs)

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

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 concepts and **kwargs values

add(element[, fill_up_cache])

Add an element to semilattice.

add_concept(new_concept)

Add concept new_concept into the lattice

calc_concepts_measures(measure[, context])

Calculate the values of measure for each concept in a lattice

concepts_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_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

from_context(context[, algo])

Return a ConceptLattice constructed on the context by algorithm algo

get_all_subconcepts_dict(concepts, …)

Return the transitively closed superconcept relation of concept from subconcepts_dict

get_all_superconcepts_dict(concepts, …)

Return the transitively closed superconcept relation of concept from superconcepts_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_elements(element_indexes)

join_elements([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_elements([element_indexes])

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

read_json([path, json_data, pattern_types])

Read ConceptLattice from .json file .json formatted string data

remove(element)

Remove and element from the semilattice

remove_concept(concept_i)

Remove concept concept_i into the lattice

sort_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 context to a set of bottom/all the concepts which cover it

trace_element(element, direction)

Get the sets of all and direct superelements (or subelements) of an element in the POSet

write_json([path])

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

Attributes

CLASS_NAME

all_subconcepts_dict

list of indexes of all concepts smaller than concept concept_index}

all_superconcepts_dict

list of indexes of all concepts bigger than concept concept_index}

bottom_concept

The bottom (the smallest) concept

bottom_concept_i

An index of the bottom (the smallest) concept

bottom_element

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

bottom_elements

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

concepts

A list of concepts of the lattice

direct_sub_elements_dict

list`[indexes of closest elements smaller than `element_index]

direct_super_elements_dict

list`[indexes of closest elements bigger than `element_index]

elements

A set of elements of the POSet

leq_func

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

sub_elements_dict

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

subconcepts_dict

list of indexes of closest concepts smaller than concept concept_index}

super_elements_dict

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

superconcepts_dict

list of indexes of closest concepts bigger than concept concept_index}

top_concept

The top (biggest) concept

top_concept_i

An index of the top (the biggest) concept

top_element

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

top_elements

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

add(element, fill_up_cache=True)

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

add_concept(new_concept: fcapy.lattice.formal_concept.FormalConcept)

Add concept new_concept into the lattice

property all_subconcepts_dict

list of indexes of all concepts smaller than concept concept_index}

Type

A dictionary {concept_index

property all_superconcepts_dict

list of indexes of all concepts bigger than concept concept_index}

Type

A dictionary {concept_index

property bottom_concept

The bottom (the smallest) concept

property bottom_concept_i

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[fcapy.context.formal_context.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.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

Returns

Return type

None

property concepts

A list of concepts of the lattice

static concepts_leq_func(a, b)

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 a is a direct sub element of element b if a``<``b and there is no element c such that a``<``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 a is a direct super element of element b if a>``b`` and there is no element c such that a>``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: fcapy.context.formal_context.FormalContext, algo=None, **kwargs)

Return a ConceptLattice constructed on the context by algorithm algo

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 context by algorithm algo

Return type

ConceptLattice

classmethod get_all_subconcepts_dict(concepts, subconcepts_dict)

Return the transitively closed superconcept relation of concept from subconcepts_dict

The transitively closed subconcept relation of concept from subconcepts_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

  • 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 concept from subconcepts_dict

Return type

dict of type {int: list of int}

classmethod get_all_superconcepts_dict(concepts, superconcepts_dict)

Return the transitively closed superconcept relation of concept from superconcepts_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

  • 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 concept from superconcepts_dict

Return type

dict of type {int: list of int}

get_chains()

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)

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: fcapy.mvcontext.mvcontext.MVContext, use_tqdm=False, algo='exact')

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, is_concepts_sorted=False)

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_i (int) – An index of the top (biggest) concept from the list of concepts

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

index(element)

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

infimum(element_indexes: Optional[collections.abc.Collection] = None)

Alias for self.meet_elements(element_indexes)

join_elements(element_indexes: Optional[collections.abc.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 ``b or not

meet_elements(element_indexes: Optional[collections.abc.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[fcapy.mvcontext.pattern_structure.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 element from the semilattice

remove_concept(concept_i)

Remove concept concept_i into the lattice

static sort_concepts(concepts)

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

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

list of indexes of closest concepts bigger than concept concept_index}

Type

A dictionary {concept_index

supremum(element_indexes: Optional[collections.abc.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

Returns

Return type

nx.DiGraph

property top_concept

The top (biggest) concept

property top_concept_i

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: fcapy.context.formal_context.FormalContext, use_object_indices=False, use_generators=False, use_tqdm=False, return_generators_extents=False)

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, direction: str)

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

Returns

  • final_elements (set) – A set of direct super (or sub) elements of element in the POSet

  • traced_elements (set) – A set of all super (or sub) elements of element in the POSet

write_json(path=None)

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 path is None)

  • None – (if path is not None)