from pathlib import Path
import warnings


def load_results(data_dir, prefix=""):
    """Load results from text file to dict

    Will load results from file [data_dir]/eval_results.txt with format:

        k1=v1
        k2=v2

    to dict:

        {"[prefix]k1": v1, "[prefix]k2": v2}

    Args:
        data_dir (str): Directory to load from
        prefix (str, optional): Key prefix. Defaults to "".

    Returns:
        dict: Dictionary of results
    """
    res_file = Path(data_dir) / "eval_results.txt"
    results = {}
    if res_file.exists():
        with res_file.open("rt") as f:
            for line in f.readlines():
                key, val = line.strip().split(" = ")
                results[prefix + key] = val
    else:
        warnings.warn(f"No results for {data_dir}")
    return results