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_gain.py 3.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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_gain_eager():
  30. """
  31. Feature: Gain
  32. Description: test Gain in eager mode
  33. Expectation: the data is processed successfully
  34. """
  35. logger.info("test Gain in eager mode")
  36. # Original waveform
  37. waveform = np.array([1, 2, 3, 4, 5, 6], dtype=np.float64)
  38. # Expect waveform
  39. expect_waveform = np.array([1.1220184, 2.2440369, 3.3660554, 4.4880738, 5.6100923, 6.7321107], dtype=np.float64)
  40. gain_op = audio.Gain()
  41. # Filtered waveform by Gain
  42. output = gain_op(waveform)
  43. count_unequal_element(expect_waveform, output, 0.00001, 0.00001)
  44. def test_gain_pipeline():
  45. """
  46. Feature: Gain
  47. Description: test Gain in pipeline mode
  48. Expectation: the data is processed successfully
  49. """
  50. logger.info("test Gain in pipeline mode")
  51. # Original waveform
  52. waveform = np.array([[1, 2, 3], [0.1, 0.2, 0.3]], dtype=np.float64)
  53. # Expect waveform
  54. expect_waveform = np.array([[1.05925, 2.1185, 3.1778],
  55. [0.10592537, 0.21185075, 0.31777612]], dtype=np.float64)
  56. dataset = ds.NumpySlicesDataset(waveform, ["audio"], shuffle=False)
  57. gain_op = audio.Gain(0.5)
  58. # Filtered waveform by Gain
  59. dataset = dataset.map(input_columns=["audio"], operations=gain_op, num_parallel_workers=8)
  60. i = 0
  61. for item in dataset.create_dict_iterator(output_numpy=True):
  62. count_unequal_element(expect_waveform[i, :], item['audio'], 0.00001, 0.00001)
  63. i += 1
  64. def test_gain_invalid_input():
  65. """
  66. Feature: Gain
  67. Description: test param check of Gain
  68. Expectation: throw correct error and message
  69. """
  70. logger.info("test param check of Gain")
  71. def test_invalid_input(test_name, gain_db, error, error_msg):
  72. logger.info("Test Gain with bad input: {0}".format(test_name))
  73. with pytest.raises(error) as error_info:
  74. audio.Gain(gain_db)
  75. assert error_msg in str(error_info.value)
  76. test_invalid_input("invalid gain_db parameter type as a String", "1.0", TypeError,
  77. "Argument gain_db with value 1.0 is not of type [<class 'float'>, <class 'int'>],"
  78. " but got <class 'str'>.")
  79. test_invalid_input("invalid gain_db parameter value", 122323242445423534543, ValueError,
  80. "Input gain_db is not within the required interval of [-16777216, 16777216].")
  81. if __name__ == "__main__":
  82. test_gain_eager()
  83. test_gain_pipeline()
  84. test_gain_invalid_input()