def test_TimeArray_index_at(): time1 = ts.TimeArray(list(range(10)), time_unit='ms') for i in range(5): # The return value is always an array, so we keep it for multiple tests i_arr = np.array(i) # Check 'closest' indexing mode first idx = time1.index_at(i) npt.assert_equal(idx, i_arr) # If we index with seconds/1000, results shouldn't vary idx_secs = time1.index_at(ts.TimeArray(i / 1000., time_unit='s')) npt.assert_equal(idx_secs, i_arr) # If we now change the tolerance # In this case, it should still return idx = time1.index_at(i + 0.1, tol=0.1) npt.assert_equal(idx, i_arr) # But with a smaller tolerance, we should get no indices idx = time1.index_at(i + 0.1, tol=0.05) npt.assert_equal(idx, np.array([])) # Now, check before/after modes idx = time1.index_at(i + 0.1, mode='before') npt.assert_equal(idx, i_arr) idx = time1.index_at(i + 0.1, mode='after') npt.assert_equal(idx, i_arr + 1)
def test_time_series_from_file(): """Testing reading of data from nifti files, using nibabel""" TR = 1.35 ts_ff = io.time_series_from_file #File names: fmri_file1 = os.path.join(data_path, 'fmri1.nii.gz') fmri_file2 = os.path.join(data_path, 'fmri2.nii.gz') #Spatial coordinates into the volumes: coords1 = np.array([[5, 5, 5, 5], [5, 5, 5, 5], [1, 2, 3, 4]]) coords2 = np.array([[6, 6, 6, 6], [6, 6, 6, 6], [3, 4, 5, 6]]) #No averaging, no normalization: t1 = ts_ff([fmri_file1, fmri_file2], [coords1, coords2], TR) npt.assert_equal(t1[0].shape, (4, 80)) # 4 coordinates, 80 time-points t2 = ts_ff([fmri_file1, fmri_file2], [coords1, coords2], TR, average=True) npt.assert_equal(t2[0].shape, (80, )) # collapse coordinates,80 time-points t3 = ts_ff(fmri_file1, coords1, TR, normalize='zscore') #The mean of each channel should be almost equal to 0: npt.assert_almost_equal(t3.data[0].mean(), 0) #And the standard deviation should be almost equal to 1: npt.assert_almost_equal(t3.data[0].std(), 1) t4 = ts_ff(fmri_file1, coords1, TR, normalize='percent') #In this case, the average is almost equal to 0, but no constraint on the #std: npt.assert_almost_equal(t4.data[0].mean(), 0) #Make sure that we didn't mess up the sampling interval: npt.assert_equal(t4.sampling_interval, nitime.TimeArray(1.35)) # Test the default behavior: data = io.load(fmri_file1).get_data() t5 = ts_ff(fmri_file1) npt.assert_equal(t5.shape, data.shape) npt.assert_equal(t5.sampling_interval, ts.TimeArray(1, time_unit='s')) # Test initializing TR with a TimeArray: t6 = ts_ff(fmri_file1, TR=ts.TimeArray(1350, time_unit='ms')) npt.assert_equal(t4.sampling_interval, t6.sampling_interval) # Check the concatenation dimensions: t7 = ts_ff([fmri_file1, fmri_file2]) npt.assert_equal([t7.shape[:3], t7.shape[-1]], [data.shape[:3], data.shape[-1] * 2]) t8 = ts_ff([fmri_file1, fmri_file2], average=True) npt.assert_equal(t8.shape[0], data.shape[-1] * 2) t9 = ts_ff([fmri_file1, fmri_file2], average=True, normalize='zscore') npt.assert_almost_equal(t9.data.mean(), 0)
def test_UniformTime_index_at(): time1 = ts.UniformTime(t0=1000, length=10, sampling_rate=1000, time_unit='ms') mask = [False] * 10 for i in xrange(10): idx = time1.index_at(ts.TimeArray(1000 + i, time_unit='ms')) npt.assert_equal(idx, np.array(i)) mask[i] = True mask_idx = time1.index_at(ts.TimeArray(1000 + i, time_unit='ms'), boolean=True) npt.assert_equal(mask_idx, mask) if i > 0 and i < 9: mask[i - 1] = True mask[i + 1] = True mask_idx = time1.index_at(ts.TimeArray( [999 + i, 1000 + i, 1001 + i], time_unit='ms'), boolean=True) npt.assert_equal(mask_idx, mask) mask[i - 1] = False mask[i + 1] = False mask[i] = False
def test_TimeArray_at2(): time1 = ts.TimeArray(list(range(10)), time_unit='ms') for i in [1]: i_ms = ts.TimeArray(i / 1000.) this_secs = time1.at(i_ms, tol=1) npt.assert_equal(this_secs, ts.TimeArray([i - 1, i, i + 1], time_unit='ms'))
def test_TimeArray_bool(): time1 = ts.TimeArray([1, 2, 3], time_unit='s') time2 = ts.TimeArray([1000, 2000, 3000], time_unit='ms') bool_arr = np.ones(time1.shape, dtype=bool) npt.assert_equal(time1, time2) npt.assert_equal(bool_arr, time1 == time2) nt.assert_not_equal(type(time1 == time2), ts.TimeArray)
def test_TimeArray_math(): "Addition and subtraction should convert to TimeArray units" time1 = ts.TimeArray(list(range(10)), time_unit='ms') time2 = ts.TimeArray(list(range(1,11)), time_unit='ms') # units should be converted to whatever units the array has time3 = time1 + 1 npt.assert_equal(time2,time3) time4 = time2 - 1 npt.assert_equal(time1,time4) # floats should also work time3 = time1 + 1.0 npt.assert_equal(time2,time3) time4 = time2 - 1.0 npt.assert_equal(time1,time4) # test the r* versions time3 = 1 + time1 npt.assert_equal(time2,time3) time4 = 1 - time2 npt.assert_equal(-time1,time4) # floats should also work time3 = 1.0 + time1 npt.assert_equal(time2,time3) time4 = 1.0 - time2 npt.assert_equal(-time1,time4) timeunits = ts.TimeArray(list(range(10)), time_unit='s') timeunits.convert_unit('ms') # now, math with non-TimeArrays should be based on the new time_unit # here the range() list gets converted to a TimeArray with the same units # as timeunits (which is now 'ms') tnew = timeunits + list(range(10)) npt.assert_equal(tnew, timeunits+time1) # recall that time1 was 0-10ms
def test_TimeArray_init_list(): """Initializing with a list that contains TimeArray should work. """ for t in [0.001, ts.TimeArray(0.001, time_unit='s')]: tl = [t] ta = ts.TimeArray(t, time_unit='s') tla = ts.TimeArray(tl, time_unit='s') nt.assert_equal(ta, tla)
def test_FilterAnalyzer(): """Testing the FilterAnalyzer """ t = np.arange(np.pi / 100, 10 * np.pi, np.pi / 100) fast = np.sin(50 * t) + 10 slow = np.sin(10 * t) - 20 fast_mean = np.mean(fast) slow_mean = np.mean(slow) fast_ts = ts.TimeSeries(data=fast, sampling_rate=np.pi) slow_ts = ts.TimeSeries(data=slow, sampling_rate=np.pi) #Make sure that the DC is preserved f_slow = nta.FilterAnalyzer(slow_ts, ub=0.6) f_fast = nta.FilterAnalyzer(fast_ts, lb=0.6) npt.assert_almost_equal(f_slow.filtered_fourier.data.mean(), slow_mean, decimal=2) npt.assert_almost_equal(f_slow.filtered_boxcar.data.mean(), slow_mean, decimal=2) npt.assert_almost_equal(f_slow.fir.data.mean(), slow_mean) npt.assert_almost_equal(f_slow.iir.data.mean(), slow_mean) npt.assert_almost_equal(f_fast.filtered_fourier.data.mean(), 10) npt.assert_almost_equal(f_fast.filtered_boxcar.data.mean(), 10, decimal=2) npt.assert_almost_equal(f_fast.fir.data.mean(), 10) npt.assert_almost_equal(f_fast.iir.data.mean(), 10) #Check that things work with a two-channel time-series: T2 = ts.TimeSeries(np.vstack([fast, slow]), sampling_rate=np.pi) f_both = nta.FilterAnalyzer(T2, ub=1.0, lb=0.1) #These are rather basic tests: npt.assert_equal(f_both.fir.shape, T2.shape) npt.assert_equal(f_both.iir.shape, T2.shape) npt.assert_equal(f_both.filtered_boxcar.shape, T2.shape) npt.assert_equal(f_both.filtered_fourier.shape, T2.shape) # Check that t0 is propagated to the filtered time-series t0 = np.pi T3 = ts.TimeSeries(np.vstack([fast, slow]), sampling_rate=np.pi, t0=t0) f_both = nta.FilterAnalyzer(T3, ub=1.0, lb=0.1) # These are rather basic tests: npt.assert_equal(f_both.fir.t0, ts.TimeArray(t0, time_unit=T3.time_unit)) npt.assert_equal(f_both.iir.t0, ts.TimeArray(t0, time_unit=T3.time_unit)) npt.assert_equal(f_both.filtered_boxcar.t0, ts.TimeArray(t0, time_unit=T3.time_unit)) npt.assert_equal(f_both.filtered_fourier.t0, ts.TimeArray(t0, time_unit=T3.time_unit))
def test_TimeArray_copyflag(): """Testing the setting of the copy-flag, where that makes sense""" #These two should both generate a TimeArray, with one picosecond. #This one holds time_unit='s' t1 = ts.TimeArray(np.array([1], dtype=np.int64), copy=False) #This one holds time_unit='ps': t2 = ts.TimeArray(1, time_unit='ps') t3 = ts.TimeArray(t2, copy=False) npt.assert_equal(t1, t2) npt.assert_equal(t2.ctypes.data, t3.ctypes.data)
def test_get_time_unit(): number = 4 npt.assert_equal(ts.get_time_unit(number), None) list_of_numbers = [4, 5, 6] npt.assert_equal(ts.get_time_unit(list_of_numbers), None) for tu in ['ps', 's', 'D']: time_point = ts.TimeArray([4], time_unit=tu) npt.assert_equal(ts.get_time_unit(time_point), tu) list_of_time = [ ts.TimeArray(4, time_unit=tu), ts.TimeArray(5, time_unit=tu) ] npt.assert_equal(ts.get_time_unit(list_of_time), tu) # Go crazy, we don't mind: list_of_lists = [[ ts.TimeArray(4, time_unit=tu), ts.TimeArray(5, time_unit=tu) ], [ts.TimeArray(4, time_unit=tu), ts.TimeArray(5, time_unit=tu)]] npt.assert_equal(ts.get_time_unit(list_of_lists), tu) time_arr = ts.TimeArray([4, 5], time_unit=tu) npt.assert_equal(ts.get_time_unit(time_arr), tu)
def test_timearray_var_prod(): """ Variance and product change the TimeArray units, so they are not implemented and raise an error """ a = ts.TimeArray(list(range(10))) npt.assert_raises(NotImplementedError, a.var) npt.assert_raises(NotImplementedError, a.prod)
def test_index_int64(): "indexing with int64 should still return a valid TimeArray" a = list(range(10)) b = ts.TimeArray(a) assert b[0] == b[np.int64(0)] assert repr(b[0]) == repr(b[np.int64(0)]) assert b[0] == b[np.int32(0)] assert repr(b[0]) == repr(b[np.int32(0)])
def test_epochs_subclass_slicing(): "Subclassing Epochs should preserve the subclass after slicing" class Epochs_with_X(ts.Epochs): "An epoch class with extra 'stuff'" def total_duration(self): """Duration array for the epoch""" # XXX: bug in duration after slicing - attr_onread should be reset # after slicing #return self.duration.sum() return (self.stop - self.start).sum() time_0 = list(range(10)) e = Epochs_with_X(time_0, duration=.2) npt.assert_equal(e.total_duration(), ts.TimeArray(2.0)) slice_of_e = e[:5] npt.assert_equal(slice_of_e.total_duration(), ts.TimeArray(1.0)) assert(slice_of_e.__class__ == Epochs_with_X)
def test_timearray_var_prod(): """ Variance and product change the TimeArray units, so they are not implemented and raise an error """ a = ts.TimeArray(list(range(10))) with pytest.raises(NotImplementedError) as e_info: a.var() with pytest.raises(NotImplementedError) as e_info: a.prod()
def test_timearray_math_functions(): "Calling TimeArray.min() .max(), mean() should return TimeArrays" a = np.arange(2, 11) for f in ['min', 'max', 'mean', 'ptp', 'sum']: for tu in ['s', 'ms', 'ps', 'D']: b = ts.TimeArray(a, time_unit=tu) npt.assert_(getattr(b, f)().__class__ == ts.TimeArray) npt.assert_(getattr(b, f)().time_unit == b.time_unit) # comparison with unitless should convert to the TimeArray's units npt.assert_(getattr(b, f)() == getattr(a, f)())
def test_TimeArray(): time1 = ts.TimeArray(list(range(100)), time_unit='ms') time2 = time1 + time1 npt.assert_equal(time2.time_unit, 'ms') time1 = ts.TimeArray(10 ** 6) npt.assert_equal(time1.__repr__(), '1000000.0 s') #TimeArray can't be more than 1-d: nt.assert_raises(ValueError, ts.TimeArray, np.zeros((2, 2))) dt = ts.TimeArray(0.001, time_unit='s') tt = ts.TimeArray([dt]) npt.assert_equal(dt, tt) t1 = ts.TimeArray([0, 1, 2, 3]) t2 = ts.TimeArray([ts.TimeArray(0), ts.TimeArray(1), ts.TimeArray(2), ts.TimeArray(3)]) npt.assert_equal(t1, t2)
def test_Events_scalar(): t = ts.TimeArray(1, time_unit='ms') i, j = 4, 5 ev = ts.Events(t, i=i, j=j) # The semantics of scalar indexing into events are such that the returned # value is always a new Events object (the mental model is that of python # strings, where slicing OR scalar indexing still return the same thing, a # string again -- there are no 'string scalars', and there are no 'Event # scalars' either). npt.assert_equal(ev.data['i'][0], i) npt.assert_equal(ev.data['j'][0], j)
def test_TimeArray_new(): for unit in ['ns', 'ms', 's', None]: for flag, assertion in [(True, nt.assert_not_equal), (False, nt.assert_equal)]: #list -doesn't make sense to set copy=True time2 = ts.TimeArray(list(range(5)), time_unit=unit, copy=True) #numpy array (float) - doesn't make sense to set copy=True time2f = ts.TimeArray(np.arange(5.), time_unit=unit, copy=True) #TimeArray time3 = ts.TimeArray(time2, time_unit=unit, copy=flag) #integer time4 = ts.TimeArray(5, time_unit=unit, copy=True) #float time5 = ts.TimeArray(5.0, time_unit=unit, copy=True) npt.assert_equal(time2, time2f) npt.assert_equal(time2, time3) time3[0] += 100 assertion(time2[0], time3[0]) npt.assert_equal(time2[1:], time3[1:]) npt.assert_equal(time4, time5)
def test_TimeArray_div(): #divide singelton by singleton: a = 2.0 b = 6.0 time1 = ts.TimeArray(a, time_unit='s') time2 = ts.TimeArray(b, time_unit='s') div1 = a / b #This should eliminate the units and return a float, not a TimeArray: div2 = time1 / time2 npt.assert_equal(div1, div2) #Divide a TimeArray by a singelton: a = np.array([1, 2, 3]) b = 6.0 time1 = ts.TimeArray(a, time_unit='s') time2 = ts.TimeArray(b, time_unit='s') div1 = a / b #This should eliminate the units and return a float array, not a TimeArray: div2 = time1 / time2 npt.assert_equal(div1, div2) #Divide a TimeArray by another TimeArray: a = np.array([1, 2, 3]) b = np.array([2, 2, 2]).astype(float) # TimeArray division is float division! time1 = ts.TimeArray(a, time_unit='s') time2 = ts.TimeArray(b, time_unit='s') div1 = a / b #This should eliminate the units and return a float array, not a TimeArray: div2 = time1 / time2 npt.assert_equal(div1, div2)
def test_TimeArray_comparison(): "Comparison with unitless quantities should convert to TimeArray units" time = ts.TimeArray(range(10), time_unit='ms') npt.assert_equal(time < 5 , [True]*5+[False]*5) npt.assert_equal(time >= 5 , [False]*5+[True]*5) npt.assert_equal(time <= 5 , [True]*6+[False]*4) npt.assert_equal(time > 5 , [False]*6+[True]*4) time.convert_unit('s') # now all of time is < 1 in the new time_unit npt.assert_equal(time < 5 , [True]*10) npt.assert_equal(time <= 5 , [True]*10) npt.assert_equal(time > 5 , [False]*10) npt.assert_equal(time >= 5 , [False]*10)
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_TimeArray_at(): time1 = ts.TimeArray(list(range(10)), time_unit='ms') for i in range(10): this = time1.at(i) i_ms = ts.TimeArray(i / 1000.) npt.assert_equal(this, ts.TimeArray(i, time_unit='ms')) this_secs = time1.at(i_ms) npt.assert_equal(this_secs, ts.TimeArray(i, time_unit='ms')) seconds_array = ts.TimeArray(time1, time_unit='s') this_secs = seconds_array.at(i / 1000.) npt.assert_equal(this_secs, ts.TimeArray(i, time_unit='ms')) all = time1.at(i_ms, tol=10) npt.assert_equal(all, time1) if i > 0 and i < 9: this_secs = time1.at(i_ms, tol=1) npt.assert_equal(this_secs, ts.TimeArray([i - 1, i, i + 1], time_unit='ms'))
def test_TimeArray_init_int64(): """Make sure that we can initialize TimeArray with an array of ints""" time = ts.TimeArray(np.int64(1)) npt.assert_equal(time.__repr__(), '1.0 s') pass
def test_UniformTime(): tuc = ts.time_unit_conversion for unit, duration in zip(['ns', 'ms', 's', None], [2 * 10 ** 9, 2 * 10 ** 6, 100, 20]): t1 = ts.UniformTime(duration=duration, sampling_rate=1, time_unit=unit) t2 = ts.UniformTime(duration=duration, sampling_rate=20, time_unit=unit) #The following two tests verify that first-last are equal to the #duration, but it is unclear whether that is really the behavior we #want, because the t_i held by a TimeSeries is the left #(smaller) side of the time-duration defined by the bin #The difference between the first and last item is the duration: #npt.assert_equal(t1[-1]-t1[0], # ts.TimeArray(duration,time_unit=unit)) #Duration doesn't depend on the sampling rate: #npt.assert_equal(t1[-1]-t2[0], # ts.TimeArray(duration,time_unit=unit)) a = ts.UniformTime(duration=10, sampling_rate=1) b = ts.UniformTime(a, time_unit=unit) npt.assert_equal(a.sampling_interval, b.sampling_interval) npt.assert_equal(a.sampling_rate, b.sampling_rate) b = ts.UniformTime(a, duration=2 * duration, time_unit=unit) npt.assert_equal(a.sampling_interval, b.sampling_interval) npt.assert_equal(a.sampling_rate, b.sampling_rate) b = ts.UniformTime(a, length=100, time_unit=unit) npt.assert_equal(a.sampling_interval, b.sampling_interval) npt.assert_equal(a.sampling_rate, b.sampling_rate) b = ts.UniformTime(a, length=100, time_unit=unit) npt.assert_equal(a.sampling_interval, b.sampling_interval) npt.assert_equal(a.sampling_rate, b.sampling_rate) b = ts.UniformTime(a, length=100, duration=duration, time_unit=unit) c = ts.UniformTime(length=100, duration=duration, time_unit=unit) npt.assert_equal(c, b) b = ts.UniformTime(sampling_interval=1, duration=10, time_unit=unit) c = ts.UniformTime(sampling_rate=tuc['s'] / tuc[unit], length=10, time_unit=unit) npt.assert_equal(c, b) #This should raise a value error, because the duration is shorter than #the sampling_interval: npt.assert_raises(ValueError, ts.UniformTime, dict(sampling_interval=10, duration=1)) #Time objects can be initialized with other time objects setting the #duration, sampling_interval and sampling_rate: a = ts.UniformTime(length=1, sampling_rate=1) npt.assert_raises(ValueError, ts.UniformTime, dict(data=a, sampling_rate=10, sampling_interval=.1)) b = ts.UniformTime(duration=2 * a.sampling_interval, sampling_rate=2 * a.sampling_rate) npt.assert_equal(ts.Frequency(b.sampling_rate), ts.Frequency(2 * a.sampling_rate)) npt.assert_equal(b.sampling_interval, ts.TimeArray(0.5 * a.sampling_rate)) b = ts.UniformTime(duration=10, sampling_interval=a.sampling_interval) npt.assert_equal(b.sampling_rate, a.sampling_rate) b = ts.UniformTime(duration=10, sampling_rate=a.sampling_rate) npt.assert_equal(b.sampling_interval, a.sampling_interval) # make sure the t0 ando other attribute is copied a = ts.UniformTime(length=1, sampling_rate=1) b = a.copy() npt.assert_equal(b.duration, a.duration) npt.assert_equal(b.sampling_rate, a.sampling_rate) npt.assert_equal(b.sampling_interval, a.sampling_interval) npt.assert_equal(b.t0, a.t0)
def test_TimeSeries(): """Testing the initialization of the uniform time series object """ #Test initialization with duration: tseries1 = ts.TimeSeries([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], duration=10) tseries2 = ts.TimeSeries([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], sampling_interval=1) npt.assert_equal(tseries1.time, tseries2.time) #downsampling: t1 = ts.UniformTime(length=8, sampling_rate=2) #duration is the same, but we're downsampling to 1Hz tseries1 = ts.TimeSeries(data=[1, 2, 3, 4], time=t1, sampling_rate=1) #If you didn't explicitely provide the rate you want to downsample to, that #is an error: npt.assert_raises(ValueError, ts.TimeSeries, dict(data=[1, 2, 3, 4], time=t1)) tseries2 = ts.TimeSeries(data=[1, 2, 3, 4], sampling_rate=1) tseries3 = ts.TimeSeries(data=[1, 2, 3, 4], sampling_rate=1000, time_unit='ms') #you can specify the sampling_rate or the sampling_interval, to the same #effect, where specificying the sampling_interval is in the units of that #time-series: tseries4 = ts.TimeSeries(data=[1, 2, 3, 4], sampling_interval=1, time_unit='ms') npt.assert_equal(tseries4.time, tseries3.time) #The units you use shouldn't matter - time is time: tseries6 = ts.TimeSeries(data=[1, 2, 3, 4], sampling_interval=0.001, time_unit='s') npt.assert_equal(tseries6.time, tseries3.time) #And this too - perverse, but should be possible: tseries5 = ts.TimeSeries(data=[1, 2, 3, 4], sampling_interval=ts.TimeArray(0.001, time_unit='s'), time_unit='ms') npt.assert_equal(tseries5.time, tseries3.time) #initializing with a UniformTime object: t = ts.UniformTime(length=3, sampling_rate=3) data = [1, 2, 3] tseries7 = ts.TimeSeries(data=data, time=t) npt.assert_equal(tseries7.data, data) data = [1, 2, 3, 4] #If the data is not the right length, that should throw an error: npt.assert_raises(ValueError, ts.TimeSeries, dict(data=data, time=t)) # test basic arithmetics wiht TimeSeries tseries1 = ts.TimeSeries([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], sampling_rate=1) tseries2 = tseries1 + 1 npt.assert_equal(tseries1.data + 1, tseries2.data) npt.assert_equal(tseries1.time, tseries2.time) tseries2 -= 1 npt.assert_equal(tseries1.data, tseries2.data) npt.assert_equal(tseries1.time, tseries2.time) tseries2 = tseries1 * 2 npt.assert_equal(tseries1.data * 2, tseries2.data) npt.assert_equal(tseries1.time, tseries2.time) tseries2 = tseries2 / 2 npt.assert_equal(tseries1.data, tseries2.data) npt.assert_equal(tseries1.time, tseries2.time)
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))
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)