当前位置: 首页>>代码示例>>Python>>正文


Python types.Data类代码示例

本文整理汇总了Python中wyrm.types.Data的典型用法代码示例。如果您正苦于以下问题:Python Data类的具体用法?Python Data怎么用?Python Data使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了Data类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_eq_and_ne

 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)
开发者ID:awakenting,项目名称:wyrm,代码行数:7,代码来源:test_data.py

示例2: convert_mushu_data

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
开发者ID:drajguru,项目名称:wyrm,代码行数:59,代码来源:io.py

示例3: TestClearMarkers

class TestClearMarkers(unittest.TestCase):

    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(-1000, 2000, 30, endpoint=False)
        self.good_markers = [[-1000, 'a'], [-999, 'b'], [0, 'c'], [1999.9999999, 'd']]
        bad_markers = [[-1001, 'x'], [2000, 'x']]
        markers = self.good_markers[:]
        markers.extend(bad_markers)
        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.markers = markers
        self.dat.fs = 10

    def test_clear_markers(self):
        """Clear markers."""
        dat = clear_markers(self.dat)
        self.assertEqual(dat.markers, self.good_markers)

    def test_clear_emtpy_markers(self):
        """Clearing emtpy markers has no effect."""
        dat = self.dat.copy()
        dat.markers = []
        dat2 = clear_markers(dat)
        self.assertEqual(dat, dat2)

    def test_clear_nonexisting_markers(self):
        """Clearing emtpy markers has no effect."""
        dat = self.dat.copy()
        del dat.markers
        dat2 = clear_markers(dat)
        self.assertEqual(dat, dat2)

    def test_clear_markers_w_empty_data(self):
        """Clearing emtpy dat should remove all markers."""
        dat = self.dat.copy()
        dat.data = np.array([])
        dat2 = clear_markers(dat)
        self.assertEqual(dat2.markers, [])

    def test_clear_markes_swapaxes(self):
        """clear_markers must work with nonstandard timeaxis."""
        dat = clear_markers(swapaxes(self.dat, 1, 2), timeaxis=2)
        dat = swapaxes(dat, 1, 2)
        dat2 = clear_markers(self.dat)
        self.assertEqual(dat, dat2)

    def test_clear_markers_copy(self):
        """clear_markers must not modify argument."""
        cpy = self.dat.copy()
        clear_markers(self.dat)
        self.assertEqual(self.dat, cpy)
开发者ID:awakenting,项目名称:wyrm,代码行数:58,代码来源:test_clear_markers.py

示例4: test_copy

 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')
开发者ID:awakenting,项目名称:wyrm,代码行数:11,代码来源:test_data.py

示例5: TestAppendEpo

class TestAppendEpo(unittest.TestCase):

    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']

    def test_append_epo(self):
        """append_epo."""
        dat = append_epo(self.dat, self.dat)
        self.assertEqual(dat.data.shape[0], 2*self.dat.data.shape[0])
        self.assertEqual(len(dat.axes[0]), 2*len(self.dat.axes[0]))
        np.testing.assert_array_equal(dat.data, np.concatenate([self.dat.data, self.dat.data], axis=0))
        np.testing.assert_array_equal(dat.axes[0], np.concatenate([self.dat.axes[0], self.dat.axes[0]]))
        self.assertEqual(dat.class_names, self.dat.class_names)

    def test_append_epo_with_extra(self):
        """append_epo with extra must work with list and ndarrays."""
        self.dat.a = list(range(10))
        self.dat.b = np.arange(10)
        dat = append_epo(self.dat, self.dat, extra=['a', 'b'])
        self.assertEqual(dat.a, list(range(10)) + list(range(10)))
        np.testing.assert_array_equal(dat.b, np.concatenate([np.arange(10), np.arange(10)]))

    def test_append_epo_with_different_class_names(self):
        """test_append must raise a ValueError if class_names are different."""
        a = self.dat.copy()
        a.class_names = a.class_names[:-1]
        with self.assertRaises(ValueError):
            append_epo(a, self.dat)
            append_epo(self.dat, a)

    def test_append_epo_swapaxes(self):
        """append_epo must work with nonstandard timeaxis."""
        dat = append_epo(swapaxes(self.dat, 0, 2), swapaxes(self.dat, 0, 2), classaxis=2)
        dat = swapaxes(dat, 0, 2)
        dat2 = append_epo(self.dat, self.dat)
        self.assertEqual(dat, dat2)

    def test_append_epo_copy(self):
        """append_epo means must not modify argument."""
        cpy = self.dat.copy()
        append_epo(self.dat, self.dat)
        self.assertEqual(self.dat, cpy)
