Ejemplo n.º 1
0
 def test_correct_classlabels(self):
     """lda_train must throw an error if the class labels are not exactly [0, 1]."""
     data = np.random.random((50, 100))
     labels = np.zeros(50)
     # only 0s -> fail
     fv = Data(data=data,
               axes=[labels, np.arange(100)],
               units=['x', 'y'],
               names=['foo', 'bar'])
     with self.assertRaises(ValueError):
         lda_train(fv)
     # 0s and 1s -> ok
     labels[1] = 1
     fv = Data(data=data,
               axes=[labels, np.arange(100)],
               units=['x', 'y'],
               names=['foo', 'bar'])
     try:
         lda_train(fv)
     except ValueError:
         self.fail()
     # 0s, 1s, and 2s -> fail
     labels[2] = 2
     fv = Data(data=data,
               axes=[labels, np.arange(100)],
               units=['x', 'y'],
               names=['foo', 'bar'])
     with self.assertRaises(ValueError):
         lda_train(fv)
Ejemplo n.º 2
0
 def setUp(self):
     self.empty_dat = Data(np.array([]), [], [], [])
     self.dat_1 = Data(
         np.array([0, 0])[np.newaxis, :],
         [np.array([0]), np.array(['ch1', 'ch2'])], ['time', 'channel'],
         ['ms', '#'])
     self.dat_1.fs = 1000
     self.dat_1.markers = [[0, 'x']]
     self.dat_5 = reduce(append_cnt, [self.dat_1 for i in range(5)])
Ejemplo n.º 3
0
 def setUp(self):
     dat = np.zeros((SAMPLES, CHANS))
     # [-10, -9, ... 20)
     dat[:, 0] = np.arange(SAMPLES) - SAMPLES / 2
     channels = ['chan{i}'.format(i=i) for i in range(CHANS)]
     time = np.arange(SAMPLES)
     self.cnt = Data(dat, [time, channels], ['time', 'channels'],
                     ['ms', '#'])
     # construct epo
     epo_dat = np.array([dat + i for i in range(EPOS)])
     classes = ['class{i}'.format(i=i) for i in range(EPOS)]
     self.epo = Data(epo_dat, [classes, time, channels],
                     ['class', 'time', 'channels'], ['#', 'ms', '#'])
    def setUp(self):
        data = np.random.randn(SAMPLES, CHANS)
        data[:, 1] += 0.5 * data[:, 0]
        data[:, 2] -= 0.5 * data[:, 0]
        t = np.arange(SAMPLES)
        chans = ['chan{i}'.format(i=i) for i in range(CHANS)]
        self.cnt = Data(data, [t, chans], ['time', 'channels'], ['ms', '#'])

        # construct epo
        epo_dat = np.array([data for i in range(EPOS)])
        classes = ['class{i}'.format(i=i) for i in range(EPOS)]
        self.epo = Data(epo_dat, [classes, t, chans],
                        ['class', 'time', 'channels'], ['#', 'ms', '#'])

        # my little spatial filter
        self.w = np.array([[0, 0.5, 1], [-1, 0.5, 0], [1, 0.5, 0]])
 def setUp(self):
     data = np.random.randn(SAMPLES, CHANS)
     data[:, 1] += 0.5 * data[:, 0]
     data[:, 2] -= 0.5 * data[:, 0]
     t = np.arange(SAMPLES)
     chans = ['chan{i}'.format(i=i) for i in range(CHANS)]
     self.cnt = Data(data, [t, chans], ['time', 'channels'], ['ms', '#'])
