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_create_dct.py 4.6 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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.audio.utils as audio
  18. from mindspore import log as logger
  19. def count_unequal_element(data_expected, data_me, rtol, atol):
  20. assert data_expected.shape == data_me.shape
  21. total_count = len(data_expected.flatten())
  22. error = np.abs(data_expected - data_me)
  23. greater = np.greater(error, atol + np.abs(data_expected) * rtol)
  24. loss_count = np.count_nonzero(greater)
  25. assert (loss_count / total_count) < rtol, \
  26. "\ndata_expected_std:{0}\ndata_me_error:{1}\nloss:{2}". \
  27. format(data_expected[greater], data_me[greater], error[greater])
  28. def test_create_dct_none():
  29. """
  30. Feature: CreateDct
  31. Description: test CreateDct in eager mode
  32. Expectation: the returned result is as expected
  33. """
  34. expect = np.array([[2.00000000, 1.84775901],
  35. [2.00000000, 0.76536685],
  36. [2.00000000, -0.76536703],
  37. [2.00000000, -1.84775925]], dtype=np.float64)
  38. output = audio.CreateDct(2, 4, audio.NormMode.NONE)
  39. count_unequal_element(expect, output, 0.0001, 0.0001)
  40. def test_create_dct_ortho():
  41. """
  42. Feature: CreateDct
  43. Description: test CreateDct in eager mode
  44. Expectation: the returned result is as expected
  45. """
  46. output = audio.CreateDct(1, 3, audio.NormMode.ORTHO)
  47. expect = np.array([[0.57735026],
  48. [0.57735026],
  49. [0.57735026]], dtype=np.float64)
  50. count_unequal_element(expect, output, 0.0001, 0.0001)
  51. def test_createdct_invalid_input():
  52. """
  53. Feature: CreateDct
  54. Description: Error detection
  55. Expectation: return error
  56. """
  57. def test_invalid_input(test_name, n_mfcc, n_mels, norm, error, error_msg):
  58. logger.info("Test CreateDct with bad input: {0}".format(test_name))
  59. with pytest.raises(error) as error_info:
  60. audio.CreateDct(n_mfcc, n_mels, norm)
  61. assert error_msg in str(error_info.value)
  62. test_invalid_input("invalid n_mfcc parameter type as a float", 100.5, 200, audio.NormMode.NONE, TypeError,
  63. "n_mfcc with value 100.5 is not of type <class 'int'>, but got <class 'float'>.")
  64. test_invalid_input("invalid n_mfcc parameter type as a String", "100", 200, audio.NormMode.NONE, TypeError,
  65. "n_mfcc with value 100 is not of type <class 'int'>, but got <class 'str'>.")
  66. test_invalid_input("invalid n_mels parameter type as a String", 100, "200", audio.NormMode.NONE, TypeError,
  67. "n_mels with value 200 is not of type <class 'int'>, but got <class 'str'>.")
  68. test_invalid_input("invalid n_mels parameter type as a String", 0, 200, audio.NormMode.NONE, ValueError,
  69. "n_mfcc must be greater than 0, but got 0.")
  70. test_invalid_input("invalid n_mels parameter type as a String", 100, 0, audio.NormMode.NONE, ValueError,
  71. "n_mels must be greater than 0, but got 0.")
  72. test_invalid_input("invalid n_mels parameter type as a String", -100, 200, audio.NormMode.NONE, ValueError,
  73. "n_mfcc must be greater than 0, but got -100.")
  74. test_invalid_input("invalid n_mfcc parameter value", None, 100, audio.NormMode.NONE, TypeError,
  75. "n_mfcc with value None is not of type <class 'int'>, but got <class 'NoneType'>.")
  76. test_invalid_input("invalid n_mels parameter value", 100, None, audio.NormMode.NONE, TypeError,
  77. "n_mels with value None is not of type <class 'int'>, but got <class 'NoneType'>.")
  78. test_invalid_input("invalid n_mels parameter value", 100, 200, "None", TypeError,
  79. "norm with value None is not of type <enum 'NormMode'>, but got <class 'str'>.")
  80. if __name__ == "__main__":
  81. test_create_dct_none()
  82. test_create_dct_ortho()
  83. test_createdct_invalid_input()