Ejemplo n.º 1
0
 def __init__(self, row1, row2):
     dtype = self.dtype
     a, b = row1
     c, d = row2
     self._a = convert(a, dtype)
     self._b = convert(b, dtype)
     self._c = convert(c, dtype)
     self._d = convert(d, dtype)
Ejemplo n.º 2
0
 def __init__(self, row1, row2):
     dtype = self.dtype
     a, b = row1
     c, d = row2
     self._a = convert(a, dtype)
     self._b = convert(b, dtype)
     self._c = convert(c, dtype)
     self._d = convert(d, dtype)
Ejemplo n.º 3
0
def test_set_conversions(T1, T2):
    set_conversion(T1, T2, lambda x: T2(x.data))
    a, b = T1(1), T2(1)

    assert a != b
    assert convert(a, T2) == b

    with pytest.raises(TypeError):
        convert(b, T1)
Ejemplo n.º 4
0
 def __setitem__(self, idx, value):
     if isinstance(idx, tuple):
         i, j = idx
         self.flat[i * self.ncols + j] = convert(value, self.dtype)
     elif isinstance(idx, int):
         M = self.ncols
         start = idx * M
         dtype = self.dtype
         self.flat[start:start + M] = [convert(x, dtype) for x in value]
Ejemplo n.º 5
0
 def __setitem__(self, idx, value):
     if isinstance(idx, tuple):
         i, j = idx
         self.flat[i * self.ncols + j] = convert(value, self.dtype)
     elif isinstance(idx, int):
         M = self.ncols
         start = idx * M
         dtype = self.dtype
         self.flat[start:start + M] = [convert(x, dtype) for x in value]
Ejemplo n.º 6
0
def test_same_type_and_subtypes_conversion():
    class A: pass
    class B(A): pass
    class C(int): pass
    a, b, c = A(), B(), C()

    assert convert(a, A) is a
    assert convert(b, B) is b
    assert convert(c, C) is c
    assert convert(b, A) is b
Ejemplo n.º 7
0
 def __flatsetitem__(self, i, value):
     if i == 0:
         self._a = convert(value, self.dtype)
     elif i == 1:
         self._b = convert(value, self.dtype)
     elif i == 2:
         self._c = convert(value, self.dtype)
     elif i == 3:
         self._d = convert(value, self.dtype)
     else:
         raise IndexError(i)
Ejemplo n.º 8
0
 def from_flat(cls, data, copy=True, dtype=None, shape=None):
     if cls.__concrete__ and dtype is None:
         a, b, c, d = data
         dtype = cls.dtype
         new = object.__new__(cls)
         new._a = convert(a, dtype)
         new._b = convert(b, dtype)
         new._c = convert(c, dtype)
         new._d = convert(d, dtype)
         return new
     return super(Mat2x2, cls).from_flat(data, copy=copy, dtype=dtype)
Ejemplo n.º 9
0
 def from_flat(cls, data, copy=True, dtype=None, shape=None):
     if cls.__concrete__ and dtype is None:
         a, b, c, d = data
         dtype = cls.dtype
         new = object.__new__(cls)
         new._a = convert(a, dtype)
         new._b = convert(b, dtype)
         new._c = convert(c, dtype)
         new._d = convert(d, dtype)
         return new
     return super(Mat2x2, cls).from_flat(data, copy=copy, dtype=dtype)
Ejemplo n.º 10
0
 def __flatsetitem__(self, i, value):
     if i == 0:
         self._a = convert(value, self.dtype)
     elif i == 1:
         self._b = convert(value, self.dtype)
     elif i == 2:
         self._c = convert(value, self.dtype)
     elif i == 3:
         self._d = convert(value, self.dtype)
     else:
         raise IndexError(i)
