예제 #1
0
 def test_can_invert_frequency_weighting(self):
     td = TimeDimension(Seconds(1), Seconds(1))
     fd = FrequencyDimension(LinearScale(FrequencyBand(20, 22050), 100))
     tf = ArrayWithUnits(np.random.random_sample((90, 100)), [td, fd])
     weighted = tf * AWeighting()
     inverted = weighted / AWeighting()
     np.testing.assert_allclose(tf, inverted)
예제 #2
0
 def test_from_example(self):
     td = TimeDimension(Seconds(1), Seconds(1))
     fd = FrequencyDimension(LinearScale(FrequencyBand(20, 22050), 100))
     tf = ArrayWithUnits(np.ones((30, 100)), [td, fd])
     from_example = ArrayWithUnits.from_example(np.ones((30, 100)), tf)
     self.assertEqual(tf.shape, from_example.shape)
     self.assertSequenceEqual(tf.dimensions, from_example.dimensions)
예제 #3
0
    def test_can_pool_non_stored_time_slice_feature(self):
        @simple_in_memory_settings
        class WithOnsets(self.STFT):
            transience = ArrayWithUnitsFeature(MeasureOfTransience,
                                               needs=self.STFT.fft,
                                               store=True)

            sliding_detection = ArrayWithUnitsFeature(
                SlidingWindow,
                needs=transience,
                wscheme=self.wscheme * Stride(frequency=1, duration=10),
                padwith=5,
                store=False)

            slices = TimeSliceFeature(MovingAveragePeakPicker,
                                      needs=sliding_detection,
                                      aggregate=np.median,
                                      store=True)

        signal = self.ticks(self.samplerate, Seconds(4), Seconds(1))
        _id = WithOnsets.process(meta=signal)

        class WithPooled(WithOnsets):
            pooled = VariableRateTimeSeriesFeature(Pooled,
                                                   needs=(self.STFT.fft,
                                                          WithOnsets.slices),
                                                   op=np.max,
                                                   axis=0,
                                                   store=True)

        doc = WithPooled(_id)
        self.assertEqual((4, 1025), doc.pooled.slicedata.shape)
예제 #4
0
 def test_inversion_returns_time_series(self):
     data = np.random.random_sample((33, 30))
     ts = ArrayWithUnits(data,
                         [TimeDimension(Seconds(1)),
                          IdentityDimension()])
     inverted = self.invert_and_assert_class(ts)
     self.assertEqual(Seconds(1), inverted.dimensions[0].frequency)
예제 #5
0
    def test_can_apply_sliding_window_to_time_frequency_representation(self):
        band = FrequencyBand(0, 22000)
        scale = LinearScale(band, 100)
        arr = ArrayWithUnits(np.zeros(
            (200,
             100)), [TimeDimension(Seconds(1)),
                     FrequencyDimension(scale)])
        sw = SampleRate(Seconds(2), Seconds(2))

        @simple_in_memory_settings
        class Document(BaseModel):
            windowed = ArrayWithUnitsFeature(SlidingWindow,
                                             wscheme=sw,
                                             store=True)

        _id = Document.process(windowed=arr)
        result = Document(_id).windowed

        self.assertIsInstance(result, ArrayWithUnits)
        self.assertEqual((100, 2, 100), result.shape)
        self.assertEqual(3, len(result.dimensions))
        self.assertIsInstance(result.dimensions[0], TimeDimension)
        self.assertEqual(Seconds(2), result.dimensions[0].frequency)
        self.assertIsInstance(result.dimensions[1], TimeDimension)
        self.assertEqual(Seconds(1), result.dimensions[1].frequency)
        self.assertIsInstance(result.dimensions[2], FrequencyDimension)
예제 #6
0
 def test_can_create_instance_with_no_slice_data(self):
     ts = VariableRateTimeSeries((
         (TimeSlice(start=Seconds(1), duration=Seconds(1)), np.zeros(0)),
         (TimeSlice(start=Seconds(2), duration=Seconds(1)), np.zeros(0)),
     ))
     self.assertEqual(2, len(ts))
     self.assertEqual((2, 0), ts.slicedata.shape)
