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.

utils.py 2.1 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. """Utils."""
  16. import math
  17. from mindinsight.datavisual.common.log import logger
  18. def calc_histogram_bins(count):
  19. """
  20. Calculates experience-based optimal bins number for histogram.
  21. To suppress re-sample bias, there should be enough number in each bin. So we calc bin numbers according to
  22. count. For very small count(1 - 10), we assign carefully chosen number. For large count, we tried to make
  23. sure there are 9-10 numbers in each bucket on average. Too many bins will also distract users, so we set max
  24. number of bins to 30.
  25. Args:
  26. count (int): Valid number count for the tensor.
  27. Returns:
  28. int, number of histogram bins.
  29. """
  30. number_per_bucket = 10
  31. max_bins = 30
  32. if not count:
  33. return 1
  34. if count <= 5:
  35. return 2
  36. if count <= 10:
  37. return 3
  38. if count <= 280:
  39. # note that math.ceil(281/10) + 1 equals 30
  40. return math.ceil(count / number_per_bucket) + 1
  41. return max_bins
  42. def contains_null_byte(**kwargs):
  43. """
  44. Check if arg contains null byte.
  45. Args:
  46. kwargs (Any): Check if arg contains null byte.
  47. Returns:
  48. bool, indicates if any arg contains null byte.
  49. """
  50. for key, value in kwargs.items():
  51. if not isinstance(value, str):
  52. continue
  53. if '\x00' in value:
  54. logger.warning('%s contains null byte \\x00.', key)
  55. return True
  56. return False