class TestBootstrapAnalysis(TestCase):

    def setUp(self):
        self.number_of_resamples = 10000

        # import dataset from file
        filename = 'flicker.xlsx'
        self.__fh = FileHandling()
        self.original_data_dict = self.__fh.import_spreadsheet(filename)

        # resample dataset
        self.__bootstrapper = Bootstrapper(self.original_data_dict, self.number_of_resamples)

        #init bootstrap analysis tools
        self.analysis = BootstrapAnalysis(self.__bootstrapper)

    def check_keys_in_dict(self, dict):
        key_count = 0
        key_list = []
        for key in dict:
            result_dict = dict[key]
            for key, values in result_dict.items():
                print(key)
                key_list.append(key)
                key_count+= 1

        return key_list, key_count

    def test_mean_dictionary_keys(self):
        """
        Tests if the correct number of keys (experiment column names) is contained in the result dict and
        if the contained keys are identical to the column names.
        """
        mean_dict = self.analysis.get_bootstrapped_mean()
        key_list, key_count = self.check_keys_in_dict(mean_dict)

        self.assertEqual(3, key_count)
        self.assertTrue(sorted(["Brown", "Green", "Blue"]) == sorted(key_list))

    def test_median_dictionary_keys(self):
        """
        Tests if the correct number of keys (experiment column names) is contained in the result dict and
        if the contained keys are identical to the column names.
        """
        median_dict = self.analysis.get_bootstrapped_median()
        key_list, key_count = self.check_keys_in_dict(median_dict)

        self.assertEqual(3, key_count)
        self.assertTrue(sorted(["Brown", "Green", "Blue"]) == sorted(key_list))

    def test_SEM(self):
        """
        Tests if the correct number of keys (experiment column names) is contained in the result dict and
        if the contained keys are identical to the column names.
        """
        sem_dict = self.analysis.get_bootstrapped_median()
        key_list, key_count = self.check_keys_in_dict(sem_dict)

        self.assertEqual(3, key_count)
        self.assertTrue(sorted(["Brown", "Green", "Blue"]) == sorted(key_list))
class TestBootstrapAnalysis(TestCase):
    def setUp(self):
        self.number_of_resamples = 10000

        # import dataset from file
        filename = 'flicker.xlsx'
        self.__fh = FileHandling()
        self.original_data_dict = self.__fh.import_spreadsheet(filename)

        # resample dataset
        self.__bootstrapper = Bootstrapper(self.original_data_dict,
                                           self.number_of_resamples)

        #init bootstrap analysis tools
        self.analysis = BootstrapAnalysis(self.__bootstrapper)

    def check_keys_in_dict(self, dict):
        key_count = 0
        key_list = []
        for key in dict:
            result_dict = dict[key]
            for key, values in result_dict.items():
                print(key)
                key_list.append(key)
                key_count += 1

        return key_list, key_count

    def test_mean_dictionary_keys(self):
        """
        Tests if the correct number of keys (experiment column names) is contained in the result dict and
        if the contained keys are identical to the column names.
        """
        mean_dict = self.analysis.get_bootstrapped_mean()
        key_list, key_count = self.check_keys_in_dict(mean_dict)

        self.assertEqual(3, key_count)
        self.assertTrue(sorted(["Brown", "Green", "Blue"]) == sorted(key_list))

    def test_median_dictionary_keys(self):
        """
        Tests if the correct number of keys (experiment column names) is contained in the result dict and
        if the contained keys are identical to the column names.
        """
        median_dict = self.analysis.get_bootstrapped_median()
        key_list, key_count = self.check_keys_in_dict(median_dict)

        self.assertEqual(3, key_count)
        self.assertTrue(sorted(["Brown", "Green", "Blue"]) == sorted(key_list))

    def test_SEM(self):
        """
        Tests if the correct number of keys (experiment column names) is contained in the result dict and
        if the contained keys are identical to the column names.
        """
        sem_dict = self.analysis.get_bootstrapped_median()
        key_list, key_count = self.check_keys_in_dict(sem_dict)

        self.assertEqual(3, key_count)
        self.assertTrue(sorted(["Brown", "Green", "Blue"]) == sorted(key_list))
    def setUp(self):
        self.number_of_resamples = 10000
        filename = 'flicker.xlsx'

        self.bootstrapper = None
        fh = FileHandling()
        self.data_dict = fh.import_spreadsheet(filename)
        self.bootstrapper = Bootstrapper(self.data_dict, self.number_of_resamples)
        self.bootstrapped_dict = self.bootstrapper.bootstrapped_data
    def setUp(self):
        self.number_of_resamples = 10000
        filename = 'flicker.xlsx'

        self.bootstrapper = None
        fh = FileHandling()
        self.data_dict = fh.import_spreadsheet(filename)
        self.bootstrapper = Bootstrapper(self.data_dict,
                                         self.number_of_resamples)
        self.bootstrapped_dict = self.bootstrapper.bootstrapped_data
class Bootstrapit:
    """
    The Bootstrapit class builds the API for the Bootstrapit application. The Methods inside this class can directly
    be used to interact with the application.
    """
    def __init__(self, filename, number_of_resamples=10000):

        """
        Initializing the Bootstrapit class executes the resampling of the imported dataset.
        :param filename: The filename including filepath to the import data file.
        :param number_of_resamples: The number of resamples to perform.
        """
        self.number_of_resamples = number_of_resamples

        # import dataset from file
        self.__fh = FileHandling()
        self.original_data_dict = self.__fh.import_spreadsheet(filename)

        # resample dataset
        self.__bootstrapper = Bootstrapper(self.original_data_dict, number_of_resamples)

        #init bootstrap analysis tools
        self.__analysis = BootstrapAnalysis(self.__bootstrapper)

        #init plotter
        self.__plotter = Plotting(self.__fh.export_order)

    # TODO: Export bootstrapped data array to json or excel
    def export(self, *export_datasets_dicts, filename="bootstrapit_results.xlsx"):

        """
        The export method does merge all data analysis result dictionaries and exports them using the FileHandler class.
        :param export_datasets_dicts: All result dictionaries from the bootstrapping analysis.
        :param filename: the export filename.
        """
        merged_dict = self.__merge_dicts(*export_datasets_dicts)

        filetype_check = filename.split('.')
        filetype = filetype_check[-1]
        filename = filetype_check[0]

        if filetype == "xlsx":
            self.__fh.file_type = FileType.XLSX
        elif filetype == "xls":
            self.__fh.file_type = FileType.XLS
        elif filetype == "csv":
            self.__fh.file_type = FileType.CSV
        else:
            print("Error: Unsupported file type.")

        self.__fh.file_name = filename
        self.__fh.export(merged_dict)

    def __merge_dicts(self, *dict_args):

        """
        Helper method to merge multiple result dictionaries.
        :param dict_args: List of dictionaries
        :return: The merged dictionary containing all results of the analysis.
        """
        result = {}
        for dictionary in dict_args:
            result.update(dictionary)
        return result

    def mean(self):
        return self.__analysis.get_bootstrapped_mean()

    def median(self):
        return self.__analysis.get_bootstrapped_median()

    def SEM(self):
        return self.__analysis.get_SEM()

    def barchart(self, figure, data_dict, errorbar = {}):
        return self.__plotter.plot_barchart(figure, data_dict, errorbar)


    def set_axis_label(self, axes, title, xlabel, ylabel):
        self.__plotter.set_axis_labels(axes, title, xlabel, ylabel)