예제 #7
0
    def test_can_round_trip_3d_constant_rate_time_series_with_frequency_dim(
            self):
        dim1 = TimeDimension(Seconds(2), Milliseconds(1000))
        dim2 = TimeDimension(Seconds(1), Milliseconds(500))
        scale = LinearScale(FrequencyBand(20, 20000), 100)
        dim3 = FrequencyDimension(scale)
        raw = np.random.random_sample((5, 2, 100))
        ts = ArrayWithUnits(raw, (dim1, dim2, dim3))

        decoded = self._roundtrip(ts)
        self.assertIsInstance(decoded, ArrayWithUnits)
        self.assertEqual(3, len(decoded.dimensions))

        td1 = decoded.dimensions[0]
        self.assertIsInstance(td1, TimeDimension)
        self.assertEqual(Seconds(2), td1.frequency)
        self.assertEqual(Milliseconds(1000), td1.duration)

        td2 = decoded.dimensions[1]
        self.assertIsInstance(td2, TimeDimension)
        self.assertEqual(Seconds(1), td2.frequency)
        self.assertEqual(Milliseconds(500), td2.duration)

        fd = decoded.dimensions[2]
        self.assertIsInstance(fd, FrequencyDimension)
        self.assertEqual(scale, fd.scale)
        np.testing.assert_allclose(decoded, raw)
예제 #8
0
 def test_generates_correct_number_of_samples(self):
     samplerate = SR22050()
     duration = Seconds(1)
     ss = SineSynthesizer(samplerate)
     audio = ss.synthesize(duration, freqs_in_hz=[440.])
     expected_samples = int(samplerate) * int(duration / Seconds(1))
     self.assertEqual(expected_samples, len(audio))
예제 #9
0
 def test_can_apply_a_weighting_to_time_frequency_representation(self):
     td = TimeDimension(Seconds(1), Seconds(1))
     fd = FrequencyDimension(LinearScale(FrequencyBand(20, 22050), 100))
     tf = ArrayWithUnits(np.ones((90, 100)), [td, fd])
     weighting = AWeighting()
     result = tf * weighting
     self.assertGreater(result[0, -1], result[0, 0])
예제 #10
0
 def test_can_get_weights_from_tf_representation(self):
     td = TimeDimension(Seconds(1), Seconds(1))
     fd = FrequencyDimension(LinearScale(FrequencyBand(20, 22050), 100))
     tf = ArrayWithUnits(np.ones((90, 100)), [td, fd])
     weighting = AWeighting()
     weights = weighting.weights(tf)
     self.assertEqual((100,), weights.shape)
예제 #11
0
 def test_can_round_trip(self):
     td = TimeDimension(Seconds(1), Milliseconds(500), 100)
     encoded = self.encoder.encode(td)
     decoded = self.decoder.decode(encoded)
     self.assertIsInstance(decoded, TimeDimension)
     self.assertEqual(Seconds(1), decoded.frequency)
     self.assertEqual(Milliseconds(500), decoded.duration)
     self.assertEqual(100, decoded.size)
예제 #12
0
 def test_can_construct_instance(self):
     frequency = Seconds(1)
     duration = Seconds(1)
     scale = LinearScale(FrequencyBand(20, 22050), 100)
     td = TimeDimension(frequency, duration)
     fd = FrequencyDimension(scale)
     tf = ArrayWithUnits(np.zeros((30, 100)), [td, fd])
     self.assertIsInstance(tf, ArrayWithUnits)
예제 #13
0
 def test_sum_along_frequency_axis(self):
     td = TimeDimension(Seconds(1), Seconds(1))
     fd = FrequencyDimension(LinearScale(FrequencyBand(20, 22050), 100))
     tf = ArrayWithUnits(np.ones((30, 100)), [td, fd])
     result = tf.sum(axis=1)
     self.assertIsInstance(result, ArrayWithUnits)
     self.assertEqual(1, len(result.dimensions))
     self.assertEqual((30, ), result.shape)
     self.assertIsInstance(result.dimensions[0], TimeDimension)
예제 #14
0
 def test_can_use_negative_axis_indices_max(self):
     td = TimeDimension(Seconds(1), Seconds(1))
     fd = FrequencyDimension(LinearScale(FrequencyBand(20, 22050), 100))
     tf = ArrayWithUnits(np.ones((30, 100)), [td, fd])
     result = tf.max(axis=-1)
     self.assertIsInstance(result, ArrayWithUnits)
     self.assertEqual(1, len(result.dimensions))
     self.assertEqual((30, ), result.shape)
     self.assertIsInstance(result.dimensions[0], TimeDimension)
예제 #15
0
 def test_can_apply_empty_time_slice_to_wrapper(self):
     synth = SineSynthesizer(SR11025())
     samples = synth.synthesize(Seconds(10))
     encoded = samples.encode(fmt='OGG', subtype='VORBIS')
     wrapper = OggVorbisWrapper(encoded)
     samples = wrapper[TimeSlice()]
     expected = Seconds(10) / Seconds(1)
     actual = samples.end / Seconds(1)
     self.assertAlmostEqual(expected, actual, places=6)
