def artifactify(dataset, window_size, randomly_add_artifacts=True):
        dataset = dataset.clone()

        artifact_list = []

        artifact_dataset = DataSet()
        spike_size = (window_size // 4) * 3

        for window in ExperimentorService.windows(dataset, window_size):
            artificer = Artificer(window)
            if randomly_add_artifacts:
                decision = random.randrange(0, 10)
                if decision==1:
                    artifact_window = artificer.add_artifacts(spike_size)
                    artifact_list += [1]
                else:
                    artifact_window = window
                    artifact_list += [0]
            else:
                artifact_window = artificer.add_artifacts(spike_size)
                artifact_list += [1]
            artifact_dataset += artifact_window

        if len(dataset) > len(artifact_dataset):
            amount_missing = len(dataset) - len(artifact_dataset)

            return DataSet(artifact_dataset + dataset[-amount_missing:]), artifact_list

        return artifact_dataset, artifact_list
    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
    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'