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_overdrive.py 4.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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, "\ndata_expected_std:{0}\ndata_me_error:{1}\nloss:{2}".format(
  27. data_expected[greater], data_me[greater], error[greater])
  28. def test_overdrive_eager():
  29. """
  30. Feature: Overdrive
  31. Description: test Overdrive in eager mode
  32. Expectation: the results are as expected
  33. """
  34. # Original waveform
  35. waveform = np.array([[1.47, 4.722, 5.863], [0.492, 0.235, 0.56]], dtype=np.float32)
  36. # Expect waveform
  37. expect_waveform = np.array([[1., 1., 1.],
  38. [0.74600005, 0.615, 0.77501255]], dtype=np.float32)
  39. overdrive_op = audio.Overdrive()
  40. # Filtered waveform by overdrive
  41. output = overdrive_op(waveform)
  42. count_unequal_element(expect_waveform, output, 0.0001, 0.0001)
  43. def test_overdrive_pipeline():
  44. """
  45. Feature: Overdrive
  46. Description: test Overdrive in pipeline mode
  47. Expectation: the results are as expected
  48. """
  49. # Original waveform
  50. waveform = np.array([[0.1, 0.2], [0.4, 2.6]], dtype=np.float32)
  51. # Expect waveform
  52. expect_waveform = np.array([[0.29598799, 0.52081579],
  53. [0.7, 1.]], dtype=np.float32)
  54. dataset = ds.NumpySlicesDataset(waveform, ["waveform"], shuffle=False)
  55. overdrive_op = audio.Overdrive(10.0, 5.0)
  56. # Filtered waveform by overdrive
  57. dataset = dataset.map(
  58. input_columns=["waveform"], operations=overdrive_op)
  59. i = 0
  60. for item in dataset.create_dict_iterator(num_epochs=1, output_numpy=True):
  61. count_unequal_element(expect_waveform[i, :],
  62. item['waveform'], 0.0001, 0.0001)
  63. i += 1
  64. def test_overdrive_invalid_input():
  65. """
  66. Feature: Overdrive
  67. Description: test invalid parameter of Overdrive
  68. Expectation: catch exceptions correctly
  69. """
  70. def test_invalid_input(test_name, gain, color, error, error_msg):
  71. logger.info("Test Overdrive with bad input: {0}".format(test_name))
  72. with pytest.raises(error) as error_info:
  73. audio.Overdrive(gain, color)
  74. assert error_msg in str(error_info.value)
  75. test_invalid_input("invalid gain parameter type as a str", "20", 20, TypeError,
  76. "Argument gain with value 20 is not of type [<class 'float'>, <class 'int'>],"
  77. + " but got <class 'str'>.")
  78. test_invalid_input("invalid color parameter type as a str", 10, "5", TypeError,
  79. "Argument color with value 5 is not of type [<class 'float'>, <class 'int'>],"
  80. + " but got <class 'str'>.")
  81. test_invalid_input("invalid gain out of range [0, 100]", 100.23, 5.0, ValueError,
  82. "Input gain is not within the required interval of [0, 100].")
  83. test_invalid_input("invalid color out of range [0, 100]", 30, -0.333, ValueError,
  84. "Input color is not within the required interval of [0, 100].")
  85. if __name__ == "__main__":
  86. test_overdrive_eager()
  87. test_overdrive_pipeline()
  88. test_overdrive_invalid_input()