fcapy.lattice.ConceptLattice

class fcapy.lattice.ConceptLattice(concepts=None, **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=None, **kwargs)

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

Parameters
  • concepts (list of FormalConcept or PatternConcept) –

  • kwargs

    subconcepts_dict: dict of type {int: list of int}

    A dictionary with subconcept (order) relation on the concepts

    superconcepts_dict: dict of type {int: list of int}

    A dictionary with superconcept (inverse order) relation on the concepts

    top_concept_i: int

    An index of the top (biggest) concept from concepts

    bottom_concept_i: int

    An index of the bottom (smallest) concept from concepts

Methods

__init__([concepts])

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

add_concept(new_concept)

Add the concept new_concept into the ConceptLattice

calc_concepts_measures(measure[, context])

Calculate the values of measure for each concept in a lattice

from_context(context[, algo])

Return a ConceptLattice constructed on the context by algorithm algo

from_json([path, json_data])

Read ConceptLattice from .json file .json formatted string data

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

remove_concept(concept_i)

Remove the concept with concept_i index from the ConceptLattice

sort_concepts(concepts)

Return the topologically sorted set of concepts

to_json([path])

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

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

transpose_hierarchy(hierarchy_dict)

Return transposed hierarchy of concepts (i.e.

Attributes

bottom_concept

The bottom (smallest) concept in a lattice

bottom_concept_i

An index of the bottom (smallest) concept in a lattice

concepts

A list of concepts from the ConceptLattice

subconcepts_dict

A dictionary which keeps the subconcept (order) relation of the ConceptLattice.concepts

superconcepts_dict

A dictionary which keeps the superconcept (order) relation of the ConceptLattice.concepts

top_concept

The top (biggest) concept in a lattice

top_concept_i

An index of the top (biggest) concept in a lattice

add_concept(new_concept)

Add the concept new_concept into the ConceptLattice

property bottom_concept

The bottom (smallest) concept in a lattice

property bottom_concept_i

An index of the bottom (smallest) concept in a lattice

calc_concepts_measures(measure, context=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 from the ConceptLattice

classmethod from_context(context, 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

static from_json(path=None, json_data=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

Returns

ltc

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

remove_concept(concept_i)

Remove the concept with concept_i index from the ConceptLattice

static sort_concepts(concepts)

Return the topologically sorted set of concepts

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

property subconcepts_dict

A dictionary which keeps the subconcept (order) relation of the ConceptLattice.concepts

The dictionary is of format {parent concept index: list of children concept indexes} Parent concept is a concept which is bigger (or more general) then a child concept and there is no such concept in a lattice which is smaller than the parent and bigger than the child

property superconcepts_dict

A dictionary which keeps the superconcept (order) relation of the ConceptLattice.concepts

The dictionary is of format {child concept index: list of parent concept indexes} Parent concept is a concept which is bigger (or more general) then a child concept and there is no such concept in a lattice which is smaller than the parent and bigger than the child

to_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)

property top_concept

The top (biggest) concept in a lattice

property top_concept_i

An index of the top (biggest) concept in a lattice

trace_context(context: fcapy.mvcontext.mvcontext.MVContext, use_object_indices=False, use_generators=False, use_tqdm=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

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

static transpose_hierarchy(hierarchy_dict)

Return transposed hierarchy of concepts (i.e. turn superconcept_dict into subconcepts_dict and vice versa)

Parameters

hierarchy_dict (dict of type {int: list of int}) – A subconcepts_dict of superconcepts_dict of ConceptLattice

Returns

new_dict – A superconcepts_dict if subconcepts_dict is given, subconcepts_dict if superconcepts_dict is given

Return type

dict of type {int: list of int}