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.

tools.py 3.9 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. """
  16. Description: This file is used for some common util.
  17. """
  18. import io
  19. import os
  20. import shutil
  21. import time
  22. import json
  23. from urllib.parse import urlencode
  24. import numpy as np
  25. from PIL import Image
  26. from mindinsight.datavisual.common.enums import DataManagerStatus
  27. def get_url(url, params):
  28. """
  29. Concatenate the URL and params.
  30. Args:
  31. url (str): A link requested. For example, http://example.com.
  32. params (dict): A dict consists of params. For example, {'offset': 1, 'limit':'100}.
  33. Returns:
  34. str, like http://example.com?offset=1&limit=100
  35. """
  36. return url + '?' + urlencode(params)
  37. def delete_files_or_dirs(path_list):
  38. """Delete files or dirs in path_list."""
  39. for path in path_list:
  40. if os.path.isdir(path):
  41. shutil.rmtree(path)
  42. else:
  43. os.remove(path)
  44. def check_loading_done(data_manager, time_limit=15, first_sleep_time=0):
  45. """If loading data for more than `time_limit` seconds, exit."""
  46. if first_sleep_time > 0:
  47. time.sleep(first_sleep_time)
  48. start_time = time.time()
  49. while data_manager.status not in (DataManagerStatus.DONE.value, DataManagerStatus.INVALID.value):
  50. time_used = time.time() - start_time
  51. if time_used > time_limit:
  52. break
  53. time.sleep(0.1)
  54. continue
  55. return bool(data_manager.status == DataManagerStatus.DONE.value)
  56. def get_image_tensor_from_bytes(image_string):
  57. """Get image tensor from bytes."""
  58. img = Image.open(io.BytesIO(image_string))
  59. image_tensor = np.array(img)
  60. return image_tensor
  61. def compare_result_with_file(result, expected_file_path):
  62. """Compare result with file which contain the expected results."""
  63. with open(expected_file_path, 'r') as file:
  64. expected_results = json.load(file)
  65. assert result == expected_results
  66. def deal_float_for_dict(res: dict, expected_res: dict):
  67. """
  68. Deal float rounded to five decimals in dict.
  69. For example:
  70. res:{
  71. "model_lineages": {
  72. "metric": {"acc": 0.1234561}
  73. }
  74. }
  75. expected_res:
  76. {
  77. "model_lineages": {
  78. "metric": {"acc": 0.1234562}
  79. }
  80. }
  81. After:
  82. res:{
  83. "model_lineages": {
  84. "metric": {"acc": 0.12346}
  85. }
  86. }
  87. expected_res:
  88. {
  89. "model_lineages": {
  90. "metric": {"acc": 0.12346}
  91. }
  92. }
  93. Args:
  94. res (dict): e.g.
  95. {
  96. "model_lineages": {
  97. "metric": {"acc": 0.1234561}
  98. }
  99. }
  100. expected_res (dict):
  101. {
  102. "model_lineages": {
  103. "metric": {"acc": 0.1234562}
  104. }
  105. }
  106. """
  107. decimal_num = 5
  108. for key in res:
  109. value = res[key]
  110. expected_value = expected_res[key]
  111. if isinstance(value, dict):
  112. deal_float_for_dict(value, expected_value)
  113. elif isinstance(value, float):
  114. res[key] = round(value, decimal_num)
  115. expected_res[key] = round(expected_value, decimal_num)