Data types related to simulated populations#
Random number generator#
Populations of diploids#
- class fwdpy11.DiploidPopulation(N, length, *, ll_pop=None)#
A population of diploid individuals.
To initialize a population of size N and genome length L:
fwdpy11.DiploidPopulation(N, L)
To initialize a population with two demes of initial sizes N1 and N2 and genome length L:
fwdpy11.DiploidPopulation([N1, N2], L)
Changed in version 0.16.0: Added __copy__ and __deepcopy__. In general, the correct way to copy instances if this class is via
copy.deepcopy()
. Callingcopy.copy()
will copy some of the underlying C++ objects, but the two objects will share the same table collection. This situation of sharing is almost certainly not what one wants.- generation#
- mutations#
- fixations#
- fixation_times#
- haploid_genomes#
- genetic_values#
Return the genetic values as a readonly 2d numpy.ndarray.
Rows are individuals. Columns are genetic values.
- ancient_sample_genetic_values#
Return the genetic values for ancient samples as a readonly 2d numpy.ndarray.
Rows are individuals. Columns are genetic values.
- add_mutation(rng, *, window=None, ndescendants=1, deme=None, data=None)#
Add a new mutation to the population.
New in version 0.16.0.
- Parameters:
rng (fwdpy11.GSLrng) – Random number generator
The following arguments are keyword-only:
- Parameters:
window (tuple[float, float]) – A window [left, right) within which to place the mutation. The default is None, meaning the window is the entire genome.
ndescendants (int) – The number of alive nodes carrying the new mutation. Default is 1, implying that a singleton mutation will be generated.
deme (int) – The deme in which to place the new mutation The default is None, meaning that alive node demes are not considered.
- Returns:
The key of the new mutation. (The index of the variant in
DiploidPopulation.mutations
.)
Implementation details:
A set of nodes are found within window that are ancestral to exactly ndescendants alive nodes.
From this list of candidate nodes, one is chosen randomly to be the node where we place the new mutation.
If the node’s parent’s time is NULL, then the mutations’ origin time is the first 64 bit integer ancestral to the node time, which could be the node time itself. Otherwise, a 64 bit integer is chosen uniformly between the node time and the node’s parent’s time.
If deme is None, any set of ndescendants will be considered.
If deme is >= 0, all ndescendants alive nodes must be from that deme.
If the deme/ndescendants requirements cannot be satisified, the function returns None.
The genetic values of individuals are not updated by this function. Such updates will happen when the population begins evolution forwards in time.
Exceptions:
- Raises:
ValueError – for bad input.
RuntimeError – if this function is applied during a simulation
RuntimeError – if internal checks fail.
- property alive_nodes#
List of alive nodes corresponding to individuals.
New in version 0.5.3.
- property ancient_sample_metadata#
Supports buffer protocol
- property ancient_sample_nodes#
Return an array of nodes associated with preserved/ancient samples.
Alias for
fwdpy11.DiploidPopulation.preserved_nodes
.New in version 0.13.0.
- classmethod create_from_tskit(ts, *, import_mutations=False, import_individuals=False)#
Create a new object from an tskit.TreeSequence
- Parameters:
ts (tskit.TreeSequence) – A tree sequence from tskit
import_mutations (bool) – if True, import mutations and sites from the tree sequence
- Returns:
A population object with an initialized
fwdpy11.TableCollection
- Return type:
Note
In general, initializing a population using the output from a coalescent simulation is a tricky business. There are issues of parameter scaling and the appropriateness of the coalescent model itself. A key issue is that your input tree sequence must have node times in the correct time units! (Generations, for example.)
Warning
Support for importing mutations is currently limited to two cases:
The tree seq has mutations entirely from tskit’s API (see Importing mutations from tskit).
The tree seq has mutations entirely from a previous run of fwdpy11.
New in version 0.2.0.
Changed in version 0.19.8: Correctly handle two cases:
The tree seq has mutations entirely from tskit’s API (see Importing mutations from tskit).
The tree seq has mutations entirely from a previous run of fwdpy11.
- deme_sizes(as_dict=False)#
Return the number of individuals in each deme.
- Parameters:
as_dict – If True, return results as a dict. If False, numpy arrays are returned
The default behavior of this function is equivalent to:
md = numpy.array(self.diploid_metadata, copy=False) return numpy.unique(md['deme'], return_counts=True)
New in version 0.6.0.
- property diploid_metadata#
Supports buffer protocol
- property diploids#
Supports buffer protocol
- dump_tables_to_tskit(*, model_params=None, population_metadata=None, data=None, seed=None, parameters=None, destructive=False)#
Dump the population’s TableCollection into an tskit TreeSequence
- Parameters:
model_params (
fwdpy11.ModelParams
) – Model parameters to be stored as top-level metadatapopulation_metadata (dict) – A mapping from integer id of a deme/population to metadata
data (object) – User-generated data to add to top-level metadata
seed (int) – Random number seed for top-level metadata. If not None, seed must be >= 0.
parameters (None or dict) – The simulation parameters for the provenance table.
destructive (bool) – If True, delete data held by the current instance.
- Return type:
For examples, see Converting data to tskit format.
Warning
If destructive is True, further opertations on the current instance should be considered undefined behavior that may lead to a crash.
Changed in version 0.8.2: Added parameters. Generate provenance information for return value. The provenance information is validated using
tskit.validate_provenance()
, which may raise an exception.Changed in version 0.10.0: Use tskit metadata schema. Mutation time is now stored in the tskit.MutationTable column. Origin time of mutations is part of the metadata.
Changed in version 0.14.0: Added destructive option.
Changed in version 0.15.0: Added model_params, demes_graph, population_metadata, data keyword args.
Changed in version 0.15.2: Fixed bug that could generate a
tskit.PopulationTable
with an incorrect number of rows.Changed in version 0.17.0: The wrapped keyword argument is deprecated.
New in version 0.19.0: Removed deprecated wrapped keyword argument.
New in version 0.23.0: Remove deprecated demes_graph argument and update type hints.
- dump_to_file(filename)#
Write a population to a file in binary format.
- classmethod load_from_file(filename)#
Load population from the output of
fwdpy11.DiploidPopulation.dump_to_file()
.- Parameters:
filename (str) – A file name
- classmethod load_from_pickle_file(filename)#
Read in a pickled population from a file. The file muse have been generated by a call to
fwdpy11.DiploidPopulation.pickle_to_file()
.- Parameters:
f – A handle to a file opened in ‘rb’ mode.
- pickle_to_file(filename)#
Pickle the population to an open file.
This function may be preferred over the direct pickling method because it uses less memory. It is, however, slower.
To read the population back in, you must call
fwdpy11.DiploidPopulation.load_from_pickle_file()
.- Parameters:
f – A handle to an open file
New in version 0.3.0.
- property preserved_nodes#
Return an array of nodes associated with preserved/ancient samples.
- Return type:
New in version 0.13.0.
- sample_timepoints(include_alive=True)#
Return an iterator over all sample time points. The iterator yields time, nodes, and metadata.
- Parameters:
include_alive – If True, include currently-alive individuals.
New in version 0.5.1.
Changed in version 0.5.3: Monkey-patched into pybind11 class
- property tables#
Access the
fwdpy11.TableCollection
Diploid Genotypes#
This class contains two integers pointing to the
genomes making up the individual. The genomes themselves
are represented by fwdpy11.HaploidGenome
.
- class fwdpy11.DiploidGenotype#
Class holding indexes of the gametes/genomes making up an individual
- first#
Key to first gamete. (read-only)
- second#
Key to second gamete. (read-only)
The diploid genotypes are stored in the following container:
- class fwdpy11.DiploidVector#
C++ representation of a list of
fwdpy11.DiploidGenotype
. Typically, access will be read-only.
For all intents and purposes, this container behaves as a standard Python list, but its actual representation is a contiguous array that is handled on the C++ side.
This type supports the Python buffer protocol, meaning that it can be cast to a numpy structured array without a copy, and the attribute names are used as field names.
Diploid metadata#
- class fwdpy11.DiploidMetadata#
Diploid meta data.
- property deme#
Deme.
- property e#
Random component of trait value.
- property g#
Genetic value.
- property geography#
Array containing the geographic location of the individual.
- property label#
Index of the individual in the population.
- property nodes#
Node ids for individual
- property parents#
Array containing the label fields of the parents.
- property sex#
Sex.
- property w#
Fitness.
Metadata are stored in the following container:
- class fwdpy11.DiploidMetadataVector#
Container of diploid metadata.
For all intents and purposes, this container behaves as a standard Python list, but its actual representation is a contiguous array that is handled on the C++ side.
This type supports the Python buffer protocol, meaning that it can be cast to a numpy structured array without a copy, and the attribute names are used as field names.
HaploidGenomes#
Class fwdpy11.HaploidGenome
describes a gamete:
- class fwdpy11.HaploidGenome#
A haploid genome. This object represents the ordered mutations inherited from a single parent.
- n#
Number of occurrences in the population. This has little meaning beyond book-keeping used by the C++ back-end. (read-only)
- mutations#
List of keys to neutral mutations. Contains unsigned 32-bit integers corresponding to mutations in the population. (read-only)
- smutations#
List of keys to selected mutations. Contains unsigned 32-bit integers corresponding to mutations in the population. (read-only)
HaploidGenomes are stored in the following container:
- class fwdpy11.HaploidGenomeVector#
C++ representations of a list of
fwdpy11.HaploidGenome
. Typically, access will be read-only.
For all intents and purposes, this container behaves as a standard Python list, but its actual representation is a contiguous array that is handled on the C++ side.
Mutations#
A mutation is described by fwdpy11.Mutation
:
- class fwdpy11.Mutation#
Mutation with effect size and dominance
The position of a mutation is a floating-point value:
- pos#
Position (float).
For simulations with a single effect size/selection coefficient, the value is a float and held in the field:
- s#
Selection coefficient/effect size. (read-only)
Likewise, the heterozygous effect/dominance of the variant is in the attribute:
- h#
Dominance/effect in heterozygotes. (read-only)
For simulations with multivariate effects, the analogs of s and h are stored as numpy arrays:
- esizes#
Vector of effect sizes.
New in version 0.2.0.
Changed in version 0.4.0: Property is now a readonly numpy.ndarray
- heffects#
Vector of heterozygous effects.
New in version 0.2.0.
Changed in version 0.4.0: Property is now a readonly numpy.ndarray
Other attributes of mutations include:
- g#
Generation when mutation arose (origination time). (read-only)
- label#
A 16-bit unsigned integer that can be used for adding “meta-data” to mutations
Note
The label attribute is assigned when a mutation is generated.
Mutations are stored in the following container:
- class fwdpy11.MutationVector#
C++ representation of a list of
fwdpy11.Mutation
. Typically, access will be read-only.
For all intents and purposes, this container behaves as a standard Python list, but its actual representation is a contiguous array that is handled on the C++ side.
Types related to tree sequence recording#
- class fwdpy11.TableCollection(*args)#
- build_indexes()#
Build edge input/output indexes.
New in version 0.13.0.
- property edges#
Supports buffer protocol
- property genome_length#
Genome length
- property input_left#
Edge input order
- property mutations#
Supports buffer protocol
- property nodes#
Supports buffer protocol
- property output_right#
Edge output order
- property sites#
Supports buffer protocol
- class fwdpy11.EdgeTable#
An EdgeTable is a container of
fwdpy11.Edge
.An EdgeTable supports the Python buffer protocol, allowing data to be viewed in Python as a numpy record array. No copy is made when generating such views.
New in version 0.2.0.
- class fwdpy11.Edge#
An edge in a tree sequence. An edge represents the transmission of the half-open genomic interval [left,right) from parent to child.
New in version 0.2.0.
- parent#
Node id of parent
- child#
Node id of child
- left#
Left edge of interval, inclusive.
- right#
Right edge of interval, exclusive.
- class fwdpy11.NodeTable#
An NodeTable is a container of
fwdpy11.Node
.An NodeTable supports the Python buffer protocol, allowing data to be viewed in Python as a numpy record array. No copy is made when generating such views.
New in version 0.2.0.
- class fwdpy11.Node#
A node in a tree sequence.
New in version 0.2.0.
Changed in version 0.6.0: The population field is renamed deme
- deme#
For models of discrete population structure, this field is the deme of the node.
- time#
Birth time of the node, recorded forwards in time.
- class fwdpy11.MutationTable#
An MutationTable is a container of
fwdpy11.MutationRecord
.An MutationTable supports the Python buffer protocol, allowing data to be viewed in Python as a numpy record array. No copy is made when generating such views.
New in version 0.2.0.
- class fwdpy11.MutationRecord#
A mutation entry in a tree sequence. A MutationRecord stores the node ID of the mutation and the index (“key”) of the mutation in the population.
New in version 0.2.0.
- key#
Index of the mutation in the population
- node#
Node id of the mutation
- site#
Index of the mutation’s site in the population.
New in version 0.5.0.
- derived_state#
The derived state of the mutation.
New in version 0.5.0.
- neutral#
Boolean descriptor of whether or not the mutation affects fitness.
New in version 0.5.0.
- class fwdpy11.SiteTable#
An SiteTable is a container of
fwdpy11.Site
.An SiteTable supports the Python buffer protocol, allowing data to be viewed in Python as a numpy record array. No copy is made when generating such views.
New in version 0.5.0.
- class fwdpy11.Site#
A site in a genome
New in version 0.5.0.
- position#
The position of the site in the genome
- ancestral_state#
The ancestral state of the site
- class fwdpy11.TreeIterator(tables, samples, *, update_samples=False, ancient_samples=None, begin=0.0, end=None)#
Iterate over the marginal trees in a
fwdpy11.TableCollection
Positional arguments for initialization:
- Parameters:
tables (TableCollection) – A table collection
samples (list or numpy.ndarray) – List of sample nodes
The following are keyword only:
- Parameters:
ancient_samples (list or numpy.ndarray) – List of ancient_samples nodes
begin (float) – Start iteration at this position
end (float or None) – End iteration at this position
If end is None, then iteration proceeds over all trees with after positions \(\geq \mathrm{begin}\).
If ancient_samples is not None, then
TreeIterator.preserved_leaf_counts()
will return the number of ancient samples below a node.Changed in version 0.4.1: Add begin, end options as floats for initializing
- leaf_counts(u)#
- property left#
Left coordinate of current tree (inclusive)
- mutations()#
Return iterator over all
fwdpy11.MutationRecord
objects on the current tree.New in version 0.5.1.
- nodes()#
Return the nodes in the current tree.
The return order is preorder.
New in version 0.5.1.
- preserved_leaf_counts(u)#
- property right#
Right coordinate of current tree (exclusive).
- property roots#
Roots of the current tree
- samples()#
- Returns:
The samples list
- Return type:
- samples_below(node, sort=False)#
Return the list of samples descending from a node.
Note
Do not store these sample lists without making a “deep” copy. The internal buffer is re-used.
- sites()#
Return iterator over all
fwdpy11.Site
objects on the current tree.New in version 0.5.1.
- property tables#
The underlying TableCollection
- class fwdpy11.VariantIterator(tables, samples, *, begin=0.0, end=None, include_neutral_variants=True, include_selected_variants=True)#
An iterable class for traversing genotypes in a tree sequence.
To initialize:
- Parameters:
tables (fwdpy11.TableCollection) – The table collection
samples (list or numpy.ndarray) – Samples list
The following are kwarg-only:
- Parameters:
Changed in version 0.4.1: Add begin, end options as floats
Changed in version 0.4.2: Add include_neutral_variants and include_selected_variants
Changed in version 0.5.0: No longer requires a
fwdpy11.MutationVector
.Changed in version 0.14.0: Initialization now requires a TableCollection. Some initialization parmeters are now keyword-only.
- property genotypes#
Genotype array. Index order is same as sample input
- property position#
Current mutation position
- property records#
fwdpy11.MutationRecord
objects for the current Site.
- property site#
The current
fwdpy11.Site
- class fwdpy11.DataMatrixIterator(tables, samples, intervals, neutral, selected, fixations=False)#
Efficient iteration over genomic windows.
This class allows efficient traversal across multiple genomic intervals. The class is iterable, and encapsulates the data fields of
fwdpy11.DataMatrix
, which are accessible as properties.To initialize:
- Parameters:
tables (
fwdpy11.TableCollection
) – A table collectionsamples (list or numpy.ndarray) – A list of samples
intervals (list[tuple]) – The \([start, stop)\) positions of each interval
neutral (bool) – If True, include neutral variants
selected (bool) – If True, include selected variants
fixations (bool) – (False) If True, include fixations in the sample
New in version 0.4.4.
Changed in version 0.5.0: Initialization longer requires
fwdpy11.MutationVector
The class constructor is:
- __init__ = <function DataMatrixIterator.__init__>#
The following properties are numpy arrays providing read-only access to an internal instance of
fwdpy11.DataMatrix
:- neutral#
Genotypes at neutral variants
- selected#
Genotypes at selected variants.
- neutral_keys#
Indexes of the neutral variants.
- selected_keys#
Indexes of selected variants.
- neutral_positions#
Positions of neutral variants.
- selected_positions#
Positions of selected variants.
Types related to discrete demographic events#
- class fwdpy11.ForwardTimeInterval(start_time, end_time)#
A half-open interval [start_time, end_time). The times represent values moving forward in time, in generations since the begining of a model.
- class fwdpy11.ForwardDemesGraph(yaml, burnin, burnin_is_exact, round_non_integer_sizes)#
A forward-in-time representation of a demes.Graph
- Parameters:
yaml (str) – The string representation of a demes model.
burnin (int) – The number of generations of evolution to occur before the events in the yaml take place.
burnin_is_exact (bool) – If False, burnin will be treated as an integer multiple of the sum of ancestor population sizes. If True, burnin will be used as-is.
round_non_integer_sizes (bool) – If True, a graph containing non-integer values for epoch start and/or end sizes will have those values rounded to the nearest integer.
The recommended method for construction is
ForwardDemesGraph.from_demes()
orForwardDemesGraph.tubes()
.New in version 0.20.0.
- asdict()#
Return dict representation
- property burnin_duration#
The length of the burning time. Equal to burnin_generation + 1.
- property deme_labels#
A dictionary mapping integer deme IDs to deme names (strings).
- deme_time_intervals()#
- Returns:
a list of the time intervals during which each deme exists.
- Return type:
The order of the demes is the same as in the input
demes.Graph
.
- property demes_at_final_generation#
List of integer ids of extant demes in the final generation of the demes graph.
- property demes_graph#
Obtain the internal representation of the
demes.Graph
This object is not necessarily equivalent to the input graph:
It is fully resolved.
It has been rounded to discrete time and deme sizes.
- epoch_time_intervals()#
- Returns:
a list of the time intervals during which each epoch of each deme exists.
- Return type:
The order of the demes is the same as in the input
demes.Graph
. Within each deme, the epoch order is the same as in the input graph.
- property final_generation#
This is the final offspring generation of the model. In other words, a model evolved from parental generation zero until time zero of the graph (backwards in time) will result in individuals whose birth times equal the value of this property.
- classmethod from_demes(model, burnin, *, burnin_is_exact=None, round_non_integer_sizes=None)#
Build from a demes graph.
- Parameters:
model (str or demes.Graph) – A demes model.
burnin (int) – The number of generations of evolution to occur before the events in the yaml take place.
burnin_is_exact (bool) – If False, burnin will be treated as an integer multiple of the sum of ancestor population sizes. If True, burnin will be used as-is.
- Returns:
A forward-time representation of a demes graph
- Return type:
- classmethod fromdict(d)#
Build an instance from a dictionary
- property initial_sizes#
A list of the nonzero (parental) deme sizes at time 0 of the model (thinking forwards in time).
Due to the structure of a demes graph, the indexes of the list are the deme integer identifiers.
This property is useful for setting up a
fwdpy11.DiploidPopulation
.
- property model_duration#
The duration of the model from time 0 until the most recent time point.
- to_backwards_time(forwards_time)#
Convert an integer value representing time moving in the forward direction to time in the past (“backwards time”).
If no conversion is possible, None is returned.
- to_forwards_time(backwards_time)#
Convert an integer value representing time moving in the backward direction to time in the forward direction.
If no conversion is possible, None is returned.
- classmethod tubes(sizes, burnin, *, burnin_is_exact=None, round_non_integer_sizes=None)#
Build a demographic model for one or more demes of constant size.
- Parameters:
- Returns:
A forward-time representation of a demes graph
- Return type:
- class fwdpy11.DemographyDebugger(initial_deme_sizes, events, simlen=None, deme_labels=None)#
Efficiently debug demographic events.
The class executes a mock simulation of demographic events in order to quickly detect errors. It also generates a “report” of the model details for double-checking.
If model errors are found, a
ValueError
exception is raised. The goal is to catch the types of errors that would result in an exception during a simulation.The class is initialized with the following arguments, which may be either positional or keyword:
- Parameters:
initial_deme_sizes (list or fwdpy11.DiploidPopulation) – The initial sizes of each deme
events (fwdpy11.ForwardDemesGraph or fwdpy11.DemographicModelDetails) – The demographic model
simlen (int) – The length of the simulation. Defaults to None.
deme_labels (dict) – A map from deme index to a printable name
New in version 0.6.0.
Changed in version 0.8.1: Initialization now done with attr. A list of initial deme sizes is now accepted.
- class fwdpy11.DemographicModelDetails(*args, **kwargs)#
Stores rich information about a demographic model. Instances of this class get returned by functions generating pre-calculated models.
This class has the following attributes, whose names are also
kwargs
for intitialization. The attribute names also determine the order of positional arguments:- Parameters:
model (fwdpy11.ForwardDemesGraph) – The demographic model parameters
name (str) – The name of the model
source (dict) – The source of the model
parameters (object) – The parameters used to generate
model
citation (dict or fwdpy11.DemographicModelCitation or None) – Citation information
metadata (object) – Optional field for additional info
New in version 0.8.0.
- asblack()#
Return a string representation formatted with black
- asdict()#
Return dict representation
- property deme_labels#
A dictionary mapping integer deme IDs to deme names (strings).
- classmethod fromdict(d)#
Build an instance from a dictionary
- property initial_sizes_list#
A list of the nonzero (parental) deme sizes at time 0 of the model (thinking forwards in time).
This property is useful for setting up a
fwdpy11.DiploidPopulation
.
- property total_simulation_length#
The total number of generations in the model.
This value includes any burn-in time and starts from time 0 (forwards in time).
- class fwdpy11.DemographicModelCitation(*args, **kwargs)#
Citation information for a demographic model
This class has the following attributes, whose names are also
kwargs
for intitialization. The attribute names also determine the order of positional arguments:- Parameters:
DOI – The Digital Object Identifier
full_citation – Something string-like giving the full citation.
metadata – Any additional information that may be needed.
New in version 0.8.0.
Miscellaneous types#
- class fwdpy11.NewMutationData(*, effect_size, dominance, esizes=None, heffects=None, label=0)#
Data object for
fwdpy11.DiploidPopulation.add_mutation()
New in version 0.16.0.
This class has the following attributes, whose names are also kwargs for intitialization. The attribute names also determine the order of positional arguments:
- Parameters:
effect_size (float) – The fixed/univariate effect size. Will fill in
fwdpy11.Mutation.s
dominance (float) – Heterozygous effect of the mutation. Will fill in
fwdpy11.Mutation.h
esizes (list[float]) – Data to fill
fwdpy11.Mutation.esizes
. Default is None.heffects (list[float]) – Data to fill
fwdpy11.Mutation.heffects
. Default is None.label (int) – Data for
fwdpy11.Mutation.label
. Default is 0.