py-galactic¶
py-galactic is a package for studying Formal Concept Analysis.
Install py-galactic¶
py-galactic requires python 3, a programming language that comes pre-installed on linux and Mac OS X, and which is easily installed on Windows.
Install py-galactic using the bash command
pip install py-galactic
To upgrade to the most recent release, use
pip install --upgrade py-galactic
pip is a script that downloads and installs modules from the Python Package Index, PyPI. It should come installed with your python distribution. If you are running linux, pip may be bundled separately. On a Debian-based system (including Ubuntu), you can install it using
apt-get update
apt-get install python-pip
Getting Help¶
Important
If you have any difficulties with py-galactic, please feel welcome to file an issue on github so that we can help.
Context¶
The galactic.context
package defines generic classes for using contexts:
Context
: a context is composed by a population and a modelPopulation
a population is a container for individualsModel
a model is a container for attributesIndividual
an individual has an identifier and valuesAttribute
an attribute has a name and a type
-
class
galactic.context.
P
¶ Generic subclass of the
Population
class
-
class
galactic.context.
X
¶ Generic subclass of the
Individual
class
-
class
galactic.context.
Context
[source]¶ A
Context
handles a model and a population.It’s possible to access to the context
population
attribute or to the contextmodel
attribute.Example
>>> from galactic.context.memory import MemoryContext >>> context = MemoryContext( ... definition={'mybool': bool, 'myint': int}, ... individuals=['0', '1'] ... ) >>> print(context.population) ['0', '1'] >>> print(context.model) {'mybool': <class 'bool'>, 'myint': <class 'int'>}
It’s possible to check if a context is not empty (both
model
andpopulation
are not empty) using the python builtinbool()
function.It’s possible to get a readable representation of a context using the python builtin
str()
function.Example
>>> print(context) {'population': ['0', '1'], 'model': {'mybool': <class 'bool'>, 'myint': <class 'int'>}}
Example
>>> bool(context) True
Contexts are container for individuals and attributes. It’s possible to know if an individual or an attribute belongs to a context using the python
in
keyword.Example
>>> context.model['mybool'] in context True >>> context.population['0'] in context True
New in version 0.0.1.
-
class
galactic.context.
Population
[source]¶ A
Population
is a container for individuals.It’s possible to access to the population
context
attribute.Example
>>> from galactic.context.memory import MemoryContext >>> context = MemoryContext( ... definition={'mybool': bool, 'myint': int}, ... individuals=['0', '1'] ... ) >>> population = context.population >>> print(population.context) {'population': ['0', '1'], 'model': {'mybool': <class 'bool'>, 'myint': <class 'int'>}}
It’s possible to get a readable representation of a population.
Example
>>> print(population) ['0', '1']
It’s possible to check if a population is not empty using the python builtin
bool()
function.Example
>>> bool(population) True
It’s possible to access to an individual with its identifier using the python array access construct.
Example
>>> print(population['0']) {'mybool': False, 'myint': 0}
It’s possible to check if an individual belongs to a population using the python
in
keyword.Example
>>> '0' in population True
It’s possible to iterate over a population using the python
for
keyword.Example
>>> {ident: str(individual) for ident, individual in population.items()} {'0': "{'mybool': False, 'myint': 0}", '1': "{'mybool': False, 'myint': 0}"}
It’s possible to get the length of a population using the python builtin
len()
function.Example
>>> len(population) 2
New in version 0.0.1.
-
class
galactic.context.
Model
[source]¶ A
Model
is a container for attributes.It’s possible to access to the model
context
attribute.Example
>>> from galactic.context.memory import MemoryContext >>> context = MemoryContext( ... definition={'mybool': bool, 'myint': int}, ... individuals=['0', '1'] ... ) >>> model = context.model >>> print(model.context) {'population': ['0', '1'], 'model': {'mybool': <class 'bool'>, 'myint': <class 'int'>}}
It’s possible to get a readable representation of a model using the python builtin
str()
function.Example
>>> print(model) {'mybool': <class 'bool'>, 'myint': <class 'int'>}
It’s possible to check if a model is not empty using the python builtin
bool()
function.Example
>>> bool(model) True
It’s possible to access to an attribute with its name using the python array access construct.
Example
>>> print(model['mybool']) {'name': 'mybool', 'type': <class 'bool'>}
It’s possible to check if an attribute belongs to a model using the python
in
keyword.Example
>>> 'mybool' in model True
It’s possible to iterate over a model using the python
for
keyword.Example
>>> {name: attribute.type for name, attribute in model.items()} {'mybool': <class 'bool'>, 'myint': <class 'int'>}
It’s possible to get the length of a population using the python builtin
len()
function.Example
>>> len(model) 2
New in version 0.0.1.
-
class
galactic.context.
Individual
[source]¶ A
Individual
is a container for values.It’s possible to access to the individual
identifier
attribute.Example
>>> from galactic.context.memory import MemoryContext >>> context = MemoryContext( ... definition={'mybool': bool, 'myint': int}, ... individuals=['0', '1'] ... ) >>> individual = context.population['0'] >>> individual.identifier '0'
It’s possible to access to the individual
context
attribute.Example
>>> print(individual.context) {'population': ['0', '1'], 'model': {'mybool': <class 'bool'>, 'myint': <class 'int'>}}
It’s possible to access to the individual
model
attribute.Example
>>> print(individual.model) {'mybool': <class 'bool'>, 'myint': <class 'int'>}
It’s possible to access to the individual
population
attribute.Example
>>> print(individual.population) ['0', '1']
It’s possible to get a readable representation of an individual using the python builtin
str()
function.Example
>>> print(individual) {'mybool': False, 'myint': 0}
It’s possible to access to the individual values using the
value()
method.Example
>>> attribute = individual.model['mybool'] >>> individual.value(attribute) False
It’s possible to access to the individual values using the python array access construct.
Example
>>> individual['mybool'] False
It’s possible to get the length of an individual using the python builtin
len()
function.Example
>>> len(individual) 2
It’s possible to iterate over an individual using the python
for
keyword.Example
>>> {name: value for name, value in individual.items()} {'mybool': False, 'myint': 0}
New in version 0.0.1.
-
identifier
¶ Get this individual identifier.
Returns: the individual identifier Return type: str
New in version 0.0.1.
-
population
¶ Get the underlying population.
Returns: the underlying population Return type: P
New in version 0.0.1.
-
context
¶ Get the underlying context.
Returns: the underlying context Return type: C
New in version 0.0.1.
-
value
(attribute: A)[source]¶ Get the attribute value for this individual.
Parameters: attribute ( A
) – the attributeReturns: the value Return type: object
Raises: ValueError
– if the attribute does not belong to the underlying model.New in version 0.0.1.
-
-
class
galactic.context.
Attribute
[source]¶ A
Attribute
is described by aname
and atype
.It’s possible to access to the attribute
identifier
andtype
attributes.Example
>>> from galactic.context.memory import MemoryContext >>> context = MemoryContext( ... definition={'mybool': bool, 'myint': int}, ... individuals=['0', '1'] ... ) >>> attribute = context.model['mybool'] >>> attribute.name 'mybool' >>> attribute.type <class 'bool'>
It’s possible to access to the individual
context
attribute.Example
>>> print(attribute.context) {'population': ['0', '1'], 'model': {'mybool': <class 'bool'>, 'myint': <class 'int'>}}
It’s possible to access to the individual
model
attribute.Example
>>> print(attribute.model) {'mybool': <class 'bool'>, 'myint': <class 'int'>}
It’s possible to access to the individual
population
attribute.Example
>>> print(attribute.population) ['0', '1']
It’s possible to get a readable representation of an attribute using the python builtin
str()
function.Example
>>> print(attribute) {'name': 'mybool': 'type': <class 'bool'>}
It’s possible to access to the attribute values using the
value()
method.Example
>>> individual = attribute.population['0'] >>> attribute.value(individual) False
It’s possible to access to the attribute values using the python array access construct.
Example
>>> attribute['0'] False
It’s possible to get the length of an attribute using the python builtin
len()
function.Example
>>> len(attribute) 2
It’s possible to iterate over an attribute using the python
for
keyword.Example
>>> {identifier: value for identifier, value in attribute.items()} {'0': False, '1': False}
New in version 0.0.1.
-
context
¶ Get the underlying context.
Returns: the underlying context Return type: C
New in version 0.0.1.
-
population
¶ Get the underlying population.
Returns: the underlying population Return type: P
New in version 0.0.1.
-
value
(individual: X)[source]¶ Get the individual value for this attribute.
Parameters: individual ( X
) – the individualReturns: the value Return type: object
Raises: ValueError
– if the individual does not belong to the underlying population.New in version 0.0.1.
-
Mixins¶
The galactic.context.mixins
package defines mixins classes for defining new types of
contexts:
ConcreteIndividual
for defining individuals that own their identifier as a fieldConcreteAttribute
for defining attributes that own their name and their type as fieldsContextHolder
for defining elements that own their context as a fieldPopulationHolder
for defining elements that own their population as a fieldModelHolder
for defining elements that own their model as a fieldAttributesHolder
for defining models that own their attributes as a fieldIndividualsHolder
for defining population that own their individuals as a fieldValuesHolder
for defining individuals that own their values as a field
They are widely used for defining
in the galactic.context.memory
package.
New in version 0.0.1.
-
class
galactic.context.mixins.
ConcreteIndividual
(**kwargs)[source]¶ The
ConcreteIndividual
class is a mixin used in subclassing theIndividual
class for storing their identifier as a field.New in version 0.0.1.
-
class
galactic.context.mixins.
ConcreteAttribute
(**kwargs)[source]¶ The
ConcreteAttribute
class is a mixin used in subclassing theAttribute
class for storing their name and their type as a class.New in version 0.0.1.
-
class
galactic.context.mixins.
ContextHolder
(**kwargs)[source]¶ The
ContextHolder[C]
is a mixin used for storing an element context as a field.It’s a generic class that depends of a
Context
subclassC
.New in version 0.0.1.
-
__init__
(**kwargs)[source]¶ Initialise an element by setting its context.
Keyword Arguments: context ( C
) – the contextNew in version 0.0.1.
-
context
¶ Get the context.
Returns: the context Return type: C
New in version 0.0.1.
-
-
class
galactic.context.mixins.
PopulationHolder
(**kwargs)[source]¶ The
PopulationHolder[P]
is a mixin used for storing an element population as a field.It’s a generic class that depends of a
Population
subclassP
.New in version 0.0.1.
-
__init__
(**kwargs)[source]¶ Initialise an element by setting its population.
Keyword Arguments: population ( P
) – the populationNew in version 0.0.1.
-
population
¶ Get the population.
Returns: the population Return type: P
New in version 0.0.1.
-
-
class
galactic.context.mixins.
ModelHolder
(**kwargs)[source]¶ The
ModelHolder[M]
class is a mixin used for storing an element model as a field.It’s a generic class that depends of a
Model
subclassM
.New in version 0.0.1.
-
__init__
(**kwargs)[source]¶ Initialise an element by setting its model.
Keyword Arguments: model ( M
) – the modelNew in version 0.0.1.
-
model
¶ Get the model.
Returns: the model Return type: M
New in version 0.0.1.
-
-
class
galactic.context.mixins.
AttributesHolder
(**kwargs)[source]¶ The
AttributesHolder[A]
class is a mixin used for storing the model attributes in memory.It’s a generic class that depends of an
Attribute
subclassA
.New in version 0.0.1.
-
__init__
(**kwargs)[source]¶ Initialise an attribute holder.
Keyword Arguments: attributes ( Iterable[A]
) – the attributesNew in version 0.0.1.
-
-
class
galactic.context.mixins.
IndividualsHolder
(**kwargs)[source]¶ The
IndividualsHolder[X]
class is a mixin used for storing the population individuals in memory.It’s a generic class that depends of an
Individual
subclassX
.New in version 0.0.1.
-
__init__
(**kwargs)[source]¶ Initialise an individuals holder.
Keyword Arguments: individuals ( Iterable[A]
) – the individualsNew in version 0.0.1.
-
-
class
galactic.context.mixins.
ValuesHolder
(**kwargs)[source]¶ The
ValuesHolder[A]
class is a mixin for storing the individual values in memory.It’s a generic class that depends of an
Attribute
subclassA
.New in version 0.0.1.
-
__init__
(**kwargs)[source]¶ Initialise a values holder.
Keyword Arguments: values ( Mapping[str, object]
) – the initial (name, value) pairsNew in version 0.0.1.
-
MemoryContext¶
The galactic.context.memory
module give the ability to define
Context
that resides in memory.
-
class
galactic.context.memory.
MemoryContext
(**kwargs)[source]¶ The
MemoryContext
class is designed to define contexts in memory. It inherits of all the behavior from theContext
class and allows direct creation and modification of a context.It’s possible to create a context without nothing.
Example
>>> from galactic.context.memory import MemoryContext >>> context = MemoryContext() >>> print(context) {'population': [], 'model': {}}
It’s possible to create a context specifying the model definition.
Example
>>> from galactic.context.memory import MemoryContext >>> context = MemoryContext(definition={'mybool': bool, 'myint': int}) >>> print(context) {'population': [], 'model': {'mybool': <class 'bool'>, 'myint': <class 'int'>}}
It’s possible to create a context specifying the model definition and the list of individual identifiers.
Example
>>> context = MemoryContext( ... definition={'mybool': bool, 'myint': int}, ... individuals=['0', '1'] ... ) >>> print(context) {'population': ['0', '1'], 'model': {'mybool': <class 'bool'>, 'myint': <class 'int'>}}
It’s possible to create a context specifying the model definition and the individual values.
Example
>>> context = MemoryContext( ... definition={'mybool': bool, 'myint': int}, ... individuals={'0': {'mybool': True}, '1':{'myint': 1}} ... ) >>> {ident: str(context.population[ident]) for ident in context.population} {'0': "{'mybool': True, 'myint': 0}", '1': "{'mybool': False, 'myint': 1}"}
New in version 0.0.1.
-
__init__
(**kwargs)[source]¶ Initialise a context in memory.
Keyword Arguments: - definition (
Mapping[str, type]
) – definition of the context by a mapping from name of attributes to their type - individuals (
Union[Iterable[str], Mapping[str, Mapping[str, object]]]
) – initial iterable of individual identifiers or a mapping from individual identifiers to individual values
Raises: KeyError
– if an attribute is not in the definitionValueError
– if a value does not correspond to an attribute typeTypeError
– if the definition or if the individuals parameter are not of the correct type
New in version 0.0.1.
- definition (
-
-
class
galactic.context.memory.
MemoryModel
(context: galactic.context.memory.MemoryContext, definition: typing.Mapping[str, typing.type])[source]¶ The
MemoryModel
class is designed to define models that resides in memory. It inherits of all the behavior from theModel
class.It’s possible to change or to set attribute values.
Example
>>> from galactic.context.memory import MemoryContext >>> context = MemoryContext( ... definition={'mybool': bool, 'myint': int}, ... individuals=['0', '1'] ... ) >>> model = context.model >>> model['mybool'] = int >>> model['myint2'] = int >>> print(context.population['0']) {'mybool': 0, 'myint': 0, 'myint2': 0}
It’s possible to delete an attribute using its name.
Example
>>> from galactic.context.memory import MemoryContext >>> context = MemoryContext( ... definition={'mybool': bool, 'myint': int}, ... individuals=['0', '1'] ... ) >>> model = context.model >>> del model['mybool'] >>> {ident: str(context.population[ident]) for ident in context.population} {'0': "{'myint': 0}", '1': "{'myint': 0}"}
New in version 0.0.1.
-
__init__
(context: galactic.context.memory.MemoryContext, definition: typing.Mapping[str, typing.type])[source]¶ Initialise a model in memory.
Parameters: - context (
MemoryContext
) – the underlying context - definition (
Mapping[str, type]
) – the attributes definition
New in version 0.0.1.
- context (
-
-
class
galactic.context.memory.
MemoryPopulation
(context: galactic.context.memory.MemoryContext, identifiers: typing.Iterable[str])[source]¶ The
MemoryPopulation
class is designed to define populations that resides in memory. It inherits of all the behavior from thePopulation
class.It’s possible to change or to set individual values.
Example
>>> from galactic.context.memory import MemoryContext >>> context = MemoryContext( ... definition={'mybool': bool, 'myint': int}, ... individuals=['0', '1'] ... ) >>> population = context.population >>> population['0'] = {'mybool': True} >>> population['2'] = {'myint': 1} >>> print(population['0']) {'mybool': True, 'myint': 0} >>> print(population['2']) {'mybool': False, 'myint': 1}
It’s possible to delete an individual using its identifier.
Example
>>> from galactic.context.memory import MemoryContext >>> context = MemoryContext( ... definition={'mybool': bool, 'myint': int}, ... individuals=['0', '1'] ... ) >>> population = context.population >>> del population['0'] >>> {ident: str(context.population[ident]) for ident in context.population} {'1': "{'mybool': False, 'myint': 0}"}
New in version 0.0.1.
-
__init__
(context: galactic.context.memory.MemoryContext, identifiers: typing.Iterable[str])[source]¶ Initialise a population in memory.
Parameters: - context (
MemoryContext
) – the underlying context - identifiers (
Iterable[str]
) – an iterable of identifiers
New in version 0.0.1.
- context (
-
-
class
galactic.context.memory.
MemoryIndividual
(population: galactic.context.memory.MemoryPopulation, identifier: str)[source]¶ The
MemoryIndividual
is designed to define individuals that resides in memory. It inherits of all the behavior from theIndividual
class.It’s possible to modify a value for an individual using an attribute name.
Example
>>> from galactic.context.memory import MemoryContext >>> context = MemoryContext( ... definition={'mybool': bool, 'myint': int}, ... individuals=['0', '1'] ... ) >>> individual = context.population['0'] >>> individual['mybool'] = True >>> individual['myint'] = 1 >>> {ident: str(context.population[ident]) for ident in context.population} {'0': "{'mybool': True, 'myint': 1}", '1': "{'mybool': False, 'myint': 0}"}
New in version 0.0.1.
-
__init__
(population: galactic.context.memory.MemoryPopulation, identifier: str)[source]¶ Initialise an individual.
Parameters: - population (
MemoryPopulation
) – the population - identifier (
str
) – the individual identifier
New in version 0.0.1.
- population (
-
-
class
galactic.context.memory.
MemoryAttribute
(model: galactic.context.memory.MemoryModel, name: str, cls: type)[source]¶ The
MemoryAttribute
is designed to define attributes that resides in memory. It inherits of all the behavior from theAttribute
class.It’s possible to modify a value for an attribute using an individual identifier.
Example
>>> from galactic.context.memory import MemoryContext >>> context = MemoryContext( ... definition={'mybool': bool, 'myint': int}, ... individuals=['0', '1'] ... ) >>> attribute = context.model['myint'] >>> attribute['0'] = 3 >>> attribute['1'] = 4 >>> {ident: str(context.population[ident]) for ident in context.population} {'0': "{'mybool': False, 'myint': 3}", '1': "{'mybool': False, 'myint': 4}"}
New in version 0.0.1.
-
__init__
(model: galactic.context.memory.MemoryModel, name: str, cls: type)[source]¶ Initialise an attribute.
Parameters: - model (
MemoryModel
) – the underlying model - name (
str
) – the attribute name - cls (
type
) – the attribute type
New in version 0.0.1.
- model (
-
Types¶
A type is associated to an Attribute
. A type is valid if it is
a class that can be called
- without argument: in that case a default value is returned for that type
- with an argument: in that case, the class try to convert it to an acceptable value for that type
For example the int
and bool
classes are acceptable types as they accept to be
called without arguments or with an argument which is converted to the desired type.
Categories¶
Boolean¶
Number¶
This module one type useful in Context
values:
ImpreciseFloat
for representing imprecise float as intervals.ImpreciseInteger
for representing imprecise int as intervals.
-
class
galactic.type.number.
ImpreciseNumber
(**kwargs)[source]¶ The
ImpreciseNumber
class ca be used to represent intervals.New in version 0.0.3.
-
classmethod
default_lower
()[source]¶ Get the default lower limit for that class.
Returns: The default lower limit Return type: N
New in version 0.0.3.
-
classmethod
default_upper
()[source]¶ Get the default upper limit for that class.
Returns: The default upper limit Return type: N
New in version 0.0.3.
-
classmethod
convert
(value)[source]¶ Convert the value to the desired type.
Parameters: value – Value to be converted Returns: The default lower limit Return type: N
New in version 0.0.3.
-
__init__
(**kwargs)[source]¶ Initialise an
ImpreciseNumber
.Keyword Arguments: - lower (
N
) – the lower limit of the interval - upper (
N
) – the upper limit of the interval
New in version 0.0.2.
- lower (
-
lower
¶ Get the lower limit.
Returns: the lower limit Return type: N
New in version 0.0.2.
-
upper
¶ Get the upper limit.
Returns: the upper limit Return type: N
New in version 0.0.2.
-
isdisjoint
(other: galactic.type.number.ImpreciseNumber[N]) → bool[source]¶ Return True if the imprecise number has no elements in common with the other. Imprecise numbers are disjoint if and only if their intersection is the empty imprecise number.
Parameters: other ( ImpreciseNumber[N]
) – the other imprecise numberReturns: True if the imprecise number is disjoint from the other Return type: bool
New in version 0.0.2.
-
issubset
(other: galactic.type.number.ImpreciseNumber[N]) → bool[source]¶ Test if the imprecise number is included (or equal) to the other.
Parameters: other ( ImpreciseNumber[N]
) –Returns: True if this imprecise number is included or equal to the other Return type: bool
New in version 0.0.2.
-
issuperset
(other: galactic.type.number.ImpreciseNumber[N]) → bool[source]¶ Test if the imprecise number includes (or is equal to) the other.
Parameters: other ( ImpreciseNumber[N]
) –Returns: True if this imprecise number includes (or is equal to) the other Return type: bool
New in version 0.0.2.
-
union
(*others) → galactic.type.number.ImpreciseNumber[N][source]¶ Compute the union between this imprecise number and the others.
Parameters: *others – Variable length argument list Returns: the union between this imprecise number and the others Return type: ImpreciseNumber[N]
New in version 0.0.2.
-
intersection
(*others) → galactic.type.number.ImpreciseNumber[N][source]¶ Compute the intersection between this imprecise number and the others.
Parameters: *others – Variable length argument list Returns: the intersection between this imprecise number and the others Return type: ImpreciseNumber[N]
New in version 0.0.2.
-
classmethod
-
class
galactic.type.number.
ImpreciseFloat
(**kwargs)[source]¶ ImpreciseFloat
instances represents subset of the real line.New in version 0.0.2.
-
classmethod
convert
(value)[source]¶ Convert the value to a float.
Parameters: value – The value to convert Returns: The value converted Return type: float
New in version 0.0.3.
-
classmethod
-
class
galactic.type.number.
ImpreciseInteger
(**kwargs)[source]¶ ImpreciseInteger
instances represents subset of the integer set.New in version 0.0.3.
-
classmethod
convert
(value)[source]¶ Convert the value to an int.
Parameters: value – The value to convert Returns: The value converted Return type: int
New in version 0.0.3.
-
classmethod