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)
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
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 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)
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
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
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
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
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))
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))
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)
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)
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)
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)
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)
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
def __imul__(self, other): other = convert(other, self.dtype) for i, x in enumerate(self.flat): self.flat[i] *= other return self
def __isub__(self, other): other = convert(other, self.dtype) for i, x in enumerate(other.flat): self.flat[i] -= x return self
def __ifloordiv__(self, other): other = convert(other, self.dtype) for i, x in enumerate(self.flat): self.flat[i] //= other return self
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
def __init__(self, x, y): self._x = convert(x, self.dtype) self._y = convert(y, self.dtype)
def y(self, value): _assure_mutable_set_coord(self) self._y = convert(value, self.dtype)
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 __init__(self, data): dtype = self.dtype self.flat = Flat([convert(x, dtype) for x in data], copy=False)
def __init__(self, x): self._x = convert(x, self.dtype)
def x(self, value): _assure_mutable_set_coord(self) self._x = convert(value, self.dtype)
def astype(self, T: type=torch.Tensor): return NormalizeConst(*[gen.convert(item, T) for item in self])
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
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
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
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