Ejemplo n.º 6
0
 def setUp(self):
     # generate sources with independent variance modulations, the
     # first source will be the target source
     z = np.abs(np.random.randn(self.EPOCHS, self.SOURCES))
     for i in range(self.SOURCES):
         z[:, i] /= z[:, i].std()
     self.s = np.random.randn(self.EPOCHS, self.SAMPLES, self.SOURCES)
     for i in range(self.SOURCES):
         for j in range(self.EPOCHS):
             self.s[j, :, i] *= z[j, i]
     # the mixmatrix which converts our sources to channels
     # X = As + noise
     self.A = np.random.randn(self.CHANNELS, self.SOURCES)
     # our 'signal' which 50 epochs, 100 samples and 10 channels
     self.X = np.empty((self.EPOCHS, self.SAMPLES, self.CHANNELS))
     for i in range(self.EPOCHS):
         self.X[i] = np.dot(self.A, self.s[i].T).T
     noise = np.random.randn(self.EPOCHS, self.SAMPLES,
                             self.CHANNELS) * 0.01
     self.X += noise
     # convert to epo
     axes = [
         z[:, 0],
         np.arange(self.X.shape[1]),
         np.arange(self.X.shape[2])
     ]
     self.epo = Data(self.X,
                     axes=axes,
                     names=['target_variable', 'time', 'channel'],
                     units=['#', 'ms', '#'])
Ejemplo n.º 7
0
    def setUp(self):
        # create a random noise signal with 50 epochs, 100 samples, and
        # 2 sources
        # even epochs and source 0: *= 5
        # odd epochs and source 1: *= 5
        self.s = np.random.randn(self.EPOCHS, self.SAMPLES, self.SOURCES)
        self.s[::2, :, 0] *= 5
        self.s[1::2, :, 1] *= 5
        # the mixmatrix which converts our sources to channels
        # X = As + noise
        self.A = np.random.randn(self.CHANNELS, self.SOURCES)
        # our 'signal' which 50 epochs, 100 samples and 10 channels
        self.X = np.empty((self.EPOCHS, self.SAMPLES, self.CHANNELS))
        for i in range(self.EPOCHS):
            self.X[i] = np.dot(self.A, self.s[i].T).T
        noise = np.random.randn(self.EPOCHS, self.SAMPLES,
                                self.CHANNELS) * 0.01
        self.X += noise

        a = np.array([1 for i in range(self.X.shape[0])])
        a[0::2] = 0
        axes = [a, np.arange(self.X.shape[1]), np.arange(self.X.shape[2])]
        self.epo = Data(self.X,
                        axes=axes,
                        names=['class', 'time', 'channel'],
                        units=['#', 'ms', '#'])
        self.epo.class_names = ['foo', 'bar']

        self.filter = np.random.random((self.CHANNELS, self.CHANNELS))
Ejemplo n.º 8
0
 def test_eq_and_ne(self):
     """Check if __ne__ is properly implemented."""
     d1 = Data(self.data, self.axes, self.names, self.units)
     d2 = d1.copy()
     # if __eq__ is implemented and __ne__ is not, this evaluates to
     # True!
     self.assertFalse(d1 == d2 and d1 != d2)
Ejemplo n.º 9
0
 def setUp(self):
     # X is a random mixture matrix of random variables
     Sx = randn(self.SAMPLES, self.CHANNELS_X)
     Ax = randn(self.CHANNELS_X, self.CHANNELS_X)
     self.X = np.dot(Sx, Ax)
     # Y is a random mixture matrix of random variables except the
     # first component
     Sy = randn(self.SAMPLES, self.CHANNELS_Y)
     Sy[:, 0] = Sx[:, 0] + self.NOISE_LEVEL * randn(self.SAMPLES)
     Ay = randn(self.CHANNELS_Y, self.CHANNELS_Y)
     self.Y = np.dot(Sy, Ay)
     # generate Data object
     axes_x = [np.arange(self.X.shape[0]), np.arange(self.X.shape[1])]
     axes_y = [np.arange(self.Y.shape[0]), np.arange(self.Y.shape[1])]
     self.dat_x = Data(self.X, axes=axes_x, names=['time', 'channel'], units=['ms', '#'])
     self.dat_y = Data(self.Y, axes=axes_y, names=['time', 'channel'], units=['ms', '#'])
