Beispiel #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)
Beispiel #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)
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)
Beispiel #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]
Beispiel #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]
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
Beispiel #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)
Beispiel #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)
Beispiel #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)
Beispiel #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)
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
Beispiel #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
Beispiel #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
Beispiel #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)
Beispiel #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
Beispiel #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
Beispiel #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
Beispiel #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))
Beispiel #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))
Beispiel #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
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)
    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)
Beispiel #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)
Beispiel #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)
Beispiel #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)
Beispiel #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)
Beispiel #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)
Beispiel #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)
Beispiel #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)
Beispiel #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)
Beispiel #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
Beispiel #35
0
 def __imul__(self, other):
     other = convert(other, self.dtype)
     for i, x in enumerate(self.flat):
         self.flat[i] *= other
     return self
Beispiel #36
0
 def __isub__(self, other):
     other = convert(other, self.dtype)
     for i, x in enumerate(other.flat):
         self.flat[i] -= x
     return self
Beispiel #37
0
 def __ifloordiv__(self, other):
     other = convert(other, self.dtype)
     for i, x in enumerate(self.flat):
         self.flat[i] //= other
     return self
Beispiel #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
 def promote_to_int(x, y):
     return convert(x.data, int), y
Beispiel #40
0
 def __imul__(self, other):
     other = convert(other, self.dtype)
     for i, x in enumerate(self.flat):
         self.flat[i] *= other
     return self
Beispiel #41
0
 def __init__(self, x, y):
     self._x = convert(x, self.dtype)
     self._y = convert(y, self.dtype)
Beispiel #42
0
 def y(self, value):
     _assure_mutable_set_coord(self)
     self._y = convert(value, self.dtype)
Beispiel #43
0
 def __init__(self, x, y):
     self._x = convert(x, self.dtype)
     self._y = convert(y, self.dtype)
Beispiel #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)
Beispiel #45
0
 def __init__(self, data):
     dtype = self.dtype
     self.flat = Flat([convert(x, dtype) for x in data], copy=False)
Beispiel #46
0
 def __init__(self, x):
     self._x = convert(x, self.dtype)
Beispiel #47
0
 def x(self, value):
     _assure_mutable_set_coord(self)
     self._x = convert(value, self.dtype)
Beispiel #48
0
 def astype(self, T: type=torch.Tensor):
     return NormalizeConst(*[gen.convert(item, T) for item in self])
Beispiel #49
0
 def __init__(self, x):
     self._x = convert(x, self.dtype)
 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
Beispiel #51
0
 def __init__(self, data):
     dtype = self.dtype
     self.flat = Flat([convert(x, dtype) for x in data], copy=False)
 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
Beispiel #53
0
 def __ifloordiv__(self, other):
     other = convert(other, self.dtype)
     for i, x in enumerate(self.flat):
         self.flat[i] //= other
     return self
 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
Beispiel #55
0
 def __isub__(self, other):
     other = convert(other, self.dtype)
     for i, x in enumerate(other.flat):
         self.flat[i] -= x
     return self
Beispiel #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
Beispiel #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)
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