A Population Simulation System Written in Python

peanutunderwearΛογισμικό & κατασκευή λογ/κού

7 Νοε 2013 (πριν από 3 χρόνια και 11 μήνες)

100 εμφανίσεις

Presented at Python Community Conference (PyCon) Washington, DC 28 March 2003 P.
1
/
20

A Population Simulation System Written in Python


Tom Olivier


Green Creek Paradigms, LLC

Schuyler, VA

tolivier@cstone.net



Introduction


This paper describes a population simulation system being developed in
Python. This system is
intended as a tool for biological researchers and conservationists who seek to model a range of
demographic, genetic and other processes in animal populations of several hundred to a few
thousand individuals. A goal of this system
is modeling of populations subdivided by social
groupings, space or landscape features into subpopulations. These subpopulations may be
unstructured internally or may consist of social groups with further internal subdivisions. The
system includes detail
ed portrayal of life histories of individuals and their kinship relationships.


To be useful to biologists working with a range of real populations, a system of this type must be
capable of modeling variations of important population processes. For example
, it should treat
animal survival processes that are population density dependent and independent. The system
should be configurable for different forms of gene transmission (for example, diploid inheritance
common in mammals and haplodiploid inheritance

seen in some social insects).


The intent of the author is to provide a modeling system of Python classes and modules that can be
customized with modest programming effort to meet many of the preceding biological modeling
challenges. My goal in this pa
per is to describe the design, implementation and experience with
operation of the system to date.


Motivation


Mathematical models have played an important role in many areas of population biology, such as
genetics and demography, throughout the twentieth

century. With the development of digital
computers, computer simulations appeared as an alternative to mathematical analysis. However, for
decades after they first appeared, many scientists viewed computer simulations as a tool to be used
only when mat
hematical treatments were not possible.


This attitude began to change around 1980, driven by several factors. First, empirical researchers in
biology were finding that real populations often exhibited complex structures and processes that
could not be r
epresented adequately in rigorous mathematical models. For example, classical
mathematical population genetic models often assumed for analytical convenience that regional
populations were subdivided into permanent local subpopulations of fixed size, wit
h essentially no
significant internal structure (Crow and Kimura, 1970). However, field biologists often found
populations of animals, such as Old World monkeys, divided into local groups that were both highly
complex and dynamic. In contrast to mathemat
ical models, data structures native to computers
Presented at Python Community Conference (PyCon) Washington, DC 28 March 2003 P.
2
/
20

conveniently represent many of the complexities observed in real populations. For example,
syntactically organized character strings, can represent complex, dynamic social group structures
found in baboons,

rhesus monkeys and other primate populations (Olivier, 1985). Finally, cheap,
fast, microcomputers have become available, making simulation more accessible as a routine
modeling tool for biologists.


For many biological modeling problems, computer simu
lations now seem a natural choice. This
trend perhaps has come to a head with Stephen Wolfram’s text,
A New Kind of Science
, in which
he argues that digital simulations are broadly preferable to analytical mathematical models
(Wolfram, 2002).


Predecess
ors of the system described in this paper were written during the 1980’s in BASIC, Forth
and C. Object oriented methods available today in Python and many other languages offer broadly
different options for representing simulated populations than did the
procedural languages of the
past. Scripting languages offer data type flexibility not easily available in compiled languages.
However, flexibilities like this often come with new prices and pitfalls.


The system described here now consists of over 20 Pyt
hon modules and 5,000 lines of code.
During development, it has been used to construct models of two different kinds of populations


a
simply subdivided hypothetical mammalian population and a monkey population with complex
local group organization. Mor
e testing, some refactoring and extensions remain to be done, but the
system is at a point that differences between building a modeling system of this type in Python and
many other languages are plainly evident.



Design and Implementation



In this syste
m, keyed character strings (also referred to as key strings), equivalent graphs, or
indented text structures provide data structures for representing subpopulations. Keyed strings also
facilitate syntactic analysis of correctness of subpopulation structur
es generated in simulations.
Alternately, structures portrayed in keyed strings can be represented in graphs implemented in
dictionaries in Python. These usually are easier to read by humans and are used as principal storage
medium for keyed structures i
n the current system. Indented text can be used to represent
population structure as well. This form of keyed structure information seems most easily read by
humans and routinely is used for input and output of subpopulation structure information. In th
is
system, each element of a keyed structure is given a unique tuple identifier. The first element of the
tuple is a text string that identifies the element type. The second is a decimal integer that identifies
the specific instance of that type. See Ol
ivier (1985) and Appendix A for further discussion and
examples of keyed character strings, graphs and indented text structures.


The system targets creation of models that run as unattended batch applications. Text file input and
output are used to the e
xtent practical. Standard files supported by the system include input of
initial simulation state, output of terminal simulation state, console messages regarding run
progress, a temporal simulation event log and an error log that captures and reports on
unhandled
exceptions that arise in the course of a simulation. A design guideline stipulates that system classes
Presented at Python Community Conference (PyCon) Washington, DC 28 March 2003 P.
3
/
20

provide input and output methods sufficient for their use. Text tags identify and format data input
and output, improving human readability a
nd facilitating identification of ill
-
formed input.


The system includes some utility modules. One module, texio, supports tagged input of data in
various formats including lists and dictionaries. Another, randy, wraps the rv random number
generator
library (Frohne, 1998). This allows simple generation of random numbers fitting many
widely used probability distributions within simulations.


A single execution of a simulation application steps at least one population through a series of
discrete time
periods, with births, deaths, migrations and other biological events potentially taking
place in each time period. Each population contains subpopulations, representing occupants of a
different spatial neighborhoods or social groups. Attributes of simu
lated individuals are read from
the input file initially or generated in the simulation when new individuals are born. Individual
attributes are deleted at simulated death. A kinshipserver class instance in each population stores
parentage records of ind
ividuals in a dictionary. The system retains kinship links after death of
simulated individuals for analyses of familial relationships. Such relationships can affect the course
of processes in the model and are of direct interest to geneticists. In one
of the examples considered
in this paper, the rsb model, family relationships traced by female descent are particularly
important. This model uses an instance of class uniline, derived from class kinshipserver, to
facilitate identifying and utilizing info
rmation on female familial relationships.