Ejemplo n.º 10
0
def convert_mushu_data(data, markers, fs, channels):
    """Convert mushu data into wyrm's ``Data`` format.

    This convenience method creates a continuous ``Data`` object from
    the parameters given. The timeaxis always starts from zero and its
    values are calculated from the sampling frequency ``fs`` and the
    length of ``data``. The ``names`` and ``units`` attributes are
    filled with default vaules.

    Parameters
    ----------
    data : 2d array
        an 2 dimensional numpy array with the axes: (time, channel)
    markers : list of tuples: (float, str)
        a list of markers. Each element is a tuple of timestamp and
        string. The timestamp is the time in ms relative to the onset of
        the block of data. Note that negative values are *allowed* as
        well as values bigger than the length of the block of data
        returned. That is to be interpreted as a marker from the last
        block and a marker for a future block respectively.
    fs : float
        the sampling frequency, this number is used to calculate the
        timeaxis for the data
    channels : list or 1d array of strings
        the channel names

    Returns
    -------
    cnt : continuous ``Data`` object

    Examples
    --------

    Assuming that ``amp`` is an Amplifier instance from ``libmushu``,
    already configured but not started yet:

    >>> amp_fs = amp.get_sampling_frequency()
    >>> amp_channels = amp.get_channels()
    >>> amp.start()
    >>> while True:
    ...     data, markers = amp.get_data()
    ...     cnt = convert_mushu_data(data, markers, amp_fs, amp_channels)
    ...     # some more code
    >>> amp.stop()

    References
    ----------
    https://github.com/bbci/mushu

    """
    time_axis = np.linspace(0, 1000 * data.shape[0] / fs, data.shape[0], endpoint=False)
    chan_axis = channels[:]
    axes = [time_axis, chan_axis]
    names = ['time', 'channel']
    units = ['uV', '#']
    cnt = Data(data=data.copy(), axes=axes, names=names, units=units)
    cnt.markers = markers[:]
    cnt.fs = fs
    return cnt
Ejemplo n.º 11
0
 def test_init_with_inconsistent_values(self):
     """Test init with inconsistent values."""
     data = self.data[np.newaxis, :]
     with self.assertRaises(AssertionError):
         Data(data, self.axes, self.names, self.units)
     axes = self.axes[:]
     axes[0] = np.arange(100)
     with self.assertRaises(AssertionError):
         Data(self.data, axes, self.names, self.units)
     names = self.names[:]
     names.append('baz')
     with self.assertRaises(AssertionError):
         Data(self.data, self.axes, names, self.units)
     units = self.units[:]
     units.append('u3')
     with self.assertRaises(AssertionError):
         Data(self.data, self.axes, self.names, units)
Ejemplo n.º 12
0
 def setUp(self):
     data = np.arange(10).reshape(5, 2)
     axes = [np.arange(5), np.array(['ch1', 'ch2'])]
     names = ['time', 'channel']
     units = ['ms', '#']
     fs = 1000
     self.dat = Data(data, axes, names, units)
     self.dat.fs = fs
Ejemplo n.º 13
0
 def test_init(self):
     """Test init with correct values."""
     d = Data(self.data, self.axes, self.names, self.units)
     np.testing.assert_array_equal(d.data, self.data)
     for a, b in zip(d.axes, self.axes):
         np.testing.assert_array_equal(a, b)
     self.assertEqual(self.names, d.names)
     self.assertEqual(self.units, d.units)
Ejemplo n.º 14
0
 def test_raise_error_with_non_continuous_data(self):
     """Raise error if ``dat_x`` is not continuous Data object."""
     dat = Data(randn(2, self.SAMPLES, self.CHANNELS_X),
                axes=[[0, 1], self.dat_x.axes[0], self.dat_x.axes[1]],
                names=['class', 'time', 'channel'],
                units=['#', 'ms', '#'])
     with self.assertRaises(AssertionError):
         calculate_cca(dat, self.dat_x)
