User Guide


concepts is a pure-python package that runs under both Python 2.7 and 3.5+. It is available from PyPI. To install it with pip, run the following command:

$ pip install concepts

For a system-wide install, this typically requires administrator access. For an isolated installation, you can run the same inside a virtualenv or a venv (Python 3 only).

The pip-command will automatically install the (pure-Python) bitsets and graphviz packages from PyPI as required dependencies.

To render graph visualizations (to PDF, SVG, PNG, etc.) of concept lattices, you also need to have a working installation of the Graphviz software (download page).

After installing Graphviz, make sure that its bin/ subdirectory containing the layout commands for rendering graph descriptions (dot, circo, neato, etc.) is on your systems’ path: On the command-line, dot -V should print the version of your Graphiz installation.

Formal contexts

With concepts, formal contexts (Context objects) can be created from a string with an ASCII-art style cross-table. The objects and properties will simply be represented by strings. Separate the property columns with pipe symbols ('|'), create one row for each objects, one column for each property, and indicate the presence of a property with the character 'X'.

Note that the object and property names need to be disjoint to uniquely identify them.

>>> from concepts import Context

>>> c = Context.fromstring('''
...            |human|knight|king |mysterious|
... King Arthur|  X  |  X   |  X  |          |
... Sir Robin  |  X  |  X   |     |          |
... holy grail |     |      |     |     X    |
... ''')

>>> c  
<Context object mapping 3 objects to 4 properties [dae7402a] at 0x...>

You can also load contexts from files in different plain-text formats, see below.

After creation, the parsed content of the table is available on the Context object.

>>> c.objects  # row headings
('King Arthur', 'Sir Robin', 'holy grail')

>>>  # column headings
('human', 'knight', 'king', 'mysterious')

>>> c.bools  # data cells
[(True, True, True, False), (True, True, False, False), (False, False, False, True)]

The Context object can be queried to return the common properties for a collection of objects (common intent, intension()) as well as the common objects for a collection of properties (common extent, extension()):

>>> c.intension(['King Arthur', 'Sir Robin'])  # common properties?
('human', 'knight')

>>> c.extension(['knight', 'mysterious'])  # objects with these properties?

In FCA these operations are called derivations and usually notated with the prime symbol(‘).

>>> c.extension(['knight', 'king'])
('King Arthur',)

>>> c.extension(['mysterious', 'human'])

Formal concepts

A pair of objects and properties such that the objects share exactly the properties and the properties apply to exactly the objects is called formal concept. Informally, they result from maximal rectangles of X-marks in the context table, when rows and columns can be reordered freely.

You can retrieve the closest matching concept corresponding to a collection of objects or properties with the __getitem__() method of the Context object:

>>> c['king',]  # closest concept matching intent/extent
(('King Arthur',), ('human', 'knight', 'king'))

>>> assert c.intension(('King Arthur',)) == ('human', 'knight', 'king')
>>> assert c.extension(('human', 'knight', 'king')) == ('King Arthur',)

>>> c['King Arthur', 'Sir Robin']
(('King Arthur', 'Sir Robin'), ('human', 'knight'))

Within each Context, there is a maximally general concept comprising all of the objects as extent and having an empty intent (supremum).

>>> c['Sir Robin', 'holy grail']  # maximal concept, supremum
(('King Arthur', 'Sir Robin', 'holy grail'), ())

Furthermore there is a minimally general concept comprising no object at all and having all properties as intent (infimum).

>>> c['mysterious', 'knight']  # minimal concept, infimum
((), ('human', 'knight', 'king', 'mysterious'))

The concepts of a context can be ordered by extent set-inclusion (or, dually, by intent set-inclusion). With this (partial) order, they form a concept lattice (Lattice object) having the supremum concept (i.e. the tautology) at the top, the infimum concept (i.e. the contradiction) at the bottom, and the other concepts in between.

Concept lattice

The concept lattice of a Context contains all pairs of objects and properties (formal concepts) that can be retrieved from a formal context. You can iterate over the Lattice to visit all concepts:

>>> c  
<Context object mapping 3 objects to 4 properties [dae7402a] at 0x...>

>>> l = c.lattice

>>> l  
<Lattice object of 2 atoms 5 concepts 2 coatoms at 0x...>

>>> for extent, intent in l:
...     print('%r %r' % (extent, intent))
() ('human', 'knight', 'king', 'mysterious')
('King Arthur',) ('human', 'knight', 'king')
('holy grail',) ('mysterious',)
('King Arthur', 'Sir Robin') ('human', 'knight')
('King Arthur', 'Sir Robin', 'holy grail') ()

Individual Concept objets can be retrieved from the Lattice object by different means :

>>> l.infimum  # first concept, index 0
<Infimum {} <-> [human knight king mysterious]>

>>> l.supremum  # last concept
<Supremum {King Arthur, Sir Robin, holy grail} <-> []>

>>> l[1]
<Atom {King Arthur} <-> [human knight king] <=> King Arthur <=> king>

>>> l['mysterious',]
<Atom {holy grail} <-> [mysterious] <=> holy grail <=> mysterious>

The concepts form a directed acyclic graph and are linked upward (more general concepts, superconcepts) and downward (less general concepts, subconcepts):