开发者ID:awakenting,项目名称:wyrm,代码行数:52,代码来源:test_append_epo.py

示例6: TestCorrectForBaseline

class TestCorrectForBaseline(unittest.TestCase):

    def setUp(self):
        ones = np.ones((10, 5))
        classes = [0, 0, 0]
        channels = ['ca1', 'ca2', 'cb1', 'cb2', 'cc1']
        time = np.linspace(-1000, 0, 10, endpoint=False)
        # three cnts: 1s, -1s, and 0s
        data = np.array([ones, ones * -1, ones * 0])
        self.dat = Data(data, [classes, time, channels], ['class', 'time', 'channels'], ['#', 'ms', '#'])

    def test_correct_for_baseline_epo(self):
        """Test baselineing w/ epo like."""
        # normal case
        dat = correct_for_baseline(self.dat, [-500, 0])
        np.testing.assert_array_equal(np.zeros((3, 10, 5)), dat.data)
        # the full dat interval
        dat = correct_for_baseline(self.dat, [dat.axes[-2][0], dat.axes[-2][-1]])
        np.testing.assert_array_equal(np.zeros((3, 10, 5)), dat.data)

    def test_correct_for_baseline_cnt(self):
        """Test baselineing w/ cnt like."""
        data = self.dat.data.reshape(30, 5)
        axes = [np.linspace(-1000, 2000, 30, endpoint=False), self.dat.axes[-1]]
        units = self.dat.units[1:]
        names = self.dat.names[1:]
        dat = self.dat.copy(data=data, axes=axes, names=names, units=units)
        dat2 = correct_for_baseline(dat, [-1000, 0])
        np.testing.assert_array_equal(dat2.data, dat.data - 1)

    def test_ival_checks(self):
        """Test for malformed ival parameter."""
        with self.assertRaises(AssertionError):
            correct_for_baseline(self.dat, [0, -1])
        with self.assertRaises(AssertionError):
            correct_for_baseline(self.dat, [self.dat.axes[-2][0]-1, 0])
        with self.assertRaises(AssertionError):
            correct_for_baseline(self.dat, [0, self.dat.axes[-2][1]+1])

    def test_correct_for_baseline_copy(self):
        """Correct for baseline must not modify dat argument."""
        cpy = self.dat.copy()
        correct_for_baseline(self.dat, [-1000, 0])
        self.assertEqual(cpy, self.dat)

    def test_correct_for_baseline_swapaxes(self):
        """Correct for baseline must work with nonstandard timeaxis."""
        dat = correct_for_baseline(swapaxes(self.dat, 0, 1), [-1000, 0], timeaxis=0)
        dat = swapaxes(dat, 0, 1)
        dat2 = correct_for_baseline(self.dat, [-1000, 0])
        self.assertEqual(dat, dat2)
开发者ID:alistairwalsh,项目名称:wyrm,代码行数:51,代码来源:test_correct_for_baseline.py

示例7: load_mushu_data

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, 'r') 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, 'r') 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
开发者ID:drajguru,项目名称:wyrm,代码行数:51,代码来源:io.py

示例8: test_segment_dat_with_restriction_to_new_data_ival_pos_pos

 def test_segment_dat_with_restriction_to_new_data_ival_pos_pos(self):
     """Online Segmentation with ival +something..+something must work correctly."""
     data = np.ones((9, 3))
     time = np.linspace(0, 900, 9, endpoint=False)
     channels = 'a', 'b', 'c'
     markers = [[100, 'x'], [200, 'x'], [300, 'x']]
     dat = Data(data, [time, channels], ['time', 'channels'], ['ms', '#'])
     dat.fs = 10
     dat.markers = markers
     mrk_def = {'class 1': ['x']}
     # each tuple has (number of new samples, expected epocs)
     samples_epos = [(0, 0), (1, 0), (2, 1), (3, 2), (4, 3), (5, 3)]
     for s, e in samples_epos:
         epo = segment_dat(dat, mrk_def, [100, 500], newsamples=s)
         self.assertEqual(epo.data.shape[0], e)
开发者ID:alistairwalsh,项目名称:wyrm,代码行数:15,代码来源:test_segment_dat.py

示例9: setUp

    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))
