def test_basic_slicing(): t = ts.TimeArray(list(range(4))) for x in range(3): ep = ts.Epochs(.5,x+.5) npt.assert_equal(len(t[ep]), x) # epoch starts before timeseries npt.assert_equal(len(t[ts.Epochs(-1,3)]), len(t)-1) # epoch ends after timeseries npt.assert_equal(len(t[ts.Epochs(.5,5)]), len(t)-1) # epoch starts before and ends after timeseries npt.assert_equal(len(t[ts.Epochs(-1,100)]), len(t)) ep = ts.Epochs(20,100) npt.assert_equal(len(t[ep]), 0)
def test_Epochs_duration_after_slicing(): "some attributes which get set on read should be reset after slicing" e = ts.Epochs(list(range(10)),duration=.1) npt.assert_equal(len(e.duration), len(e)) slice_of_e = e[:3] npt.assert_equal(len(slice_of_e.duration), len(slice_of_e))
def test_Events(): # time has to be one-dimensional nt.assert_raises(ValueError, ts.Events, np.zeros((2, 2))) t = ts.TimeArray([1, 2, 3], time_unit='ms') x = [1, 2, 3] y = [2, 4, 6] z = [10., 20., 30.] i0 = [0, 0, 1] i1 = [0, 1, 2] for unit in [None, 's', 'ns', 'D']: # events with data ev1 = ts.Events(t, time_unit=unit, i=x, j=y, k=z) # events with indices ev2 = ts.Events(t, time_unit=unit, indices=[i0, i1]) # events with indices and labels ev3 = ts.Events(t, time_unit=unit, labels=['trial', 'other'], indices=[i0, i1]) # Note that the length of indices and labels has to be identical: nt.assert_raises(ValueError, ts.Events, t, time_unit=unit, labels=['trial', 'other'], indices=[i0])# Only # one of # the # indices! # make sure the time is retained npt.assert_equal(ev1.time, t) npt.assert_equal(ev2.time, t) # make sure time unit is correct if unit is not None: npt.assert_equal(ev1.time_unit, unit) npt.assert_equal(ev2.time_unit, unit) else: npt.assert_equal(ev1.time_unit, t.time_unit) npt.assert_equal(ev2.time_unit, t.time_unit) # make sure we can extract data npt.assert_equal(ev1.data['i'], x) npt.assert_equal(ev1.data['j'], y) npt.assert_equal(ev1.data['k'], z) # make sure we can get the indices by label npt.assert_equal(ev3.index.trial, i0) npt.assert_equal(ev3.index.other, i1) # make sure we can get the indices by position npt.assert_equal(ev2.index.i0, i0) npt.assert_equal(ev2.index.i1, i1) #make sure slicing works #one_event = ts.Events(t[[0]],time_unit=unit,i=[x[0]],j=[y[0]],k=[z[0]]) #regular indexing npt.assert_equal(ev1[0].data['i'], x[0]) npt.assert_equal(ev1[0:2].data['i'], x[0:2]) # indexing w/ time npt.assert_equal(ev1[0.].data['i'], x[0]) # indexing w/ epoch ep = ts.Epochs(start=0, stop=1.5, time_unit='ms') npt.assert_equal(ev1[ep].data['i'], x[0]) # fancy indexing (w/ boolean mask) npt.assert_equal(ev1[ev3.index.trial == 0].data['j'], y[0:2]) # len() function is implemented and working assert len(t) == len(ev1) == len(ev2) == len(ev3)
def test_Epochs(): tms = ts.TimeArray(data=list(range(100)), time_unit='ms') tmin = ts.TimeArray(data=list(range(100)), time_unit='m') tsec = ts.TimeArray(data=list(range(100)), time_unit='s') utms = ts.UniformTime(length=100, sampling_interval=1, time_unit='ms') utmin = ts.UniformTime(length=100, sampling_interval=1, time_unit='m') utsec = ts.UniformTime(length=100, sampling_interval=1, time_unit='s') tsms = ts.TimeSeries(data=list(range(100)), sampling_interval=1, time_unit='ms') tsmin = ts.TimeSeries(data=list(range(100)), sampling_interval=1, time_unit='m') tssec = ts.TimeSeries(data=list(range(100)), sampling_interval=1, time_unit='s') # one millisecond epoch e1ms = ts.Epochs(0, 1, time_unit='ms') e09ms = ts.Epochs(0.1, 1, time_unit='ms') msg = "Seems like a problem with copy=False in TimeArray constructor." npt.assert_equal(e1ms.duration, ts.TimeArray(1, time_unit='ms'), msg) # one day e1d = ts.Epochs(0, 1, time_unit='D') npt.assert_equal(e1d.duration, ts.TimeArray(1, time_unit='D'), msg) e1ms_ar = ts.Epochs([0, 0], [1, 1], time_unit='ms') for t in [tms, tmin, tsec, utms, utmin, utsec]: # the sample time arrays are all at least 1ms long, so this should # return a timearray that has exactly one time point in it npt.assert_equal(len(t.during(e1ms)), 1) # this time epoch should not contain any point npt.assert_equal(len(t.during(e09ms)), 0) # make sure, slicing doesn't change the class npt.assert_equal(type(t), type(t.during(e1ms))) for t in [tsms, tsmin, tssec]: # the sample time series are all at least 1ms long, so this should # return a timeseries that has exactly one time point in it npt.assert_equal(len(t.during(e1ms)), 1) # make sure, slicing doesn't change the class npt.assert_equal(type(t), type(t.during(e1ms))) # same thing but now there's an array of epochs e2 = ts.Epochs([0, 10], [10, 20], time_unit=t.time_unit) # make sure, slicing doesn't change the class for array of epochs npt.assert_equal(type(t), type(t.during(e2))) # Indexing with an array of epochs (all of which are the same length) npt.assert_equal(t[e2].data.shape, (2, 10)) npt.assert_equal(len(t.during(e2)), 10) npt.assert_equal(t[e2].data.ndim, 2) # check the data at some timepoints (a dimension was added) npt.assert_equal(t[e2][0], (0, 10)) npt.assert_equal(t[e2][1], (1, 11)) # check the data for each epoch npt.assert_equal(t[e2].data[0], list(range(10))) npt.assert_equal(t[e2].data[1], list(range(10, 20))) npt.assert_equal(t[e2].duration, e2[0].duration) # slice with Epochs of different length (not supported for timeseries, # raise error, though future jagged array implementation could go here) ejag = ts.Epochs([0, 10], [10, 40], time_unit=t.time_unit) # next line is the same as t[ejag] npt.assert_raises(ValueError, t.__getitem__, ejag) # if an epoch lies entirely between samples in the timeseries, # return an empty array eshort = ts.Epochs(2.5, 2.7, time_unit=t.time_unit) npt.assert_equal(len(t[eshort].data), 0) e1ms_outofrange = ts.Epochs(200, 300, time_unit=t.time_unit) # assert that with the epoch moved outside of the time range of our # data, slicing with the epoch now yields an empty array npt.assert_raises(ValueError, t.during, dict(e=e1ms_outofrange)) # the sample timeseries are all shorter than a day, so this should # raise an error (instead of padding, or returning a shorter than # expected array. npt.assert_raises(ValueError, t.during, dict(e=e1d))