cellsystem.simulation package

Submodules

cellsystem.simulation.cells module

Structures representing the biological entities.

class cellsystem.simulation.cells.Cell(lineage, index)[source]

Bases: object

A single cell.

It acts according to it’s state, and the states of nearby cells and sites.

+ Index

A label that identifies it among others in the same lineage.

+ Father

The index (lineage label) of it’s father.

+ CellLine

The lineage this cell belongs to.

+ Site

The place in the grid this cell inhabits in.

+ Mutations

The mutations in this cell relative to the cell lineage’s reference

add_mutation(position, mutated)[source]

Add a mutation to the cell in the given position of the genome.

Note: The genome may not represent a nucleotide sequence, so these mutations may not represent SNPs.

add_to(site)[source]

Add the cell to the site.

ancestral_genome

Ancestral genome of the cell lineage.

coordinates

Coordinates of the site that the cell inhabits.

genome

Genome of the cell.

It is assembled from the cell’s ancestral genome and it’s mutations.

genome_alphabet

Genome alphabet of the cell line.

The set of characters a genome is composed of. (The genome characters may really represent genes, aminoacids, etc…)

initialize(site=None, father=None, mutations=None)[source]

Initialize grid site, mutations and father.

mutations

Record of the mutations the cell has had.

The mutations are relative to the ancestral genome.

new_daughter()[source]

Initialize a new daughter cell.

Initialize with father and mutation attributes.

process(*args, **kwargs)[source]

Select an action and perform it.

class cellsystem.simulation.cells.CellLine(*args, genome=None, genome_alphabet=None, recycle_dead=True, **kwargs)[source]

Bases: object

Handles the specimens of a specific cell lineage.

A cell lineage is a group of cells that have a common ancestor, we represent the common ancestor by it’s ancestral genome. This structure is in charge of holding this ancestral code and managing the cells creating new cells when needed and cleaning up the dead ones.

Atributes:
  • Ancestral genome: A string-like object.
  • Cells: The cells inherited from this cell line.
  • Alive/Dead cells.
  • Current cell index: Each cell has a unique index. This is the
    index to place in the next cell to be born.
add_behaviors(behaviors, weights=None)[source]

Add the behaviors defining the cells from this cell line.

Params:

behaviors (list of callables):
The list of actions that the cells in this lineage will be able to perform.
weights (optional list of numeric values):
The list of relative weights for selecting each action. The bigger the weight of an action relative to the weights of the others, the more likely is that that action will be selected by the cell at each step. default is all actions have the same weights.
Raises:ValueError – If the weights are not of the same length as the behaviors.
cell_to_recycle()[source]

Return a cell from the dead ones.

fetch_behaviors()[source]

The behaviors that the cells in this lineage perform.

handle_death(dying)[source]

Process a dying cell.

This means removing from the alive cells and adding to the dead ones, maybe to recycle it when another is born.

new_cell()[source]

Get a new blank cell in this lineage and system.

process(*args, **kwargs)[source]

Move a step forward in time.

recycle_cell(cell)[source]

Clear previous information from a cell.

register_log(log)[source]
sample(all=False, n=1)[source]

Take a sample of alive cells.

Parameters:
  • all – If True, return all alive cells, else, return a sample of size n.
  • n – The size of the sample. If 1, return the cell without a container.
total_cells
cellsystem.simulation.cells.behavior(actionname, actionfn=None, probability=None, prepare=True)[source]

Assemble a cell behavior.

Adds logging and asociates a name and a probability function to the raw action function. Allows to specify if the logging of the action requires to prepare the log.

Can be used as a function decorator or as a normal function.

cellsystem.simulation.logging module

cellsystem.simulation.system module

System-related classes.

This module defines a general system that can be used as the base of a computation graph.

A system is composed of entities and interactions btw them, it coordinates all processes.

class cellsystem.simulation.system.Entity[source]

Bases: object

An entity is something that resides in the system.

It processes information according to it’s internal state and the information flowing through the links it shares with other entities.

An entity registers the following methods:
  • process(time)
process(time)[source]
class cellsystem.simulation.system.Interaction(entities, effect)[source]

Bases: object

A structure representing flow of information btw entities.

append(effect)[source]

Effects btw the same entities can be appended and executed in order

process()[source]

Executes the interaction.

class cellsystem.simulation.system.Process(entities, effects)

Bases: tuple

effects

Alias for field number 1

entities

Alias for field number 0

class cellsystem.simulation.system.System(*args, **kwargs)[source]

Bases: object

The global system and event dispatcher.

Aware of the passage of time (steps). A system is composed of entities and interactions between them, at each time step, the system triggers the proceses associated with them.

add_entity(entity, name, procesable=True, inithook=None)[source]

Add an entity to the graph.

If procesable, the entity.process(time) method is called on each time step.

Inithooks are callables called at initialization.

add_interaction_to(container, effect, entitynames)[source]

Add an interaction btw named entities to a dict-like container.

Add an interation btw named entities.

process_interactions_in(interactions)[source]

From the dict-like container of interactions, process items.

run(steps, init=None, after_step=None, before_step=None)[source]

Start running the simulation.

start()[source]

Initialize things before starting simulation.

stateof(entityname)[source]

Ask the entity for it’s state

step()[source]

Take a single step forward in time.

update_hooks(hooktype, newhooks)[source]

Add the given hooks to the system.

cellsystem.simulation.world module

Classes associated with physical space where entities live and interact.

class cellsystem.simulation.world.Site(world, coordinates)[source]

Bases: object

A unit of space.

Cells inhabitate in these spaces and interact with their neighborhood.

Is aware of:
  • World: The world it forms a part of.
  • Coordinates <i,j>: Coordinates in the matrix.
  • Guests: <List>: The guests currently inhabiting this site.
