Exemplo n.º 1
0
 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)
Exemplo n.º 2
0
 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]))
Exemplo n.º 3
0
 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)
Exemplo n.º 4
0
 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]))
Exemplo n.º 5
0
 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)
Exemplo n.º 6
0
 def test_can_invert_array_with_units(self):
     td = TimeDimension(Seconds(1))
     fd = FrequencyDimension(LinearScale(FrequencyBand(0, 20000), 100))
     dimensions = [IdentityDimension(), td, fd]
     training = ArrayWithUnits(np.zeros((10, 5, 100)), dimensions)
     Model = self.get_model(slicex=FrequencyBand(1000, 10000))
     _id = Model.process(sliced=training)
     model = Model(_id)
     data = ArrayWithUnits(np.ones((2, 5, 100)), dimensions)
     transformed = model.pipeline.transform(data)
     inverted = transformed.inverse_transform()
     self.assertEqual((2, 5, 100), inverted.shape)
     self.assertEqual(IdentityDimension(), inverted.dimensions[0])
     self.assertEqual(td, inverted.dimensions[1])
     self.assertEqual(fd, inverted.dimensions[2])
Exemplo n.º 7
0
    def _enqueue(self, data, pusher):
        if self._r is None:
            shape = (self._nsamples,) + data.shape[1:]
            self._r = np.zeros(shape, dtype=data.dtype)
            try:
                self._r = ArrayWithUnits(
                    self._r, (IdentityDimension(),) + data.dimensions[1:])
            except AttributeError:
                # samples were likely a plain numpy array, and not an
                # ArrayWithUnits instance
                pass

        diff = 0
        if self._index < self._nsamples:
            diff = self._nsamples - self._index
            available = len(data[:diff])
            self._r[self._index: self._index + available] = data[:diff]
            self._index += available

        remaining = len(data[diff:])
        if not remaining:
            return
        indices = np.random.random_integers(0, self._index, size=remaining)
        indices = indices[indices < self._nsamples]
        self._r[indices, ...] = data[diff:][list(range(len(indices)))]
        self._index += remaining
Exemplo n.º 8
0
 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)
Exemplo n.º 9
0
 def test_should_preserve_time_dimension_in_forward_transform(self):
     td = TimeDimension(Seconds(1))
     td2 = TimeDimension(Milliseconds(500))
     fd = FrequencyDimension(LinearScale(FrequencyBand(10, 100), 3))
     training_data = ArrayWithUnits(np.zeros((10, 5, 3)),
                                    dimensions=(IdentityDimension(), td2,
                                                fd))
     _id = Document.process(l=training_data)
     doc = Document(_id)
     test_data = ArrayWithUnits(np.zeros((11, 5, 3)),
                                dimensions=(td, td2, fd))
     result = doc.pipeline.transform(test_data)
     self.assertEqual((11, 15), result.data.shape)
     self.assertIsInstance(result.data, ArrayWithUnits)
     self.assertEqual(td, result.data.dimensions[0])
     self.assertEqual(IdentityDimension(), result.data.dimensions[1])
Exemplo n.º 10
0
def categorical(x, mu=255, normalize=True):
    """
    Mu-law compress a block of audio samples, and convert them into a
    categorical distribution
    """

    if normalize:
        # normalize the signal
        mx = x.max()
        x = np.divide(x, mx, where=mx != 0)

    # mu law compression
    x = mu_law(x)

    # translate and scale to [0, 1]
    x = (x - x.min()) * 0.5

    # convert to the range [0, 255]
    x = (x * mu).astype(np.uint8)

    # create the array to house the categorical representation
    c = np.zeros((np.product(x.shape), mu + 1), dtype=np.uint8)
    c[np.arange(len(c)), x.flatten()] = 1

    return ArrayWithUnits(c.reshape(x.shape + (mu + 1, )),
                          x.dimensions + (IdentityDimension(), ))
Exemplo n.º 11
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)
Exemplo n.º 12
0
 def test_can_slice_frequency_dim_with_negative_start_hz(self):
     scale = LinearScale(FrequencyBand(0, 100), 10)
     arr = ArrayWithUnits(np.zeros((13, 10)),
                          [IdentityDimension(),
                           FrequencyDimension(scale)])
     sliced = arr[:, -Hertz(20):]
     self.assertEqual((13, 3), sliced.shape)
Exemplo n.º 13
0
    def _process(self, data):
        data = np.abs(data)
        bfcc = dct(safe_log(data), axis=1) \
            [:, self._exclude: self._exclude + self._n_coeffs]

        yield ArrayWithUnits(
            bfcc.copy(),
            [data.dimensions[0], IdentityDimension()])
Exemplo n.º 14
0
 def x(d, model=None):
     from zounds.core import ArrayWithUnits, IdentityDimension
     transformed = model.transform(d.reshape((d.shape[0], -1)))
     try:
         return ArrayWithUnits(transformed,
                               (d.dimensions[0], IdentityDimension()))
     except AttributeError:
         return transformed
Exemplo n.º 15
0
 def test_raises_when_first_dimension_is_not_time_dimension(self):
     raw = np.zeros((10, 3))
     arr = ArrayWithUnits(raw,
                          dimensions=[
                              IdentityDimension(),
                              TimeDimension(frequency=Seconds(1))
                          ])
     self.assertRaises(ValueError, lambda: ConstantRateTimeSeries(arr))
Exemplo n.º 16
0
 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)