A hierarchy of class objects embedded within other objects is used to provide a basic framework in
the modeling system. A simulation class provides an envelope in which other objects exist. The
simulation class
opens and closes input files, manages the procession of simulated time periods,
provides random number generation and captures and logs exceptions not dealt with at lower levels.
A simulation normally contains a population class instance and might contain

more than one
population.


Embedded within each population instance are objects that manage many kinds of information
including individual attributes, familial relationships, fertility and mortality rates. A template server
instance in each population
reads the keyed string template for allowable subpopulation structures
from the simulation initialization file. The template server automatically creates a parser that allows
it to check the structures of strings submitted to it at run time for consistenc
y with the template.
Methods present in the system allow simple translation of subpopulation keyed graphs to keyed
strings. These keyed strings then can be forwarded to the template server for syntactic checking
after modeling of biological events that c
ould disrupt subpopulation structures. Discrepancies
between template and keyed string structures raise exceptions that include detailed information
regarding the discrepancy. Such exceptions can be used to trigger corrective restructurings of the
subpopu
lation organizations or may be allowed to go unhandled and trigger stoppage of simulations
by the simulation objects. A dictionary embedded in each population instance stores subpopulation
objects. Each subpopulation object contains a keyed graph object
that stores its composition and
organization in a keyed structure graph. An identification number generator, an instance of class
idnserver, is created and initialized within each population instance at run time. It generates unique
id numbers for new su
bpopulations, group elements and individuals that do not clash with numbers
identifying elements of same types entered into the simulation from the initialization file.


Presented at Python Community Conference (PyCon) Washington, DC 28 March 2003 P.
4
/
20

At present, modules providing classes that model diploid genetic inheritance and simpl
e infectious
disease transmission exist and can be included in a population should modeling of these kinds of
events be desired. The system is designed to support simultaneous modeling of populations of
different, potentially interacting species populati
ons within a single simulation. Multi
-
species
population models allow studies of ecological competition, predator
-
prey interactions and parasite
-
host relationships.


Class inheritance is used to specialize objects to the requirements of models of populati
ons of
particular species. Classes base_popframe and base_subpopframe provide some basic facilities for
input, output, information management and key structure verification. Base classes provide no
modeling of biological processes. Classes mammal_popfra
me (derived from base_popframe) and
mammal_subpopframe (derived from base_subpopframe) add functionality to assist modeling of
age
-
specific survival and reproduction widely seen in mammals, as well as emigration and
immigration processes approximately like

those seen in some mammalian populations. Module
mammal_demes includes population and subpopulation classes, derived from their mammalian
framework counterparts, that complete the functionality required for modeling of a simply
subdivided hypothetical ma
mmalian population. Module rsb also provides population and
subpopulation classes derived from the mammalian framework classes. However, the rsb classes
provide the more elaborate capabilities needed to model rhesus monkey or savanna baboon
populations.

One can imagine different sequences of derivations from base population and
subpopulation frameworks to model populations of other kinds of organisms. For example, a bees
and wasp framework could be developed to provide features matching observations on
these
biologically interesting and important animals.


One aim of the system is to provide means of representing spatial effects in population processes.
At present, the system deals with subdivision but provides no support for spatial distance effects.

A
modest treatment of distance effects could be obtained by adding geographic coordinate attributes to
each subpopulation, permitting calculation of distances between subpopulations and modeling of
the influence of distance on processes such as migration
of individuals among subpopulations.
Simple network structures attached to each subpopulation also could be used to reflect
connectedness between subpopulations as affected by location.


Ideally, one would like to represent subpopulation locations in spa
ce along with local landscape
variations in altitude, vegetation, water resources and other environmental features known to
influence organisms. One would like to incorporate into simulations the interactions between the
landscape states and population p
rocesses and states. Modeling such landscape and population
interactions would seem particularly valuable to conservation
-
oriented land use planning and
management.


Geographic information systems (GIS) provide many of the desired landscape portrayal a
nd
analysis capabilities. As a means of exploiting the ability of GIS in this simulation system, I have
constructed a Python interface to the Idrisi GIS (Clark Labs, 2001). This package provides
extensive facilities for visualization and analysis of man
y forms of landscape data. Idrisi supports
the Microsoft Component Object Model (COM) interface (Rogerson, 1997). I constructed a class
with a Python interface to Idrisi using the win32com package (Stein and Hammond, 2001). This
interface now permits ca
lling of IDRISI functions from Python programs. However, a derived
Presented at Python Community Conference (PyCon) Washington, DC 28 March 2003 P.
5
/
20

interface containing methods particularly adapted to the needs of population simulation remains to
be developed.


To date, the system has been developed on a Microsoft Windows 2000 platfor
m. One goal of
development is to produce a system that for the most part also will run under the Linux operating
system. While most modules are expected to port in straightforward fashion, some, such as the
interface to Idrisi (a Windows program), are li
kely to remain platform specific.


Examples


Appendices present code samples plus initial and terminal output from simulations of two different
kinds of populations. Appendix B discusses a simply subdivided hypothetical mammalian
population, mammal_demes.

The mammal_demes listing shows modeling in each time period of
birth, death, ageing and migration between two subpopulations. Sections from the input and output
files illustrate use of tags to format files, use of indented text to represent subpopulatio
n
compositions, entries for values of parameters that control population process, attributes of
simulated individuals and their familial links. A close comparison of the input and output contents
shows that in the single simulated time period, some indivi
duals have appeared through births, some
have disappeared through death and some have moved between groups.


Samples from a second model,rsb, are presented in Appendix C. The rsb model simulates
populations of rhesus monkeys or savanna baboons. Much lik
e the mammal_demes model, births,
deaths, migration and ageing occur in the rsb model. However, unlike the simpler mammal_demes
model, rsb subpopulations are social groups that are further subdivided internally into a segment of
immigrant adult males that
relate in a dominance hierarchy and a natal segment composed of
matrilineally related females born in the group and their immature offspring. Groups may fission
when large (largely along matrilineal lines) or fuse together if they become small. Matrilin
es also
may fission if they become large. A comparison of rsb inputs and outputs illustrates the more
complex group structures, more process parameters and changes in groups present as well as
individuals present. Appendix A provides details on rsb subp
opulation features and their
representation in keyed structures.


