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:
- 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.key(data["i1"]) 'i1' >>> 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.key(data[0]) '0' >>> 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.
- Keyword Arguments:
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:
- get(k[, d]) D[k] if k in D, else d. d defaults to None.
- index(individual: Any) int
Get the index of an individual.
- Parameters:
individual – The individual whose index is requested.
- Returns:
- Return type:
The index of the individual.
- Raises:
ValueError – If the individual does not belong to the population.
- items() a set-like object providing a view on D's items
- key(individual: Any) str
Get the identifier of an individual.
- Parameters:
individual – The individual whose identifier is requested.
- Returns:
- Return type:
The identifier of the individual.
- Raises:
ValueError – If the individual does not belong to the population.
- 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:
- 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