开发者ID:awakenting,项目名称:wyrm,代码行数:25,代码来源:test_apply_csp.py

示例10: setUp

 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)
     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)
     Y = np.dot(Sy, Ay)
     # generate Data object
     axes_x = [np.arange(X.shape[0]), np.arange(X.shape[1])]
     axes_y = [np.arange(Y.shape[0]), np.arange(Y.shape[1])]
     self.dat_x = Data(X, axes=axes_x, names=['time', 'channel'], units=['ms', '#'])
     self.dat_y = Data(Y, axes=axes_y, names=['time', 'channel'], units=['ms', '#'])
开发者ID:awakenting,项目名称:wyrm,代码行数:16,代码来源:test_calculate_cca.py

示例11: setUp

 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', '#'])
开发者ID:awakenting,项目名称:wyrm,代码行数:7,代码来源:test_calculate_whitening_matrix.py

示例12: setUp

 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', '#'])
开发者ID:awakenting,项目名称:wyrm,代码行数:25,代码来源:test_calculate_spoc.py

示例13: TestSwapaxes

class TestSwapaxes(unittest.TestCase):

    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

    def test_swapaxes(self):
        """Swapping axes."""
        new = swapaxes(self.dat, 0, 1)
        self.assertTrue((new.axes[0] == self.dat.axes[1]).all())
        self.assertTrue((new.axes[1] == self.dat.axes[0]).all())
        self.assertEqual(new.names[0], self.dat.names[1])
        self.assertEqual(new.names[1], self.dat.names[0])
        self.assertEqual(new.units[0], self.dat.units[1])
        self.assertEqual(new.units[1], self.dat.units[0])
        self.assertEqual(new.data.shape[::-1], self.dat.data.shape)
        np.testing.assert_array_equal(new.data.swapaxes(0, 1), self.dat.data)

    def test_swapaxes_copy(self):
        """Swapaxes must not modify argument."""
        cpy = self.dat.copy()
        swapaxes(self.dat, 0, 1)
        self.assertEqual(cpy, self.dat)

    def test_swapaxes_twice(self):
        """Swapping the same axes twice must result in original."""
        dat = swapaxes(self.dat, 0, 1)
        dat = swapaxes(dat, 0, 1)
        self.assertEqual(dat, self.dat)
开发者ID:alistairwalsh,项目名称:wyrm,代码行数:35,代码来源:test_swapaxes.py

示例14: TestSelectChannels

class TestSelectChannels(unittest.TestCase):
    def setUp(self):
        raw = np.arange(20).reshape(4, 5)
        channels = ["ca1", "ca2", "cb1", "cb2", "cc1"]
        time = np.arange(4)
        self.dat = Data(raw, [time, channels], ["time", "channels"], ["ms", "#"])

    def test_select_channels(self):
        """Selecting channels with an array of regexes."""
        channels = self.dat.data.copy()
        self.dat = select_channels(self.dat, ["ca.*", "cc1"])
        np.testing.assert_array_equal(self.dat.axes[-1], np.array(["ca1", "ca2", "cc1"]))
        np.testing.assert_array_equal(self.dat.data, channels[:, np.array([0, 1, -1])])

    def test_select_channels_inverse(self):
        """Removing channels with an array of regexes."""
        channels = self.dat.data.copy()
        self.dat = select_channels(self.dat, ["ca.*", "cc1"], invert=True)
        np.testing.assert_array_equal(self.dat.axes[-1], np.array(["cb1", "cb2"]))
        np.testing.assert_array_equal(self.dat.data, channels[:, np.array([2, 3])])

    def test_select_channels_copy(self):
        """Select channels must not change the original parameter."""
        cpy = self.dat.copy()
        select_channels(self.dat, ["ca.*"])
        self.assertEqual(cpy, self.dat)

    def test_select_channels_swapaxis(self):
        """Select channels works with non default chanaxis."""
        dat1 = select_channels(swapaxes(self.dat, 0, 1), ["ca.*"], chanaxis=0)
        dat1 = swapaxes(dat1, 0, 1)
        dat2 = select_channels(self.dat, ["ca.*"])
        self.assertEqual(dat1, dat2)
开发者ID:usmanayubsh,项目名称:wyrm,代码行数:33,代码来源:test_select_channels.py

示例15: setUp

 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', '#'])
开发者ID:alistairwalsh,项目名称:wyrm,代码行数:8,代码来源:test_select_ival.py


注:本文中的wyrm.types.Data类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。