def test_calculate_classwise_average(self): """Calculate classwise average.""" avg_dat = calculate_classwise_average(self.dat) # check for two datches (one for each class) self.assertEqual(avg_dat.data.shape[0], 2) # check if the data is correct self.assertEqual(np.average(avg_dat.data[0]), 1) self.assertEqual(np.average(avg_dat.data[1]), 2) # check if we have as many classes on axes as we have in data self.assertEqual(avg_dat.data.shape[0], len(avg_dat.axes[0])) # self.assertEqual(avg_dat.class_names, self.dat.class_names)
def plot_channels(dat, figsize=(12, 12), ncols=8, chanaxis=-1, otheraxis=-2): """Plot all channels for a continuous or epo. In case of an epoched Data object, the classwise average is calculated, and for each channel the respective classes are plotted. Parameters ---------- dat : Data continous or epoched Data object ncols : int, optional the number of colums in the grid. The number of rows is calculated depending on ``ncols`` and the number of channels """ # test if epo is_epo = False if dat.data.ndim == 3: is_epo = True dat = proc.calculate_classwise_average(dat) ax = [] n_channels = dat.data.shape[chanaxis] nrows = int(np.ceil(n_channels / ncols)) f, ax = plt.subplots(nrows=nrows, ncols=ncols, squeeze=False, sharex=True, sharey=True, figsize=figsize) for i, chan in enumerate(dat.axes[chanaxis]): a = ax[i // ncols, i % ncols] dat.axes[otheraxis], dat.data.take([i], chanaxis) if is_epo: for j, name in enumerate(dat.class_names): cnt = proc.select_classes(dat, [j]) a.plot(cnt.axes[otheraxis], cnt.data.take([i], chanaxis).squeeze(), label=name) else: a.plot(dat.axes[otheraxis], dat.data.take([i], chanaxis).squeeze()) a.set_title(chan) a.axvline(x=0, color='black') a.axhline(y=0, color='black') plt.legend()
def plot_channels(dat, ncols=8, chanaxis=-1, otheraxis=-2): """Plot all channels for a continuous or epo. In case of an epoched Data object, the classwise average is calculated, and for each channel the respective classes are plotted. Parameters ---------- dat : Data continous or epoched Data object ncols : int, optional the number of colums in the grid. The number of rows is calculated depending on ``ncols`` and the number of channels """ # test if epo is_epo = False if dat.data.ndim == 3: is_epo = True dat = proc.calculate_classwise_average(dat) ax = [] n_channels = dat.data.shape[chanaxis] nrows = int(np.ceil(n_channels / ncols)) f, ax = plt.subplots(nrows=nrows, ncols=ncols, sharex=True, sharey=True); for i, chan in enumerate(dat.axes[chanaxis]): a = ax[i // ncols, i % ncols] dat.axes[otheraxis], dat.data.take([i], chanaxis) if is_epo: for j, name in enumerate(dat.class_names): cnt = proc.select_classes(dat, [j]) a.plot(cnt.axes[otheraxis], cnt.data.take([i], chanaxis).squeeze(), label=name) else: a.plot(dat.axes[otheraxis], dat.data.take([i], chanaxis).squeeze()) a.set_title(chan) a.axvline(x=0, color='black') a.axhline(y=0, color='black') plt.legend()
def test_calculate_classwise_average_copy(self): """Calculate classwise avg must not modify the argument.""" cpy = self.dat.copy() calculate_classwise_average(self.dat) self.assertEqual(self.dat, cpy)
def test_calculate_classwise_average_with_cnt(self): """Calculate classwise avg must raise an error if called with continouos data.""" del (self.dat.class_names) with self.assertRaises(AssertionError): calculate_classwise_average(self.dat)
def test_calculate_classwise_average_with_cnt(self): """Calculate classwise avg must raise an error if called with continouos data.""" del(self.dat.class_names) with self.assertRaises(AssertionError): calculate_classwise_average(self.dat)