SerializedPopulation
Class to load and manipulate a saved population.
SerializedPopulation
Opens the passed file and reads in all the nodes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file
|
str
|
serialized population file |
required |
Examples:
Create an instance of SerializedPopulation:: import emod_api.serialization.SerializedPopulation as SerPop ser_pop = SerPop.SerializedPopulation('state-00001.dtk')
Source code in emod_api/serialization/SerializedPopulation.py
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 | |
nodes
property
All nodes.
Examples:
Delete number_of_ind individuals from node 0:: node = ser_pop.nodes[0] del node.individualHumans[0:number_of_ind]
Only keep individuals with a certain condition:: node.individualHumans = [ind for ind in node.individualHumans if keep_fct(ind)]
Change susceptibility of an individual:: print(node.individualHumans[0].susceptibility) new_susceptibility = {"age": 101.01, "mod_acquire": 0} node.individualHumans[0].susceptibility.update(new_susceptibility)
Copy individual[0] from node 0, change properties and add individual as new individual:: import copy individual_properties = {"m_age": 1234} individual = copy.deepcopy(node.individualHumans[0]) individual["suid"] = ser_pop.get_next_individual_suid(0) individual.update(individual_properties) ser_pop.nodes[0].individualHumans.append(individual)
Infect an individual with an infection copied from another individual:: import copy individual_0 = node.individualHumans[0] individual_1 = node.individualHumans[1] new_infection = copy.deepcopy(individual_0.infections[0]) new_infection["suid"] = ser_pop.get_next_infection_suid() individual_1.infections.append(new_infection) individual_1.m_is_infected = True
flush()
get_next_individual_suid(node_id)
Each individual needs a unique identifier, this function returns one.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node_id
|
int
|
The first parameter. |
required |
Returns:
| Type | Description |
|---|---|
dict
|
The return value. True for success, False otherwise. |
Examples:
To get a unique id for an individual::
1 2 | |
Source code in emod_api/serialization/SerializedPopulation.py
get_next_infection_suid()
Each infection needs a unique identifier, this function returns one.
Source code in emod_api/serialization/SerializedPopulation.py
write(output_file='my_sp_file.dtk')
Write the population to a file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
output_file
|
str
|
output file |
'my_sp_file.dtk'
|
Source code in emod_api/serialization/SerializedPopulation.py
find(name, handle, currentlevel='dtk.nodes')
Recursively searches for a paramters that matches or is close to name and prints out where to find it in the file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
the paramter you are looking for e.g. "age", "gender". |
required |
handle
|
Union[str, Iterable]
|
some iterable data structure, can be a list of nodes, a node, list of individuals, etc |
required |
currentlevel
|
str
|
just a string to print out where the found item is located e.g. "dtk.nodes" or "dtk.node.individuals" |
'dtk.nodes'
|
Examples:
What is the exact paramteter name used for the age of an individual?::
1 2 3 4 5 6 7 | |
Source code in emod_api/serialization/SerializedPopulation.py
get_parameters(handle, currentlevel='dtk.nodes')
Return a set of all parameters in the serialized population file. Helpful to get an overview about what is in the serialized population file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
handle
|
Union[str, Iterable]
|
some iterable data structure, can be a list of nodes, a node, list of individuals, etc |
required |
currentlevel
|
str
|
just a string to print out where the found item is located e.g. "dtk.nodes" or "dtk.node.individuals |
'dtk.nodes'
|
Examples: Print all parameters in serialized population file:: for n in sorted(SerPop.get_parameters(node)): print(n)