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_db_to_amplitude.py 4.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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_db_to_amplitude_eager():
  30. """
  31. Feature: DBToAmplitude
  32. Description: test DBToAmplitude in eager mode
  33. Expectation: the data is processed successfully
  34. """
  35. logger.info("mindspore eager mode normal testcase:DBToAmplitude op")
  36. # Original waveform
  37. waveform = np.array([1, 2, 3, 4, 5, 6], dtype=np.float64)
  38. # Expect waveform
  39. expect_waveform = np.array([3.1698, 5.0238, 7.9621, 12.6191, 20.0000, 31.6979], dtype=np.float64)
  40. DBToAmplitude_op = audio.DBToAmplitude(2, 2)
  41. # Filtered waveform by DBToAmplitude
  42. output = DBToAmplitude_op(waveform)
  43. count_unequal_element(expect_waveform, output, 0.0001, 0.0001)
  44. def test_db_to_amplitude_pipeline():
  45. """
  46. Feature: DBToAmplitude
  47. Description: test DBToAmplitude in pipeline mode
  48. Expectation: the data is processed successfully
  49. """
  50. logger.info("mindspore pipeline mode normal testcase:DBToAmplitude op")
  51. # Original waveform
  52. waveform = np.array([[2, 2, 3], [0.1, 0.2, 0.3]], dtype=np.float64)
  53. # Expect waveform
  54. expect_waveform = np.array([[2.5119, 2.5119, 3.9811],
  55. [1.0471, 1.0965, 1.1482]], dtype=np.float64)
  56. dataset = ds.NumpySlicesDataset(waveform, ["audio"], shuffle=False)
  57. DBToAmplitude_op = audio.DBToAmplitude(1, 2)
  58. # Filtered waveform by DBToAmplitude
  59. dataset = dataset.map(input_columns=["audio"], operations=DBToAmplitude_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.0001, 0.0001)
  63. i += 1
  64. def test_db_to_amplitude_invalid_input():
  65. """
  66. Feature: DBToAmplitude
  67. Description: test param check of DBToAmplitude
  68. Expectation: throw correct error and message
  69. """
  70. logger.info("mindspore eager mode invalid input testcase:filter_wikipedia_xml op")
  71. def test_invalid_input(test_name, ref, power, error, error_msg):
  72. logger.info("Test DBToAmplitude with bad input: {0}".format(test_name))
  73. with pytest.raises(error) as error_info:
  74. audio.DBToAmplitude(ref, power)
  75. assert error_msg in str(error_info.value)
  76. test_invalid_input("invalid ref parameter type as a String", "1.0", 1.0, TypeError,
  77. "Argument ref with value 1.0 is not of type [<class 'float'>, <class 'int'>],"
  78. " but got <class 'str'>.")
  79. test_invalid_input("invalid ref parameter value", 122323242445423534543, 1.0, ValueError,
  80. "Input ref is not within the required interval of [-16777216, 16777216].")
  81. test_invalid_input("invalid power parameter type as a String", 1.0, "1.0", TypeError,
  82. "Argument power with value 1.0 is not of type [<class 'float'>, <class 'int'>],"
  83. " but got <class 'str'>.")
  84. test_invalid_input("invalid power parameter value", 1.0, 1343454254325445, ValueError,
  85. "Input power is not within the required interval of [-16777216, 16777216].")
  86. if __name__ == "__main__":
  87. test_db_to_amplitude_eager()
  88. test_db_to_amplitude_pipeline()
  89. test_db_to_amplitude_invalid_input()