Python as a Language Choice


So far, Python appears well suited for implementation of a system of this type. Object oriented
programming, with its objects, attributes and methods, seems bet
ter suited than strictly procedural
languages to modeling biological entities that contain information, are related in complex ways and
engage in a variety of behaviors. Lists and dictionaries, with their automatic garbage collecting and
flexible typing,
make quick work of tasks that take far longer to accomplish in low
-
level languages.
The high logical level of Python language features means that fewer steps are required to map the
concepts in a biologist’s mind into an implemented simulation. Additiona
lly, the code for such a
simulation is more likely to be intelligible to scientists who are occasional programmers. Use of an
interpreted language must come with a price regarding execution speed. However, so far execution
speed appears sufficient.


Presented at Python Community Conference (PyCon) Washington, DC 28 March 2003 P.
6
/
20

Next

Steps


Additional testing, extensions and population modeling applications are planned in the coming
months. Development of a geographic information system interface class with methods specifically
useful for population simulations is a high priority. I

intend to release the system with
documentation under an open source license by the end of 2003.



References


Clark Labs. Idrisi32. Vers. I32.21. Worcester, 2001.


Crow, J. and M. Kimura. An Introduction to Population Genetics Theory. Harper and Row:

New
York, 1970.


Frohne, I. rv. Vers. 1.1 Nov. 1998. <
http://www.frohne.westhost.com/
>.


Olivier, T.J. “Use of Keyed Character String Data Structures and Operators in Models of Primate
Groups,” J. Theor.

Biol. 115, 539
-
549, 1985.



Rogerson, D.
Inside COM. Microsoft Press: Redmond
, 1997.


Stein, G. and M. Hammond. Win32com. Ver 145. 2001.
<http://starship.python.net/crew/mhammond/>.


Wolfram, S.
A New Kind of Science. Wolfram Media: Champaign, 2002.



Ap
pendix A.


Use of Templates, Keyed Strings, Graphs and Indented Text

To Represent Composition and Structure in Subpopulations



Keyed strings flexibly represent simple subpopulation structures and complex subpopulation
structures. Keyed strings begin with

a text label of letters and perhaps digits, followed by an open
parenthesis, then some content and then a terminating closed parenthesis. The text label specifies
the type of population entity represented by the keyed string (a subpopulation or group or a

biologically recognizable subset of a subpopulation or an individual animal). Keyed strings may
contain other keyed strings in their content. Thus, nested, inverted tree structures can be
represented in the linear sequence of characters in a keyed string
. In the keyed strings used in this
simulation system, an identifying number unique in the system for elements of that label type
occurs immediately after the opening parenthesis of each keyed string. Thus, in this system each
keyed string is uniquely id
entified by a tuple composed of its text type label and an identification
number.


Presented at Python Community Conference (PyCon) Washington, DC 28 March 2003 P.
7
/
20

Subpopulation structures vary greatly between different animal species. Subpopulations in some
species may be simple while others may exist in social groups with different

kinds of recognizable
subgroups. These complex social structures are not static but are conserved in the face of many
changes, such as births, deaths and movements of individuals. One of the challenges in modeling
complex, dynamic populations is develop
ing models of population events that capture both the
dynamism of the social groups and conservation of their organization. To this end, the system
provides means of comparing subpopulation structures against templates that specify subpopulation
structure
s anticipated in simulations. These templates are similar to, but not identical to, keyed
strings. They play a role similar to the patterns used in text search processes. Templates are
provided by modelers and read at the start of simulations for recurre
nt testing of subpopulation
structures as simulations proceed.


A template for a subpopulation (or group) with no internal structure is presented below:



G(+,A(+)$)


The preceding template is used for the mammal_demes simulation. The letter “G” is us
ed here as
label for the outer key element, in this case a subpopulation in the simulation system. One could use
the letter “D”, as in demes, or the whole word “Deme” instead of “G” as key element type label if
one wished. The letter “A” is used in this t
emplate to label the keyed string of an individual animal.
By rule, individual keyed strings may only contain a decimal integer identifying number. The “+”
character in a template indicates a decimal number should occur. The “$” character in a template

indicates that one or more occurrences of the preceding type of keyed string must occur in a well
-
formed example of the subpopulation.



Here is an example of a keyed string conforming to the mammal deme template:




G(101,A(123)A(456)A(678)A(910)A(11
12))


A template for an rsb group or subpopulation follows:



G(+,H(+,A(+)$)N(+,M(+,A(+)$)$))


Subpopulations in this model, identified by the key label G, are further divided into H elements
containing adult males that migrate between groups from time t
o time and a natal (N) element
containing animals born in the group. In these monkey populations, females remain for life in the
group in which they are born (or a derivative of it). The natal element is further divided into
matriline (M) elements, which

are multigenerational families of females related by female descent
and their immature offspring. The letter “A” again is used to label individuals.


This rsb template can be displayed using blanks and line breaks to make clearer its internal
structure:



G(+,


H(+,


A(+)$


)


N(+,


M(+,

Presented at Python Community Conference (PyCon) Washington, DC 28 March 2003 P.
8
/
20


A(+)$


)$


)


)


A keyed string representation of an rsb group follows:




G(2,H(2,A(630)A(634)A(635)A(683)A(681)A(639)A(638)A(641)A(688)A(693)


A(691)A(
696)A(695)A(702)A(703)A(704)A(707)A(709)A(710))N(2,M(21,A(626)


A(740)A(739)A(632)A(636)A(643)A(646)A(649)A(667)A(694)A(697))M(22,A(629)


A(747)A(701)A(714)A(721)A(743)A(749)A(751)A(757))))




Alternately, the information in a keyed string can be st
ored in a graph implemented via a Python
dictionary:



Key Value




('G', 2), [('H', 2), ('N', 2)]


('H', 2), [('A', 630), ('A', 634), ('A', 635), ('A', 683), ('A', 681),


('A', 639), ('A', 638), ('A', 641), ('A', 688), ('A', 6
93),


('A', 691), ('A', 696), ('A', 695), ('A', 702), ('A', 703),


('A', 704), ('A', 707), ('A', 709), ('A', 710)]


