from pathlib import Path
import yaml
import json


def print_results_from_evaluation_dirs(work_dir_path: Path, run_numbers: list,
                                       print_results_only: bool = False) -> None:
    """Print the aggregated results from multiple evaluation runs."""

    def float_representer(dumper, value):
        text = '{0:.4f}'.format(value)
        return dumper.represent_scalar(u'tag:yaml.org,2002:float', text)

    yaml.add_representer(float, float_representer)

    for run_number in run_numbers:
        eval_dir_path = work_dir_path / f'evaluation_{run_number}'
        eval_file_name = f'evaluation_results_{run_number}.json'
        print(f'--- Evaluation summary run {run_number} ---')
        with open(eval_dir_path / eval_file_name, 'r') as infile:
            results = json.load(infile)
            test_set_name = results['test_set_name']
            if print_results_only:
                results = {key: val for key, val in results.items() if 'result' in key}
                results['test_set_name'] = test_set_name
            print(yaml.dump(results))