def test_import_files(self): importing_files = [ assets.File('a', '/some/path.wav'), assets.File('b', '/some/other/path.wav'), assets.File('existing_file', '/some/otherer/path.wav'), ] idx_mapping = self.corpus.import_files(importing_files) assert self.corpus.num_files == 4 assert 'a' in self.corpus.files.keys() assert self.corpus.files['a'].path == '/some/path.wav' assert 'b' in self.corpus.files.keys() assert self.corpus.files['b'].path == '/some/other/path.wav' assert 'existing_file_1' in self.corpus.files.keys() assert self.corpus.files[ 'existing_file_1'].path == '/some/otherer/path.wav' assert len(idx_mapping) == 3 assert 'a' in idx_mapping['a'].idx assert 'b' in idx_mapping['b'].idx assert idx_mapping['existing_file'].idx == 'existing_file_1'
def setUp(self): file = assets.File('wav', resources.sample_wav_file('wav_1.wav')) utt = assets.Utterance('utt', file, start=0.3, end=-1) ll = assets.LabelList() self.test_label = assets.Label('a', start=0.5, end=-1) ll.append(self.test_label) utt.set_label_list(ll)
def test_read_samples(self): file = assets.File('wav', resources.sample_wav_file('wav_1.wav')) issuer = assets.Issuer('toni') utt = assets.Utterance('test', file, issuer=issuer, start=1.0, end=2.30) l1 = assets.Label('a', 0.15, 0.448) l2 = assets.Label('a', 0.5, 0.73) ll = assets.LabelList(labels=[l1, l2]) utt.set_label_list(ll) expected, __ = librosa.core.load(file.path, sr=None, offset=1.15, duration=0.298) assert np.array_equal(l1.read_samples(), expected) expected, __ = librosa.core.load(file.path, sr=None, offset=1.5, duration=0.23) assert np.array_equal(l2.read_samples(), expected)
def test_import_utterance_no_file(self): importing_utterances = [ assets.Utterance('a', assets.File('notexist', 'notexist'), self.ex_issuer, 0, 10) ] with pytest.raises(ValueError): self.corpus.import_utterances(importing_utterances)
def test_read_samples_fix_sampling_rate(self, name, audio_path): audio_path = os.path.join(audio_path, name) file_obj = assets.File('some_idx', audio_path) expected, __ = librosa.core.load(audio_path, sr=16000, mono=True) actual = file_obj.read_samples(sr=16000) assert np.array_equal(actual, expected)
def test_read_samples_range(self, name, audio_path): audio_path = os.path.join(audio_path, name) file_obj = assets.File('some_idx', audio_path) expected, __ = librosa.core.load(audio_path, sr=None, mono=True, offset=1.0, duration=1.7) actual = file_obj.read_samples(offset=1.0, duration=1.7) assert np.array_equal(actual, expected)
def setUp(self): self.tempdir = tempfile.mkdtemp() self.corpus = audiomate.Corpus(self.tempdir) self.ex_file = assets.File('existing_file', '../any/path.wav') self.ex_issuer = assets.Issuer('existing_issuer') self.ex_utterance = assets.Utterance('existing_utt', self.ex_file, issuer=self.ex_issuer) self.corpus.files['existing_file'] = self.ex_file self.corpus.issuers['existing_issuer'] = self.ex_issuer self.corpus.utterances['existing_utt'] = self.ex_utterance
def test_encode_utterance_takes_lower_index_first(self): file = assets.File('file-idx', resources.sample_wav_file('wav_1.wav')) utt = assets.Utterance('utt-idx', file, start=0, end=5) ll = assets.LabelList( labels=[assets.Label('music', 0, 3), assets.Label('speech', 3, 5)]) utt.set_label_list(ll) enc = label_encoding.FrameOrdinalEncoder( ['speech', 'music', 'noise'], frame_settings=units.FrameSettings(32000, 16000), sr=16000) actual = enc.encode(utt) expected = np.array([1, 1, 0, 0]).astype(np.int) assert np.array_equal(expected, actual)
def new_file(self, path, file_idx, copy_file=False): """ Adds a new file to the corpus with the given data. Parameters: path (str): Path of the file to add. file_idx (str): The id to associate the file with. copy_file (bool): If True the file is copied to the data set folder, otherwise the given path is used directly. Returns: File: The newly added File. """ new_file_idx = file_idx new_file_path = os.path.abspath(path) # Add index to idx if already existing if new_file_idx in self._files.keys(): new_file_idx = naming.index_name_if_in_list( new_file_idx, self._files.keys()) # Copy file to default file dir if copy_file: if not os.path.isdir(self.path): raise ValueError( 'To copy file the dataset needs to have a path.') __, ext = os.path.splitext(path) new_file_folder = os.path.join(self.path, DEFAULT_FILE_SUBDIR) new_file_path = os.path.join(new_file_folder, '{}{}'.format(new_file_idx, ext)) os.makedirs(new_file_folder, exist_ok=True) shutil.copy(path, new_file_path) # Create file obj new_file = assets.File(new_file_idx, new_file_path) self._files[new_file_idx] = new_file return new_file
def setUp(self): self.ll_1 = assets.LabelList(idx='alpha', labels=[ assets.Label('a', 3.2, 4.5), assets.Label('b', 5.1, 8.9), assets.Label('c', 7.2, 10.5), assets.Label('d', 10.5, 14), assets.Label('d', 15, 18) ]) self.ll_2 = assets.LabelList(idx='bravo', labels=[ assets.Label('a', 1.0, 4.2), assets.Label('e', 4.2, 7.9), assets.Label('c', 7.2, 10.5), assets.Label('f', 10.5, 14), assets.Label('d', 15, 17.3) ]) self.ll_duplicate_idx = assets.LabelList( idx='charlie', labels=[assets.Label('t', 1.0, 4.2), assets.Label('h', 4.2, 7.9)]) self.ll_3 = assets.LabelList( idx='charlie', labels=[assets.Label('a', 1.0, 4.2), assets.Label('g', 4.2, 7.9)]) self.file = assets.File('wav', resources.sample_wav_file('wav_1.wav')) self.issuer = assets.Issuer('toni') self.utt = assets.Utterance('test', self.file, issuer=self.issuer, start=1.25, end=1.30, label_lists=[ self.ll_1, self.ll_2, self.ll_duplicate_idx, self.ll_3 ])
def test_num_duration_compressed_formats(self, name, duration, audio_path): file_obj = assets.File('some_idx', os.path.join(audio_path, name)) assert file_obj.duration == pytest.approx(duration, abs=0.1)
def test_num_duration(self, name, duration, audio_path): file_obj = assets.File('some_idx', os.path.join(audio_path, name)) assert file_obj.duration == pytest.approx(duration)
def test_num_samples_compressed_formats(self, name, num_samples, audio_path): file_obj = assets.File('some_idx', os.path.join(audio_path, name)) assert file_obj.num_samples == pytest.approx(num_samples, abs=2000)
def test_num_samples(self, name, num_samples, audio_path): file_obj = assets.File('some_idx', os.path.join(audio_path, name)) assert file_obj.num_samples == num_samples
def test_sampling_rate(self, name, sampling_rate, audio_path): file_obj = assets.File('some_idx', os.path.join(audio_path, name)) assert file_obj.sampling_rate == sampling_rate
def sample_utterance(): file = assets.File('test_file', resources.sample_wav_file('wav_1.wav')) utterance = assets.Utterance('test', file) return utterance