def test_Frequency(): """Test frequency representation object""" tuc = ts.time_unit_conversion for unit in ['ns', 'ms', 's', None]: f = ts.Frequency(1, time_unit=unit) npt.assert_equal(f.to_period(), tuc[unit]) f = ts.Frequency(1000, time_unit=unit) npt.assert_equal(f.to_period(), tuc[unit] / 1000) f = ts.Frequency(0.001, time_unit=unit) npt.assert_equal(f.to_period(), tuc[unit] * 1000)
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)