Ejemplo n.º 15
0
def train_test(data, test_size):
    """Splits data into train and test set of equal class proportions

    Params
    ------
        data : wyrm.Data
            data to be split
        test_size : float
            between 0 and 1, proportion of test to training size

    Returns
    -------
        dat_train : wyrm.Data
            Data object holding training data
        dat_test : wyrm.Data
            Data object holding test data

    Raises
    ------
        TypeError if data of wrong shape
    """
    labels = data.axes[0]
    n_classes = len(np.unique(labels))
    ind = equalize_proportions(labels=labels, n_classes=n_classes)
    if len(data.axes) > 2:
        try:
            data = prep.create_fvs(data)
        except Exception as e:
            msg = ('It seems you have to reshape your data first.\n\n' +
                   str(e))
            raise TypeError(msg)
    dat = data.data[ind, :]
    labels = data.axes[0][ind]
    X_train, X_test, y_train, y_test = train_test_split(dat,
                                                        labels,
                                                        test_size=test_size,
                                                        shuffle=True)
    ax_train = [y_train, data.axes[1]]
    ax_test = [y_test, data.axes[1]]
    names = data.names
    units = data.units
    dat_train = Data(data=X_train, axes=ax_train, names=names, units=units)
    dat_test = Data(data=X_test, axes=ax_test, names=names, units=units)

    return dat_train, dat_test
Ejemplo n.º 16
0
 def setUp(self):
     ones = np.ones((10, 5))
     # epo with 0, 1, 2
     data = np.array([0 * ones, ones, 2 * ones])
     channels = ['ca1', 'ca2', 'cb1', 'cb2', 'cc1']
     time = np.linspace(0, 1000, 10, endpoint=False)
     classes = [0, 1, 2]
     self.dat = Data(data, [classes, time, channels],
                     ['class', 'time', 'channel'], ['#', 'ms', '#'])
 def setUp(self):
     ones = np.ones((10, 5))
     # three blocks: 1s, -1s, and 0s
     cnt_data = np.concatenate([ones, -ones, 0 * ones], axis=0)
     classes = [0, 0, 0]
     channels = ['ca1', 'ca2', 'cb1', 'cb2', 'cc1']
     time = np.linspace(-1000, 2000, 30, endpoint=False)
     epo_data = np.array([cnt_data, cnt_data, cnt_data])
     self.dat = Data(epo_data, [classes, time, channels],
                     ['class', 'time', 'channels'], ['#', 'ms', '#'])
Ejemplo n.º 18
0
 def setUp(self):
     ones = np.ones((10, 5))
     channels = ['ca1', 'ca2', 'cb1', 'cb2', 'cc1']
     time = np.linspace(-1000, 0, 10, endpoint=False)
     classes = [0, 0, 0]
     # three cnts: 1s, -1s, and 0s
     data = np.array([ones, ones * -1, ones * 0])
     self.dat = Data(data, [classes, time, channels],
                     ['class', 'time', 'channel'], ['#', 'ms', '#'])
     self.dat.fs = 10
Ejemplo n.º 19
0
 def setUp(self):
     ones = np.ones((10, 5))
     channels = ['ca1', 'ca2', 'cb1', 'cb2', 'cc1']
     time = np.linspace(0, 1000, 10, endpoint=False)
     classes = [0, 1, 2, 1]
     class_names = ['zeros', 'ones', 'twoes']
     # four cnts: 0s, 1s, -1s, and 0s
     data = np.array([ones * 0, ones * 1, ones * 2, ones * 0])
     self.dat = Data(data, [classes, time, channels], ['class', 'time', 'channel'], ['#', 'ms', '#'])
     self.dat.class_names = class_names
Ejemplo n.º 20
0
 def setUp(self):
     raw = np.arange(2000).reshape(-1, 5)
     channels = ['ca1', 'ca2', 'cb1', 'cb2', 'cc1']
     time = np.linspace(0, 4000, 400, endpoint=False)
     fs = 100
     marker = [[100, 'foo'], [200, 'bar']]
     self.dat = Data(raw, [time, channels], ['time', 'channels'],
                     ['ms', '#'])
     self.dat.fs = fs
     self.dat.markers = marker