Exemplo n.º 17
0
 def test_discrete_samples_multiple_dimensions(self):
     sr = SR22050()
     samples = SilenceSynthesizer(sr).synthesize(Milliseconds(6666))
     stacked = ArrayWithUnits(
         np.zeros((10, ) + samples.shape, dtype=samples.dtype),
         (IdentityDimension(), ) + samples.dimensions)
     stacked[:] = samples
     self.assertEqual((512, 1024), HalfLapped().discrete_samples(stacked))
Exemplo n.º 18
0
 def test_can_round_trip_mixed_dimensions(self):
     original = [
         IdentityDimension(),
         TimeDimension(Seconds(1), Milliseconds(500)),
         FrequencyDimension(LinearScale(FrequencyBand(100, 1000), 10))
     ]
     restored = self.roundtrip(original)
     self.assertSequenceEqual(original, restored)
Exemplo n.º 19
0
 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)
Exemplo n.º 20
0
 def test_can_round_trip_1d_identity_dimension(self):
     raw = np.arange(10)
     arr = ArrayWithUnits(raw, (IdentityDimension(), ))
     decoded = self._roundtrip(arr)
     self.assertIsInstance(decoded, ArrayWithUnits)
     self.assertEqual(1, len(decoded.dimensions))
     d = decoded.dimensions[0]
     self.assertIsInstance(d, IdentityDimension)
     np.testing.assert_allclose(decoded, raw)
Exemplo n.º 21
0
 def test_can_pack_bits(self):
     raw = np.random.binomial(1, 0.5, (100, 64))
     arr = ArrayWithUnits(raw,
                          [TimeDimension(Seconds(1)),
                           IdentityDimension()])
     decoded = self._roundtrip(arr, encoder=PackedArrayWithUnitsEncoder())
     self.assertIsInstance(decoded, ArrayWithUnits)
     self.assertEqual(2, len(decoded.dimensions))
     self.assertEqual((100, 8), decoded.shape)
Exemplo n.º 22
0
 def test_can_stretch_audio_batch(self):
     sr = SR22050()
     samples = SilenceSynthesizer(sr).synthesize(Milliseconds(6666))
     stacked = ArrayWithUnits(
         np.zeros((10, ) + samples.shape, dtype=samples.dtype),
         (IdentityDimension(), ) + samples.dimensions)
     stacked[:] = samples
     stretched = time_stretch(stacked, 2.0)
     self.assertEqual(10, stretched.shape[0])
     self.assertEqual(int(len(samples) // 2), stretched.shape[1])
Exemplo n.º 23
0
 def test_can_slice_frequency_dim_with_end_hz(self):
     scale = LinearScale(FrequencyBand(0, 100), 10)
     arr = ArrayWithUnits(np.zeros((13, 10)),
                          [IdentityDimension(),
                           FrequencyDimension(scale)])
     sliced = arr[:, :Hertz(50)]
     self.assertEqual((13, 5), sliced.shape)
     self.assertEqual(
         FrequencyDimension(LinearScale(FrequencyBand(0, 50), 5)),
         sliced.dimensions[-1])
Exemplo n.º 24
0
        def x(d, network=None, chunk_size=None):
            from zounds.core import ArrayWithUnits, IdentityDimension
            from zounds.learn import apply_network

            result = apply_network(network, d, chunksize=chunk_size)
            try:
                return ArrayWithUnits(
                    result,
                    [d.dimensions[0], IdentityDimension()])
            except (AttributeError, ValueError):
                return result
Exemplo n.º 25
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)
Exemplo n.º 26
0
 def test_ellipsis(self):
     scale = LinearScale(FrequencyBand(0, 10000), 100)
     arr = ArrayWithUnits(np.zeros((10, 3, 100)), [
         IdentityDimension(),
         TimeDimension(Seconds(1)),
         FrequencyDimension(scale)
     ])
     sliced = arr[..., FrequencyBand(1000, 5000)]
     self.assertEqual((10, 3, 41), sliced.shape)
     self.assertIsInstance(sliced.dimensions[0], IdentityDimension)
     self.assertIsInstance(sliced.dimensions[1], TimeDimension)
     self.assertIsInstance(sliced.dimensions[2], FrequencyDimension)
Exemplo n.º 27
0
    def _init_arr(self, samples):
        if self.arr is not None:
            return

        shape = (self.nsamples,) + samples.shape[1:]
        self.arr = np.zeros(shape, dtype=self.dtype or samples.dtype)

        try:
            self.arr = ArrayWithUnits(
                self.arr, (IdentityDimension(),) + samples.dimensions[1:])
        except AttributeError:
            pass
Exemplo n.º 28
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)
Exemplo n.º 29
0
    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)
Exemplo n.º 30
0
 def test_should_have_correct_shape_and_dimensions(self):
     samplerate = SR22050()
     samples = SineSynthesizer(samplerate).synthesize(Milliseconds(8888))
     wscheme = samplerate.windowing_scheme(256, 128)
     tf = stft(samples, wscheme, HanningWindowingFunc())
     rg = rainbowgram(tf, cm.rainbow)
     self.assertEqual(3, rg.ndim)
     self.assertEqual(tf.shape + (3, ), rg.shape)
     self.assertEqual(3, len(rg.dimensions))
     self.assertEqual(tf.dimensions[0], rg.dimensions[0])
     self.assertEqual(tf.dimensions[1], rg.dimensions[1])
     self.assertEqual(rg.dimensions[2], IdentityDimension())
     self.assertEqual(3, rg.shape[-1])