예제 #16
0
 def test_can_slice_frequency_dimension_with_integer_indices(self):
     frequency = Seconds(1)
     duration = Seconds(1)
     scale = LinearScale(FrequencyBand(20, 22050), 100)
     td = TimeDimension(frequency, duration)
     fd = FrequencyDimension(scale)
     tf = ArrayWithUnits(np.zeros((30, 100)), [td, fd])
     sliced = tf[:, 10:20]
     self.assertEqual((30, 10), sliced.shape)
     self.assertIsInstance(sliced, ArrayWithUnits)
예제 #17
0
    def test_raises_if_scale_length_does_not_match_frequency_dimension(self):
        frequency = Seconds(1)
        duration = Seconds(1)
        scale = LinearScale(FrequencyBand(20, 22050), 1000)

        td = TimeDimension(frequency, duration)
        fd = FrequencyDimension(scale)

        self.assertRaises(ValueError,
                          lambda: ArrayWithUnits(np.ones((30, 100)), [td, fd]))
예제 #18
0
 def test_can_access_int_index_and_frequency_band(self):
     tf = ArrayWithUnits(np.ones((10, 10)),
                         dimensions=[
                             TimeDimension(Seconds(1), Seconds(1)),
                             FrequencyDimension(
                                 LinearScale(FrequencyBand(0, 1000), 10))
                         ])
     sliced = tf[0, FrequencyBand(201, 400)]
     self.assertEqual((2, ), sliced.shape)
     self.assertIsInstance(sliced.dimensions[0], FrequencyDimension)
예제 #19
0
 def test_can_access_time_slice_and_int_index(self):
     tf = ArrayWithUnits(np.ones((10, 10)),
                         dimensions=[
                             TimeDimension(Seconds(1), Seconds(1)),
                             FrequencyDimension(
                                 LinearScale(FrequencyBand(0, 1000), 10))
                         ])
     sliced = tf[TimeSlice(start=Seconds(1), duration=Seconds(2)), 0]
     self.assertEqual((2, ), sliced.shape)
     self.assertIsInstance(sliced.dimensions[0], TimeDimension)
예제 #20
0
 def test_can_use_keepdims_with_sum(self):
     td = TimeDimension(Seconds(1), Seconds(1))
     fd = FrequencyDimension(LinearScale(FrequencyBand(20, 22050), 100))
     tf = ArrayWithUnits(np.ones((30, 100)), [td, fd])
     result = tf.sum(axis=-1, keepdims=True)
     self.assertIsInstance(result, ArrayWithUnits)
     self.assertEqual(2, len(result.dimensions))
     self.assertEqual((30, 1), result.shape)
     self.assertIsInstance(result.dimensions[0], TimeDimension)
     self.assertIsInstance(result.dimensions[1], IdentityDimension)
예제 #21
0
 def test_can_multiply_by_array(self):
     frequency = Seconds(1)
     duration = Seconds(1)
     scale = LinearScale(FrequencyBand(20, 22050), 100)
     td = TimeDimension(frequency, duration)
     fd = FrequencyDimension(scale)
     tf = ArrayWithUnits(np.ones((30, 100)), [td, fd])
     result = tf * np.ones(100)
     self.assertIsInstance(result, ArrayWithUnits)
     np.testing.assert_allclose(tf, result)
예제 #22
0
 def test_can_add_axis_at_end(self):
     _id = IdentityDimension()
     td = TimeDimension(Seconds(1), Seconds(1))
     fd = FrequencyDimension(LinearScale(FrequencyBand(20, 22050), 100))
     tf = ArrayWithUnits(np.ones((3, 30, 100)), [_id, td, fd])
     tf2 = tf[..., None]
     self.assertEqual(4, tf2.ndim)
     self.assertIsInstance(tf2.dimensions[0], IdentityDimension)
     self.assertIsInstance(tf2.dimensions[1], TimeDimension)
     self.assertIsInstance(tf2.dimensions[2], FrequencyDimension)
     self.assertIsInstance(tf2.dimensions[3], IdentityDimension)
예제 #23
0
 def test_inversion_returns_time_frequency_representation(self):
     data = np.random.random_sample((33, 30))
     scale = LinearScale(FrequencyBand(20, 20000), 30)
     tf = ArrayWithUnits(
         data,
         [TimeDimension(Seconds(1), Seconds(2)),
          FrequencyDimension(scale)])
     inverted = self.invert_and_assert_class(tf)
     self.assertEqual(Seconds(1), inverted.dimensions[0].frequency)
     self.assertEqual(Seconds(2), inverted.dimensions[0].duration)
     self.assertEqual(scale, inverted.dimensions[1].scale)
