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.

histogram_processor.py 2.7 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. """Histogram Processor APIs."""
  16. from mindinsight.utils.exceptions import ParamValueError
  17. from mindinsight.datavisual.common.log import logger
  18. from mindinsight.datavisual.common.validation import Validation
  19. from mindinsight.datavisual.common.exceptions import HistogramNotExistError
  20. from mindinsight.datavisual.processors.base_processor import BaseProcessor
  21. class HistogramProcessor(BaseProcessor):
  22. """Histogram Processor."""
  23. def get_histograms(self, train_id, tag):
  24. """
  25. Builds a JSON-serializable object with information about histogram data.
  26. Args:
  27. train_id (str): The ID of the events data.
  28. tag (str): The name of the tag the histogram data all belong to.
  29. Returns:
  30. dict, a dict including the `train_id`, `tag`, and `histograms'.
  31. {
  32. "train_id": ****,
  33. "tag": ****,
  34. "histograms": [{
  35. "wall_time": ****,
  36. "step": ****,
  37. "bucket": [[**, **, **]],
  38. },
  39. {...}
  40. ]
  41. }
  42. """
  43. Validation.check_param_empty(train_id=train_id, tag=tag)
  44. logger.info("Start to process histogram data...")
  45. try:
  46. tensors = self._data_manager.list_tensors(train_id, tag)
  47. except ParamValueError as err:
  48. raise HistogramNotExistError(err.message)
  49. histograms = []
  50. for tensor in tensors:
  51. histogram = tensor.value
  52. buckets = histogram.buckets()
  53. histograms.append({
  54. "wall_time": tensor.wall_time,
  55. "step": tensor.step,
  56. "buckets": buckets
  57. })
  58. logger.info("Histogram data processing is finished!")
  59. response = {
  60. "train_id": train_id,
  61. "tag": tag,
  62. "histograms": histograms
  63. }
  64. return response