test_mel_bank_options.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #!/usr/bin/env python3
  2. #
  3. # Copyright (c) 2021 Xiaomi Corporation (authors: Fangjun Kuang)
  4. import pickle
  5. import kaldi_native_fbank as knf
  6. def test_default():
  7. opts = knf.MelBanksOptions()
  8. assert opts.num_bins == 25
  9. assert opts.low_freq == 20
  10. assert opts.high_freq == 0
  11. assert opts.vtln_low == 100
  12. assert opts.vtln_high == -500
  13. assert opts.debug_mel is False
  14. assert opts.htk_mode is False
  15. def test_set_get():
  16. opts = knf.MelBanksOptions()
  17. opts.num_bins = 100
  18. assert opts.num_bins == 100
  19. opts.low_freq = 22
  20. assert opts.low_freq == 22
  21. opts.high_freq = 1
  22. assert opts.high_freq == 1
  23. opts.vtln_low = 101
  24. assert opts.vtln_low == 101
  25. opts.vtln_high = -100
  26. assert opts.vtln_high == -100
  27. opts.debug_mel = True
  28. assert opts.debug_mel is True
  29. opts.htk_mode = True
  30. assert opts.htk_mode is True
  31. def test_from_empty_dict():
  32. opts = knf.MelBanksOptions.from_dict({})
  33. opts2 = knf.MelBanksOptions()
  34. assert str(opts) == str(opts2)
  35. def test_from_dict_partial():
  36. d = {"num_bins": 10, "debug_mel": True}
  37. opts = knf.MelBanksOptions.from_dict(d)
  38. opts2 = knf.MelBanksOptions()
  39. assert str(opts) != str(opts2)
  40. opts2.num_bins = 10
  41. assert str(opts) != str(opts2)
  42. opts2.debug_mel = True
  43. assert str(opts) == str(opts2)
  44. opts2.debug_mel = False
  45. assert str(opts) != str(opts2)
  46. def test_from_dict_full_and_as_dict():
  47. opts = knf.MelBanksOptions()
  48. opts.num_bins = 80
  49. opts.vtln_high = 2
  50. d = opts.as_dict()
  51. for key, value in d.items():
  52. assert value == getattr(opts, key)
  53. opts2 = knf.MelBanksOptions.from_dict(d)
  54. assert str(opts2) == str(opts)
  55. d["htk_mode"] = True
  56. opts3 = knf.MelBanksOptions.from_dict(d)
  57. assert opts3.htk_mode is True
  58. def test_pickle():
  59. opts = knf.MelBanksOptions()
  60. opts.num_bins = 100
  61. opts.low_freq = 22
  62. data = pickle.dumps(opts)
  63. opts2 = pickle.loads(data)
  64. assert str(opts) == str(opts2)
  65. def main():
  66. test_default()
  67. test_set_get()
  68. test_from_empty_dict()
  69. test_from_dict_partial()
  70. test_from_dict_full_and_as_dict()
  71. test_pickle()
  72. if __name__ == "__main__":
  73. main()