You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_biquad.py 6.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. # Copyright 2021 Huawei Technologies Co., Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. # ==============================================================================
  15. import numpy as np
  16. import pytest
  17. import mindspore.dataset as ds
  18. import mindspore.dataset.audio.transforms as audio
  19. from mindspore import log as logger
  20. def count_unequal_element(data_expected, data_me, rtol, atol):
  21. assert data_expected.shape == data_me.shape
  22. total_count = len(data_expected.flatten())
  23. error = np.abs(data_expected - data_me)
  24. greater = np.greater(error, atol + np.abs(data_expected) * rtol)
  25. loss_count = np.count_nonzero(greater)
  26. assert (loss_count / total_count) < rtol, \
  27. "\ndata_expected_std:{0}\ndata_me_error:{1}\nloss:{2}". \
  28. format(data_expected[greater], data_me[greater], error[greater])
  29. def test_func_biquad_eager():
  30. """ mindspore eager mode normal testcase:biquad op"""
  31. # Original waveform
  32. waveform = np.array([[1, 2, 3], [4, 5, 6]], dtype=np.float64)
  33. # Expect waveform
  34. expect_waveform = np.array([[0.0100, 0.0388, 0.1923],
  35. [0.0400, 0.1252, 0.6530]], dtype=np.float64)
  36. biquad_op = audio.Biquad(0.01, 0.02, 0.13, 1, 0.12, 0.3)
  37. # Filtered waveform by biquad
  38. output = biquad_op(waveform)
  39. count_unequal_element(expect_waveform, output, 0.0001, 0.0001)
  40. def test_func_biquad_pipeline():
  41. """ mindspore pipeline mode normal testcase:biquad op"""
  42. # Original waveform
  43. waveform = np.array([[3.2, 2.1, 1.3], [6.2, 5.3, 6]], dtype=np.float64)
  44. # Expect waveform
  45. expect_waveform = np.array([[1.0000, 1.0000, 0.5844],
  46. [1.0000, 1.0000, 1.0000]], dtype=np.float64)
  47. dataset = ds.NumpySlicesDataset(waveform, ["audio"], shuffle=False)
  48. biquad_op = audio.Biquad(1, 0.02, 0.13, 1, 0.12, 0.3)
  49. # Filtered waveform by biquad
  50. dataset = dataset.map(input_columns=["audio"], operations=biquad_op, num_parallel_workers=8)
  51. i = 0
  52. for item in dataset.create_dict_iterator(num_epochs=1, output_numpy=True):
  53. count_unequal_element(expect_waveform[i, :],
  54. item['audio'], 0.0001, 0.0001)
  55. i += 1
  56. def test_biquad_invalid_input():
  57. def test_invalid_input(test_name, b0, b1, b2, a0, a1, a2, error, error_msg):
  58. logger.info("Test Biquad with bad input: {0}".format(test_name))
  59. with pytest.raises(error) as error_info:
  60. audio.Biquad(b0, b1, b2, a0, a1, a2)
  61. assert error_msg in str(error_info.value)
  62. test_invalid_input("invalid b0 parameter type as a String", "0.01", 0.02, 0.13, 1, 0.12, 0.3, TypeError,
  63. "Argument b0 with value 0.01 is not of type [<class 'float'>, <class 'int'>],"
  64. " but got <class 'str'>.")
  65. test_invalid_input("invalid b0 parameter value", 441324343243242342345300, 0.02, 0.13, 1, 0.12, 0.3, ValueError,
  66. "Input b0 is not within the required interval of [-16777216, 16777216].")
  67. test_invalid_input("invalid b1 parameter type as a String", 0.01, "0.02", 0.13, 0, 0.12, 0.3, TypeError,
  68. "Argument b1 with value 0.02 is not of type [<class 'float'>, <class 'int'>],"
  69. " but got <class 'str'>.")
  70. test_invalid_input("invalid b1 parameter value", 0.01, 441324343243242342345300, 0.13, 1, 0.12, 0.3, ValueError,
  71. "Input b1 is not within the required interval of [-16777216, 16777216].")
  72. test_invalid_input("invalid b2 parameter type as a String", 0.01, 0.02, "0.13", 0, 0.12, 0.3, TypeError,
  73. "Argument b2 with value 0.13 is not of type [<class 'float'>, <class 'int'>],"
  74. " but got <class 'str'>.")
  75. test_invalid_input("invalid b2 parameter value", 0.01, 0.02, 441324343243242342345300, 1, 0.12, 0.3, ValueError,
  76. "Input b2 is not within the required interval of [-16777216, 16777216].")
  77. test_invalid_input("invalid a0 parameter type as a String", 0.01, 0.02, 0.13, '1', 0.12, 0.3, TypeError,
  78. "Argument a0 with value 1 is not of type [<class 'float'>, <class 'int'>],"
  79. " but got <class 'str'>.")
  80. test_invalid_input("invalid a0 parameter value", 0.01, 0.02, 0.13, 0, 0.12, 0.3, ValueError,
  81. "Input a0 is not within the required interval of [-16777216, 0) and (0, 16777216].")
  82. test_invalid_input("invalid a0 parameter value", 0.01, 0.02, 0.13, 441324343243242342345300, 0.12, 0.3, ValueError,
  83. "Input a0 is not within the required interval of [-16777216, 0) and (0, 16777216].")
  84. test_invalid_input("invalid a1 parameter type as a String", 0.01, 0.02, 0.13, 1, '0.12', 0.3, TypeError,
  85. "Argument a1 with value 0.12 is not of type [<class 'float'>, <class 'int'>],"
  86. " but got <class 'str'>.")
  87. test_invalid_input("invalid a1 parameter value", 0.01, 0.02, 0.13, 1, 441324343243242342345300, 0.3, ValueError,
  88. "Input a1 is not within the required interval of [-16777216, 16777216].")
  89. test_invalid_input("invalid a2 parameter type as a String", 0.01, 0.02, 0.13, 1, 0.12, '0.3', TypeError,
  90. "Argument a2 with value 0.3 is not of type [<class 'float'>, <class 'int'>],"
  91. " but got <class 'str'>.")
  92. test_invalid_input("invalid a1 parameter value", 0.01, 0.02, 0.13, 1, 0.12, 441324343243242342345300, ValueError,
  93. "Input a2 is not within the required interval of [-16777216, 16777216].")
  94. if __name__ == '__main__':
  95. test_func_biquad_eager()
  96. test_func_biquad_pipeline()
  97. test_biquad_invalid_input()