Skip to content

new_demographics_old_format

create_old_demographic_files(country_name, output_dir=None, suffix=None)

A function that outputs the demographics files for the Zambia model in the old format of four overlay files: Demographics.json, PFA_Overlay.json, Accessibility_and_Risk_IP_Overlay.json, Risk_Assortivity_Overlay.json. The files are created in the output_dir directory. This can be helpful wnen comparing the new and old models.

Parameters:

Name Type Description Default
output_dir str or Path

The directory where the files will be created. If None, the current directory will be used.

None
suffix str

A suffix to add to the file names. If None, no suffix will be added.

None
Source code in emodpy_hiv/countries/converting/new_demographics_old_format.py
def create_old_demographic_files(country_name: str,
                                 output_dir: Union[str, Path] = None,
                                 suffix: str = None):
    """
    A function that outputs the demographics files for the Zambia model in the old format
    of four overlay files: Demographics.json, PFA_Overlay.json, Accessibility_and_Risk_IP_Overlay.json,
    Risk_Assortivity_Overlay.json. The files are created in the output_dir directory.  This can be
    helpful wnen comparing the new and old models.

    Args:
        output_dir (str or Path): The directory where the files will be created. If None, the current
            directory will be used.

        suffix (str):
            A suffix to add to the file names. If None, no suffix will be added.
    """
    if country_name is None:
        raise ValueError("You must specify a country to create the demographics for.")

    country_class = country_model.get_country_class(country_class_name=country_name)

    if output_dir is None:
        output_dir = "."

    reduce_list = ["population", "ip", "society", "assortivity"]
    for reduce in reduce_list:
        demog = country_class.build_demographics()
        demog_json = demog.to_dict()
        reduced_fn = None
        if reduce == "population":
            demog_json = _reduce_to_population_only(demog_json=demog_json)
            reduced_fn = OLD_FILENAMES[0]
        elif reduce == "society":
            demog_json = _reduce_to_society_only(demog_json=demog_json)
            reduced_fn = OLD_FILENAMES[1]
        elif reduce == "ip":
            demog_json = _reduce_to_ip_only(demog_json=demog_json)
            reduced_fn = OLD_FILENAMES[2]
        elif reduce == "assortivity":
            demog_json = _reduce_to_assortivity_only(demog_json=demog_json)
            reduced_fn = OLD_FILENAMES[3]

        if suffix is not None:
            reduced_fn = reduced_fn + "_" + suffix

        tmp_filename = Path(output_dir).joinpath(reduced_fn + ".json")

        with open(tmp_filename, "w") as file:
            tmp = json.dumps(demog_json, indent=4, sort_keys=True)
            tmp = json.loads(tmp, parse_float=lambda x: round(float(x), 9))
            json.dump(tmp, file, indent=4, sort_keys=True)