def test_max_sensitivity_specificity(self):
        filename = '../../data/emotiv/EEG_Data_filtered.csv'
        dataset = DataReader.read_data(filename, ',')

        training_set, test_set = ExperimentorService.split_dataset(dataset, ratio=0.1)

        artifact_size = 20
        window_size = 20

        threshold_max, threshold_avg, threshold_avg_max = ExperimentorService.calibrate(training_set, window_size)

        artifact_test_set, artifact_list = ExperimentorService.artifactify(test_set, artifact_size, randomly_add_artifacts=True)

        reconstructed_test_set_max, rejections_max = ExperimentorService.pca_reconstruction(artifact_test_set, window_size, threshold_max)
        reconstructed_test_set_avg, rejections_avg = ExperimentorService.pca_reconstruction(artifact_test_set, window_size, threshold_avg)
        reconstructed_test_set_avg_max, rejections_max_avg = ExperimentorService.pca_reconstruction(artifact_test_set, window_size, threshold_avg_max)

        sensitivity_max, specificity_max = ExperimentorService.sensitivity_specificity(rejections_max, artifact_list)
        sensitivity_avg, specificity_avg = ExperimentorService.sensitivity_specificity(rejections_avg, artifact_list)
        sensitivity_avg_max, specificity_avg_max = ExperimentorService.sensitivity_specificity(rejections_max_avg, artifact_list)

        print '--- MAX THRESHOLD ---'
        print 'Sensitivity: ', sensitivity_max
        print 'Specificity: ', specificity_max
        print '--- AVG THRESHOLD ---'
        print 'Sensitivity: ', sensitivity_avg
        print 'Specificity: ', specificity_avg
        print '--- AVG_MAX THRESHOLD ---'
        print 'Sensitivity: ', sensitivity_avg_max
        print 'Specificity: ', specificity_avg_max
    def test_visualize_mse_bars_difference(self):
        filename = '../../data/emotiv/EEG_Data_filtered.csv'
        dataset = DataReader.read_data(filename, ',')

        training_set, test_set = ExperimentorService.split_dataset(dataset, ratio=0.2)

        artifact_size = 60

        artifact_dataset, _ = ExperimentorService.artifactify(test_set, artifact_size, True)

        window_sizes = range(5, 151, 5)
        Visualizer.visualize_cross_validation_bars_percentage(training_set, test_set, artifact_dataset, window_sizes, name="figure_cross_validation_bars_difference_no_artifacts")
    def test_visualize_mse_bars(self):
        filename = '../../data/emotiv/EEG_Data_filtered.csv'
        dataset = DataReader.read_data(filename, ',')

        training_set, test_set = ExperimentorService.split_dataset(dataset, ratio=0.2)

        artifact_size = 20

        artifact_dataset, _ = ExperimentorService.artifactify(test_set, artifact_size, randomly_add_artifacts=True)

        window_sizes = range(10, 301, 5)
        Visualizer.visualize_cross_validation_bars(training_set, test_set, artifact_dataset, window_sizes, name="figure_cross_validation_bars2")
    def test_visualize_mse_bars_no_artifacts(self):
        filename = '../../data/emotiv/EEG_Data_filtered.csv'
        dataset = DataReader.read_data(filename, ',')

        training_set, test_set = ExperimentorService.split_dataset(dataset, ratio=0.2)

        window_size = 40

        threshold_max, threshold_avg, threshold_avg_max = ExperimentorService.calibrate(training_set, window_size)

        thresholds = [threshold_max, threshold_avg, threshold_avg_max]
        window_sizes = [10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100]
        Visualizer.visualize_cross_validation_bars(test_set, test_set, thresholds, window_sizes, name="figure_cross_validation_bars_no_artifacts")
    def test_knn_threshold(self):

        # filename = '../../data/subject1_csv/eeg_200605191428_epochs/small.csv'
        filename = '../../data/emotiv/EEG_Data_filtered.csv'

        dataset = DataReader.read_data(filename, ',')
        dataset_slice = DataSet(dataset[0:40])

        artificer = Artificer(dataset_slice, add_artifacts=True)

        threshold = artificer.knn_threshold()

        artificer.pca_reconstruction(threshold)
        artificer.visualize()
    def test_visualize_mse_curves(self):
        """
        :return:
        """

        filename = '../../data/emotiv/EEG_Data_filtered.csv'
        dataset = DataReader.read_data(filename, ',')

        training_set, test_set = ExperimentorService.split_dataset(dataset, ratio=0.2)

        artifact_size = 60

        artifact_dataset, _ = ExperimentorService.artifactify(test_set, artifact_size, randomly_add_artifacts=True)

        window_sizes = range(5, 401, 1)
        Visualizer.visualize_cross_validation_curves(training_set, test_set, artifact_dataset, window_sizes, "figure_cross_validation_curves")
    def test_pca(self):

        # filename = '../../data/subject1_csv/eeg_200605191428_epochs/small.csv'
        filename = '../../data/emotiv/EEG_Data_filtered.csv'

        dataset = DataReader.read_data(filename, ',')

        threshold = 0
        for idx in range(len(dataset) // 40):
            current_dataset = DataSet(dataset[idx * 40:(idx + 1) * 40])

            if idx < 10:
                artificer = Artificer(current_dataset, add_artifacts=False)
                max_eigenvalue, rejected = artificer.pca_reconstruction()
                threshold = max(threshold, max_eigenvalue)
            else:
                artificer = Artificer(current_dataset, add_artifacts=True)
                artificer.pca_reconstruction(threshold)
                artificer.visualize()
                break
    def test_for_report(self):
        filename = '../../data/emotiv/EEG_Data_filtered.csv'
        dataset = DataReader.read_data(filename, ',')

        training_set, test_set = ExperimentorService.split_dataset(dataset, ratio=0.2)

        artifact_size = 20
        window_size = 20

        threshold_max, threshold_avg, threshold_avg_max = ExperimentorService.calibrate(training_set, window_size)

        print 'max: ' + str(threshold_max)
        print 'avg: ' + str(threshold_avg)
        print 'avg_max: ' + str(threshold_avg_max)

        artifact_dataset, _ = ExperimentorService.artifactify(test_set, artifact_size, True)

        reconstructed_dataset_max, rejections = ExperimentorService.pca_reconstruction(artifact_dataset, window_size, threshold_max)

        Visualizer.visualize_timeLine(dataset, test_set, artifact_dataset, reconstructed_dataset_max)
    def test_pca_speed(self):

        filename = '../../data/subject1_csv/eeg_200605191428_epochs/eeg_200605191428_epochs.csv'
        dataset = DataReader.read_data(filename, ',')

        print 'Length of dataset: ' + str(len(dataset))
        print 'Number of windows: ' + str(len(dataset) // 40)

        print 'Calculating thresholds...'

        max_threshold = 0
        avg_threshold = 0
        for idx in range(25):
            current_dataset = DataSet(dataset[idx * 40:(idx + 1) * 40])

            artificer = Artificer(current_dataset, add_artifacts=False)
            avg_eigenvalue, max_eigenvalue, rejected = artificer.pca_reconstruction()
            max_threshold = max(max_threshold, max_eigenvalue)
            avg_threshold = max(avg_threshold, avg_eigenvalue)

        print 'Largest threshold: ' + str(max_threshold)
        print 'Average threshold: ' + str(avg_threshold)

        print '\nTiming PCA reconstructor...'

        times = []
        for idx in range(len(dataset) // 40):
            current_dataset = DataSet(dataset[idx * 40:(idx + 1) * 40])

            start_time_constructor = time.time()
            artificer = Artificer(current_dataset, add_artifacts=False)
            midway_time = time.time() - start_time_constructor

            artificer.put_artifacts()

            start_time_reconstructor = time.time()
            artificer.pca_reconstruction(max_threshold)
            times.append(time.time() - start_time_reconstructor + midway_time)

        print 'Time per PCA reconstruction: ' + str(reduce(lambda x, y: x + y, times) / len(times)) + ' sec'
    def test_compare_mse(self):
        filename = '../../data/emotiv/EEG_Data_filtered.csv'
        dataset = DataReader.read_data(filename, ',')

        training_set, test_set = ExperimentorService.split_dataset(dataset, ratio=0.2)

        artifact_size = 20
        window_size = 40

        threshold_max, threshold_avg, threshold_avg_max = ExperimentorService.calibrate(training_set, window_size)

        print threshold_max
        print threshold_avg
        print threshold_avg_max

        artifact_dataset, _ = ExperimentorService.artifactify(test_set, artifact_size, True)

        reconstructed_dataset_avg, rejections = ExperimentorService.pca_reconstruction(artifact_dataset, window_size, threshold_avg)
        reconstructed_dataset_max, rejections = ExperimentorService.pca_reconstruction(artifact_dataset, window_size, threshold_max)
        reconstructed_dataset_avg_max, rejections = ExperimentorService.pca_reconstruction(artifact_dataset, window_size, threshold_avg_max)

        Visualizer.visualize_mse_on_same(test_set, reconstructed_dataset_max, reconstructed_dataset_avg, reconstructed_dataset_avg_max, window_size)
    def test_speed(self):
        filename = '../../data/emotiv/EEG_Data_filtered.csv'
        dataset = DataReader.read_data(filename, ',')

        training_set, test_set = ExperimentorService.split_dataset(dataset, ratio=0.2)

        artifact_size = 20
        window_size = 20

        threshold_max, threshold_avg, threshold_avg_max = ExperimentorService.calibrate(training_set, window_size)

        artifact_dataset, _ = ExperimentorService.artifactify(test_set, artifact_size, randomly_add_artifacts=False)

        start_time_max = time.time()
        reconstructed_dataset_max, _ = ExperimentorService.pca_reconstruction(artifact_dataset, window_size, threshold_max)
        end_time_max = time.time() - start_time_max

        # reconstructed_dataset_avg, _ = ExperimentorService.pca_reconstruction(artifact_dataset, window_size, threshold_avg)
        # reconstructed_dataset_avg_max, _ = ExperimentorService.pca_reconstr

        print 'We were able to reconstruct the entire test set in ' + str(end_time_max) + ' seconds.'
        print 'There are ' + str(len(ExperimentorService.windows(test_set, window_size))) + ' windows in the test set.'
        print 'On average, we can reconstruct a window in ' + str(end_time_max / len(ExperimentorService.windows(test_set, window_size))) + ' seconds.'
        print 'We can do pca projection at a rate of ' + str(1/(end_time_max / len(ExperimentorService.windows(test_set, window_size)))) + 'Hz'
    def test_equals(self):
        filename = '../../data/emotiv/EEG_Data_filtered.csv'
        dataset1 = DataReader.read_data(filename, ',')
        dataset2 = DataReader.read_data(filename, ',')

        print dataset1 == dataset2