Population

The galactic.population module.

It contains several classes for creating populations:

  • DataReader class is used to read a population from a file;

  • Population class is used to represent collections of individuals;

class DataReader

Data reader.

abstract classmethod extensions() Iterator[str]

Get an iterator over the supported extensions.

Returns:

An iterator over the supported extensions

Return type:

Iterator[str]

abstract classmethod read(data_file: TextIO) Iterable[Any]

Read a data file.

Keyword Arguments:

data_file (FileIO) – A readable file.

Returns:

An iterator over the (identifier, data) couple.

Return type:

Iterator[Tuple[str, Any]]

class Population(individuals: Iterable[Any])

The Population class is used to handle collection of individuals.

It implements the Mapping abstract class from identifiers (strings) to objects.

Example

>>> from galactic.population import Population
>>> data = {"i1": {"x": 1}, "i2": {}, "i3": {"x": 1}}
>>> population = Population(data)
>>> len(population)
3
>>> list(population)
['i1', 'i2', 'i3']
>>> "i1" in population
True
>>> population["i1"]
{'x': 1}
>>> list(population.keys())
['i1', 'i2', 'i3']
>>> list(population.values())
[{'x': 1}, {}, {'x': 1}]

Example

>>> from galactic.population import Population
>>> data = [{"x": 1}, {}, {"x": 1}]
>>> population = Population(data)
>>> len(population)
3
>>> list(population)
['0', '1', '2']
>>> "0" in population
True
>>> population['0']
{'x': 1}
>>> list(population.keys())
['0', '1', '2']
>>> list(population.values())
[{'x': 1}, {}, {'x': 1}]
__init__(individuals: Iterable[Any])

Initialise a Population instance.

If the individuals argument is a mapping, their identifiers will be the string conversion of their keys.

If the individuals argument is an collection, their identifiers will be the string conversion of their positions.

Parameters:

individuals (Union[Mapping, Iterable]) – An iterable of individuals.

Raises:

TypeError – If the individuals argument is not an instance of Union[Mapping, Iterable].

classmethod from_file(stream: TextIO) galactic.population.Population

Create a population from a readable opened file.

Keyword Arguments:

stream (TextIO) – File to read

Returns:

A new population

Return type:

Population

get(k[, d]) D[k] if k in D, else d.  d defaults to None.
items() a set-like object providing a view on D's items
keys() a set-like object providing a view on D's keys
classmethod readers() Iterator[galactic.population.DataReader]

Get all the registered data reader plugins.

Returns:

An iterator on registered data reader plugins.

Return type:

Iterator[DataReader]

classmethod register_reader(reader: galactic.population.DataReader) None

Register a new reader.

Parameters:

reader (DataReader) – The reader

values() an object providing a view on D's values