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_to_number_op.py 8.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. # Copyright 2020 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.common.dtype as mstype
  18. import mindspore.dataset as ds
  19. import mindspore.dataset.text as text
  20. np_integral_types = [np.int8, np.int16, np.int32, np.int64, np.uint8, np.uint16,
  21. np.uint32, np.uint64]
  22. ms_integral_types = [mstype.int8, mstype.int16, mstype.int32, mstype.int64, mstype.uint8,
  23. mstype.uint16, mstype.uint32, mstype.uint64]
  24. np_non_integral_types = [np.float16, np.float32, np.float64]
  25. ms_non_integral_types = [mstype.float16, mstype.float32, mstype.float64]
  26. def string_dataset_generator(strings):
  27. for string in strings:
  28. yield (np.array(string, dtype='S'),)
  29. def test_to_number_typical_case_integral():
  30. input_strings = [["-121", "14"], ["-2219", "7623"], ["-8162536", "162371864"],
  31. ["-1726483716", "98921728421"]]
  32. for ms_type, inputs in zip(ms_integral_types, input_strings):
  33. dataset = ds.GeneratorDataset(string_dataset_generator(inputs), "strings")
  34. dataset = dataset.map(input_columns=["strings"], operations=text.ToNumber(ms_type))
  35. expected_output = [int(string) for string in inputs]
  36. output = []
  37. for data in dataset.create_dict_iterator():
  38. output.append(data["strings"])
  39. assert output == expected_output
  40. def test_to_number_typical_case_non_integral():
  41. input_strings = [["-1.1", "1.4"], ["-2219.321", "7623.453"], ["-816256.234282", "162371864.243243"]]
  42. epsilons = [0.001, 0.001, 0.0001, 0.0001, 0.0000001, 0.0000001]
  43. for ms_type, inputs in zip(ms_non_integral_types, input_strings):
  44. dataset = ds.GeneratorDataset(string_dataset_generator(inputs), "strings")
  45. dataset = dataset.map(input_columns=["strings"], operations=text.ToNumber(ms_type))
  46. expected_output = [float(string) for string in inputs]
  47. output = []
  48. for data in dataset.create_dict_iterator():
  49. output.append(data["strings"])
  50. for expected, actual, epsilon in zip(expected_output, output, epsilons):
  51. assert abs(expected - actual) < epsilon
  52. def out_of_bounds_error_message_check(dataset, np_type, value_to_cast):
  53. type_info = np.iinfo(np_type)
  54. type_max = str(type_info.max)
  55. type_min = str(type_info.min)
  56. type_name = str(np.dtype(np_type))
  57. with pytest.raises(RuntimeError) as info:
  58. for _ in dataset.create_dict_iterator():
  59. pass
  60. assert "String input " + value_to_cast + " will be out of bounds if casted to " + type_name in str(info.value)
  61. assert "valid range is: [" + type_min + ", " + type_max + "]" in str(info.value)
  62. def test_to_number_out_of_bounds_integral():
  63. for np_type, ms_type in zip(np_integral_types, ms_integral_types):
  64. type_info = np.iinfo(np_type)
  65. input_strings = [str(type_info.max + 10)]
  66. dataset = ds.GeneratorDataset(string_dataset_generator(input_strings), "strings")
  67. dataset = dataset.map(input_columns=["strings"], operations=text.ToNumber(ms_type))
  68. out_of_bounds_error_message_check(dataset, np_type, input_strings[0])
  69. input_strings = [str(type_info.min - 10)]
  70. dataset = ds.GeneratorDataset(string_dataset_generator(input_strings), "strings")
  71. dataset = dataset.map(input_columns=["strings"], operations=text.ToNumber(ms_type))
  72. out_of_bounds_error_message_check(dataset, np_type, input_strings[0])
  73. def test_to_number_out_of_bounds_non_integral():
  74. above_range = [str(np.finfo(np.float16).max * 10), str(np.finfo(np.float32).max * 10), "1.8e+308"]
  75. input_strings = [above_range[0]]
  76. dataset = ds.GeneratorDataset(string_dataset_generator(input_strings), "strings")
  77. dataset = dataset.map(input_columns=["strings"], operations=text.ToNumber(ms_non_integral_types[0]))
  78. with pytest.raises(RuntimeError) as info:
  79. for _ in dataset.create_dict_iterator():
  80. pass
  81. assert "outside of valid float16 range" in str(info.value)
  82. input_strings = [above_range[1]]
  83. dataset = ds.GeneratorDataset(string_dataset_generator(input_strings), "strings")
  84. dataset = dataset.map(input_columns=["strings"], operations=text.ToNumber(ms_non_integral_types[1]))
  85. with pytest.raises(RuntimeError) as info:
  86. for _ in dataset.create_dict_iterator():
  87. pass
  88. assert "String input " + input_strings[0] + " will be out of bounds if casted to float32" in str(info.value)
  89. input_strings = [above_range[2]]
  90. dataset = ds.GeneratorDataset(string_dataset_generator(input_strings), "strings")
  91. dataset = dataset.map(input_columns=["strings"], operations=text.ToNumber(ms_non_integral_types[2]))
  92. with pytest.raises(RuntimeError) as info:
  93. for _ in dataset.create_dict_iterator():
  94. pass
  95. assert "String input " + input_strings[0] + " will be out of bounds if casted to float64" in str(info.value)
  96. below_range = [str(np.finfo(np.float16).min * 10), str(np.finfo(np.float32).min * 10), "-1.8e+308"]
  97. input_strings = [below_range[0]]
  98. dataset = ds.GeneratorDataset(string_dataset_generator(input_strings), "strings")
  99. dataset = dataset.map(input_columns=["strings"], operations=text.ToNumber(ms_non_integral_types[0]))
  100. with pytest.raises(RuntimeError) as info:
  101. for _ in dataset.create_dict_iterator():
  102. pass
  103. assert "outside of valid float16 range" in str(info.value)
  104. input_strings = [below_range[1]]
  105. dataset = ds.GeneratorDataset(string_dataset_generator(input_strings), "strings")
  106. dataset = dataset.map(input_columns=["strings"], operations=text.ToNumber(ms_non_integral_types[1]))
  107. with pytest.raises(RuntimeError) as info:
  108. for _ in dataset.create_dict_iterator():
  109. pass
  110. assert "String input " + input_strings[0] + " will be out of bounds if casted to float32" in str(info.value)
  111. input_strings = [below_range[2]]
  112. dataset = ds.GeneratorDataset(string_dataset_generator(input_strings), "strings")
  113. dataset = dataset.map(input_columns=["strings"], operations=text.ToNumber(ms_non_integral_types[2]))
  114. with pytest.raises(RuntimeError) as info:
  115. for _ in dataset.create_dict_iterator():
  116. pass
  117. assert "String input " + input_strings[0] + " will be out of bounds if casted to float64" in str(info.value)
  118. def test_to_number_boundaries_integral():
  119. for np_type, ms_type in zip(np_integral_types, ms_integral_types):
  120. type_info = np.iinfo(np_type)
  121. input_strings = [str(type_info.max)]
  122. dataset = ds.GeneratorDataset(string_dataset_generator(input_strings), "strings")
  123. dataset = dataset.map(input_columns=["strings"], operations=text.ToNumber(ms_type))
  124. for data in dataset.create_dict_iterator():
  125. assert data["strings"] == int(input_strings[0])
  126. input_strings = [str(type_info.min)]
  127. dataset = ds.GeneratorDataset(string_dataset_generator(input_strings), "strings")
  128. dataset = dataset.map(input_columns=["strings"], operations=text.ToNumber(ms_type))
  129. for data in dataset.create_dict_iterator():
  130. assert data["strings"] == int(input_strings[0])
  131. input_strings = [str(0)]
  132. dataset = ds.GeneratorDataset(string_dataset_generator(input_strings), "strings")
  133. dataset = dataset.map(input_columns=["strings"], operations=text.ToNumber(ms_type))
  134. for data in dataset.create_dict_iterator():
  135. assert data["strings"] == int(input_strings[0])
  136. def test_to_number_invalid_input():
  137. input_strings = ["a8fa9ds8fa"]
  138. dataset = ds.GeneratorDataset(string_dataset_generator(input_strings), "strings")
  139. dataset = dataset.map(input_columns=["strings"], operations=text.ToNumber(mstype.int32))
  140. with pytest.raises(RuntimeError) as info:
  141. for _ in dataset.create_dict_iterator():
  142. pass
  143. assert "It is invalid to convert " + input_strings[0] + " to a number" in str(info.value)
  144. if __name__ == '__main__':
  145. test_to_number_typical_case_integral()
  146. test_to_number_typical_case_non_integral()
  147. test_to_number_boundaries_integral()
  148. test_to_number_out_of_bounds_integral()
  149. test_to_number_out_of_bounds_non_integral()
  150. test_to_number_invalid_input()