def test_concat_along_first_axis(self): td1 = TimeDimension(Seconds(1), Seconds(2)) ts1 = ArrayWithUnits(np.ones((10, 3)), [td1, IdentityDimension()]) td2 = TimeDimension(Seconds(1), Seconds(2)) ts2 = ArrayWithUnits(np.ones((13, 3)), [td2, IdentityDimension()]) result = ArrayWithUnits.concat([ts1, ts2]) self.assertEqual((23, 3), result.shape)
def test_concat_with_differing_durations(self): td1 = TimeDimension(Seconds(1), Seconds(2)) ts1 = ArrayWithUnits(np.ones((10, 3)), [td1, IdentityDimension()]) td2 = TimeDimension(Seconds(1), Seconds(3)) ts2 = ArrayWithUnits(np.ones((13, 3)), [td2, IdentityDimension()]) self.assertRaises(ValueError, lambda: ArrayWithUnits.concat([ts1, ts2]))
def test_concat_along_second_axis(self): td1 = TimeDimension(Seconds(1), Seconds(2)) ts1 = ArrayWithUnits(np.ones((10, 3)), [td1, IdentityDimension()]) td2 = TimeDimension(Seconds(1), Seconds(2)) ts2 = ArrayWithUnits(np.ones((10, 5)), [td2, IdentityDimension()]) result = ArrayWithUnits.concat([ts1, ts2], axis=1) self.assertEqual((10, 8), result.shape) self.assertIsInstance(result.dimensions[0], TimeDimension) self.assertIsInstance(result.dimensions[1], IdentityDimension)
def test_concat_with_differing_freqs(self): ts = ArrayWithUnits(np.ones( (10, 3)), [TimeDimension(Seconds(2), Seconds(2)), IdentityDimension()]) ts2 = ArrayWithUnits(np.ones( (13, 3)), [TimeDimension(Seconds(1), Seconds(2)), IdentityDimension()]) self.assertRaises(ValueError, lambda: ArrayWithUnits.concat([ts, ts2]))
def test_concatenation_with_matching_freqs_and_duration_results_in_crts( self): ts = ArrayWithUnits(np.ones( (10, 3)), [TimeDimension(Seconds(1), Seconds(2)), IdentityDimension()]) ts2 = ArrayWithUnits(np.ones( (13, 3)), [TimeDimension(Seconds(1), Seconds(2)), IdentityDimension()]) result = ts.concatenate(ts2) self.assertIsInstance(result, ArrayWithUnits) self.assertEqual((23, 3), result.shape)
def __new__(cls, array, samplerate): if array.ndim == 1: dimensions = [TimeDimension(*samplerate)] elif array.ndim == 2: dimensions = [TimeDimension(*samplerate), IdentityDimension()] else: raise ValueError( 'array must be one (mono) or two (multi-channel) dimensions') if not isinstance(samplerate, AudioSampleRate): raise TypeError('samplerate should be an AudioSampleRate instance') return ArrayWithUnits.__new__(cls, array, dimensions)
def test_can_slice_time_series_with_open_ended_time_slice(self): arr = np.arange(10) freq = Seconds(1) ts = ArrayWithUnits(arr, [TimeDimension(freq)]) sl = TimeSlice(None, start=Seconds(2)) ts2 = ts[sl] self.assertEqual(8, len(ts2))
def test_can_add_constant_factor_to_time_series(self): arr = np.arange(10) freq = Seconds(1) ts = ArrayWithUnits(arr, [TimeDimension(freq)]) ts2 = ts + 10 self.assertTrue(np.all(np.arange(10, 20) == ts2)) self.assertIsInstance(ts2, ArrayWithUnits)
def test_can_slice_constant_rate_time_series_with_integer_indices(self): arr = np.arange(10) freq = Seconds(1) ts = ArrayWithUnits(arr, [TimeDimension(freq)]) ts2 = ts[:5] self.assertEqual(5, len(ts2)) self.assertIsInstance(ts2, ArrayWithUnits)
def test_can_mix_time_slice_and_integer_indices(self): arr = np.ones((10, 5)) freq = Seconds(1) ts = ArrayWithUnits(arr, [TimeDimension(freq), IdentityDimension()]) sl = TimeSlice(duration=Seconds(5), start=Seconds(5)) ts2 = ts[sl, 2:] self.assertEqual((5, 3), ts2.shape)
def test_can_get_entire_time_series_with_empty_time_slice(self): arr = np.arange(10) freq = Seconds(1) ts = ArrayWithUnits(arr, [TimeDimension(freq)]) sl = TimeSlice() ts2 = ts[sl] self.assertEqual(10, len(ts2))
def test_can_get_entire_time_series_with_empty_slice(self): arr = np.arange(10) freq = Seconds(1) duration = Milliseconds(500) ts = ArrayWithUnits(arr, [TimeDimension(freq, duration)]) ts2 = ts[:] self.assertIsInstance(ts2, ArrayWithUnits) self.assertTrue(np.all(np.arange(10) == ts2))
def test_get_empty_time_series_when_using_out_of_range_time_slice(self): arr = np.arange(10) freq = Seconds(1) ts = ArrayWithUnits(arr, [TimeDimension(freq)]) sl = TimeSlice(Seconds(2), start=Seconds(11)) ts2 = ts[sl] self.assertEqual(0, ts2.size) self.assertIsInstance(ts2, ArrayWithUnits)
def test_discrete_samples_44100(self): sr = SR44100() ts = ArrayWithUnits(np.zeros(sr.samples_per_second), [TimeDimension(*sr)]) hl = HalfLapped() freq, duration = hl.discrete_samples(ts) self.assertEqual(1024, freq) self.assertEqual(2048, duration)
def test_sum_along_second_axis(self): td = TimeDimension(Seconds(1), Seconds(2)) ts = ArrayWithUnits(np.ones((10, 3)), [td, IdentityDimension()]) result = ts.sum(axis=1) self.assertIsInstance(result, ArrayWithUnits) self.assertEqual((10, ), result.shape) self.assertEqual(1, len(result.dimensions)) self.assertIsInstance(result.dimensions[0], TimeDimension)
def test_time_slice_spanning_multiple_samples_returns_all_samples(self): arr = np.arange(10) freq = Seconds(1) ts = ArrayWithUnits(arr, [TimeDimension(freq)]) sl = TimeSlice(Milliseconds(2000), start=Milliseconds(1500)) ts2 = ts[sl] self.assertIsInstance(ts2, ArrayWithUnits) self.assertEqual(3, ts2.size) self.assertTrue(np.all(np.arange(1, 4) == ts2))
def test_time_slice_spanning_less_than_one_sample_returns_one_sample(self): arr = np.arange(10) freq = Seconds(1) ts = ArrayWithUnits(arr, [TimeDimension(freq)]) sl = TimeSlice(Milliseconds(100), start=Milliseconds(1500)) ts2 = ts[sl] self.assertIsInstance(ts2, ArrayWithUnits) self.assertEqual(1, ts2.size) self.assertEqual(1, ts2[0])
def test_frequency_less_than_one(self): arr = np.arange(10) freq = Milliseconds(500) ts = ArrayWithUnits(arr, [TimeDimension(freq)]) sl = TimeSlice(Seconds(2), start=Milliseconds(600)) ts2 = ts[sl] self.assertIsInstance(ts2, ArrayWithUnits) self.assertEqual(5, ts2.size) self.assertTrue(np.all(np.arange(1, 6) == ts2))
def test_can_sum_2d_timeseries(self): arr = np.zeros((10, 3)) freq = Seconds(1) ts = ArrayWithUnits(arr, [TimeDimension(freq), IdentityDimension()]) ts2 = ts.sum(axis=1) self.assertIsInstance(ts2, ArrayWithUnits) self.assertEqual(1, len(ts2.dimensions)) self.assertEqual(freq, ts2.dimensions[0].frequency) self.assertEqual(freq, ts2.dimensions[0].duration)
def test_frequency_and_duration_differ3(self): arr = np.arange(10) freq = Seconds(1) duration = Seconds(3) ts = ArrayWithUnits(arr, [TimeDimension(freq, duration)]) sl = TimeSlice(Seconds(2), start=Seconds(6)) ts2 = ts[sl] self.assertIsInstance(ts2, ArrayWithUnits) self.assertEqual(4, ts2.size) self.assertTrue(np.all(np.arange(4, 8) == ts2))
def test_frequency_less_than_one_freq_and_duration_differ(self): arr = np.arange(10) freq = Milliseconds(500) duration = Seconds(1) ts = ArrayWithUnits(arr, [TimeDimension(freq, duration)]) sl = TimeSlice(Seconds(3), start=Milliseconds(250)) ts2 = ts[sl] self.assertIsInstance(ts2, ArrayWithUnits) self.assertEqual(7, ts2.size) self.assertTrue(np.all(np.arange(0, 7) == ts2))
def test_duration_less_than_frequency(self): arr = np.arange(10) freq = Seconds(1) duration = Milliseconds(500) ts = ArrayWithUnits(arr, [TimeDimension(freq, duration)]) sl = TimeSlice(Seconds(3), start=Milliseconds(1250)) ts2 = ts[sl] self.assertIsInstance(ts2, ArrayWithUnits) self.assertEqual(4, ts2.size) self.assertTrue(np.all(np.arange(1, 5) == ts2))
def test_samplerate_three_per_second(self): arr = np.arange(10) freq = Milliseconds(333) ts = ArrayWithUnits(arr, [TimeDimension(freq)]) self.assertEqual(3, int(ts.dimensions[0].samples_per_second))
def test_equals(self): td1 = TimeDimension(Seconds(1), Milliseconds(900)) td2 = TimeDimension(Seconds(1), Milliseconds(900)) self.assertEqual(td1, td2)
def test_integer_based_slice(self): td = TimeDimension(*SR44100(), size=44100 * 5) sl = td.integer_based_slice(TimeSlice(duration=Seconds(1))) self.assertEqual(slice(0, 44100), sl)
def test_duration_is_equal_to_frequency_if_not_provided(self): td = TimeDimension(Seconds(1)) self.assertEqual(Seconds(1), td.frequency) self.assertEqual(Seconds(1), td.duration)
def test_raises_if_duration_is_not_timedelta_instance(self): self.assertRaises(ValueError, lambda: TimeDimension(Seconds(2), 's'))
def test_raises_if_frequency_is_not_timedelta_instance(self): self.assertRaises(ValueError, lambda: TimeDimension('s'))
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_samplerate_audio(self): arr = np.arange(10) freq = Picoseconds(int(1e12)) / 44100. ts = ArrayWithUnits(arr, [TimeDimension(freq)]) self.assertEqual(44100, int(ts.dimensions[0].samples_per_second))