def test_conditional_entropy_2D(self): xs = np.random.randint(0, 5, 20) ys = np.random.randint(0, 5, 20) expect = conditional_entropy(xs, ys) us = np.copy(np.reshape(xs, (4, 5))) vs = np.copy(np.reshape(ys, (4, 5))) got = conditional_entropy(us, vs) self.assertAlmostEqual(expect, got)
def test_conditional_entropy_2D(self): xs = np.random.randint(0, 5, 20) ys = np.random.randint(0, 5, 20) expect = conditional_entropy(xs, ys, local=True) self.assertEqual(xs.shape, expect.shape) us = np.copy(np.reshape(xs, (4, 5))) vs = np.copy(np.reshape(ys, (4, 5))) got = conditional_entropy(us, vs, local=True) self.assertTrue(us.shape, got.shape) self.assertTrue((expect == np.reshape(got, expect.shape)).all())
def test_conditional_entropy_empty(self): with self.assertRaises(ValueError): conditional_entropy([], [], local=True) with self.assertRaises(ValueError): conditional_entropy([1, 2, 3], [], local=True) with self.assertRaises(ValueError): conditional_entropy([], [1, 2, 3], local=True)
def calculate_CE(signal, savePath): ''' Calculate conditional entropy between each channels Parameters ---------- signal : np.ndarray (epoch, channel, time) Signal data savePath : str Path for saving the data Returns ------- CE : np.ndarray (epoch, features) Conditional entropy between each channels ''' assert isinstance(signal, np.ndarray) and signal.ndim == 3 assert isinstance(savePath, str) num_channels = signal.shape[1] channel_perm = list(permutations(range(num_channels), 2)) CE_all = np.zeros((signal.shape[0], len(channel_perm))) for i_perm, (i, j) in enumerate(channel_perm): for i_sample, sample in enumerate(signal): # Begin from 0 and round to integer signal1 = np.round(signal[i_sample, i, :] - np.min(signal[i_sample, i, :])) signal2 = np.round(signal[i_sample, j, :] - np.min(signal[i_sample, j, :])) CE = conditional_entropy(signal1, signal2) CE_all[i_sample, i_perm] = CE with open(savePath, 'wb') as fp: pickle.dump(CE_all, fp) return CE_all
def test_conditional_entropy(self): self.assertAlmostEqual(0.899985, conditional_entropy([0, 0, 1, 1, 1, 1, 0, 0, 0], [1, 0, 0, 1, 0, 0, 1, 0, 0]), places=6) self.assertAlmostEqual(0.972765, conditional_entropy([1, 0, 0, 1, 0, 0, 1, 0, 0], [0, 0, 1, 1, 1, 1, 0, 0, 0]), places=6) self.assertAlmostEqual(0.000000, conditional_entropy([0, 0, 0, 0, 1, 1, 1, 1], [1, 1, 1, 1, 0, 0, 0, 0]), places=6) self.assertAlmostEqual(0.000000, conditional_entropy([0, 0, 1, 1, 1, 1, 0, 0, 0], [1, 1, 0, 0, 0, 0, 1, 1, 1]), places=6) self.assertAlmostEqual(0.918296, conditional_entropy([1, 1, 0, 1, 0, 1, 1, 1, 0], [1, 1, 0, 0, 0, 1, 0, 1, 1]), places=6) self.assertAlmostEqual(0.918296, conditional_entropy([0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 0, 0, 0, 1, 1, 1]), places=6) self.assertAlmostEqual(0.845516, conditional_entropy([1, 1, 1, 1, 0, 0, 0, 0, 1], [1, 1, 1, 0, 0, 0, 1, 1, 1]), places=6) self.assertAlmostEqual(0.899985, conditional_entropy([1, 1, 0, 0, 1, 1, 0, 0, 1], [1, 1, 1, 0, 0, 0, 1, 1, 1]), places=6) self.assertAlmostEqual(0.000000, conditional_entropy([0, 1, 0, 1, 0, 1, 0, 1], [0, 2, 0, 2, 0, 2, 0, 2]), places=6) self.assertAlmostEqual(0.918296, conditional_entropy([0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1], [0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2]), places=6) self.assertAlmostEqual(0.444444, conditional_entropy([0, 0, 1, 1, 2, 1, 1, 0, 0], [0, 0, 0, 1, 1, 1, 0, 0, 0]), places=6) self.assertAlmostEqual(0.666667, conditional_entropy([0, 1, 0, 0, 1, 0, 0, 1, 0], [1, 0, 0, 1, 0, 0, 1, 0, 0]), places=6) self.assertAlmostEqual(0.606844, conditional_entropy([1, 0, 0, 1, 0, 0, 1, 0], [2, 0, 1, 2, 0, 1, 2, 0]), places=6)
def test_conditional_entropy_negative_states(self): with self.assertRaises(InformError): conditional_entropy([-1, 0, 0], [0, 0, 1]) with self.assertRaises(InformError): conditional_entropy([1, 0, 0], [0, 0, -1])
def test_conditional_entropy_size(self): with self.assertRaises(ValueError): conditional_entropy([1, 2, 3], [1, 2]) with self.assertRaises(ValueError): conditional_entropy([1, 2], [1, 2, 3])
def test_conditional_entropy_dimensions(self): with self.assertRaises(ValueError): conditional_entropy([[1]], [1]) with self.assertRaises(ValueError): conditional_entropy([1], [[1]])