def slice_cqt(row, window_length): """Generate slices of CQT observations. Parameters ---------- row : pd.Series Row from a features dataframe. window_length : int Length of the CQT slice in time. Yields ------ x_obs : np.ndarray Slice of cqt data. meta : dict Metadata corresponding to the observation. """ try: data = np.load(row.features)['cqt'] except IOError as derp: print("Failed reading row: {}\n\n{}".format(row.to_dict(), derp)) raise derp num_obs = data.shape[1] # Break the remainder out into a subfunction for reuse with embedding # sampling. idx = np.random.permutation(num_obs) if num_obs > 0 else None if idx is None: raise ValueError("Misshapen CQT ({}) - {}".format( data.shape, row.to_dict())) np.random.shuffle(idx) counter = 0 meta = dict(instrument=row.instrument, note_number=row.note_number, fcode=row.fcode) while num_obs > 0: n = idx[counter] obs = utils.padded_slice_ndarray(data, n, length=window_length, axis=1) obs = obs[np.newaxis, ...] meta['idx'] = n yield obs, meta counter += 1 if counter >= len(idx): np.random.shuffle(idx) counter = 0
def slice_cqt(row, window_length): """Generate slices of CQT observations. Parameters ---------- row : pd.Series Row from a features dataframe. window_length : int Length of the CQT slice in time. Yields ------ x_obs : np.ndarray Slice of cqt data. meta : dict Metadata corresponding to the observation. """ try: data = np.load(row.features)['cqt'] except IOError as derp: print("Failed reading row: {}\n\n{}".format(row.to_dict(), derp)) raise derp num_obs = data.shape[1] # Break the remainder out into a subfunction for reuse with embedding # sampling. idx = np.random.permutation(num_obs) if num_obs > 0 else None if idx is None: raise ValueError( "Misshapen CQT ({}) - {}".format(data.shape, row.to_dict())) np.random.shuffle(idx) counter = 0 meta = dict(instrument=row.instrument, note_number=row.note_number, fcode=row.fcode) while num_obs > 0: n = idx[counter] obs = utils.padded_slice_ndarray(data, n, length=window_length, axis=1) obs = obs[np.newaxis, ...] meta['idx'] = n yield obs, meta counter += 1 if counter >= len(idx): np.random.shuffle(idx) counter = 0
def slice_cqt_weighted(row, window_length): """Generate slices of CQT observations. Parameters ---------- row : pd.Series Row from a features dataframe. window_length : int Length of the CQT slice in time. Yields ------ x_obs : np.ndarray Slice of cqt data. meta : dict Metadata corresponding to the observation. """ try: data = np.load(row.features)['cqt'] except IOError as derp: print("Failed reading row: {}\n\n{}".format(row.to_dict(), derp)) raise derp # Create an index likelihood as a function of amplitude weights = data.squeeze().sum(axis=-1) weights /= weights.sum() meta = dict(instrument=row.instrument, note_number=row.note_number, fcode=row.fcode) while True: n = np.random.multinomial(1, weights).argmax() obs = utils.padded_slice_ndarray(data, n, length=window_length, axis=1) obs = obs[np.newaxis, ...] meta['idx'] = n yield obs, meta
def test_padded_slice_ndarray(): x_in = np.arange(64).reshape(2, 4, 8) np.testing.assert_equal( utils.padded_slice_ndarray(x_in, idx=0, length=4, axis=0), np.concatenate([np.zeros([2, 4, 8]), x_in[:2, ...]], axis=0))