Ejemplo n.º 21
0
 def test_copy(self):
     """Copy must work."""
     d1 = Data(self.data, self.axes, self.names, self.units)
     d2 = d1.copy()
     self.assertEqual(d1, d2)
     # we can't really check of all references to be different in
     # depth recursively, so we only check on the first level
     for k in d1.__dict__:
         self.assertNotEqual(id(getattr(d1, k)), id(getattr(d2, k)))
     d2 = d1.copy(foo='bar')
     self.assertEqual(d2.foo, 'bar')
Ejemplo n.º 22
0
 def setUp(self):
     ones = np.ones((10, 5))
     # cnt with 1, 2, 3
     cnt = np.append(ones, ones * 2, axis=0)
     cnt = np.append(cnt, ones * 3, axis=0)
     channels = ['ca1', 'ca2', 'cb1', 'cb2', 'cc1']
     time = np.linspace(0, 3000, 30, endpoint=False)
     self.dat = Data(cnt, [time, channels], ['time', 'channel'],
                     ['ms', '#'])
     self.dat.markers = [[0, 'a'], [1, 'b']]
     self.dat.fs = 10
Ejemplo n.º 23
0
 def setUp(self):
     # create epoched data with only 0s in class0, 1s in class1 and
     # 2s in class2
     cnt = np.ones((10, 3))
     epo = np.array([0 * cnt, 1 * cnt, 2 * cnt])
     time = np.arange(10)
     channels = np.array(['ch1', 'ch2', 'ch3'])
     classes = np.arange(3)
     axes = ['class', 'time', 'channel']
     units = ['#', 'ms', '#']
     self.dat = Data(epo, [classes, time, channels], axes, units)
Ejemplo n.º 24
0
def compute_cleaner(data,
                    eog_data,
                    marker_positions,
                    ival,
                    max_min=2,
                    whisker_percent=5,
                    whisker_length=3):
    """For Cleaner tests..."""
    assert eog_data.shape[0] == data.shape[0]

    axes = [range(data.shape[0]), range(data.shape[1])]
    markers = zip(marker_positions, [0] * len(marker_positions))
    marker_def = {'0': [0]}
    cnt = Data(data, axes=axes, names=['time', 'channels'], units=['ms', '#'])
    cnt.fs = 1000
    cnt.markers = markers

    eog_axes = [range(eog_data.shape[0]), range(eog_data.shape[1])]
    eog_cnt = Data(eog_data,
                   axes=eog_axes,
                   names=['time', 'channels'],
                   units=['ms', '#'])
    eog_cnt.fs = 1000
    eog_cnt.markers = markers
    eog_proc = SignalProcessor(FakeLoader(eog_cnt),
                               segment_ival=ival,
                               marker_def=marker_def)

    cleaner = Cleaner(cnt,
                      eog_proc,
                      rejection_blink_ival=ival,
                      max_min=max_min,
                      rejection_var_ival=ival,
                      whisker_percent=whisker_percent,
                      whisker_length=whisker_length,
                      low_cut_hz=None,
                      high_cut_hz=None,
                      filt_order=None,
                      marker_def=marker_def)
    cleaner.clean()
    return cleaner
Ejemplo n.º 25
0
 def setUp(self):
     # create 100 samples and tree channels data
     ones = np.ones((100, 3))
     data = np.array([ones, ones * 2, ones * 3]).reshape(-1, 3)
     time = np.linspace(0, 3000, 300, endpoint=False)
     channels = ['a', 'b', 'c']
     markers = [[500, 'M1'], [1500, 'M2'], [2500, 'M3']]
     self.dat = Data(data, [time, channels], ['time', 'channels'],
                     ['ms', '#'])
     self.dat.markers = markers
     self.dat.fs = 100
     self.mrk_def = {'class 1': ['M1'], 'class 2': ['M2', 'M3']}