('N', 2), [('M', 21), ('M', 22)]


('M', 21), [('A', 626), ('A', 740), ('A', 739), ('A', 632), ('A', 636),



('A', 643), ('A', 646), ('A', 649), ('A', 667), ('A', 694),


('A', 697)]


('M', 22), [('A', 629), ('A', 747), ('A', 701), ('A', 714), ('A', 721),


('A', 743), ('A', 749), ('A', 751), ('A', 757)]



Indented t
ext also can represent the information in a keyed string. Indentation is used to reflect
nesting, as seen in the following portrayal of an rsb group:



G,2


H,2


A,630 A,634 A,635 A,683 A,681 A,639 A,638 A,641


A,688 A,693 A,691 A,696 A,
695 A,702 A,703 A,704


A,707 A,709 A,710


N,2


M,21


A,626 A,740 A,739 A,632 A,636 A,643 A,646 A,649


A,667 A,694 A,697


M,22


A,629 A,747 A,701 A,714 A,721 A,743 A,749 A,751


A,757


In the rsb model, l
arge groups may divide or fission into two groups. In such fissions, the adult
male element fissions essentially randomly, while the natal element fissions along matrilineal lines.
The groups produced by such fissions should conform to the rsb template.

Here is an example of
groups that might be generated in an rsb simulation from fission of the above example.



G,2

Presented at Python Community Conference (PyCon) Washington, DC 28 March 2003 P.
9
/
20


H,2


A,630 A,683 A,639 A,638 A,688 A,696 A,695 A,703 A,710


N,2


M,22


A,629 A,747 A,701 A,714 A,721 A,743 A,
749 A,751


A,757



G,3


H,3


A,634 A,635 A,681 A,641 A,693 A,691 A,702 A,704


A,707 A,709


N,3


M,21


A,626 A,740 A,739 A,632 A,636 A,643 A,646 A,649


A,667 A,694 A,697






Appendix B.


Code, Input an
d Output Samples from Mammal Demes Simulation



1 ) main mammal_demes code control sequence



sim = mammal_demes_simulation ()



try:


sim.initialize_io()


sim.start_reading_gin()


randy.rndgen.read_initial_random_conditions( sim.simgin

)


sim.pop.read_population( sim.simgin )


sim.pop.set_idn_server_types()


sim.finish_reading_gin()


sim.pop.initialize_id_processing ()






sim.line_to_console()


sim.start_simulation_console_messages()


si
m.start_simulation_tym_messages()


sim.start_daytime_tym_message()





while sim.currtime <= sim.timelimit:


sim.line_to_console()


sim.start_time_period_console_message()


sim.start_time_period_tym_message()


si
m.pop.population_survival()


sim.pop.population_ageing()


sim.pop.population_reproduction( sim.pop.min_adult_male_age,
\


sim.pop.fraction_male_births, sim.pop.initial_age_class,
\


si
m.pop.individual_label)


sim.pop.population_migration()

Presented at Python Community Conference (PyCon) Washington, DC 28 March 2003 P.
10
/
20


sim.finish_time_period_tym_message()


sim.end_time_period_console_message()


sim.pop.template_check_subpopulations()




sim.currtime = sim.currtime +
1




sim.finish_daytime_tym_message()


sim.finish_simulation_tym_messages()


sim.end_simulation_console_messages()


sim.line_to_console


sim.initialize_fin()


sim.start_writing_fin()


randy.rndgen.write_random
_conditions
\


( sim.simpathnm + ".ro", sim.simfin )


sim.pop.write_population( sim.simfin )


sim.finish_writing_fin()





except Exception:




sim.initialize_err()


sim.start_writing_err()


sim.write_exception_to_err()


sim.finish_writing_err()


sim.simerr.close()


raise



2) Extract of mammal_demes simulation initialization file


[simulation_start]

[run_name:] Pycon Mammal Demes Simulation

[current_time:] 20

[time_limit:] 20

[random_initialization_seed:]

56789

[population_start]

[population_name:] Pycon Mammal Demes Population

[initial_age_class:] 1

[age_classes:] 5

[life_table_start]


f, fertility, 0.0, 1.2, 1.3, 0.9, 0.7


f, survival, 0.9, 0.8, 0.75, 0.7, 0.0


m, survival, 0.9, 0.8, 0.75, 0.7, 0.0

[life
_table_end]

[fraction_male_births:] 0.5

[minimum_adult_male_age:] 2

[adult_male_migration_probability:] 0.6

[individual_label:] A

[template_start]

G(+,A(+)$)

[template_end]

[key_io_mode:] indent

[subpopulation_number:] 2

[subpopulation_start]

[keyindent_st
art]

G,3

Presented at Python Community Conference (PyCon) Washington, DC 28 March 2003 P.
11
/
20


A,901 A,903 A,1077 A,772 A,975 A,1079 A,1078 A,839


A,980 A,844 A,981 A,910 A,1080 A,982 A,909 A,1081


A,984 A,983 A,1082 A,912 A,1084 A,1083 A,988 A,987


A,1085 A,986 A,1087 A,1086 A,985 A,911 A,1089 A,1088


A,991 A,990 A,989 A,1090 A,783 A
,1092 A,1091 A,848


A,1093 A,993 A,1094 A,847 A,1097 A,1096 A,1095 A,914


A,1101 A,1100 A,1099 A,1098 A,999 A,1105 A,1104 A,1103


A,1102 A,998 A,997 A,1106 A,782 A,1003 A,1002 A,1001


A,1107 A,850 A,1109 A,1108 A,1005 A,1004 A,849 A,1111


A,1110 A,915

A,1113 A,1112 A,1009 A,1008 A,1116 A,1115


A,1114 A,785 A,1117 A,784 A,919 A,1119 A,1118 A,1013


A,1120 A,918 A,1016 A,1015 A,1123 A,1122 A,1121 A,855


A,923 A,1017 A,1124 A,922 A,859 A,1125 A,926 A,1129


