test_mel_bank_options.py (2405B)
1 #!/usr/bin/env python3 2 # 3 # Copyright (c) 2021 Xiaomi Corporation (authors: Fangjun Kuang) 4 5 import pickle 6 7 import kaldi_native_fbank as knf 8 9 10 def test_default(): 11 opts = knf.MelBanksOptions() 12 assert opts.num_bins == 25 13 assert opts.low_freq == 20 14 assert opts.high_freq == 0 15 assert opts.vtln_low == 100 16 assert opts.vtln_high == -500 17 assert opts.debug_mel is False 18 assert opts.htk_mode is False 19 assert opts.is_librosa is False 20 assert opts.norm == "slaney" 21 22 23 def test_set_get(): 24 opts = knf.MelBanksOptions() 25 opts.num_bins = 100 26 assert opts.num_bins == 100 27 28 opts.low_freq = 22 29 assert opts.low_freq == 22 30 31 opts.high_freq = 1 32 assert opts.high_freq == 1 33 34 opts.vtln_low = 101 35 assert opts.vtln_low == 101 36 37 opts.vtln_high = -100 38 assert opts.vtln_high == -100 39 40 opts.debug_mel = True 41 assert opts.debug_mel is True 42 43 opts.htk_mode = True 44 assert opts.htk_mode is True 45 46 opts.is_librosa = True 47 assert opts.is_librosa is True 48 49 opts.norm = "slaney" 50 assert opts.norm == "slaney" 51 52 53 def test_from_empty_dict(): 54 opts = knf.MelBanksOptions.from_dict({}) 55 opts2 = knf.MelBanksOptions() 56 57 assert str(opts) == str(opts2) 58 59 60 def test_from_dict_partial(): 61 d = {"num_bins": 10, "debug_mel": True} 62 63 opts = knf.MelBanksOptions.from_dict(d) 64 65 opts2 = knf.MelBanksOptions() 66 assert str(opts) != str(opts2) 67 68 opts2.num_bins = 10 69 assert str(opts) != str(opts2) 70 71 opts2.debug_mel = True 72 assert str(opts) == str(opts2) 73 74 opts2.debug_mel = False 75 assert str(opts) != str(opts2) 76 77 78 def test_from_dict_full_and_as_dict(): 79 opts = knf.MelBanksOptions() 80 opts.num_bins = 80 81 opts.vtln_high = 2 82 83 d = opts.as_dict() 84 for key, value in d.items(): 85 assert value == getattr(opts, key) 86 87 opts2 = knf.MelBanksOptions.from_dict(d) 88 assert str(opts2) == str(opts) 89 90 d["htk_mode"] = True 91 opts3 = knf.MelBanksOptions.from_dict(d) 92 assert opts3.htk_mode is True 93 94 95 def test_pickle(): 96 opts = knf.MelBanksOptions() 97 opts.num_bins = 100 98 opts.low_freq = 22 99 data = pickle.dumps(opts) 100 101 opts2 = pickle.loads(data) 102 assert str(opts) == str(opts2) 103 104 105 def main(): 106 test_default() 107 test_set_get() 108 test_from_empty_dict() 109 test_from_dict_partial() 110 test_from_dict_full_and_as_dict() 111 test_pickle() 112 113 114 if __name__ == "__main__": 115 main()