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_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_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 pca_reconstruction(dataset, window_size, threshold):
        dataset = dataset.clone()

        reconstructed_dataset = DataSet()
        rejections = []

        for window in ExperimentorService.windows(dataset, window_size):
            artificer = Artificer(window)
            reconstructed_window, _, _, rejected = artificer.pca_reconstruction(threshold=threshold)
            reconstructed_dataset += reconstructed_window
            rejections += [rejected]

        return reconstructed_dataset, rejections
    def calibrate(dataset, window_size=40):
        dataset = dataset.clone()

        threshold_max = 0
        threshold_avg = 0
        threshold_avg_max = []
        for window in ExperimentorService.windows(dataset, window_size):

            artificer = Artificer(window)
            _, avg_eigenvalue, max_eigenvalue, _ = artificer.pca_reconstruction()

            threshold_max = max(threshold_max, max_eigenvalue)
            threshold_avg = max(threshold_avg, avg_eigenvalue)
            threshold_avg_max += [max_eigenvalue]

        threshold_avg_max = np.mean(threshold_avg_max)

        return threshold_max, threshold_avg, threshold_avg_max