import csv
from collections import defaultdict
from pathlib import Path
import numpy as np
from util.config import MConfig


outpath = Path('..', 'output')
metric_file_name = 'metrics.csv'
config_file_name = 'config.ini'


if __name__ == '__main__':
    for model_path in outpath.iterdir():
        if not model_path.is_dir():
            continue
        out_file = (model_path / metric_file_name)
        for paramter_configuration in model_path.iterdir():
            if not model_path.is_dir():
                continue
            uar_scores = defaultdict(list)
            for metric_file in paramter_configuration.rglob(metric_file_name):
                with metric_file.open('r') as f:
                    config = MConfig()
                    with (metric_file.parent / config_file_name).open('r') as c:
                        config.read_file(c)
                    for key, val in config.data.__dict__.items():
                        uar_scores[key].append(val)

                    headers = f.readline().split(',')
                    metric_dict = defaultdict(list)
                    for line in f:
                        values = line.split(',')
                        for header, value in zip(headers, values):
                            if value:
                                try:
                                    metric_dict[header].append(float(value))
                                except ValueError:
                                    metric_dict[header].append(value)
                for score, func in zip(['max'], [np.max]):  # [np.mean, np.max, np.median, np.std]):
                    try:
                        uar_scores[score].append(round(func(np.asarray(metric_dict['uar_score'])) * 100, 2))
                    except ValueError as e:
                        print(e)
                        pass
            file_existed = out_file.exists()
            with out_file.open('a+') as f:
                headers = list(uar_scores.keys())

                writer = csv.DictWriter(f, delimiter=',', lineterminator='\n', fieldnames=headers)
                if not file_existed:
                    writer.writeheader()  # file doesn't exist yet, write a header
                try:
                    for row_idx in range(len(uar_scores['max'])):
                        writer.writerow({key: uar_scores[key][row_idx] for key in headers})
                except IndexError:
                    print('could not read: ')
