API Reference¶
Load and return formal context from file. |
|
Load and return formal context from CSV file. |
|
Load and return formal context from CXT file. |
|
Return a new context from source string in the given format. |
|
Formal context defining a relation between objects and properties. |
|
Mutable triple of |
|
Tuple of |
|
Formal concept lattice as directed acyclic graph of concepts. |
|
Formal concept as pair of extent and intent. |
Top-level functions¶
Short-cuts to the most common uses of Context.fromfile()
and
Context.fromstring()
:
- concepts.load(filename, encoding='utf-8', frmat=None)¶
Load and return formal context from file.
- Parameters:
- Returns:
New
Context
instance.- Return type:
Example
>>> import concepts >>> concepts.load('examples/liveinwater.txt') <Context object mapping 8 objects to 9 properties [b1e86589] 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'
, …).
- Returns:
New
Context
instance.- Return type:
Example
>>> import concepts >>> concepts.load_csv('examples/vowels.csv') <Context object mapping 12 objects to 8 properties [a717eee4] at 0x...>
- 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'
, …).
- Returns:
New
Context
instance.- Return type:
Example
>>> import concepts >>> concepts.load_cxt('examples/digits.cxt') <Context object mapping 10 objects to 7 properties [51e571e6] at 0x...>
- concepts.make_context(source, frmat='table')¶
Return a new context from source string in the given format.
- Parameters:
- Returns:
New
Context
instance.- Return type:
Example
>>> import concepts >>> concepts.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 [65aa9782] at 0x...>
Context¶
- class concepts.Context(objects, properties, bools)¶
Formal context defining a relation between objects and properties.
Create context from
objects
,properties
, andbools
correspondence.- Parameters:
- Returns:
New
Context
instance.- Return type:
Example
>>> from concepts import Context >>> Context(['man', 'woman'], ['male', 'female'], [(True, False), (False, True)]) <Context object mapping 2 objects to 2 properties [47e29724] at 0x...>
- __eq__(other)¶
Return whether two contexts are equivalent.
- Parameters:
- Return type:
- Returns:
True
if the contexts are equal,False
otherwise.
Example
>>> import concepts >>> context = concepts.Context.fromstring(concepts.EXAMPLE) >>> context == context.copy() True
Note
Ignores
self.lattice
andother.lattice
objects.
- __getitem__(items, raw=False)¶
Return
(extension, intension)
pair by shared objects or properties.- Parameters:
- Return type:
- Returns:
The smallest concept having all
items
as(extent, intent)
pair.
Example
>>> import concepts >>> context = concepts.Context.fromstring(concepts.EXAMPLE) >>> context['1sg',] (('1sg',), ('+1', '-2', '-3', '+sg', '-pl')) >>> context['1sg', '1pl', '2pl'] (('1sg', '1pl', '2sg', '2pl'), ('-3',)) >>> context['-1', '-sg'] (('2pl', '3pl'), ('-1', '+pl', '-sg'))
- __ne__(other)¶
Return whether two contexts are inequivalent.
- Parameters:
- Return type:
- Returns:
True
if the contexts are unequal,False
otherwise.
Example
>>> import concepts >>> context = concepts.Context.fromstring(concepts.EXAMPLE) >>> definition = context.definition() >>> definition['1sg', '+3'] = True >>> context != concepts.Context(*definition) True
Note
Ignores
self.lattice
andother.lattice
objects.
- property bools: List[Tuple[bool, ...]]¶
Row-wise boolean relation matrix between objects and properties.
- Returns:
Table with the relation between context
objects
andproperties
.
Example
>>> import concepts >>> context = concepts.Context.fromstring(concepts.EXAMPLE) >>> context.bools [(True, False, False, True, False, True, True, False, False, True), (True, False, False, True, False, True, False, True, True, False), (False, True, True, False, False, True, True, False, False, True), (False, True, True, False, False, True, False, True, True, False), (False, True, False, True, True, False, True, False, False, True), (False, True, False, True, True, False, False, True, True, False)]
- crc32(encoding='utf-8')¶
Return hex-encoded unsigned CRC32 over encoded context table string.
- Parameters:
encoding (
str
) – Encoding of the serialzation ('utf-8'
,'latin1'
,'ascii'
, …).- Return type:
- Returns:
The unsigned CRC32 checksum as hex-string.
Example
>>> import concepts >>> context = concepts.Context.fromstring(concepts.EXAMPLE) >>> context.crc32() 'b9d20179'
- definition()¶
Return
(objects, properties, bools)
triple as mutable object.- Returns:
New
Definition
instance.- Return type:
Example
>>> import concepts >>> context = concepts.Context.fromstring(concepts.EXAMPLE) >>> context.definition() <Definition(['1sg', '1pl', '2sg', '2pl', '3sg', '3pl'], ['+1', '-1', '+2', '-2', '+3', '-3', '+sg', '+pl', '-sg', '-pl'], [(True, False, False, True, False, True, True, False, False, True), (True, False, False, True, False, True, False, True, True, False), (False, True, True, False, False, True, True, False, False, True), (False, True, True, False, False, True, False, True, True, False), (False, True, False, True, True, False, True, False, False, True), (False, True, False, True, True, False, False, True, True, False)])>
- extension(properties, raw=False)¶
Return all objects sharing the given
properties
.- Parameters:
- Return type:
- Returns:
A tuple of
str
labels taken fromself.objects
.
Example
>>> import concepts >>> context = concepts.Context.fromstring(concepts.EXAMPLE) >>> context.extension(['+1']) ('1sg', '1pl')
- fill_ratio¶
The fill ratio (density of
True
values) of the context.Fill ratio 0.25 means that 25% of the values in
self.bools
areTrue
values.- Returns:
Context fill ratio (can be interpreted as percentages).
Example
>>> import concepts >>> context = concepts.Context.fromstring(concepts.EXAMPLE) >>> float(context.fill_ratio) 0.5 >>> context.fill_ratio Fraction(1, 2)
- classmethod fromdict(d, ignore_lattice=False, require_lattice=False, raw=False)¶
Return a new context from dict
d
.- Parameters:
- Return type:
- Returns:
New
Context
instance.
- classmethod fromfile(filename, frmat='cxt', encoding=None, **kwargs)¶
Return a new context from file source in given format.
- Parameters:
- Return type:
- Returns:
New
Context
instance.
- classmethod fromjson(path_or_fileobj, encoding='utf-8', ignore_lattice=False, require_lattice=False, raw=False)¶
Return a new context from json path or file-like object.
- Parameters:
path_or_fileobj –
str
,os.PathLike
, or file-like object open for reading.encoding (
str
) – Ignored for file-like objects under Python 3.ignore_lattice (
bool
) – Don’t load lattice from json serialization.require_lattice (
bool
) – Raise if no lattice in json serialization.raw (
bool
) – If set, sort so the input sequences can be in any order. If unset (default), assume input is already ordered for speedup
- Return type:
- Returns:
New
Context
instance.
- classmethod fromstring(source, frmat='table', **kwargs)¶
Return a new context from string
source
in given format.- Parameters:
- Return type:
- Returns:
New
Context
instance.
Example
>>> import concepts >>> context = concepts.Context.fromstring(concepts.EXAMPLE) >>> print(context) <Context object mapping 6 objects to 10 properties [b9d20179] at 0x...> |+1|-1|+2|-2|+3|-3|+sg|+pl|-sg|-pl| 1sg|X | | |X | |X |X | | |X | 1pl|X | | |X | |X | |X |X | | 2sg| |X |X | | |X |X | | |X | 2pl| |X |X | | |X | |X |X | | 3sg| |X | |X |X | |X | | |X | 3pl| |X | |X |X | | |X |X | |
- intension(objects, raw=False)¶
Return all properties shared by the given
objects
.- Parameters:
- Return type:
- Returns:
A tuple of
str
labels taken fromself.properties
.
Example
>>> import concepts >>> context = concepts.Context.fromstring(concepts.EXAMPLE) >>> context.intension(['1sg']) ('+1', '-2', '-3', '+sg', '-pl')
- lattice¶
The concept lattice of the formal context.
- Returns:
Cached or new
lattices.Lattice
instance.- Return type:
- neighbors(objects, raw=False)¶
Return the upper neighbors of the concept having all given
objects
.- Parameters:
- Return type:
- Returns:
A list of upper neighbor concepts as
(extent, intent)
pairs.
Example
>>> import concepts >>> context = concepts.Context.fromstring(concepts.EXAMPLE) >>> context.neighbors(['1sg', '1pl', '2pl']) [(('1sg', '1pl', '2sg', '2pl', '3sg', '3pl'), ())]
- property objects: Tuple[str, ...]¶
(Names of the) objects described by the context.
- Returns:
Context object labels.
Example
>>> import concepts >>> context = concepts.Context.fromstring(concepts.EXAMPLE) >>> context.objects ('1sg', '1pl', '2sg', '2pl', '3sg', '3pl')
- property properties: Tuple[str, ...]¶
(Names of the) properties that describe the objects.
- Returns:
Context property labels.
Example
>>> import concepts >>> context = concepts.Context.fromstring(concepts.EXAMPLE) >>> context.properties ('+1', '-1', '+2', '-2', '+3', '-3', '+sg', '+pl', '-sg', '-pl')
- relations(include_unary=False)¶
Return the logical relations between the context properties.
- Parameters:
include_unary (
bool
) – Include unary relations in result.- Return type:
Relations
- Returns:
Collection of binary (and optionally unary) logical relations.
Example
>>> import concepts >>> context = concepts.Context.fromstring(concepts.EXAMPLE) >>> print(context.relations()) +sg equivalent -pl +pl equivalent -sg +1 complement -1 +2 complement -2 +3 complement -3 +sg complement +pl +sg complement -sg +pl complement -pl -sg complement -pl +1 incompatible +2 +1 incompatible +3 +2 incompatible +3 +1 implication -2 +1 implication -3 +2 implication -1 +3 implication -1 +2 implication -3 +3 implication -2 -1 subcontrary -2 -1 subcontrary -3 -2 subcontrary -3
- shape¶
The shape/dimensions of the context.
- Returns:
Shape
instance.
Example
>>> import concepts >>> context = concepts.Context.fromstring(concepts.EXAMPLE) >>> context.shape Shape(objects=6, properties=10)
- todict(ignore_lattice=False)¶
Return serialized context with optional lattice.
- Parameters:
ingnore_lattice – Omit
'lattice'
in result. IfNone
,'lattice'
is omitted if it has not yet been computed.- Return type:
Dict
[str
,Union
[Tuple
[str
,...
],List
[Tuple
[int
,...
]],List
[Tuple
[Tuple
[int
,...
],Tuple
[int
,...
],Tuple
[int
,...
],Tuple
[int
,...
]]]]]- Returns:
A new
dict
with the serialized context.
Example
>>> import concepts >>> context = concepts.Context.fromstring(concepts.EXAMPLE) >>> context.todict() {'objects': ('1sg', '1pl', '2sg', '2pl', '3sg', '3pl'), 'properties': ('+1', '-1', '+2', '-2', '+3', '-3', '+sg', '+pl', '-sg', '-pl'), 'context': [(0, 3, 5, 6, 9), (0, 3, 5, 7, 8), (1, 2, 5, 6, 9), (1, 2, 5, 7, 8), (1, 3, 4, 6, 9), (1, 3, 4, 7, 8)], 'lattice': [((), (0, 1, 2, 3, 4, 5, 6, 7, 8, 9), (1, 2, 3, 4, 5, 6), ()), ((0,), (0, 3, 5, 6, 9), (7, 8, 9), (0,)), ((1,), (0, 3, 5, 7, 8), (7, 10, 11), (0,)), ((2,), (1, 2, 5, 6, 9), (8, 12, 13), (0,)), ((3,), (1, 2, 5, 7, 8), (10, 12, 14), (0,)), ((4,), (1, 3, 4, 6, 9), (9, 13, 15), (0,)), ((5,), (1, 3, 4, 7, 8), (11, 14, 15), (0,)), ((0, 1), (0, 3, 5), (18, 19), (1, 2)), ((0, 2), (5, 6, 9), (16, 18), (1, 3)), ((0, 4), (3, 6, 9), (16, 19), (1, 5)), ((1, 3), (5, 7, 8), (17, 18), (2, 4)), ((1, 5), (3, 7, 8), (17, 19), (2, 6)), ((2, 3), (1, 2, 5), (18, 20), (3, 4)), ((2, 4), (1, 6, 9), (16, 20), (3, 5)), ((3, 5), (1, 7, 8), (17, 20), (4, 6)), ((4, 5), (1, 3, 4), (19, 20), (5, 6)), ((0, 2, 4), (6, 9), (21,), (8, 9, 13)), ((1, 3, 5), (7, 8), (21,), (10, 11, 14)), ((0, 1, 2, 3), (5,), (21,), (7, 8, 10, 12)), ((0, 1, 4, 5), (3,), (21,), (7, 9, 11, 15)), ((2, 3, 4, 5), (1,), (21,), (12, 13, 14, 15)), ((0, 1, 2, 3, 4, 5), (), (), (18, 19, 20, 16, 17))]}
- tofile(filename, frmat='cxt', encoding='utf-8', **kwargs)¶
Save the context serialized to file in the given format.
- tojson(path_or_fileobj, encoding='utf-8', indent=None, sort_keys=True, ignore_lattice=False)¶
Write serialized context as json to path or file-like object.
- Parameters:
path_or_fileobj –
str
,os.PathLike
, or file-like object open for writing.encoding (
str
) – Ignored for file-like objects under Python 3.indent (
Optional
[int
]) –json.dump()
indent
for pretty-printing.sort_keys (
bool
) –json.dump()
sort_keys
for diffability.ingnore_lattice – Omit
'lattice'
in result. IfNone
,'lattice'
is omitted if it has not yet been computed.
- Return type:
- Returns:
None
Definition¶
- class concepts.Definition(objects=(), properties=(), bools=())¶
Mutable triple of
(objects, properties, bools)
for creating a context.Create definition from
objects
,properties
, andbools
correspondence.- Parameters:
- Returns:
New
Definition
instance.- Return type:
Example
>>> Definition(['man', 'woman'], ... ['male', 'female'], ... [(True, False), (False, True)]) <Definition(['man', 'woman'], ['male', 'female'], [(True, False), (False, True)])> >>> Definition() <Definition([], [], [])>
- __getitem__(pair)¶
Return the relation value for an (object, property) pair.
- Return type:
- Returns:
True
ifobject
hasproperty
elseFalse
.
Example
>>> from concepts import Definition >>> definition = Definition(['Mr. Praline', 'parrot'], ... ['alive', 'dead'], ... [(True, False), (False, True)]) >>> definition['Mr. Praline', 'alive'] True >>> definition['parrot', 'alive'] False >>> (definition[0], ... definition[1], ... definition[2]) == (definition.objects, ... definition.properties, ... definition.bools) True
- __iter__()¶
Yield
objects
,properties
, andbools
(e.g. for triple unpacking).Example
>>> from concepts import Definition >>> definition = Definition(['Mr. Praline', 'parrot'], ... ['alive', 'dead'], ... [(True, False), (False, True)]) >>> list(definition) [('Mr. Praline', 'parrot'), ('alive', 'dead'), [(True, False), (False, True)]]
- __setitem__(pair, value)¶
- Return type:
Example
>>> import concepts >>> definition = concepts.Definition(['King Arthur', 'grail'], ... ['holy'], ... [(False,), (True,)]) >>> definition['King Arthur', 'holy'] = True >>> print(definition) |holy| King Arthur|X | grail |X | >>> definition['King Arthur', 'holy'] = False >>> print(definition) |holy| King Arthur| | grail |X |
- add_object(obj, properties=())¶
Add an object to the definition and add
properties
as related.- Parameters:
- Return type:
- Returns:
None
Example
>>> import concepts >>> definition = concepts.Definition() >>> print(definition) | >>> definition.add_object('King Arthur') >>> print(definition) | King Arthur| >>> definition.add_object('King Arthur', ['human', 'knight', 'king']) >>> print(definition) |human|knight|king| King Arthur|X |X |X |
- add_property(prop, objects=())¶
Add a property to the definition and add
objects
as related.- Parameters:
- Return type:
- Returns:
None
Example
>>> import concepts >>> definition = concepts.Definition() >>> print(definition) | >>> definition.add_property('mysterious') >>> print(definition) |mysterious| >>> definition.add_property('mysterious', ['holy grail', 'Sir Robin']) >>> print(definition) |mysterious| holy grail|X | Sir Robin |X |
- property bools: List[Tuple[bool, ...]]¶
Row-major
list
of boolean tuples.Example
>>> from concepts import Definition >>> definition = Definition(['Mr. Praline', 'parrot'], ... ['alive', 'dead'], ... [(True, False), (False, True)]) >>> definition.bools [(True, False), (False, True)]
- copy()¶
Return an independent copy of the instance.
- Returns:
An instance of
type(self)
.
- crc32(*, encoding='utf-8')¶
Return hex-encoded unsigned CRC32 over encoded definition table string.
- Parameters:
encoding (
str
) – Encoding of the serialzation ('utf-8'
,'latin1'
,'ascii'
, …).- Return type:
- Returns:
The unsigned CRC32 checksum as hex-string.
Example
>>> from concepts import Definition >>> definition = Definition(['Mr. Praline', 'parrot'], ... ['alive', 'dead'], ... [(True, False), (False, True)]) >>> definition.crc32() '9642849e'
- property fill_ratio: Fraction¶
The fill ratio (density of
True
values) of the definition.Fill ratio 0.25 means that 25% of the values in
self.bools
areTrue
.- Returns:
Definition fill ratio (can be interpreted as percentages).
Example
>>> import concepts >>> definition = concepts.Definition(['King Arthur', 'grail'], ... ['holy', 'historic'], ... [(False, True), (True, True)]) >>> float(definition.fill_ratio) 0.75 >>> definition.fill_ratio Fraction(3, 4)
- classmethod fromfile(filename, frmat='cxt', encoding=None, **kwargs)¶
Return a new definiton from file source in given format.
- Parameters:
- Returns:
A new
Definition
instance.- Return type:
- intersection(other, ignore_conflicts=False)¶
Return a new definition from the intersection of the definitions.
- Parameters:
other (
Definition
) – AnotherDefinition
instance.ignore_conflicts (
bool
) – Allow overwrite from other.
- Returns:
A new
Definition
instance.- Return type:
Example
>>> import concepts >>> definition = concepts.Definition(['King Arthur'], ... ['human'], ... [(True,)]) >>> other = concepts.Definition(['King Arthur', 'grail'], ... ['human', 'holy'], ... [(True, True), (False, True)]) >>> print(definition.intersection(other)) |human| King Arthur|X |
- intersection_update(other, ignore_conflicts=False)¶
Update the definition with the intersection of the
other
.- Parameters:
other (
Definition
) – AnotherDefinition
instance.ignore_conflicts (
bool
) – Allow overwrite from other.
- Return type:
- Returns:
None
Example
>>> import concepts >>> definition = concepts.Definition(['King Arthur'], ... ['human'], ... [(True,)]) >>> other = concepts.Definition(['King Arthur', 'grail'], ... ['human', 'holy'], ... [(True, True), (False, True)]) >>> definition.intersection_update(other) >>> print(definition) |human| King Arthur|X |
- inverted()¶
Return a new definition flipping all booleans.
- Returns:
A new
Definition
instance.- Return type:
Example
>>> from concepts import Definition >>> definition = Definition(['Mr. Praline', 'parrot'], ... ['alive', 'dead'], ... [(True, False), (False, True)]) >>> print(definition.inverted()) |alive|dead| Mr. Praline| |X | parrot |X | |
- move_object(obj, index)¶
Reorder the definition such that object is at
index
.- Parameters:
- Return type:
- Returns:
None
Example
>>> import concepts >>> definition = concepts.Definition(['King Arthur', 'grail'], ... ['holy'], ... [(False,), (True,)]) >>> definition.move_object('grail', 0) >>> print(definition) |holy| grail |X | King Arthur| |
- move_property(prop, index)¶
Reorder the definition such that property is at
index
.- Parameters:
- Return type:
- Returns:
None
Example
>>> import concepts >>> definition = concepts.Definition(['King Arthur'], ... ['human', 'night', 'holy'], ... [(True, True, False)]) >>> definition.move_property('holy', 0) >>> print(definition) |holy|human|night| King Arthur| |X |X |
- property objects: Tuple[str, ...]¶
(Names of the) objects described by the definition.
Example
>>> from concepts import Definition >>> definition = Definition(['Mr. Praline', 'parrot'], ... ['alive', 'dead'], ... [(True, False), (False, True)]) >>> definition.objects ('Mr. Praline', 'parrot')
- property properties: Tuple[str, ...]¶
(Names of the) properties that describe the objects.
Example
>>> from concepts import Definition >>> definition = Definition(['Mr. Praline', 'parrot'], ... ['alive', 'dead'], ... [(True, False), (False, True)]) >>> definition.properties ('alive', 'dead')
- remove_empty_objects()¶
Remove objects without any
True
propertyExample
>>> import concepts >>> definition = concepts.Definition(['King Arthur', 'holy grail'], ... ['human'], ... [(True,), (False,)]) >>> definition.remove_empty_objects() ['holy grail'] >>> print(definition) |human| King Arthur|X |
- remove_empty_properties()¶
Remove properties without any
True
object.Example
>>> import concepts >>> definition = concepts.Definition(['King Arthur', 'holy grail'], ... ['moose'], ... [(False,), (False,)]) >>> definition.remove_empty_properties() ['moose'] >>> print(definition) | King Arthur| holy grail |
- remove_object(obj)¶
Remove an object from the definition.
Example
>>> import concepts >>> definition = concepts.Definition(['King Arthur', 'holy grail'], ... ['human'], ... [(True,), (False,)]) >>> definition.remove_object('holy grail') >>> print(definition) |human| King Arthur|X |
- remove_property(prop)¶
Remove a property from the definition.
Example
>>> import concepts >>> definition = concepts.Definition(['King Arthur', 'holy grail'], ... ['human'], ... [(True,), (False,)]) >>> definition.remove_property('human') >>> print(definition) | King Arthur| holy grail |
- rename_object(old, new)¶
Replace the name of an object by a new one.
- Parameters:
- Return type:
- Returns:
None
Example
>>> import concepts >>> definition = concepts.Definition(['King Arthur', 'holy grail'], ... ['human'], ... [(True,), (False,)]) >>> definition.rename_object('holy grail', 'grail') >>> print(definition) |human| King Arthur|X | grail | |
- rename_property(old, new)¶
Replace the name of a property by a new one.
- Parameters:
- Return type:
- Returns:
None
Example
>>> import concepts >>> definition = concepts.Definition(['King Arthur', 'grail'], ... ['holy'], ... [(False,), (True,)]) >>> definition.rename_property('holy', 'mysterious') >>> print(definition) |mysterious| King Arthur| | grail |X |
- set_object(obj, properties)¶
Add an object to the definition and set its
properties
.- Parameters:
- Return type:
- Returns:
None
Example
>>> import concepts >>> definition = concepts.Definition(['King Arthur', 'holy grail'], ... ['moose'], ... [(False,), (True,)]) >>> definition.set_object('holy grail', ['holy']) >>> print(definition) |moose|holy| King Arthur| | | holy grail | |X |
- set_property(prop, objects)¶
Add a property to the definition and set its
objects
.- Parameters:
- Return type:
- Returns:
None
Example
>>> import concepts >>> definition = concepts.Definition(['King Arthur', 'holy grail'], ... ['moose'], ... [(False,), (True,)]) >>> definition.set_property('moose', []) >>> print(definition) |moose| King Arthur| | holy grail | | >>> definition.set_property('moose', ['King Arthur', 'holy grail']) >>> print(definition) |moose| King Arthur|X | holy grail |X |
- property shape: Shape¶
The shape/dimensions of the definition.
- Returns:
New
Shape
instance.
Example
>>> import concepts >>> definition = concepts.Definition(['King Arthur', 'grail'], ... ['holy'], ... [(False,), (True,)]) >>> definition.shape Shape(objects=2, properties=1)
- take(objects=None, properties=None, reorder=False)¶
Return a subset with given
objects
/properties
as new definition.- Parameters:
- Returns:
A new
Definition
instance.- Return type:
Example
>>> from concepts import Definition >>> definition = Definition(['Mr. Praline', 'parrot'], ... ['alive', 'dead'], ... [(True, False), (False, True)]) >>> definition.take(['parrot']) <Definition(['parrot'], ['alive', 'dead'], [(False, True)])> >>> definition.take(properties=['dead']) <Definition(['Mr. Praline', 'parrot'], ['dead'], [(False,), (True,)])> >>> definition.take(['Brian'], ['alive', 'holy']) Traceback (most recent call last): ... KeyError: ['Brian', 'holy'] >>> definition.take(['parrot', 'Mr. Praline'], ['alive'], reorder=True) <Definition(['parrot', 'Mr. Praline'], ['alive'], [(False,), (True,)])>
- tostring(frmat='table', **kwargs)¶
Return the definition serialized in the given string-based format.
- Parameters:
frmat (
str
) – Format of the string ('table'
,'cxt'
,'csv'
).- Return type:
- Returns:
The definition as seralized string.
Example
>>> from concepts import Definition >>> definition = Definition(['Mr. Praline', 'parrot'], ... ['alive', 'dead'], ... [(True, False), (False, True)]) >>> print(definition.tostring()) |alive|dead| Mr. Praline|X | | parrot | |X |
- transposed()¶
Return a new definition swapping
objects
andproperties
.- Returns:
A new
Definition
instance.- Return type:
Example
>>> from concepts import Definition >>> definition = Definition(['Mr. Praline', 'parrot'], ... ['alive', 'dead'], ... [(True, False), (False, True)]) >>> print(definition.transposed()) |Mr. Praline|parrot| alive|X | | dead | |X |
- union(other, ignore_conflicts=False)¶
Return a new definition from the union of the definitions.
- Parameters:
other (
Definition
) – AnotherDefinition
instance.ignore_conflicts) –
- Returns:
A new
Definition
instance.- Return type:
Example
>>> import concepts >>> definition = concepts.Definition(['King Arthur'], ... ['human'], ... [(True,)]) >>> other = concepts.Definition(['grail'], ... ['holy'], ... [(True,)]) >>> print(definition.union(other)) |human|holy| King Arthur|X | | grail | |X |
- union_update(other, ignore_conflicts=False)¶
Update the definition with the union of the
other
.- Parameters:
other (
Definition
) – AnotherDefinition
instance.ignore_conflicts (
bool
) – Allow overwrite from other.
- Return type:
Example
>>> import concepts >>> definition = concepts.Definition(['King Arthur'], ... ['human'], ... [(True,)]) >>> other = concepts.Definition(['grail'], ... ['holy'], ... [(True,)]) >>> definition.union_update(other) >>> print(definition) |human|holy| King Arthur|X | | grail | |X |
Shape¶
- class concepts.Shape(objects: int, properties: int)¶
Tuple of
len(objects)
andlen(properties))
.Example
>>> import concepts >>> c = concepts.Context.fromstring(concepts.EXAMPLE) >>> c.shape Shape(objects=6, properties=10) >>> n_objects, n_properties = c.shape >>> n_objects, n_properties (6, 10)
- property columns: int¶
The number of (property) columns.
Example
>>> import concepts >>> c = concepts.Context.fromstring(concepts.EXAMPLE) >>> c.shape.columns 10
Lattice¶
- class concepts.lattices.Lattice(context, infimum=())¶
Formal concept lattice as directed acyclic graph of concepts.
Example
>>> import concepts >>> lattice = concepts.Context.fromstring(concepts.EXAMPLE).lattice >>> lattice <Lattice object of 6 atoms 22 concepts 5 coatoms at 0x...>
- __call__(properties)¶
Return concept having all given
properties
as intension.- Parameters:
- Return type:
- Returns:
Concept
instance from this lattice.
Example
>>> import concepts >>> lattice = concepts.Context.fromstring(concepts.EXAMPLE).lattice >>> lattice(['+1', '-sg']) <Atom {1pl} <-> [+1 -2 -3 +pl -sg] <=> 1pl>
- __getitem__(key)¶
Return concept by index, intension, or extension.
- Parameters:
key (
Union
[int
,Tuple
[str
,...
]]) – Integer index, properties tuple, or objects tuple.- Return type:
- Returns:
Concept
instance from this lattice.
Example
>>> import concepts >>> lattice = concepts.Context.fromstring(concepts.EXAMPLE).lattice >>> lattice[1:3] [<Atom {1sg} <-> [+1 -2 -3 +sg -pl] <=> 1sg>, <Atom {1pl} <-> [+1 -2 -3 +pl -sg] <=> 1pl>] >>> lattice['-1', '-sg'] <Concept {2pl, 3pl} <-> [-1 +pl -sg]> >>> lattice['1sg', '1pl', '2pl'] <Concept {1sg, 1pl, 2sg, 2pl} <-> [-3] <=> -3>
- __iter__()¶
Yield all concepts of the lattice.
Example
>>> import concepts >>> lattice = concepts.Context.fromstring(concepts.EXAMPLE).lattice >>> iterconcepts = iter(lattice) >>> next(iterconcepts) <Infimum {} <-> [+1 -1 +2 -2 +3 -3 +sg +pl -sg -pl]> >>> next(iterconcepts) <Atom {1sg} <-> [+1 -2 -3 +sg -pl] <=> 1sg>
- __len__()¶
Return the number of concepts in the lattice.
- Return type:
- Returns:
Number of lattice concepts.
Example
>>> import concepts >>> lattice = concepts.Context.fromstring(concepts.EXAMPLE).lattice >>> len(lattice) 22
- property atoms: Tuple[Atom, ...]¶
The minimal non-infimum concepts of the lattice.
- Returns:
Atom concepts of the lattice.
Example
>>> import concepts >>> lattice = concepts.Context.fromstring(concepts.EXAMPLE).lattice >>> lattice.atoms (<Atom {1sg} <-> [+1 -2 -3 +sg -pl] <=> 1sg>, <Atom {1pl} <-> [+1 -2 -3 +pl -sg] <=> 1pl>, <Atom {2sg} <-> [-1 +2 -3 +sg -pl] <=> 2sg>, <Atom {2pl} <-> [-1 +2 -3 +pl -sg] <=> 2pl>, <Atom {3sg} <-> [-1 -2 +3 +sg -pl] <=> 3sg>, <Atom {3pl} <-> [-1 -2 +3 +pl -sg] <=> 3pl>)
- downset_union(concepts, _sortkey=operator.attrgetter('dindex'), _next_concepts=operator.attrgetter('lower_neighbors'))¶
Yield all concepts that imply any of the given ones.
- Parameters:
concepts (
Iterable
[Concept
]) –Concept
instances from this lattice.- Yields:
Concept
instances from this lattice.- Return type:
Example
>>> import concepts >>> lattice = concepts.Context.fromstring(concepts.EXAMPLE).lattice >>> list(lattice.downset_union([lattice['+1',], lattice['+2',]])) [<Concept {1sg, 1pl} <-> [+1 -2 -3] <=> +1>, <Concept {2sg, 2pl} <-> [-1 +2 -3] <=> +2>, <Atom {1sg} <-> [+1 -2 -3 +sg -pl] <=> 1sg>, <Atom {1pl} <-> [+1 -2 -3 +pl -sg] <=> 1pl>, <Atom {2sg} <-> [-1 +2 -3 +sg -pl] <=> 2sg>, <Atom {2pl} <-> [-1 +2 -3 +pl -sg] <=> 2pl>, <Infimum {} <-> [+1 -1 +2 -2 +3 -3 +sg +pl -sg -pl]>]
- graphviz(filename=None, directory=None, render=False, view=False, make_object_label=<built-in method join of str object>, make_property_label=<built-in method join of str object>, **kwargs)¶
Return DOT source for visualizing the lattice graph.
- Parameters:
filename – Path to the DOT source file for the Digraph.
directory – (Sub)directory for DOT source saving and rendering.
render (
bool
) – Call.render()
on the result.view (
bool
) – Call.render(view=True)
on the result.make_object_label – Callable with iterable of objects argument returning a string to be used as object label.
make_property_label – Callable with iterable of properties argument returning a string to be used as object label.
- Return type:
Digraph
- Returns:
A
graphviz.Digraph
instance.
- property infimum: Infimum¶
The most specific concept of the lattice.
- Returns:
Infimum concept of the lattice.
Example
>>> import concepts >>> lattice = concepts.Context.fromstring(concepts.EXAMPLE).lattice >>> lattice.infimum <Infimum {} <-> [+1 -1 +2 -2 +3 -3 +sg +pl -sg -pl]>
- join(concepts)¶
Return the nearest concept that subsumes all given concepts.
- Parameters:
concepts (
Iterable
[Concept
]) –Concept
instances from this lattice.- Return type:
- Returns:
Concept
instance from this lattice.
Example
>>> import concepts >>> lattice = concepts.Context.fromstring(concepts.EXAMPLE).lattice >>> lattice.join([]) <Infimum {} <-> [+1 -1 +2 -2 +3 -3 +sg +pl -sg -pl]> >>> lattice.join([lattice['1sg',], lattice['1pl',], lattice['2sg',]]) <Concept {1sg, 1pl, 2sg, 2pl} <-> [-3] <=> -3>
- meet(concepts)¶
Return the nearest concept that implies all given concepts.
- Parameters:
concepts (
Iterable
[Concept
]) –Concept
instances from this lattice.- Return type:
- Returns:
Concept
instance from this lattice.
Example
>>> import concepts >>> lattice = concepts.Context.fromstring(concepts.EXAMPLE).lattice >>> lattice.meet([]) <Supremum {1sg, 1pl, 2sg, 2pl, 3sg, 3pl} <-> []> >>> lattice.meet([lattice['-1',], lattice['-2',], lattice['-pl',]]) <Atom {3sg} <-> [-1 -2 +3 +sg -pl] <=> 3sg>
- property supremum: Supremum¶
The most general concept of the lattice.
- Returns:
Supremum concept of the lattice.
Example
>>> import concepts >>> lattice = concepts.Context.fromstring(concepts.EXAMPLE).lattice >>> lattice.supremum <Supremum {1sg, 1pl, 2sg, 2pl, 3sg, 3pl} <-> []>
- upset_generalization(concepts)¶
Yield all concepts that subsume only the given ones.
- Parameters:
concepts (
Iterable
[Concept
]) –Concept
instances from this lattice.- Yields:
Concept
instances from this lattice.- Return type:
Note
This method is EXPERIMENTAL and might change without notice.
- upset_union(concepts, _sortkey=operator.attrgetter('index'), _next_concepts=operator.attrgetter('upper_neighbors'))¶
Yield all concepts that subsume any of the given ones.
- Parameters:
concepts (
Iterable
[Concept
]) –Concept
instances from this lattice.- Yields:
Concept
instances from this lattice.- Return type:
Example
>>> import concepts >>> lattice = concepts.Context.fromstring(concepts.EXAMPLE).lattice >>> list(lattice.upset_union([lattice['+1',], lattice['+2',]])) [<Concept {1sg, 1pl} <-> [+1 -2 -3] <=> +1>, <Concept {2sg, 2pl} <-> [-1 +2 -3] <=> +2>, <Concept {1sg, 1pl, 2sg, 2pl} <-> [-3] <=> -3>, <Concept {1sg, 1pl, 3sg, 3pl} <-> [-2] <=> -2>, <Concept {2sg, 2pl, 3sg, 3pl} <-> [-1] <=> -1>, <Supremum {1sg, 1pl, 2sg, 2pl, 3sg, 3pl} <-> []>]
Concept¶
- class concepts.lattices.Concept(lattice, extent, intent, upper, lower)¶
Formal concept as pair of extent and intent.
Example
>>> import concepts >>> lattice = concepts.Context.fromstring(concepts.EXAMPLE).lattice >>> concept = lattice['+1',] >>> concept <Concept {1sg, 1pl} <-> [+1 -2 -3] <=> +1> >>> concept.index, concept.dindex (7, 6) >>> concept.objects, concept.properties ((), ('+1',)) >>> concept.atoms (<Atom {1sg} <-> [+1 -2 -3 +sg -pl] <=> 1sg>, <Atom {1pl} <-> [+1 -2 -3 +pl -sg] <=> 1pl>) >>> concept.upper_neighbors (<Concept {1sg, 1pl, 2sg, 2pl} <-> [-3] <=> -3>, <Concept {1sg, 1pl, 3sg, 3pl} <-> [-2] <=> -2>) >>> concept.lower_neighbors (<Atom {1sg} <-> [+1 -2 -3 +sg -pl] <=> 1sg>, <Atom {1pl} <-> [+1 -2 -3 +pl -sg] <=> 1pl>)
- __iter__()¶
Yield
extent
andintent
(e.g. for pair unpacking).Example
>>> import concepts >>> lattice = concepts.Context.fromstring(concepts.EXAMPLE).lattice >>> extent, intent = lattice['+1',] >>> print(extent, intent) ('1sg', '1pl') ('+1', '-2', '-3')
- attributes()¶
Yield properties generating the concept in shortlex order.
Example
>>> import concepts >>> lattice = concepts.Context.fromstring(concepts.EXAMPLE).lattice >>> list(lattice['+1',].attributes()) [('+1',), ('+1', '-2'), ('+1', '-3'), ('-2', '-3'), ('+1', '-2', '-3')]
- complement_of(other)¶
Infimum meet and supremum join comparison.
- Parameters:
- Returns:
True
ifself
is the complement ofother
elseFalse
.- Return type:
Example
>>> import concepts >>> lattice = concepts.Context.fromstring(concepts.EXAMPLE).lattice >>> lattice['+1',].complement_of(lattice['-1',]) True >>> lattice['+1',].complement_of(lattice['+3',]) False
- downset(_sortkey=operator.attrgetter('dindex'), _next_concepts=operator.attrgetter('lower_neighbors'))¶
Yield subsumed concepts including
self
.- Yields:
Concept
instances.
Example
>>> import concepts >>> lattice = concepts.Context.fromstring(concepts.EXAMPLE).lattice >>> list(lattice['+1',].downset()) [<Concept {1sg, 1pl} <-> [+1 -2 -3] <=> +1>, <Atom {1sg} <-> [+1 -2 -3 +sg -pl] <=> 1sg>, <Atom {1pl} <-> [+1 -2 -3 +pl -sg] <=> 1pl>, <Infimum {} <-> [+1 -1 +2 -2 +3 -3 +sg +pl -sg -pl]>]
- property extent: Tuple[str, ...]¶
The objects subsumed by the concept.
Example
>>> import concepts >>> lattice = concepts.Context.fromstring(concepts.EXAMPLE).lattice >>> lattice['+1',].extent ('1sg', '1pl')
- implies(other)¶
Implication comparison.
- Parameters:
- Returns:
True
ifself
impliesother
elseFalse
.- Return type:
Example
>>> import concepts >>> lattice = concepts.Context.fromstring(concepts.EXAMPLE).lattice >>> lattice['+1',] <= lattice['-3',] <= lattice['-3',] <= lattice[()] True >>> lattice['+1',] <= lattice['+sg',] or lattice['+sg',] <= lattice['+1',] False
- incompatible_with(other)¶
Infimum meet comparison.
- Parameters:
- Returns:
True
ifself
is incompatible withother
elseFalse
.- Return type:
Example
>>> import concepts >>> lattice = concepts.Context.fromstring(concepts.EXAMPLE).lattice >>> lattice['+1',].incompatible_with(lattice['+3',]) True >>> lattice['+1',].incompatible_with(lattice['+sg',]) False
- property intent: Tuple[str, ...]¶
The properties implied by the concept.”
Example
>>> import concepts >>> lattice = concepts.Context.fromstring(concepts.EXAMPLE).lattice >>> lattice['+1',].intent ('+1', '-2', '-3')
- join(other)¶
Least upper bound, supremum, or, generalization.
- Parameters:
- Return type:
- Returns:
Concept
instance from the same lattice.
Example
>>> import concepts >>> lattice = concepts.Context.fromstring(concepts.EXAMPLE).lattice >>> lattice['+1',].join(lattice['+2',]) <Concept {1sg, 1pl, 2sg, 2pl} <-> [-3] <=> -3> >>> lattice['+2',] | lattice['+1',] <Concept {1sg, 1pl, 2sg, 2pl} <-> [-3] <=> -3>
- lattice¶
The lattice containing the concept.
- lower_neighbors¶
The directly subsumed concepts.
- meet(other)¶
Greatest lower bound, infimum, and, unification.
- Parameters:
- Return type:
- Returns:
Concept
instance from the same lattice.
Example
>>> import concepts >>> lattice = concepts.Context.fromstring(concepts.EXAMPLE).lattice >>> lattice['-1', '-2'].meet(lattice['-pl',]) <Atom {3sg} <-> [-1 -2 +3 +sg -pl] <=> 3sg> >>> lattice['-pl',] & lattice['-1', '-2'] <Atom {3sg} <-> [-1 -2 +3 +sg -pl] <=> 3sg>
- minimal()¶
Shortlex minimal properties generating the concept.
Example
>>> import concepts >>> lattice = concepts.Context.fromstring(concepts.EXAMPLE).lattice >>> lattice['+1',].minimal() ('+1',)
Note
For
Infimum
, this returns all properties instead of the first contradictory subset of properties.
- orthogonal_to(other)¶
Non-infimum meet, incomparable, and non-supremum join comparison.
- Parameters:
- Returns:
True
ifself
is orthogonal toother
elseFalse
.- Return type:
Example
>>> import concepts >>> lattice = concepts.Context.fromstring(concepts.EXAMPLE).lattice >>> lattice['+1',].orthogonal_to(lattice['+sg',]) True >>> lattice['+1',].orthogonal_to(lattice['+3',]) False
- properly_implies(other)¶
Proper implication comparison.
- Parameters:
- Returns:
True
ifself
properly impliesother
elseFalse
.- Return type:
Example
>>> import concepts >>> lattice = concepts.Context.fromstring(concepts.EXAMPLE).lattice >>> lattice['+1',] < lattice['-3',] < lattice[()] True
- properly_subsumes(other)¶
Proper subsumption comparison.
- Parameters:
- Returns:
True
ifself
properly subsumesother
elseFalse
.- Return type:
Example
>>> import concepts >>> lattice = concepts.Context.fromstring(concepts.EXAMPLE).lattice >>> lattice['+1',] > lattice['+1', '+sg'] > lattice['+1', '-1'] True
- subcontrary_with(other)¶
Non-infimum meet and supremum join comparison.
- Parameters:
- Returns:
True
ifself
is the subcontrary toother
elseFalse
.- Return type:
Example
>>> import concepts >>> lattice = concepts.Context.fromstring(concepts.EXAMPLE).lattice >>> lattice['-1',].subcontrary_with(lattice['-3',]) True >>> lattice['-1',].subcontrary_with(lattice['+sg',]) False
- subsumes(other)¶
Subsumption comparison.
- Parameters:
- Returns:
True
ifself
subsumesother
elseFalse
.- Return type:
Example
>>> import concepts >>> lattice = concepts.Context.fromstring(concepts.EXAMPLE).lattice >>> lattice['+1',] >= lattice['+1', '+sg'] >= lattice['+1', '+sg'] >= lattice['+1', '-1'] True >>> lattice['+1',] >= lattice['+sg',] or lattice['+sg',] >= lattice['+1',] False
- upper_neighbors¶
The directly implied concepts.
- upset(_sortkey=operator.attrgetter('index'), _next_concepts=operator.attrgetter('upper_neighbors'))¶
Yield implied concepts including
self
.- Yields:
Concept
instances.
Example
>>> import concepts >>> lattice = concepts.Context.fromstring(concepts.EXAMPLE).lattice >>> list(lattice['+1',].upset()) [<Concept {1sg, 1pl} <-> [+1 -2 -3] <=> +1>, <Concept {1sg, 1pl, 2sg, 2pl} <-> [-3] <=> -3>, <Concept {1sg, 1pl, 3sg, 3pl} <-> [-2] <=> -2>, <Supremum {1sg, 1pl, 2sg, 2pl, 3sg, 3pl} <-> []>]