#!python
# Copyright 2020,2021 Sony Corporation.
# Copyright 2021 Sony Group Corporation.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import argparse
import pathlib

import numpy as np


def load_data(path):
    path = pathlib.Path(path)
    return np.loadtxt(str(path), delimiter='\t', skiprows=1)


if __name__ == '__main__':
    parser = argparse.ArgumentParser()

    parser.add_argument('--tsvpath',
                        type=str,
                        required=True,
                        help='evaluation tsv filepath')

    args = parser.parse_args()

    results = load_data(args.tsvpath)

    best_mean_iteration = np.argmax(results[:, 1])
    best_median_iteration = np.argmax(results[:, 5])

    print('best mean_iteration {}, score: mean: {}+/-{}, median: {}'.format(int(results[best_mean_iteration][0]),
                                                                            results[best_mean_iteration][1],
                                                                            results[best_mean_iteration][2],
                                                                            results[best_mean_iteration][5]))
    print('best median_iteration {}, score: mean: {}+/-{}, median: {}'.format(int(results[best_median_iteration][0]),
                                                                              results[best_median_iteration][1],
                                                                              results[best_median_iteration][2],
                                                                              results[best_median_iteration][5]))