Ejemplo n.º 11
0
def test_conversion_failures():
    class A: pass
    class B: pass
    a, b = A(), B()

    with pytest.raises(TypeError):
        convert(a, B)
    with pytest.raises(TypeError):
        convert(b, A)
    with pytest.raises(TypeError):
        get_conversion(A, B)
    with pytest.raises(ValueError):
        get_conversion(a, b)
def draw_spectrogram(data: TensArr, to_db=True, show=False, dpi=150, **kwargs):
    """
    
    :param data:
    :param to_db:
    :param show:
    :param dpi:
    :param kwargs: vmin, vmax
    :return: 
    """

    if to_db:
        data[data == 0] = data[data > 0].min()
        data = LogModule.log(data)
        data *= 20
    data = data.squeeze()
    data = gen.convert(data, astype=ndarray)

    fig, ax = plt.subplots(dpi=dpi,)
    ax.imshow(data,
              cmap=plt.get_cmap('CMRmap'),
              extent=(0, data.shape[1], 0, hp.fs // 2),
              origin='lower', aspect='auto', **kwargs)
    ax.set_xlabel('Frame Index')
    ax.set_ylabel('Frequency (Hz)')
    fig.colorbar(ax.images[0])
    if show:
        fig.show()

    return fig
Ejemplo n.º 13
0
 def __setitem__(self, idx, value):
     _assure_mutable_set_coord(self)
     value = convert(value, self.dtype)
     if idx == 0:
         self._x = value
     elif idx == 1:
         self._y = value
Ejemplo n.º 14
0
    def __iadd__(self, other):
        return self

        dtype = self.dtype
        for i, x in enumerate(other.flat):
            self.flat[i] = convert(self.flat[i] + x, dtype)
        return self
Ejemplo n.º 15
0
    def null(cls, shape=None):
        """
        Return an object in which all components are zero.
        """

        null = convert(cls._nullvalue, cls.dtype)
        return cls.from_flat([null] * cls.size, shape=shape)
Ejemplo n.º 16
0
    def __iadd__(self, other):
        return self

        dtype = self.dtype
        for i, x in enumerate(other.flat):
            self.flat[i] = convert(self.flat[i] + x, dtype)
        return self
Ejemplo n.º 17
0
    def null(cls, shape=None):
        """
        Return an object in which all components are zero.
        """

        null = convert(cls._nullvalue, cls.dtype)
        return cls.from_flat([null] * cls.size, shape=shape)
def draw_spectrogram(data: gen.TensArr,
                     fs: int,
                     to_db=True,
                     show=False,
                     **kwargs):
    """
    
    :param data: 
    :param to_db:
    :param show: 
    :param kwargs: vmin, vmax
    :return: 
    """

    data = data.squeeze()
    data = gen.convert(data, astype=ndarray)
    if to_db:
        data = librosa.amplitude_to_db(data)

    fig, ax = plt.subplots(dpi=150)
    ax.imshow(data,
              cmap=plt.get_cmap('CMRmap'),
              extent=(0, data.shape[1], 0, fs // 2),
              origin='lower',
              aspect='auto',
              **kwargs)
    ax.set_xlabel('Frame Index')
    ax.set_ylabel('Frequency (Hz)')
    fig.colorbar(ax.images[0], format='%+2.0f dB')

    fig.tight_layout()
    if show:
        fig.show()

    return fig
Ejemplo n.º 19
0
 def __setitem__(self, idx, value):
     _assure_mutable_set_coord(self)
     value = convert(value, self.dtype)
     if idx == 0:
         self._x = value
     elif idx == 1:
         self._y = value
Ejemplo n.º 20
0
 def from_flat(cls, data, copy=True, dtype=None, shape=None):
     if shape is not None and shape != (3, ):
         raise TypeError('Vec3D cannot have a shape different from (3,)')
     if dtype is None or dtype is cls.dtype:
         x, y, z = data
         return cls._from_coords_unsafe(x, y, z)
     else:
         return cls._from_coords_unsafe(*(convert(x, dtype) for x in data))
Ejemplo n.º 21
0
 def from_flat(cls, data, copy=True, dtype=None, shape=None):
     if shape is not None and shape != (3,):
         raise TypeError('Vec3D cannot have a shape different from (3,)')
     if dtype is None or dtype is cls.dtype:
         x, y, z = data
         return cls._from_coords_unsafe(x, y, z)
     else:
         return cls._from_coords_unsafe(*(convert(x, dtype) for x in data))
Ejemplo n.º 22
0
    def postprocess(self, data: Dict,
                    dataset: data_manager.CustomDataset) -> List[Dict]:
        samples, outs = [], []

        # speech de-normalization, de-emphasis
        xs = data['x'].permute(0, 2, 1)  # B, T, C
        xs = dataset.norm_modules['x'].denormalize_(xs)
        xs = de_emphasis(gen.convert(xs[..., 0], astype=np.ndarray))

        ys = data['y']
        ys = dataset.norm_modules['y'].denormalize_(ys)
        ys = de_emphasis(gen.convert(ys, astype=np.ndarray))

        outs = data['out']
        outs = dataset.norm_modules['y'].denormalize_(outs)
        outs = de_emphasis(gen.convert(outs, astype=np.ndarray))

        data['x'], data['y'], data['out'] = xs, ys, outs

        T_xs = data['T_xs'].int()
        T_ys = data['T_ys'].int()

        # Dict[List] -> List[Dict]
        for idx in range(len(data['x'])):
            sample = dict()
            T_x, T_y = T_xs[idx], T_ys[idx]
            sample['T_xs'], sample['T_ys'] = T_x, T_y

            for key, value in data.items():
                value = value[idx]
                if key == 'x':
                    value = value[..., :T_x]
                    value = np.nan_to_num(value, posinf=1., neginf=-1.)
                    value = np.asfortranarray(np.clip(value, -1, 1))
                elif len(key) > 3:
                    pass
                else:
                    value = value[..., :T_y]
                    value = np.nan_to_num(value, posinf=1., neginf=-1.)
                    value = np.asfortranarray(np.clip(value, -1, 1))
                sample[key] = value
            samples.append(sample)

        return samples
Ejemplo n.º 23
0
def test_failed_conversions_of_base_types():
    with pytest.raises(InexactError):
        convert(1.5, int)

    with pytest.raises(InexactError):
        convert(2, bool)

    with pytest.raises(InexactError):
        convert(1.5, bool)

    with pytest.raises(TypeError):
        convert("42", int)
Ejemplo n.º 24
0
    def __init__(self, *args):
        """
        Directly called upon instantiation of concrete subtypes. (e.g.: at
        Vec[2, float](1, 2) rather than Vec(1, 2))."""

        dtype = self.dtype
        if dtype is None:
            self.flat = self._flatclass(args)
        else:
            self.flat = self._flatclass([convert(x, dtype) for x in args],
                                        False)
Ejemplo n.º 25
0
    def isum_row(self, i, vec):
        """
        Adds the contents of a vector into the i-th row *INPLACE*
        """

        N, M, dtype = self.__parameters__
        if i < 0:
            i = M - i
        data = self.flat
        for j in range(M):
            data[i * M + j] = convert(data[i * M + j] + vec[j], dtype)
Ejemplo n.º 26
0
    def isum_col(self, i, vec):
        """
        Adds the contents of a vector into the i-th column *INPLACE*.
        """

        N, M, dtype = self.__parameters__
        if i < 0:
            i = N - i
        data = self.flat
        for j in range(N):
            data[j * M + i] = convert(data[j * M + i] + vec[j], dtype)
Ejemplo n.º 27
0
    def imul_row(self, i, value):
        """
        Multiply the i-th row by the given value *INPLACE*.
        """

        N, M, dtype = self.__parameters__
        if i < 0:
            i = M - i
        data = self.flat
        for j in range(M):
            data[i * N + j] = convert(data[i * N + j] * value, dtype)
Ejemplo n.º 28
0
    def imul_col(self, i, value):
        """
        Multiply the i-th column by the given value *INPLACE*.
        """

        N, M, dtype = self.__parameters__
        if i < 0:
            i = N - i
        data = self.flat
        for j in range(N):
            data[j * N + i] = convert(data[j * N + i] * value, dtype)
Ejemplo n.º 29
0
    def isum_row(self, i, vec):
        """
        Adds the contents of a vector into the i-th row *INPLACE*
        """

        N, M, dtype = self.__parameters__
        if i < 0:
            i = M - i
        data = self.flat
        for j in range(M):
            data[i * M + j] = convert(data[i * M + j] + vec[j], dtype)
Ejemplo n.º 30
0
    def isum_col(self, i, vec):
        """
        Adds the contents of a vector into the i-th column *INPLACE*.
        """

        N, M, dtype = self.__parameters__
        if i < 0:
            i = N - i
        data = self.flat
        for j in range(N):
            data[j * M + i] = convert(data[j * M + i] + vec[j], dtype)
Ejemplo n.º 31
0
    def imul_row(self, i, value):
        """
        Multiply the i-th row by the given value *INPLACE*.
        """

        N, M, dtype = self.__parameters__
        if i < 0:
            i = M - i
        data = self.flat
        for j in range(M):
            data[i * N + j] = convert(data[i * N + j] * value, dtype)
Ejemplo n.º 32
0
    def imul_col(self, i, value):
        """
        Multiply the i-th column by the given value *INPLACE*.
        """

        N, M, dtype = self.__parameters__
        if i < 0:
            i = N - i
        data = self.flat
        for j in range(N):
            data[j * N + i] = convert(data[j * N + i] * value, dtype)
Ejemplo n.º 33
0
    def __init__(self, *args):
        """
        Directly called upon instantiation of concrete subtypes. (e.g.: at
        Vec2(1, 2) rather than Vec(1, 2))."""

        dtype = self.dtype
        if dtype is None:
            self.flat = self._flatclass(args)
        else:
            self.flat = self._flatclass([convert(x, dtype) for x in args],
                                        False)
def draw_audio(data: gen.TensArr,
               fs: int,
               show=False,
               xlim=None,
               ylim=(-1, 1)):
    data = data.squeeze()
    data = gen.convert(data, astype=ndarray)
    t_axis = np.arange(len(data)) / fs
    if xlim is None:
        xlim = (0, t_axis[-1])

    fig, ax = plt.subplots(figsize=(xlim[1] * 10, 2), dpi=150)
    ax.plot(t_axis, data)
    ax.set_xlabel('time')
    ax.xaxis.set_major_locator(tckr.MultipleLocator(0.5))

    ax.set_xlim(*xlim)
    ax.set_ylim(*ylim)

    fig.tight_layout()
    if show:
        fig.show()

    return fig
Ejemplo n.º 35
0
 def __imul__(self, other):
     other = convert(other, self.dtype)
     for i, x in enumerate(self.flat):
         self.flat[i] *= other
     return self
Ejemplo n.º 36
0
 def __isub__(self, other):
     other = convert(other, self.dtype)
     for i, x in enumerate(other.flat):
         self.flat[i] -= x
     return self
Ejemplo n.º 37
0
 def __ifloordiv__(self, other):
     other = convert(other, self.dtype)
     for i, x in enumerate(self.flat):
         self.flat[i] //= other
     return self
Ejemplo n.º 38
0
 def __ifloordiv__(self, other):
     data = [x // y for (x, y) in zip(self.flat, other.flat)]
     for i, x in enumerate(data):
         self.flat[i] = convert(x, self.dtype)
     return self
Ejemplo n.º 39
0
 def promote_to_int(x, y):
     return convert(x.data, int), y
Ejemplo n.º 40
0
 def __imul__(self, other):
     other = convert(other, self.dtype)
     for i, x in enumerate(self.flat):
         self.flat[i] *= other
     return self
Ejemplo n.º 41
0
 def __init__(self, x, y):
     self._x = convert(x, self.dtype)
     self._y = convert(y, self.dtype)
Ejemplo n.º 42
0
 def y(self, value):
     _assure_mutable_set_coord(self)
     self._y = convert(value, self.dtype)
Ejemplo n.º 43
0
 def __init__(self, x, y):
     self._x = convert(x, self.dtype)
     self._y = convert(y, self.dtype)
Ejemplo n.º 44
0
 def __init__(self, x, y, z):
     dtype = self.dtype
     self._x = convert(x, dtype)
     self._y = convert(y, dtype)
     self._z = convert(z, dtype)
Ejemplo n.º 45
0
 def __init__(self, data):
     dtype = self.dtype
     self.flat = Flat([convert(x, dtype) for x in data], copy=False)
Ejemplo n.º 46
0
 def __init__(self, x):
     self._x = convert(x, self.dtype)
Ejemplo n.º 47
0
 def x(self, value):
     _assure_mutable_set_coord(self)
     self._x = convert(value, self.dtype)
Ejemplo n.º 48
0
 def astype(self, T: type=torch.Tensor):
     return NormalizeConst(*[gen.convert(item, T) for item in self])
Ejemplo n.º 49
0
 def __init__(self, x):
     self._x = convert(x, self.dtype)
Ejemplo n.º 50
0
 def __itruediv__(self, other):
     dtype = self.dtype
     data = (x / y for (x, y) in zip(self.flat, other.flat))
     self.flat[:] = (convert(x, dtype) for i, x in enumerate(data))
     return self
Ejemplo n.º 51
0
 def __init__(self, data):
     dtype = self.dtype
     self.flat = Flat([convert(x, dtype) for x in data], copy=False)
Ejemplo n.º 52
0
 def __isub__(self, other):
     dtype = self.dtype
     data = (x - y for (x, y) in zip(self.flat, other.flat))
     self.flat[:] = (convert(x, dtype) for x in data)
     return self
Ejemplo n.º 53
0
 def __ifloordiv__(self, other):
     other = convert(other, self.dtype)
     for i, x in enumerate(self.flat):
         self.flat[i] //= other
     return self
Ejemplo n.º 54
0
 def __iadd__(self, other):
     dtype = self.dtype
     data = (x + y for (x, y) in zip_longest(self.flat, other.flat))
     self.flat[:] = (convert(x, dtype) for x in data)
     return self
Ejemplo n.º 55
0
 def __isub__(self, other):
     other = convert(other, self.dtype)
     for i, x in enumerate(other.flat):
         self.flat[i] -= x
     return self
Ejemplo n.º 56
0
 def __ifloordiv__(self, other):
     data = [x // y for (x, y) in zip(self.flat, other.flat)]
     for i, x in enumerate(data):
         self.flat[i] = convert(x, self.dtype)
     return self
Ejemplo n.º 57
0
 def __init__(self, x, y, z):
     dtype = self.dtype
     self._x = convert(x, dtype)
     self._y = convert(y, dtype)
     self._z = convert(z, dtype)
Ejemplo n.º 58
0
def test_successfull_conversions_of_base_types():
    # Same types
    assert convert(1, int) == 1
    assert type(convert(1, int)) is int
    assert convert(1.0, float) == 1.0
    assert type(convert(1.0, float)) is float

    # Mixing types
    assert convert(1, float) == 1.0
    assert type(convert(1, float)) is float
    assert convert(1.0, int) == 1
    assert type(convert(1.0, int)) is int
    assert convert(1, bool) is True
    assert convert(0, bool) is False
    assert convert(True, float) == 1.0
    assert type(convert(True, float)) is float
    assert convert(True, int) == 1
    assert type(convert(True, int)) is int