import json
import re


def load_tuning_results(filename):
    """Loads JSON data from file and pre-processes it"""
    with open(filename) as f:
        json_data = json.load(f)

    # Removes the numbering following the kernel family name
    json_data["kernel_family"] = re.sub(r'_\d+', '', json_data["kernel_family"])

    # Adds the kernel name to the section instead of to the individual results
    assert len(json_data["results"]) > 0
    json_data["kernel"] = json_data["results"][0]["kernel"]
    for result in json_data["results"]:
        assert json_data["kernel"] == result["kernel"]
        result.pop("kernel", None)

    # Removes the 'PRECISION' parameter from the individual results: it is redundant
    for result in json_data["results"]:
        assert json_data["precision"] == str(result["parameters"]["PRECISION"])
        result["parameters"].pop("PRECISION", None)

    # All done
    return json_data