A,1128 A,1127 A,1126 A,925 A,1132 A,1131 A,113
0 A,1020


A,1133 A,793 A,1134 A,1023 A,1022 A,1135 A,795 A,882


A,939 A,947 A,948 A,842 A,1028 A,932 A,1033 A,1035


A,1034 A,1040 A,941 A,1042 A,1043 A,950 A,1049 A,1055


A,1058 A,960 A,1060 A,1073 A,1075 A,1076 A,787 A,900


A,904 A,921 A,818 A,873 A,
871 A,815 A,881 A,895


A,778

[keyindent_end]

[subpopulation_end]

[subpopulation_start]

[keyindent_start]

G,4


A,800 A,1136 A,1027 A,1137 A,1026 A,1025 A,1138 A,801


A,1030 A,1142 A,1141 A,1140 A,1139 A,1029 A,864 A,930


A,929 A,927 A,802 A,931 A,1143 A
,1032 A,867 A,1144


A,869 A,1145 A,1036 A,1149 A,1148 A,1147 A,1146 A,934


A,804 A,1037 A,1150 A,936 A,1038 A,1151 A,1039 A,1153


A,1152 A,942 A,1041 A,809 A,1154 A,876 A,812 A,877


A,878 A,1156 A,1155 A,946 A,1159 A,1158 A,1157 A,949


A,1162 A,1161 A
,1160 A,1046 A,1164 A,1163 A,1045 A,1044


A,822 A,1167 A,1166 A,1165 A,952 A,1168 A,951 A,830


A,1048 A,1169 A,1050 A,888 A,1170 A,1051 A,1172 A,1171


A,956 A,1174 A,1173 A,1052 A,1175 A,887 A,1054 A,1053


A,958 A,1176 A,1056 A,1178 A,1177 A,962 A,1057

A,833


A,1179 A,1059 A,1180 A,1062 A,964 A,1182 A,1181 A,1063


A,963 A,1066 A,1186 A,1185 A,1184 A,1183 A,1065 A,835


A,1187 A,1068 A,1188 A,1067 A,968 A,967 A,1189 A,894


A,1190 A,1069 A,1191 A,969 A,1193 A,1192 A,1070 A,836


A,1194 A,1072 A,1071 A,
972 A,1195 A,1074 A,1196 A,970


A,1197 A,846 A,902 A,916 A,885 A,974 A,973 A,976


A,977 A,979 A,978 A,908 A,841 A,992 A,995 A,1006


A,1007 A,1010 A,917 A,1012 A,1014 A,1019 A,1018 A,1021


A,924 A,823 A,892 A,933 A,935 A,937 A,943 A,944


A,945 A,953 A,
957 A,959 A,961 A,971 A,773 A,776


A,777 A,799 A,826

[keyindent_end]

[subpopulation_end]

[individual_fields_start]

[individual_fields:] 772, {'age': 5, 'sex': 'f'}

[individual_fields:] 773, {'age': 5, 'sex': 'm'}

[individual_fields:] 776, {'age': 5, 'sex'
: 'm'}

.

.

[individual_fields:] 900, {'age': 3, 'sex': 'm'}

Presented at Python Community Conference (PyCon) Washington, DC 28 March 2003 P.
12
/
20

[individual_fields:] 901, {'age': 3, 'sex': 'm'}

[individual_fields:] 902, {'age': 3, 'sex': 'm'}

.

.


[individual_fields:] 1195, {'age': 1, 'sex': 'f'}

[individual_fields:] 1196, {'age': 1, 'sex'
: 'm'}

[individual_fields:] 1197, {'age': 1, 'sex': 'f'}

[individual_fields_end]

[kinship_links_start]

[kinship_links:] 101, {'fa': None, 'mo': None}

[kinship_links:] 102, {'fa': None, 'mo': None}

[kinship_links:] 103, {'fa': None, 'mo': None}

.

.


[kinshi
p_links:] 474, {'fa': 423, 'mo': 417}

[kinship_links:] 475, {'fa': 437, 'mo': 436}

[kinship_links:] 476, {'fa': 437, 'mo': 440}

.

.

[kinship_links:] 1195, {'fa': 921, 'mo': 972}

[kinship_links:] 1196, {'fa': 921, 'mo': 1074}

[kinship_links:] 1197, {'fa': 1
050, 'mo': 970}

[kinship_links_end]

[population_end]

[simulation_end]



3) Extract mammal_demes simulation terminal state file


[simulation_start]

[run_name:] Pycon Mammal Demes Simulation

[current_time:] 21

[time_limit:] 20

[random_initialization_file:] c
://Pycon/Pycon_demes.ro

[population_start]

[population_name:] Pycon Mammal Demes Population

[initial_age_class:] 1

[age_classes:] 5

[life_table_start]


f, fertility, 0.0, 1.2, 1.3, 0.9, 0.7


f, survival, 0.9, 0.8, 0.75, 0.7, 0.0


m, survival, 0.9, 0.8, 0.7
5, 0.7, 0.0

[life_table_end]

[fraction_male_births:] 0.5

[minimum_adult_male_age:] 2

[adult_male_migration_probability:] 0.6

[individual_label:] A

[template_start]

G(+,A(+)$)

[template_end]

[key_io_mode:] indent

[subpopulation_number:] 2

[subpopulation_sta
rt]

[keyindent_start]

G,3

Presented at Python Community Conference (PyCon) Washington, DC 28 March 2003 P.
13
/
20


A,903 A,1077 A,1199 A,1198 A,975 A,1201 A,1200 A,1079


A,839 A,1202 A,980 A,844 A,981 A,1204 A,1203 A,910


A,1205 A,1080 A,1206 A,982 A,1207 A,1081 A,1208 A,912


A,1209 A,1084 A,1210 A,1083 A,1211 A,988 A,1212 A,1085


A,986
A,1213 A,1087 A,1086 A,985 A,1215 A,1214 A,911


A,1088 A,1217 A,1216 A,991 A,989 A,1220 A,1219 A,1218


A,1090 A,1092 A,1221 A,1093 A,1094 A,1224 A,1223 A,1222


A,847 A,1097 A,1225 A,1096 A,1095 A,1229 A,1228 A,1227


