def test_apply_baddim(features_collection): feats = FeaturesCollection( {k: v.copy() for k, v in features_collection.items()}) feats['new'] = Features( np.random.random((2, 1)), np.asarray([0, 1])) with pytest.raises(ValueError) as err: apply_cmvn(feats) assert 'must have consistent dimensions' in str(err.value)
def test_apply_weights(features_collection): cmvn1 = apply_cmvn(features_collection) with pytest.raises(ValueError) as err: apply_cmvn(features_collection, weights={}) assert 'keys differ for ' in str(err.value) weights = {k: None for k in features_collection.keys()} cmvn3 = apply_cmvn(features_collection, weights=weights) assert cmvn3 == cmvn1
def test_apply_cmvn_skipdims(features_collection, skip_dims): if skip_dims in ([-1], [13]): with pytest.raises(ValueError) as err: apply_cmvn(features_collection, skip_dims=skip_dims) assert 'out of bounds dimensions' in str(err.value) else: cmvns = apply_cmvn( features_collection, skip_dims=skip_dims, by_collection=False) for feats in cmvns.values(): assert feats.data[:, 2:].mean(axis=0) == pytest.approx(0, abs=1e-5) assert feats.data[:, 2:].var(axis=0) == pytest.approx(1, abs=1e-5) assert feats.data[:, :2].mean(axis=0) != pytest.approx(0, abs=1e-5) assert feats.data[:, :2].var(axis=0) != pytest.approx(1, abs=1e-5)
def test_apply_cmvn_bycollection(features_collection): cmvns = apply_cmvn(features_collection, by_collection=True) cmvns = np.concatenate([f.data for f in cmvns.values()], axis=0) assert cmvns.shape == ( sum(f.nframes for f in features_collection.values()), features_collection['0'].ndims) assert cmvns.mean(axis=0) == pytest.approx(0, abs=1e-5) assert cmvns.var(axis=0) == pytest.approx(1, abs=1e-5)
def test_apply_cmvn_byfeatures(features_collection): cmvns = apply_cmvn(features_collection, by_collection=False) for feat in cmvns.values(): assert feat.data.mean(axis=0) == pytest.approx(0, abs=1e-5) assert feat.data.var(axis=0) == pytest.approx(1, abs=1e-5)