fcapy.algorithms.lattice_construction

This module contains a number of function which take a set of formal (or pattern) concepts and return its children_dict i.e. the order of given concepts in the form {parent_concept_index: child_concept_index}.

Parent_concept is a concept which is bigger (or more general) than the child concept and there is no other concept between these two.

Functions

add_concept(new_concept, concepts, ...[, ...])

Add new_concept into a set of concepts regarding its subconcept relation

complete_comparison(concepts[, ...])

Return a dict with subconcepts relation on given concepts.

construct_lattice_by_spanning_tree(concepts)

Return a dict with subconcepts relation on given concepts.

construct_lattice_from_spanning_tree(...[, ...])

Return a dict with subconcepts relation on given concepts from given spanning tree of the relation.

construct_lattice_from_spanning_tree_parallel(...)

Return a dict with subconcepts relation on given concepts from given spanning tree of the relation.

construct_spanning_tree(concepts[, ...])

Return a spanning tree of subconcepts relation on given concepts.

remove_concept(concept_i, concepts, ...[, ...])

Remove a concept_i from a set of concepts regarding its subconcept relation

fcapy.algorithms.lattice_construction.add_concept(new_concept, concepts, subconcepts_dict, superconcepts_dict, top_concept_i=None, bottom_concept_i=None, inplace=True)

Add new_concept into a set of concepts regarding its subconcept relation

Parameters
  • new_concept (FormalConcept or PatternConcept) – A concept to add

  • concepts (list of FormalConcept or PatternConcept) – A list of concepts to add a new_concept to

  • subconcepts_dict (dict of {int: int}) – A dict of the current subconcept relation of the concepts

  • superconcepts_dict (dict of {int: int}) – A dict of the current superconcept relation of 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

  • inplace (bool) – A flag whether to modify the concepts list inplace or create a new one

Returns

  • concepts (list of FormalConcept or PatternConcept) – A modified list of concepts

  • children_dict (dict of {int: int}) – A dict of modified subconcept relation of returned concepts

  • parents_dict (dict of {int: int}) – A dict of modified superconcept relation of returned concepts

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

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

fcapy.algorithms.lattice_construction.complete_comparison(concepts: Collection[FormalConcept], is_concepts_sorted: bool = False, n_jobs: int = 1, use_tqdm: bool = False)

Return a dict with subconcepts relation on given concepts. A slow but accurate bruteforce method

Parameters
  • concepts (list of FormalConcept or PatternConcept) – A list of concepts to compare

  • is_concepts_sorted (bool) – A flag whether the set of concepts is topologically sorted or it should be sorted inside the function

  • n_jobs (int) – A number of parallel jobs to run. WARNING: It is more efficient to run just one job for some reason

  • use_tqdm (bool) – A flag whether to visualize the progress of the algorithm with tqdm bar or not

Returns

children_dict – A dict of indexes with subconcept relation on the given concepts (of type {parent_concept_index: child_concept_index})

Return type

dict

fcapy.algorithms.lattice_construction.construct_lattice_by_spanning_tree(concepts, is_concepts_sorted=False, n_jobs=1, use_tqdm=False)

Return a dict with subconcepts relation on given concepts. Uses spanning tree approach to fasten the computation

Parameters
  • concepts (list of FormalConcept or PatternConcept) – A list of concepts to compare

  • is_concepts_sorted (bool) – A flag whether the set of concepts is topologically sorted or it should be sorted inside the function

  • n_jobs (int) – A number of parallel jobs to run. WARNING: It is more efficient to run just one job for some reason

  • use_tqdm (bool) – A flag whether to visualize the progress of the algorithm with tqdm bar or not

Returns

children_dict – A dict of indexes with subconcept relation on the given concepts (of type {parent_concept_index: child_concept_index})

Return type

dict

fcapy.algorithms.lattice_construction.construct_lattice_from_spanning_tree(concepts, sptree_chains, is_concepts_sorted=False, use_tqdm=False)

Return a dict with subconcepts relation on given concepts from given spanning tree of the relation.

Parameters
  • concepts (list of FormalConcept or PatternConcept) – A list of concepts to compare

  • sptree_chains (list of list of int) – A list of chains of a spanning tree of concepts subconcept relation. Can be constructed with ConceptLattice._get_chains(…) method

  • is_concepts_sorted (boot) – A flag whether the set of concepts is topologically sorted or it should be sorted inside the function

  • use_tqdm (bool) – A flag whether to visualize the progress of the algorithm with tqdm bar or not

Returns

children_dict – A dict of indexes with subconcept relation on the given concepts (of type {parent_concept_index: child_concept_index})

Return type

dict

fcapy.algorithms.lattice_construction.construct_lattice_from_spanning_tree_parallel(concepts, sptree_chains, is_concepts_sorted=False, n_jobs=1)

Return a dict with subconcepts relation on given concepts from given spanning tree of the relation.

WARNING: This parallelized function works slower than the original construct_lattice_from_spanning_tree function.

Parameters
  • concepts (list of FormalConcept or PatternConcept) – A list of concepts to compare

  • sptree_chains (list of list of int) – A list of chains of a spanning tree of concepts subconcept relation. Can be constructed with ConceptLattice._get_chains(...) method

  • is_concepts_sorted (boot) – A flag whether the set of concepts is topologically sorted or it should be sorted inside the function

  • n_jobs (int) – A number of parallel jobs to run.

Returns

children_dict – A dict of indexes with subconcept relation on the given concepts (of type {parent_concept_index: child_concept_index})

Return type

dict

fcapy.algorithms.lattice_construction.construct_spanning_tree(concepts, is_concepts_sorted=False, use_tqdm=False)

Return a spanning tree of subconcepts relation on given concepts.

A spanning tree means that for each concept from concepts we look for one parent concept only (even if there are actually many of them)

Parameters
  • concepts (list of FormalConcept or PatternConcept) – A list of concepts to compare

  • is_concepts_sorted (boot) – A flag whether the set of concepts is topologically sorted or it should be sorted inside the function

  • use_tqdm (bool) – A flag whether to visualize the progress of the algorithm with tqdm bar or not

Returns

children_dict – A dict of indexes with spanning tree of subconcept relation on the given concepts (of type {parent_concept_index: child_concept_index})

Return type

dict

fcapy.algorithms.lattice_construction.remove_concept(concept_i, concepts, subconcepts_dict, superconcepts_dict, top_concept_i=None, bottom_concept_i=None, inplace=True)

Remove a concept_i from a set of concepts regarding its subconcept relation

Parameters
  • concept_i (int) – An index of concept to be removed

  • concepts (list of FormalConcept or PatternConcept) – A list of concepts to remove a concept_i from

  • subconcepts_dict (dict of {int: int}) – A dict of the current subconcept relation of the concepts

  • superconcepts_dict (dict of {int: int}) – A dict of the current superconcept relation of 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

  • inplace (bool) – A flag whether to modify the concepts list inplace or create a new one

Returns

  • concepts (list of FormalConcept or PatternConcept) – A modified list of concepts

  • children_dict (dict of {int: int}) – A dict of modified subconcept relation of returned concepts

  • parents_dict (dict of {int: int}) – A dict of modified superconcept relation of returned concepts

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

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