API Reference

load_cxt Load and return formal context from CXT file.
load_csv Load and return formal context from CSV file.
make_context Return a new context from source string in the given format.
Context Formal context defining a relation between objects and properties.
Definition Mutable triple of (objects, properties, bools) for creating a context.
concepts.lattices.Lattice Formal concept lattice as directed acyclic graph of concepts.
concepts.lattices.Concept Formal concept as pair of extent and intent.

Context

concepts.load_cxt(filename, encoding=None)

Load and return formal context from CXT file.

Parameters:
  • filename – Path to the CXT file to load the context from.
  • encoding (str) – Encoding of the file ('utf-8', 'latin1', 'ascii', …).

Example

>>> load_cxt('examples/digits.cxt')  
<Context object mapping 10 objects to 7 properties at 0x...>
concepts.load_csv(filename, dialect='excel', encoding='utf-8')

Load and return formal context from CSV file.

Parameters:
  • filename – Path to the CSV file to load the context from.
  • dialect – Syntax variant of the CSV file ('excel', 'excel-tab').
  • encoding (str) – Encoding of the file ('utf-8', 'latin1', 'ascii', …).

Example

>>> load_csv('examples/vowels.csv')  
<Context object mapping 12 objects to 8 properties at 0x...>
concepts.make_context(source, frmat='table')

Return a new context from source string in the given format.

Parameters:
  • source (str) – Formal context table as plain-text string.
  • frmat (str) – Format of the context string ('table', 'cxt', 'csv').

Example

>>> make_context('''
...      |male|female|adult|child|
... man  |  X |      |  X  |     |
... woman|    |   X  |  X  |     |
... boy  |  X |      |     |  X  |
... girl |    |   X  |     |  X  |
... ''')  
<Context object mapping 4 objects to 4 properties at 0x...>
class concepts.Context(objects, properties, bools)

Formal context defining a relation between objects and properties.

Example

>>> Context(['man', 'woman'], ['male', 'female'], [(True, False), (False, True)])  
<Context object mapping 2 objects to 2 properties at 0x...>
__getitem__(items, raw=False)

Return (extension, intension) pair by shared objects or properties.

bools

Row-wise boolean relation matrix between objects and properties.

definition()

Return (objects, properties, bools) triple as mutable object.

extension(properties, raw=False)

Return all objects sharing the given properties.

classmethod fromfile(filename, frmat='cxt', encoding=None, **kwargs)

Return a new context from file source in given format.

classmethod fromstring(source, frmat='table', **kwargs)

Return a new context from string source in given format.

intension(objects, raw=False)

Return all properties shared by the given objects.

lattice

The concept lattice of the formal context.

neighbors(objects, raw=False)

Return the upper neighbors of the concept having all given objects.

objects

(Names of the) objects described by the context.

properties

(Names of the) properties that describe the objects.

relations(include_unary=False)

Return the logical relations between the context properties.

tofile(filename, frmat='cxt', encoding='utf-8', **kwargs)

Save the context serialized to file in the given format.

tostring(frmat='table', **kwargs)

Return the context serialized in the given string-based format.

Definition

class concepts.Definition(objects=(), properties=(), bools=())

Mutable triple of (objects, properties, bools) for creating a context.

Example

>>> Definition(['man', 'woman'], ['male', 'female'], [(True, False), (False, True)])
<Definition(['man', 'woman'], ['male', 'female'], [(True, False), (False, True)])>
__iter__()

Yield objects, properties, and bools (e.g. for triple unpacking).

add_object(obj, properties=())

Add an object to the definition and add properties as related.

add_property(prop, objects=())

Add a property to the definition and add objects as related.

bools

Row-major list of boolean tuples.

copy()

Return an independent copy of the instance.

classmethod fromfile(filename, frmat='cxt', encoding=None, **kwargs)

Return a new definiton from file source in given format.

intersection(other, ignore_conflicts=False)

Return a new definition from the intersection of the definitions.

intersection_update(other, ignore_conflicts=False)

Update the definition with the intersection of the other.

inverted()

Return a new definition flipping all booleans.

move_object(obj, index)

Reorder the definition such that object is at index.

move_property(prop, index)

Reorder the definition such that property is at index.

objects

(Names of the) objects described by the definition.

properties

(Names of the) properties that describe the objects.

remove_object(obj)

Remove an object from the definition.

remove_property(prop)

Remove a property from the definition.

rename_object(old, new)

Replace the name of an object by a new one.

rename_property(old, new)

Replace the name of a property by a new one.

set_object(obj, properties)

Add an object to the definition and set its properties.

set_property(prop, objects)

Add a property to the definition and set its objects.

take(objects=None, properties=None, reorder=False)

Return a subset with given objects/properties as new definition.

tostring(frmat='table', **kwargs)

Return the definition serialized in the given string-based format.

transposed()

Return a new definition swapping objects and properties.

union(other, ignore_conflicts=False)

Return a new definition from the union of the definitions.

union_update(other, ignore_conflicts=False)

Update the definition with the union of the other.

Lattice

class concepts.lattices.Lattice(context, infimum=())

Formal concept lattice as directed acyclic graph of concepts.

__call__(properties)

Return concept having all given properties as intension.

__getitem__(key)

Return concept by index, intension, or extension.

__iter__()

Yield all concepts of the lattice.

__len__()

Return the number of concepts in the lattice.

atoms

The minimal non-infimum concepts of the lattice.

downset_union(concepts)

Yield all concepts that imply any of the given ones.

graphviz(filename=None, directory=None, render=False, view=False, **kwargs)

Return graphviz source for visualizing the lattice graph.

infimum

The most specific concept of the lattice.

join(concepts)

Return the nearest concept that subsumes all given concepts.

meet(concepts)

Return the nearest concept that implies all given concepts.

supremum

The most general concept of the lattice.

upset_generalization(concepts)

Yield all concepts that subsume only the given ones.

upset_union(concepts)

Yield all concepts that subsume any of the given ones.

Concept

class concepts.lattices.Concept(lattice, extent, intent, upper, lower)

Formal concept as pair of extent and intent.

__iter__()

Yield extent and intent (e.g. for pair unpacking).

attributes()

Shortlex ordered properties generating the concept.

complement_of(other)

Infimum meet and supremum join comparison.

downset()

Yield subsumed concepts including self.

extent

The objects subsumed by the concept.

implies(other)

Implication comparison.

incompatible_with(other)

Infimum meet comparison.

intent

The properties implied by the concept.

join(other)

Least upper bound, supremum, or, generalization.

lattice

The lattice containing the concept.

lower_neighbors

The directly subsumed concepts.

meet(other)

Greatest lower bound, infimum, and, unification.

minimal()

Shortlex minimal properties generating the concept.

orthogonal_to(other)

Non-infimum meet, incomparable, and non-supremum join comparison.

properly_implies(other)

Proper implication comparison.

properly_subsumes(other)

Proper subsumption comparison.

subcontrary_with(other)

Non-infimum meet and supremum join comparison.

subsumes(other)

Subsumption comparison.

upper_neighbors

The directly implied concepts.

upset()

Yield implied concepts including self.