emod_api.serialization.SerializedPopulation module

Class to load and manipulate a saved population.

class emod_api.serialization.SerializedPopulation.SerializedPopulation(file: str)[source]

Bases: object

Opens the passed file and reads in all the nodes.

Parameters

file – serialized population file

Examples

Create an instance of SerializedPopulation:

import emod_api.serialization.SerializedPopulation as SerPop
ser_pop = SerPop.SerializedPopulation('state-00001.dtk')
property nodes

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:

infection = node["individualHumans"][0]["infections"][0]
infection["suid"] = self.get_next_infection_suid()
node["individualHumans"][1]["infections"].append(infection)
node["individualHumans"][1].m_is_infected = True
flush()[source]

Save all made changes to the node(s).

write(output_file: str = 'my_sp_file.dtk')[source]

Write the population to a file.

Parameters

output_file – output file

get_next_infection_suid()[source]

Each infection needs a unique identifier, this function returns one.

get_next_individual_suid(node_id: int)dict[source]

Each individual needs a unique identifier, this function returns one.

Parameters

node_id – The first parameter.

Returns

The return value. True for success, False otherwise.

Examples

To get a unique id for an individual:

print(sp.get_next_individual_suid(0))
{'id': 2}
emod_api.serialization.SerializedPopulation.find(name: str, handle, currentlevel='dtk.nodes')[source]

Recursively searches for a paramters that matches or is close to name and prints out where to find it in the file.

Parameters
  • name – the paramter you are looking for e.g. “age”, “gender”.

  • handle – some iterable data structure, can be a list of nodes, a node, list of individuals, etc currentlevel: just a string to print out where the found item is located e.g. “dtk.nodes” or “dtk.node.individuals”

Examples

What is the exact paramteter name used for the age of an individual?:

SerPop.find("age", node)
...
1998   Found in:  dtk.nodes.individualHumans[999].m_age
1999   Found in:  dtk.nodes.individualHumans[999].susceptibility.age
2000   Found in:  dtk.nodes.m_vectorpopulations[0].EggQueues[0].age
2001   Found in:  dtk.nodes.m_vectorpopulations[0].EggQueues[1].age
...
emod_api.serialization.SerializedPopulation.get_parameters(handle, currentlevel='dtk.nodes')[source]

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
  • handle – some iterable data structure, can be a list of nodes, a node, list of individuals, etc

  • currentlevel – just a string to print out where the found item is located e.g. “dtk.nodes”or “dtk.node.individuals

Examples

Print all parameters in serialized population file:

for n in sorted(SerPop.get_parameters(node)):
    print(n)