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_phaser.py 6.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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_phaser_eager():
  29. """
  30. Feature: Phaser
  31. Description: test Phaser in eager mode
  32. Expectation: the results are as expected
  33. """
  34. # Original waveform
  35. waveform = np.array([[1, 2, 3], [4, 5, 6]], dtype=np.float32)
  36. # Expect waveform
  37. expect_waveform = np.array([[0.296, 0.71040004, 1.],
  38. [1., 1., 1.]], dtype=np.float32)
  39. sample_rate = 44100
  40. # Filtered waveform by phaser
  41. output = audio.Phaser(sample_rate=sample_rate)(waveform)
  42. count_unequal_element(expect_waveform, output, 0.0001, 0.0001)
  43. def test_phaser_pipeline():
  44. """
  45. Feature: Phaser
  46. Description: test Phaser in pipline mode
  47. Expectation: the results are as expected
  48. """
  49. # Original waveform
  50. waveform = np.array([[0.1, 1.2, 5.3], [0.4, 5.5, 1.6]], dtype=np.float32)
  51. # Expect waveform
  52. expect_waveform = np.array([[0.0296, 0.36704, 1.],
  53. [0.11840001, 1., 1.]], dtype=np.float32)
  54. sample_rate = 44100
  55. dataset = ds.NumpySlicesDataset(waveform, ["waveform"], shuffle=False)
  56. phaser_op = audio.Phaser(sample_rate)
  57. # Filtered waveform by phaser
  58. dataset = dataset.map(
  59. input_columns=["waveform"], operations=phaser_op)
  60. i = 0
  61. for item in dataset.create_dict_iterator(num_epochs=1, output_numpy=True):
  62. count_unequal_element(expect_waveform[i, :],
  63. item['waveform'], 0.0001, 0.0001)
  64. i += 1
  65. def test_phaser_invalid_input():
  66. """
  67. Feature: Phaser
  68. Description: test invalid parameter of Phaser
  69. Expectation: catch exceptions correctly
  70. """
  71. def test_invalid_input(test_name, sample_rate, gain_in, gain_out, delay_ms, decay, mod_speed, sinusoidal, error,
  72. error_msg):
  73. logger.info("Test Phaser with bad input: {0}".format(test_name))
  74. with pytest.raises(error) as error_info:
  75. audio.Phaser(sample_rate, gain_in, gain_out, delay_ms, decay, mod_speed, sinusoidal)
  76. assert error_msg in str(error_info.value)
  77. test_invalid_input("invalid sample_rate parameter type as a float", 44100.5, 0.4, 0.74, 3.0, 0.4, 0.5, True,
  78. TypeError, "Argument sample_rate with value 44100.5 is not of type [<class 'int'>],"
  79. " but got <class 'float'>.")
  80. test_invalid_input("invalid gain_in parameter type as a str", 44100, "1", 0.74, 3.0, 0.4, 0.5, True,
  81. TypeError, "Argument gain_in with value 1 is not of type [<class 'float'>, <class 'int'>],"
  82. + " but got <class 'str'>.")
  83. test_invalid_input("invalid gain_out parameter type as a str", 44100, 0.4, "10", 3.0, 0.4, 0.5, True, TypeError,
  84. "Argument gain_out with value 10 is not of type [<class 'float'>, <class 'int'>],"
  85. + " but got <class 'str'>.")
  86. test_invalid_input("invalid delay_ms parameter type as a str", 44100, 0.4, 0.74, "2", 0.4, 0.5, True, TypeError,
  87. "Argument delay_ms with value 2 is not of type [<class 'float'>, <class 'int'>],"
  88. + " but got <class 'str'>.")
  89. test_invalid_input("invalid decay parameter type as a str", 44100, 0.4, 0.74, 3.0, "0", 0.5, True, TypeError,
  90. "Argument decay with value 0 is not of type [<class 'float'>, <class 'int'>],"
  91. + " but got <class 'str'>.")
  92. test_invalid_input("invalid mod_speed parameter type as a str", 44100, 0.4, 0.74, 3.0, 0.4, "3", True, TypeError,
  93. "Argument mod_speed with value 3 is not of type [<class 'float'>, <class 'int'>],"
  94. + " but got <class 'str'>.")
  95. test_invalid_input("invalid sinusoidal parameter type as a str", 44100, 0.4, 0.74, 3.0, 0.4, 0.5, "True", TypeError,
  96. "Argument sinusoidal with value True is not of type [<class 'bool'>],"
  97. + " but got <class 'str'>.")
  98. test_invalid_input("invalid sample_rate parameter value", 441324343243242342345300, 0.5, 0.74, 3.0, 0.4, 0.5, True,
  99. ValueError, "Input sample_rate is not within the required interval of "
  100. "[-2147483648, 2147483647].")
  101. test_invalid_input("invalid gain_in out of range [0, 1]", 44100, 2.0, 0.74, 3.0, 0.4, 0.5, True, ValueError,
  102. "Input gain_in is not within the required interval of [0, 1].")
  103. test_invalid_input("invalid gain_out out of range [0, 1e9]", 44100, 0.4, -2.0, 3.0, 0.4, 0.5, True, ValueError,
  104. "Input gain_out is not within the required interval of [0, 1000000000.0].")
  105. test_invalid_input("invalid delay_ms out of range [0, 5.0]", 44100, 0.4, 0.74, 6.0, 0.4, 0.5, True, ValueError,
  106. "Input delay_ms is not within the required interval of [0, 5.0].")
  107. test_invalid_input("invalid decay out of range [0, 0.99]", 44100, 0.4, 0.74, 3.0, 1.2, 0.5, True, ValueError,
  108. "Input decay is not within the required interval of [0, 0.99].")
  109. test_invalid_input("invalid mod_speed out of range [0.1, 2]", 44100, 0.4, 0.74, 3.0, 0.4, 0.003, True, ValueError,
  110. "Input mod_speed is not within the required interval of [0.1, 2].")
  111. if __name__ == "__main__":
  112. test_phaser_eager()
  113. test_phaser_pipeline()
  114. test_phaser_invalid_input()