A,1226 A,914 A,1100 A,1230 A,1098 A,
1232 A,1231 A,999


A,1105 A,998 A,1106 A,1233 A,1002 A,1236 A,1235 A,1234


A,1001 A,1107 A,1237 A,850 A,1108 A,1239 A,1238 A,1005


A,1241 A,1240 A,1004 A,1242 A,849 A,1115 A,1114 A,1245


A,1244 A,1243 A,1117 A,919 A,1118 A,1247 A,1246 A,1013


A,1120 A
,918 A,1249 A,1248 A,1016 A,1015 A,1123 A,1251


A,1250 A,1122 A,1252 A,1121 A,1254 A,1253 A,855 A,1124


A,859 A,1257 A,1256 A,1255 A,1125 A,926 A,1259 A,1258


A,1129 A,1127 A,1126 A,1132 A,1260 A,1020 A,1262 A,1261


A,1133 A,1022 A,842 A,941 A,1042 A,1
043 A,1060 A,1026


A,930 A,1032 A,1149 A,1148 A,1150 A,877 A,1161 A,1160


A,1044 A,1167 A,1172 A,1173 A,1054 A,1176 A,1178 A,1179


A,1067 A,902 A,974 A,973 A,977 A,979 A,978 A,1007


A,1010 A,1019 A,1021 A,924 A,935 A,937 A,953 A,957


A,971

[keyindent_
end]

[subpopulation_end]

[subpopulation_start]

[keyindent_start]

G,4


A,1136 A,1027 A,1264 A,1263 A,1025 A,1265 A,1138 A,1030


A,1267 A,1266 A,1142 A,1141 A,1268 A,1139 A,1270 A,1269


A,1029 A,1271 A,929 A,931 A,1272 A,1143 A,869 A,1145


A,1036 A,1273
A,1147 A,1274 A,1146 A,1276 A,1275 A,936


A,1278 A,1277 A,1038 A,1151 A,1279 A,1039 A,1280 A,1152


A,1282 A,1281 A,942 A,1285 A,1284 A,1283 A,1041 A,1154


A,876 A,1156 A,1155 A,1158 A,1157 A,1287 A,1286 A,949


A,1162 A,1289 A,1288 A,1046 A,1291 A,1290
A,1164 A,1292


A,1163 A,1294 A,1293 A,1045 A,1166 A,1296 A,1295 A,1165


A,1297 A,952 A,1168 A,951 A,1299 A,1298 A,1048 A,1169


A,1050 A,1170 A,1051 A,1171 A,956 A,1300 A,1174 A,1302


A,1301 A,1052 A,1304 A,1303 A,1175 A,887 A,1053 A,958


A,1305 A,1177

A,1308 A,1307 A,1306 A,962 A,1057 A,1059


A,1180 A,1062 A,1309 A,964 A,1182 A,1310 A,1181 A,1063


A,1311 A,963 A,1066 A,1312 A,1186 A,1314 A,1313 A,1185


A,1316 A,1315 A,1184 A,1318 A,1317 A,1183 A,1065 A,1187


A,1068 A,1319 A,1188 A,1189 A,894 A,1069

A,1191 A,1193


A,1320 A,1192 A,1070 A,1194 A,972 A,1322 A,1321 A,1195


A,1323 A,1196 A,970 A,1324 A,1197 A,846 A,916 A,976


A,908 A,841 A,1006 A,917 A,1014 A,892 A,944 A,945


A,961 A,1078 A,984 A,1089 A,990 A,1101 A,1104 A,1109


A,1110 A,1113 A,1116
A,1119 A,922 A,1131 A,1130 A,1134


A,1023 A,1135 A,882 A,939 A,1028 A,1033 A,1035 A,1034


A,1040 A,950 A,1055 A,1058 A,960 A,1073 A,900 A,904


A,921 A,873 A,881 A,895

[keyindent_end]

[subpopulation_end]

[individual_fields_start]

[individual_fields:] 839
, {'age': 5, 'sex': 'f'}

Presented at Python Community Conference (PyCon) Washington, DC 28 March 2003 P.
14
/
20

[individual_fields:] 841, {'age': 5, 'sex': 'm'}

[individual_fields:] 842, {'age': 5, 'sex': 'm'}

.

.

[individual_fields:] 999, {'age': 3, 'sex': 'f'}

[individual_fields:] 1001, {'age': 3, 'sex': 'f'}

[individual_fields:] 1002, {'a
ge': 3, 'sex': 'f'}

.

.

[individual_fields:] 1322, {'age': 1, 'sex': 'm'}

[individual_fields:] 1323, {'age': 1, 'sex': 'f'}

[individual_fields:] 1324, {'age': 1, 'sex': 'f'}

[individual_fields_end]

[kinship_links_start]

[kinship_links:] 101, {'fa': None, '
mo': None}

[kinship_links:] 102, {'fa': None, 'mo': None}

[kinship_links:] 103, {'fa': None, 'mo': None}

.

.

[kinship_links:] 474, {'fa': 423, 'mo': 417}

[kinship_links:] 475, {'fa': 437, 'mo': 436}

[kinship_links:] 476, {'fa': 437, 'mo': 440}

.

.

[kinship
_links:] 1322, {'fa': 957, 'mo': 972}

[kinship_links:] 1323, {'fa': 1188, 'mo': 1195}

[kinship_links:] 1324, {'fa': 944, 'mo': 970}

[kinship_links_end]

[population_end]

[simulation_end]




Appendix C.


Code, Input and Output Samples from RSB Simulation


(

Rhesus Monkey, Savanna Baboon Model )



1 ) main rsb control code sequence



sim = rsb_simulation ()



try:


sim.initialize_io()


sim.start_reading_gin()


randy.rndgen.read_initial_random_conditions( sim.simgin )


sim.pop.read_popul
ation( sim.simgin )


sim.pop.set_idn_server_types()


sim.finish_reading_gin()


sim.pop.initialize_id_processing ( )


sim.pop.kinlinks.set_trace_parent_from_sex()




sim.line_to_console()

Presented at Python Community Conference (PyCon) Washington, DC 28 March 2003 P.
15
/
20


sim.start_simulation_console_messag
es()


sim.start_simulation_tym_messages()


sim.start_daytime_tym_message()





while sim.currtime <= sim.timelimit:


sim.line_to_console()


sim.start_time_period_console_message()


sim.start_time_period_tym_messa
ge()


sim.pop.population_survival()


sim.pop.population_delete_empty_matrilines ()


sim.pop.population_ageing()


sim.pop.population_migration()


sim.pop.population_delete_empty_matrilines ()


sim.pop.
population_reproduction( sim.pop.min_adult_male_age,
\


sim.pop.fraction_male_births, sim.pop.initial_age_class,
\


sim.pop.individual_label)


sim.pop.population_matriline_fissions ( )



sim.pop.population_group_fissions()


sim.pop.population_group_fusions ( )


sim.pop.population_disperse_groups_with_empty_natal_segment ( )


sim.finish_time_period_tym_message()


sim.end_time_period_console_message()



sim.pop.template_check_subpopulations()




sim.currtime = sim.currtime + 1





sim.finish_daytime_tym_message()


sim.finish_simulation_tym_messages()


sim.end_simulation_console_messages(
)


sim.line_to_console


sim.initialize_fin()


sim.start_writing_fin()


randy.rndgen.write_random_conditions ( sim.simpathnm + ".ro", sim.simfin )


sim.pop.write_population( sim.simfin )


sim.finish_writing_fin()





except

Exception:




sim.initialize_err()


sim.start_writing_err()


sim.write_exception_to_err()


sim.finish_writing_err()


sim.simerr.close()


raise




2) Extract of rsb simulation initialization file


