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.

explain_job_encap.py 3.4 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. """Explain job list encapsulator."""
  16. import copy
  17. from datetime import datetime
  18. from mindinsight.explainer.encapsulator.explain_data_encap import ExplainDataEncap
  19. from mindinsight.datavisual.common.exceptions import TrainJobNotExistError
  20. class ExplainJobEncap(ExplainDataEncap):
  21. """Explain job list encapsulator."""
  22. DATETIME_FORMAT = "%Y-%m-%d %H:%M:%S"
  23. DEFAULT_MIN_CONFIDENCE = 0.5
  24. def query_explain_jobs(self, offset, limit):
  25. """
  26. Query explain job list.
  27. Args:
  28. offset (int): Page offset.
  29. limit (int): Max. no. of items to be returned.
  30. Returns:
  31. tuple[int, list[Dict]], total no. of jobs and job list.
  32. """
  33. total, dir_infos = self.job_manager.get_job_list(offset=offset, limit=limit)
  34. job_infos = [self._dir_2_info(dir_info) for dir_info in dir_infos]
  35. return total, job_infos
  36. def query_meta(self, train_id):
  37. """
  38. Query explain job meta-data.
  39. Args:
  40. train_id (str): Job ID.
  41. Returns:
  42. dict, the metadata.
  43. """
  44. job = self.job_manager.get_job(train_id)
  45. if job is None:
  46. raise TrainJobNotExistError(train_id)
  47. return self._job_2_meta(job)
  48. @classmethod
  49. def _dir_2_info(cls, dir_info):
  50. """Convert ExplainJob object to jsonable info object."""
  51. info = dict()
  52. info["train_id"] = dir_info["relative_path"]
  53. info["create_time"] = dir_info["create_time"].strftime(cls.DATETIME_FORMAT)
  54. info["update_time"] = dir_info["update_time"].strftime(cls.DATETIME_FORMAT)
  55. return info
  56. @classmethod
  57. def _job_2_info(cls, job):
  58. """Convert ExplainJob object to jsonable info object."""
  59. info = dict()
  60. info["train_id"] = job.train_id
  61. info["create_time"] = datetime.fromtimestamp(job.create_time)\
  62. .strftime(cls.DATETIME_FORMAT)
  63. info["update_time"] = datetime.fromtimestamp(job.update_time)\
  64. .strftime(cls.DATETIME_FORMAT)
  65. return info
  66. @classmethod
  67. def _job_2_meta(cls, job):
  68. """Convert ExplainJob's meta-data to jsonable info object."""
  69. info = cls._job_2_info(job)
  70. info["sample_count"] = job.sample_count
  71. info["classes"] = copy.deepcopy(job.all_classes)
  72. saliency_info = dict()
  73. if job.min_confidence is None:
  74. saliency_info["min_confidence"] = cls.DEFAULT_MIN_CONFIDENCE
  75. else:
  76. saliency_info["min_confidence"] = job.min_confidence
  77. saliency_info["explainers"] = list(job.explainers)
  78. saliency_info["metrics"] = list(job.metrics)
  79. info["saliency"] = saliency_info
  80. info["uncertainty"] = {"enabled": job.uncertainty_enabled}
  81. return info