def test_manual_class_selection(self):
     """Manual class indices selection must work."""
     w, a, d = calculate_csp(self.epo)
     w2, a2, d2 = calculate_csp(self.epo, [0, 1])
     np.testing.assert_array_equal(w, w2)
     np.testing.assert_array_equal(a, a2)
     np.testing.assert_array_equal(d, d2)
     w2, a2, d2 = calculate_csp(self.epo, [1, 0])
     np.testing.assert_array_almost_equal(np.abs(w), np.abs(w2[:, ::-1]))
     np.testing.assert_array_almost_equal(np.abs(a), np.abs(a2[:, ::-1]))
     np.testing.assert_array_almost_equal(np.abs(d), np.abs(d2[::-1]))
Example #2
0
def preprocess(data, filt=None):
    # copying data
    dat = data.copy()
    fs_n = dat.fs / 2.0

    # butter filtering low
    b, a = proc.signal.butter(5, [13 / fs_n], btype='low')
    #dat = proc.filtfilt(dat, b, a)

    # butter filtering high
    b, a = proc.signal.butter(4, [9 / fs_n], btype='high')
    dat = proc.filtfilt(dat, b, a)

    # subsampling
    #dat = proc.subsample(dat, 50)

    if filt is None:
        # calculate_csp
        filt, pattern, _ = proc.calculate_csp(dat)

        # plot_csp_pattern
        #plot_csp_pattern(pattern)

    # apply_csp
    dat = proc.apply_csp(dat, filt)

    # variance and logarithm
    dat = proc.variance(dat)
    #dat = proc.logarithm(dat)
    return dat, filt
Example #3
0
def apply_csp(data,
              return_as='filtered',
              time_axis=1,
              columns_to_apply=(0, 1, -2, -1)):
    """Calculates and applies CSP"""
    w, a, d = calculate_csp(data)
    if return_as == 'patterns':
        return w, a
    w = w[:, list(columns_to_apply)]
    filtered = apply_spatial_filter(data, w)
    if return_as == 'logvar':
        filtered.data = np.log(np.var(filtered.data, axis=time_axis))
        filtered.axes[1] = np.arange(filtered.data.shape[0])
        return filtered
    else:
        return filtered
Example #4
0
 def test_s(self):
     """Test if s_est is elementwise almost equal s."""
     W, A_est, d = calculate_csp(self.X[::2], self.X[1::2])
     # applying the filter to X gives us s_est which should be almost
     # equal s
     s_est = np.empty(self.s.shape)
     for i in range(self.EPOCHS):
         s_est[i] = np.dot(self.X[i], W[:, [0, -1]])
     # correct for scaling, and sign
     self.s = self.s.reshape(-1, self.SOURCES)
     s_est2 = s_est.reshape(-1, self.SOURCES)
     epsilon = 0.01
     for i in range(self.SOURCES):
         idx = np.argmax(np.abs(s_est2[:, i]))
         s_est2[:, i] /= s_est2[idx, i]
         idx = np.argmax(np.abs(self.s[:, i]))
         self.s[:, i] /= self.s[idx, i]
         diff = np.sum(np.abs(self.s[:, i] - s_est2[:, i])) / self.s.shape[0]
         self.assertTrue(diff < epsilon)
Example #5
0
 def test_A(self):
     """Test if A_est is elementwise almost equal A."""
     W, A_est, d = calculate_csp(self.X[::2], self.X[1::2])
     # A and A_est can have a different scaling, after normalizing
     # and correcting for sign, they should be almost equal
     # normalize (we're only interested in the first and last column)
     for i in 0, -1:
         idx = np.argmax(np.abs(A_est[:, i]))
         A_est[:, i] /= A_est[idx, i]
         idx = np.argmax(np.abs(self.A[:, i]))
         self.A[:, i] /= self.A[idx, i]
     # for i in 0, -1:
     #   check elementwise if A[:, i] almost A_est[:, i]
     epsilon = 0.01
     for i in 0, -1:
         diff = self.A[:, i] - A_est[:, i]
         diff = np.abs(diff)
         diff = np.sum(diff) / self.A.shape[0]
         self.assertTrue(diff < epsilon)
 def test_s(self):
     """Test if s_est is elementwise almost equal s."""
     W, A_est, d = calculate_csp(self.epo)
     # applying the filter to X gives us s_est which should be almost
     # equal s
     s_est = np.empty(self.s.shape)
     for i in range(self.EPOCHS):
         s_est[i] = np.dot(self.X[i], W[:, [0, -1]])
     # correct for scaling, and sign
     self.s = self.s.reshape(-1, self.SOURCES)
     s_est2 = s_est.reshape(-1, self.SOURCES)
     epsilon = 0.01
     for i in range(self.SOURCES):
         idx = np.argmax(np.abs(s_est2[:, i]))
         s_est2[:, i] /= s_est2[idx, i]
         idx = np.argmax(np.abs(self.s[:, i]))
         self.s[:, i] /= self.s[idx, i]
         diff = np.sum(np.abs(self.s[:, i] - s_est2[:, i])) / self.s.shape[0]
         self.assertTrue(diff < epsilon)
 def test_A(self):
     """Test if A_est is elementwise almost equal A."""
     W, A_est, d = calculate_csp(self.epo)
     # A and A_est can have a different scaling, after normalizing
     # and correcting for sign, they should be almost equal
     # normalize (we're only interested in the first and last column)
     for i in 0, -1:
         idx = np.argmax(np.abs(A_est[:, i]))
         A_est[:, i] /= A_est[idx, i]
         idx = np.argmax(np.abs(self.A[:, i]))
         self.A[:, i] /= self.A[idx, i]
     # for i in 0, -1:
     #   check elementwise if A[:, i] almost A_est[:, i]
     epsilon = 0.01
     for i in 0, -1:
         diff = self.A[:, i] - A_est[:, i]
         diff = np.abs(diff)
         diff = np.sum(diff) / self.A.shape[0]
         self.assertTrue(diff < epsilon)