>>> l.infimum.upper_neighbors  
(<Atom {King Arthur} <-> [human knight king] <=> King Arthur <=> king>,
 <Atom {holy grail} <-> [mysterious] <=> holy grail <=> mysterious>)

>>> l[1].lower_neighbors
(<Infimum {} <-> [human knight king mysterious]>,)


To visualize the Lattice, use its graphviz() method:

>>> dot = l.graphviz()

>>> print(dot.source)  
// <Lattice object of 2 atoms 5 concepts 2 coatoms at 0x...>
digraph Lattice {
    node [label="" shape=circle style=filled width=.25]
    edge [dir=none labeldistance=1.5 minlen=2]
                    c1 -> c1 [color=transparent headlabel="King Arthur" labelangle=270]
                    c1 -> c1 [color=transparent labelangle=90 taillabel=king]
                    c1 -> c0
                    c2 -> c2 [color=transparent headlabel="holy grail" labelangle=270]
                    c2 -> c2 [color=transparent labelangle=90 taillabel=mysterious]
                    c2 -> c0
                    c3 -> c3 [color=transparent headlabel="Sir Robin" labelangle=270]
                    c3 -> c3 [color=transparent labelangle=90 taillabel="human knight"]
                    c3 -> c1
                    c4 -> c2
                    c4 -> c3

For example:

>>> h = Context.fromstring('''
...      |male|female|adult|child|
... man  |  X |      |  X  |     |
... woman|    |   X  |  X  |     |
... boy  |  X |      |     |  X  |
... girl |    |   X  |     |  X  |
... ''')
>>> dot = h.lattice.graphviz()

>>> print(dot.source)  
// <Lattice object of 4 atoms 10 concepts 4 coatoms at 0x...>
digraph Lattice {
    node [label="" shape=circle style=filled width=.25]
    edge [dir=none labeldistance=1.5 minlen=2]
                    c1 -> c1 [color=transparent headlabel=man labelangle=270]
                    c1 -> c0
                    c2 -> c2 [color=transparent headlabel=woman labelangle=270]
                    c2 -> c0
                    c3 -> c3 [color=transparent headlabel=boy labelangle=270]
                    c3 -> c0

A more complex example:

>>> w = Context.fromfile('examples/liveinwater.cxt')
>>> dot = w.lattice.graphviz()

>>> print(dot.source)  
// <Lattice object of 4 atoms 19 concepts 4 coatoms at 0x...>
digraph Lattice {
    node [label="" shape=circle style=filled width=.25]
    edge [dir=none labeldistance=1.5 minlen=2]
                    c1 -> c1 [color=transparent headlabel=frog labelangle=270]
                    c1 -> c0
                    c2 -> c2 [color=transparent headlabel=dog labelangle=270]
                    c2 -> c2 [color=transparent labelangle=90 taillabel="breast feeds"]
                    c2 -> c0
                    c3 -> c3 [color=transparent headlabel=reed labelangle=270]
                    c3 -> c0

For details on the resulting objects’ interface, check the documentation of the Python graphviz interface used.


CXT, CXT, table

Context objects can be loaded from and saved to files and strings in CXT, CSV and ASCII-art table formats. For loading, use Context.fromfile() or Context.fromstring():

>>> c1 = Context.fromfile('examples/liveinwater.cxt')
>>> c1  
<Context object mapping 8 objects to 9 properties [b1e86589] at 0x...>

>>> c2 = Context.fromfile('examples/liveinwater.csv', frmat='csv')
>>> c2  
<Context object mapping 8 objects to 9 properties [b1e86589] at 0x...>

>>> c3 = Context.fromfile('examples/liveinwater.txt', frmat='table')
>>> c3  
<Context object mapping 8 objects to 9 properties [b1e86589] at 0x...>

>>> assert c1 == c2 == c3

To save a Context object, use its tofile() or tostring() methods. All four methods allow to specify the frmat argument ('cxt', 'csv', or 'table').

The load() function can be used to infer the format from the filename suffix. There is also a dedicated load_cxt() for loading CXT files, and load_csv() for loading contexts from CSV files in different formats via the dialect argument (e.g. 'excel-tab' for tab-separated, see csv docs).


These methods/functions load/save only the Context, not the structure of its lattice (i.e. only the information to recreate the Context; its lattice can be recomputed on demand).

Custom json-compatible format

Context objects can also be serialized and deserialized using a custom json-based format with tojson() and Context.fromjson(). This format allows to include the lattice structure, so it can be used for long-term storage of large graphs that are expensive to compute:

>>> c = Context.fromjson('examples/example.json', encoding='utf-8')
>>> c
<Context object mapping 6 objects to 10 properties [b9d20179] at 0x...>

The same custom storage format is also available as plain Python dict, e.g. to be used with other methods of (de)serialization such as pickle, pprint.pprint() + ast.literal_eval(), yaml, toml, xml, a database, etc. Use todict() and Context.fromdict():

>>> print(', '.join(sorted(c.todict())))
context, lattice, objects, properties

See Custom serialization format for details.

With pickle

Context objects are also pickleable:

>>> import pickle

>>> pickle.loads(pickle.dumps(c)) == c