def patch_json()

in llm_perf/update_llm_perf_leaderboard.py [0:0]


def patch_json(file):
    """
    Patch a JSON file by adding a 'stdev_' key with the same value as 'stdev' for all occurrences,
    but only if 'stdev_' doesn't already exist at the same level.
    This is to make the old optimum benchmark compatible with the new one.

    This function reads a JSON file, recursively traverses the data structure,
    and for each dictionary that contains a 'stdev' key without a corresponding 'stdev_' key,
    it adds a 'stdev_' key with the same value. The modified data is then written back to the file.

    Args:
        file (str): The path to the JSON file to be patched.

    Returns:
        None
    """
    with open(file, "r") as f:
        data = json.load(f)

    def add_stdev_(obj):
        if isinstance(obj, dict):
            new_items = []
            for key, value in obj.items():
                if key == "stdev" and "stdev_" not in obj:
                    new_items.append(("stdev_", value))
                if isinstance(value, (dict, list)):
                    add_stdev_(value)
            for key, value in new_items:
                obj[key] = value
        elif isinstance(obj, list):
            for item in obj:
                add_stdev_(item)

    add_stdev_(data)

    with open(file, "w") as f:
        json.dump(data, f, indent=4)