def preprocess(data, filt=None):
    dat = data.copy()
    fs_n = 250 # sample rate is 250 for us

    b, a = proc.signal.butter(5, [13 / fs_n], btype='low')
    dat = proc.filtfilt(dat, b, a)

    b, a = proc.signal.butter(5, [9 / fs_n], btype='high')
    dat = proc.filtfilt(dat, b, a)

    dat = proc.subsample(dat, 50)

    if filt is None:
        filt, pattern, _ = proc.calculate_csp(dat)
        plot_csp_pattern(pattern)
    dat = proc.apply_csp(dat, filt)

    dat = proc.variance(dat)
    dat = proc.logarithm(dat)
    return dat, filt
def preprocess(data, filt=None):
    dat = data.copy()
    fs_n = dat.fs / 2

    b, a = proc.signal.butter(5, [13 / fs_n], btype='low')
    dat = proc.filtfilt(dat, b, a)


    b, a = proc.signal.butter(5, [9 / fs_n], btype='high')
    dat = proc.filtfilt(dat, b, a)
    
    dat = proc.subsample(dat, 50)

    if filt is None:
        filt, pattern, _ = proc.calculate_csp(dat)
        plot_csp_pattern(pattern)
    dat = proc.apply_csp(dat, filt)
    
    dat = proc.variance(dat)
    dat = proc.logarithm(dat)
    return dat, filt
Example #10
0
def preprocess(dat,filt=None):
    	'''fs_n = dat.fs / 2
    	b, a = proc.signal.butter(5, [13 / fs_n], btype='low')
	dat = proc.lfilter(dat, b, a)
	b, a = proc.signal.butter(5, [8 / fs_n], btype='high')
	dat = proc.lfilter(dat, b, a)
	print dat'''
   

        dat = proc.subsample(dat, 64)
        #epo = proc.segment_dat(dat, MRK_DEF, SEG_IVAL)
	
        #fv = proc.jumping_means(epo, JUMPING_MEANS_IVALS)
        #fv = proc.create_feature_vectors(dat)

        if filt is None:
        	filt, pattern, _ = proc.calculate_csp(dat)
        	#plot_csp_pattern(pattern)
    	dat = proc.apply_csp(dat, filt)
    
   	dat = proc.variance(dat)
    	dat = proc.logarithm(dat)
    	return dat, filt
Example #11
0
 def test_d(self):
     """Test if the first lambda is almost 1 and the last one almost -1."""
     W, A_est, d = calculate_csp(self.X[::2], self.X[1::2])
     epsilon = 0.1
     self.assertAlmostEqual(d[0], 1, delta=epsilon)
     self.assertAlmostEqual(d[-1], -1, delta=epsilon)
Example #12
0
    for x in range(len(epoch)):
        cA_Energy.append(abs(np.sum(np.square(cA_values[x]))))
        features.append(abs(np.sum(np.square(cA_values[x]))))

    for x in range(len(epoch)):
        cD_Energy.append(abs(np.sum(np.square(cD_values[x]))))
        features.append(abs(np.sum(np.square(cD_values[x]))))

    return features


final_epoch1 = append_epo(epoch_subject1_class1, epoch_subject1_class2)
final_epoch_ch1 = append_epo(
    epoch_subject1_ch1_class1,
    epoch_subject1_ch1_class2)  #appended both the epoch data sets
w1, a1, d1 = calculate_csp(final_epoch1)
w2, a2, d2 = calculate_csp(
    final_epoch_ch1
)  #calculate csp   but why we need to append the data and calculate the csp paramters waht if we calculate it individually
fil_epoch_subject1_class1 = apply_csp(
    epoch_subject1_class1, w1,
    [0, 1, 2, 3, 4, -5, -4, -3, -2, -1
     ])  # brackets number are the column number to use
fil_epoch_subject1_class2 = apply_csp(epoch_subject1_class2, w1,
                                      [0, 1, 2, 3, 4, -5, -4, -3, -2, -1])
fil_final_epoch1 = append_epo(
    fil_epoch_subject1_class1,
    fil_epoch_subject1_class2)  # final filtered epo     class*time*channel
fil_epoch_subject1_ch1_class1 = apply_csp(
    epoch_subject1_ch1_class1, w2,
    [0, 1, 2, 3, 4, -5, -4, -3, -2, -1
 def test_d(self):
     """Test if the first lambda is almost 1 and the last one almost -1."""
     W, A_est, d = calculate_csp(self.epo)
     epsilon = 0.1
     self.assertAlmostEqual(d[0], 1, delta=epsilon)
     self.assertAlmostEqual(d[-1], -1, delta=epsilon)
 def test_calculate_csp_copy(self):
     """caluclate_csp must not modify argument."""
     cpy = self.epo.copy()
     calculate_csp(self.epo)
     self.assertEqual(self.epo, cpy)
 def test_raise_error_with_automatic_classes(self):
     """Raise error if not enough classes in epo."""
     self.epo.axes[0][:] = 0
     with self.assertRaises(AssertionError):
         calculate_csp(self.epo)
 def test_raise_error_on_wrong_manual_classes(self):
     """Raise error if classes not in epo."""
     with self.assertRaises(AssertionError):
         calculate_csp(self.epo, [0, 2])
         calculate_csp(self.epo, [0, -1])