[simulation_start]

[run_name
:] PyCon RSB Simulation

[current_time:] 20

[time_limit:] 20

Presented at Python Community Conference (PyCon) Washington, DC 28 March 2003 P.
16
/
20

[random_initialization_seed:] 56789

[population_start]

[population_name:] PyCon RSB Population

[initial_age_class:] 1

[age_classes:] 5

[life_table_start]


f, fertility, 0.0, 1.4, 1.5, 1.0, 0.7


f,

survival, 0.9, 0.85, 0.8, 0.75, 0.0


m, survival, 0.9, 0.8, 0.75, 0.7, 0.0

[life_table_end]

[binomial_trials_per_period:] 4

[fraction_male_births:] 0.5

[minimum_adult_male_age:] 2

[adult_male_migration_probability:] 0.6

[group_fission_size_threshold:] 50

[group_fusion_size_threshold:] 18

[matriline_fission_size_threshold:] 9

[lineage_trace_sex:] f

[ancestor_trace_depth:] 5

[individual_label:] A

[template_start]

G(+,H(+,A(+)$)N(+,M(+,A(+)$)$))

[template_end]

[key_io_mode:] indent

[subpopulation_number:] 23

[subpopulation_start]

[keyindent_start]

G,1


H,1


A,851 A,1021 A,1442 A,1425 A,947 A,890 A,1193 A,1321


A,1244 A,1342 A,1209


N,1


M,51


A,860 A,1340 A,1644 A,1339 A,1646 A,1645 A,1338 A,1648


A,1647


M,87


A,950 A,1658 A,1109
A,1661 A,1660 A,1659


M,84


A,1044 A,1744 A,1464 A,1745


M,93


A,853


M,66


A,847 A,1224 A,1743 A,1742 A,1741 A,1740

[keyindent_end]

[subpopulation_end]

[subpopulation_start]

[keyindent_start]

G,2


H,2


A,1221 A,1205 A,1290 A,12
99 A,1267 A,1198 A,1028 A,1367


A,1135 A,1444 A,1387 A,1432 A,971


N,2


M,21


A,963 A,1348 A,1122 A,1667 A,1666 A,1350 A,1121 A,1669


A,1668


M,59


A,878 A,1671 A,1670 A,1356 A,1672 A,1355 A,1673 A,1142

Presented at Python Community Conference (PyCon) Washington, DC 28 March 2003 P.
17
/
20


A,1674


M,75


A,1117 A,1676 A,1675


M,88


A,966 A,1679 A,1678 A,1677 A,1352


M,107


A,1119 A,1663 A,1662 A,1347 A,1664 A,1665

[keyindent_end]

[subpopulation_end]

.

.

[subpopulation_start]

[keyindent_start]

G,25


H,25


A,1113 A,1075 A,1380 A,1351 A,11
84 A,1458


N,25


M,90


A,1386 A,1753 A,1752


M,112


A,912 A,1406 A,1751


M,43


A,1162 A,1765 A,1764 A,1383 A,1766 A,998 A,1767


M,55


A,1388 A,1755 A,1754 A,1004 A,1758 A,1393 A,1760 A,1759


A,1392 A,1761 A,1168 A,1763

A,1762


M,78


A,1170 A,1748 A,1747 A,1746 A,1405 A,1749 A,1404 A,1750

[keyindent_end]

[subpopulation_end]

[subpopulation_start]

[keyindent_start]

G,26


H,26


A,1124 A,1095 A,1301 A,1331 A,1199


N,26


M,79


A,935 A,1768 A,1191 A,1769 A
,1423 A,1772 A,1771 A,1770


A,1029 A,1426


M,82


A,1429 A,1773 A,1020 A,1431 A,1775 A,1774

[keyindent_end]

[subpopulation_end]

[individual_fields_start]

[individual_fields:] 845, {'age': 5, 'sex': 'f'}