예제 #24
0
 def test_can_slice_freq_dimension_with_freq_band_spanning_bins(self):
     frequency = Seconds(1)
     duration = Seconds(1)
     scale = LinearScale(FrequencyBand(20, 22050), 100)
     td = TimeDimension(frequency, duration)
     fd = FrequencyDimension(scale)
     tf = ArrayWithUnits(np.zeros((30, 100)), [td, fd])
     bands = list(scale)
     wide_band = FrequencyBand(bands[0].start_hz, bands[9].stop_hz)
     sliced = tf[:, wide_band]
     self.assertEqual((30, 10), sliced.shape)
     self.assertIsInstance(sliced, ArrayWithUnits)
예제 #25
0
 def test_can_round_trip_1d_constant_rate_time_series(self):
     dim = TimeDimension(Seconds(1), Milliseconds(500))
     raw = np.arange(10)
     ts = ArrayWithUnits(raw, (dim, ))
     decoded = self._roundtrip(ts)
     self.assertIsInstance(decoded, ArrayWithUnits)
     self.assertEqual(1, len(decoded.dimensions))
     self.assertIsInstance(decoded.dimensions[0], TimeDimension)
     td = decoded.dimensions[0]
     self.assertEqual(Seconds(1), td.frequency)
     self.assertEqual(Milliseconds(500), td.duration)
     np.testing.assert_allclose(decoded, raw)
예제 #26
0
 def test_can_iterate_over_timeseries(self):
     tf = ArrayWithUnits(np.ones((10, 10)),
                         dimensions=[
                             TimeDimension(Seconds(1), Seconds(1)),
                             IdentityDimension()
                         ])
     rows = [row for row in tf]
     self.assertEqual(10, len(rows))
     for row in rows:
         self.assertIsInstance(row, ArrayWithUnits)
         self.assertIsInstance(row.dimensions[0], IdentityDimension)
         self.assertEqual((10, ), row.shape)
예제 #27
0
 def test_can_use_tuple_indices_for_first_dimension(self):
     tf = ArrayWithUnits(np.ones((10, 10)),
                         dimensions=[
                             TimeDimension(Seconds(1), Seconds(1)),
                             FrequencyDimension(
                                 LinearScale(FrequencyBand(0, 1000), 10))
                         ])
     subset = tf[tuple([2, 4, 6]), ...]
     self.assertEqual((3, 10), subset.shape)
     self.assertIsInstance(subset, ArrayWithUnits)
     self.assertIsInstance(subset.dimensions[0], TimeDimension)
     self.assertIsInstance(subset.dimensions[1], FrequencyDimension)
예제 #28
0
 def test_can_iterate_over_time_frequency_representation(self):
     tf = ArrayWithUnits(np.ones((10, 10)),
                         dimensions=[
                             TimeDimension(Seconds(1), Seconds(1)),
                             FrequencyDimension(
                                 LinearScale(FrequencyBand(0, 1000), 10))
                         ])
     rows = [row for row in tf]
     self.assertEqual(10, len(rows))
     for row in rows:
         self.assertIsInstance(row, ArrayWithUnits)
         self.assertIsInstance(row.dimensions[0], FrequencyDimension)
         self.assertEqual((10, ), row.shape)
예제 #29
0
 def test_span(self):
     ts = VariableRateTimeSeries(
         ((TimeSlice(start=Seconds(0), duration=Seconds(1)), np.zeros(10)),
          (TimeSlice(start=Seconds(1), duration=Seconds(2)), np.zeros(10)),
          (TimeSlice(start=Seconds(3), duration=Seconds(1)), np.zeros(10))))
     self.assertEqual(TimeSlice(start=Seconds(0), duration=Seconds(4)),
                      ts.span)
예제 #30
0
 def test_scale_is_modified_after_slice(self):
     frequency = Seconds(1)
     duration = Seconds(1)
     scale = LinearScale(FrequencyBand(20, 22050), 100)
     td = TimeDimension(frequency, duration)
     fd = FrequencyDimension(scale)
     tf = ArrayWithUnits(np.zeros((30, 100)), [td, fd])
     bands = list(scale)
     wide_band = FrequencyBand(bands[0].start_hz, bands[9].stop_hz)
     sliced = tf[:, wide_band]
     self.assertEqual((30, 10), sliced.shape)
     self.assertIsInstance(sliced, ArrayWithUnits)
     self.assertLess(sliced.dimensions[1].scale.stop_hz, scale.stop_hz)
     self.assertEqual(10, sliced.dimensions[1].scale.n_bands)