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_magphase.py 4.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. """
  16. Testing Magphase Python API
  17. """
  18. import numpy as np
  19. import mindspore.dataset as ds
  20. import mindspore.dataset.audio.transforms as audio
  21. from mindspore import log as logger
  22. def test_magphase_pipeline():
  23. """
  24. Test magphase (pipeline).
  25. """
  26. logger.info("Test Magphase pipeline.")
  27. data1 = [[[3.0, -4.0], [-5.0, 12.0]]]
  28. expected = [5, 13, -0.927295, 1.965587]
  29. dataset = ds.NumpySlicesDataset(data1, column_names=["col1"], shuffle=False)
  30. magphase_window = audio.Magphase(power=1.0)
  31. dataset = dataset.map(operations=magphase_window, input_columns=["col1"],
  32. output_columns=["mag", "phase"], column_order=["mag", "phase"])
  33. for data1, data2 in dataset.create_tuple_iterator(num_epochs=1, output_numpy=True):
  34. assert abs(data1[0] - expected[0]) < 0.00001
  35. assert abs(data1[1] - expected[1]) < 0.00001
  36. assert abs(data2[0] - expected[2]) < 0.00001
  37. assert abs(data2[1] - expected[3]) < 0.00001
  38. logger.info("Finish testing Magphase.")
  39. def test_magphase_eager():
  40. """
  41. Test magphase (eager).
  42. """
  43. logger.info("Test Magphase eager.")
  44. input_number = np.array([41, 67, 34, 0, 69, 24, 78, 58]).reshape((2, 2, 2)).astype("double")
  45. mag = np.array([78.54934755, 34., 73.05477397, 97.20082304]).reshape((2, 2)).astype("double")
  46. phase = np.array([1.02164342, 0, 0.33473684, 0.63938591]).reshape((2, 2)).astype("double")
  47. magphase_window = audio.Magphase()
  48. data1, data2 = magphase_window(input_number)
  49. assert (abs(data1 - mag) < 0.00001).all()
  50. assert (abs(data2 - phase) < 0.00001).all()
  51. logger.info("Finish testing Magphase.")
  52. def test_magphase_exception():
  53. """
  54. Test magphase not callable.
  55. """
  56. logger.info("Test Magphase not callable.")
  57. try:
  58. input_number = np.array([1, 2, 3, 4]).reshape(4,).astype("double")
  59. magphase_window = audio.Magphase(power=2.0)
  60. _ = magphase_window(input_number)
  61. except RuntimeError as error:
  62. logger.info("Got an exception in Magphase: {}".format(str(error)))
  63. assert "the shape of input tensor does not match the requirement of operator" in str(error)
  64. try:
  65. input_number = np.array([1, 2, 3, 4]).reshape(1, 4).astype("double")
  66. magphase_window = audio.Magphase(power=2.0)
  67. _ = magphase_window(input_number)
  68. except RuntimeError as error:
  69. logger.info("Got an exception in Magphase: {}".format(str(error)))
  70. assert "the shape of input tensor does not match the requirement of operator" in str(error)
  71. try:
  72. input_number = np.array(['test', 'test']).reshape(1, 2)
  73. magphase_window = audio.Magphase(power=2.0)
  74. _ = magphase_window(input_number)
  75. except RuntimeError as error:
  76. logger.info("Got an exception in Magphase: {}".format(str(error)))
  77. assert "the data type of input tensor does not match the requirement of operator" in str(error)
  78. try:
  79. input_number = np.array([1, 2, 3, 4]).reshape(2, 2).astype("double")
  80. magphase_window = audio.Magphase(power=-1.0)
  81. _ = magphase_window(input_number)
  82. except ValueError as error:
  83. logger.info("Got an exception in Magphase: {}".format(str(error)))
  84. assert "Input power is not within the required interval of [0, 16777216]." in str(error)
  85. logger.info("Finish testing Magphase.")
  86. if __name__ == "__main__":
  87. test_magphase_pipeline()
  88. test_magphase_eager()
  89. test_magphase_exception()