class VariableStepAudioFeatureModelTest(unittest.TestCase): def setUp(self): self.audio_file_meta = AudioFileMeta(file_name="file.wav", channels_count=2, sample_rate=44100, file_size_bytes=88200) self.feature_values = [1.0, 2.0, 3.0, 4.0] self.feature_steps = [ StepFeature(v, values=numpy.asanyarray(self.feature_values), label="text_{}".format(v)) for v in self.feature_values ] self.variable_step_feature = VampyVariableStepFeature( task_id="0f961f20-b036-5740-b526-013523dd88c7", step_features=self.feature_steps) def test_should_serialize_and_deserialize_feature(self): serialized = self.variable_step_feature.to_serializable() deserialized = VampyVariableStepFeature.from_serializable(serialized) assert_that(deserialized).is_not_none().is_equal_to( self.variable_step_feature) def test_should_serialized_to_json(self): as_json = json.dumps(self.variable_step_feature.to_serializable()) assert_that(as_json).is_not_none().is_not_empty()
def setUp(self): self.audio_file_meta = AudioFileMeta(file_name="file.wav", channels_count=2, sample_rate=44100, file_size_bytes=88200) self.feature_values = [1.0, 2.0, 3.0, 4.0] self.feature_steps = [ StepFeature(v, values=numpy.asanyarray(self.feature_values), label="text_{}".format(v)) for v in self.feature_values ] self.variable_step_feature = VampyVariableStepFeature( task_id="0f961f20-b036-5740-b526-013523dd88c7", step_features=self.feature_steps)
def build_feature_object(task_id: str, extracted_data: Dict[str, Any]) -> VampyFeatureAbstraction: data_type = list(extracted_data.keys())[0] if data_type == "list": value_list = [StepFeature(f.get("timestamp").to_float(), f.get("values"), f.get("label") or None) for f in extracted_data["list"]] return VampyVariableStepFeature(task_id, step_features=value_list) elif data_type in ("vector", "matrix"): data = extracted_data.get("vector") or extracted_data.get("matrix") return VampyConstantStepFeature(task_id, time_step=data[0].to_float(), matrix=data[1]) # type: ignore else: raise NotImplementedError("Can not recognize feature type: {}".format(extracted_data.keys()))
def setUp(self): self.audio_meta = CompressedAudioFileMeta("some_name.mp3", 1024, 1, 44100, length_sec=4., bit_rate_kbps=128) self.single_dimensional_feature = array([0.38888031], dtype=float32) self.two_dimensional_feature = array([0.38888031, 0.3144314, 0.46564227, 0.31890243, 0.22512659], dtype=float32) self.three_dimensional_feature = array([[0.38888031, 0.3144314], [0.46564227, 0.31890243], [0.22512659, 0.31890243]], dtype=float32) self.constant_step_2d_feature = VampyConstantStepFeature("", time_step=0.1, matrix=self.two_dimensional_feature) self.constant_step_3d_feature = VampyConstantStepFeature("", time_step=0.1, matrix=self.three_dimensional_feature) self.variable_step_single_value_feature = VampyVariableStepFeature("", [StepFeature(timestamp=0.0, values=array([519.51]))]) self.constant_step_single_value_feature = VampyConstantStepFeature("", time_step=0.1, matrix=self.single_dimensional_feature)
def test_should_transform_segment_based_simple_variable_feature_into_ratio_metric(self): segment_share_ratio = SegmentLabelShareRatioTransformation(audio_meta=self.audio_meta, label="b") segment_variable_step_feature = VampyVariableStepFeature("task_id", [StepFeature(timestamp=0.0, values=array([0]), label="a"), StepFeature(timestamp=1.0, values=array([1]), label="b"), StepFeature(timestamp=2.0, values=array([0]), label="a"), StepFeature(timestamp=3.0, values=array([1]), label="b") ]) actual_metric_vector = segment_share_ratio.call(segment_variable_step_feature) assert_that(actual_metric_vector).is_length(2).contains(0.25)
def test_should_serialize_and_deserialize_feature(self): serialized = self.variable_step_feature.to_serializable() deserialized = VampyVariableStepFeature.from_serializable(serialized) assert_that(deserialized).is_not_none().is_equal_to( self.variable_step_feature)
def _call_on_variable_step( self, feature: VampyVariableStepFeature) -> numpy.ndarray: return feature.values()
def from_compressed_feature(comp_feat: CompressedFeatureDTO) -> Union[VampyConstantStepFeature, VampyVariableStepFeature]: decompressed_feature = decompress_model(comp_feat.compression, comp_feat.data) if "time_step" in decompressed_feature.keys(): return VampyConstantStepFeature.from_serializable(decompressed_feature) else: return VampyVariableStepFeature.from_serializable(decompressed_feature)