_metric.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #
  2. # Copyright 2019 The FATE Authors. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. import typing
  17. from enum import Enum
  18. from fate_flow.entity import BaseEntity
  19. class MetricType(Enum):
  20. LOSS = 'LOSS'
  21. DOWNLOAD = 'DOWNLOAD'
  22. CACHE_INFO = 'cache_info'
  23. CHECKPOINT_INFO = 'checkpoint_info'
  24. UPLOAD = 'UPLOAD'
  25. COMPONENT_MODEL_INFO = 'component_model_info'
  26. class Metric(BaseEntity):
  27. def __init__(self, key, value: float, timestamp: float = None):
  28. self.key = key
  29. self.value = value
  30. self.timestamp = timestamp
  31. @classmethod
  32. def from_dict(cls, d: dict):
  33. return Metric(d.get("key"), d.get("value"), d.get("timestamp"))
  34. class MetricMeta(BaseEntity):
  35. def __init__(self, name: str, metric_type: typing.Union[MetricType, str], extra_metas: dict = None):
  36. self.name = name
  37. self.metric_type = metric_type
  38. self.metas = {}
  39. if extra_metas:
  40. self.metas.update(extra_metas)
  41. self.metas['name'] = name
  42. self.metas['metric_type'] = metric_type
  43. def update_metas(self, metas: dict):
  44. self.metas.update(metas)
  45. def to_dict(self):
  46. return self.metas
  47. @classmethod
  48. def from_dict(cls, d: dict):
  49. metas = d.get("metas", {})
  50. if d.get("extra_metas"):
  51. metas.update(d["extra_metas"])
  52. return MetricMeta(d.get("name"), d.get("metric_type"), extra_metas=metas)