Context

The galactic.context package defines generic classes for using contexts:

  • Context: a context is composed by a population and a model
  • Population a population is a container for individuals
  • Model a model is a container for attributes
  • Individual an individual has an identifier and values
  • Attribute an attribute has a name and a type
class galactic.context.C

Generic subclass of the Context class

class galactic.context.P

Generic subclass of the Population class

class galactic.context.M

Generic subclass of the Model class

class galactic.context.X

Generic subclass of the Individual class

class galactic.context.A

Generic subclass of the Attribute 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 context model 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 and population are not empty) using the python builtin bool() 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.

population

Get the population for this context.

Returns:the underlying population
Return type:P

New in version 0.0.1.

model

Get the underlying model.

Returns:the underlying model
Return type:M

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.

context

Get the underlying context.

Returns:the underlying context
Return type:C

New in version 0.0.1.

model

Get the underlying model.

Returns:the underlying model
Return type:M

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.

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.

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.

model

Get the underlying model.

Returns:the underlying model
Return type:M

New in version 0.0.1.

value(attribute: A)[source]

Get the attribute value for this individual.

Parameters:attribute (A) – the attribute
Returns: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 a name and a type.

It’s possible to access to the attribute identifier and type 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.

name

Get the attribute name.

Returns:the attribute name
Return type:str

New in version 0.0.1.

type

Get the attribute type.

Returns:the attribute type
Return type:type

New in version 0.0.1.

model

Get the underlying model.

Returns:the underlying model
Return type:M

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 individual
Returns:the value
Return type:object
Raises:ValueError – if the individual does not belong to the underlying population.

New in version 0.0.1.