Skip to content

_export

Export serialized population data to external formats.

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
def export_humans_to_json(
    ser_pop: SerializedPopulation,
    output_file: str | Path,
) -> None:
    """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.

    Args:
        ser_pop (SerializedPopulation): A loaded SerializedPopulation.
        output_file (str | Path): Destination JSON file path. Parent directories are
            created if they do not exist.
    """
    output_path = Path(output_file)
    output_path.parent.mkdir(parents=True, exist_ok=True)

    human_data = {}
    for idx in range(len(ser_pop.nodes)):
        node = ser_pop.nodes[idx]
        human_data[f"Node {node.externalId}"] = node.individualHumans

    with open(output_path, "w") as f:
        json.dump(human_data, f)

    logger.info("Exported human data to %s", output_path)