Ejemplo n.º 26
0
 def setUp(self):
     ones = np.ones((10, 5))
     # cnt with 1, 2, 3
     cnt = np.append(ones, ones*2, axis=0)
     cnt = np.append(cnt, ones*3, axis=0)
     channels = ['ca1', 'ca2', 'cb1', 'cb2', 'cc1']
     time = np.linspace(0, 3000, 30, endpoint=False)
     classes = [0, 1, 2, 1]
     # four cnts: 1s, -1s, and 0s
     data = np.array([cnt * 0, cnt * 1, cnt * 2, cnt * 0])
     self.dat = Data(data, [classes, time, channels], ['class', 'time', 'channel'], ['#', 'ms', '#'])
     self.dat.class_names = ['zero', 'one', 'two']
Ejemplo n.º 27
0
 def setUp(self):
     ones = np.ones((10, 2))
     twoes = ones * 2
     # 7 epochs
     data = np.array([ones, ones, twoes, twoes, ones, twoes, twoes])
     channels = ['c1', 'c2']
     time = np.linspace(0, 1000, 10)
     classes = [0, 0, 1, 1, 0, 1, 1]
     class_names = ['ones', 'twoes']
     self.dat = Data(data, [classes, time, channels],
                     ['class', 'time', 'channel'], ['#', 'ms', '#'])
     self.dat.class_names = class_names
Ejemplo n.º 28
0
def load_mushu_data(meta):
    """Load saved EEG data in Mushu's format.

    This method loads saved data in Mushu's format and returns a
    continuous ``Data`` object.

    Parameters
    ----------
    meta : str
        Path to `.meta` file. A Mushu recording consists of three
        different files: `.eeg`, `.marker`, and `.meta`.

    Returns
    -------
    dat : Data
        Continuous Data object

    Examples
    --------

    >>> dat = load_mushu_data('testrecording.meta')

    """
    # reverse and replace and reverse again to replace only the last
    # (occurrence of .meta)
    datafile = meta[::-1].replace('atem.', 'gee.', 1)[::-1]
    markerfile = meta[::-1].replace('atem.', 'rekram.', 1)[::-1]
    assert path.exists(meta) and path.exists(datafile) and path.exists(
        markerfile)
    # load meta data
    with open(meta) as fh:
        metadata = json.load(fh)
    fs = metadata['Sampling Frequency']
    channels = np.array(metadata['Channels'])
    # load eeg data
    data = np.fromfile(datafile, np.float32)
    data = data.reshape((-1, len(channels)))
    # load markers
    markers = []
    with open(markerfile) as fh:
        for line in fh:
            ts, m = line.split(' ', 1)
            markers.append([float(ts), str(m).strip()])
    # construct Data
    duration = len(data) * 1000 / fs
    axes = [np.linspace(0, duration, len(data), endpoint=False), channels]
    names = ['time', 'channels']
    units = ['ms', '#']
    dat = Data(data=data, axes=axes, names=names, units=units)
    dat.fs = fs
    dat.markers = markers
    return dat
Ejemplo n.º 29
0
 def setUp(self):
     self.sorted_channels = np.array([name for name, pos in CHANNEL_10_20])
     channels = self.sorted_channels.copy()
     random.shuffle(channels)
     raw = np.random.random((5, 10, len(channels)))
     time = np.linspace(0, 1000, 10, endpoint=False)
     epochs = np.array([0, 1, 0, 1, 0])
     fs = 100
     marker = [[100, 'foo'], [200, 'bar']]
     self.dat = Data(raw, [epochs, time, channels],
                     ['class', 'time', 'channels'], ['#', 'ms', '#'])
     self.dat.fs = fs
     self.dat.markers = marker
Ejemplo n.º 30
0
 def setUp(self):
     # create some data
     fs = 100
     dt = 5
     self.freqs = [2, 7, 15]
     amps = [30, 10, 2]
     t = np.linspace(0, dt, fs*dt)
     data = np.sum([a * np.sin(2*np.pi*t*f) for a, f in zip(amps, self.freqs)], axis=0)
     data = data[:, np.newaxis]
     data = np.concatenate([data, data], axis=1)
     channel = np.array(['ch1', 'ch2'])
     self.dat = Data(data, [t, channel], ['time', 'channel'], ['s', '#'])
     self.dat.fs = fs