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.

scalars_processor.py 3.5 kB

6 years ago
6 years ago
6 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. # Copyright 2019 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. """Scalar Processor APIs."""
  16. from urllib.parse import unquote
  17. from mindinsight.utils.exceptions import ParamValueError, UrlDecodeError
  18. from mindinsight.datavisual.utils.tools import if_nan_inf_to_none
  19. from mindinsight.datavisual.common.exceptions import ScalarNotExistError
  20. from mindinsight.datavisual.common.validation import Validation
  21. from mindinsight.datavisual.processors.base_processor import BaseProcessor
  22. class ScalarsProcessor(BaseProcessor):
  23. """Scalar Processor."""
  24. def get_metadata_list(self, train_id, tag):
  25. """
  26. Builds a JSON-serializable object with information about scalars.
  27. Args:
  28. train_id (str): The ID of the events data.
  29. tag (str): The name of the tag the scalars all belonging to.
  30. Returns:
  31. list[dict], a list of dictionaries containing the `wall_time`, `step`, `value` for each scalar.
  32. """
  33. Validation.check_param_empty(train_id=train_id, tag=tag)
  34. job_response = []
  35. try:
  36. tensors = self._data_manager.list_tensors(train_id, tag)
  37. except ParamValueError as ex:
  38. raise ScalarNotExistError(ex.message)
  39. for tensor in tensors:
  40. job_response.append({
  41. 'wall_time': tensor.wall_time,
  42. 'step': tensor.step,
  43. 'value': tensor.value})
  44. return dict(metadatas=job_response)
  45. def get_scalars(self, train_ids, tags):
  46. """
  47. Get scalar data for given train_ids and tags.
  48. Args:
  49. train_ids (list): Specify list of train job ID.
  50. tags (list): Specify list of tags.
  51. Returns:
  52. list[dict], a list of dictionaries containing the `wall_time`, `step`, `value` for each scalar.
  53. """
  54. for index, train_id in enumerate(train_ids):
  55. try:
  56. train_id = unquote(train_id, errors='strict')
  57. except UnicodeDecodeError:
  58. raise UrlDecodeError('Unquote train id error with strict mode')
  59. else:
  60. train_ids[index] = train_id
  61. scalars = []
  62. for train_id in train_ids:
  63. for tag in tags:
  64. try:
  65. tensors = self._data_manager.list_tensors(train_id, tag)
  66. except ParamValueError:
  67. continue
  68. scalar = {
  69. 'train_id': train_id,
  70. 'tag': tag,
  71. 'values': [],
  72. }
  73. for tensor in tensors:
  74. scalar['values'].append({
  75. 'wall_time': tensor.wall_time,
  76. 'step': tensor.step,
  77. 'value': if_nan_inf_to_none('scalar_value', tensor.value),
  78. })
  79. scalars.append(scalar)
  80. return scalars