add_guest(guest)[source]

Add the given cell as a new guest to this site.

coordinates

Getter for the site’s coordinates.

guest_count()[source]

Return the number of guests residing in this site.

random_neighbor()[source]

Return a random site in the neighborhood of the current site.

remove_guest(guest)[source]

Remove the given cell as guest for this site.

If the cell is not currently in this site, an error is throwed.

class cellsystem.simulation.world.World(shape=(10, 10), wrap=<function toroidal_wrap>)[source]

Bases: object

The space in which cells inhabit.

It represents physical space and enforces rules and properties like distance and closeness.

A world is aware of:
  • Grid: The sites the action develops in.
  • Neighborhood: How many and which sites may directly influence or
    be influenced by another.
at(coordinates)[source]

Get the site at the specified coordinates.

middle

Get the site at the middle of the world.

random_neighbor_of(site)[source]

Return the relative coordinates of the available neighbors.

Returns a container holding pairs of numbers. the prescence of an item (a,b) means that a site at i,j has a neighbor at (i+a, j+b).

cellsystem.simulation.world.toroidal_wrap(grid, coord)[source]

Return the coordinates wrapped on the grid dimensions.

cellsystem.simulation.world.wrap(n, maxValue)[source]

Auxiliary function to wrap an integer on maxValue.

Examples

>>> # For positives: wrap(n, maxValue) = n % maxValue
>>> [ wrap(i,3) for i in range(9) ]
[0, 1, 2, 0, 1, 2, 0, 1, 2]
>>> # For negatives, the pattern is continued in a natural way
>>> for i in range(-5, 5+1): print(f'{i} : {wrap(i, 3)}')
...
-3 : 0
-2 : 1
-1 : 2
0 : 0
1 : 1
2 : 2

Module contents

class cellsystem.simulation.System(*args, **kwargs)[source]

Bases: object

The global system and event dispatcher.

Aware of the passage of time (steps). A system is composed of entities and interactions between them, at each time step, the system triggers the proceses associated with them.

add_entity(entity, name, procesable=True, inithook=None)[source]

Add an entity to the graph.

If procesable, the entity.process(time) method is called on each time step.

Inithooks are callables called at initialization.

add_interaction_to(container, effect, entitynames)[source]

Add an interaction btw named entities to a dict-like container.

Add an interation btw named entities.

process_interactions_in(interactions)[source]

From the dict-like container of interactions, process items.

run(steps, init=None, after_step=None, before_step=None)[source]

Start running the simulation.

start()[source]

Initialize things before starting simulation.

stateof(entityname)[source]

Ask the entity for it’s state

step()[source]

Take a single step forward in time.

update_hooks(hooktype, newhooks)[source]

Add the given hooks to the system.

class cellsystem.simulation.CellLine(*args, genome=None, genome_alphabet=None, recycle_dead=True, **kwargs)[source]

Bases: object

Handles the specimens of a specific cell lineage.

A cell lineage is a group of cells that have a common ancestor, we represent the common ancestor by it’s ancestral genome. This structure is in charge of holding this ancestral code and managing the cells creating new cells when needed and cleaning up the dead ones.

Atributes:
  • Ancestral genome: A string-like object.
  • Cells: The cells inherited from this cell line.
  • Alive/Dead cells.
  • Current cell index: Each cell has a unique index. This is the
    index to place in the next cell to be born.
add_behaviors(behaviors, weights=None)[source]

Add the behaviors defining the cells from this cell line.

Params:

behaviors (list of callables):
The list of actions that the cells in this lineage will be able to perform.
weights (optional list of numeric values):
The list of relative weights for selecting each action. The bigger the weight of an action relative to the weights of the others, the more likely is that that action will be selected by the cell at each step. default is all actions have the same weights.
Raises:ValueError – If the weights are not of the same length as the behaviors.
cell_to_recycle()[source]

Return a cell from the dead ones.

fetch_behaviors()[source]

The behaviors that the cells in this lineage perform.

handle_death(dying)[source]

Process a dying cell.

This means removing from the alive cells and adding to the dead ones, maybe to recycle it when another is born.

new_cell()[source]

Get a new blank cell in this lineage and system.

process(*args, **kwargs)[source]

Move a step forward in time.

recycle_cell(cell)[source]

Clear previous information from a cell.

register_log(log)[source]
sample(all=False, n=1)[source]

Take a sample of alive cells.

Parameters:
  • all – If True, return all alive cells, else, return a sample of size n.
  • n – The size of the sample. If 1, return the cell without a container.
total_cells
class cellsystem.simulation.Action(action, probability=None, name=None)[source]

Bases: object

Objects of this class represent actions with an associated probability.

try_action(*args, probability=None, **kwargs)[source]

Perform the action according to it’s probability.

class cellsystem.simulation.World(shape=(10, 10), wrap=<function toroidal_wrap>)[source]

Bases: object

The space in which cells inhabit.

It represents physical space and enforces rules and properties like distance and closeness.

A world is aware of:
  • Grid: The sites the action develops in.
  • Neighborhood: How many and which sites may directly influence or
    be influenced by another.
at(coordinates)[source]

Get the site at the specified coordinates.

middle

Get the site at the middle of the world.

random_neighbor_of(site)[source]

Return the relative coordinates of the available neighbors.

Returns a container holding pairs of numbers. the prescence of an item (a,b) means that a site at i,j has a neighbor at (i+a, j+b).

cellsystem.simulation.behavior(actionname, actionfn=None, probability=None, prepare=True)[source]

Assemble a cell behavior.

Adds logging and asociates a name and a probability function to the raw action function. Allows to specify if the logging of the action requires to prepare the log.

Can be used as a function decorator or as a normal function.