serialization
Malaria-specific utilities for working with EMOD .dtk serialized population files.
Builds on top of emod_api's SerializedPopulation with malaria-aware inspection, modification, and export capabilities.
Example::
from emodpy_malaria.serialization import (
MalariaSerializedPopulation, zero_infections, count_humans
)
population = MalariaSerializedPopulation("state-00100.dtk")
print(population.summary())
print(count_humans(population.ser_pop))
zero_infections(population.ser_pop)
population.write("state-00100-zeroed.dtk")
Genome
Represents a single parasite genome with barcode-to-DTK-dict conversion.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
barcode
|
str
|
Nucleotide string (characters A/C/G/T). |
required |
allele_root_id
|
int
|
Root ID for allele tracking. Typically the individual's SUID or -999 for vectors. |
required |
Source code in emodpy_malaria/serialization/_genomes.py
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 121 122 123 124 125 126 127 128 129 130 131 132 133 | |
from_dtk_dict(dtk_dict)
staticmethod
Construct a Genome from a DTK genome dict.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dtk_dict
|
dict
|
A dict with |
required |
Returns:
| Type | Description |
|---|---|
Genome
|
Genome instance with matching barcode and hash codes. |
Source code in emodpy_malaria/serialization/_genomes.py
int_to_nucleotide(val)
staticmethod
Convert an integer encoding back to a nucleotide character.
Source code in emodpy_malaria/serialization/_genomes.py
nucleotide_to_int(ch)
staticmethod
Convert a single nucleotide character to its integer encoding.
Source code in emodpy_malaria/serialization/_genomes.py
to_dtk_dict()
Convert to DTK genome dict format (m_pInner structure).
Source code in emodpy_malaria/serialization/_genomes.py
to_dtk_map_entry()
Convert to DTK genome map entry format (key/value pair).
MalariaSerializedPopulation
Malaria-aware wrapper around emod_api's SerializedPopulation.
Provides malaria-specific convenience methods for inspection,
modification, and export of .dtk serialized population files.
Delegates to the underlying SerializedPopulation for all low-level
file I/O.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_path
|
str | Path
|
Path to a |
required |
Source code in emodpy_malaria/serialization/_population.py
file_path
property
Original file path.
header
property
File header metadata.
nodes
property
All nodes (delegated to SerializedPopulation.nodes).
num_nodes
property
Number of nodes in the file.
ser_pop
property
The underlying emod_api SerializedPopulation object.
simulation
property
Simulation-level data dict.
version
property
DTK file format version.
find_parameter(name)
Search for a parameter by name (fuzzy) and return matching paths.
get_next_individual_suid(node_id)
Get a unique SUID for a new individual in the given node.
get_next_infection_suid()
summary()
Return a summary dict with node counts, human counts, etc.
write(output_file='my_sp_file.dtk')
Write the (possibly modified) population to a file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
output_file
|
str | Path
|
Destination file path. Parent directories are created if they do not exist. |
'my_sp_file.dtk'
|
Source code in emodpy_malaria/serialization/_population.py
count_humans(ser_pop, *, node_index=None)
Count individuals across all nodes or in a specific node.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ser_pop
|
SerializedPopulation
|
A loaded SerializedPopulation. |
required |
node_index
|
int
|
If provided, count only in this node (0-based index). |
None
|
Returns:
| Type | Description |
|---|---|
int
|
Number of individuals. |
Source code in emodpy_malaria/serialization/_inspect.py
count_infections(ser_pop, *, node_index=None)
Count total infections across all nodes or in a specific node.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ser_pop
|
SerializedPopulation
|
A loaded SerializedPopulation. |
required |
node_index
|
int
|
If provided, count only in this node. |
None
|
Returns:
| Type | Description |
|---|---|
int
|
Number of infections. |
Source code in emodpy_malaria/serialization/_inspect.py
count_vectors(ser_pop, *, node_index=None, queue=None)
Count vector cohorts across all nodes or in a specific node.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ser_pop
|
SerializedPopulation
|
A loaded SerializedPopulation. |
required |
node_index
|
int
|
If provided, count only in this node. |
None
|
queue
|
str
|
If provided, count only in this queue (e.g.,
|
None
|
Returns:
| Type | Description |
|---|---|
int
|
Number of vector cohorts. |
Source code in emodpy_malaria/serialization/_inspect.py
count_vectors_by_state(ser_pop, *, node_index=None)
Count vector cohorts grouped by species and state.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ser_pop
|
SerializedPopulation
|
A loaded SerializedPopulation. |
required |
node_index
|
int
|
If given, count only this node. Otherwise sums all nodes. |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, dict[str, int]]
|
Nested dict: |
Source code in emodpy_malaria/serialization/_vectors.py
export_humans_to_json(ser_pop, output_file)
Export human data to a JSON file, grouped by node.
Each key in the output JSON is "Node <external_id>" and the value
is the list of individual dicts from that node.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ser_pop
|
SerializedPopulation
|
A loaded SerializedPopulation. |
required |
output_file
|
str | Path
|
Destination JSON file path. Parent directories are created if they do not exist. |
required |
Source code in emodpy_malaria/serialization/_export.py
find_parameter(ser_pop, name, *, cutoff=0.6)
Search for parameters matching the given name using fuzzy matching.
Improved version of emod_api's find() that returns results as a list
of dot-path strings instead of printing them.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ser_pop
|
SerializedPopulation
|
A loaded SerializedPopulation. |
required |
name
|
str
|
Parameter name to search for (e.g., |
required |
cutoff
|
float
|
Similarity threshold for fuzzy matching (0.0-1.0). |
0.6
|
Returns:
| Type | Description |
|---|---|
list[str]
|
List of dot-notation paths where the parameter was found. |
Source code in emodpy_malaria/serialization/_inspect.py
get_all_barcodes(ser_pop)
Extract all unique barcode strings from the population's genome map.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ser_pop
|
SerializedPopulation
|
A loaded SerializedPopulation. |
required |
Returns:
| Type | Description |
|---|---|
list[str]
|
List of unique barcode strings. |
Source code in emodpy_malaria/serialization/_genomes.py
get_all_parameters(ser_pop)
Return the set of all parameter paths in the serialized population.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ser_pop
|
SerializedPopulation
|
A loaded SerializedPopulation. |
required |
Returns:
| Type | Description |
|---|---|
set[str]
|
Set of dot-notation parameter paths. |
Source code in emodpy_malaria/serialization/_inspect.py
get_infection_barcodes(ser_pop, *, node_index=None)
Extract barcode information for each infection in the population.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ser_pop
|
SerializedPopulation
|
A loaded SerializedPopulation. |
required |
node_index
|
int
|
If provided, inspect only this node. |
None
|
Returns:
| Type | Description |
|---|---|
list[dict]
|
List of dicts with keys |
list[dict]
|
|
Source code in emodpy_malaria/serialization/_genomes.py
get_vector_infection_summary(ser_pop, *, node_index=None)
Summarize vector infection state across all species and queues.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ser_pop
|
SerializedPopulation
|
A loaded SerializedPopulation. |
required |
node_index
|
int
|
If given, summarize only this node. |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dict with |
dict[str, Any]
|
|
dict[str, Any]
|
|
Source code in emodpy_malaria/serialization/_vectors.py
get_vector_species_names(ser_pop, *, node_index=None)
Return the names of vector species present in the population.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ser_pop
|
SerializedPopulation
|
A loaded SerializedPopulation. |
required |
node_index
|
int
|
If given, inspect only this node. Otherwise inspects the first node (species are typically identical across nodes). |
None
|
Returns:
| Type | Description |
|---|---|
list[str]
|
List of species name strings. |
Source code in emodpy_malaria/serialization/_vectors.py
list_node_ids(ser_pop)
Return the external IDs of all nodes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ser_pop
|
SerializedPopulation
|
A loaded SerializedPopulation. |
required |
Returns:
| Type | Description |
|---|---|
list[int]
|
List of node external IDs. |
Source code in emodpy_malaria/serialization/_inspect.py
read_header(file_path)
Read only the header of a .dtk file without loading node data.
Useful for quickly checking file version, compression, node count, and EMOD build info without the cost of decompressing population data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_path
|
str | Path
|
Path to the .dtk file. |
required |
Returns:
| Type | Description |
|---|---|
dict
|
Header dict with keys like |
dict
|
|
Source code in emodpy_malaria/serialization/_inspect.py
replace_genomes(ser_pop, next_barcode_fn)
Replace all parasite genomes in humans and vectors (in-place).
Clears the simulation-level ParasiteGenomeMap and rebuilds it with new
genomes generated by calling next_barcode_fn() for each infection.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ser_pop
|
SerializedPopulation
|
A loaded SerializedPopulation. |
required |
next_barcode_fn
|
Callable[[], str]
|
Callable returning a barcode string each time it is called. |
required |
Returns:
| Type | Description |
|---|---|
int
|
Total number of genomes replaced. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If a generated barcode has a different length than the existing barcode at that position. |
Source code in emodpy_malaria/serialization/_genomes.py
summarize(ser_pop)
Return a comprehensive summary of the serialized population.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ser_pop
|
SerializedPopulation
|
A loaded SerializedPopulation. |
required |
Returns:
| Type | Description |
|---|---|
dict
|
Dict with |
dict
|
and a |
Source code in emodpy_malaria/serialization/_inspect.py
zero_human_infections(humans, *, keep_ids=None)
Reset infection state of individuals to uninfected.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
humans
|
Any
|
Iterable of individual dicts (e.g., |
required |
keep_ids
|
list[int]
|
SUID IDs of individuals to skip. |
None
|
Returns:
| Type | Description |
|---|---|
int
|
Number of individuals whose infections were zeroed. |
Raises:
| Type | Description |
|---|---|
KeyError
|
If an individual is missing expected infection fields. |
Source code in emodpy_malaria/serialization/_infections.py
zero_infections(ser_pop, *, ignore_node_ids=None, keep_individual_ids=None, remove_vectors=False)
Zero all infections in the loaded population (in-place).
Resets human infection fields to uninfected state and either resets or removes infected vectors.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ser_pop
|
SerializedPopulation
|
A loaded SerializedPopulation. |
required |
ignore_node_ids
|
list[int]
|
Node external IDs to skip entirely. |
None
|
keep_individual_ids
|
list[int]
|
Individual SUID IDs whose infections are preserved. |
None
|
remove_vectors
|
bool
|
If True, remove infected/infectious vector cohorts. If False (default), reset their state to STATE_ADULT. |
False
|
Source code in emodpy_malaria/serialization/_infections.py
zero_vector_infections(vector_pop_list, *, remove=False)
Reset or remove infections from vector populations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vector_pop_list
|
Any
|
List of vector populations
( |
required |
remove
|
bool
|
If True, remove infected/infectious cohorts entirely. If False (default), reset to STATE_ADULT. |
False
|
Returns:
| Type | Description |
|---|---|
int
|
Number of vector cohorts affected. |