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.

data_loader.py 2.5 kB

5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. """
  16. DataLoader is an adapter for all other loaders.
  17. This module can identify what loader should be used to load data.
  18. """
  19. from mindinsight.datavisual.common.log import logger
  20. from mindinsight.datavisual.data_transform.ms_data_loader import MSDataLoader
  21. from mindinsight.datavisual.common import exceptions
  22. class DataLoader:
  23. """
  24. The adapter of all kinds of loaders.
  25. Args:
  26. summary_dir (str): A directory path.
  27. """
  28. def __init__(self, summary_dir):
  29. self._summary_dir = summary_dir
  30. self._loader = None
  31. def load(self, executor=None):
  32. """Load the data when loader is exist.
  33. Args:
  34. executor (Optional[Executor]): The executor instance.
  35. Returns:
  36. bool, True if the loader is finished loading.
  37. """
  38. if self._loader is None:
  39. ms_dataloader = MSDataLoader(self._summary_dir)
  40. loaders = [ms_dataloader]
  41. for loader in loaders:
  42. if loader.filter_valid_files():
  43. self._loader = loader
  44. break
  45. if self._loader is None:
  46. logger.warning("No valid files can be loaded, summary_dir: %s.", self._summary_dir)
  47. raise exceptions.SummaryLogPathInvalid()
  48. return self._loader.load(executor)
  49. def get_events_data(self):
  50. """
  51. Get events data from log file.
  52. Returns:
  53. EventsData, indiciates events data.
  54. """
  55. return self._loader.get_events_data()
  56. def has_valid_files(self):
  57. """
  58. Check the directory for valid files.
  59. Returns:
  60. bool, if the directory has valid files, return True.
  61. """
  62. ms_dataloader = MSDataLoader(self._summary_dir)
  63. return bool(ms_dataloader.filter_valid_files())