def test_calculate_signed_r_square_swapaxes(self): """caluclate_r_square must work with nonstandard classaxis.""" dat = calculate_signed_r_square(swapaxes(self.dat, 0, 2), classaxis=2) # the class-axis just dissapears during # calculate_signed_r_square, so axis 2 becomes axis 1 dat = dat.swapaxes(0, 1) dat2 = calculate_signed_r_square(self.dat) np.testing.assert_array_equal(dat, dat2)
def test_calculate_signed_r_square_swapaxes(self): """caluclate_r_square must work with nonstandard classaxis.""" dat = calculate_signed_r_square(swapaxes(self.dat, 0, 2), classaxis=2) # the class-axis just dissapears during # calculate_signed_r_square, so axis 2 becomes axis 1 dat = dat.swapaxes(0, 1) dat2 = calculate_signed_r_square(self.dat) np.testing.assert_array_equal(dat, dat2)
def test_calculate_signed_r_square_swapaxes(self): """caluclate_r_square must work with nonstandard classaxis.""" dat = calculate_signed_r_square(swapaxes(self.dat, 0, 2), classaxis=2) # the class-axis just dissapears during # calculate_signed_r_square, so axis 2 becomes axis 1 dat = dat.swapaxes(0, 1) dat2 = calculate_signed_r_square(self.dat) # this used to work with numpy 1.8, but with 1.9 the arrays # differ slightly after e-15, I don't see why this change # happened, but it is barely noticeable wo we check for almost # equality # np.testing.assert_array_equal(dat, dat2) np.testing.assert_array_almost_equal(dat, dat2)
def test_calculate_signed_r_square_swapaxes(self): """caluclate_r_square must work with nonstandard classaxis.""" dat = calculate_signed_r_square(swapaxes(self.dat, 0, 2), classaxis=2) # the class-axis just dissapears during # calculate_signed_r_square, so axis 2 becomes axis 1 dat = dat.swapaxes(0, 1) dat2 = calculate_signed_r_square(self.dat) # this used to work with numpy 1.8, but with 1.9 the arrays # differ slightly after e-15, I don't see why this change # happened, but it is barely noticeable wo we check for almost # equality # np.testing.assert_array_equal(dat, dat2) np.testing.assert_array_almost_equal(dat, dat2)
def plot_spatio_temporal_r2_values(dat): """Calculate the signed r^2 values and plot them in a heatmap. Parameters ---------- dat : Data epoched data """ r2 = proc.calculate_signed_r_square(dat) max = np.max(np.abs(r2)) plt.imshow(r2.T, aspect='auto', interpolation='None', vmin=-max, vmax=max, cmap='RdBu') ax = plt.gca() # TODO: sort front-back, left-right # use the locators to fine-tune the ticks #ax.yaxis.set_major_locator(ticker.MaxNLocator()) #ax.xaxis.set_major_locator(ticker.MaxNLocator()) ax.yaxis.set_major_formatter(ticker.IndexFormatter(dat.axes[-1])) ax.xaxis.set_major_formatter( ticker.IndexFormatter(['%.1f' % i for i in dat.axes[-2]])) plt.xlabel('%s [%s]' % (dat.names[-2], dat.units[-2])) plt.ylabel('%s [%s]' % (dat.names[-1], dat.units[-1])) plt.tight_layout(True) plt.colorbar() plt.grid(True)
def plot_spatio_temporal_r2_values(dat): """Calculate the signed r^2 values and plot them in a heatmap. Parameters ---------- dat : Data epoched data """ r2 = proc.calculate_signed_r_square(dat) r2 *= -1 max = np.max(np.abs(r2)) plt.imshow(r2.T, aspect='auto', interpolation='None', vmin=-max, vmax=max, cmap='RdBu') ax = plt.gca() # TODO: sort front-back, left-right # use the locators to fine-tune the ticks mask = [True if chan.endswith('z') else False for chan in dat.axes[-1]] ax.yaxis.set_major_locator(ticker.FixedLocator(np.nonzero(mask)[0])) ax.yaxis.set_major_formatter(ticker.IndexFormatter(dat.axes[-1])) ax.xaxis.set_major_locator(ticker.MultipleLocator( np.max(dat.axes[-2]) // 100)) ax.xaxis.set_major_formatter(ticker.IndexFormatter(['%.1f' % i for i in dat.axes[-2]])) plt.xlabel('%s [%s]' % (dat.names[-2], dat.units[-2])) plt.ylabel('%s [%s]' % (dat.names[-1], dat.units[-1])) plt.tight_layout(True) plt.colorbar() plt.grid(True)
def test_calculate_signed_r_square(self): """Calculating signed r**2.""" dat = calculate_signed_r_square(self.dat) self.assertEqual(dat.ndim + 1, self.dat.data.ndim) # average over channels (one could also take just one channel) dat = dat.mean(axis=1) # check the intervals self.assertTrue(all(dat[0:20] < .2)) self.assertTrue(all(dat[20:40] > .5)) self.assertTrue(all(dat[40:60] < .2)) self.assertTrue(all(dat[60:80] < .5)) self.assertTrue(all(dat[80:100] < .2))
def test_calculate_signed_r_square(self): """Calculating signed r**2.""" dat = calculate_signed_r_square(self.dat) self.assertEqual(dat.ndim + 1, self.dat.data.ndim) # average over channels (one could also take just one channel) dat = dat.mean(axis=1) # check the intervals self.assertTrue(all(dat[0:20] < .2)) self.assertTrue(all(dat[20:40] > .5)) self.assertTrue(all(dat[40:60] < .2)) self.assertTrue(all(dat[60:80] < .5)) self.assertTrue(all(dat[80:100] < .2))
def test_calculate_signed_r_square_copy(self): """caluclate_r_square must not modify argument.""" cpy = self.dat.copy() calculate_signed_r_square(self.dat) self.assertEqual(self.dat, cpy)
def test_calculate_signed_r_square_with_cnt(self): """Select epochs must raise an exception if called with cnt argument.""" del(self.dat.class_names) with self.assertRaises(AssertionError): calculate_signed_r_square(self.dat)
def test_calculate_signed_r_square_min_max(self): """Min and max values must be in [-1, 1].""" dat = calculate_signed_r_square(self.dat) self.assertTrue(-1 <= np.min(dat) <= 1) self.assertTrue(-1 <= np.max(dat) <= 1)
def test_calculate_signed_r_square_copy(self): """caluclate_r_square must not modify argument.""" cpy = self.dat.copy() calculate_signed_r_square(self.dat) self.assertEqual(self.dat, cpy)
def test_calculate_signed_r_square_with_cnt(self): """Select epochs must raise an exception if called with cnt argument.""" del (self.dat.class_names) with self.assertRaises(AssertionError): calculate_signed_r_square(self.dat)
def test_calculate_signed_r_square_min_max(self): """Min and max values must be in [-1, 1].""" dat = calculate_signed_r_square(self.dat) self.assertTrue(-1 <= np.min(dat) <= 1) self.assertTrue(-1 <= np.max(dat) <= 1)