def test_does_not_contain_slice_completely_after(self): ts1 = TimeSlice(Seconds(100), start=Seconds(200)) ts2 = TimeSlice(Seconds(100), Seconds(310)) self.assertFalse(ts2 in ts1)
def test_contains_point_in_time_during(self): ts = TimeSlice(Seconds(100), start=Seconds(200)) self.assertTrue(Seconds(210) in ts)
def end(self): try: return self._data.timeslice[-1].end except IndexError: return Seconds(0)
def test_can_intersect_two_time_slices(self): ts1 = TimeSlice(Seconds(100), start=Seconds(100)) ts2 = TimeSlice(Seconds(100), start=Seconds(101)) intersection = ts1 & ts2 self.assertEqual(Seconds(99), intersection.duration)
def test_seconds_equal(self): a = Seconds(1) b = np.timedelta64(1, 's') self.assertEqual(a, b)
def test_samplerate_one_per_second(self): arr = np.arange(10) freq = Seconds(1) ts = ArrayWithUnits(arr, [TimeDimension(freq)]) self.assertEqual(1, ts.dimensions[0].samples_per_second)
def test_can_repr_open_ended_slice(self): r = repr(TimeSlice(start=Seconds(2))) self.assertIsNotNone(r)
def test_can_slice_time_series_with_negative_end_time(self): arr = np.arange(10) freq = Seconds(1) ts = ArrayWithUnits(arr, [TimeDimension(freq)]) sliced = ts[:-Seconds(2)] self.assertEqual(8, len(sliced))
def test_can_index_constant_rate_time_series_with_integer_index(self): arr = np.arange(10) freq = Seconds(1) ts = ArrayWithUnits(arr, [TimeDimension(freq)]) ts2 = ts[5] self.assertEqual(5, ts2)
def test_ne_when_starts_differ(self): ts1 = TimeSlice(Seconds(2), start=Seconds(2)) ts2 = TimeSlice(Seconds(2), start=Seconds(3)) self.assertNotEqual(ts1, ts2)
def test_can_slice_time_series_with_start_and_end_time(self): arr = np.arange(10) freq = Seconds(1) ts = ArrayWithUnits(arr, [TimeDimension(freq)]) sliced = ts[Seconds(5):Seconds(7)] self.assertEqual(2, len(sliced))
def test_eq_when_start_and_duration_equal(self): ts1 = TimeSlice(Seconds(2), start=Seconds(2)) ts2 = TimeSlice(Seconds(2), start=Seconds(2)) self.assertEqual(ts1, ts2)
def test_raises_value_error_if_item_is_not_timedelta_or_timeslice(self): ts1 = TimeSlice(Seconds(100), start=Seconds(200)) self.assertRaises(ValueError, lambda: 's' in ts1)
def test_does_not_contain_slice_beginning_after(self): ts1 = TimeSlice(Seconds(100), start=Seconds(200)) ts2 = TimeSlice(Seconds(100), Seconds(210)) self.assertFalse(ts2 in ts1)
def test_span_duration_less_than_frequency(self): arr = np.arange(10) freq = Seconds(1) duration = Milliseconds(500) ts = ArrayWithUnits(arr, [TimeDimension(freq, duration)]) self.assertEqual(TimeSlice(Milliseconds(9500)), ts.dimensions[0].span)
def test_can_index_2d_time_series_with_single_integer_index(self): arr = np.random.random_sample((10, 3)) freq = Seconds(1) ts = ArrayWithUnits(arr, [TimeDimension(freq), IdentityDimension()]) ts2 = ts[5] self.assertEqual((3, ), ts2.shape)
def test_duration_in_seconds_two_seconds(self): arr = np.arange(10) freq = Seconds(2) ts = ArrayWithUnits(arr, [TimeDimension(freq)]) self.assertEqual(2, ts.dimensions[0].duration_in_seconds)
def test_get_index_error_when_using_out_of_range_int_index(self): arr = np.arange(10) freq = Seconds(1) ts = ArrayWithUnits(arr, [TimeDimension(freq)]) self.assertRaises(IndexError, lambda: ts[100])
def test_concatenation_with_differing_freqs_and_durations_raises(self): ts = ArrayWithUnits(np.arange(10), [TimeDimension(Seconds(1), Seconds(2))]) ts2 = ArrayWithUnits(np.arange(10, 20), [TimeDimension(Seconds(1), Seconds(1))]) self.assertRaises(ValueError, lambda: ts.concatenate(ts2))
def test_different_units_equal(self): self.assertEqual(Seconds(1), Milliseconds(1000))
def test_can_instantiate_time_slice_instance_without_start_argument(self): duration = Seconds(100) ts = TimeSlice(duration) self.assertEqual(duration, ts.duration) self.assertEqual(Seconds(0), ts.start)
def test_equivalent_timeslices_hash_to_the_same_value(self): ts1 = TimeSlice(start=Seconds(1), duration=Seconds(7)) ts2 = TimeSlice(start=Milliseconds(1000), duration=Milliseconds(7000)) self.assertEqual(hash(ts1), hash(ts2))
def test_can_find_null_intersection(self): ts1 = TimeSlice(Seconds(100), start=Seconds(100)) ts2 = TimeSlice(Seconds(100), start=Seconds(200)) intersection = ts1 & ts2 self.assertEqual(Seconds(0), intersection.duration)
def test_exactly_equal_timeslices_hash_to_the_same_value(self): ts1 = TimeSlice(start=Seconds(1), duration=Seconds(7)) ts2 = TimeSlice(start=Seconds(1), duration=Seconds(7)) self.assertEqual(hash(ts1), hash(ts2))
def test_does_not_contain_point_in_time_before(self): ts = TimeSlice(Seconds(100), start=Seconds(200)) self.assertFalse(Seconds(10) in ts)
def test_span_freq_and_duration_equal(self): arr = np.arange(10) freq = Seconds(1) ts = ArrayWithUnits(arr, [TimeDimension(freq)]) self.assertEqual(TimeSlice(Seconds(10)), ts.dimensions[0].span)
def span(self): try: start = self._data.timeslice[0].start return TimeSlice(start=start, duration=self.end - start) except IndexError: return TimeSlice(duration=Seconds(0))
def test_different_timeslices_hash_to_different_values(self): ts1 = TimeSlice(start=Seconds(1), duration=Seconds(7)) ts2 = TimeSlice(start=Seconds(1), duration=Seconds(8)) self.assertNotEqual(hash(ts1), hash(ts2))
def __str__(self): f = self.frequency / Seconds(1) d = self.duration / Seconds(1) return '{self.__class__.__name__}(f={f}, d={d})'.format(**locals())
def test_contains_slice(self): ts1 = TimeSlice(Seconds(100), start=Seconds(200)) ts2 = TimeSlice(Seconds(10), Seconds(250)) self.assertTrue(ts2 in ts1)