[individual_fields:] 847, {'age': 5, 'sex
': 'f'}

[individual_fields:] 848, {'age': 5, 'sex': 'm'}

.

.

[individual_fields:] 1090, {'age': 3, 'sex': 'f'}

[individual_fields:] 1091, {'age': 3, 'sex': 'm'}

[individual_fields:] 1092, {'age': 3, 'sex': 'f'}

.

.

[individual_fields:] 1793, {'age': 1, 'se
x': 'm'}

[individual_fields:] 1794, {'age': 1, 'sex': 'm'}

[individual_fields:] 1795, {'age': 1, 'sex': 'f'}

Presented at Python Community Conference (PyCon) Washington, DC 28 March 2003 P.
18
/
20

[individual_fields_end]

[kinship_links_start]

[kinship_links:] 101, {'fa': None, 'mo': None}

[kinship_links:] 102, {'fa': None, 'mo': None}

[kinshi
p_links:] 103, {'fa': None, 'mo': None}

.

.

[kinship_links:] 474, {'fa': 423, 'mo': 417}

[kinship_links:] 475, {'fa': 437, 'mo': 436}

[kinship_links:] 476, {'fa': 437, 'mo': 440}

.

.

[kinship_links:] 1793, {'fa': 1003, 'mo': 1215}

[kinship_links:] 1794, {'
fa': 1003, 'mo': 1215}

[kinship_links:] 1795, {'fa': 1069, 'mo': 1462}

[kinship_links_end]

[population_end]

[simulation_end]



3) Extract from rsb simulation terminal state file


[simulation_start]

[run_name:] PyCon RSB Simulation

[current_time:] 21

[time_
limit:] 20

[random_initialization_file:] c://Pycon/Pycon_rsb.ro

[population_start]

[population_name:] PyCon RSB Population

[initial_age_class:] 1

[age_classes:] 5

[life_table_start]


f, fertility, 0.0, 1.4, 1.5, 1.0, 0.7


f, survival, 0.9, 0.85, 0.8, 0.75,

0.0


m, survival, 0.9, 0.8, 0.75, 0.7, 0.0

[life_table_end]

[binomial_trials_per_period:] 4

[fraction_male_births:] 0.5

[minimum_adult_male_age:] 2

[adult_male_migration_probability:] 0.6

[group_fission_size_threshold:] 50

[group_fusion_size_threshold:] 1
8

[matriline_fission_size_threshold:] 9

[lineage_trace_sex:] f

[ancestor_trace_depth:] 5

[individual_label:] A

[template_start]

G(+,H(+,A(+)$)N(+,M(+,A(+)$)$))

[template_end]

[key_io_mode:] indent

[subpopulation_number:] 29

[subpopulation_start]

[keyindent
_start]

G,1


H,1


A,1021 A,1349 A,1521 A,1553 A,1674 A,1669 A,1748 A,1766

Presented at Python Community Conference (PyCon) Washington, DC 28 March 2003 P.
19
/
20


A,1085 A,1792 A,1650 A,1641


N,1


M,51


A,1340 A,1945 A,1944 A,1644 A,1947 A,1946 A,1338


M,87


A,950 A,1658 A,1950 A,1109 A,1952 A,1951 A,1660 A,1954


A,1953


M,84


A,1044 A,1955 A,1744 A,1956 A,1464 A,1957


M,66


A,1224 A,1959 A,1958 A,1743 A,1961 A,1960


M,123


A,1339 A,1948 A,1949

[keyindent_end]

[subpopulation_end]

[subpopulation_start]

[keyindent_start]

G,2


H,2


A,1205 A
,1028 A,1543 A,1292 A,1161 A,1390 A,1704 A,961


N,2


M,21


A,963 A,1963 A,1962 A,1348 A,1964 A,1122 A,1965 A,1667


A,1969 A,1968 A,1967 A,1966 A,1350 A,1970 A,1121 A,1971


M,59


A,1356 A,1355 A,1973 A,1972 A,1142


M,88


A,966
A,1352 A,1978 A,1977 A,1976 A,1975


M,107


A,1119 A,1979 A,1663 A,1980 A,1662 A,1981 A,1347 A,1983


A,1982 A,1664 A,1986 A,1985 A,1984

[keyindent_end]

[subpopulation_end]

.

.

[subpopulation_start]

[keyindent_start]

G,33


H,33


A,1761 A,130
8 A,1430 A,1102 A,1753


N,33


M,11


A,1434 A,1725 A,1433 A,2040 A,2039 A,1727 A,2042 A,2041


A,1726 A,1729 A,2045 A,2044 A,2043 A,1437 A,2046 A,1731


A,2048 A,2047 A,1436 A,2049 A,1438 A,2051 A,2050 A,1734

[keyindent_end]

[subpopulation_
end]

[subpopulation_start]

[keyindent_start]

G,34


H,34


A,1003 A,1113 A,1636 A,1450


N,34


M,55


A,1004 A,2074 A,1393 A,1760 A,1759 A,2075 A,1392 A,2077


A,2076 A,1168 A,1763 A,2078 A,1762 A,2082 A,2081 A,2080


A,2079

Presented at Python Community Conference (PyCon) Washington, DC 28 March 2003 P.
20
/
20


M,43



A,1162 A,2066 A,1765 A,2067 A,1764 A,2069 A,2068 A,1383


A,2070


M,112


A,1406 A,2065

[keyindent_end]

[subpopulation_end]

[individual_fields_start]

[individual_fields:] 942, {'age': 5, 'sex': 'f'}

[individual_fields:] 943, {'age': 5, 'sex': '
f'}

[individual_fields:] 948, {'age': 5, 'sex': 'f'}

.

.

[individual_fields:] 1226, {'age': 3, 'sex': 'f'}

[individual_fields:] 1227, {'age': 3, 'sex': 'm'}

[individual_fields:] 1231, {'age': 3, 'sex': 'f'}

.

.

[individual_fields:] 2205, {'age': 1, 'sex':
'f'}

[individual_fields:] 2206, {'age': 1, 'sex': 'f'}

[individual_fields:] 2207, {'age': 1, 'sex': 'f'}

[individual_fields_end]

[kinship_links_start]

[kinship_links:] 101, {'fa': None, 'mo': None}

[kinship_links:] 102, {'fa': None, 'mo': None}

[kinship_li
nks:] 103, {'fa': None, 'mo': None}

.

.

[kinship_links:] 474, {'fa': 423, 'mo': 417}

[kinship_links:] 475, {'fa': 437, 'mo': 436}

[kinship_links:] 476, {'fa': 437, 'mo': 440}

.

.

[kinship_links:] 2205, {'fa': 1379, 'mo': 1335}

[kinship_links:] 2206, {'fa':

1721, 'mo': 1335}

[kinship_links:] 2207, {'fa': 1618, 'mo': 1335}

[kinship_links_end]

[population_end]

[simulation_end]