# Path: msaf/exceptions.py
# class NoEstimationsError(MSAFError):
#     '''Exception class for missing estimations'''
#     pass
# 
# class NoReferencesError(MSAFError):
#     '''Exception class for trying evaluations without references'''
#     pass
# 
# Path: msaf/input_output.py
# class FileStruct:
#     def __init__(self, audio_file):
#         """Creates the entire file structure given the audio file."""
#         self.ds_path = os.path.dirname(os.path.dirname(audio_file))
#         self.audio_file = audio_file
#         self.est_file = self._get_dataset_file(ds_config.estimations_dir,
#                                                ds_config.estimations_ext)
#         self.features_file = self._get_dataset_file(ds_config.features_dir,
#                                                     ds_config.features_ext)
#         self.ref_file = self._get_dataset_file(ds_config.references_dir,
#                                                ds_config.references_ext)
# 
#     def _get_dataset_file(self, dir, ext):
#         """Gets the desired dataset file."""
#         audio_file_ext = "." + self.audio_file.split(".")[-1]
#         base_file = os.path.basename(self.audio_file).replace(
#             audio_file_ext, ext)
#         return os.path.join(self.ds_path, dir, base_file)
# 
#     def __repr__(self):
#         """Prints the file structure."""
#         return "FileStruct(\n\tds_path=%s,\n\taudio_file=%s,\n\test_file=%s," \
#             "\n\tfeatures_file=%s,\n\tref_file=%s\n)" % (
#                 self.ds_path, self.audio_file, self.est_file,
#                 self.features_file, self.ref_file)


from nose.tools import raises
from types import ModuleType
from msaf.exceptions import NoEstimationsError, NoReferencesError
from msaf.input_output import FileStruct
import numpy.testing as npt
import numpy as np
import os
import pandas as pd
import shutil
import msaf
import msaf.eval as E
def test_print_results():
    # Empty results shouldn't crash
    E.print_results([])

    results = [
        {"HitRate_3F": 0.5, "HitRate_3P": 0.5, "HitRate_3R": 0.5},
        {"HitRate_3F": 0.32, "HitRate_3P": 0.8, "HitRate_3R": 0.2}]
    E.print_results(pd.DataFrame(results))


def test_compute_gt_results():
    def __compute_gt_results(est_file, ref_file, boundaries_id,
                             labels_id, config):
        res = E.compute_gt_results(est_file, ref_file, boundaries_id,
                                   labels_id, config)
        assert "HitRate_3F" in res.keys()
        assert "HitRate_3P" in res.keys()
        assert "HitRate_3R" in res.keys()

    def __compute_gt_results_hier(est_file, ref_file, boundaries_id,
                                  labels_id, config):
        res = E.compute_gt_results(est_file, ref_file, boundaries_id,
                                   labels_id, config)
        assert "t_measure10" in res.keys()
        assert "t_measure15" in res.keys()
        assert "t_precision10" in res.keys()
        assert "t_precision15" in res.keys()
        assert "t_recall10" in res.keys()
        assert "t_recall15" in res.keys()


# Path: msaf/exceptions.py
# class NoEstimationsError(MSAFError):
#     '''Exception class for missing estimations'''
#     pass
# 
# class NoReferencesError(MSAFError):
#     '''Exception class for trying evaluations without references'''
#     pass
# 
# Path: msaf/input_output.py
# class FileStruct:
#     def __init__(self, audio_file):
#         """Creates the entire file structure given the audio file."""
#         self.ds_path = os.path.dirname(os.path.dirname(audio_file))
#         self.audio_file = audio_file
#         self.est_file = self._get_dataset_file(ds_config.estimations_dir,
#                                                ds_config.estimations_ext)
#         self.features_file = self._get_dataset_file(ds_config.features_dir,
#                                                     ds_config.features_ext)
#         self.ref_file = self._get_dataset_file(ds_config.references_dir,
#                                                ds_config.references_ext)
# 
#     def _get_dataset_file(self, dir, ext):
#         """Gets the desired dataset file."""
#         audio_file_ext = "." + self.audio_file.split(".")[-1]
#         base_file = os.path.basename(self.audio_file).replace(
#             audio_file_ext, ext)
#         return os.path.join(self.ds_path, dir, base_file)
# 
#     def __repr__(self):
#         """Prints the file structure."""
#         return "FileStruct(\n\tds_path=%s,\n\taudio_file=%s,\n\test_file=%s," \
#             "\n\tfeatures_file=%s,\n\tref_file=%s\n)" % (
#                 self.ds_path, self.audio_file, self.est_file,
#                 self.features_file, self.ref_file)


# Path: tests/test_eval.py
from nose.tools import raises
from types import ModuleType
from msaf.exceptions import NoEstimationsError, NoReferencesError
from msaf.input_output import FileStruct
import numpy.testing as npt
import numpy as np
import os
import pandas as pd
import shutil
import msaf
import msaf.eval as E

def test_print_results():
    # Empty results shouldn't crash
    E.print_results([])

    results = [
        {"HitRate_3F": 0.5, "HitRate_3P": 0.5, "HitRate_3R": 0.5},
        {"HitRate_3F": 0.32, "HitRate_3P": 0.8, "HitRate_3R": 0.2}]
    E.print_results(pd.DataFrame(results))


def test_compute_gt_results():
    def __compute_gt_results(est_file, ref_file, boundaries_id,
                             labels_id, config):
        res = E.compute_gt_results(est_file, ref_file, boundaries_id,
                                   labels_id, config)
        assert "HitRate_3F" in res.keys()
        assert "HitRate_3P" in res.keys()
        assert "HitRate_3R" in res.keys()

    def __compute_gt_results_hier(est_file, ref_file, boundaries_id,
                                  labels_id, config):
        res = E.compute_gt_results(est_file, ref_file, boundaries_id,
                                   labels_id, config)
        assert "t_measure10" in res.keys()
        assert "t_measure15" in res.keys()
        assert "t_precision10" in res.keys()
        assert "t_precision15" in res.keys()
        assert "t_recall10" in res.keys()
        assert "t_recall15" in res.keys()


    @raises(NoEstimationsError)